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 { |
| 836 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 837 | getContext().getTypeInfoInChars(Ty), |
| 838 | CharUnits::fromQuantity(4), |
| 839 | /*AllowHigherAlign=*/ true); |
| 840 | } |
| 841 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 842 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 843 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 844 | // |
| 845 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 846 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 847 | //===----------------------------------------------------------------------===// |
| 848 | |
| 849 | class PNaClABIInfo : public ABIInfo { |
| 850 | public: |
| 851 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 852 | |
| 853 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 854 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 855 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 856 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 857 | Address EmitVAArg(CodeGenFunction &CGF, |
| 858 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 859 | }; |
| 860 | |
| 861 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 862 | public: |
| 863 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 864 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 865 | }; |
| 866 | |
| 867 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 868 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 869 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 870 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 871 | for (auto &I : FI.arguments()) |
| 872 | I.info = classifyArgumentType(I.type); |
| 873 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 874 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 875 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 876 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 877 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 878 | // function classification. Structs get passed directly for varargs |
| 879 | // functions, through a rewriting transform in |
| 880 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 881 | // this target to actually support a va_arg instructions with an |
| 882 | // aggregate type, unlike other targets. |
| 883 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 886 | /// Classify argument of given type \p Ty. |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 887 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 888 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 889 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 890 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 891 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 892 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 893 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 894 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 895 | } else if (Ty->isFloatingType()) { |
| 896 | // Floating-point types don't go inreg. |
| 897 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 898 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 899 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 900 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 901 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 905 | if (RetTy->isVoidType()) |
| 906 | return ABIArgInfo::getIgnore(); |
| 907 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 908 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 909 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 910 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 911 | |
| 912 | // Treat an enum type as its underlying type. |
| 913 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 914 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 915 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 916 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 917 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 920 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 921 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 922 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
| 923 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 924 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 925 | IRType->getScalarSizeInBits() != 64; |
| 926 | } |
| 927 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 928 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 929 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 930 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 931 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 932 | .Cases("y", "&y", "^Ym", true) |
| 933 | .Default(false); |
| 934 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 935 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 936 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 937 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 940 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 944 | return Ty; |
| 945 | } |
| 946 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 947 | /// Returns true if this type can be passed in SSE registers with the |
| 948 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 949 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 950 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 951 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 952 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 953 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 954 | &llvm::APFloat::x87DoubleExtended()) |
| 955 | return false; |
| 956 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 957 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 958 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 959 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 960 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 961 | // registers specially. |
| 962 | unsigned VecSize = Context.getTypeSize(VT); |
| 963 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 964 | return true; |
| 965 | } |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 970 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 971 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 972 | return NumMembers <= 4; |
| 973 | } |
| 974 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 975 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 976 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 977 | auto AI = ABIArgInfo::getDirect(T); |
| 978 | AI.setInReg(true); |
| 979 | AI.setCanBeFlattened(false); |
| 980 | return AI; |
| 981 | } |
| 982 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 983 | //===----------------------------------------------------------------------===// |
| 984 | // X86-32 ABI Implementation |
| 985 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 986 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 987 | /// Similar to llvm::CCState, but for Clang. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 988 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 989 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 990 | |
| 991 | unsigned CC; |
| 992 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 993 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 994 | }; |
| 995 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 996 | enum { |
| 997 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 998 | VectorcallMaxParamNumAsReg = 6 |
| 999 | }; |
| 1000 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1001 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1002 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1003 | enum Class { |
| 1004 | Integer, |
| 1005 | Float |
| 1006 | }; |
| 1007 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1008 | static const unsigned MinABIStackAlignInBytes = 4; |
| 1009 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1010 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1011 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1012 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1013 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1014 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1015 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1016 | |
| 1017 | static bool isRegisterSize(unsigned Size) { |
| 1018 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 1019 | } |
| 1020 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1021 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1022 | // FIXME: Assumes vectorcall is in use. |
| 1023 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1024 | } |
| 1025 | |
| 1026 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1027 | uint64_t NumMembers) const override { |
| 1028 | // FIXME: Assumes vectorcall is in use. |
| 1029 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1030 | } |
| 1031 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1032 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1033 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1034 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 1035 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1036 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 1037 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1038 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1039 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1040 | /// Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1041 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1042 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1043 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1044 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1045 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1046 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1047 | /// Updates the number of available free registers, returns |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1048 | /// true if any registers were allocated. |
| 1049 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 1050 | |
| 1051 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1052 | bool &NeedsPadding) const; |
| 1053 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1054 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1055 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1056 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1057 | /// Rewrite the function info so that all memory arguments use |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1058 | /// inalloca. |
| 1059 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1060 | |
| 1061 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1062 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1063 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1064 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1065 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1066 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1067 | public: |
| 1068 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1069 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1070 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1071 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1072 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1073 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1074 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1075 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1076 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1077 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1078 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1079 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1080 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1081 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1082 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1083 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1084 | bool asReturnValue) const override { |
| 1085 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1086 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1087 | // four vector registers for vectors, but those can overlap with the |
| 1088 | // scalar registers. |
| 1089 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1090 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1091 | |
| 1092 | bool isSwiftErrorInRegister() const override { |
| 1093 | // x86-32 lowering does not support passing swifterror in a register. |
| 1094 | return false; |
| 1095 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1096 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1097 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1098 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1099 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1100 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1101 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1102 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1103 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1104 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1105 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1106 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1107 | static bool isStructReturnInRegABI( |
| 1108 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1109 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1110 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1111 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1112 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1113 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1114 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1115 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1116 | return 4; |
| 1117 | } |
| 1118 | |
| 1119 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1120 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1121 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1122 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1123 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1124 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1125 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1126 | } |
| 1127 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1128 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1129 | std::string &Constraints, |
| 1130 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1131 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1132 | std::vector<LValue> &ResultRegDests, |
| 1133 | std::string &AsmString, |
| 1134 | unsigned NumOutputs) const override; |
| 1135 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1136 | llvm::Constant * |
| 1137 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1138 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1139 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1140 | ('v' << 16) | |
| 1141 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1142 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1143 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1144 | |
| 1145 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1146 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1147 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1148 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1149 | }; |
| 1150 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1151 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1152 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1153 | /// Rewrite input constraint references after adding some output constraints. |
| 1154 | /// In the case where there is one output and one input and we add one output, |
| 1155 | /// we need to replace all operand references greater than or equal to 1: |
| 1156 | /// mov $0, $1 |
| 1157 | /// mov eax, $1 |
| 1158 | /// The result will be: |
| 1159 | /// mov $0, $2 |
| 1160 | /// mov eax, $2 |
| 1161 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1162 | unsigned NumNewOuts, |
| 1163 | std::string &AsmString) { |
| 1164 | std::string Buf; |
| 1165 | llvm::raw_string_ostream OS(Buf); |
| 1166 | size_t Pos = 0; |
| 1167 | while (Pos < AsmString.size()) { |
| 1168 | size_t DollarStart = AsmString.find('$', Pos); |
| 1169 | if (DollarStart == std::string::npos) |
| 1170 | DollarStart = AsmString.size(); |
| 1171 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1172 | if (DollarEnd == std::string::npos) |
| 1173 | DollarEnd = AsmString.size(); |
| 1174 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1175 | Pos = DollarEnd; |
| 1176 | size_t NumDollars = DollarEnd - DollarStart; |
| 1177 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1178 | // We have an operand reference. |
| 1179 | size_t DigitStart = Pos; |
| 1180 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1181 | if (DigitEnd == std::string::npos) |
| 1182 | DigitEnd = AsmString.size(); |
| 1183 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1184 | unsigned OperandIndex; |
| 1185 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1186 | if (OperandIndex >= FirstIn) |
| 1187 | OperandIndex += NumNewOuts; |
| 1188 | OS << OperandIndex; |
| 1189 | } else { |
| 1190 | OS << OperandStr; |
| 1191 | } |
| 1192 | Pos = DigitEnd; |
| 1193 | } |
| 1194 | } |
| 1195 | AsmString = std::move(OS.str()); |
| 1196 | } |
| 1197 | |
| 1198 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1199 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1200 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1201 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1202 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1203 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1204 | unsigned NumOutputs) const { |
| 1205 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1206 | |
| 1207 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1208 | // larger. |
| 1209 | if (!Constraints.empty()) |
| 1210 | Constraints += ','; |
| 1211 | if (RetWidth <= 32) { |
| 1212 | Constraints += "={eax}"; |
| 1213 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1214 | } else { |
| 1215 | // Use the 'A' constraint for EAX:EDX. |
| 1216 | Constraints += "=A"; |
| 1217 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1218 | } |
| 1219 | |
| 1220 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1221 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1222 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1223 | |
| 1224 | // Coerce the integer by bitcasting the return slot pointer. |
| 1225 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1226 | CoerceTy->getPointerTo())); |
| 1227 | ResultRegDests.push_back(ReturnSlot); |
| 1228 | |
| 1229 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1230 | } |
| 1231 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1232 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1233 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1234 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1235 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1236 | uint64_t Size = Context.getTypeSize(Ty); |
| 1237 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1238 | // For i386, type must be register sized. |
| 1239 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1240 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1241 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1242 | |
| 1243 | if (Ty->isVectorType()) { |
| 1244 | // 64- and 128- bit vectors inside structures are not returned in |
| 1245 | // registers. |
| 1246 | if (Size == 64 || Size == 128) |
| 1247 | return false; |
| 1248 | |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1252 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1253 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1254 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1255 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1256 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1257 | return true; |
| 1258 | |
| 1259 | // Arrays are treated like records. |
| 1260 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1261 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1262 | |
| 1263 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1264 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1265 | if (!RT) return false; |
| 1266 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1267 | // FIXME: Traverse bases here too. |
| 1268 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1269 | // Structure types are passed in register if all fields would be |
| 1270 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1271 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1272 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1273 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1274 | continue; |
| 1275 | |
| 1276 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1277 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1278 | return false; |
| 1279 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1280 | return true; |
| 1281 | } |
| 1282 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1283 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1284 | // Treat complex types as the element type. |
| 1285 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1286 | Ty = CTy->getElementType(); |
| 1287 | |
| 1288 | // Check for a type which we know has a simple scalar argument-passing |
| 1289 | // convention without any padding. (We're specifically looking for 32 |
| 1290 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1291 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1292 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1293 | return false; |
| 1294 | |
| 1295 | uint64_t Size = Context.getTypeSize(Ty); |
| 1296 | return Size == 32 || Size == 64; |
| 1297 | } |
| 1298 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1299 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1300 | uint64_t &Size) { |
| 1301 | for (const auto *FD : RD->fields()) { |
| 1302 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1303 | // argument is smaller than 32-bits, expanding the struct will create |
| 1304 | // alignment padding. |
| 1305 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1306 | return false; |
| 1307 | |
| 1308 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1309 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1310 | // counts as "basic" is more complicated than what we were doing previously. |
| 1311 | if (FD->isBitField()) |
| 1312 | return false; |
| 1313 | |
| 1314 | Size += Context.getTypeSize(FD->getType()); |
| 1315 | } |
| 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1320 | uint64_t &Size) { |
| 1321 | // Don't do this if there are any non-empty bases. |
| 1322 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1323 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1324 | Size)) |
| 1325 | return false; |
| 1326 | } |
| 1327 | if (!addFieldSizes(Context, RD, Size)) |
| 1328 | return false; |
| 1329 | return true; |
| 1330 | } |
| 1331 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1332 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1333 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1334 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1335 | /// optimizations. |
| 1336 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1337 | // We can only expand structure types. |
| 1338 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1339 | if (!RT) |
| 1340 | return false; |
| 1341 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1342 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1343 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1344 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1345 | // On non-Windows, we have to conservatively match our old bitcode |
| 1346 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1347 | if (!CXXRD->isCLike()) |
| 1348 | return false; |
| 1349 | } else { |
| 1350 | // Don't do this for dynamic classes. |
| 1351 | if (CXXRD->isDynamicClass()) |
| 1352 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1353 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1354 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1355 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1356 | } else { |
| 1357 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1358 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | // We can do this if there was no alignment padding. |
| 1362 | return Size == getContext().getTypeSize(Ty); |
| 1363 | } |
| 1364 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1365 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1366 | // If the return value is indirect, then the hidden argument is consuming one |
| 1367 | // integer register. |
| 1368 | if (State.FreeRegs) { |
| 1369 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1370 | if (!IsMCUABI) |
| 1371 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1372 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1373 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1376 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1377 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1378 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1379 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1380 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1381 | const Type *Base = nullptr; |
| 1382 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1383 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1384 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1385 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1386 | // The LLVM struct type for such an aggregate should lower properly. |
| 1387 | return ABIArgInfo::getDirect(); |
| 1388 | } |
| 1389 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1390 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1391 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1392 | if (IsDarwinVectorABI) { |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1393 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 1394 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1395 | // 128-bit vectors are a special case; they are returned in |
| 1396 | // registers and we need to make sure to pick a type the LLVM |
| 1397 | // backend will like. |
| 1398 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1399 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1400 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Always return in register if it fits in a general purpose |
| 1403 | // register, or if it is 64 bits and has a single element. |
| 1404 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1405 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1406 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1407 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1408 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1409 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1413 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1414 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1415 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1416 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1417 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1418 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1419 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1420 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1421 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1422 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1423 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1424 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1425 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1426 | // Ignore empty structs/unions. |
| 1427 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1428 | return ABIArgInfo::getIgnore(); |
| 1429 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1430 | // Small structures which are register sized are generally returned |
| 1431 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1432 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1433 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1434 | |
| 1435 | // As a special-case, if the struct is a "single-element" struct, and |
| 1436 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1437 | // floating-point register. (MSVC does not apply this special case.) |
| 1438 | // We apply a similar transformation for pointer types to improve the |
| 1439 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1440 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1441 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1442 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1443 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1444 | |
| 1445 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1446 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1447 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1450 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1451 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1452 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1453 | // Treat an enum type as its underlying type. |
| 1454 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1455 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1456 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1457 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1458 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1461 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1462 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1463 | } |
| 1464 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1465 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1466 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1467 | if (!RT) |
| 1468 | return 0; |
| 1469 | const RecordDecl *RD = RT->getDecl(); |
| 1470 | |
| 1471 | // If this is a C++ record, check the bases first. |
| 1472 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1473 | for (const auto &I : CXXRD->bases()) |
| 1474 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1475 | return false; |
| 1476 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1477 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1478 | QualType FT = i->getType(); |
| 1479 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1480 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1481 | return true; |
| 1482 | |
| 1483 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1490 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1491 | unsigned Align) const { |
| 1492 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1493 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1494 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1495 | return 0; // Use default alignment. |
| 1496 | |
Pengfei Wang | 48387ec | 2019-05-31 01:50:07 +0000 | [diff] [blame] | 1497 | // On non-Darwin, the stack type alignment is always 4. |
| 1498 | if (!IsDarwinVectorABI) { |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1499 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1500 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1501 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1502 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1503 | // 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] | 1504 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1505 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1506 | return 16; |
| 1507 | |
| 1508 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1511 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1512 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1513 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1514 | if (State.FreeRegs) { |
| 1515 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1516 | if (!IsMCUABI) |
| 1517 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1518 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1519 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1520 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1521 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1522 | // Compute the byval alignment. |
| 1523 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1524 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1525 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1526 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1527 | |
| 1528 | // If the stack alignment is less than the type alignment, realign the |
| 1529 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1530 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1531 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1532 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1535 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1536 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1537 | if (!T) |
| 1538 | T = Ty.getTypePtr(); |
| 1539 | |
| 1540 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1541 | BuiltinType::Kind K = BT->getKind(); |
| 1542 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1543 | return Float; |
| 1544 | } |
| 1545 | return Integer; |
| 1546 | } |
| 1547 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1548 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1549 | if (!IsSoftFloatABI) { |
| 1550 | Class C = classify(Ty); |
| 1551 | if (C == Float) |
| 1552 | return false; |
| 1553 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1554 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1555 | unsigned Size = getContext().getTypeSize(Ty); |
| 1556 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (SizeInRegs == 0) |
| 1559 | return false; |
| 1560 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1561 | if (!IsMCUABI) { |
| 1562 | if (SizeInRegs > State.FreeRegs) { |
| 1563 | State.FreeRegs = 0; |
| 1564 | return false; |
| 1565 | } |
| 1566 | } else { |
| 1567 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1568 | // earlier parameters that are passed on the stack. Also, |
| 1569 | // it does not allow passing >8-byte structs in-register, |
| 1570 | // even if there are 3 free registers available. |
| 1571 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1572 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1573 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1574 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1575 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1576 | return true; |
| 1577 | } |
| 1578 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1579 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1580 | bool &InReg, |
| 1581 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1582 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1583 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1584 | // (HFAs) have already been dealt with at this point. |
| 1585 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1586 | return false; |
| 1587 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1588 | NeedsPadding = false; |
| 1589 | InReg = !IsMCUABI; |
| 1590 | |
| 1591 | if (!updateFreeRegs(Ty, State)) |
| 1592 | return false; |
| 1593 | |
| 1594 | if (IsMCUABI) |
| 1595 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1596 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1597 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1598 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1599 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1600 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1601 | NeedsPadding = true; |
| 1602 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1603 | return false; |
| 1604 | } |
| 1605 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1606 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1609 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1610 | if (!updateFreeRegs(Ty, State)) |
| 1611 | return false; |
| 1612 | |
| 1613 | if (IsMCUABI) |
| 1614 | return false; |
| 1615 | |
| 1616 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1617 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1618 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1619 | if (getContext().getTypeSize(Ty) > 32) |
| 1620 | return false; |
| 1621 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1622 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1623 | Ty->isReferenceType()); |
| 1624 | } |
| 1625 | |
| 1626 | return true; |
| 1627 | } |
| 1628 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1629 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1630 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1631 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1632 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1633 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1634 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1635 | // Check with the C++ ABI first. |
| 1636 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1637 | if (RT) { |
| 1638 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1639 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1640 | return getIndirectResult(Ty, false, State); |
| 1641 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1642 | // The field index doesn't matter, we'll fix it up later. |
| 1643 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1644 | } |
| 1645 | } |
| 1646 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1647 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1648 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1649 | const Type *Base = nullptr; |
| 1650 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1651 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1652 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1653 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1654 | if (State.FreeSSERegs >= NumElts) { |
| 1655 | State.FreeSSERegs -= NumElts; |
| 1656 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1657 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1658 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1659 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1660 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1664 | // Structures with flexible arrays are always indirect. |
| 1665 | // FIXME: This should not be byval! |
| 1666 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1667 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1668 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1669 | // Ignore empty structs/unions on non-Windows. |
| 1670 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1671 | return ABIArgInfo::getIgnore(); |
| 1672 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1673 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1674 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1675 | bool NeedsPadding = false; |
| 1676 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1677 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1678 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1679 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1680 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1681 | if (InReg) |
| 1682 | return ABIArgInfo::getDirectInReg(Result); |
| 1683 | else |
| 1684 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1685 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1686 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1687 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1688 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1689 | // of those arguments will match the struct. This is important because the |
| 1690 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1691 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1692 | // Don't do this for the MCU if there are still free integer registers |
| 1693 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1694 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1695 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1696 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1697 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1698 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1699 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1700 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1701 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1702 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1705 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1706 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1707 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1708 | if (IsDarwinVectorABI) { |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1709 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1710 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1711 | (Size == 64 && VT->getNumElements() == 1)) |
| 1712 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1713 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1714 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1715 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1716 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1717 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
| 1718 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1719 | return ABIArgInfo::getDirect(); |
| 1720 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1721 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1722 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1723 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1724 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1725 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1726 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1727 | |
| 1728 | if (Ty->isPromotableIntegerType()) { |
| 1729 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1730 | return ABIArgInfo::getExtendInReg(Ty); |
| 1731 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1732 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1733 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1734 | if (InReg) |
| 1735 | return ABIArgInfo::getDirectInReg(); |
| 1736 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1739 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1740 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1741 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1742 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1743 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1744 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1745 | // registers. |
| 1746 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1747 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1748 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1749 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1750 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1751 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1752 | // First pass do all the vector types. |
| 1753 | const Type *Base = nullptr; |
| 1754 | uint64_t NumElts = 0; |
| 1755 | const QualType& Ty = I.type; |
| 1756 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1757 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1758 | if (State.FreeSSERegs >= NumElts) { |
| 1759 | State.FreeSSERegs -= NumElts; |
| 1760 | I.info = ABIArgInfo::getDirect(); |
| 1761 | } else { |
| 1762 | I.info = classifyArgumentType(Ty, State); |
| 1763 | } |
| 1764 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1765 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1766 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1767 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1768 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1769 | // Second pass, do the rest! |
| 1770 | const Type *Base = nullptr; |
| 1771 | uint64_t NumElts = 0; |
| 1772 | const QualType& Ty = I.type; |
| 1773 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1774 | |
| 1775 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1776 | // Assign true HVAs (non vector/native FP types). |
| 1777 | if (State.FreeSSERegs >= NumElts) { |
| 1778 | State.FreeSSERegs -= NumElts; |
| 1779 | I.info = getDirectX86Hva(); |
| 1780 | } else { |
| 1781 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1782 | } |
| 1783 | } else if (!IsHva) { |
| 1784 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1785 | I.info = classifyArgumentType(Ty, State); |
| 1786 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1787 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1791 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1792 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1793 | if (IsMCUABI) |
| 1794 | State.FreeRegs = 3; |
| 1795 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1796 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1797 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1798 | State.FreeRegs = 2; |
| 1799 | State.FreeSSERegs = 6; |
| 1800 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1801 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1802 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1803 | State.FreeRegs = 5; |
| 1804 | State.FreeSSERegs = 8; |
| 1805 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1806 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1807 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 1808 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1809 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1810 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1811 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1812 | // return value was sret and put it in a register ourselves if appropriate. |
| 1813 | if (State.FreeRegs) { |
| 1814 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1815 | if (!IsMCUABI) |
| 1816 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1819 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1820 | // The chain argument effectively gives us another free register. |
| 1821 | if (FI.isChainCall()) |
| 1822 | ++State.FreeRegs; |
| 1823 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1824 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1825 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1826 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1827 | } else { |
| 1828 | // If not vectorcall, revert to normal behavior. |
| 1829 | for (auto &I : FI.arguments()) { |
| 1830 | I.info = classifyArgumentType(I.type, State); |
| 1831 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1832 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1836 | // all the memory arguments to use inalloca. |
| 1837 | if (UsedInAlloca) |
| 1838 | rewriteWithInAlloca(FI); |
| 1839 | } |
| 1840 | |
| 1841 | void |
| 1842 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1843 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1844 | QualType Type) const { |
| 1845 | // Arguments are always 4-byte-aligned. |
| 1846 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1847 | |
| 1848 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1849 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1850 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1851 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1852 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1853 | // Insert padding bytes to respect alignment. |
| 1854 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1855 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1856 | if (StackOffset != FieldEnd) { |
| 1857 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1858 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1859 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1860 | FrameFields.push_back(Ty); |
| 1861 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1864 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1865 | // Leave ignored and inreg arguments alone. |
| 1866 | switch (Info.getKind()) { |
| 1867 | case ABIArgInfo::InAlloca: |
| 1868 | return true; |
| 1869 | case ABIArgInfo::Indirect: |
| 1870 | assert(Info.getIndirectByVal()); |
| 1871 | return true; |
| 1872 | case ABIArgInfo::Ignore: |
| 1873 | return false; |
| 1874 | case ABIArgInfo::Direct: |
| 1875 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1876 | if (Info.getInReg()) |
| 1877 | return false; |
| 1878 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1879 | case ABIArgInfo::Expand: |
| 1880 | case ABIArgInfo::CoerceAndExpand: |
| 1881 | // These are aggregate types which are never passed in registers when |
| 1882 | // inalloca is involved. |
| 1883 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1884 | } |
| 1885 | llvm_unreachable("invalid enum"); |
| 1886 | } |
| 1887 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1888 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1889 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1890 | |
| 1891 | // Build a packed struct type for all of the arguments in memory. |
| 1892 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1893 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1894 | // The stack alignment is always 4. |
| 1895 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1896 | |
| 1897 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1898 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1899 | |
| 1900 | // Put 'this' into the struct before 'sret', if necessary. |
| 1901 | bool IsThisCall = |
| 1902 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1903 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1904 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1905 | isArgInAlloca(I->info)) { |
| 1906 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1907 | ++I; |
| 1908 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1909 | |
| 1910 | // 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] | 1911 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1912 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1913 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1914 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1915 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1919 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1920 | ++I; |
| 1921 | |
| 1922 | // Put arguments passed in memory into the struct. |
| 1923 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1924 | if (isArgInAlloca(I->info)) |
| 1925 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1929 | /*isPacked=*/true), |
| 1930 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1933 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1934 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1935 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1936 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1937 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1938 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1939 | // |
| 1940 | // Just messing with TypeInfo like this works because we never pass |
| 1941 | // anything indirectly. |
| 1942 | TypeInfo.second = CharUnits::fromQuantity( |
| 1943 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1944 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1945 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1946 | TypeInfo, CharUnits::fromQuantity(4), |
| 1947 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1950 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1951 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1952 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1953 | |
| 1954 | switch (Opts.getStructReturnConvention()) { |
| 1955 | case CodeGenOptions::SRCK_Default: |
| 1956 | break; |
| 1957 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1958 | return false; |
| 1959 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1960 | return true; |
| 1961 | } |
| 1962 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1963 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1964 | return true; |
| 1965 | |
| 1966 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1967 | case llvm::Triple::DragonFly: |
| 1968 | case llvm::Triple::FreeBSD: |
| 1969 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1970 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1971 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1972 | default: |
| 1973 | return false; |
| 1974 | } |
| 1975 | } |
| 1976 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1977 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1978 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1979 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1980 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1981 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1982 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1983 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 1984 | Fn->addFnAttr("stackrealign"); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1985 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1986 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1987 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1988 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1989 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1990 | } |
| 1991 | } |
| 1992 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1993 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1994 | CodeGen::CodeGenFunction &CGF, |
| 1995 | llvm::Value *Address) const { |
| 1996 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1997 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1998 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1999 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2000 | // 0-7 are the eight integer registers; the order is different |
| 2001 | // on Darwin (for EH), but the range is the same. |
| 2002 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2003 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2004 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2005 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2006 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 2007 | // These have size 16, which is sizeof(long double) on |
| 2008 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2009 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2010 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2011 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2012 | } else { |
| 2013 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 2014 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2015 | Builder.CreateAlignedStore( |
| 2016 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 2017 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2018 | |
| 2019 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 2020 | // These have size 12, which is sizeof(long double) on |
| 2021 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2022 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2023 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 2024 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2025 | |
| 2026 | return false; |
| 2027 | } |
| 2028 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2029 | //===----------------------------------------------------------------------===// |
| 2030 | // X86-64 ABI Implementation |
| 2031 | //===----------------------------------------------------------------------===// |
| 2032 | |
| 2033 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2034 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2035 | /// The AVX ABI level for X86 targets. |
| 2036 | enum class X86AVXABILevel { |
| 2037 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2038 | AVX, |
| 2039 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2040 | }; |
| 2041 | |
| 2042 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2043 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2044 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2045 | case X86AVXABILevel::AVX512: |
| 2046 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2047 | case X86AVXABILevel::AVX: |
| 2048 | return 256; |
| 2049 | case X86AVXABILevel::None: |
| 2050 | return 128; |
| 2051 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2052 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2055 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2056 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2057 | enum Class { |
| 2058 | Integer = 0, |
| 2059 | SSE, |
| 2060 | SSEUp, |
| 2061 | X87, |
| 2062 | X87Up, |
| 2063 | ComplexX87, |
| 2064 | NoClass, |
| 2065 | Memory |
| 2066 | }; |
| 2067 | |
| 2068 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2069 | /// |
| 2070 | /// Merge an accumulating classification \arg Accum with a field |
| 2071 | /// classification \arg Field. |
| 2072 | /// |
| 2073 | /// \param Accum - The accumulating classification. This should |
| 2074 | /// always be either NoClass or the result of a previous merge |
| 2075 | /// call. In addition, this should never be Memory (the caller |
| 2076 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2077 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2078 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2079 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2080 | /// |
| 2081 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2082 | /// final MEMORY or SSE classes when necessary. |
| 2083 | /// |
| 2084 | /// \param AggregateSize - The size of the current aggregate in |
| 2085 | /// the classification process. |
| 2086 | /// |
| 2087 | /// \param Lo - The classification for the parts of the type |
| 2088 | /// residing in the low word of the containing object. |
| 2089 | /// |
| 2090 | /// \param Hi - The classification for the parts of the type |
| 2091 | /// residing in the higher words of the containing object. |
| 2092 | /// |
| 2093 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2094 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2095 | /// classify - Determine the x86_64 register classes in which the |
| 2096 | /// given type T should be passed. |
| 2097 | /// |
| 2098 | /// \param Lo - The classification for the parts of the type |
| 2099 | /// residing in the low word of the containing object. |
| 2100 | /// |
| 2101 | /// \param Hi - The classification for the parts of the type |
| 2102 | /// residing in the high word of the containing object. |
| 2103 | /// |
| 2104 | /// \param OffsetBase - The bit offset of this type in the |
| 2105 | /// containing object. Some parameters are classified different |
| 2106 | /// depending on whether they straddle an eightbyte boundary. |
| 2107 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2108 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2109 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2110 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2111 | /// If a word is unused its result will be NoClass; if a type should |
| 2112 | /// be passed in Memory then at least the classification of \arg Lo |
| 2113 | /// will be Memory. |
| 2114 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2115 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2116 | /// |
| 2117 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2118 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2119 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2120 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2121 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2122 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2123 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2124 | unsigned IROffset, QualType SourceTy, |
| 2125 | unsigned SourceOffset) const; |
| 2126 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2127 | unsigned IROffset, QualType SourceTy, |
| 2128 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2129 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2130 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2131 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2132 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2133 | |
| 2134 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2135 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2136 | /// |
| 2137 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2138 | /// available. |
| 2139 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2140 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2141 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2142 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2143 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2144 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2145 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2146 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2147 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2148 | unsigned &NeededSSE) const; |
| 2149 | |
| 2150 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2151 | unsigned &NeededSSE) const; |
| 2152 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2153 | bool IsIllegalVectorType(QualType Ty) const; |
| 2154 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2155 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2156 | /// unfortunately in ways that were not always consistent with |
| 2157 | /// certain previous compilers. In particular, platforms which |
| 2158 | /// required strict binary compatibility with older versions of GCC |
| 2159 | /// may need to exempt themselves. |
| 2160 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2161 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2164 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2165 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2166 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2167 | // Clang <= 3.8 did not do this. |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 2168 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2169 | LangOptions::ClangABI::Ver3_8) |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2170 | return false; |
| 2171 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2172 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2173 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2174 | return false; |
| 2175 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2176 | return false; |
| 2177 | return true; |
| 2178 | } |
| 2179 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2180 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2181 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2182 | // 64-bit hardware. |
| 2183 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2184 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2185 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2186 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2187 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2188 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2189 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2190 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2191 | bool isPassedUsingAVXType(QualType type) const { |
| 2192 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2193 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2194 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2195 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2196 | if (info.isDirect()) { |
| 2197 | llvm::Type *ty = info.getCoerceToType(); |
| 2198 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2199 | return (vectorTy->getBitWidth() > 128); |
| 2200 | } |
| 2201 | return false; |
| 2202 | } |
| 2203 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2204 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2205 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2206 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2207 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2208 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2209 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2210 | |
| 2211 | bool has64BitPointers() const { |
| 2212 | return Has64BitPointers; |
| 2213 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2214 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2215 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2216 | bool asReturnValue) const override { |
| 2217 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2218 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2219 | bool isSwiftErrorInRegister() const override { |
| 2220 | return true; |
| 2221 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2222 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2223 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2224 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2225 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2226 | public: |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2227 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2228 | : SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2229 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2230 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2231 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2232 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2233 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2234 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2235 | |
| 2236 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2237 | // FIXME: Assumes vectorcall is in use. |
| 2238 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2239 | } |
| 2240 | |
| 2241 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2242 | uint64_t NumMembers) const override { |
| 2243 | // FIXME: Assumes vectorcall is in use. |
| 2244 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2245 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2246 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2247 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2248 | bool asReturnValue) const override { |
| 2249 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2250 | } |
| 2251 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2252 | bool isSwiftErrorInRegister() const override { |
| 2253 | return true; |
| 2254 | } |
| 2255 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2256 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2257 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2258 | bool IsVectorCall, bool IsRegCall) const; |
| 2259 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2260 | const ABIArgInfo ¤t) const; |
| 2261 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2262 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2263 | |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2264 | X86AVXABILevel AVXLevel; |
| 2265 | |
| 2266 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2267 | }; |
| 2268 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2269 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2270 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2271 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2272 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2273 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2274 | const X86_64ABIInfo &getABIInfo() const { |
| 2275 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2276 | } |
| 2277 | |
Akira Hatanaka | 65bb3f9 | 2019-03-21 19:59:49 +0000 | [diff] [blame] | 2278 | /// Disable tail call on x86-64. The epilogue code before the tail jump blocks |
| 2279 | /// the autoreleaseRV/retainRV optimization. |
| 2280 | bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override { |
| 2281 | return true; |
| 2282 | } |
| 2283 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2284 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2285 | return 7; |
| 2286 | } |
| 2287 | |
| 2288 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2289 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2290 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2291 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2292 | // 0-15 are the 16 integer registers. |
| 2293 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2294 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2295 | return false; |
| 2296 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2297 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2298 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2299 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2300 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2301 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2302 | } |
| 2303 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2304 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2305 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2306 | // The default CC on x86-64 sets %al to the number of SSA |
| 2307 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2308 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2309 | // that when AVX types are involved: the ABI explicitly states it is |
| 2310 | // undefined, and it doesn't work in practice because of how the ABI |
| 2311 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2312 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2313 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2314 | for (CallArgList::const_iterator |
| 2315 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2316 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2317 | HasAVXType = true; |
| 2318 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2321 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2322 | if (!HasAVXType) |
| 2323 | return true; |
| 2324 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2325 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2326 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2329 | llvm::Constant * |
| 2330 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2331 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2332 | (0x06 << 8) | // .+0x08 |
| 2333 | ('v' << 16) | |
| 2334 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2335 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2336 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2337 | |
| 2338 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2339 | CodeGen::CodeGenModule &CGM) const override { |
| 2340 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2341 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2342 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2343 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2344 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2345 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2346 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2347 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2348 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2349 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2350 | } |
| 2351 | } |
| 2352 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2353 | }; |
| 2354 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2355 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2356 | // If the argument does not end in .lib, automatically add the suffix. |
| 2357 | // If the argument contains a space, enclose it in quotes. |
| 2358 | // This matches the behavior of MSVC. |
| 2359 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2360 | std::string ArgStr = Quote ? "\"" : ""; |
| 2361 | ArgStr += Lib; |
Martin Storsjo | 3cd67c9 | 2018-10-10 09:01:00 +0000 | [diff] [blame] | 2362 | if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2363 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2364 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2365 | return ArgStr; |
| 2366 | } |
| 2367 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2368 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2369 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2370 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2371 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2372 | unsigned NumRegisterParameters) |
| 2373 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2374 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2375 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2376 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2377 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2378 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2379 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2380 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2381 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2382 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2383 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2384 | |
| 2385 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2386 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2387 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2388 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2389 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2390 | }; |
| 2391 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2392 | static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2393 | CodeGen::CodeGenModule &CGM) { |
| 2394 | if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2395 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2396 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2397 | Fn->addFnAttr("stack-probe-size", |
| 2398 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2399 | if (CGM.getCodeGenOpts().NoStackArgProbe) |
| 2400 | Fn->addFnAttr("no-stack-arg-probe"); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2404 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2405 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2406 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2407 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2408 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2409 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2412 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2413 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2414 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2415 | X86AVXABILevel AVXLevel) |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2416 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT, AVXLevel)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2417 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2418 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2419 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2420 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2421 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2422 | return 7; |
| 2423 | } |
| 2424 | |
| 2425 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2426 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2427 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2429 | // 0-15 are the 16 integer registers. |
| 2430 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2431 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2432 | return false; |
| 2433 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2434 | |
| 2435 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2436 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2437 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2438 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2439 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2440 | |
| 2441 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2442 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2443 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2444 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2445 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2446 | }; |
| 2447 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2448 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2449 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2450 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2451 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2452 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2453 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2454 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2455 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2456 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2457 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2458 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2459 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2460 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2461 | } |
| 2462 | } |
| 2463 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2464 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2465 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2466 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2467 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2468 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2469 | Class &Hi) const { |
| 2470 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2471 | // |
| 2472 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2473 | // memory. |
| 2474 | // |
| 2475 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2476 | // memory. |
| 2477 | // |
| 2478 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2479 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2480 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2481 | // ABI working for processors that don't support the __m256 type. |
| 2482 | // |
| 2483 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2484 | // |
| 2485 | // Some of these are enforced by the merging logic. Others can arise |
| 2486 | // only with unions; for example: |
| 2487 | // union { _Complex double; unsigned; } |
| 2488 | // |
| 2489 | // Note that clauses (b) and (c) were added in 0.98. |
| 2490 | // |
| 2491 | if (Hi == Memory) |
| 2492 | Lo = Memory; |
| 2493 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2494 | Lo = Memory; |
| 2495 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2496 | Lo = Memory; |
| 2497 | if (Hi == SSEUp && Lo != SSE) |
| 2498 | Hi = SSE; |
| 2499 | } |
| 2500 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2501 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2502 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2503 | // classified recursively so that always two fields are |
| 2504 | // considered. The resulting class is calculated according to |
| 2505 | // the classes of the fields in the eightbyte: |
| 2506 | // |
| 2507 | // (a) If both classes are equal, this is the resulting class. |
| 2508 | // |
| 2509 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2510 | // the other class. |
| 2511 | // |
| 2512 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2513 | // class. |
| 2514 | // |
| 2515 | // (d) If one of the classes is INTEGER, the result is the |
| 2516 | // INTEGER. |
| 2517 | // |
| 2518 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2519 | // MEMORY is used as class. |
| 2520 | // |
| 2521 | // (f) Otherwise class SSE is used. |
| 2522 | |
| 2523 | // Accum should never be memory (we should have returned) or |
| 2524 | // ComplexX87 (because this cannot be passed in a structure). |
| 2525 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2526 | "Invalid accumulated classification during merge."); |
| 2527 | if (Accum == Field || Field == NoClass) |
| 2528 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2529 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2530 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2531 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2532 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2533 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2534 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2535 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2536 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2537 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2538 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2541 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2542 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2543 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2544 | // Class pairs with appropriate constructor methods for the various |
| 2545 | // situations. |
| 2546 | |
| 2547 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2548 | // shouldn't be passed in registers for example, so there is no chance they |
| 2549 | // can straddle an eightbyte. Verify & simplify. |
| 2550 | |
| 2551 | Lo = Hi = NoClass; |
| 2552 | |
| 2553 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2554 | Current = Memory; |
| 2555 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2556 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2557 | BuiltinType::Kind k = BT->getKind(); |
| 2558 | |
| 2559 | if (k == BuiltinType::Void) { |
| 2560 | Current = NoClass; |
| 2561 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2562 | Lo = Integer; |
| 2563 | Hi = Integer; |
| 2564 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2565 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2566 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2567 | Current = SSE; |
| 2568 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2569 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2570 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2571 | Lo = SSE; |
| 2572 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2573 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2574 | Lo = X87; |
| 2575 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2576 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2577 | Current = SSE; |
| 2578 | } else |
| 2579 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2580 | } |
| 2581 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2582 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2583 | return; |
| 2584 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2585 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2586 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2587 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2588 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2589 | return; |
| 2590 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2591 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2592 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2593 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2594 | return; |
| 2595 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2596 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2597 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2598 | if (Ty->isMemberFunctionPointerType()) { |
| 2599 | if (Has64BitPointers) { |
| 2600 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2601 | // Lo and Hi now. |
| 2602 | Lo = Hi = Integer; |
| 2603 | } else { |
| 2604 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2605 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2606 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2607 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2608 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2609 | Lo = Hi = Integer; |
| 2610 | } else { |
| 2611 | Current = Integer; |
| 2612 | } |
| 2613 | } |
| 2614 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2615 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2616 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2617 | return; |
| 2618 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2620 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2621 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2622 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2623 | // gcc passes the following as integer: |
| 2624 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2625 | // 2 bytes - <2 x char>, <1 x short> |
| 2626 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2627 | Current = Integer; |
| 2628 | |
| 2629 | // If this type crosses an eightbyte boundary, it should be |
| 2630 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2631 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2632 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2633 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2634 | Hi = Lo; |
| 2635 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2636 | QualType ElementType = VT->getElementType(); |
| 2637 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2638 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2639 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2640 | return; |
| 2641 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2642 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2643 | // pass them as integer. For platforms where clang is the de facto |
| 2644 | // platform compiler, we must continue to use integer. |
| 2645 | if (!classifyIntegerMMXAsSSE() && |
| 2646 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2647 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2648 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2649 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2650 | Current = Integer; |
| 2651 | else |
| 2652 | Current = SSE; |
| 2653 | |
| 2654 | // If this type crosses an eightbyte boundary, it should be |
| 2655 | // split. |
| 2656 | if (OffsetBase && OffsetBase != 64) |
| 2657 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2658 | } else if (Size == 128 || |
| 2659 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2660 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2661 | // least significant one belongs to class SSE and all the others to class |
| 2662 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2663 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2664 | // This design isn't correct for 256-bits, but since there're no cases |
| 2665 | // where the upper parts would need to be inspected, avoid adding |
| 2666 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2667 | // |
| 2668 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2669 | // registers if they are "named", i.e. not part of the "..." of a |
| 2670 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2671 | // |
| 2672 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2673 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2674 | Lo = SSE; |
| 2675 | Hi = SSEUp; |
| 2676 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2677 | return; |
| 2678 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2680 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2681 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2682 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2683 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2684 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2685 | if (Size <= 64) |
| 2686 | Current = Integer; |
| 2687 | else if (Size <= 128) |
| 2688 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2689 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2690 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2691 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2692 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2693 | } else if (ET == getContext().LongDoubleTy) { |
| 2694 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2695 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2696 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2697 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2698 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2699 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2700 | Lo = Hi = SSE; |
| 2701 | else |
| 2702 | llvm_unreachable("unexpected long double representation!"); |
| 2703 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2704 | |
| 2705 | // If this complex type crosses an eightbyte boundary then it |
| 2706 | // should be split. |
| 2707 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2708 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2709 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2710 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2712 | return; |
| 2713 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2715 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2716 | // Arrays are treated like structures. |
| 2717 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2718 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2719 | |
| 2720 | // 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] | 2721 | // than eight eightbytes, ..., it has class MEMORY. |
| 2722 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2723 | return; |
| 2724 | |
| 2725 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2726 | // fields, it has class MEMORY. |
| 2727 | // |
| 2728 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2729 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2730 | return; |
| 2731 | |
| 2732 | // Otherwise implement simplified merge. We could be smarter about |
| 2733 | // this, but it isn't worth it and would be harder to verify. |
| 2734 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2735 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2736 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2737 | |
| 2738 | // The only case a 256-bit wide vector could be used is when the array |
| 2739 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2740 | // 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] | 2741 | // |
| 2742 | if (Size > 128 && |
| 2743 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2744 | return; |
| 2745 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2746 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2747 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2748 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2749 | Lo = merge(Lo, FieldLo); |
| 2750 | Hi = merge(Hi, FieldHi); |
| 2751 | if (Lo == Memory || Hi == Memory) |
| 2752 | break; |
| 2753 | } |
| 2754 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2755 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2756 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2757 | return; |
| 2758 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2760 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2761 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2762 | |
| 2763 | // 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] | 2764 | // than eight eightbytes, ..., it has class MEMORY. |
| 2765 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2766 | return; |
| 2767 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2768 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2769 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2770 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2771 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2772 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2773 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2774 | const RecordDecl *RD = RT->getDecl(); |
| 2775 | |
| 2776 | // Assume variable sized types are passed in memory. |
| 2777 | if (RD->hasFlexibleArrayMember()) |
| 2778 | return; |
| 2779 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2780 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2781 | |
| 2782 | // Reset Lo class, this will be recomputed. |
| 2783 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2784 | |
| 2785 | // If this is a C++ record, classify the bases first. |
| 2786 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2787 | for (const auto &I : CXXRD->bases()) { |
| 2788 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2789 | "Unexpected base class!"); |
| 2790 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2791 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2792 | |
| 2793 | // Classify this field. |
| 2794 | // |
| 2795 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2796 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2797 | // initialized to class NO_CLASS. |
| 2798 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2799 | uint64_t Offset = |
| 2800 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2801 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2802 | Lo = merge(Lo, FieldLo); |
| 2803 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2804 | if (Lo == Memory || Hi == Memory) { |
| 2805 | postMerge(Size, Lo, Hi); |
| 2806 | return; |
| 2807 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2812 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2813 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2814 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2815 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2816 | bool BitField = i->isBitField(); |
| 2817 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2818 | // Ignore padding bit-fields. |
| 2819 | if (BitField && i->isUnnamedBitfield()) |
| 2820 | continue; |
| 2821 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2822 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2823 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2824 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2825 | // The only case a 256-bit wide vector could be used is when the struct |
| 2826 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2827 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2828 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2829 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2830 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2831 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2832 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2833 | return; |
| 2834 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2835 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2836 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2837 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2838 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2839 | return; |
| 2840 | } |
| 2841 | |
| 2842 | // Classify this field. |
| 2843 | // |
| 2844 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2845 | // exceeds a single eightbyte, each is classified |
| 2846 | // separately. Each eightbyte gets initialized to class |
| 2847 | // NO_CLASS. |
| 2848 | Class FieldLo, FieldHi; |
| 2849 | |
| 2850 | // Bit-fields require special handling, they do not force the |
| 2851 | // structure to be passed in memory even if unaligned, and |
| 2852 | // therefore they can straddle an eightbyte. |
| 2853 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2854 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2855 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2856 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2857 | |
| 2858 | uint64_t EB_Lo = Offset / 64; |
| 2859 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2860 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2861 | if (EB_Lo) { |
| 2862 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2863 | FieldLo = NoClass; |
| 2864 | FieldHi = Integer; |
| 2865 | } else { |
| 2866 | FieldLo = Integer; |
| 2867 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2868 | } |
| 2869 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2870 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2871 | Lo = merge(Lo, FieldLo); |
| 2872 | Hi = merge(Hi, FieldHi); |
| 2873 | if (Lo == Memory || Hi == Memory) |
| 2874 | break; |
| 2875 | } |
| 2876 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2877 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2878 | } |
| 2879 | } |
| 2880 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2881 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2882 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2883 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2884 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2885 | // Treat an enum type as its underlying type. |
| 2886 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2887 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2888 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2889 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2890 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2893 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2896 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2897 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2898 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2899 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2900 | if (Size <= 64 || Size > LargestVector) |
| 2901 | return true; |
| 2902 | } |
| 2903 | |
| 2904 | return false; |
| 2905 | } |
| 2906 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2907 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2908 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2909 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2910 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2911 | // |
| 2912 | // This assumption is optimistic, as there could be free registers available |
| 2913 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2914 | // the argument in the free register. This does not seem to happen currently, |
| 2915 | // but this code would be much safer if we could mark the argument with |
| 2916 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2917 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2918 | // Treat an enum type as its underlying type. |
| 2919 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2920 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2921 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2922 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2923 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2924 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2925 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2926 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2927 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2928 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2929 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2930 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2931 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2932 | |
| 2933 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2934 | // is important for good codegen. |
| 2935 | // |
| 2936 | // We do this by coercing the value into a scalar type which the backend can |
| 2937 | // handle naturally (i.e., without using byval). |
| 2938 | // |
| 2939 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2940 | // free integer registers. Doing this when there are free integer registers |
| 2941 | // would require more care, as we would have to ensure that the coerced value |
| 2942 | // did not claim the unused register. That would require either reording the |
| 2943 | // arguments to the function (so that any subsequent inreg values came first), |
| 2944 | // or only doing this optimization when there were no following arguments that |
| 2945 | // might be inreg. |
| 2946 | // |
| 2947 | // We currently expect it to be rare (particularly in well written code) for |
| 2948 | // arguments to be passed on the stack when there are still free integer |
| 2949 | // registers available (this would typically imply large structs being passed |
| 2950 | // by value), so this seems like a fair tradeoff for now. |
| 2951 | // |
| 2952 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2953 | // attributes. See PR12193. |
| 2954 | if (freeIntRegs == 0) { |
| 2955 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2956 | |
| 2957 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2958 | // type, which will end up on the stack (with alignment 8). |
| 2959 | if (Align == 8 && Size <= 64) |
| 2960 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2961 | Size)); |
| 2962 | } |
| 2963 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2964 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2967 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2968 | /// 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] | 2969 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2970 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2971 | // vectors; strip them off if present. |
| 2972 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2973 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2974 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2975 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2976 | if (isa<llvm::VectorType>(IRType) || |
| 2977 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2978 | return IRType; |
| 2979 | |
| 2980 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2981 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2982 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2983 | |
| 2984 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2985 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2986 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2989 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2990 | /// is known to either be off the end of the specified type or being in |
| 2991 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2992 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2993 | /// classification that put one of the two halves in the INTEGER class. |
| 2994 | /// |
| 2995 | /// It is conservatively correct to return false. |
| 2996 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2997 | unsigned EndBit, ASTContext &Context) { |
| 2998 | // If the bytes being queried are off the end of the type, there is no user |
| 2999 | // data hiding here. This handles analysis of builtins, vectors and other |
| 3000 | // types that don't contain interesting padding. |
| 3001 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 3002 | if (TySize <= StartBit) |
| 3003 | return true; |
| 3004 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3005 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3006 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 3007 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 3008 | |
| 3009 | // Check each element to see if the element overlaps with the queried range. |
| 3010 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3011 | // If the element is after the span we care about, then we're done.. |
| 3012 | unsigned EltOffset = i*EltSize; |
| 3013 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3014 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3015 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 3016 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3017 | EndBit-EltOffset, Context)) |
| 3018 | return false; |
| 3019 | } |
| 3020 | // If it overlaps no elements, then it is safe to process as padding. |
| 3021 | return true; |
| 3022 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3024 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3025 | const RecordDecl *RD = RT->getDecl(); |
| 3026 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3027 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3028 | // If this is a C++ record, check the bases first. |
| 3029 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3030 | for (const auto &I : CXXRD->bases()) { |
| 3031 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3032 | "Unexpected base class!"); |
| 3033 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3034 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3035 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3036 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3037 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3038 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3039 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3040 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3041 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3042 | EndBit-BaseOffset, Context)) |
| 3043 | return false; |
| 3044 | } |
| 3045 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3046 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3047 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3048 | // this could be sped up a lot by being smarter about queried fields, |
| 3049 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3050 | // much. |
| 3051 | unsigned idx = 0; |
| 3052 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3053 | i != e; ++i, ++idx) { |
| 3054 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3055 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3056 | // If we found a field after the region we care about, then we're done. |
| 3057 | if (FieldOffset >= EndBit) break; |
| 3058 | |
| 3059 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3060 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3061 | Context)) |
| 3062 | return false; |
| 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 nothing in this record overlapped the area of interest, then we're |
| 3066 | // clean. |
| 3067 | return true; |
| 3068 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3069 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3070 | return false; |
| 3071 | } |
| 3072 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3073 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3074 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3075 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3076 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3077 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3078 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3079 | // Base case if we find a float. |
| 3080 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3081 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3082 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3083 | // 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] | 3084 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3085 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3086 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3087 | IROffset -= SL->getElementOffset(Elt); |
| 3088 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3089 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3090 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3091 | // 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] | 3092 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3093 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3094 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3095 | IROffset -= IROffset/EltSize*EltSize; |
| 3096 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3097 | } |
| 3098 | |
| 3099 | return false; |
| 3100 | } |
| 3101 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3102 | |
| 3103 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3104 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3105 | llvm::Type *X86_64ABIInfo:: |
| 3106 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3107 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3108 | // 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] | 3109 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3110 | // structs that contain 3 floats. |
| 3111 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3112 | SourceOffset*8+64, getContext())) |
| 3113 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3114 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3115 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3116 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3117 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3118 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3119 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3120 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3121 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3122 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3123 | } |
| 3124 | |
| 3125 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3126 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3127 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3128 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3129 | /// 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] | 3130 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3131 | /// etc). |
| 3132 | /// |
| 3133 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3134 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3135 | /// the 8-byte value references. PrefType may be null. |
| 3136 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3137 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3138 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3139 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3140 | llvm::Type *X86_64ABIInfo:: |
| 3141 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3142 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3143 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3144 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3145 | if (IROffset == 0) { |
| 3146 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3147 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3148 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3149 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3150 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3151 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3152 | // goodness in the source type is just tail padding. This is allowed to |
| 3153 | // kick in for struct {double,int} on the int, but not on |
| 3154 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3155 | // have to do this analysis on the source type because we can't depend on |
| 3156 | // unions being lowered a specific way etc. |
| 3157 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3158 | IRType->isIntegerTy(32) || |
| 3159 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3160 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3161 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3162 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3163 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3164 | SourceOffset*8+64, getContext())) |
| 3165 | return IRType; |
| 3166 | } |
| 3167 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3168 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3169 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3170 | // 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] | 3171 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3172 | if (IROffset < SL->getSizeInBytes()) { |
| 3173 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3174 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3175 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3176 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3177 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3178 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3179 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3180 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3181 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3182 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3183 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3184 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3185 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3186 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3187 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3188 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3189 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3190 | // 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] | 3191 | unsigned TySizeInBytes = |
| 3192 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3193 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3194 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3195 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3196 | // It is always safe to classify this as an integer type up to i64 that |
| 3197 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3198 | return llvm::IntegerType::get(getVMContext(), |
| 3199 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3202 | |
| 3203 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3204 | /// be used as elements of a two register pair to pass or return, return a |
| 3205 | /// first class aggregate to represent them. For example, if the low part of |
| 3206 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3207 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3208 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3209 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3210 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3211 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3212 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3213 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3214 | // the second element at offset 8. Check for this: |
| 3215 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3216 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3217 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3218 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3219 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3220 | // To handle this, we have to increase the size of the low part so that the |
| 3221 | // second element will start at an 8 byte offset. We can't increase the size |
| 3222 | // of the second element because it might make us access off the end of the |
| 3223 | // struct. |
| 3224 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3225 | // There are usually two sorts of types the ABI generation code can produce |
| 3226 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3227 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3228 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3229 | // Promote these to a larger type. |
| 3230 | if (Lo->isFloatTy()) |
| 3231 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3232 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3233 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3234 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3235 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3236 | } |
| 3237 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3238 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3239 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3240 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3241 | // Verify that the second element is at an 8-byte offset. |
| 3242 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3243 | "Invalid x86-64 argument pair!"); |
| 3244 | return Result; |
| 3245 | } |
| 3246 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3247 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3248 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3249 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3250 | // classification algorithm. |
| 3251 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3252 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3253 | |
| 3254 | // Check some invariants. |
| 3255 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3256 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3257 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3258 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3259 | switch (Lo) { |
| 3260 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3261 | if (Hi == NoClass) |
| 3262 | return ABIArgInfo::getIgnore(); |
| 3263 | // If the low part is just padding, it takes no register, leave ResType |
| 3264 | // null. |
| 3265 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3266 | "Unknown missing lo part"); |
| 3267 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3268 | |
| 3269 | case SSEUp: |
| 3270 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3271 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3272 | |
| 3273 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3274 | // hidden argument. |
| 3275 | case Memory: |
| 3276 | return getIndirectReturnResult(RetTy); |
| 3277 | |
| 3278 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3279 | // available register of the sequence %rax, %rdx is used. |
| 3280 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3281 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3282 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3283 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3284 | // so that the parameter gets the right LLVM IR attributes. |
| 3285 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3286 | // Treat an enum type as its underlying type. |
| 3287 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3288 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3289 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3290 | if (RetTy->isIntegralOrEnumerationType() && |
| 3291 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3292 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3293 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3294 | break; |
| 3295 | |
| 3296 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3297 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3298 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3299 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3300 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3301 | |
| 3302 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3303 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3304 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3305 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3306 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3307 | |
| 3308 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3309 | // part of the value is returned in %st0 and the imaginary part in |
| 3310 | // %st1. |
| 3311 | case ComplexX87: |
| 3312 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3313 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3314 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3315 | break; |
| 3316 | } |
| 3317 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3318 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3319 | switch (Hi) { |
| 3320 | // Memory was handled previously and X87 should |
| 3321 | // never occur as a hi class. |
| 3322 | case Memory: |
| 3323 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3324 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3325 | |
| 3326 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3327 | case NoClass: |
| 3328 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3329 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3330 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3331 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3332 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3333 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3334 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3335 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3336 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3337 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3338 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3339 | break; |
| 3340 | |
| 3341 | // 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] | 3342 | // is passed in the next available eightbyte chunk if the last used |
| 3343 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3344 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3345 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3346 | case SSEUp: |
| 3347 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3348 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3349 | break; |
| 3350 | |
| 3351 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3352 | // returned together with the previous X87 value in %st0. |
| 3353 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3354 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3355 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3356 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3357 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3358 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3359 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3360 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3361 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3362 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3363 | break; |
| 3364 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3365 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3366 | // 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] | 3367 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3368 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3369 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3370 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3371 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3372 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3375 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3376 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3377 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3378 | const |
| 3379 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3380 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3381 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3382 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3383 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3384 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3385 | // Check some invariants. |
| 3386 | // FIXME: Enforce these by construction. |
| 3387 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3388 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3389 | |
| 3390 | neededInt = 0; |
| 3391 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3392 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3393 | switch (Lo) { |
| 3394 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3395 | if (Hi == NoClass) |
| 3396 | return ABIArgInfo::getIgnore(); |
| 3397 | // If the low part is just padding, it takes no register, leave ResType |
| 3398 | // null. |
| 3399 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3400 | "Unknown missing lo part"); |
| 3401 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3402 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3403 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3404 | // on the stack. |
| 3405 | case Memory: |
| 3406 | |
| 3407 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3408 | // COMPLEX_X87, it is passed in memory. |
| 3409 | case X87: |
| 3410 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3411 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3412 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3413 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3414 | |
| 3415 | case SSEUp: |
| 3416 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3417 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3418 | |
| 3419 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3420 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3421 | // and %r9 is used. |
| 3422 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3423 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3424 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3425 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3426 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3427 | |
| 3428 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3429 | // so that the parameter gets the right LLVM IR attributes. |
| 3430 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3431 | // Treat an enum type as its underlying type. |
| 3432 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3433 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3434 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3435 | if (Ty->isIntegralOrEnumerationType() && |
| 3436 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3437 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3438 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3439 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3440 | break; |
| 3441 | |
| 3442 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3443 | // available SSE register is used, the registers are taken in the |
| 3444 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3445 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3446 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3447 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3448 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3449 | break; |
| 3450 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3451 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3452 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3453 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3454 | switch (Hi) { |
| 3455 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3456 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3457 | // which is passed in memory. |
| 3458 | case Memory: |
| 3459 | case X87: |
| 3460 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3461 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3462 | |
| 3463 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3464 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3465 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3466 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3467 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3468 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3469 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3470 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3471 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3472 | break; |
| 3473 | |
| 3474 | // X87Up generally doesn't occur here (long double is passed in |
| 3475 | // memory), except in situations involving unions. |
| 3476 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3477 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3478 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3479 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3480 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3481 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3482 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3483 | ++neededSSE; |
| 3484 | break; |
| 3485 | |
| 3486 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3487 | // 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] | 3488 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3489 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3490 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3491 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3492 | break; |
| 3493 | } |
| 3494 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3495 | // If a high part was specified, merge it together with the low part. It is |
| 3496 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3497 | // first class struct aggregate with the high and low part: {low, high} |
| 3498 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3499 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3500 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3501 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3504 | ABIArgInfo |
| 3505 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3506 | unsigned &NeededSSE) const { |
| 3507 | auto RT = Ty->getAs<RecordType>(); |
| 3508 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3509 | |
| 3510 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3511 | return getIndirectReturnResult(Ty); |
| 3512 | |
| 3513 | // Sum up bases |
| 3514 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3515 | if (CXXRD->isDynamicClass()) { |
| 3516 | NeededInt = NeededSSE = 0; |
| 3517 | return getIndirectReturnResult(Ty); |
| 3518 | } |
| 3519 | |
| 3520 | for (const auto &I : CXXRD->bases()) |
| 3521 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3522 | .isIndirect()) { |
| 3523 | NeededInt = NeededSSE = 0; |
| 3524 | return getIndirectReturnResult(Ty); |
| 3525 | } |
| 3526 | } |
| 3527 | |
| 3528 | // Sum up members |
| 3529 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3530 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3531 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3532 | .isIndirect()) { |
| 3533 | NeededInt = NeededSSE = 0; |
| 3534 | return getIndirectReturnResult(Ty); |
| 3535 | } |
| 3536 | } else { |
| 3537 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3538 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3539 | LocalNeededSSE, true) |
| 3540 | .isIndirect()) { |
| 3541 | NeededInt = NeededSSE = 0; |
| 3542 | return getIndirectReturnResult(Ty); |
| 3543 | } |
| 3544 | NeededInt += LocalNeededInt; |
| 3545 | NeededSSE += LocalNeededSSE; |
| 3546 | } |
| 3547 | } |
| 3548 | |
| 3549 | return ABIArgInfo::getDirect(); |
| 3550 | } |
| 3551 | |
| 3552 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3553 | unsigned &NeededInt, |
| 3554 | unsigned &NeededSSE) const { |
| 3555 | |
| 3556 | NeededInt = 0; |
| 3557 | NeededSSE = 0; |
| 3558 | |
| 3559 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3560 | } |
| 3561 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3562 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3563 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3564 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3565 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3566 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3567 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3568 | if (CallingConv == llvm::CallingConv::Win64) { |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 3569 | WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel); |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3570 | Win64ABIInfo.computeInfo(FI); |
| 3571 | return; |
| 3572 | } |
| 3573 | |
| 3574 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3575 | |
| 3576 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3577 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3578 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3579 | unsigned NeededInt, NeededSSE; |
| 3580 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 3581 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3582 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3583 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3584 | FI.getReturnInfo() = |
| 3585 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3586 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3587 | FreeIntRegs -= NeededInt; |
| 3588 | FreeSSERegs -= NeededSSE; |
| 3589 | } else { |
| 3590 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3591 | } |
| 3592 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3593 | // Complex Long Double Type is passed in Memory when Regcall |
| 3594 | // calling convention is used. |
| 3595 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3596 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3597 | getContext().LongDoubleTy) |
| 3598 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3599 | } else |
| 3600 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3601 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3602 | |
| 3603 | // If the return value is indirect, then the hidden argument is consuming one |
| 3604 | // integer register. |
| 3605 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3606 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3607 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3608 | // The chain argument effectively gives us another free register. |
| 3609 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3610 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3611 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3612 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3613 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3614 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3615 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3616 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3617 | it != ie; ++it, ++ArgNo) { |
| 3618 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3619 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3620 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3621 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3622 | else |
| 3623 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3624 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3625 | |
| 3626 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3627 | // eightbyte of an argument, the whole argument is passed on the |
| 3628 | // stack. If registers have already been assigned for some |
| 3629 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3630 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3631 | FreeIntRegs -= NeededInt; |
| 3632 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3633 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3634 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3635 | } |
| 3636 | } |
| 3637 | } |
| 3638 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3639 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3640 | Address VAListAddr, QualType Ty) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3641 | Address overflow_arg_area_p = |
| 3642 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3643 | llvm::Value *overflow_arg_area = |
| 3644 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3645 | |
| 3646 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3647 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3648 | // It isn't stated explicitly in the standard, but in practice we use |
| 3649 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3650 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3651 | if (Align > CharUnits::fromQuantity(8)) { |
| 3652 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3653 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | // 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] | 3657 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3658 | llvm::Value *Res = |
| 3659 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3660 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3661 | |
| 3662 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3663 | // l->overflow_arg_area + sizeof(type). |
| 3664 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3665 | // an 8 byte boundary. |
| 3666 | |
| 3667 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3668 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3669 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3670 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3671 | "overflow_arg_area.next"); |
| 3672 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3673 | |
| 3674 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3675 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3678 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3679 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3680 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3681 | // struct { |
| 3682 | // i32 gp_offset; |
| 3683 | // i32 fp_offset; |
| 3684 | // i8* overflow_arg_area; |
| 3685 | // i8* reg_save_area; |
| 3686 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3687 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3688 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3689 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3690 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3691 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3692 | |
| 3693 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3694 | // in the registers. If not go to step 7. |
| 3695 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3696 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3697 | |
| 3698 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3699 | // general purpose registers needed to pass type and num_fp to hold |
| 3700 | // the number of floating point registers needed. |
| 3701 | |
| 3702 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3703 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3704 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3705 | // |
| 3706 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3707 | // register save space). |
| 3708 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3709 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3710 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3711 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3712 | if (neededInt) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3713 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3714 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3715 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3716 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | if (neededSSE) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3720 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3721 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3722 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3723 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3724 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3725 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3726 | } |
| 3727 | |
| 3728 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3729 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3730 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3731 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3732 | |
| 3733 | // Emit code to load the value if it was passed in registers. |
| 3734 | |
| 3735 | CGF.EmitBlock(InRegBlock); |
| 3736 | |
| 3737 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3738 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3739 | // copying to a temporary location in case the parameter is passed |
| 3740 | // in different register classes or requires an alignment greater |
| 3741 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3742 | // |
| 3743 | // FIXME: This really results in shameful code when we end up needing to |
| 3744 | // collect arguments from different places; often what should result in a |
| 3745 | // simple assembling of a structure from scattered addresses has many more |
| 3746 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3747 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3748 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3749 | CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3750 | |
| 3751 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3752 | if (neededInt && neededSSE) { |
| 3753 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3754 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3755 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3756 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3757 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3758 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3759 | llvm::Type *TyLo = ST->getElementType(0); |
| 3760 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3761 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3762 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3763 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3764 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3765 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3766 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3767 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3768 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3769 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3770 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3771 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3772 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3773 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3774 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3775 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3776 | |
| 3777 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3778 | V = CGF.Builder.CreateAlignedLoad( |
| 3779 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3780 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3781 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3782 | |
| 3783 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3784 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3785 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3786 | CharUnits::fromQuantity(8)); |
| 3787 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3788 | |
| 3789 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3790 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3791 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3792 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3793 | CharUnits TyAlign = SizeAlign.second; |
| 3794 | |
| 3795 | // Copy into a temporary if the type is more aligned than the |
| 3796 | // register save area. |
| 3797 | if (TyAlign.getQuantity() > 8) { |
| 3798 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3799 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3800 | RegAddr = Tmp; |
| 3801 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3802 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3803 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3804 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3805 | CharUnits::fromQuantity(16)); |
| 3806 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3807 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3808 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3809 | // SSE registers are spaced 16 bytes apart in the register save |
| 3810 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3811 | // The ABI isn't explicit about this, but it seems reasonable |
| 3812 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3813 | // naturally 16-byte aligned and the prologue is expected to store |
| 3814 | // all the SSE registers to the RSA. |
| 3815 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3816 | CharUnits::fromQuantity(16)); |
| 3817 | Address RegAddrHi = |
| 3818 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3819 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3820 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3821 | ? AI.getCoerceToType() |
| 3822 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3823 | llvm::Value *V; |
| 3824 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3825 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3826 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3827 | RegAddrLo, ST->getStructElementType(0))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3828 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3829 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3830 | RegAddrHi, ST->getStructElementType(1))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3831 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3832 | |
| 3833 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3837 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3838 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3839 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3840 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3841 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3842 | gp_offset_p); |
| 3843 | } |
| 3844 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3845 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3846 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3847 | fp_offset_p); |
| 3848 | } |
| 3849 | CGF.EmitBranch(ContBlock); |
| 3850 | |
| 3851 | // Emit code to load the value if it was passed in memory. |
| 3852 | |
| 3853 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3854 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3855 | |
| 3856 | // Return the appropriate result. |
| 3857 | |
| 3858 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3859 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3860 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3861 | return ResAddr; |
| 3862 | } |
| 3863 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3864 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3865 | QualType Ty) const { |
| 3866 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3867 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3868 | CharUnits::fromQuantity(8), |
| 3869 | /*allowHigherAlign*/ false); |
| 3870 | } |
| 3871 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3872 | ABIArgInfo |
| 3873 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3874 | const ABIArgInfo ¤t) const { |
| 3875 | // Assumes vectorCall calling convention. |
| 3876 | const Type *Base = nullptr; |
| 3877 | uint64_t NumElts = 0; |
| 3878 | |
| 3879 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3880 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3881 | FreeSSERegs -= NumElts; |
| 3882 | return getDirectX86Hva(); |
| 3883 | } |
| 3884 | return current; |
| 3885 | } |
| 3886 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3887 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3888 | bool IsReturnType, bool IsVectorCall, |
| 3889 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3890 | |
| 3891 | if (Ty->isVoidType()) |
| 3892 | return ABIArgInfo::getIgnore(); |
| 3893 | |
| 3894 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3895 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3896 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3897 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3898 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3899 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3900 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3901 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3902 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3903 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3904 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3905 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3909 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3910 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3911 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3912 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3913 | const Type *Base = nullptr; |
| 3914 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3915 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3916 | // other targets. |
| 3917 | if ((IsVectorCall || IsRegCall) && |
| 3918 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3919 | if (IsRegCall) { |
| 3920 | if (FreeSSERegs >= NumElts) { |
| 3921 | FreeSSERegs -= NumElts; |
| 3922 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3923 | return ABIArgInfo::getDirect(); |
| 3924 | return ABIArgInfo::getExpand(); |
| 3925 | } |
| 3926 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3927 | } else if (IsVectorCall) { |
| 3928 | if (FreeSSERegs >= NumElts && |
| 3929 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3930 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3931 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3932 | } else if (IsReturnType) { |
| 3933 | return ABIArgInfo::getExpand(); |
| 3934 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3935 | // HVAs are delayed and reclassified in the 2nd step. |
| 3936 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3937 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3938 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3941 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3942 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3943 | // directly. |
| 3944 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3945 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3946 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3949 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3950 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3951 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3952 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3953 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3954 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3955 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3956 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Reid Kleckner | 08f64e9 | 2018-10-31 17:43:55 +0000 | [diff] [blame] | 3959 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3960 | switch (BT->getKind()) { |
| 3961 | case BuiltinType::Bool: |
| 3962 | // Bool type is always extended to the ABI, other builtin types are not |
| 3963 | // extended. |
| 3964 | return ABIArgInfo::getExtend(Ty); |
| 3965 | |
| 3966 | case BuiltinType::LongDouble: |
| 3967 | // Mingw64 GCC uses the old 80 bit extended precision floating point |
| 3968 | // unit. It passes them indirectly through memory. |
| 3969 | if (IsMingw64) { |
| 3970 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3971 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
| 3972 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3973 | } |
| 3974 | break; |
| 3975 | |
| 3976 | case BuiltinType::Int128: |
| 3977 | case BuiltinType::UInt128: |
| 3978 | // If it's a parameter type, the normal ABI rule is that arguments larger |
| 3979 | // than 8 bytes are passed indirectly. GCC follows it. We follow it too, |
| 3980 | // even though it isn't particularly efficient. |
| 3981 | if (!IsReturnType) |
| 3982 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3983 | |
| 3984 | // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. |
| 3985 | // Clang matches them for compatibility. |
| 3986 | return ABIArgInfo::getDirect( |
| 3987 | llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2)); |
| 3988 | |
| 3989 | default: |
| 3990 | break; |
| 3991 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3994 | return ABIArgInfo::getDirect(); |
| 3995 | } |
| 3996 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3997 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3998 | unsigned FreeSSERegs, |
| 3999 | bool IsVectorCall, |
| 4000 | bool IsRegCall) const { |
| 4001 | unsigned Count = 0; |
| 4002 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4003 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 4004 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4005 | if (Count < VectorcallMaxParamNumAsReg) |
| 4006 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4007 | else { |
| 4008 | // Since these cannot be passed in registers, pretend no registers |
| 4009 | // are left. |
| 4010 | unsigned ZeroSSERegsAvail = 0; |
| 4011 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 4012 | IsVectorCall, IsRegCall); |
| 4013 | } |
| 4014 | ++Count; |
| 4015 | } |
| 4016 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4017 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4018 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4019 | } |
| 4020 | } |
| 4021 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4022 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 4023 | const unsigned CC = FI.getCallingConvention(); |
| 4024 | bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall; |
| 4025 | bool IsRegCall = CC == llvm::CallingConv::X86_RegCall; |
| 4026 | |
| 4027 | // If __attribute__((sysv_abi)) is in use, use the SysV argument |
| 4028 | // classification rules. |
| 4029 | if (CC == llvm::CallingConv::X86_64_SysV) { |
| 4030 | X86_64ABIInfo SysVABIInfo(CGT, AVXLevel); |
| 4031 | SysVABIInfo.computeInfo(FI); |
| 4032 | return; |
| 4033 | } |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 4034 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4035 | unsigned FreeSSERegs = 0; |
| 4036 | if (IsVectorCall) { |
| 4037 | // We can use up to 4 SSE return registers with vectorcall. |
| 4038 | FreeSSERegs = 4; |
| 4039 | } else if (IsRegCall) { |
| 4040 | // RegCall gives us 16 SSE registers. |
| 4041 | FreeSSERegs = 16; |
| 4042 | } |
| 4043 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4044 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4045 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 4046 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4047 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4048 | if (IsVectorCall) { |
| 4049 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4050 | FreeSSERegs = 6; |
| 4051 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4052 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4053 | FreeSSERegs = 16; |
| 4054 | } |
| 4055 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4056 | if (IsVectorCall) { |
| 4057 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4058 | } else { |
| 4059 | for (auto &I : FI.arguments()) |
| 4060 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4061 | } |
| 4062 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4065 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4066 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4067 | |
| 4068 | bool IsIndirect = false; |
| 4069 | |
| 4070 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4071 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4072 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4073 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4074 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4075 | } |
| 4076 | |
| 4077 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4078 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4079 | CharUnits::fromQuantity(8), |
| 4080 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4081 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4082 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4083 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4084 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4085 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4086 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4087 | bool IsSoftFloatABI; |
| 4088 | |
| 4089 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4090 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4091 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4092 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4093 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4094 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4095 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4096 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4097 | }; |
| 4098 | |
| 4099 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4100 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4101 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4102 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4103 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4104 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4105 | // This is recovered from gcc output. |
| 4106 | return 1; // r1 is the dedicated stack pointer |
| 4107 | } |
| 4108 | |
| 4109 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4110 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4111 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4112 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4113 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4114 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4115 | // Complex types are passed just like their elements |
| 4116 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4117 | Ty = CTy->getElementType(); |
| 4118 | |
| 4119 | if (Ty->isVectorType()) |
| 4120 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4121 | : 4); |
| 4122 | |
| 4123 | // For single-element float/vector structs, we consider the whole type |
| 4124 | // to have the same alignment requirements as its single element. |
| 4125 | const Type *AlignTy = nullptr; |
| 4126 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4127 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4128 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4129 | (BT && BT->isFloatingPoint())) |
| 4130 | AlignTy = EltType; |
| 4131 | } |
| 4132 | |
| 4133 | if (AlignTy) |
| 4134 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4135 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4136 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4137 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4138 | // TODO: this implementation is now likely redundant with |
| 4139 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4140 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4141 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4142 | if (getTarget().getTriple().isOSDarwin()) { |
| 4143 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4144 | TI.second = getParamTypeAlignment(Ty); |
| 4145 | |
| 4146 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4147 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4148 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4149 | /*AllowHigherAlign=*/true); |
| 4150 | } |
| 4151 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4152 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4153 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4154 | // TODO: Implement this. For now ignore. |
| 4155 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4156 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4159 | // struct __va_list_tag { |
| 4160 | // unsigned char gpr; |
| 4161 | // unsigned char fpr; |
| 4162 | // unsigned short reserved; |
| 4163 | // void *overflow_arg_area; |
| 4164 | // void *reg_save_area; |
| 4165 | // }; |
| 4166 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4167 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4168 | bool isInt = |
| 4169 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4170 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4171 | |
| 4172 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4173 | // with the argument-lowering code. |
| 4174 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4175 | |
| 4176 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4177 | |
| 4178 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4179 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4180 | if (isInt || IsSoftFloatABI) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4181 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4182 | } else { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4183 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4184 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4185 | |
| 4186 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4187 | |
| 4188 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4189 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4190 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4191 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4192 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4193 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4194 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4195 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4196 | |
| 4197 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4198 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4199 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4200 | |
| 4201 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4202 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4203 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4204 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4205 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4206 | // Case 1: consume registers. |
| 4207 | Address RegAddr = Address::invalid(); |
| 4208 | { |
| 4209 | CGF.EmitBlock(UsingRegs); |
| 4210 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4211 | Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4212 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4213 | CharUnits::fromQuantity(8)); |
| 4214 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4215 | |
| 4216 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4217 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4218 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4219 | CharUnits::fromQuantity(32)); |
| 4220 | } |
| 4221 | |
| 4222 | // Get the address of the saved value by scaling the number of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4223 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4224 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4225 | llvm::Value *RegOffset = |
| 4226 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4227 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4228 | RegAddr.getPointer(), RegOffset), |
| 4229 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4230 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4231 | |
| 4232 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4233 | NumRegs = |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4234 | Builder.CreateAdd(NumRegs, |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4235 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4236 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4237 | |
| 4238 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4239 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4240 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4241 | // Case 2: consume space in the overflow area. |
| 4242 | Address MemAddr = Address::invalid(); |
| 4243 | { |
| 4244 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4245 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4246 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4247 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4248 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4249 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4250 | |
| 4251 | CharUnits Size; |
| 4252 | if (!isIndirect) { |
| 4253 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4254 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4255 | } else { |
| 4256 | Size = CGF.getPointerSize(); |
| 4257 | } |
| 4258 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4259 | Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4260 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4261 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4262 | // Round up address of argument to alignment |
| 4263 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4264 | if (Align > OverflowAreaAlign) { |
| 4265 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4266 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4267 | Align); |
| 4268 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4269 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4270 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4271 | |
| 4272 | // Increase the overflow area. |
| 4273 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4274 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4275 | CGF.EmitBranch(Cont); |
| 4276 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4277 | |
| 4278 | CGF.EmitBlock(Cont); |
| 4279 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4280 | // Merge the cases with a phi. |
| 4281 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4282 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4283 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4284 | // Load the pointer if the argument was passed indirectly. |
| 4285 | if (isIndirect) { |
| 4286 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4287 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | return Result; |
| 4291 | } |
| 4292 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4293 | bool |
| 4294 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4295 | llvm::Value *Address) const { |
| 4296 | // This is calculated from the LLVM and GCC tables and verified |
| 4297 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4298 | |
| 4299 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4300 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4301 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4302 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4303 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4304 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4305 | |
| 4306 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4307 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4308 | |
| 4309 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4310 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4311 | |
| 4312 | // 64-76 are various 4-byte special-purpose registers: |
| 4313 | // 64: mq |
| 4314 | // 65: lr |
| 4315 | // 66: ctr |
| 4316 | // 67: ap |
| 4317 | // 68-75 cr0-7 |
| 4318 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4319 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4320 | |
| 4321 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4322 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4323 | |
| 4324 | // 109: vrsave |
| 4325 | // 110: vscr |
| 4326 | // 111: spe_acc |
| 4327 | // 112: spefscr |
| 4328 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4329 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4330 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4331 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4334 | // PowerPC-64 |
| 4335 | |
| 4336 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4337 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4338 | class PPC64_SVR4_ABIInfo : public SwiftABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4339 | public: |
| 4340 | enum ABIKind { |
| 4341 | ELFv1 = 0, |
| 4342 | ELFv2 |
| 4343 | }; |
| 4344 | |
| 4345 | private: |
| 4346 | static const unsigned GPRBits = 64; |
| 4347 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4348 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4349 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4350 | |
| 4351 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4352 | // will be passed in a QPX register. |
| 4353 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4354 | if (!HasQPX) |
| 4355 | return false; |
| 4356 | |
| 4357 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4358 | unsigned NumElements = VT->getNumElements(); |
| 4359 | if (NumElements == 1) |
| 4360 | return false; |
| 4361 | |
| 4362 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4363 | if (getContext().getTypeSize(Ty) <= 256) |
| 4364 | return true; |
| 4365 | } else if (VT->getElementType()-> |
| 4366 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4367 | if (getContext().getTypeSize(Ty) <= 128) |
| 4368 | return true; |
| 4369 | } |
| 4370 | } |
| 4371 | |
| 4372 | return false; |
| 4373 | } |
| 4374 | |
| 4375 | bool IsQPXVectorTy(QualType Ty) const { |
| 4376 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4377 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4378 | |
| 4379 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4380 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4381 | bool SoftFloatABI) |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4382 | : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4383 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4384 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4385 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4386 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4387 | |
| 4388 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4389 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4390 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4391 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4392 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4393 | uint64_t Members) const override; |
| 4394 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4395 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4396 | // Example: For aggregate arguments that fit in a register, we could |
| 4397 | // use getDirectInReg (as is done below for structs containing a single |
| 4398 | // floating-point value) to avoid pushing them to memory on function |
| 4399 | // entry. This would require changing the logic in PPCISelLowering |
| 4400 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4401 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4402 | if (!getCXXABI().classifyReturnType(FI)) |
| 4403 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4404 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4405 | // We rely on the default argument classification for the most part. |
| 4406 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4407 | // 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] | 4408 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4409 | if (T) { |
| 4410 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4411 | if (IsQPXVectorTy(T) || |
| 4412 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4413 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4414 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4415 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4416 | continue; |
| 4417 | } |
| 4418 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4419 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4420 | } |
| 4421 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4422 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4423 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4424 | QualType Ty) const override; |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4425 | |
| 4426 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 4427 | bool asReturnValue) const override { |
| 4428 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4429 | } |
| 4430 | |
| 4431 | bool isSwiftErrorInRegister() const override { |
| 4432 | return false; |
| 4433 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4434 | }; |
| 4435 | |
| 4436 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4437 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4438 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4439 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4440 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4441 | bool SoftFloatABI) |
| 4442 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4443 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4444 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4445 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4446 | // This is recovered from gcc output. |
| 4447 | return 1; // r1 is the dedicated stack pointer |
| 4448 | } |
| 4449 | |
| 4450 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4451 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4452 | }; |
| 4453 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4454 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4455 | public: |
| 4456 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4457 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4458 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4459 | // This is recovered from gcc output. |
| 4460 | return 1; // r1 is the dedicated stack pointer |
| 4461 | } |
| 4462 | |
| 4463 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4464 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4465 | }; |
| 4466 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4467 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4468 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4469 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4470 | // extended to 64 bits. |
| 4471 | bool |
| 4472 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4473 | // Treat an enum type as its underlying type. |
| 4474 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4475 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4476 | |
| 4477 | // Promotable integer types are required to be promoted by the ABI. |
| 4478 | if (Ty->isPromotableIntegerType()) |
| 4479 | return true; |
| 4480 | |
| 4481 | // In addition to the usual promotable integer types, we also need to |
| 4482 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4483 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4484 | switch (BT->getKind()) { |
| 4485 | case BuiltinType::Int: |
| 4486 | case BuiltinType::UInt: |
| 4487 | return true; |
| 4488 | default: |
| 4489 | break; |
| 4490 | } |
| 4491 | |
| 4492 | return false; |
| 4493 | } |
| 4494 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4495 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4496 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4497 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4498 | // Complex types are passed just like their elements. |
| 4499 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4500 | Ty = CTy->getElementType(); |
| 4501 | |
| 4502 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4503 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4504 | if (IsQPXVectorTy(Ty)) { |
| 4505 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4506 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4507 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4508 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4509 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4510 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4511 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4512 | |
| 4513 | // For single-element float/vector structs, we consider the whole type |
| 4514 | // to have the same alignment requirements as its single element. |
| 4515 | const Type *AlignAsType = nullptr; |
| 4516 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4517 | if (EltType) { |
| 4518 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4519 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4520 | getContext().getTypeSize(EltType) == 128) || |
| 4521 | (BT && BT->isFloatingPoint())) |
| 4522 | AlignAsType = EltType; |
| 4523 | } |
| 4524 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4525 | // Likewise for ELFv2 homogeneous aggregates. |
| 4526 | const Type *Base = nullptr; |
| 4527 | uint64_t Members = 0; |
| 4528 | if (!AlignAsType && Kind == ELFv2 && |
| 4529 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4530 | AlignAsType = Base; |
| 4531 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4532 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4533 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4534 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4535 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4536 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4537 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4538 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4539 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4540 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4541 | |
| 4542 | // Otherwise, we only need alignment for any aggregate type that |
| 4543 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4544 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4545 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4546 | return CharUnits::fromQuantity(32); |
| 4547 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4548 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4549 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4550 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4553 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4554 | /// aggregate. Base is set to the base element type, and Members is set |
| 4555 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4556 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4557 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4558 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4559 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4560 | if (NElements == 0) |
| 4561 | return false; |
| 4562 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4563 | return false; |
| 4564 | Members *= NElements; |
| 4565 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4566 | const RecordDecl *RD = RT->getDecl(); |
| 4567 | if (RD->hasFlexibleArrayMember()) |
| 4568 | return false; |
| 4569 | |
| 4570 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4571 | |
| 4572 | // If this is a C++ record, check the bases first. |
| 4573 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4574 | for (const auto &I : CXXRD->bases()) { |
| 4575 | // Ignore empty records. |
| 4576 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4577 | continue; |
| 4578 | |
| 4579 | uint64_t FldMembers; |
| 4580 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4581 | return false; |
| 4582 | |
| 4583 | Members += FldMembers; |
| 4584 | } |
| 4585 | } |
| 4586 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4587 | for (const auto *FD : RD->fields()) { |
| 4588 | // Ignore (non-zero arrays of) empty records. |
| 4589 | QualType FT = FD->getType(); |
| 4590 | while (const ConstantArrayType *AT = |
| 4591 | getContext().getAsConstantArrayType(FT)) { |
| 4592 | if (AT->getSize().getZExtValue() == 0) |
| 4593 | return false; |
| 4594 | FT = AT->getElementType(); |
| 4595 | } |
| 4596 | if (isEmptyRecord(getContext(), FT, true)) |
| 4597 | continue; |
| 4598 | |
| 4599 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4600 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4601 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4602 | continue; |
| 4603 | |
| 4604 | uint64_t FldMembers; |
| 4605 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4606 | return false; |
| 4607 | |
| 4608 | Members = (RD->isUnion() ? |
| 4609 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4610 | } |
| 4611 | |
| 4612 | if (!Base) |
| 4613 | return false; |
| 4614 | |
| 4615 | // Ensure there is no padding. |
| 4616 | if (getContext().getTypeSize(Base) * Members != |
| 4617 | getContext().getTypeSize(Ty)) |
| 4618 | return false; |
| 4619 | } else { |
| 4620 | Members = 1; |
| 4621 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4622 | Members = 2; |
| 4623 | Ty = CT->getElementType(); |
| 4624 | } |
| 4625 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4626 | // Most ABIs only support float, double, and some vector type widths. |
| 4627 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4628 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4629 | |
| 4630 | // The base type must be the same for all members. Types that |
| 4631 | // agree in both total size and mode (float vs. vector) are |
| 4632 | // treated as being equivalent here. |
| 4633 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4634 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4635 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4636 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4637 | // so make sure to widen it explicitly. |
| 4638 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4639 | QualType EltTy = VT->getElementType(); |
| 4640 | unsigned NumElements = |
| 4641 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4642 | Base = getContext() |
| 4643 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4644 | .getTypePtr(); |
| 4645 | } |
| 4646 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4647 | |
| 4648 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4649 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4650 | return false; |
| 4651 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4652 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4653 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4654 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4655 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4656 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4657 | // double, long double, or 128-bit vectors. |
| 4658 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4659 | if (BT->getKind() == BuiltinType::Float || |
| 4660 | BT->getKind() == BuiltinType::Double || |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4661 | BT->getKind() == BuiltinType::LongDouble || |
| 4662 | (getContext().getTargetInfo().hasFloat128Type() && |
| 4663 | (BT->getKind() == BuiltinType::Float128))) { |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4664 | if (IsSoftFloatABI) |
| 4665 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4666 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4667 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4668 | } |
| 4669 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4670 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4671 | return true; |
| 4672 | } |
| 4673 | return false; |
| 4674 | } |
| 4675 | |
| 4676 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4677 | const Type *Base, uint64_t Members) const { |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4678 | // Vector and fp128 types require one register, other floating point types |
| 4679 | // require one or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4680 | uint32_t NumRegs = |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4681 | ((getContext().getTargetInfo().hasFloat128Type() && |
| 4682 | Base->isFloat128Type()) || |
| 4683 | Base->isVectorType()) ? 1 |
| 4684 | : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4685 | |
| 4686 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4687 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4690 | ABIArgInfo |
| 4691 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4692 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4693 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4694 | if (Ty->isAnyComplexType()) |
| 4695 | return ABIArgInfo::getDirect(); |
| 4696 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4697 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4698 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4699 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4700 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4701 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4702 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4703 | else if (Size < 128) { |
| 4704 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4705 | return ABIArgInfo::getDirect(CoerceTy); |
| 4706 | } |
| 4707 | } |
| 4708 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4709 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4710 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4711 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4712 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4713 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4714 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4715 | |
| 4716 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4717 | const Type *Base = nullptr; |
| 4718 | uint64_t Members = 0; |
| 4719 | if (Kind == ELFv2 && |
| 4720 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4721 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4722 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4723 | return ABIArgInfo::getDirect(CoerceTy); |
| 4724 | } |
| 4725 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4726 | // If an aggregate may end up fully in registers, we do not |
| 4727 | // use the ByVal method, but pass the aggregate as array. |
| 4728 | // This is usually beneficial since we avoid forcing the |
| 4729 | // back-end to store the argument to memory. |
| 4730 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4731 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4732 | llvm::Type *CoerceTy; |
| 4733 | |
| 4734 | // Types up to 8 bytes are passed as integer type (which will be |
| 4735 | // properly aligned in the argument save area doubleword). |
| 4736 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4737 | CoerceTy = |
| 4738 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4739 | // Larger types are passed as arrays, with the base type selected |
| 4740 | // according to the required alignment in the save area. |
| 4741 | else { |
| 4742 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4743 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4744 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4745 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4746 | } |
| 4747 | |
| 4748 | return ABIArgInfo::getDirect(CoerceTy); |
| 4749 | } |
| 4750 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4751 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4752 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4753 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4754 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4757 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4758 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | ABIArgInfo |
| 4762 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4763 | if (RetTy->isVoidType()) |
| 4764 | return ABIArgInfo::getIgnore(); |
| 4765 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4766 | if (RetTy->isAnyComplexType()) |
| 4767 | return ABIArgInfo::getDirect(); |
| 4768 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4769 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4770 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4771 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4772 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4773 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4774 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4775 | else if (Size < 128) { |
| 4776 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4777 | return ABIArgInfo::getDirect(CoerceTy); |
| 4778 | } |
| 4779 | } |
| 4780 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4781 | if (isAggregateTypeForABI(RetTy)) { |
| 4782 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4783 | const Type *Base = nullptr; |
| 4784 | uint64_t Members = 0; |
| 4785 | if (Kind == ELFv2 && |
| 4786 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4787 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4788 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4789 | return ABIArgInfo::getDirect(CoerceTy); |
| 4790 | } |
| 4791 | |
| 4792 | // ELFv2 small aggregates are returned in up to two registers. |
| 4793 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4794 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4795 | if (Bits == 0) |
| 4796 | return ABIArgInfo::getIgnore(); |
| 4797 | |
| 4798 | llvm::Type *CoerceTy; |
| 4799 | if (Bits > GPRBits) { |
| 4800 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4801 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4802 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4803 | CoerceTy = |
| 4804 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4805 | return ABIArgInfo::getDirect(CoerceTy); |
| 4806 | } |
| 4807 | |
| 4808 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4809 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4810 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4811 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4812 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4813 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4816 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4817 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4818 | QualType Ty) const { |
| 4819 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4820 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4821 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4822 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4823 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4824 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4825 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4826 | // in separate doublewords. However, Clang expects us to produce a |
| 4827 | // pointer to a structure with the two parts packed tightly. So generate |
| 4828 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4829 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4830 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4831 | CharUnits EltSize = TypeInfo.first / 2; |
| 4832 | if (EltSize < SlotSize) { |
| 4833 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4834 | SlotSize * 2, SlotSize, |
| 4835 | SlotSize, /*AllowHigher*/ true); |
| 4836 | |
| 4837 | Address RealAddr = Addr; |
| 4838 | Address ImagAddr = RealAddr; |
| 4839 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4840 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4841 | SlotSize - EltSize); |
| 4842 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4843 | 2 * SlotSize - EltSize); |
| 4844 | } else { |
| 4845 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4846 | } |
| 4847 | |
| 4848 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4849 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4850 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4851 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4852 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4853 | |
| 4854 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4855 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4856 | /*init*/ true); |
| 4857 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4858 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4861 | // Otherwise, just use the general rule. |
| 4862 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4863 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | static bool |
| 4867 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4868 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4869 | // This is calculated from the LLVM and GCC tables and verified |
| 4870 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4871 | |
| 4872 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4873 | |
| 4874 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4875 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4876 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4877 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4878 | |
| 4879 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4880 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4881 | |
| 4882 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4883 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4884 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4885 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4886 | // 64: mq |
| 4887 | // 65: lr |
| 4888 | // 66: ctr |
| 4889 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4890 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4891 | |
| 4892 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4893 | // 68-75 cr0-7 |
| 4894 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4895 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4896 | |
| 4897 | // 77-108: v0-31, the 16-byte vector registers |
| 4898 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4899 | |
| 4900 | // 109: vrsave |
| 4901 | // 110: vscr |
| 4902 | // 111: spe_acc |
| 4903 | // 112: spefscr |
| 4904 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4905 | // 114: tfhar |
| 4906 | // 115: tfiar |
| 4907 | // 116: texasr |
| 4908 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4909 | |
| 4910 | return false; |
| 4911 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4912 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4913 | bool |
| 4914 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4915 | CodeGen::CodeGenFunction &CGF, |
| 4916 | llvm::Value *Address) const { |
| 4917 | |
| 4918 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4919 | } |
| 4920 | |
| 4921 | bool |
| 4922 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4923 | llvm::Value *Address) const { |
| 4924 | |
| 4925 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4926 | } |
| 4927 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4928 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4929 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4930 | //===----------------------------------------------------------------------===// |
| 4931 | |
| 4932 | namespace { |
| 4933 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4934 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4935 | public: |
| 4936 | enum ABIKind { |
| 4937 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4938 | DarwinPCS, |
| 4939 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4940 | }; |
| 4941 | |
| 4942 | private: |
| 4943 | ABIKind Kind; |
| 4944 | |
| 4945 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4946 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4947 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4948 | |
| 4949 | private: |
| 4950 | ABIKind getABIKind() const { return Kind; } |
| 4951 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4952 | |
| 4953 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4954 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4955 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4956 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4957 | uint64_t Members) const override; |
| 4958 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4959 | bool isIllegalVectorType(QualType Ty) const; |
| 4960 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4961 | void computeInfo(CGFunctionInfo &FI) const override { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4962 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4963 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4964 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4965 | for (auto &it : FI.arguments()) |
| 4966 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4967 | } |
| 4968 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4969 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4970 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4971 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4972 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4973 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4974 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4975 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4976 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4977 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4978 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4979 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4980 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4981 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4982 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4983 | QualType Ty) const override; |
| 4984 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 4985 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4986 | bool asReturnValue) const override { |
| 4987 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4988 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4989 | bool isSwiftErrorInRegister() const override { |
| 4990 | return true; |
| 4991 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4992 | |
| 4993 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4994 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4995 | }; |
| 4996 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4997 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4998 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4999 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 5000 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5001 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5002 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5003 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5006 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 5007 | return 31; |
| 5008 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5009 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5010 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5011 | |
| 5012 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5013 | CodeGen::CodeGenModule &CGM) const override { |
| 5014 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 5015 | if (!FD) |
| 5016 | return; |
| 5017 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5018 | |
| 5019 | auto Kind = CGM.getCodeGenOpts().getSignReturnAddress(); |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5020 | if (Kind != CodeGenOptions::SignReturnAddressScope::None) { |
| 5021 | Fn->addFnAttr("sign-return-address", |
| 5022 | Kind == CodeGenOptions::SignReturnAddressScope::All |
| 5023 | ? "all" |
| 5024 | : "non-leaf"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5025 | |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5026 | auto Key = CGM.getCodeGenOpts().getSignReturnAddressKey(); |
| 5027 | Fn->addFnAttr("sign-return-address-key", |
| 5028 | Key == CodeGenOptions::SignReturnAddressKeyValue::AKey |
| 5029 | ? "a_key" |
| 5030 | : "b_key"); |
| 5031 | } |
| 5032 | |
| 5033 | if (CGM.getCodeGenOpts().BranchTargetEnforcement) |
| 5034 | Fn->addFnAttr("branch-target-enforcement"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5035 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5036 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5037 | |
| 5038 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 5039 | public: |
| 5040 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 5041 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 5042 | |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5043 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5044 | CodeGen::CodeGenModule &CGM) const override; |
| 5045 | |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5046 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5047 | llvm::SmallString<24> &Opt) const override { |
| 5048 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5049 | } |
| 5050 | |
| 5051 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5052 | llvm::SmallString<32> &Opt) const override { |
| 5053 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5054 | } |
| 5055 | }; |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5056 | |
| 5057 | void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( |
| 5058 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5059 | AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5060 | if (GV->isDeclaration()) |
| 5061 | return; |
| 5062 | addStackProbeTargetAttributes(D, GV, CGM); |
| 5063 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5064 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5065 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5066 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5067 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5068 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5069 | // Handle illegal vector types here. |
| 5070 | if (isIllegalVectorType(Ty)) { |
| 5071 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5072 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 5073 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5074 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 5075 | return ABIArgInfo::getDirect(ResType); |
| 5076 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5077 | if (Size <= 32) { |
| 5078 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5079 | return ABIArgInfo::getDirect(ResType); |
| 5080 | } |
| 5081 | if (Size == 64) { |
| 5082 | llvm::Type *ResType = |
| 5083 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5084 | return ABIArgInfo::getDirect(ResType); |
| 5085 | } |
| 5086 | if (Size == 128) { |
| 5087 | llvm::Type *ResType = |
| 5088 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5089 | return ABIArgInfo::getDirect(ResType); |
| 5090 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5091 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5092 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5093 | |
| 5094 | if (!isAggregateTypeForABI(Ty)) { |
| 5095 | // Treat an enum type as its underlying type. |
| 5096 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5097 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5098 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5099 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5100 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5101 | : ABIArgInfo::getDirect()); |
| 5102 | } |
| 5103 | |
| 5104 | // Structures with either a non-trivial destructor or a non-trivial |
| 5105 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5106 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5107 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5108 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5112 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5113 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5114 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5115 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5116 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5117 | return ABIArgInfo::getIgnore(); |
| 5118 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5119 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5120 | // 0. |
| 5121 | if (IsEmpty && Size == 0) |
| 5122 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5123 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5124 | } |
| 5125 | |
| 5126 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5127 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5128 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5129 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5130 | return ABIArgInfo::getDirect( |
| 5131 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5135 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5136 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5137 | // same size and alignment. |
| 5138 | if (getTarget().isRenderScriptTarget()) { |
| 5139 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5140 | } |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5141 | unsigned Alignment; |
| 5142 | if (Kind == AArch64ABIInfo::AAPCS) { |
| 5143 | Alignment = getContext().getTypeUnadjustedAlign(Ty); |
| 5144 | Alignment = Alignment < 128 ? 64 : 128; |
| 5145 | } else { |
| 5146 | Alignment = getContext().getTypeAlign(Ty); |
| 5147 | } |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5148 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5149 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5150 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5151 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5152 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5153 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5154 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5155 | } |
| 5156 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5157 | } |
| 5158 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5159 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5162 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5163 | if (RetTy->isVoidType()) |
| 5164 | return ABIArgInfo::getIgnore(); |
| 5165 | |
| 5166 | // Large vector types should be returned via memory. |
| 5167 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5168 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5169 | |
| 5170 | if (!isAggregateTypeForABI(RetTy)) { |
| 5171 | // Treat an enum type as its underlying type. |
| 5172 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5173 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5174 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5175 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5176 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5177 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5180 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5181 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5182 | return ABIArgInfo::getIgnore(); |
| 5183 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5184 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5185 | uint64_t Members = 0; |
| 5186 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5187 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5188 | return ABIArgInfo::getDirect(); |
| 5189 | |
| 5190 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5191 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5192 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5193 | // same size and alignment. |
| 5194 | if (getTarget().isRenderScriptTarget()) { |
| 5195 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5196 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5197 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5198 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5199 | |
| 5200 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5201 | // For aggregates with 16-byte alignment, we use i128. |
| 5202 | if (Alignment < 128 && Size == 128) { |
| 5203 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5204 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5205 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5206 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5207 | } |
| 5208 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5209 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5212 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5213 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5214 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5215 | // Check whether VT is legal. |
| 5216 | unsigned NumElements = VT->getNumElements(); |
| 5217 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5218 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5219 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5220 | return true; |
| 5221 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5222 | } |
| 5223 | return false; |
| 5224 | } |
| 5225 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5226 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5227 | llvm::Type *eltTy, |
| 5228 | unsigned elts) const { |
| 5229 | if (!llvm::isPowerOf2_32(elts)) |
| 5230 | return false; |
| 5231 | if (totalSize.getQuantity() != 8 && |
| 5232 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5233 | return false; |
| 5234 | return true; |
| 5235 | } |
| 5236 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5237 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5238 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5239 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5240 | // but with the difference that any floating-point type is allowed, |
| 5241 | // including __fp16. |
| 5242 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5243 | if (BT->isFloatingPoint()) |
| 5244 | return true; |
| 5245 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5246 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5247 | if (VecSize == 64 || VecSize == 128) |
| 5248 | return true; |
| 5249 | } |
| 5250 | return false; |
| 5251 | } |
| 5252 | |
| 5253 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5254 | uint64_t Members) const { |
| 5255 | return Members <= 4; |
| 5256 | } |
| 5257 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5258 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5259 | QualType Ty, |
| 5260 | CodeGenFunction &CGF) const { |
| 5261 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5262 | bool IsIndirect = AI.isIndirect(); |
| 5263 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5264 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5265 | if (IsIndirect) |
| 5266 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5267 | else if (AI.getCoerceToType()) |
| 5268 | BaseTy = AI.getCoerceToType(); |
| 5269 | |
| 5270 | unsigned NumRegs = 1; |
| 5271 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5272 | BaseTy = ArrTy->getElementType(); |
| 5273 | NumRegs = ArrTy->getNumElements(); |
| 5274 | } |
| 5275 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5276 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5277 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5278 | // Standard, section B.4: |
| 5279 | // |
| 5280 | // struct { |
| 5281 | // void *__stack; |
| 5282 | // void *__gr_top; |
| 5283 | // void *__vr_top; |
| 5284 | // int __gr_offs; |
| 5285 | // int __vr_offs; |
| 5286 | // }; |
| 5287 | |
| 5288 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5289 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5290 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5291 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5292 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5293 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 5294 | CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5295 | |
| 5296 | Address reg_offs_p = Address::invalid(); |
| 5297 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5298 | int reg_top_index; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5299 | int RegSize = IsIndirect ? 8 : TySize.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5300 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5301 | // 3 is the field number of __gr_offs |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5302 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5303 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5304 | reg_top_index = 1; // field number for __gr_top |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5305 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5306 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5307 | // 4 is the field number of __vr_offs. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5308 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5309 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5310 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5311 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | //======================================= |
| 5315 | // Find out where argument was passed |
| 5316 | //======================================= |
| 5317 | |
| 5318 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5319 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5320 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5321 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5322 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5323 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5324 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5325 | |
| 5326 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5327 | |
| 5328 | // 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] | 5329 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5330 | CGF.EmitBlock(MaybeRegBlock); |
| 5331 | |
| 5332 | // Integer arguments may need to correct register alignment (for example a |
| 5333 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5334 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5335 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5336 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5337 | |
| 5338 | reg_offs = CGF.Builder.CreateAdd( |
| 5339 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5340 | "align_regoffs"); |
| 5341 | reg_offs = CGF.Builder.CreateAnd( |
| 5342 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5343 | "aligned_regoffs"); |
| 5344 | } |
| 5345 | |
| 5346 | // 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] | 5347 | // The fact that this is done unconditionally reflects the fact that |
| 5348 | // allocating an argument to the stack also uses up all the remaining |
| 5349 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5350 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5351 | NewOffset = CGF.Builder.CreateAdd( |
| 5352 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5353 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5354 | |
| 5355 | // Now we're in a position to decide whether this argument really was in |
| 5356 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5357 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5358 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5359 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5360 | |
| 5361 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5362 | |
| 5363 | //======================================= |
| 5364 | // Argument was in registers |
| 5365 | //======================================= |
| 5366 | |
| 5367 | // Now we emit the code for if the argument was originally passed in |
| 5368 | // registers. First start the appropriate block: |
| 5369 | CGF.EmitBlock(InRegBlock); |
| 5370 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5371 | llvm::Value *reg_top = nullptr; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5372 | Address reg_top_p = |
| 5373 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5374 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5375 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5376 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5377 | Address RegAddr = Address::invalid(); |
| 5378 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5379 | |
| 5380 | if (IsIndirect) { |
| 5381 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5382 | // stored registers or on the stack will actually be a struct **. |
| 5383 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5384 | } |
| 5385 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5386 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5387 | uint64_t NumMembers = 0; |
| 5388 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5389 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5390 | // Homogeneous aggregates passed in registers will have their elements split |
| 5391 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5392 | // qN+1, ...). We reload and store into a temporary local variable |
| 5393 | // contiguously. |
| 5394 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5395 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5396 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5397 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5398 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5399 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5400 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5401 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5402 | int Offset = 0; |
| 5403 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5404 | BaseTyInfo.first.getQuantity() < 16) |
| 5405 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5406 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5407 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5408 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5409 | Address LoadAddr = |
| 5410 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5411 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5412 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5413 | Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5414 | |
| 5415 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5416 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5417 | } |
| 5418 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5419 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5420 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5421 | // Otherwise the object is contiguous in memory. |
| 5422 | |
| 5423 | // It might be right-aligned in its slot. |
| 5424 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5425 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5426 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5427 | TySize < SlotSize) { |
| 5428 | CharUnits Offset = SlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5429 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5430 | } |
| 5431 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5432 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | CGF.EmitBranch(ContBlock); |
| 5436 | |
| 5437 | //======================================= |
| 5438 | // Argument was on the stack |
| 5439 | //======================================= |
| 5440 | CGF.EmitBlock(OnStackBlock); |
| 5441 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5442 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5443 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5444 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5445 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5446 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5447 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5448 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5449 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5450 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5451 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5452 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5453 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5454 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5455 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5456 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5457 | "align_stack"); |
| 5458 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5459 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5460 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5461 | Address OnStackAddr(OnStackPtr, |
| 5462 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5463 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5464 | // All stack slots are multiples of 8 bytes. |
| 5465 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5466 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5467 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5468 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5469 | else |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5470 | StackSize = TySize.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5471 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5472 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5473 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5474 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5475 | |
| 5476 | // Write the new value of __stack for the next call to va_arg |
| 5477 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5478 | |
| 5479 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5480 | TySize < StackSlotSize) { |
| 5481 | CharUnits Offset = StackSlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5482 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5485 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5486 | |
| 5487 | CGF.EmitBranch(ContBlock); |
| 5488 | |
| 5489 | //======================================= |
| 5490 | // Tidy up |
| 5491 | //======================================= |
| 5492 | CGF.EmitBlock(ContBlock); |
| 5493 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5494 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5495 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5496 | |
| 5497 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5498 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5499 | TyAlign); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5500 | |
| 5501 | return ResAddr; |
| 5502 | } |
| 5503 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5504 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5505 | CodeGenFunction &CGF) const { |
| 5506 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5507 | // illegal vector types. Lower VAArg here for these cases and use |
| 5508 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5509 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5510 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5511 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5512 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5513 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5514 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5515 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5516 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5517 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5518 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5521 | // The size of the actual thing passed, which might end up just |
| 5522 | // being a pointer for indirect types. |
| 5523 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5524 | |
| 5525 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5526 | // aggregates should be passed indirectly. |
| 5527 | bool IsIndirect = false; |
| 5528 | if (TyInfo.first.getQuantity() > 16) { |
| 5529 | const Type *Base = nullptr; |
| 5530 | uint64_t Members = 0; |
| 5531 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5532 | } |
| 5533 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5534 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5535 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5538 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5539 | QualType Ty) const { |
| 5540 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5541 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5542 | CharUnits::fromQuantity(8), |
| 5543 | /*allowHigherAlign*/ false); |
| 5544 | } |
| 5545 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5546 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5547 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5548 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5549 | |
| 5550 | namespace { |
| 5551 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5552 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5553 | public: |
| 5554 | enum ABIKind { |
| 5555 | APCS = 0, |
| 5556 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5557 | AAPCS_VFP = 2, |
| 5558 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5559 | }; |
| 5560 | |
| 5561 | private: |
| 5562 | ABIKind Kind; |
| 5563 | |
| 5564 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5565 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5566 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5567 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5568 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5569 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5570 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5571 | switch (getTarget().getTriple().getEnvironment()) { |
| 5572 | case llvm::Triple::Android: |
| 5573 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5574 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5575 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5576 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5577 | case llvm::Triple::MuslEABI: |
| 5578 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5579 | return true; |
| 5580 | default: |
| 5581 | return false; |
| 5582 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5585 | bool isEABIHF() const { |
| 5586 | switch (getTarget().getTriple().getEnvironment()) { |
| 5587 | case llvm::Triple::EABIHF: |
| 5588 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5589 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5590 | return true; |
| 5591 | default: |
| 5592 | return false; |
| 5593 | } |
| 5594 | } |
| 5595 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5596 | ABIKind getABIKind() const { return Kind; } |
| 5597 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5598 | private: |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5599 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic, |
| 5600 | unsigned functionCallConv) const; |
| 5601 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, |
| 5602 | unsigned functionCallConv) const; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5603 | ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, |
| 5604 | uint64_t Members) const; |
| 5605 | ABIArgInfo coerceIllegalVector(QualType Ty) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5606 | bool isIllegalVectorType(QualType Ty) const; |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 5607 | bool containsAnyFP16Vectors(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5608 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5609 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5610 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5611 | uint64_t Members) const override; |
| 5612 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5613 | bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const; |
| 5614 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5615 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5616 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5617 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5618 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5619 | |
| 5620 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5621 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5622 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5623 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5624 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5625 | bool asReturnValue) const override { |
| 5626 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5627 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5628 | bool isSwiftErrorInRegister() const override { |
| 5629 | return true; |
| 5630 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5631 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5632 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5633 | }; |
| 5634 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5635 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5636 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5637 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5638 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5639 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5640 | const ARMABIInfo &getABIInfo() const { |
| 5641 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5642 | } |
| 5643 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5644 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5645 | return 13; |
| 5646 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5647 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5648 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5649 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5650 | } |
| 5651 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5652 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5653 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5654 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5655 | |
| 5656 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5657 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5658 | return false; |
| 5659 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5660 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5661 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5662 | if (getABIInfo().isEABI()) return 88; |
| 5663 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5664 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5665 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5666 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5667 | CodeGen::CodeGenModule &CGM) const override { |
| 5668 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5669 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5670 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5671 | if (!FD) |
| 5672 | return; |
| 5673 | |
| 5674 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5675 | if (!Attr) |
| 5676 | return; |
| 5677 | |
| 5678 | const char *Kind; |
| 5679 | switch (Attr->getInterrupt()) { |
| 5680 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5681 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5682 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5683 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5684 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5685 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5686 | } |
| 5687 | |
| 5688 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5689 | |
| 5690 | Fn->addFnAttr("interrupt", Kind); |
| 5691 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5692 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5693 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5694 | return; |
| 5695 | |
| 5696 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5697 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5698 | // the backend to perform a realignment as part of the function prologue. |
| 5699 | llvm::AttrBuilder B; |
| 5700 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5701 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5702 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5703 | }; |
| 5704 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5705 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5706 | public: |
| 5707 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5708 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5709 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5710 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5711 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5712 | |
| 5713 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5714 | llvm::SmallString<24> &Opt) const override { |
| 5715 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5716 | } |
| 5717 | |
| 5718 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5719 | llvm::SmallString<32> &Opt) const override { |
| 5720 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5721 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5722 | }; |
| 5723 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5724 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5725 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5726 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5727 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5728 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 5729 | addStackProbeTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5730 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5731 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5732 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5733 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5734 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5735 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(), |
| 5736 | FI.getCallingConvention()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5737 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5738 | for (auto &I : FI.arguments()) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5739 | I.info = classifyArgumentType(I.type, FI.isVariadic(), |
| 5740 | FI.getCallingConvention()); |
| 5741 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5742 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5743 | // Always honor user-specified calling convention. |
| 5744 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5745 | return; |
| 5746 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5747 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5748 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5749 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5750 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5751 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5752 | /// Return the default calling convention that LLVM will use. |
| 5753 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5754 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5755 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5756 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5757 | else if (isEABI()) |
| 5758 | return llvm::CallingConv::ARM_AAPCS; |
| 5759 | else |
| 5760 | return llvm::CallingConv::ARM_APCS; |
| 5761 | } |
| 5762 | |
| 5763 | /// Return the calling convention that our ABI would like us to use |
| 5764 | /// as the C calling convention. |
| 5765 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5766 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5767 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5768 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5769 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5770 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5771 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5772 | llvm_unreachable("bad ABI kind"); |
| 5773 | } |
| 5774 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5775 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5776 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5777 | |
| 5778 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5779 | // they'd just match what LLVM will infer from the triple. |
| 5780 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5781 | if (abiCC != getLLVMDefaultCC()) |
| 5782 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5785 | ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { |
| 5786 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5787 | if (Size <= 32) { |
| 5788 | llvm::Type *ResType = |
| 5789 | llvm::Type::getInt32Ty(getVMContext()); |
| 5790 | return ABIArgInfo::getDirect(ResType); |
| 5791 | } |
| 5792 | if (Size == 64 || Size == 128) { |
| 5793 | llvm::Type *ResType = llvm::VectorType::get( |
| 5794 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5795 | return ABIArgInfo::getDirect(ResType); |
| 5796 | } |
| 5797 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5798 | } |
| 5799 | |
| 5800 | ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, |
| 5801 | const Type *Base, |
| 5802 | uint64_t Members) const { |
| 5803 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 5804 | // Base can be a floating-point or a vector. |
| 5805 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 5806 | // FP16 vectors should be converted to integer vectors |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 5807 | if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5808 | uint64_t Size = getContext().getTypeSize(VT); |
| 5809 | llvm::Type *NewVecTy = llvm::VectorType::get( |
| 5810 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5811 | llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); |
| 5812 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5813 | } |
| 5814 | } |
| 5815 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 5816 | } |
| 5817 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5818 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, |
| 5819 | unsigned functionCallConv) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5820 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5821 | // A single-precision floating-point type (including promoted |
| 5822 | // half-precision types); A double-precision floating-point type; |
| 5823 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5824 | // with a Base Type of a single- or double-precision floating-point type, |
| 5825 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5826 | // to four Elements. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5827 | // Variadic functions should always marshal to the base standard. |
| 5828 | bool IsAAPCS_VFP = |
| 5829 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5830 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5831 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5832 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5833 | // Handle illegal vector types here. |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5834 | if (isIllegalVectorType(Ty)) |
| 5835 | return coerceIllegalVector(Ty); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5836 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5837 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5838 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5839 | // half type natively, and does not need to interwork with AAPCS code. |
| 5840 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5841 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5842 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5843 | llvm::Type::getFloatTy(getVMContext()) : |
| 5844 | llvm::Type::getInt32Ty(getVMContext()); |
| 5845 | return ABIArgInfo::getDirect(ResType); |
| 5846 | } |
| 5847 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5848 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5849 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5850 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5851 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5852 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5853 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5854 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5855 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5856 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5857 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5858 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5859 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5860 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5861 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5862 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5863 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5864 | return ABIArgInfo::getIgnore(); |
| 5865 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5866 | if (IsAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5867 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5868 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5869 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5870 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5871 | if (isHomogeneousAggregate(Ty, Base, Members)) |
| 5872 | return classifyHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5873 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5874 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5875 | // this convention even for a variadic function: the backend will use GPRs |
| 5876 | // if needed. |
| 5877 | const Type *Base = nullptr; |
| 5878 | uint64_t Members = 0; |
| 5879 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5880 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5881 | llvm::Type *Ty = |
| 5882 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5883 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5888 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5889 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5890 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5891 | // and a pointer is passed. |
| 5892 | return ABIArgInfo::getIndirect( |
| 5893 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5894 | } |
| 5895 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5896 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5897 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5898 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5899 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5900 | uint64_t ABIAlign = 4; |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5901 | uint64_t TyAlign; |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5902 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5903 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5904 | TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5905 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5906 | } else { |
| 5907 | TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 5908 | } |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5909 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5910 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5911 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5912 | /*ByVal=*/true, |
| 5913 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5916 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5917 | // same size and alignment. |
| 5918 | if (getTarget().isRenderScriptTarget()) { |
| 5919 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5920 | } |
| 5921 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5922 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5923 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5924 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5925 | // FIXME: Try to match the types of the arguments more accurately where |
| 5926 | // we can. |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5927 | if (TyAlign <= 4) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5928 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5929 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5930 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5931 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5932 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5933 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5934 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5935 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5936 | } |
| 5937 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5938 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5939 | llvm::LLVMContext &VMContext) { |
| 5940 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5941 | // is called integer-like if its size is less than or equal to one word, and |
| 5942 | // the offset of each of its addressable sub-fields is zero. |
| 5943 | |
| 5944 | uint64_t Size = Context.getTypeSize(Ty); |
| 5945 | |
| 5946 | // Check that the type fits in a word. |
| 5947 | if (Size > 32) |
| 5948 | return false; |
| 5949 | |
| 5950 | // FIXME: Handle vector types! |
| 5951 | if (Ty->isVectorType()) |
| 5952 | return false; |
| 5953 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5954 | // Float types are never treated as "integer like". |
| 5955 | if (Ty->isRealFloatingType()) |
| 5956 | return false; |
| 5957 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5958 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5959 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5960 | return true; |
| 5961 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5962 | // Small complex integer types are "integer like". |
| 5963 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5964 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5965 | |
| 5966 | // Single element and zero sized arrays should be allowed, by the definition |
| 5967 | // above, but they are not. |
| 5968 | |
| 5969 | // Otherwise, it must be a record type. |
| 5970 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5971 | if (!RT) return false; |
| 5972 | |
| 5973 | // Ignore records with flexible arrays. |
| 5974 | const RecordDecl *RD = RT->getDecl(); |
| 5975 | if (RD->hasFlexibleArrayMember()) |
| 5976 | return false; |
| 5977 | |
| 5978 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5979 | // like". |
| 5980 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5981 | |
| 5982 | bool HadField = false; |
| 5983 | unsigned idx = 0; |
| 5984 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5985 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5986 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5987 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5988 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5989 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5990 | // struct { int : 0; int x } |
| 5991 | // is non-integer like according to gcc. |
| 5992 | if (FD->isBitField()) { |
| 5993 | if (!RD->isUnion()) |
| 5994 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5995 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5996 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5997 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5998 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5999 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6002 | // Check if this field is at offset 0. |
| 6003 | if (Layout.getFieldOffset(idx) != 0) |
| 6004 | return false; |
| 6005 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6006 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6007 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 6008 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6009 | // Only allow at most one field in a structure. This doesn't match the |
| 6010 | // wording above, but follows gcc in situations with a field following an |
| 6011 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6012 | if (!RD->isUnion()) { |
| 6013 | if (HadField) |
| 6014 | return false; |
| 6015 | |
| 6016 | HadField = true; |
| 6017 | } |
| 6018 | } |
| 6019 | |
| 6020 | return true; |
| 6021 | } |
| 6022 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6023 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic, |
| 6024 | unsigned functionCallConv) const { |
| 6025 | |
| 6026 | // Variadic functions should always marshal to the base standard. |
| 6027 | bool IsAAPCS_VFP = |
| 6028 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6029 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6030 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6031 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6032 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6033 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| 6034 | // Large vector types should be returned via memory. |
| 6035 | if (getContext().getTypeSize(RetTy) > 128) |
| 6036 | return getNaturalAlignIndirect(RetTy); |
| 6037 | // FP16 vectors should be converted to integer vectors |
| 6038 | if (!getTarget().hasLegalHalfType() && |
| 6039 | (VT->getElementType()->isFloat16Type() || |
| 6040 | VT->getElementType()->isHalfType())) |
| 6041 | return coerceIllegalVector(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6042 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 6043 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 6044 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 6045 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 6046 | // half type natively, and does not need to interwork with AAPCS code. |
| 6047 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 6048 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6049 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 6050 | llvm::Type::getFloatTy(getVMContext()) : |
| 6051 | llvm::Type::getInt32Ty(getVMContext()); |
| 6052 | return ABIArgInfo::getDirect(ResType); |
| 6053 | } |
| 6054 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 6055 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6056 | // Treat an enum type as its underlying type. |
| 6057 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6058 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6059 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6060 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6061 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6062 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6063 | |
| 6064 | // Are we following APCS? |
| 6065 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6066 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6067 | return ABIArgInfo::getIgnore(); |
| 6068 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6069 | // Complex types are all returned as packed integers. |
| 6070 | // |
| 6071 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 6072 | // correctly. |
| 6073 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6074 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 6075 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6076 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6077 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6078 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6079 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6080 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6081 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6082 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6083 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6084 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6085 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6089 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6090 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6091 | |
| 6092 | // Otherwise this is an AAPCS variant. |
| 6093 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6094 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6095 | return ABIArgInfo::getIgnore(); |
| 6096 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6097 | // Check for homogeneous aggregates with AAPCS-VFP. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6098 | if (IsAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6099 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6100 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6101 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
| 6102 | return classifyHomogeneousAggregate(RetTy, Base, Members); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6103 | } |
| 6104 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6105 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 6106 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6107 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6108 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 6109 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 6110 | // same size and alignment. |
| 6111 | if (getTarget().isRenderScriptTarget()) { |
| 6112 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 6113 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6114 | if (getDataLayout().isBigEndian()) |
| 6115 | // 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] | 6116 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6117 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6118 | // Return in the smallest viable integer type. |
| 6119 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6120 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6121 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6122 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6123 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6124 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6125 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6126 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6127 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6128 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6129 | } |
| 6130 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6131 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6134 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6135 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6136 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6137 | // On targets that don't support FP16, FP16 is expanded into float, and we |
| 6138 | // don't want the ABI to depend on whether or not FP16 is supported in |
| 6139 | // hardware. Thus return false to coerce FP16 vectors into integer vectors. |
| 6140 | if (!getTarget().hasLegalHalfType() && |
| 6141 | (VT->getElementType()->isFloat16Type() || |
| 6142 | VT->getElementType()->isHalfType())) |
| 6143 | return true; |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6144 | if (isAndroid()) { |
| 6145 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6146 | // vector ABI. The primary differences were that 3-element vector types |
| 6147 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6148 | // accepts that legacy behavior for Android only. |
| 6149 | // Check whether VT is legal. |
| 6150 | unsigned NumElements = VT->getNumElements(); |
| 6151 | // NumElements should be power of 2 or equal to 3. |
| 6152 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6153 | return true; |
| 6154 | } else { |
| 6155 | // Check whether VT is legal. |
| 6156 | unsigned NumElements = VT->getNumElements(); |
| 6157 | uint64_t Size = getContext().getTypeSize(VT); |
| 6158 | // NumElements should be power of 2. |
| 6159 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6160 | return true; |
| 6161 | // Size should be greater than 32 bits. |
| 6162 | return Size <= 32; |
| 6163 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6164 | } |
| 6165 | return false; |
| 6166 | } |
| 6167 | |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 6168 | /// Return true if a type contains any 16-bit floating point vectors |
| 6169 | bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const { |
| 6170 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 6171 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 6172 | if (NElements == 0) |
| 6173 | return false; |
| 6174 | return containsAnyFP16Vectors(AT->getElementType()); |
| 6175 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6176 | const RecordDecl *RD = RT->getDecl(); |
| 6177 | |
| 6178 | // If this is a C++ record, check the bases first. |
| 6179 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6180 | if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) { |
| 6181 | return containsAnyFP16Vectors(B.getType()); |
| 6182 | })) |
| 6183 | return true; |
| 6184 | |
| 6185 | if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) { |
| 6186 | return FD && containsAnyFP16Vectors(FD->getType()); |
| 6187 | })) |
| 6188 | return true; |
| 6189 | |
| 6190 | return false; |
| 6191 | } else { |
| 6192 | if (const VectorType *VT = Ty->getAs<VectorType>()) |
| 6193 | return (VT->getElementType()->isFloat16Type() || |
| 6194 | VT->getElementType()->isHalfType()); |
| 6195 | return false; |
| 6196 | } |
| 6197 | } |
| 6198 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6199 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6200 | llvm::Type *eltTy, |
| 6201 | unsigned numElts) const { |
| 6202 | if (!llvm::isPowerOf2_32(numElts)) |
| 6203 | return false; |
| 6204 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6205 | if (size > 64) |
| 6206 | return false; |
| 6207 | if (vectorSize.getQuantity() != 8 && |
| 6208 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6209 | return false; |
| 6210 | return true; |
| 6211 | } |
| 6212 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6213 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6214 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6215 | // double, or 64-bit or 128-bit vectors. |
| 6216 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6217 | if (BT->getKind() == BuiltinType::Float || |
| 6218 | BT->getKind() == BuiltinType::Double || |
| 6219 | BT->getKind() == BuiltinType::LongDouble) |
| 6220 | return true; |
| 6221 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6222 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6223 | if (VecSize == 64 || VecSize == 128) |
| 6224 | return true; |
| 6225 | } |
| 6226 | return false; |
| 6227 | } |
| 6228 | |
| 6229 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6230 | uint64_t Members) const { |
| 6231 | return Members <= 4; |
| 6232 | } |
| 6233 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6234 | bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention, |
| 6235 | bool acceptHalf) const { |
| 6236 | // Give precedence to user-specified calling conventions. |
| 6237 | if (callConvention != llvm::CallingConv::C) |
| 6238 | return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP); |
| 6239 | else |
| 6240 | return (getABIKind() == AAPCS_VFP) || |
| 6241 | (acceptHalf && (getABIKind() == AAPCS16_VFP)); |
| 6242 | } |
| 6243 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6244 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6245 | QualType Ty) const { |
| 6246 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6247 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6248 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6249 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6250 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6251 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6252 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6255 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 6256 | CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6257 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6258 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6259 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6260 | const Type *Base = nullptr; |
| 6261 | uint64_t Members = 0; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6262 | if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6263 | IsIndirect = true; |
| 6264 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6265 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6266 | // allocated by the caller. |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6267 | } else if (TySize > CharUnits::fromQuantity(16) && |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6268 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6269 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6270 | IsIndirect = true; |
| 6271 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6272 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6273 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6274 | // 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] | 6275 | // Our callers should be prepared to handle an under-aligned address. |
| 6276 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6277 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6278 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6279 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6280 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6281 | // ARMv7k allows type alignment up to 16 bytes. |
| 6282 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6283 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6284 | } else { |
| 6285 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6286 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6287 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6288 | std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6289 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6290 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6293 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6294 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6295 | //===----------------------------------------------------------------------===// |
| 6296 | |
| 6297 | namespace { |
| 6298 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6299 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6300 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6301 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6302 | |
| 6303 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6304 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6305 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6306 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6307 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6308 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6309 | }; |
| 6310 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6311 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6312 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6313 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6314 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6315 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6316 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6317 | CodeGen::CodeGenModule &M) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6318 | bool shouldEmitStaticExternCAliases() const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6319 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6320 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6321 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6322 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6323 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6324 | }; |
| 6325 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6326 | /// Checks if the type is unsupported directly by the current target. |
| 6327 | static bool isUnsupportedType(ASTContext &Context, QualType T) { |
| 6328 | if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type()) |
| 6329 | return true; |
Alexey Bataev | 7ae267d | 2019-06-18 19:04:27 +0000 | [diff] [blame] | 6330 | if (!Context.getTargetInfo().hasFloat128Type() && |
| 6331 | (T->isFloat128Type() || |
| 6332 | (T->isRealFloatingType() && Context.getTypeSize(T) == 128))) |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6333 | return true; |
| 6334 | if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() && |
| 6335 | Context.getTypeSize(T) > 64) |
| 6336 | return true; |
| 6337 | if (const auto *AT = T->getAsArrayTypeUnsafe()) |
| 6338 | return isUnsupportedType(Context, AT->getElementType()); |
| 6339 | const auto *RT = T->getAs<RecordType>(); |
| 6340 | if (!RT) |
| 6341 | return false; |
| 6342 | const RecordDecl *RD = RT->getDecl(); |
| 6343 | |
| 6344 | // If this is a C++ record, check the bases first. |
| 6345 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6346 | for (const CXXBaseSpecifier &I : CXXRD->bases()) |
| 6347 | if (isUnsupportedType(Context, I.getType())) |
| 6348 | return true; |
| 6349 | |
| 6350 | for (const FieldDecl *I : RD->fields()) |
| 6351 | if (isUnsupportedType(Context, I->getType())) |
| 6352 | return true; |
| 6353 | return false; |
| 6354 | } |
| 6355 | |
| 6356 | /// Coerce the given type into an array with maximum allowed size of elements. |
| 6357 | static ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, ASTContext &Context, |
| 6358 | llvm::LLVMContext &LLVMContext, |
| 6359 | unsigned MaxSize) { |
| 6360 | // Alignment and Size are measured in bits. |
| 6361 | const uint64_t Size = Context.getTypeSize(Ty); |
| 6362 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 6363 | const unsigned Div = std::min<unsigned>(MaxSize, Alignment); |
| 6364 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Div); |
| 6365 | const uint64_t NumElements = (Size + Div - 1) / Div; |
| 6366 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 6367 | } |
| 6368 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6369 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6370 | if (RetTy->isVoidType()) |
| 6371 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6372 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6373 | if (getContext().getLangOpts().OpenMP && |
| 6374 | getContext().getLangOpts().OpenMPIsDevice && |
| 6375 | isUnsupportedType(getContext(), RetTy)) |
| 6376 | return coerceToIntArrayWithLimit(RetTy, getContext(), getVMContext(), 64); |
| 6377 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6378 | // note: this is different from default ABI |
| 6379 | if (!RetTy->isScalarType()) |
| 6380 | return ABIArgInfo::getDirect(); |
| 6381 | |
| 6382 | // Treat an enum type as its underlying type. |
| 6383 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6384 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6385 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6386 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6387 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6388 | } |
| 6389 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6390 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6391 | // Treat an enum type as its underlying type. |
| 6392 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6393 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6394 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6395 | // Return aggregates type as indirect by value |
| 6396 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6397 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6398 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6399 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6400 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6401 | } |
| 6402 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6403 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6404 | if (!getCXXABI().classifyReturnType(FI)) |
| 6405 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6406 | for (auto &I : FI.arguments()) |
| 6407 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6408 | |
| 6409 | // Always honor user-specified calling convention. |
| 6410 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6411 | return; |
| 6412 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6413 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6414 | } |
| 6415 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6416 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6417 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6418 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6421 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6422 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6423 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6424 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6425 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6426 | if (!FD) return; |
| 6427 | |
| 6428 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6429 | |
| 6430 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6431 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6432 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6433 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6434 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6435 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6436 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6437 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6438 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6439 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6440 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6441 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6442 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6443 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6444 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6445 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6446 | // __global__ functions cannot be called from the device, we do not |
| 6447 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6448 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6449 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6450 | addNVVMMetadata(F, "kernel", 1); |
| 6451 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6452 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6453 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6454 | llvm::APSInt MaxThreads(32); |
| 6455 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6456 | if (MaxThreads > 0) |
| 6457 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6458 | |
| 6459 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6460 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6461 | // we don't have to add a PTX directive. |
| 6462 | if (Attr->getMinBlocks()) { |
| 6463 | llvm::APSInt MinBlocks(32); |
| 6464 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6465 | if (MinBlocks > 0) |
| 6466 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6467 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6468 | } |
| 6469 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6470 | } |
| 6471 | } |
| 6472 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6473 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6474 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6475 | llvm::Module *M = F->getParent(); |
| 6476 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6477 | |
| 6478 | // Get "nvvm.annotations" metadata node |
| 6479 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6480 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6481 | llvm::Metadata *MDVals[] = { |
| 6482 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6483 | llvm::ConstantAsMetadata::get( |
| 6484 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6485 | // Append metadata to nvvm.annotations |
| 6486 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6487 | } |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6488 | |
| 6489 | bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 6490 | return false; |
| 6491 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6492 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6493 | |
| 6494 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6495 | // SystemZ ABI Implementation |
| 6496 | //===----------------------------------------------------------------------===// |
| 6497 | |
| 6498 | namespace { |
| 6499 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6500 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6501 | bool HasVector; |
| 6502 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6503 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6504 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6505 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6506 | |
| 6507 | bool isPromotableIntegerType(QualType Ty) const; |
| 6508 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6509 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6510 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6511 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6512 | |
| 6513 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6514 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6515 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6516 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6517 | if (!getCXXABI().classifyReturnType(FI)) |
| 6518 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6519 | for (auto &I : FI.arguments()) |
| 6520 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6521 | } |
| 6522 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6523 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6524 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6525 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6526 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6527 | bool asReturnValue) const override { |
| 6528 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6529 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6530 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6531 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6532 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6533 | }; |
| 6534 | |
| 6535 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6536 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6537 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6538 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6539 | }; |
| 6540 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6541 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6542 | |
| 6543 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6544 | // Treat an enum type as its underlying type. |
| 6545 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6546 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6547 | |
| 6548 | // Promotable integer types are required to be promoted by the ABI. |
| 6549 | if (Ty->isPromotableIntegerType()) |
| 6550 | return true; |
| 6551 | |
| 6552 | // 32-bit values must also be promoted. |
| 6553 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6554 | switch (BT->getKind()) { |
| 6555 | case BuiltinType::Int: |
| 6556 | case BuiltinType::UInt: |
| 6557 | return true; |
| 6558 | default: |
| 6559 | return false; |
| 6560 | } |
| 6561 | return false; |
| 6562 | } |
| 6563 | |
| 6564 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6565 | return (Ty->isAnyComplexType() || |
| 6566 | Ty->isVectorType() || |
| 6567 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6570 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6571 | return (HasVector && |
| 6572 | Ty->isVectorType() && |
| 6573 | getContext().getTypeSize(Ty) <= 128); |
| 6574 | } |
| 6575 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6576 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6577 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6578 | switch (BT->getKind()) { |
| 6579 | case BuiltinType::Float: |
| 6580 | case BuiltinType::Double: |
| 6581 | return true; |
| 6582 | default: |
| 6583 | return false; |
| 6584 | } |
| 6585 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6586 | return false; |
| 6587 | } |
| 6588 | |
| 6589 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6590 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6591 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6592 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6593 | |
| 6594 | // If this is a C++ record, check the bases first. |
| 6595 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6596 | for (const auto &I : CXXRD->bases()) { |
| 6597 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6598 | |
| 6599 | // Empty bases don't affect things either way. |
| 6600 | if (isEmptyRecord(getContext(), Base, true)) |
| 6601 | continue; |
| 6602 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6603 | if (!Found.isNull()) |
| 6604 | return Ty; |
| 6605 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
| 6608 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6609 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6610 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6611 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6612 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6613 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 6614 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6615 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6616 | |
| 6617 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6618 | // Nested structures still do though. |
| 6619 | if (!Found.isNull()) |
| 6620 | return Ty; |
| 6621 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6622 | } |
| 6623 | |
| 6624 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6625 | // 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] | 6626 | if (!Found.isNull()) |
| 6627 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6628 | } |
| 6629 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6630 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6633 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6634 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6635 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6636 | // struct { |
| 6637 | // i64 __gpr; |
| 6638 | // i64 __fpr; |
| 6639 | // i8 *__overflow_arg_area; |
| 6640 | // i8 *__reg_save_area; |
| 6641 | // }; |
| 6642 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6643 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6644 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6645 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6646 | Ty = getContext().getCanonicalType(Ty); |
| 6647 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6648 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6649 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6650 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6651 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6652 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6653 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6654 | CharUnits UnpaddedSize; |
| 6655 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6656 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6657 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6658 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6659 | } else { |
| 6660 | if (AI.getCoerceToType()) |
| 6661 | ArgTy = AI.getCoerceToType(); |
| 6662 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6663 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6664 | UnpaddedSize = TyInfo.first; |
| 6665 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6666 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6667 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6668 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6669 | PaddedSize = CharUnits::fromQuantity(16); |
| 6670 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6671 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6672 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6673 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6674 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6675 | llvm::Value *PaddedSizeV = |
| 6676 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6677 | |
| 6678 | if (IsVector) { |
| 6679 | // Work out the address of a vector argument on the stack. |
| 6680 | // Vector arguments are always passed in the high bits of a |
| 6681 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6682 | Address OverflowArgAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6683 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6684 | Address OverflowArgArea = |
| 6685 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6686 | TyInfo.second); |
| 6687 | Address MemAddr = |
| 6688 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6689 | |
| 6690 | // Update overflow_arg_area_ptr pointer |
| 6691 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6692 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6693 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6694 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6695 | |
| 6696 | return MemAddr; |
| 6697 | } |
| 6698 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6699 | assert(PaddedSize.getQuantity() == 8); |
| 6700 | |
| 6701 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6702 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6703 | if (InFPRs) { |
| 6704 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6705 | RegCountField = 1; // __fpr |
| 6706 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6707 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6708 | } else { |
| 6709 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6710 | RegCountField = 0; // __gpr |
| 6711 | RegSaveIndex = 2; // save offset for r2 |
| 6712 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6713 | } |
| 6714 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6715 | Address RegCountPtr = |
| 6716 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6717 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6718 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6719 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6720 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6721 | |
| 6722 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6723 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6724 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6725 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6726 | |
| 6727 | // Emit code to load the value if it was passed in registers. |
| 6728 | CGF.EmitBlock(InRegBlock); |
| 6729 | |
| 6730 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6731 | llvm::Value *ScaledRegCount = |
| 6732 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6733 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6734 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6735 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6736 | llvm::Value *RegOffset = |
| 6737 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6738 | Address RegSaveAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6739 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6740 | llvm::Value *RegSaveArea = |
| 6741 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6742 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6743 | "raw_reg_addr"), |
| 6744 | PaddedSize); |
| 6745 | Address RegAddr = |
| 6746 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6747 | |
| 6748 | // Update the register count |
| 6749 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6750 | llvm::Value *NewRegCount = |
| 6751 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6752 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6753 | CGF.EmitBranch(ContBlock); |
| 6754 | |
| 6755 | // Emit code to load the value if it was passed in memory. |
| 6756 | CGF.EmitBlock(InMemBlock); |
| 6757 | |
| 6758 | // Work out the address of a stack argument. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6759 | Address OverflowArgAreaPtr = |
| 6760 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6761 | Address OverflowArgArea = |
| 6762 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6763 | PaddedSize); |
| 6764 | Address RawMemAddr = |
| 6765 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6766 | Address MemAddr = |
| 6767 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6768 | |
| 6769 | // Update overflow_arg_area_ptr pointer |
| 6770 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6771 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6772 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6773 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6774 | CGF.EmitBranch(ContBlock); |
| 6775 | |
| 6776 | // Return the appropriate result. |
| 6777 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6778 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6779 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6780 | |
| 6781 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6782 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6783 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6784 | |
| 6785 | return ResAddr; |
| 6786 | } |
| 6787 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6788 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6789 | if (RetTy->isVoidType()) |
| 6790 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6791 | if (isVectorArgumentType(RetTy)) |
| 6792 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6793 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6794 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6795 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6796 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6800 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6801 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6802 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6803 | |
| 6804 | // Integers and enums are extended to full register width. |
| 6805 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6806 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6807 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6808 | // Handle vector types and vector-like structure types. Note that |
| 6809 | // as opposed to float-like structure types, we do not allow any |
| 6810 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6811 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6812 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6813 | if (isVectorArgumentType(SingleElementTy) && |
| 6814 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6815 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6816 | |
| 6817 | // 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] | 6818 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6819 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6820 | |
| 6821 | // Handle small structures. |
| 6822 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6823 | // Structures with flexible arrays have variable length, so really |
| 6824 | // fail the size test above. |
| 6825 | const RecordDecl *RD = RT->getDecl(); |
| 6826 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6827 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6828 | |
| 6829 | // The structure is passed as an unextended integer, a float, or a double. |
| 6830 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6831 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6832 | assert(Size == 32 || Size == 64); |
| 6833 | if (Size == 32) |
| 6834 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6835 | else |
| 6836 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6837 | } else |
| 6838 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6839 | return ABIArgInfo::getDirect(PassTy); |
| 6840 | } |
| 6841 | |
| 6842 | // Non-structure compounds are passed indirectly. |
| 6843 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6844 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6845 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6846 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6850 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6851 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6852 | |
| 6853 | namespace { |
| 6854 | |
| 6855 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6856 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6857 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6858 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6859 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6860 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6861 | }; |
| 6862 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6863 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6864 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6865 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6866 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6867 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6868 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6869 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6870 | const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); |
| 6871 | if (!InterruptAttr) |
| 6872 | return; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6873 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6874 | // Handle 'interrupt' attribute: |
| 6875 | llvm::Function *F = cast<llvm::Function>(GV); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6876 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6877 | // Step 1: Set ISR calling convention. |
| 6878 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6879 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6880 | // Step 2: Add attributes goodness. |
| 6881 | F->addFnAttr(llvm::Attribute::NoInline); |
| 6882 | F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6883 | } |
| 6884 | } |
| 6885 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6886 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6887 | // MIPS ABI Implementation. This works for both little-endian and |
| 6888 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6889 | //===----------------------------------------------------------------------===// |
| 6890 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6891 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6892 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6893 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6894 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6895 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6896 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6897 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6898 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6899 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6900 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6901 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6902 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6903 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6904 | |
| 6905 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6906 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6907 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6908 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6909 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6910 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6911 | }; |
| 6912 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6913 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6914 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6915 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6916 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6917 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6918 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6919 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6920 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6921 | return 29; |
| 6922 | } |
| 6923 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6924 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6925 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6926 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6927 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6928 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6929 | |
| 6930 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6931 | Fn->addFnAttr("long-call"); |
| 6932 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6933 | Fn->addFnAttr("short-call"); |
| 6934 | |
| 6935 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6936 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6937 | return; |
| 6938 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6939 | if (FD->hasAttr<Mips16Attr>()) { |
| 6940 | Fn->addFnAttr("mips16"); |
| 6941 | } |
| 6942 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6943 | Fn->addFnAttr("nomips16"); |
| 6944 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6945 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6946 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6947 | Fn->addFnAttr("micromips"); |
| 6948 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6949 | Fn->addFnAttr("nomicromips"); |
| 6950 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6951 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6952 | if (!Attr) |
| 6953 | return; |
| 6954 | |
| 6955 | const char *Kind; |
| 6956 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6957 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6958 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6959 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6960 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6961 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6962 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6963 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6964 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6965 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6966 | } |
| 6967 | |
| 6968 | Fn->addFnAttr("interrupt", Kind); |
| 6969 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6970 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6971 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6972 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6973 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6974 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6975 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6976 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6977 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6978 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6979 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6980 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6981 | void MipsABIInfo::CoerceToIntArgs( |
| 6982 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6983 | llvm::IntegerType *IntTy = |
| 6984 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6985 | |
| 6986 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6987 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6988 | ArgList.push_back(IntTy); |
| 6989 | |
| 6990 | // If necessary, add one more integer type to ArgList. |
| 6991 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6992 | |
| 6993 | if (R) |
| 6994 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6997 | // In N32/64, an aligned double precision floating point field is passed in |
| 6998 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6999 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7000 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 7001 | |
| 7002 | if (IsO32) { |
| 7003 | CoerceToIntArgs(TySize, ArgList); |
| 7004 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7005 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7006 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 7007 | if (Ty->isComplexType()) |
| 7008 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 7009 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 7010 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7011 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7012 | // Unions/vectors are passed in integer registers. |
| 7013 | if (!RT || !RT->isStructureOrClassType()) { |
| 7014 | CoerceToIntArgs(TySize, ArgList); |
| 7015 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7016 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7017 | |
| 7018 | const RecordDecl *RD = RT->getDecl(); |
| 7019 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7020 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7021 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7022 | uint64_t LastOffset = 0; |
| 7023 | unsigned idx = 0; |
| 7024 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 7025 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 7026 | // Iterate over fields in the struct/class and check if there are any aligned |
| 7027 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7028 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 7029 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7030 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7031 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 7032 | |
| 7033 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 7034 | continue; |
| 7035 | |
| 7036 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 7037 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 7038 | continue; |
| 7039 | |
| 7040 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 7041 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 7042 | ArgList.push_back(I64); |
| 7043 | |
| 7044 | // Add double type. |
| 7045 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 7046 | LastOffset = Offset + 64; |
| 7047 | } |
| 7048 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7049 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 7050 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7051 | |
| 7052 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7053 | } |
| 7054 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7055 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 7056 | uint64_t Offset) const { |
| 7057 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7058 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7059 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7060 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7061 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 7062 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7063 | ABIArgInfo |
| 7064 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 7065 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7066 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7067 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7068 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7069 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7070 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7071 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 7072 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7073 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 7074 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7075 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7076 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7077 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7078 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7079 | return ABIArgInfo::getIgnore(); |
| 7080 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 7081 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7082 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7083 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7084 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 7085 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7086 | // If we have reached here, aggregates are passed directly by coercing to |
| 7087 | // another structure type. Padding is inserted if the offset of the |
| 7088 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 7089 | ABIArgInfo ArgInfo = |
| 7090 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 7091 | getPaddingType(OrigOffset, CurrOffset)); |
| 7092 | ArgInfo.setInReg(true); |
| 7093 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7094 | } |
| 7095 | |
| 7096 | // Treat an enum type as its underlying type. |
| 7097 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7098 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7099 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 7100 | // All integral types are promoted to the GPR width. |
| 7101 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7102 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7103 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7104 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7105 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7106 | } |
| 7107 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7108 | llvm::Type* |
| 7109 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7110 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7111 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7112 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7113 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7114 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7115 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 7116 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7117 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7118 | // N32/64 returns struct/classes in floating point registers if the |
| 7119 | // following conditions are met: |
| 7120 | // 1. The size of the struct/class is no larger than 128-bit. |
| 7121 | // 2. The struct/class has one or two fields all of which are floating |
| 7122 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7123 | // 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] | 7124 | // |
| 7125 | // Any other composite results are returned in integer registers. |
| 7126 | // |
| 7127 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 7128 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 7129 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7130 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7131 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7132 | if (!BT || !BT->isFloatingPoint()) |
| 7133 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7134 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7135 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
| 7138 | if (b == e) |
| 7139 | return llvm::StructType::get(getVMContext(), RTList, |
| 7140 | RD->hasAttr<PackedAttr>()); |
| 7141 | |
| 7142 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7143 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7144 | } |
| 7145 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7146 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7147 | return llvm::StructType::get(getVMContext(), RTList); |
| 7148 | } |
| 7149 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7150 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 7151 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7152 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 7153 | if (RetTy->isVoidType()) |
| 7154 | return ABIArgInfo::getIgnore(); |
| 7155 | |
| 7156 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 7157 | // However, N32/N64 ignores zero sized return values. |
| 7158 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7159 | return ABIArgInfo::getIgnore(); |
| 7160 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 7161 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7162 | if (Size <= 128) { |
| 7163 | if (RetTy->isAnyComplexType()) |
| 7164 | return ABIArgInfo::getDirect(); |
| 7165 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7166 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 7167 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7168 | if (!IsO32 || |
| 7169 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 7170 | ABIArgInfo ArgInfo = |
| 7171 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 7172 | ArgInfo.setInReg(true); |
| 7173 | return ArgInfo; |
| 7174 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7175 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7176 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7177 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7178 | } |
| 7179 | |
| 7180 | // Treat an enum type as its underlying type. |
| 7181 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7182 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7183 | |
Stefan Maksimovic | b9da8a5 | 2018-07-30 10:44:46 +0000 | [diff] [blame] | 7184 | if (RetTy->isPromotableIntegerType()) |
| 7185 | return ABIArgInfo::getExtend(RetTy); |
| 7186 | |
| 7187 | if ((RetTy->isUnsignedIntegerOrEnumerationType() || |
| 7188 | RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) |
| 7189 | return ABIArgInfo::getSignExtend(RetTy); |
| 7190 | |
| 7191 | return ABIArgInfo::getDirect(); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7192 | } |
| 7193 | |
| 7194 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7195 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7196 | if (!getCXXABI().classifyReturnType(FI)) |
| 7197 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7198 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7199 | // 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] | 7200 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7201 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7202 | for (auto &I : FI.arguments()) |
| 7203 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7204 | } |
| 7205 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7206 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7207 | QualType OrigTy) const { |
| 7208 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7209 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7210 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 7211 | // 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] | 7212 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7213 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7214 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7215 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7216 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7217 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7218 | DidPromote = true; |
| 7219 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7220 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7221 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7222 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7223 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7224 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7225 | // The alignment of things in the argument area is never larger than |
| 7226 | // StackAlignInBytes. |
| 7227 | TyInfo.second = |
| 7228 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7229 | |
| 7230 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7231 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7232 | |
| 7233 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7234 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7235 | |
| 7236 | |
| 7237 | // If there was a promotion, "unpromote" into a temporary. |
| 7238 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7239 | if (DidPromote) { |
| 7240 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7241 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7242 | |
| 7243 | // Truncate down to the right width. |
| 7244 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7245 | : CGF.IntPtrTy); |
| 7246 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7247 | if (OrigTy->isPointerType()) |
| 7248 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7249 | |
| 7250 | CGF.Builder.CreateStore(V, Temp); |
| 7251 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7252 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7253 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7254 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7257 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7258 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7259 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7260 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7261 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7262 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7263 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7264 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7267 | bool |
| 7268 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7269 | llvm::Value *Address) const { |
| 7270 | // This information comes from gcc's implementation, which seems to |
| 7271 | // as canonical as it gets. |
| 7272 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7273 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7274 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7275 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7276 | |
| 7277 | // 0-31 are the general purpose registers, $0 - $31. |
| 7278 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7279 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7280 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7281 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7282 | |
| 7283 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7284 | // They are one bit wide and ignored here. |
| 7285 | |
| 7286 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7287 | // (coprocessor 1 is the FP unit) |
| 7288 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7289 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7290 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7291 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7292 | return false; |
| 7293 | } |
| 7294 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7295 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7296 | // AVR ABI Implementation. |
| 7297 | //===----------------------------------------------------------------------===// |
| 7298 | |
| 7299 | namespace { |
| 7300 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7301 | public: |
| 7302 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7303 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7304 | |
| 7305 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7306 | CodeGen::CodeGenModule &CGM) const override { |
| 7307 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7308 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7309 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7310 | if (!FD) return; |
| 7311 | auto *Fn = cast<llvm::Function>(GV); |
| 7312 | |
| 7313 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7314 | Fn->addFnAttr("interrupt"); |
| 7315 | |
| 7316 | if (FD->getAttr<AVRSignalAttr>()) |
| 7317 | Fn->addFnAttr("signal"); |
| 7318 | } |
| 7319 | }; |
| 7320 | } |
| 7321 | |
| 7322 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7323 | // 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] | 7324 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7325 | // handling. |
| 7326 | //===----------------------------------------------------------------------===// |
| 7327 | |
| 7328 | namespace { |
| 7329 | |
| 7330 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7331 | public: |
| 7332 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7333 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7334 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7335 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7336 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7337 | }; |
| 7338 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7339 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7340 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7341 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7342 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7343 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7344 | if (!FD) return; |
| 7345 | |
| 7346 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7347 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7348 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7349 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7350 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7351 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7352 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7353 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7354 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7355 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7356 | llvm::NamedMDNode *OpenCLMetadata = |
| 7357 | M.getModule().getOrInsertNamedMetadata( |
| 7358 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7359 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7360 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7361 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7362 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7363 | Operands.push_back( |
| 7364 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7365 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7366 | Operands.push_back( |
| 7367 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7368 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7369 | Operands.push_back( |
| 7370 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7371 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7372 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7373 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7374 | // (false) for implementing the work_group_size_hint attr later. |
| 7375 | // 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] | 7376 | Operands.push_back( |
| 7377 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7378 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7379 | } |
| 7380 | } |
| 7381 | } |
| 7382 | } |
| 7383 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7384 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7385 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7386 | //===----------------------------------------------------------------------===// |
| 7387 | // Hexagon ABI Implementation |
| 7388 | //===----------------------------------------------------------------------===// |
| 7389 | |
| 7390 | namespace { |
| 7391 | |
| 7392 | class HexagonABIInfo : public ABIInfo { |
| 7393 | |
| 7394 | |
| 7395 | public: |
| 7396 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7397 | |
| 7398 | private: |
| 7399 | |
| 7400 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7401 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7402 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7403 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7404 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7405 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7406 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7407 | }; |
| 7408 | |
| 7409 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7410 | public: |
| 7411 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7412 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7413 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7414 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7415 | return 29; |
| 7416 | } |
| 7417 | }; |
| 7418 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7419 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7420 | |
| 7421 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7422 | if (!getCXXABI().classifyReturnType(FI)) |
| 7423 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7424 | for (auto &I : FI.arguments()) |
| 7425 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7426 | } |
| 7427 | |
| 7428 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7429 | if (!isAggregateTypeForABI(Ty)) { |
| 7430 | // Treat an enum type as its underlying type. |
| 7431 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7432 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7433 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7434 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7435 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7436 | } |
| 7437 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7438 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7439 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7440 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7441 | // Ignore empty records. |
| 7442 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7443 | return ABIArgInfo::getIgnore(); |
| 7444 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7445 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7446 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7447 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7448 | // Pass in the smallest viable integer type. |
| 7449 | else if (Size > 32) |
| 7450 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7451 | else if (Size > 16) |
| 7452 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7453 | else if (Size > 8) |
| 7454 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7455 | else |
| 7456 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7457 | } |
| 7458 | |
| 7459 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7460 | if (RetTy->isVoidType()) |
| 7461 | return ABIArgInfo::getIgnore(); |
| 7462 | |
| 7463 | // Large vector types should be returned via memory. |
| 7464 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7465 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7466 | |
| 7467 | if (!isAggregateTypeForABI(RetTy)) { |
| 7468 | // Treat an enum type as its underlying type. |
| 7469 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7470 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7471 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7472 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7473 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7474 | } |
| 7475 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7476 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7477 | return ABIArgInfo::getIgnore(); |
| 7478 | |
| 7479 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7480 | // are returned indirectly. |
| 7481 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7482 | if (Size <= 64) { |
| 7483 | // Return in the smallest viable integer type. |
| 7484 | if (Size <= 8) |
| 7485 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7486 | if (Size <= 16) |
| 7487 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7488 | if (Size <= 32) |
| 7489 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7490 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7491 | } |
| 7492 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7493 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7494 | } |
| 7495 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7496 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7497 | QualType Ty) const { |
| 7498 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7499 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7500 | getContext().getTypeInfoInChars(Ty), |
| 7501 | CharUnits::fromQuantity(4), |
| 7502 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7503 | } |
| 7504 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7505 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7506 | // Lanai ABI Implementation |
| 7507 | //===----------------------------------------------------------------------===// |
| 7508 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7509 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7510 | class LanaiABIInfo : public DefaultABIInfo { |
| 7511 | public: |
| 7512 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7513 | |
| 7514 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7515 | |
| 7516 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7517 | CCState State(FI.getCallingConvention()); |
| 7518 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7519 | // regparm attribute set. |
| 7520 | if (FI.getHasRegParm()) { |
| 7521 | State.FreeRegs = FI.getRegParm(); |
| 7522 | } else { |
| 7523 | State.FreeRegs = 4; |
| 7524 | } |
| 7525 | |
| 7526 | if (!getCXXABI().classifyReturnType(FI)) |
| 7527 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7528 | for (auto &I : FI.arguments()) |
| 7529 | I.info = classifyArgumentType(I.type, State); |
| 7530 | } |
| 7531 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7532 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7533 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7534 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7535 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7536 | |
| 7537 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7538 | unsigned Size = getContext().getTypeSize(Ty); |
| 7539 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7540 | |
| 7541 | if (SizeInRegs == 0) |
| 7542 | return false; |
| 7543 | |
| 7544 | if (SizeInRegs > State.FreeRegs) { |
| 7545 | State.FreeRegs = 0; |
| 7546 | return false; |
| 7547 | } |
| 7548 | |
| 7549 | State.FreeRegs -= SizeInRegs; |
| 7550 | |
| 7551 | return true; |
| 7552 | } |
| 7553 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7554 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7555 | CCState &State) const { |
| 7556 | if (!ByVal) { |
| 7557 | if (State.FreeRegs) { |
| 7558 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7559 | return getNaturalAlignIndirectInReg(Ty); |
| 7560 | } |
| 7561 | return getNaturalAlignIndirect(Ty, false); |
| 7562 | } |
| 7563 | |
| 7564 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7565 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7566 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7567 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7568 | /*Realign=*/TypeAlign > |
| 7569 | MinABIStackAlignInBytes); |
| 7570 | } |
| 7571 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7572 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7573 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7574 | // Check with the C++ ABI first. |
| 7575 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7576 | if (RT) { |
| 7577 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7578 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7579 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7580 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7581 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7582 | } |
| 7583 | } |
| 7584 | |
| 7585 | if (isAggregateTypeForABI(Ty)) { |
| 7586 | // Structures with flexible arrays are always indirect. |
| 7587 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7588 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7589 | |
| 7590 | // Ignore empty structs/unions. |
| 7591 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7592 | return ABIArgInfo::getIgnore(); |
| 7593 | |
| 7594 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7595 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7596 | if (SizeInRegs <= State.FreeRegs) { |
| 7597 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7598 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7599 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7600 | State.FreeRegs -= SizeInRegs; |
| 7601 | return ABIArgInfo::getDirectInReg(Result); |
| 7602 | } else { |
| 7603 | State.FreeRegs = 0; |
| 7604 | } |
| 7605 | return getIndirectResult(Ty, true, State); |
| 7606 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7607 | |
| 7608 | // Treat an enum type as its underlying type. |
| 7609 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7610 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7611 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7612 | bool InReg = shouldUseInReg(Ty, State); |
| 7613 | if (Ty->isPromotableIntegerType()) { |
| 7614 | if (InReg) |
| 7615 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7616 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7617 | } |
| 7618 | if (InReg) |
| 7619 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7620 | return ABIArgInfo::getDirect(); |
| 7621 | } |
| 7622 | |
| 7623 | namespace { |
| 7624 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7625 | public: |
| 7626 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7627 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7628 | }; |
| 7629 | } |
| 7630 | |
| 7631 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7632 | // AMDGPU ABI Implementation |
| 7633 | //===----------------------------------------------------------------------===// |
| 7634 | |
| 7635 | namespace { |
| 7636 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7637 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7638 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7639 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7640 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7641 | unsigned numRegsForType(QualType Ty) const; |
| 7642 | |
| 7643 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7644 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7645 | uint64_t Members) const override; |
| 7646 | |
| 7647 | public: |
| 7648 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7649 | DefaultABIInfo(CGT) {} |
| 7650 | |
| 7651 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7652 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7653 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7654 | |
| 7655 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7656 | }; |
| 7657 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7658 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7659 | return true; |
| 7660 | } |
| 7661 | |
| 7662 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7663 | const Type *Base, uint64_t Members) const { |
| 7664 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7665 | |
| 7666 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7667 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7668 | } |
| 7669 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7670 | /// Estimate number of registers the type will use when passed in registers. |
| 7671 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7672 | unsigned NumRegs = 0; |
| 7673 | |
| 7674 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7675 | // Compute from the number of elements. The reported size is based on the |
| 7676 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7677 | QualType EltTy = VT->getElementType(); |
| 7678 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7679 | |
| 7680 | // 16-bit element vectors should be passed as packed. |
| 7681 | if (EltSize == 16) |
| 7682 | return (VT->getNumElements() + 1) / 2; |
| 7683 | |
| 7684 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7685 | return EltNumRegs * VT->getNumElements(); |
| 7686 | } |
| 7687 | |
| 7688 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7689 | const RecordDecl *RD = RT->getDecl(); |
| 7690 | assert(!RD->hasFlexibleArrayMember()); |
| 7691 | |
| 7692 | for (const FieldDecl *Field : RD->fields()) { |
| 7693 | QualType FieldTy = Field->getType(); |
| 7694 | NumRegs += numRegsForType(FieldTy); |
| 7695 | } |
| 7696 | |
| 7697 | return NumRegs; |
| 7698 | } |
| 7699 | |
| 7700 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7701 | } |
| 7702 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7703 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7704 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7705 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7706 | if (!getCXXABI().classifyReturnType(FI)) |
| 7707 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7708 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7709 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7710 | for (auto &Arg : FI.arguments()) { |
| 7711 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7712 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7713 | } else { |
| 7714 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7715 | } |
| 7716 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7717 | } |
| 7718 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7719 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7720 | if (isAggregateTypeForABI(RetTy)) { |
| 7721 | // Records with non-trivial destructors/copy-constructors should not be |
| 7722 | // returned by value. |
| 7723 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7724 | // Ignore empty structs/unions. |
| 7725 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7726 | return ABIArgInfo::getIgnore(); |
| 7727 | |
| 7728 | // Lower single-element structs to just return a regular value. |
| 7729 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7730 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7731 | |
| 7732 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7733 | const RecordDecl *RD = RT->getDecl(); |
| 7734 | if (RD->hasFlexibleArrayMember()) |
| 7735 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7736 | } |
| 7737 | |
| 7738 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7739 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7740 | if (Size <= 16) |
| 7741 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7742 | |
| 7743 | if (Size <= 32) |
| 7744 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7745 | |
| 7746 | if (Size <= 64) { |
| 7747 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7748 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7749 | } |
| 7750 | |
| 7751 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7752 | return ABIArgInfo::getDirect(); |
| 7753 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7754 | } |
| 7755 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7756 | // Otherwise just do the default thing. |
| 7757 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7758 | } |
| 7759 | |
| 7760 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7761 | /// make sense to pass anything byval, so everything must be direct. |
| 7762 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7763 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7764 | |
| 7765 | // TODO: Can we omit empty structs? |
| 7766 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7767 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7768 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7769 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7770 | |
| 7771 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7772 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7773 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7774 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7775 | } |
| 7776 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7777 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7778 | unsigned &NumRegsLeft) const { |
| 7779 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7780 | |
| 7781 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7782 | |
| 7783 | if (isAggregateTypeForABI(Ty)) { |
| 7784 | // Records with non-trivial destructors/copy-constructors should not be |
| 7785 | // passed by value. |
| 7786 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7787 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7788 | |
| 7789 | // Ignore empty structs/unions. |
| 7790 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7791 | return ABIArgInfo::getIgnore(); |
| 7792 | |
| 7793 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7794 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7795 | // though watch out for things like bitfields. |
| 7796 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7797 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7798 | |
| 7799 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7800 | const RecordDecl *RD = RT->getDecl(); |
| 7801 | if (RD->hasFlexibleArrayMember()) |
| 7802 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7803 | } |
| 7804 | |
| 7805 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7806 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7807 | if (Size <= 64) { |
| 7808 | unsigned NumRegs = (Size + 31) / 32; |
| 7809 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7810 | |
| 7811 | if (Size <= 16) |
| 7812 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7813 | |
| 7814 | if (Size <= 32) |
| 7815 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7816 | |
| 7817 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7818 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7819 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7820 | } |
| 7821 | |
| 7822 | if (NumRegsLeft > 0) { |
| 7823 | unsigned NumRegs = numRegsForType(Ty); |
| 7824 | if (NumRegsLeft >= NumRegs) { |
| 7825 | NumRegsLeft -= NumRegs; |
| 7826 | return ABIArgInfo::getDirect(); |
| 7827 | } |
| 7828 | } |
| 7829 | } |
| 7830 | |
| 7831 | // Otherwise just do the default thing. |
| 7832 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7833 | if (!ArgInfo.isIndirect()) { |
| 7834 | unsigned NumRegs = numRegsForType(Ty); |
| 7835 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7836 | } |
| 7837 | |
| 7838 | return ArgInfo; |
| 7839 | } |
| 7840 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7841 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7842 | public: |
| 7843 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7844 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7845 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7846 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7847 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7848 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7849 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7850 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7851 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7852 | LangAS getASTAllocaAddressSpace() const override { |
| 7853 | return getLangASFromTargetAS( |
| 7854 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7855 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7856 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7857 | const VarDecl *D) const override; |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 7858 | llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 7859 | SyncScope Scope, |
| 7860 | llvm::AtomicOrdering Ordering, |
| 7861 | llvm::LLVMContext &Ctx) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7862 | llvm::Function * |
| 7863 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7864 | llvm::Function *BlockInvokeFunc, |
| 7865 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7866 | bool shouldEmitStaticExternCAliases() const override; |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7867 | void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7868 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7869 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7870 | |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7871 | static bool requiresAMDGPUProtectedVisibility(const Decl *D, |
| 7872 | llvm::GlobalValue *GV) { |
| 7873 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 7874 | return false; |
| 7875 | |
| 7876 | return D->hasAttr<OpenCLKernelAttr>() || |
| 7877 | (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) || |
Michael Liao | 3820506 | 2019-04-26 19:31:48 +0000 | [diff] [blame] | 7878 | (isa<VarDecl>(D) && |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 7879 | (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || |
| 7880 | D->hasAttr<HIPPinnedShadowAttr>())); |
| 7881 | } |
| 7882 | |
| 7883 | static bool requiresAMDGPUDefaultVisibility(const Decl *D, |
| 7884 | llvm::GlobalValue *GV) { |
| 7885 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 7886 | return false; |
| 7887 | |
| 7888 | return isa<VarDecl>(D) && D->hasAttr<HIPPinnedShadowAttr>(); |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7889 | } |
| 7890 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7891 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7892 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 7893 | if (requiresAMDGPUDefaultVisibility(D, GV)) { |
| 7894 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 7895 | GV->setDSOLocal(false); |
| 7896 | } else if (requiresAMDGPUProtectedVisibility(D, GV)) { |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7897 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 7898 | GV->setDSOLocal(true); |
| 7899 | } |
| 7900 | |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7901 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7902 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7903 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7904 | if (!FD) |
| 7905 | return; |
| 7906 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7907 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7908 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7909 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7910 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7911 | |
Yaxun Liu | cabce71 | 2019-06-14 15:54:47 +0000 | [diff] [blame] | 7912 | if (((M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>()) || |
| 7913 | (M.getLangOpts().HIP && FD->hasAttr<CUDAGlobalAttr>())) && |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7914 | (M.getTriple().getOS() == llvm::Triple::AMDHSA)) |
Christudasan Devadasan | 18ba9d6 | 2019-07-10 15:10:08 +0000 | [diff] [blame] | 7915 | F->addFnAttr("amdgpu-implicitarg-num-bytes", "56"); |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7916 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7917 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7918 | if (ReqdWGS || FlatWGS) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 7919 | unsigned Min = 0; |
| 7920 | unsigned Max = 0; |
| 7921 | if (FlatWGS) { |
| 7922 | Min = FlatWGS->getMin() |
| 7923 | ->EvaluateKnownConstInt(M.getContext()) |
| 7924 | .getExtValue(); |
| 7925 | Max = FlatWGS->getMax() |
| 7926 | ->EvaluateKnownConstInt(M.getContext()) |
| 7927 | .getExtValue(); |
| 7928 | } |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7929 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7930 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7931 | |
| 7932 | if (Min != 0) { |
| 7933 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7934 | |
| 7935 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7936 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7937 | } else |
| 7938 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7939 | } |
| 7940 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7941 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 7942 | unsigned Min = |
| 7943 | Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue(); |
| 7944 | unsigned Max = Attr->getMax() ? Attr->getMax() |
| 7945 | ->EvaluateKnownConstInt(M.getContext()) |
| 7946 | .getExtValue() |
| 7947 | : 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7948 | |
| 7949 | if (Min != 0) { |
| 7950 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7951 | |
| 7952 | std::string AttrVal = llvm::utostr(Min); |
| 7953 | if (Max != 0) |
| 7954 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7955 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7956 | } else |
| 7957 | assert(Max == 0 && "Max must be zero"); |
| 7958 | } |
| 7959 | |
| 7960 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7961 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7962 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7963 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7964 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7965 | } |
| 7966 | |
| 7967 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7968 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7969 | |
| 7970 | if (NumVGPR != 0) |
| 7971 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7972 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7973 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7974 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7975 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7976 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7977 | } |
| 7978 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7979 | // Currently LLVM assumes null pointers always have value 0, |
| 7980 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7981 | // emitting null pointers in private and local address spaces, a null |
| 7982 | // pointer in generic address space is emitted which is casted to a |
| 7983 | // pointer in local or private address space. |
| 7984 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7985 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7986 | QualType QT) const { |
| 7987 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7988 | return llvm::ConstantPointerNull::get(PT); |
| 7989 | |
| 7990 | auto &Ctx = CGM.getContext(); |
| 7991 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7992 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7993 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7994 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7995 | } |
| 7996 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7997 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7998 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7999 | const VarDecl *D) const { |
| 8000 | assert(!CGM.getLangOpts().OpenCL && |
| 8001 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 8002 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8003 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 8004 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8005 | if (!D) |
| 8006 | return DefaultGlobalAS; |
| 8007 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8008 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 8009 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8010 | if (AddrSpace != LangAS::Default) |
| 8011 | return AddrSpace; |
| 8012 | |
| 8013 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 8014 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 8015 | return ConstAS.getValue(); |
| 8016 | } |
| 8017 | return DefaultGlobalAS; |
| 8018 | } |
| 8019 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8020 | llvm::SyncScope::ID |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 8021 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 8022 | SyncScope Scope, |
| 8023 | llvm::AtomicOrdering Ordering, |
| 8024 | llvm::LLVMContext &Ctx) const { |
| 8025 | std::string Name; |
| 8026 | switch (Scope) { |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8027 | case SyncScope::OpenCLWorkGroup: |
| 8028 | Name = "workgroup"; |
| 8029 | break; |
| 8030 | case SyncScope::OpenCLDevice: |
| 8031 | Name = "agent"; |
| 8032 | break; |
| 8033 | case SyncScope::OpenCLAllSVMDevices: |
| 8034 | Name = ""; |
| 8035 | break; |
| 8036 | case SyncScope::OpenCLSubGroup: |
Konstantin Zhuravlyov | 3161c89 | 2019-03-06 20:54:48 +0000 | [diff] [blame] | 8037 | Name = "wavefront"; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8038 | } |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 8039 | |
| 8040 | if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) { |
| 8041 | if (!Name.empty()) |
| 8042 | Name = Twine(Twine(Name) + Twine("-")).str(); |
| 8043 | |
| 8044 | Name = Twine(Twine(Name) + Twine("one-as")).str(); |
| 8045 | } |
| 8046 | |
| 8047 | return Ctx.getOrInsertSyncScopeID(Name); |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 8050 | bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 8051 | return false; |
| 8052 | } |
| 8053 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 8054 | void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 8055 | const FunctionType *&FT) const { |
| 8056 | FT = getABIInfo().getContext().adjustFunctionType( |
| 8057 | FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 8058 | } |
| 8059 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8060 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8061 | // SPARC v8 ABI Implementation. |
| 8062 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8063 | // |
| 8064 | // Ensures that complex values are passed in registers. |
| 8065 | // |
| 8066 | namespace { |
| 8067 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 8068 | public: |
| 8069 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 8070 | |
| 8071 | private: |
| 8072 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8073 | void computeInfo(CGFunctionInfo &FI) const override; |
| 8074 | }; |
| 8075 | } // end anonymous namespace |
| 8076 | |
| 8077 | |
| 8078 | ABIArgInfo |
| 8079 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 8080 | if (Ty->isAnyComplexType()) { |
| 8081 | return ABIArgInfo::getDirect(); |
| 8082 | } |
| 8083 | else { |
| 8084 | return DefaultABIInfo::classifyReturnType(Ty); |
| 8085 | } |
| 8086 | } |
| 8087 | |
| 8088 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8089 | |
| 8090 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8091 | for (auto &Arg : FI.arguments()) |
| 8092 | Arg.info = classifyArgumentType(Arg.type); |
| 8093 | } |
| 8094 | |
| 8095 | namespace { |
| 8096 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8097 | public: |
| 8098 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8099 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 8100 | }; |
| 8101 | } // end anonymous namespace |
| 8102 | |
| 8103 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8104 | // SPARC v9 ABI Implementation. |
| 8105 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8106 | // |
| 8107 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 8108 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 8109 | // the array, structs larger than 16 bytes are passed indirectly. |
| 8110 | // |
| 8111 | // One case requires special care: |
| 8112 | // |
| 8113 | // struct mixed { |
| 8114 | // int i; |
| 8115 | // float f; |
| 8116 | // }; |
| 8117 | // |
| 8118 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 8119 | // parameter array, but the int is passed in an integer register, and the float |
| 8120 | // is passed in a floating point register. This is represented as two arguments |
| 8121 | // with the LLVM IR inreg attribute: |
| 8122 | // |
| 8123 | // declare void f(i32 inreg %i, float inreg %f) |
| 8124 | // |
| 8125 | // The code generator will only allocate 4 bytes from the parameter array for |
| 8126 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 8127 | // bytes. |
| 8128 | // |
| 8129 | namespace { |
| 8130 | class SparcV9ABIInfo : public ABIInfo { |
| 8131 | public: |
| 8132 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 8133 | |
| 8134 | private: |
| 8135 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8136 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8137 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8138 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8139 | |
| 8140 | // Coercion type builder for structs passed in registers. The coercion type |
| 8141 | // serves two purposes: |
| 8142 | // |
| 8143 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 8144 | // in registers. |
| 8145 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 8146 | // code generator knows to pass them in floating point registers. |
| 8147 | // |
| 8148 | // We also compute the InReg flag which indicates that the struct contains |
| 8149 | // aligned 32-bit floats. |
| 8150 | // |
| 8151 | struct CoerceBuilder { |
| 8152 | llvm::LLVMContext &Context; |
| 8153 | const llvm::DataLayout &DL; |
| 8154 | SmallVector<llvm::Type*, 8> Elems; |
| 8155 | uint64_t Size; |
| 8156 | bool InReg; |
| 8157 | |
| 8158 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 8159 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 8160 | |
| 8161 | // Pad Elems with integers until Size is ToSize. |
| 8162 | void pad(uint64_t ToSize) { |
| 8163 | assert(ToSize >= Size && "Cannot remove elements"); |
| 8164 | if (ToSize == Size) |
| 8165 | return; |
| 8166 | |
| 8167 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8168 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8169 | if (Aligned > Size && Aligned <= ToSize) { |
| 8170 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 8171 | Size = Aligned; |
| 8172 | } |
| 8173 | |
| 8174 | // Add whole 64-bit words. |
| 8175 | while (Size + 64 <= ToSize) { |
| 8176 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 8177 | Size += 64; |
| 8178 | } |
| 8179 | |
| 8180 | // Final in-word padding. |
| 8181 | if (Size < ToSize) { |
| 8182 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 8183 | Size = ToSize; |
| 8184 | } |
| 8185 | } |
| 8186 | |
| 8187 | // Add a floating point element at Offset. |
| 8188 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 8189 | // Unaligned floats are treated as integers. |
| 8190 | if (Offset % Bits) |
| 8191 | return; |
| 8192 | // The InReg flag is only required if there are any floats < 64 bits. |
| 8193 | if (Bits < 64) |
| 8194 | InReg = true; |
| 8195 | pad(Offset); |
| 8196 | Elems.push_back(Ty); |
| 8197 | Size = Offset + Bits; |
| 8198 | } |
| 8199 | |
| 8200 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 8201 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 8202 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 8203 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 8204 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 8205 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 8206 | switch (ElemTy->getTypeID()) { |
| 8207 | case llvm::Type::StructTyID: |
| 8208 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 8209 | break; |
| 8210 | case llvm::Type::FloatTyID: |
| 8211 | addFloat(ElemOffset, ElemTy, 32); |
| 8212 | break; |
| 8213 | case llvm::Type::DoubleTyID: |
| 8214 | addFloat(ElemOffset, ElemTy, 64); |
| 8215 | break; |
| 8216 | case llvm::Type::FP128TyID: |
| 8217 | addFloat(ElemOffset, ElemTy, 128); |
| 8218 | break; |
| 8219 | case llvm::Type::PointerTyID: |
| 8220 | if (ElemOffset % 64 == 0) { |
| 8221 | pad(ElemOffset); |
| 8222 | Elems.push_back(ElemTy); |
| 8223 | Size += 64; |
| 8224 | } |
| 8225 | break; |
| 8226 | default: |
| 8227 | break; |
| 8228 | } |
| 8229 | } |
| 8230 | } |
| 8231 | |
| 8232 | // Check if Ty is a usable substitute for the coercion type. |
| 8233 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 8234 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8235 | } |
| 8236 | |
| 8237 | // Get the coercion type as a literal struct type. |
| 8238 | llvm::Type *getType() const { |
| 8239 | if (Elems.size() == 1) |
| 8240 | return Elems.front(); |
| 8241 | else |
| 8242 | return llvm::StructType::get(Context, Elems); |
| 8243 | } |
| 8244 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8245 | }; |
| 8246 | } // end anonymous namespace |
| 8247 | |
| 8248 | ABIArgInfo |
| 8249 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 8250 | if (Ty->isVoidType()) |
| 8251 | return ABIArgInfo::getIgnore(); |
| 8252 | |
| 8253 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8254 | |
| 8255 | // Anything too big to fit in registers is passed with an explicit indirect |
| 8256 | // pointer / sret pointer. |
| 8257 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8258 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8259 | |
| 8260 | // Treat an enum type as its underlying type. |
| 8261 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8262 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8263 | |
| 8264 | // Integer types smaller than a register are extended. |
| 8265 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 8266 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8267 | |
| 8268 | // Other non-aggregates go in registers. |
| 8269 | if (!isAggregateTypeForABI(Ty)) |
| 8270 | return ABIArgInfo::getDirect(); |
| 8271 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8272 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 8273 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 8274 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8275 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8276 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8277 | // 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] | 8278 | // Build a coercion type from the LLVM struct type. |
| 8279 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 8280 | if (!StrTy) |
| 8281 | return ABIArgInfo::getDirect(); |
| 8282 | |
| 8283 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 8284 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8285 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8286 | |
| 8287 | // Try to use the original type for coercion. |
| 8288 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8289 | |
| 8290 | if (CB.InReg) |
| 8291 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8292 | else |
| 8293 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8294 | } |
| 8295 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8296 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8297 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8298 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8299 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8300 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8301 | AI.setCoerceToType(ArgTy); |
| 8302 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8303 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8304 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8305 | CGBuilderTy &Builder = CGF.Builder; |
| 8306 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8307 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8308 | |
| 8309 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8310 | |
| 8311 | Address ArgAddr = Address::invalid(); |
| 8312 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8313 | switch (AI.getKind()) { |
| 8314 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8315 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8316 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8317 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8318 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8319 | case ABIArgInfo::Extend: { |
| 8320 | Stride = SlotSize; |
| 8321 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8322 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8323 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8324 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8325 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8326 | case ABIArgInfo::Direct: { |
| 8327 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8328 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8329 | ArgAddr = Addr; |
| 8330 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8331 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8332 | |
| 8333 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8334 | Stride = SlotSize; |
| 8335 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8336 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8337 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8338 | break; |
| 8339 | |
| 8340 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8341 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8342 | } |
| 8343 | |
| 8344 | // Update VAList. |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8345 | Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); |
| 8346 | Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8347 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8348 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8349 | } |
| 8350 | |
| 8351 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8352 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8353 | for (auto &I : FI.arguments()) |
| 8354 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8355 | } |
| 8356 | |
| 8357 | namespace { |
| 8358 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8359 | public: |
| 8360 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8361 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8362 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8363 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8364 | return 14; |
| 8365 | } |
| 8366 | |
| 8367 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8368 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8369 | }; |
| 8370 | } // end anonymous namespace |
| 8371 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8372 | bool |
| 8373 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8374 | llvm::Value *Address) const { |
| 8375 | // This is calculated from the LLVM and GCC tables and verified |
| 8376 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8377 | |
| 8378 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8379 | |
| 8380 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8381 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8382 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8383 | |
| 8384 | // 0-31: the 8-byte general-purpose registers |
| 8385 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8386 | |
| 8387 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8388 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8389 | |
| 8390 | // Y = 64 |
| 8391 | // PSR = 65 |
| 8392 | // WIM = 66 |
| 8393 | // TBR = 67 |
| 8394 | // PC = 68 |
| 8395 | // NPC = 69 |
| 8396 | // FSR = 70 |
| 8397 | // CSR = 71 |
| 8398 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8399 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8400 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8401 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8402 | |
| 8403 | return false; |
| 8404 | } |
| 8405 | |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8406 | // ARC ABI implementation. |
| 8407 | namespace { |
| 8408 | |
| 8409 | class ARCABIInfo : public DefaultABIInfo { |
| 8410 | public: |
| 8411 | using DefaultABIInfo::DefaultABIInfo; |
| 8412 | |
| 8413 | private: |
| 8414 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8415 | QualType Ty) const override; |
| 8416 | |
| 8417 | void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { |
| 8418 | if (!State.FreeRegs) |
| 8419 | return; |
| 8420 | if (Info.isIndirect() && Info.getInReg()) |
| 8421 | State.FreeRegs--; |
| 8422 | else if (Info.isDirect() && Info.getInReg()) { |
| 8423 | unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; |
| 8424 | if (sz < State.FreeRegs) |
| 8425 | State.FreeRegs -= sz; |
| 8426 | else |
| 8427 | State.FreeRegs = 0; |
| 8428 | } |
| 8429 | } |
| 8430 | |
| 8431 | void computeInfo(CGFunctionInfo &FI) const override { |
| 8432 | CCState State(FI.getCallingConvention()); |
| 8433 | // ARC uses 8 registers to pass arguments. |
| 8434 | State.FreeRegs = 8; |
| 8435 | |
| 8436 | if (!getCXXABI().classifyReturnType(FI)) |
| 8437 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8438 | updateState(FI.getReturnInfo(), FI.getReturnType(), State); |
| 8439 | for (auto &I : FI.arguments()) { |
| 8440 | I.info = classifyArgumentType(I.type, State.FreeRegs); |
| 8441 | updateState(I.info, I.type, State); |
| 8442 | } |
| 8443 | } |
| 8444 | |
| 8445 | ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; |
| 8446 | ABIArgInfo getIndirectByValue(QualType Ty) const; |
| 8447 | ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; |
| 8448 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8449 | }; |
| 8450 | |
| 8451 | class ARCTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8452 | public: |
| 8453 | ARCTargetCodeGenInfo(CodeGenTypes &CGT) |
| 8454 | : TargetCodeGenInfo(new ARCABIInfo(CGT)) {} |
| 8455 | }; |
| 8456 | |
| 8457 | |
| 8458 | ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { |
| 8459 | return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : |
| 8460 | getNaturalAlignIndirect(Ty, false); |
| 8461 | } |
| 8462 | |
| 8463 | ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8464 | // Compute the byval alignment. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8465 | const unsigned MinABIStackAlignInBytes = 4; |
| 8466 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 8467 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 8468 | TypeAlign > MinABIStackAlignInBytes); |
| 8469 | } |
| 8470 | |
| 8471 | Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8472 | QualType Ty) const { |
| 8473 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 8474 | getContext().getTypeInfoInChars(Ty), |
| 8475 | CharUnits::fromQuantity(4), true); |
| 8476 | } |
| 8477 | |
| 8478 | ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, |
| 8479 | uint8_t FreeRegs) const { |
| 8480 | // Handle the generic C++ ABI. |
| 8481 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 8482 | if (RT) { |
| 8483 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 8484 | if (RAA == CGCXXABI::RAA_Indirect) |
| 8485 | return getIndirectByRef(Ty, FreeRegs > 0); |
| 8486 | |
| 8487 | if (RAA == CGCXXABI::RAA_DirectInMemory) |
| 8488 | return getIndirectByValue(Ty); |
| 8489 | } |
| 8490 | |
| 8491 | // Treat an enum type as its underlying type. |
| 8492 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8493 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8494 | |
| 8495 | auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; |
| 8496 | |
| 8497 | if (isAggregateTypeForABI(Ty)) { |
| 8498 | // Structures with flexible arrays are always indirect. |
| 8499 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 8500 | return getIndirectByValue(Ty); |
| 8501 | |
| 8502 | // Ignore empty structs/unions. |
| 8503 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8504 | return ABIArgInfo::getIgnore(); |
| 8505 | |
| 8506 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 8507 | |
| 8508 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 8509 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 8510 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 8511 | |
| 8512 | return FreeRegs >= SizeInRegs ? |
| 8513 | ABIArgInfo::getDirectInReg(Result) : |
| 8514 | ABIArgInfo::getDirect(Result, 0, nullptr, false); |
| 8515 | } |
| 8516 | |
| 8517 | return Ty->isPromotableIntegerType() ? |
| 8518 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) : |
| 8519 | ABIArgInfo::getExtend(Ty)) : |
| 8520 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() : |
| 8521 | ABIArgInfo::getDirect()); |
| 8522 | } |
| 8523 | |
| 8524 | ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { |
| 8525 | if (RetTy->isAnyComplexType()) |
| 8526 | return ABIArgInfo::getDirectInReg(); |
| 8527 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8528 | // Arguments of size > 4 registers are indirect. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8529 | auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; |
| 8530 | if (RetSize > 4) |
| 8531 | return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); |
| 8532 | |
| 8533 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 8534 | } |
| 8535 | |
| 8536 | } // End anonymous namespace. |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8537 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8538 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8539 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8540 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8541 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8542 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8543 | |
| 8544 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8545 | /// it by reference between functions that append to it. |
| 8546 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8547 | |
| 8548 | /// TypeStringCache caches the meta encodings of Types. |
| 8549 | /// |
| 8550 | /// The reason for caching TypeStrings is two fold: |
| 8551 | /// 1. To cache a type's encoding for later uses; |
| 8552 | /// 2. As a means to break recursive member type inclusion. |
| 8553 | /// |
| 8554 | /// A cache Entry can have a Status of: |
| 8555 | /// NonRecursive: The type encoding is not recursive; |
| 8556 | /// Recursive: The type encoding is recursive; |
| 8557 | /// Incomplete: An incomplete TypeString; |
| 8558 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8559 | /// Recursive type encoding. |
| 8560 | /// |
| 8561 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8562 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8563 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8564 | /// the type is encountered. |
| 8565 | /// |
| 8566 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8567 | /// possible. The type itself is recursive and it may contain other types which |
| 8568 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8569 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8570 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8571 | /// |
| 8572 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8573 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8574 | /// are placed into the cache during type expansion as a means to identify and |
| 8575 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8576 | /// the entry becomes IncompleteUsed. |
| 8577 | /// |
| 8578 | /// During the expansion of a RecordType's members: |
| 8579 | /// |
| 8580 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8581 | /// cached encoding is used; |
| 8582 | /// |
| 8583 | /// If the cache contains a Recursive encoding for the member type, the |
| 8584 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8585 | /// |
| 8586 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8587 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8588 | /// |
| 8589 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8590 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8591 | /// it is swapped back in; |
| 8592 | /// |
| 8593 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8594 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8595 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8596 | /// |
| 8597 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8598 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8599 | /// Else the member is part of a recursive type and thus the recursion has |
| 8600 | /// been exited too soon for the encoding to be correct for the member. |
| 8601 | /// |
| 8602 | class TypeStringCache { |
| 8603 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8604 | struct Entry { |
| 8605 | std::string Str; // The encoded TypeString for the type. |
| 8606 | enum Status State; // Information about the encoding in 'Str'. |
| 8607 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8608 | // during the expansion of RecordType's members. |
| 8609 | }; |
| 8610 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8611 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8612 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8613 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8614 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8615 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8616 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8617 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8618 | bool IsRecursive); |
| 8619 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8620 | }; |
| 8621 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8622 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8623 | /// FieldEncoding is a helper for this ordering process. |
| 8624 | class FieldEncoding { |
| 8625 | bool HasName; |
| 8626 | std::string Enc; |
| 8627 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8628 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8629 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8630 | bool operator<(const FieldEncoding &rhs) const { |
| 8631 | if (HasName != rhs.HasName) return HasName; |
| 8632 | return Enc < rhs.Enc; |
| 8633 | } |
| 8634 | }; |
| 8635 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8636 | class XCoreABIInfo : public DefaultABIInfo { |
| 8637 | public: |
| 8638 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8639 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8640 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8641 | }; |
| 8642 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8643 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8644 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8645 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8646 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8647 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8648 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8649 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8650 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8651 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8652 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8653 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8654 | // TODO: this implementation is likely now redundant with the default |
| 8655 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8656 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8657 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8658 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8659 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8660 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8661 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8662 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8663 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8664 | // Handle the argument. |
| 8665 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8666 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8667 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8668 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8669 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8670 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8671 | |
| 8672 | Address Val = Address::invalid(); |
| 8673 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8674 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8675 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8676 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8677 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8678 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8679 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8680 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8681 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8682 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8683 | case ABIArgInfo::Extend: |
| 8684 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8685 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8686 | ArgSize = CharUnits::fromQuantity( |
| 8687 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8688 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8689 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8690 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8691 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8692 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8693 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8694 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8695 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8696 | |
| 8697 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8698 | if (!ArgSize.isZero()) { |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8699 | Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize); |
| 8700 | Builder.CreateStore(APN.getPointer(), VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8701 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8702 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8703 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8704 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8705 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8706 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8707 | /// into the cache as a means to identify and break recursion. |
| 8708 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8709 | /// be reinserted by removeIncomplete(). |
| 8710 | /// All other types of encoding should have been used rather than arriving here. |
| 8711 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8712 | std::string StubEnc) { |
| 8713 | if (!ID) |
| 8714 | return; |
| 8715 | Entry &E = Map[ID]; |
| 8716 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8717 | "Incorrectly use of addIncomplete"); |
| 8718 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8719 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8720 | E.Str.swap(StubEnc); |
| 8721 | E.State = Incomplete; |
| 8722 | ++IncompleteCount; |
| 8723 | } |
| 8724 | |
| 8725 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8726 | /// must be removed from the cache. |
| 8727 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8728 | /// Returns true if the RecordType was defined recursively. |
| 8729 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8730 | if (!ID) |
| 8731 | return false; |
| 8732 | auto I = Map.find(ID); |
| 8733 | assert(I != Map.end() && "Entry not present"); |
| 8734 | Entry &E = I->second; |
| 8735 | assert( (E.State == Incomplete || |
| 8736 | E.State == IncompleteUsed) && |
| 8737 | "Entry must be an incomplete type"); |
| 8738 | bool IsRecursive = false; |
| 8739 | if (E.State == IncompleteUsed) { |
| 8740 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8741 | IsRecursive = true; |
| 8742 | --IncompleteUsedCount; |
| 8743 | } |
| 8744 | if (E.Swapped.empty()) |
| 8745 | Map.erase(I); |
| 8746 | else { |
| 8747 | // Swap the Recursive back. |
| 8748 | E.Swapped.swap(E.Str); |
| 8749 | E.Swapped.clear(); |
| 8750 | E.State = Recursive; |
| 8751 | } |
| 8752 | --IncompleteCount; |
| 8753 | return IsRecursive; |
| 8754 | } |
| 8755 | |
| 8756 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8757 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8758 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8759 | bool IsRecursive) { |
| 8760 | if (!ID || IncompleteUsedCount) |
| 8761 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8762 | Entry &E = Map[ID]; |
| 8763 | if (IsRecursive && !E.Str.empty()) { |
| 8764 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8765 | "This is not the same Recursive entry"); |
| 8766 | // The parent container was not recursive after all, so we could have used |
| 8767 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8768 | // we started viz: IncompleteCount!=0. |
| 8769 | return; |
| 8770 | } |
| 8771 | assert(E.Str.empty() && "Entry already present"); |
| 8772 | E.Str = Str.str(); |
| 8773 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8774 | } |
| 8775 | |
| 8776 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8777 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8778 | /// encoding is Recursive, return an empty StringRef. |
| 8779 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8780 | if (!ID) |
| 8781 | return StringRef(); // We have no key. |
| 8782 | auto I = Map.find(ID); |
| 8783 | if (I == Map.end()) |
| 8784 | return StringRef(); // We have no encoding. |
| 8785 | Entry &E = I->second; |
| 8786 | if (E.State == Recursive && IncompleteCount) |
| 8787 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8788 | |
| 8789 | if (E.State == Incomplete) { |
| 8790 | // The incomplete type is being used to break out of recursion. |
| 8791 | E.State = IncompleteUsed; |
| 8792 | ++IncompleteUsedCount; |
| 8793 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8794 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8795 | } |
| 8796 | |
| 8797 | /// The XCore ABI includes a type information section that communicates symbol |
| 8798 | /// type information to the linker. The linker uses this information to verify |
| 8799 | /// safety/correctness of things such as array bound and pointers et al. |
| 8800 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8801 | /// This type information (TypeString) is emitted into meta data for all global |
| 8802 | /// symbols: definitions, declarations, functions & variables. |
| 8803 | /// |
| 8804 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8805 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8806 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8807 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8808 | /// |
| 8809 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8810 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8811 | |
| 8812 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8813 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8814 | CodeGen::CodeGenModule &CGM) const { |
| 8815 | SmallStringEnc Enc; |
| 8816 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8817 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8818 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8819 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8820 | llvm::NamedMDNode *MD = |
| 8821 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8822 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8823 | } |
| 8824 | } |
| 8825 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8826 | //===----------------------------------------------------------------------===// |
| 8827 | // SPIR ABI Implementation |
| 8828 | //===----------------------------------------------------------------------===// |
| 8829 | |
| 8830 | namespace { |
| 8831 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8832 | public: |
| 8833 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8834 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8835 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8836 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8837 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8838 | } // End anonymous namespace. |
| 8839 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8840 | namespace clang { |
| 8841 | namespace CodeGen { |
| 8842 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8843 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8844 | SPIRABI.computeInfo(FI); |
| 8845 | } |
| 8846 | } |
| 8847 | } |
| 8848 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8849 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8850 | return llvm::CallingConv::SPIR_KERNEL; |
| 8851 | } |
| 8852 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8853 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8854 | const CodeGen::CodeGenModule &CGM, |
| 8855 | TypeStringCache &TSC); |
| 8856 | |
| 8857 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8858 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8859 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8860 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8861 | const RecordDecl *RD, |
| 8862 | const CodeGen::CodeGenModule &CGM, |
| 8863 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8864 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8865 | SmallStringEnc Enc; |
| 8866 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8867 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8868 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8869 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8870 | Enc += "b("; |
| 8871 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8872 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8873 | Enc += ':'; |
| 8874 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8875 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8876 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8877 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8878 | Enc += ')'; |
| 8879 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8880 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8881 | } |
| 8882 | return true; |
| 8883 | } |
| 8884 | |
| 8885 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8886 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8887 | /// Union types have their fields ordered according to the ABI. |
| 8888 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8889 | const CodeGen::CodeGenModule &CGM, |
| 8890 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8891 | // Append the cached TypeString if we have one. |
| 8892 | StringRef TypeString = TSC.lookupStr(ID); |
| 8893 | if (!TypeString.empty()) { |
| 8894 | Enc += TypeString; |
| 8895 | return true; |
| 8896 | } |
| 8897 | |
| 8898 | // Start to emit an incomplete TypeString. |
| 8899 | size_t Start = Enc.size(); |
| 8900 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8901 | Enc += '('; |
| 8902 | if (ID) |
| 8903 | Enc += ID->getName(); |
| 8904 | Enc += "){"; |
| 8905 | |
| 8906 | // We collect all encoded fields and order as necessary. |
| 8907 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8908 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8909 | if (RD && !RD->field_empty()) { |
| 8910 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8911 | // so that recursive calls to this RecordType will use it whilst building a |
| 8912 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8913 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8914 | std::string StubEnc(Enc.substr(Start).str()); |
| 8915 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8916 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8917 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8918 | (void) TSC.removeIncomplete(ID); |
| 8919 | return false; |
| 8920 | } |
| 8921 | IsRecursive = TSC.removeIncomplete(ID); |
| 8922 | // The ABI requires unions to be sorted but not structures. |
| 8923 | // See FieldEncoding::operator< for sort algorithm. |
| 8924 | if (RT->isUnionType()) |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8925 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8926 | // We can now complete the TypeString. |
| 8927 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8928 | for (unsigned I = 0; I != E; ++I) { |
| 8929 | if (I) |
| 8930 | Enc += ','; |
| 8931 | Enc += FE[I].str(); |
| 8932 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8933 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8934 | Enc += '}'; |
| 8935 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8936 | return true; |
| 8937 | } |
| 8938 | |
| 8939 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8940 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8941 | TypeStringCache &TSC, |
| 8942 | const IdentifierInfo *ID) { |
| 8943 | // Append the cached TypeString if we have one. |
| 8944 | StringRef TypeString = TSC.lookupStr(ID); |
| 8945 | if (!TypeString.empty()) { |
| 8946 | Enc += TypeString; |
| 8947 | return true; |
| 8948 | } |
| 8949 | |
| 8950 | size_t Start = Enc.size(); |
| 8951 | Enc += "e("; |
| 8952 | if (ID) |
| 8953 | Enc += ID->getName(); |
| 8954 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8955 | |
| 8956 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8957 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8958 | SmallVector<FieldEncoding, 16> FE; |
| 8959 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8960 | ++I) { |
| 8961 | SmallStringEnc EnumEnc; |
| 8962 | EnumEnc += "m("; |
| 8963 | EnumEnc += I->getName(); |
| 8964 | EnumEnc += "){"; |
| 8965 | I->getInitVal().toString(EnumEnc); |
| 8966 | EnumEnc += '}'; |
| 8967 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8968 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8969 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8970 | unsigned E = FE.size(); |
| 8971 | for (unsigned I = 0; I != E; ++I) { |
| 8972 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8973 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8974 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8975 | } |
| 8976 | } |
| 8977 | Enc += '}'; |
| 8978 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8979 | return true; |
| 8980 | } |
| 8981 | |
| 8982 | /// Appends type's qualifier to Enc. |
| 8983 | /// This is done prior to appending the type's encoding. |
| 8984 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8985 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8986 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8987 | int Lookup = 0; |
| 8988 | if (QT.isConstQualified()) |
| 8989 | Lookup += 1<<0; |
| 8990 | if (QT.isRestrictQualified()) |
| 8991 | Lookup += 1<<1; |
| 8992 | if (QT.isVolatileQualified()) |
| 8993 | Lookup += 1<<2; |
| 8994 | Enc += Table[Lookup]; |
| 8995 | } |
| 8996 | |
| 8997 | /// Appends built-in types to Enc. |
| 8998 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8999 | const char *EncType; |
| 9000 | switch (BT->getKind()) { |
| 9001 | case BuiltinType::Void: |
| 9002 | EncType = "0"; |
| 9003 | break; |
| 9004 | case BuiltinType::Bool: |
| 9005 | EncType = "b"; |
| 9006 | break; |
| 9007 | case BuiltinType::Char_U: |
| 9008 | EncType = "uc"; |
| 9009 | break; |
| 9010 | case BuiltinType::UChar: |
| 9011 | EncType = "uc"; |
| 9012 | break; |
| 9013 | case BuiltinType::SChar: |
| 9014 | EncType = "sc"; |
| 9015 | break; |
| 9016 | case BuiltinType::UShort: |
| 9017 | EncType = "us"; |
| 9018 | break; |
| 9019 | case BuiltinType::Short: |
| 9020 | EncType = "ss"; |
| 9021 | break; |
| 9022 | case BuiltinType::UInt: |
| 9023 | EncType = "ui"; |
| 9024 | break; |
| 9025 | case BuiltinType::Int: |
| 9026 | EncType = "si"; |
| 9027 | break; |
| 9028 | case BuiltinType::ULong: |
| 9029 | EncType = "ul"; |
| 9030 | break; |
| 9031 | case BuiltinType::Long: |
| 9032 | EncType = "sl"; |
| 9033 | break; |
| 9034 | case BuiltinType::ULongLong: |
| 9035 | EncType = "ull"; |
| 9036 | break; |
| 9037 | case BuiltinType::LongLong: |
| 9038 | EncType = "sll"; |
| 9039 | break; |
| 9040 | case BuiltinType::Float: |
| 9041 | EncType = "ft"; |
| 9042 | break; |
| 9043 | case BuiltinType::Double: |
| 9044 | EncType = "d"; |
| 9045 | break; |
| 9046 | case BuiltinType::LongDouble: |
| 9047 | EncType = "ld"; |
| 9048 | break; |
| 9049 | default: |
| 9050 | return false; |
| 9051 | } |
| 9052 | Enc += EncType; |
| 9053 | return true; |
| 9054 | } |
| 9055 | |
| 9056 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 9057 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 9058 | const CodeGen::CodeGenModule &CGM, |
| 9059 | TypeStringCache &TSC) { |
| 9060 | Enc += "p("; |
| 9061 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 9062 | return false; |
| 9063 | Enc += ')'; |
| 9064 | return true; |
| 9065 | } |
| 9066 | |
| 9067 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9068 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 9069 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9070 | const CodeGen::CodeGenModule &CGM, |
| 9071 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 9072 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 9073 | return false; |
| 9074 | Enc += "a("; |
| 9075 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 9076 | CAT->getSize().toStringUnsigned(Enc); |
| 9077 | else |
| 9078 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 9079 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9080 | // The Qualifiers should be attached to the type rather than the array. |
| 9081 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9082 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 9083 | return false; |
| 9084 | Enc += ')'; |
| 9085 | return true; |
| 9086 | } |
| 9087 | |
| 9088 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 9089 | /// and the arguments. |
| 9090 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 9091 | const CodeGen::CodeGenModule &CGM, |
| 9092 | TypeStringCache &TSC) { |
| 9093 | Enc += "f{"; |
| 9094 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 9095 | return false; |
| 9096 | Enc += "}("; |
| 9097 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 9098 | // N.B. we are only interested in the adjusted param types. |
| 9099 | auto I = FPT->param_type_begin(); |
| 9100 | auto E = FPT->param_type_end(); |
| 9101 | if (I != E) { |
| 9102 | do { |
| 9103 | if (!appendType(Enc, *I, CGM, TSC)) |
| 9104 | return false; |
| 9105 | ++I; |
| 9106 | if (I != E) |
| 9107 | Enc += ','; |
| 9108 | } while (I != E); |
| 9109 | if (FPT->isVariadic()) |
| 9110 | Enc += ",va"; |
| 9111 | } else { |
| 9112 | if (FPT->isVariadic()) |
| 9113 | Enc += "va"; |
| 9114 | else |
| 9115 | Enc += '0'; |
| 9116 | } |
| 9117 | } |
| 9118 | Enc += ')'; |
| 9119 | return true; |
| 9120 | } |
| 9121 | |
| 9122 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 9123 | /// type encodings. |
| 9124 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 9125 | const CodeGen::CodeGenModule &CGM, |
| 9126 | TypeStringCache &TSC) { |
| 9127 | |
| 9128 | QualType QT = QType.getCanonicalType(); |
| 9129 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9130 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 9131 | // The Qualifiers should be attached to the type rather than the array. |
| 9132 | // Thus we don't call appendQualifier() here. |
| 9133 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 9134 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9135 | appendQualifier(Enc, QT); |
| 9136 | |
| 9137 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 9138 | return appendBuiltinType(Enc, BT); |
| 9139 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9140 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 9141 | return appendPointerType(Enc, PT, CGM, TSC); |
| 9142 | |
| 9143 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 9144 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 9145 | |
| 9146 | if (const RecordType *RT = QT->getAsStructureType()) |
| 9147 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9148 | |
| 9149 | if (const RecordType *RT = QT->getAsUnionType()) |
| 9150 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9151 | |
| 9152 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 9153 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 9154 | |
| 9155 | return false; |
| 9156 | } |
| 9157 | |
| 9158 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 9159 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 9160 | if (!D) |
| 9161 | return false; |
| 9162 | |
| 9163 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9164 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 9165 | return false; |
| 9166 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 9167 | } |
| 9168 | |
| 9169 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 9170 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 9171 | return false; |
| 9172 | QualType QT = VD->getType().getCanonicalType(); |
| 9173 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 9174 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9175 | // The Qualifiers should be attached to the type rather than the array. |
| 9176 | // Thus we don't call appendQualifier() here. |
| 9177 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9178 | } |
| 9179 | return appendType(Enc, QT, CGM, TSC); |
| 9180 | } |
| 9181 | return false; |
| 9182 | } |
| 9183 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9184 | //===----------------------------------------------------------------------===// |
| 9185 | // RISCV ABI Implementation |
| 9186 | //===----------------------------------------------------------------------===// |
| 9187 | |
| 9188 | namespace { |
| 9189 | class RISCVABIInfo : public DefaultABIInfo { |
| 9190 | private: |
| 9191 | unsigned XLen; // Size of the integer ('x') registers in bits. |
| 9192 | static const int NumArgGPRs = 8; |
| 9193 | |
| 9194 | public: |
| 9195 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9196 | : DefaultABIInfo(CGT), XLen(XLen) {} |
| 9197 | |
| 9198 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 9199 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 9200 | void computeInfo(CGFunctionInfo &FI) const override; |
| 9201 | |
| 9202 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, |
| 9203 | int &ArgGPRsLeft) const; |
| 9204 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 9205 | |
| 9206 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9207 | QualType Ty) const override; |
| 9208 | |
| 9209 | ABIArgInfo extendType(QualType Ty) const; |
| 9210 | }; |
| 9211 | } // end anonymous namespace |
| 9212 | |
| 9213 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 9214 | QualType RetTy = FI.getReturnType(); |
| 9215 | if (!getCXXABI().classifyReturnType(FI)) |
| 9216 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 9217 | |
| 9218 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 9219 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 9220 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 9221 | // rewrite the argument list and pass indirectly on RV32. |
| 9222 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 9223 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 9224 | |
| 9225 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 9226 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 9227 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 9228 | // different for variadic arguments, we must also track whether we are |
| 9229 | // examining a vararg or not. |
| 9230 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
| 9231 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 9232 | |
| 9233 | int ArgNum = 0; |
| 9234 | for (auto &ArgInfo : FI.arguments()) { |
| 9235 | bool IsFixed = ArgNum < NumFixedArgs; |
| 9236 | ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft); |
| 9237 | ArgNum++; |
| 9238 | } |
| 9239 | } |
| 9240 | |
| 9241 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
| 9242 | int &ArgGPRsLeft) const { |
| 9243 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 9244 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 9245 | |
| 9246 | // Structures with either a non-trivial destructor or a non-trivial |
| 9247 | // copy constructor are always passed indirectly. |
| 9248 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 9249 | if (ArgGPRsLeft) |
| 9250 | ArgGPRsLeft -= 1; |
| 9251 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 9252 | CGCXXABI::RAA_DirectInMemory); |
| 9253 | } |
| 9254 | |
| 9255 | // Ignore empty structs/unions. |
| 9256 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9257 | return ABIArgInfo::getIgnore(); |
| 9258 | |
| 9259 | uint64_t Size = getContext().getTypeSize(Ty); |
| 9260 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 9261 | bool MustUseStack = false; |
| 9262 | // Determine the number of GPRs needed to pass the current argument |
| 9263 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 9264 | // register pairs, so may consume 3 registers. |
| 9265 | int NeededArgGPRs = 1; |
| 9266 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 9267 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 9268 | else if (Size > XLen && Size <= 2 * XLen) |
| 9269 | NeededArgGPRs = 2; |
| 9270 | |
| 9271 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 9272 | MustUseStack = true; |
| 9273 | NeededArgGPRs = ArgGPRsLeft; |
| 9274 | } |
| 9275 | |
| 9276 | ArgGPRsLeft -= NeededArgGPRs; |
| 9277 | |
| 9278 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 9279 | // Treat an enum type as its underlying type. |
| 9280 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 9281 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 9282 | |
| 9283 | // All integral types are promoted to XLen width, unless passed on the |
| 9284 | // stack. |
| 9285 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 9286 | return extendType(Ty); |
| 9287 | } |
| 9288 | |
| 9289 | return ABIArgInfo::getDirect(); |
| 9290 | } |
| 9291 | |
| 9292 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 9293 | // so coerce to integers. |
| 9294 | if (Size <= 2 * XLen) { |
| 9295 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 9296 | |
| 9297 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 9298 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 9299 | if (Size <= XLen) { |
| 9300 | return ABIArgInfo::getDirect( |
| 9301 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 9302 | } else if (Alignment == 2 * XLen) { |
| 9303 | return ABIArgInfo::getDirect( |
| 9304 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 9305 | } else { |
| 9306 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 9307 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 9308 | } |
| 9309 | } |
| 9310 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 9311 | } |
| 9312 | |
| 9313 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 9314 | if (RetTy->isVoidType()) |
| 9315 | return ABIArgInfo::getIgnore(); |
| 9316 | |
| 9317 | int ArgGPRsLeft = 2; |
| 9318 | |
| 9319 | // The rules for return and argument types are the same, so defer to |
| 9320 | // classifyArgumentType. |
| 9321 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft); |
| 9322 | } |
| 9323 | |
| 9324 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9325 | QualType Ty) const { |
| 9326 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 9327 | |
| 9328 | // Empty records are ignored for parameter passing purposes. |
| 9329 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 9330 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 9331 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 9332 | return Addr; |
| 9333 | } |
| 9334 | |
| 9335 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 9336 | getContext().getTypeInfoInChars(Ty); |
| 9337 | |
| 9338 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 9339 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 9340 | |
| 9341 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 9342 | SlotSize, /*AllowHigherAlign=*/true); |
| 9343 | } |
| 9344 | |
| 9345 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 9346 | int TySize = getContext().getTypeSize(Ty); |
| 9347 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 9348 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 9349 | return ABIArgInfo::getSignExtend(Ty); |
| 9350 | return ABIArgInfo::getExtend(Ty); |
| 9351 | } |
| 9352 | |
| 9353 | namespace { |
| 9354 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 9355 | public: |
| 9356 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9357 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen)) {} |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 9358 | |
| 9359 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 9360 | CodeGen::CodeGenModule &CGM) const override { |
| 9361 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 9362 | if (!FD) return; |
| 9363 | |
| 9364 | const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); |
| 9365 | if (!Attr) |
| 9366 | return; |
| 9367 | |
| 9368 | const char *Kind; |
| 9369 | switch (Attr->getInterrupt()) { |
| 9370 | case RISCVInterruptAttr::user: Kind = "user"; break; |
| 9371 | case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; |
| 9372 | case RISCVInterruptAttr::machine: Kind = "machine"; break; |
| 9373 | } |
| 9374 | |
| 9375 | auto *Fn = cast<llvm::Function>(GV); |
| 9376 | |
| 9377 | Fn->addFnAttr("interrupt", Kind); |
| 9378 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9379 | }; |
| 9380 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9381 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9382 | //===----------------------------------------------------------------------===// |
| 9383 | // Driver code |
| 9384 | //===----------------------------------------------------------------------===// |
| 9385 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9386 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 9387 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9388 | } |
| 9389 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 9390 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9391 | if (TheTargetCodeGenInfo) |
| 9392 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9393 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9394 | // Helper to set the unique_ptr while still keeping the return value. |
| 9395 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 9396 | this->TheTargetCodeGenInfo.reset(P); |
| 9397 | return *P; |
| 9398 | }; |
| 9399 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 9400 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 9401 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9402 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9403 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9404 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 9405 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9406 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 9407 | case llvm::Triple::mips: |
| 9408 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 9409 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9410 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 9411 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9412 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 9413 | case llvm::Triple::mips64: |
| 9414 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9415 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9416 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 9417 | case llvm::Triple::avr: |
| 9418 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 9419 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 9420 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 9421 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9422 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 9423 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9424 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 9425 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 9426 | return SetCGInfo( |
| 9427 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9428 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9429 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9430 | } |
| 9431 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9432 | case llvm::Triple::wasm32: |
| 9433 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9434 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9435 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9436 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 9437 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9438 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9439 | case llvm::Triple::thumbeb: { |
| 9440 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9441 | return SetCGInfo( |
| 9442 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9443 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9444 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9445 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9446 | StringRef ABIStr = getTarget().getABI(); |
| 9447 | if (ABIStr == "apcs-gnu") |
| 9448 | Kind = ARMABIInfo::APCS; |
| 9449 | else if (ABIStr == "aapcs16") |
| 9450 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9451 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9452 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9453 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9454 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9455 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9456 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9457 | |
| 9458 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9459 | } |
| 9460 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9461 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9462 | return SetCGInfo( |
| 9463 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9464 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9465 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9466 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9467 | if (getTarget().getABI() == "elfv2") |
| 9468 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9469 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9470 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9471 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9472 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9473 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9474 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9475 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9476 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9477 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9478 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9479 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9480 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9481 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9482 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9483 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9484 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9485 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9486 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9487 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9488 | case llvm::Triple::nvptx: |
| 9489 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9490 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9491 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9492 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9493 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9494 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9495 | case llvm::Triple::riscv32: |
| 9496 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 32)); |
| 9497 | case llvm::Triple::riscv64: |
| 9498 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 64)); |
| 9499 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9500 | case llvm::Triple::systemz: { |
| 9501 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9502 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9503 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9504 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9505 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9506 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9507 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9508 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9509 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9510 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9511 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9512 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9513 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9514 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9515 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9516 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9517 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9518 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9519 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9520 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9521 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9522 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 9523 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9524 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9525 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9526 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9527 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9528 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9529 | X86AVXABILevel AVXLevel = |
| 9530 | (ABI == "avx512" |
| 9531 | ? X86AVXABILevel::AVX512 |
| 9532 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9533 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9534 | switch (Triple.getOS()) { |
| 9535 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9536 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9537 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9538 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9539 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9540 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9541 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9542 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9543 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9544 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9545 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9546 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9547 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9548 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9549 | case llvm::Triple::sparc: |
| 9550 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9551 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9552 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9553 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9554 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 9555 | case llvm::Triple::arc: |
| 9556 | return SetCGInfo(new ARCTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9557 | case llvm::Triple::spir: |
| 9558 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9559 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9560 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9561 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9562 | |
| 9563 | /// Create an OpenCL kernel for an enqueued block. |
| 9564 | /// |
| 9565 | /// The kernel has the same function type as the block invoke function. Its |
| 9566 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9567 | /// It simply calls the block invoke function then returns. |
| 9568 | llvm::Function * |
| 9569 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9570 | llvm::Function *Invoke, |
| 9571 | llvm::Value *BlockLiteral) const { |
| 9572 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9573 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9574 | for (auto &P : InvokeFT->params()) |
| 9575 | ArgTys.push_back(P); |
| 9576 | auto &C = CGF.getLLVMContext(); |
| 9577 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9578 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9579 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9580 | &CGF.CGM.getModule()); |
| 9581 | auto IP = CGF.Builder.saveIP(); |
| 9582 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9583 | auto &Builder = CGF.Builder; |
| 9584 | Builder.SetInsertPoint(BB); |
| 9585 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9586 | for (auto &A : F->args()) |
| 9587 | Args.push_back(&A); |
| 9588 | Builder.CreateCall(Invoke, Args); |
| 9589 | Builder.CreateRetVoid(); |
| 9590 | Builder.restoreIP(IP); |
| 9591 | return F; |
| 9592 | } |
| 9593 | |
| 9594 | /// Create an OpenCL kernel for an enqueued block. |
| 9595 | /// |
| 9596 | /// The type of the first argument (the block literal) is the struct type |
| 9597 | /// of the block literal instead of a pointer type. The first argument |
| 9598 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9599 | /// allocates the same type of struct on stack and stores the block literal |
| 9600 | /// to it and passes its pointer to the block invoke function. The kernel |
| 9601 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 9602 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 9603 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 9604 | llvm::Value *BlockLiteral) const { |
| 9605 | auto &Builder = CGF.Builder; |
| 9606 | auto &C = CGF.getLLVMContext(); |
| 9607 | |
| 9608 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 9609 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9610 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9611 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 9612 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 9613 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 9614 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 9615 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 9616 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 9617 | |
| 9618 | ArgTys.push_back(BlockTy); |
| 9619 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9620 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 9621 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9622 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9623 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9624 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 9625 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 9626 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9627 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9628 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 9629 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9630 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9631 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9632 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 9633 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9634 | } |
| 9635 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9636 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9637 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9638 | &CGF.CGM.getModule()); |
| 9639 | F->addFnAttr("enqueued-block"); |
| 9640 | auto IP = CGF.Builder.saveIP(); |
| 9641 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9642 | Builder.SetInsertPoint(BB); |
| 9643 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 9644 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
| 9645 | BlockPtr->setAlignment(BlockAlign); |
| 9646 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 9647 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 9648 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9649 | Args.push_back(Cast); |
| 9650 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 9651 | Args.push_back(I); |
| 9652 | Builder.CreateCall(Invoke, Args); |
| 9653 | Builder.CreateRetVoid(); |
| 9654 | Builder.restoreIP(IP); |
| 9655 | |
| 9656 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 9657 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 9658 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 9659 | F->setMetadata("kernel_arg_base_type", |
| 9660 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 9661 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 9662 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 9663 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 9664 | |
| 9665 | return F; |
| 9666 | } |