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); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 312 | llvm::Value *NextPtr = |
| 313 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 314 | "argp.next"); |
| 315 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 316 | |
| 317 | // If the argument is smaller than a slot, and this is a big-endian |
| 318 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 319 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 320 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 321 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 322 | } |
| 323 | |
| 324 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 325 | return Addr; |
| 326 | } |
| 327 | |
| 328 | /// Emit va_arg for a platform using the common void* representation, |
| 329 | /// where arguments are simply emitted in an array of slots on the stack. |
| 330 | /// |
| 331 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 332 | /// \param ValueInfo - The size and alignment of this type, generally |
| 333 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 334 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 335 | /// Each argument will be allocated to a multiple of this number of |
| 336 | /// slots, and all the slots will be aligned to this value. |
| 337 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 338 | /// an argument type with an alignment greater than the slot size |
| 339 | /// will be emitted on a higher-alignment address, potentially |
| 340 | /// leaving one or more empty slots behind as padding. |
| 341 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 342 | QualType ValueTy, bool IsIndirect, |
| 343 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 344 | CharUnits SlotSizeAndAlign, |
| 345 | bool AllowHigherAlign) { |
| 346 | // The size and alignment of the value that was passed directly. |
| 347 | CharUnits DirectSize, DirectAlign; |
| 348 | if (IsIndirect) { |
| 349 | DirectSize = CGF.getPointerSize(); |
| 350 | DirectAlign = CGF.getPointerAlign(); |
| 351 | } else { |
| 352 | DirectSize = ValueInfo.first; |
| 353 | DirectAlign = ValueInfo.second; |
| 354 | } |
| 355 | |
| 356 | // Cast the address we've calculated to the right type. |
| 357 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 358 | if (IsIndirect) |
| 359 | DirectTy = DirectTy->getPointerTo(0); |
| 360 | |
| 361 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 362 | DirectSize, DirectAlign, |
| 363 | SlotSizeAndAlign, |
| 364 | AllowHigherAlign); |
| 365 | |
| 366 | if (IsIndirect) { |
| 367 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 368 | } |
| 369 | |
| 370 | return Addr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 371 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 375 | Address Addr1, llvm::BasicBlock *Block1, |
| 376 | Address Addr2, llvm::BasicBlock *Block2, |
| 377 | const llvm::Twine &Name = "") { |
| 378 | assert(Addr1.getType() == Addr2.getType()); |
| 379 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 380 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 381 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 382 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 383 | return Address(PHI, Align); |
| 384 | } |
| 385 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 386 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 387 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 388 | // If someone can figure out a general rule for this, that would be great. |
| 389 | // It's probably just doomed to be platform-dependent, though. |
| 390 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 391 | // Verified for: |
| 392 | // x86-64 FreeBSD, Linux, Darwin |
| 393 | // x86-32 FreeBSD, Linux, Darwin |
| 394 | // PowerPC Linux, Darwin |
| 395 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 396 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 397 | return 32; |
| 398 | } |
| 399 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 400 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 401 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 402 | // The following conventions are known to require this to be false: |
| 403 | // x86_stdcall |
| 404 | // MIPS |
| 405 | // For everything else, we just prefer false unless we opt out. |
| 406 | return false; |
| 407 | } |
| 408 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 409 | void |
| 410 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 411 | llvm::SmallString<24> &Opt) const { |
| 412 | // This assumes the user is passing a library name like "rt" instead of a |
| 413 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 414 | // dynamic. |
| 415 | Opt = "-l"; |
| 416 | Opt += Lib; |
| 417 | } |
| 418 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 419 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 420 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 421 | // set with clSetKernelArg(), not as normal sub-functions. |
| 422 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 423 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 424 | // gets one matching argument in the produced kernel function argument |
| 425 | // list to enable feasible implementation of clSetKernelArg() with |
| 426 | // aggregates etc. In case we would use the default C calling conv here, |
| 427 | // clSetKernelArg() might break depending on the target-specific |
| 428 | // conventions; different targets might split structs passed as values |
| 429 | // to multiple function arguments etc. |
| 430 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 431 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 432 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 433 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 434 | llvm::PointerType *T, QualType QT) const { |
| 435 | return llvm::ConstantPointerNull::get(T); |
| 436 | } |
| 437 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 438 | LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 439 | const VarDecl *D) const { |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 440 | assert(!CGM.getLangOpts().OpenCL && |
| 441 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 442 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 443 | return D ? D->getType().getAddressSpace() : LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 446 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 447 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, |
| 448 | LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 449 | // Since target may map different address spaces in AST to the same address |
| 450 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 451 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 452 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 453 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 456 | llvm::Constant * |
| 457 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 458 | LangAS SrcAddr, LangAS DestAddr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 459 | llvm::Type *DestTy) const { |
| 460 | // Since target may map different address spaces in AST to the same address |
| 461 | // space, an address space conversion may end up as a bitcast. |
| 462 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 463 | } |
| 464 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 465 | llvm::SyncScope::ID |
| 466 | TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const { |
| 467 | return C.getOrInsertSyncScopeID(""); /* default sync scope */ |
| 468 | } |
| 469 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 470 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 471 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 472 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 473 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 474 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 475 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 476 | if (FD->isUnnamedBitfield()) |
| 477 | return true; |
| 478 | |
| 479 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 480 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 481 | // Constant arrays of empty records count as empty, strip them off. |
| 482 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 483 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 484 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 485 | if (AT->getSize() == 0) |
| 486 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 487 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 488 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 490 | const RecordType *RT = FT->getAs<RecordType>(); |
| 491 | if (!RT) |
| 492 | return false; |
| 493 | |
| 494 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 495 | // |
| 496 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 497 | // current ABI. |
| 498 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 499 | return false; |
| 500 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 501 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 504 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 505 | /// fields. Note that a structure with a flexible array member is not |
| 506 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 507 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 508 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 509 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 510 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 511 | const RecordDecl *RD = RT->getDecl(); |
| 512 | if (RD->hasFlexibleArrayMember()) |
| 513 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 514 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 515 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 516 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 517 | for (const auto &I : CXXRD->bases()) |
| 518 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 519 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 520 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 521 | for (const auto *I : RD->fields()) |
| 522 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 523 | return false; |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | /// isSingleElementStruct - Determine if a structure is a "single |
| 528 | /// element struct", i.e. it has exactly one non-empty field or |
| 529 | /// exactly one field which is itself a single element |
| 530 | /// struct. Structures with flexible array members are never |
| 531 | /// considered single element structs. |
| 532 | /// |
| 533 | /// \return The field declaration for the single non-empty field, if |
| 534 | /// it exists. |
| 535 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 536 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 537 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 538 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 539 | |
| 540 | const RecordDecl *RD = RT->getDecl(); |
| 541 | if (RD->hasFlexibleArrayMember()) |
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 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 544 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 545 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 546 | // If this is a C++ record, check the bases first. |
| 547 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 548 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 549 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 550 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 551 | continue; |
| 552 | |
| 553 | // If we already found an element then this isn't a single-element struct. |
| 554 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 555 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 556 | |
| 557 | // If this is non-empty and not a single element struct, the composite |
| 558 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 559 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 560 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 561 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 566 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 567 | QualType FT = FD->getType(); |
| 568 | |
| 569 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 570 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 571 | continue; |
| 572 | |
| 573 | // If we already found an element then this isn't a single-element |
| 574 | // struct. |
| 575 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 576 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 577 | |
| 578 | // Treat single element arrays as the element. |
| 579 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 580 | if (AT->getSize().getZExtValue() != 1) |
| 581 | break; |
| 582 | FT = AT->getElementType(); |
| 583 | } |
| 584 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 585 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 586 | Found = FT.getTypePtr(); |
| 587 | } else { |
| 588 | Found = isSingleElementStruct(FT, Context); |
| 589 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 590 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 594 | // We don't consider a struct a single-element struct if it has |
| 595 | // padding beyond the element type. |
| 596 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 597 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 598 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 599 | return Found; |
| 600 | } |
| 601 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 602 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 603 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 604 | const ABIArgInfo &AI) { |
| 605 | // This default implementation defers to the llvm backend's va_arg |
| 606 | // instruction. It can handle only passing arguments directly |
| 607 | // (typically only handled in the backend for primitive types), or |
| 608 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 609 | // flag has ABI impact in the callee, this implementation cannot |
| 610 | // work.) |
| 611 | |
| 612 | // Only a few cases are covered here at the moment -- those needed |
| 613 | // by the default abi. |
| 614 | llvm::Value *Val; |
| 615 | |
| 616 | if (AI.isIndirect()) { |
| 617 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 618 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 619 | assert( |
| 620 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 621 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 622 | |
| 623 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 624 | CharUnits TyAlignForABI = TyInfo.second; |
| 625 | |
| 626 | llvm::Type *BaseTy = |
| 627 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 628 | llvm::Value *Addr = |
| 629 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 630 | return Address(Addr, TyAlignForABI); |
| 631 | } else { |
| 632 | assert((AI.isDirect() || AI.isExtend()) && |
| 633 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 634 | |
| 635 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 636 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 637 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 638 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 639 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 640 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 641 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 642 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 643 | |
| 644 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 645 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 646 | CGF.Builder.CreateStore(Val, Temp); |
| 647 | return Temp; |
| 648 | } |
| 649 | } |
| 650 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 651 | /// DefaultABIInfo - The default implementation for ABI specific |
| 652 | /// details. This implementation provides information which results in |
| 653 | /// self-consistent and sensible LLVM IR generation, but does not |
| 654 | /// conform to any particular ABI. |
| 655 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 656 | public: |
| 657 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 659 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 660 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 661 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 662 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 663 | if (!getCXXABI().classifyReturnType(FI)) |
| 664 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 665 | for (auto &I : FI.arguments()) |
| 666 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 667 | } |
| 668 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 669 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 670 | QualType Ty) const override { |
| 671 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 672 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 673 | }; |
| 674 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 675 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 676 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 677 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 678 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 679 | }; |
| 680 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 681 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 682 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 683 | |
| 684 | if (isAggregateTypeForABI(Ty)) { |
| 685 | // Records with non-trivial destructors/copy-constructors should not be |
| 686 | // passed by value. |
| 687 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 688 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 689 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 690 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 691 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 693 | // Treat an enum type as its underlying type. |
| 694 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 695 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 696 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 697 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 698 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 701 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 702 | if (RetTy->isVoidType()) |
| 703 | return ABIArgInfo::getIgnore(); |
| 704 | |
| 705 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 706 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 707 | |
| 708 | // Treat an enum type as its underlying type. |
| 709 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 710 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 711 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 712 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 713 | : ABIArgInfo::getDirect()); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 716 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 717 | // WebAssembly ABI Implementation |
| 718 | // |
| 719 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 720 | //===----------------------------------------------------------------------===// |
| 721 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 722 | class WebAssemblyABIInfo final : public SwiftABIInfo { |
| 723 | DefaultABIInfo defaultInfo; |
| 724 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 725 | public: |
| 726 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 727 | : SwiftABIInfo(CGT), defaultInfo(CGT) {} |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 728 | |
| 729 | private: |
| 730 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 731 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 732 | |
| 733 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 734 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 735 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 736 | void computeInfo(CGFunctionInfo &FI) const override { |
| 737 | if (!getCXXABI().classifyReturnType(FI)) |
| 738 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 739 | for (auto &Arg : FI.arguments()) |
| 740 | Arg.info = classifyArgumentType(Arg.type); |
| 741 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 742 | |
| 743 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 744 | QualType Ty) const override; |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 745 | |
| 746 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 747 | bool asReturnValue) const override { |
| 748 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 749 | } |
| 750 | |
| 751 | bool isSwiftErrorInRegister() const override { |
| 752 | return false; |
| 753 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 754 | }; |
| 755 | |
| 756 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 757 | public: |
| 758 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 759 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 760 | |
| 761 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 762 | CodeGen::CodeGenModule &CGM) const override { |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 763 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 764 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 765 | if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) { |
| 766 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 767 | llvm::AttrBuilder B; |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame^] | 768 | B.addAttribute("wasm-import-module", Attr->getImportModule()); |
| 769 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 770 | } |
| 771 | if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) { |
| 772 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 773 | llvm::AttrBuilder B; |
| 774 | B.addAttribute("wasm-import-name", Attr->getImportName()); |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 775 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 776 | } |
| 777 | } |
| 778 | |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 779 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 780 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 781 | if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) |
| 782 | Fn->addFnAttr("no-prototype"); |
| 783 | } |
| 784 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 787 | /// Classify argument of given type \p Ty. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 788 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 789 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 790 | |
| 791 | if (isAggregateTypeForABI(Ty)) { |
| 792 | // Records with non-trivial destructors/copy-constructors should not be |
| 793 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 794 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 795 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 796 | // Ignore empty structs/unions. |
| 797 | if (isEmptyRecord(getContext(), Ty, true)) |
| 798 | return ABIArgInfo::getIgnore(); |
| 799 | // Lower single-element structs to just pass a regular value. TODO: We |
| 800 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 801 | // though watch out for things like bitfields. |
| 802 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 803 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 807 | return defaultInfo.classifyArgumentType(Ty); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 811 | if (isAggregateTypeForABI(RetTy)) { |
| 812 | // Records with non-trivial destructors/copy-constructors should not be |
| 813 | // returned by value. |
| 814 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 815 | // Ignore empty structs/unions. |
| 816 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 817 | return ABIArgInfo::getIgnore(); |
| 818 | // Lower single-element structs to just return a regular value. TODO: We |
| 819 | // could do reasonable-size multiple-element structs too, using |
| 820 | // ABIArgInfo::getDirect(). |
| 821 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 822 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 827 | return defaultInfo.classifyReturnType(RetTy); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 830 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 831 | QualType Ty) const { |
| 832 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 833 | getContext().getTypeInfoInChars(Ty), |
| 834 | CharUnits::fromQuantity(4), |
| 835 | /*AllowHigherAlign=*/ true); |
| 836 | } |
| 837 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 838 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 839 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 840 | // |
| 841 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 842 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 843 | //===----------------------------------------------------------------------===// |
| 844 | |
| 845 | class PNaClABIInfo : public ABIInfo { |
| 846 | public: |
| 847 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 848 | |
| 849 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 850 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 851 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 852 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 853 | Address EmitVAArg(CodeGenFunction &CGF, |
| 854 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 855 | }; |
| 856 | |
| 857 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 858 | public: |
| 859 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 860 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 861 | }; |
| 862 | |
| 863 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 864 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 865 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 866 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 867 | for (auto &I : FI.arguments()) |
| 868 | I.info = classifyArgumentType(I.type); |
| 869 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 870 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 871 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 872 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 873 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 874 | // function classification. Structs get passed directly for varargs |
| 875 | // functions, through a rewriting transform in |
| 876 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 877 | // this target to actually support a va_arg instructions with an |
| 878 | // aggregate type, unlike other targets. |
| 879 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 882 | /// Classify argument of given type \p Ty. |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 883 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 884 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 885 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 886 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 887 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 888 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 889 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 890 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 891 | } else if (Ty->isFloatingType()) { |
| 892 | // Floating-point types don't go inreg. |
| 893 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 894 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 895 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 896 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 897 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 901 | if (RetTy->isVoidType()) |
| 902 | return ABIArgInfo::getIgnore(); |
| 903 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 904 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 905 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 906 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 907 | |
| 908 | // Treat an enum type as its underlying type. |
| 909 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 910 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 911 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 912 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 913 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 916 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 917 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 918 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 919 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 920 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 921 | IRType->getScalarSizeInBits() != 64; |
| 922 | } |
| 923 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 924 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 925 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 926 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 927 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 928 | .Cases("y", "&y", "^Ym", true) |
| 929 | .Default(false); |
| 930 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 931 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 932 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 933 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 936 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 940 | return Ty; |
| 941 | } |
| 942 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 943 | /// Returns true if this type can be passed in SSE registers with the |
| 944 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 945 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 946 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 947 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 948 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 949 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 950 | &llvm::APFloat::x87DoubleExtended()) |
| 951 | return false; |
| 952 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 953 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 954 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 955 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 956 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 957 | // registers specially. |
| 958 | unsigned VecSize = Context.getTypeSize(VT); |
| 959 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 960 | return true; |
| 961 | } |
| 962 | return false; |
| 963 | } |
| 964 | |
| 965 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 966 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 967 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 968 | return NumMembers <= 4; |
| 969 | } |
| 970 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 971 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 972 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 973 | auto AI = ABIArgInfo::getDirect(T); |
| 974 | AI.setInReg(true); |
| 975 | AI.setCanBeFlattened(false); |
| 976 | return AI; |
| 977 | } |
| 978 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 979 | //===----------------------------------------------------------------------===// |
| 980 | // X86-32 ABI Implementation |
| 981 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 982 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 983 | /// Similar to llvm::CCState, but for Clang. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 984 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 985 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 986 | |
| 987 | unsigned CC; |
| 988 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 989 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 990 | }; |
| 991 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 992 | enum { |
| 993 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 994 | VectorcallMaxParamNumAsReg = 6 |
| 995 | }; |
| 996 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 997 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 998 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 999 | enum Class { |
| 1000 | Integer, |
| 1001 | Float |
| 1002 | }; |
| 1003 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1004 | static const unsigned MinABIStackAlignInBytes = 4; |
| 1005 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1006 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1007 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1008 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1009 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1010 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1011 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1012 | |
| 1013 | static bool isRegisterSize(unsigned Size) { |
| 1014 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 1015 | } |
| 1016 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1017 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1018 | // FIXME: Assumes vectorcall is in use. |
| 1019 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1020 | } |
| 1021 | |
| 1022 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1023 | uint64_t NumMembers) const override { |
| 1024 | // FIXME: Assumes vectorcall is in use. |
| 1025 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1026 | } |
| 1027 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1028 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1029 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1030 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 1031 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1032 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 1033 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1034 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1035 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1036 | /// Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1037 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1038 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1039 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1040 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1041 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1042 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1043 | /// Updates the number of available free registers, returns |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1044 | /// true if any registers were allocated. |
| 1045 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 1046 | |
| 1047 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1048 | bool &NeedsPadding) const; |
| 1049 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1050 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1051 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1052 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1053 | /// Rewrite the function info so that all memory arguments use |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1054 | /// inalloca. |
| 1055 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1056 | |
| 1057 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1058 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1059 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1060 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1061 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1062 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1063 | public: |
| 1064 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1065 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1066 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1067 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1068 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1069 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1070 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1071 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1072 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1073 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1074 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1075 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1076 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1077 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1078 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1079 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1080 | bool asReturnValue) const override { |
| 1081 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1082 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1083 | // four vector registers for vectors, but those can overlap with the |
| 1084 | // scalar registers. |
| 1085 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1086 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1087 | |
| 1088 | bool isSwiftErrorInRegister() const override { |
| 1089 | // x86-32 lowering does not support passing swifterror in a register. |
| 1090 | return false; |
| 1091 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1092 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1093 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1094 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1095 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1096 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1097 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1098 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1099 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1100 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1101 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1102 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1103 | static bool isStructReturnInRegABI( |
| 1104 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1105 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1106 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1107 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1108 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1109 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1110 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1111 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1112 | return 4; |
| 1113 | } |
| 1114 | |
| 1115 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1116 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1117 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1118 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1119 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1120 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1121 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1122 | } |
| 1123 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1124 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1125 | std::string &Constraints, |
| 1126 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1127 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1128 | std::vector<LValue> &ResultRegDests, |
| 1129 | std::string &AsmString, |
| 1130 | unsigned NumOutputs) const override; |
| 1131 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1132 | llvm::Constant * |
| 1133 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1134 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1135 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1136 | ('v' << 16) | |
| 1137 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1138 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1139 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1140 | |
| 1141 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1142 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1143 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1144 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1145 | }; |
| 1146 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1147 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1148 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1149 | /// Rewrite input constraint references after adding some output constraints. |
| 1150 | /// In the case where there is one output and one input and we add one output, |
| 1151 | /// we need to replace all operand references greater than or equal to 1: |
| 1152 | /// mov $0, $1 |
| 1153 | /// mov eax, $1 |
| 1154 | /// The result will be: |
| 1155 | /// mov $0, $2 |
| 1156 | /// mov eax, $2 |
| 1157 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1158 | unsigned NumNewOuts, |
| 1159 | std::string &AsmString) { |
| 1160 | std::string Buf; |
| 1161 | llvm::raw_string_ostream OS(Buf); |
| 1162 | size_t Pos = 0; |
| 1163 | while (Pos < AsmString.size()) { |
| 1164 | size_t DollarStart = AsmString.find('$', Pos); |
| 1165 | if (DollarStart == std::string::npos) |
| 1166 | DollarStart = AsmString.size(); |
| 1167 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1168 | if (DollarEnd == std::string::npos) |
| 1169 | DollarEnd = AsmString.size(); |
| 1170 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1171 | Pos = DollarEnd; |
| 1172 | size_t NumDollars = DollarEnd - DollarStart; |
| 1173 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1174 | // We have an operand reference. |
| 1175 | size_t DigitStart = Pos; |
| 1176 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1177 | if (DigitEnd == std::string::npos) |
| 1178 | DigitEnd = AsmString.size(); |
| 1179 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1180 | unsigned OperandIndex; |
| 1181 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1182 | if (OperandIndex >= FirstIn) |
| 1183 | OperandIndex += NumNewOuts; |
| 1184 | OS << OperandIndex; |
| 1185 | } else { |
| 1186 | OS << OperandStr; |
| 1187 | } |
| 1188 | Pos = DigitEnd; |
| 1189 | } |
| 1190 | } |
| 1191 | AsmString = std::move(OS.str()); |
| 1192 | } |
| 1193 | |
| 1194 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1195 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1196 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1197 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1198 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1199 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1200 | unsigned NumOutputs) const { |
| 1201 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1202 | |
| 1203 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1204 | // larger. |
| 1205 | if (!Constraints.empty()) |
| 1206 | Constraints += ','; |
| 1207 | if (RetWidth <= 32) { |
| 1208 | Constraints += "={eax}"; |
| 1209 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1210 | } else { |
| 1211 | // Use the 'A' constraint for EAX:EDX. |
| 1212 | Constraints += "=A"; |
| 1213 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1214 | } |
| 1215 | |
| 1216 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1217 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1218 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1219 | |
| 1220 | // Coerce the integer by bitcasting the return slot pointer. |
| 1221 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1222 | CoerceTy->getPointerTo())); |
| 1223 | ResultRegDests.push_back(ReturnSlot); |
| 1224 | |
| 1225 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1226 | } |
| 1227 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1228 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1229 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1230 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1231 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1232 | uint64_t Size = Context.getTypeSize(Ty); |
| 1233 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1234 | // For i386, type must be register sized. |
| 1235 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1236 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1237 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1238 | |
| 1239 | if (Ty->isVectorType()) { |
| 1240 | // 64- and 128- bit vectors inside structures are not returned in |
| 1241 | // registers. |
| 1242 | if (Size == 64 || Size == 128) |
| 1243 | return false; |
| 1244 | |
| 1245 | return true; |
| 1246 | } |
| 1247 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1248 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1249 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1250 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1251 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1252 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1253 | return true; |
| 1254 | |
| 1255 | // Arrays are treated like records. |
| 1256 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1257 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1260 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1261 | if (!RT) return false; |
| 1262 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1263 | // FIXME: Traverse bases here too. |
| 1264 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1265 | // Structure types are passed in register if all fields would be |
| 1266 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1267 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1268 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1269 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1270 | continue; |
| 1271 | |
| 1272 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1273 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1274 | return false; |
| 1275 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1276 | return true; |
| 1277 | } |
| 1278 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1279 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1280 | // Treat complex types as the element type. |
| 1281 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1282 | Ty = CTy->getElementType(); |
| 1283 | |
| 1284 | // Check for a type which we know has a simple scalar argument-passing |
| 1285 | // convention without any padding. (We're specifically looking for 32 |
| 1286 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1287 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1288 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1289 | return false; |
| 1290 | |
| 1291 | uint64_t Size = Context.getTypeSize(Ty); |
| 1292 | return Size == 32 || Size == 64; |
| 1293 | } |
| 1294 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1295 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1296 | uint64_t &Size) { |
| 1297 | for (const auto *FD : RD->fields()) { |
| 1298 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1299 | // argument is smaller than 32-bits, expanding the struct will create |
| 1300 | // alignment padding. |
| 1301 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1302 | return false; |
| 1303 | |
| 1304 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1305 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1306 | // counts as "basic" is more complicated than what we were doing previously. |
| 1307 | if (FD->isBitField()) |
| 1308 | return false; |
| 1309 | |
| 1310 | Size += Context.getTypeSize(FD->getType()); |
| 1311 | } |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
| 1315 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1316 | uint64_t &Size) { |
| 1317 | // Don't do this if there are any non-empty bases. |
| 1318 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1319 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1320 | Size)) |
| 1321 | return false; |
| 1322 | } |
| 1323 | if (!addFieldSizes(Context, RD, Size)) |
| 1324 | return false; |
| 1325 | return true; |
| 1326 | } |
| 1327 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1328 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1329 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1330 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1331 | /// optimizations. |
| 1332 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1333 | // We can only expand structure types. |
| 1334 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1335 | if (!RT) |
| 1336 | return false; |
| 1337 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1338 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1339 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1340 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1341 | // On non-Windows, we have to conservatively match our old bitcode |
| 1342 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1343 | if (!CXXRD->isCLike()) |
| 1344 | return false; |
| 1345 | } else { |
| 1346 | // Don't do this for dynamic classes. |
| 1347 | if (CXXRD->isDynamicClass()) |
| 1348 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1349 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1350 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1351 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1352 | } else { |
| 1353 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1354 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | // We can do this if there was no alignment padding. |
| 1358 | return Size == getContext().getTypeSize(Ty); |
| 1359 | } |
| 1360 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1361 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1362 | // If the return value is indirect, then the hidden argument is consuming one |
| 1363 | // integer register. |
| 1364 | if (State.FreeRegs) { |
| 1365 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1366 | if (!IsMCUABI) |
| 1367 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1368 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1369 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1372 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1373 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1374 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1375 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1376 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1377 | const Type *Base = nullptr; |
| 1378 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1379 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1380 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1381 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1382 | // The LLVM struct type for such an aggregate should lower properly. |
| 1383 | return ABIArgInfo::getDirect(); |
| 1384 | } |
| 1385 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1386 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1387 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1388 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1389 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1390 | |
| 1391 | // 128-bit vectors are a special case; they are returned in |
| 1392 | // registers and we need to make sure to pick a type the LLVM |
| 1393 | // backend will like. |
| 1394 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1395 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1396 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Always return in register if it fits in a general purpose |
| 1399 | // register, or if it is 64 bits and has a single element. |
| 1400 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1401 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1402 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1403 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1404 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1405 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1409 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1410 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1411 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1412 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1413 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1414 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1415 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1416 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1417 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1418 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1419 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1420 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1421 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1422 | // Ignore empty structs/unions. |
| 1423 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1424 | return ABIArgInfo::getIgnore(); |
| 1425 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1426 | // Small structures which are register sized are generally returned |
| 1427 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1428 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1429 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1430 | |
| 1431 | // As a special-case, if the struct is a "single-element" struct, and |
| 1432 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1433 | // floating-point register. (MSVC does not apply this special case.) |
| 1434 | // We apply a similar transformation for pointer types to improve the |
| 1435 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1436 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1437 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1438 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1439 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1440 | |
| 1441 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1442 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1443 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1446 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1447 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1449 | // Treat an enum type as its underlying type. |
| 1450 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1451 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1452 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1453 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1454 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1457 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1458 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1459 | } |
| 1460 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1461 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1462 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1463 | if (!RT) |
| 1464 | return 0; |
| 1465 | const RecordDecl *RD = RT->getDecl(); |
| 1466 | |
| 1467 | // If this is a C++ record, check the bases first. |
| 1468 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1469 | for (const auto &I : CXXRD->bases()) |
| 1470 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1471 | return false; |
| 1472 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1473 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1474 | QualType FT = i->getType(); |
| 1475 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1476 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1477 | return true; |
| 1478 | |
| 1479 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1480 | return true; |
| 1481 | } |
| 1482 | |
| 1483 | return false; |
| 1484 | } |
| 1485 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1486 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1487 | unsigned Align) const { |
| 1488 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1489 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1490 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1491 | return 0; // Use default alignment. |
| 1492 | |
| 1493 | // On non-Darwin, the stack type alignment is always 4. |
| 1494 | if (!IsDarwinVectorABI) { |
| 1495 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1496 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1497 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1498 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1499 | // 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] | 1500 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1501 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1502 | return 16; |
| 1503 | |
| 1504 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1507 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1508 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1509 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1510 | if (State.FreeRegs) { |
| 1511 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1512 | if (!IsMCUABI) |
| 1513 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1514 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1515 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1516 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1517 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1518 | // Compute the byval alignment. |
| 1519 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1520 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1521 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1522 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1523 | |
| 1524 | // If the stack alignment is less than the type alignment, realign the |
| 1525 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1526 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1527 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1528 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1531 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1532 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1533 | if (!T) |
| 1534 | T = Ty.getTypePtr(); |
| 1535 | |
| 1536 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1537 | BuiltinType::Kind K = BT->getKind(); |
| 1538 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1539 | return Float; |
| 1540 | } |
| 1541 | return Integer; |
| 1542 | } |
| 1543 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1544 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1545 | if (!IsSoftFloatABI) { |
| 1546 | Class C = classify(Ty); |
| 1547 | if (C == Float) |
| 1548 | return false; |
| 1549 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1550 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1551 | unsigned Size = getContext().getTypeSize(Ty); |
| 1552 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1553 | |
| 1554 | if (SizeInRegs == 0) |
| 1555 | return false; |
| 1556 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1557 | if (!IsMCUABI) { |
| 1558 | if (SizeInRegs > State.FreeRegs) { |
| 1559 | State.FreeRegs = 0; |
| 1560 | return false; |
| 1561 | } |
| 1562 | } else { |
| 1563 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1564 | // earlier parameters that are passed on the stack. Also, |
| 1565 | // it does not allow passing >8-byte structs in-register, |
| 1566 | // even if there are 3 free registers available. |
| 1567 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1568 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1569 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1570 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1571 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1572 | return true; |
| 1573 | } |
| 1574 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1575 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1576 | bool &InReg, |
| 1577 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1578 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1579 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1580 | // (HFAs) have already been dealt with at this point. |
| 1581 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1582 | return false; |
| 1583 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1584 | NeedsPadding = false; |
| 1585 | InReg = !IsMCUABI; |
| 1586 | |
| 1587 | if (!updateFreeRegs(Ty, State)) |
| 1588 | return false; |
| 1589 | |
| 1590 | if (IsMCUABI) |
| 1591 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1592 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1593 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1594 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1595 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1596 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1597 | NeedsPadding = true; |
| 1598 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1599 | return false; |
| 1600 | } |
| 1601 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1602 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1605 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1606 | if (!updateFreeRegs(Ty, State)) |
| 1607 | return false; |
| 1608 | |
| 1609 | if (IsMCUABI) |
| 1610 | return false; |
| 1611 | |
| 1612 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1613 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1614 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1615 | if (getContext().getTypeSize(Ty) > 32) |
| 1616 | return false; |
| 1617 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1618 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1619 | Ty->isReferenceType()); |
| 1620 | } |
| 1621 | |
| 1622 | return true; |
| 1623 | } |
| 1624 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1625 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1626 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1627 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1628 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1629 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1630 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1631 | // Check with the C++ ABI first. |
| 1632 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1633 | if (RT) { |
| 1634 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1635 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1636 | return getIndirectResult(Ty, false, State); |
| 1637 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1638 | // The field index doesn't matter, we'll fix it up later. |
| 1639 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1640 | } |
| 1641 | } |
| 1642 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1643 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1644 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1645 | const Type *Base = nullptr; |
| 1646 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1647 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1648 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1649 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1650 | if (State.FreeSSERegs >= NumElts) { |
| 1651 | State.FreeSSERegs -= NumElts; |
| 1652 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1653 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1654 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1655 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1656 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1660 | // Structures with flexible arrays are always indirect. |
| 1661 | // FIXME: This should not be byval! |
| 1662 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1663 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1664 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1665 | // Ignore empty structs/unions on non-Windows. |
| 1666 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1667 | return ABIArgInfo::getIgnore(); |
| 1668 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1669 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1670 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1671 | bool NeedsPadding = false; |
| 1672 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1673 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1674 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1675 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1676 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1677 | if (InReg) |
| 1678 | return ABIArgInfo::getDirectInReg(Result); |
| 1679 | else |
| 1680 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1681 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1682 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1683 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1684 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1685 | // of those arguments will match the struct. This is important because the |
| 1686 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1687 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1688 | // Don't do this for the MCU if there are still free integer registers |
| 1689 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1690 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1691 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1692 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1693 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1694 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1695 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1696 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1697 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1698 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1701 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1702 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1703 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1704 | if (IsDarwinVectorABI) { |
| 1705 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1706 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1707 | (Size == 64 && VT->getNumElements() == 1)) |
| 1708 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1709 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1710 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1711 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1712 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1713 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1714 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1715 | return ABIArgInfo::getDirect(); |
| 1716 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1717 | |
| 1718 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1719 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1720 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1721 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1722 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1723 | |
| 1724 | if (Ty->isPromotableIntegerType()) { |
| 1725 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1726 | return ABIArgInfo::getExtendInReg(Ty); |
| 1727 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1728 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1729 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1730 | if (InReg) |
| 1731 | return ABIArgInfo::getDirectInReg(); |
| 1732 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1735 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1736 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1737 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1738 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1739 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1740 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1741 | // registers. |
| 1742 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1743 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1744 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1745 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1746 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1747 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1748 | // First pass do all the vector types. |
| 1749 | const Type *Base = nullptr; |
| 1750 | uint64_t NumElts = 0; |
| 1751 | const QualType& Ty = I.type; |
| 1752 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1753 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1754 | if (State.FreeSSERegs >= NumElts) { |
| 1755 | State.FreeSSERegs -= NumElts; |
| 1756 | I.info = ABIArgInfo::getDirect(); |
| 1757 | } else { |
| 1758 | I.info = classifyArgumentType(Ty, State); |
| 1759 | } |
| 1760 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1761 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1762 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1763 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1764 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1765 | // Second pass, do the rest! |
| 1766 | const Type *Base = nullptr; |
| 1767 | uint64_t NumElts = 0; |
| 1768 | const QualType& Ty = I.type; |
| 1769 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1770 | |
| 1771 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1772 | // Assign true HVAs (non vector/native FP types). |
| 1773 | if (State.FreeSSERegs >= NumElts) { |
| 1774 | State.FreeSSERegs -= NumElts; |
| 1775 | I.info = getDirectX86Hva(); |
| 1776 | } else { |
| 1777 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1778 | } |
| 1779 | } else if (!IsHva) { |
| 1780 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1781 | I.info = classifyArgumentType(Ty, State); |
| 1782 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1783 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1787 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1788 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1789 | if (IsMCUABI) |
| 1790 | State.FreeRegs = 3; |
| 1791 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1792 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1793 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1794 | State.FreeRegs = 2; |
| 1795 | State.FreeSSERegs = 6; |
| 1796 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1797 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1798 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1799 | State.FreeRegs = 5; |
| 1800 | State.FreeSSERegs = 8; |
| 1801 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1802 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1803 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 1804 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1805 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1806 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1807 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1808 | // return value was sret and put it in a register ourselves if appropriate. |
| 1809 | if (State.FreeRegs) { |
| 1810 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1811 | if (!IsMCUABI) |
| 1812 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1813 | } |
| 1814 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1815 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1816 | // The chain argument effectively gives us another free register. |
| 1817 | if (FI.isChainCall()) |
| 1818 | ++State.FreeRegs; |
| 1819 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1820 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1821 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1822 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1823 | } else { |
| 1824 | // If not vectorcall, revert to normal behavior. |
| 1825 | for (auto &I : FI.arguments()) { |
| 1826 | I.info = classifyArgumentType(I.type, State); |
| 1827 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1828 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1832 | // all the memory arguments to use inalloca. |
| 1833 | if (UsedInAlloca) |
| 1834 | rewriteWithInAlloca(FI); |
| 1835 | } |
| 1836 | |
| 1837 | void |
| 1838 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1839 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1840 | QualType Type) const { |
| 1841 | // Arguments are always 4-byte-aligned. |
| 1842 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1843 | |
| 1844 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1845 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1846 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1847 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1848 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1849 | // Insert padding bytes to respect alignment. |
| 1850 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1851 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1852 | if (StackOffset != FieldEnd) { |
| 1853 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1854 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1855 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1856 | FrameFields.push_back(Ty); |
| 1857 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1860 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1861 | // Leave ignored and inreg arguments alone. |
| 1862 | switch (Info.getKind()) { |
| 1863 | case ABIArgInfo::InAlloca: |
| 1864 | return true; |
| 1865 | case ABIArgInfo::Indirect: |
| 1866 | assert(Info.getIndirectByVal()); |
| 1867 | return true; |
| 1868 | case ABIArgInfo::Ignore: |
| 1869 | return false; |
| 1870 | case ABIArgInfo::Direct: |
| 1871 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1872 | if (Info.getInReg()) |
| 1873 | return false; |
| 1874 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1875 | case ABIArgInfo::Expand: |
| 1876 | case ABIArgInfo::CoerceAndExpand: |
| 1877 | // These are aggregate types which are never passed in registers when |
| 1878 | // inalloca is involved. |
| 1879 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1880 | } |
| 1881 | llvm_unreachable("invalid enum"); |
| 1882 | } |
| 1883 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1884 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1885 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1886 | |
| 1887 | // Build a packed struct type for all of the arguments in memory. |
| 1888 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1889 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1890 | // The stack alignment is always 4. |
| 1891 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1892 | |
| 1893 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1894 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1895 | |
| 1896 | // Put 'this' into the struct before 'sret', if necessary. |
| 1897 | bool IsThisCall = |
| 1898 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1899 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1900 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1901 | isArgInAlloca(I->info)) { |
| 1902 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1903 | ++I; |
| 1904 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1905 | |
| 1906 | // 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] | 1907 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1908 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1909 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1910 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1911 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1915 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1916 | ++I; |
| 1917 | |
| 1918 | // Put arguments passed in memory into the struct. |
| 1919 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1920 | if (isArgInAlloca(I->info)) |
| 1921 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1925 | /*isPacked=*/true), |
| 1926 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1929 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1930 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1931 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1932 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1933 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1934 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1935 | // |
| 1936 | // Just messing with TypeInfo like this works because we never pass |
| 1937 | // anything indirectly. |
| 1938 | TypeInfo.second = CharUnits::fromQuantity( |
| 1939 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1940 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1941 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1942 | TypeInfo, CharUnits::fromQuantity(4), |
| 1943 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1946 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1947 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1948 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1949 | |
| 1950 | switch (Opts.getStructReturnConvention()) { |
| 1951 | case CodeGenOptions::SRCK_Default: |
| 1952 | break; |
| 1953 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1954 | return false; |
| 1955 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1956 | return true; |
| 1957 | } |
| 1958 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1959 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1960 | return true; |
| 1961 | |
| 1962 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1963 | case llvm::Triple::DragonFly: |
| 1964 | case llvm::Triple::FreeBSD: |
| 1965 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1966 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1967 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1968 | default: |
| 1969 | return false; |
| 1970 | } |
| 1971 | } |
| 1972 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1973 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1974 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1975 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1976 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1977 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1978 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1979 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 1980 | Fn->addFnAttr("stackrealign"); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1981 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1982 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1983 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1984 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1985 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1989 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1990 | CodeGen::CodeGenFunction &CGF, |
| 1991 | llvm::Value *Address) const { |
| 1992 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1994 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1995 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1996 | // 0-7 are the eight integer registers; the order is different |
| 1997 | // on Darwin (for EH), but the range is the same. |
| 1998 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1999 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2000 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2001 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2002 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 2003 | // These have size 16, which is sizeof(long double) on |
| 2004 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2005 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2006 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2007 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2008 | } else { |
| 2009 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 2010 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2011 | Builder.CreateAlignedStore( |
| 2012 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 2013 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2014 | |
| 2015 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 2016 | // These have size 12, which is sizeof(long double) on |
| 2017 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2018 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2019 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 2020 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2021 | |
| 2022 | return false; |
| 2023 | } |
| 2024 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2025 | //===----------------------------------------------------------------------===// |
| 2026 | // X86-64 ABI Implementation |
| 2027 | //===----------------------------------------------------------------------===// |
| 2028 | |
| 2029 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2030 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2031 | /// The AVX ABI level for X86 targets. |
| 2032 | enum class X86AVXABILevel { |
| 2033 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2034 | AVX, |
| 2035 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2036 | }; |
| 2037 | |
| 2038 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2039 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2040 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2041 | case X86AVXABILevel::AVX512: |
| 2042 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2043 | case X86AVXABILevel::AVX: |
| 2044 | return 256; |
| 2045 | case X86AVXABILevel::None: |
| 2046 | return 128; |
| 2047 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2048 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2051 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2052 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2053 | enum Class { |
| 2054 | Integer = 0, |
| 2055 | SSE, |
| 2056 | SSEUp, |
| 2057 | X87, |
| 2058 | X87Up, |
| 2059 | ComplexX87, |
| 2060 | NoClass, |
| 2061 | Memory |
| 2062 | }; |
| 2063 | |
| 2064 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2065 | /// |
| 2066 | /// Merge an accumulating classification \arg Accum with a field |
| 2067 | /// classification \arg Field. |
| 2068 | /// |
| 2069 | /// \param Accum - The accumulating classification. This should |
| 2070 | /// always be either NoClass or the result of a previous merge |
| 2071 | /// call. In addition, this should never be Memory (the caller |
| 2072 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2073 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2074 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2075 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2076 | /// |
| 2077 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2078 | /// final MEMORY or SSE classes when necessary. |
| 2079 | /// |
| 2080 | /// \param AggregateSize - The size of the current aggregate in |
| 2081 | /// the classification process. |
| 2082 | /// |
| 2083 | /// \param Lo - The classification for the parts of the type |
| 2084 | /// residing in the low word of the containing object. |
| 2085 | /// |
| 2086 | /// \param Hi - The classification for the parts of the type |
| 2087 | /// residing in the higher words of the containing object. |
| 2088 | /// |
| 2089 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2090 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2091 | /// classify - Determine the x86_64 register classes in which the |
| 2092 | /// given type T should be passed. |
| 2093 | /// |
| 2094 | /// \param Lo - The classification for the parts of the type |
| 2095 | /// residing in the low word of the containing object. |
| 2096 | /// |
| 2097 | /// \param Hi - The classification for the parts of the type |
| 2098 | /// residing in the high word of the containing object. |
| 2099 | /// |
| 2100 | /// \param OffsetBase - The bit offset of this type in the |
| 2101 | /// containing object. Some parameters are classified different |
| 2102 | /// depending on whether they straddle an eightbyte boundary. |
| 2103 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2104 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2105 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2106 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2107 | /// If a word is unused its result will be NoClass; if a type should |
| 2108 | /// be passed in Memory then at least the classification of \arg Lo |
| 2109 | /// will be Memory. |
| 2110 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2111 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2112 | /// |
| 2113 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2114 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2115 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2116 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2117 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2118 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2119 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2120 | unsigned IROffset, QualType SourceTy, |
| 2121 | unsigned SourceOffset) const; |
| 2122 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2123 | unsigned IROffset, QualType SourceTy, |
| 2124 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2125 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2126 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2127 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2128 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2129 | |
| 2130 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2131 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2132 | /// |
| 2133 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2134 | /// available. |
| 2135 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2137 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2138 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2139 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2140 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2141 | bool isNamedArg) 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 classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2144 | unsigned &NeededSSE) const; |
| 2145 | |
| 2146 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2147 | unsigned &NeededSSE) const; |
| 2148 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2149 | bool IsIllegalVectorType(QualType Ty) const; |
| 2150 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2151 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2152 | /// unfortunately in ways that were not always consistent with |
| 2153 | /// certain previous compilers. In particular, platforms which |
| 2154 | /// required strict binary compatibility with older versions of GCC |
| 2155 | /// may need to exempt themselves. |
| 2156 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2157 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2160 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2161 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2162 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2163 | // Clang <= 3.8 did not do this. |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 2164 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2165 | LangOptions::ClangABI::Ver3_8) |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2166 | return false; |
| 2167 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2168 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2169 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2170 | return false; |
| 2171 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2172 | return false; |
| 2173 | return true; |
| 2174 | } |
| 2175 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2176 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2177 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2178 | // 64-bit hardware. |
| 2179 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2180 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2181 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2182 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2183 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2184 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2185 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2186 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2187 | bool isPassedUsingAVXType(QualType type) const { |
| 2188 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2189 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2190 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2191 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2192 | if (info.isDirect()) { |
| 2193 | llvm::Type *ty = info.getCoerceToType(); |
| 2194 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2195 | return (vectorTy->getBitWidth() > 128); |
| 2196 | } |
| 2197 | return false; |
| 2198 | } |
| 2199 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2200 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2201 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2202 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2203 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2204 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2205 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2206 | |
| 2207 | bool has64BitPointers() const { |
| 2208 | return Has64BitPointers; |
| 2209 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2210 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2211 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2212 | bool asReturnValue) const override { |
| 2213 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2214 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2215 | bool isSwiftErrorInRegister() const override { |
| 2216 | return true; |
| 2217 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2218 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2219 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2220 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2221 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2222 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2223 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2224 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2225 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2226 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2227 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2228 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2229 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2230 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2231 | |
| 2232 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2233 | // FIXME: Assumes vectorcall is in use. |
| 2234 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2235 | } |
| 2236 | |
| 2237 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2238 | uint64_t NumMembers) const override { |
| 2239 | // FIXME: Assumes vectorcall is in use. |
| 2240 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2241 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2242 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2243 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2244 | bool asReturnValue) const override { |
| 2245 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2246 | } |
| 2247 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2248 | bool isSwiftErrorInRegister() const override { |
| 2249 | return true; |
| 2250 | } |
| 2251 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2252 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2253 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2254 | bool IsVectorCall, bool IsRegCall) const; |
| 2255 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2256 | const ABIArgInfo ¤t) const; |
| 2257 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2258 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2259 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2260 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2261 | }; |
| 2262 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2263 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2264 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2265 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2266 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2267 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2268 | const X86_64ABIInfo &getABIInfo() const { |
| 2269 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2270 | } |
| 2271 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2272 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2273 | return 7; |
| 2274 | } |
| 2275 | |
| 2276 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2277 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2278 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2279 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2280 | // 0-15 are the 16 integer registers. |
| 2281 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2282 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2283 | return false; |
| 2284 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2285 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2286 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2287 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2288 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2289 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2290 | } |
| 2291 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2292 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2293 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2294 | // The default CC on x86-64 sets %al to the number of SSA |
| 2295 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2296 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2297 | // that when AVX types are involved: the ABI explicitly states it is |
| 2298 | // undefined, and it doesn't work in practice because of how the ABI |
| 2299 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2300 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2301 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2302 | for (CallArgList::const_iterator |
| 2303 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2304 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2305 | HasAVXType = true; |
| 2306 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2309 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2310 | if (!HasAVXType) |
| 2311 | return true; |
| 2312 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2313 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2314 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2317 | llvm::Constant * |
| 2318 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2319 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2320 | (0x06 << 8) | // .+0x08 |
| 2321 | ('v' << 16) | |
| 2322 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2323 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2324 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2325 | |
| 2326 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2327 | CodeGen::CodeGenModule &CGM) const override { |
| 2328 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2329 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2330 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2331 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2332 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2333 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2334 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2335 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2336 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2337 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2338 | } |
| 2339 | } |
| 2340 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2341 | }; |
| 2342 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2343 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2344 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2345 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2346 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2347 | |
| 2348 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2349 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2350 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2351 | // If the argument contains a space, enclose it in quotes. |
| 2352 | if (Lib.find(" ") != StringRef::npos) |
| 2353 | Opt += "\"" + Lib.str() + "\""; |
| 2354 | else |
| 2355 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2356 | } |
| 2357 | }; |
| 2358 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2359 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2360 | // If the argument does not end in .lib, automatically add the suffix. |
| 2361 | // If the argument contains a space, enclose it in quotes. |
| 2362 | // This matches the behavior of MSVC. |
| 2363 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2364 | std::string ArgStr = Quote ? "\"" : ""; |
| 2365 | ArgStr += Lib; |
Martin Storsjo | 3cd67c9 | 2018-10-10 09:01:00 +0000 | [diff] [blame] | 2366 | if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2367 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2368 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2369 | return ArgStr; |
| 2370 | } |
| 2371 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2372 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2373 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2374 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2375 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2376 | unsigned NumRegisterParameters) |
| 2377 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2378 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2379 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2380 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2381 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2382 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2383 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2384 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2385 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2386 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2387 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2388 | |
| 2389 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2390 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2391 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2392 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2393 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2394 | }; |
| 2395 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2396 | static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2397 | CodeGen::CodeGenModule &CGM) { |
| 2398 | if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2399 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2400 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2401 | Fn->addFnAttr("stack-probe-size", |
| 2402 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2403 | if (CGM.getCodeGenOpts().NoStackArgProbe) |
| 2404 | Fn->addFnAttr("no-stack-arg-probe"); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2408 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2409 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2410 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2411 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2412 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2413 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2416 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2417 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2418 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2419 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2420 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2421 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2422 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2423 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2424 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2425 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2426 | return 7; |
| 2427 | } |
| 2428 | |
| 2429 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2430 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2431 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2432 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2433 | // 0-15 are the 16 integer registers. |
| 2434 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2435 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2436 | return false; |
| 2437 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2438 | |
| 2439 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2440 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2441 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2442 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2443 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2444 | |
| 2445 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2446 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2447 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2448 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2449 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2450 | }; |
| 2451 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2452 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2453 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2454 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2455 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2456 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2457 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2458 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2459 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2460 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2461 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2462 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2463 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2464 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2465 | } |
| 2466 | } |
| 2467 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2468 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2469 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2470 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2471 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2472 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2473 | Class &Hi) const { |
| 2474 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2475 | // |
| 2476 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2477 | // memory. |
| 2478 | // |
| 2479 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2480 | // memory. |
| 2481 | // |
| 2482 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2483 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2484 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2485 | // ABI working for processors that don't support the __m256 type. |
| 2486 | // |
| 2487 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2488 | // |
| 2489 | // Some of these are enforced by the merging logic. Others can arise |
| 2490 | // only with unions; for example: |
| 2491 | // union { _Complex double; unsigned; } |
| 2492 | // |
| 2493 | // Note that clauses (b) and (c) were added in 0.98. |
| 2494 | // |
| 2495 | if (Hi == Memory) |
| 2496 | Lo = Memory; |
| 2497 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2498 | Lo = Memory; |
| 2499 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2500 | Lo = Memory; |
| 2501 | if (Hi == SSEUp && Lo != SSE) |
| 2502 | Hi = SSE; |
| 2503 | } |
| 2504 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2505 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2506 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2507 | // classified recursively so that always two fields are |
| 2508 | // considered. The resulting class is calculated according to |
| 2509 | // the classes of the fields in the eightbyte: |
| 2510 | // |
| 2511 | // (a) If both classes are equal, this is the resulting class. |
| 2512 | // |
| 2513 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2514 | // the other class. |
| 2515 | // |
| 2516 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2517 | // class. |
| 2518 | // |
| 2519 | // (d) If one of the classes is INTEGER, the result is the |
| 2520 | // INTEGER. |
| 2521 | // |
| 2522 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2523 | // MEMORY is used as class. |
| 2524 | // |
| 2525 | // (f) Otherwise class SSE is used. |
| 2526 | |
| 2527 | // Accum should never be memory (we should have returned) or |
| 2528 | // ComplexX87 (because this cannot be passed in a structure). |
| 2529 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2530 | "Invalid accumulated classification during merge."); |
| 2531 | if (Accum == Field || Field == NoClass) |
| 2532 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2533 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2534 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2535 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2536 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2537 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2538 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2539 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2540 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2541 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2542 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2545 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2546 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2547 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2548 | // Class pairs with appropriate constructor methods for the various |
| 2549 | // situations. |
| 2550 | |
| 2551 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2552 | // shouldn't be passed in registers for example, so there is no chance they |
| 2553 | // can straddle an eightbyte. Verify & simplify. |
| 2554 | |
| 2555 | Lo = Hi = NoClass; |
| 2556 | |
| 2557 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2558 | Current = Memory; |
| 2559 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2560 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2561 | BuiltinType::Kind k = BT->getKind(); |
| 2562 | |
| 2563 | if (k == BuiltinType::Void) { |
| 2564 | Current = NoClass; |
| 2565 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2566 | Lo = Integer; |
| 2567 | Hi = Integer; |
| 2568 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2569 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2570 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2571 | Current = SSE; |
| 2572 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2573 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2574 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2575 | Lo = SSE; |
| 2576 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2577 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2578 | Lo = X87; |
| 2579 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2580 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2581 | Current = SSE; |
| 2582 | } else |
| 2583 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2584 | } |
| 2585 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2586 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2587 | return; |
| 2588 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2589 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2590 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2591 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2592 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2593 | return; |
| 2594 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2596 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2597 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2598 | return; |
| 2599 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2601 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2602 | if (Ty->isMemberFunctionPointerType()) { |
| 2603 | if (Has64BitPointers) { |
| 2604 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2605 | // Lo and Hi now. |
| 2606 | Lo = Hi = Integer; |
| 2607 | } else { |
| 2608 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2609 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2610 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2611 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2612 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2613 | Lo = Hi = Integer; |
| 2614 | } else { |
| 2615 | Current = Integer; |
| 2616 | } |
| 2617 | } |
| 2618 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2619 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2620 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2621 | return; |
| 2622 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2623 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2624 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2625 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2626 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2627 | // gcc passes the following as integer: |
| 2628 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2629 | // 2 bytes - <2 x char>, <1 x short> |
| 2630 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2631 | Current = Integer; |
| 2632 | |
| 2633 | // If this type crosses an eightbyte boundary, it should be |
| 2634 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2635 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2636 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2637 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2638 | Hi = Lo; |
| 2639 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2640 | QualType ElementType = VT->getElementType(); |
| 2641 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2642 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2643 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2644 | return; |
| 2645 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2646 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2647 | // pass them as integer. For platforms where clang is the de facto |
| 2648 | // platform compiler, we must continue to use integer. |
| 2649 | if (!classifyIntegerMMXAsSSE() && |
| 2650 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2651 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2652 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2653 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2654 | Current = Integer; |
| 2655 | else |
| 2656 | Current = SSE; |
| 2657 | |
| 2658 | // If this type crosses an eightbyte boundary, it should be |
| 2659 | // split. |
| 2660 | if (OffsetBase && OffsetBase != 64) |
| 2661 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2662 | } else if (Size == 128 || |
| 2663 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2664 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2665 | // least significant one belongs to class SSE and all the others to class |
| 2666 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2667 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2668 | // This design isn't correct for 256-bits, but since there're no cases |
| 2669 | // where the upper parts would need to be inspected, avoid adding |
| 2670 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2671 | // |
| 2672 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2673 | // registers if they are "named", i.e. not part of the "..." of a |
| 2674 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2675 | // |
| 2676 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2677 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2678 | Lo = SSE; |
| 2679 | Hi = SSEUp; |
| 2680 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2681 | return; |
| 2682 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2683 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2684 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2685 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2686 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2687 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2688 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2689 | if (Size <= 64) |
| 2690 | Current = Integer; |
| 2691 | else if (Size <= 128) |
| 2692 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2693 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2694 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2695 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2696 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2697 | } else if (ET == getContext().LongDoubleTy) { |
| 2698 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2699 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2700 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2701 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2702 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2703 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2704 | Lo = Hi = SSE; |
| 2705 | else |
| 2706 | llvm_unreachable("unexpected long double representation!"); |
| 2707 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2708 | |
| 2709 | // If this complex type crosses an eightbyte boundary then it |
| 2710 | // should be split. |
| 2711 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2712 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2713 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2714 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2715 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2716 | return; |
| 2717 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2718 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2719 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2720 | // Arrays are treated like structures. |
| 2721 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2722 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2723 | |
| 2724 | // 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] | 2725 | // than eight eightbytes, ..., it has class MEMORY. |
| 2726 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | |
| 2729 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2730 | // fields, it has class MEMORY. |
| 2731 | // |
| 2732 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2733 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2734 | return; |
| 2735 | |
| 2736 | // Otherwise implement simplified merge. We could be smarter about |
| 2737 | // this, but it isn't worth it and would be harder to verify. |
| 2738 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2739 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2740 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2741 | |
| 2742 | // The only case a 256-bit wide vector could be used is when the array |
| 2743 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2744 | // 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] | 2745 | // |
| 2746 | if (Size > 128 && |
| 2747 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2748 | return; |
| 2749 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2750 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2751 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2752 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2753 | Lo = merge(Lo, FieldLo); |
| 2754 | Hi = merge(Hi, FieldHi); |
| 2755 | if (Lo == Memory || Hi == Memory) |
| 2756 | break; |
| 2757 | } |
| 2758 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2759 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2760 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2761 | return; |
| 2762 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2763 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2764 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2765 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2766 | |
| 2767 | // 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] | 2768 | // than eight eightbytes, ..., it has class MEMORY. |
| 2769 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2770 | return; |
| 2771 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2772 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2773 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2774 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2775 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2776 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2777 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2778 | const RecordDecl *RD = RT->getDecl(); |
| 2779 | |
| 2780 | // Assume variable sized types are passed in memory. |
| 2781 | if (RD->hasFlexibleArrayMember()) |
| 2782 | return; |
| 2783 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2784 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2785 | |
| 2786 | // Reset Lo class, this will be recomputed. |
| 2787 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2788 | |
| 2789 | // If this is a C++ record, classify the bases first. |
| 2790 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2791 | for (const auto &I : CXXRD->bases()) { |
| 2792 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2793 | "Unexpected base class!"); |
| 2794 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2795 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2796 | |
| 2797 | // Classify this field. |
| 2798 | // |
| 2799 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2800 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2801 | // initialized to class NO_CLASS. |
| 2802 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2803 | uint64_t Offset = |
| 2804 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2805 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2806 | Lo = merge(Lo, FieldLo); |
| 2807 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2808 | if (Lo == Memory || Hi == Memory) { |
| 2809 | postMerge(Size, Lo, Hi); |
| 2810 | return; |
| 2811 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2816 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2817 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2818 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2819 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2820 | bool BitField = i->isBitField(); |
| 2821 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2822 | // Ignore padding bit-fields. |
| 2823 | if (BitField && i->isUnnamedBitfield()) |
| 2824 | continue; |
| 2825 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2826 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2827 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2828 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2829 | // The only case a 256-bit wide vector could be used is when the struct |
| 2830 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2831 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2832 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2833 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2834 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2835 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2836 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2837 | return; |
| 2838 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2839 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2840 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2841 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2842 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2843 | return; |
| 2844 | } |
| 2845 | |
| 2846 | // Classify this field. |
| 2847 | // |
| 2848 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2849 | // exceeds a single eightbyte, each is classified |
| 2850 | // separately. Each eightbyte gets initialized to class |
| 2851 | // NO_CLASS. |
| 2852 | Class FieldLo, FieldHi; |
| 2853 | |
| 2854 | // Bit-fields require special handling, they do not force the |
| 2855 | // structure to be passed in memory even if unaligned, and |
| 2856 | // therefore they can straddle an eightbyte. |
| 2857 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2858 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2859 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2860 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2861 | |
| 2862 | uint64_t EB_Lo = Offset / 64; |
| 2863 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2864 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2865 | if (EB_Lo) { |
| 2866 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2867 | FieldLo = NoClass; |
| 2868 | FieldHi = Integer; |
| 2869 | } else { |
| 2870 | FieldLo = Integer; |
| 2871 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2872 | } |
| 2873 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2874 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2875 | Lo = merge(Lo, FieldLo); |
| 2876 | Hi = merge(Hi, FieldHi); |
| 2877 | if (Lo == Memory || Hi == Memory) |
| 2878 | break; |
| 2879 | } |
| 2880 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2881 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2882 | } |
| 2883 | } |
| 2884 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2885 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2886 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2887 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2888 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2889 | // Treat an enum type as its underlying type. |
| 2890 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2891 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2892 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2893 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2894 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2897 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2900 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2901 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2902 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2903 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2904 | if (Size <= 64 || Size > LargestVector) |
| 2905 | return true; |
| 2906 | } |
| 2907 | |
| 2908 | return false; |
| 2909 | } |
| 2910 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2911 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2912 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2913 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2914 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2915 | // |
| 2916 | // This assumption is optimistic, as there could be free registers available |
| 2917 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2918 | // the argument in the free register. This does not seem to happen currently, |
| 2919 | // but this code would be much safer if we could mark the argument with |
| 2920 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2921 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2922 | // Treat an enum type as its underlying type. |
| 2923 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2924 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2925 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2926 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2927 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2928 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2929 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2930 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2931 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2932 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2933 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2934 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2935 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2936 | |
| 2937 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2938 | // is important for good codegen. |
| 2939 | // |
| 2940 | // We do this by coercing the value into a scalar type which the backend can |
| 2941 | // handle naturally (i.e., without using byval). |
| 2942 | // |
| 2943 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2944 | // free integer registers. Doing this when there are free integer registers |
| 2945 | // would require more care, as we would have to ensure that the coerced value |
| 2946 | // did not claim the unused register. That would require either reording the |
| 2947 | // arguments to the function (so that any subsequent inreg values came first), |
| 2948 | // or only doing this optimization when there were no following arguments that |
| 2949 | // might be inreg. |
| 2950 | // |
| 2951 | // We currently expect it to be rare (particularly in well written code) for |
| 2952 | // arguments to be passed on the stack when there are still free integer |
| 2953 | // registers available (this would typically imply large structs being passed |
| 2954 | // by value), so this seems like a fair tradeoff for now. |
| 2955 | // |
| 2956 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2957 | // attributes. See PR12193. |
| 2958 | if (freeIntRegs == 0) { |
| 2959 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2960 | |
| 2961 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2962 | // type, which will end up on the stack (with alignment 8). |
| 2963 | if (Align == 8 && Size <= 64) |
| 2964 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2965 | Size)); |
| 2966 | } |
| 2967 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2968 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2971 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2972 | /// 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] | 2973 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2974 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2975 | // vectors; strip them off if present. |
| 2976 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2977 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2978 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2979 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2980 | if (isa<llvm::VectorType>(IRType) || |
| 2981 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2982 | return IRType; |
| 2983 | |
| 2984 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2985 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2986 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2987 | |
| 2988 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2989 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2990 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2993 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2994 | /// is known to either be off the end of the specified type or being in |
| 2995 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2996 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2997 | /// classification that put one of the two halves in the INTEGER class. |
| 2998 | /// |
| 2999 | /// It is conservatively correct to return false. |
| 3000 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 3001 | unsigned EndBit, ASTContext &Context) { |
| 3002 | // If the bytes being queried are off the end of the type, there is no user |
| 3003 | // data hiding here. This handles analysis of builtins, vectors and other |
| 3004 | // types that don't contain interesting padding. |
| 3005 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 3006 | if (TySize <= StartBit) |
| 3007 | return true; |
| 3008 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3009 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3010 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 3011 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 3012 | |
| 3013 | // Check each element to see if the element overlaps with the queried range. |
| 3014 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3015 | // If the element is after the span we care about, then we're done.. |
| 3016 | unsigned EltOffset = i*EltSize; |
| 3017 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3019 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 3020 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3021 | EndBit-EltOffset, Context)) |
| 3022 | return false; |
| 3023 | } |
| 3024 | // If it overlaps no elements, then it is safe to process as padding. |
| 3025 | return true; |
| 3026 | } |
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 (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3029 | const RecordDecl *RD = RT->getDecl(); |
| 3030 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3031 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3032 | // If this is a C++ record, check the bases first. |
| 3033 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3034 | for (const auto &I : CXXRD->bases()) { |
| 3035 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3036 | "Unexpected base class!"); |
| 3037 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3038 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
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 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3041 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3042 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3043 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3044 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3045 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3046 | EndBit-BaseOffset, Context)) |
| 3047 | return false; |
| 3048 | } |
| 3049 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3050 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3051 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3052 | // this could be sped up a lot by being smarter about queried fields, |
| 3053 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3054 | // much. |
| 3055 | unsigned idx = 0; |
| 3056 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3057 | i != e; ++i, ++idx) { |
| 3058 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3059 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3060 | // If we found a field after the region we care about, then we're done. |
| 3061 | if (FieldOffset >= EndBit) break; |
| 3062 | |
| 3063 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3064 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3065 | Context)) |
| 3066 | return false; |
| 3067 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3069 | // If nothing in this record overlapped the area of interest, then we're |
| 3070 | // clean. |
| 3071 | return true; |
| 3072 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3073 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3074 | return false; |
| 3075 | } |
| 3076 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3077 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3078 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3079 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3080 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3081 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3082 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3083 | // Base case if we find a float. |
| 3084 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3085 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3086 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3087 | // 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] | 3088 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3089 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3090 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3091 | IROffset -= SL->getElementOffset(Elt); |
| 3092 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3093 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3095 | // 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] | 3096 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3097 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3098 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3099 | IROffset -= IROffset/EltSize*EltSize; |
| 3100 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3101 | } |
| 3102 | |
| 3103 | return false; |
| 3104 | } |
| 3105 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3106 | |
| 3107 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3108 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3109 | llvm::Type *X86_64ABIInfo:: |
| 3110 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3111 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3112 | // 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] | 3113 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3114 | // structs that contain 3 floats. |
| 3115 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3116 | SourceOffset*8+64, getContext())) |
| 3117 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3118 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3119 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3120 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3121 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3122 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3123 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3124 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3125 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3126 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3127 | } |
| 3128 | |
| 3129 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3130 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3131 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3132 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3133 | /// 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] | 3134 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3135 | /// etc). |
| 3136 | /// |
| 3137 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3138 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3139 | /// the 8-byte value references. PrefType may be null. |
| 3140 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3141 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3142 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3143 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3144 | llvm::Type *X86_64ABIInfo:: |
| 3145 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3146 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3147 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3148 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3149 | if (IROffset == 0) { |
| 3150 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3151 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3152 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3153 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3154 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3155 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3156 | // goodness in the source type is just tail padding. This is allowed to |
| 3157 | // kick in for struct {double,int} on the int, but not on |
| 3158 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3159 | // have to do this analysis on the source type because we can't depend on |
| 3160 | // unions being lowered a specific way etc. |
| 3161 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3162 | IRType->isIntegerTy(32) || |
| 3163 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3164 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3165 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3167 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3168 | SourceOffset*8+64, getContext())) |
| 3169 | return IRType; |
| 3170 | } |
| 3171 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3172 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3173 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3174 | // 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] | 3175 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3176 | if (IROffset < SL->getSizeInBytes()) { |
| 3177 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3178 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3179 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3180 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3181 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3182 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3183 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3184 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3185 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3186 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3187 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3188 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3189 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3190 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3191 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3192 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3193 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3194 | // 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] | 3195 | unsigned TySizeInBytes = |
| 3196 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3198 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3199 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3200 | // It is always safe to classify this as an integer type up to i64 that |
| 3201 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3202 | return llvm::IntegerType::get(getVMContext(), |
| 3203 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3206 | |
| 3207 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3208 | /// be used as elements of a two register pair to pass or return, return a |
| 3209 | /// first class aggregate to represent them. For example, if the low part of |
| 3210 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3211 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3212 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3213 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3214 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3215 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3216 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3217 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3218 | // the second element at offset 8. Check for this: |
| 3219 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3220 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3221 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3222 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3223 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3224 | // To handle this, we have to increase the size of the low part so that the |
| 3225 | // second element will start at an 8 byte offset. We can't increase the size |
| 3226 | // of the second element because it might make us access off the end of the |
| 3227 | // struct. |
| 3228 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3229 | // There are usually two sorts of types the ABI generation code can produce |
| 3230 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3231 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3232 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3233 | // Promote these to a larger type. |
| 3234 | if (Lo->isFloatTy()) |
| 3235 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3236 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3237 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3238 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3239 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3240 | } |
| 3241 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3242 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3243 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3244 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3245 | // Verify that the second element is at an 8-byte offset. |
| 3246 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3247 | "Invalid x86-64 argument pair!"); |
| 3248 | return Result; |
| 3249 | } |
| 3250 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3251 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3252 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3253 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3254 | // classification algorithm. |
| 3255 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3256 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3257 | |
| 3258 | // Check some invariants. |
| 3259 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3260 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3261 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3262 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3263 | switch (Lo) { |
| 3264 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3265 | if (Hi == NoClass) |
| 3266 | return ABIArgInfo::getIgnore(); |
| 3267 | // If the low part is just padding, it takes no register, leave ResType |
| 3268 | // null. |
| 3269 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3270 | "Unknown missing lo part"); |
| 3271 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3272 | |
| 3273 | case SSEUp: |
| 3274 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3275 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3276 | |
| 3277 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3278 | // hidden argument. |
| 3279 | case Memory: |
| 3280 | return getIndirectReturnResult(RetTy); |
| 3281 | |
| 3282 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3283 | // available register of the sequence %rax, %rdx is used. |
| 3284 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3285 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3287 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3288 | // so that the parameter gets the right LLVM IR attributes. |
| 3289 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3290 | // Treat an enum type as its underlying type. |
| 3291 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3292 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3293 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3294 | if (RetTy->isIntegralOrEnumerationType() && |
| 3295 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3296 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3297 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3298 | break; |
| 3299 | |
| 3300 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3301 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3302 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3303 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3304 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3305 | |
| 3306 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3307 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3308 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3309 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3310 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3311 | |
| 3312 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3313 | // part of the value is returned in %st0 and the imaginary part in |
| 3314 | // %st1. |
| 3315 | case ComplexX87: |
| 3316 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3317 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3318 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3319 | break; |
| 3320 | } |
| 3321 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3322 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3323 | switch (Hi) { |
| 3324 | // Memory was handled previously and X87 should |
| 3325 | // never occur as a hi class. |
| 3326 | case Memory: |
| 3327 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3328 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3329 | |
| 3330 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3331 | case NoClass: |
| 3332 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3333 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3334 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3335 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3336 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3337 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3338 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3339 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3340 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3341 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3342 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3343 | break; |
| 3344 | |
| 3345 | // 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] | 3346 | // is passed in the next available eightbyte chunk if the last used |
| 3347 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3348 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3349 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3350 | case SSEUp: |
| 3351 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3352 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3353 | break; |
| 3354 | |
| 3355 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3356 | // returned together with the previous X87 value in %st0. |
| 3357 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3358 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3359 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3360 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3361 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3362 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3363 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3364 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3365 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3366 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3367 | break; |
| 3368 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3369 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3370 | // 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] | 3371 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3372 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3373 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3374 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3375 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3376 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3379 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3380 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3381 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3382 | const |
| 3383 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3384 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3385 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3386 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3387 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3388 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3389 | // Check some invariants. |
| 3390 | // FIXME: Enforce these by construction. |
| 3391 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3392 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3393 | |
| 3394 | neededInt = 0; |
| 3395 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3396 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3397 | switch (Lo) { |
| 3398 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3399 | if (Hi == NoClass) |
| 3400 | return ABIArgInfo::getIgnore(); |
| 3401 | // If the low part is just padding, it takes no register, leave ResType |
| 3402 | // null. |
| 3403 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3404 | "Unknown missing lo part"); |
| 3405 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3406 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3407 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3408 | // on the stack. |
| 3409 | case Memory: |
| 3410 | |
| 3411 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3412 | // COMPLEX_X87, it is passed in memory. |
| 3413 | case X87: |
| 3414 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3415 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3416 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3417 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3418 | |
| 3419 | case SSEUp: |
| 3420 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3421 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3422 | |
| 3423 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3424 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3425 | // and %r9 is used. |
| 3426 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3427 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3428 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3429 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3430 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3431 | |
| 3432 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3433 | // so that the parameter gets the right LLVM IR attributes. |
| 3434 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3435 | // Treat an enum type as its underlying type. |
| 3436 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3437 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3438 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3439 | if (Ty->isIntegralOrEnumerationType() && |
| 3440 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3441 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3442 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3443 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3444 | break; |
| 3445 | |
| 3446 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3447 | // available SSE register is used, the registers are taken in the |
| 3448 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3449 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3450 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3451 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3452 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3453 | break; |
| 3454 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3455 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3456 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3457 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3458 | switch (Hi) { |
| 3459 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3460 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3461 | // which is passed in memory. |
| 3462 | case Memory: |
| 3463 | case X87: |
| 3464 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3465 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3466 | |
| 3467 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3468 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3469 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3470 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3471 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3472 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3473 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3474 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3475 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3476 | break; |
| 3477 | |
| 3478 | // X87Up generally doesn't occur here (long double is passed in |
| 3479 | // memory), except in situations involving unions. |
| 3480 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3481 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3482 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3483 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3484 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3485 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3486 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3487 | ++neededSSE; |
| 3488 | break; |
| 3489 | |
| 3490 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3491 | // 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] | 3492 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3493 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3494 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3495 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3496 | break; |
| 3497 | } |
| 3498 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3499 | // If a high part was specified, merge it together with the low part. It is |
| 3500 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3501 | // first class struct aggregate with the high and low part: {low, high} |
| 3502 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3503 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3504 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3505 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3508 | ABIArgInfo |
| 3509 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3510 | unsigned &NeededSSE) const { |
| 3511 | auto RT = Ty->getAs<RecordType>(); |
| 3512 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3513 | |
| 3514 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3515 | return getIndirectReturnResult(Ty); |
| 3516 | |
| 3517 | // Sum up bases |
| 3518 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3519 | if (CXXRD->isDynamicClass()) { |
| 3520 | NeededInt = NeededSSE = 0; |
| 3521 | return getIndirectReturnResult(Ty); |
| 3522 | } |
| 3523 | |
| 3524 | for (const auto &I : CXXRD->bases()) |
| 3525 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3526 | .isIndirect()) { |
| 3527 | NeededInt = NeededSSE = 0; |
| 3528 | return getIndirectReturnResult(Ty); |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | // Sum up members |
| 3533 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3534 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3535 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3536 | .isIndirect()) { |
| 3537 | NeededInt = NeededSSE = 0; |
| 3538 | return getIndirectReturnResult(Ty); |
| 3539 | } |
| 3540 | } else { |
| 3541 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3542 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3543 | LocalNeededSSE, true) |
| 3544 | .isIndirect()) { |
| 3545 | NeededInt = NeededSSE = 0; |
| 3546 | return getIndirectReturnResult(Ty); |
| 3547 | } |
| 3548 | NeededInt += LocalNeededInt; |
| 3549 | NeededSSE += LocalNeededSSE; |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | return ABIArgInfo::getDirect(); |
| 3554 | } |
| 3555 | |
| 3556 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3557 | unsigned &NeededInt, |
| 3558 | unsigned &NeededSSE) const { |
| 3559 | |
| 3560 | NeededInt = 0; |
| 3561 | NeededSSE = 0; |
| 3562 | |
| 3563 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3564 | } |
| 3565 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3566 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3567 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3568 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3569 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3570 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3571 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3572 | if (CallingConv == llvm::CallingConv::Win64) { |
| 3573 | WinX86_64ABIInfo Win64ABIInfo(CGT); |
| 3574 | Win64ABIInfo.computeInfo(FI); |
| 3575 | return; |
| 3576 | } |
| 3577 | |
| 3578 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3579 | |
| 3580 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3581 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3582 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3583 | unsigned NeededInt, NeededSSE; |
| 3584 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 3585 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3586 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3587 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3588 | FI.getReturnInfo() = |
| 3589 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3590 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3591 | FreeIntRegs -= NeededInt; |
| 3592 | FreeSSERegs -= NeededSSE; |
| 3593 | } else { |
| 3594 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3595 | } |
| 3596 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3597 | // Complex Long Double Type is passed in Memory when Regcall |
| 3598 | // calling convention is used. |
| 3599 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3600 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3601 | getContext().LongDoubleTy) |
| 3602 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3603 | } else |
| 3604 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3605 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3606 | |
| 3607 | // If the return value is indirect, then the hidden argument is consuming one |
| 3608 | // integer register. |
| 3609 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3610 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3611 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3612 | // The chain argument effectively gives us another free register. |
| 3613 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3614 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3615 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3616 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3617 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3618 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3619 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3620 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3621 | it != ie; ++it, ++ArgNo) { |
| 3622 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3623 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3624 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3625 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3626 | else |
| 3627 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3628 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3629 | |
| 3630 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3631 | // eightbyte of an argument, the whole argument is passed on the |
| 3632 | // stack. If registers have already been assigned for some |
| 3633 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3634 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3635 | FreeIntRegs -= NeededInt; |
| 3636 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3637 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3638 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3639 | } |
| 3640 | } |
| 3641 | } |
| 3642 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3643 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3644 | Address VAListAddr, QualType Ty) { |
| 3645 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3646 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3647 | llvm::Value *overflow_arg_area = |
| 3648 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3649 | |
| 3650 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3651 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3652 | // It isn't stated explicitly in the standard, but in practice we use |
| 3653 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3654 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3655 | if (Align > CharUnits::fromQuantity(8)) { |
| 3656 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3657 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | // 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] | 3661 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3662 | llvm::Value *Res = |
| 3663 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3664 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3665 | |
| 3666 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3667 | // l->overflow_arg_area + sizeof(type). |
| 3668 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3669 | // an 8 byte boundary. |
| 3670 | |
| 3671 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3672 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3673 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3674 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3675 | "overflow_arg_area.next"); |
| 3676 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3677 | |
| 3678 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3679 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3682 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3683 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3684 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3685 | // struct { |
| 3686 | // i32 gp_offset; |
| 3687 | // i32 fp_offset; |
| 3688 | // i8* overflow_arg_area; |
| 3689 | // i8* reg_save_area; |
| 3690 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3691 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3692 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3693 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3694 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3695 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3696 | |
| 3697 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3698 | // in the registers. If not go to step 7. |
| 3699 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3700 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3701 | |
| 3702 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3703 | // general purpose registers needed to pass type and num_fp to hold |
| 3704 | // the number of floating point registers needed. |
| 3705 | |
| 3706 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3707 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3708 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3709 | // |
| 3710 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3711 | // register save space). |
| 3712 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3713 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3714 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3715 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3716 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3717 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3718 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3719 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3720 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3721 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3722 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3726 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3727 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3728 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3729 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3730 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3731 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3732 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3733 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3734 | } |
| 3735 | |
| 3736 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3737 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3738 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3739 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3740 | |
| 3741 | // Emit code to load the value if it was passed in registers. |
| 3742 | |
| 3743 | CGF.EmitBlock(InRegBlock); |
| 3744 | |
| 3745 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3746 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3747 | // copying to a temporary location in case the parameter is passed |
| 3748 | // in different register classes or requires an alignment greater |
| 3749 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3750 | // |
| 3751 | // FIXME: This really results in shameful code when we end up needing to |
| 3752 | // collect arguments from different places; often what should result in a |
| 3753 | // simple assembling of a structure from scattered addresses has many more |
| 3754 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3755 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3756 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3757 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3758 | "reg_save_area"); |
| 3759 | |
| 3760 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3761 | if (neededInt && neededSSE) { |
| 3762 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3763 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3764 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3765 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3766 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3767 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3768 | llvm::Type *TyLo = ST->getElementType(0); |
| 3769 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3770 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3771 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3772 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3773 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3774 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3775 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3776 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3777 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3778 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3779 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3780 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3781 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3782 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3783 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3784 | CGF.Builder.CreateStore(V, |
| 3785 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3786 | |
| 3787 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3788 | V = CGF.Builder.CreateAlignedLoad( |
| 3789 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3790 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3791 | CharUnits Offset = CharUnits::fromQuantity( |
| 3792 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3793 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3794 | |
| 3795 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3796 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3797 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3798 | CharUnits::fromQuantity(8)); |
| 3799 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3800 | |
| 3801 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3802 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3803 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3804 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3805 | CharUnits TyAlign = SizeAlign.second; |
| 3806 | |
| 3807 | // Copy into a temporary if the type is more aligned than the |
| 3808 | // register save area. |
| 3809 | if (TyAlign.getQuantity() > 8) { |
| 3810 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3811 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3812 | RegAddr = Tmp; |
| 3813 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3814 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3815 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3816 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3817 | CharUnits::fromQuantity(16)); |
| 3818 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3819 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3820 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3821 | // SSE registers are spaced 16 bytes apart in the register save |
| 3822 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3823 | // The ABI isn't explicit about this, but it seems reasonable |
| 3824 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3825 | // naturally 16-byte aligned and the prologue is expected to store |
| 3826 | // all the SSE registers to the RSA. |
| 3827 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3828 | CharUnits::fromQuantity(16)); |
| 3829 | Address RegAddrHi = |
| 3830 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3831 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3832 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3833 | ? AI.getCoerceToType() |
| 3834 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3835 | llvm::Value *V; |
| 3836 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3837 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3838 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3839 | RegAddrLo, ST->getStructElementType(0))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3840 | CGF.Builder.CreateStore(V, |
| 3841 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3842 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3843 | RegAddrHi, ST->getStructElementType(1))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3844 | CGF.Builder.CreateStore(V, |
| 3845 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3846 | |
| 3847 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3851 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3852 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3853 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3854 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3855 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3856 | gp_offset_p); |
| 3857 | } |
| 3858 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3859 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3860 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3861 | fp_offset_p); |
| 3862 | } |
| 3863 | CGF.EmitBranch(ContBlock); |
| 3864 | |
| 3865 | // Emit code to load the value if it was passed in memory. |
| 3866 | |
| 3867 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3868 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3869 | |
| 3870 | // Return the appropriate result. |
| 3871 | |
| 3872 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3873 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3874 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3875 | return ResAddr; |
| 3876 | } |
| 3877 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3878 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3879 | QualType Ty) const { |
| 3880 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3881 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3882 | CharUnits::fromQuantity(8), |
| 3883 | /*allowHigherAlign*/ false); |
| 3884 | } |
| 3885 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3886 | ABIArgInfo |
| 3887 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3888 | const ABIArgInfo ¤t) const { |
| 3889 | // Assumes vectorCall calling convention. |
| 3890 | const Type *Base = nullptr; |
| 3891 | uint64_t NumElts = 0; |
| 3892 | |
| 3893 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3894 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3895 | FreeSSERegs -= NumElts; |
| 3896 | return getDirectX86Hva(); |
| 3897 | } |
| 3898 | return current; |
| 3899 | } |
| 3900 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3901 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3902 | bool IsReturnType, bool IsVectorCall, |
| 3903 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3904 | |
| 3905 | if (Ty->isVoidType()) |
| 3906 | return ABIArgInfo::getIgnore(); |
| 3907 | |
| 3908 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3909 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3910 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3911 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3912 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3913 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3914 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3915 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3916 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3917 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3918 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3919 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3923 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3924 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3925 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3926 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3927 | const Type *Base = nullptr; |
| 3928 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3929 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3930 | // other targets. |
| 3931 | if ((IsVectorCall || IsRegCall) && |
| 3932 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3933 | if (IsRegCall) { |
| 3934 | if (FreeSSERegs >= NumElts) { |
| 3935 | FreeSSERegs -= NumElts; |
| 3936 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3937 | return ABIArgInfo::getDirect(); |
| 3938 | return ABIArgInfo::getExpand(); |
| 3939 | } |
| 3940 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3941 | } else if (IsVectorCall) { |
| 3942 | if (FreeSSERegs >= NumElts && |
| 3943 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3944 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3945 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3946 | } else if (IsReturnType) { |
| 3947 | return ABIArgInfo::getExpand(); |
| 3948 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3949 | // HVAs are delayed and reclassified in the 2nd step. |
| 3950 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3951 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3952 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3955 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3956 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3957 | // directly. |
| 3958 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3959 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3960 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3963 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3964 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3965 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3966 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3967 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3968 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3969 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3970 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Reid Kleckner | 08f64e9 | 2018-10-31 17:43:55 +0000 | [diff] [blame] | 3973 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3974 | switch (BT->getKind()) { |
| 3975 | case BuiltinType::Bool: |
| 3976 | // Bool type is always extended to the ABI, other builtin types are not |
| 3977 | // extended. |
| 3978 | return ABIArgInfo::getExtend(Ty); |
| 3979 | |
| 3980 | case BuiltinType::LongDouble: |
| 3981 | // Mingw64 GCC uses the old 80 bit extended precision floating point |
| 3982 | // unit. It passes them indirectly through memory. |
| 3983 | if (IsMingw64) { |
| 3984 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3985 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
| 3986 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3987 | } |
| 3988 | break; |
| 3989 | |
| 3990 | case BuiltinType::Int128: |
| 3991 | case BuiltinType::UInt128: |
| 3992 | // If it's a parameter type, the normal ABI rule is that arguments larger |
| 3993 | // than 8 bytes are passed indirectly. GCC follows it. We follow it too, |
| 3994 | // even though it isn't particularly efficient. |
| 3995 | if (!IsReturnType) |
| 3996 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3997 | |
| 3998 | // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. |
| 3999 | // Clang matches them for compatibility. |
| 4000 | return ABIArgInfo::getDirect( |
| 4001 | llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2)); |
| 4002 | |
| 4003 | default: |
| 4004 | break; |
| 4005 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4008 | return ABIArgInfo::getDirect(); |
| 4009 | } |
| 4010 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4011 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 4012 | unsigned FreeSSERegs, |
| 4013 | bool IsVectorCall, |
| 4014 | bool IsRegCall) const { |
| 4015 | unsigned Count = 0; |
| 4016 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4017 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 4018 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4019 | if (Count < VectorcallMaxParamNumAsReg) |
| 4020 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4021 | else { |
| 4022 | // Since these cannot be passed in registers, pretend no registers |
| 4023 | // are left. |
| 4024 | unsigned ZeroSSERegsAvail = 0; |
| 4025 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 4026 | IsVectorCall, IsRegCall); |
| 4027 | } |
| 4028 | ++Count; |
| 4029 | } |
| 4030 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4031 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4032 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4033 | } |
| 4034 | } |
| 4035 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4036 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4037 | bool IsVectorCall = |
| 4038 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4039 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 4040 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4041 | unsigned FreeSSERegs = 0; |
| 4042 | if (IsVectorCall) { |
| 4043 | // We can use up to 4 SSE return registers with vectorcall. |
| 4044 | FreeSSERegs = 4; |
| 4045 | } else if (IsRegCall) { |
| 4046 | // RegCall gives us 16 SSE registers. |
| 4047 | FreeSSERegs = 16; |
| 4048 | } |
| 4049 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4050 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4051 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 4052 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4053 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4054 | if (IsVectorCall) { |
| 4055 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4056 | FreeSSERegs = 6; |
| 4057 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4058 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4059 | FreeSSERegs = 16; |
| 4060 | } |
| 4061 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4062 | if (IsVectorCall) { |
| 4063 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4064 | } else { |
| 4065 | for (auto &I : FI.arguments()) |
| 4066 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4067 | } |
| 4068 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4071 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4072 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4073 | |
| 4074 | bool IsIndirect = false; |
| 4075 | |
| 4076 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4077 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4078 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4079 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4080 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4081 | } |
| 4082 | |
| 4083 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4084 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4085 | CharUnits::fromQuantity(8), |
| 4086 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4087 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4088 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4089 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4090 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4091 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4092 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4093 | bool IsSoftFloatABI; |
| 4094 | |
| 4095 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4096 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4097 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4098 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4099 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4100 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4101 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4102 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4103 | }; |
| 4104 | |
| 4105 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4106 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4107 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4108 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4109 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4110 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4111 | // This is recovered from gcc output. |
| 4112 | return 1; // r1 is the dedicated stack pointer |
| 4113 | } |
| 4114 | |
| 4115 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4116 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4117 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4118 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4119 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4120 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4121 | // Complex types are passed just like their elements |
| 4122 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4123 | Ty = CTy->getElementType(); |
| 4124 | |
| 4125 | if (Ty->isVectorType()) |
| 4126 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4127 | : 4); |
| 4128 | |
| 4129 | // For single-element float/vector structs, we consider the whole type |
| 4130 | // to have the same alignment requirements as its single element. |
| 4131 | const Type *AlignTy = nullptr; |
| 4132 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4133 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4134 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4135 | (BT && BT->isFloatingPoint())) |
| 4136 | AlignTy = EltType; |
| 4137 | } |
| 4138 | |
| 4139 | if (AlignTy) |
| 4140 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4141 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4142 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4143 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4144 | // TODO: this implementation is now likely redundant with |
| 4145 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4146 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4147 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4148 | if (getTarget().getTriple().isOSDarwin()) { |
| 4149 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4150 | TI.second = getParamTypeAlignment(Ty); |
| 4151 | |
| 4152 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4153 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4154 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4155 | /*AllowHigherAlign=*/true); |
| 4156 | } |
| 4157 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4158 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4159 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4160 | // TODO: Implement this. For now ignore. |
| 4161 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4162 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4165 | // struct __va_list_tag { |
| 4166 | // unsigned char gpr; |
| 4167 | // unsigned char fpr; |
| 4168 | // unsigned short reserved; |
| 4169 | // void *overflow_arg_area; |
| 4170 | // void *reg_save_area; |
| 4171 | // }; |
| 4172 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4173 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4174 | bool isInt = |
| 4175 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4176 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4177 | |
| 4178 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4179 | // with the argument-lowering code. |
| 4180 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4181 | |
| 4182 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4183 | |
| 4184 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4185 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4186 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4187 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4188 | } else { |
| 4189 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4190 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4191 | |
| 4192 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4193 | |
| 4194 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4195 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4196 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4197 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4198 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4199 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4200 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4201 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4202 | |
| 4203 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4204 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4205 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4206 | |
| 4207 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4208 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4209 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4210 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4211 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4212 | // Case 1: consume registers. |
| 4213 | Address RegAddr = Address::invalid(); |
| 4214 | { |
| 4215 | CGF.EmitBlock(UsingRegs); |
| 4216 | |
| 4217 | Address RegSaveAreaPtr = |
| 4218 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4219 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4220 | CharUnits::fromQuantity(8)); |
| 4221 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4222 | |
| 4223 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4224 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4225 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4226 | CharUnits::fromQuantity(32)); |
| 4227 | } |
| 4228 | |
| 4229 | // Get the address of the saved value by scaling the number of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4230 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4231 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4232 | llvm::Value *RegOffset = |
| 4233 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4234 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4235 | RegAddr.getPointer(), RegOffset), |
| 4236 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4237 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4238 | |
| 4239 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4240 | NumRegs = |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4241 | Builder.CreateAdd(NumRegs, |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4242 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4243 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4244 | |
| 4245 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4246 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4247 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4248 | // Case 2: consume space in the overflow area. |
| 4249 | Address MemAddr = Address::invalid(); |
| 4250 | { |
| 4251 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4252 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4253 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4254 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4255 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4256 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4257 | |
| 4258 | CharUnits Size; |
| 4259 | if (!isIndirect) { |
| 4260 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4261 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4262 | } else { |
| 4263 | Size = CGF.getPointerSize(); |
| 4264 | } |
| 4265 | |
| 4266 | Address OverflowAreaAddr = |
| 4267 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4268 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4269 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4270 | // Round up address of argument to alignment |
| 4271 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4272 | if (Align > OverflowAreaAlign) { |
| 4273 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4274 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4275 | Align); |
| 4276 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4277 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4278 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4279 | |
| 4280 | // Increase the overflow area. |
| 4281 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4282 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4283 | CGF.EmitBranch(Cont); |
| 4284 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4285 | |
| 4286 | CGF.EmitBlock(Cont); |
| 4287 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4288 | // Merge the cases with a phi. |
| 4289 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4290 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4291 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4292 | // Load the pointer if the argument was passed indirectly. |
| 4293 | if (isIndirect) { |
| 4294 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4295 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | return Result; |
| 4299 | } |
| 4300 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4301 | bool |
| 4302 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4303 | llvm::Value *Address) const { |
| 4304 | // This is calculated from the LLVM and GCC tables and verified |
| 4305 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4306 | |
| 4307 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4308 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4309 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4310 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4311 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4312 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4313 | |
| 4314 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4315 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4316 | |
| 4317 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4318 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4319 | |
| 4320 | // 64-76 are various 4-byte special-purpose registers: |
| 4321 | // 64: mq |
| 4322 | // 65: lr |
| 4323 | // 66: ctr |
| 4324 | // 67: ap |
| 4325 | // 68-75 cr0-7 |
| 4326 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4327 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4328 | |
| 4329 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4330 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4331 | |
| 4332 | // 109: vrsave |
| 4333 | // 110: vscr |
| 4334 | // 111: spe_acc |
| 4335 | // 112: spefscr |
| 4336 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4337 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4338 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4339 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4342 | // PowerPC-64 |
| 4343 | |
| 4344 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4345 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4346 | class PPC64_SVR4_ABIInfo : public SwiftABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4347 | public: |
| 4348 | enum ABIKind { |
| 4349 | ELFv1 = 0, |
| 4350 | ELFv2 |
| 4351 | }; |
| 4352 | |
| 4353 | private: |
| 4354 | static const unsigned GPRBits = 64; |
| 4355 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4356 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4357 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4358 | |
| 4359 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4360 | // will be passed in a QPX register. |
| 4361 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4362 | if (!HasQPX) |
| 4363 | return false; |
| 4364 | |
| 4365 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4366 | unsigned NumElements = VT->getNumElements(); |
| 4367 | if (NumElements == 1) |
| 4368 | return false; |
| 4369 | |
| 4370 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4371 | if (getContext().getTypeSize(Ty) <= 256) |
| 4372 | return true; |
| 4373 | } else if (VT->getElementType()-> |
| 4374 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4375 | if (getContext().getTypeSize(Ty) <= 128) |
| 4376 | return true; |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | return false; |
| 4381 | } |
| 4382 | |
| 4383 | bool IsQPXVectorTy(QualType Ty) const { |
| 4384 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4385 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4386 | |
| 4387 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4388 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4389 | bool SoftFloatABI) |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4390 | : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4391 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4392 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4393 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4394 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4395 | |
| 4396 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4397 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4398 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4399 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4400 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4401 | uint64_t Members) const override; |
| 4402 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4403 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4404 | // Example: For aggregate arguments that fit in a register, we could |
| 4405 | // use getDirectInReg (as is done below for structs containing a single |
| 4406 | // floating-point value) to avoid pushing them to memory on function |
| 4407 | // entry. This would require changing the logic in PPCISelLowering |
| 4408 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4409 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4410 | if (!getCXXABI().classifyReturnType(FI)) |
| 4411 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4412 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4413 | // We rely on the default argument classification for the most part. |
| 4414 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4415 | // 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] | 4416 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4417 | if (T) { |
| 4418 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4419 | if (IsQPXVectorTy(T) || |
| 4420 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4421 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4422 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4423 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4424 | continue; |
| 4425 | } |
| 4426 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4427 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4428 | } |
| 4429 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4430 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4431 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4432 | QualType Ty) const override; |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4433 | |
| 4434 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 4435 | bool asReturnValue) const override { |
| 4436 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4437 | } |
| 4438 | |
| 4439 | bool isSwiftErrorInRegister() const override { |
| 4440 | return false; |
| 4441 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4442 | }; |
| 4443 | |
| 4444 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4445 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4446 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4447 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4448 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4449 | bool SoftFloatABI) |
| 4450 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4451 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4452 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4453 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4454 | // This is recovered from gcc output. |
| 4455 | return 1; // r1 is the dedicated stack pointer |
| 4456 | } |
| 4457 | |
| 4458 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4459 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4460 | }; |
| 4461 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4462 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4463 | public: |
| 4464 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4465 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4466 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4467 | // This is recovered from gcc output. |
| 4468 | return 1; // r1 is the dedicated stack pointer |
| 4469 | } |
| 4470 | |
| 4471 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4472 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4473 | }; |
| 4474 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4475 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4476 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4477 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4478 | // extended to 64 bits. |
| 4479 | bool |
| 4480 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4481 | // Treat an enum type as its underlying type. |
| 4482 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4483 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4484 | |
| 4485 | // Promotable integer types are required to be promoted by the ABI. |
| 4486 | if (Ty->isPromotableIntegerType()) |
| 4487 | return true; |
| 4488 | |
| 4489 | // In addition to the usual promotable integer types, we also need to |
| 4490 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4491 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4492 | switch (BT->getKind()) { |
| 4493 | case BuiltinType::Int: |
| 4494 | case BuiltinType::UInt: |
| 4495 | return true; |
| 4496 | default: |
| 4497 | break; |
| 4498 | } |
| 4499 | |
| 4500 | return false; |
| 4501 | } |
| 4502 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4503 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4504 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4505 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4506 | // Complex types are passed just like their elements. |
| 4507 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4508 | Ty = CTy->getElementType(); |
| 4509 | |
| 4510 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4511 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4512 | if (IsQPXVectorTy(Ty)) { |
| 4513 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4514 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4515 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4516 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4517 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4518 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4519 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4520 | |
| 4521 | // For single-element float/vector structs, we consider the whole type |
| 4522 | // to have the same alignment requirements as its single element. |
| 4523 | const Type *AlignAsType = nullptr; |
| 4524 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4525 | if (EltType) { |
| 4526 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4527 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4528 | getContext().getTypeSize(EltType) == 128) || |
| 4529 | (BT && BT->isFloatingPoint())) |
| 4530 | AlignAsType = EltType; |
| 4531 | } |
| 4532 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4533 | // Likewise for ELFv2 homogeneous aggregates. |
| 4534 | const Type *Base = nullptr; |
| 4535 | uint64_t Members = 0; |
| 4536 | if (!AlignAsType && Kind == ELFv2 && |
| 4537 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4538 | AlignAsType = Base; |
| 4539 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4540 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4541 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4542 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4543 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4544 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4545 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4546 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4547 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
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 | |
| 4550 | // Otherwise, we only need alignment for any aggregate type that |
| 4551 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4552 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4553 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4554 | return CharUnits::fromQuantity(32); |
| 4555 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4556 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4557 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4558 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4561 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4562 | /// aggregate. Base is set to the base element type, and Members is set |
| 4563 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4564 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4565 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4566 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4567 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4568 | if (NElements == 0) |
| 4569 | return false; |
| 4570 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4571 | return false; |
| 4572 | Members *= NElements; |
| 4573 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4574 | const RecordDecl *RD = RT->getDecl(); |
| 4575 | if (RD->hasFlexibleArrayMember()) |
| 4576 | return false; |
| 4577 | |
| 4578 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4579 | |
| 4580 | // If this is a C++ record, check the bases first. |
| 4581 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4582 | for (const auto &I : CXXRD->bases()) { |
| 4583 | // Ignore empty records. |
| 4584 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4585 | continue; |
| 4586 | |
| 4587 | uint64_t FldMembers; |
| 4588 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4589 | return false; |
| 4590 | |
| 4591 | Members += FldMembers; |
| 4592 | } |
| 4593 | } |
| 4594 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4595 | for (const auto *FD : RD->fields()) { |
| 4596 | // Ignore (non-zero arrays of) empty records. |
| 4597 | QualType FT = FD->getType(); |
| 4598 | while (const ConstantArrayType *AT = |
| 4599 | getContext().getAsConstantArrayType(FT)) { |
| 4600 | if (AT->getSize().getZExtValue() == 0) |
| 4601 | return false; |
| 4602 | FT = AT->getElementType(); |
| 4603 | } |
| 4604 | if (isEmptyRecord(getContext(), FT, true)) |
| 4605 | continue; |
| 4606 | |
| 4607 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4608 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4609 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4610 | continue; |
| 4611 | |
| 4612 | uint64_t FldMembers; |
| 4613 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4614 | return false; |
| 4615 | |
| 4616 | Members = (RD->isUnion() ? |
| 4617 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4618 | } |
| 4619 | |
| 4620 | if (!Base) |
| 4621 | return false; |
| 4622 | |
| 4623 | // Ensure there is no padding. |
| 4624 | if (getContext().getTypeSize(Base) * Members != |
| 4625 | getContext().getTypeSize(Ty)) |
| 4626 | return false; |
| 4627 | } else { |
| 4628 | Members = 1; |
| 4629 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4630 | Members = 2; |
| 4631 | Ty = CT->getElementType(); |
| 4632 | } |
| 4633 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4634 | // Most ABIs only support float, double, and some vector type widths. |
| 4635 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4636 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4637 | |
| 4638 | // The base type must be the same for all members. Types that |
| 4639 | // agree in both total size and mode (float vs. vector) are |
| 4640 | // treated as being equivalent here. |
| 4641 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4642 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4643 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4644 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4645 | // so make sure to widen it explicitly. |
| 4646 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4647 | QualType EltTy = VT->getElementType(); |
| 4648 | unsigned NumElements = |
| 4649 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4650 | Base = getContext() |
| 4651 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4652 | .getTypePtr(); |
| 4653 | } |
| 4654 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4655 | |
| 4656 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4657 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4658 | return false; |
| 4659 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4660 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4661 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4662 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4663 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4664 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4665 | // double, long double, or 128-bit vectors. |
| 4666 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4667 | if (BT->getKind() == BuiltinType::Float || |
| 4668 | BT->getKind() == BuiltinType::Double || |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4669 | BT->getKind() == BuiltinType::LongDouble || |
| 4670 | (getContext().getTargetInfo().hasFloat128Type() && |
| 4671 | (BT->getKind() == BuiltinType::Float128))) { |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4672 | if (IsSoftFloatABI) |
| 4673 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4674 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4675 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4676 | } |
| 4677 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4678 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4679 | return true; |
| 4680 | } |
| 4681 | return false; |
| 4682 | } |
| 4683 | |
| 4684 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4685 | const Type *Base, uint64_t Members) const { |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4686 | // Vector and fp128 types require one register, other floating point types |
| 4687 | // require one or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4688 | uint32_t NumRegs = |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4689 | ((getContext().getTargetInfo().hasFloat128Type() && |
| 4690 | Base->isFloat128Type()) || |
| 4691 | Base->isVectorType()) ? 1 |
| 4692 | : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4693 | |
| 4694 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4695 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4698 | ABIArgInfo |
| 4699 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4700 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4701 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4702 | if (Ty->isAnyComplexType()) |
| 4703 | return ABIArgInfo::getDirect(); |
| 4704 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4705 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4706 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4707 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4708 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4709 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4710 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4711 | else if (Size < 128) { |
| 4712 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4713 | return ABIArgInfo::getDirect(CoerceTy); |
| 4714 | } |
| 4715 | } |
| 4716 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4717 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4718 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4719 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4720 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4721 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4722 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4723 | |
| 4724 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4725 | const Type *Base = nullptr; |
| 4726 | uint64_t Members = 0; |
| 4727 | if (Kind == ELFv2 && |
| 4728 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4729 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4730 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4731 | return ABIArgInfo::getDirect(CoerceTy); |
| 4732 | } |
| 4733 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4734 | // If an aggregate may end up fully in registers, we do not |
| 4735 | // use the ByVal method, but pass the aggregate as array. |
| 4736 | // This is usually beneficial since we avoid forcing the |
| 4737 | // back-end to store the argument to memory. |
| 4738 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4739 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4740 | llvm::Type *CoerceTy; |
| 4741 | |
| 4742 | // Types up to 8 bytes are passed as integer type (which will be |
| 4743 | // properly aligned in the argument save area doubleword). |
| 4744 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4745 | CoerceTy = |
| 4746 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4747 | // Larger types are passed as arrays, with the base type selected |
| 4748 | // according to the required alignment in the save area. |
| 4749 | else { |
| 4750 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4751 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4752 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4753 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4754 | } |
| 4755 | |
| 4756 | return ABIArgInfo::getDirect(CoerceTy); |
| 4757 | } |
| 4758 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4759 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4760 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4761 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4762 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4765 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4766 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | ABIArgInfo |
| 4770 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4771 | if (RetTy->isVoidType()) |
| 4772 | return ABIArgInfo::getIgnore(); |
| 4773 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4774 | if (RetTy->isAnyComplexType()) |
| 4775 | return ABIArgInfo::getDirect(); |
| 4776 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4777 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4778 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4779 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4780 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4781 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4782 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4783 | else if (Size < 128) { |
| 4784 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4785 | return ABIArgInfo::getDirect(CoerceTy); |
| 4786 | } |
| 4787 | } |
| 4788 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4789 | if (isAggregateTypeForABI(RetTy)) { |
| 4790 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4791 | const Type *Base = nullptr; |
| 4792 | uint64_t Members = 0; |
| 4793 | if (Kind == ELFv2 && |
| 4794 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4795 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4796 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4797 | return ABIArgInfo::getDirect(CoerceTy); |
| 4798 | } |
| 4799 | |
| 4800 | // ELFv2 small aggregates are returned in up to two registers. |
| 4801 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4802 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4803 | if (Bits == 0) |
| 4804 | return ABIArgInfo::getIgnore(); |
| 4805 | |
| 4806 | llvm::Type *CoerceTy; |
| 4807 | if (Bits > GPRBits) { |
| 4808 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4809 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4810 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4811 | CoerceTy = |
| 4812 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4813 | return ABIArgInfo::getDirect(CoerceTy); |
| 4814 | } |
| 4815 | |
| 4816 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4817 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4818 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4819 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4820 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4821 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4824 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4825 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4826 | QualType Ty) const { |
| 4827 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4828 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4829 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4830 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4831 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4832 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4833 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4834 | // in separate doublewords. However, Clang expects us to produce a |
| 4835 | // pointer to a structure with the two parts packed tightly. So generate |
| 4836 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4837 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4838 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4839 | CharUnits EltSize = TypeInfo.first / 2; |
| 4840 | if (EltSize < SlotSize) { |
| 4841 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4842 | SlotSize * 2, SlotSize, |
| 4843 | SlotSize, /*AllowHigher*/ true); |
| 4844 | |
| 4845 | Address RealAddr = Addr; |
| 4846 | Address ImagAddr = RealAddr; |
| 4847 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4848 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4849 | SlotSize - EltSize); |
| 4850 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4851 | 2 * SlotSize - EltSize); |
| 4852 | } else { |
| 4853 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4854 | } |
| 4855 | |
| 4856 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4857 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4858 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4859 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4860 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4861 | |
| 4862 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4863 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4864 | /*init*/ true); |
| 4865 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4866 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4869 | // Otherwise, just use the general rule. |
| 4870 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4871 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | static bool |
| 4875 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4876 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4877 | // This is calculated from the LLVM and GCC tables and verified |
| 4878 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4879 | |
| 4880 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4881 | |
| 4882 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4883 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4884 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4885 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4886 | |
| 4887 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4888 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4889 | |
| 4890 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4891 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4892 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4893 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4894 | // 64: mq |
| 4895 | // 65: lr |
| 4896 | // 66: ctr |
| 4897 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4898 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4899 | |
| 4900 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4901 | // 68-75 cr0-7 |
| 4902 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4903 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4904 | |
| 4905 | // 77-108: v0-31, the 16-byte vector registers |
| 4906 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4907 | |
| 4908 | // 109: vrsave |
| 4909 | // 110: vscr |
| 4910 | // 111: spe_acc |
| 4911 | // 112: spefscr |
| 4912 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4913 | // 114: tfhar |
| 4914 | // 115: tfiar |
| 4915 | // 116: texasr |
| 4916 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4917 | |
| 4918 | return false; |
| 4919 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4920 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4921 | bool |
| 4922 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4923 | CodeGen::CodeGenFunction &CGF, |
| 4924 | llvm::Value *Address) const { |
| 4925 | |
| 4926 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4927 | } |
| 4928 | |
| 4929 | bool |
| 4930 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4931 | llvm::Value *Address) const { |
| 4932 | |
| 4933 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4934 | } |
| 4935 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4936 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4937 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4938 | //===----------------------------------------------------------------------===// |
| 4939 | |
| 4940 | namespace { |
| 4941 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4942 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4943 | public: |
| 4944 | enum ABIKind { |
| 4945 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4946 | DarwinPCS, |
| 4947 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4948 | }; |
| 4949 | |
| 4950 | private: |
| 4951 | ABIKind Kind; |
| 4952 | |
| 4953 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4954 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4955 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4956 | |
| 4957 | private: |
| 4958 | ABIKind getABIKind() const { return Kind; } |
| 4959 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4960 | |
| 4961 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4962 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4963 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4964 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4965 | uint64_t Members) const override; |
| 4966 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4967 | bool isIllegalVectorType(QualType Ty) const; |
| 4968 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4969 | void computeInfo(CGFunctionInfo &FI) const override { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4970 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4971 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4972 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4973 | for (auto &it : FI.arguments()) |
| 4974 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4977 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4978 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4979 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4980 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4981 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4982 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4983 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4984 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4985 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4986 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4987 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4988 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4989 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4990 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4991 | QualType Ty) const override; |
| 4992 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 4993 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4994 | bool asReturnValue) const override { |
| 4995 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4996 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4997 | bool isSwiftErrorInRegister() const override { |
| 4998 | return true; |
| 4999 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5000 | |
| 5001 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5002 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5003 | }; |
| 5004 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5005 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5006 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5007 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 5008 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
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 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5011 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5014 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 5015 | return 31; |
| 5016 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5017 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5018 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5019 | |
| 5020 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5021 | CodeGen::CodeGenModule &CGM) const override { |
| 5022 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 5023 | if (!FD) |
| 5024 | return; |
| 5025 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5026 | |
| 5027 | auto Kind = CGM.getCodeGenOpts().getSignReturnAddress(); |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5028 | if (Kind != CodeGenOptions::SignReturnAddressScope::None) { |
| 5029 | Fn->addFnAttr("sign-return-address", |
| 5030 | Kind == CodeGenOptions::SignReturnAddressScope::All |
| 5031 | ? "all" |
| 5032 | : "non-leaf"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5033 | |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5034 | auto Key = CGM.getCodeGenOpts().getSignReturnAddressKey(); |
| 5035 | Fn->addFnAttr("sign-return-address-key", |
| 5036 | Key == CodeGenOptions::SignReturnAddressKeyValue::AKey |
| 5037 | ? "a_key" |
| 5038 | : "b_key"); |
| 5039 | } |
| 5040 | |
| 5041 | if (CGM.getCodeGenOpts().BranchTargetEnforcement) |
| 5042 | Fn->addFnAttr("branch-target-enforcement"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5043 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5044 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5045 | |
| 5046 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 5047 | public: |
| 5048 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 5049 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 5050 | |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5051 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5052 | CodeGen::CodeGenModule &CGM) const override; |
| 5053 | |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5054 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5055 | llvm::SmallString<24> &Opt) const override { |
| 5056 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5057 | } |
| 5058 | |
| 5059 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5060 | llvm::SmallString<32> &Opt) const override { |
| 5061 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5062 | } |
| 5063 | }; |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5064 | |
| 5065 | void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( |
| 5066 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5067 | AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5068 | if (GV->isDeclaration()) |
| 5069 | return; |
| 5070 | addStackProbeTargetAttributes(D, GV, CGM); |
| 5071 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5072 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5073 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5074 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5075 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5076 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5077 | // Handle illegal vector types here. |
| 5078 | if (isIllegalVectorType(Ty)) { |
| 5079 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5080 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 5081 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5082 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 5083 | return ABIArgInfo::getDirect(ResType); |
| 5084 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5085 | if (Size <= 32) { |
| 5086 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5087 | return ABIArgInfo::getDirect(ResType); |
| 5088 | } |
| 5089 | if (Size == 64) { |
| 5090 | llvm::Type *ResType = |
| 5091 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5092 | return ABIArgInfo::getDirect(ResType); |
| 5093 | } |
| 5094 | if (Size == 128) { |
| 5095 | llvm::Type *ResType = |
| 5096 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5097 | return ABIArgInfo::getDirect(ResType); |
| 5098 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5099 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5100 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5101 | |
| 5102 | if (!isAggregateTypeForABI(Ty)) { |
| 5103 | // Treat an enum type as its underlying type. |
| 5104 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5105 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5106 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5107 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5108 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5109 | : ABIArgInfo::getDirect()); |
| 5110 | } |
| 5111 | |
| 5112 | // Structures with either a non-trivial destructor or a non-trivial |
| 5113 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5114 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5115 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5116 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5120 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5121 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5122 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5123 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5124 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5125 | return ABIArgInfo::getIgnore(); |
| 5126 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5127 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5128 | // 0. |
| 5129 | if (IsEmpty && Size == 0) |
| 5130 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5131 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5132 | } |
| 5133 | |
| 5134 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5135 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5136 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5137 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5138 | return ABIArgInfo::getDirect( |
| 5139 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5143 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5144 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5145 | // same size and alignment. |
| 5146 | if (getTarget().isRenderScriptTarget()) { |
| 5147 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5148 | } |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5149 | unsigned Alignment; |
| 5150 | if (Kind == AArch64ABIInfo::AAPCS) { |
| 5151 | Alignment = getContext().getTypeUnadjustedAlign(Ty); |
| 5152 | Alignment = Alignment < 128 ? 64 : 128; |
| 5153 | } else { |
| 5154 | Alignment = getContext().getTypeAlign(Ty); |
| 5155 | } |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5156 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5157 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5158 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5159 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5160 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5161 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5162 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5163 | } |
| 5164 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5165 | } |
| 5166 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5167 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5170 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5171 | if (RetTy->isVoidType()) |
| 5172 | return ABIArgInfo::getIgnore(); |
| 5173 | |
| 5174 | // Large vector types should be returned via memory. |
| 5175 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5176 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5177 | |
| 5178 | if (!isAggregateTypeForABI(RetTy)) { |
| 5179 | // Treat an enum type as its underlying type. |
| 5180 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5181 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5182 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5183 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5184 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5185 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5188 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5189 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5190 | return ABIArgInfo::getIgnore(); |
| 5191 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5192 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5193 | uint64_t Members = 0; |
| 5194 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5195 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5196 | return ABIArgInfo::getDirect(); |
| 5197 | |
| 5198 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5199 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5200 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5201 | // same size and alignment. |
| 5202 | if (getTarget().isRenderScriptTarget()) { |
| 5203 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5204 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5205 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5206 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5207 | |
| 5208 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5209 | // For aggregates with 16-byte alignment, we use i128. |
| 5210 | if (Alignment < 128 && Size == 128) { |
| 5211 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5212 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5213 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5214 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5215 | } |
| 5216 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5217 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5220 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5221 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5222 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5223 | // Check whether VT is legal. |
| 5224 | unsigned NumElements = VT->getNumElements(); |
| 5225 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5226 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5227 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5228 | return true; |
| 5229 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5230 | } |
| 5231 | return false; |
| 5232 | } |
| 5233 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5234 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5235 | llvm::Type *eltTy, |
| 5236 | unsigned elts) const { |
| 5237 | if (!llvm::isPowerOf2_32(elts)) |
| 5238 | return false; |
| 5239 | if (totalSize.getQuantity() != 8 && |
| 5240 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5241 | return false; |
| 5242 | return true; |
| 5243 | } |
| 5244 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5245 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5246 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5247 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5248 | // but with the difference that any floating-point type is allowed, |
| 5249 | // including __fp16. |
| 5250 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5251 | if (BT->isFloatingPoint()) |
| 5252 | return true; |
| 5253 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5254 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5255 | if (VecSize == 64 || VecSize == 128) |
| 5256 | return true; |
| 5257 | } |
| 5258 | return false; |
| 5259 | } |
| 5260 | |
| 5261 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5262 | uint64_t Members) const { |
| 5263 | return Members <= 4; |
| 5264 | } |
| 5265 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5266 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5267 | QualType Ty, |
| 5268 | CodeGenFunction &CGF) const { |
| 5269 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5270 | bool IsIndirect = AI.isIndirect(); |
| 5271 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5272 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5273 | if (IsIndirect) |
| 5274 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5275 | else if (AI.getCoerceToType()) |
| 5276 | BaseTy = AI.getCoerceToType(); |
| 5277 | |
| 5278 | unsigned NumRegs = 1; |
| 5279 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5280 | BaseTy = ArrTy->getElementType(); |
| 5281 | NumRegs = ArrTy->getNumElements(); |
| 5282 | } |
| 5283 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5284 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5285 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5286 | // Standard, section B.4: |
| 5287 | // |
| 5288 | // struct { |
| 5289 | // void *__stack; |
| 5290 | // void *__gr_top; |
| 5291 | // void *__vr_top; |
| 5292 | // int __gr_offs; |
| 5293 | // int __vr_offs; |
| 5294 | // }; |
| 5295 | |
| 5296 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5297 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5298 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5299 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5300 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5301 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5302 | CharUnits TyAlign = TyInfo.second; |
| 5303 | |
| 5304 | Address reg_offs_p = Address::invalid(); |
| 5305 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5306 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5307 | CharUnits reg_top_offset; |
| 5308 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5309 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5310 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5311 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5312 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5313 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5314 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5315 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5316 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5317 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5318 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5319 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5320 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5321 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5322 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5323 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5324 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5325 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5326 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | //======================================= |
| 5330 | // Find out where argument was passed |
| 5331 | //======================================= |
| 5332 | |
| 5333 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5334 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5335 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5336 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5337 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5338 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5339 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5340 | |
| 5341 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5342 | |
| 5343 | // 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] | 5344 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5345 | CGF.EmitBlock(MaybeRegBlock); |
| 5346 | |
| 5347 | // Integer arguments may need to correct register alignment (for example a |
| 5348 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5349 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5350 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5351 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5352 | |
| 5353 | reg_offs = CGF.Builder.CreateAdd( |
| 5354 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5355 | "align_regoffs"); |
| 5356 | reg_offs = CGF.Builder.CreateAnd( |
| 5357 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5358 | "aligned_regoffs"); |
| 5359 | } |
| 5360 | |
| 5361 | // 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] | 5362 | // The fact that this is done unconditionally reflects the fact that |
| 5363 | // allocating an argument to the stack also uses up all the remaining |
| 5364 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5365 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5366 | NewOffset = CGF.Builder.CreateAdd( |
| 5367 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5368 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5369 | |
| 5370 | // Now we're in a position to decide whether this argument really was in |
| 5371 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5372 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5373 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5374 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5375 | |
| 5376 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5377 | |
| 5378 | //======================================= |
| 5379 | // Argument was in registers |
| 5380 | //======================================= |
| 5381 | |
| 5382 | // Now we emit the code for if the argument was originally passed in |
| 5383 | // registers. First start the appropriate block: |
| 5384 | CGF.EmitBlock(InRegBlock); |
| 5385 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5386 | llvm::Value *reg_top = nullptr; |
| 5387 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5388 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5389 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5390 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5391 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5392 | Address RegAddr = Address::invalid(); |
| 5393 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5394 | |
| 5395 | if (IsIndirect) { |
| 5396 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5397 | // stored registers or on the stack will actually be a struct **. |
| 5398 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5399 | } |
| 5400 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5401 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5402 | uint64_t NumMembers = 0; |
| 5403 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5404 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5405 | // Homogeneous aggregates passed in registers will have their elements split |
| 5406 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5407 | // qN+1, ...). We reload and store into a temporary local variable |
| 5408 | // contiguously. |
| 5409 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5410 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5411 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5412 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5413 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5414 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5415 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5416 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5417 | int Offset = 0; |
| 5418 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5419 | BaseTyInfo.first.getQuantity() < 16) |
| 5420 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5421 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5422 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5423 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5424 | Address LoadAddr = |
| 5425 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5426 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5427 | |
| 5428 | Address StoreAddr = |
| 5429 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5430 | |
| 5431 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5432 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5433 | } |
| 5434 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5435 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5436 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5437 | // Otherwise the object is contiguous in memory. |
| 5438 | |
| 5439 | // It might be right-aligned in its slot. |
| 5440 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5441 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5442 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5443 | TyInfo.first < SlotSize) { |
| 5444 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5445 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5448 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | CGF.EmitBranch(ContBlock); |
| 5452 | |
| 5453 | //======================================= |
| 5454 | // Argument was on the stack |
| 5455 | //======================================= |
| 5456 | CGF.EmitBlock(OnStackBlock); |
| 5457 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5458 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5459 | CharUnits::Zero(), "stack_p"); |
| 5460 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5461 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5462 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5463 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5464 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5465 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5466 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5467 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5468 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5469 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5470 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5471 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5472 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5473 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5474 | "align_stack"); |
| 5475 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5476 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5477 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5478 | Address OnStackAddr(OnStackPtr, |
| 5479 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5480 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5481 | // All stack slots are multiples of 8 bytes. |
| 5482 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5483 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5484 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5485 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5486 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5487 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5488 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5489 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5490 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5491 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5492 | |
| 5493 | // Write the new value of __stack for the next call to va_arg |
| 5494 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5495 | |
| 5496 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5497 | TyInfo.first < StackSlotSize) { |
| 5498 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5499 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5502 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5503 | |
| 5504 | CGF.EmitBranch(ContBlock); |
| 5505 | |
| 5506 | //======================================= |
| 5507 | // Tidy up |
| 5508 | //======================================= |
| 5509 | CGF.EmitBlock(ContBlock); |
| 5510 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5511 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5512 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5513 | |
| 5514 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5515 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5516 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5517 | |
| 5518 | return ResAddr; |
| 5519 | } |
| 5520 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5521 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5522 | CodeGenFunction &CGF) const { |
| 5523 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5524 | // illegal vector types. Lower VAArg here for these cases and use |
| 5525 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5526 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5527 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5528 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5529 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5530 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5531 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5532 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5533 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5534 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5535 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5538 | // The size of the actual thing passed, which might end up just |
| 5539 | // being a pointer for indirect types. |
| 5540 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5541 | |
| 5542 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5543 | // aggregates should be passed indirectly. |
| 5544 | bool IsIndirect = false; |
| 5545 | if (TyInfo.first.getQuantity() > 16) { |
| 5546 | const Type *Base = nullptr; |
| 5547 | uint64_t Members = 0; |
| 5548 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5551 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5552 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5553 | } |
| 5554 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5555 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5556 | QualType Ty) const { |
| 5557 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5558 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5559 | CharUnits::fromQuantity(8), |
| 5560 | /*allowHigherAlign*/ false); |
| 5561 | } |
| 5562 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5563 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5564 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5565 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5566 | |
| 5567 | namespace { |
| 5568 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5569 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5570 | public: |
| 5571 | enum ABIKind { |
| 5572 | APCS = 0, |
| 5573 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5574 | AAPCS_VFP = 2, |
| 5575 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5576 | }; |
| 5577 | |
| 5578 | private: |
| 5579 | ABIKind Kind; |
| 5580 | |
| 5581 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5582 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5583 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5584 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5585 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5586 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5587 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5588 | switch (getTarget().getTriple().getEnvironment()) { |
| 5589 | case llvm::Triple::Android: |
| 5590 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5591 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5592 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5593 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5594 | case llvm::Triple::MuslEABI: |
| 5595 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5596 | return true; |
| 5597 | default: |
| 5598 | return false; |
| 5599 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5602 | bool isEABIHF() const { |
| 5603 | switch (getTarget().getTriple().getEnvironment()) { |
| 5604 | case llvm::Triple::EABIHF: |
| 5605 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5606 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5607 | return true; |
| 5608 | default: |
| 5609 | return false; |
| 5610 | } |
| 5611 | } |
| 5612 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5613 | ABIKind getABIKind() const { return Kind; } |
| 5614 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5615 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5616 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5617 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5618 | ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, |
| 5619 | uint64_t Members) const; |
| 5620 | ABIArgInfo coerceIllegalVector(QualType Ty) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5621 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5622 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5623 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5624 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5625 | uint64_t Members) const override; |
| 5626 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5627 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5628 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5629 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5630 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5631 | |
| 5632 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5633 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5634 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5635 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5636 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5637 | bool asReturnValue) const override { |
| 5638 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5639 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5640 | bool isSwiftErrorInRegister() const override { |
| 5641 | return true; |
| 5642 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5643 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5644 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5645 | }; |
| 5646 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5647 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5648 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5649 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5650 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5651 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5652 | const ARMABIInfo &getABIInfo() const { |
| 5653 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5654 | } |
| 5655 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5656 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5657 | return 13; |
| 5658 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5659 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5660 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5661 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5664 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5665 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5666 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5667 | |
| 5668 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5669 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5670 | return false; |
| 5671 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5672 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5673 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5674 | if (getABIInfo().isEABI()) return 88; |
| 5675 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5676 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5677 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5678 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5679 | CodeGen::CodeGenModule &CGM) const override { |
| 5680 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5681 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5682 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5683 | if (!FD) |
| 5684 | return; |
| 5685 | |
| 5686 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5687 | if (!Attr) |
| 5688 | return; |
| 5689 | |
| 5690 | const char *Kind; |
| 5691 | switch (Attr->getInterrupt()) { |
| 5692 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5693 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5694 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5695 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5696 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5697 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5698 | } |
| 5699 | |
| 5700 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5701 | |
| 5702 | Fn->addFnAttr("interrupt", Kind); |
| 5703 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5704 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5705 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5706 | return; |
| 5707 | |
| 5708 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5709 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5710 | // the backend to perform a realignment as part of the function prologue. |
| 5711 | llvm::AttrBuilder B; |
| 5712 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5713 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5714 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5715 | }; |
| 5716 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5717 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5718 | public: |
| 5719 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5720 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5721 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5722 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5723 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5724 | |
| 5725 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5726 | llvm::SmallString<24> &Opt) const override { |
| 5727 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5728 | } |
| 5729 | |
| 5730 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5731 | llvm::SmallString<32> &Opt) const override { |
| 5732 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5733 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5734 | }; |
| 5735 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5736 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5737 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5738 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5739 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5740 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 5741 | addStackProbeTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5742 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5743 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5744 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5745 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5746 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5747 | FI.getReturnInfo() = |
| 5748 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5749 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5750 | for (auto &I : FI.arguments()) |
| 5751 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5752 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5753 | // Always honor user-specified calling convention. |
| 5754 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5755 | return; |
| 5756 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5757 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5758 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5759 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5760 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5761 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5762 | /// Return the default calling convention that LLVM will use. |
| 5763 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5764 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5765 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5766 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5767 | else if (isEABI()) |
| 5768 | return llvm::CallingConv::ARM_AAPCS; |
| 5769 | else |
| 5770 | return llvm::CallingConv::ARM_APCS; |
| 5771 | } |
| 5772 | |
| 5773 | /// Return the calling convention that our ABI would like us to use |
| 5774 | /// as the C calling convention. |
| 5775 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5776 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5777 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5778 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5779 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5780 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5781 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5782 | llvm_unreachable("bad ABI kind"); |
| 5783 | } |
| 5784 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5785 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5786 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5787 | |
| 5788 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5789 | // they'd just match what LLVM will infer from the triple. |
| 5790 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5791 | if (abiCC != getLLVMDefaultCC()) |
| 5792 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5795 | ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { |
| 5796 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5797 | if (Size <= 32) { |
| 5798 | llvm::Type *ResType = |
| 5799 | llvm::Type::getInt32Ty(getVMContext()); |
| 5800 | return ABIArgInfo::getDirect(ResType); |
| 5801 | } |
| 5802 | if (Size == 64 || Size == 128) { |
| 5803 | llvm::Type *ResType = llvm::VectorType::get( |
| 5804 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5805 | return ABIArgInfo::getDirect(ResType); |
| 5806 | } |
| 5807 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5808 | } |
| 5809 | |
| 5810 | ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, |
| 5811 | const Type *Base, |
| 5812 | uint64_t Members) const { |
| 5813 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 5814 | // Base can be a floating-point or a vector. |
| 5815 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 5816 | // FP16 vectors should be converted to integer vectors |
| 5817 | if (!getTarget().hasLegalHalfType() && |
| 5818 | (VT->getElementType()->isFloat16Type() || |
| 5819 | VT->getElementType()->isHalfType())) { |
| 5820 | uint64_t Size = getContext().getTypeSize(VT); |
| 5821 | llvm::Type *NewVecTy = llvm::VectorType::get( |
| 5822 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5823 | llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); |
| 5824 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5825 | } |
| 5826 | } |
| 5827 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 5828 | } |
| 5829 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5830 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5831 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5832 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5833 | // A single-precision floating-point type (including promoted |
| 5834 | // half-precision types); A double-precision floating-point type; |
| 5835 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5836 | // with a Base Type of a single- or double-precision floating-point type, |
| 5837 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5838 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5839 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5840 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5841 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5842 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5843 | // Handle illegal vector types here. |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5844 | if (isIllegalVectorType(Ty)) |
| 5845 | return coerceIllegalVector(Ty); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5846 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5847 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5848 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5849 | // half type natively, and does not need to interwork with AAPCS code. |
| 5850 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5851 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5852 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5853 | llvm::Type::getFloatTy(getVMContext()) : |
| 5854 | llvm::Type::getInt32Ty(getVMContext()); |
| 5855 | return ABIArgInfo::getDirect(ResType); |
| 5856 | } |
| 5857 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5858 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5859 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5860 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5861 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5862 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5863 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5864 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5865 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5866 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5867 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5868 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5869 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5870 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5871 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5872 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5873 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5874 | return ABIArgInfo::getIgnore(); |
| 5875 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5876 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5877 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5878 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5879 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5880 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5881 | if (isHomogeneousAggregate(Ty, Base, Members)) |
| 5882 | return classifyHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5883 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5884 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5885 | // this convention even for a variadic function: the backend will use GPRs |
| 5886 | // if needed. |
| 5887 | const Type *Base = nullptr; |
| 5888 | uint64_t Members = 0; |
| 5889 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5890 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5891 | llvm::Type *Ty = |
| 5892 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5893 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5894 | } |
| 5895 | } |
| 5896 | |
| 5897 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5898 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5899 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5900 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5901 | // and a pointer is passed. |
| 5902 | return ABIArgInfo::getIndirect( |
| 5903 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5904 | } |
| 5905 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5906 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5907 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5908 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5909 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5910 | uint64_t ABIAlign = 4; |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5911 | uint64_t TyAlign; |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5912 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5913 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5914 | TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5915 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5916 | } else { |
| 5917 | TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 5918 | } |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5919 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5920 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5921 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5922 | /*ByVal=*/true, |
| 5923 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5926 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5927 | // same size and alignment. |
| 5928 | if (getTarget().isRenderScriptTarget()) { |
| 5929 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5930 | } |
| 5931 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5932 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5933 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5934 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5935 | // FIXME: Try to match the types of the arguments more accurately where |
| 5936 | // we can. |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5937 | if (TyAlign <= 4) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5938 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5939 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5940 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5941 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5942 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5943 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5944 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5945 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5948 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5949 | llvm::LLVMContext &VMContext) { |
| 5950 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5951 | // is called integer-like if its size is less than or equal to one word, and |
| 5952 | // the offset of each of its addressable sub-fields is zero. |
| 5953 | |
| 5954 | uint64_t Size = Context.getTypeSize(Ty); |
| 5955 | |
| 5956 | // Check that the type fits in a word. |
| 5957 | if (Size > 32) |
| 5958 | return false; |
| 5959 | |
| 5960 | // FIXME: Handle vector types! |
| 5961 | if (Ty->isVectorType()) |
| 5962 | return false; |
| 5963 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5964 | // Float types are never treated as "integer like". |
| 5965 | if (Ty->isRealFloatingType()) |
| 5966 | return false; |
| 5967 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5968 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5969 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5970 | return true; |
| 5971 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5972 | // Small complex integer types are "integer like". |
| 5973 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5974 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5975 | |
| 5976 | // Single element and zero sized arrays should be allowed, by the definition |
| 5977 | // above, but they are not. |
| 5978 | |
| 5979 | // Otherwise, it must be a record type. |
| 5980 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5981 | if (!RT) return false; |
| 5982 | |
| 5983 | // Ignore records with flexible arrays. |
| 5984 | const RecordDecl *RD = RT->getDecl(); |
| 5985 | if (RD->hasFlexibleArrayMember()) |
| 5986 | return false; |
| 5987 | |
| 5988 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5989 | // like". |
| 5990 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5991 | |
| 5992 | bool HadField = false; |
| 5993 | unsigned idx = 0; |
| 5994 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5995 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5996 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5997 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5998 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5999 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 6000 | // struct { int : 0; int x } |
| 6001 | // is non-integer like according to gcc. |
| 6002 | if (FD->isBitField()) { |
| 6003 | if (!RD->isUnion()) |
| 6004 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6005 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6006 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6007 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6008 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6009 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6012 | // Check if this field is at offset 0. |
| 6013 | if (Layout.getFieldOffset(idx) != 0) |
| 6014 | return false; |
| 6015 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6016 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6017 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 6018 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6019 | // Only allow at most one field in a structure. This doesn't match the |
| 6020 | // wording above, but follows gcc in situations with a field following an |
| 6021 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6022 | if (!RD->isUnion()) { |
| 6023 | if (HadField) |
| 6024 | return false; |
| 6025 | |
| 6026 | HadField = true; |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | return true; |
| 6031 | } |
| 6032 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6033 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 6034 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6035 | bool IsEffectivelyAAPCS_VFP = |
| 6036 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6037 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6038 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6039 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6040 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6041 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| 6042 | // Large vector types should be returned via memory. |
| 6043 | if (getContext().getTypeSize(RetTy) > 128) |
| 6044 | return getNaturalAlignIndirect(RetTy); |
| 6045 | // FP16 vectors should be converted to integer vectors |
| 6046 | if (!getTarget().hasLegalHalfType() && |
| 6047 | (VT->getElementType()->isFloat16Type() || |
| 6048 | VT->getElementType()->isHalfType())) |
| 6049 | return coerceIllegalVector(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6050 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 6051 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 6052 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 6053 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 6054 | // half type natively, and does not need to interwork with AAPCS code. |
| 6055 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 6056 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 6057 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 6058 | llvm::Type::getFloatTy(getVMContext()) : |
| 6059 | llvm::Type::getInt32Ty(getVMContext()); |
| 6060 | return ABIArgInfo::getDirect(ResType); |
| 6061 | } |
| 6062 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 6063 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6064 | // Treat an enum type as its underlying type. |
| 6065 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6066 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6067 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6068 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6069 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6070 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6071 | |
| 6072 | // Are we following APCS? |
| 6073 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6074 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6075 | return ABIArgInfo::getIgnore(); |
| 6076 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6077 | // Complex types are all returned as packed integers. |
| 6078 | // |
| 6079 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 6080 | // correctly. |
| 6081 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6082 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 6083 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6084 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6085 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6086 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6087 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6088 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6089 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6090 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6091 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6092 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6093 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6094 | } |
| 6095 | |
| 6096 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6097 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6098 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6099 | |
| 6100 | // Otherwise this is an AAPCS variant. |
| 6101 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6102 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6103 | return ABIArgInfo::getIgnore(); |
| 6104 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6105 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6106 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6107 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6108 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6109 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
| 6110 | return classifyHomogeneousAggregate(RetTy, Base, Members); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6113 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 6114 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6115 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6116 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 6117 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 6118 | // same size and alignment. |
| 6119 | if (getTarget().isRenderScriptTarget()) { |
| 6120 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 6121 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6122 | if (getDataLayout().isBigEndian()) |
| 6123 | // 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] | 6124 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6125 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6126 | // Return in the smallest viable integer type. |
| 6127 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6128 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6129 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6130 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6131 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6132 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6133 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6134 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6135 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6136 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6137 | } |
| 6138 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6139 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6142 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6143 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6144 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6145 | // On targets that don't support FP16, FP16 is expanded into float, and we |
| 6146 | // don't want the ABI to depend on whether or not FP16 is supported in |
| 6147 | // hardware. Thus return false to coerce FP16 vectors into integer vectors. |
| 6148 | if (!getTarget().hasLegalHalfType() && |
| 6149 | (VT->getElementType()->isFloat16Type() || |
| 6150 | VT->getElementType()->isHalfType())) |
| 6151 | return true; |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6152 | if (isAndroid()) { |
| 6153 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6154 | // vector ABI. The primary differences were that 3-element vector types |
| 6155 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6156 | // accepts that legacy behavior for Android only. |
| 6157 | // Check whether VT is legal. |
| 6158 | unsigned NumElements = VT->getNumElements(); |
| 6159 | // NumElements should be power of 2 or equal to 3. |
| 6160 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6161 | return true; |
| 6162 | } else { |
| 6163 | // Check whether VT is legal. |
| 6164 | unsigned NumElements = VT->getNumElements(); |
| 6165 | uint64_t Size = getContext().getTypeSize(VT); |
| 6166 | // NumElements should be power of 2. |
| 6167 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6168 | return true; |
| 6169 | // Size should be greater than 32 bits. |
| 6170 | return Size <= 32; |
| 6171 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6172 | } |
| 6173 | return false; |
| 6174 | } |
| 6175 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6176 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6177 | llvm::Type *eltTy, |
| 6178 | unsigned numElts) const { |
| 6179 | if (!llvm::isPowerOf2_32(numElts)) |
| 6180 | return false; |
| 6181 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6182 | if (size > 64) |
| 6183 | return false; |
| 6184 | if (vectorSize.getQuantity() != 8 && |
| 6185 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6186 | return false; |
| 6187 | return true; |
| 6188 | } |
| 6189 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6190 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6191 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6192 | // double, or 64-bit or 128-bit vectors. |
| 6193 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6194 | if (BT->getKind() == BuiltinType::Float || |
| 6195 | BT->getKind() == BuiltinType::Double || |
| 6196 | BT->getKind() == BuiltinType::LongDouble) |
| 6197 | return true; |
| 6198 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6199 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6200 | if (VecSize == 64 || VecSize == 128) |
| 6201 | return true; |
| 6202 | } |
| 6203 | return false; |
| 6204 | } |
| 6205 | |
| 6206 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6207 | uint64_t Members) const { |
| 6208 | return Members <= 4; |
| 6209 | } |
| 6210 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6211 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6212 | QualType Ty) const { |
| 6213 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6214 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6215 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6216 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6217 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6218 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6219 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6220 | } |
| 6221 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6222 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6223 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6224 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6225 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6226 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6227 | const Type *Base = nullptr; |
| 6228 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6229 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6230 | IsIndirect = true; |
| 6231 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6232 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6233 | // allocated by the caller. |
| 6234 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6235 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6236 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6237 | IsIndirect = true; |
| 6238 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6239 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6240 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6241 | // 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] | 6242 | // Our callers should be prepared to handle an under-aligned address. |
| 6243 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6244 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6245 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6246 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6247 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6248 | // ARMv7k allows type alignment up to 16 bytes. |
| 6249 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6250 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6251 | } else { |
| 6252 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6253 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6254 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6255 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6256 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6257 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6260 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6261 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6262 | //===----------------------------------------------------------------------===// |
| 6263 | |
| 6264 | namespace { |
| 6265 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6266 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6267 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6268 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6269 | |
| 6270 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6271 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6272 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6273 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6274 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6275 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6276 | }; |
| 6277 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6278 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6279 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6280 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6281 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6282 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6283 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6284 | CodeGen::CodeGenModule &M) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6285 | bool shouldEmitStaticExternCAliases() const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6286 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6287 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6288 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6289 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6290 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6291 | }; |
| 6292 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6293 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6294 | if (RetTy->isVoidType()) |
| 6295 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6296 | |
| 6297 | // note: this is different from default ABI |
| 6298 | if (!RetTy->isScalarType()) |
| 6299 | return ABIArgInfo::getDirect(); |
| 6300 | |
| 6301 | // Treat an enum type as its underlying type. |
| 6302 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6303 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6304 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6305 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6306 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6307 | } |
| 6308 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6309 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6310 | // Treat an enum type as its underlying type. |
| 6311 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6312 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6313 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6314 | // Return aggregates type as indirect by value |
| 6315 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6316 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6317 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6318 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6319 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6320 | } |
| 6321 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6322 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6323 | if (!getCXXABI().classifyReturnType(FI)) |
| 6324 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6325 | for (auto &I : FI.arguments()) |
| 6326 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6327 | |
| 6328 | // Always honor user-specified calling convention. |
| 6329 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6330 | return; |
| 6331 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6332 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6333 | } |
| 6334 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6335 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6336 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6337 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6338 | } |
| 6339 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6340 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6341 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6342 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6343 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6344 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6345 | if (!FD) return; |
| 6346 | |
| 6347 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6348 | |
| 6349 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6350 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6351 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6352 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6353 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6354 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6355 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6356 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6357 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6358 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6359 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6360 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6361 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6362 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6363 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6364 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6365 | // __global__ functions cannot be called from the device, we do not |
| 6366 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6367 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6368 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6369 | addNVVMMetadata(F, "kernel", 1); |
| 6370 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6371 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6372 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6373 | llvm::APSInt MaxThreads(32); |
| 6374 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6375 | if (MaxThreads > 0) |
| 6376 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6377 | |
| 6378 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6379 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6380 | // we don't have to add a PTX directive. |
| 6381 | if (Attr->getMinBlocks()) { |
| 6382 | llvm::APSInt MinBlocks(32); |
| 6383 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6384 | if (MinBlocks > 0) |
| 6385 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6386 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6387 | } |
| 6388 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6389 | } |
| 6390 | } |
| 6391 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6392 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6393 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6394 | llvm::Module *M = F->getParent(); |
| 6395 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6396 | |
| 6397 | // Get "nvvm.annotations" metadata node |
| 6398 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6399 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6400 | llvm::Metadata *MDVals[] = { |
| 6401 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6402 | llvm::ConstantAsMetadata::get( |
| 6403 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6404 | // Append metadata to nvvm.annotations |
| 6405 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6406 | } |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6407 | |
| 6408 | bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 6409 | return false; |
| 6410 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6411 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6412 | |
| 6413 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6414 | // SystemZ ABI Implementation |
| 6415 | //===----------------------------------------------------------------------===// |
| 6416 | |
| 6417 | namespace { |
| 6418 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6419 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6420 | bool HasVector; |
| 6421 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6422 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6423 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6424 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6425 | |
| 6426 | bool isPromotableIntegerType(QualType Ty) const; |
| 6427 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6428 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6429 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6430 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6431 | |
| 6432 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6433 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6434 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6435 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6436 | if (!getCXXABI().classifyReturnType(FI)) |
| 6437 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6438 | for (auto &I : FI.arguments()) |
| 6439 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6440 | } |
| 6441 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6442 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6443 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6444 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6445 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6446 | bool asReturnValue) const override { |
| 6447 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6448 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6449 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6450 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6451 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6452 | }; |
| 6453 | |
| 6454 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6455 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6456 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6457 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6458 | }; |
| 6459 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6460 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6461 | |
| 6462 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6463 | // Treat an enum type as its underlying type. |
| 6464 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6465 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6466 | |
| 6467 | // Promotable integer types are required to be promoted by the ABI. |
| 6468 | if (Ty->isPromotableIntegerType()) |
| 6469 | return true; |
| 6470 | |
| 6471 | // 32-bit values must also be promoted. |
| 6472 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6473 | switch (BT->getKind()) { |
| 6474 | case BuiltinType::Int: |
| 6475 | case BuiltinType::UInt: |
| 6476 | return true; |
| 6477 | default: |
| 6478 | return false; |
| 6479 | } |
| 6480 | return false; |
| 6481 | } |
| 6482 | |
| 6483 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6484 | return (Ty->isAnyComplexType() || |
| 6485 | Ty->isVectorType() || |
| 6486 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6489 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6490 | return (HasVector && |
| 6491 | Ty->isVectorType() && |
| 6492 | getContext().getTypeSize(Ty) <= 128); |
| 6493 | } |
| 6494 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6495 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6496 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6497 | switch (BT->getKind()) { |
| 6498 | case BuiltinType::Float: |
| 6499 | case BuiltinType::Double: |
| 6500 | return true; |
| 6501 | default: |
| 6502 | return false; |
| 6503 | } |
| 6504 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6505 | return false; |
| 6506 | } |
| 6507 | |
| 6508 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6509 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6510 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6511 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6512 | |
| 6513 | // If this is a C++ record, check the bases first. |
| 6514 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6515 | for (const auto &I : CXXRD->bases()) { |
| 6516 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6517 | |
| 6518 | // Empty bases don't affect things either way. |
| 6519 | if (isEmptyRecord(getContext(), Base, true)) |
| 6520 | continue; |
| 6521 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6522 | if (!Found.isNull()) |
| 6523 | return Ty; |
| 6524 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6525 | } |
| 6526 | |
| 6527 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6528 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6529 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6530 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6531 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6532 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 6533 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6534 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6535 | |
| 6536 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6537 | // Nested structures still do though. |
| 6538 | if (!Found.isNull()) |
| 6539 | return Ty; |
| 6540 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6541 | } |
| 6542 | |
| 6543 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6544 | // 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] | 6545 | if (!Found.isNull()) |
| 6546 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6547 | } |
| 6548 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6549 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6552 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6553 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6554 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6555 | // struct { |
| 6556 | // i64 __gpr; |
| 6557 | // i64 __fpr; |
| 6558 | // i8 *__overflow_arg_area; |
| 6559 | // i8 *__reg_save_area; |
| 6560 | // }; |
| 6561 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6562 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6563 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6564 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6565 | Ty = getContext().getCanonicalType(Ty); |
| 6566 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6567 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6568 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6569 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6570 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6571 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6572 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6573 | CharUnits UnpaddedSize; |
| 6574 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6575 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6576 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6577 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6578 | } else { |
| 6579 | if (AI.getCoerceToType()) |
| 6580 | ArgTy = AI.getCoerceToType(); |
| 6581 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6582 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6583 | UnpaddedSize = TyInfo.first; |
| 6584 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6585 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6586 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6587 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6588 | PaddedSize = CharUnits::fromQuantity(16); |
| 6589 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6590 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6591 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6592 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6593 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6594 | llvm::Value *PaddedSizeV = |
| 6595 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6596 | |
| 6597 | if (IsVector) { |
| 6598 | // Work out the address of a vector argument on the stack. |
| 6599 | // Vector arguments are always passed in the high bits of a |
| 6600 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6601 | Address OverflowArgAreaPtr = |
| 6602 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6603 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6604 | Address OverflowArgArea = |
| 6605 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6606 | TyInfo.second); |
| 6607 | Address MemAddr = |
| 6608 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6609 | |
| 6610 | // Update overflow_arg_area_ptr pointer |
| 6611 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6612 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6613 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6614 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6615 | |
| 6616 | return MemAddr; |
| 6617 | } |
| 6618 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6619 | assert(PaddedSize.getQuantity() == 8); |
| 6620 | |
| 6621 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6622 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6623 | if (InFPRs) { |
| 6624 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6625 | RegCountField = 1; // __fpr |
| 6626 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6627 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6628 | } else { |
| 6629 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6630 | RegCountField = 0; // __gpr |
| 6631 | RegSaveIndex = 2; // save offset for r2 |
| 6632 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6633 | } |
| 6634 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6635 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6636 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6637 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6638 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6639 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6640 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6641 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6642 | |
| 6643 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6644 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6645 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6646 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6647 | |
| 6648 | // Emit code to load the value if it was passed in registers. |
| 6649 | CGF.EmitBlock(InRegBlock); |
| 6650 | |
| 6651 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6652 | llvm::Value *ScaledRegCount = |
| 6653 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6654 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6655 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6656 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6657 | llvm::Value *RegOffset = |
| 6658 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6659 | Address RegSaveAreaPtr = |
| 6660 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6661 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6662 | llvm::Value *RegSaveArea = |
| 6663 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6664 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6665 | "raw_reg_addr"), |
| 6666 | PaddedSize); |
| 6667 | Address RegAddr = |
| 6668 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6669 | |
| 6670 | // Update the register count |
| 6671 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6672 | llvm::Value *NewRegCount = |
| 6673 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6674 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6675 | CGF.EmitBranch(ContBlock); |
| 6676 | |
| 6677 | // Emit code to load the value if it was passed in memory. |
| 6678 | CGF.EmitBlock(InMemBlock); |
| 6679 | |
| 6680 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6681 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6682 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6683 | Address OverflowArgArea = |
| 6684 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6685 | PaddedSize); |
| 6686 | Address RawMemAddr = |
| 6687 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6688 | Address MemAddr = |
| 6689 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6690 | |
| 6691 | // Update overflow_arg_area_ptr pointer |
| 6692 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6693 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6694 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6695 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6696 | CGF.EmitBranch(ContBlock); |
| 6697 | |
| 6698 | // Return the appropriate result. |
| 6699 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6700 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6701 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6702 | |
| 6703 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6704 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6705 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6706 | |
| 6707 | return ResAddr; |
| 6708 | } |
| 6709 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6710 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6711 | if (RetTy->isVoidType()) |
| 6712 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6713 | if (isVectorArgumentType(RetTy)) |
| 6714 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6715 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6716 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6717 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6718 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
| 6721 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6722 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6723 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6724 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6725 | |
| 6726 | // Integers and enums are extended to full register width. |
| 6727 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6728 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6729 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6730 | // Handle vector types and vector-like structure types. Note that |
| 6731 | // as opposed to float-like structure types, we do not allow any |
| 6732 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6733 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6734 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6735 | if (isVectorArgumentType(SingleElementTy) && |
| 6736 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6737 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6738 | |
| 6739 | // 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] | 6740 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6741 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6742 | |
| 6743 | // Handle small structures. |
| 6744 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6745 | // Structures with flexible arrays have variable length, so really |
| 6746 | // fail the size test above. |
| 6747 | const RecordDecl *RD = RT->getDecl(); |
| 6748 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6749 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6750 | |
| 6751 | // The structure is passed as an unextended integer, a float, or a double. |
| 6752 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6753 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6754 | assert(Size == 32 || Size == 64); |
| 6755 | if (Size == 32) |
| 6756 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6757 | else |
| 6758 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6759 | } else |
| 6760 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6761 | return ABIArgInfo::getDirect(PassTy); |
| 6762 | } |
| 6763 | |
| 6764 | // Non-structure compounds are passed indirectly. |
| 6765 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6766 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6767 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6768 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6769 | } |
| 6770 | |
| 6771 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6772 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6773 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6774 | |
| 6775 | namespace { |
| 6776 | |
| 6777 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6778 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6779 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6780 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6781 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6782 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6783 | }; |
| 6784 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6785 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6786 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6787 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6788 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6789 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6790 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6791 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6792 | const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); |
| 6793 | if (!InterruptAttr) |
| 6794 | return; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6795 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6796 | // Handle 'interrupt' attribute: |
| 6797 | llvm::Function *F = cast<llvm::Function>(GV); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6798 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6799 | // Step 1: Set ISR calling convention. |
| 6800 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6801 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6802 | // Step 2: Add attributes goodness. |
| 6803 | F->addFnAttr(llvm::Attribute::NoInline); |
| 6804 | F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6805 | } |
| 6806 | } |
| 6807 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6808 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6809 | // MIPS ABI Implementation. This works for both little-endian and |
| 6810 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6811 | //===----------------------------------------------------------------------===// |
| 6812 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6813 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6814 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6815 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6816 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6817 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6818 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6819 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6820 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6821 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6822 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6823 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6824 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6825 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6826 | |
| 6827 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6828 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6829 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6830 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6831 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6832 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6833 | }; |
| 6834 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6835 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6836 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6837 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6838 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6839 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6840 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6841 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6842 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6843 | return 29; |
| 6844 | } |
| 6845 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6846 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6847 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6848 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6849 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6850 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6851 | |
| 6852 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6853 | Fn->addFnAttr("long-call"); |
| 6854 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6855 | Fn->addFnAttr("short-call"); |
| 6856 | |
| 6857 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6858 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6859 | return; |
| 6860 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6861 | if (FD->hasAttr<Mips16Attr>()) { |
| 6862 | Fn->addFnAttr("mips16"); |
| 6863 | } |
| 6864 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6865 | Fn->addFnAttr("nomips16"); |
| 6866 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6867 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6868 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6869 | Fn->addFnAttr("micromips"); |
| 6870 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6871 | Fn->addFnAttr("nomicromips"); |
| 6872 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6873 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6874 | if (!Attr) |
| 6875 | return; |
| 6876 | |
| 6877 | const char *Kind; |
| 6878 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6879 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6880 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6881 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6882 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6883 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6884 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6885 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6886 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6887 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6888 | } |
| 6889 | |
| 6890 | Fn->addFnAttr("interrupt", Kind); |
| 6891 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6892 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6893 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6894 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6895 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6896 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6897 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6898 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6899 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6900 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6901 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6902 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6903 | void MipsABIInfo::CoerceToIntArgs( |
| 6904 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6905 | llvm::IntegerType *IntTy = |
| 6906 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6907 | |
| 6908 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6909 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6910 | ArgList.push_back(IntTy); |
| 6911 | |
| 6912 | // If necessary, add one more integer type to ArgList. |
| 6913 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6914 | |
| 6915 | if (R) |
| 6916 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6917 | } |
| 6918 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6919 | // In N32/64, an aligned double precision floating point field is passed in |
| 6920 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6921 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6922 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6923 | |
| 6924 | if (IsO32) { |
| 6925 | CoerceToIntArgs(TySize, ArgList); |
| 6926 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6927 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6928 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6929 | if (Ty->isComplexType()) |
| 6930 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6931 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6932 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6933 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6934 | // Unions/vectors are passed in integer registers. |
| 6935 | if (!RT || !RT->isStructureOrClassType()) { |
| 6936 | CoerceToIntArgs(TySize, ArgList); |
| 6937 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6938 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6939 | |
| 6940 | const RecordDecl *RD = RT->getDecl(); |
| 6941 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6942 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6943 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6944 | uint64_t LastOffset = 0; |
| 6945 | unsigned idx = 0; |
| 6946 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6947 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6948 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6949 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6950 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6951 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6952 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6953 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6954 | |
| 6955 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6956 | continue; |
| 6957 | |
| 6958 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6959 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6960 | continue; |
| 6961 | |
| 6962 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6963 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6964 | ArgList.push_back(I64); |
| 6965 | |
| 6966 | // Add double type. |
| 6967 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6968 | LastOffset = Offset + 64; |
| 6969 | } |
| 6970 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6971 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6972 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6973 | |
| 6974 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6975 | } |
| 6976 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6977 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6978 | uint64_t Offset) const { |
| 6979 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6980 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6981 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6982 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6983 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6984 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6985 | ABIArgInfo |
| 6986 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6987 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6988 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6989 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6990 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6991 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6992 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6993 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6994 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6995 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6996 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6997 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6998 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6999 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7000 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7001 | return ABIArgInfo::getIgnore(); |
| 7002 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 7003 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7004 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7005 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7006 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 7007 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7008 | // If we have reached here, aggregates are passed directly by coercing to |
| 7009 | // another structure type. Padding is inserted if the offset of the |
| 7010 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 7011 | ABIArgInfo ArgInfo = |
| 7012 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 7013 | getPaddingType(OrigOffset, CurrOffset)); |
| 7014 | ArgInfo.setInReg(true); |
| 7015 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7016 | } |
| 7017 | |
| 7018 | // Treat an enum type as its underlying type. |
| 7019 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7020 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7021 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 7022 | // All integral types are promoted to the GPR width. |
| 7023 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7024 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7025 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7026 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7027 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7028 | } |
| 7029 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7030 | llvm::Type* |
| 7031 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7032 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7033 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7034 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7035 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7036 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7037 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 7038 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7039 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7040 | // N32/64 returns struct/classes in floating point registers if the |
| 7041 | // following conditions are met: |
| 7042 | // 1. The size of the struct/class is no larger than 128-bit. |
| 7043 | // 2. The struct/class has one or two fields all of which are floating |
| 7044 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7045 | // 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] | 7046 | // |
| 7047 | // Any other composite results are returned in integer registers. |
| 7048 | // |
| 7049 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 7050 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 7051 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7052 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7053 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7054 | if (!BT || !BT->isFloatingPoint()) |
| 7055 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7056 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7057 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
| 7060 | if (b == e) |
| 7061 | return llvm::StructType::get(getVMContext(), RTList, |
| 7062 | RD->hasAttr<PackedAttr>()); |
| 7063 | |
| 7064 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7065 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7066 | } |
| 7067 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7068 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7069 | return llvm::StructType::get(getVMContext(), RTList); |
| 7070 | } |
| 7071 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7072 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 7073 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7074 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 7075 | if (RetTy->isVoidType()) |
| 7076 | return ABIArgInfo::getIgnore(); |
| 7077 | |
| 7078 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 7079 | // However, N32/N64 ignores zero sized return values. |
| 7080 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7081 | return ABIArgInfo::getIgnore(); |
| 7082 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 7083 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7084 | if (Size <= 128) { |
| 7085 | if (RetTy->isAnyComplexType()) |
| 7086 | return ABIArgInfo::getDirect(); |
| 7087 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7088 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 7089 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7090 | if (!IsO32 || |
| 7091 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 7092 | ABIArgInfo ArgInfo = |
| 7093 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 7094 | ArgInfo.setInReg(true); |
| 7095 | return ArgInfo; |
| 7096 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7097 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7098 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7099 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7100 | } |
| 7101 | |
| 7102 | // Treat an enum type as its underlying type. |
| 7103 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7104 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7105 | |
Stefan Maksimovic | b9da8a5 | 2018-07-30 10:44:46 +0000 | [diff] [blame] | 7106 | if (RetTy->isPromotableIntegerType()) |
| 7107 | return ABIArgInfo::getExtend(RetTy); |
| 7108 | |
| 7109 | if ((RetTy->isUnsignedIntegerOrEnumerationType() || |
| 7110 | RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) |
| 7111 | return ABIArgInfo::getSignExtend(RetTy); |
| 7112 | |
| 7113 | return ABIArgInfo::getDirect(); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
| 7116 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7117 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7118 | if (!getCXXABI().classifyReturnType(FI)) |
| 7119 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7120 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7121 | // 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] | 7122 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7123 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7124 | for (auto &I : FI.arguments()) |
| 7125 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7126 | } |
| 7127 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7128 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7129 | QualType OrigTy) const { |
| 7130 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7131 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7132 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 7133 | // 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] | 7134 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7135 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7136 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7137 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7138 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7139 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7140 | DidPromote = true; |
| 7141 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7142 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7143 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7144 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7145 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7146 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7147 | // The alignment of things in the argument area is never larger than |
| 7148 | // StackAlignInBytes. |
| 7149 | TyInfo.second = |
| 7150 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7151 | |
| 7152 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7153 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7154 | |
| 7155 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7156 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7157 | |
| 7158 | |
| 7159 | // If there was a promotion, "unpromote" into a temporary. |
| 7160 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7161 | if (DidPromote) { |
| 7162 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7163 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7164 | |
| 7165 | // Truncate down to the right width. |
| 7166 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7167 | : CGF.IntPtrTy); |
| 7168 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7169 | if (OrigTy->isPointerType()) |
| 7170 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7171 | |
| 7172 | CGF.Builder.CreateStore(V, Temp); |
| 7173 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7174 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7175 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7176 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7177 | } |
| 7178 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7179 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7180 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7181 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7182 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7183 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7184 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7185 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7186 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7187 | } |
| 7188 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7189 | bool |
| 7190 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7191 | llvm::Value *Address) const { |
| 7192 | // This information comes from gcc's implementation, which seems to |
| 7193 | // as canonical as it gets. |
| 7194 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7195 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7196 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7197 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7198 | |
| 7199 | // 0-31 are the general purpose registers, $0 - $31. |
| 7200 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7201 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7202 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7203 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7204 | |
| 7205 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7206 | // They are one bit wide and ignored here. |
| 7207 | |
| 7208 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7209 | // (coprocessor 1 is the FP unit) |
| 7210 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7211 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7212 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7213 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7214 | return false; |
| 7215 | } |
| 7216 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7217 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7218 | // AVR ABI Implementation. |
| 7219 | //===----------------------------------------------------------------------===// |
| 7220 | |
| 7221 | namespace { |
| 7222 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7223 | public: |
| 7224 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7225 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7226 | |
| 7227 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7228 | CodeGen::CodeGenModule &CGM) const override { |
| 7229 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7230 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7231 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7232 | if (!FD) return; |
| 7233 | auto *Fn = cast<llvm::Function>(GV); |
| 7234 | |
| 7235 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7236 | Fn->addFnAttr("interrupt"); |
| 7237 | |
| 7238 | if (FD->getAttr<AVRSignalAttr>()) |
| 7239 | Fn->addFnAttr("signal"); |
| 7240 | } |
| 7241 | }; |
| 7242 | } |
| 7243 | |
| 7244 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7245 | // 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] | 7246 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7247 | // handling. |
| 7248 | //===----------------------------------------------------------------------===// |
| 7249 | |
| 7250 | namespace { |
| 7251 | |
| 7252 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7253 | public: |
| 7254 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7255 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7256 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7257 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7258 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7259 | }; |
| 7260 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7261 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7262 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7263 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7264 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7265 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7266 | if (!FD) return; |
| 7267 | |
| 7268 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7269 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7270 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7271 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7272 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7273 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7274 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7275 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7276 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7277 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7278 | llvm::NamedMDNode *OpenCLMetadata = |
| 7279 | M.getModule().getOrInsertNamedMetadata( |
| 7280 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7281 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7282 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7283 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7284 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7285 | Operands.push_back( |
| 7286 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7287 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7288 | Operands.push_back( |
| 7289 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7290 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7291 | Operands.push_back( |
| 7292 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7293 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7294 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7295 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7296 | // (false) for implementing the work_group_size_hint attr later. |
| 7297 | // 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] | 7298 | Operands.push_back( |
| 7299 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7300 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7301 | } |
| 7302 | } |
| 7303 | } |
| 7304 | } |
| 7305 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7306 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7307 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7308 | //===----------------------------------------------------------------------===// |
| 7309 | // Hexagon ABI Implementation |
| 7310 | //===----------------------------------------------------------------------===// |
| 7311 | |
| 7312 | namespace { |
| 7313 | |
| 7314 | class HexagonABIInfo : public ABIInfo { |
| 7315 | |
| 7316 | |
| 7317 | public: |
| 7318 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7319 | |
| 7320 | private: |
| 7321 | |
| 7322 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7323 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7324 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7325 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7326 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7327 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7328 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7329 | }; |
| 7330 | |
| 7331 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7332 | public: |
| 7333 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7334 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7335 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7336 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7337 | return 29; |
| 7338 | } |
| 7339 | }; |
| 7340 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7341 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7342 | |
| 7343 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7344 | if (!getCXXABI().classifyReturnType(FI)) |
| 7345 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7346 | for (auto &I : FI.arguments()) |
| 7347 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7348 | } |
| 7349 | |
| 7350 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7351 | if (!isAggregateTypeForABI(Ty)) { |
| 7352 | // Treat an enum type as its underlying type. |
| 7353 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7354 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7355 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7356 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7357 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7358 | } |
| 7359 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7360 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7361 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7362 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7363 | // Ignore empty records. |
| 7364 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7365 | return ABIArgInfo::getIgnore(); |
| 7366 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7367 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7368 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7369 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7370 | // Pass in the smallest viable integer type. |
| 7371 | else if (Size > 32) |
| 7372 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7373 | else if (Size > 16) |
| 7374 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7375 | else if (Size > 8) |
| 7376 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7377 | else |
| 7378 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7379 | } |
| 7380 | |
| 7381 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7382 | if (RetTy->isVoidType()) |
| 7383 | return ABIArgInfo::getIgnore(); |
| 7384 | |
| 7385 | // Large vector types should be returned via memory. |
| 7386 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7387 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7388 | |
| 7389 | if (!isAggregateTypeForABI(RetTy)) { |
| 7390 | // Treat an enum type as its underlying type. |
| 7391 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7392 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7393 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7394 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7395 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7396 | } |
| 7397 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7398 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7399 | return ABIArgInfo::getIgnore(); |
| 7400 | |
| 7401 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7402 | // are returned indirectly. |
| 7403 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7404 | if (Size <= 64) { |
| 7405 | // Return in the smallest viable integer type. |
| 7406 | if (Size <= 8) |
| 7407 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7408 | if (Size <= 16) |
| 7409 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7410 | if (Size <= 32) |
| 7411 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7412 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7413 | } |
| 7414 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7415 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7416 | } |
| 7417 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7418 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7419 | QualType Ty) const { |
| 7420 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7421 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7422 | getContext().getTypeInfoInChars(Ty), |
| 7423 | CharUnits::fromQuantity(4), |
| 7424 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7425 | } |
| 7426 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7427 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7428 | // Lanai ABI Implementation |
| 7429 | //===----------------------------------------------------------------------===// |
| 7430 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7431 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7432 | class LanaiABIInfo : public DefaultABIInfo { |
| 7433 | public: |
| 7434 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7435 | |
| 7436 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7437 | |
| 7438 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7439 | CCState State(FI.getCallingConvention()); |
| 7440 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7441 | // regparm attribute set. |
| 7442 | if (FI.getHasRegParm()) { |
| 7443 | State.FreeRegs = FI.getRegParm(); |
| 7444 | } else { |
| 7445 | State.FreeRegs = 4; |
| 7446 | } |
| 7447 | |
| 7448 | if (!getCXXABI().classifyReturnType(FI)) |
| 7449 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7450 | for (auto &I : FI.arguments()) |
| 7451 | I.info = classifyArgumentType(I.type, State); |
| 7452 | } |
| 7453 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7454 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7455 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7456 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7457 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7458 | |
| 7459 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7460 | unsigned Size = getContext().getTypeSize(Ty); |
| 7461 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7462 | |
| 7463 | if (SizeInRegs == 0) |
| 7464 | return false; |
| 7465 | |
| 7466 | if (SizeInRegs > State.FreeRegs) { |
| 7467 | State.FreeRegs = 0; |
| 7468 | return false; |
| 7469 | } |
| 7470 | |
| 7471 | State.FreeRegs -= SizeInRegs; |
| 7472 | |
| 7473 | return true; |
| 7474 | } |
| 7475 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7476 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7477 | CCState &State) const { |
| 7478 | if (!ByVal) { |
| 7479 | if (State.FreeRegs) { |
| 7480 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7481 | return getNaturalAlignIndirectInReg(Ty); |
| 7482 | } |
| 7483 | return getNaturalAlignIndirect(Ty, false); |
| 7484 | } |
| 7485 | |
| 7486 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7487 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7488 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7489 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7490 | /*Realign=*/TypeAlign > |
| 7491 | MinABIStackAlignInBytes); |
| 7492 | } |
| 7493 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7494 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7495 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7496 | // Check with the C++ ABI first. |
| 7497 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7498 | if (RT) { |
| 7499 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7500 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7501 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7502 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7503 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7504 | } |
| 7505 | } |
| 7506 | |
| 7507 | if (isAggregateTypeForABI(Ty)) { |
| 7508 | // Structures with flexible arrays are always indirect. |
| 7509 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7510 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7511 | |
| 7512 | // Ignore empty structs/unions. |
| 7513 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7514 | return ABIArgInfo::getIgnore(); |
| 7515 | |
| 7516 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7517 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7518 | if (SizeInRegs <= State.FreeRegs) { |
| 7519 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7520 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7521 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7522 | State.FreeRegs -= SizeInRegs; |
| 7523 | return ABIArgInfo::getDirectInReg(Result); |
| 7524 | } else { |
| 7525 | State.FreeRegs = 0; |
| 7526 | } |
| 7527 | return getIndirectResult(Ty, true, State); |
| 7528 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7529 | |
| 7530 | // Treat an enum type as its underlying type. |
| 7531 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7532 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7533 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7534 | bool InReg = shouldUseInReg(Ty, State); |
| 7535 | if (Ty->isPromotableIntegerType()) { |
| 7536 | if (InReg) |
| 7537 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7538 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7539 | } |
| 7540 | if (InReg) |
| 7541 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7542 | return ABIArgInfo::getDirect(); |
| 7543 | } |
| 7544 | |
| 7545 | namespace { |
| 7546 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7547 | public: |
| 7548 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7549 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7550 | }; |
| 7551 | } |
| 7552 | |
| 7553 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7554 | // AMDGPU ABI Implementation |
| 7555 | //===----------------------------------------------------------------------===// |
| 7556 | |
| 7557 | namespace { |
| 7558 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7559 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7560 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7561 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7562 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7563 | unsigned numRegsForType(QualType Ty) const; |
| 7564 | |
| 7565 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7566 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7567 | uint64_t Members) const override; |
| 7568 | |
| 7569 | public: |
| 7570 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7571 | DefaultABIInfo(CGT) {} |
| 7572 | |
| 7573 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7574 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7575 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7576 | |
| 7577 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7578 | }; |
| 7579 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7580 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7581 | return true; |
| 7582 | } |
| 7583 | |
| 7584 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7585 | const Type *Base, uint64_t Members) const { |
| 7586 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7587 | |
| 7588 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7589 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7590 | } |
| 7591 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7592 | /// Estimate number of registers the type will use when passed in registers. |
| 7593 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7594 | unsigned NumRegs = 0; |
| 7595 | |
| 7596 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7597 | // Compute from the number of elements. The reported size is based on the |
| 7598 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7599 | QualType EltTy = VT->getElementType(); |
| 7600 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7601 | |
| 7602 | // 16-bit element vectors should be passed as packed. |
| 7603 | if (EltSize == 16) |
| 7604 | return (VT->getNumElements() + 1) / 2; |
| 7605 | |
| 7606 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7607 | return EltNumRegs * VT->getNumElements(); |
| 7608 | } |
| 7609 | |
| 7610 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7611 | const RecordDecl *RD = RT->getDecl(); |
| 7612 | assert(!RD->hasFlexibleArrayMember()); |
| 7613 | |
| 7614 | for (const FieldDecl *Field : RD->fields()) { |
| 7615 | QualType FieldTy = Field->getType(); |
| 7616 | NumRegs += numRegsForType(FieldTy); |
| 7617 | } |
| 7618 | |
| 7619 | return NumRegs; |
| 7620 | } |
| 7621 | |
| 7622 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7623 | } |
| 7624 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7625 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7626 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7627 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7628 | if (!getCXXABI().classifyReturnType(FI)) |
| 7629 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7630 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7631 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7632 | for (auto &Arg : FI.arguments()) { |
| 7633 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7634 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7635 | } else { |
| 7636 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7637 | } |
| 7638 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7641 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7642 | if (isAggregateTypeForABI(RetTy)) { |
| 7643 | // Records with non-trivial destructors/copy-constructors should not be |
| 7644 | // returned by value. |
| 7645 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7646 | // Ignore empty structs/unions. |
| 7647 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7648 | return ABIArgInfo::getIgnore(); |
| 7649 | |
| 7650 | // Lower single-element structs to just return a regular value. |
| 7651 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7652 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7653 | |
| 7654 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7655 | const RecordDecl *RD = RT->getDecl(); |
| 7656 | if (RD->hasFlexibleArrayMember()) |
| 7657 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7658 | } |
| 7659 | |
| 7660 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7661 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7662 | if (Size <= 16) |
| 7663 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7664 | |
| 7665 | if (Size <= 32) |
| 7666 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7667 | |
| 7668 | if (Size <= 64) { |
| 7669 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7670 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7671 | } |
| 7672 | |
| 7673 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7674 | return ABIArgInfo::getDirect(); |
| 7675 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7676 | } |
| 7677 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7678 | // Otherwise just do the default thing. |
| 7679 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7680 | } |
| 7681 | |
| 7682 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7683 | /// make sense to pass anything byval, so everything must be direct. |
| 7684 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7685 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7686 | |
| 7687 | // TODO: Can we omit empty structs? |
| 7688 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7689 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7690 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7691 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7692 | |
| 7693 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7694 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7695 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7696 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7697 | } |
| 7698 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7699 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7700 | unsigned &NumRegsLeft) const { |
| 7701 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7702 | |
| 7703 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7704 | |
| 7705 | if (isAggregateTypeForABI(Ty)) { |
| 7706 | // Records with non-trivial destructors/copy-constructors should not be |
| 7707 | // passed by value. |
| 7708 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7709 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7710 | |
| 7711 | // Ignore empty structs/unions. |
| 7712 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7713 | return ABIArgInfo::getIgnore(); |
| 7714 | |
| 7715 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7716 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7717 | // though watch out for things like bitfields. |
| 7718 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7719 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7720 | |
| 7721 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7722 | const RecordDecl *RD = RT->getDecl(); |
| 7723 | if (RD->hasFlexibleArrayMember()) |
| 7724 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7725 | } |
| 7726 | |
| 7727 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7728 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7729 | if (Size <= 64) { |
| 7730 | unsigned NumRegs = (Size + 31) / 32; |
| 7731 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7732 | |
| 7733 | if (Size <= 16) |
| 7734 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7735 | |
| 7736 | if (Size <= 32) |
| 7737 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7738 | |
| 7739 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7740 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7741 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7742 | } |
| 7743 | |
| 7744 | if (NumRegsLeft > 0) { |
| 7745 | unsigned NumRegs = numRegsForType(Ty); |
| 7746 | if (NumRegsLeft >= NumRegs) { |
| 7747 | NumRegsLeft -= NumRegs; |
| 7748 | return ABIArgInfo::getDirect(); |
| 7749 | } |
| 7750 | } |
| 7751 | } |
| 7752 | |
| 7753 | // Otherwise just do the default thing. |
| 7754 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7755 | if (!ArgInfo.isIndirect()) { |
| 7756 | unsigned NumRegs = numRegsForType(Ty); |
| 7757 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7758 | } |
| 7759 | |
| 7760 | return ArgInfo; |
| 7761 | } |
| 7762 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7763 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7764 | public: |
| 7765 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7766 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7767 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7768 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7769 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7770 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7771 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7772 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7773 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7774 | LangAS getASTAllocaAddressSpace() const override { |
| 7775 | return getLangASFromTargetAS( |
| 7776 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7777 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7778 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7779 | const VarDecl *D) const override; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7780 | llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, |
| 7781 | llvm::LLVMContext &C) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7782 | llvm::Function * |
| 7783 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7784 | llvm::Function *BlockInvokeFunc, |
| 7785 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7786 | bool shouldEmitStaticExternCAliases() const override; |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7787 | void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7788 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7789 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7790 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7791 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7792 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7793 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7794 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7795 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7796 | if (!FD) |
| 7797 | return; |
| 7798 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7799 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7800 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7801 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7802 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7803 | |
| 7804 | if (M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>() && |
| 7805 | (M.getTriple().getOS() == llvm::Triple::AMDHSA)) |
Tony Tye | 68e11a6 | 2018-03-23 18:51:45 +0000 | [diff] [blame] | 7806 | F->addFnAttr("amdgpu-implicitarg-num-bytes", "48"); |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7807 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7808 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7809 | if (ReqdWGS || FlatWGS) { |
| 7810 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7811 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7812 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7813 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7814 | |
| 7815 | if (Min != 0) { |
| 7816 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7817 | |
| 7818 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7819 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7820 | } else |
| 7821 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7822 | } |
| 7823 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7824 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7825 | unsigned Min = Attr->getMin(); |
| 7826 | unsigned Max = Attr->getMax(); |
| 7827 | |
| 7828 | if (Min != 0) { |
| 7829 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7830 | |
| 7831 | std::string AttrVal = llvm::utostr(Min); |
| 7832 | if (Max != 0) |
| 7833 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7834 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7835 | } else |
| 7836 | assert(Max == 0 && "Max must be zero"); |
| 7837 | } |
| 7838 | |
| 7839 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7840 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7841 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7842 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7843 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7844 | } |
| 7845 | |
| 7846 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7847 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7848 | |
| 7849 | if (NumVGPR != 0) |
| 7850 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7851 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7852 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7853 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7854 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7855 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7856 | } |
| 7857 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7858 | // Currently LLVM assumes null pointers always have value 0, |
| 7859 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7860 | // emitting null pointers in private and local address spaces, a null |
| 7861 | // pointer in generic address space is emitted which is casted to a |
| 7862 | // pointer in local or private address space. |
| 7863 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7864 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7865 | QualType QT) const { |
| 7866 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7867 | return llvm::ConstantPointerNull::get(PT); |
| 7868 | |
| 7869 | auto &Ctx = CGM.getContext(); |
| 7870 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7871 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7872 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7873 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7874 | } |
| 7875 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7876 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7877 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7878 | const VarDecl *D) const { |
| 7879 | assert(!CGM.getLangOpts().OpenCL && |
| 7880 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7881 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7882 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 7883 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7884 | if (!D) |
| 7885 | return DefaultGlobalAS; |
| 7886 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7887 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 7888 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7889 | if (AddrSpace != LangAS::Default) |
| 7890 | return AddrSpace; |
| 7891 | |
| 7892 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7893 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7894 | return ConstAS.getValue(); |
| 7895 | } |
| 7896 | return DefaultGlobalAS; |
| 7897 | } |
| 7898 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7899 | llvm::SyncScope::ID |
| 7900 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, |
| 7901 | llvm::LLVMContext &C) const { |
| 7902 | StringRef Name; |
| 7903 | switch (S) { |
| 7904 | case SyncScope::OpenCLWorkGroup: |
| 7905 | Name = "workgroup"; |
| 7906 | break; |
| 7907 | case SyncScope::OpenCLDevice: |
| 7908 | Name = "agent"; |
| 7909 | break; |
| 7910 | case SyncScope::OpenCLAllSVMDevices: |
| 7911 | Name = ""; |
| 7912 | break; |
| 7913 | case SyncScope::OpenCLSubGroup: |
| 7914 | Name = "subgroup"; |
| 7915 | } |
| 7916 | return C.getOrInsertSyncScopeID(Name); |
| 7917 | } |
| 7918 | |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7919 | bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 7920 | return false; |
| 7921 | } |
| 7922 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 7923 | void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7924 | const FunctionType *&FT) const { |
| 7925 | FT = getABIInfo().getContext().adjustFunctionType( |
| 7926 | FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7929 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7930 | // SPARC v8 ABI Implementation. |
| 7931 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7932 | // |
| 7933 | // Ensures that complex values are passed in registers. |
| 7934 | // |
| 7935 | namespace { |
| 7936 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7937 | public: |
| 7938 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7939 | |
| 7940 | private: |
| 7941 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7942 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7943 | }; |
| 7944 | } // end anonymous namespace |
| 7945 | |
| 7946 | |
| 7947 | ABIArgInfo |
| 7948 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7949 | if (Ty->isAnyComplexType()) { |
| 7950 | return ABIArgInfo::getDirect(); |
| 7951 | } |
| 7952 | else { |
| 7953 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7954 | } |
| 7955 | } |
| 7956 | |
| 7957 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7958 | |
| 7959 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7960 | for (auto &Arg : FI.arguments()) |
| 7961 | Arg.info = classifyArgumentType(Arg.type); |
| 7962 | } |
| 7963 | |
| 7964 | namespace { |
| 7965 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7966 | public: |
| 7967 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7968 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7969 | }; |
| 7970 | } // end anonymous namespace |
| 7971 | |
| 7972 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7973 | // SPARC v9 ABI Implementation. |
| 7974 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7975 | // |
| 7976 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7977 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7978 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7979 | // |
| 7980 | // One case requires special care: |
| 7981 | // |
| 7982 | // struct mixed { |
| 7983 | // int i; |
| 7984 | // float f; |
| 7985 | // }; |
| 7986 | // |
| 7987 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7988 | // parameter array, but the int is passed in an integer register, and the float |
| 7989 | // is passed in a floating point register. This is represented as two arguments |
| 7990 | // with the LLVM IR inreg attribute: |
| 7991 | // |
| 7992 | // declare void f(i32 inreg %i, float inreg %f) |
| 7993 | // |
| 7994 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7995 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7996 | // bytes. |
| 7997 | // |
| 7998 | namespace { |
| 7999 | class SparcV9ABIInfo : public ABIInfo { |
| 8000 | public: |
| 8001 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 8002 | |
| 8003 | private: |
| 8004 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8005 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8006 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8007 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8008 | |
| 8009 | // Coercion type builder for structs passed in registers. The coercion type |
| 8010 | // serves two purposes: |
| 8011 | // |
| 8012 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 8013 | // in registers. |
| 8014 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 8015 | // code generator knows to pass them in floating point registers. |
| 8016 | // |
| 8017 | // We also compute the InReg flag which indicates that the struct contains |
| 8018 | // aligned 32-bit floats. |
| 8019 | // |
| 8020 | struct CoerceBuilder { |
| 8021 | llvm::LLVMContext &Context; |
| 8022 | const llvm::DataLayout &DL; |
| 8023 | SmallVector<llvm::Type*, 8> Elems; |
| 8024 | uint64_t Size; |
| 8025 | bool InReg; |
| 8026 | |
| 8027 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 8028 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 8029 | |
| 8030 | // Pad Elems with integers until Size is ToSize. |
| 8031 | void pad(uint64_t ToSize) { |
| 8032 | assert(ToSize >= Size && "Cannot remove elements"); |
| 8033 | if (ToSize == Size) |
| 8034 | return; |
| 8035 | |
| 8036 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8037 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8038 | if (Aligned > Size && Aligned <= ToSize) { |
| 8039 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 8040 | Size = Aligned; |
| 8041 | } |
| 8042 | |
| 8043 | // Add whole 64-bit words. |
| 8044 | while (Size + 64 <= ToSize) { |
| 8045 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 8046 | Size += 64; |
| 8047 | } |
| 8048 | |
| 8049 | // Final in-word padding. |
| 8050 | if (Size < ToSize) { |
| 8051 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 8052 | Size = ToSize; |
| 8053 | } |
| 8054 | } |
| 8055 | |
| 8056 | // Add a floating point element at Offset. |
| 8057 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 8058 | // Unaligned floats are treated as integers. |
| 8059 | if (Offset % Bits) |
| 8060 | return; |
| 8061 | // The InReg flag is only required if there are any floats < 64 bits. |
| 8062 | if (Bits < 64) |
| 8063 | InReg = true; |
| 8064 | pad(Offset); |
| 8065 | Elems.push_back(Ty); |
| 8066 | Size = Offset + Bits; |
| 8067 | } |
| 8068 | |
| 8069 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 8070 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 8071 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 8072 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 8073 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 8074 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 8075 | switch (ElemTy->getTypeID()) { |
| 8076 | case llvm::Type::StructTyID: |
| 8077 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 8078 | break; |
| 8079 | case llvm::Type::FloatTyID: |
| 8080 | addFloat(ElemOffset, ElemTy, 32); |
| 8081 | break; |
| 8082 | case llvm::Type::DoubleTyID: |
| 8083 | addFloat(ElemOffset, ElemTy, 64); |
| 8084 | break; |
| 8085 | case llvm::Type::FP128TyID: |
| 8086 | addFloat(ElemOffset, ElemTy, 128); |
| 8087 | break; |
| 8088 | case llvm::Type::PointerTyID: |
| 8089 | if (ElemOffset % 64 == 0) { |
| 8090 | pad(ElemOffset); |
| 8091 | Elems.push_back(ElemTy); |
| 8092 | Size += 64; |
| 8093 | } |
| 8094 | break; |
| 8095 | default: |
| 8096 | break; |
| 8097 | } |
| 8098 | } |
| 8099 | } |
| 8100 | |
| 8101 | // Check if Ty is a usable substitute for the coercion type. |
| 8102 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 8103 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8104 | } |
| 8105 | |
| 8106 | // Get the coercion type as a literal struct type. |
| 8107 | llvm::Type *getType() const { |
| 8108 | if (Elems.size() == 1) |
| 8109 | return Elems.front(); |
| 8110 | else |
| 8111 | return llvm::StructType::get(Context, Elems); |
| 8112 | } |
| 8113 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8114 | }; |
| 8115 | } // end anonymous namespace |
| 8116 | |
| 8117 | ABIArgInfo |
| 8118 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 8119 | if (Ty->isVoidType()) |
| 8120 | return ABIArgInfo::getIgnore(); |
| 8121 | |
| 8122 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8123 | |
| 8124 | // Anything too big to fit in registers is passed with an explicit indirect |
| 8125 | // pointer / sret pointer. |
| 8126 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8127 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8128 | |
| 8129 | // Treat an enum type as its underlying type. |
| 8130 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8131 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8132 | |
| 8133 | // Integer types smaller than a register are extended. |
| 8134 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 8135 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8136 | |
| 8137 | // Other non-aggregates go in registers. |
| 8138 | if (!isAggregateTypeForABI(Ty)) |
| 8139 | return ABIArgInfo::getDirect(); |
| 8140 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8141 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 8142 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 8143 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8144 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8145 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8146 | // 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] | 8147 | // Build a coercion type from the LLVM struct type. |
| 8148 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 8149 | if (!StrTy) |
| 8150 | return ABIArgInfo::getDirect(); |
| 8151 | |
| 8152 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 8153 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8154 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8155 | |
| 8156 | // Try to use the original type for coercion. |
| 8157 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8158 | |
| 8159 | if (CB.InReg) |
| 8160 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8161 | else |
| 8162 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8163 | } |
| 8164 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8165 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8166 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8167 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8168 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8169 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8170 | AI.setCoerceToType(ArgTy); |
| 8171 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8172 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8173 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8174 | CGBuilderTy &Builder = CGF.Builder; |
| 8175 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8176 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8177 | |
| 8178 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8179 | |
| 8180 | Address ArgAddr = Address::invalid(); |
| 8181 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8182 | switch (AI.getKind()) { |
| 8183 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8184 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8185 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8186 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8187 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8188 | case ABIArgInfo::Extend: { |
| 8189 | Stride = SlotSize; |
| 8190 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8191 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8192 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8193 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8194 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8195 | case ABIArgInfo::Direct: { |
| 8196 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8197 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8198 | ArgAddr = Addr; |
| 8199 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8200 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8201 | |
| 8202 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8203 | Stride = SlotSize; |
| 8204 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8205 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8206 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8207 | break; |
| 8208 | |
| 8209 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8210 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8211 | } |
| 8212 | |
| 8213 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8214 | llvm::Value *NextPtr = |
| 8215 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 8216 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8217 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8218 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8219 | } |
| 8220 | |
| 8221 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8222 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8223 | for (auto &I : FI.arguments()) |
| 8224 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8225 | } |
| 8226 | |
| 8227 | namespace { |
| 8228 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8229 | public: |
| 8230 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8231 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8232 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8233 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8234 | return 14; |
| 8235 | } |
| 8236 | |
| 8237 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8238 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8239 | }; |
| 8240 | } // end anonymous namespace |
| 8241 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8242 | bool |
| 8243 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8244 | llvm::Value *Address) const { |
| 8245 | // This is calculated from the LLVM and GCC tables and verified |
| 8246 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8247 | |
| 8248 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8249 | |
| 8250 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8251 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8252 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8253 | |
| 8254 | // 0-31: the 8-byte general-purpose registers |
| 8255 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8256 | |
| 8257 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8258 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8259 | |
| 8260 | // Y = 64 |
| 8261 | // PSR = 65 |
| 8262 | // WIM = 66 |
| 8263 | // TBR = 67 |
| 8264 | // PC = 68 |
| 8265 | // NPC = 69 |
| 8266 | // FSR = 70 |
| 8267 | // CSR = 71 |
| 8268 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8269 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8270 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8271 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8272 | |
| 8273 | return false; |
| 8274 | } |
| 8275 | |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8276 | // ARC ABI implementation. |
| 8277 | namespace { |
| 8278 | |
| 8279 | class ARCABIInfo : public DefaultABIInfo { |
| 8280 | public: |
| 8281 | using DefaultABIInfo::DefaultABIInfo; |
| 8282 | |
| 8283 | private: |
| 8284 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8285 | QualType Ty) const override; |
| 8286 | |
| 8287 | void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { |
| 8288 | if (!State.FreeRegs) |
| 8289 | return; |
| 8290 | if (Info.isIndirect() && Info.getInReg()) |
| 8291 | State.FreeRegs--; |
| 8292 | else if (Info.isDirect() && Info.getInReg()) { |
| 8293 | unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; |
| 8294 | if (sz < State.FreeRegs) |
| 8295 | State.FreeRegs -= sz; |
| 8296 | else |
| 8297 | State.FreeRegs = 0; |
| 8298 | } |
| 8299 | } |
| 8300 | |
| 8301 | void computeInfo(CGFunctionInfo &FI) const override { |
| 8302 | CCState State(FI.getCallingConvention()); |
| 8303 | // ARC uses 8 registers to pass arguments. |
| 8304 | State.FreeRegs = 8; |
| 8305 | |
| 8306 | if (!getCXXABI().classifyReturnType(FI)) |
| 8307 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8308 | updateState(FI.getReturnInfo(), FI.getReturnType(), State); |
| 8309 | for (auto &I : FI.arguments()) { |
| 8310 | I.info = classifyArgumentType(I.type, State.FreeRegs); |
| 8311 | updateState(I.info, I.type, State); |
| 8312 | } |
| 8313 | } |
| 8314 | |
| 8315 | ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; |
| 8316 | ABIArgInfo getIndirectByValue(QualType Ty) const; |
| 8317 | ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; |
| 8318 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8319 | }; |
| 8320 | |
| 8321 | class ARCTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8322 | public: |
| 8323 | ARCTargetCodeGenInfo(CodeGenTypes &CGT) |
| 8324 | : TargetCodeGenInfo(new ARCABIInfo(CGT)) {} |
| 8325 | }; |
| 8326 | |
| 8327 | |
| 8328 | ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { |
| 8329 | return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : |
| 8330 | getNaturalAlignIndirect(Ty, false); |
| 8331 | } |
| 8332 | |
| 8333 | ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8334 | // Compute the byval alignment. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8335 | const unsigned MinABIStackAlignInBytes = 4; |
| 8336 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 8337 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 8338 | TypeAlign > MinABIStackAlignInBytes); |
| 8339 | } |
| 8340 | |
| 8341 | Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8342 | QualType Ty) const { |
| 8343 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 8344 | getContext().getTypeInfoInChars(Ty), |
| 8345 | CharUnits::fromQuantity(4), true); |
| 8346 | } |
| 8347 | |
| 8348 | ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, |
| 8349 | uint8_t FreeRegs) const { |
| 8350 | // Handle the generic C++ ABI. |
| 8351 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 8352 | if (RT) { |
| 8353 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 8354 | if (RAA == CGCXXABI::RAA_Indirect) |
| 8355 | return getIndirectByRef(Ty, FreeRegs > 0); |
| 8356 | |
| 8357 | if (RAA == CGCXXABI::RAA_DirectInMemory) |
| 8358 | return getIndirectByValue(Ty); |
| 8359 | } |
| 8360 | |
| 8361 | // Treat an enum type as its underlying type. |
| 8362 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8363 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8364 | |
| 8365 | auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; |
| 8366 | |
| 8367 | if (isAggregateTypeForABI(Ty)) { |
| 8368 | // Structures with flexible arrays are always indirect. |
| 8369 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 8370 | return getIndirectByValue(Ty); |
| 8371 | |
| 8372 | // Ignore empty structs/unions. |
| 8373 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8374 | return ABIArgInfo::getIgnore(); |
| 8375 | |
| 8376 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 8377 | |
| 8378 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 8379 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 8380 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 8381 | |
| 8382 | return FreeRegs >= SizeInRegs ? |
| 8383 | ABIArgInfo::getDirectInReg(Result) : |
| 8384 | ABIArgInfo::getDirect(Result, 0, nullptr, false); |
| 8385 | } |
| 8386 | |
| 8387 | return Ty->isPromotableIntegerType() ? |
| 8388 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) : |
| 8389 | ABIArgInfo::getExtend(Ty)) : |
| 8390 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() : |
| 8391 | ABIArgInfo::getDirect()); |
| 8392 | } |
| 8393 | |
| 8394 | ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { |
| 8395 | if (RetTy->isAnyComplexType()) |
| 8396 | return ABIArgInfo::getDirectInReg(); |
| 8397 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8398 | // Arguments of size > 4 registers are indirect. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8399 | auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; |
| 8400 | if (RetSize > 4) |
| 8401 | return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); |
| 8402 | |
| 8403 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 8404 | } |
| 8405 | |
| 8406 | } // End anonymous namespace. |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8407 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8408 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8409 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8410 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8411 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8412 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8413 | |
| 8414 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8415 | /// it by reference between functions that append to it. |
| 8416 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8417 | |
| 8418 | /// TypeStringCache caches the meta encodings of Types. |
| 8419 | /// |
| 8420 | /// The reason for caching TypeStrings is two fold: |
| 8421 | /// 1. To cache a type's encoding for later uses; |
| 8422 | /// 2. As a means to break recursive member type inclusion. |
| 8423 | /// |
| 8424 | /// A cache Entry can have a Status of: |
| 8425 | /// NonRecursive: The type encoding is not recursive; |
| 8426 | /// Recursive: The type encoding is recursive; |
| 8427 | /// Incomplete: An incomplete TypeString; |
| 8428 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8429 | /// Recursive type encoding. |
| 8430 | /// |
| 8431 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8432 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8433 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8434 | /// the type is encountered. |
| 8435 | /// |
| 8436 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8437 | /// possible. The type itself is recursive and it may contain other types which |
| 8438 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8439 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8440 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8441 | /// |
| 8442 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8443 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8444 | /// are placed into the cache during type expansion as a means to identify and |
| 8445 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8446 | /// the entry becomes IncompleteUsed. |
| 8447 | /// |
| 8448 | /// During the expansion of a RecordType's members: |
| 8449 | /// |
| 8450 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8451 | /// cached encoding is used; |
| 8452 | /// |
| 8453 | /// If the cache contains a Recursive encoding for the member type, the |
| 8454 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8455 | /// |
| 8456 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8457 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8458 | /// |
| 8459 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8460 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8461 | /// it is swapped back in; |
| 8462 | /// |
| 8463 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8464 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8465 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8466 | /// |
| 8467 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8468 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8469 | /// Else the member is part of a recursive type and thus the recursion has |
| 8470 | /// been exited too soon for the encoding to be correct for the member. |
| 8471 | /// |
| 8472 | class TypeStringCache { |
| 8473 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8474 | struct Entry { |
| 8475 | std::string Str; // The encoded TypeString for the type. |
| 8476 | enum Status State; // Information about the encoding in 'Str'. |
| 8477 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8478 | // during the expansion of RecordType's members. |
| 8479 | }; |
| 8480 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8481 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8482 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8483 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8484 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8485 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8486 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8487 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8488 | bool IsRecursive); |
| 8489 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8490 | }; |
| 8491 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8492 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8493 | /// FieldEncoding is a helper for this ordering process. |
| 8494 | class FieldEncoding { |
| 8495 | bool HasName; |
| 8496 | std::string Enc; |
| 8497 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8498 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8499 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8500 | bool operator<(const FieldEncoding &rhs) const { |
| 8501 | if (HasName != rhs.HasName) return HasName; |
| 8502 | return Enc < rhs.Enc; |
| 8503 | } |
| 8504 | }; |
| 8505 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8506 | class XCoreABIInfo : public DefaultABIInfo { |
| 8507 | public: |
| 8508 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8509 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8510 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8511 | }; |
| 8512 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8513 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8514 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8515 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8516 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8517 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8518 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8519 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8520 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8521 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8522 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8523 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8524 | // TODO: this implementation is likely now redundant with the default |
| 8525 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8526 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8527 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8528 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8529 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8530 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8531 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8532 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8533 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8534 | // Handle the argument. |
| 8535 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8536 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8537 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8538 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8539 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8540 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8541 | |
| 8542 | Address Val = Address::invalid(); |
| 8543 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8544 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8545 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8546 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8547 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8548 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8549 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8550 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8551 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8552 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8553 | case ABIArgInfo::Extend: |
| 8554 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8555 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8556 | ArgSize = CharUnits::fromQuantity( |
| 8557 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8558 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8559 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8560 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8561 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8562 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8563 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8564 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8565 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8566 | |
| 8567 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8568 | if (!ArgSize.isZero()) { |
| 8569 | llvm::Value *APN = |
| 8570 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 8571 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8572 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8573 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8574 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8575 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8576 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8577 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8578 | /// into the cache as a means to identify and break recursion. |
| 8579 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8580 | /// be reinserted by removeIncomplete(). |
| 8581 | /// All other types of encoding should have been used rather than arriving here. |
| 8582 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8583 | std::string StubEnc) { |
| 8584 | if (!ID) |
| 8585 | return; |
| 8586 | Entry &E = Map[ID]; |
| 8587 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8588 | "Incorrectly use of addIncomplete"); |
| 8589 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8590 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8591 | E.Str.swap(StubEnc); |
| 8592 | E.State = Incomplete; |
| 8593 | ++IncompleteCount; |
| 8594 | } |
| 8595 | |
| 8596 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8597 | /// must be removed from the cache. |
| 8598 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8599 | /// Returns true if the RecordType was defined recursively. |
| 8600 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8601 | if (!ID) |
| 8602 | return false; |
| 8603 | auto I = Map.find(ID); |
| 8604 | assert(I != Map.end() && "Entry not present"); |
| 8605 | Entry &E = I->second; |
| 8606 | assert( (E.State == Incomplete || |
| 8607 | E.State == IncompleteUsed) && |
| 8608 | "Entry must be an incomplete type"); |
| 8609 | bool IsRecursive = false; |
| 8610 | if (E.State == IncompleteUsed) { |
| 8611 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8612 | IsRecursive = true; |
| 8613 | --IncompleteUsedCount; |
| 8614 | } |
| 8615 | if (E.Swapped.empty()) |
| 8616 | Map.erase(I); |
| 8617 | else { |
| 8618 | // Swap the Recursive back. |
| 8619 | E.Swapped.swap(E.Str); |
| 8620 | E.Swapped.clear(); |
| 8621 | E.State = Recursive; |
| 8622 | } |
| 8623 | --IncompleteCount; |
| 8624 | return IsRecursive; |
| 8625 | } |
| 8626 | |
| 8627 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8628 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8629 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8630 | bool IsRecursive) { |
| 8631 | if (!ID || IncompleteUsedCount) |
| 8632 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8633 | Entry &E = Map[ID]; |
| 8634 | if (IsRecursive && !E.Str.empty()) { |
| 8635 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8636 | "This is not the same Recursive entry"); |
| 8637 | // The parent container was not recursive after all, so we could have used |
| 8638 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8639 | // we started viz: IncompleteCount!=0. |
| 8640 | return; |
| 8641 | } |
| 8642 | assert(E.Str.empty() && "Entry already present"); |
| 8643 | E.Str = Str.str(); |
| 8644 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8645 | } |
| 8646 | |
| 8647 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8648 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8649 | /// encoding is Recursive, return an empty StringRef. |
| 8650 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8651 | if (!ID) |
| 8652 | return StringRef(); // We have no key. |
| 8653 | auto I = Map.find(ID); |
| 8654 | if (I == Map.end()) |
| 8655 | return StringRef(); // We have no encoding. |
| 8656 | Entry &E = I->second; |
| 8657 | if (E.State == Recursive && IncompleteCount) |
| 8658 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8659 | |
| 8660 | if (E.State == Incomplete) { |
| 8661 | // The incomplete type is being used to break out of recursion. |
| 8662 | E.State = IncompleteUsed; |
| 8663 | ++IncompleteUsedCount; |
| 8664 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8665 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8666 | } |
| 8667 | |
| 8668 | /// The XCore ABI includes a type information section that communicates symbol |
| 8669 | /// type information to the linker. The linker uses this information to verify |
| 8670 | /// safety/correctness of things such as array bound and pointers et al. |
| 8671 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8672 | /// This type information (TypeString) is emitted into meta data for all global |
| 8673 | /// symbols: definitions, declarations, functions & variables. |
| 8674 | /// |
| 8675 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8676 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8677 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8678 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8679 | /// |
| 8680 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8681 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8682 | |
| 8683 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8684 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8685 | CodeGen::CodeGenModule &CGM) const { |
| 8686 | SmallStringEnc Enc; |
| 8687 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8688 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8689 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8690 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8691 | llvm::NamedMDNode *MD = |
| 8692 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8693 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8694 | } |
| 8695 | } |
| 8696 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8697 | //===----------------------------------------------------------------------===// |
| 8698 | // SPIR ABI Implementation |
| 8699 | //===----------------------------------------------------------------------===// |
| 8700 | |
| 8701 | namespace { |
| 8702 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8703 | public: |
| 8704 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8705 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8706 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8707 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8708 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8709 | } // End anonymous namespace. |
| 8710 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8711 | namespace clang { |
| 8712 | namespace CodeGen { |
| 8713 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8714 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8715 | SPIRABI.computeInfo(FI); |
| 8716 | } |
| 8717 | } |
| 8718 | } |
| 8719 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8720 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8721 | return llvm::CallingConv::SPIR_KERNEL; |
| 8722 | } |
| 8723 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8724 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8725 | const CodeGen::CodeGenModule &CGM, |
| 8726 | TypeStringCache &TSC); |
| 8727 | |
| 8728 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8729 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8730 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8731 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8732 | const RecordDecl *RD, |
| 8733 | const CodeGen::CodeGenModule &CGM, |
| 8734 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8735 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8736 | SmallStringEnc Enc; |
| 8737 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8738 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8739 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8740 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8741 | Enc += "b("; |
| 8742 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8743 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8744 | Enc += ':'; |
| 8745 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8746 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8747 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8748 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8749 | Enc += ')'; |
| 8750 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8751 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8752 | } |
| 8753 | return true; |
| 8754 | } |
| 8755 | |
| 8756 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8757 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8758 | /// Union types have their fields ordered according to the ABI. |
| 8759 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8760 | const CodeGen::CodeGenModule &CGM, |
| 8761 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8762 | // Append the cached TypeString if we have one. |
| 8763 | StringRef TypeString = TSC.lookupStr(ID); |
| 8764 | if (!TypeString.empty()) { |
| 8765 | Enc += TypeString; |
| 8766 | return true; |
| 8767 | } |
| 8768 | |
| 8769 | // Start to emit an incomplete TypeString. |
| 8770 | size_t Start = Enc.size(); |
| 8771 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8772 | Enc += '('; |
| 8773 | if (ID) |
| 8774 | Enc += ID->getName(); |
| 8775 | Enc += "){"; |
| 8776 | |
| 8777 | // We collect all encoded fields and order as necessary. |
| 8778 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8779 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8780 | if (RD && !RD->field_empty()) { |
| 8781 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8782 | // so that recursive calls to this RecordType will use it whilst building a |
| 8783 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8784 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8785 | std::string StubEnc(Enc.substr(Start).str()); |
| 8786 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8787 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8788 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8789 | (void) TSC.removeIncomplete(ID); |
| 8790 | return false; |
| 8791 | } |
| 8792 | IsRecursive = TSC.removeIncomplete(ID); |
| 8793 | // The ABI requires unions to be sorted but not structures. |
| 8794 | // See FieldEncoding::operator< for sort algorithm. |
| 8795 | if (RT->isUnionType()) |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8796 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8797 | // We can now complete the TypeString. |
| 8798 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8799 | for (unsigned I = 0; I != E; ++I) { |
| 8800 | if (I) |
| 8801 | Enc += ','; |
| 8802 | Enc += FE[I].str(); |
| 8803 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8804 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8805 | Enc += '}'; |
| 8806 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8807 | return true; |
| 8808 | } |
| 8809 | |
| 8810 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8811 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8812 | TypeStringCache &TSC, |
| 8813 | const IdentifierInfo *ID) { |
| 8814 | // Append the cached TypeString if we have one. |
| 8815 | StringRef TypeString = TSC.lookupStr(ID); |
| 8816 | if (!TypeString.empty()) { |
| 8817 | Enc += TypeString; |
| 8818 | return true; |
| 8819 | } |
| 8820 | |
| 8821 | size_t Start = Enc.size(); |
| 8822 | Enc += "e("; |
| 8823 | if (ID) |
| 8824 | Enc += ID->getName(); |
| 8825 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8826 | |
| 8827 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8828 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8829 | SmallVector<FieldEncoding, 16> FE; |
| 8830 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8831 | ++I) { |
| 8832 | SmallStringEnc EnumEnc; |
| 8833 | EnumEnc += "m("; |
| 8834 | EnumEnc += I->getName(); |
| 8835 | EnumEnc += "){"; |
| 8836 | I->getInitVal().toString(EnumEnc); |
| 8837 | EnumEnc += '}'; |
| 8838 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8839 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8840 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8841 | unsigned E = FE.size(); |
| 8842 | for (unsigned I = 0; I != E; ++I) { |
| 8843 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8844 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8845 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8846 | } |
| 8847 | } |
| 8848 | Enc += '}'; |
| 8849 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8850 | return true; |
| 8851 | } |
| 8852 | |
| 8853 | /// Appends type's qualifier to Enc. |
| 8854 | /// This is done prior to appending the type's encoding. |
| 8855 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8856 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8857 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8858 | int Lookup = 0; |
| 8859 | if (QT.isConstQualified()) |
| 8860 | Lookup += 1<<0; |
| 8861 | if (QT.isRestrictQualified()) |
| 8862 | Lookup += 1<<1; |
| 8863 | if (QT.isVolatileQualified()) |
| 8864 | Lookup += 1<<2; |
| 8865 | Enc += Table[Lookup]; |
| 8866 | } |
| 8867 | |
| 8868 | /// Appends built-in types to Enc. |
| 8869 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8870 | const char *EncType; |
| 8871 | switch (BT->getKind()) { |
| 8872 | case BuiltinType::Void: |
| 8873 | EncType = "0"; |
| 8874 | break; |
| 8875 | case BuiltinType::Bool: |
| 8876 | EncType = "b"; |
| 8877 | break; |
| 8878 | case BuiltinType::Char_U: |
| 8879 | EncType = "uc"; |
| 8880 | break; |
| 8881 | case BuiltinType::UChar: |
| 8882 | EncType = "uc"; |
| 8883 | break; |
| 8884 | case BuiltinType::SChar: |
| 8885 | EncType = "sc"; |
| 8886 | break; |
| 8887 | case BuiltinType::UShort: |
| 8888 | EncType = "us"; |
| 8889 | break; |
| 8890 | case BuiltinType::Short: |
| 8891 | EncType = "ss"; |
| 8892 | break; |
| 8893 | case BuiltinType::UInt: |
| 8894 | EncType = "ui"; |
| 8895 | break; |
| 8896 | case BuiltinType::Int: |
| 8897 | EncType = "si"; |
| 8898 | break; |
| 8899 | case BuiltinType::ULong: |
| 8900 | EncType = "ul"; |
| 8901 | break; |
| 8902 | case BuiltinType::Long: |
| 8903 | EncType = "sl"; |
| 8904 | break; |
| 8905 | case BuiltinType::ULongLong: |
| 8906 | EncType = "ull"; |
| 8907 | break; |
| 8908 | case BuiltinType::LongLong: |
| 8909 | EncType = "sll"; |
| 8910 | break; |
| 8911 | case BuiltinType::Float: |
| 8912 | EncType = "ft"; |
| 8913 | break; |
| 8914 | case BuiltinType::Double: |
| 8915 | EncType = "d"; |
| 8916 | break; |
| 8917 | case BuiltinType::LongDouble: |
| 8918 | EncType = "ld"; |
| 8919 | break; |
| 8920 | default: |
| 8921 | return false; |
| 8922 | } |
| 8923 | Enc += EncType; |
| 8924 | return true; |
| 8925 | } |
| 8926 | |
| 8927 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8928 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8929 | const CodeGen::CodeGenModule &CGM, |
| 8930 | TypeStringCache &TSC) { |
| 8931 | Enc += "p("; |
| 8932 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8933 | return false; |
| 8934 | Enc += ')'; |
| 8935 | return true; |
| 8936 | } |
| 8937 | |
| 8938 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8939 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8940 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8941 | const CodeGen::CodeGenModule &CGM, |
| 8942 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8943 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8944 | return false; |
| 8945 | Enc += "a("; |
| 8946 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8947 | CAT->getSize().toStringUnsigned(Enc); |
| 8948 | else |
| 8949 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8950 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8951 | // The Qualifiers should be attached to the type rather than the array. |
| 8952 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8953 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8954 | return false; |
| 8955 | Enc += ')'; |
| 8956 | return true; |
| 8957 | } |
| 8958 | |
| 8959 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8960 | /// and the arguments. |
| 8961 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8962 | const CodeGen::CodeGenModule &CGM, |
| 8963 | TypeStringCache &TSC) { |
| 8964 | Enc += "f{"; |
| 8965 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8966 | return false; |
| 8967 | Enc += "}("; |
| 8968 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8969 | // N.B. we are only interested in the adjusted param types. |
| 8970 | auto I = FPT->param_type_begin(); |
| 8971 | auto E = FPT->param_type_end(); |
| 8972 | if (I != E) { |
| 8973 | do { |
| 8974 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8975 | return false; |
| 8976 | ++I; |
| 8977 | if (I != E) |
| 8978 | Enc += ','; |
| 8979 | } while (I != E); |
| 8980 | if (FPT->isVariadic()) |
| 8981 | Enc += ",va"; |
| 8982 | } else { |
| 8983 | if (FPT->isVariadic()) |
| 8984 | Enc += "va"; |
| 8985 | else |
| 8986 | Enc += '0'; |
| 8987 | } |
| 8988 | } |
| 8989 | Enc += ')'; |
| 8990 | return true; |
| 8991 | } |
| 8992 | |
| 8993 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8994 | /// type encodings. |
| 8995 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8996 | const CodeGen::CodeGenModule &CGM, |
| 8997 | TypeStringCache &TSC) { |
| 8998 | |
| 8999 | QualType QT = QType.getCanonicalType(); |
| 9000 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9001 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 9002 | // The Qualifiers should be attached to the type rather than the array. |
| 9003 | // Thus we don't call appendQualifier() here. |
| 9004 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 9005 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9006 | appendQualifier(Enc, QT); |
| 9007 | |
| 9008 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 9009 | return appendBuiltinType(Enc, BT); |
| 9010 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9011 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 9012 | return appendPointerType(Enc, PT, CGM, TSC); |
| 9013 | |
| 9014 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 9015 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 9016 | |
| 9017 | if (const RecordType *RT = QT->getAsStructureType()) |
| 9018 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9019 | |
| 9020 | if (const RecordType *RT = QT->getAsUnionType()) |
| 9021 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9022 | |
| 9023 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 9024 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 9025 | |
| 9026 | return false; |
| 9027 | } |
| 9028 | |
| 9029 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 9030 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 9031 | if (!D) |
| 9032 | return false; |
| 9033 | |
| 9034 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9035 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 9036 | return false; |
| 9037 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 9038 | } |
| 9039 | |
| 9040 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 9041 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 9042 | return false; |
| 9043 | QualType QT = VD->getType().getCanonicalType(); |
| 9044 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 9045 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9046 | // The Qualifiers should be attached to the type rather than the array. |
| 9047 | // Thus we don't call appendQualifier() here. |
| 9048 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9049 | } |
| 9050 | return appendType(Enc, QT, CGM, TSC); |
| 9051 | } |
| 9052 | return false; |
| 9053 | } |
| 9054 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9055 | //===----------------------------------------------------------------------===// |
| 9056 | // RISCV ABI Implementation |
| 9057 | //===----------------------------------------------------------------------===// |
| 9058 | |
| 9059 | namespace { |
| 9060 | class RISCVABIInfo : public DefaultABIInfo { |
| 9061 | private: |
| 9062 | unsigned XLen; // Size of the integer ('x') registers in bits. |
| 9063 | static const int NumArgGPRs = 8; |
| 9064 | |
| 9065 | public: |
| 9066 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9067 | : DefaultABIInfo(CGT), XLen(XLen) {} |
| 9068 | |
| 9069 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 9070 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 9071 | void computeInfo(CGFunctionInfo &FI) const override; |
| 9072 | |
| 9073 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, |
| 9074 | int &ArgGPRsLeft) const; |
| 9075 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 9076 | |
| 9077 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9078 | QualType Ty) const override; |
| 9079 | |
| 9080 | ABIArgInfo extendType(QualType Ty) const; |
| 9081 | }; |
| 9082 | } // end anonymous namespace |
| 9083 | |
| 9084 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 9085 | QualType RetTy = FI.getReturnType(); |
| 9086 | if (!getCXXABI().classifyReturnType(FI)) |
| 9087 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 9088 | |
| 9089 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 9090 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 9091 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 9092 | // rewrite the argument list and pass indirectly on RV32. |
| 9093 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 9094 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 9095 | |
| 9096 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 9097 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 9098 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 9099 | // different for variadic arguments, we must also track whether we are |
| 9100 | // examining a vararg or not. |
| 9101 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
| 9102 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 9103 | |
| 9104 | int ArgNum = 0; |
| 9105 | for (auto &ArgInfo : FI.arguments()) { |
| 9106 | bool IsFixed = ArgNum < NumFixedArgs; |
| 9107 | ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft); |
| 9108 | ArgNum++; |
| 9109 | } |
| 9110 | } |
| 9111 | |
| 9112 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
| 9113 | int &ArgGPRsLeft) const { |
| 9114 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 9115 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 9116 | |
| 9117 | // Structures with either a non-trivial destructor or a non-trivial |
| 9118 | // copy constructor are always passed indirectly. |
| 9119 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 9120 | if (ArgGPRsLeft) |
| 9121 | ArgGPRsLeft -= 1; |
| 9122 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 9123 | CGCXXABI::RAA_DirectInMemory); |
| 9124 | } |
| 9125 | |
| 9126 | // Ignore empty structs/unions. |
| 9127 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9128 | return ABIArgInfo::getIgnore(); |
| 9129 | |
| 9130 | uint64_t Size = getContext().getTypeSize(Ty); |
| 9131 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 9132 | bool MustUseStack = false; |
| 9133 | // Determine the number of GPRs needed to pass the current argument |
| 9134 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 9135 | // register pairs, so may consume 3 registers. |
| 9136 | int NeededArgGPRs = 1; |
| 9137 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 9138 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 9139 | else if (Size > XLen && Size <= 2 * XLen) |
| 9140 | NeededArgGPRs = 2; |
| 9141 | |
| 9142 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 9143 | MustUseStack = true; |
| 9144 | NeededArgGPRs = ArgGPRsLeft; |
| 9145 | } |
| 9146 | |
| 9147 | ArgGPRsLeft -= NeededArgGPRs; |
| 9148 | |
| 9149 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 9150 | // Treat an enum type as its underlying type. |
| 9151 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 9152 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 9153 | |
| 9154 | // All integral types are promoted to XLen width, unless passed on the |
| 9155 | // stack. |
| 9156 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 9157 | return extendType(Ty); |
| 9158 | } |
| 9159 | |
| 9160 | return ABIArgInfo::getDirect(); |
| 9161 | } |
| 9162 | |
| 9163 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 9164 | // so coerce to integers. |
| 9165 | if (Size <= 2 * XLen) { |
| 9166 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 9167 | |
| 9168 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 9169 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 9170 | if (Size <= XLen) { |
| 9171 | return ABIArgInfo::getDirect( |
| 9172 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 9173 | } else if (Alignment == 2 * XLen) { |
| 9174 | return ABIArgInfo::getDirect( |
| 9175 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 9176 | } else { |
| 9177 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 9178 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 9179 | } |
| 9180 | } |
| 9181 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 9182 | } |
| 9183 | |
| 9184 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 9185 | if (RetTy->isVoidType()) |
| 9186 | return ABIArgInfo::getIgnore(); |
| 9187 | |
| 9188 | int ArgGPRsLeft = 2; |
| 9189 | |
| 9190 | // The rules for return and argument types are the same, so defer to |
| 9191 | // classifyArgumentType. |
| 9192 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft); |
| 9193 | } |
| 9194 | |
| 9195 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9196 | QualType Ty) const { |
| 9197 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 9198 | |
| 9199 | // Empty records are ignored for parameter passing purposes. |
| 9200 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 9201 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 9202 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 9203 | return Addr; |
| 9204 | } |
| 9205 | |
| 9206 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 9207 | getContext().getTypeInfoInChars(Ty); |
| 9208 | |
| 9209 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 9210 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 9211 | |
| 9212 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 9213 | SlotSize, /*AllowHigherAlign=*/true); |
| 9214 | } |
| 9215 | |
| 9216 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 9217 | int TySize = getContext().getTypeSize(Ty); |
| 9218 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 9219 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 9220 | return ABIArgInfo::getSignExtend(Ty); |
| 9221 | return ABIArgInfo::getExtend(Ty); |
| 9222 | } |
| 9223 | |
| 9224 | namespace { |
| 9225 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 9226 | public: |
| 9227 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9228 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen)) {} |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 9229 | |
| 9230 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 9231 | CodeGen::CodeGenModule &CGM) const override { |
| 9232 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 9233 | if (!FD) return; |
| 9234 | |
| 9235 | const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); |
| 9236 | if (!Attr) |
| 9237 | return; |
| 9238 | |
| 9239 | const char *Kind; |
| 9240 | switch (Attr->getInterrupt()) { |
| 9241 | case RISCVInterruptAttr::user: Kind = "user"; break; |
| 9242 | case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; |
| 9243 | case RISCVInterruptAttr::machine: Kind = "machine"; break; |
| 9244 | } |
| 9245 | |
| 9246 | auto *Fn = cast<llvm::Function>(GV); |
| 9247 | |
| 9248 | Fn->addFnAttr("interrupt", Kind); |
| 9249 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9250 | }; |
| 9251 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9252 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9253 | //===----------------------------------------------------------------------===// |
| 9254 | // Driver code |
| 9255 | //===----------------------------------------------------------------------===// |
| 9256 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9257 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 9258 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9259 | } |
| 9260 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 9261 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9262 | if (TheTargetCodeGenInfo) |
| 9263 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9264 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9265 | // Helper to set the unique_ptr while still keeping the return value. |
| 9266 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 9267 | this->TheTargetCodeGenInfo.reset(P); |
| 9268 | return *P; |
| 9269 | }; |
| 9270 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 9271 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 9272 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9273 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9274 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9275 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 9276 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9277 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 9278 | case llvm::Triple::mips: |
| 9279 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 9280 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9281 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 9282 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9283 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 9284 | case llvm::Triple::mips64: |
| 9285 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9286 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9287 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 9288 | case llvm::Triple::avr: |
| 9289 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 9290 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 9291 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 9292 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9293 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 9294 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9295 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 9296 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 9297 | return SetCGInfo( |
| 9298 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9299 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9300 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9301 | } |
| 9302 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9303 | case llvm::Triple::wasm32: |
| 9304 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9305 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9306 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9307 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 9308 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9309 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9310 | case llvm::Triple::thumbeb: { |
| 9311 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9312 | return SetCGInfo( |
| 9313 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9314 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9315 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9316 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9317 | StringRef ABIStr = getTarget().getABI(); |
| 9318 | if (ABIStr == "apcs-gnu") |
| 9319 | Kind = ARMABIInfo::APCS; |
| 9320 | else if (ABIStr == "aapcs16") |
| 9321 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9322 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9323 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9324 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9325 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9326 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9327 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9328 | |
| 9329 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9330 | } |
| 9331 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9332 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9333 | return SetCGInfo( |
| 9334 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9335 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9336 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9337 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9338 | if (getTarget().getABI() == "elfv2") |
| 9339 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9340 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9341 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9342 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9343 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9344 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9345 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9346 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9347 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9348 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9349 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9350 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9351 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9352 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9353 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9354 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9355 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9356 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9357 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9358 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9359 | case llvm::Triple::nvptx: |
| 9360 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9361 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9362 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9363 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9364 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9365 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9366 | case llvm::Triple::riscv32: |
| 9367 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 32)); |
| 9368 | case llvm::Triple::riscv64: |
| 9369 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 64)); |
| 9370 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9371 | case llvm::Triple::systemz: { |
| 9372 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9373 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9374 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9375 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9376 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9377 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9378 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9379 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9380 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9381 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9382 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9383 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9384 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9385 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9386 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9387 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9388 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9389 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9390 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9391 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9392 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9393 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 9394 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9395 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9396 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9397 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9398 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9399 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9400 | X86AVXABILevel AVXLevel = |
| 9401 | (ABI == "avx512" |
| 9402 | ? X86AVXABILevel::AVX512 |
| 9403 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9404 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9405 | switch (Triple.getOS()) { |
| 9406 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9407 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 9408 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9409 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9410 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9411 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9412 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9413 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9414 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9415 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9416 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9417 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9418 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9419 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9420 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9421 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9422 | case llvm::Triple::sparc: |
| 9423 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9424 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9425 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9426 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9427 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 9428 | case llvm::Triple::arc: |
| 9429 | return SetCGInfo(new ARCTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9430 | case llvm::Triple::spir: |
| 9431 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9432 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9433 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9434 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9435 | |
| 9436 | /// Create an OpenCL kernel for an enqueued block. |
| 9437 | /// |
| 9438 | /// The kernel has the same function type as the block invoke function. Its |
| 9439 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9440 | /// It simply calls the block invoke function then returns. |
| 9441 | llvm::Function * |
| 9442 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9443 | llvm::Function *Invoke, |
| 9444 | llvm::Value *BlockLiteral) const { |
| 9445 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9446 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9447 | for (auto &P : InvokeFT->params()) |
| 9448 | ArgTys.push_back(P); |
| 9449 | auto &C = CGF.getLLVMContext(); |
| 9450 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9451 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9452 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9453 | &CGF.CGM.getModule()); |
| 9454 | auto IP = CGF.Builder.saveIP(); |
| 9455 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9456 | auto &Builder = CGF.Builder; |
| 9457 | Builder.SetInsertPoint(BB); |
| 9458 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9459 | for (auto &A : F->args()) |
| 9460 | Args.push_back(&A); |
| 9461 | Builder.CreateCall(Invoke, Args); |
| 9462 | Builder.CreateRetVoid(); |
| 9463 | Builder.restoreIP(IP); |
| 9464 | return F; |
| 9465 | } |
| 9466 | |
| 9467 | /// Create an OpenCL kernel for an enqueued block. |
| 9468 | /// |
| 9469 | /// The type of the first argument (the block literal) is the struct type |
| 9470 | /// of the block literal instead of a pointer type. The first argument |
| 9471 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9472 | /// allocates the same type of struct on stack and stores the block literal |
| 9473 | /// to it and passes its pointer to the block invoke function. The kernel |
| 9474 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 9475 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 9476 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 9477 | llvm::Value *BlockLiteral) const { |
| 9478 | auto &Builder = CGF.Builder; |
| 9479 | auto &C = CGF.getLLVMContext(); |
| 9480 | |
| 9481 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 9482 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9483 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9484 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 9485 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 9486 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 9487 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 9488 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 9489 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 9490 | |
| 9491 | ArgTys.push_back(BlockTy); |
| 9492 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9493 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 9494 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9495 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9496 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9497 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 9498 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 9499 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9500 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9501 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 9502 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9503 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9504 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9505 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 9506 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9507 | } |
| 9508 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9509 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9510 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9511 | &CGF.CGM.getModule()); |
| 9512 | F->addFnAttr("enqueued-block"); |
| 9513 | auto IP = CGF.Builder.saveIP(); |
| 9514 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9515 | Builder.SetInsertPoint(BB); |
| 9516 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 9517 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
| 9518 | BlockPtr->setAlignment(BlockAlign); |
| 9519 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 9520 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 9521 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9522 | Args.push_back(Cast); |
| 9523 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 9524 | Args.push_back(I); |
| 9525 | Builder.CreateCall(Invoke, Args); |
| 9526 | Builder.CreateRetVoid(); |
| 9527 | Builder.restoreIP(IP); |
| 9528 | |
| 9529 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 9530 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 9531 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 9532 | F->setMetadata("kernel_arg_base_type", |
| 9533 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 9534 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 9535 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 9536 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 9537 | |
| 9538 | return F; |
| 9539 | } |