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" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 21 | #include "clang/Basic/CodeGenOptions.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 22 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 23 | #include "clang/CodeGen/SwiftCallingConv.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Triple.h" |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DataLayout.h" |
| 29 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 31 | #include <algorithm> // std::sort |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 32 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | using namespace CodeGen; |
| 35 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 36 | // Helper for coercing an aggregate argument or return value into an integer |
| 37 | // array of the same size (including padding) and alignment. This alternate |
| 38 | // coercion happens only for the RenderScript ABI and can be removed after |
| 39 | // runtimes that rely on it are no longer supported. |
| 40 | // |
| 41 | // RenderScript assumes that the size of the argument / return value in the IR |
| 42 | // is the same as the size of the corresponding qualified type. This helper |
| 43 | // coerces the aggregate type into an array of the same size (including |
| 44 | // padding). This coercion is used in lieu of expansion of struct members or |
| 45 | // other canonical coercions that return a coerced-type of larger size. |
| 46 | // |
| 47 | // Ty - The argument / return value type |
| 48 | // Context - The associated ASTContext |
| 49 | // LLVMContext - The associated LLVMContext |
| 50 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 51 | ASTContext &Context, |
| 52 | llvm::LLVMContext &LLVMContext) { |
| 53 | // Alignment and Size are measured in bits. |
| 54 | const uint64_t Size = Context.getTypeSize(Ty); |
| 55 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 56 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 57 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 58 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 59 | } |
| 60 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 61 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 62 | llvm::Value *Array, |
| 63 | llvm::Value *Value, |
| 64 | unsigned FirstIndex, |
| 65 | unsigned LastIndex) { |
| 66 | // Alternatively, we could emit this as a loop in the source. |
| 67 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 68 | llvm::Value *Cell = |
| 69 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 70 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 74 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 75 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 76 | T->isMemberFunctionPointerType(); |
| 77 | } |
| 78 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 79 | ABIArgInfo |
| 80 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 81 | llvm::Type *Padding) const { |
| 82 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 83 | ByRef, Realign, Padding); |
| 84 | } |
| 85 | |
| 86 | ABIArgInfo |
| 87 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 88 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 89 | /*ByRef*/ false, Realign); |
| 90 | } |
| 91 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 92 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 93 | QualType Ty) const { |
| 94 | return Address::invalid(); |
| 95 | } |
| 96 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 97 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 98 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 99 | /// Does the given lowering require more than the given number of |
| 100 | /// registers when expanded? |
| 101 | /// |
| 102 | /// This is intended to be the basis of a reasonable basic implementation |
| 103 | /// of should{Pass,Return}IndirectlyForSwift. |
| 104 | /// |
| 105 | /// For most targets, a limit of four total registers is reasonable; this |
| 106 | /// limits the amount of code required in order to move around the value |
| 107 | /// in case it wasn't produced immediately prior to the call by the caller |
| 108 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 109 | /// immediately within the callee. But some targets may need to further |
| 110 | /// limit the register count due to an inability to support that many |
| 111 | /// return registers. |
| 112 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 113 | ArrayRef<llvm::Type*> scalarTypes, |
| 114 | unsigned maxAllRegisters) { |
| 115 | unsigned intCount = 0, fpCount = 0; |
| 116 | for (llvm::Type *type : scalarTypes) { |
| 117 | if (type->isPointerTy()) { |
| 118 | intCount++; |
| 119 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 120 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 121 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 122 | } else { |
| 123 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 124 | fpCount++; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return (intCount + fpCount > maxAllRegisters); |
| 129 | } |
| 130 | |
| 131 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 132 | llvm::Type *eltTy, |
| 133 | unsigned numElts) const { |
| 134 | // The default implementation of this assumes that the target guarantees |
| 135 | // 128-bit SIMD support but nothing more. |
| 136 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 137 | } |
| 138 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 139 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 140 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 141 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 142 | if (!RD) { |
| 143 | if (!RT->getDecl()->canPassInRegisters()) |
| 144 | return CGCXXABI::RAA_Indirect; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 145 | return CGCXXABI::RAA_Default; |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 146 | } |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 147 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 151 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 152 | const RecordType *RT = T->getAs<RecordType>(); |
| 153 | if (!RT) |
| 154 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 155 | return getRecordArgABI(RT, CXXABI); |
| 156 | } |
| 157 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 158 | static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, |
| 159 | const ABIInfo &Info) { |
| 160 | QualType Ty = FI.getReturnType(); |
| 161 | |
| 162 | if (const auto *RT = Ty->getAs<RecordType>()) |
| 163 | if (!isa<CXXRecordDecl>(RT->getDecl()) && |
| 164 | !RT->getDecl()->canPassInRegisters()) { |
| 165 | FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | return CXXABI.classifyReturnType(FI); |
| 170 | } |
| 171 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 172 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 173 | /// should ensure that all elements of the union have the same "machine type". |
| 174 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 175 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 176 | const RecordDecl *UD = UT->getDecl(); |
| 177 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 178 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 179 | return UD->field_begin()->getType(); |
| 180 | } |
| 181 | } |
| 182 | return Ty; |
| 183 | } |
| 184 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 185 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 186 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 189 | ASTContext &ABIInfo::getContext() const { |
| 190 | return CGT.getContext(); |
| 191 | } |
| 192 | |
| 193 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 194 | return CGT.getLLVMContext(); |
| 195 | } |
| 196 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 197 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 198 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 199 | } |
| 200 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 201 | const TargetInfo &ABIInfo::getTarget() const { |
| 202 | return CGT.getTarget(); |
| 203 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 204 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 205 | const CodeGenOptions &ABIInfo::getCodeGenOpts() const { |
| 206 | return CGT.getCodeGenOpts(); |
| 207 | } |
| 208 | |
| 209 | bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); } |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 210 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 211 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 216 | uint64_t Members) const { |
| 217 | return false; |
| 218 | } |
| 219 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 220 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 221 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 222 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 223 | switch (TheKind) { |
| 224 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 225 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 226 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 227 | Ty->print(OS); |
| 228 | else |
| 229 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 230 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 231 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 232 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 233 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 234 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 235 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 236 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 237 | case InAlloca: |
| 238 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 239 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 240 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 241 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 242 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 243 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 244 | break; |
| 245 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 246 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 247 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 248 | case CoerceAndExpand: |
| 249 | OS << "CoerceAndExpand Type="; |
| 250 | getCoerceAndExpandType()->print(OS); |
| 251 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 252 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 253 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 256 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 257 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 258 | llvm::Value *Ptr, |
| 259 | CharUnits Align) { |
| 260 | llvm::Value *PtrAsInt = Ptr; |
| 261 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 262 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 263 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 264 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 265 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 266 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 267 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 268 | Ptr->getType(), |
| 269 | Ptr->getName() + ".aligned"); |
| 270 | return PtrAsInt; |
| 271 | } |
| 272 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 273 | /// Emit va_arg for a platform using the common void* representation, |
| 274 | /// where arguments are simply emitted in an array of slots on the stack. |
| 275 | /// |
| 276 | /// This version implements the core direct-value passing rules. |
| 277 | /// |
| 278 | /// \param SlotSize - The size and alignment of a stack slot. |
| 279 | /// Each argument will be allocated to a multiple of this number of |
| 280 | /// slots, and all the slots will be aligned to this value. |
| 281 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 282 | /// an argument type with an alignment greater than the slot size |
| 283 | /// will be emitted on a higher-alignment address, potentially |
| 284 | /// leaving one or more empty slots behind as padding. If this |
| 285 | /// is false, the returned address might be less-aligned than |
| 286 | /// DirectAlign. |
| 287 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 288 | Address VAListAddr, |
| 289 | llvm::Type *DirectTy, |
| 290 | CharUnits DirectSize, |
| 291 | CharUnits DirectAlign, |
| 292 | CharUnits SlotSize, |
| 293 | bool AllowHigherAlign) { |
| 294 | // Cast the element type to i8* if necessary. Some platforms define |
| 295 | // va_list as a struct containing an i8* instead of just an i8*. |
| 296 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 297 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 298 | |
| 299 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 300 | |
| 301 | // If the CC aligns values higher than the slot size, do so if needed. |
| 302 | Address Addr = Address::invalid(); |
| 303 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 304 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 305 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 306 | } else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 307 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 311 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 312 | Address NextPtr = |
| 313 | CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next"); |
| 314 | CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 315 | |
| 316 | // If the argument is smaller than a slot, and this is a big-endian |
| 317 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 318 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 319 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 320 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 321 | } |
| 322 | |
| 323 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 324 | return Addr; |
| 325 | } |
| 326 | |
| 327 | /// Emit va_arg for a platform using the common void* representation, |
| 328 | /// where arguments are simply emitted in an array of slots on the stack. |
| 329 | /// |
| 330 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 331 | /// \param ValueInfo - The size and alignment of this type, generally |
| 332 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 333 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 334 | /// Each argument will be allocated to a multiple of this number of |
| 335 | /// slots, and all the slots will be aligned to this value. |
| 336 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 337 | /// an argument type with an alignment greater than the slot size |
| 338 | /// will be emitted on a higher-alignment address, potentially |
| 339 | /// leaving one or more empty slots behind as padding. |
| 340 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 341 | QualType ValueTy, bool IsIndirect, |
| 342 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 343 | CharUnits SlotSizeAndAlign, |
| 344 | bool AllowHigherAlign) { |
| 345 | // The size and alignment of the value that was passed directly. |
| 346 | CharUnits DirectSize, DirectAlign; |
| 347 | if (IsIndirect) { |
| 348 | DirectSize = CGF.getPointerSize(); |
| 349 | DirectAlign = CGF.getPointerAlign(); |
| 350 | } else { |
| 351 | DirectSize = ValueInfo.first; |
| 352 | DirectAlign = ValueInfo.second; |
| 353 | } |
| 354 | |
| 355 | // Cast the address we've calculated to the right type. |
| 356 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 357 | if (IsIndirect) |
| 358 | DirectTy = DirectTy->getPointerTo(0); |
| 359 | |
| 360 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 361 | DirectSize, DirectAlign, |
| 362 | SlotSizeAndAlign, |
| 363 | AllowHigherAlign); |
| 364 | |
| 365 | if (IsIndirect) { |
| 366 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 367 | } |
| 368 | |
| 369 | return Addr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 370 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 374 | Address Addr1, llvm::BasicBlock *Block1, |
| 375 | Address Addr2, llvm::BasicBlock *Block2, |
| 376 | const llvm::Twine &Name = "") { |
| 377 | assert(Addr1.getType() == Addr2.getType()); |
| 378 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 379 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 380 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 381 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 382 | return Address(PHI, Align); |
| 383 | } |
| 384 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 385 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 386 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 387 | // If someone can figure out a general rule for this, that would be great. |
| 388 | // It's probably just doomed to be platform-dependent, though. |
| 389 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 390 | // Verified for: |
| 391 | // x86-64 FreeBSD, Linux, Darwin |
| 392 | // x86-32 FreeBSD, Linux, Darwin |
| 393 | // PowerPC Linux, Darwin |
| 394 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 395 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 396 | return 32; |
| 397 | } |
| 398 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 399 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 400 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 401 | // The following conventions are known to require this to be false: |
| 402 | // x86_stdcall |
| 403 | // MIPS |
| 404 | // For everything else, we just prefer false unless we opt out. |
| 405 | return false; |
| 406 | } |
| 407 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 408 | void |
| 409 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 410 | llvm::SmallString<24> &Opt) const { |
| 411 | // This assumes the user is passing a library name like "rt" instead of a |
| 412 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 413 | // dynamic. |
| 414 | Opt = "-l"; |
| 415 | Opt += Lib; |
| 416 | } |
| 417 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 418 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 419 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 420 | // set with clSetKernelArg(), not as normal sub-functions. |
| 421 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 422 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 423 | // gets one matching argument in the produced kernel function argument |
| 424 | // list to enable feasible implementation of clSetKernelArg() with |
| 425 | // aggregates etc. In case we would use the default C calling conv here, |
| 426 | // clSetKernelArg() might break depending on the target-specific |
| 427 | // conventions; different targets might split structs passed as values |
| 428 | // to multiple function arguments etc. |
| 429 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 430 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 431 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 432 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 433 | llvm::PointerType *T, QualType QT) const { |
| 434 | return llvm::ConstantPointerNull::get(T); |
| 435 | } |
| 436 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 437 | LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 438 | const VarDecl *D) const { |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 439 | assert(!CGM.getLangOpts().OpenCL && |
| 440 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 441 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 442 | return D ? D->getType().getAddressSpace() : LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 445 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 446 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, |
| 447 | LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 448 | // Since target may map different address spaces in AST to the same address |
| 449 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 450 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 451 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Vyacheslav Zakharin | de811d1 | 2019-07-10 17:10:05 +0000 | [diff] [blame] | 452 | // Try to preserve the source's name to make IR more readable. |
| 453 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 454 | Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : ""); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 457 | llvm::Constant * |
| 458 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 459 | LangAS SrcAddr, LangAS DestAddr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 460 | llvm::Type *DestTy) const { |
| 461 | // Since target may map different address spaces in AST to the same address |
| 462 | // space, an address space conversion may end up as a bitcast. |
| 463 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 464 | } |
| 465 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 466 | llvm::SyncScope::ID |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 467 | TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 468 | SyncScope Scope, |
| 469 | llvm::AtomicOrdering Ordering, |
| 470 | llvm::LLVMContext &Ctx) const { |
| 471 | return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */ |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 474 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 475 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 476 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 477 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 478 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 479 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 480 | if (FD->isUnnamedBitfield()) |
| 481 | return true; |
| 482 | |
| 483 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 484 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 485 | // Constant arrays of empty records count as empty, strip them off. |
| 486 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 487 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 488 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 489 | if (AT->getSize() == 0) |
| 490 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 491 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 492 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 493 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 494 | const RecordType *RT = FT->getAs<RecordType>(); |
| 495 | if (!RT) |
| 496 | return false; |
| 497 | |
| 498 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 499 | // |
| 500 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 501 | // current ABI. |
| 502 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 503 | return false; |
| 504 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 505 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 508 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 509 | /// fields. Note that a structure with a flexible array member is not |
| 510 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 511 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 512 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 513 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 514 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 515 | const RecordDecl *RD = RT->getDecl(); |
| 516 | if (RD->hasFlexibleArrayMember()) |
| 517 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 518 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 519 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 520 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 521 | for (const auto &I : CXXRD->bases()) |
| 522 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 523 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 524 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 525 | for (const auto *I : RD->fields()) |
| 526 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 527 | return false; |
| 528 | return true; |
| 529 | } |
| 530 | |
| 531 | /// isSingleElementStruct - Determine if a structure is a "single |
| 532 | /// element struct", i.e. it has exactly one non-empty field or |
| 533 | /// exactly one field which is itself a single element |
| 534 | /// struct. Structures with flexible array members are never |
| 535 | /// considered single element structs. |
| 536 | /// |
| 537 | /// \return The field declaration for the single non-empty field, if |
| 538 | /// it exists. |
| 539 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 540 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 541 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 542 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 543 | |
| 544 | const RecordDecl *RD = RT->getDecl(); |
| 545 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 546 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 547 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 548 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 549 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 550 | // If this is a C++ record, check the bases first. |
| 551 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 552 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 553 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 554 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 555 | continue; |
| 556 | |
| 557 | // If we already found an element then this isn't a single-element struct. |
| 558 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 559 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 560 | |
| 561 | // If this is non-empty and not a single element struct, the composite |
| 562 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 563 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 564 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 565 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 570 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 571 | QualType FT = FD->getType(); |
| 572 | |
| 573 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 574 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 575 | continue; |
| 576 | |
| 577 | // If we already found an element then this isn't a single-element |
| 578 | // struct. |
| 579 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 580 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 581 | |
| 582 | // Treat single element arrays as the element. |
| 583 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 584 | if (AT->getSize().getZExtValue() != 1) |
| 585 | break; |
| 586 | FT = AT->getElementType(); |
| 587 | } |
| 588 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 589 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 590 | Found = FT.getTypePtr(); |
| 591 | } else { |
| 592 | Found = isSingleElementStruct(FT, Context); |
| 593 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 594 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 598 | // We don't consider a struct a single-element struct if it has |
| 599 | // padding beyond the element type. |
| 600 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 601 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 602 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 603 | return Found; |
| 604 | } |
| 605 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 606 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 607 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 608 | const ABIArgInfo &AI) { |
| 609 | // This default implementation defers to the llvm backend's va_arg |
| 610 | // instruction. It can handle only passing arguments directly |
| 611 | // (typically only handled in the backend for primitive types), or |
| 612 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 613 | // flag has ABI impact in the callee, this implementation cannot |
| 614 | // work.) |
| 615 | |
| 616 | // Only a few cases are covered here at the moment -- those needed |
| 617 | // by the default abi. |
| 618 | llvm::Value *Val; |
| 619 | |
| 620 | if (AI.isIndirect()) { |
| 621 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 622 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 623 | assert( |
| 624 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 625 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 626 | |
| 627 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 628 | CharUnits TyAlignForABI = TyInfo.second; |
| 629 | |
| 630 | llvm::Type *BaseTy = |
| 631 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 632 | llvm::Value *Addr = |
| 633 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 634 | return Address(Addr, TyAlignForABI); |
| 635 | } else { |
| 636 | assert((AI.isDirect() || AI.isExtend()) && |
| 637 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 638 | |
| 639 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 640 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 641 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 642 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 643 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 644 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 645 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 646 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 647 | |
| 648 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 649 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 650 | CGF.Builder.CreateStore(Val, Temp); |
| 651 | return Temp; |
| 652 | } |
| 653 | } |
| 654 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 655 | /// DefaultABIInfo - The default implementation for ABI specific |
| 656 | /// details. This implementation provides information which results in |
| 657 | /// self-consistent and sensible LLVM IR generation, but does not |
| 658 | /// conform to any particular ABI. |
| 659 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 660 | public: |
| 661 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 663 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 664 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 665 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 666 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 667 | if (!getCXXABI().classifyReturnType(FI)) |
| 668 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 669 | for (auto &I : FI.arguments()) |
| 670 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 671 | } |
| 672 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 673 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 674 | QualType Ty) const override { |
| 675 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 676 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 677 | }; |
| 678 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 679 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 680 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 681 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 682 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 683 | }; |
| 684 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 685 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 686 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 687 | |
| 688 | if (isAggregateTypeForABI(Ty)) { |
| 689 | // Records with non-trivial destructors/copy-constructors should not be |
| 690 | // passed by value. |
| 691 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 692 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 693 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 694 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 695 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 697 | // Treat an enum type as its underlying type. |
| 698 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 699 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 700 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 701 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 702 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 705 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 706 | if (RetTy->isVoidType()) |
| 707 | return ABIArgInfo::getIgnore(); |
| 708 | |
| 709 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 710 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 711 | |
| 712 | // Treat an enum type as its underlying type. |
| 713 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 714 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 715 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 716 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 717 | : ABIArgInfo::getDirect()); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 720 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 721 | // WebAssembly ABI Implementation |
| 722 | // |
| 723 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 724 | //===----------------------------------------------------------------------===// |
| 725 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 726 | class WebAssemblyABIInfo final : public SwiftABIInfo { |
| 727 | DefaultABIInfo defaultInfo; |
| 728 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 729 | public: |
| 730 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 731 | : SwiftABIInfo(CGT), defaultInfo(CGT) {} |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 732 | |
| 733 | private: |
| 734 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 735 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 736 | |
| 737 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 738 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 739 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 740 | void computeInfo(CGFunctionInfo &FI) const override { |
| 741 | if (!getCXXABI().classifyReturnType(FI)) |
| 742 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 743 | for (auto &Arg : FI.arguments()) |
| 744 | Arg.info = classifyArgumentType(Arg.type); |
| 745 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 746 | |
| 747 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 748 | QualType Ty) const override; |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 749 | |
| 750 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 751 | bool asReturnValue) const override { |
| 752 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 753 | } |
| 754 | |
| 755 | bool isSwiftErrorInRegister() const override { |
| 756 | return false; |
| 757 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 758 | }; |
| 759 | |
| 760 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 761 | public: |
| 762 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 763 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 764 | |
| 765 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 766 | CodeGen::CodeGenModule &CGM) const override { |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 767 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 768 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 769 | if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) { |
| 770 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 771 | llvm::AttrBuilder B; |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 772 | B.addAttribute("wasm-import-module", Attr->getImportModule()); |
| 773 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 774 | } |
| 775 | if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) { |
| 776 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 777 | llvm::AttrBuilder B; |
| 778 | B.addAttribute("wasm-import-name", Attr->getImportName()); |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 779 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 780 | } |
| 781 | } |
| 782 | |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 783 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 784 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 785 | if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) |
| 786 | Fn->addFnAttr("no-prototype"); |
| 787 | } |
| 788 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 789 | }; |
| 790 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 791 | /// Classify argument of given type \p Ty. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 792 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 793 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 794 | |
| 795 | if (isAggregateTypeForABI(Ty)) { |
| 796 | // Records with non-trivial destructors/copy-constructors should not be |
| 797 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 798 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 799 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 800 | // Ignore empty structs/unions. |
| 801 | if (isEmptyRecord(getContext(), Ty, true)) |
| 802 | return ABIArgInfo::getIgnore(); |
| 803 | // Lower single-element structs to just pass a regular value. TODO: We |
| 804 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 805 | // though watch out for things like bitfields. |
| 806 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 807 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 811 | return defaultInfo.classifyArgumentType(Ty); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 815 | if (isAggregateTypeForABI(RetTy)) { |
| 816 | // Records with non-trivial destructors/copy-constructors should not be |
| 817 | // returned by value. |
| 818 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 819 | // Ignore empty structs/unions. |
| 820 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 821 | return ABIArgInfo::getIgnore(); |
| 822 | // Lower single-element structs to just return a regular value. TODO: We |
| 823 | // could do reasonable-size multiple-element structs too, using |
| 824 | // ABIArgInfo::getDirect(). |
| 825 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 826 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 831 | return defaultInfo.classifyReturnType(RetTy); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 834 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 835 | QualType Ty) const { |
Guanzhong Chen | 82bfd1d | 2019-08-15 19:33:36 +0000 | [diff] [blame] | 836 | bool IsIndirect = isAggregateTypeForABI(Ty) && |
| 837 | !isEmptyRecord(getContext(), Ty, true) && |
| 838 | !isSingleElementStruct(Ty, getContext()); |
Guanzhong Chen | 8a503e4 | 2019-08-13 21:41:11 +0000 | [diff] [blame] | 839 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 840 | getContext().getTypeInfoInChars(Ty), |
| 841 | CharUnits::fromQuantity(4), |
Guanzhong Chen | 8a503e4 | 2019-08-13 21:41:11 +0000 | [diff] [blame] | 842 | /*AllowHigherAlign=*/true); |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 845 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 846 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 847 | // |
| 848 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 849 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 850 | //===----------------------------------------------------------------------===// |
| 851 | |
| 852 | class PNaClABIInfo : public ABIInfo { |
| 853 | public: |
| 854 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 855 | |
| 856 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 857 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 858 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 859 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 860 | Address EmitVAArg(CodeGenFunction &CGF, |
| 861 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 862 | }; |
| 863 | |
| 864 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 865 | public: |
| 866 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 867 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 868 | }; |
| 869 | |
| 870 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 871 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 872 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 873 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 874 | for (auto &I : FI.arguments()) |
| 875 | I.info = classifyArgumentType(I.type); |
| 876 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 877 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 878 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 879 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 880 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 881 | // function classification. Structs get passed directly for varargs |
| 882 | // functions, through a rewriting transform in |
| 883 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 884 | // this target to actually support a va_arg instructions with an |
| 885 | // aggregate type, unlike other targets. |
| 886 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 889 | /// Classify argument of given type \p Ty. |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 890 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 891 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 892 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 893 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 894 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 895 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 896 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 897 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 898 | } else if (Ty->isFloatingType()) { |
| 899 | // Floating-point types don't go inreg. |
| 900 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 901 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 902 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 903 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 904 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 908 | if (RetTy->isVoidType()) |
| 909 | return ABIArgInfo::getIgnore(); |
| 910 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 911 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 912 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 913 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 914 | |
| 915 | // Treat an enum type as its underlying type. |
| 916 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 917 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 918 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 919 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 920 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 923 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 924 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 925 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
| 926 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 927 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 928 | IRType->getScalarSizeInBits() != 64; |
| 929 | } |
| 930 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 931 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 932 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 933 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 934 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 935 | .Cases("y", "&y", "^Ym", true) |
| 936 | .Default(false); |
| 937 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 938 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 939 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 940 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 943 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 947 | return Ty; |
| 948 | } |
| 949 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 950 | /// Returns true if this type can be passed in SSE registers with the |
| 951 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 952 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 953 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 954 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 955 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 956 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 957 | &llvm::APFloat::x87DoubleExtended()) |
| 958 | return false; |
| 959 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 960 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 961 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 962 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 963 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 964 | // registers specially. |
| 965 | unsigned VecSize = Context.getTypeSize(VT); |
| 966 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 967 | return true; |
| 968 | } |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 973 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 974 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 975 | return NumMembers <= 4; |
| 976 | } |
| 977 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 978 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 979 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 980 | auto AI = ABIArgInfo::getDirect(T); |
| 981 | AI.setInReg(true); |
| 982 | AI.setCanBeFlattened(false); |
| 983 | return AI; |
| 984 | } |
| 985 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 986 | //===----------------------------------------------------------------------===// |
| 987 | // X86-32 ABI Implementation |
| 988 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 989 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 990 | /// Similar to llvm::CCState, but for Clang. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 991 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 992 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 993 | |
| 994 | unsigned CC; |
| 995 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 996 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 997 | }; |
| 998 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 999 | enum { |
| 1000 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 1001 | VectorcallMaxParamNumAsReg = 6 |
| 1002 | }; |
| 1003 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1004 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1005 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1006 | enum Class { |
| 1007 | Integer, |
| 1008 | Float |
| 1009 | }; |
| 1010 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1011 | static const unsigned MinABIStackAlignInBytes = 4; |
| 1012 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1013 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1014 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1015 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1016 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1017 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1018 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1019 | |
| 1020 | static bool isRegisterSize(unsigned Size) { |
| 1021 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 1022 | } |
| 1023 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1024 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1025 | // FIXME: Assumes vectorcall is in use. |
| 1026 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1027 | } |
| 1028 | |
| 1029 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1030 | uint64_t NumMembers) const override { |
| 1031 | // FIXME: Assumes vectorcall is in use. |
| 1032 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1033 | } |
| 1034 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1035 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1036 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1037 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 1038 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1039 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 1040 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1041 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1042 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1043 | /// Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1044 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1045 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1046 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1047 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1048 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1049 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1050 | /// Updates the number of available free registers, returns |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1051 | /// true if any registers were allocated. |
| 1052 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 1053 | |
| 1054 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1055 | bool &NeedsPadding) const; |
| 1056 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1057 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1058 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1059 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1060 | /// Rewrite the function info so that all memory arguments use |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1061 | /// inalloca. |
| 1062 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1063 | |
| 1064 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1065 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1066 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1067 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1068 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1069 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1070 | public: |
| 1071 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1072 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1073 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1074 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1075 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1076 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1077 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1078 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1079 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1080 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1081 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1082 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1083 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1084 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1085 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1086 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1087 | bool asReturnValue) const override { |
| 1088 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1089 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1090 | // four vector registers for vectors, but those can overlap with the |
| 1091 | // scalar registers. |
| 1092 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1093 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1094 | |
| 1095 | bool isSwiftErrorInRegister() const override { |
| 1096 | // x86-32 lowering does not support passing swifterror in a register. |
| 1097 | return false; |
| 1098 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1099 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1100 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1101 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1102 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1103 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1104 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1105 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1106 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1107 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1108 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1109 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1110 | static bool isStructReturnInRegABI( |
| 1111 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1112 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1113 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1114 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1115 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1116 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1117 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1118 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1119 | return 4; |
| 1120 | } |
| 1121 | |
| 1122 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1123 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1124 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1125 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1126 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1127 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1128 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1129 | } |
| 1130 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1131 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1132 | std::string &Constraints, |
| 1133 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1134 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1135 | std::vector<LValue> &ResultRegDests, |
| 1136 | std::string &AsmString, |
| 1137 | unsigned NumOutputs) const override; |
| 1138 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1139 | llvm::Constant * |
| 1140 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1141 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1142 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1143 | ('v' << 16) | |
| 1144 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1145 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1146 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1147 | |
| 1148 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1149 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1150 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1151 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1152 | }; |
| 1153 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1154 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1155 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1156 | /// Rewrite input constraint references after adding some output constraints. |
| 1157 | /// In the case where there is one output and one input and we add one output, |
| 1158 | /// we need to replace all operand references greater than or equal to 1: |
| 1159 | /// mov $0, $1 |
| 1160 | /// mov eax, $1 |
| 1161 | /// The result will be: |
| 1162 | /// mov $0, $2 |
| 1163 | /// mov eax, $2 |
| 1164 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1165 | unsigned NumNewOuts, |
| 1166 | std::string &AsmString) { |
| 1167 | std::string Buf; |
| 1168 | llvm::raw_string_ostream OS(Buf); |
| 1169 | size_t Pos = 0; |
| 1170 | while (Pos < AsmString.size()) { |
| 1171 | size_t DollarStart = AsmString.find('$', Pos); |
| 1172 | if (DollarStart == std::string::npos) |
| 1173 | DollarStart = AsmString.size(); |
| 1174 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1175 | if (DollarEnd == std::string::npos) |
| 1176 | DollarEnd = AsmString.size(); |
| 1177 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1178 | Pos = DollarEnd; |
| 1179 | size_t NumDollars = DollarEnd - DollarStart; |
| 1180 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1181 | // We have an operand reference. |
| 1182 | size_t DigitStart = Pos; |
| 1183 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1184 | if (DigitEnd == std::string::npos) |
| 1185 | DigitEnd = AsmString.size(); |
| 1186 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1187 | unsigned OperandIndex; |
| 1188 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1189 | if (OperandIndex >= FirstIn) |
| 1190 | OperandIndex += NumNewOuts; |
| 1191 | OS << OperandIndex; |
| 1192 | } else { |
| 1193 | OS << OperandStr; |
| 1194 | } |
| 1195 | Pos = DigitEnd; |
| 1196 | } |
| 1197 | } |
| 1198 | AsmString = std::move(OS.str()); |
| 1199 | } |
| 1200 | |
| 1201 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1202 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1203 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1204 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1205 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1206 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1207 | unsigned NumOutputs) const { |
| 1208 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1209 | |
| 1210 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1211 | // larger. |
| 1212 | if (!Constraints.empty()) |
| 1213 | Constraints += ','; |
| 1214 | if (RetWidth <= 32) { |
| 1215 | Constraints += "={eax}"; |
| 1216 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1217 | } else { |
| 1218 | // Use the 'A' constraint for EAX:EDX. |
| 1219 | Constraints += "=A"; |
| 1220 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1221 | } |
| 1222 | |
| 1223 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1224 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1225 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1226 | |
| 1227 | // Coerce the integer by bitcasting the return slot pointer. |
Akira Hatanaka | 9f37c0e | 2019-12-03 13:07:22 -0800 | [diff] [blame^] | 1228 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1229 | CoerceTy->getPointerTo())); |
| 1230 | ResultRegDests.push_back(ReturnSlot); |
| 1231 | |
| 1232 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1233 | } |
| 1234 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1235 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1236 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1237 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1238 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1239 | uint64_t Size = Context.getTypeSize(Ty); |
| 1240 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1241 | // For i386, type must be register sized. |
| 1242 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1243 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1244 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1245 | |
| 1246 | if (Ty->isVectorType()) { |
| 1247 | // 64- and 128- bit vectors inside structures are not returned in |
| 1248 | // registers. |
| 1249 | if (Size == 64 || Size == 128) |
| 1250 | return false; |
| 1251 | |
| 1252 | return true; |
| 1253 | } |
| 1254 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1255 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1256 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1257 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1258 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1259 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1260 | return true; |
| 1261 | |
| 1262 | // Arrays are treated like records. |
| 1263 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1264 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1265 | |
| 1266 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1267 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1268 | if (!RT) return false; |
| 1269 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1270 | // FIXME: Traverse bases here too. |
| 1271 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1272 | // Structure types are passed in register if all fields would be |
| 1273 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1274 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1275 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1276 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1277 | continue; |
| 1278 | |
| 1279 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1280 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1281 | return false; |
| 1282 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1283 | return true; |
| 1284 | } |
| 1285 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1286 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1287 | // Treat complex types as the element type. |
| 1288 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1289 | Ty = CTy->getElementType(); |
| 1290 | |
| 1291 | // Check for a type which we know has a simple scalar argument-passing |
| 1292 | // convention without any padding. (We're specifically looking for 32 |
| 1293 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1294 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1295 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1296 | return false; |
| 1297 | |
| 1298 | uint64_t Size = Context.getTypeSize(Ty); |
| 1299 | return Size == 32 || Size == 64; |
| 1300 | } |
| 1301 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1302 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1303 | uint64_t &Size) { |
| 1304 | for (const auto *FD : RD->fields()) { |
| 1305 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1306 | // argument is smaller than 32-bits, expanding the struct will create |
| 1307 | // alignment padding. |
| 1308 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1309 | return false; |
| 1310 | |
| 1311 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1312 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1313 | // counts as "basic" is more complicated than what we were doing previously. |
| 1314 | if (FD->isBitField()) |
| 1315 | return false; |
| 1316 | |
| 1317 | Size += Context.getTypeSize(FD->getType()); |
| 1318 | } |
| 1319 | return true; |
| 1320 | } |
| 1321 | |
| 1322 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1323 | uint64_t &Size) { |
| 1324 | // Don't do this if there are any non-empty bases. |
| 1325 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1326 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1327 | Size)) |
| 1328 | return false; |
| 1329 | } |
| 1330 | if (!addFieldSizes(Context, RD, Size)) |
| 1331 | return false; |
| 1332 | return true; |
| 1333 | } |
| 1334 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1335 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1336 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1337 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1338 | /// optimizations. |
| 1339 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1340 | // We can only expand structure types. |
| 1341 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1342 | if (!RT) |
| 1343 | return false; |
| 1344 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1345 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1346 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1347 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1348 | // On non-Windows, we have to conservatively match our old bitcode |
| 1349 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1350 | if (!CXXRD->isCLike()) |
| 1351 | return false; |
| 1352 | } else { |
| 1353 | // Don't do this for dynamic classes. |
| 1354 | if (CXXRD->isDynamicClass()) |
| 1355 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1356 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1357 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1358 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1359 | } else { |
| 1360 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1361 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | // We can do this if there was no alignment padding. |
| 1365 | return Size == getContext().getTypeSize(Ty); |
| 1366 | } |
| 1367 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1368 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1369 | // If the return value is indirect, then the hidden argument is consuming one |
| 1370 | // integer register. |
| 1371 | if (State.FreeRegs) { |
| 1372 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1373 | if (!IsMCUABI) |
| 1374 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1375 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1376 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1379 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1380 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1381 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1382 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1383 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1384 | const Type *Base = nullptr; |
| 1385 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1386 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1387 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1388 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1389 | // The LLVM struct type for such an aggregate should lower properly. |
| 1390 | return ABIArgInfo::getDirect(); |
| 1391 | } |
| 1392 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1393 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1394 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1395 | if (IsDarwinVectorABI) { |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1396 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 1397 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1398 | // 128-bit vectors are a special case; they are returned in |
| 1399 | // registers and we need to make sure to pick a type the LLVM |
| 1400 | // backend will like. |
| 1401 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1402 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1403 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Always return in register if it fits in a general purpose |
| 1406 | // register, or if it is 64 bits and has a single element. |
| 1407 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1408 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1409 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1410 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1411 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1412 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1416 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1417 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1418 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1419 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1420 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1421 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1422 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1423 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1424 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1425 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1426 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1427 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1428 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1429 | // Ignore empty structs/unions. |
| 1430 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1431 | return ABIArgInfo::getIgnore(); |
| 1432 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1433 | // Small structures which are register sized are generally returned |
| 1434 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1435 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1436 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1437 | |
| 1438 | // As a special-case, if the struct is a "single-element" struct, and |
| 1439 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1440 | // floating-point register. (MSVC does not apply this special case.) |
| 1441 | // We apply a similar transformation for pointer types to improve the |
| 1442 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1443 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1444 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1445 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1446 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1447 | |
| 1448 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1449 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1450 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1453 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1454 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1455 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1456 | // Treat an enum type as its underlying type. |
| 1457 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1458 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1459 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1460 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1461 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1464 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1465 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1466 | } |
| 1467 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1468 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1469 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1470 | if (!RT) |
| 1471 | return 0; |
| 1472 | const RecordDecl *RD = RT->getDecl(); |
| 1473 | |
| 1474 | // If this is a C++ record, check the bases first. |
| 1475 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1476 | for (const auto &I : CXXRD->bases()) |
| 1477 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1478 | return false; |
| 1479 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1480 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1481 | QualType FT = i->getType(); |
| 1482 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1483 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1484 | return true; |
| 1485 | |
| 1486 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1487 | return true; |
| 1488 | } |
| 1489 | |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1493 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1494 | unsigned Align) const { |
| 1495 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1496 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1497 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1498 | return 0; // Use default alignment. |
| 1499 | |
Pengfei Wang | 48387ec | 2019-05-31 01:50:07 +0000 | [diff] [blame] | 1500 | // On non-Darwin, the stack type alignment is always 4. |
| 1501 | if (!IsDarwinVectorABI) { |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1502 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1503 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1504 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1505 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1506 | // 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] | 1507 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1508 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1509 | return 16; |
| 1510 | |
| 1511 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1514 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1515 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1516 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1517 | if (State.FreeRegs) { |
| 1518 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1519 | if (!IsMCUABI) |
| 1520 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1521 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1522 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1523 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1524 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1525 | // Compute the byval alignment. |
| 1526 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1527 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1528 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1529 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1530 | |
| 1531 | // If the stack alignment is less than the type alignment, realign the |
| 1532 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1533 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1534 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1535 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1538 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1539 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1540 | if (!T) |
| 1541 | T = Ty.getTypePtr(); |
| 1542 | |
| 1543 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1544 | BuiltinType::Kind K = BT->getKind(); |
| 1545 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1546 | return Float; |
| 1547 | } |
| 1548 | return Integer; |
| 1549 | } |
| 1550 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1551 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1552 | if (!IsSoftFloatABI) { |
| 1553 | Class C = classify(Ty); |
| 1554 | if (C == Float) |
| 1555 | return false; |
| 1556 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1557 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1558 | unsigned Size = getContext().getTypeSize(Ty); |
| 1559 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1560 | |
| 1561 | if (SizeInRegs == 0) |
| 1562 | return false; |
| 1563 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1564 | if (!IsMCUABI) { |
| 1565 | if (SizeInRegs > State.FreeRegs) { |
| 1566 | State.FreeRegs = 0; |
| 1567 | return false; |
| 1568 | } |
| 1569 | } else { |
| 1570 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1571 | // earlier parameters that are passed on the stack. Also, |
| 1572 | // it does not allow passing >8-byte structs in-register, |
| 1573 | // even if there are 3 free registers available. |
| 1574 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1575 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1576 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1577 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1578 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1579 | return true; |
| 1580 | } |
| 1581 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1582 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1583 | bool &InReg, |
| 1584 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1585 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1586 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1587 | // (HFAs) have already been dealt with at this point. |
| 1588 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1589 | return false; |
| 1590 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1591 | NeedsPadding = false; |
| 1592 | InReg = !IsMCUABI; |
| 1593 | |
| 1594 | if (!updateFreeRegs(Ty, State)) |
| 1595 | return false; |
| 1596 | |
| 1597 | if (IsMCUABI) |
| 1598 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1599 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1600 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1601 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1602 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1603 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1604 | NeedsPadding = true; |
| 1605 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1606 | return false; |
| 1607 | } |
| 1608 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1609 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1612 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1613 | if (!updateFreeRegs(Ty, State)) |
| 1614 | return false; |
| 1615 | |
| 1616 | if (IsMCUABI) |
| 1617 | return false; |
| 1618 | |
| 1619 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1620 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1621 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1622 | if (getContext().getTypeSize(Ty) > 32) |
| 1623 | return false; |
| 1624 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1625 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1626 | Ty->isReferenceType()); |
| 1627 | } |
| 1628 | |
| 1629 | return true; |
| 1630 | } |
| 1631 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1632 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1633 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1634 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1635 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1636 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1637 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1638 | // Check with the C++ ABI first. |
| 1639 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1640 | if (RT) { |
| 1641 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1642 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1643 | return getIndirectResult(Ty, false, State); |
| 1644 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1645 | // The field index doesn't matter, we'll fix it up later. |
| 1646 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1647 | } |
| 1648 | } |
| 1649 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1650 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1651 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1652 | const Type *Base = nullptr; |
| 1653 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1654 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1655 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1656 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1657 | if (State.FreeSSERegs >= NumElts) { |
| 1658 | State.FreeSSERegs -= NumElts; |
| 1659 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1660 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1661 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1662 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1663 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1667 | // Structures with flexible arrays are always indirect. |
| 1668 | // FIXME: This should not be byval! |
| 1669 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1670 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1671 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1672 | // Ignore empty structs/unions on non-Windows. |
| 1673 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1674 | return ABIArgInfo::getIgnore(); |
| 1675 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1676 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1677 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1678 | bool NeedsPadding = false; |
| 1679 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1680 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1681 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1682 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1683 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1684 | if (InReg) |
| 1685 | return ABIArgInfo::getDirectInReg(Result); |
| 1686 | else |
| 1687 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1688 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1689 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1690 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1691 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1692 | // of those arguments will match the struct. This is important because the |
| 1693 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1694 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1695 | // Don't do this for the MCU if there are still free integer registers |
| 1696 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1697 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1698 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1699 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1700 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1701 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1702 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1703 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1704 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1705 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1708 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1709 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1710 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1711 | if (IsDarwinVectorABI) { |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1712 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1713 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1714 | (Size == 64 && VT->getNumElements() == 1)) |
| 1715 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1716 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1717 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1718 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1719 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1720 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
| 1721 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1722 | return ABIArgInfo::getDirect(); |
| 1723 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1724 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1725 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1726 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1727 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1728 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1729 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1730 | |
| 1731 | if (Ty->isPromotableIntegerType()) { |
| 1732 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1733 | return ABIArgInfo::getExtendInReg(Ty); |
| 1734 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1735 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1736 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1737 | if (InReg) |
| 1738 | return ABIArgInfo::getDirectInReg(); |
| 1739 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1742 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1743 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1744 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1745 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1746 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1747 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1748 | // registers. |
| 1749 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1750 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1751 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1752 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1753 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1754 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1755 | // First pass do all the vector types. |
| 1756 | const Type *Base = nullptr; |
| 1757 | uint64_t NumElts = 0; |
| 1758 | const QualType& Ty = I.type; |
| 1759 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1760 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1761 | if (State.FreeSSERegs >= NumElts) { |
| 1762 | State.FreeSSERegs -= NumElts; |
| 1763 | I.info = ABIArgInfo::getDirect(); |
| 1764 | } else { |
| 1765 | I.info = classifyArgumentType(Ty, State); |
| 1766 | } |
| 1767 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1768 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1769 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1770 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1771 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1772 | // Second pass, do the rest! |
| 1773 | const Type *Base = nullptr; |
| 1774 | uint64_t NumElts = 0; |
| 1775 | const QualType& Ty = I.type; |
| 1776 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1777 | |
| 1778 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1779 | // Assign true HVAs (non vector/native FP types). |
| 1780 | if (State.FreeSSERegs >= NumElts) { |
| 1781 | State.FreeSSERegs -= NumElts; |
| 1782 | I.info = getDirectX86Hva(); |
| 1783 | } else { |
| 1784 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1785 | } |
| 1786 | } else if (!IsHva) { |
| 1787 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1788 | I.info = classifyArgumentType(Ty, State); |
| 1789 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1790 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1794 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1795 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1796 | if (IsMCUABI) |
| 1797 | State.FreeRegs = 3; |
| 1798 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1799 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1800 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1801 | State.FreeRegs = 2; |
| 1802 | State.FreeSSERegs = 6; |
| 1803 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1804 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1805 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1806 | State.FreeRegs = 5; |
| 1807 | State.FreeSSERegs = 8; |
| 1808 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1809 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1810 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 1811 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1812 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1813 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1814 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1815 | // return value was sret and put it in a register ourselves if appropriate. |
| 1816 | if (State.FreeRegs) { |
| 1817 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1818 | if (!IsMCUABI) |
| 1819 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1820 | } |
| 1821 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1822 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1823 | // The chain argument effectively gives us another free register. |
| 1824 | if (FI.isChainCall()) |
| 1825 | ++State.FreeRegs; |
| 1826 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1827 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1828 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1829 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1830 | } else { |
| 1831 | // If not vectorcall, revert to normal behavior. |
| 1832 | for (auto &I : FI.arguments()) { |
| 1833 | I.info = classifyArgumentType(I.type, State); |
| 1834 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1835 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1839 | // all the memory arguments to use inalloca. |
| 1840 | if (UsedInAlloca) |
| 1841 | rewriteWithInAlloca(FI); |
| 1842 | } |
| 1843 | |
| 1844 | void |
| 1845 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1846 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1847 | QualType Type) const { |
| 1848 | // Arguments are always 4-byte-aligned. |
| 1849 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1850 | |
| 1851 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1852 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1853 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1854 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1855 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1856 | // Insert padding bytes to respect alignment. |
| 1857 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1858 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1859 | if (StackOffset != FieldEnd) { |
| 1860 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1861 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1862 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1863 | FrameFields.push_back(Ty); |
| 1864 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1867 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1868 | // Leave ignored and inreg arguments alone. |
| 1869 | switch (Info.getKind()) { |
| 1870 | case ABIArgInfo::InAlloca: |
| 1871 | return true; |
| 1872 | case ABIArgInfo::Indirect: |
| 1873 | assert(Info.getIndirectByVal()); |
| 1874 | return true; |
| 1875 | case ABIArgInfo::Ignore: |
| 1876 | return false; |
| 1877 | case ABIArgInfo::Direct: |
| 1878 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1879 | if (Info.getInReg()) |
| 1880 | return false; |
| 1881 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1882 | case ABIArgInfo::Expand: |
| 1883 | case ABIArgInfo::CoerceAndExpand: |
| 1884 | // These are aggregate types which are never passed in registers when |
| 1885 | // inalloca is involved. |
| 1886 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1887 | } |
| 1888 | llvm_unreachable("invalid enum"); |
| 1889 | } |
| 1890 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1891 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1892 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1893 | |
| 1894 | // Build a packed struct type for all of the arguments in memory. |
| 1895 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1896 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1897 | // The stack alignment is always 4. |
| 1898 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1899 | |
| 1900 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1901 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1902 | |
| 1903 | // Put 'this' into the struct before 'sret', if necessary. |
| 1904 | bool IsThisCall = |
| 1905 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1906 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1907 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1908 | isArgInAlloca(I->info)) { |
| 1909 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1910 | ++I; |
| 1911 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1912 | |
| 1913 | // 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] | 1914 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1915 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1916 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1917 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1918 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1922 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1923 | ++I; |
| 1924 | |
| 1925 | // Put arguments passed in memory into the struct. |
| 1926 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1927 | if (isArgInAlloca(I->info)) |
| 1928 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1932 | /*isPacked=*/true), |
| 1933 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1936 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1937 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1938 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1939 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1940 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1941 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1942 | // |
| 1943 | // Just messing with TypeInfo like this works because we never pass |
| 1944 | // anything indirectly. |
| 1945 | TypeInfo.second = CharUnits::fromQuantity( |
| 1946 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
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 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1949 | TypeInfo, CharUnits::fromQuantity(4), |
| 1950 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1953 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1954 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1955 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1956 | |
| 1957 | switch (Opts.getStructReturnConvention()) { |
| 1958 | case CodeGenOptions::SRCK_Default: |
| 1959 | break; |
| 1960 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1961 | return false; |
| 1962 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1963 | return true; |
| 1964 | } |
| 1965 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1966 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1967 | return true; |
| 1968 | |
| 1969 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1970 | case llvm::Triple::DragonFly: |
| 1971 | case llvm::Triple::FreeBSD: |
| 1972 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1973 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1974 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1975 | default: |
| 1976 | return false; |
| 1977 | } |
| 1978 | } |
| 1979 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1980 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1981 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1982 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1983 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1984 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1985 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1986 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 1987 | Fn->addFnAttr("stackrealign"); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1988 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1989 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1990 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1991 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1992 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1993 | } |
| 1994 | } |
| 1995 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1996 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1997 | CodeGen::CodeGenFunction &CGF, |
| 1998 | llvm::Value *Address) const { |
| 1999 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2000 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2001 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2002 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2003 | // 0-7 are the eight integer registers; the order is different |
| 2004 | // on Darwin (for EH), but the range is the same. |
| 2005 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2006 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2007 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2008 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2009 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 2010 | // These have size 16, which is sizeof(long double) on |
| 2011 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2012 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2013 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2014 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2015 | } else { |
| 2016 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 2017 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2018 | Builder.CreateAlignedStore( |
| 2019 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 2020 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2021 | |
| 2022 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 2023 | // These have size 12, which is sizeof(long double) on |
| 2024 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2025 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2026 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 2027 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2028 | |
| 2029 | return false; |
| 2030 | } |
| 2031 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2032 | //===----------------------------------------------------------------------===// |
| 2033 | // X86-64 ABI Implementation |
| 2034 | //===----------------------------------------------------------------------===// |
| 2035 | |
| 2036 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2037 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2038 | /// The AVX ABI level for X86 targets. |
| 2039 | enum class X86AVXABILevel { |
| 2040 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2041 | AVX, |
| 2042 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2043 | }; |
| 2044 | |
| 2045 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2046 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2047 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2048 | case X86AVXABILevel::AVX512: |
| 2049 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2050 | case X86AVXABILevel::AVX: |
| 2051 | return 256; |
| 2052 | case X86AVXABILevel::None: |
| 2053 | return 128; |
| 2054 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2055 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2058 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2059 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2060 | enum Class { |
| 2061 | Integer = 0, |
| 2062 | SSE, |
| 2063 | SSEUp, |
| 2064 | X87, |
| 2065 | X87Up, |
| 2066 | ComplexX87, |
| 2067 | NoClass, |
| 2068 | Memory |
| 2069 | }; |
| 2070 | |
| 2071 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2072 | /// |
| 2073 | /// Merge an accumulating classification \arg Accum with a field |
| 2074 | /// classification \arg Field. |
| 2075 | /// |
| 2076 | /// \param Accum - The accumulating classification. This should |
| 2077 | /// always be either NoClass or the result of a previous merge |
| 2078 | /// call. In addition, this should never be Memory (the caller |
| 2079 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2080 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2081 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2082 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2083 | /// |
| 2084 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2085 | /// final MEMORY or SSE classes when necessary. |
| 2086 | /// |
| 2087 | /// \param AggregateSize - The size of the current aggregate in |
| 2088 | /// the classification process. |
| 2089 | /// |
| 2090 | /// \param Lo - The classification for the parts of the type |
| 2091 | /// residing in the low word of the containing object. |
| 2092 | /// |
| 2093 | /// \param Hi - The classification for the parts of the type |
| 2094 | /// residing in the higher words of the containing object. |
| 2095 | /// |
| 2096 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2097 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2098 | /// classify - Determine the x86_64 register classes in which the |
| 2099 | /// given type T should be passed. |
| 2100 | /// |
| 2101 | /// \param Lo - The classification for the parts of the type |
| 2102 | /// residing in the low word of the containing object. |
| 2103 | /// |
| 2104 | /// \param Hi - The classification for the parts of the type |
| 2105 | /// residing in the high word of the containing object. |
| 2106 | /// |
| 2107 | /// \param OffsetBase - The bit offset of this type in the |
| 2108 | /// containing object. Some parameters are classified different |
| 2109 | /// depending on whether they straddle an eightbyte boundary. |
| 2110 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2111 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2112 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2113 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2114 | /// If a word is unused its result will be NoClass; if a type should |
| 2115 | /// be passed in Memory then at least the classification of \arg Lo |
| 2116 | /// will be Memory. |
| 2117 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2118 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2119 | /// |
| 2120 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2121 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2122 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2123 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2124 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2125 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2126 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2127 | unsigned IROffset, QualType SourceTy, |
| 2128 | unsigned SourceOffset) const; |
| 2129 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2130 | unsigned IROffset, QualType SourceTy, |
| 2131 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2132 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2133 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2134 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2135 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2136 | |
| 2137 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2138 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2139 | /// |
| 2140 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2141 | /// available. |
| 2142 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2143 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2144 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2145 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2146 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2147 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2148 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2149 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2150 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2151 | unsigned &NeededSSE) const; |
| 2152 | |
| 2153 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2154 | unsigned &NeededSSE) const; |
| 2155 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2156 | bool IsIllegalVectorType(QualType Ty) const; |
| 2157 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2158 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2159 | /// unfortunately in ways that were not always consistent with |
| 2160 | /// certain previous compilers. In particular, platforms which |
| 2161 | /// required strict binary compatibility with older versions of GCC |
| 2162 | /// may need to exempt themselves. |
| 2163 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2164 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2167 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2168 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2169 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2170 | // Clang <= 3.8 did not do this. |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 2171 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2172 | LangOptions::ClangABI::Ver3_8) |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2173 | return false; |
| 2174 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2175 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2176 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2177 | return false; |
| 2178 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2179 | return false; |
| 2180 | return true; |
| 2181 | } |
| 2182 | |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 2183 | // GCC classifies vectors of __int128 as memory. |
| 2184 | bool passInt128VectorsInMem() const { |
| 2185 | // Clang <= 9.0 did not do this. |
| 2186 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2187 | LangOptions::ClangABI::Ver9) |
| 2188 | return false; |
| 2189 | |
| 2190 | const llvm::Triple &T = getTarget().getTriple(); |
| 2191 | return T.isOSLinux() || T.isOSNetBSD(); |
| 2192 | } |
| 2193 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2194 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2195 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2196 | // 64-bit hardware. |
| 2197 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2198 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2199 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2200 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2201 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2202 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2203 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2204 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2205 | bool isPassedUsingAVXType(QualType type) const { |
| 2206 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2207 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2208 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2209 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2210 | if (info.isDirect()) { |
| 2211 | llvm::Type *ty = info.getCoerceToType(); |
| 2212 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2213 | return (vectorTy->getBitWidth() > 128); |
| 2214 | } |
| 2215 | return false; |
| 2216 | } |
| 2217 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2218 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2219 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2220 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2221 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2222 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2223 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2224 | |
| 2225 | bool has64BitPointers() const { |
| 2226 | return Has64BitPointers; |
| 2227 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2228 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2229 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2230 | bool asReturnValue) const override { |
| 2231 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2232 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2233 | bool isSwiftErrorInRegister() const override { |
| 2234 | return true; |
| 2235 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2236 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2237 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2238 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2239 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2240 | public: |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2241 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2242 | : SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2243 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2244 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2245 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2246 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2247 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2248 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2249 | |
| 2250 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2251 | // FIXME: Assumes vectorcall is in use. |
| 2252 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2253 | } |
| 2254 | |
| 2255 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2256 | uint64_t NumMembers) const override { |
| 2257 | // FIXME: Assumes vectorcall is in use. |
| 2258 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2259 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2260 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2261 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2262 | bool asReturnValue) const override { |
| 2263 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2264 | } |
| 2265 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2266 | bool isSwiftErrorInRegister() const override { |
| 2267 | return true; |
| 2268 | } |
| 2269 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2270 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2271 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2272 | bool IsVectorCall, bool IsRegCall) const; |
| 2273 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2274 | const ABIArgInfo ¤t) const; |
| 2275 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2276 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2277 | |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2278 | X86AVXABILevel AVXLevel; |
| 2279 | |
| 2280 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2281 | }; |
| 2282 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2283 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2284 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2285 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2286 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2287 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2288 | const X86_64ABIInfo &getABIInfo() const { |
| 2289 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2290 | } |
| 2291 | |
Akira Hatanaka | 65bb3f9 | 2019-03-21 19:59:49 +0000 | [diff] [blame] | 2292 | /// Disable tail call on x86-64. The epilogue code before the tail jump blocks |
| 2293 | /// the autoreleaseRV/retainRV optimization. |
| 2294 | bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override { |
| 2295 | return true; |
| 2296 | } |
| 2297 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2298 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2299 | return 7; |
| 2300 | } |
| 2301 | |
| 2302 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2303 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2304 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2305 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2306 | // 0-15 are the 16 integer registers. |
| 2307 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2308 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2309 | return false; |
| 2310 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2311 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2312 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2313 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2314 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2315 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2316 | } |
| 2317 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2318 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2319 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2320 | // The default CC on x86-64 sets %al to the number of SSA |
| 2321 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2322 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2323 | // that when AVX types are involved: the ABI explicitly states it is |
| 2324 | // undefined, and it doesn't work in practice because of how the ABI |
| 2325 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2326 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2327 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2328 | for (CallArgList::const_iterator |
| 2329 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2330 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2331 | HasAVXType = true; |
| 2332 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2333 | } |
| 2334 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2335 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2336 | if (!HasAVXType) |
| 2337 | return true; |
| 2338 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2339 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2340 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2343 | llvm::Constant * |
| 2344 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2345 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2346 | (0x06 << 8) | // .+0x08 |
| 2347 | ('v' << 16) | |
| 2348 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2349 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2350 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2351 | |
| 2352 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2353 | CodeGen::CodeGenModule &CGM) const override { |
| 2354 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2355 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2356 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2357 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2358 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2359 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2360 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2361 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2362 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2363 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2364 | } |
| 2365 | } |
| 2366 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2367 | }; |
| 2368 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2369 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2370 | // If the argument does not end in .lib, automatically add the suffix. |
| 2371 | // If the argument contains a space, enclose it in quotes. |
| 2372 | // This matches the behavior of MSVC. |
| 2373 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2374 | std::string ArgStr = Quote ? "\"" : ""; |
| 2375 | ArgStr += Lib; |
Martin Storsjo | 3cd67c9 | 2018-10-10 09:01:00 +0000 | [diff] [blame] | 2376 | if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2377 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2378 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2379 | return ArgStr; |
| 2380 | } |
| 2381 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2382 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2383 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2384 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2385 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2386 | unsigned NumRegisterParameters) |
| 2387 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2388 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2389 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2390 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2391 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2392 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2393 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2394 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2395 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2396 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2397 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2398 | |
| 2399 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2400 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2401 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2402 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2403 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2404 | }; |
| 2405 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2406 | static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2407 | CodeGen::CodeGenModule &CGM) { |
| 2408 | if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2409 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2410 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2411 | Fn->addFnAttr("stack-probe-size", |
| 2412 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2413 | if (CGM.getCodeGenOpts().NoStackArgProbe) |
| 2414 | Fn->addFnAttr("no-stack-arg-probe"); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2415 | } |
| 2416 | } |
| 2417 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2418 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2419 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2420 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2421 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2422 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2423 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2426 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2427 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2428 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2429 | X86AVXABILevel AVXLevel) |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2430 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT, AVXLevel)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2431 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2432 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2433 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2434 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2435 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2436 | return 7; |
| 2437 | } |
| 2438 | |
| 2439 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2440 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2441 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2442 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2443 | // 0-15 are the 16 integer registers. |
| 2444 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2445 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2446 | return false; |
| 2447 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2448 | |
| 2449 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2450 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2451 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2452 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2453 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2454 | |
| 2455 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2456 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2457 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2458 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2459 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2460 | }; |
| 2461 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2462 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2463 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2464 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2465 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2466 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2467 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2468 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2469 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2470 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2471 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2472 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2473 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2474 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2475 | } |
| 2476 | } |
| 2477 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2478 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2479 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2480 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2481 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2482 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2483 | Class &Hi) const { |
| 2484 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2485 | // |
| 2486 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2487 | // memory. |
| 2488 | // |
| 2489 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2490 | // memory. |
| 2491 | // |
| 2492 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2493 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2494 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2495 | // ABI working for processors that don't support the __m256 type. |
| 2496 | // |
| 2497 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2498 | // |
| 2499 | // Some of these are enforced by the merging logic. Others can arise |
| 2500 | // only with unions; for example: |
| 2501 | // union { _Complex double; unsigned; } |
| 2502 | // |
| 2503 | // Note that clauses (b) and (c) were added in 0.98. |
| 2504 | // |
| 2505 | if (Hi == Memory) |
| 2506 | Lo = Memory; |
| 2507 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2508 | Lo = Memory; |
| 2509 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2510 | Lo = Memory; |
| 2511 | if (Hi == SSEUp && Lo != SSE) |
| 2512 | Hi = SSE; |
| 2513 | } |
| 2514 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2515 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2516 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2517 | // classified recursively so that always two fields are |
| 2518 | // considered. The resulting class is calculated according to |
| 2519 | // the classes of the fields in the eightbyte: |
| 2520 | // |
| 2521 | // (a) If both classes are equal, this is the resulting class. |
| 2522 | // |
| 2523 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2524 | // the other class. |
| 2525 | // |
| 2526 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2527 | // class. |
| 2528 | // |
| 2529 | // (d) If one of the classes is INTEGER, the result is the |
| 2530 | // INTEGER. |
| 2531 | // |
| 2532 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2533 | // MEMORY is used as class. |
| 2534 | // |
| 2535 | // (f) Otherwise class SSE is used. |
| 2536 | |
| 2537 | // Accum should never be memory (we should have returned) or |
| 2538 | // ComplexX87 (because this cannot be passed in a structure). |
| 2539 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2540 | "Invalid accumulated classification during merge."); |
| 2541 | if (Accum == Field || Field == NoClass) |
| 2542 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2543 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2544 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2545 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2546 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2547 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2548 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2549 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2550 | Accum == X87 || Accum == X87Up) |
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 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2555 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2556 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2557 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2558 | // Class pairs with appropriate constructor methods for the various |
| 2559 | // situations. |
| 2560 | |
| 2561 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2562 | // shouldn't be passed in registers for example, so there is no chance they |
| 2563 | // can straddle an eightbyte. Verify & simplify. |
| 2564 | |
| 2565 | Lo = Hi = NoClass; |
| 2566 | |
| 2567 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2568 | Current = Memory; |
| 2569 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2570 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2571 | BuiltinType::Kind k = BT->getKind(); |
| 2572 | |
| 2573 | if (k == BuiltinType::Void) { |
| 2574 | Current = NoClass; |
| 2575 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2576 | Lo = Integer; |
| 2577 | Hi = Integer; |
| 2578 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2579 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2580 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2581 | Current = SSE; |
| 2582 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2583 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2584 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2585 | Lo = SSE; |
| 2586 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2587 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2588 | Lo = X87; |
| 2589 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2590 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2591 | Current = SSE; |
| 2592 | } else |
| 2593 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2594 | } |
| 2595 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2596 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2597 | return; |
| 2598 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2599 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2600 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2601 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2602 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2603 | return; |
| 2604 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2606 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2607 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2608 | return; |
| 2609 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2610 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2611 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2612 | if (Ty->isMemberFunctionPointerType()) { |
| 2613 | if (Has64BitPointers) { |
| 2614 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2615 | // Lo and Hi now. |
| 2616 | Lo = Hi = Integer; |
| 2617 | } else { |
| 2618 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2619 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2620 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2621 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2622 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2623 | Lo = Hi = Integer; |
| 2624 | } else { |
| 2625 | Current = Integer; |
| 2626 | } |
| 2627 | } |
| 2628 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2629 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2630 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2631 | return; |
| 2632 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2634 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2635 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2636 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2637 | // gcc passes the following as integer: |
| 2638 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2639 | // 2 bytes - <2 x char>, <1 x short> |
| 2640 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2641 | Current = Integer; |
| 2642 | |
| 2643 | // If this type crosses an eightbyte boundary, it should be |
| 2644 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2645 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2646 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2647 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2648 | Hi = Lo; |
| 2649 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2650 | QualType ElementType = VT->getElementType(); |
| 2651 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2652 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2653 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2654 | return; |
| 2655 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2656 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2657 | // pass them as integer. For platforms where clang is the de facto |
| 2658 | // platform compiler, we must continue to use integer. |
| 2659 | if (!classifyIntegerMMXAsSSE() && |
| 2660 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2661 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2662 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2663 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2664 | Current = Integer; |
| 2665 | else |
| 2666 | Current = SSE; |
| 2667 | |
| 2668 | // If this type crosses an eightbyte boundary, it should be |
| 2669 | // split. |
| 2670 | if (OffsetBase && OffsetBase != 64) |
| 2671 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2672 | } else if (Size == 128 || |
| 2673 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 2674 | QualType ElementType = VT->getElementType(); |
| 2675 | |
| 2676 | // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :( |
| 2677 | if (passInt128VectorsInMem() && Size != 128 && |
| 2678 | (ElementType->isSpecificBuiltinType(BuiltinType::Int128) || |
| 2679 | ElementType->isSpecificBuiltinType(BuiltinType::UInt128))) |
| 2680 | return; |
| 2681 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2682 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2683 | // least significant one belongs to class SSE and all the others to class |
| 2684 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2685 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2686 | // This design isn't correct for 256-bits, but since there're no cases |
| 2687 | // where the upper parts would need to be inspected, avoid adding |
| 2688 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2689 | // |
| 2690 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2691 | // registers if they are "named", i.e. not part of the "..." of a |
| 2692 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2693 | // |
| 2694 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2695 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2696 | Lo = SSE; |
| 2697 | Hi = SSEUp; |
| 2698 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2699 | return; |
| 2700 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2702 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2703 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2705 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2706 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2707 | if (Size <= 64) |
| 2708 | Current = Integer; |
| 2709 | else if (Size <= 128) |
| 2710 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2711 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2712 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2713 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2714 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2715 | } else if (ET == getContext().LongDoubleTy) { |
| 2716 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2717 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2718 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2719 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2720 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2721 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2722 | Lo = Hi = SSE; |
| 2723 | else |
| 2724 | llvm_unreachable("unexpected long double representation!"); |
| 2725 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2726 | |
| 2727 | // If this complex type crosses an eightbyte boundary then it |
| 2728 | // should be split. |
| 2729 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2730 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2731 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2732 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2733 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2734 | return; |
| 2735 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2736 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2737 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2738 | // Arrays are treated like structures. |
| 2739 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2740 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2741 | |
| 2742 | // 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] | 2743 | // than eight eightbytes, ..., it has class MEMORY. |
| 2744 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2745 | return; |
| 2746 | |
| 2747 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2748 | // fields, it has class MEMORY. |
| 2749 | // |
| 2750 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2751 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2752 | return; |
| 2753 | |
| 2754 | // Otherwise implement simplified merge. We could be smarter about |
| 2755 | // this, but it isn't worth it and would be harder to verify. |
| 2756 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2757 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2758 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2759 | |
| 2760 | // The only case a 256-bit wide vector could be used is when the array |
| 2761 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2762 | // 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] | 2763 | // |
| 2764 | if (Size > 128 && |
| 2765 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2766 | return; |
| 2767 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2768 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2769 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2770 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2771 | Lo = merge(Lo, FieldLo); |
| 2772 | Hi = merge(Hi, FieldHi); |
| 2773 | if (Lo == Memory || Hi == Memory) |
| 2774 | break; |
| 2775 | } |
| 2776 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2777 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2778 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2779 | return; |
| 2780 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2781 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2782 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2783 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2784 | |
| 2785 | // 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] | 2786 | // than eight eightbytes, ..., it has class MEMORY. |
| 2787 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2788 | return; |
| 2789 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2790 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2791 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2792 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2793 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2794 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2795 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2796 | const RecordDecl *RD = RT->getDecl(); |
| 2797 | |
| 2798 | // Assume variable sized types are passed in memory. |
| 2799 | if (RD->hasFlexibleArrayMember()) |
| 2800 | return; |
| 2801 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2802 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2803 | |
| 2804 | // Reset Lo class, this will be recomputed. |
| 2805 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2806 | |
| 2807 | // If this is a C++ record, classify the bases first. |
| 2808 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2809 | for (const auto &I : CXXRD->bases()) { |
| 2810 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2811 | "Unexpected base class!"); |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 2812 | const auto *Base = |
| 2813 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2814 | |
| 2815 | // Classify this field. |
| 2816 | // |
| 2817 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2818 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2819 | // initialized to class NO_CLASS. |
| 2820 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2821 | uint64_t Offset = |
| 2822 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2823 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2824 | Lo = merge(Lo, FieldLo); |
| 2825 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2826 | if (Lo == Memory || Hi == Memory) { |
| 2827 | postMerge(Size, Lo, Hi); |
| 2828 | return; |
| 2829 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2834 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2835 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2836 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2837 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2838 | bool BitField = i->isBitField(); |
| 2839 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2840 | // Ignore padding bit-fields. |
| 2841 | if (BitField && i->isUnnamedBitfield()) |
| 2842 | continue; |
| 2843 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2844 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2845 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2846 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2847 | // The only case a 256-bit wide vector could be used is when the struct |
| 2848 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2849 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2850 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2851 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2852 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2853 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2854 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2855 | return; |
| 2856 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2857 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2858 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2859 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2860 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2861 | return; |
| 2862 | } |
| 2863 | |
| 2864 | // Classify this field. |
| 2865 | // |
| 2866 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2867 | // exceeds a single eightbyte, each is classified |
| 2868 | // separately. Each eightbyte gets initialized to class |
| 2869 | // NO_CLASS. |
| 2870 | Class FieldLo, FieldHi; |
| 2871 | |
| 2872 | // Bit-fields require special handling, they do not force the |
| 2873 | // structure to be passed in memory even if unaligned, and |
| 2874 | // therefore they can straddle an eightbyte. |
| 2875 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2876 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2877 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2878 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2879 | |
| 2880 | uint64_t EB_Lo = Offset / 64; |
| 2881 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2882 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2883 | if (EB_Lo) { |
| 2884 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2885 | FieldLo = NoClass; |
| 2886 | FieldHi = Integer; |
| 2887 | } else { |
| 2888 | FieldLo = Integer; |
| 2889 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2890 | } |
| 2891 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2892 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2893 | Lo = merge(Lo, FieldLo); |
| 2894 | Hi = merge(Hi, FieldHi); |
| 2895 | if (Lo == Memory || Hi == Memory) |
| 2896 | break; |
| 2897 | } |
| 2898 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2899 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2903 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2904 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2905 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2906 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2907 | // Treat an enum type as its underlying type. |
| 2908 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2909 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2910 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2911 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2912 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2915 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2918 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2919 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2920 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2921 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2922 | if (Size <= 64 || Size > LargestVector) |
| 2923 | return true; |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 2924 | QualType EltTy = VecTy->getElementType(); |
| 2925 | if (passInt128VectorsInMem() && |
| 2926 | (EltTy->isSpecificBuiltinType(BuiltinType::Int128) || |
| 2927 | EltTy->isSpecificBuiltinType(BuiltinType::UInt128))) |
| 2928 | return true; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | return false; |
| 2932 | } |
| 2933 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2934 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2935 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2936 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2937 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2938 | // |
| 2939 | // This assumption is optimistic, as there could be free registers available |
| 2940 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2941 | // the argument in the free register. This does not seem to happen currently, |
| 2942 | // but this code would be much safer if we could mark the argument with |
| 2943 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2944 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2945 | // Treat an enum type as its underlying type. |
| 2946 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2947 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2948 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2949 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2950 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2951 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2952 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2953 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2954 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2955 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2956 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2957 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2958 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2959 | |
| 2960 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2961 | // is important for good codegen. |
| 2962 | // |
| 2963 | // We do this by coercing the value into a scalar type which the backend can |
| 2964 | // handle naturally (i.e., without using byval). |
| 2965 | // |
| 2966 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2967 | // free integer registers. Doing this when there are free integer registers |
| 2968 | // would require more care, as we would have to ensure that the coerced value |
| 2969 | // did not claim the unused register. That would require either reording the |
| 2970 | // arguments to the function (so that any subsequent inreg values came first), |
| 2971 | // or only doing this optimization when there were no following arguments that |
| 2972 | // might be inreg. |
| 2973 | // |
| 2974 | // We currently expect it to be rare (particularly in well written code) for |
| 2975 | // arguments to be passed on the stack when there are still free integer |
| 2976 | // registers available (this would typically imply large structs being passed |
| 2977 | // by value), so this seems like a fair tradeoff for now. |
| 2978 | // |
| 2979 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2980 | // attributes. See PR12193. |
| 2981 | if (freeIntRegs == 0) { |
| 2982 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2983 | |
| 2984 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2985 | // type, which will end up on the stack (with alignment 8). |
| 2986 | if (Align == 8 && Size <= 64) |
| 2987 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2988 | Size)); |
| 2989 | } |
| 2990 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2991 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2994 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2995 | /// 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] | 2996 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2997 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2998 | // vectors; strip them off if present. |
| 2999 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 3000 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3001 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 3002 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 3003 | if (isa<llvm::VectorType>(IRType)) { |
| 3004 | // Don't pass vXi128 vectors in their native type, the backend can't |
| 3005 | // legalize them. |
| 3006 | if (passInt128VectorsInMem() && |
| 3007 | IRType->getVectorElementType()->isIntegerTy(128)) { |
| 3008 | // Use a vXi64 vector. |
| 3009 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3010 | return llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), |
| 3011 | Size / 64); |
| 3012 | } |
| 3013 | |
| 3014 | return IRType; |
| 3015 | } |
| 3016 | |
| 3017 | if (IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 3018 | return IRType; |
| 3019 | |
| 3020 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 3021 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 3022 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 3023 | |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 3024 | |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 3025 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 3026 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 3027 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3030 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 3031 | /// is known to either be off the end of the specified type or being in |
| 3032 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 3033 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 3034 | /// classification that put one of the two halves in the INTEGER class. |
| 3035 | /// |
| 3036 | /// It is conservatively correct to return false. |
| 3037 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 3038 | unsigned EndBit, ASTContext &Context) { |
| 3039 | // If the bytes being queried are off the end of the type, there is no user |
| 3040 | // data hiding here. This handles analysis of builtins, vectors and other |
| 3041 | // types that don't contain interesting padding. |
| 3042 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 3043 | if (TySize <= StartBit) |
| 3044 | return true; |
| 3045 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3046 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3047 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 3048 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 3049 | |
| 3050 | // Check each element to see if the element overlaps with the queried range. |
| 3051 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3052 | // If the element is after the span we care about, then we're done.. |
| 3053 | unsigned EltOffset = i*EltSize; |
| 3054 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3055 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3056 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 3057 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3058 | EndBit-EltOffset, Context)) |
| 3059 | return false; |
| 3060 | } |
| 3061 | // If it overlaps no elements, then it is safe to process as padding. |
| 3062 | return true; |
| 3063 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3064 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3065 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3066 | const RecordDecl *RD = RT->getDecl(); |
| 3067 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3069 | // If this is a C++ record, check the bases first. |
| 3070 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3071 | for (const auto &I : CXXRD->bases()) { |
| 3072 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3073 | "Unexpected base class!"); |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 3074 | const auto *Base = |
| 3075 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3076 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3077 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3078 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3079 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3080 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3081 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3082 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3083 | EndBit-BaseOffset, Context)) |
| 3084 | return false; |
| 3085 | } |
| 3086 | } |
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 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3089 | // this could be sped up a lot by being smarter about queried fields, |
| 3090 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3091 | // much. |
| 3092 | unsigned idx = 0; |
| 3093 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3094 | i != e; ++i, ++idx) { |
| 3095 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3096 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3097 | // If we found a field after the region we care about, then we're done. |
| 3098 | if (FieldOffset >= EndBit) break; |
| 3099 | |
| 3100 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3101 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3102 | Context)) |
| 3103 | return false; |
| 3104 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3105 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3106 | // If nothing in this record overlapped the area of interest, then we're |
| 3107 | // clean. |
| 3108 | return true; |
| 3109 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3111 | return false; |
| 3112 | } |
| 3113 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3114 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3115 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3116 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3117 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3118 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3119 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3120 | // Base case if we find a float. |
| 3121 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3122 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3123 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3124 | // 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] | 3125 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3126 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3127 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3128 | IROffset -= SL->getElementOffset(Elt); |
| 3129 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3130 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3132 | // 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] | 3133 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3134 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3135 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3136 | IROffset -= IROffset/EltSize*EltSize; |
| 3137 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3138 | } |
| 3139 | |
| 3140 | return false; |
| 3141 | } |
| 3142 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3143 | |
| 3144 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3145 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3146 | llvm::Type *X86_64ABIInfo:: |
| 3147 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3148 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3149 | // 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] | 3150 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3151 | // structs that contain 3 floats. |
| 3152 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3153 | SourceOffset*8+64, getContext())) |
| 3154 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3155 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3156 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3157 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3158 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3159 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3160 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3161 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3162 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3163 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3164 | } |
| 3165 | |
| 3166 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3167 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3168 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3169 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3170 | /// 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] | 3171 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3172 | /// etc). |
| 3173 | /// |
| 3174 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3175 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3176 | /// the 8-byte value references. PrefType may be null. |
| 3177 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3178 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3179 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3180 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3181 | llvm::Type *X86_64ABIInfo:: |
| 3182 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3183 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3184 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3185 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3186 | if (IROffset == 0) { |
| 3187 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3188 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3189 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3190 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3191 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3192 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3193 | // goodness in the source type is just tail padding. This is allowed to |
| 3194 | // kick in for struct {double,int} on the int, but not on |
| 3195 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3196 | // have to do this analysis on the source type because we can't depend on |
| 3197 | // unions being lowered a specific way etc. |
| 3198 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3199 | IRType->isIntegerTy(32) || |
| 3200 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3201 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3202 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3203 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3204 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3205 | SourceOffset*8+64, getContext())) |
| 3206 | return IRType; |
| 3207 | } |
| 3208 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3209 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3210 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3211 | // 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] | 3212 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3213 | if (IROffset < SL->getSizeInBytes()) { |
| 3214 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3215 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3216 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3217 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3218 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3219 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3220 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3221 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3222 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3223 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3224 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3225 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3226 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3227 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3228 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3229 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3230 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3231 | // 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] | 3232 | unsigned TySizeInBytes = |
| 3233 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3234 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3235 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
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 | // It is always safe to classify this as an integer type up to i64 that |
| 3238 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3239 | return llvm::IntegerType::get(getVMContext(), |
| 3240 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3243 | |
| 3244 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3245 | /// be used as elements of a two register pair to pass or return, return a |
| 3246 | /// first class aggregate to represent them. For example, if the low part of |
| 3247 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3248 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3249 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3250 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3251 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3252 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3253 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3254 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3255 | // the second element at offset 8. Check for this: |
| 3256 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3257 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3258 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3259 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3260 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3261 | // To handle this, we have to increase the size of the low part so that the |
| 3262 | // second element will start at an 8 byte offset. We can't increase the size |
| 3263 | // of the second element because it might make us access off the end of the |
| 3264 | // struct. |
| 3265 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3266 | // There are usually two sorts of types the ABI generation code can produce |
| 3267 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3268 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3269 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3270 | // Promote these to a larger type. |
| 3271 | if (Lo->isFloatTy()) |
| 3272 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3273 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3274 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3275 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3276 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3277 | } |
| 3278 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3279 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3280 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3281 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3282 | // Verify that the second element is at an 8-byte offset. |
| 3283 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3284 | "Invalid x86-64 argument pair!"); |
| 3285 | return Result; |
| 3286 | } |
| 3287 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3288 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3289 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3290 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3291 | // classification algorithm. |
| 3292 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3293 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3294 | |
| 3295 | // Check some invariants. |
| 3296 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3297 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3298 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3299 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3300 | switch (Lo) { |
| 3301 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3302 | if (Hi == NoClass) |
| 3303 | return ABIArgInfo::getIgnore(); |
| 3304 | // If the low part is just padding, it takes no register, leave ResType |
| 3305 | // null. |
| 3306 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3307 | "Unknown missing lo part"); |
| 3308 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3309 | |
| 3310 | case SSEUp: |
| 3311 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3312 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3313 | |
| 3314 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3315 | // hidden argument. |
| 3316 | case Memory: |
| 3317 | return getIndirectReturnResult(RetTy); |
| 3318 | |
| 3319 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3320 | // available register of the sequence %rax, %rdx is used. |
| 3321 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3322 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3323 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3324 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3325 | // so that the parameter gets the right LLVM IR attributes. |
| 3326 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3327 | // Treat an enum type as its underlying type. |
| 3328 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3329 | RetTy = EnumTy->getDecl()->getIntegerType(); |
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 (RetTy->isIntegralOrEnumerationType() && |
| 3332 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3333 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3334 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3335 | break; |
| 3336 | |
| 3337 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3338 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3339 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3340 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3341 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3342 | |
| 3343 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3344 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3345 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3346 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3347 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3348 | |
| 3349 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3350 | // part of the value is returned in %st0 and the imaginary part in |
| 3351 | // %st1. |
| 3352 | case ComplexX87: |
| 3353 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3354 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3355 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3356 | break; |
| 3357 | } |
| 3358 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3359 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3360 | switch (Hi) { |
| 3361 | // Memory was handled previously and X87 should |
| 3362 | // never occur as a hi class. |
| 3363 | case Memory: |
| 3364 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3365 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3366 | |
| 3367 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3368 | case NoClass: |
| 3369 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3370 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3371 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3372 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3373 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3374 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3375 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3376 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3377 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3378 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3379 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3380 | break; |
| 3381 | |
| 3382 | // 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] | 3383 | // is passed in the next available eightbyte chunk if the last used |
| 3384 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3385 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3386 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3387 | case SSEUp: |
| 3388 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3389 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3390 | break; |
| 3391 | |
| 3392 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3393 | // returned together with the previous X87 value in %st0. |
| 3394 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3395 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3396 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3397 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3398 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3399 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3400 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3401 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3402 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3403 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3404 | break; |
| 3405 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3406 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3407 | // 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] | 3408 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3409 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3410 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3411 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3412 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3413 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3416 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3417 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3418 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3419 | const |
| 3420 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3421 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3422 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3423 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3424 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3425 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3426 | // Check some invariants. |
| 3427 | // FIXME: Enforce these by construction. |
| 3428 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3429 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3430 | |
| 3431 | neededInt = 0; |
| 3432 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3433 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3434 | switch (Lo) { |
| 3435 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3436 | if (Hi == NoClass) |
| 3437 | return ABIArgInfo::getIgnore(); |
| 3438 | // If the low part is just padding, it takes no register, leave ResType |
| 3439 | // null. |
| 3440 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3441 | "Unknown missing lo part"); |
| 3442 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3443 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3444 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3445 | // on the stack. |
| 3446 | case Memory: |
| 3447 | |
| 3448 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3449 | // COMPLEX_X87, it is passed in memory. |
| 3450 | case X87: |
| 3451 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3452 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3453 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3454 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3455 | |
| 3456 | case SSEUp: |
| 3457 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3458 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3459 | |
| 3460 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3461 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3462 | // and %r9 is used. |
| 3463 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3464 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3465 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3466 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3467 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3468 | |
| 3469 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3470 | // so that the parameter gets the right LLVM IR attributes. |
| 3471 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3472 | // Treat an enum type as its underlying type. |
| 3473 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3474 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3475 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3476 | if (Ty->isIntegralOrEnumerationType() && |
| 3477 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3478 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3479 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3480 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3481 | break; |
| 3482 | |
| 3483 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3484 | // available SSE register is used, the registers are taken in the |
| 3485 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3486 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3487 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3488 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3489 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3490 | break; |
| 3491 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3492 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3493 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3494 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3495 | switch (Hi) { |
| 3496 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3497 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3498 | // which is passed in memory. |
| 3499 | case Memory: |
| 3500 | case X87: |
| 3501 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3502 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3503 | |
| 3504 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3505 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3506 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3507 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3508 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3509 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3510 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3511 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3512 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3513 | break; |
| 3514 | |
| 3515 | // X87Up generally doesn't occur here (long double is passed in |
| 3516 | // memory), except in situations involving unions. |
| 3517 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3518 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3519 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3520 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3521 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3522 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3523 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3524 | ++neededSSE; |
| 3525 | break; |
| 3526 | |
| 3527 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3528 | // 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] | 3529 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3530 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3531 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3532 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3533 | break; |
| 3534 | } |
| 3535 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3536 | // If a high part was specified, merge it together with the low part. It is |
| 3537 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3538 | // first class struct aggregate with the high and low part: {low, high} |
| 3539 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3540 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3541 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3542 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3545 | ABIArgInfo |
| 3546 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3547 | unsigned &NeededSSE) const { |
| 3548 | auto RT = Ty->getAs<RecordType>(); |
| 3549 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3550 | |
| 3551 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3552 | return getIndirectReturnResult(Ty); |
| 3553 | |
| 3554 | // Sum up bases |
| 3555 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3556 | if (CXXRD->isDynamicClass()) { |
| 3557 | NeededInt = NeededSSE = 0; |
| 3558 | return getIndirectReturnResult(Ty); |
| 3559 | } |
| 3560 | |
| 3561 | for (const auto &I : CXXRD->bases()) |
| 3562 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3563 | .isIndirect()) { |
| 3564 | NeededInt = NeededSSE = 0; |
| 3565 | return getIndirectReturnResult(Ty); |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | // Sum up members |
| 3570 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3571 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3572 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3573 | .isIndirect()) { |
| 3574 | NeededInt = NeededSSE = 0; |
| 3575 | return getIndirectReturnResult(Ty); |
| 3576 | } |
| 3577 | } else { |
| 3578 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3579 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3580 | LocalNeededSSE, true) |
| 3581 | .isIndirect()) { |
| 3582 | NeededInt = NeededSSE = 0; |
| 3583 | return getIndirectReturnResult(Ty); |
| 3584 | } |
| 3585 | NeededInt += LocalNeededInt; |
| 3586 | NeededSSE += LocalNeededSSE; |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | return ABIArgInfo::getDirect(); |
| 3591 | } |
| 3592 | |
| 3593 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3594 | unsigned &NeededInt, |
| 3595 | unsigned &NeededSSE) const { |
| 3596 | |
| 3597 | NeededInt = 0; |
| 3598 | NeededSSE = 0; |
| 3599 | |
| 3600 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3601 | } |
| 3602 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3603 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3604 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3605 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3606 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3607 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3608 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3609 | if (CallingConv == llvm::CallingConv::Win64) { |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 3610 | WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel); |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3611 | Win64ABIInfo.computeInfo(FI); |
| 3612 | return; |
| 3613 | } |
| 3614 | |
| 3615 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3616 | |
| 3617 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3618 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3619 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3620 | unsigned NeededInt, NeededSSE; |
| 3621 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 3622 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3623 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3624 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3625 | FI.getReturnInfo() = |
| 3626 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3627 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3628 | FreeIntRegs -= NeededInt; |
| 3629 | FreeSSERegs -= NeededSSE; |
| 3630 | } else { |
| 3631 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3632 | } |
| 3633 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3634 | // Complex Long Double Type is passed in Memory when Regcall |
| 3635 | // calling convention is used. |
| 3636 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3637 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3638 | getContext().LongDoubleTy) |
| 3639 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3640 | } else |
| 3641 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3642 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3643 | |
| 3644 | // If the return value is indirect, then the hidden argument is consuming one |
| 3645 | // integer register. |
| 3646 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3647 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3648 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3649 | // The chain argument effectively gives us another free register. |
| 3650 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3651 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3652 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3653 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3654 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3655 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3656 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3657 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3658 | it != ie; ++it, ++ArgNo) { |
| 3659 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3660 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3661 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3662 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3663 | else |
| 3664 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3665 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3666 | |
| 3667 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3668 | // eightbyte of an argument, the whole argument is passed on the |
| 3669 | // stack. If registers have already been assigned for some |
| 3670 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3671 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3672 | FreeIntRegs -= NeededInt; |
| 3673 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3674 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3675 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3676 | } |
| 3677 | } |
| 3678 | } |
| 3679 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3680 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3681 | Address VAListAddr, QualType Ty) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3682 | Address overflow_arg_area_p = |
| 3683 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3684 | llvm::Value *overflow_arg_area = |
| 3685 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3686 | |
| 3687 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3688 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3689 | // It isn't stated explicitly in the standard, but in practice we use |
| 3690 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3691 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3692 | if (Align > CharUnits::fromQuantity(8)) { |
| 3693 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3694 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | // 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] | 3698 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3699 | llvm::Value *Res = |
| 3700 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3701 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3702 | |
| 3703 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3704 | // l->overflow_arg_area + sizeof(type). |
| 3705 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3706 | // an 8 byte boundary. |
| 3707 | |
| 3708 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3709 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3710 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3711 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3712 | "overflow_arg_area.next"); |
| 3713 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3714 | |
| 3715 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3716 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3719 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3720 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3721 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3722 | // struct { |
| 3723 | // i32 gp_offset; |
| 3724 | // i32 fp_offset; |
| 3725 | // i8* overflow_arg_area; |
| 3726 | // i8* reg_save_area; |
| 3727 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3728 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3729 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3730 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3731 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3732 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3733 | |
| 3734 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3735 | // in the registers. If not go to step 7. |
| 3736 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3737 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3738 | |
| 3739 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3740 | // general purpose registers needed to pass type and num_fp to hold |
| 3741 | // the number of floating point registers needed. |
| 3742 | |
| 3743 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3744 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3745 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3746 | // |
| 3747 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3748 | // register save space). |
| 3749 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3750 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3751 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3752 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3753 | if (neededInt) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3754 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3755 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3756 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3757 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | if (neededSSE) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3761 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3762 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3763 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3764 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3765 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3766 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3767 | } |
| 3768 | |
| 3769 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3770 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3771 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3772 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3773 | |
| 3774 | // Emit code to load the value if it was passed in registers. |
| 3775 | |
| 3776 | CGF.EmitBlock(InRegBlock); |
| 3777 | |
| 3778 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3779 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3780 | // copying to a temporary location in case the parameter is passed |
| 3781 | // in different register classes or requires an alignment greater |
| 3782 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3783 | // |
| 3784 | // FIXME: This really results in shameful code when we end up needing to |
| 3785 | // collect arguments from different places; often what should result in a |
| 3786 | // simple assembling of a structure from scattered addresses has many more |
| 3787 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3788 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3789 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3790 | CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3791 | |
| 3792 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3793 | if (neededInt && neededSSE) { |
| 3794 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3795 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3796 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3797 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3798 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3799 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3800 | llvm::Type *TyLo = ST->getElementType(0); |
| 3801 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3802 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3803 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3804 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3805 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3806 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3807 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3808 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3809 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3810 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3811 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3812 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3813 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3814 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3815 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3816 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3817 | |
| 3818 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3819 | V = CGF.Builder.CreateAlignedLoad( |
| 3820 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3821 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3822 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3823 | |
| 3824 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3825 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3826 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3827 | CharUnits::fromQuantity(8)); |
| 3828 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3829 | |
| 3830 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3831 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3832 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3833 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3834 | CharUnits TyAlign = SizeAlign.second; |
| 3835 | |
| 3836 | // Copy into a temporary if the type is more aligned than the |
| 3837 | // register save area. |
| 3838 | if (TyAlign.getQuantity() > 8) { |
| 3839 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3840 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3841 | RegAddr = Tmp; |
| 3842 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3843 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3844 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3845 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3846 | CharUnits::fromQuantity(16)); |
| 3847 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3848 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3849 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3850 | // SSE registers are spaced 16 bytes apart in the register save |
| 3851 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3852 | // The ABI isn't explicit about this, but it seems reasonable |
| 3853 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3854 | // naturally 16-byte aligned and the prologue is expected to store |
| 3855 | // all the SSE registers to the RSA. |
| 3856 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3857 | CharUnits::fromQuantity(16)); |
| 3858 | Address RegAddrHi = |
| 3859 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3860 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3861 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3862 | ? AI.getCoerceToType() |
| 3863 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3864 | llvm::Value *V; |
| 3865 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3866 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3867 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3868 | RegAddrLo, ST->getStructElementType(0))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3869 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3870 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3871 | RegAddrHi, ST->getStructElementType(1))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3872 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3873 | |
| 3874 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3878 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3879 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3880 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3881 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3882 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3883 | gp_offset_p); |
| 3884 | } |
| 3885 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3886 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3887 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3888 | fp_offset_p); |
| 3889 | } |
| 3890 | CGF.EmitBranch(ContBlock); |
| 3891 | |
| 3892 | // Emit code to load the value if it was passed in memory. |
| 3893 | |
| 3894 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3895 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3896 | |
| 3897 | // Return the appropriate result. |
| 3898 | |
| 3899 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3900 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3901 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3902 | return ResAddr; |
| 3903 | } |
| 3904 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3905 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3906 | QualType Ty) const { |
| 3907 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3908 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3909 | CharUnits::fromQuantity(8), |
| 3910 | /*allowHigherAlign*/ false); |
| 3911 | } |
| 3912 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3913 | ABIArgInfo |
| 3914 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3915 | const ABIArgInfo ¤t) const { |
| 3916 | // Assumes vectorCall calling convention. |
| 3917 | const Type *Base = nullptr; |
| 3918 | uint64_t NumElts = 0; |
| 3919 | |
| 3920 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3921 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3922 | FreeSSERegs -= NumElts; |
| 3923 | return getDirectX86Hva(); |
| 3924 | } |
| 3925 | return current; |
| 3926 | } |
| 3927 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3928 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3929 | bool IsReturnType, bool IsVectorCall, |
| 3930 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3931 | |
| 3932 | if (Ty->isVoidType()) |
| 3933 | return ABIArgInfo::getIgnore(); |
| 3934 | |
| 3935 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3936 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3937 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3938 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3939 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3940 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3941 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3942 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3943 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3944 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3945 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3946 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3950 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3951 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3952 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3953 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3954 | const Type *Base = nullptr; |
| 3955 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3956 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3957 | // other targets. |
| 3958 | if ((IsVectorCall || IsRegCall) && |
| 3959 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3960 | if (IsRegCall) { |
| 3961 | if (FreeSSERegs >= NumElts) { |
| 3962 | FreeSSERegs -= NumElts; |
| 3963 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3964 | return ABIArgInfo::getDirect(); |
| 3965 | return ABIArgInfo::getExpand(); |
| 3966 | } |
| 3967 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3968 | } else if (IsVectorCall) { |
| 3969 | if (FreeSSERegs >= NumElts && |
| 3970 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3971 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3972 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3973 | } else if (IsReturnType) { |
| 3974 | return ABIArgInfo::getExpand(); |
| 3975 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3976 | // HVAs are delayed and reclassified in the 2nd step. |
| 3977 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3978 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3979 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3982 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3983 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3984 | // directly. |
| 3985 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3986 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3987 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3990 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3991 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3992 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3993 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3994 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3995 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3996 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3997 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Reid Kleckner | 08f64e9 | 2018-10-31 17:43:55 +0000 | [diff] [blame] | 4000 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4001 | switch (BT->getKind()) { |
| 4002 | case BuiltinType::Bool: |
| 4003 | // Bool type is always extended to the ABI, other builtin types are not |
| 4004 | // extended. |
| 4005 | return ABIArgInfo::getExtend(Ty); |
| 4006 | |
| 4007 | case BuiltinType::LongDouble: |
| 4008 | // Mingw64 GCC uses the old 80 bit extended precision floating point |
| 4009 | // unit. It passes them indirectly through memory. |
| 4010 | if (IsMingw64) { |
| 4011 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 4012 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
| 4013 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 4014 | } |
| 4015 | break; |
| 4016 | |
| 4017 | case BuiltinType::Int128: |
| 4018 | case BuiltinType::UInt128: |
| 4019 | // If it's a parameter type, the normal ABI rule is that arguments larger |
| 4020 | // than 8 bytes are passed indirectly. GCC follows it. We follow it too, |
| 4021 | // even though it isn't particularly efficient. |
| 4022 | if (!IsReturnType) |
| 4023 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 4024 | |
| 4025 | // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. |
| 4026 | // Clang matches them for compatibility. |
| 4027 | return ABIArgInfo::getDirect( |
| 4028 | llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2)); |
| 4029 | |
| 4030 | default: |
| 4031 | break; |
| 4032 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4035 | return ABIArgInfo::getDirect(); |
| 4036 | } |
| 4037 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4038 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 4039 | unsigned FreeSSERegs, |
| 4040 | bool IsVectorCall, |
| 4041 | bool IsRegCall) const { |
| 4042 | unsigned Count = 0; |
| 4043 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4044 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 4045 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4046 | if (Count < VectorcallMaxParamNumAsReg) |
| 4047 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4048 | else { |
| 4049 | // Since these cannot be passed in registers, pretend no registers |
| 4050 | // are left. |
| 4051 | unsigned ZeroSSERegsAvail = 0; |
| 4052 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 4053 | IsVectorCall, IsRegCall); |
| 4054 | } |
| 4055 | ++Count; |
| 4056 | } |
| 4057 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4058 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4059 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4060 | } |
| 4061 | } |
| 4062 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4063 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 4064 | const unsigned CC = FI.getCallingConvention(); |
| 4065 | bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall; |
| 4066 | bool IsRegCall = CC == llvm::CallingConv::X86_RegCall; |
| 4067 | |
| 4068 | // If __attribute__((sysv_abi)) is in use, use the SysV argument |
| 4069 | // classification rules. |
| 4070 | if (CC == llvm::CallingConv::X86_64_SysV) { |
| 4071 | X86_64ABIInfo SysVABIInfo(CGT, AVXLevel); |
| 4072 | SysVABIInfo.computeInfo(FI); |
| 4073 | return; |
| 4074 | } |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 4075 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4076 | unsigned FreeSSERegs = 0; |
| 4077 | if (IsVectorCall) { |
| 4078 | // We can use up to 4 SSE return registers with vectorcall. |
| 4079 | FreeSSERegs = 4; |
| 4080 | } else if (IsRegCall) { |
| 4081 | // RegCall gives us 16 SSE registers. |
| 4082 | FreeSSERegs = 16; |
| 4083 | } |
| 4084 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4085 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4086 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 4087 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4088 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4089 | if (IsVectorCall) { |
| 4090 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4091 | FreeSSERegs = 6; |
| 4092 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4093 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4094 | FreeSSERegs = 16; |
| 4095 | } |
| 4096 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4097 | if (IsVectorCall) { |
| 4098 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4099 | } else { |
| 4100 | for (auto &I : FI.arguments()) |
| 4101 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4102 | } |
| 4103 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4106 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4107 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4108 | |
| 4109 | bool IsIndirect = false; |
| 4110 | |
| 4111 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4112 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4113 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4114 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4115 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4116 | } |
| 4117 | |
| 4118 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4119 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4120 | CharUnits::fromQuantity(8), |
| 4121 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4122 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4123 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4124 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4125 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4126 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4127 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4128 | bool IsSoftFloatABI; |
| 4129 | |
| 4130 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4131 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4132 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4133 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4134 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4135 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4136 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4137 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4138 | }; |
| 4139 | |
| 4140 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4141 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4142 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4143 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4144 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4145 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4146 | // This is recovered from gcc output. |
| 4147 | return 1; // r1 is the dedicated stack pointer |
| 4148 | } |
| 4149 | |
| 4150 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4151 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4152 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4153 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4154 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4155 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4156 | // Complex types are passed just like their elements |
| 4157 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4158 | Ty = CTy->getElementType(); |
| 4159 | |
| 4160 | if (Ty->isVectorType()) |
| 4161 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4162 | : 4); |
| 4163 | |
| 4164 | // For single-element float/vector structs, we consider the whole type |
| 4165 | // to have the same alignment requirements as its single element. |
| 4166 | const Type *AlignTy = nullptr; |
| 4167 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4168 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4169 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4170 | (BT && BT->isFloatingPoint())) |
| 4171 | AlignTy = EltType; |
| 4172 | } |
| 4173 | |
| 4174 | if (AlignTy) |
| 4175 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4176 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4177 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4178 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4179 | // TODO: this implementation is now likely redundant with |
| 4180 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4181 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4182 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4183 | if (getTarget().getTriple().isOSDarwin()) { |
| 4184 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4185 | TI.second = getParamTypeAlignment(Ty); |
| 4186 | |
| 4187 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4188 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4189 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4190 | /*AllowHigherAlign=*/true); |
| 4191 | } |
| 4192 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4193 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4194 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4195 | // TODO: Implement this. For now ignore. |
| 4196 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4197 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4200 | // struct __va_list_tag { |
| 4201 | // unsigned char gpr; |
| 4202 | // unsigned char fpr; |
| 4203 | // unsigned short reserved; |
| 4204 | // void *overflow_arg_area; |
| 4205 | // void *reg_save_area; |
| 4206 | // }; |
| 4207 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4208 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4209 | bool isInt = |
| 4210 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4211 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4212 | |
| 4213 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4214 | // with the argument-lowering code. |
| 4215 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4216 | |
| 4217 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4218 | |
| 4219 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4220 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4221 | if (isInt || IsSoftFloatABI) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4222 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4223 | } else { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4224 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4225 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4226 | |
| 4227 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4228 | |
| 4229 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4230 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4231 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4232 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4233 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4234 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4235 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4236 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4237 | |
| 4238 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4239 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4240 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4241 | |
| 4242 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4243 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4244 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4245 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4246 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4247 | // Case 1: consume registers. |
| 4248 | Address RegAddr = Address::invalid(); |
| 4249 | { |
| 4250 | CGF.EmitBlock(UsingRegs); |
| 4251 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4252 | Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4253 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4254 | CharUnits::fromQuantity(8)); |
| 4255 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4256 | |
| 4257 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4258 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4259 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4260 | CharUnits::fromQuantity(32)); |
| 4261 | } |
| 4262 | |
| 4263 | // Get the address of the saved value by scaling the number of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4264 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4265 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4266 | llvm::Value *RegOffset = |
| 4267 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4268 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4269 | RegAddr.getPointer(), RegOffset), |
| 4270 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4271 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4272 | |
| 4273 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4274 | NumRegs = |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4275 | Builder.CreateAdd(NumRegs, |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4276 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4277 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4278 | |
| 4279 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4280 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4281 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4282 | // Case 2: consume space in the overflow area. |
| 4283 | Address MemAddr = Address::invalid(); |
| 4284 | { |
| 4285 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4286 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4287 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4288 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4289 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4290 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4291 | |
| 4292 | CharUnits Size; |
| 4293 | if (!isIndirect) { |
| 4294 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4295 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4296 | } else { |
| 4297 | Size = CGF.getPointerSize(); |
| 4298 | } |
| 4299 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4300 | Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4301 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4302 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4303 | // Round up address of argument to alignment |
| 4304 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4305 | if (Align > OverflowAreaAlign) { |
| 4306 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4307 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4308 | Align); |
| 4309 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4310 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4311 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4312 | |
| 4313 | // Increase the overflow area. |
| 4314 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4315 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4316 | CGF.EmitBranch(Cont); |
| 4317 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4318 | |
| 4319 | CGF.EmitBlock(Cont); |
| 4320 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4321 | // Merge the cases with a phi. |
| 4322 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4323 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4324 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4325 | // Load the pointer if the argument was passed indirectly. |
| 4326 | if (isIndirect) { |
| 4327 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4328 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | return Result; |
| 4332 | } |
| 4333 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4334 | bool |
| 4335 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4336 | llvm::Value *Address) const { |
| 4337 | // This is calculated from the LLVM and GCC tables and verified |
| 4338 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4339 | |
| 4340 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4341 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4342 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4343 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4344 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4345 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4346 | |
| 4347 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4348 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4349 | |
| 4350 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4351 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4352 | |
| 4353 | // 64-76 are various 4-byte special-purpose registers: |
| 4354 | // 64: mq |
| 4355 | // 65: lr |
| 4356 | // 66: ctr |
| 4357 | // 67: ap |
| 4358 | // 68-75 cr0-7 |
| 4359 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4360 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4361 | |
| 4362 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4363 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4364 | |
| 4365 | // 109: vrsave |
| 4366 | // 110: vscr |
| 4367 | // 111: spe_acc |
| 4368 | // 112: spefscr |
| 4369 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4370 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4371 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4372 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4375 | // PowerPC-64 |
| 4376 | |
| 4377 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4378 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4379 | class PPC64_SVR4_ABIInfo : public SwiftABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4380 | public: |
| 4381 | enum ABIKind { |
| 4382 | ELFv1 = 0, |
| 4383 | ELFv2 |
| 4384 | }; |
| 4385 | |
| 4386 | private: |
| 4387 | static const unsigned GPRBits = 64; |
| 4388 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4389 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4390 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4391 | |
| 4392 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4393 | // will be passed in a QPX register. |
| 4394 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4395 | if (!HasQPX) |
| 4396 | return false; |
| 4397 | |
| 4398 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4399 | unsigned NumElements = VT->getNumElements(); |
| 4400 | if (NumElements == 1) |
| 4401 | return false; |
| 4402 | |
| 4403 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4404 | if (getContext().getTypeSize(Ty) <= 256) |
| 4405 | return true; |
| 4406 | } else if (VT->getElementType()-> |
| 4407 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4408 | if (getContext().getTypeSize(Ty) <= 128) |
| 4409 | return true; |
| 4410 | } |
| 4411 | } |
| 4412 | |
| 4413 | return false; |
| 4414 | } |
| 4415 | |
| 4416 | bool IsQPXVectorTy(QualType Ty) const { |
| 4417 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4418 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4419 | |
| 4420 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4421 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4422 | bool SoftFloatABI) |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4423 | : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4424 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4425 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4426 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4427 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4428 | |
| 4429 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4430 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4431 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4432 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4433 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4434 | uint64_t Members) const override; |
| 4435 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4436 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4437 | // Example: For aggregate arguments that fit in a register, we could |
| 4438 | // use getDirectInReg (as is done below for structs containing a single |
| 4439 | // floating-point value) to avoid pushing them to memory on function |
| 4440 | // entry. This would require changing the logic in PPCISelLowering |
| 4441 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4442 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4443 | if (!getCXXABI().classifyReturnType(FI)) |
| 4444 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4445 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4446 | // We rely on the default argument classification for the most part. |
| 4447 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4448 | // 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] | 4449 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4450 | if (T) { |
| 4451 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4452 | if (IsQPXVectorTy(T) || |
| 4453 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4454 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4455 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4456 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4457 | continue; |
| 4458 | } |
| 4459 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4460 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4461 | } |
| 4462 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4463 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4464 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4465 | QualType Ty) const override; |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4466 | |
| 4467 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 4468 | bool asReturnValue) const override { |
| 4469 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4470 | } |
| 4471 | |
| 4472 | bool isSwiftErrorInRegister() const override { |
| 4473 | return false; |
| 4474 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4475 | }; |
| 4476 | |
| 4477 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4478 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4479 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4480 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4481 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4482 | bool SoftFloatABI) |
| 4483 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4484 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4485 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4486 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4487 | // This is recovered from gcc output. |
| 4488 | return 1; // r1 is the dedicated stack pointer |
| 4489 | } |
| 4490 | |
| 4491 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4492 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4493 | }; |
| 4494 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4495 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4496 | public: |
| 4497 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4498 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4499 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4500 | // This is recovered from gcc output. |
| 4501 | return 1; // r1 is the dedicated stack pointer |
| 4502 | } |
| 4503 | |
| 4504 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4505 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4506 | }; |
| 4507 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4508 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4509 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4510 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4511 | // extended to 64 bits. |
| 4512 | bool |
| 4513 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4514 | // Treat an enum type as its underlying type. |
| 4515 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4516 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4517 | |
| 4518 | // Promotable integer types are required to be promoted by the ABI. |
| 4519 | if (Ty->isPromotableIntegerType()) |
| 4520 | return true; |
| 4521 | |
| 4522 | // In addition to the usual promotable integer types, we also need to |
| 4523 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4524 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4525 | switch (BT->getKind()) { |
| 4526 | case BuiltinType::Int: |
| 4527 | case BuiltinType::UInt: |
| 4528 | return true; |
| 4529 | default: |
| 4530 | break; |
| 4531 | } |
| 4532 | |
| 4533 | return false; |
| 4534 | } |
| 4535 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4536 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4537 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4538 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4539 | // Complex types are passed just like their elements. |
| 4540 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4541 | Ty = CTy->getElementType(); |
| 4542 | |
| 4543 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4544 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4545 | if (IsQPXVectorTy(Ty)) { |
| 4546 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4547 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4548 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4549 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4550 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4551 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4552 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4553 | |
| 4554 | // For single-element float/vector structs, we consider the whole type |
| 4555 | // to have the same alignment requirements as its single element. |
| 4556 | const Type *AlignAsType = nullptr; |
| 4557 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4558 | if (EltType) { |
| 4559 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4560 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4561 | getContext().getTypeSize(EltType) == 128) || |
| 4562 | (BT && BT->isFloatingPoint())) |
| 4563 | AlignAsType = EltType; |
| 4564 | } |
| 4565 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4566 | // Likewise for ELFv2 homogeneous aggregates. |
| 4567 | const Type *Base = nullptr; |
| 4568 | uint64_t Members = 0; |
| 4569 | if (!AlignAsType && Kind == ELFv2 && |
| 4570 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4571 | AlignAsType = Base; |
| 4572 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4573 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4574 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4575 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4576 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4577 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4578 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4579 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4580 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4581 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4582 | |
| 4583 | // Otherwise, we only need alignment for any aggregate type that |
| 4584 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4585 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4586 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4587 | return CharUnits::fromQuantity(32); |
| 4588 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4589 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4590 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4591 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4594 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4595 | /// aggregate. Base is set to the base element type, and Members is set |
| 4596 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4597 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4598 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4599 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4600 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4601 | if (NElements == 0) |
| 4602 | return false; |
| 4603 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4604 | return false; |
| 4605 | Members *= NElements; |
| 4606 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4607 | const RecordDecl *RD = RT->getDecl(); |
| 4608 | if (RD->hasFlexibleArrayMember()) |
| 4609 | return false; |
| 4610 | |
| 4611 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4612 | |
| 4613 | // If this is a C++ record, check the bases first. |
| 4614 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4615 | for (const auto &I : CXXRD->bases()) { |
| 4616 | // Ignore empty records. |
| 4617 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4618 | continue; |
| 4619 | |
| 4620 | uint64_t FldMembers; |
| 4621 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4622 | return false; |
| 4623 | |
| 4624 | Members += FldMembers; |
| 4625 | } |
| 4626 | } |
| 4627 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4628 | for (const auto *FD : RD->fields()) { |
| 4629 | // Ignore (non-zero arrays of) empty records. |
| 4630 | QualType FT = FD->getType(); |
| 4631 | while (const ConstantArrayType *AT = |
| 4632 | getContext().getAsConstantArrayType(FT)) { |
| 4633 | if (AT->getSize().getZExtValue() == 0) |
| 4634 | return false; |
| 4635 | FT = AT->getElementType(); |
| 4636 | } |
| 4637 | if (isEmptyRecord(getContext(), FT, true)) |
| 4638 | continue; |
| 4639 | |
| 4640 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4641 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4642 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4643 | continue; |
| 4644 | |
| 4645 | uint64_t FldMembers; |
| 4646 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4647 | return false; |
| 4648 | |
| 4649 | Members = (RD->isUnion() ? |
| 4650 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4651 | } |
| 4652 | |
| 4653 | if (!Base) |
| 4654 | return false; |
| 4655 | |
| 4656 | // Ensure there is no padding. |
| 4657 | if (getContext().getTypeSize(Base) * Members != |
| 4658 | getContext().getTypeSize(Ty)) |
| 4659 | return false; |
| 4660 | } else { |
| 4661 | Members = 1; |
| 4662 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4663 | Members = 2; |
| 4664 | Ty = CT->getElementType(); |
| 4665 | } |
| 4666 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4667 | // Most ABIs only support float, double, and some vector type widths. |
| 4668 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4669 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4670 | |
| 4671 | // The base type must be the same for all members. Types that |
| 4672 | // agree in both total size and mode (float vs. vector) are |
| 4673 | // treated as being equivalent here. |
| 4674 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4675 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4676 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4677 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4678 | // so make sure to widen it explicitly. |
| 4679 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4680 | QualType EltTy = VT->getElementType(); |
| 4681 | unsigned NumElements = |
| 4682 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4683 | Base = getContext() |
| 4684 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4685 | .getTypePtr(); |
| 4686 | } |
| 4687 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4688 | |
| 4689 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4690 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4691 | return false; |
| 4692 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4693 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4694 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4695 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4696 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4697 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4698 | // double, long double, or 128-bit vectors. |
| 4699 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4700 | if (BT->getKind() == BuiltinType::Float || |
| 4701 | BT->getKind() == BuiltinType::Double || |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4702 | BT->getKind() == BuiltinType::LongDouble || |
| 4703 | (getContext().getTargetInfo().hasFloat128Type() && |
| 4704 | (BT->getKind() == BuiltinType::Float128))) { |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4705 | if (IsSoftFloatABI) |
| 4706 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4707 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4708 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4709 | } |
| 4710 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4711 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4712 | return true; |
| 4713 | } |
| 4714 | return false; |
| 4715 | } |
| 4716 | |
| 4717 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4718 | const Type *Base, uint64_t Members) const { |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4719 | // Vector and fp128 types require one register, other floating point types |
| 4720 | // require one or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4721 | uint32_t NumRegs = |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4722 | ((getContext().getTargetInfo().hasFloat128Type() && |
| 4723 | Base->isFloat128Type()) || |
| 4724 | Base->isVectorType()) ? 1 |
| 4725 | : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4726 | |
| 4727 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4728 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4731 | ABIArgInfo |
| 4732 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4733 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4734 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4735 | if (Ty->isAnyComplexType()) |
| 4736 | return ABIArgInfo::getDirect(); |
| 4737 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4738 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4739 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4740 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4741 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4742 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4743 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4744 | else if (Size < 128) { |
| 4745 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4746 | return ABIArgInfo::getDirect(CoerceTy); |
| 4747 | } |
| 4748 | } |
| 4749 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4750 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4751 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4752 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4753 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4754 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4755 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4756 | |
| 4757 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4758 | const Type *Base = nullptr; |
| 4759 | uint64_t Members = 0; |
| 4760 | if (Kind == ELFv2 && |
| 4761 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4762 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4763 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4764 | return ABIArgInfo::getDirect(CoerceTy); |
| 4765 | } |
| 4766 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4767 | // If an aggregate may end up fully in registers, we do not |
| 4768 | // use the ByVal method, but pass the aggregate as array. |
| 4769 | // This is usually beneficial since we avoid forcing the |
| 4770 | // back-end to store the argument to memory. |
| 4771 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4772 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4773 | llvm::Type *CoerceTy; |
| 4774 | |
| 4775 | // Types up to 8 bytes are passed as integer type (which will be |
| 4776 | // properly aligned in the argument save area doubleword). |
| 4777 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4778 | CoerceTy = |
| 4779 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4780 | // Larger types are passed as arrays, with the base type selected |
| 4781 | // according to the required alignment in the save area. |
| 4782 | else { |
| 4783 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4784 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4785 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4786 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4787 | } |
| 4788 | |
| 4789 | return ABIArgInfo::getDirect(CoerceTy); |
| 4790 | } |
| 4791 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4792 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4793 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4794 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4795 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4798 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4799 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | ABIArgInfo |
| 4803 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4804 | if (RetTy->isVoidType()) |
| 4805 | return ABIArgInfo::getIgnore(); |
| 4806 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4807 | if (RetTy->isAnyComplexType()) |
| 4808 | return ABIArgInfo::getDirect(); |
| 4809 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4810 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4811 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4812 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4813 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4814 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4815 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4816 | else if (Size < 128) { |
| 4817 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4818 | return ABIArgInfo::getDirect(CoerceTy); |
| 4819 | } |
| 4820 | } |
| 4821 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4822 | if (isAggregateTypeForABI(RetTy)) { |
| 4823 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4824 | const Type *Base = nullptr; |
| 4825 | uint64_t Members = 0; |
| 4826 | if (Kind == ELFv2 && |
| 4827 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4828 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4829 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4830 | return ABIArgInfo::getDirect(CoerceTy); |
| 4831 | } |
| 4832 | |
| 4833 | // ELFv2 small aggregates are returned in up to two registers. |
| 4834 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4835 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4836 | if (Bits == 0) |
| 4837 | return ABIArgInfo::getIgnore(); |
| 4838 | |
| 4839 | llvm::Type *CoerceTy; |
| 4840 | if (Bits > GPRBits) { |
| 4841 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4842 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4843 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4844 | CoerceTy = |
| 4845 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4846 | return ABIArgInfo::getDirect(CoerceTy); |
| 4847 | } |
| 4848 | |
| 4849 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4850 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4851 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4852 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4853 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4854 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4857 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4858 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4859 | QualType Ty) const { |
| 4860 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4861 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4862 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4863 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4864 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4865 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4866 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4867 | // in separate doublewords. However, Clang expects us to produce a |
| 4868 | // pointer to a structure with the two parts packed tightly. So generate |
| 4869 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4870 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4871 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4872 | CharUnits EltSize = TypeInfo.first / 2; |
| 4873 | if (EltSize < SlotSize) { |
| 4874 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4875 | SlotSize * 2, SlotSize, |
| 4876 | SlotSize, /*AllowHigher*/ true); |
| 4877 | |
| 4878 | Address RealAddr = Addr; |
| 4879 | Address ImagAddr = RealAddr; |
| 4880 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4881 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4882 | SlotSize - EltSize); |
| 4883 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4884 | 2 * SlotSize - EltSize); |
| 4885 | } else { |
| 4886 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4887 | } |
| 4888 | |
| 4889 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4890 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4891 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4892 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4893 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4894 | |
| 4895 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4896 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4897 | /*init*/ true); |
| 4898 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4899 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4902 | // Otherwise, just use the general rule. |
| 4903 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4904 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | static bool |
| 4908 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4909 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4910 | // This is calculated from the LLVM and GCC tables and verified |
| 4911 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4912 | |
| 4913 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4914 | |
| 4915 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4916 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4917 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4918 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4919 | |
| 4920 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4921 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4922 | |
| 4923 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4924 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4925 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4926 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4927 | // 64: mq |
| 4928 | // 65: lr |
| 4929 | // 66: ctr |
| 4930 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4931 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4932 | |
| 4933 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4934 | // 68-75 cr0-7 |
| 4935 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4936 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4937 | |
| 4938 | // 77-108: v0-31, the 16-byte vector registers |
| 4939 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4940 | |
| 4941 | // 109: vrsave |
| 4942 | // 110: vscr |
| 4943 | // 111: spe_acc |
| 4944 | // 112: spefscr |
| 4945 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4946 | // 114: tfhar |
| 4947 | // 115: tfiar |
| 4948 | // 116: texasr |
| 4949 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4950 | |
| 4951 | return false; |
| 4952 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4953 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4954 | bool |
| 4955 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4956 | CodeGen::CodeGenFunction &CGF, |
| 4957 | llvm::Value *Address) const { |
| 4958 | |
| 4959 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4960 | } |
| 4961 | |
| 4962 | bool |
| 4963 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4964 | llvm::Value *Address) const { |
| 4965 | |
| 4966 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4967 | } |
| 4968 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4969 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4970 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4971 | //===----------------------------------------------------------------------===// |
| 4972 | |
| 4973 | namespace { |
| 4974 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4975 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4976 | public: |
| 4977 | enum ABIKind { |
| 4978 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4979 | DarwinPCS, |
| 4980 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4981 | }; |
| 4982 | |
| 4983 | private: |
| 4984 | ABIKind Kind; |
| 4985 | |
| 4986 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4987 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4988 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4989 | |
| 4990 | private: |
| 4991 | ABIKind getABIKind() const { return Kind; } |
| 4992 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4993 | |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 4994 | ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4995 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4996 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4997 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4998 | uint64_t Members) const override; |
| 4999 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5000 | bool isIllegalVectorType(QualType Ty) const; |
| 5001 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 5002 | void computeInfo(CGFunctionInfo &FI) const override { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5003 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5004 | FI.getReturnInfo() = |
| 5005 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 5006 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5007 | for (auto &it : FI.arguments()) |
| 5008 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5011 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5012 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5013 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5014 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 5015 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5016 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5017 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5018 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5019 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 5020 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 5021 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5022 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5023 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5024 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5025 | QualType Ty) const override; |
| 5026 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5027 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5028 | bool asReturnValue) const override { |
| 5029 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5030 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5031 | bool isSwiftErrorInRegister() const override { |
| 5032 | return true; |
| 5033 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5034 | |
| 5035 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5036 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5037 | }; |
| 5038 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5039 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5040 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5041 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 5042 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5043 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5044 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5045 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5046 | } |
| 5047 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5048 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 5049 | return 31; |
| 5050 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5051 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5052 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5053 | |
| 5054 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5055 | CodeGen::CodeGenModule &CGM) const override { |
| 5056 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 5057 | if (!FD) |
| 5058 | return; |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5059 | |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 5060 | CodeGenOptions::SignReturnAddressScope Scope = CGM.getCodeGenOpts().getSignReturnAddress(); |
| 5061 | CodeGenOptions::SignReturnAddressKeyValue Key = CGM.getCodeGenOpts().getSignReturnAddressKey(); |
| 5062 | bool BranchTargetEnforcement = CGM.getCodeGenOpts().BranchTargetEnforcement; |
| 5063 | if (const auto *TA = FD->getAttr<TargetAttr>()) { |
| 5064 | TargetAttr::ParsedTargetAttr Attr = TA->parse(); |
| 5065 | if (!Attr.BranchProtection.empty()) { |
| 5066 | TargetInfo::BranchProtectionInfo BPI; |
| 5067 | StringRef Error; |
| 5068 | (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection, |
| 5069 | BPI, Error); |
| 5070 | assert(Error.empty()); |
| 5071 | Scope = BPI.SignReturnAddr; |
| 5072 | Key = BPI.SignKey; |
| 5073 | BranchTargetEnforcement = BPI.BranchTargetEnforcement; |
| 5074 | } |
| 5075 | } |
| 5076 | |
| 5077 | auto *Fn = cast<llvm::Function>(GV); |
| 5078 | if (Scope != CodeGenOptions::SignReturnAddressScope::None) { |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5079 | Fn->addFnAttr("sign-return-address", |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 5080 | Scope == CodeGenOptions::SignReturnAddressScope::All |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5081 | ? "all" |
| 5082 | : "non-leaf"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5083 | |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5084 | Fn->addFnAttr("sign-return-address-key", |
| 5085 | Key == CodeGenOptions::SignReturnAddressKeyValue::AKey |
| 5086 | ? "a_key" |
| 5087 | : "b_key"); |
| 5088 | } |
| 5089 | |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 5090 | if (BranchTargetEnforcement) |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5091 | Fn->addFnAttr("branch-target-enforcement"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5092 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5093 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5094 | |
| 5095 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 5096 | public: |
| 5097 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 5098 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 5099 | |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5100 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5101 | CodeGen::CodeGenModule &CGM) const override; |
| 5102 | |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5103 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5104 | llvm::SmallString<24> &Opt) const override { |
| 5105 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5106 | } |
| 5107 | |
| 5108 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5109 | llvm::SmallString<32> &Opt) const override { |
| 5110 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5111 | } |
| 5112 | }; |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5113 | |
| 5114 | void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( |
| 5115 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5116 | AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5117 | if (GV->isDeclaration()) |
| 5118 | return; |
| 5119 | addStackProbeTargetAttributes(D, GV, CGM); |
| 5120 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5121 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5122 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5123 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5124 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5125 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5126 | // Handle illegal vector types here. |
| 5127 | if (isIllegalVectorType(Ty)) { |
| 5128 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5129 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 5130 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5131 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 5132 | return ABIArgInfo::getDirect(ResType); |
| 5133 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5134 | if (Size <= 32) { |
| 5135 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5136 | return ABIArgInfo::getDirect(ResType); |
| 5137 | } |
| 5138 | if (Size == 64) { |
| 5139 | llvm::Type *ResType = |
| 5140 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5141 | return ABIArgInfo::getDirect(ResType); |
| 5142 | } |
| 5143 | if (Size == 128) { |
| 5144 | llvm::Type *ResType = |
| 5145 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5146 | return ABIArgInfo::getDirect(ResType); |
| 5147 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5148 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5149 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5150 | |
| 5151 | if (!isAggregateTypeForABI(Ty)) { |
| 5152 | // Treat an enum type as its underlying type. |
| 5153 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5154 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5155 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5156 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5157 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5158 | : ABIArgInfo::getDirect()); |
| 5159 | } |
| 5160 | |
| 5161 | // Structures with either a non-trivial destructor or a non-trivial |
| 5162 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5163 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5164 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5165 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5169 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5170 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5171 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5172 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5173 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5174 | return ABIArgInfo::getIgnore(); |
| 5175 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5176 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5177 | // 0. |
| 5178 | if (IsEmpty && Size == 0) |
| 5179 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5180 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5181 | } |
| 5182 | |
| 5183 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5184 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5185 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5186 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5187 | return ABIArgInfo::getDirect( |
| 5188 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5192 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5193 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5194 | // same size and alignment. |
| 5195 | if (getTarget().isRenderScriptTarget()) { |
| 5196 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5197 | } |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5198 | unsigned Alignment; |
| 5199 | if (Kind == AArch64ABIInfo::AAPCS) { |
| 5200 | Alignment = getContext().getTypeUnadjustedAlign(Ty); |
| 5201 | Alignment = Alignment < 128 ? 64 : 128; |
| 5202 | } else { |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5203 | Alignment = std::max(getContext().getTypeAlign(Ty), |
| 5204 | (unsigned)getTarget().getPointerWidth(0)); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5205 | } |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5206 | Size = llvm::alignTo(Size, Alignment); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5207 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5208 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5209 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5210 | llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment); |
| 5211 | return ABIArgInfo::getDirect( |
| 5212 | Size == Alignment ? BaseTy |
| 5213 | : llvm::ArrayType::get(BaseTy, Size / Alignment)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5216 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5219 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy, |
| 5220 | bool IsVariadic) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5221 | if (RetTy->isVoidType()) |
| 5222 | return ABIArgInfo::getIgnore(); |
| 5223 | |
| 5224 | // Large vector types should be returned via memory. |
| 5225 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5226 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5227 | |
| 5228 | if (!isAggregateTypeForABI(RetTy)) { |
| 5229 | // Treat an enum type as its underlying type. |
| 5230 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5231 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5232 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5233 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5234 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5235 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5238 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5239 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5240 | return ABIArgInfo::getIgnore(); |
| 5241 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5242 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5243 | uint64_t Members = 0; |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5244 | if (isHomogeneousAggregate(RetTy, Base, Members) && |
| 5245 | !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 && |
| 5246 | IsVariadic)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5247 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5248 | return ABIArgInfo::getDirect(); |
| 5249 | |
| 5250 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5251 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5252 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5253 | // same size and alignment. |
| 5254 | if (getTarget().isRenderScriptTarget()) { |
| 5255 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5256 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5257 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5258 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5259 | |
| 5260 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5261 | // For aggregates with 16-byte alignment, we use i128. |
| 5262 | if (Alignment < 128 && Size == 128) { |
| 5263 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5264 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5265 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5266 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5267 | } |
| 5268 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5269 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5272 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5273 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5274 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5275 | // Check whether VT is legal. |
| 5276 | unsigned NumElements = VT->getNumElements(); |
| 5277 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5278 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5279 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5280 | return true; |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5281 | |
| 5282 | // arm64_32 has to be compatible with the ARM logic here, which allows huge |
| 5283 | // vectors for some reason. |
| 5284 | llvm::Triple Triple = getTarget().getTriple(); |
| 5285 | if (Triple.getArch() == llvm::Triple::aarch64_32 && |
| 5286 | Triple.isOSBinFormatMachO()) |
| 5287 | return Size <= 32; |
| 5288 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5289 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5290 | } |
| 5291 | return false; |
| 5292 | } |
| 5293 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5294 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5295 | llvm::Type *eltTy, |
| 5296 | unsigned elts) const { |
| 5297 | if (!llvm::isPowerOf2_32(elts)) |
| 5298 | return false; |
| 5299 | if (totalSize.getQuantity() != 8 && |
| 5300 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5301 | return false; |
| 5302 | return true; |
| 5303 | } |
| 5304 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5305 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5306 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5307 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5308 | // but with the difference that any floating-point type is allowed, |
| 5309 | // including __fp16. |
| 5310 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5311 | if (BT->isFloatingPoint()) |
| 5312 | return true; |
| 5313 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5314 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5315 | if (VecSize == 64 || VecSize == 128) |
| 5316 | return true; |
| 5317 | } |
| 5318 | return false; |
| 5319 | } |
| 5320 | |
| 5321 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5322 | uint64_t Members) const { |
| 5323 | return Members <= 4; |
| 5324 | } |
| 5325 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5326 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5327 | QualType Ty, |
| 5328 | CodeGenFunction &CGF) const { |
| 5329 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5330 | bool IsIndirect = AI.isIndirect(); |
| 5331 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5332 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5333 | if (IsIndirect) |
| 5334 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5335 | else if (AI.getCoerceToType()) |
| 5336 | BaseTy = AI.getCoerceToType(); |
| 5337 | |
| 5338 | unsigned NumRegs = 1; |
| 5339 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5340 | BaseTy = ArrTy->getElementType(); |
| 5341 | NumRegs = ArrTy->getNumElements(); |
| 5342 | } |
| 5343 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5344 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5345 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5346 | // Standard, section B.4: |
| 5347 | // |
| 5348 | // struct { |
| 5349 | // void *__stack; |
| 5350 | // void *__gr_top; |
| 5351 | // void *__vr_top; |
| 5352 | // int __gr_offs; |
| 5353 | // int __vr_offs; |
| 5354 | // }; |
| 5355 | |
| 5356 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5357 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5358 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5359 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5360 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5361 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 5362 | CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5363 | |
| 5364 | Address reg_offs_p = Address::invalid(); |
| 5365 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5366 | int reg_top_index; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5367 | int RegSize = IsIndirect ? 8 : TySize.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5368 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5369 | // 3 is the field number of __gr_offs |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5370 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5371 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5372 | reg_top_index = 1; // field number for __gr_top |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5373 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5374 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5375 | // 4 is the field number of __vr_offs. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5376 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5377 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5378 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5379 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | //======================================= |
| 5383 | // Find out where argument was passed |
| 5384 | //======================================= |
| 5385 | |
| 5386 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5387 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5388 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5389 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5390 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5391 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5392 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5393 | |
| 5394 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5395 | |
| 5396 | // 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] | 5397 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5398 | CGF.EmitBlock(MaybeRegBlock); |
| 5399 | |
| 5400 | // Integer arguments may need to correct register alignment (for example a |
| 5401 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5402 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5403 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5404 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5405 | |
| 5406 | reg_offs = CGF.Builder.CreateAdd( |
| 5407 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5408 | "align_regoffs"); |
| 5409 | reg_offs = CGF.Builder.CreateAnd( |
| 5410 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5411 | "aligned_regoffs"); |
| 5412 | } |
| 5413 | |
| 5414 | // 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] | 5415 | // The fact that this is done unconditionally reflects the fact that |
| 5416 | // allocating an argument to the stack also uses up all the remaining |
| 5417 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5418 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5419 | NewOffset = CGF.Builder.CreateAdd( |
| 5420 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5421 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5422 | |
| 5423 | // Now we're in a position to decide whether this argument really was in |
| 5424 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5425 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5426 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5427 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5428 | |
| 5429 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5430 | |
| 5431 | //======================================= |
| 5432 | // Argument was in registers |
| 5433 | //======================================= |
| 5434 | |
| 5435 | // Now we emit the code for if the argument was originally passed in |
| 5436 | // registers. First start the appropriate block: |
| 5437 | CGF.EmitBlock(InRegBlock); |
| 5438 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5439 | llvm::Value *reg_top = nullptr; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5440 | Address reg_top_p = |
| 5441 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5442 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5443 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5444 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5445 | Address RegAddr = Address::invalid(); |
| 5446 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5447 | |
| 5448 | if (IsIndirect) { |
| 5449 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5450 | // stored registers or on the stack will actually be a struct **. |
| 5451 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5452 | } |
| 5453 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5454 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5455 | uint64_t NumMembers = 0; |
| 5456 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5457 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5458 | // Homogeneous aggregates passed in registers will have their elements split |
| 5459 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5460 | // qN+1, ...). We reload and store into a temporary local variable |
| 5461 | // contiguously. |
| 5462 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5463 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5464 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5465 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5466 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5467 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5468 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5469 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5470 | int Offset = 0; |
| 5471 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5472 | BaseTyInfo.first.getQuantity() < 16) |
| 5473 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5474 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5475 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5476 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5477 | Address LoadAddr = |
| 5478 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5479 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5480 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5481 | Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5482 | |
| 5483 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5484 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5485 | } |
| 5486 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5487 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5488 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5489 | // Otherwise the object is contiguous in memory. |
| 5490 | |
| 5491 | // It might be right-aligned in its slot. |
| 5492 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5493 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5494 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5495 | TySize < SlotSize) { |
| 5496 | CharUnits Offset = SlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5497 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5500 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | CGF.EmitBranch(ContBlock); |
| 5504 | |
| 5505 | //======================================= |
| 5506 | // Argument was on the stack |
| 5507 | //======================================= |
| 5508 | CGF.EmitBlock(OnStackBlock); |
| 5509 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5510 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5511 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5512 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5513 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5514 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5515 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5516 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5517 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5518 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
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 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5521 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5522 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5523 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5524 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5525 | "align_stack"); |
| 5526 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5527 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5528 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5529 | Address OnStackAddr(OnStackPtr, |
| 5530 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5531 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5532 | // All stack slots are multiples of 8 bytes. |
| 5533 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5534 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5535 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5536 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5537 | else |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5538 | StackSize = TySize.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5539 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5540 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5541 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5542 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5543 | |
| 5544 | // Write the new value of __stack for the next call to va_arg |
| 5545 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5546 | |
| 5547 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5548 | TySize < StackSlotSize) { |
| 5549 | CharUnits Offset = StackSlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5550 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5553 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5554 | |
| 5555 | CGF.EmitBranch(ContBlock); |
| 5556 | |
| 5557 | //======================================= |
| 5558 | // Tidy up |
| 5559 | //======================================= |
| 5560 | CGF.EmitBlock(ContBlock); |
| 5561 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5562 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5563 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5564 | |
| 5565 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5566 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5567 | TyAlign); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5568 | |
| 5569 | return ResAddr; |
| 5570 | } |
| 5571 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5572 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5573 | CodeGenFunction &CGF) const { |
| 5574 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5575 | // illegal vector types. Lower VAArg here for these cases and use |
| 5576 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5577 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5578 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5579 | |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5580 | uint64_t PointerSize = getTarget().getPointerWidth(0) / 8; |
| 5581 | CharUnits SlotSize = CharUnits::fromQuantity(PointerSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5582 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5583 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5584 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5585 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5586 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5587 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5590 | // The size of the actual thing passed, which might end up just |
| 5591 | // being a pointer for indirect types. |
| 5592 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5593 | |
| 5594 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5595 | // aggregates should be passed indirectly. |
| 5596 | bool IsIndirect = false; |
| 5597 | if (TyInfo.first.getQuantity() > 16) { |
| 5598 | const Type *Base = nullptr; |
| 5599 | uint64_t Members = 0; |
| 5600 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5601 | } |
| 5602 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5603 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5604 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5607 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5608 | QualType Ty) const { |
| 5609 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5610 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5611 | CharUnits::fromQuantity(8), |
| 5612 | /*allowHigherAlign*/ false); |
| 5613 | } |
| 5614 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5615 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5616 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5617 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5618 | |
| 5619 | namespace { |
| 5620 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5621 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5622 | public: |
| 5623 | enum ABIKind { |
| 5624 | APCS = 0, |
| 5625 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5626 | AAPCS_VFP = 2, |
| 5627 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5628 | }; |
| 5629 | |
| 5630 | private: |
| 5631 | ABIKind Kind; |
| 5632 | |
| 5633 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5634 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5635 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5636 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5637 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5638 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5639 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5640 | switch (getTarget().getTriple().getEnvironment()) { |
| 5641 | case llvm::Triple::Android: |
| 5642 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5643 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5644 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5645 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5646 | case llvm::Triple::MuslEABI: |
| 5647 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5648 | return true; |
| 5649 | default: |
| 5650 | return false; |
| 5651 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5654 | bool isEABIHF() const { |
| 5655 | switch (getTarget().getTriple().getEnvironment()) { |
| 5656 | case llvm::Triple::EABIHF: |
| 5657 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5658 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5659 | return true; |
| 5660 | default: |
| 5661 | return false; |
| 5662 | } |
| 5663 | } |
| 5664 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5665 | ABIKind getABIKind() const { return Kind; } |
| 5666 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5667 | private: |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5668 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic, |
| 5669 | unsigned functionCallConv) const; |
| 5670 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, |
| 5671 | unsigned functionCallConv) const; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5672 | ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, |
| 5673 | uint64_t Members) const; |
| 5674 | ABIArgInfo coerceIllegalVector(QualType Ty) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5675 | bool isIllegalVectorType(QualType Ty) const; |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 5676 | bool containsAnyFP16Vectors(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5677 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5678 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5679 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5680 | uint64_t Members) const override; |
| 5681 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5682 | bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const; |
| 5683 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5684 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5685 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5686 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5687 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5688 | |
| 5689 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5690 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5691 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5692 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5693 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5694 | bool asReturnValue) const override { |
| 5695 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5696 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5697 | bool isSwiftErrorInRegister() const override { |
| 5698 | return true; |
| 5699 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5700 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5701 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5702 | }; |
| 5703 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5704 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5705 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5706 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5707 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5708 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5709 | const ARMABIInfo &getABIInfo() const { |
| 5710 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5711 | } |
| 5712 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5713 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5714 | return 13; |
| 5715 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5716 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5717 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5718 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5721 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5722 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5723 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5724 | |
| 5725 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5726 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5727 | return false; |
| 5728 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5729 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5730 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5731 | if (getABIInfo().isEABI()) return 88; |
| 5732 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5733 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5734 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5735 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5736 | CodeGen::CodeGenModule &CGM) const override { |
| 5737 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5738 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5739 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5740 | if (!FD) |
| 5741 | return; |
| 5742 | |
| 5743 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5744 | if (!Attr) |
| 5745 | return; |
| 5746 | |
| 5747 | const char *Kind; |
| 5748 | switch (Attr->getInterrupt()) { |
| 5749 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5750 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5751 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5752 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5753 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5754 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5755 | } |
| 5756 | |
| 5757 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5758 | |
| 5759 | Fn->addFnAttr("interrupt", Kind); |
| 5760 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5761 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5762 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5763 | return; |
| 5764 | |
| 5765 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5766 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5767 | // the backend to perform a realignment as part of the function prologue. |
| 5768 | llvm::AttrBuilder B; |
| 5769 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5770 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5771 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5772 | }; |
| 5773 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5774 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5775 | public: |
| 5776 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5777 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5778 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5779 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5780 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5781 | |
| 5782 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5783 | llvm::SmallString<24> &Opt) const override { |
| 5784 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5785 | } |
| 5786 | |
| 5787 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5788 | llvm::SmallString<32> &Opt) const override { |
| 5789 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5790 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5791 | }; |
| 5792 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5793 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5794 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5795 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5796 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5797 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 5798 | addStackProbeTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5799 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5800 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5801 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5802 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5803 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5804 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(), |
| 5805 | FI.getCallingConvention()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5806 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5807 | for (auto &I : FI.arguments()) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5808 | I.info = classifyArgumentType(I.type, FI.isVariadic(), |
| 5809 | FI.getCallingConvention()); |
| 5810 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5811 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5812 | // Always honor user-specified calling convention. |
| 5813 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5814 | return; |
| 5815 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5816 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5817 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5818 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5819 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5820 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5821 | /// Return the default calling convention that LLVM will use. |
| 5822 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5823 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5824 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5825 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5826 | else if (isEABI()) |
| 5827 | return llvm::CallingConv::ARM_AAPCS; |
| 5828 | else |
| 5829 | return llvm::CallingConv::ARM_APCS; |
| 5830 | } |
| 5831 | |
| 5832 | /// Return the calling convention that our ABI would like us to use |
| 5833 | /// as the C calling convention. |
| 5834 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5835 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5836 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5837 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5838 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5839 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5840 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5841 | llvm_unreachable("bad ABI kind"); |
| 5842 | } |
| 5843 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5844 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5845 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5846 | |
| 5847 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5848 | // they'd just match what LLVM will infer from the triple. |
| 5849 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5850 | if (abiCC != getLLVMDefaultCC()) |
| 5851 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5852 | } |
| 5853 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5854 | ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { |
| 5855 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5856 | if (Size <= 32) { |
| 5857 | llvm::Type *ResType = |
| 5858 | llvm::Type::getInt32Ty(getVMContext()); |
| 5859 | return ABIArgInfo::getDirect(ResType); |
| 5860 | } |
| 5861 | if (Size == 64 || Size == 128) { |
| 5862 | llvm::Type *ResType = llvm::VectorType::get( |
| 5863 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5864 | return ABIArgInfo::getDirect(ResType); |
| 5865 | } |
| 5866 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5867 | } |
| 5868 | |
| 5869 | ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, |
| 5870 | const Type *Base, |
| 5871 | uint64_t Members) const { |
| 5872 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 5873 | // Base can be a floating-point or a vector. |
| 5874 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 5875 | // FP16 vectors should be converted to integer vectors |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 5876 | if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5877 | uint64_t Size = getContext().getTypeSize(VT); |
| 5878 | llvm::Type *NewVecTy = llvm::VectorType::get( |
| 5879 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5880 | llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); |
| 5881 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5882 | } |
| 5883 | } |
| 5884 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 5885 | } |
| 5886 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5887 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, |
| 5888 | unsigned functionCallConv) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5889 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5890 | // A single-precision floating-point type (including promoted |
| 5891 | // half-precision types); A double-precision floating-point type; |
| 5892 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5893 | // with a Base Type of a single- or double-precision floating-point type, |
| 5894 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5895 | // to four Elements. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5896 | // Variadic functions should always marshal to the base standard. |
| 5897 | bool IsAAPCS_VFP = |
| 5898 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5899 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5900 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5901 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5902 | // Handle illegal vector types here. |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5903 | if (isIllegalVectorType(Ty)) |
| 5904 | return coerceIllegalVector(Ty); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5905 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5906 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5907 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5908 | // half type natively, and does not need to interwork with AAPCS code. |
| 5909 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5910 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5911 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5912 | llvm::Type::getFloatTy(getVMContext()) : |
| 5913 | llvm::Type::getInt32Ty(getVMContext()); |
| 5914 | return ABIArgInfo::getDirect(ResType); |
| 5915 | } |
| 5916 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5917 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5918 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5919 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5920 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5921 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5922 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5923 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5924 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5925 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5926 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5927 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5928 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5929 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5930 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5931 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5932 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5933 | return ABIArgInfo::getIgnore(); |
| 5934 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5935 | if (IsAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5936 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5937 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5938 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5939 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5940 | if (isHomogeneousAggregate(Ty, Base, Members)) |
| 5941 | return classifyHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5942 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5943 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5944 | // this convention even for a variadic function: the backend will use GPRs |
| 5945 | // if needed. |
| 5946 | const Type *Base = nullptr; |
| 5947 | uint64_t Members = 0; |
| 5948 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5949 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5950 | llvm::Type *Ty = |
| 5951 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5952 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5953 | } |
| 5954 | } |
| 5955 | |
| 5956 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5957 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5958 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5959 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5960 | // and a pointer is passed. |
| 5961 | return ABIArgInfo::getIndirect( |
| 5962 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5963 | } |
| 5964 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5965 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5966 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5967 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5968 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5969 | uint64_t ABIAlign = 4; |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5970 | uint64_t TyAlign; |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5971 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5972 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5973 | TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5974 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5975 | } else { |
| 5976 | TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 5977 | } |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5978 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5979 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5980 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5981 | /*ByVal=*/true, |
| 5982 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5985 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5986 | // same size and alignment. |
| 5987 | if (getTarget().isRenderScriptTarget()) { |
| 5988 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5989 | } |
| 5990 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5991 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5992 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5993 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5994 | // FIXME: Try to match the types of the arguments more accurately where |
| 5995 | // we can. |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5996 | if (TyAlign <= 4) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5997 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5998 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5999 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 6000 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 6001 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 6002 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 6003 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6004 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6007 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6008 | llvm::LLVMContext &VMContext) { |
| 6009 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 6010 | // is called integer-like if its size is less than or equal to one word, and |
| 6011 | // the offset of each of its addressable sub-fields is zero. |
| 6012 | |
| 6013 | uint64_t Size = Context.getTypeSize(Ty); |
| 6014 | |
| 6015 | // Check that the type fits in a word. |
| 6016 | if (Size > 32) |
| 6017 | return false; |
| 6018 | |
| 6019 | // FIXME: Handle vector types! |
| 6020 | if (Ty->isVectorType()) |
| 6021 | return false; |
| 6022 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 6023 | // Float types are never treated as "integer like". |
| 6024 | if (Ty->isRealFloatingType()) |
| 6025 | return false; |
| 6026 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6027 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6028 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6029 | return true; |
| 6030 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 6031 | // Small complex integer types are "integer like". |
| 6032 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 6033 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6034 | |
| 6035 | // Single element and zero sized arrays should be allowed, by the definition |
| 6036 | // above, but they are not. |
| 6037 | |
| 6038 | // Otherwise, it must be a record type. |
| 6039 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 6040 | if (!RT) return false; |
| 6041 | |
| 6042 | // Ignore records with flexible arrays. |
| 6043 | const RecordDecl *RD = RT->getDecl(); |
| 6044 | if (RD->hasFlexibleArrayMember()) |
| 6045 | return false; |
| 6046 | |
| 6047 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 6048 | // like". |
| 6049 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 6050 | |
| 6051 | bool HadField = false; |
| 6052 | unsigned idx = 0; |
| 6053 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6054 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6055 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6056 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6057 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 6058 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 6059 | // struct { int : 0; int x } |
| 6060 | // is non-integer like according to gcc. |
| 6061 | if (FD->isBitField()) { |
| 6062 | if (!RD->isUnion()) |
| 6063 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6064 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6065 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6066 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6067 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6068 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6069 | } |
| 6070 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6071 | // Check if this field is at offset 0. |
| 6072 | if (Layout.getFieldOffset(idx) != 0) |
| 6073 | return false; |
| 6074 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6075 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6076 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 6077 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6078 | // Only allow at most one field in a structure. This doesn't match the |
| 6079 | // wording above, but follows gcc in situations with a field following an |
| 6080 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6081 | if (!RD->isUnion()) { |
| 6082 | if (HadField) |
| 6083 | return false; |
| 6084 | |
| 6085 | HadField = true; |
| 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | return true; |
| 6090 | } |
| 6091 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6092 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic, |
| 6093 | unsigned functionCallConv) const { |
| 6094 | |
| 6095 | // Variadic functions should always marshal to the base standard. |
| 6096 | bool IsAAPCS_VFP = |
| 6097 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6098 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6099 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6100 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6101 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6102 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| 6103 | // Large vector types should be returned via memory. |
| 6104 | if (getContext().getTypeSize(RetTy) > 128) |
| 6105 | return getNaturalAlignIndirect(RetTy); |
| 6106 | // FP16 vectors should be converted to integer vectors |
| 6107 | if (!getTarget().hasLegalHalfType() && |
| 6108 | (VT->getElementType()->isFloat16Type() || |
| 6109 | VT->getElementType()->isHalfType())) |
| 6110 | return coerceIllegalVector(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6111 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 6112 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 6113 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 6114 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 6115 | // half type natively, and does not need to interwork with AAPCS code. |
| 6116 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 6117 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6118 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 6119 | llvm::Type::getFloatTy(getVMContext()) : |
| 6120 | llvm::Type::getInt32Ty(getVMContext()); |
| 6121 | return ABIArgInfo::getDirect(ResType); |
| 6122 | } |
| 6123 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 6124 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6125 | // Treat an enum type as its underlying type. |
| 6126 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6127 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6128 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6129 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6130 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6131 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6132 | |
| 6133 | // Are we following APCS? |
| 6134 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6135 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6136 | return ABIArgInfo::getIgnore(); |
| 6137 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6138 | // Complex types are all returned as packed integers. |
| 6139 | // |
| 6140 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 6141 | // correctly. |
| 6142 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6143 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 6144 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6145 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6146 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6147 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6148 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6149 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6150 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6151 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6152 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6153 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6154 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6158 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6159 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6160 | |
| 6161 | // Otherwise this is an AAPCS variant. |
| 6162 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6163 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6164 | return ABIArgInfo::getIgnore(); |
| 6165 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6166 | // Check for homogeneous aggregates with AAPCS-VFP. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6167 | if (IsAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6168 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6169 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6170 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
| 6171 | return classifyHomogeneousAggregate(RetTy, Base, Members); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6174 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 6175 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6176 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6177 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 6178 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 6179 | // same size and alignment. |
| 6180 | if (getTarget().isRenderScriptTarget()) { |
| 6181 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 6182 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6183 | if (getDataLayout().isBigEndian()) |
| 6184 | // 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] | 6185 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6186 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6187 | // Return in the smallest viable integer type. |
| 6188 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6189 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6190 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6191 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6192 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6193 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6194 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6195 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6196 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6197 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6200 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6201 | } |
| 6202 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6203 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6204 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6205 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6206 | // On targets that don't support FP16, FP16 is expanded into float, and we |
| 6207 | // don't want the ABI to depend on whether or not FP16 is supported in |
| 6208 | // hardware. Thus return false to coerce FP16 vectors into integer vectors. |
| 6209 | if (!getTarget().hasLegalHalfType() && |
| 6210 | (VT->getElementType()->isFloat16Type() || |
| 6211 | VT->getElementType()->isHalfType())) |
| 6212 | return true; |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6213 | if (isAndroid()) { |
| 6214 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6215 | // vector ABI. The primary differences were that 3-element vector types |
| 6216 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6217 | // accepts that legacy behavior for Android only. |
| 6218 | // Check whether VT is legal. |
| 6219 | unsigned NumElements = VT->getNumElements(); |
| 6220 | // NumElements should be power of 2 or equal to 3. |
| 6221 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6222 | return true; |
| 6223 | } else { |
| 6224 | // Check whether VT is legal. |
| 6225 | unsigned NumElements = VT->getNumElements(); |
| 6226 | uint64_t Size = getContext().getTypeSize(VT); |
| 6227 | // NumElements should be power of 2. |
| 6228 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6229 | return true; |
| 6230 | // Size should be greater than 32 bits. |
| 6231 | return Size <= 32; |
| 6232 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6233 | } |
| 6234 | return false; |
| 6235 | } |
| 6236 | |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 6237 | /// Return true if a type contains any 16-bit floating point vectors |
| 6238 | bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const { |
| 6239 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 6240 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 6241 | if (NElements == 0) |
| 6242 | return false; |
| 6243 | return containsAnyFP16Vectors(AT->getElementType()); |
| 6244 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6245 | const RecordDecl *RD = RT->getDecl(); |
| 6246 | |
| 6247 | // If this is a C++ record, check the bases first. |
| 6248 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6249 | if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) { |
| 6250 | return containsAnyFP16Vectors(B.getType()); |
| 6251 | })) |
| 6252 | return true; |
| 6253 | |
| 6254 | if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) { |
| 6255 | return FD && containsAnyFP16Vectors(FD->getType()); |
| 6256 | })) |
| 6257 | return true; |
| 6258 | |
| 6259 | return false; |
| 6260 | } else { |
| 6261 | if (const VectorType *VT = Ty->getAs<VectorType>()) |
| 6262 | return (VT->getElementType()->isFloat16Type() || |
| 6263 | VT->getElementType()->isHalfType()); |
| 6264 | return false; |
| 6265 | } |
| 6266 | } |
| 6267 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6268 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6269 | llvm::Type *eltTy, |
| 6270 | unsigned numElts) const { |
| 6271 | if (!llvm::isPowerOf2_32(numElts)) |
| 6272 | return false; |
| 6273 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6274 | if (size > 64) |
| 6275 | return false; |
| 6276 | if (vectorSize.getQuantity() != 8 && |
| 6277 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6278 | return false; |
| 6279 | return true; |
| 6280 | } |
| 6281 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6282 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6283 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6284 | // double, or 64-bit or 128-bit vectors. |
| 6285 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6286 | if (BT->getKind() == BuiltinType::Float || |
| 6287 | BT->getKind() == BuiltinType::Double || |
| 6288 | BT->getKind() == BuiltinType::LongDouble) |
| 6289 | return true; |
| 6290 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6291 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6292 | if (VecSize == 64 || VecSize == 128) |
| 6293 | return true; |
| 6294 | } |
| 6295 | return false; |
| 6296 | } |
| 6297 | |
| 6298 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6299 | uint64_t Members) const { |
| 6300 | return Members <= 4; |
| 6301 | } |
| 6302 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6303 | bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention, |
| 6304 | bool acceptHalf) const { |
| 6305 | // Give precedence to user-specified calling conventions. |
| 6306 | if (callConvention != llvm::CallingConv::C) |
| 6307 | return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP); |
| 6308 | else |
| 6309 | return (getABIKind() == AAPCS_VFP) || |
| 6310 | (acceptHalf && (getABIKind() == AAPCS16_VFP)); |
| 6311 | } |
| 6312 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6313 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6314 | QualType Ty) const { |
| 6315 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6316 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6317 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6318 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6319 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6320 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6321 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6324 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 6325 | CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6326 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6327 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6328 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6329 | const Type *Base = nullptr; |
| 6330 | uint64_t Members = 0; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6331 | if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6332 | IsIndirect = true; |
| 6333 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6334 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6335 | // allocated by the caller. |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6336 | } else if (TySize > CharUnits::fromQuantity(16) && |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6337 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6338 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6339 | IsIndirect = true; |
| 6340 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6341 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6342 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6343 | // 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] | 6344 | // Our callers should be prepared to handle an under-aligned address. |
| 6345 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6346 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6347 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6348 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6349 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6350 | // ARMv7k allows type alignment up to 16 bytes. |
| 6351 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6352 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6353 | } else { |
| 6354 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6355 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6356 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6357 | std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6358 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6359 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6362 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6363 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6364 | //===----------------------------------------------------------------------===// |
| 6365 | |
| 6366 | namespace { |
| 6367 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6368 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6369 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6370 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6371 | |
| 6372 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6373 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6374 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6375 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6376 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6377 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6378 | }; |
| 6379 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6380 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6381 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6382 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6383 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6384 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6385 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6386 | CodeGen::CodeGenModule &M) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6387 | bool shouldEmitStaticExternCAliases() const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6388 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6389 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6390 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6391 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6392 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6393 | }; |
| 6394 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6395 | /// Checks if the type is unsupported directly by the current target. |
| 6396 | static bool isUnsupportedType(ASTContext &Context, QualType T) { |
| 6397 | if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type()) |
| 6398 | return true; |
Alexey Bataev | 7ae267d | 2019-06-18 19:04:27 +0000 | [diff] [blame] | 6399 | if (!Context.getTargetInfo().hasFloat128Type() && |
| 6400 | (T->isFloat128Type() || |
| 6401 | (T->isRealFloatingType() && Context.getTypeSize(T) == 128))) |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6402 | return true; |
| 6403 | if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() && |
| 6404 | Context.getTypeSize(T) > 64) |
| 6405 | return true; |
| 6406 | if (const auto *AT = T->getAsArrayTypeUnsafe()) |
| 6407 | return isUnsupportedType(Context, AT->getElementType()); |
| 6408 | const auto *RT = T->getAs<RecordType>(); |
| 6409 | if (!RT) |
| 6410 | return false; |
| 6411 | const RecordDecl *RD = RT->getDecl(); |
| 6412 | |
| 6413 | // If this is a C++ record, check the bases first. |
| 6414 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6415 | for (const CXXBaseSpecifier &I : CXXRD->bases()) |
| 6416 | if (isUnsupportedType(Context, I.getType())) |
| 6417 | return true; |
| 6418 | |
| 6419 | for (const FieldDecl *I : RD->fields()) |
| 6420 | if (isUnsupportedType(Context, I->getType())) |
| 6421 | return true; |
| 6422 | return false; |
| 6423 | } |
| 6424 | |
| 6425 | /// Coerce the given type into an array with maximum allowed size of elements. |
| 6426 | static ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, ASTContext &Context, |
| 6427 | llvm::LLVMContext &LLVMContext, |
| 6428 | unsigned MaxSize) { |
| 6429 | // Alignment and Size are measured in bits. |
| 6430 | const uint64_t Size = Context.getTypeSize(Ty); |
| 6431 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 6432 | const unsigned Div = std::min<unsigned>(MaxSize, Alignment); |
| 6433 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Div); |
| 6434 | const uint64_t NumElements = (Size + Div - 1) / Div; |
| 6435 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 6436 | } |
| 6437 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6438 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6439 | if (RetTy->isVoidType()) |
| 6440 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6441 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6442 | if (getContext().getLangOpts().OpenMP && |
| 6443 | getContext().getLangOpts().OpenMPIsDevice && |
| 6444 | isUnsupportedType(getContext(), RetTy)) |
| 6445 | return coerceToIntArrayWithLimit(RetTy, getContext(), getVMContext(), 64); |
| 6446 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6447 | // note: this is different from default ABI |
| 6448 | if (!RetTy->isScalarType()) |
| 6449 | return ABIArgInfo::getDirect(); |
| 6450 | |
| 6451 | // Treat an enum type as its underlying type. |
| 6452 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6453 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6454 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6455 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6456 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6457 | } |
| 6458 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6459 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6460 | // Treat an enum type as its underlying type. |
| 6461 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6462 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6463 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6464 | // Return aggregates type as indirect by value |
| 6465 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6466 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6467 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6468 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6469 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6472 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6473 | if (!getCXXABI().classifyReturnType(FI)) |
| 6474 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6475 | for (auto &I : FI.arguments()) |
| 6476 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6477 | |
| 6478 | // Always honor user-specified calling convention. |
| 6479 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6480 | return; |
| 6481 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6482 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6483 | } |
| 6484 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6485 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6486 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6487 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6490 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6491 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6492 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6493 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6494 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6495 | if (!FD) return; |
| 6496 | |
| 6497 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6498 | |
| 6499 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6500 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6501 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6502 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6503 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6504 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6505 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6506 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6507 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6508 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6509 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6510 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6511 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6512 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6513 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6514 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6515 | // __global__ functions cannot be called from the device, we do not |
| 6516 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6517 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6518 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6519 | addNVVMMetadata(F, "kernel", 1); |
| 6520 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6521 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6522 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6523 | llvm::APSInt MaxThreads(32); |
| 6524 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6525 | if (MaxThreads > 0) |
| 6526 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6527 | |
| 6528 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6529 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6530 | // we don't have to add a PTX directive. |
| 6531 | if (Attr->getMinBlocks()) { |
| 6532 | llvm::APSInt MinBlocks(32); |
| 6533 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6534 | if (MinBlocks > 0) |
| 6535 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6536 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6537 | } |
| 6538 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6539 | } |
| 6540 | } |
| 6541 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6542 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6543 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6544 | llvm::Module *M = F->getParent(); |
| 6545 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6546 | |
| 6547 | // Get "nvvm.annotations" metadata node |
| 6548 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6549 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6550 | llvm::Metadata *MDVals[] = { |
| 6551 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6552 | llvm::ConstantAsMetadata::get( |
| 6553 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6554 | // Append metadata to nvvm.annotations |
| 6555 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6556 | } |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6557 | |
| 6558 | bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 6559 | return false; |
| 6560 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6561 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6562 | |
| 6563 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6564 | // SystemZ ABI Implementation |
| 6565 | //===----------------------------------------------------------------------===// |
| 6566 | |
| 6567 | namespace { |
| 6568 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6569 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6570 | bool HasVector; |
| 6571 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6572 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6573 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6574 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6575 | |
| 6576 | bool isPromotableIntegerType(QualType Ty) const; |
| 6577 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6578 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6579 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6580 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6581 | |
| 6582 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6583 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6584 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6585 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6586 | if (!getCXXABI().classifyReturnType(FI)) |
| 6587 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6588 | for (auto &I : FI.arguments()) |
| 6589 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6590 | } |
| 6591 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6592 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6593 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6594 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6595 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6596 | bool asReturnValue) const override { |
| 6597 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6598 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6599 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6600 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6601 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6602 | }; |
| 6603 | |
| 6604 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6605 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6606 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6607 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6608 | }; |
| 6609 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6610 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6611 | |
| 6612 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6613 | // Treat an enum type as its underlying type. |
| 6614 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6615 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6616 | |
| 6617 | // Promotable integer types are required to be promoted by the ABI. |
| 6618 | if (Ty->isPromotableIntegerType()) |
| 6619 | return true; |
| 6620 | |
| 6621 | // 32-bit values must also be promoted. |
| 6622 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6623 | switch (BT->getKind()) { |
| 6624 | case BuiltinType::Int: |
| 6625 | case BuiltinType::UInt: |
| 6626 | return true; |
| 6627 | default: |
| 6628 | return false; |
| 6629 | } |
| 6630 | return false; |
| 6631 | } |
| 6632 | |
| 6633 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6634 | return (Ty->isAnyComplexType() || |
| 6635 | Ty->isVectorType() || |
| 6636 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6637 | } |
| 6638 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6639 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6640 | return (HasVector && |
| 6641 | Ty->isVectorType() && |
| 6642 | getContext().getTypeSize(Ty) <= 128); |
| 6643 | } |
| 6644 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6645 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6646 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6647 | switch (BT->getKind()) { |
| 6648 | case BuiltinType::Float: |
| 6649 | case BuiltinType::Double: |
| 6650 | return true; |
| 6651 | default: |
| 6652 | return false; |
| 6653 | } |
| 6654 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6655 | return false; |
| 6656 | } |
| 6657 | |
| 6658 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6659 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6660 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6661 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6662 | |
| 6663 | // If this is a C++ record, check the bases first. |
| 6664 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6665 | for (const auto &I : CXXRD->bases()) { |
| 6666 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6667 | |
| 6668 | // Empty bases don't affect things either way. |
| 6669 | if (isEmptyRecord(getContext(), Base, true)) |
| 6670 | continue; |
| 6671 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6672 | if (!Found.isNull()) |
| 6673 | return Ty; |
| 6674 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6675 | } |
| 6676 | |
| 6677 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6678 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6679 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6680 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6681 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6682 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 6683 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6684 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6685 | |
| 6686 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6687 | // Nested structures still do though. |
| 6688 | if (!Found.isNull()) |
| 6689 | return Ty; |
| 6690 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6694 | // 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] | 6695 | if (!Found.isNull()) |
| 6696 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6697 | } |
| 6698 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6699 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6700 | } |
| 6701 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6702 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6703 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6704 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6705 | // struct { |
| 6706 | // i64 __gpr; |
| 6707 | // i64 __fpr; |
| 6708 | // i8 *__overflow_arg_area; |
| 6709 | // i8 *__reg_save_area; |
| 6710 | // }; |
| 6711 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6712 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6713 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6714 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6715 | Ty = getContext().getCanonicalType(Ty); |
| 6716 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6717 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6718 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6719 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6720 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6721 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6722 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6723 | CharUnits UnpaddedSize; |
| 6724 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6725 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6726 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6727 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6728 | } else { |
| 6729 | if (AI.getCoerceToType()) |
| 6730 | ArgTy = AI.getCoerceToType(); |
| 6731 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6732 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6733 | UnpaddedSize = TyInfo.first; |
| 6734 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6735 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6736 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6737 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6738 | PaddedSize = CharUnits::fromQuantity(16); |
| 6739 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6740 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6741 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6742 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6743 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6744 | llvm::Value *PaddedSizeV = |
| 6745 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6746 | |
| 6747 | if (IsVector) { |
| 6748 | // Work out the address of a vector argument on the stack. |
| 6749 | // Vector arguments are always passed in the high bits of a |
| 6750 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6751 | Address OverflowArgAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6752 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6753 | Address OverflowArgArea = |
| 6754 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6755 | TyInfo.second); |
| 6756 | Address MemAddr = |
| 6757 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6758 | |
| 6759 | // Update overflow_arg_area_ptr pointer |
| 6760 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6761 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6762 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6763 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6764 | |
| 6765 | return MemAddr; |
| 6766 | } |
| 6767 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6768 | assert(PaddedSize.getQuantity() == 8); |
| 6769 | |
| 6770 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6771 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6772 | if (InFPRs) { |
| 6773 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6774 | RegCountField = 1; // __fpr |
| 6775 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6776 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6777 | } else { |
| 6778 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6779 | RegCountField = 0; // __gpr |
| 6780 | RegSaveIndex = 2; // save offset for r2 |
| 6781 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6782 | } |
| 6783 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6784 | Address RegCountPtr = |
| 6785 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6786 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6787 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6788 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6789 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6790 | |
| 6791 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6792 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6793 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6794 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6795 | |
| 6796 | // Emit code to load the value if it was passed in registers. |
| 6797 | CGF.EmitBlock(InRegBlock); |
| 6798 | |
| 6799 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6800 | llvm::Value *ScaledRegCount = |
| 6801 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6802 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6803 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6804 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6805 | llvm::Value *RegOffset = |
| 6806 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6807 | Address RegSaveAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6808 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6809 | llvm::Value *RegSaveArea = |
| 6810 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6811 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6812 | "raw_reg_addr"), |
| 6813 | PaddedSize); |
| 6814 | Address RegAddr = |
| 6815 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6816 | |
| 6817 | // Update the register count |
| 6818 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6819 | llvm::Value *NewRegCount = |
| 6820 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6821 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6822 | CGF.EmitBranch(ContBlock); |
| 6823 | |
| 6824 | // Emit code to load the value if it was passed in memory. |
| 6825 | CGF.EmitBlock(InMemBlock); |
| 6826 | |
| 6827 | // Work out the address of a stack argument. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6828 | Address OverflowArgAreaPtr = |
| 6829 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6830 | Address OverflowArgArea = |
| 6831 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6832 | PaddedSize); |
| 6833 | Address RawMemAddr = |
| 6834 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6835 | Address MemAddr = |
| 6836 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6837 | |
| 6838 | // Update overflow_arg_area_ptr pointer |
| 6839 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6840 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6841 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6842 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6843 | CGF.EmitBranch(ContBlock); |
| 6844 | |
| 6845 | // Return the appropriate result. |
| 6846 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6847 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6848 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6849 | |
| 6850 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6851 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6852 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6853 | |
| 6854 | return ResAddr; |
| 6855 | } |
| 6856 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6857 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6858 | if (RetTy->isVoidType()) |
| 6859 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6860 | if (isVectorArgumentType(RetTy)) |
| 6861 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6862 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6863 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6864 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6865 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6869 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6870 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6871 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6872 | |
| 6873 | // Integers and enums are extended to full register width. |
| 6874 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6875 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6876 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6877 | // Handle vector types and vector-like structure types. Note that |
| 6878 | // as opposed to float-like structure types, we do not allow any |
| 6879 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6880 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6881 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6882 | if (isVectorArgumentType(SingleElementTy) && |
| 6883 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6884 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6885 | |
| 6886 | // 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] | 6887 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6888 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6889 | |
| 6890 | // Handle small structures. |
| 6891 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6892 | // Structures with flexible arrays have variable length, so really |
| 6893 | // fail the size test above. |
| 6894 | const RecordDecl *RD = RT->getDecl(); |
| 6895 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6896 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6897 | |
| 6898 | // The structure is passed as an unextended integer, a float, or a double. |
| 6899 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6900 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6901 | assert(Size == 32 || Size == 64); |
| 6902 | if (Size == 32) |
| 6903 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6904 | else |
| 6905 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6906 | } else |
| 6907 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6908 | return ABIArgInfo::getDirect(PassTy); |
| 6909 | } |
| 6910 | |
| 6911 | // Non-structure compounds are passed indirectly. |
| 6912 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6913 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6914 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6915 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6916 | } |
| 6917 | |
| 6918 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6919 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6920 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6921 | |
| 6922 | namespace { |
| 6923 | |
| 6924 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6925 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6926 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6927 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6928 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6929 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6930 | }; |
| 6931 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6932 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6933 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6934 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6935 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6936 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6937 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6938 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6939 | const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); |
| 6940 | if (!InterruptAttr) |
| 6941 | return; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6942 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6943 | // Handle 'interrupt' attribute: |
| 6944 | llvm::Function *F = cast<llvm::Function>(GV); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6945 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6946 | // Step 1: Set ISR calling convention. |
| 6947 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6948 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6949 | // Step 2: Add attributes goodness. |
| 6950 | F->addFnAttr(llvm::Attribute::NoInline); |
| 6951 | F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6952 | } |
| 6953 | } |
| 6954 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6955 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6956 | // MIPS ABI Implementation. This works for both little-endian and |
| 6957 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6958 | //===----------------------------------------------------------------------===// |
| 6959 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6960 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6961 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6962 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6963 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6964 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6965 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6966 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6967 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6968 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6969 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6970 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6971 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6972 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6973 | |
| 6974 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6975 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6976 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6977 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6978 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6979 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6980 | }; |
| 6981 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6982 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6983 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6984 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6985 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6986 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6987 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6988 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6989 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6990 | return 29; |
| 6991 | } |
| 6992 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6993 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6994 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6995 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6996 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6997 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6998 | |
| 6999 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 7000 | Fn->addFnAttr("long-call"); |
| 7001 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 7002 | Fn->addFnAttr("short-call"); |
| 7003 | |
| 7004 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7005 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7006 | return; |
| 7007 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 7008 | if (FD->hasAttr<Mips16Attr>()) { |
| 7009 | Fn->addFnAttr("mips16"); |
| 7010 | } |
| 7011 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 7012 | Fn->addFnAttr("nomips16"); |
| 7013 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 7014 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 7015 | if (FD->hasAttr<MicroMipsAttr>()) |
| 7016 | Fn->addFnAttr("micromips"); |
| 7017 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 7018 | Fn->addFnAttr("nomicromips"); |
| 7019 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 7020 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 7021 | if (!Attr) |
| 7022 | return; |
| 7023 | |
| 7024 | const char *Kind; |
| 7025 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 7026 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 7027 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 7028 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 7029 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 7030 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 7031 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 7032 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 7033 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 7034 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 7035 | } |
| 7036 | |
| 7037 | Fn->addFnAttr("interrupt", Kind); |
| 7038 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 7039 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 7040 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7041 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7042 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 7043 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7044 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 7045 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 7046 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7047 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7048 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7049 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7050 | void MipsABIInfo::CoerceToIntArgs( |
| 7051 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7052 | llvm::IntegerType *IntTy = |
| 7053 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7054 | |
| 7055 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 7056 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 7057 | ArgList.push_back(IntTy); |
| 7058 | |
| 7059 | // If necessary, add one more integer type to ArgList. |
| 7060 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 7061 | |
| 7062 | if (R) |
| 7063 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7066 | // In N32/64, an aligned double precision floating point field is passed in |
| 7067 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7068 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7069 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 7070 | |
| 7071 | if (IsO32) { |
| 7072 | CoerceToIntArgs(TySize, ArgList); |
| 7073 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7074 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7075 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 7076 | if (Ty->isComplexType()) |
| 7077 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 7078 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 7079 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7080 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7081 | // Unions/vectors are passed in integer registers. |
| 7082 | if (!RT || !RT->isStructureOrClassType()) { |
| 7083 | CoerceToIntArgs(TySize, ArgList); |
| 7084 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7085 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7086 | |
| 7087 | const RecordDecl *RD = RT->getDecl(); |
| 7088 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7089 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7090 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7091 | uint64_t LastOffset = 0; |
| 7092 | unsigned idx = 0; |
| 7093 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 7094 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 7095 | // Iterate over fields in the struct/class and check if there are any aligned |
| 7096 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7097 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 7098 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7099 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7100 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 7101 | |
| 7102 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 7103 | continue; |
| 7104 | |
| 7105 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 7106 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 7107 | continue; |
| 7108 | |
| 7109 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 7110 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 7111 | ArgList.push_back(I64); |
| 7112 | |
| 7113 | // Add double type. |
| 7114 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 7115 | LastOffset = Offset + 64; |
| 7116 | } |
| 7117 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7118 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 7119 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7120 | |
| 7121 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7122 | } |
| 7123 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7124 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 7125 | uint64_t Offset) const { |
| 7126 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7127 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7128 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7129 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7130 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 7131 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7132 | ABIArgInfo |
| 7133 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 7134 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7135 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7136 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7137 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7138 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7139 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7140 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 7141 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7142 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 7143 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7144 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7145 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7146 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7147 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7148 | return ABIArgInfo::getIgnore(); |
| 7149 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 7150 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7151 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7152 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7153 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 7154 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7155 | // If we have reached here, aggregates are passed directly by coercing to |
| 7156 | // another structure type. Padding is inserted if the offset of the |
| 7157 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 7158 | ABIArgInfo ArgInfo = |
| 7159 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 7160 | getPaddingType(OrigOffset, CurrOffset)); |
| 7161 | ArgInfo.setInReg(true); |
| 7162 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7163 | } |
| 7164 | |
| 7165 | // Treat an enum type as its underlying type. |
| 7166 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7167 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7168 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 7169 | // All integral types are promoted to the GPR width. |
| 7170 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7171 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7172 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7173 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7174 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7177 | llvm::Type* |
| 7178 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7179 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7180 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7181 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7182 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7183 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7184 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 7185 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7186 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7187 | // N32/64 returns struct/classes in floating point registers if the |
| 7188 | // following conditions are met: |
| 7189 | // 1. The size of the struct/class is no larger than 128-bit. |
| 7190 | // 2. The struct/class has one or two fields all of which are floating |
| 7191 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7192 | // 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] | 7193 | // |
| 7194 | // Any other composite results are returned in integer registers. |
| 7195 | // |
| 7196 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 7197 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 7198 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7199 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7200 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7201 | if (!BT || !BT->isFloatingPoint()) |
| 7202 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7203 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7204 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7205 | } |
| 7206 | |
| 7207 | if (b == e) |
| 7208 | return llvm::StructType::get(getVMContext(), RTList, |
| 7209 | RD->hasAttr<PackedAttr>()); |
| 7210 | |
| 7211 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7212 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7213 | } |
| 7214 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7215 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7216 | return llvm::StructType::get(getVMContext(), RTList); |
| 7217 | } |
| 7218 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7219 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 7220 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7221 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 7222 | if (RetTy->isVoidType()) |
| 7223 | return ABIArgInfo::getIgnore(); |
| 7224 | |
| 7225 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 7226 | // However, N32/N64 ignores zero sized return values. |
| 7227 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7228 | return ABIArgInfo::getIgnore(); |
| 7229 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 7230 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7231 | if (Size <= 128) { |
| 7232 | if (RetTy->isAnyComplexType()) |
| 7233 | return ABIArgInfo::getDirect(); |
| 7234 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7235 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 7236 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7237 | if (!IsO32 || |
| 7238 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 7239 | ABIArgInfo ArgInfo = |
| 7240 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 7241 | ArgInfo.setInReg(true); |
| 7242 | return ArgInfo; |
| 7243 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7244 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7245 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7246 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7247 | } |
| 7248 | |
| 7249 | // Treat an enum type as its underlying type. |
| 7250 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7251 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7252 | |
Stefan Maksimovic | b9da8a5 | 2018-07-30 10:44:46 +0000 | [diff] [blame] | 7253 | if (RetTy->isPromotableIntegerType()) |
| 7254 | return ABIArgInfo::getExtend(RetTy); |
| 7255 | |
| 7256 | if ((RetTy->isUnsignedIntegerOrEnumerationType() || |
| 7257 | RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) |
| 7258 | return ABIArgInfo::getSignExtend(RetTy); |
| 7259 | |
| 7260 | return ABIArgInfo::getDirect(); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7264 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7265 | if (!getCXXABI().classifyReturnType(FI)) |
| 7266 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7267 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7268 | // 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] | 7269 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7270 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7271 | for (auto &I : FI.arguments()) |
| 7272 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7273 | } |
| 7274 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7275 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7276 | QualType OrigTy) const { |
| 7277 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7278 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7279 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 7280 | // 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] | 7281 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7282 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7283 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7284 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7285 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7286 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7287 | DidPromote = true; |
| 7288 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7289 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7290 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7291 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7292 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7293 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7294 | // The alignment of things in the argument area is never larger than |
| 7295 | // StackAlignInBytes. |
| 7296 | TyInfo.second = |
| 7297 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7298 | |
| 7299 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7300 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7301 | |
| 7302 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7303 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7304 | |
| 7305 | |
| 7306 | // If there was a promotion, "unpromote" into a temporary. |
| 7307 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7308 | if (DidPromote) { |
| 7309 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7310 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7311 | |
| 7312 | // Truncate down to the right width. |
| 7313 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7314 | : CGF.IntPtrTy); |
| 7315 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7316 | if (OrigTy->isPointerType()) |
| 7317 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7318 | |
| 7319 | CGF.Builder.CreateStore(V, Temp); |
| 7320 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7321 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7322 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7323 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7324 | } |
| 7325 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7326 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7327 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7328 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7329 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7330 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7331 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7332 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7333 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7334 | } |
| 7335 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7336 | bool |
| 7337 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7338 | llvm::Value *Address) const { |
| 7339 | // This information comes from gcc's implementation, which seems to |
| 7340 | // as canonical as it gets. |
| 7341 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7342 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7343 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7344 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7345 | |
| 7346 | // 0-31 are the general purpose registers, $0 - $31. |
| 7347 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7348 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7349 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7350 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7351 | |
| 7352 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7353 | // They are one bit wide and ignored here. |
| 7354 | |
| 7355 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7356 | // (coprocessor 1 is the FP unit) |
| 7357 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7358 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7359 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7360 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7361 | return false; |
| 7362 | } |
| 7363 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7364 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7365 | // AVR ABI Implementation. |
| 7366 | //===----------------------------------------------------------------------===// |
| 7367 | |
| 7368 | namespace { |
| 7369 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7370 | public: |
| 7371 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7372 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7373 | |
| 7374 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7375 | CodeGen::CodeGenModule &CGM) const override { |
| 7376 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7377 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7378 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7379 | if (!FD) return; |
| 7380 | auto *Fn = cast<llvm::Function>(GV); |
| 7381 | |
| 7382 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7383 | Fn->addFnAttr("interrupt"); |
| 7384 | |
| 7385 | if (FD->getAttr<AVRSignalAttr>()) |
| 7386 | Fn->addFnAttr("signal"); |
| 7387 | } |
| 7388 | }; |
| 7389 | } |
| 7390 | |
| 7391 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7392 | // 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] | 7393 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7394 | // handling. |
| 7395 | //===----------------------------------------------------------------------===// |
| 7396 | |
| 7397 | namespace { |
| 7398 | |
| 7399 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7400 | public: |
| 7401 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7402 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7403 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7404 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7405 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7406 | }; |
| 7407 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7408 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7409 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7410 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7411 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7412 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7413 | if (!FD) return; |
| 7414 | |
| 7415 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7416 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7417 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7418 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7419 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7420 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7421 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7422 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7423 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7424 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7425 | llvm::NamedMDNode *OpenCLMetadata = |
| 7426 | M.getModule().getOrInsertNamedMetadata( |
| 7427 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7428 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7429 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7430 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7431 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7432 | Operands.push_back( |
| 7433 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7434 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7435 | Operands.push_back( |
| 7436 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7437 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7438 | Operands.push_back( |
| 7439 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7440 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7441 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7442 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7443 | // (false) for implementing the work_group_size_hint attr later. |
| 7444 | // 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] | 7445 | Operands.push_back( |
| 7446 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7447 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7448 | } |
| 7449 | } |
| 7450 | } |
| 7451 | } |
| 7452 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7453 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7454 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7455 | //===----------------------------------------------------------------------===// |
| 7456 | // Hexagon ABI Implementation |
| 7457 | //===----------------------------------------------------------------------===// |
| 7458 | |
| 7459 | namespace { |
| 7460 | |
| 7461 | class HexagonABIInfo : public ABIInfo { |
| 7462 | |
| 7463 | |
| 7464 | public: |
| 7465 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7466 | |
| 7467 | private: |
| 7468 | |
| 7469 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7470 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7471 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7472 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7473 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7474 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7475 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7476 | }; |
| 7477 | |
| 7478 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7479 | public: |
| 7480 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7481 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7482 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7483 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7484 | return 29; |
| 7485 | } |
| 7486 | }; |
| 7487 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7488 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7489 | |
| 7490 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7491 | if (!getCXXABI().classifyReturnType(FI)) |
| 7492 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7493 | for (auto &I : FI.arguments()) |
| 7494 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7495 | } |
| 7496 | |
| 7497 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7498 | if (!isAggregateTypeForABI(Ty)) { |
| 7499 | // Treat an enum type as its underlying type. |
| 7500 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7501 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7502 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7503 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7504 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7505 | } |
| 7506 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7507 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7508 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7509 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7510 | // Ignore empty records. |
| 7511 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7512 | return ABIArgInfo::getIgnore(); |
| 7513 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7514 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7515 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7516 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7517 | // Pass in the smallest viable integer type. |
| 7518 | else if (Size > 32) |
| 7519 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7520 | else if (Size > 16) |
| 7521 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7522 | else if (Size > 8) |
| 7523 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7524 | else |
| 7525 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7526 | } |
| 7527 | |
| 7528 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7529 | if (RetTy->isVoidType()) |
| 7530 | return ABIArgInfo::getIgnore(); |
| 7531 | |
| 7532 | // Large vector types should be returned via memory. |
| 7533 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7534 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7535 | |
| 7536 | if (!isAggregateTypeForABI(RetTy)) { |
| 7537 | // Treat an enum type as its underlying type. |
| 7538 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7539 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7540 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7541 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7542 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7543 | } |
| 7544 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7545 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7546 | return ABIArgInfo::getIgnore(); |
| 7547 | |
| 7548 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7549 | // are returned indirectly. |
| 7550 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7551 | if (Size <= 64) { |
| 7552 | // Return in the smallest viable integer type. |
| 7553 | if (Size <= 8) |
| 7554 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7555 | if (Size <= 16) |
| 7556 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7557 | if (Size <= 32) |
| 7558 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7559 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7560 | } |
| 7561 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7562 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7563 | } |
| 7564 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7565 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7566 | QualType Ty) const { |
| 7567 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7568 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7569 | getContext().getTypeInfoInChars(Ty), |
| 7570 | CharUnits::fromQuantity(4), |
| 7571 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7572 | } |
| 7573 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7574 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7575 | // Lanai ABI Implementation |
| 7576 | //===----------------------------------------------------------------------===// |
| 7577 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7578 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7579 | class LanaiABIInfo : public DefaultABIInfo { |
| 7580 | public: |
| 7581 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7582 | |
| 7583 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7584 | |
| 7585 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7586 | CCState State(FI.getCallingConvention()); |
| 7587 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7588 | // regparm attribute set. |
| 7589 | if (FI.getHasRegParm()) { |
| 7590 | State.FreeRegs = FI.getRegParm(); |
| 7591 | } else { |
| 7592 | State.FreeRegs = 4; |
| 7593 | } |
| 7594 | |
| 7595 | if (!getCXXABI().classifyReturnType(FI)) |
| 7596 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7597 | for (auto &I : FI.arguments()) |
| 7598 | I.info = classifyArgumentType(I.type, State); |
| 7599 | } |
| 7600 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7601 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7602 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7603 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7604 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7605 | |
| 7606 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7607 | unsigned Size = getContext().getTypeSize(Ty); |
| 7608 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7609 | |
| 7610 | if (SizeInRegs == 0) |
| 7611 | return false; |
| 7612 | |
| 7613 | if (SizeInRegs > State.FreeRegs) { |
| 7614 | State.FreeRegs = 0; |
| 7615 | return false; |
| 7616 | } |
| 7617 | |
| 7618 | State.FreeRegs -= SizeInRegs; |
| 7619 | |
| 7620 | return true; |
| 7621 | } |
| 7622 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7623 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7624 | CCState &State) const { |
| 7625 | if (!ByVal) { |
| 7626 | if (State.FreeRegs) { |
| 7627 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7628 | return getNaturalAlignIndirectInReg(Ty); |
| 7629 | } |
| 7630 | return getNaturalAlignIndirect(Ty, false); |
| 7631 | } |
| 7632 | |
| 7633 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7634 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7635 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7636 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7637 | /*Realign=*/TypeAlign > |
| 7638 | MinABIStackAlignInBytes); |
| 7639 | } |
| 7640 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7641 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7642 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7643 | // Check with the C++ ABI first. |
| 7644 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7645 | if (RT) { |
| 7646 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7647 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7648 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7649 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7650 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7651 | } |
| 7652 | } |
| 7653 | |
| 7654 | if (isAggregateTypeForABI(Ty)) { |
| 7655 | // Structures with flexible arrays are always indirect. |
| 7656 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7657 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7658 | |
| 7659 | // Ignore empty structs/unions. |
| 7660 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7661 | return ABIArgInfo::getIgnore(); |
| 7662 | |
| 7663 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7664 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7665 | if (SizeInRegs <= State.FreeRegs) { |
| 7666 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7667 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7668 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7669 | State.FreeRegs -= SizeInRegs; |
| 7670 | return ABIArgInfo::getDirectInReg(Result); |
| 7671 | } else { |
| 7672 | State.FreeRegs = 0; |
| 7673 | } |
| 7674 | return getIndirectResult(Ty, true, State); |
| 7675 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7676 | |
| 7677 | // Treat an enum type as its underlying type. |
| 7678 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7679 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7680 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7681 | bool InReg = shouldUseInReg(Ty, State); |
| 7682 | if (Ty->isPromotableIntegerType()) { |
| 7683 | if (InReg) |
| 7684 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7685 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7686 | } |
| 7687 | if (InReg) |
| 7688 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7689 | return ABIArgInfo::getDirect(); |
| 7690 | } |
| 7691 | |
| 7692 | namespace { |
| 7693 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7694 | public: |
| 7695 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7696 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7697 | }; |
| 7698 | } |
| 7699 | |
| 7700 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7701 | // AMDGPU ABI Implementation |
| 7702 | //===----------------------------------------------------------------------===// |
| 7703 | |
| 7704 | namespace { |
| 7705 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7706 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7707 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7708 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7709 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7710 | unsigned numRegsForType(QualType Ty) const; |
| 7711 | |
| 7712 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7713 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7714 | uint64_t Members) const override; |
| 7715 | |
Michael Liao | 15140e4 | 2019-11-04 11:41:07 -0500 | [diff] [blame] | 7716 | // Coerce HIP pointer arguments from generic pointers to global ones. |
| 7717 | llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS, |
| 7718 | unsigned ToAS) const { |
| 7719 | // Structure types. |
| 7720 | if (auto STy = dyn_cast<llvm::StructType>(Ty)) { |
| 7721 | SmallVector<llvm::Type *, 8> EltTys; |
| 7722 | bool Changed = false; |
| 7723 | for (auto T : STy->elements()) { |
| 7724 | auto NT = coerceKernelArgumentType(T, FromAS, ToAS); |
| 7725 | EltTys.push_back(NT); |
| 7726 | Changed |= (NT != T); |
| 7727 | } |
| 7728 | // Skip if there is no change in element types. |
| 7729 | if (!Changed) |
| 7730 | return STy; |
| 7731 | if (STy->hasName()) |
| 7732 | return llvm::StructType::create( |
| 7733 | EltTys, (STy->getName() + ".coerce").str(), STy->isPacked()); |
| 7734 | return llvm::StructType::get(getVMContext(), EltTys, STy->isPacked()); |
| 7735 | } |
| 7736 | // Arrary types. |
| 7737 | if (auto ATy = dyn_cast<llvm::ArrayType>(Ty)) { |
| 7738 | auto T = ATy->getElementType(); |
| 7739 | auto NT = coerceKernelArgumentType(T, FromAS, ToAS); |
| 7740 | // Skip if there is no change in that element type. |
| 7741 | if (NT == T) |
| 7742 | return ATy; |
| 7743 | return llvm::ArrayType::get(NT, ATy->getNumElements()); |
| 7744 | } |
| 7745 | // Single value types. |
| 7746 | if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS) |
| 7747 | return llvm::PointerType::get( |
| 7748 | cast<llvm::PointerType>(Ty)->getElementType(), ToAS); |
| 7749 | return Ty; |
| 7750 | } |
| 7751 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7752 | public: |
| 7753 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7754 | DefaultABIInfo(CGT) {} |
| 7755 | |
| 7756 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7757 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7758 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7759 | |
| 7760 | void computeInfo(CGFunctionInfo &FI) const override; |
Michael Liao | 5a48678 | 2019-10-24 11:41:11 -0400 | [diff] [blame] | 7761 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7762 | QualType Ty) const override; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7763 | }; |
| 7764 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7765 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7766 | return true; |
| 7767 | } |
| 7768 | |
| 7769 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7770 | const Type *Base, uint64_t Members) const { |
| 7771 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7772 | |
| 7773 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7774 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7775 | } |
| 7776 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7777 | /// Estimate number of registers the type will use when passed in registers. |
| 7778 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7779 | unsigned NumRegs = 0; |
| 7780 | |
| 7781 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7782 | // Compute from the number of elements. The reported size is based on the |
| 7783 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7784 | QualType EltTy = VT->getElementType(); |
| 7785 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7786 | |
| 7787 | // 16-bit element vectors should be passed as packed. |
| 7788 | if (EltSize == 16) |
| 7789 | return (VT->getNumElements() + 1) / 2; |
| 7790 | |
| 7791 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7792 | return EltNumRegs * VT->getNumElements(); |
| 7793 | } |
| 7794 | |
| 7795 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7796 | const RecordDecl *RD = RT->getDecl(); |
| 7797 | assert(!RD->hasFlexibleArrayMember()); |
| 7798 | |
| 7799 | for (const FieldDecl *Field : RD->fields()) { |
| 7800 | QualType FieldTy = Field->getType(); |
| 7801 | NumRegs += numRegsForType(FieldTy); |
| 7802 | } |
| 7803 | |
| 7804 | return NumRegs; |
| 7805 | } |
| 7806 | |
| 7807 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7808 | } |
| 7809 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7810 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7811 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7812 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7813 | if (!getCXXABI().classifyReturnType(FI)) |
| 7814 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7815 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7816 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7817 | for (auto &Arg : FI.arguments()) { |
| 7818 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7819 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7820 | } else { |
| 7821 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7822 | } |
| 7823 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7824 | } |
| 7825 | |
Michael Liao | 5a48678 | 2019-10-24 11:41:11 -0400 | [diff] [blame] | 7826 | Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7827 | QualType Ty) const { |
| 7828 | llvm_unreachable("AMDGPU does not support varargs"); |
| 7829 | } |
| 7830 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7831 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7832 | if (isAggregateTypeForABI(RetTy)) { |
| 7833 | // Records with non-trivial destructors/copy-constructors should not be |
| 7834 | // returned by value. |
| 7835 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7836 | // Ignore empty structs/unions. |
| 7837 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7838 | return ABIArgInfo::getIgnore(); |
| 7839 | |
| 7840 | // Lower single-element structs to just return a regular value. |
| 7841 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7842 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7843 | |
| 7844 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7845 | const RecordDecl *RD = RT->getDecl(); |
| 7846 | if (RD->hasFlexibleArrayMember()) |
| 7847 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7848 | } |
| 7849 | |
| 7850 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7851 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7852 | if (Size <= 16) |
| 7853 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7854 | |
| 7855 | if (Size <= 32) |
| 7856 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7857 | |
| 7858 | if (Size <= 64) { |
| 7859 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7860 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7861 | } |
| 7862 | |
| 7863 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7864 | return ABIArgInfo::getDirect(); |
| 7865 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7866 | } |
| 7867 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7868 | // Otherwise just do the default thing. |
| 7869 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7870 | } |
| 7871 | |
| 7872 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7873 | /// make sense to pass anything byval, so everything must be direct. |
| 7874 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7875 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7876 | |
| 7877 | // TODO: Can we omit empty structs? |
| 7878 | |
Michael Liao | 15140e4 | 2019-11-04 11:41:07 -0500 | [diff] [blame] | 7879 | llvm::Type *LTy = nullptr; |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7880 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
Michael Liao | 15140e4 | 2019-11-04 11:41:07 -0500 | [diff] [blame] | 7881 | LTy = CGT.ConvertType(QualType(SeltTy, 0)); |
| 7882 | |
| 7883 | if (getContext().getLangOpts().HIP) { |
| 7884 | if (!LTy) |
| 7885 | LTy = CGT.ConvertType(Ty); |
| 7886 | LTy = coerceKernelArgumentType( |
| 7887 | LTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default), |
| 7888 | /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device)); |
| 7889 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7890 | |
| 7891 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7892 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7893 | // 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] | 7894 | return ABIArgInfo::getDirect(LTy, 0, nullptr, false); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7895 | } |
| 7896 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7897 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7898 | unsigned &NumRegsLeft) const { |
| 7899 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7900 | |
| 7901 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7902 | |
| 7903 | if (isAggregateTypeForABI(Ty)) { |
| 7904 | // Records with non-trivial destructors/copy-constructors should not be |
| 7905 | // passed by value. |
| 7906 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7907 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7908 | |
| 7909 | // Ignore empty structs/unions. |
| 7910 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7911 | return ABIArgInfo::getIgnore(); |
| 7912 | |
| 7913 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7914 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7915 | // though watch out for things like bitfields. |
| 7916 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7917 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7918 | |
| 7919 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7920 | const RecordDecl *RD = RT->getDecl(); |
| 7921 | if (RD->hasFlexibleArrayMember()) |
| 7922 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7923 | } |
| 7924 | |
| 7925 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7926 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7927 | if (Size <= 64) { |
| 7928 | unsigned NumRegs = (Size + 31) / 32; |
| 7929 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7930 | |
| 7931 | if (Size <= 16) |
| 7932 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7933 | |
| 7934 | if (Size <= 32) |
| 7935 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7936 | |
| 7937 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7938 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7939 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7940 | } |
| 7941 | |
| 7942 | if (NumRegsLeft > 0) { |
| 7943 | unsigned NumRegs = numRegsForType(Ty); |
| 7944 | if (NumRegsLeft >= NumRegs) { |
| 7945 | NumRegsLeft -= NumRegs; |
| 7946 | return ABIArgInfo::getDirect(); |
| 7947 | } |
| 7948 | } |
| 7949 | } |
| 7950 | |
| 7951 | // Otherwise just do the default thing. |
| 7952 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7953 | if (!ArgInfo.isIndirect()) { |
| 7954 | unsigned NumRegs = numRegsForType(Ty); |
| 7955 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7956 | } |
| 7957 | |
| 7958 | return ArgInfo; |
| 7959 | } |
| 7960 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7961 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7962 | public: |
| 7963 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7964 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7965 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7966 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7967 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7968 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7969 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7970 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7971 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7972 | LangAS getASTAllocaAddressSpace() const override { |
| 7973 | return getLangASFromTargetAS( |
| 7974 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7975 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7976 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7977 | const VarDecl *D) const override; |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 7978 | llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 7979 | SyncScope Scope, |
| 7980 | llvm::AtomicOrdering Ordering, |
| 7981 | llvm::LLVMContext &Ctx) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7982 | llvm::Function * |
| 7983 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7984 | llvm::Function *BlockInvokeFunc, |
| 7985 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7986 | bool shouldEmitStaticExternCAliases() const override; |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7987 | void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7988 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7989 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7990 | |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7991 | static bool requiresAMDGPUProtectedVisibility(const Decl *D, |
| 7992 | llvm::GlobalValue *GV) { |
| 7993 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 7994 | return false; |
| 7995 | |
| 7996 | return D->hasAttr<OpenCLKernelAttr>() || |
| 7997 | (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) || |
Michael Liao | 3820506 | 2019-04-26 19:31:48 +0000 | [diff] [blame] | 7998 | (isa<VarDecl>(D) && |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 7999 | (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || |
| 8000 | D->hasAttr<HIPPinnedShadowAttr>())); |
| 8001 | } |
| 8002 | |
| 8003 | static bool requiresAMDGPUDefaultVisibility(const Decl *D, |
| 8004 | llvm::GlobalValue *GV) { |
| 8005 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 8006 | return false; |
| 8007 | |
| 8008 | return isa<VarDecl>(D) && D->hasAttr<HIPPinnedShadowAttr>(); |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 8009 | } |
| 8010 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 8011 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 8012 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 8013 | if (requiresAMDGPUDefaultVisibility(D, GV)) { |
| 8014 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 8015 | GV->setDSOLocal(false); |
| 8016 | } else if (requiresAMDGPUProtectedVisibility(D, GV)) { |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 8017 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 8018 | GV->setDSOLocal(true); |
| 8019 | } |
| 8020 | |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 8021 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 8022 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 8023 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8024 | if (!FD) |
| 8025 | return; |
| 8026 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8027 | llvm::Function *F = cast<llvm::Function>(GV); |
| 8028 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 8029 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 8030 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 8031 | |
Matt Arsenault | eac783a | 2019-08-27 19:25:40 +0000 | [diff] [blame] | 8032 | |
| 8033 | const bool IsOpenCLKernel = M.getLangOpts().OpenCL && |
| 8034 | FD->hasAttr<OpenCLKernelAttr>(); |
Yaxun Liu | 1bea97c | 2019-09-03 18:50:24 +0000 | [diff] [blame] | 8035 | const bool IsHIPKernel = M.getLangOpts().HIP && |
| 8036 | FD->hasAttr<CUDAGlobalAttr>(); |
| 8037 | if ((IsOpenCLKernel || IsHIPKernel) && |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 8038 | (M.getTriple().getOS() == llvm::Triple::AMDHSA)) |
Christudasan Devadasan | 18ba9d6 | 2019-07-10 15:10:08 +0000 | [diff] [blame] | 8039 | F->addFnAttr("amdgpu-implicitarg-num-bytes", "56"); |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 8040 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 8041 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 8042 | if (ReqdWGS || FlatWGS) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 8043 | unsigned Min = 0; |
| 8044 | unsigned Max = 0; |
| 8045 | if (FlatWGS) { |
| 8046 | Min = FlatWGS->getMin() |
| 8047 | ->EvaluateKnownConstInt(M.getContext()) |
| 8048 | .getExtValue(); |
| 8049 | Max = FlatWGS->getMax() |
| 8050 | ->EvaluateKnownConstInt(M.getContext()) |
| 8051 | .getExtValue(); |
| 8052 | } |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 8053 | if (ReqdWGS && Min == 0 && Max == 0) |
| 8054 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8055 | |
| 8056 | if (Min != 0) { |
| 8057 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 8058 | |
| 8059 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 8060 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 8061 | } else |
| 8062 | assert(Max == 0 && "Max must be zero"); |
Yaxun Liu | 1bea97c | 2019-09-03 18:50:24 +0000 | [diff] [blame] | 8063 | } else if (IsOpenCLKernel || IsHIPKernel) { |
Matt Arsenault | eac783a | 2019-08-27 19:25:40 +0000 | [diff] [blame] | 8064 | // By default, restrict the maximum size to 256. |
| 8065 | F->addFnAttr("amdgpu-flat-work-group-size", "1,256"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8066 | } |
| 8067 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8068 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 8069 | unsigned Min = |
| 8070 | Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue(); |
| 8071 | unsigned Max = Attr->getMax() ? Attr->getMax() |
| 8072 | ->EvaluateKnownConstInt(M.getContext()) |
| 8073 | .getExtValue() |
| 8074 | : 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8075 | |
| 8076 | if (Min != 0) { |
| 8077 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 8078 | |
| 8079 | std::string AttrVal = llvm::utostr(Min); |
| 8080 | if (Max != 0) |
| 8081 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 8082 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 8083 | } else |
| 8084 | assert(Max == 0 && "Max must be zero"); |
| 8085 | } |
| 8086 | |
| 8087 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8088 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8089 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8090 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8091 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 8092 | } |
| 8093 | |
| 8094 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 8095 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 8096 | |
| 8097 | if (NumVGPR != 0) |
| 8098 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8099 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 8100 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8101 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8102 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8103 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 8104 | } |
| 8105 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 8106 | // Currently LLVM assumes null pointers always have value 0, |
| 8107 | // which results in incorrectly transformed IR. Therefore, instead of |
| 8108 | // emitting null pointers in private and local address spaces, a null |
| 8109 | // pointer in generic address space is emitted which is casted to a |
| 8110 | // pointer in local or private address space. |
| 8111 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 8112 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 8113 | QualType QT) const { |
| 8114 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 8115 | return llvm::ConstantPointerNull::get(PT); |
| 8116 | |
| 8117 | auto &Ctx = CGM.getContext(); |
| 8118 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 8119 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 8120 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 8121 | llvm::ConstantPointerNull::get(NPT), PT); |
| 8122 | } |
| 8123 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8124 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8125 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 8126 | const VarDecl *D) const { |
| 8127 | assert(!CGM.getLangOpts().OpenCL && |
| 8128 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 8129 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8130 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 8131 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8132 | if (!D) |
| 8133 | return DefaultGlobalAS; |
| 8134 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8135 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 8136 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8137 | if (AddrSpace != LangAS::Default) |
| 8138 | return AddrSpace; |
| 8139 | |
| 8140 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 8141 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 8142 | return ConstAS.getValue(); |
| 8143 | } |
| 8144 | return DefaultGlobalAS; |
| 8145 | } |
| 8146 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8147 | llvm::SyncScope::ID |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 8148 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 8149 | SyncScope Scope, |
| 8150 | llvm::AtomicOrdering Ordering, |
| 8151 | llvm::LLVMContext &Ctx) const { |
| 8152 | std::string Name; |
| 8153 | switch (Scope) { |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8154 | case SyncScope::OpenCLWorkGroup: |
| 8155 | Name = "workgroup"; |
| 8156 | break; |
| 8157 | case SyncScope::OpenCLDevice: |
| 8158 | Name = "agent"; |
| 8159 | break; |
| 8160 | case SyncScope::OpenCLAllSVMDevices: |
| 8161 | Name = ""; |
| 8162 | break; |
| 8163 | case SyncScope::OpenCLSubGroup: |
Konstantin Zhuravlyov | 3161c89 | 2019-03-06 20:54:48 +0000 | [diff] [blame] | 8164 | Name = "wavefront"; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8165 | } |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 8166 | |
| 8167 | if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) { |
| 8168 | if (!Name.empty()) |
| 8169 | Name = Twine(Twine(Name) + Twine("-")).str(); |
| 8170 | |
| 8171 | Name = Twine(Twine(Name) + Twine("one-as")).str(); |
| 8172 | } |
| 8173 | |
| 8174 | return Ctx.getOrInsertSyncScopeID(Name); |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8175 | } |
| 8176 | |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 8177 | bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 8178 | return false; |
| 8179 | } |
| 8180 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 8181 | void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 8182 | const FunctionType *&FT) const { |
| 8183 | FT = getABIInfo().getContext().adjustFunctionType( |
| 8184 | FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 8185 | } |
| 8186 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8187 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8188 | // SPARC v8 ABI Implementation. |
| 8189 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8190 | // |
| 8191 | // Ensures that complex values are passed in registers. |
| 8192 | // |
| 8193 | namespace { |
| 8194 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 8195 | public: |
| 8196 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 8197 | |
| 8198 | private: |
| 8199 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8200 | void computeInfo(CGFunctionInfo &FI) const override; |
| 8201 | }; |
| 8202 | } // end anonymous namespace |
| 8203 | |
| 8204 | |
| 8205 | ABIArgInfo |
| 8206 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 8207 | if (Ty->isAnyComplexType()) { |
| 8208 | return ABIArgInfo::getDirect(); |
| 8209 | } |
| 8210 | else { |
| 8211 | return DefaultABIInfo::classifyReturnType(Ty); |
| 8212 | } |
| 8213 | } |
| 8214 | |
| 8215 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8216 | |
| 8217 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8218 | for (auto &Arg : FI.arguments()) |
| 8219 | Arg.info = classifyArgumentType(Arg.type); |
| 8220 | } |
| 8221 | |
| 8222 | namespace { |
| 8223 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8224 | public: |
| 8225 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8226 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 8227 | }; |
| 8228 | } // end anonymous namespace |
| 8229 | |
| 8230 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8231 | // SPARC v9 ABI Implementation. |
| 8232 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8233 | // |
| 8234 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 8235 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 8236 | // the array, structs larger than 16 bytes are passed indirectly. |
| 8237 | // |
| 8238 | // One case requires special care: |
| 8239 | // |
| 8240 | // struct mixed { |
| 8241 | // int i; |
| 8242 | // float f; |
| 8243 | // }; |
| 8244 | // |
| 8245 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 8246 | // parameter array, but the int is passed in an integer register, and the float |
| 8247 | // is passed in a floating point register. This is represented as two arguments |
| 8248 | // with the LLVM IR inreg attribute: |
| 8249 | // |
| 8250 | // declare void f(i32 inreg %i, float inreg %f) |
| 8251 | // |
| 8252 | // The code generator will only allocate 4 bytes from the parameter array for |
| 8253 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 8254 | // bytes. |
| 8255 | // |
| 8256 | namespace { |
| 8257 | class SparcV9ABIInfo : public ABIInfo { |
| 8258 | public: |
| 8259 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 8260 | |
| 8261 | private: |
| 8262 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8263 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8264 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8265 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8266 | |
| 8267 | // Coercion type builder for structs passed in registers. The coercion type |
| 8268 | // serves two purposes: |
| 8269 | // |
| 8270 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 8271 | // in registers. |
| 8272 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 8273 | // code generator knows to pass them in floating point registers. |
| 8274 | // |
| 8275 | // We also compute the InReg flag which indicates that the struct contains |
| 8276 | // aligned 32-bit floats. |
| 8277 | // |
| 8278 | struct CoerceBuilder { |
| 8279 | llvm::LLVMContext &Context; |
| 8280 | const llvm::DataLayout &DL; |
| 8281 | SmallVector<llvm::Type*, 8> Elems; |
| 8282 | uint64_t Size; |
| 8283 | bool InReg; |
| 8284 | |
| 8285 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 8286 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 8287 | |
| 8288 | // Pad Elems with integers until Size is ToSize. |
| 8289 | void pad(uint64_t ToSize) { |
| 8290 | assert(ToSize >= Size && "Cannot remove elements"); |
| 8291 | if (ToSize == Size) |
| 8292 | return; |
| 8293 | |
| 8294 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8295 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8296 | if (Aligned > Size && Aligned <= ToSize) { |
| 8297 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 8298 | Size = Aligned; |
| 8299 | } |
| 8300 | |
| 8301 | // Add whole 64-bit words. |
| 8302 | while (Size + 64 <= ToSize) { |
| 8303 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 8304 | Size += 64; |
| 8305 | } |
| 8306 | |
| 8307 | // Final in-word padding. |
| 8308 | if (Size < ToSize) { |
| 8309 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 8310 | Size = ToSize; |
| 8311 | } |
| 8312 | } |
| 8313 | |
| 8314 | // Add a floating point element at Offset. |
| 8315 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 8316 | // Unaligned floats are treated as integers. |
| 8317 | if (Offset % Bits) |
| 8318 | return; |
| 8319 | // The InReg flag is only required if there are any floats < 64 bits. |
| 8320 | if (Bits < 64) |
| 8321 | InReg = true; |
| 8322 | pad(Offset); |
| 8323 | Elems.push_back(Ty); |
| 8324 | Size = Offset + Bits; |
| 8325 | } |
| 8326 | |
| 8327 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 8328 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 8329 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 8330 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 8331 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 8332 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 8333 | switch (ElemTy->getTypeID()) { |
| 8334 | case llvm::Type::StructTyID: |
| 8335 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 8336 | break; |
| 8337 | case llvm::Type::FloatTyID: |
| 8338 | addFloat(ElemOffset, ElemTy, 32); |
| 8339 | break; |
| 8340 | case llvm::Type::DoubleTyID: |
| 8341 | addFloat(ElemOffset, ElemTy, 64); |
| 8342 | break; |
| 8343 | case llvm::Type::FP128TyID: |
| 8344 | addFloat(ElemOffset, ElemTy, 128); |
| 8345 | break; |
| 8346 | case llvm::Type::PointerTyID: |
| 8347 | if (ElemOffset % 64 == 0) { |
| 8348 | pad(ElemOffset); |
| 8349 | Elems.push_back(ElemTy); |
| 8350 | Size += 64; |
| 8351 | } |
| 8352 | break; |
| 8353 | default: |
| 8354 | break; |
| 8355 | } |
| 8356 | } |
| 8357 | } |
| 8358 | |
| 8359 | // Check if Ty is a usable substitute for the coercion type. |
| 8360 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 8361 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8362 | } |
| 8363 | |
| 8364 | // Get the coercion type as a literal struct type. |
| 8365 | llvm::Type *getType() const { |
| 8366 | if (Elems.size() == 1) |
| 8367 | return Elems.front(); |
| 8368 | else |
| 8369 | return llvm::StructType::get(Context, Elems); |
| 8370 | } |
| 8371 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8372 | }; |
| 8373 | } // end anonymous namespace |
| 8374 | |
| 8375 | ABIArgInfo |
| 8376 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 8377 | if (Ty->isVoidType()) |
| 8378 | return ABIArgInfo::getIgnore(); |
| 8379 | |
| 8380 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8381 | |
| 8382 | // Anything too big to fit in registers is passed with an explicit indirect |
| 8383 | // pointer / sret pointer. |
| 8384 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8385 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8386 | |
| 8387 | // Treat an enum type as its underlying type. |
| 8388 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8389 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8390 | |
| 8391 | // Integer types smaller than a register are extended. |
| 8392 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 8393 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8394 | |
| 8395 | // Other non-aggregates go in registers. |
| 8396 | if (!isAggregateTypeForABI(Ty)) |
| 8397 | return ABIArgInfo::getDirect(); |
| 8398 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8399 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 8400 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 8401 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8402 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8403 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8404 | // 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] | 8405 | // Build a coercion type from the LLVM struct type. |
| 8406 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 8407 | if (!StrTy) |
| 8408 | return ABIArgInfo::getDirect(); |
| 8409 | |
| 8410 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 8411 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8412 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8413 | |
| 8414 | // Try to use the original type for coercion. |
| 8415 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8416 | |
| 8417 | if (CB.InReg) |
| 8418 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8419 | else |
| 8420 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8421 | } |
| 8422 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8423 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8424 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8425 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8426 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8427 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8428 | AI.setCoerceToType(ArgTy); |
| 8429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8430 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8431 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8432 | CGBuilderTy &Builder = CGF.Builder; |
| 8433 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8434 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8435 | |
| 8436 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8437 | |
| 8438 | Address ArgAddr = Address::invalid(); |
| 8439 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8440 | switch (AI.getKind()) { |
| 8441 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8442 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8443 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8444 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8445 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8446 | case ABIArgInfo::Extend: { |
| 8447 | Stride = SlotSize; |
| 8448 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8449 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8450 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8451 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8452 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8453 | case ABIArgInfo::Direct: { |
| 8454 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8455 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8456 | ArgAddr = Addr; |
| 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 | |
| 8460 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8461 | Stride = SlotSize; |
| 8462 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8463 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8464 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8465 | break; |
| 8466 | |
| 8467 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8468 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8469 | } |
| 8470 | |
| 8471 | // Update VAList. |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8472 | Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); |
| 8473 | Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8474 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8475 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8479 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8480 | for (auto &I : FI.arguments()) |
| 8481 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8482 | } |
| 8483 | |
| 8484 | namespace { |
| 8485 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8486 | public: |
| 8487 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8488 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8489 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8490 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8491 | return 14; |
| 8492 | } |
| 8493 | |
| 8494 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8495 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8496 | }; |
| 8497 | } // end anonymous namespace |
| 8498 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8499 | bool |
| 8500 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8501 | llvm::Value *Address) const { |
| 8502 | // This is calculated from the LLVM and GCC tables and verified |
| 8503 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8504 | |
| 8505 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8506 | |
| 8507 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8508 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8509 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8510 | |
| 8511 | // 0-31: the 8-byte general-purpose registers |
| 8512 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8513 | |
| 8514 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8515 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8516 | |
| 8517 | // Y = 64 |
| 8518 | // PSR = 65 |
| 8519 | // WIM = 66 |
| 8520 | // TBR = 67 |
| 8521 | // PC = 68 |
| 8522 | // NPC = 69 |
| 8523 | // FSR = 70 |
| 8524 | // CSR = 71 |
| 8525 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8526 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8527 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8528 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8529 | |
| 8530 | return false; |
| 8531 | } |
| 8532 | |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8533 | // ARC ABI implementation. |
| 8534 | namespace { |
| 8535 | |
| 8536 | class ARCABIInfo : public DefaultABIInfo { |
| 8537 | public: |
| 8538 | using DefaultABIInfo::DefaultABIInfo; |
| 8539 | |
| 8540 | private: |
| 8541 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8542 | QualType Ty) const override; |
| 8543 | |
| 8544 | void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { |
| 8545 | if (!State.FreeRegs) |
| 8546 | return; |
| 8547 | if (Info.isIndirect() && Info.getInReg()) |
| 8548 | State.FreeRegs--; |
| 8549 | else if (Info.isDirect() && Info.getInReg()) { |
| 8550 | unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; |
| 8551 | if (sz < State.FreeRegs) |
| 8552 | State.FreeRegs -= sz; |
| 8553 | else |
| 8554 | State.FreeRegs = 0; |
| 8555 | } |
| 8556 | } |
| 8557 | |
| 8558 | void computeInfo(CGFunctionInfo &FI) const override { |
| 8559 | CCState State(FI.getCallingConvention()); |
| 8560 | // ARC uses 8 registers to pass arguments. |
| 8561 | State.FreeRegs = 8; |
| 8562 | |
| 8563 | if (!getCXXABI().classifyReturnType(FI)) |
| 8564 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8565 | updateState(FI.getReturnInfo(), FI.getReturnType(), State); |
| 8566 | for (auto &I : FI.arguments()) { |
| 8567 | I.info = classifyArgumentType(I.type, State.FreeRegs); |
| 8568 | updateState(I.info, I.type, State); |
| 8569 | } |
| 8570 | } |
| 8571 | |
| 8572 | ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; |
| 8573 | ABIArgInfo getIndirectByValue(QualType Ty) const; |
| 8574 | ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; |
| 8575 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8576 | }; |
| 8577 | |
| 8578 | class ARCTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8579 | public: |
| 8580 | ARCTargetCodeGenInfo(CodeGenTypes &CGT) |
| 8581 | : TargetCodeGenInfo(new ARCABIInfo(CGT)) {} |
| 8582 | }; |
| 8583 | |
| 8584 | |
| 8585 | ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { |
| 8586 | return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : |
| 8587 | getNaturalAlignIndirect(Ty, false); |
| 8588 | } |
| 8589 | |
| 8590 | ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8591 | // Compute the byval alignment. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8592 | const unsigned MinABIStackAlignInBytes = 4; |
| 8593 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 8594 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 8595 | TypeAlign > MinABIStackAlignInBytes); |
| 8596 | } |
| 8597 | |
| 8598 | Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8599 | QualType Ty) const { |
| 8600 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 8601 | getContext().getTypeInfoInChars(Ty), |
| 8602 | CharUnits::fromQuantity(4), true); |
| 8603 | } |
| 8604 | |
| 8605 | ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, |
| 8606 | uint8_t FreeRegs) const { |
| 8607 | // Handle the generic C++ ABI. |
| 8608 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 8609 | if (RT) { |
| 8610 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 8611 | if (RAA == CGCXXABI::RAA_Indirect) |
| 8612 | return getIndirectByRef(Ty, FreeRegs > 0); |
| 8613 | |
| 8614 | if (RAA == CGCXXABI::RAA_DirectInMemory) |
| 8615 | return getIndirectByValue(Ty); |
| 8616 | } |
| 8617 | |
| 8618 | // Treat an enum type as its underlying type. |
| 8619 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8620 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8621 | |
| 8622 | auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; |
| 8623 | |
| 8624 | if (isAggregateTypeForABI(Ty)) { |
| 8625 | // Structures with flexible arrays are always indirect. |
| 8626 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 8627 | return getIndirectByValue(Ty); |
| 8628 | |
| 8629 | // Ignore empty structs/unions. |
| 8630 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8631 | return ABIArgInfo::getIgnore(); |
| 8632 | |
| 8633 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 8634 | |
| 8635 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 8636 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 8637 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 8638 | |
| 8639 | return FreeRegs >= SizeInRegs ? |
| 8640 | ABIArgInfo::getDirectInReg(Result) : |
| 8641 | ABIArgInfo::getDirect(Result, 0, nullptr, false); |
| 8642 | } |
| 8643 | |
| 8644 | return Ty->isPromotableIntegerType() ? |
| 8645 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) : |
| 8646 | ABIArgInfo::getExtend(Ty)) : |
| 8647 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() : |
| 8648 | ABIArgInfo::getDirect()); |
| 8649 | } |
| 8650 | |
| 8651 | ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { |
| 8652 | if (RetTy->isAnyComplexType()) |
| 8653 | return ABIArgInfo::getDirectInReg(); |
| 8654 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8655 | // Arguments of size > 4 registers are indirect. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8656 | auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; |
| 8657 | if (RetSize > 4) |
| 8658 | return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); |
| 8659 | |
| 8660 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 8661 | } |
| 8662 | |
| 8663 | } // End anonymous namespace. |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8664 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8665 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8666 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8667 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8668 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8669 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8670 | |
| 8671 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8672 | /// it by reference between functions that append to it. |
| 8673 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8674 | |
| 8675 | /// TypeStringCache caches the meta encodings of Types. |
| 8676 | /// |
| 8677 | /// The reason for caching TypeStrings is two fold: |
| 8678 | /// 1. To cache a type's encoding for later uses; |
| 8679 | /// 2. As a means to break recursive member type inclusion. |
| 8680 | /// |
| 8681 | /// A cache Entry can have a Status of: |
| 8682 | /// NonRecursive: The type encoding is not recursive; |
| 8683 | /// Recursive: The type encoding is recursive; |
| 8684 | /// Incomplete: An incomplete TypeString; |
| 8685 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8686 | /// Recursive type encoding. |
| 8687 | /// |
| 8688 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8689 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8690 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8691 | /// the type is encountered. |
| 8692 | /// |
| 8693 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8694 | /// possible. The type itself is recursive and it may contain other types which |
| 8695 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8696 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8697 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8698 | /// |
| 8699 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8700 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8701 | /// are placed into the cache during type expansion as a means to identify and |
| 8702 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8703 | /// the entry becomes IncompleteUsed. |
| 8704 | /// |
| 8705 | /// During the expansion of a RecordType's members: |
| 8706 | /// |
| 8707 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8708 | /// cached encoding is used; |
| 8709 | /// |
| 8710 | /// If the cache contains a Recursive encoding for the member type, the |
| 8711 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8712 | /// |
| 8713 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8714 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8715 | /// |
| 8716 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8717 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8718 | /// it is swapped back in; |
| 8719 | /// |
| 8720 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8721 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8722 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8723 | /// |
| 8724 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8725 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8726 | /// Else the member is part of a recursive type and thus the recursion has |
| 8727 | /// been exited too soon for the encoding to be correct for the member. |
| 8728 | /// |
| 8729 | class TypeStringCache { |
| 8730 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8731 | struct Entry { |
| 8732 | std::string Str; // The encoded TypeString for the type. |
| 8733 | enum Status State; // Information about the encoding in 'Str'. |
| 8734 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8735 | // during the expansion of RecordType's members. |
| 8736 | }; |
| 8737 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8738 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8739 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8740 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8741 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8742 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8743 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8744 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8745 | bool IsRecursive); |
| 8746 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8747 | }; |
| 8748 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8749 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8750 | /// FieldEncoding is a helper for this ordering process. |
| 8751 | class FieldEncoding { |
| 8752 | bool HasName; |
| 8753 | std::string Enc; |
| 8754 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8755 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8756 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8757 | bool operator<(const FieldEncoding &rhs) const { |
| 8758 | if (HasName != rhs.HasName) return HasName; |
| 8759 | return Enc < rhs.Enc; |
| 8760 | } |
| 8761 | }; |
| 8762 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8763 | class XCoreABIInfo : public DefaultABIInfo { |
| 8764 | public: |
| 8765 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8766 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8767 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8768 | }; |
| 8769 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8770 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8771 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8772 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8773 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8774 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8775 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8776 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8777 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8778 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8779 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8780 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8781 | // TODO: this implementation is likely now redundant with the default |
| 8782 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8783 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8784 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8785 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8786 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8787 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8788 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8789 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8790 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8791 | // Handle the argument. |
| 8792 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8793 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8794 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8795 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8796 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8797 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8798 | |
| 8799 | Address Val = Address::invalid(); |
| 8800 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8801 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8802 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8803 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8804 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8805 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8806 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8807 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8808 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8809 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8810 | case ABIArgInfo::Extend: |
| 8811 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8812 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8813 | ArgSize = CharUnits::fromQuantity( |
| 8814 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8815 | ArgSize = ArgSize.alignTo(SlotSize); |
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::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8818 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8819 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8820 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8821 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8822 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8823 | |
| 8824 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8825 | if (!ArgSize.isZero()) { |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8826 | Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize); |
| 8827 | Builder.CreateStore(APN.getPointer(), VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8828 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8829 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8830 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8831 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8832 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8833 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8834 | /// into the cache as a means to identify and break recursion. |
| 8835 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8836 | /// be reinserted by removeIncomplete(). |
| 8837 | /// All other types of encoding should have been used rather than arriving here. |
| 8838 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8839 | std::string StubEnc) { |
| 8840 | if (!ID) |
| 8841 | return; |
| 8842 | Entry &E = Map[ID]; |
| 8843 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8844 | "Incorrectly use of addIncomplete"); |
| 8845 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8846 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8847 | E.Str.swap(StubEnc); |
| 8848 | E.State = Incomplete; |
| 8849 | ++IncompleteCount; |
| 8850 | } |
| 8851 | |
| 8852 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8853 | /// must be removed from the cache. |
| 8854 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8855 | /// Returns true if the RecordType was defined recursively. |
| 8856 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8857 | if (!ID) |
| 8858 | return false; |
| 8859 | auto I = Map.find(ID); |
| 8860 | assert(I != Map.end() && "Entry not present"); |
| 8861 | Entry &E = I->second; |
| 8862 | assert( (E.State == Incomplete || |
| 8863 | E.State == IncompleteUsed) && |
| 8864 | "Entry must be an incomplete type"); |
| 8865 | bool IsRecursive = false; |
| 8866 | if (E.State == IncompleteUsed) { |
| 8867 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8868 | IsRecursive = true; |
| 8869 | --IncompleteUsedCount; |
| 8870 | } |
| 8871 | if (E.Swapped.empty()) |
| 8872 | Map.erase(I); |
| 8873 | else { |
| 8874 | // Swap the Recursive back. |
| 8875 | E.Swapped.swap(E.Str); |
| 8876 | E.Swapped.clear(); |
| 8877 | E.State = Recursive; |
| 8878 | } |
| 8879 | --IncompleteCount; |
| 8880 | return IsRecursive; |
| 8881 | } |
| 8882 | |
| 8883 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8884 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8885 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8886 | bool IsRecursive) { |
| 8887 | if (!ID || IncompleteUsedCount) |
| 8888 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8889 | Entry &E = Map[ID]; |
| 8890 | if (IsRecursive && !E.Str.empty()) { |
| 8891 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8892 | "This is not the same Recursive entry"); |
| 8893 | // The parent container was not recursive after all, so we could have used |
| 8894 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8895 | // we started viz: IncompleteCount!=0. |
| 8896 | return; |
| 8897 | } |
| 8898 | assert(E.Str.empty() && "Entry already present"); |
| 8899 | E.Str = Str.str(); |
| 8900 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8901 | } |
| 8902 | |
| 8903 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8904 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8905 | /// encoding is Recursive, return an empty StringRef. |
| 8906 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8907 | if (!ID) |
| 8908 | return StringRef(); // We have no key. |
| 8909 | auto I = Map.find(ID); |
| 8910 | if (I == Map.end()) |
| 8911 | return StringRef(); // We have no encoding. |
| 8912 | Entry &E = I->second; |
| 8913 | if (E.State == Recursive && IncompleteCount) |
| 8914 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8915 | |
| 8916 | if (E.State == Incomplete) { |
| 8917 | // The incomplete type is being used to break out of recursion. |
| 8918 | E.State = IncompleteUsed; |
| 8919 | ++IncompleteUsedCount; |
| 8920 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8921 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8922 | } |
| 8923 | |
| 8924 | /// The XCore ABI includes a type information section that communicates symbol |
| 8925 | /// type information to the linker. The linker uses this information to verify |
| 8926 | /// safety/correctness of things such as array bound and pointers et al. |
| 8927 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8928 | /// This type information (TypeString) is emitted into meta data for all global |
| 8929 | /// symbols: definitions, declarations, functions & variables. |
| 8930 | /// |
| 8931 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8932 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8933 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8934 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8935 | /// |
| 8936 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8937 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8938 | |
| 8939 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8940 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8941 | CodeGen::CodeGenModule &CGM) const { |
| 8942 | SmallStringEnc Enc; |
| 8943 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8944 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8945 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8946 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8947 | llvm::NamedMDNode *MD = |
| 8948 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8949 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8950 | } |
| 8951 | } |
| 8952 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8953 | //===----------------------------------------------------------------------===// |
| 8954 | // SPIR ABI Implementation |
| 8955 | //===----------------------------------------------------------------------===// |
| 8956 | |
| 8957 | namespace { |
| 8958 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8959 | public: |
| 8960 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8961 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8962 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8963 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8964 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8965 | } // End anonymous namespace. |
| 8966 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8967 | namespace clang { |
| 8968 | namespace CodeGen { |
| 8969 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8970 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8971 | SPIRABI.computeInfo(FI); |
| 8972 | } |
| 8973 | } |
| 8974 | } |
| 8975 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8976 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8977 | return llvm::CallingConv::SPIR_KERNEL; |
| 8978 | } |
| 8979 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8980 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8981 | const CodeGen::CodeGenModule &CGM, |
| 8982 | TypeStringCache &TSC); |
| 8983 | |
| 8984 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8985 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8986 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8987 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8988 | const RecordDecl *RD, |
| 8989 | const CodeGen::CodeGenModule &CGM, |
| 8990 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8991 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8992 | SmallStringEnc Enc; |
| 8993 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8994 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8995 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8996 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8997 | Enc += "b("; |
| 8998 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8999 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9000 | Enc += ':'; |
| 9001 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 9002 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9003 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 9004 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9005 | Enc += ')'; |
| 9006 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 9007 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9008 | } |
| 9009 | return true; |
| 9010 | } |
| 9011 | |
| 9012 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 9013 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 9014 | /// Union types have their fields ordered according to the ABI. |
| 9015 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 9016 | const CodeGen::CodeGenModule &CGM, |
| 9017 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 9018 | // Append the cached TypeString if we have one. |
| 9019 | StringRef TypeString = TSC.lookupStr(ID); |
| 9020 | if (!TypeString.empty()) { |
| 9021 | Enc += TypeString; |
| 9022 | return true; |
| 9023 | } |
| 9024 | |
| 9025 | // Start to emit an incomplete TypeString. |
| 9026 | size_t Start = Enc.size(); |
| 9027 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 9028 | Enc += '('; |
| 9029 | if (ID) |
| 9030 | Enc += ID->getName(); |
| 9031 | Enc += "){"; |
| 9032 | |
| 9033 | // We collect all encoded fields and order as necessary. |
| 9034 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9035 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 9036 | if (RD && !RD->field_empty()) { |
| 9037 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 9038 | // so that recursive calls to this RecordType will use it whilst building a |
| 9039 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9040 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9041 | std::string StubEnc(Enc.substr(Start).str()); |
| 9042 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 9043 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 9044 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 9045 | (void) TSC.removeIncomplete(ID); |
| 9046 | return false; |
| 9047 | } |
| 9048 | IsRecursive = TSC.removeIncomplete(ID); |
| 9049 | // The ABI requires unions to be sorted but not structures. |
| 9050 | // See FieldEncoding::operator< for sort algorithm. |
| 9051 | if (RT->isUnionType()) |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 9052 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9053 | // We can now complete the TypeString. |
| 9054 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9055 | for (unsigned I = 0; I != E; ++I) { |
| 9056 | if (I) |
| 9057 | Enc += ','; |
| 9058 | Enc += FE[I].str(); |
| 9059 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9060 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9061 | Enc += '}'; |
| 9062 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 9063 | return true; |
| 9064 | } |
| 9065 | |
| 9066 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 9067 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 9068 | TypeStringCache &TSC, |
| 9069 | const IdentifierInfo *ID) { |
| 9070 | // Append the cached TypeString if we have one. |
| 9071 | StringRef TypeString = TSC.lookupStr(ID); |
| 9072 | if (!TypeString.empty()) { |
| 9073 | Enc += TypeString; |
| 9074 | return true; |
| 9075 | } |
| 9076 | |
| 9077 | size_t Start = Enc.size(); |
| 9078 | Enc += "e("; |
| 9079 | if (ID) |
| 9080 | Enc += ID->getName(); |
| 9081 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9082 | |
| 9083 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9084 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9085 | SmallVector<FieldEncoding, 16> FE; |
| 9086 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 9087 | ++I) { |
| 9088 | SmallStringEnc EnumEnc; |
| 9089 | EnumEnc += "m("; |
| 9090 | EnumEnc += I->getName(); |
| 9091 | EnumEnc += "){"; |
| 9092 | I->getInitVal().toString(EnumEnc); |
| 9093 | EnumEnc += '}'; |
| 9094 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 9095 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 9096 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9097 | unsigned E = FE.size(); |
| 9098 | for (unsigned I = 0; I != E; ++I) { |
| 9099 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9100 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9101 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9102 | } |
| 9103 | } |
| 9104 | Enc += '}'; |
| 9105 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 9106 | return true; |
| 9107 | } |
| 9108 | |
| 9109 | /// Appends type's qualifier to Enc. |
| 9110 | /// This is done prior to appending the type's encoding. |
| 9111 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 9112 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 9113 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9114 | int Lookup = 0; |
| 9115 | if (QT.isConstQualified()) |
| 9116 | Lookup += 1<<0; |
| 9117 | if (QT.isRestrictQualified()) |
| 9118 | Lookup += 1<<1; |
| 9119 | if (QT.isVolatileQualified()) |
| 9120 | Lookup += 1<<2; |
| 9121 | Enc += Table[Lookup]; |
| 9122 | } |
| 9123 | |
| 9124 | /// Appends built-in types to Enc. |
| 9125 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 9126 | const char *EncType; |
| 9127 | switch (BT->getKind()) { |
| 9128 | case BuiltinType::Void: |
| 9129 | EncType = "0"; |
| 9130 | break; |
| 9131 | case BuiltinType::Bool: |
| 9132 | EncType = "b"; |
| 9133 | break; |
| 9134 | case BuiltinType::Char_U: |
| 9135 | EncType = "uc"; |
| 9136 | break; |
| 9137 | case BuiltinType::UChar: |
| 9138 | EncType = "uc"; |
| 9139 | break; |
| 9140 | case BuiltinType::SChar: |
| 9141 | EncType = "sc"; |
| 9142 | break; |
| 9143 | case BuiltinType::UShort: |
| 9144 | EncType = "us"; |
| 9145 | break; |
| 9146 | case BuiltinType::Short: |
| 9147 | EncType = "ss"; |
| 9148 | break; |
| 9149 | case BuiltinType::UInt: |
| 9150 | EncType = "ui"; |
| 9151 | break; |
| 9152 | case BuiltinType::Int: |
| 9153 | EncType = "si"; |
| 9154 | break; |
| 9155 | case BuiltinType::ULong: |
| 9156 | EncType = "ul"; |
| 9157 | break; |
| 9158 | case BuiltinType::Long: |
| 9159 | EncType = "sl"; |
| 9160 | break; |
| 9161 | case BuiltinType::ULongLong: |
| 9162 | EncType = "ull"; |
| 9163 | break; |
| 9164 | case BuiltinType::LongLong: |
| 9165 | EncType = "sll"; |
| 9166 | break; |
| 9167 | case BuiltinType::Float: |
| 9168 | EncType = "ft"; |
| 9169 | break; |
| 9170 | case BuiltinType::Double: |
| 9171 | EncType = "d"; |
| 9172 | break; |
| 9173 | case BuiltinType::LongDouble: |
| 9174 | EncType = "ld"; |
| 9175 | break; |
| 9176 | default: |
| 9177 | return false; |
| 9178 | } |
| 9179 | Enc += EncType; |
| 9180 | return true; |
| 9181 | } |
| 9182 | |
| 9183 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 9184 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 9185 | const CodeGen::CodeGenModule &CGM, |
| 9186 | TypeStringCache &TSC) { |
| 9187 | Enc += "p("; |
| 9188 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 9189 | return false; |
| 9190 | Enc += ')'; |
| 9191 | return true; |
| 9192 | } |
| 9193 | |
| 9194 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9195 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 9196 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9197 | const CodeGen::CodeGenModule &CGM, |
| 9198 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 9199 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 9200 | return false; |
| 9201 | Enc += "a("; |
| 9202 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 9203 | CAT->getSize().toStringUnsigned(Enc); |
| 9204 | else |
| 9205 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 9206 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9207 | // The Qualifiers should be attached to the type rather than the array. |
| 9208 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9209 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 9210 | return false; |
| 9211 | Enc += ')'; |
| 9212 | return true; |
| 9213 | } |
| 9214 | |
| 9215 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 9216 | /// and the arguments. |
| 9217 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 9218 | const CodeGen::CodeGenModule &CGM, |
| 9219 | TypeStringCache &TSC) { |
| 9220 | Enc += "f{"; |
| 9221 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 9222 | return false; |
| 9223 | Enc += "}("; |
| 9224 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 9225 | // N.B. we are only interested in the adjusted param types. |
| 9226 | auto I = FPT->param_type_begin(); |
| 9227 | auto E = FPT->param_type_end(); |
| 9228 | if (I != E) { |
| 9229 | do { |
| 9230 | if (!appendType(Enc, *I, CGM, TSC)) |
| 9231 | return false; |
| 9232 | ++I; |
| 9233 | if (I != E) |
| 9234 | Enc += ','; |
| 9235 | } while (I != E); |
| 9236 | if (FPT->isVariadic()) |
| 9237 | Enc += ",va"; |
| 9238 | } else { |
| 9239 | if (FPT->isVariadic()) |
| 9240 | Enc += "va"; |
| 9241 | else |
| 9242 | Enc += '0'; |
| 9243 | } |
| 9244 | } |
| 9245 | Enc += ')'; |
| 9246 | return true; |
| 9247 | } |
| 9248 | |
| 9249 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 9250 | /// type encodings. |
| 9251 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 9252 | const CodeGen::CodeGenModule &CGM, |
| 9253 | TypeStringCache &TSC) { |
| 9254 | |
| 9255 | QualType QT = QType.getCanonicalType(); |
| 9256 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9257 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 9258 | // The Qualifiers should be attached to the type rather than the array. |
| 9259 | // Thus we don't call appendQualifier() here. |
| 9260 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 9261 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9262 | appendQualifier(Enc, QT); |
| 9263 | |
| 9264 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 9265 | return appendBuiltinType(Enc, BT); |
| 9266 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9267 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 9268 | return appendPointerType(Enc, PT, CGM, TSC); |
| 9269 | |
| 9270 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 9271 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 9272 | |
| 9273 | if (const RecordType *RT = QT->getAsStructureType()) |
| 9274 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9275 | |
| 9276 | if (const RecordType *RT = QT->getAsUnionType()) |
| 9277 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9278 | |
| 9279 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 9280 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 9281 | |
| 9282 | return false; |
| 9283 | } |
| 9284 | |
| 9285 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 9286 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 9287 | if (!D) |
| 9288 | return false; |
| 9289 | |
| 9290 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9291 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 9292 | return false; |
| 9293 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 9294 | } |
| 9295 | |
| 9296 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 9297 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 9298 | return false; |
| 9299 | QualType QT = VD->getType().getCanonicalType(); |
| 9300 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 9301 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9302 | // The Qualifiers should be attached to the type rather than the array. |
| 9303 | // Thus we don't call appendQualifier() here. |
| 9304 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9305 | } |
| 9306 | return appendType(Enc, QT, CGM, TSC); |
| 9307 | } |
| 9308 | return false; |
| 9309 | } |
| 9310 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9311 | //===----------------------------------------------------------------------===// |
| 9312 | // RISCV ABI Implementation |
| 9313 | //===----------------------------------------------------------------------===// |
| 9314 | |
| 9315 | namespace { |
| 9316 | class RISCVABIInfo : public DefaultABIInfo { |
| 9317 | private: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9318 | // Size of the integer ('x') registers in bits. |
| 9319 | unsigned XLen; |
| 9320 | // Size of the floating point ('f') registers in bits. Note that the target |
| 9321 | // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target |
| 9322 | // with soft float ABI has FLen==0). |
| 9323 | unsigned FLen; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9324 | static const int NumArgGPRs = 8; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9325 | static const int NumArgFPRs = 8; |
| 9326 | bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, |
| 9327 | llvm::Type *&Field1Ty, |
| 9328 | CharUnits &Field1Off, |
| 9329 | llvm::Type *&Field2Ty, |
| 9330 | CharUnits &Field2Off) const; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9331 | |
| 9332 | public: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9333 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen) |
| 9334 | : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {} |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9335 | |
| 9336 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 9337 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 9338 | void computeInfo(CGFunctionInfo &FI) const override; |
| 9339 | |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9340 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft, |
| 9341 | int &ArgFPRsLeft) const; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9342 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 9343 | |
| 9344 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9345 | QualType Ty) const override; |
| 9346 | |
| 9347 | ABIArgInfo extendType(QualType Ty) const; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9348 | |
| 9349 | bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, |
| 9350 | CharUnits &Field1Off, llvm::Type *&Field2Ty, |
| 9351 | CharUnits &Field2Off, int &NeededArgGPRs, |
| 9352 | int &NeededArgFPRs) const; |
| 9353 | ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty, |
| 9354 | CharUnits Field1Off, |
| 9355 | llvm::Type *Field2Ty, |
| 9356 | CharUnits Field2Off) const; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9357 | }; |
| 9358 | } // end anonymous namespace |
| 9359 | |
| 9360 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 9361 | QualType RetTy = FI.getReturnType(); |
| 9362 | if (!getCXXABI().classifyReturnType(FI)) |
| 9363 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 9364 | |
| 9365 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 9366 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 9367 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 9368 | // rewrite the argument list and pass indirectly on RV32. |
| 9369 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 9370 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 9371 | |
| 9372 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 9373 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 9374 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 9375 | // different for variadic arguments, we must also track whether we are |
| 9376 | // examining a vararg or not. |
| 9377 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9378 | int ArgFPRsLeft = FLen ? NumArgFPRs : 0; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9379 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 9380 | |
| 9381 | int ArgNum = 0; |
| 9382 | for (auto &ArgInfo : FI.arguments()) { |
| 9383 | bool IsFixed = ArgNum < NumFixedArgs; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9384 | ArgInfo.info = |
| 9385 | classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft); |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9386 | ArgNum++; |
| 9387 | } |
| 9388 | } |
| 9389 | |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9390 | // Returns true if the struct is a potential candidate for the floating point |
| 9391 | // calling convention. If this function returns true, the caller is |
| 9392 | // responsible for checking that if there is only a single field then that |
| 9393 | // field is a float. |
| 9394 | bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, |
| 9395 | llvm::Type *&Field1Ty, |
| 9396 | CharUnits &Field1Off, |
| 9397 | llvm::Type *&Field2Ty, |
| 9398 | CharUnits &Field2Off) const { |
| 9399 | bool IsInt = Ty->isIntegralOrEnumerationType(); |
| 9400 | bool IsFloat = Ty->isRealFloatingType(); |
| 9401 | |
| 9402 | if (IsInt || IsFloat) { |
| 9403 | uint64_t Size = getContext().getTypeSize(Ty); |
| 9404 | if (IsInt && Size > XLen) |
| 9405 | return false; |
| 9406 | // Can't be eligible if larger than the FP registers. Half precision isn't |
| 9407 | // currently supported on RISC-V and the ABI hasn't been confirmed, so |
| 9408 | // default to the integer ABI in that case. |
| 9409 | if (IsFloat && (Size > FLen || Size < 32)) |
| 9410 | return false; |
| 9411 | // Can't be eligible if an integer type was already found (int+int pairs |
| 9412 | // are not eligible). |
| 9413 | if (IsInt && Field1Ty && Field1Ty->isIntegerTy()) |
| 9414 | return false; |
| 9415 | if (!Field1Ty) { |
| 9416 | Field1Ty = CGT.ConvertType(Ty); |
| 9417 | Field1Off = CurOff; |
| 9418 | return true; |
| 9419 | } |
| 9420 | if (!Field2Ty) { |
| 9421 | Field2Ty = CGT.ConvertType(Ty); |
| 9422 | Field2Off = CurOff; |
| 9423 | return true; |
| 9424 | } |
| 9425 | return false; |
| 9426 | } |
| 9427 | |
| 9428 | if (auto CTy = Ty->getAs<ComplexType>()) { |
| 9429 | if (Field1Ty) |
| 9430 | return false; |
| 9431 | QualType EltTy = CTy->getElementType(); |
| 9432 | if (getContext().getTypeSize(EltTy) > FLen) |
| 9433 | return false; |
| 9434 | Field1Ty = CGT.ConvertType(EltTy); |
| 9435 | Field1Off = CurOff; |
| 9436 | assert(CurOff.isZero() && "Unexpected offset for first field"); |
| 9437 | Field2Ty = Field1Ty; |
| 9438 | Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy); |
| 9439 | return true; |
| 9440 | } |
| 9441 | |
| 9442 | if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) { |
| 9443 | uint64_t ArraySize = ATy->getSize().getZExtValue(); |
| 9444 | QualType EltTy = ATy->getElementType(); |
| 9445 | CharUnits EltSize = getContext().getTypeSizeInChars(EltTy); |
| 9446 | for (uint64_t i = 0; i < ArraySize; ++i) { |
| 9447 | bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty, |
| 9448 | Field1Off, Field2Ty, Field2Off); |
| 9449 | if (!Ret) |
| 9450 | return false; |
| 9451 | CurOff += EltSize; |
| 9452 | } |
| 9453 | return true; |
| 9454 | } |
| 9455 | |
| 9456 | if (const auto *RTy = Ty->getAs<RecordType>()) { |
| 9457 | // Structures with either a non-trivial destructor or a non-trivial |
| 9458 | // copy constructor are not eligible for the FP calling convention. |
Denis Bakhvalov | a29002e | 2019-07-19 21:59:42 +0000 | [diff] [blame] | 9459 | if (getRecordArgABI(Ty, CGT.getCXXABI())) |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9460 | return false; |
| 9461 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9462 | return true; |
| 9463 | const RecordDecl *RD = RTy->getDecl(); |
| 9464 | // Unions aren't eligible unless they're empty (which is caught above). |
| 9465 | if (RD->isUnion()) |
| 9466 | return false; |
| 9467 | int ZeroWidthBitFieldCount = 0; |
| 9468 | for (const FieldDecl *FD : RD->fields()) { |
| 9469 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 9470 | uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex()); |
| 9471 | QualType QTy = FD->getType(); |
| 9472 | if (FD->isBitField()) { |
| 9473 | unsigned BitWidth = FD->getBitWidthValue(getContext()); |
| 9474 | // Allow a bitfield with a type greater than XLen as long as the |
| 9475 | // bitwidth is XLen or less. |
| 9476 | if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen) |
| 9477 | QTy = getContext().getIntTypeForBitwidth(XLen, false); |
| 9478 | if (BitWidth == 0) { |
| 9479 | ZeroWidthBitFieldCount++; |
| 9480 | continue; |
| 9481 | } |
| 9482 | } |
| 9483 | |
| 9484 | bool Ret = detectFPCCEligibleStructHelper( |
| 9485 | QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits), |
| 9486 | Field1Ty, Field1Off, Field2Ty, Field2Off); |
| 9487 | if (!Ret) |
| 9488 | return false; |
| 9489 | |
| 9490 | // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp |
| 9491 | // or int+fp structs, but are ignored for a struct with an fp field and |
| 9492 | // any number of zero-width bitfields. |
| 9493 | if (Field2Ty && ZeroWidthBitFieldCount > 0) |
| 9494 | return false; |
| 9495 | } |
| 9496 | return Field1Ty != nullptr; |
| 9497 | } |
| 9498 | |
| 9499 | return false; |
| 9500 | } |
| 9501 | |
| 9502 | // Determine if a struct is eligible for passing according to the floating |
| 9503 | // point calling convention (i.e., when flattened it contains a single fp |
| 9504 | // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and |
| 9505 | // NeededArgGPRs are incremented appropriately. |
| 9506 | bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, |
| 9507 | CharUnits &Field1Off, |
| 9508 | llvm::Type *&Field2Ty, |
| 9509 | CharUnits &Field2Off, |
| 9510 | int &NeededArgGPRs, |
| 9511 | int &NeededArgFPRs) const { |
| 9512 | Field1Ty = nullptr; |
| 9513 | Field2Ty = nullptr; |
| 9514 | NeededArgGPRs = 0; |
| 9515 | NeededArgFPRs = 0; |
| 9516 | bool IsCandidate = detectFPCCEligibleStructHelper( |
| 9517 | Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off); |
| 9518 | // Not really a candidate if we have a single int but no float. |
| 9519 | if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy()) |
Sylvestre Ledru | fb190c8 | 2019-10-08 09:17:46 +0000 | [diff] [blame] | 9520 | return false; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9521 | if (!IsCandidate) |
| 9522 | return false; |
| 9523 | if (Field1Ty && Field1Ty->isFloatingPointTy()) |
| 9524 | NeededArgFPRs++; |
| 9525 | else if (Field1Ty) |
| 9526 | NeededArgGPRs++; |
| 9527 | if (Field2Ty && Field2Ty->isFloatingPointTy()) |
| 9528 | NeededArgFPRs++; |
| 9529 | else if (Field2Ty) |
| 9530 | NeededArgGPRs++; |
| 9531 | return IsCandidate; |
| 9532 | } |
| 9533 | |
| 9534 | // Call getCoerceAndExpand for the two-element flattened struct described by |
| 9535 | // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an |
| 9536 | // appropriate coerceToType and unpaddedCoerceToType. |
| 9537 | ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct( |
| 9538 | llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty, |
| 9539 | CharUnits Field2Off) const { |
| 9540 | SmallVector<llvm::Type *, 3> CoerceElts; |
| 9541 | SmallVector<llvm::Type *, 2> UnpaddedCoerceElts; |
| 9542 | if (!Field1Off.isZero()) |
| 9543 | CoerceElts.push_back(llvm::ArrayType::get( |
| 9544 | llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity())); |
| 9545 | |
| 9546 | CoerceElts.push_back(Field1Ty); |
| 9547 | UnpaddedCoerceElts.push_back(Field1Ty); |
| 9548 | |
| 9549 | if (!Field2Ty) { |
| 9550 | return ABIArgInfo::getCoerceAndExpand( |
| 9551 | llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()), |
| 9552 | UnpaddedCoerceElts[0]); |
| 9553 | } |
| 9554 | |
| 9555 | CharUnits Field2Align = |
| 9556 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty)); |
| 9557 | CharUnits Field1Size = |
| 9558 | CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty)); |
| 9559 | CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align); |
| 9560 | |
| 9561 | CharUnits Padding = CharUnits::Zero(); |
| 9562 | if (Field2Off > Field2OffNoPadNoPack) |
| 9563 | Padding = Field2Off - Field2OffNoPadNoPack; |
| 9564 | else if (Field2Off != Field2Align && Field2Off > Field1Size) |
| 9565 | Padding = Field2Off - Field1Size; |
| 9566 | |
| 9567 | bool IsPacked = !Field2Off.isMultipleOf(Field2Align); |
| 9568 | |
| 9569 | if (!Padding.isZero()) |
| 9570 | CoerceElts.push_back(llvm::ArrayType::get( |
| 9571 | llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity())); |
| 9572 | |
| 9573 | CoerceElts.push_back(Field2Ty); |
| 9574 | UnpaddedCoerceElts.push_back(Field2Ty); |
| 9575 | |
| 9576 | auto CoerceToType = |
| 9577 | llvm::StructType::get(getVMContext(), CoerceElts, IsPacked); |
| 9578 | auto UnpaddedCoerceToType = |
| 9579 | llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked); |
| 9580 | |
| 9581 | return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType); |
| 9582 | } |
| 9583 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9584 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9585 | int &ArgGPRsLeft, |
| 9586 | int &ArgFPRsLeft) const { |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9587 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 9588 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 9589 | |
| 9590 | // Structures with either a non-trivial destructor or a non-trivial |
| 9591 | // copy constructor are always passed indirectly. |
| 9592 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 9593 | if (ArgGPRsLeft) |
| 9594 | ArgGPRsLeft -= 1; |
| 9595 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 9596 | CGCXXABI::RAA_DirectInMemory); |
| 9597 | } |
| 9598 | |
| 9599 | // Ignore empty structs/unions. |
| 9600 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9601 | return ABIArgInfo::getIgnore(); |
| 9602 | |
| 9603 | uint64_t Size = getContext().getTypeSize(Ty); |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9604 | |
| 9605 | // Pass floating point values via FPRs if possible. |
| 9606 | if (IsFixed && Ty->isFloatingType() && FLen >= Size && ArgFPRsLeft) { |
| 9607 | ArgFPRsLeft--; |
| 9608 | return ABIArgInfo::getDirect(); |
| 9609 | } |
| 9610 | |
| 9611 | // Complex types for the hard float ABI must be passed direct rather than |
| 9612 | // using CoerceAndExpand. |
| 9613 | if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) { |
Simon Pilgrim | 7e38f0c | 2019-10-07 16:42:25 +0000 | [diff] [blame] | 9614 | QualType EltTy = Ty->castAs<ComplexType>()->getElementType(); |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9615 | if (getContext().getTypeSize(EltTy) <= FLen) { |
| 9616 | ArgFPRsLeft -= 2; |
| 9617 | return ABIArgInfo::getDirect(); |
| 9618 | } |
| 9619 | } |
| 9620 | |
| 9621 | if (IsFixed && FLen && Ty->isStructureOrClassType()) { |
| 9622 | llvm::Type *Field1Ty = nullptr; |
| 9623 | llvm::Type *Field2Ty = nullptr; |
| 9624 | CharUnits Field1Off = CharUnits::Zero(); |
| 9625 | CharUnits Field2Off = CharUnits::Zero(); |
| 9626 | int NeededArgGPRs; |
| 9627 | int NeededArgFPRs; |
| 9628 | bool IsCandidate = |
| 9629 | detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off, |
| 9630 | NeededArgGPRs, NeededArgFPRs); |
| 9631 | if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft && |
| 9632 | NeededArgFPRs <= ArgFPRsLeft) { |
| 9633 | ArgGPRsLeft -= NeededArgGPRs; |
| 9634 | ArgFPRsLeft -= NeededArgFPRs; |
| 9635 | return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty, |
| 9636 | Field2Off); |
| 9637 | } |
| 9638 | } |
| 9639 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9640 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 9641 | bool MustUseStack = false; |
| 9642 | // Determine the number of GPRs needed to pass the current argument |
| 9643 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 9644 | // register pairs, so may consume 3 registers. |
| 9645 | int NeededArgGPRs = 1; |
| 9646 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 9647 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 9648 | else if (Size > XLen && Size <= 2 * XLen) |
| 9649 | NeededArgGPRs = 2; |
| 9650 | |
| 9651 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 9652 | MustUseStack = true; |
| 9653 | NeededArgGPRs = ArgGPRsLeft; |
| 9654 | } |
| 9655 | |
| 9656 | ArgGPRsLeft -= NeededArgGPRs; |
| 9657 | |
| 9658 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 9659 | // Treat an enum type as its underlying type. |
| 9660 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 9661 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 9662 | |
| 9663 | // All integral types are promoted to XLen width, unless passed on the |
| 9664 | // stack. |
| 9665 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 9666 | return extendType(Ty); |
| 9667 | } |
| 9668 | |
| 9669 | return ABIArgInfo::getDirect(); |
| 9670 | } |
| 9671 | |
| 9672 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 9673 | // so coerce to integers. |
| 9674 | if (Size <= 2 * XLen) { |
| 9675 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 9676 | |
| 9677 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 9678 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 9679 | if (Size <= XLen) { |
| 9680 | return ABIArgInfo::getDirect( |
| 9681 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 9682 | } else if (Alignment == 2 * XLen) { |
| 9683 | return ABIArgInfo::getDirect( |
| 9684 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 9685 | } else { |
| 9686 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 9687 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 9688 | } |
| 9689 | } |
| 9690 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 9691 | } |
| 9692 | |
| 9693 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 9694 | if (RetTy->isVoidType()) |
| 9695 | return ABIArgInfo::getIgnore(); |
| 9696 | |
| 9697 | int ArgGPRsLeft = 2; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9698 | int ArgFPRsLeft = FLen ? 2 : 0; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9699 | |
| 9700 | // The rules for return and argument types are the same, so defer to |
| 9701 | // classifyArgumentType. |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9702 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft, |
| 9703 | ArgFPRsLeft); |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9704 | } |
| 9705 | |
| 9706 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9707 | QualType Ty) const { |
| 9708 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 9709 | |
| 9710 | // Empty records are ignored for parameter passing purposes. |
| 9711 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 9712 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 9713 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 9714 | return Addr; |
| 9715 | } |
| 9716 | |
| 9717 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 9718 | getContext().getTypeInfoInChars(Ty); |
| 9719 | |
| 9720 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 9721 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 9722 | |
| 9723 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 9724 | SlotSize, /*AllowHigherAlign=*/true); |
| 9725 | } |
| 9726 | |
| 9727 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 9728 | int TySize = getContext().getTypeSize(Ty); |
| 9729 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 9730 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 9731 | return ABIArgInfo::getSignExtend(Ty); |
| 9732 | return ABIArgInfo::getExtend(Ty); |
| 9733 | } |
| 9734 | |
| 9735 | namespace { |
| 9736 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 9737 | public: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9738 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, |
| 9739 | unsigned FLen) |
| 9740 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen, FLen)) {} |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 9741 | |
| 9742 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 9743 | CodeGen::CodeGenModule &CGM) const override { |
| 9744 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 9745 | if (!FD) return; |
| 9746 | |
| 9747 | const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); |
| 9748 | if (!Attr) |
| 9749 | return; |
| 9750 | |
| 9751 | const char *Kind; |
| 9752 | switch (Attr->getInterrupt()) { |
| 9753 | case RISCVInterruptAttr::user: Kind = "user"; break; |
| 9754 | case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; |
| 9755 | case RISCVInterruptAttr::machine: Kind = "machine"; break; |
| 9756 | } |
| 9757 | |
| 9758 | auto *Fn = cast<llvm::Function>(GV); |
| 9759 | |
| 9760 | Fn->addFnAttr("interrupt", Kind); |
| 9761 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9762 | }; |
| 9763 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9764 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9765 | //===----------------------------------------------------------------------===// |
| 9766 | // Driver code |
| 9767 | //===----------------------------------------------------------------------===// |
| 9768 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9769 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 9770 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9771 | } |
| 9772 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 9773 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9774 | if (TheTargetCodeGenInfo) |
| 9775 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9776 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9777 | // Helper to set the unique_ptr while still keeping the return value. |
| 9778 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 9779 | this->TheTargetCodeGenInfo.reset(P); |
| 9780 | return *P; |
| 9781 | }; |
| 9782 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 9783 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 9784 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9785 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9786 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9787 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 9788 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9789 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 9790 | case llvm::Triple::mips: |
| 9791 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 9792 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9793 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 9794 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9795 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 9796 | case llvm::Triple::mips64: |
| 9797 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9798 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9799 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 9800 | case llvm::Triple::avr: |
| 9801 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 9802 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 9803 | case llvm::Triple::aarch64: |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 9804 | case llvm::Triple::aarch64_32: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 9805 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9806 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 9807 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9808 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 9809 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 9810 | return SetCGInfo( |
| 9811 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9812 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9813 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9814 | } |
| 9815 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9816 | case llvm::Triple::wasm32: |
| 9817 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9818 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9819 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9820 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 9821 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9822 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9823 | case llvm::Triple::thumbeb: { |
| 9824 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9825 | return SetCGInfo( |
| 9826 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9827 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9828 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9829 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9830 | StringRef ABIStr = getTarget().getABI(); |
| 9831 | if (ABIStr == "apcs-gnu") |
| 9832 | Kind = ARMABIInfo::APCS; |
| 9833 | else if (ABIStr == "aapcs16") |
| 9834 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9835 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9836 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9837 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9838 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9839 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9840 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9841 | |
| 9842 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9843 | } |
| 9844 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9845 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9846 | return SetCGInfo( |
Justin Hibbits | 3dac214 | 2019-09-05 13:38:46 +0000 | [diff] [blame] | 9847 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft" || |
| 9848 | getTarget().hasFeature("spe"))); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9849 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9850 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9851 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9852 | if (getTarget().getABI() == "elfv2") |
| 9853 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9854 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9855 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9856 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9857 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9858 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9859 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9860 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9861 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9862 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9863 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9864 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9865 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9866 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9867 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9868 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9869 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9870 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9871 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9872 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9873 | case llvm::Triple::nvptx: |
| 9874 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9875 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9876 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9877 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9878 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9879 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9880 | case llvm::Triple::riscv32: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9881 | case llvm::Triple::riscv64: { |
| 9882 | StringRef ABIStr = getTarget().getABI(); |
| 9883 | unsigned XLen = getTarget().getPointerWidth(0); |
| 9884 | unsigned ABIFLen = 0; |
| 9885 | if (ABIStr.endswith("f")) |
| 9886 | ABIFLen = 32; |
| 9887 | else if (ABIStr.endswith("d")) |
| 9888 | ABIFLen = 64; |
| 9889 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen)); |
| 9890 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9891 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9892 | case llvm::Triple::systemz: { |
| 9893 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9894 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9895 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9896 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9897 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9898 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9899 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9900 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9901 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9902 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9903 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9904 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9905 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9906 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9907 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9908 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9909 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9910 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9911 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9912 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9913 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9914 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 9915 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9916 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9917 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9918 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9919 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9920 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9921 | X86AVXABILevel AVXLevel = |
| 9922 | (ABI == "avx512" |
| 9923 | ? X86AVXABILevel::AVX512 |
| 9924 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9925 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9926 | switch (Triple.getOS()) { |
| 9927 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9928 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9929 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9930 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9931 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9932 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9933 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9934 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9935 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9936 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9937 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9938 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9939 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9940 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9941 | case llvm::Triple::sparc: |
| 9942 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9943 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9944 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9945 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9946 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 9947 | case llvm::Triple::arc: |
| 9948 | return SetCGInfo(new ARCTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9949 | case llvm::Triple::spir: |
| 9950 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9951 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9952 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9953 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9954 | |
| 9955 | /// Create an OpenCL kernel for an enqueued block. |
| 9956 | /// |
| 9957 | /// The kernel has the same function type as the block invoke function. Its |
| 9958 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9959 | /// It simply calls the block invoke function then returns. |
| 9960 | llvm::Function * |
| 9961 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9962 | llvm::Function *Invoke, |
| 9963 | llvm::Value *BlockLiteral) const { |
| 9964 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9965 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9966 | for (auto &P : InvokeFT->params()) |
| 9967 | ArgTys.push_back(P); |
| 9968 | auto &C = CGF.getLLVMContext(); |
| 9969 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9970 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9971 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9972 | &CGF.CGM.getModule()); |
| 9973 | auto IP = CGF.Builder.saveIP(); |
| 9974 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9975 | auto &Builder = CGF.Builder; |
| 9976 | Builder.SetInsertPoint(BB); |
| 9977 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9978 | for (auto &A : F->args()) |
| 9979 | Args.push_back(&A); |
| 9980 | Builder.CreateCall(Invoke, Args); |
| 9981 | Builder.CreateRetVoid(); |
| 9982 | Builder.restoreIP(IP); |
| 9983 | return F; |
| 9984 | } |
| 9985 | |
| 9986 | /// Create an OpenCL kernel for an enqueued block. |
| 9987 | /// |
| 9988 | /// The type of the first argument (the block literal) is the struct type |
| 9989 | /// of the block literal instead of a pointer type. The first argument |
| 9990 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9991 | /// allocates the same type of struct on stack and stores the block literal |
| 9992 | /// to it and passes its pointer to the block invoke function. The kernel |
| 9993 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 9994 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 9995 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 9996 | llvm::Value *BlockLiteral) const { |
| 9997 | auto &Builder = CGF.Builder; |
| 9998 | auto &C = CGF.getLLVMContext(); |
| 9999 | |
| 10000 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 10001 | auto *InvokeFT = Invoke->getFunctionType(); |
| 10002 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 10003 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 10004 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 10005 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 10006 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 10007 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 10008 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 10009 | |
| 10010 | ArgTys.push_back(BlockTy); |
| 10011 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 10012 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 10013 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 10014 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 10015 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 10016 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 10017 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 10018 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 10019 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 10020 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 10021 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 10022 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 10023 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 10024 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 10025 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 10026 | } |
| 10027 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 10028 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 10029 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 10030 | &CGF.CGM.getModule()); |
| 10031 | F->addFnAttr("enqueued-block"); |
| 10032 | auto IP = CGF.Builder.saveIP(); |
| 10033 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 10034 | Builder.SetInsertPoint(BB); |
| 10035 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 10036 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
Guillaume Chatelet | ab11b91 | 2019-09-30 13:34:44 +0000 | [diff] [blame] | 10037 | BlockPtr->setAlignment(llvm::MaybeAlign(BlockAlign)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 10038 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 10039 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 10040 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 10041 | Args.push_back(Cast); |
| 10042 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 10043 | Args.push_back(I); |
| 10044 | Builder.CreateCall(Invoke, Args); |
| 10045 | Builder.CreateRetVoid(); |
| 10046 | Builder.restoreIP(IP); |
| 10047 | |
| 10048 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 10049 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 10050 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 10051 | F->setMetadata("kernel_arg_base_type", |
| 10052 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 10053 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 10054 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 10055 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 10056 | |
| 10057 | return F; |
| 10058 | } |