Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // These classes wrap the information about a call or function |
| 10 | // definition used to handle ABI compliancy. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 14 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 15 | #include "ABIInfo.h" |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 16 | #include "CGBlocks.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 21 | #include "clang/Basic/CodeGenOptions.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 22 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 23 | #include "clang/CodeGen/SwiftCallingConv.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Triple.h" |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DataLayout.h" |
| 29 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 31 | #include <algorithm> // std::sort |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 32 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | using namespace CodeGen; |
| 35 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 36 | // Helper for coercing an aggregate argument or return value into an integer |
| 37 | // array of the same size (including padding) and alignment. This alternate |
| 38 | // coercion happens only for the RenderScript ABI and can be removed after |
| 39 | // runtimes that rely on it are no longer supported. |
| 40 | // |
| 41 | // RenderScript assumes that the size of the argument / return value in the IR |
| 42 | // is the same as the size of the corresponding qualified type. This helper |
| 43 | // coerces the aggregate type into an array of the same size (including |
| 44 | // padding). This coercion is used in lieu of expansion of struct members or |
| 45 | // other canonical coercions that return a coerced-type of larger size. |
| 46 | // |
| 47 | // Ty - The argument / return value type |
| 48 | // Context - The associated ASTContext |
| 49 | // LLVMContext - The associated LLVMContext |
| 50 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 51 | ASTContext &Context, |
| 52 | llvm::LLVMContext &LLVMContext) { |
| 53 | // Alignment and Size are measured in bits. |
| 54 | const uint64_t Size = Context.getTypeSize(Ty); |
| 55 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 56 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 57 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 58 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 59 | } |
| 60 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 61 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 62 | llvm::Value *Array, |
| 63 | llvm::Value *Value, |
| 64 | unsigned FirstIndex, |
| 65 | unsigned LastIndex) { |
| 66 | // Alternatively, we could emit this as a loop in the source. |
| 67 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 68 | llvm::Value *Cell = |
| 69 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 70 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 74 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 75 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 76 | T->isMemberFunctionPointerType(); |
| 77 | } |
| 78 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 79 | ABIArgInfo |
| 80 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 81 | llvm::Type *Padding) const { |
| 82 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 83 | ByRef, Realign, Padding); |
| 84 | } |
| 85 | |
| 86 | ABIArgInfo |
| 87 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 88 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 89 | /*ByRef*/ false, Realign); |
| 90 | } |
| 91 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 92 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 93 | QualType Ty) const { |
| 94 | return Address::invalid(); |
| 95 | } |
| 96 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 97 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 98 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 99 | /// Does the given lowering require more than the given number of |
| 100 | /// registers when expanded? |
| 101 | /// |
| 102 | /// This is intended to be the basis of a reasonable basic implementation |
| 103 | /// of should{Pass,Return}IndirectlyForSwift. |
| 104 | /// |
| 105 | /// For most targets, a limit of four total registers is reasonable; this |
| 106 | /// limits the amount of code required in order to move around the value |
| 107 | /// in case it wasn't produced immediately prior to the call by the caller |
| 108 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 109 | /// immediately within the callee. But some targets may need to further |
| 110 | /// limit the register count due to an inability to support that many |
| 111 | /// return registers. |
| 112 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 113 | ArrayRef<llvm::Type*> scalarTypes, |
| 114 | unsigned maxAllRegisters) { |
| 115 | unsigned intCount = 0, fpCount = 0; |
| 116 | for (llvm::Type *type : scalarTypes) { |
| 117 | if (type->isPointerTy()) { |
| 118 | intCount++; |
| 119 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 120 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 121 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 122 | } else { |
| 123 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 124 | fpCount++; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return (intCount + fpCount > maxAllRegisters); |
| 129 | } |
| 130 | |
| 131 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 132 | llvm::Type *eltTy, |
| 133 | unsigned numElts) const { |
| 134 | // The default implementation of this assumes that the target guarantees |
| 135 | // 128-bit SIMD support but nothing more. |
| 136 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 137 | } |
| 138 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 139 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 140 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 141 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 142 | if (!RD) { |
| 143 | if (!RT->getDecl()->canPassInRegisters()) |
| 144 | return CGCXXABI::RAA_Indirect; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 145 | return CGCXXABI::RAA_Default; |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 146 | } |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 147 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 151 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 152 | const RecordType *RT = T->getAs<RecordType>(); |
| 153 | if (!RT) |
| 154 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 155 | return getRecordArgABI(RT, CXXABI); |
| 156 | } |
| 157 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 158 | static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, |
| 159 | const ABIInfo &Info) { |
| 160 | QualType Ty = FI.getReturnType(); |
| 161 | |
| 162 | if (const auto *RT = Ty->getAs<RecordType>()) |
| 163 | if (!isa<CXXRecordDecl>(RT->getDecl()) && |
| 164 | !RT->getDecl()->canPassInRegisters()) { |
| 165 | FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | return CXXABI.classifyReturnType(FI); |
| 170 | } |
| 171 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 172 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 173 | /// should ensure that all elements of the union have the same "machine type". |
| 174 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 175 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 176 | const RecordDecl *UD = UT->getDecl(); |
| 177 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 178 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 179 | return UD->field_begin()->getType(); |
| 180 | } |
| 181 | } |
| 182 | return Ty; |
| 183 | } |
| 184 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 185 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 186 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 189 | ASTContext &ABIInfo::getContext() const { |
| 190 | return CGT.getContext(); |
| 191 | } |
| 192 | |
| 193 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 194 | return CGT.getLLVMContext(); |
| 195 | } |
| 196 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 197 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 198 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 199 | } |
| 200 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 201 | const TargetInfo &ABIInfo::getTarget() const { |
| 202 | return CGT.getTarget(); |
| 203 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 204 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 205 | const CodeGenOptions &ABIInfo::getCodeGenOpts() const { |
| 206 | return CGT.getCodeGenOpts(); |
| 207 | } |
| 208 | |
| 209 | bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); } |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 210 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 211 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 216 | uint64_t Members) const { |
| 217 | return false; |
| 218 | } |
| 219 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 220 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 221 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 222 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 223 | switch (TheKind) { |
| 224 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 225 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 226 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 227 | Ty->print(OS); |
| 228 | else |
| 229 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 230 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 231 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 232 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 233 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 234 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 235 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 236 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 237 | case InAlloca: |
| 238 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 239 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 240 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 241 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 242 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 243 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 244 | break; |
| 245 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 246 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 247 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 248 | case CoerceAndExpand: |
| 249 | OS << "CoerceAndExpand Type="; |
| 250 | getCoerceAndExpandType()->print(OS); |
| 251 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 252 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 253 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 256 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 257 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 258 | llvm::Value *Ptr, |
| 259 | CharUnits Align) { |
| 260 | llvm::Value *PtrAsInt = Ptr; |
| 261 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 262 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 263 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 264 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 265 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 266 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 267 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 268 | Ptr->getType(), |
| 269 | Ptr->getName() + ".aligned"); |
| 270 | return PtrAsInt; |
| 271 | } |
| 272 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 273 | /// Emit va_arg for a platform using the common void* representation, |
| 274 | /// where arguments are simply emitted in an array of slots on the stack. |
| 275 | /// |
| 276 | /// This version implements the core direct-value passing rules. |
| 277 | /// |
| 278 | /// \param SlotSize - The size and alignment of a stack slot. |
| 279 | /// Each argument will be allocated to a multiple of this number of |
| 280 | /// slots, and all the slots will be aligned to this value. |
| 281 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 282 | /// an argument type with an alignment greater than the slot size |
| 283 | /// will be emitted on a higher-alignment address, potentially |
| 284 | /// leaving one or more empty slots behind as padding. If this |
| 285 | /// is false, the returned address might be less-aligned than |
| 286 | /// DirectAlign. |
| 287 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 288 | Address VAListAddr, |
| 289 | llvm::Type *DirectTy, |
| 290 | CharUnits DirectSize, |
| 291 | CharUnits DirectAlign, |
| 292 | CharUnits SlotSize, |
| 293 | bool AllowHigherAlign) { |
| 294 | // Cast the element type to i8* if necessary. Some platforms define |
| 295 | // va_list as a struct containing an i8* instead of just an i8*. |
| 296 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 297 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 298 | |
| 299 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 300 | |
| 301 | // If the CC aligns values higher than the slot size, do so if needed. |
| 302 | Address Addr = Address::invalid(); |
| 303 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 304 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 305 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 306 | } else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 307 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 311 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 312 | Address NextPtr = |
| 313 | CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next"); |
| 314 | CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 315 | |
| 316 | // If the argument is smaller than a slot, and this is a big-endian |
| 317 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 318 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 319 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 320 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 321 | } |
| 322 | |
| 323 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 324 | return Addr; |
| 325 | } |
| 326 | |
| 327 | /// Emit va_arg for a platform using the common void* representation, |
| 328 | /// where arguments are simply emitted in an array of slots on the stack. |
| 329 | /// |
| 330 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 331 | /// \param ValueInfo - The size and alignment of this type, generally |
| 332 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 333 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 334 | /// Each argument will be allocated to a multiple of this number of |
| 335 | /// slots, and all the slots will be aligned to this value. |
| 336 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 337 | /// an argument type with an alignment greater than the slot size |
| 338 | /// will be emitted on a higher-alignment address, potentially |
| 339 | /// leaving one or more empty slots behind as padding. |
| 340 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 341 | QualType ValueTy, bool IsIndirect, |
| 342 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 343 | CharUnits SlotSizeAndAlign, |
| 344 | bool AllowHigherAlign) { |
| 345 | // The size and alignment of the value that was passed directly. |
| 346 | CharUnits DirectSize, DirectAlign; |
| 347 | if (IsIndirect) { |
| 348 | DirectSize = CGF.getPointerSize(); |
| 349 | DirectAlign = CGF.getPointerAlign(); |
| 350 | } else { |
| 351 | DirectSize = ValueInfo.first; |
| 352 | DirectAlign = ValueInfo.second; |
| 353 | } |
| 354 | |
| 355 | // Cast the address we've calculated to the right type. |
| 356 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 357 | if (IsIndirect) |
| 358 | DirectTy = DirectTy->getPointerTo(0); |
| 359 | |
| 360 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 361 | DirectSize, DirectAlign, |
| 362 | SlotSizeAndAlign, |
| 363 | AllowHigherAlign); |
| 364 | |
| 365 | if (IsIndirect) { |
| 366 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 367 | } |
| 368 | |
| 369 | return Addr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 370 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 374 | Address Addr1, llvm::BasicBlock *Block1, |
| 375 | Address Addr2, llvm::BasicBlock *Block2, |
| 376 | const llvm::Twine &Name = "") { |
| 377 | assert(Addr1.getType() == Addr2.getType()); |
| 378 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 379 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 380 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 381 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 382 | return Address(PHI, Align); |
| 383 | } |
| 384 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 385 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 386 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 387 | // If someone can figure out a general rule for this, that would be great. |
| 388 | // It's probably just doomed to be platform-dependent, though. |
| 389 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 390 | // Verified for: |
| 391 | // x86-64 FreeBSD, Linux, Darwin |
| 392 | // x86-32 FreeBSD, Linux, Darwin |
| 393 | // PowerPC Linux, Darwin |
| 394 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 395 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 396 | return 32; |
| 397 | } |
| 398 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 399 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 400 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 401 | // The following conventions are known to require this to be false: |
| 402 | // x86_stdcall |
| 403 | // MIPS |
| 404 | // For everything else, we just prefer false unless we opt out. |
| 405 | return false; |
| 406 | } |
| 407 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 408 | void |
| 409 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 410 | llvm::SmallString<24> &Opt) const { |
| 411 | // This assumes the user is passing a library name like "rt" instead of a |
| 412 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 413 | // dynamic. |
| 414 | Opt = "-l"; |
| 415 | Opt += Lib; |
| 416 | } |
| 417 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 418 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 419 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 420 | // set with clSetKernelArg(), not as normal sub-functions. |
| 421 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 422 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 423 | // gets one matching argument in the produced kernel function argument |
| 424 | // list to enable feasible implementation of clSetKernelArg() with |
| 425 | // aggregates etc. In case we would use the default C calling conv here, |
| 426 | // clSetKernelArg() might break depending on the target-specific |
| 427 | // conventions; different targets might split structs passed as values |
| 428 | // to multiple function arguments etc. |
| 429 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 430 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 431 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 432 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 433 | llvm::PointerType *T, QualType QT) const { |
| 434 | return llvm::ConstantPointerNull::get(T); |
| 435 | } |
| 436 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 437 | LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 438 | const VarDecl *D) const { |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 439 | assert(!CGM.getLangOpts().OpenCL && |
| 440 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 441 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 442 | return D ? D->getType().getAddressSpace() : LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 445 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 446 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, |
| 447 | LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 448 | // Since target may map different address spaces in AST to the same address |
| 449 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 450 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 451 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 452 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 455 | llvm::Constant * |
| 456 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 457 | LangAS SrcAddr, LangAS DestAddr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 458 | llvm::Type *DestTy) const { |
| 459 | // Since target may map different address spaces in AST to the same address |
| 460 | // space, an address space conversion may end up as a bitcast. |
| 461 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 462 | } |
| 463 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 464 | llvm::SyncScope::ID |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 465 | TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 466 | SyncScope Scope, |
| 467 | llvm::AtomicOrdering Ordering, |
| 468 | llvm::LLVMContext &Ctx) const { |
| 469 | return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */ |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 472 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 473 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 474 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 475 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 476 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 477 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 478 | if (FD->isUnnamedBitfield()) |
| 479 | return true; |
| 480 | |
| 481 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 482 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 483 | // Constant arrays of empty records count as empty, strip them off. |
| 484 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 485 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 486 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 487 | if (AT->getSize() == 0) |
| 488 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 489 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 490 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 491 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 492 | const RecordType *RT = FT->getAs<RecordType>(); |
| 493 | if (!RT) |
| 494 | return false; |
| 495 | |
| 496 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 497 | // |
| 498 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 499 | // current ABI. |
| 500 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 501 | return false; |
| 502 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 503 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 506 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 507 | /// fields. Note that a structure with a flexible array member is not |
| 508 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 509 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 510 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 511 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 512 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 513 | const RecordDecl *RD = RT->getDecl(); |
| 514 | if (RD->hasFlexibleArrayMember()) |
| 515 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 516 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 517 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 518 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 519 | for (const auto &I : CXXRD->bases()) |
| 520 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 521 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 522 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 523 | for (const auto *I : RD->fields()) |
| 524 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 525 | return false; |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | /// isSingleElementStruct - Determine if a structure is a "single |
| 530 | /// element struct", i.e. it has exactly one non-empty field or |
| 531 | /// exactly one field which is itself a single element |
| 532 | /// struct. Structures with flexible array members are never |
| 533 | /// considered single element structs. |
| 534 | /// |
| 535 | /// \return The field declaration for the single non-empty field, if |
| 536 | /// it exists. |
| 537 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 538 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 539 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 540 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 541 | |
| 542 | const RecordDecl *RD = RT->getDecl(); |
| 543 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 544 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 545 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 546 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 547 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 548 | // If this is a C++ record, check the bases first. |
| 549 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 550 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 551 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 552 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 553 | continue; |
| 554 | |
| 555 | // If we already found an element then this isn't a single-element struct. |
| 556 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 557 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 558 | |
| 559 | // If this is non-empty and not a single element struct, the composite |
| 560 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 561 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 562 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 563 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 568 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 569 | QualType FT = FD->getType(); |
| 570 | |
| 571 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 572 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 573 | continue; |
| 574 | |
| 575 | // If we already found an element then this isn't a single-element |
| 576 | // struct. |
| 577 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 578 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 579 | |
| 580 | // Treat single element arrays as the element. |
| 581 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 582 | if (AT->getSize().getZExtValue() != 1) |
| 583 | break; |
| 584 | FT = AT->getElementType(); |
| 585 | } |
| 586 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 587 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 588 | Found = FT.getTypePtr(); |
| 589 | } else { |
| 590 | Found = isSingleElementStruct(FT, Context); |
| 591 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 592 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 596 | // We don't consider a struct a single-element struct if it has |
| 597 | // padding beyond the element type. |
| 598 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 599 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 600 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 601 | return Found; |
| 602 | } |
| 603 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 604 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 605 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 606 | const ABIArgInfo &AI) { |
| 607 | // This default implementation defers to the llvm backend's va_arg |
| 608 | // instruction. It can handle only passing arguments directly |
| 609 | // (typically only handled in the backend for primitive types), or |
| 610 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 611 | // flag has ABI impact in the callee, this implementation cannot |
| 612 | // work.) |
| 613 | |
| 614 | // Only a few cases are covered here at the moment -- those needed |
| 615 | // by the default abi. |
| 616 | llvm::Value *Val; |
| 617 | |
| 618 | if (AI.isIndirect()) { |
| 619 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 620 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 621 | assert( |
| 622 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 623 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 624 | |
| 625 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 626 | CharUnits TyAlignForABI = TyInfo.second; |
| 627 | |
| 628 | llvm::Type *BaseTy = |
| 629 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 630 | llvm::Value *Addr = |
| 631 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 632 | return Address(Addr, TyAlignForABI); |
| 633 | } else { |
| 634 | assert((AI.isDirect() || AI.isExtend()) && |
| 635 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 636 | |
| 637 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 638 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 639 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 640 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 641 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 642 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 643 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 644 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 645 | |
| 646 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 647 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 648 | CGF.Builder.CreateStore(Val, Temp); |
| 649 | return Temp; |
| 650 | } |
| 651 | } |
| 652 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 653 | /// DefaultABIInfo - The default implementation for ABI specific |
| 654 | /// details. This implementation provides information which results in |
| 655 | /// self-consistent and sensible LLVM IR generation, but does not |
| 656 | /// conform to any particular ABI. |
| 657 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 658 | public: |
| 659 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 660 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 661 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 662 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 663 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 664 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 665 | if (!getCXXABI().classifyReturnType(FI)) |
| 666 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 667 | for (auto &I : FI.arguments()) |
| 668 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 669 | } |
| 670 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 671 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 672 | QualType Ty) const override { |
| 673 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 674 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 677 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 678 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 679 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 680 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 683 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 684 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 685 | |
| 686 | if (isAggregateTypeForABI(Ty)) { |
| 687 | // Records with non-trivial destructors/copy-constructors should not be |
| 688 | // passed by value. |
| 689 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 690 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 691 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 692 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 693 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 694 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 695 | // Treat an enum type as its underlying type. |
| 696 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 697 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 698 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 699 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 700 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 703 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 704 | if (RetTy->isVoidType()) |
| 705 | return ABIArgInfo::getIgnore(); |
| 706 | |
| 707 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 708 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 709 | |
| 710 | // Treat an enum type as its underlying type. |
| 711 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 712 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 713 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 714 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 715 | : ABIArgInfo::getDirect()); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 718 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 719 | // WebAssembly ABI Implementation |
| 720 | // |
| 721 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 722 | //===----------------------------------------------------------------------===// |
| 723 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 724 | class WebAssemblyABIInfo final : public SwiftABIInfo { |
| 725 | DefaultABIInfo defaultInfo; |
| 726 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 727 | public: |
| 728 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 729 | : SwiftABIInfo(CGT), defaultInfo(CGT) {} |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 730 | |
| 731 | private: |
| 732 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 733 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 734 | |
| 735 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 736 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 737 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 738 | void computeInfo(CGFunctionInfo &FI) const override { |
| 739 | if (!getCXXABI().classifyReturnType(FI)) |
| 740 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 741 | for (auto &Arg : FI.arguments()) |
| 742 | Arg.info = classifyArgumentType(Arg.type); |
| 743 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 744 | |
| 745 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 746 | QualType Ty) const override; |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 747 | |
| 748 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 749 | bool asReturnValue) const override { |
| 750 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 751 | } |
| 752 | |
| 753 | bool isSwiftErrorInRegister() const override { |
| 754 | return false; |
| 755 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 756 | }; |
| 757 | |
| 758 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 759 | public: |
| 760 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 761 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 762 | |
| 763 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 764 | CodeGen::CodeGenModule &CGM) const override { |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 765 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 766 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 767 | if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) { |
| 768 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 769 | llvm::AttrBuilder B; |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 770 | B.addAttribute("wasm-import-module", Attr->getImportModule()); |
| 771 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 772 | } |
| 773 | if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) { |
| 774 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 775 | llvm::AttrBuilder B; |
| 776 | B.addAttribute("wasm-import-name", Attr->getImportName()); |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 777 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 778 | } |
| 779 | } |
| 780 | |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 781 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 782 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 783 | if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) |
| 784 | Fn->addFnAttr("no-prototype"); |
| 785 | } |
| 786 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 787 | }; |
| 788 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 789 | /// Classify argument of given type \p Ty. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 790 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 791 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 792 | |
| 793 | if (isAggregateTypeForABI(Ty)) { |
| 794 | // Records with non-trivial destructors/copy-constructors should not be |
| 795 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 796 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 797 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 798 | // Ignore empty structs/unions. |
| 799 | if (isEmptyRecord(getContext(), Ty, true)) |
| 800 | return ABIArgInfo::getIgnore(); |
| 801 | // Lower single-element structs to just pass a regular value. TODO: We |
| 802 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 803 | // though watch out for things like bitfields. |
| 804 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 805 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 809 | return defaultInfo.classifyArgumentType(Ty); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 813 | if (isAggregateTypeForABI(RetTy)) { |
| 814 | // Records with non-trivial destructors/copy-constructors should not be |
| 815 | // returned by value. |
| 816 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 817 | // Ignore empty structs/unions. |
| 818 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 819 | return ABIArgInfo::getIgnore(); |
| 820 | // Lower single-element structs to just return a regular value. TODO: We |
| 821 | // could do reasonable-size multiple-element structs too, using |
| 822 | // ABIArgInfo::getDirect(). |
| 823 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 824 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 829 | return defaultInfo.classifyReturnType(RetTy); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 832 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 833 | QualType Ty) const { |
| 834 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 835 | getContext().getTypeInfoInChars(Ty), |
| 836 | CharUnits::fromQuantity(4), |
| 837 | /*AllowHigherAlign=*/ true); |
| 838 | } |
| 839 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 840 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 841 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 842 | // |
| 843 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 844 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 845 | //===----------------------------------------------------------------------===// |
| 846 | |
| 847 | class PNaClABIInfo : public ABIInfo { |
| 848 | public: |
| 849 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 850 | |
| 851 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 852 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 853 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 854 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 855 | Address EmitVAArg(CodeGenFunction &CGF, |
| 856 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 857 | }; |
| 858 | |
| 859 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 860 | public: |
| 861 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 862 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 863 | }; |
| 864 | |
| 865 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 866 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 867 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 868 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 869 | for (auto &I : FI.arguments()) |
| 870 | I.info = classifyArgumentType(I.type); |
| 871 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 872 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 873 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 874 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 875 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 876 | // function classification. Structs get passed directly for varargs |
| 877 | // functions, through a rewriting transform in |
| 878 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 879 | // this target to actually support a va_arg instructions with an |
| 880 | // aggregate type, unlike other targets. |
| 881 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 884 | /// Classify argument of given type \p Ty. |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 885 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 886 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 887 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 888 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 889 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 890 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 891 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 892 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 893 | } else if (Ty->isFloatingType()) { |
| 894 | // Floating-point types don't go inreg. |
| 895 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 896 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 897 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 898 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 899 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 903 | if (RetTy->isVoidType()) |
| 904 | return ABIArgInfo::getIgnore(); |
| 905 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 906 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 907 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 908 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 909 | |
| 910 | // Treat an enum type as its underlying type. |
| 911 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 912 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 913 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 914 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 915 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 918 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 919 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 920 | // 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] | 921 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 922 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 923 | IRType->getScalarSizeInBits() != 64; |
| 924 | } |
| 925 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 926 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 927 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 928 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 929 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 930 | .Cases("y", "&y", "^Ym", true) |
| 931 | .Default(false); |
| 932 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 933 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 934 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 935 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 938 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 942 | return Ty; |
| 943 | } |
| 944 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 945 | /// Returns true if this type can be passed in SSE registers with the |
| 946 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 947 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 948 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 949 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 950 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 951 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 952 | &llvm::APFloat::x87DoubleExtended()) |
| 953 | return false; |
| 954 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 955 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 956 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 957 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 958 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 959 | // registers specially. |
| 960 | unsigned VecSize = Context.getTypeSize(VT); |
| 961 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 962 | return true; |
| 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 968 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 969 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 970 | return NumMembers <= 4; |
| 971 | } |
| 972 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 973 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 974 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 975 | auto AI = ABIArgInfo::getDirect(T); |
| 976 | AI.setInReg(true); |
| 977 | AI.setCanBeFlattened(false); |
| 978 | return AI; |
| 979 | } |
| 980 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 981 | //===----------------------------------------------------------------------===// |
| 982 | // X86-32 ABI Implementation |
| 983 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 984 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 985 | /// Similar to llvm::CCState, but for Clang. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 986 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 987 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 988 | |
| 989 | unsigned CC; |
| 990 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 991 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 992 | }; |
| 993 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 994 | enum { |
| 995 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 996 | VectorcallMaxParamNumAsReg = 6 |
| 997 | }; |
| 998 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 999 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1000 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1001 | enum Class { |
| 1002 | Integer, |
| 1003 | Float |
| 1004 | }; |
| 1005 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1006 | static const unsigned MinABIStackAlignInBytes = 4; |
| 1007 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1008 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1009 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1010 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1011 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1012 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1013 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1014 | |
| 1015 | static bool isRegisterSize(unsigned Size) { |
| 1016 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 1017 | } |
| 1018 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1019 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1020 | // FIXME: Assumes vectorcall is in use. |
| 1021 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1022 | } |
| 1023 | |
| 1024 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1025 | uint64_t NumMembers) const override { |
| 1026 | // FIXME: Assumes vectorcall is in use. |
| 1027 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1028 | } |
| 1029 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1030 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1031 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1032 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 1033 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1034 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 1035 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1036 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1037 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1038 | /// Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1039 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1040 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1041 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1042 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1043 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1044 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1045 | /// Updates the number of available free registers, returns |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1046 | /// true if any registers were allocated. |
| 1047 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 1048 | |
| 1049 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1050 | bool &NeedsPadding) const; |
| 1051 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1052 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1053 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1054 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1055 | /// Rewrite the function info so that all memory arguments use |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1056 | /// inalloca. |
| 1057 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1058 | |
| 1059 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1060 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1061 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1062 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1063 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1064 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1065 | public: |
| 1066 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1067 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1068 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1069 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1070 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1071 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1072 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1073 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1074 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1075 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1076 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1077 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1078 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1079 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1080 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1081 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1082 | bool asReturnValue) const override { |
| 1083 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1084 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1085 | // four vector registers for vectors, but those can overlap with the |
| 1086 | // scalar registers. |
| 1087 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1088 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1089 | |
| 1090 | bool isSwiftErrorInRegister() const override { |
| 1091 | // x86-32 lowering does not support passing swifterror in a register. |
| 1092 | return false; |
| 1093 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1094 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1095 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1096 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1097 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1098 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1099 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1100 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1101 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1102 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1103 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1104 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1105 | static bool isStructReturnInRegABI( |
| 1106 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1107 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1108 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1109 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1110 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1111 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1112 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1113 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1114 | return 4; |
| 1115 | } |
| 1116 | |
| 1117 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1118 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1119 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1120 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1121 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1122 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1123 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1124 | } |
| 1125 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1126 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1127 | std::string &Constraints, |
| 1128 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1129 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1130 | std::vector<LValue> &ResultRegDests, |
| 1131 | std::string &AsmString, |
| 1132 | unsigned NumOutputs) const override; |
| 1133 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1134 | llvm::Constant * |
| 1135 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1136 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1137 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1138 | ('v' << 16) | |
| 1139 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1140 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1141 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1142 | |
| 1143 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1144 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1145 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1146 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1147 | }; |
| 1148 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1149 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1150 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1151 | /// Rewrite input constraint references after adding some output constraints. |
| 1152 | /// In the case where there is one output and one input and we add one output, |
| 1153 | /// we need to replace all operand references greater than or equal to 1: |
| 1154 | /// mov $0, $1 |
| 1155 | /// mov eax, $1 |
| 1156 | /// The result will be: |
| 1157 | /// mov $0, $2 |
| 1158 | /// mov eax, $2 |
| 1159 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1160 | unsigned NumNewOuts, |
| 1161 | std::string &AsmString) { |
| 1162 | std::string Buf; |
| 1163 | llvm::raw_string_ostream OS(Buf); |
| 1164 | size_t Pos = 0; |
| 1165 | while (Pos < AsmString.size()) { |
| 1166 | size_t DollarStart = AsmString.find('$', Pos); |
| 1167 | if (DollarStart == std::string::npos) |
| 1168 | DollarStart = AsmString.size(); |
| 1169 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1170 | if (DollarEnd == std::string::npos) |
| 1171 | DollarEnd = AsmString.size(); |
| 1172 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1173 | Pos = DollarEnd; |
| 1174 | size_t NumDollars = DollarEnd - DollarStart; |
| 1175 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1176 | // We have an operand reference. |
| 1177 | size_t DigitStart = Pos; |
| 1178 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1179 | if (DigitEnd == std::string::npos) |
| 1180 | DigitEnd = AsmString.size(); |
| 1181 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1182 | unsigned OperandIndex; |
| 1183 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1184 | if (OperandIndex >= FirstIn) |
| 1185 | OperandIndex += NumNewOuts; |
| 1186 | OS << OperandIndex; |
| 1187 | } else { |
| 1188 | OS << OperandStr; |
| 1189 | } |
| 1190 | Pos = DigitEnd; |
| 1191 | } |
| 1192 | } |
| 1193 | AsmString = std::move(OS.str()); |
| 1194 | } |
| 1195 | |
| 1196 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1197 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1198 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1199 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1200 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1201 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1202 | unsigned NumOutputs) const { |
| 1203 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1204 | |
| 1205 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1206 | // larger. |
| 1207 | if (!Constraints.empty()) |
| 1208 | Constraints += ','; |
| 1209 | if (RetWidth <= 32) { |
| 1210 | Constraints += "={eax}"; |
| 1211 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1212 | } else { |
| 1213 | // Use the 'A' constraint for EAX:EDX. |
| 1214 | Constraints += "=A"; |
| 1215 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1216 | } |
| 1217 | |
| 1218 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1219 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1220 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1221 | |
| 1222 | // Coerce the integer by bitcasting the return slot pointer. |
| 1223 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1224 | CoerceTy->getPointerTo())); |
| 1225 | ResultRegDests.push_back(ReturnSlot); |
| 1226 | |
| 1227 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1228 | } |
| 1229 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1230 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1231 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1232 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1233 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1234 | uint64_t Size = Context.getTypeSize(Ty); |
| 1235 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1236 | // For i386, type must be register sized. |
| 1237 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1238 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1239 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (Ty->isVectorType()) { |
| 1242 | // 64- and 128- bit vectors inside structures are not returned in |
| 1243 | // registers. |
| 1244 | if (Size == 64 || Size == 128) |
| 1245 | return false; |
| 1246 | |
| 1247 | return true; |
| 1248 | } |
| 1249 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1250 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1251 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1252 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1253 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1254 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1255 | return true; |
| 1256 | |
| 1257 | // Arrays are treated like records. |
| 1258 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1259 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1262 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1263 | if (!RT) return false; |
| 1264 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1265 | // FIXME: Traverse bases here too. |
| 1266 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1267 | // Structure types are passed in register if all fields would be |
| 1268 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1269 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1270 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1271 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1272 | continue; |
| 1273 | |
| 1274 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1275 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1276 | return false; |
| 1277 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1278 | return true; |
| 1279 | } |
| 1280 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1281 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1282 | // Treat complex types as the element type. |
| 1283 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1284 | Ty = CTy->getElementType(); |
| 1285 | |
| 1286 | // Check for a type which we know has a simple scalar argument-passing |
| 1287 | // convention without any padding. (We're specifically looking for 32 |
| 1288 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1289 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1290 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1291 | return false; |
| 1292 | |
| 1293 | uint64_t Size = Context.getTypeSize(Ty); |
| 1294 | return Size == 32 || Size == 64; |
| 1295 | } |
| 1296 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1297 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1298 | uint64_t &Size) { |
| 1299 | for (const auto *FD : RD->fields()) { |
| 1300 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1301 | // argument is smaller than 32-bits, expanding the struct will create |
| 1302 | // alignment padding. |
| 1303 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1304 | return false; |
| 1305 | |
| 1306 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1307 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1308 | // counts as "basic" is more complicated than what we were doing previously. |
| 1309 | if (FD->isBitField()) |
| 1310 | return false; |
| 1311 | |
| 1312 | Size += Context.getTypeSize(FD->getType()); |
| 1313 | } |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
| 1317 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1318 | uint64_t &Size) { |
| 1319 | // Don't do this if there are any non-empty bases. |
| 1320 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1321 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1322 | Size)) |
| 1323 | return false; |
| 1324 | } |
| 1325 | if (!addFieldSizes(Context, RD, Size)) |
| 1326 | return false; |
| 1327 | return true; |
| 1328 | } |
| 1329 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1330 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1331 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1332 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1333 | /// optimizations. |
| 1334 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1335 | // We can only expand structure types. |
| 1336 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1337 | if (!RT) |
| 1338 | return false; |
| 1339 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1340 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1341 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1342 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1343 | // On non-Windows, we have to conservatively match our old bitcode |
| 1344 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1345 | if (!CXXRD->isCLike()) |
| 1346 | return false; |
| 1347 | } else { |
| 1348 | // Don't do this for dynamic classes. |
| 1349 | if (CXXRD->isDynamicClass()) |
| 1350 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1351 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1352 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1353 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1354 | } else { |
| 1355 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1356 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | // We can do this if there was no alignment padding. |
| 1360 | return Size == getContext().getTypeSize(Ty); |
| 1361 | } |
| 1362 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1363 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1364 | // If the return value is indirect, then the hidden argument is consuming one |
| 1365 | // integer register. |
| 1366 | if (State.FreeRegs) { |
| 1367 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1368 | if (!IsMCUABI) |
| 1369 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1370 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1371 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1374 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1375 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1376 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1377 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1378 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1379 | const Type *Base = nullptr; |
| 1380 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1381 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1382 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1383 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1384 | // The LLVM struct type for such an aggregate should lower properly. |
| 1385 | return ABIArgInfo::getDirect(); |
| 1386 | } |
| 1387 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1388 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1389 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1390 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1391 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1392 | |
| 1393 | // 128-bit vectors are a special case; they are returned in |
| 1394 | // registers and we need to make sure to pick a type the LLVM |
| 1395 | // backend will like. |
| 1396 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1397 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1398 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1399 | |
| 1400 | // Always return in register if it fits in a general purpose |
| 1401 | // register, or if it is 64 bits and has a single element. |
| 1402 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1403 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1404 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1405 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1406 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1407 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1411 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1412 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1413 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1414 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1415 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1416 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1417 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1418 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1419 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1420 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1421 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1422 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1423 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1424 | // Ignore empty structs/unions. |
| 1425 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1426 | return ABIArgInfo::getIgnore(); |
| 1427 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1428 | // Small structures which are register sized are generally returned |
| 1429 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1430 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1431 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1432 | |
| 1433 | // As a special-case, if the struct is a "single-element" struct, and |
| 1434 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1435 | // floating-point register. (MSVC does not apply this special case.) |
| 1436 | // We apply a similar transformation for pointer types to improve the |
| 1437 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1438 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1439 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1440 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1441 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1442 | |
| 1443 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1444 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1445 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1448 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1449 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1451 | // Treat an enum type as its underlying type. |
| 1452 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1453 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1454 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1455 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1456 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1459 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1460 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1461 | } |
| 1462 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1463 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1464 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1465 | if (!RT) |
| 1466 | return 0; |
| 1467 | const RecordDecl *RD = RT->getDecl(); |
| 1468 | |
| 1469 | // If this is a C++ record, check the bases first. |
| 1470 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1471 | for (const auto &I : CXXRD->bases()) |
| 1472 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1473 | return false; |
| 1474 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1475 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1476 | QualType FT = i->getType(); |
| 1477 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1478 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1479 | return true; |
| 1480 | |
| 1481 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1482 | return true; |
| 1483 | } |
| 1484 | |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1488 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1489 | unsigned Align) const { |
| 1490 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1491 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1492 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1493 | return 0; // Use default alignment. |
| 1494 | |
Pengfei Wang | 48387ec | 2019-05-31 01:50:07 +0000 | [diff] [blame^] | 1495 | // On non-Darwin, the stack type alignment is always 4. |
| 1496 | if (!IsDarwinVectorABI) { |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1497 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1498 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1499 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1500 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1501 | // 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] | 1502 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1503 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1504 | return 16; |
| 1505 | |
| 1506 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1509 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1510 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1511 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1512 | if (State.FreeRegs) { |
| 1513 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1514 | if (!IsMCUABI) |
| 1515 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1516 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1517 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1518 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1519 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1520 | // Compute the byval alignment. |
| 1521 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1522 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1523 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1524 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1525 | |
| 1526 | // If the stack alignment is less than the type alignment, realign the |
| 1527 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1528 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1529 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1530 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1533 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1534 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1535 | if (!T) |
| 1536 | T = Ty.getTypePtr(); |
| 1537 | |
| 1538 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1539 | BuiltinType::Kind K = BT->getKind(); |
| 1540 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1541 | return Float; |
| 1542 | } |
| 1543 | return Integer; |
| 1544 | } |
| 1545 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1546 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1547 | if (!IsSoftFloatABI) { |
| 1548 | Class C = classify(Ty); |
| 1549 | if (C == Float) |
| 1550 | return false; |
| 1551 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1552 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1553 | unsigned Size = getContext().getTypeSize(Ty); |
| 1554 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1555 | |
| 1556 | if (SizeInRegs == 0) |
| 1557 | return false; |
| 1558 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1559 | if (!IsMCUABI) { |
| 1560 | if (SizeInRegs > State.FreeRegs) { |
| 1561 | State.FreeRegs = 0; |
| 1562 | return false; |
| 1563 | } |
| 1564 | } else { |
| 1565 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1566 | // earlier parameters that are passed on the stack. Also, |
| 1567 | // it does not allow passing >8-byte structs in-register, |
| 1568 | // even if there are 3 free registers available. |
| 1569 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1570 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1571 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1572 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1573 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1574 | return true; |
| 1575 | } |
| 1576 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1577 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1578 | bool &InReg, |
| 1579 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1580 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1581 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1582 | // (HFAs) have already been dealt with at this point. |
| 1583 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1584 | return false; |
| 1585 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1586 | NeedsPadding = false; |
| 1587 | InReg = !IsMCUABI; |
| 1588 | |
| 1589 | if (!updateFreeRegs(Ty, State)) |
| 1590 | return false; |
| 1591 | |
| 1592 | if (IsMCUABI) |
| 1593 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1594 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1595 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1596 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1597 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1598 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1599 | NeedsPadding = true; |
| 1600 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
| 1603 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1604 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1607 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1608 | if (!updateFreeRegs(Ty, State)) |
| 1609 | return false; |
| 1610 | |
| 1611 | if (IsMCUABI) |
| 1612 | return false; |
| 1613 | |
| 1614 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1615 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1616 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1617 | if (getContext().getTypeSize(Ty) > 32) |
| 1618 | return false; |
| 1619 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1620 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1621 | Ty->isReferenceType()); |
| 1622 | } |
| 1623 | |
| 1624 | return true; |
| 1625 | } |
| 1626 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1627 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1628 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1629 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1630 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1631 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1632 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1633 | // Check with the C++ ABI first. |
| 1634 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1635 | if (RT) { |
| 1636 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1637 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1638 | return getIndirectResult(Ty, false, State); |
| 1639 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1640 | // The field index doesn't matter, we'll fix it up later. |
| 1641 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1642 | } |
| 1643 | } |
| 1644 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1645 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1646 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1647 | const Type *Base = nullptr; |
| 1648 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1649 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1650 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1651 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1652 | if (State.FreeSSERegs >= NumElts) { |
| 1653 | State.FreeSSERegs -= NumElts; |
| 1654 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1655 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1656 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1657 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1658 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1662 | // Structures with flexible arrays are always indirect. |
| 1663 | // FIXME: This should not be byval! |
| 1664 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1665 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1666 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1667 | // Ignore empty structs/unions on non-Windows. |
| 1668 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1669 | return ABIArgInfo::getIgnore(); |
| 1670 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1671 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1672 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1673 | bool NeedsPadding = false; |
| 1674 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1675 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1676 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1677 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1678 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1679 | if (InReg) |
| 1680 | return ABIArgInfo::getDirectInReg(Result); |
| 1681 | else |
| 1682 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1683 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1684 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1685 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1686 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1687 | // of those arguments will match the struct. This is important because the |
| 1688 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1689 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1690 | // Don't do this for the MCU if there are still free integer registers |
| 1691 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1692 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1693 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1694 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1695 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1696 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1697 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1698 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1699 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1700 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1703 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1704 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1705 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1706 | if (IsDarwinVectorABI) { |
| 1707 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1708 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1709 | (Size == 64 && VT->getNumElements() == 1)) |
| 1710 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1711 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1712 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1713 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1714 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1715 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1717 | return ABIArgInfo::getDirect(); |
| 1718 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1719 | |
| 1720 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1721 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1722 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1723 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1724 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1725 | |
| 1726 | if (Ty->isPromotableIntegerType()) { |
| 1727 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1728 | return ABIArgInfo::getExtendInReg(Ty); |
| 1729 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1730 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1731 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1732 | if (InReg) |
| 1733 | return ABIArgInfo::getDirectInReg(); |
| 1734 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1737 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1738 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1739 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1740 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1741 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1742 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1743 | // registers. |
| 1744 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1745 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1746 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1747 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1748 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1749 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1750 | // First pass do all the vector types. |
| 1751 | const Type *Base = nullptr; |
| 1752 | uint64_t NumElts = 0; |
| 1753 | const QualType& Ty = I.type; |
| 1754 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1755 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1756 | if (State.FreeSSERegs >= NumElts) { |
| 1757 | State.FreeSSERegs -= NumElts; |
| 1758 | I.info = ABIArgInfo::getDirect(); |
| 1759 | } else { |
| 1760 | I.info = classifyArgumentType(Ty, State); |
| 1761 | } |
| 1762 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1763 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1764 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1765 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1766 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1767 | // Second pass, do the rest! |
| 1768 | const Type *Base = nullptr; |
| 1769 | uint64_t NumElts = 0; |
| 1770 | const QualType& Ty = I.type; |
| 1771 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1772 | |
| 1773 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1774 | // Assign true HVAs (non vector/native FP types). |
| 1775 | if (State.FreeSSERegs >= NumElts) { |
| 1776 | State.FreeSSERegs -= NumElts; |
| 1777 | I.info = getDirectX86Hva(); |
| 1778 | } else { |
| 1779 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1780 | } |
| 1781 | } else if (!IsHva) { |
| 1782 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1783 | I.info = classifyArgumentType(Ty, State); |
| 1784 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1785 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1789 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1790 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1791 | if (IsMCUABI) |
| 1792 | State.FreeRegs = 3; |
| 1793 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1794 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1795 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1796 | State.FreeRegs = 2; |
| 1797 | State.FreeSSERegs = 6; |
| 1798 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1799 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1800 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1801 | State.FreeRegs = 5; |
| 1802 | State.FreeSSERegs = 8; |
| 1803 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1804 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1805 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 1806 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1807 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1808 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1809 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1810 | // return value was sret and put it in a register ourselves if appropriate. |
| 1811 | if (State.FreeRegs) { |
| 1812 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1813 | if (!IsMCUABI) |
| 1814 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1815 | } |
| 1816 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1817 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1818 | // The chain argument effectively gives us another free register. |
| 1819 | if (FI.isChainCall()) |
| 1820 | ++State.FreeRegs; |
| 1821 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1822 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1823 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1824 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1825 | } else { |
| 1826 | // If not vectorcall, revert to normal behavior. |
| 1827 | for (auto &I : FI.arguments()) { |
| 1828 | I.info = classifyArgumentType(I.type, State); |
| 1829 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1830 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1834 | // all the memory arguments to use inalloca. |
| 1835 | if (UsedInAlloca) |
| 1836 | rewriteWithInAlloca(FI); |
| 1837 | } |
| 1838 | |
| 1839 | void |
| 1840 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1841 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1842 | QualType Type) const { |
| 1843 | // Arguments are always 4-byte-aligned. |
| 1844 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1845 | |
| 1846 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1847 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1848 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1849 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1850 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1851 | // Insert padding bytes to respect alignment. |
| 1852 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1853 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1854 | if (StackOffset != FieldEnd) { |
| 1855 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1856 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1857 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1858 | FrameFields.push_back(Ty); |
| 1859 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1862 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1863 | // Leave ignored and inreg arguments alone. |
| 1864 | switch (Info.getKind()) { |
| 1865 | case ABIArgInfo::InAlloca: |
| 1866 | return true; |
| 1867 | case ABIArgInfo::Indirect: |
| 1868 | assert(Info.getIndirectByVal()); |
| 1869 | return true; |
| 1870 | case ABIArgInfo::Ignore: |
| 1871 | return false; |
| 1872 | case ABIArgInfo::Direct: |
| 1873 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1874 | if (Info.getInReg()) |
| 1875 | return false; |
| 1876 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1877 | case ABIArgInfo::Expand: |
| 1878 | case ABIArgInfo::CoerceAndExpand: |
| 1879 | // These are aggregate types which are never passed in registers when |
| 1880 | // inalloca is involved. |
| 1881 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1882 | } |
| 1883 | llvm_unreachable("invalid enum"); |
| 1884 | } |
| 1885 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1886 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1887 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1888 | |
| 1889 | // Build a packed struct type for all of the arguments in memory. |
| 1890 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1891 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1892 | // The stack alignment is always 4. |
| 1893 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1894 | |
| 1895 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1896 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1897 | |
| 1898 | // Put 'this' into the struct before 'sret', if necessary. |
| 1899 | bool IsThisCall = |
| 1900 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1901 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1902 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1903 | isArgInAlloca(I->info)) { |
| 1904 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1905 | ++I; |
| 1906 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1907 | |
| 1908 | // 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] | 1909 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1910 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1911 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1912 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1913 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1917 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1918 | ++I; |
| 1919 | |
| 1920 | // Put arguments passed in memory into the struct. |
| 1921 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1922 | if (isArgInAlloca(I->info)) |
| 1923 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1927 | /*isPacked=*/true), |
| 1928 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1931 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1932 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1933 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1934 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1935 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1936 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1937 | // |
| 1938 | // Just messing with TypeInfo like this works because we never pass |
| 1939 | // anything indirectly. |
| 1940 | TypeInfo.second = CharUnits::fromQuantity( |
| 1941 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1942 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1943 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1944 | TypeInfo, CharUnits::fromQuantity(4), |
| 1945 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1948 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1949 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1950 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1951 | |
| 1952 | switch (Opts.getStructReturnConvention()) { |
| 1953 | case CodeGenOptions::SRCK_Default: |
| 1954 | break; |
| 1955 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1956 | return false; |
| 1957 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1958 | return true; |
| 1959 | } |
| 1960 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1961 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1962 | return true; |
| 1963 | |
| 1964 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1965 | case llvm::Triple::DragonFly: |
| 1966 | case llvm::Triple::FreeBSD: |
| 1967 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1968 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1969 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1970 | default: |
| 1971 | return false; |
| 1972 | } |
| 1973 | } |
| 1974 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1975 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1976 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1977 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1978 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1979 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1980 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1981 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 1982 | Fn->addFnAttr("stackrealign"); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1983 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1984 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1985 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1986 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1987 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1991 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1992 | CodeGen::CodeGenFunction &CGF, |
| 1993 | llvm::Value *Address) const { |
| 1994 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1995 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1996 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1997 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1998 | // 0-7 are the eight integer registers; the order is different |
| 1999 | // on Darwin (for EH), but the range is the same. |
| 2000 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2001 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2002 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2003 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2004 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 2005 | // These have size 16, which is sizeof(long double) on |
| 2006 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2007 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2008 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2009 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2010 | } else { |
| 2011 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 2012 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2013 | Builder.CreateAlignedStore( |
| 2014 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 2015 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2016 | |
| 2017 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 2018 | // These have size 12, which is sizeof(long double) on |
| 2019 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2020 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2021 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 2022 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2023 | |
| 2024 | return false; |
| 2025 | } |
| 2026 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2027 | //===----------------------------------------------------------------------===// |
| 2028 | // X86-64 ABI Implementation |
| 2029 | //===----------------------------------------------------------------------===// |
| 2030 | |
| 2031 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2032 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2033 | /// The AVX ABI level for X86 targets. |
| 2034 | enum class X86AVXABILevel { |
| 2035 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2036 | AVX, |
| 2037 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2038 | }; |
| 2039 | |
| 2040 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2041 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2042 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2043 | case X86AVXABILevel::AVX512: |
| 2044 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2045 | case X86AVXABILevel::AVX: |
| 2046 | return 256; |
| 2047 | case X86AVXABILevel::None: |
| 2048 | return 128; |
| 2049 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2050 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2053 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2054 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2055 | enum Class { |
| 2056 | Integer = 0, |
| 2057 | SSE, |
| 2058 | SSEUp, |
| 2059 | X87, |
| 2060 | X87Up, |
| 2061 | ComplexX87, |
| 2062 | NoClass, |
| 2063 | Memory |
| 2064 | }; |
| 2065 | |
| 2066 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2067 | /// |
| 2068 | /// Merge an accumulating classification \arg Accum with a field |
| 2069 | /// classification \arg Field. |
| 2070 | /// |
| 2071 | /// \param Accum - The accumulating classification. This should |
| 2072 | /// always be either NoClass or the result of a previous merge |
| 2073 | /// call. In addition, this should never be Memory (the caller |
| 2074 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2075 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2076 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2077 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2078 | /// |
| 2079 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2080 | /// final MEMORY or SSE classes when necessary. |
| 2081 | /// |
| 2082 | /// \param AggregateSize - The size of the current aggregate in |
| 2083 | /// the classification process. |
| 2084 | /// |
| 2085 | /// \param Lo - The classification for the parts of the type |
| 2086 | /// residing in the low word of the containing object. |
| 2087 | /// |
| 2088 | /// \param Hi - The classification for the parts of the type |
| 2089 | /// residing in the higher words of the containing object. |
| 2090 | /// |
| 2091 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2092 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2093 | /// classify - Determine the x86_64 register classes in which the |
| 2094 | /// given type T should be passed. |
| 2095 | /// |
| 2096 | /// \param Lo - The classification for the parts of the type |
| 2097 | /// residing in the low word of the containing object. |
| 2098 | /// |
| 2099 | /// \param Hi - The classification for the parts of the type |
| 2100 | /// residing in the high word of the containing object. |
| 2101 | /// |
| 2102 | /// \param OffsetBase - The bit offset of this type in the |
| 2103 | /// containing object. Some parameters are classified different |
| 2104 | /// depending on whether they straddle an eightbyte boundary. |
| 2105 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2106 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2107 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2108 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2109 | /// If a word is unused its result will be NoClass; if a type should |
| 2110 | /// be passed in Memory then at least the classification of \arg Lo |
| 2111 | /// will be Memory. |
| 2112 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2113 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2114 | /// |
| 2115 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2116 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2117 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2118 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2119 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2120 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2121 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2122 | unsigned IROffset, QualType SourceTy, |
| 2123 | unsigned SourceOffset) const; |
| 2124 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2125 | unsigned IROffset, QualType SourceTy, |
| 2126 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2127 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2128 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2129 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2130 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2131 | |
| 2132 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2133 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2134 | /// |
| 2135 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2136 | /// available. |
| 2137 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2138 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2139 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2140 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2141 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2142 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2143 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2144 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2145 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2146 | unsigned &NeededSSE) const; |
| 2147 | |
| 2148 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2149 | unsigned &NeededSSE) const; |
| 2150 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2151 | bool IsIllegalVectorType(QualType Ty) const; |
| 2152 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2153 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2154 | /// unfortunately in ways that were not always consistent with |
| 2155 | /// certain previous compilers. In particular, platforms which |
| 2156 | /// required strict binary compatibility with older versions of GCC |
| 2157 | /// may need to exempt themselves. |
| 2158 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2159 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2162 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2163 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2164 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2165 | // Clang <= 3.8 did not do this. |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 2166 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2167 | LangOptions::ClangABI::Ver3_8) |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2168 | return false; |
| 2169 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2170 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2171 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2172 | return false; |
| 2173 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2174 | return false; |
| 2175 | return true; |
| 2176 | } |
| 2177 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2178 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2179 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2180 | // 64-bit hardware. |
| 2181 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2182 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2183 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2184 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2185 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2186 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2187 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2188 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2189 | bool isPassedUsingAVXType(QualType type) const { |
| 2190 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2191 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2192 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2193 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2194 | if (info.isDirect()) { |
| 2195 | llvm::Type *ty = info.getCoerceToType(); |
| 2196 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2197 | return (vectorTy->getBitWidth() > 128); |
| 2198 | } |
| 2199 | return false; |
| 2200 | } |
| 2201 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2202 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2203 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2204 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2205 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2206 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2207 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2208 | |
| 2209 | bool has64BitPointers() const { |
| 2210 | return Has64BitPointers; |
| 2211 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2212 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2213 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2214 | bool asReturnValue) const override { |
| 2215 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2216 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2217 | bool isSwiftErrorInRegister() const override { |
| 2218 | return true; |
| 2219 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2220 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2221 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2222 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2223 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2224 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2225 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2226 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2227 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2228 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2229 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2230 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2231 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2232 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2233 | |
| 2234 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2235 | // FIXME: Assumes vectorcall is in use. |
| 2236 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2237 | } |
| 2238 | |
| 2239 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2240 | uint64_t NumMembers) const override { |
| 2241 | // FIXME: Assumes vectorcall is in use. |
| 2242 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2243 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2244 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2245 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2246 | bool asReturnValue) const override { |
| 2247 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2248 | } |
| 2249 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2250 | bool isSwiftErrorInRegister() const override { |
| 2251 | return true; |
| 2252 | } |
| 2253 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2254 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2255 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2256 | bool IsVectorCall, bool IsRegCall) const; |
| 2257 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2258 | const ABIArgInfo ¤t) const; |
| 2259 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2260 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2261 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2262 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2263 | }; |
| 2264 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2265 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2266 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2267 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2268 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2269 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2270 | const X86_64ABIInfo &getABIInfo() const { |
| 2271 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2272 | } |
| 2273 | |
Akira Hatanaka | 65bb3f9 | 2019-03-21 19:59:49 +0000 | [diff] [blame] | 2274 | /// Disable tail call on x86-64. The epilogue code before the tail jump blocks |
| 2275 | /// the autoreleaseRV/retainRV optimization. |
| 2276 | bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override { |
| 2277 | return true; |
| 2278 | } |
| 2279 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2280 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2281 | return 7; |
| 2282 | } |
| 2283 | |
| 2284 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2285 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2286 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2287 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2288 | // 0-15 are the 16 integer registers. |
| 2289 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2290 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2291 | return false; |
| 2292 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2293 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2294 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2295 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2296 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2297 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2298 | } |
| 2299 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2300 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2301 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2302 | // The default CC on x86-64 sets %al to the number of SSA |
| 2303 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2304 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2305 | // that when AVX types are involved: the ABI explicitly states it is |
| 2306 | // undefined, and it doesn't work in practice because of how the ABI |
| 2307 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2308 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2309 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2310 | for (CallArgList::const_iterator |
| 2311 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2312 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2313 | HasAVXType = true; |
| 2314 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2315 | } |
| 2316 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2317 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2318 | if (!HasAVXType) |
| 2319 | return true; |
| 2320 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2321 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2322 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2325 | llvm::Constant * |
| 2326 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2327 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2328 | (0x06 << 8) | // .+0x08 |
| 2329 | ('v' << 16) | |
| 2330 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2331 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2332 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2333 | |
| 2334 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2335 | CodeGen::CodeGenModule &CGM) const override { |
| 2336 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2337 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2338 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2339 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2340 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2341 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2342 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2343 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2344 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2345 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2349 | }; |
| 2350 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2351 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2352 | // If the argument does not end in .lib, automatically add the suffix. |
| 2353 | // If the argument contains a space, enclose it in quotes. |
| 2354 | // This matches the behavior of MSVC. |
| 2355 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2356 | std::string ArgStr = Quote ? "\"" : ""; |
| 2357 | ArgStr += Lib; |
Martin Storsjo | 3cd67c9 | 2018-10-10 09:01:00 +0000 | [diff] [blame] | 2358 | if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2359 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2360 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2361 | return ArgStr; |
| 2362 | } |
| 2363 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2364 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2365 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2366 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2367 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2368 | unsigned NumRegisterParameters) |
| 2369 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2370 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2371 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2372 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2373 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2374 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2375 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2376 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2377 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2378 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2379 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2380 | |
| 2381 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2382 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2383 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2384 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2385 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2386 | }; |
| 2387 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2388 | static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2389 | CodeGen::CodeGenModule &CGM) { |
| 2390 | if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2391 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2392 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2393 | Fn->addFnAttr("stack-probe-size", |
| 2394 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2395 | if (CGM.getCodeGenOpts().NoStackArgProbe) |
| 2396 | Fn->addFnAttr("no-stack-arg-probe"); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2400 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2401 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2402 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2403 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2404 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2405 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2408 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2409 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2410 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2411 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2412 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2413 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2414 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2415 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2416 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2417 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2418 | return 7; |
| 2419 | } |
| 2420 | |
| 2421 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2422 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2423 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2425 | // 0-15 are the 16 integer registers. |
| 2426 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2427 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2428 | return false; |
| 2429 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2430 | |
| 2431 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2432 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2433 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2434 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2435 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2436 | |
| 2437 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2438 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2439 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2440 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2441 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2442 | }; |
| 2443 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2444 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2445 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2446 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2447 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2448 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2449 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2450 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2451 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2452 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2453 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2454 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2455 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2456 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2457 | } |
| 2458 | } |
| 2459 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2460 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2461 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2462 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2463 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2464 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2465 | Class &Hi) const { |
| 2466 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2467 | // |
| 2468 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2469 | // memory. |
| 2470 | // |
| 2471 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2472 | // memory. |
| 2473 | // |
| 2474 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2475 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2476 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2477 | // ABI working for processors that don't support the __m256 type. |
| 2478 | // |
| 2479 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2480 | // |
| 2481 | // Some of these are enforced by the merging logic. Others can arise |
| 2482 | // only with unions; for example: |
| 2483 | // union { _Complex double; unsigned; } |
| 2484 | // |
| 2485 | // Note that clauses (b) and (c) were added in 0.98. |
| 2486 | // |
| 2487 | if (Hi == Memory) |
| 2488 | Lo = Memory; |
| 2489 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2490 | Lo = Memory; |
| 2491 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2492 | Lo = Memory; |
| 2493 | if (Hi == SSEUp && Lo != SSE) |
| 2494 | Hi = SSE; |
| 2495 | } |
| 2496 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2497 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2498 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2499 | // classified recursively so that always two fields are |
| 2500 | // considered. The resulting class is calculated according to |
| 2501 | // the classes of the fields in the eightbyte: |
| 2502 | // |
| 2503 | // (a) If both classes are equal, this is the resulting class. |
| 2504 | // |
| 2505 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2506 | // the other class. |
| 2507 | // |
| 2508 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2509 | // class. |
| 2510 | // |
| 2511 | // (d) If one of the classes is INTEGER, the result is the |
| 2512 | // INTEGER. |
| 2513 | // |
| 2514 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2515 | // MEMORY is used as class. |
| 2516 | // |
| 2517 | // (f) Otherwise class SSE is used. |
| 2518 | |
| 2519 | // Accum should never be memory (we should have returned) or |
| 2520 | // ComplexX87 (because this cannot be passed in a structure). |
| 2521 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2522 | "Invalid accumulated classification during merge."); |
| 2523 | if (Accum == Field || Field == NoClass) |
| 2524 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2525 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2526 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2527 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2528 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2529 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2530 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2531 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2532 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2533 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2534 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2537 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2538 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2539 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2540 | // Class pairs with appropriate constructor methods for the various |
| 2541 | // situations. |
| 2542 | |
| 2543 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2544 | // shouldn't be passed in registers for example, so there is no chance they |
| 2545 | // can straddle an eightbyte. Verify & simplify. |
| 2546 | |
| 2547 | Lo = Hi = NoClass; |
| 2548 | |
| 2549 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2550 | Current = Memory; |
| 2551 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2552 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2553 | BuiltinType::Kind k = BT->getKind(); |
| 2554 | |
| 2555 | if (k == BuiltinType::Void) { |
| 2556 | Current = NoClass; |
| 2557 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2558 | Lo = Integer; |
| 2559 | Hi = Integer; |
| 2560 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2561 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2562 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2563 | Current = SSE; |
| 2564 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2565 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2566 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2567 | Lo = SSE; |
| 2568 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2569 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2570 | Lo = X87; |
| 2571 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2572 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2573 | Current = SSE; |
| 2574 | } else |
| 2575 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2576 | } |
| 2577 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2578 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2582 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2583 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2584 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2585 | return; |
| 2586 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2587 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2588 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2589 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2590 | return; |
| 2591 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2593 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2594 | if (Ty->isMemberFunctionPointerType()) { |
| 2595 | if (Has64BitPointers) { |
| 2596 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2597 | // Lo and Hi now. |
| 2598 | Lo = Hi = Integer; |
| 2599 | } else { |
| 2600 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2601 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2602 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2603 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2604 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2605 | Lo = Hi = Integer; |
| 2606 | } else { |
| 2607 | Current = Integer; |
| 2608 | } |
| 2609 | } |
| 2610 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2611 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2612 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2615 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2616 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2617 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2618 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2619 | // gcc passes the following as integer: |
| 2620 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2621 | // 2 bytes - <2 x char>, <1 x short> |
| 2622 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2623 | Current = Integer; |
| 2624 | |
| 2625 | // If this type crosses an eightbyte boundary, it should be |
| 2626 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2627 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2628 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2629 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2630 | Hi = Lo; |
| 2631 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2632 | QualType ElementType = VT->getElementType(); |
| 2633 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2634 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2635 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2636 | return; |
| 2637 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2638 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2639 | // pass them as integer. For platforms where clang is the de facto |
| 2640 | // platform compiler, we must continue to use integer. |
| 2641 | if (!classifyIntegerMMXAsSSE() && |
| 2642 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2643 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2644 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2645 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2646 | Current = Integer; |
| 2647 | else |
| 2648 | Current = SSE; |
| 2649 | |
| 2650 | // If this type crosses an eightbyte boundary, it should be |
| 2651 | // split. |
| 2652 | if (OffsetBase && OffsetBase != 64) |
| 2653 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2654 | } else if (Size == 128 || |
| 2655 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2656 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2657 | // least significant one belongs to class SSE and all the others to class |
| 2658 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2659 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2660 | // This design isn't correct for 256-bits, but since there're no cases |
| 2661 | // where the upper parts would need to be inspected, avoid adding |
| 2662 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2663 | // |
| 2664 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2665 | // registers if they are "named", i.e. not part of the "..." of a |
| 2666 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2667 | // |
| 2668 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2669 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2670 | Lo = SSE; |
| 2671 | Hi = SSEUp; |
| 2672 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2673 | return; |
| 2674 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2675 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2676 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2677 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2678 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2679 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2680 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2681 | if (Size <= 64) |
| 2682 | Current = Integer; |
| 2683 | else if (Size <= 128) |
| 2684 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2685 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2686 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2687 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2688 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2689 | } else if (ET == getContext().LongDoubleTy) { |
| 2690 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2691 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2692 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2693 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2694 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2695 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2696 | Lo = Hi = SSE; |
| 2697 | else |
| 2698 | llvm_unreachable("unexpected long double representation!"); |
| 2699 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2700 | |
| 2701 | // If this complex type crosses an eightbyte boundary then it |
| 2702 | // should be split. |
| 2703 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2704 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2705 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2706 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2707 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2708 | return; |
| 2709 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2710 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2711 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2712 | // Arrays are treated like structures. |
| 2713 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2714 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2715 | |
| 2716 | // 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] | 2717 | // than eight eightbytes, ..., it has class MEMORY. |
| 2718 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2719 | return; |
| 2720 | |
| 2721 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2722 | // fields, it has class MEMORY. |
| 2723 | // |
| 2724 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2725 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2726 | return; |
| 2727 | |
| 2728 | // Otherwise implement simplified merge. We could be smarter about |
| 2729 | // this, but it isn't worth it and would be harder to verify. |
| 2730 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2731 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2732 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2733 | |
| 2734 | // The only case a 256-bit wide vector could be used is when the array |
| 2735 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2736 | // 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] | 2737 | // |
| 2738 | if (Size > 128 && |
| 2739 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2742 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2743 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2744 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2745 | Lo = merge(Lo, FieldLo); |
| 2746 | Hi = merge(Hi, FieldHi); |
| 2747 | if (Lo == Memory || Hi == Memory) |
| 2748 | break; |
| 2749 | } |
| 2750 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2751 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2752 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2753 | return; |
| 2754 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2756 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2757 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2758 | |
| 2759 | // 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] | 2760 | // than eight eightbytes, ..., it has class MEMORY. |
| 2761 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2762 | return; |
| 2763 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2764 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2765 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2766 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2767 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2768 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2769 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2770 | const RecordDecl *RD = RT->getDecl(); |
| 2771 | |
| 2772 | // Assume variable sized types are passed in memory. |
| 2773 | if (RD->hasFlexibleArrayMember()) |
| 2774 | return; |
| 2775 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2776 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2777 | |
| 2778 | // Reset Lo class, this will be recomputed. |
| 2779 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2780 | |
| 2781 | // If this is a C++ record, classify the bases first. |
| 2782 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2783 | for (const auto &I : CXXRD->bases()) { |
| 2784 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2785 | "Unexpected base class!"); |
| 2786 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2787 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2788 | |
| 2789 | // Classify this field. |
| 2790 | // |
| 2791 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2792 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2793 | // initialized to class NO_CLASS. |
| 2794 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2795 | uint64_t Offset = |
| 2796 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2797 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2798 | Lo = merge(Lo, FieldLo); |
| 2799 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2800 | if (Lo == Memory || Hi == Memory) { |
| 2801 | postMerge(Size, Lo, Hi); |
| 2802 | return; |
| 2803 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2808 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2809 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2810 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2811 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2812 | bool BitField = i->isBitField(); |
| 2813 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2814 | // Ignore padding bit-fields. |
| 2815 | if (BitField && i->isUnnamedBitfield()) |
| 2816 | continue; |
| 2817 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2818 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2819 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2820 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2821 | // The only case a 256-bit wide vector could be used is when the struct |
| 2822 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2823 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2824 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2825 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2826 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2827 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2828 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2829 | return; |
| 2830 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2831 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2832 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2833 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2834 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2835 | return; |
| 2836 | } |
| 2837 | |
| 2838 | // Classify this field. |
| 2839 | // |
| 2840 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2841 | // exceeds a single eightbyte, each is classified |
| 2842 | // separately. Each eightbyte gets initialized to class |
| 2843 | // NO_CLASS. |
| 2844 | Class FieldLo, FieldHi; |
| 2845 | |
| 2846 | // Bit-fields require special handling, they do not force the |
| 2847 | // structure to be passed in memory even if unaligned, and |
| 2848 | // therefore they can straddle an eightbyte. |
| 2849 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2850 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2851 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2852 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2853 | |
| 2854 | uint64_t EB_Lo = Offset / 64; |
| 2855 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2856 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2857 | if (EB_Lo) { |
| 2858 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2859 | FieldLo = NoClass; |
| 2860 | FieldHi = Integer; |
| 2861 | } else { |
| 2862 | FieldLo = Integer; |
| 2863 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2864 | } |
| 2865 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2866 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2867 | Lo = merge(Lo, FieldLo); |
| 2868 | Hi = merge(Hi, FieldHi); |
| 2869 | if (Lo == Memory || Hi == Memory) |
| 2870 | break; |
| 2871 | } |
| 2872 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2873 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2874 | } |
| 2875 | } |
| 2876 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2877 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2878 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2879 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2880 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2881 | // Treat an enum type as its underlying type. |
| 2882 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2883 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2884 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2885 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2886 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2889 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2892 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2893 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2894 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2895 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2896 | if (Size <= 64 || Size > LargestVector) |
| 2897 | return true; |
| 2898 | } |
| 2899 | |
| 2900 | return false; |
| 2901 | } |
| 2902 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2903 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2904 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2905 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2906 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2907 | // |
| 2908 | // This assumption is optimistic, as there could be free registers available |
| 2909 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2910 | // the argument in the free register. This does not seem to happen currently, |
| 2911 | // but this code would be much safer if we could mark the argument with |
| 2912 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2913 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2914 | // Treat an enum type as its underlying type. |
| 2915 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2916 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2917 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2918 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2919 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2920 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2921 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2922 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2923 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2924 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2925 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2926 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2927 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2928 | |
| 2929 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2930 | // is important for good codegen. |
| 2931 | // |
| 2932 | // We do this by coercing the value into a scalar type which the backend can |
| 2933 | // handle naturally (i.e., without using byval). |
| 2934 | // |
| 2935 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2936 | // free integer registers. Doing this when there are free integer registers |
| 2937 | // would require more care, as we would have to ensure that the coerced value |
| 2938 | // did not claim the unused register. That would require either reording the |
| 2939 | // arguments to the function (so that any subsequent inreg values came first), |
| 2940 | // or only doing this optimization when there were no following arguments that |
| 2941 | // might be inreg. |
| 2942 | // |
| 2943 | // We currently expect it to be rare (particularly in well written code) for |
| 2944 | // arguments to be passed on the stack when there are still free integer |
| 2945 | // registers available (this would typically imply large structs being passed |
| 2946 | // by value), so this seems like a fair tradeoff for now. |
| 2947 | // |
| 2948 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2949 | // attributes. See PR12193. |
| 2950 | if (freeIntRegs == 0) { |
| 2951 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2952 | |
| 2953 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2954 | // type, which will end up on the stack (with alignment 8). |
| 2955 | if (Align == 8 && Size <= 64) |
| 2956 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2957 | Size)); |
| 2958 | } |
| 2959 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2960 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2963 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2964 | /// 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] | 2965 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2966 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2967 | // vectors; strip them off if present. |
| 2968 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2969 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2970 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2971 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2972 | if (isa<llvm::VectorType>(IRType) || |
| 2973 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2974 | return IRType; |
| 2975 | |
| 2976 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2977 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2978 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2979 | |
| 2980 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2981 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2982 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2985 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2986 | /// is known to either be off the end of the specified type or being in |
| 2987 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2988 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2989 | /// classification that put one of the two halves in the INTEGER class. |
| 2990 | /// |
| 2991 | /// It is conservatively correct to return false. |
| 2992 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2993 | unsigned EndBit, ASTContext &Context) { |
| 2994 | // If the bytes being queried are off the end of the type, there is no user |
| 2995 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2996 | // types that don't contain interesting padding. |
| 2997 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2998 | if (TySize <= StartBit) |
| 2999 | return true; |
| 3000 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3001 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3002 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 3003 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 3004 | |
| 3005 | // Check each element to see if the element overlaps with the queried range. |
| 3006 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3007 | // If the element is after the span we care about, then we're done.. |
| 3008 | unsigned EltOffset = i*EltSize; |
| 3009 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3010 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3011 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 3012 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3013 | EndBit-EltOffset, Context)) |
| 3014 | return false; |
| 3015 | } |
| 3016 | // If it overlaps no elements, then it is safe to process as padding. |
| 3017 | return true; |
| 3018 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3019 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3020 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3021 | const RecordDecl *RD = RT->getDecl(); |
| 3022 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3024 | // If this is a C++ record, check the bases first. |
| 3025 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3026 | for (const auto &I : CXXRD->bases()) { |
| 3027 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3028 | "Unexpected base class!"); |
| 3029 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3030 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
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 the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3033 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3034 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3035 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3036 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3037 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3038 | EndBit-BaseOffset, Context)) |
| 3039 | return false; |
| 3040 | } |
| 3041 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3042 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3043 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3044 | // this could be sped up a lot by being smarter about queried fields, |
| 3045 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3046 | // much. |
| 3047 | unsigned idx = 0; |
| 3048 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3049 | i != e; ++i, ++idx) { |
| 3050 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3051 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3052 | // If we found a field after the region we care about, then we're done. |
| 3053 | if (FieldOffset >= EndBit) break; |
| 3054 | |
| 3055 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3056 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3057 | Context)) |
| 3058 | return false; |
| 3059 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3060 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3061 | // If nothing in this record overlapped the area of interest, then we're |
| 3062 | // clean. |
| 3063 | return true; |
| 3064 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3065 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3066 | return false; |
| 3067 | } |
| 3068 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3069 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3070 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3071 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3072 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3073 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3074 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3075 | // Base case if we find a float. |
| 3076 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3077 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3078 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3079 | // 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] | 3080 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3081 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3082 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3083 | IROffset -= SL->getElementOffset(Elt); |
| 3084 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3085 | } |
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 an array, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3088 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3089 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3090 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3091 | IROffset -= IROffset/EltSize*EltSize; |
| 3092 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3093 | } |
| 3094 | |
| 3095 | return false; |
| 3096 | } |
| 3097 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3098 | |
| 3099 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3100 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3101 | llvm::Type *X86_64ABIInfo:: |
| 3102 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3103 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3104 | // 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] | 3105 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3106 | // structs that contain 3 floats. |
| 3107 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3108 | SourceOffset*8+64, getContext())) |
| 3109 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3111 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3112 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3113 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3114 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3115 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3116 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3118 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3119 | } |
| 3120 | |
| 3121 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3122 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3123 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3124 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3125 | /// 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] | 3126 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3127 | /// etc). |
| 3128 | /// |
| 3129 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3130 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3131 | /// the 8-byte value references. PrefType may be null. |
| 3132 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3133 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3134 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3135 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3136 | llvm::Type *X86_64ABIInfo:: |
| 3137 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3138 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3139 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3140 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3141 | if (IROffset == 0) { |
| 3142 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3143 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3144 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3145 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3146 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3147 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3148 | // goodness in the source type is just tail padding. This is allowed to |
| 3149 | // kick in for struct {double,int} on the int, but not on |
| 3150 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3151 | // have to do this analysis on the source type because we can't depend on |
| 3152 | // unions being lowered a specific way etc. |
| 3153 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3154 | IRType->isIntegerTy(32) || |
| 3155 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3156 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3157 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3158 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3159 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3160 | SourceOffset*8+64, getContext())) |
| 3161 | return IRType; |
| 3162 | } |
| 3163 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3164 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3165 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3166 | // 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] | 3167 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3168 | if (IROffset < SL->getSizeInBytes()) { |
| 3169 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3170 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3171 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3172 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3173 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3174 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3175 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3176 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3177 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3178 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3179 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3180 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3181 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3182 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3183 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3184 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3185 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3186 | // 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] | 3187 | unsigned TySizeInBytes = |
| 3188 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3190 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3191 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3192 | // It is always safe to classify this as an integer type up to i64 that |
| 3193 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3194 | return llvm::IntegerType::get(getVMContext(), |
| 3195 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3198 | |
| 3199 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3200 | /// be used as elements of a two register pair to pass or return, return a |
| 3201 | /// first class aggregate to represent them. For example, if the low part of |
| 3202 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3203 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3204 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3205 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3206 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3207 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3208 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3209 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3210 | // the second element at offset 8. Check for this: |
| 3211 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3212 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3213 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3214 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3215 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3216 | // To handle this, we have to increase the size of the low part so that the |
| 3217 | // second element will start at an 8 byte offset. We can't increase the size |
| 3218 | // of the second element because it might make us access off the end of the |
| 3219 | // struct. |
| 3220 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3221 | // There are usually two sorts of types the ABI generation code can produce |
| 3222 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3223 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3224 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3225 | // Promote these to a larger type. |
| 3226 | if (Lo->isFloatTy()) |
| 3227 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3228 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3229 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3230 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3231 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3232 | } |
| 3233 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3234 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3235 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3236 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3237 | // Verify that the second element is at an 8-byte offset. |
| 3238 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3239 | "Invalid x86-64 argument pair!"); |
| 3240 | return Result; |
| 3241 | } |
| 3242 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3243 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3244 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3245 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3246 | // classification algorithm. |
| 3247 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3248 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Check some invariants. |
| 3251 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3252 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3253 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3254 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3255 | switch (Lo) { |
| 3256 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3257 | if (Hi == NoClass) |
| 3258 | return ABIArgInfo::getIgnore(); |
| 3259 | // If the low part is just padding, it takes no register, leave ResType |
| 3260 | // null. |
| 3261 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3262 | "Unknown missing lo part"); |
| 3263 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3264 | |
| 3265 | case SSEUp: |
| 3266 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3267 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3268 | |
| 3269 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3270 | // hidden argument. |
| 3271 | case Memory: |
| 3272 | return getIndirectReturnResult(RetTy); |
| 3273 | |
| 3274 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3275 | // available register of the sequence %rax, %rdx is used. |
| 3276 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3277 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3278 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3279 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3280 | // so that the parameter gets the right LLVM IR attributes. |
| 3281 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3282 | // Treat an enum type as its underlying type. |
| 3283 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3284 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3285 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3286 | if (RetTy->isIntegralOrEnumerationType() && |
| 3287 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3288 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3289 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3290 | break; |
| 3291 | |
| 3292 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3293 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3294 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3295 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3296 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3297 | |
| 3298 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3299 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3300 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3301 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3302 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3303 | |
| 3304 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3305 | // part of the value is returned in %st0 and the imaginary part in |
| 3306 | // %st1. |
| 3307 | case ComplexX87: |
| 3308 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3309 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3310 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3311 | break; |
| 3312 | } |
| 3313 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3314 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3315 | switch (Hi) { |
| 3316 | // Memory was handled previously and X87 should |
| 3317 | // never occur as a hi class. |
| 3318 | case Memory: |
| 3319 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3320 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3321 | |
| 3322 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3323 | case NoClass: |
| 3324 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3325 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3326 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3327 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3328 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3329 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3330 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3331 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3332 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3333 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3334 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3335 | break; |
| 3336 | |
| 3337 | // 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] | 3338 | // is passed in the next available eightbyte chunk if the last used |
| 3339 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3340 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3341 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3342 | case SSEUp: |
| 3343 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3344 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3345 | break; |
| 3346 | |
| 3347 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3348 | // returned together with the previous X87 value in %st0. |
| 3349 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3350 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3351 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3352 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3353 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3354 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3355 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3356 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3357 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3358 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3359 | break; |
| 3360 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3361 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3362 | // 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] | 3363 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3364 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3365 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3366 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3367 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3368 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3371 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3372 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3373 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3374 | const |
| 3375 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3376 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3377 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3378 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3379 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3380 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3381 | // Check some invariants. |
| 3382 | // FIXME: Enforce these by construction. |
| 3383 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3384 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3385 | |
| 3386 | neededInt = 0; |
| 3387 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3388 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3389 | switch (Lo) { |
| 3390 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3391 | if (Hi == NoClass) |
| 3392 | return ABIArgInfo::getIgnore(); |
| 3393 | // If the low part is just padding, it takes no register, leave ResType |
| 3394 | // null. |
| 3395 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3396 | "Unknown missing lo part"); |
| 3397 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3398 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3399 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3400 | // on the stack. |
| 3401 | case Memory: |
| 3402 | |
| 3403 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3404 | // COMPLEX_X87, it is passed in memory. |
| 3405 | case X87: |
| 3406 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3407 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3408 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3409 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3410 | |
| 3411 | case SSEUp: |
| 3412 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3413 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3414 | |
| 3415 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3416 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3417 | // and %r9 is used. |
| 3418 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3419 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3420 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3421 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3422 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3423 | |
| 3424 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3425 | // so that the parameter gets the right LLVM IR attributes. |
| 3426 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3427 | // Treat an enum type as its underlying type. |
| 3428 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3429 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3430 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3431 | if (Ty->isIntegralOrEnumerationType() && |
| 3432 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3433 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3434 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3435 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3436 | break; |
| 3437 | |
| 3438 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3439 | // available SSE register is used, the registers are taken in the |
| 3440 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3441 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3442 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3443 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3444 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3445 | break; |
| 3446 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3447 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3448 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3449 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3450 | switch (Hi) { |
| 3451 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3452 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3453 | // which is passed in memory. |
| 3454 | case Memory: |
| 3455 | case X87: |
| 3456 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3457 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3458 | |
| 3459 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3460 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3461 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3462 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3463 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3464 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3465 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3466 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3467 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3468 | break; |
| 3469 | |
| 3470 | // X87Up generally doesn't occur here (long double is passed in |
| 3471 | // memory), except in situations involving unions. |
| 3472 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3473 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3474 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3475 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3476 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3477 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3478 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3479 | ++neededSSE; |
| 3480 | break; |
| 3481 | |
| 3482 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3483 | // 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] | 3484 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3485 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3486 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3487 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3488 | break; |
| 3489 | } |
| 3490 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3491 | // If a high part was specified, merge it together with the low part. It is |
| 3492 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3493 | // first class struct aggregate with the high and low part: {low, high} |
| 3494 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3495 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3496 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3497 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3500 | ABIArgInfo |
| 3501 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3502 | unsigned &NeededSSE) const { |
| 3503 | auto RT = Ty->getAs<RecordType>(); |
| 3504 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3505 | |
| 3506 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3507 | return getIndirectReturnResult(Ty); |
| 3508 | |
| 3509 | // Sum up bases |
| 3510 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3511 | if (CXXRD->isDynamicClass()) { |
| 3512 | NeededInt = NeededSSE = 0; |
| 3513 | return getIndirectReturnResult(Ty); |
| 3514 | } |
| 3515 | |
| 3516 | for (const auto &I : CXXRD->bases()) |
| 3517 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3518 | .isIndirect()) { |
| 3519 | NeededInt = NeededSSE = 0; |
| 3520 | return getIndirectReturnResult(Ty); |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | // Sum up members |
| 3525 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3526 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3527 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3528 | .isIndirect()) { |
| 3529 | NeededInt = NeededSSE = 0; |
| 3530 | return getIndirectReturnResult(Ty); |
| 3531 | } |
| 3532 | } else { |
| 3533 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3534 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3535 | LocalNeededSSE, true) |
| 3536 | .isIndirect()) { |
| 3537 | NeededInt = NeededSSE = 0; |
| 3538 | return getIndirectReturnResult(Ty); |
| 3539 | } |
| 3540 | NeededInt += LocalNeededInt; |
| 3541 | NeededSSE += LocalNeededSSE; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | return ABIArgInfo::getDirect(); |
| 3546 | } |
| 3547 | |
| 3548 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3549 | unsigned &NeededInt, |
| 3550 | unsigned &NeededSSE) const { |
| 3551 | |
| 3552 | NeededInt = 0; |
| 3553 | NeededSSE = 0; |
| 3554 | |
| 3555 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3556 | } |
| 3557 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3558 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3559 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3560 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3561 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3562 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3563 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3564 | if (CallingConv == llvm::CallingConv::Win64) { |
| 3565 | WinX86_64ABIInfo Win64ABIInfo(CGT); |
| 3566 | Win64ABIInfo.computeInfo(FI); |
| 3567 | return; |
| 3568 | } |
| 3569 | |
| 3570 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3571 | |
| 3572 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3573 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3574 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3575 | unsigned NeededInt, NeededSSE; |
| 3576 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 3577 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3578 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3579 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3580 | FI.getReturnInfo() = |
| 3581 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3582 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3583 | FreeIntRegs -= NeededInt; |
| 3584 | FreeSSERegs -= NeededSSE; |
| 3585 | } else { |
| 3586 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3587 | } |
| 3588 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3589 | // Complex Long Double Type is passed in Memory when Regcall |
| 3590 | // calling convention is used. |
| 3591 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3592 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3593 | getContext().LongDoubleTy) |
| 3594 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3595 | } else |
| 3596 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3597 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3598 | |
| 3599 | // If the return value is indirect, then the hidden argument is consuming one |
| 3600 | // integer register. |
| 3601 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3602 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3603 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3604 | // The chain argument effectively gives us another free register. |
| 3605 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3606 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3607 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3608 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3609 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3610 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3611 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3612 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3613 | it != ie; ++it, ++ArgNo) { |
| 3614 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3615 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3616 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3617 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3618 | else |
| 3619 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3620 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3621 | |
| 3622 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3623 | // eightbyte of an argument, the whole argument is passed on the |
| 3624 | // stack. If registers have already been assigned for some |
| 3625 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3626 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3627 | FreeIntRegs -= NeededInt; |
| 3628 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3629 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3630 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3635 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3636 | Address VAListAddr, QualType Ty) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3637 | Address overflow_arg_area_p = |
| 3638 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3639 | llvm::Value *overflow_arg_area = |
| 3640 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3641 | |
| 3642 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3643 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3644 | // It isn't stated explicitly in the standard, but in practice we use |
| 3645 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3646 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3647 | if (Align > CharUnits::fromQuantity(8)) { |
| 3648 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3649 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | // 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] | 3653 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3654 | llvm::Value *Res = |
| 3655 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3656 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3657 | |
| 3658 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3659 | // l->overflow_arg_area + sizeof(type). |
| 3660 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3661 | // an 8 byte boundary. |
| 3662 | |
| 3663 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3664 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3665 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3666 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3667 | "overflow_arg_area.next"); |
| 3668 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3669 | |
| 3670 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3671 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3674 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3675 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3676 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3677 | // struct { |
| 3678 | // i32 gp_offset; |
| 3679 | // i32 fp_offset; |
| 3680 | // i8* overflow_arg_area; |
| 3681 | // i8* reg_save_area; |
| 3682 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3683 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3684 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3685 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3686 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3687 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3688 | |
| 3689 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3690 | // in the registers. If not go to step 7. |
| 3691 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3692 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3693 | |
| 3694 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3695 | // general purpose registers needed to pass type and num_fp to hold |
| 3696 | // the number of floating point registers needed. |
| 3697 | |
| 3698 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3699 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3700 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3701 | // |
| 3702 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3703 | // register save space). |
| 3704 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3705 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3706 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3707 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3708 | if (neededInt) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3709 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3710 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3711 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3712 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | if (neededSSE) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3716 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3717 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3718 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3719 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3720 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3721 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3722 | } |
| 3723 | |
| 3724 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3725 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3726 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3727 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3728 | |
| 3729 | // Emit code to load the value if it was passed in registers. |
| 3730 | |
| 3731 | CGF.EmitBlock(InRegBlock); |
| 3732 | |
| 3733 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3734 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3735 | // copying to a temporary location in case the parameter is passed |
| 3736 | // in different register classes or requires an alignment greater |
| 3737 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3738 | // |
| 3739 | // FIXME: This really results in shameful code when we end up needing to |
| 3740 | // collect arguments from different places; often what should result in a |
| 3741 | // simple assembling of a structure from scattered addresses has many more |
| 3742 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3743 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3744 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3745 | CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3746 | |
| 3747 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3748 | if (neededInt && neededSSE) { |
| 3749 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3750 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3751 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3752 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3753 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3754 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3755 | llvm::Type *TyLo = ST->getElementType(0); |
| 3756 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3757 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3758 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3759 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3760 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3761 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3762 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3763 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3764 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3765 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3766 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3767 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3768 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3769 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3770 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3771 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3772 | |
| 3773 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3774 | V = CGF.Builder.CreateAlignedLoad( |
| 3775 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3776 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3777 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3778 | |
| 3779 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3780 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3781 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3782 | CharUnits::fromQuantity(8)); |
| 3783 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3784 | |
| 3785 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3786 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3787 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3788 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3789 | CharUnits TyAlign = SizeAlign.second; |
| 3790 | |
| 3791 | // Copy into a temporary if the type is more aligned than the |
| 3792 | // register save area. |
| 3793 | if (TyAlign.getQuantity() > 8) { |
| 3794 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3795 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3796 | RegAddr = Tmp; |
| 3797 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3798 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3799 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3800 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3801 | CharUnits::fromQuantity(16)); |
| 3802 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3803 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3804 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3805 | // SSE registers are spaced 16 bytes apart in the register save |
| 3806 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3807 | // The ABI isn't explicit about this, but it seems reasonable |
| 3808 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3809 | // naturally 16-byte aligned and the prologue is expected to store |
| 3810 | // all the SSE registers to the RSA. |
| 3811 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3812 | CharUnits::fromQuantity(16)); |
| 3813 | Address RegAddrHi = |
| 3814 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3815 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3816 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3817 | ? AI.getCoerceToType() |
| 3818 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3819 | llvm::Value *V; |
| 3820 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3821 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3822 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3823 | RegAddrLo, ST->getStructElementType(0))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3824 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3825 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3826 | RegAddrHi, ST->getStructElementType(1))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3827 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3828 | |
| 3829 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3833 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3834 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3835 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3836 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3837 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3838 | gp_offset_p); |
| 3839 | } |
| 3840 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3841 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3842 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3843 | fp_offset_p); |
| 3844 | } |
| 3845 | CGF.EmitBranch(ContBlock); |
| 3846 | |
| 3847 | // Emit code to load the value if it was passed in memory. |
| 3848 | |
| 3849 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3850 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3851 | |
| 3852 | // Return the appropriate result. |
| 3853 | |
| 3854 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3855 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3856 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3857 | return ResAddr; |
| 3858 | } |
| 3859 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3860 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3861 | QualType Ty) const { |
| 3862 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3863 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3864 | CharUnits::fromQuantity(8), |
| 3865 | /*allowHigherAlign*/ false); |
| 3866 | } |
| 3867 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3868 | ABIArgInfo |
| 3869 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3870 | const ABIArgInfo ¤t) const { |
| 3871 | // Assumes vectorCall calling convention. |
| 3872 | const Type *Base = nullptr; |
| 3873 | uint64_t NumElts = 0; |
| 3874 | |
| 3875 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3876 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3877 | FreeSSERegs -= NumElts; |
| 3878 | return getDirectX86Hva(); |
| 3879 | } |
| 3880 | return current; |
| 3881 | } |
| 3882 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3883 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3884 | bool IsReturnType, bool IsVectorCall, |
| 3885 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3886 | |
| 3887 | if (Ty->isVoidType()) |
| 3888 | return ABIArgInfo::getIgnore(); |
| 3889 | |
| 3890 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3891 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3892 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3893 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3894 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3895 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3896 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3897 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3898 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3899 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3900 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3901 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
| 3904 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3905 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3906 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3907 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3908 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3909 | const Type *Base = nullptr; |
| 3910 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3911 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3912 | // other targets. |
| 3913 | if ((IsVectorCall || IsRegCall) && |
| 3914 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3915 | if (IsRegCall) { |
| 3916 | if (FreeSSERegs >= NumElts) { |
| 3917 | FreeSSERegs -= NumElts; |
| 3918 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3919 | return ABIArgInfo::getDirect(); |
| 3920 | return ABIArgInfo::getExpand(); |
| 3921 | } |
| 3922 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3923 | } else if (IsVectorCall) { |
| 3924 | if (FreeSSERegs >= NumElts && |
| 3925 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3926 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3927 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3928 | } else if (IsReturnType) { |
| 3929 | return ABIArgInfo::getExpand(); |
| 3930 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3931 | // HVAs are delayed and reclassified in the 2nd step. |
| 3932 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3933 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3934 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3937 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3938 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3939 | // directly. |
| 3940 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3941 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3942 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3945 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3946 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3947 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3948 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3949 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3950 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3951 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3952 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Reid Kleckner | 08f64e9 | 2018-10-31 17:43:55 +0000 | [diff] [blame] | 3955 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3956 | switch (BT->getKind()) { |
| 3957 | case BuiltinType::Bool: |
| 3958 | // Bool type is always extended to the ABI, other builtin types are not |
| 3959 | // extended. |
| 3960 | return ABIArgInfo::getExtend(Ty); |
| 3961 | |
| 3962 | case BuiltinType::LongDouble: |
| 3963 | // Mingw64 GCC uses the old 80 bit extended precision floating point |
| 3964 | // unit. It passes them indirectly through memory. |
| 3965 | if (IsMingw64) { |
| 3966 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3967 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
| 3968 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3969 | } |
| 3970 | break; |
| 3971 | |
| 3972 | case BuiltinType::Int128: |
| 3973 | case BuiltinType::UInt128: |
| 3974 | // If it's a parameter type, the normal ABI rule is that arguments larger |
| 3975 | // than 8 bytes are passed indirectly. GCC follows it. We follow it too, |
| 3976 | // even though it isn't particularly efficient. |
| 3977 | if (!IsReturnType) |
| 3978 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3979 | |
| 3980 | // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. |
| 3981 | // Clang matches them for compatibility. |
| 3982 | return ABIArgInfo::getDirect( |
| 3983 | llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2)); |
| 3984 | |
| 3985 | default: |
| 3986 | break; |
| 3987 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3990 | return ABIArgInfo::getDirect(); |
| 3991 | } |
| 3992 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3993 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3994 | unsigned FreeSSERegs, |
| 3995 | bool IsVectorCall, |
| 3996 | bool IsRegCall) const { |
| 3997 | unsigned Count = 0; |
| 3998 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 3999 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 4000 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4001 | if (Count < VectorcallMaxParamNumAsReg) |
| 4002 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4003 | else { |
| 4004 | // Since these cannot be passed in registers, pretend no registers |
| 4005 | // are left. |
| 4006 | unsigned ZeroSSERegsAvail = 0; |
| 4007 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 4008 | IsVectorCall, IsRegCall); |
| 4009 | } |
| 4010 | ++Count; |
| 4011 | } |
| 4012 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4013 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4014 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4018 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4019 | bool IsVectorCall = |
| 4020 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4021 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 4022 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4023 | unsigned FreeSSERegs = 0; |
| 4024 | if (IsVectorCall) { |
| 4025 | // We can use up to 4 SSE return registers with vectorcall. |
| 4026 | FreeSSERegs = 4; |
| 4027 | } else if (IsRegCall) { |
| 4028 | // RegCall gives us 16 SSE registers. |
| 4029 | FreeSSERegs = 16; |
| 4030 | } |
| 4031 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4032 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4033 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 4034 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4035 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4036 | if (IsVectorCall) { |
| 4037 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4038 | FreeSSERegs = 6; |
| 4039 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4040 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4041 | FreeSSERegs = 16; |
| 4042 | } |
| 4043 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4044 | if (IsVectorCall) { |
| 4045 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4046 | } else { |
| 4047 | for (auto &I : FI.arguments()) |
| 4048 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4049 | } |
| 4050 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4051 | } |
| 4052 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4053 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4054 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4055 | |
| 4056 | bool IsIndirect = false; |
| 4057 | |
| 4058 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4059 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4060 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4061 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4062 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4063 | } |
| 4064 | |
| 4065 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4066 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4067 | CharUnits::fromQuantity(8), |
| 4068 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4069 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4070 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4071 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4072 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4073 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4074 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4075 | bool IsSoftFloatABI; |
| 4076 | |
| 4077 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4078 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4079 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4080 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4081 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4082 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4083 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4084 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4085 | }; |
| 4086 | |
| 4087 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4088 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4089 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4090 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4091 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4092 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4093 | // This is recovered from gcc output. |
| 4094 | return 1; // r1 is the dedicated stack pointer |
| 4095 | } |
| 4096 | |
| 4097 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4098 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4099 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4100 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4101 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4102 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4103 | // Complex types are passed just like their elements |
| 4104 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4105 | Ty = CTy->getElementType(); |
| 4106 | |
| 4107 | if (Ty->isVectorType()) |
| 4108 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4109 | : 4); |
| 4110 | |
| 4111 | // For single-element float/vector structs, we consider the whole type |
| 4112 | // to have the same alignment requirements as its single element. |
| 4113 | const Type *AlignTy = nullptr; |
| 4114 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4115 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4116 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4117 | (BT && BT->isFloatingPoint())) |
| 4118 | AlignTy = EltType; |
| 4119 | } |
| 4120 | |
| 4121 | if (AlignTy) |
| 4122 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4123 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4124 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4125 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4126 | // TODO: this implementation is now likely redundant with |
| 4127 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4128 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4129 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4130 | if (getTarget().getTriple().isOSDarwin()) { |
| 4131 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4132 | TI.second = getParamTypeAlignment(Ty); |
| 4133 | |
| 4134 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4135 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4136 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4137 | /*AllowHigherAlign=*/true); |
| 4138 | } |
| 4139 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4140 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4141 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4142 | // TODO: Implement this. For now ignore. |
| 4143 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4144 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4147 | // struct __va_list_tag { |
| 4148 | // unsigned char gpr; |
| 4149 | // unsigned char fpr; |
| 4150 | // unsigned short reserved; |
| 4151 | // void *overflow_arg_area; |
| 4152 | // void *reg_save_area; |
| 4153 | // }; |
| 4154 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4155 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4156 | bool isInt = |
| 4157 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4158 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4159 | |
| 4160 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4161 | // with the argument-lowering code. |
| 4162 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4163 | |
| 4164 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4165 | |
| 4166 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4167 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4168 | if (isInt || IsSoftFloatABI) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4169 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4170 | } else { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4171 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4172 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4173 | |
| 4174 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4175 | |
| 4176 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4177 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4178 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4179 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4180 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4181 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4182 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4183 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4184 | |
| 4185 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4186 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4187 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4188 | |
| 4189 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4190 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4191 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4192 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4193 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4194 | // Case 1: consume registers. |
| 4195 | Address RegAddr = Address::invalid(); |
| 4196 | { |
| 4197 | CGF.EmitBlock(UsingRegs); |
| 4198 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4199 | Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4200 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4201 | CharUnits::fromQuantity(8)); |
| 4202 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4203 | |
| 4204 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4205 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4206 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4207 | CharUnits::fromQuantity(32)); |
| 4208 | } |
| 4209 | |
| 4210 | // Get the address of the saved value by scaling the number of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4211 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4212 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4213 | llvm::Value *RegOffset = |
| 4214 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4215 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4216 | RegAddr.getPointer(), RegOffset), |
| 4217 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4218 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4219 | |
| 4220 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4221 | NumRegs = |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4222 | Builder.CreateAdd(NumRegs, |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4223 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4224 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4225 | |
| 4226 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4227 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4228 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4229 | // Case 2: consume space in the overflow area. |
| 4230 | Address MemAddr = Address::invalid(); |
| 4231 | { |
| 4232 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4233 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4234 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4235 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4236 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4237 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4238 | |
| 4239 | CharUnits Size; |
| 4240 | if (!isIndirect) { |
| 4241 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4242 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4243 | } else { |
| 4244 | Size = CGF.getPointerSize(); |
| 4245 | } |
| 4246 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4247 | Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4248 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4249 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4250 | // Round up address of argument to alignment |
| 4251 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4252 | if (Align > OverflowAreaAlign) { |
| 4253 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4254 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4255 | Align); |
| 4256 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4257 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4258 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4259 | |
| 4260 | // Increase the overflow area. |
| 4261 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4262 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4263 | CGF.EmitBranch(Cont); |
| 4264 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4265 | |
| 4266 | CGF.EmitBlock(Cont); |
| 4267 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4268 | // Merge the cases with a phi. |
| 4269 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4270 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4271 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4272 | // Load the pointer if the argument was passed indirectly. |
| 4273 | if (isIndirect) { |
| 4274 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4275 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | return Result; |
| 4279 | } |
| 4280 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4281 | bool |
| 4282 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4283 | llvm::Value *Address) const { |
| 4284 | // This is calculated from the LLVM and GCC tables and verified |
| 4285 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4286 | |
| 4287 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4288 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4289 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4290 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4291 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4292 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4293 | |
| 4294 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4295 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4296 | |
| 4297 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4298 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4299 | |
| 4300 | // 64-76 are various 4-byte special-purpose registers: |
| 4301 | // 64: mq |
| 4302 | // 65: lr |
| 4303 | // 66: ctr |
| 4304 | // 67: ap |
| 4305 | // 68-75 cr0-7 |
| 4306 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4307 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4308 | |
| 4309 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4310 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4311 | |
| 4312 | // 109: vrsave |
| 4313 | // 110: vscr |
| 4314 | // 111: spe_acc |
| 4315 | // 112: spefscr |
| 4316 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4317 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4318 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4319 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4322 | // PowerPC-64 |
| 4323 | |
| 4324 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4325 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4326 | class PPC64_SVR4_ABIInfo : public SwiftABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4327 | public: |
| 4328 | enum ABIKind { |
| 4329 | ELFv1 = 0, |
| 4330 | ELFv2 |
| 4331 | }; |
| 4332 | |
| 4333 | private: |
| 4334 | static const unsigned GPRBits = 64; |
| 4335 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4336 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4337 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4338 | |
| 4339 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4340 | // will be passed in a QPX register. |
| 4341 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4342 | if (!HasQPX) |
| 4343 | return false; |
| 4344 | |
| 4345 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4346 | unsigned NumElements = VT->getNumElements(); |
| 4347 | if (NumElements == 1) |
| 4348 | return false; |
| 4349 | |
| 4350 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4351 | if (getContext().getTypeSize(Ty) <= 256) |
| 4352 | return true; |
| 4353 | } else if (VT->getElementType()-> |
| 4354 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4355 | if (getContext().getTypeSize(Ty) <= 128) |
| 4356 | return true; |
| 4357 | } |
| 4358 | } |
| 4359 | |
| 4360 | return false; |
| 4361 | } |
| 4362 | |
| 4363 | bool IsQPXVectorTy(QualType Ty) const { |
| 4364 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4365 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4366 | |
| 4367 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4368 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4369 | bool SoftFloatABI) |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4370 | : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4371 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4372 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4373 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4374 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4375 | |
| 4376 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4377 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4378 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4379 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4380 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4381 | uint64_t Members) const override; |
| 4382 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4383 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4384 | // Example: For aggregate arguments that fit in a register, we could |
| 4385 | // use getDirectInReg (as is done below for structs containing a single |
| 4386 | // floating-point value) to avoid pushing them to memory on function |
| 4387 | // entry. This would require changing the logic in PPCISelLowering |
| 4388 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4389 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4390 | if (!getCXXABI().classifyReturnType(FI)) |
| 4391 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4392 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4393 | // We rely on the default argument classification for the most part. |
| 4394 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4395 | // 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] | 4396 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4397 | if (T) { |
| 4398 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4399 | if (IsQPXVectorTy(T) || |
| 4400 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4401 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4402 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4403 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4404 | continue; |
| 4405 | } |
| 4406 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4407 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4408 | } |
| 4409 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4410 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4411 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4412 | QualType Ty) const override; |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4413 | |
| 4414 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 4415 | bool asReturnValue) const override { |
| 4416 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4417 | } |
| 4418 | |
| 4419 | bool isSwiftErrorInRegister() const override { |
| 4420 | return false; |
| 4421 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4422 | }; |
| 4423 | |
| 4424 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4425 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4426 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4427 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4428 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4429 | bool SoftFloatABI) |
| 4430 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4431 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4432 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4433 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4434 | // This is recovered from gcc output. |
| 4435 | return 1; // r1 is the dedicated stack pointer |
| 4436 | } |
| 4437 | |
| 4438 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4439 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4440 | }; |
| 4441 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4442 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4443 | public: |
| 4444 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4445 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4446 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4447 | // This is recovered from gcc output. |
| 4448 | return 1; // r1 is the dedicated stack pointer |
| 4449 | } |
| 4450 | |
| 4451 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4452 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4453 | }; |
| 4454 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4455 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4456 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4457 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4458 | // extended to 64 bits. |
| 4459 | bool |
| 4460 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4461 | // Treat an enum type as its underlying type. |
| 4462 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4463 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4464 | |
| 4465 | // Promotable integer types are required to be promoted by the ABI. |
| 4466 | if (Ty->isPromotableIntegerType()) |
| 4467 | return true; |
| 4468 | |
| 4469 | // In addition to the usual promotable integer types, we also need to |
| 4470 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4471 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4472 | switch (BT->getKind()) { |
| 4473 | case BuiltinType::Int: |
| 4474 | case BuiltinType::UInt: |
| 4475 | return true; |
| 4476 | default: |
| 4477 | break; |
| 4478 | } |
| 4479 | |
| 4480 | return false; |
| 4481 | } |
| 4482 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4483 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4484 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4485 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4486 | // Complex types are passed just like their elements. |
| 4487 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4488 | Ty = CTy->getElementType(); |
| 4489 | |
| 4490 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4491 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4492 | if (IsQPXVectorTy(Ty)) { |
| 4493 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4494 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4495 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4496 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4497 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4498 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4499 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4500 | |
| 4501 | // For single-element float/vector structs, we consider the whole type |
| 4502 | // to have the same alignment requirements as its single element. |
| 4503 | const Type *AlignAsType = nullptr; |
| 4504 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4505 | if (EltType) { |
| 4506 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4507 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4508 | getContext().getTypeSize(EltType) == 128) || |
| 4509 | (BT && BT->isFloatingPoint())) |
| 4510 | AlignAsType = EltType; |
| 4511 | } |
| 4512 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4513 | // Likewise for ELFv2 homogeneous aggregates. |
| 4514 | const Type *Base = nullptr; |
| 4515 | uint64_t Members = 0; |
| 4516 | if (!AlignAsType && Kind == ELFv2 && |
| 4517 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4518 | AlignAsType = Base; |
| 4519 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4520 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4521 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4522 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4523 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4524 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4525 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4526 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4527 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4528 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4529 | |
| 4530 | // Otherwise, we only need alignment for any aggregate type that |
| 4531 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4532 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4533 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4534 | return CharUnits::fromQuantity(32); |
| 4535 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4536 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4537 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4538 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4541 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4542 | /// aggregate. Base is set to the base element type, and Members is set |
| 4543 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4544 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4545 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4546 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4547 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4548 | if (NElements == 0) |
| 4549 | return false; |
| 4550 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4551 | return false; |
| 4552 | Members *= NElements; |
| 4553 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4554 | const RecordDecl *RD = RT->getDecl(); |
| 4555 | if (RD->hasFlexibleArrayMember()) |
| 4556 | return false; |
| 4557 | |
| 4558 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4559 | |
| 4560 | // If this is a C++ record, check the bases first. |
| 4561 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4562 | for (const auto &I : CXXRD->bases()) { |
| 4563 | // Ignore empty records. |
| 4564 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4565 | continue; |
| 4566 | |
| 4567 | uint64_t FldMembers; |
| 4568 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4569 | return false; |
| 4570 | |
| 4571 | Members += FldMembers; |
| 4572 | } |
| 4573 | } |
| 4574 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4575 | for (const auto *FD : RD->fields()) { |
| 4576 | // Ignore (non-zero arrays of) empty records. |
| 4577 | QualType FT = FD->getType(); |
| 4578 | while (const ConstantArrayType *AT = |
| 4579 | getContext().getAsConstantArrayType(FT)) { |
| 4580 | if (AT->getSize().getZExtValue() == 0) |
| 4581 | return false; |
| 4582 | FT = AT->getElementType(); |
| 4583 | } |
| 4584 | if (isEmptyRecord(getContext(), FT, true)) |
| 4585 | continue; |
| 4586 | |
| 4587 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4588 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4589 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4590 | continue; |
| 4591 | |
| 4592 | uint64_t FldMembers; |
| 4593 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4594 | return false; |
| 4595 | |
| 4596 | Members = (RD->isUnion() ? |
| 4597 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4598 | } |
| 4599 | |
| 4600 | if (!Base) |
| 4601 | return false; |
| 4602 | |
| 4603 | // Ensure there is no padding. |
| 4604 | if (getContext().getTypeSize(Base) * Members != |
| 4605 | getContext().getTypeSize(Ty)) |
| 4606 | return false; |
| 4607 | } else { |
| 4608 | Members = 1; |
| 4609 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4610 | Members = 2; |
| 4611 | Ty = CT->getElementType(); |
| 4612 | } |
| 4613 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4614 | // Most ABIs only support float, double, and some vector type widths. |
| 4615 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4616 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4617 | |
| 4618 | // The base type must be the same for all members. Types that |
| 4619 | // agree in both total size and mode (float vs. vector) are |
| 4620 | // treated as being equivalent here. |
| 4621 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4622 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4623 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4624 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4625 | // so make sure to widen it explicitly. |
| 4626 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4627 | QualType EltTy = VT->getElementType(); |
| 4628 | unsigned NumElements = |
| 4629 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4630 | Base = getContext() |
| 4631 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4632 | .getTypePtr(); |
| 4633 | } |
| 4634 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4635 | |
| 4636 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4637 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4638 | return false; |
| 4639 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4640 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4641 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4642 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4643 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4644 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4645 | // double, long double, or 128-bit vectors. |
| 4646 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4647 | if (BT->getKind() == BuiltinType::Float || |
| 4648 | BT->getKind() == BuiltinType::Double || |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4649 | BT->getKind() == BuiltinType::LongDouble || |
| 4650 | (getContext().getTargetInfo().hasFloat128Type() && |
| 4651 | (BT->getKind() == BuiltinType::Float128))) { |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4652 | if (IsSoftFloatABI) |
| 4653 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4654 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4655 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4656 | } |
| 4657 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4658 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4659 | return true; |
| 4660 | } |
| 4661 | return false; |
| 4662 | } |
| 4663 | |
| 4664 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4665 | const Type *Base, uint64_t Members) const { |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4666 | // Vector and fp128 types require one register, other floating point types |
| 4667 | // require one or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4668 | uint32_t NumRegs = |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4669 | ((getContext().getTargetInfo().hasFloat128Type() && |
| 4670 | Base->isFloat128Type()) || |
| 4671 | Base->isVectorType()) ? 1 |
| 4672 | : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4673 | |
| 4674 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4675 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4678 | ABIArgInfo |
| 4679 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4680 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4681 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4682 | if (Ty->isAnyComplexType()) |
| 4683 | return ABIArgInfo::getDirect(); |
| 4684 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4685 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4686 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4687 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4688 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4689 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4690 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4691 | else if (Size < 128) { |
| 4692 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4693 | return ABIArgInfo::getDirect(CoerceTy); |
| 4694 | } |
| 4695 | } |
| 4696 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4697 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4698 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4699 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4700 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4701 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4702 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4703 | |
| 4704 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4705 | const Type *Base = nullptr; |
| 4706 | uint64_t Members = 0; |
| 4707 | if (Kind == ELFv2 && |
| 4708 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4709 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4710 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4711 | return ABIArgInfo::getDirect(CoerceTy); |
| 4712 | } |
| 4713 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4714 | // If an aggregate may end up fully in registers, we do not |
| 4715 | // use the ByVal method, but pass the aggregate as array. |
| 4716 | // This is usually beneficial since we avoid forcing the |
| 4717 | // back-end to store the argument to memory. |
| 4718 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4719 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4720 | llvm::Type *CoerceTy; |
| 4721 | |
| 4722 | // Types up to 8 bytes are passed as integer type (which will be |
| 4723 | // properly aligned in the argument save area doubleword). |
| 4724 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4725 | CoerceTy = |
| 4726 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4727 | // Larger types are passed as arrays, with the base type selected |
| 4728 | // according to the required alignment in the save area. |
| 4729 | else { |
| 4730 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4731 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4732 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4733 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4734 | } |
| 4735 | |
| 4736 | return ABIArgInfo::getDirect(CoerceTy); |
| 4737 | } |
| 4738 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4739 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4740 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4741 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4742 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4745 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4746 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | ABIArgInfo |
| 4750 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4751 | if (RetTy->isVoidType()) |
| 4752 | return ABIArgInfo::getIgnore(); |
| 4753 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4754 | if (RetTy->isAnyComplexType()) |
| 4755 | return ABIArgInfo::getDirect(); |
| 4756 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4757 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4758 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4759 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4760 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4761 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4762 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4763 | else if (Size < 128) { |
| 4764 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4765 | return ABIArgInfo::getDirect(CoerceTy); |
| 4766 | } |
| 4767 | } |
| 4768 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4769 | if (isAggregateTypeForABI(RetTy)) { |
| 4770 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4771 | const Type *Base = nullptr; |
| 4772 | uint64_t Members = 0; |
| 4773 | if (Kind == ELFv2 && |
| 4774 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4775 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4776 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4777 | return ABIArgInfo::getDirect(CoerceTy); |
| 4778 | } |
| 4779 | |
| 4780 | // ELFv2 small aggregates are returned in up to two registers. |
| 4781 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4782 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4783 | if (Bits == 0) |
| 4784 | return ABIArgInfo::getIgnore(); |
| 4785 | |
| 4786 | llvm::Type *CoerceTy; |
| 4787 | if (Bits > GPRBits) { |
| 4788 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4789 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4790 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4791 | CoerceTy = |
| 4792 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4793 | return ABIArgInfo::getDirect(CoerceTy); |
| 4794 | } |
| 4795 | |
| 4796 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4797 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4798 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4799 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4800 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4801 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4804 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4805 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4806 | QualType Ty) const { |
| 4807 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4808 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4809 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4810 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4811 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4812 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4813 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4814 | // in separate doublewords. However, Clang expects us to produce a |
| 4815 | // pointer to a structure with the two parts packed tightly. So generate |
| 4816 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4817 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4818 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4819 | CharUnits EltSize = TypeInfo.first / 2; |
| 4820 | if (EltSize < SlotSize) { |
| 4821 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4822 | SlotSize * 2, SlotSize, |
| 4823 | SlotSize, /*AllowHigher*/ true); |
| 4824 | |
| 4825 | Address RealAddr = Addr; |
| 4826 | Address ImagAddr = RealAddr; |
| 4827 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4828 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4829 | SlotSize - EltSize); |
| 4830 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4831 | 2 * SlotSize - EltSize); |
| 4832 | } else { |
| 4833 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4834 | } |
| 4835 | |
| 4836 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4837 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4838 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4839 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4840 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4841 | |
| 4842 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4843 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4844 | /*init*/ true); |
| 4845 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4846 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4849 | // Otherwise, just use the general rule. |
| 4850 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4851 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | static bool |
| 4855 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4856 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4857 | // This is calculated from the LLVM and GCC tables and verified |
| 4858 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4859 | |
| 4860 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4861 | |
| 4862 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4863 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4864 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4865 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4866 | |
| 4867 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4868 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4869 | |
| 4870 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4871 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4872 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4873 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4874 | // 64: mq |
| 4875 | // 65: lr |
| 4876 | // 66: ctr |
| 4877 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4878 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4879 | |
| 4880 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4881 | // 68-75 cr0-7 |
| 4882 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4883 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4884 | |
| 4885 | // 77-108: v0-31, the 16-byte vector registers |
| 4886 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4887 | |
| 4888 | // 109: vrsave |
| 4889 | // 110: vscr |
| 4890 | // 111: spe_acc |
| 4891 | // 112: spefscr |
| 4892 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4893 | // 114: tfhar |
| 4894 | // 115: tfiar |
| 4895 | // 116: texasr |
| 4896 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4897 | |
| 4898 | return false; |
| 4899 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4900 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4901 | bool |
| 4902 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4903 | CodeGen::CodeGenFunction &CGF, |
| 4904 | llvm::Value *Address) const { |
| 4905 | |
| 4906 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4907 | } |
| 4908 | |
| 4909 | bool |
| 4910 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4911 | llvm::Value *Address) const { |
| 4912 | |
| 4913 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4914 | } |
| 4915 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4916 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4917 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4918 | //===----------------------------------------------------------------------===// |
| 4919 | |
| 4920 | namespace { |
| 4921 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4922 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4923 | public: |
| 4924 | enum ABIKind { |
| 4925 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4926 | DarwinPCS, |
| 4927 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4928 | }; |
| 4929 | |
| 4930 | private: |
| 4931 | ABIKind Kind; |
| 4932 | |
| 4933 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4934 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4935 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4936 | |
| 4937 | private: |
| 4938 | ABIKind getABIKind() const { return Kind; } |
| 4939 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4940 | |
| 4941 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4942 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4943 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4944 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4945 | uint64_t Members) const override; |
| 4946 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4947 | bool isIllegalVectorType(QualType Ty) const; |
| 4948 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4949 | void computeInfo(CGFunctionInfo &FI) const override { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4950 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4951 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4952 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4953 | for (auto &it : FI.arguments()) |
| 4954 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4957 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4958 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4959 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4960 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4961 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4962 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4963 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4964 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4965 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4966 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4967 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4968 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4969 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4970 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4971 | QualType Ty) const override; |
| 4972 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 4973 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4974 | bool asReturnValue) const override { |
| 4975 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4976 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4977 | bool isSwiftErrorInRegister() const override { |
| 4978 | return true; |
| 4979 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4980 | |
| 4981 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4982 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4983 | }; |
| 4984 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4985 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4986 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4987 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4988 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4989 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4990 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 4991 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4994 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4995 | return 31; |
| 4996 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4997 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4998 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 4999 | |
| 5000 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5001 | CodeGen::CodeGenModule &CGM) const override { |
| 5002 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 5003 | if (!FD) |
| 5004 | return; |
| 5005 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5006 | |
| 5007 | auto Kind = CGM.getCodeGenOpts().getSignReturnAddress(); |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5008 | if (Kind != CodeGenOptions::SignReturnAddressScope::None) { |
| 5009 | Fn->addFnAttr("sign-return-address", |
| 5010 | Kind == CodeGenOptions::SignReturnAddressScope::All |
| 5011 | ? "all" |
| 5012 | : "non-leaf"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5013 | |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5014 | auto Key = CGM.getCodeGenOpts().getSignReturnAddressKey(); |
| 5015 | Fn->addFnAttr("sign-return-address-key", |
| 5016 | Key == CodeGenOptions::SignReturnAddressKeyValue::AKey |
| 5017 | ? "a_key" |
| 5018 | : "b_key"); |
| 5019 | } |
| 5020 | |
| 5021 | if (CGM.getCodeGenOpts().BranchTargetEnforcement) |
| 5022 | Fn->addFnAttr("branch-target-enforcement"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5023 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5024 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5025 | |
| 5026 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 5027 | public: |
| 5028 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 5029 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 5030 | |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5031 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5032 | CodeGen::CodeGenModule &CGM) const override; |
| 5033 | |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5034 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5035 | llvm::SmallString<24> &Opt) const override { |
| 5036 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5037 | } |
| 5038 | |
| 5039 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5040 | llvm::SmallString<32> &Opt) const override { |
| 5041 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5042 | } |
| 5043 | }; |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5044 | |
| 5045 | void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( |
| 5046 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5047 | AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5048 | if (GV->isDeclaration()) |
| 5049 | return; |
| 5050 | addStackProbeTargetAttributes(D, GV, CGM); |
| 5051 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5052 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5053 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5054 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5055 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5056 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5057 | // Handle illegal vector types here. |
| 5058 | if (isIllegalVectorType(Ty)) { |
| 5059 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5060 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 5061 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5062 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 5063 | return ABIArgInfo::getDirect(ResType); |
| 5064 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5065 | if (Size <= 32) { |
| 5066 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5067 | return ABIArgInfo::getDirect(ResType); |
| 5068 | } |
| 5069 | if (Size == 64) { |
| 5070 | llvm::Type *ResType = |
| 5071 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5072 | return ABIArgInfo::getDirect(ResType); |
| 5073 | } |
| 5074 | if (Size == 128) { |
| 5075 | llvm::Type *ResType = |
| 5076 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5077 | return ABIArgInfo::getDirect(ResType); |
| 5078 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5079 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5080 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5081 | |
| 5082 | if (!isAggregateTypeForABI(Ty)) { |
| 5083 | // Treat an enum type as its underlying type. |
| 5084 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5085 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5086 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5087 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5088 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5089 | : ABIArgInfo::getDirect()); |
| 5090 | } |
| 5091 | |
| 5092 | // Structures with either a non-trivial destructor or a non-trivial |
| 5093 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5094 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5095 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5096 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5097 | } |
| 5098 | |
| 5099 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5100 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5101 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5102 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5103 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5104 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5105 | return ABIArgInfo::getIgnore(); |
| 5106 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5107 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5108 | // 0. |
| 5109 | if (IsEmpty && Size == 0) |
| 5110 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5111 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5112 | } |
| 5113 | |
| 5114 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5115 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5116 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5117 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5118 | return ABIArgInfo::getDirect( |
| 5119 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5123 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5124 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5125 | // same size and alignment. |
| 5126 | if (getTarget().isRenderScriptTarget()) { |
| 5127 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5128 | } |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5129 | unsigned Alignment; |
| 5130 | if (Kind == AArch64ABIInfo::AAPCS) { |
| 5131 | Alignment = getContext().getTypeUnadjustedAlign(Ty); |
| 5132 | Alignment = Alignment < 128 ? 64 : 128; |
| 5133 | } else { |
| 5134 | Alignment = getContext().getTypeAlign(Ty); |
| 5135 | } |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5136 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5137 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5138 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5139 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5140 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5141 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5142 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5143 | } |
| 5144 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5145 | } |
| 5146 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5147 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5150 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5151 | if (RetTy->isVoidType()) |
| 5152 | return ABIArgInfo::getIgnore(); |
| 5153 | |
| 5154 | // Large vector types should be returned via memory. |
| 5155 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5156 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5157 | |
| 5158 | if (!isAggregateTypeForABI(RetTy)) { |
| 5159 | // Treat an enum type as its underlying type. |
| 5160 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5161 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5162 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5163 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5164 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5165 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5168 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5169 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5170 | return ABIArgInfo::getIgnore(); |
| 5171 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5172 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5173 | uint64_t Members = 0; |
| 5174 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5175 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5176 | return ABIArgInfo::getDirect(); |
| 5177 | |
| 5178 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5179 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5180 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5181 | // same size and alignment. |
| 5182 | if (getTarget().isRenderScriptTarget()) { |
| 5183 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5184 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5185 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5186 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5187 | |
| 5188 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5189 | // For aggregates with 16-byte alignment, we use i128. |
| 5190 | if (Alignment < 128 && Size == 128) { |
| 5191 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5192 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5193 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5194 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5195 | } |
| 5196 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5197 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5198 | } |
| 5199 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5200 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5201 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5202 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5203 | // Check whether VT is legal. |
| 5204 | unsigned NumElements = VT->getNumElements(); |
| 5205 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5206 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5207 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5208 | return true; |
| 5209 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5210 | } |
| 5211 | return false; |
| 5212 | } |
| 5213 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5214 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5215 | llvm::Type *eltTy, |
| 5216 | unsigned elts) const { |
| 5217 | if (!llvm::isPowerOf2_32(elts)) |
| 5218 | return false; |
| 5219 | if (totalSize.getQuantity() != 8 && |
| 5220 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5221 | return false; |
| 5222 | return true; |
| 5223 | } |
| 5224 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5225 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5226 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5227 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5228 | // but with the difference that any floating-point type is allowed, |
| 5229 | // including __fp16. |
| 5230 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5231 | if (BT->isFloatingPoint()) |
| 5232 | return true; |
| 5233 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5234 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5235 | if (VecSize == 64 || VecSize == 128) |
| 5236 | return true; |
| 5237 | } |
| 5238 | return false; |
| 5239 | } |
| 5240 | |
| 5241 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5242 | uint64_t Members) const { |
| 5243 | return Members <= 4; |
| 5244 | } |
| 5245 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5246 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5247 | QualType Ty, |
| 5248 | CodeGenFunction &CGF) const { |
| 5249 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5250 | bool IsIndirect = AI.isIndirect(); |
| 5251 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5252 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5253 | if (IsIndirect) |
| 5254 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5255 | else if (AI.getCoerceToType()) |
| 5256 | BaseTy = AI.getCoerceToType(); |
| 5257 | |
| 5258 | unsigned NumRegs = 1; |
| 5259 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5260 | BaseTy = ArrTy->getElementType(); |
| 5261 | NumRegs = ArrTy->getNumElements(); |
| 5262 | } |
| 5263 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5264 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5265 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5266 | // Standard, section B.4: |
| 5267 | // |
| 5268 | // struct { |
| 5269 | // void *__stack; |
| 5270 | // void *__gr_top; |
| 5271 | // void *__vr_top; |
| 5272 | // int __gr_offs; |
| 5273 | // int __vr_offs; |
| 5274 | // }; |
| 5275 | |
| 5276 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5277 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5278 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5279 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5280 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5281 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 5282 | CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5283 | |
| 5284 | Address reg_offs_p = Address::invalid(); |
| 5285 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5286 | int reg_top_index; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5287 | int RegSize = IsIndirect ? 8 : TySize.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5288 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5289 | // 3 is the field number of __gr_offs |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5290 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5291 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5292 | reg_top_index = 1; // field number for __gr_top |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5293 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5294 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5295 | // 4 is the field number of __vr_offs. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5296 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5297 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5298 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5299 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | //======================================= |
| 5303 | // Find out where argument was passed |
| 5304 | //======================================= |
| 5305 | |
| 5306 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5307 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5308 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5309 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5310 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5311 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5312 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5313 | |
| 5314 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5315 | |
| 5316 | // 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] | 5317 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5318 | CGF.EmitBlock(MaybeRegBlock); |
| 5319 | |
| 5320 | // Integer arguments may need to correct register alignment (for example a |
| 5321 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5322 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5323 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5324 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5325 | |
| 5326 | reg_offs = CGF.Builder.CreateAdd( |
| 5327 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5328 | "align_regoffs"); |
| 5329 | reg_offs = CGF.Builder.CreateAnd( |
| 5330 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5331 | "aligned_regoffs"); |
| 5332 | } |
| 5333 | |
| 5334 | // 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] | 5335 | // The fact that this is done unconditionally reflects the fact that |
| 5336 | // allocating an argument to the stack also uses up all the remaining |
| 5337 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5338 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5339 | NewOffset = CGF.Builder.CreateAdd( |
| 5340 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5341 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5342 | |
| 5343 | // Now we're in a position to decide whether this argument really was in |
| 5344 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5345 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5346 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5347 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5348 | |
| 5349 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5350 | |
| 5351 | //======================================= |
| 5352 | // Argument was in registers |
| 5353 | //======================================= |
| 5354 | |
| 5355 | // Now we emit the code for if the argument was originally passed in |
| 5356 | // registers. First start the appropriate block: |
| 5357 | CGF.EmitBlock(InRegBlock); |
| 5358 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5359 | llvm::Value *reg_top = nullptr; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5360 | Address reg_top_p = |
| 5361 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5362 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5363 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5364 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5365 | Address RegAddr = Address::invalid(); |
| 5366 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5367 | |
| 5368 | if (IsIndirect) { |
| 5369 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5370 | // stored registers or on the stack will actually be a struct **. |
| 5371 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5372 | } |
| 5373 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5374 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5375 | uint64_t NumMembers = 0; |
| 5376 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5377 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5378 | // Homogeneous aggregates passed in registers will have their elements split |
| 5379 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5380 | // qN+1, ...). We reload and store into a temporary local variable |
| 5381 | // contiguously. |
| 5382 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5383 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5384 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5385 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5386 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5387 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5388 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5389 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5390 | int Offset = 0; |
| 5391 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5392 | BaseTyInfo.first.getQuantity() < 16) |
| 5393 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5394 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5395 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5396 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5397 | Address LoadAddr = |
| 5398 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5399 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5400 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5401 | Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5402 | |
| 5403 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5404 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5405 | } |
| 5406 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5407 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5408 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5409 | // Otherwise the object is contiguous in memory. |
| 5410 | |
| 5411 | // It might be right-aligned in its slot. |
| 5412 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5413 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5414 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5415 | TySize < SlotSize) { |
| 5416 | CharUnits Offset = SlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5417 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5418 | } |
| 5419 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5420 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
| 5423 | CGF.EmitBranch(ContBlock); |
| 5424 | |
| 5425 | //======================================= |
| 5426 | // Argument was on the stack |
| 5427 | //======================================= |
| 5428 | CGF.EmitBlock(OnStackBlock); |
| 5429 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5430 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5431 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5432 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5433 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5434 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5435 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5436 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5437 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5438 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5439 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5440 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5441 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5442 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5443 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5444 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5445 | "align_stack"); |
| 5446 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5447 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5448 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5449 | Address OnStackAddr(OnStackPtr, |
| 5450 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5451 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5452 | // All stack slots are multiples of 8 bytes. |
| 5453 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5454 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5455 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5456 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5457 | else |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5458 | StackSize = TySize.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5460 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5461 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5462 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5463 | |
| 5464 | // Write the new value of __stack for the next call to va_arg |
| 5465 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5466 | |
| 5467 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5468 | TySize < StackSlotSize) { |
| 5469 | CharUnits Offset = StackSlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5470 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5471 | } |
| 5472 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5473 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5474 | |
| 5475 | CGF.EmitBranch(ContBlock); |
| 5476 | |
| 5477 | //======================================= |
| 5478 | // Tidy up |
| 5479 | //======================================= |
| 5480 | CGF.EmitBlock(ContBlock); |
| 5481 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5482 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5483 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5484 | |
| 5485 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5486 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5487 | TyAlign); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5488 | |
| 5489 | return ResAddr; |
| 5490 | } |
| 5491 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5492 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5493 | CodeGenFunction &CGF) const { |
| 5494 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5495 | // illegal vector types. Lower VAArg here for these cases and use |
| 5496 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5497 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5498 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5499 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5500 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5501 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5502 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5503 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5504 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5505 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5506 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5509 | // The size of the actual thing passed, which might end up just |
| 5510 | // being a pointer for indirect types. |
| 5511 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5512 | |
| 5513 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5514 | // aggregates should be passed indirectly. |
| 5515 | bool IsIndirect = false; |
| 5516 | if (TyInfo.first.getQuantity() > 16) { |
| 5517 | const Type *Base = nullptr; |
| 5518 | uint64_t Members = 0; |
| 5519 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5520 | } |
| 5521 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5522 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5523 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5524 | } |
| 5525 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5526 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5527 | QualType Ty) const { |
| 5528 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5529 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5530 | CharUnits::fromQuantity(8), |
| 5531 | /*allowHigherAlign*/ false); |
| 5532 | } |
| 5533 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5534 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5535 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5536 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5537 | |
| 5538 | namespace { |
| 5539 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5540 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5541 | public: |
| 5542 | enum ABIKind { |
| 5543 | APCS = 0, |
| 5544 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5545 | AAPCS_VFP = 2, |
| 5546 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5547 | }; |
| 5548 | |
| 5549 | private: |
| 5550 | ABIKind Kind; |
| 5551 | |
| 5552 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5553 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5554 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5555 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5556 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5557 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5558 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5559 | switch (getTarget().getTriple().getEnvironment()) { |
| 5560 | case llvm::Triple::Android: |
| 5561 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5562 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5563 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5564 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5565 | case llvm::Triple::MuslEABI: |
| 5566 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5567 | return true; |
| 5568 | default: |
| 5569 | return false; |
| 5570 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5571 | } |
| 5572 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5573 | bool isEABIHF() const { |
| 5574 | switch (getTarget().getTriple().getEnvironment()) { |
| 5575 | case llvm::Triple::EABIHF: |
| 5576 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5577 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5578 | return true; |
| 5579 | default: |
| 5580 | return false; |
| 5581 | } |
| 5582 | } |
| 5583 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5584 | ABIKind getABIKind() const { return Kind; } |
| 5585 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5586 | private: |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5587 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic, |
| 5588 | unsigned functionCallConv) const; |
| 5589 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, |
| 5590 | unsigned functionCallConv) const; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5591 | ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, |
| 5592 | uint64_t Members) const; |
| 5593 | ABIArgInfo coerceIllegalVector(QualType Ty) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5594 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5595 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5596 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5597 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5598 | uint64_t Members) const override; |
| 5599 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5600 | bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const; |
| 5601 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5602 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5603 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5604 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5605 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5606 | |
| 5607 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5608 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5609 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5610 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5611 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5612 | bool asReturnValue) const override { |
| 5613 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5614 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5615 | bool isSwiftErrorInRegister() const override { |
| 5616 | return true; |
| 5617 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5618 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5619 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5620 | }; |
| 5621 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5622 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5623 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5624 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5625 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5626 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5627 | const ARMABIInfo &getABIInfo() const { |
| 5628 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5629 | } |
| 5630 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5631 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5632 | return 13; |
| 5633 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5634 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5635 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5636 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5639 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5640 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5641 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5642 | |
| 5643 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5644 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5645 | return false; |
| 5646 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5647 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5648 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5649 | if (getABIInfo().isEABI()) return 88; |
| 5650 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5651 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5652 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5653 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5654 | CodeGen::CodeGenModule &CGM) const override { |
| 5655 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5656 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5657 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5658 | if (!FD) |
| 5659 | return; |
| 5660 | |
| 5661 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5662 | if (!Attr) |
| 5663 | return; |
| 5664 | |
| 5665 | const char *Kind; |
| 5666 | switch (Attr->getInterrupt()) { |
| 5667 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5668 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5669 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5670 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5671 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5672 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5673 | } |
| 5674 | |
| 5675 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5676 | |
| 5677 | Fn->addFnAttr("interrupt", Kind); |
| 5678 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5679 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5680 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5681 | return; |
| 5682 | |
| 5683 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5684 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5685 | // the backend to perform a realignment as part of the function prologue. |
| 5686 | llvm::AttrBuilder B; |
| 5687 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5688 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5689 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5690 | }; |
| 5691 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5692 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5693 | public: |
| 5694 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5695 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5696 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5697 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5698 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5699 | |
| 5700 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5701 | llvm::SmallString<24> &Opt) const override { |
| 5702 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5703 | } |
| 5704 | |
| 5705 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5706 | llvm::SmallString<32> &Opt) const override { |
| 5707 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5708 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5709 | }; |
| 5710 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5711 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5712 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5713 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5714 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5715 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 5716 | addStackProbeTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5717 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5718 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5719 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5720 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5721 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5722 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(), |
| 5723 | FI.getCallingConvention()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5724 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5725 | for (auto &I : FI.arguments()) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5726 | I.info = classifyArgumentType(I.type, FI.isVariadic(), |
| 5727 | FI.getCallingConvention()); |
| 5728 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5729 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5730 | // Always honor user-specified calling convention. |
| 5731 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5732 | return; |
| 5733 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5734 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5735 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5736 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5737 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5738 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5739 | /// Return the default calling convention that LLVM will use. |
| 5740 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5741 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5742 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5743 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5744 | else if (isEABI()) |
| 5745 | return llvm::CallingConv::ARM_AAPCS; |
| 5746 | else |
| 5747 | return llvm::CallingConv::ARM_APCS; |
| 5748 | } |
| 5749 | |
| 5750 | /// Return the calling convention that our ABI would like us to use |
| 5751 | /// as the C calling convention. |
| 5752 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5753 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5754 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5755 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5756 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5757 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5758 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5759 | llvm_unreachable("bad ABI kind"); |
| 5760 | } |
| 5761 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5762 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5763 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5764 | |
| 5765 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5766 | // they'd just match what LLVM will infer from the triple. |
| 5767 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5768 | if (abiCC != getLLVMDefaultCC()) |
| 5769 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5772 | ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { |
| 5773 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5774 | if (Size <= 32) { |
| 5775 | llvm::Type *ResType = |
| 5776 | llvm::Type::getInt32Ty(getVMContext()); |
| 5777 | return ABIArgInfo::getDirect(ResType); |
| 5778 | } |
| 5779 | if (Size == 64 || Size == 128) { |
| 5780 | llvm::Type *ResType = llvm::VectorType::get( |
| 5781 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5782 | return ABIArgInfo::getDirect(ResType); |
| 5783 | } |
| 5784 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5785 | } |
| 5786 | |
| 5787 | ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, |
| 5788 | const Type *Base, |
| 5789 | uint64_t Members) const { |
| 5790 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 5791 | // Base can be a floating-point or a vector. |
| 5792 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 5793 | // FP16 vectors should be converted to integer vectors |
| 5794 | if (!getTarget().hasLegalHalfType() && |
| 5795 | (VT->getElementType()->isFloat16Type() || |
| 5796 | VT->getElementType()->isHalfType())) { |
| 5797 | uint64_t Size = getContext().getTypeSize(VT); |
| 5798 | llvm::Type *NewVecTy = llvm::VectorType::get( |
| 5799 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5800 | llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); |
| 5801 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5802 | } |
| 5803 | } |
| 5804 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 5805 | } |
| 5806 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5807 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, |
| 5808 | unsigned functionCallConv) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5809 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5810 | // A single-precision floating-point type (including promoted |
| 5811 | // half-precision types); A double-precision floating-point type; |
| 5812 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5813 | // with a Base Type of a single- or double-precision floating-point type, |
| 5814 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5815 | // to four Elements. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5816 | // Variadic functions should always marshal to the base standard. |
| 5817 | bool IsAAPCS_VFP = |
| 5818 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5819 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5820 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5821 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5822 | // Handle illegal vector types here. |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5823 | if (isIllegalVectorType(Ty)) |
| 5824 | return coerceIllegalVector(Ty); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5825 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5826 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5827 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5828 | // half type natively, and does not need to interwork with AAPCS code. |
| 5829 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5830 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5831 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5832 | llvm::Type::getFloatTy(getVMContext()) : |
| 5833 | llvm::Type::getInt32Ty(getVMContext()); |
| 5834 | return ABIArgInfo::getDirect(ResType); |
| 5835 | } |
| 5836 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5837 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5838 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5839 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5840 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5841 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5842 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5843 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5844 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5845 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5846 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5847 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5848 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5849 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5850 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5851 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5852 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5853 | return ABIArgInfo::getIgnore(); |
| 5854 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5855 | if (IsAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5856 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5857 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5858 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5859 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5860 | if (isHomogeneousAggregate(Ty, Base, Members)) |
| 5861 | return classifyHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5862 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5863 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5864 | // this convention even for a variadic function: the backend will use GPRs |
| 5865 | // if needed. |
| 5866 | const Type *Base = nullptr; |
| 5867 | uint64_t Members = 0; |
| 5868 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5869 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5870 | llvm::Type *Ty = |
| 5871 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5872 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5873 | } |
| 5874 | } |
| 5875 | |
| 5876 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5877 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5878 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5879 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5880 | // and a pointer is passed. |
| 5881 | return ABIArgInfo::getIndirect( |
| 5882 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5885 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5886 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5887 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5888 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5889 | uint64_t ABIAlign = 4; |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5890 | uint64_t TyAlign; |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5891 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5892 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5893 | TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5894 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5895 | } else { |
| 5896 | TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 5897 | } |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5898 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5899 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5900 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5901 | /*ByVal=*/true, |
| 5902 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5905 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5906 | // same size and alignment. |
| 5907 | if (getTarget().isRenderScriptTarget()) { |
| 5908 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5909 | } |
| 5910 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5911 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5912 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5913 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5914 | // FIXME: Try to match the types of the arguments more accurately where |
| 5915 | // we can. |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5916 | if (TyAlign <= 4) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5917 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5918 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5919 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5920 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5921 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5922 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5923 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5924 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5925 | } |
| 5926 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5927 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5928 | llvm::LLVMContext &VMContext) { |
| 5929 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5930 | // is called integer-like if its size is less than or equal to one word, and |
| 5931 | // the offset of each of its addressable sub-fields is zero. |
| 5932 | |
| 5933 | uint64_t Size = Context.getTypeSize(Ty); |
| 5934 | |
| 5935 | // Check that the type fits in a word. |
| 5936 | if (Size > 32) |
| 5937 | return false; |
| 5938 | |
| 5939 | // FIXME: Handle vector types! |
| 5940 | if (Ty->isVectorType()) |
| 5941 | return false; |
| 5942 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5943 | // Float types are never treated as "integer like". |
| 5944 | if (Ty->isRealFloatingType()) |
| 5945 | return false; |
| 5946 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5947 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5948 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5949 | return true; |
| 5950 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5951 | // Small complex integer types are "integer like". |
| 5952 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5953 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5954 | |
| 5955 | // Single element and zero sized arrays should be allowed, by the definition |
| 5956 | // above, but they are not. |
| 5957 | |
| 5958 | // Otherwise, it must be a record type. |
| 5959 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5960 | if (!RT) return false; |
| 5961 | |
| 5962 | // Ignore records with flexible arrays. |
| 5963 | const RecordDecl *RD = RT->getDecl(); |
| 5964 | if (RD->hasFlexibleArrayMember()) |
| 5965 | return false; |
| 5966 | |
| 5967 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5968 | // like". |
| 5969 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5970 | |
| 5971 | bool HadField = false; |
| 5972 | unsigned idx = 0; |
| 5973 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5974 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5975 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5976 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5977 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5978 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5979 | // struct { int : 0; int x } |
| 5980 | // is non-integer like according to gcc. |
| 5981 | if (FD->isBitField()) { |
| 5982 | if (!RD->isUnion()) |
| 5983 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5984 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5985 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5986 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5987 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5988 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5991 | // Check if this field is at offset 0. |
| 5992 | if (Layout.getFieldOffset(idx) != 0) |
| 5993 | return false; |
| 5994 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5995 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5996 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5997 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5998 | // Only allow at most one field in a structure. This doesn't match the |
| 5999 | // wording above, but follows gcc in situations with a field following an |
| 6000 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6001 | if (!RD->isUnion()) { |
| 6002 | if (HadField) |
| 6003 | return false; |
| 6004 | |
| 6005 | HadField = true; |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | return true; |
| 6010 | } |
| 6011 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6012 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic, |
| 6013 | unsigned functionCallConv) const { |
| 6014 | |
| 6015 | // Variadic functions should always marshal to the base standard. |
| 6016 | bool IsAAPCS_VFP = |
| 6017 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6018 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6019 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6020 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6021 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6022 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| 6023 | // Large vector types should be returned via memory. |
| 6024 | if (getContext().getTypeSize(RetTy) > 128) |
| 6025 | return getNaturalAlignIndirect(RetTy); |
| 6026 | // FP16 vectors should be converted to integer vectors |
| 6027 | if (!getTarget().hasLegalHalfType() && |
| 6028 | (VT->getElementType()->isFloat16Type() || |
| 6029 | VT->getElementType()->isHalfType())) |
| 6030 | return coerceIllegalVector(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6031 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 6032 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 6033 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 6034 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 6035 | // half type natively, and does not need to interwork with AAPCS code. |
| 6036 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 6037 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6038 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 6039 | llvm::Type::getFloatTy(getVMContext()) : |
| 6040 | llvm::Type::getInt32Ty(getVMContext()); |
| 6041 | return ABIArgInfo::getDirect(ResType); |
| 6042 | } |
| 6043 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 6044 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6045 | // Treat an enum type as its underlying type. |
| 6046 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6047 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6048 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6049 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6050 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6051 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6052 | |
| 6053 | // Are we following APCS? |
| 6054 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6055 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6056 | return ABIArgInfo::getIgnore(); |
| 6057 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6058 | // Complex types are all returned as packed integers. |
| 6059 | // |
| 6060 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 6061 | // correctly. |
| 6062 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6063 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 6064 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6065 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6066 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6067 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6068 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6069 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6070 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6071 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6072 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6073 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6074 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6075 | } |
| 6076 | |
| 6077 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6078 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6079 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6080 | |
| 6081 | // Otherwise this is an AAPCS variant. |
| 6082 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6083 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6084 | return ABIArgInfo::getIgnore(); |
| 6085 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6086 | // Check for homogeneous aggregates with AAPCS-VFP. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6087 | if (IsAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6088 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6089 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6090 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
| 6091 | return classifyHomogeneousAggregate(RetTy, Base, Members); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6094 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 6095 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6096 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6097 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 6098 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 6099 | // same size and alignment. |
| 6100 | if (getTarget().isRenderScriptTarget()) { |
| 6101 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 6102 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6103 | if (getDataLayout().isBigEndian()) |
| 6104 | // 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] | 6105 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6106 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6107 | // Return in the smallest viable integer type. |
| 6108 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6109 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6110 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6111 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6112 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6113 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6114 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6115 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6116 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6117 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6118 | } |
| 6119 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6120 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6123 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6124 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6125 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6126 | // On targets that don't support FP16, FP16 is expanded into float, and we |
| 6127 | // don't want the ABI to depend on whether or not FP16 is supported in |
| 6128 | // hardware. Thus return false to coerce FP16 vectors into integer vectors. |
| 6129 | if (!getTarget().hasLegalHalfType() && |
| 6130 | (VT->getElementType()->isFloat16Type() || |
| 6131 | VT->getElementType()->isHalfType())) |
| 6132 | return true; |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6133 | if (isAndroid()) { |
| 6134 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6135 | // vector ABI. The primary differences were that 3-element vector types |
| 6136 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6137 | // accepts that legacy behavior for Android only. |
| 6138 | // Check whether VT is legal. |
| 6139 | unsigned NumElements = VT->getNumElements(); |
| 6140 | // NumElements should be power of 2 or equal to 3. |
| 6141 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6142 | return true; |
| 6143 | } else { |
| 6144 | // Check whether VT is legal. |
| 6145 | unsigned NumElements = VT->getNumElements(); |
| 6146 | uint64_t Size = getContext().getTypeSize(VT); |
| 6147 | // NumElements should be power of 2. |
| 6148 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6149 | return true; |
| 6150 | // Size should be greater than 32 bits. |
| 6151 | return Size <= 32; |
| 6152 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6153 | } |
| 6154 | return false; |
| 6155 | } |
| 6156 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6157 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6158 | llvm::Type *eltTy, |
| 6159 | unsigned numElts) const { |
| 6160 | if (!llvm::isPowerOf2_32(numElts)) |
| 6161 | return false; |
| 6162 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6163 | if (size > 64) |
| 6164 | return false; |
| 6165 | if (vectorSize.getQuantity() != 8 && |
| 6166 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6167 | return false; |
| 6168 | return true; |
| 6169 | } |
| 6170 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6171 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6172 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6173 | // double, or 64-bit or 128-bit vectors. |
| 6174 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6175 | if (BT->getKind() == BuiltinType::Float || |
| 6176 | BT->getKind() == BuiltinType::Double || |
| 6177 | BT->getKind() == BuiltinType::LongDouble) |
| 6178 | return true; |
| 6179 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6180 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6181 | if (VecSize == 64 || VecSize == 128) |
| 6182 | return true; |
| 6183 | } |
| 6184 | return false; |
| 6185 | } |
| 6186 | |
| 6187 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6188 | uint64_t Members) const { |
| 6189 | return Members <= 4; |
| 6190 | } |
| 6191 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6192 | bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention, |
| 6193 | bool acceptHalf) const { |
| 6194 | // Give precedence to user-specified calling conventions. |
| 6195 | if (callConvention != llvm::CallingConv::C) |
| 6196 | return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP); |
| 6197 | else |
| 6198 | return (getABIKind() == AAPCS_VFP) || |
| 6199 | (acceptHalf && (getABIKind() == AAPCS16_VFP)); |
| 6200 | } |
| 6201 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6202 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6203 | QualType Ty) const { |
| 6204 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6205 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6206 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6207 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6208 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6209 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6210 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6213 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 6214 | CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6215 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6216 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6217 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6218 | const Type *Base = nullptr; |
| 6219 | uint64_t Members = 0; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6220 | if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6221 | IsIndirect = true; |
| 6222 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6223 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6224 | // allocated by the caller. |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6225 | } else if (TySize > CharUnits::fromQuantity(16) && |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6226 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6227 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6228 | IsIndirect = true; |
| 6229 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6230 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6231 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6232 | // 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] | 6233 | // Our callers should be prepared to handle an under-aligned address. |
| 6234 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6235 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6236 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6237 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6238 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6239 | // ARMv7k allows type alignment up to 16 bytes. |
| 6240 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6241 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6242 | } else { |
| 6243 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6244 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6245 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6246 | std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6247 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6248 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6249 | } |
| 6250 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6251 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6252 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6253 | //===----------------------------------------------------------------------===// |
| 6254 | |
| 6255 | namespace { |
| 6256 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6257 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6258 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6259 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6260 | |
| 6261 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6262 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6263 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6264 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6265 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6266 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6267 | }; |
| 6268 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6269 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6270 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6271 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6272 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6273 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6274 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6275 | CodeGen::CodeGenModule &M) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6276 | bool shouldEmitStaticExternCAliases() const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6277 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6278 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6279 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6280 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6281 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6282 | }; |
| 6283 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6284 | /// Checks if the type is unsupported directly by the current target. |
| 6285 | static bool isUnsupportedType(ASTContext &Context, QualType T) { |
| 6286 | if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type()) |
| 6287 | return true; |
| 6288 | if (!Context.getTargetInfo().hasFloat128Type() && T->isFloat128Type()) |
| 6289 | return true; |
| 6290 | if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() && |
| 6291 | Context.getTypeSize(T) > 64) |
| 6292 | return true; |
| 6293 | if (const auto *AT = T->getAsArrayTypeUnsafe()) |
| 6294 | return isUnsupportedType(Context, AT->getElementType()); |
| 6295 | const auto *RT = T->getAs<RecordType>(); |
| 6296 | if (!RT) |
| 6297 | return false; |
| 6298 | const RecordDecl *RD = RT->getDecl(); |
| 6299 | |
| 6300 | // If this is a C++ record, check the bases first. |
| 6301 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6302 | for (const CXXBaseSpecifier &I : CXXRD->bases()) |
| 6303 | if (isUnsupportedType(Context, I.getType())) |
| 6304 | return true; |
| 6305 | |
| 6306 | for (const FieldDecl *I : RD->fields()) |
| 6307 | if (isUnsupportedType(Context, I->getType())) |
| 6308 | return true; |
| 6309 | return false; |
| 6310 | } |
| 6311 | |
| 6312 | /// Coerce the given type into an array with maximum allowed size of elements. |
| 6313 | static ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, ASTContext &Context, |
| 6314 | llvm::LLVMContext &LLVMContext, |
| 6315 | unsigned MaxSize) { |
| 6316 | // Alignment and Size are measured in bits. |
| 6317 | const uint64_t Size = Context.getTypeSize(Ty); |
| 6318 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 6319 | const unsigned Div = std::min<unsigned>(MaxSize, Alignment); |
| 6320 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Div); |
| 6321 | const uint64_t NumElements = (Size + Div - 1) / Div; |
| 6322 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 6323 | } |
| 6324 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6325 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6326 | if (RetTy->isVoidType()) |
| 6327 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6328 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6329 | if (getContext().getLangOpts().OpenMP && |
| 6330 | getContext().getLangOpts().OpenMPIsDevice && |
| 6331 | isUnsupportedType(getContext(), RetTy)) |
| 6332 | return coerceToIntArrayWithLimit(RetTy, getContext(), getVMContext(), 64); |
| 6333 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6334 | // note: this is different from default ABI |
| 6335 | if (!RetTy->isScalarType()) |
| 6336 | return ABIArgInfo::getDirect(); |
| 6337 | |
| 6338 | // Treat an enum type as its underlying type. |
| 6339 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6340 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6341 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6342 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6343 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6346 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6347 | // Treat an enum type as its underlying type. |
| 6348 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6349 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6350 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6351 | // Return aggregates type as indirect by value |
| 6352 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6353 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6354 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6355 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6356 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6359 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6360 | if (!getCXXABI().classifyReturnType(FI)) |
| 6361 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6362 | for (auto &I : FI.arguments()) |
| 6363 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6364 | |
| 6365 | // Always honor user-specified calling convention. |
| 6366 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6367 | return; |
| 6368 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6369 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6370 | } |
| 6371 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6372 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6373 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6374 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6377 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6378 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6379 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6380 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6381 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6382 | if (!FD) return; |
| 6383 | |
| 6384 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6385 | |
| 6386 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6387 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6388 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6389 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6390 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6391 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6392 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6393 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6394 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6395 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6396 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6397 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6398 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6399 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6400 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6401 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6402 | // __global__ functions cannot be called from the device, we do not |
| 6403 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6404 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6405 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6406 | addNVVMMetadata(F, "kernel", 1); |
| 6407 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6408 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6409 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6410 | llvm::APSInt MaxThreads(32); |
| 6411 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6412 | if (MaxThreads > 0) |
| 6413 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6414 | |
| 6415 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6416 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6417 | // we don't have to add a PTX directive. |
| 6418 | if (Attr->getMinBlocks()) { |
| 6419 | llvm::APSInt MinBlocks(32); |
| 6420 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6421 | if (MinBlocks > 0) |
| 6422 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6423 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6424 | } |
| 6425 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6426 | } |
| 6427 | } |
| 6428 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6429 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6430 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6431 | llvm::Module *M = F->getParent(); |
| 6432 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6433 | |
| 6434 | // Get "nvvm.annotations" metadata node |
| 6435 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6436 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6437 | llvm::Metadata *MDVals[] = { |
| 6438 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6439 | llvm::ConstantAsMetadata::get( |
| 6440 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6441 | // Append metadata to nvvm.annotations |
| 6442 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6443 | } |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6444 | |
| 6445 | bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 6446 | return false; |
| 6447 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6448 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6449 | |
| 6450 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6451 | // SystemZ ABI Implementation |
| 6452 | //===----------------------------------------------------------------------===// |
| 6453 | |
| 6454 | namespace { |
| 6455 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6456 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6457 | bool HasVector; |
| 6458 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6459 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6460 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6461 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6462 | |
| 6463 | bool isPromotableIntegerType(QualType Ty) const; |
| 6464 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6465 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6466 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6467 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6468 | |
| 6469 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6470 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6471 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6472 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6473 | if (!getCXXABI().classifyReturnType(FI)) |
| 6474 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6475 | for (auto &I : FI.arguments()) |
| 6476 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6479 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6480 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6481 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6482 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6483 | bool asReturnValue) const override { |
| 6484 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6485 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6486 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6487 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6488 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6489 | }; |
| 6490 | |
| 6491 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6492 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6493 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6494 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6495 | }; |
| 6496 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6497 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6498 | |
| 6499 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6500 | // Treat an enum type as its underlying type. |
| 6501 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6502 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6503 | |
| 6504 | // Promotable integer types are required to be promoted by the ABI. |
| 6505 | if (Ty->isPromotableIntegerType()) |
| 6506 | return true; |
| 6507 | |
| 6508 | // 32-bit values must also be promoted. |
| 6509 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6510 | switch (BT->getKind()) { |
| 6511 | case BuiltinType::Int: |
| 6512 | case BuiltinType::UInt: |
| 6513 | return true; |
| 6514 | default: |
| 6515 | return false; |
| 6516 | } |
| 6517 | return false; |
| 6518 | } |
| 6519 | |
| 6520 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6521 | return (Ty->isAnyComplexType() || |
| 6522 | Ty->isVectorType() || |
| 6523 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6526 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6527 | return (HasVector && |
| 6528 | Ty->isVectorType() && |
| 6529 | getContext().getTypeSize(Ty) <= 128); |
| 6530 | } |
| 6531 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6532 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6533 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6534 | switch (BT->getKind()) { |
| 6535 | case BuiltinType::Float: |
| 6536 | case BuiltinType::Double: |
| 6537 | return true; |
| 6538 | default: |
| 6539 | return false; |
| 6540 | } |
| 6541 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6542 | return false; |
| 6543 | } |
| 6544 | |
| 6545 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6546 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6547 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6548 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6549 | |
| 6550 | // If this is a C++ record, check the bases first. |
| 6551 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6552 | for (const auto &I : CXXRD->bases()) { |
| 6553 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6554 | |
| 6555 | // Empty bases don't affect things either way. |
| 6556 | if (isEmptyRecord(getContext(), Base, true)) |
| 6557 | continue; |
| 6558 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6559 | if (!Found.isNull()) |
| 6560 | return Ty; |
| 6561 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6562 | } |
| 6563 | |
| 6564 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6565 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6566 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6567 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6568 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6569 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 6570 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6571 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6572 | |
| 6573 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6574 | // Nested structures still do though. |
| 6575 | if (!Found.isNull()) |
| 6576 | return Ty; |
| 6577 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6578 | } |
| 6579 | |
| 6580 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6581 | // 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] | 6582 | if (!Found.isNull()) |
| 6583 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6586 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6587 | } |
| 6588 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6589 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6590 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6591 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6592 | // struct { |
| 6593 | // i64 __gpr; |
| 6594 | // i64 __fpr; |
| 6595 | // i8 *__overflow_arg_area; |
| 6596 | // i8 *__reg_save_area; |
| 6597 | // }; |
| 6598 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6599 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6600 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6601 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6602 | Ty = getContext().getCanonicalType(Ty); |
| 6603 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6604 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6605 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6606 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6607 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6608 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6609 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6610 | CharUnits UnpaddedSize; |
| 6611 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6612 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6613 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6614 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6615 | } else { |
| 6616 | if (AI.getCoerceToType()) |
| 6617 | ArgTy = AI.getCoerceToType(); |
| 6618 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6619 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6620 | UnpaddedSize = TyInfo.first; |
| 6621 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6622 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6623 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6624 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6625 | PaddedSize = CharUnits::fromQuantity(16); |
| 6626 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6627 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6628 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6629 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6630 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6631 | llvm::Value *PaddedSizeV = |
| 6632 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6633 | |
| 6634 | if (IsVector) { |
| 6635 | // Work out the address of a vector argument on the stack. |
| 6636 | // Vector arguments are always passed in the high bits of a |
| 6637 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6638 | Address OverflowArgAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6639 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6640 | Address OverflowArgArea = |
| 6641 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6642 | TyInfo.second); |
| 6643 | Address MemAddr = |
| 6644 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6645 | |
| 6646 | // Update overflow_arg_area_ptr pointer |
| 6647 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6648 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6649 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6650 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6651 | |
| 6652 | return MemAddr; |
| 6653 | } |
| 6654 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6655 | assert(PaddedSize.getQuantity() == 8); |
| 6656 | |
| 6657 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6658 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6659 | if (InFPRs) { |
| 6660 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6661 | RegCountField = 1; // __fpr |
| 6662 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6663 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6664 | } else { |
| 6665 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6666 | RegCountField = 0; // __gpr |
| 6667 | RegSaveIndex = 2; // save offset for r2 |
| 6668 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6669 | } |
| 6670 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6671 | Address RegCountPtr = |
| 6672 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6673 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6674 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6675 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6676 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6677 | |
| 6678 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6679 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6680 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6681 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6682 | |
| 6683 | // Emit code to load the value if it was passed in registers. |
| 6684 | CGF.EmitBlock(InRegBlock); |
| 6685 | |
| 6686 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6687 | llvm::Value *ScaledRegCount = |
| 6688 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6689 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6690 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6691 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6692 | llvm::Value *RegOffset = |
| 6693 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6694 | Address RegSaveAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6695 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6696 | llvm::Value *RegSaveArea = |
| 6697 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6698 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6699 | "raw_reg_addr"), |
| 6700 | PaddedSize); |
| 6701 | Address RegAddr = |
| 6702 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6703 | |
| 6704 | // Update the register count |
| 6705 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6706 | llvm::Value *NewRegCount = |
| 6707 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6708 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6709 | CGF.EmitBranch(ContBlock); |
| 6710 | |
| 6711 | // Emit code to load the value if it was passed in memory. |
| 6712 | CGF.EmitBlock(InMemBlock); |
| 6713 | |
| 6714 | // Work out the address of a stack argument. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6715 | Address OverflowArgAreaPtr = |
| 6716 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6717 | Address OverflowArgArea = |
| 6718 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6719 | PaddedSize); |
| 6720 | Address RawMemAddr = |
| 6721 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6722 | Address MemAddr = |
| 6723 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6724 | |
| 6725 | // Update overflow_arg_area_ptr pointer |
| 6726 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6727 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6728 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6729 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6730 | CGF.EmitBranch(ContBlock); |
| 6731 | |
| 6732 | // Return the appropriate result. |
| 6733 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6734 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6735 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6736 | |
| 6737 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6738 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6739 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6740 | |
| 6741 | return ResAddr; |
| 6742 | } |
| 6743 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6744 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6745 | if (RetTy->isVoidType()) |
| 6746 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6747 | if (isVectorArgumentType(RetTy)) |
| 6748 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6749 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6750 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6751 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6752 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6756 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6757 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6758 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6759 | |
| 6760 | // Integers and enums are extended to full register width. |
| 6761 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6762 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6763 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6764 | // Handle vector types and vector-like structure types. Note that |
| 6765 | // as opposed to float-like structure types, we do not allow any |
| 6766 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6767 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6768 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6769 | if (isVectorArgumentType(SingleElementTy) && |
| 6770 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6771 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6772 | |
| 6773 | // 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] | 6774 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6775 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6776 | |
| 6777 | // Handle small structures. |
| 6778 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6779 | // Structures with flexible arrays have variable length, so really |
| 6780 | // fail the size test above. |
| 6781 | const RecordDecl *RD = RT->getDecl(); |
| 6782 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6783 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6784 | |
| 6785 | // The structure is passed as an unextended integer, a float, or a double. |
| 6786 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6787 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6788 | assert(Size == 32 || Size == 64); |
| 6789 | if (Size == 32) |
| 6790 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6791 | else |
| 6792 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6793 | } else |
| 6794 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6795 | return ABIArgInfo::getDirect(PassTy); |
| 6796 | } |
| 6797 | |
| 6798 | // Non-structure compounds are passed indirectly. |
| 6799 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6800 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6801 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6802 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
| 6805 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6806 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6807 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6808 | |
| 6809 | namespace { |
| 6810 | |
| 6811 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6812 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6813 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6814 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6815 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6816 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6817 | }; |
| 6818 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6819 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6820 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6821 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6822 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6823 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6824 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6825 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6826 | const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); |
| 6827 | if (!InterruptAttr) |
| 6828 | return; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6829 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6830 | // Handle 'interrupt' attribute: |
| 6831 | llvm::Function *F = cast<llvm::Function>(GV); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6832 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6833 | // Step 1: Set ISR calling convention. |
| 6834 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6835 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6836 | // Step 2: Add attributes goodness. |
| 6837 | F->addFnAttr(llvm::Attribute::NoInline); |
| 6838 | F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6839 | } |
| 6840 | } |
| 6841 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6842 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6843 | // MIPS ABI Implementation. This works for both little-endian and |
| 6844 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6845 | //===----------------------------------------------------------------------===// |
| 6846 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6847 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6848 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6849 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6850 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6851 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6852 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6853 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6854 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6855 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6856 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6857 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6858 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6859 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6860 | |
| 6861 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6862 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6863 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6864 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6865 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6866 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6867 | }; |
| 6868 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6869 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6870 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6871 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6872 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6873 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6874 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6875 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6876 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6877 | return 29; |
| 6878 | } |
| 6879 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6880 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6881 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6882 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6883 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6884 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6885 | |
| 6886 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6887 | Fn->addFnAttr("long-call"); |
| 6888 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6889 | Fn->addFnAttr("short-call"); |
| 6890 | |
| 6891 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6892 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6893 | return; |
| 6894 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6895 | if (FD->hasAttr<Mips16Attr>()) { |
| 6896 | Fn->addFnAttr("mips16"); |
| 6897 | } |
| 6898 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6899 | Fn->addFnAttr("nomips16"); |
| 6900 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6901 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6902 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6903 | Fn->addFnAttr("micromips"); |
| 6904 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6905 | Fn->addFnAttr("nomicromips"); |
| 6906 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6907 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6908 | if (!Attr) |
| 6909 | return; |
| 6910 | |
| 6911 | const char *Kind; |
| 6912 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6913 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6914 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6915 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6916 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6917 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6918 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6919 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6920 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6921 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6922 | } |
| 6923 | |
| 6924 | Fn->addFnAttr("interrupt", Kind); |
| 6925 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6926 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6927 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6928 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6929 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6930 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6931 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6932 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6933 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6934 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6935 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6936 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6937 | void MipsABIInfo::CoerceToIntArgs( |
| 6938 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6939 | llvm::IntegerType *IntTy = |
| 6940 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6941 | |
| 6942 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6943 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6944 | ArgList.push_back(IntTy); |
| 6945 | |
| 6946 | // If necessary, add one more integer type to ArgList. |
| 6947 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6948 | |
| 6949 | if (R) |
| 6950 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6951 | } |
| 6952 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6953 | // In N32/64, an aligned double precision floating point field is passed in |
| 6954 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6955 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6956 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6957 | |
| 6958 | if (IsO32) { |
| 6959 | CoerceToIntArgs(TySize, ArgList); |
| 6960 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6961 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6962 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6963 | if (Ty->isComplexType()) |
| 6964 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6965 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6966 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6967 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6968 | // Unions/vectors are passed in integer registers. |
| 6969 | if (!RT || !RT->isStructureOrClassType()) { |
| 6970 | CoerceToIntArgs(TySize, ArgList); |
| 6971 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6972 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6973 | |
| 6974 | const RecordDecl *RD = RT->getDecl(); |
| 6975 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6976 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6977 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6978 | uint64_t LastOffset = 0; |
| 6979 | unsigned idx = 0; |
| 6980 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6981 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6982 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6983 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6984 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6985 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6986 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6987 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6988 | |
| 6989 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6990 | continue; |
| 6991 | |
| 6992 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6993 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6994 | continue; |
| 6995 | |
| 6996 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6997 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6998 | ArgList.push_back(I64); |
| 6999 | |
| 7000 | // Add double type. |
| 7001 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 7002 | LastOffset = Offset + 64; |
| 7003 | } |
| 7004 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7005 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 7006 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7007 | |
| 7008 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7009 | } |
| 7010 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7011 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 7012 | uint64_t Offset) const { |
| 7013 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7014 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7015 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7016 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7017 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 7018 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7019 | ABIArgInfo |
| 7020 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 7021 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7022 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7023 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7024 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7025 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7026 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7027 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 7028 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7029 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 7030 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7031 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7032 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7033 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7034 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7035 | return ABIArgInfo::getIgnore(); |
| 7036 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 7037 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7038 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7039 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7040 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 7041 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7042 | // If we have reached here, aggregates are passed directly by coercing to |
| 7043 | // another structure type. Padding is inserted if the offset of the |
| 7044 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 7045 | ABIArgInfo ArgInfo = |
| 7046 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 7047 | getPaddingType(OrigOffset, CurrOffset)); |
| 7048 | ArgInfo.setInReg(true); |
| 7049 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
| 7052 | // Treat an enum type as its underlying type. |
| 7053 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7054 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7055 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 7056 | // All integral types are promoted to the GPR width. |
| 7057 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7058 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7059 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7060 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7061 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7062 | } |
| 7063 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7064 | llvm::Type* |
| 7065 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7066 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7067 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7068 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7069 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7070 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7071 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 7072 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7073 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7074 | // N32/64 returns struct/classes in floating point registers if the |
| 7075 | // following conditions are met: |
| 7076 | // 1. The size of the struct/class is no larger than 128-bit. |
| 7077 | // 2. The struct/class has one or two fields all of which are floating |
| 7078 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7079 | // 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] | 7080 | // |
| 7081 | // Any other composite results are returned in integer registers. |
| 7082 | // |
| 7083 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 7084 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 7085 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7086 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7087 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7088 | if (!BT || !BT->isFloatingPoint()) |
| 7089 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7090 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7091 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7092 | } |
| 7093 | |
| 7094 | if (b == e) |
| 7095 | return llvm::StructType::get(getVMContext(), RTList, |
| 7096 | RD->hasAttr<PackedAttr>()); |
| 7097 | |
| 7098 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7099 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7100 | } |
| 7101 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7102 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7103 | return llvm::StructType::get(getVMContext(), RTList); |
| 7104 | } |
| 7105 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7106 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 7107 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7108 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 7109 | if (RetTy->isVoidType()) |
| 7110 | return ABIArgInfo::getIgnore(); |
| 7111 | |
| 7112 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 7113 | // However, N32/N64 ignores zero sized return values. |
| 7114 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7115 | return ABIArgInfo::getIgnore(); |
| 7116 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 7117 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7118 | if (Size <= 128) { |
| 7119 | if (RetTy->isAnyComplexType()) |
| 7120 | return ABIArgInfo::getDirect(); |
| 7121 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7122 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 7123 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7124 | if (!IsO32 || |
| 7125 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 7126 | ABIArgInfo ArgInfo = |
| 7127 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 7128 | ArgInfo.setInReg(true); |
| 7129 | return ArgInfo; |
| 7130 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7131 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7132 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7133 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7134 | } |
| 7135 | |
| 7136 | // Treat an enum type as its underlying type. |
| 7137 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7138 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7139 | |
Stefan Maksimovic | b9da8a5 | 2018-07-30 10:44:46 +0000 | [diff] [blame] | 7140 | if (RetTy->isPromotableIntegerType()) |
| 7141 | return ABIArgInfo::getExtend(RetTy); |
| 7142 | |
| 7143 | if ((RetTy->isUnsignedIntegerOrEnumerationType() || |
| 7144 | RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) |
| 7145 | return ABIArgInfo::getSignExtend(RetTy); |
| 7146 | |
| 7147 | return ABIArgInfo::getDirect(); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
| 7150 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7151 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7152 | if (!getCXXABI().classifyReturnType(FI)) |
| 7153 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7154 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7155 | // 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] | 7156 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7157 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7158 | for (auto &I : FI.arguments()) |
| 7159 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7160 | } |
| 7161 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7162 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7163 | QualType OrigTy) const { |
| 7164 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7165 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7166 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 7167 | // 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] | 7168 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7169 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7170 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7171 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7172 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7173 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7174 | DidPromote = true; |
| 7175 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7176 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7177 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7178 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7179 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7180 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7181 | // The alignment of things in the argument area is never larger than |
| 7182 | // StackAlignInBytes. |
| 7183 | TyInfo.second = |
| 7184 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7185 | |
| 7186 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7187 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7188 | |
| 7189 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7190 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7191 | |
| 7192 | |
| 7193 | // If there was a promotion, "unpromote" into a temporary. |
| 7194 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7195 | if (DidPromote) { |
| 7196 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7197 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7198 | |
| 7199 | // Truncate down to the right width. |
| 7200 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7201 | : CGF.IntPtrTy); |
| 7202 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7203 | if (OrigTy->isPointerType()) |
| 7204 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7205 | |
| 7206 | CGF.Builder.CreateStore(V, Temp); |
| 7207 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7208 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7209 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7210 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7211 | } |
| 7212 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7213 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7214 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7215 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7216 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7217 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7218 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7219 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7220 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7221 | } |
| 7222 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7223 | bool |
| 7224 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7225 | llvm::Value *Address) const { |
| 7226 | // This information comes from gcc's implementation, which seems to |
| 7227 | // as canonical as it gets. |
| 7228 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7229 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7230 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7231 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7232 | |
| 7233 | // 0-31 are the general purpose registers, $0 - $31. |
| 7234 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7235 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7236 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7237 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7238 | |
| 7239 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7240 | // They are one bit wide and ignored here. |
| 7241 | |
| 7242 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7243 | // (coprocessor 1 is the FP unit) |
| 7244 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7245 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7246 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7247 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7248 | return false; |
| 7249 | } |
| 7250 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7251 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7252 | // AVR ABI Implementation. |
| 7253 | //===----------------------------------------------------------------------===// |
| 7254 | |
| 7255 | namespace { |
| 7256 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7257 | public: |
| 7258 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7259 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7260 | |
| 7261 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7262 | CodeGen::CodeGenModule &CGM) const override { |
| 7263 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7264 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7265 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7266 | if (!FD) return; |
| 7267 | auto *Fn = cast<llvm::Function>(GV); |
| 7268 | |
| 7269 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7270 | Fn->addFnAttr("interrupt"); |
| 7271 | |
| 7272 | if (FD->getAttr<AVRSignalAttr>()) |
| 7273 | Fn->addFnAttr("signal"); |
| 7274 | } |
| 7275 | }; |
| 7276 | } |
| 7277 | |
| 7278 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7279 | // 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] | 7280 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7281 | // handling. |
| 7282 | //===----------------------------------------------------------------------===// |
| 7283 | |
| 7284 | namespace { |
| 7285 | |
| 7286 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7287 | public: |
| 7288 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7289 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7290 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7291 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7292 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7293 | }; |
| 7294 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7295 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7296 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7297 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7298 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7299 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7300 | if (!FD) return; |
| 7301 | |
| 7302 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7303 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7304 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7305 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7306 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7307 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7308 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7309 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7310 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7311 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7312 | llvm::NamedMDNode *OpenCLMetadata = |
| 7313 | M.getModule().getOrInsertNamedMetadata( |
| 7314 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7315 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7316 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7317 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7318 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7319 | Operands.push_back( |
| 7320 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7321 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7322 | Operands.push_back( |
| 7323 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7324 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7325 | Operands.push_back( |
| 7326 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7327 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7328 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7329 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7330 | // (false) for implementing the work_group_size_hint attr later. |
| 7331 | // 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] | 7332 | Operands.push_back( |
| 7333 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7334 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7335 | } |
| 7336 | } |
| 7337 | } |
| 7338 | } |
| 7339 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7340 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7341 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7342 | //===----------------------------------------------------------------------===// |
| 7343 | // Hexagon ABI Implementation |
| 7344 | //===----------------------------------------------------------------------===// |
| 7345 | |
| 7346 | namespace { |
| 7347 | |
| 7348 | class HexagonABIInfo : public ABIInfo { |
| 7349 | |
| 7350 | |
| 7351 | public: |
| 7352 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7353 | |
| 7354 | private: |
| 7355 | |
| 7356 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7357 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7358 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7359 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7360 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7361 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7362 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7363 | }; |
| 7364 | |
| 7365 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7366 | public: |
| 7367 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7368 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7369 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7370 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7371 | return 29; |
| 7372 | } |
| 7373 | }; |
| 7374 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7375 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7376 | |
| 7377 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7378 | if (!getCXXABI().classifyReturnType(FI)) |
| 7379 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7380 | for (auto &I : FI.arguments()) |
| 7381 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7382 | } |
| 7383 | |
| 7384 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7385 | if (!isAggregateTypeForABI(Ty)) { |
| 7386 | // Treat an enum type as its underlying type. |
| 7387 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7388 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7389 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7390 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7391 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7392 | } |
| 7393 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7394 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7395 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7396 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7397 | // Ignore empty records. |
| 7398 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7399 | return ABIArgInfo::getIgnore(); |
| 7400 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7401 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7402 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7403 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7404 | // Pass in the smallest viable integer type. |
| 7405 | else if (Size > 32) |
| 7406 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7407 | else if (Size > 16) |
| 7408 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7409 | else if (Size > 8) |
| 7410 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7411 | else |
| 7412 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7413 | } |
| 7414 | |
| 7415 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7416 | if (RetTy->isVoidType()) |
| 7417 | return ABIArgInfo::getIgnore(); |
| 7418 | |
| 7419 | // Large vector types should be returned via memory. |
| 7420 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7421 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7422 | |
| 7423 | if (!isAggregateTypeForABI(RetTy)) { |
| 7424 | // Treat an enum type as its underlying type. |
| 7425 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7426 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7427 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7428 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7429 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7430 | } |
| 7431 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7432 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7433 | return ABIArgInfo::getIgnore(); |
| 7434 | |
| 7435 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7436 | // are returned indirectly. |
| 7437 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7438 | if (Size <= 64) { |
| 7439 | // Return in the smallest viable integer type. |
| 7440 | if (Size <= 8) |
| 7441 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7442 | if (Size <= 16) |
| 7443 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7444 | if (Size <= 32) |
| 7445 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7446 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7447 | } |
| 7448 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7449 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7450 | } |
| 7451 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7452 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7453 | QualType Ty) const { |
| 7454 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7455 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7456 | getContext().getTypeInfoInChars(Ty), |
| 7457 | CharUnits::fromQuantity(4), |
| 7458 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7459 | } |
| 7460 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7461 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7462 | // Lanai ABI Implementation |
| 7463 | //===----------------------------------------------------------------------===// |
| 7464 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7465 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7466 | class LanaiABIInfo : public DefaultABIInfo { |
| 7467 | public: |
| 7468 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7469 | |
| 7470 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7471 | |
| 7472 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7473 | CCState State(FI.getCallingConvention()); |
| 7474 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7475 | // regparm attribute set. |
| 7476 | if (FI.getHasRegParm()) { |
| 7477 | State.FreeRegs = FI.getRegParm(); |
| 7478 | } else { |
| 7479 | State.FreeRegs = 4; |
| 7480 | } |
| 7481 | |
| 7482 | if (!getCXXABI().classifyReturnType(FI)) |
| 7483 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7484 | for (auto &I : FI.arguments()) |
| 7485 | I.info = classifyArgumentType(I.type, State); |
| 7486 | } |
| 7487 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7488 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7489 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7490 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7491 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7492 | |
| 7493 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7494 | unsigned Size = getContext().getTypeSize(Ty); |
| 7495 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7496 | |
| 7497 | if (SizeInRegs == 0) |
| 7498 | return false; |
| 7499 | |
| 7500 | if (SizeInRegs > State.FreeRegs) { |
| 7501 | State.FreeRegs = 0; |
| 7502 | return false; |
| 7503 | } |
| 7504 | |
| 7505 | State.FreeRegs -= SizeInRegs; |
| 7506 | |
| 7507 | return true; |
| 7508 | } |
| 7509 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7510 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7511 | CCState &State) const { |
| 7512 | if (!ByVal) { |
| 7513 | if (State.FreeRegs) { |
| 7514 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7515 | return getNaturalAlignIndirectInReg(Ty); |
| 7516 | } |
| 7517 | return getNaturalAlignIndirect(Ty, false); |
| 7518 | } |
| 7519 | |
| 7520 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7521 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7522 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7523 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7524 | /*Realign=*/TypeAlign > |
| 7525 | MinABIStackAlignInBytes); |
| 7526 | } |
| 7527 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7528 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7529 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7530 | // Check with the C++ ABI first. |
| 7531 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7532 | if (RT) { |
| 7533 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7534 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7535 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7536 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7537 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7538 | } |
| 7539 | } |
| 7540 | |
| 7541 | if (isAggregateTypeForABI(Ty)) { |
| 7542 | // Structures with flexible arrays are always indirect. |
| 7543 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7544 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7545 | |
| 7546 | // Ignore empty structs/unions. |
| 7547 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7548 | return ABIArgInfo::getIgnore(); |
| 7549 | |
| 7550 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7551 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7552 | if (SizeInRegs <= State.FreeRegs) { |
| 7553 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7554 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7555 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7556 | State.FreeRegs -= SizeInRegs; |
| 7557 | return ABIArgInfo::getDirectInReg(Result); |
| 7558 | } else { |
| 7559 | State.FreeRegs = 0; |
| 7560 | } |
| 7561 | return getIndirectResult(Ty, true, State); |
| 7562 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7563 | |
| 7564 | // Treat an enum type as its underlying type. |
| 7565 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7566 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7567 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7568 | bool InReg = shouldUseInReg(Ty, State); |
| 7569 | if (Ty->isPromotableIntegerType()) { |
| 7570 | if (InReg) |
| 7571 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7572 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7573 | } |
| 7574 | if (InReg) |
| 7575 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7576 | return ABIArgInfo::getDirect(); |
| 7577 | } |
| 7578 | |
| 7579 | namespace { |
| 7580 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7581 | public: |
| 7582 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7583 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7584 | }; |
| 7585 | } |
| 7586 | |
| 7587 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7588 | // AMDGPU ABI Implementation |
| 7589 | //===----------------------------------------------------------------------===// |
| 7590 | |
| 7591 | namespace { |
| 7592 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7593 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7594 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7595 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7596 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7597 | unsigned numRegsForType(QualType Ty) const; |
| 7598 | |
| 7599 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7600 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7601 | uint64_t Members) const override; |
| 7602 | |
| 7603 | public: |
| 7604 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7605 | DefaultABIInfo(CGT) {} |
| 7606 | |
| 7607 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7608 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7609 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7610 | |
| 7611 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7612 | }; |
| 7613 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7614 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7615 | return true; |
| 7616 | } |
| 7617 | |
| 7618 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7619 | const Type *Base, uint64_t Members) const { |
| 7620 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7621 | |
| 7622 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7623 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7624 | } |
| 7625 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7626 | /// Estimate number of registers the type will use when passed in registers. |
| 7627 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7628 | unsigned NumRegs = 0; |
| 7629 | |
| 7630 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7631 | // Compute from the number of elements. The reported size is based on the |
| 7632 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7633 | QualType EltTy = VT->getElementType(); |
| 7634 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7635 | |
| 7636 | // 16-bit element vectors should be passed as packed. |
| 7637 | if (EltSize == 16) |
| 7638 | return (VT->getNumElements() + 1) / 2; |
| 7639 | |
| 7640 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7641 | return EltNumRegs * VT->getNumElements(); |
| 7642 | } |
| 7643 | |
| 7644 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7645 | const RecordDecl *RD = RT->getDecl(); |
| 7646 | assert(!RD->hasFlexibleArrayMember()); |
| 7647 | |
| 7648 | for (const FieldDecl *Field : RD->fields()) { |
| 7649 | QualType FieldTy = Field->getType(); |
| 7650 | NumRegs += numRegsForType(FieldTy); |
| 7651 | } |
| 7652 | |
| 7653 | return NumRegs; |
| 7654 | } |
| 7655 | |
| 7656 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7657 | } |
| 7658 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7659 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7660 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7661 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7662 | if (!getCXXABI().classifyReturnType(FI)) |
| 7663 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7664 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7665 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7666 | for (auto &Arg : FI.arguments()) { |
| 7667 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7668 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7669 | } else { |
| 7670 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7671 | } |
| 7672 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7673 | } |
| 7674 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7675 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7676 | if (isAggregateTypeForABI(RetTy)) { |
| 7677 | // Records with non-trivial destructors/copy-constructors should not be |
| 7678 | // returned by value. |
| 7679 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7680 | // Ignore empty structs/unions. |
| 7681 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7682 | return ABIArgInfo::getIgnore(); |
| 7683 | |
| 7684 | // Lower single-element structs to just return a regular value. |
| 7685 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7686 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7687 | |
| 7688 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7689 | const RecordDecl *RD = RT->getDecl(); |
| 7690 | if (RD->hasFlexibleArrayMember()) |
| 7691 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7692 | } |
| 7693 | |
| 7694 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7695 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7696 | if (Size <= 16) |
| 7697 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7698 | |
| 7699 | if (Size <= 32) |
| 7700 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7701 | |
| 7702 | if (Size <= 64) { |
| 7703 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7704 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7705 | } |
| 7706 | |
| 7707 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7708 | return ABIArgInfo::getDirect(); |
| 7709 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7710 | } |
| 7711 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7712 | // Otherwise just do the default thing. |
| 7713 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7714 | } |
| 7715 | |
| 7716 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7717 | /// make sense to pass anything byval, so everything must be direct. |
| 7718 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7719 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7720 | |
| 7721 | // TODO: Can we omit empty structs? |
| 7722 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7723 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7724 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7725 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7726 | |
| 7727 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7728 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7729 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7730 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7731 | } |
| 7732 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7733 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7734 | unsigned &NumRegsLeft) const { |
| 7735 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7736 | |
| 7737 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7738 | |
| 7739 | if (isAggregateTypeForABI(Ty)) { |
| 7740 | // Records with non-trivial destructors/copy-constructors should not be |
| 7741 | // passed by value. |
| 7742 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7743 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7744 | |
| 7745 | // Ignore empty structs/unions. |
| 7746 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7747 | return ABIArgInfo::getIgnore(); |
| 7748 | |
| 7749 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7750 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7751 | // though watch out for things like bitfields. |
| 7752 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7753 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7754 | |
| 7755 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7756 | const RecordDecl *RD = RT->getDecl(); |
| 7757 | if (RD->hasFlexibleArrayMember()) |
| 7758 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7759 | } |
| 7760 | |
| 7761 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7762 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7763 | if (Size <= 64) { |
| 7764 | unsigned NumRegs = (Size + 31) / 32; |
| 7765 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7766 | |
| 7767 | if (Size <= 16) |
| 7768 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7769 | |
| 7770 | if (Size <= 32) |
| 7771 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7772 | |
| 7773 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7774 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7775 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7776 | } |
| 7777 | |
| 7778 | if (NumRegsLeft > 0) { |
| 7779 | unsigned NumRegs = numRegsForType(Ty); |
| 7780 | if (NumRegsLeft >= NumRegs) { |
| 7781 | NumRegsLeft -= NumRegs; |
| 7782 | return ABIArgInfo::getDirect(); |
| 7783 | } |
| 7784 | } |
| 7785 | } |
| 7786 | |
| 7787 | // Otherwise just do the default thing. |
| 7788 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7789 | if (!ArgInfo.isIndirect()) { |
| 7790 | unsigned NumRegs = numRegsForType(Ty); |
| 7791 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7792 | } |
| 7793 | |
| 7794 | return ArgInfo; |
| 7795 | } |
| 7796 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7797 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7798 | public: |
| 7799 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7800 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7801 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7802 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7803 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7804 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7805 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7806 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7807 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7808 | LangAS getASTAllocaAddressSpace() const override { |
| 7809 | return getLangASFromTargetAS( |
| 7810 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7811 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7812 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7813 | const VarDecl *D) const override; |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 7814 | llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 7815 | SyncScope Scope, |
| 7816 | llvm::AtomicOrdering Ordering, |
| 7817 | llvm::LLVMContext &Ctx) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7818 | llvm::Function * |
| 7819 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7820 | llvm::Function *BlockInvokeFunc, |
| 7821 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7822 | bool shouldEmitStaticExternCAliases() const override; |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7823 | void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7824 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7825 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7826 | |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7827 | static bool requiresAMDGPUProtectedVisibility(const Decl *D, |
| 7828 | llvm::GlobalValue *GV) { |
| 7829 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 7830 | return false; |
| 7831 | |
| 7832 | return D->hasAttr<OpenCLKernelAttr>() || |
| 7833 | (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) || |
Michael Liao | 3820506 | 2019-04-26 19:31:48 +0000 | [diff] [blame] | 7834 | (isa<VarDecl>(D) && |
| 7835 | (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>())); |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7836 | } |
| 7837 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7838 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7839 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7840 | if (requiresAMDGPUProtectedVisibility(D, GV)) { |
| 7841 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 7842 | GV->setDSOLocal(true); |
| 7843 | } |
| 7844 | |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7845 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7846 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7847 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7848 | if (!FD) |
| 7849 | return; |
| 7850 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7851 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7852 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7853 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7854 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7855 | |
| 7856 | if (M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>() && |
| 7857 | (M.getTriple().getOS() == llvm::Triple::AMDHSA)) |
Tony Tye | 68e11a6 | 2018-03-23 18:51:45 +0000 | [diff] [blame] | 7858 | F->addFnAttr("amdgpu-implicitarg-num-bytes", "48"); |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7859 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7860 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7861 | if (ReqdWGS || FlatWGS) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 7862 | unsigned Min = 0; |
| 7863 | unsigned Max = 0; |
| 7864 | if (FlatWGS) { |
| 7865 | Min = FlatWGS->getMin() |
| 7866 | ->EvaluateKnownConstInt(M.getContext()) |
| 7867 | .getExtValue(); |
| 7868 | Max = FlatWGS->getMax() |
| 7869 | ->EvaluateKnownConstInt(M.getContext()) |
| 7870 | .getExtValue(); |
| 7871 | } |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7872 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7873 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7874 | |
| 7875 | if (Min != 0) { |
| 7876 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7877 | |
| 7878 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7879 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7880 | } else |
| 7881 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7882 | } |
| 7883 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7884 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 7885 | unsigned Min = |
| 7886 | Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue(); |
| 7887 | unsigned Max = Attr->getMax() ? Attr->getMax() |
| 7888 | ->EvaluateKnownConstInt(M.getContext()) |
| 7889 | .getExtValue() |
| 7890 | : 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7891 | |
| 7892 | if (Min != 0) { |
| 7893 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7894 | |
| 7895 | std::string AttrVal = llvm::utostr(Min); |
| 7896 | if (Max != 0) |
| 7897 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7898 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7899 | } else |
| 7900 | assert(Max == 0 && "Max must be zero"); |
| 7901 | } |
| 7902 | |
| 7903 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7904 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7905 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7906 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7907 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7908 | } |
| 7909 | |
| 7910 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7911 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7912 | |
| 7913 | if (NumVGPR != 0) |
| 7914 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7915 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7916 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7917 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7918 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7919 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7920 | } |
| 7921 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7922 | // Currently LLVM assumes null pointers always have value 0, |
| 7923 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7924 | // emitting null pointers in private and local address spaces, a null |
| 7925 | // pointer in generic address space is emitted which is casted to a |
| 7926 | // pointer in local or private address space. |
| 7927 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7928 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7929 | QualType QT) const { |
| 7930 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7931 | return llvm::ConstantPointerNull::get(PT); |
| 7932 | |
| 7933 | auto &Ctx = CGM.getContext(); |
| 7934 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7935 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7936 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7937 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7938 | } |
| 7939 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7940 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7941 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7942 | const VarDecl *D) const { |
| 7943 | assert(!CGM.getLangOpts().OpenCL && |
| 7944 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7945 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7946 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 7947 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7948 | if (!D) |
| 7949 | return DefaultGlobalAS; |
| 7950 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7951 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 7952 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7953 | if (AddrSpace != LangAS::Default) |
| 7954 | return AddrSpace; |
| 7955 | |
| 7956 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7957 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7958 | return ConstAS.getValue(); |
| 7959 | } |
| 7960 | return DefaultGlobalAS; |
| 7961 | } |
| 7962 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7963 | llvm::SyncScope::ID |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 7964 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 7965 | SyncScope Scope, |
| 7966 | llvm::AtomicOrdering Ordering, |
| 7967 | llvm::LLVMContext &Ctx) const { |
| 7968 | std::string Name; |
| 7969 | switch (Scope) { |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7970 | case SyncScope::OpenCLWorkGroup: |
| 7971 | Name = "workgroup"; |
| 7972 | break; |
| 7973 | case SyncScope::OpenCLDevice: |
| 7974 | Name = "agent"; |
| 7975 | break; |
| 7976 | case SyncScope::OpenCLAllSVMDevices: |
| 7977 | Name = ""; |
| 7978 | break; |
| 7979 | case SyncScope::OpenCLSubGroup: |
Konstantin Zhuravlyov | 3161c89 | 2019-03-06 20:54:48 +0000 | [diff] [blame] | 7980 | Name = "wavefront"; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7981 | } |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 7982 | |
| 7983 | if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) { |
| 7984 | if (!Name.empty()) |
| 7985 | Name = Twine(Twine(Name) + Twine("-")).str(); |
| 7986 | |
| 7987 | Name = Twine(Twine(Name) + Twine("one-as")).str(); |
| 7988 | } |
| 7989 | |
| 7990 | return Ctx.getOrInsertSyncScopeID(Name); |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7993 | bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 7994 | return false; |
| 7995 | } |
| 7996 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 7997 | void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7998 | const FunctionType *&FT) const { |
| 7999 | FT = getABIInfo().getContext().adjustFunctionType( |
| 8000 | FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 8001 | } |
| 8002 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8003 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8004 | // SPARC v8 ABI Implementation. |
| 8005 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8006 | // |
| 8007 | // Ensures that complex values are passed in registers. |
| 8008 | // |
| 8009 | namespace { |
| 8010 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 8011 | public: |
| 8012 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 8013 | |
| 8014 | private: |
| 8015 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8016 | void computeInfo(CGFunctionInfo &FI) const override; |
| 8017 | }; |
| 8018 | } // end anonymous namespace |
| 8019 | |
| 8020 | |
| 8021 | ABIArgInfo |
| 8022 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 8023 | if (Ty->isAnyComplexType()) { |
| 8024 | return ABIArgInfo::getDirect(); |
| 8025 | } |
| 8026 | else { |
| 8027 | return DefaultABIInfo::classifyReturnType(Ty); |
| 8028 | } |
| 8029 | } |
| 8030 | |
| 8031 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8032 | |
| 8033 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8034 | for (auto &Arg : FI.arguments()) |
| 8035 | Arg.info = classifyArgumentType(Arg.type); |
| 8036 | } |
| 8037 | |
| 8038 | namespace { |
| 8039 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8040 | public: |
| 8041 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8042 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 8043 | }; |
| 8044 | } // end anonymous namespace |
| 8045 | |
| 8046 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8047 | // SPARC v9 ABI Implementation. |
| 8048 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8049 | // |
| 8050 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 8051 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 8052 | // the array, structs larger than 16 bytes are passed indirectly. |
| 8053 | // |
| 8054 | // One case requires special care: |
| 8055 | // |
| 8056 | // struct mixed { |
| 8057 | // int i; |
| 8058 | // float f; |
| 8059 | // }; |
| 8060 | // |
| 8061 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 8062 | // parameter array, but the int is passed in an integer register, and the float |
| 8063 | // is passed in a floating point register. This is represented as two arguments |
| 8064 | // with the LLVM IR inreg attribute: |
| 8065 | // |
| 8066 | // declare void f(i32 inreg %i, float inreg %f) |
| 8067 | // |
| 8068 | // The code generator will only allocate 4 bytes from the parameter array for |
| 8069 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 8070 | // bytes. |
| 8071 | // |
| 8072 | namespace { |
| 8073 | class SparcV9ABIInfo : public ABIInfo { |
| 8074 | public: |
| 8075 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 8076 | |
| 8077 | private: |
| 8078 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8079 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8080 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8081 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8082 | |
| 8083 | // Coercion type builder for structs passed in registers. The coercion type |
| 8084 | // serves two purposes: |
| 8085 | // |
| 8086 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 8087 | // in registers. |
| 8088 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 8089 | // code generator knows to pass them in floating point registers. |
| 8090 | // |
| 8091 | // We also compute the InReg flag which indicates that the struct contains |
| 8092 | // aligned 32-bit floats. |
| 8093 | // |
| 8094 | struct CoerceBuilder { |
| 8095 | llvm::LLVMContext &Context; |
| 8096 | const llvm::DataLayout &DL; |
| 8097 | SmallVector<llvm::Type*, 8> Elems; |
| 8098 | uint64_t Size; |
| 8099 | bool InReg; |
| 8100 | |
| 8101 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 8102 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 8103 | |
| 8104 | // Pad Elems with integers until Size is ToSize. |
| 8105 | void pad(uint64_t ToSize) { |
| 8106 | assert(ToSize >= Size && "Cannot remove elements"); |
| 8107 | if (ToSize == Size) |
| 8108 | return; |
| 8109 | |
| 8110 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8111 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8112 | if (Aligned > Size && Aligned <= ToSize) { |
| 8113 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 8114 | Size = Aligned; |
| 8115 | } |
| 8116 | |
| 8117 | // Add whole 64-bit words. |
| 8118 | while (Size + 64 <= ToSize) { |
| 8119 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 8120 | Size += 64; |
| 8121 | } |
| 8122 | |
| 8123 | // Final in-word padding. |
| 8124 | if (Size < ToSize) { |
| 8125 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 8126 | Size = ToSize; |
| 8127 | } |
| 8128 | } |
| 8129 | |
| 8130 | // Add a floating point element at Offset. |
| 8131 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 8132 | // Unaligned floats are treated as integers. |
| 8133 | if (Offset % Bits) |
| 8134 | return; |
| 8135 | // The InReg flag is only required if there are any floats < 64 bits. |
| 8136 | if (Bits < 64) |
| 8137 | InReg = true; |
| 8138 | pad(Offset); |
| 8139 | Elems.push_back(Ty); |
| 8140 | Size = Offset + Bits; |
| 8141 | } |
| 8142 | |
| 8143 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 8144 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 8145 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 8146 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 8147 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 8148 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 8149 | switch (ElemTy->getTypeID()) { |
| 8150 | case llvm::Type::StructTyID: |
| 8151 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 8152 | break; |
| 8153 | case llvm::Type::FloatTyID: |
| 8154 | addFloat(ElemOffset, ElemTy, 32); |
| 8155 | break; |
| 8156 | case llvm::Type::DoubleTyID: |
| 8157 | addFloat(ElemOffset, ElemTy, 64); |
| 8158 | break; |
| 8159 | case llvm::Type::FP128TyID: |
| 8160 | addFloat(ElemOffset, ElemTy, 128); |
| 8161 | break; |
| 8162 | case llvm::Type::PointerTyID: |
| 8163 | if (ElemOffset % 64 == 0) { |
| 8164 | pad(ElemOffset); |
| 8165 | Elems.push_back(ElemTy); |
| 8166 | Size += 64; |
| 8167 | } |
| 8168 | break; |
| 8169 | default: |
| 8170 | break; |
| 8171 | } |
| 8172 | } |
| 8173 | } |
| 8174 | |
| 8175 | // Check if Ty is a usable substitute for the coercion type. |
| 8176 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 8177 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8178 | } |
| 8179 | |
| 8180 | // Get the coercion type as a literal struct type. |
| 8181 | llvm::Type *getType() const { |
| 8182 | if (Elems.size() == 1) |
| 8183 | return Elems.front(); |
| 8184 | else |
| 8185 | return llvm::StructType::get(Context, Elems); |
| 8186 | } |
| 8187 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8188 | }; |
| 8189 | } // end anonymous namespace |
| 8190 | |
| 8191 | ABIArgInfo |
| 8192 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 8193 | if (Ty->isVoidType()) |
| 8194 | return ABIArgInfo::getIgnore(); |
| 8195 | |
| 8196 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8197 | |
| 8198 | // Anything too big to fit in registers is passed with an explicit indirect |
| 8199 | // pointer / sret pointer. |
| 8200 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8201 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8202 | |
| 8203 | // Treat an enum type as its underlying type. |
| 8204 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8205 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8206 | |
| 8207 | // Integer types smaller than a register are extended. |
| 8208 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 8209 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8210 | |
| 8211 | // Other non-aggregates go in registers. |
| 8212 | if (!isAggregateTypeForABI(Ty)) |
| 8213 | return ABIArgInfo::getDirect(); |
| 8214 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8215 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 8216 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 8217 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8218 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8219 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8220 | // 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] | 8221 | // Build a coercion type from the LLVM struct type. |
| 8222 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 8223 | if (!StrTy) |
| 8224 | return ABIArgInfo::getDirect(); |
| 8225 | |
| 8226 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 8227 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8228 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8229 | |
| 8230 | // Try to use the original type for coercion. |
| 8231 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8232 | |
| 8233 | if (CB.InReg) |
| 8234 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8235 | else |
| 8236 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8237 | } |
| 8238 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8239 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8240 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8241 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8242 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8243 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8244 | AI.setCoerceToType(ArgTy); |
| 8245 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8246 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8247 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8248 | CGBuilderTy &Builder = CGF.Builder; |
| 8249 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8250 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8251 | |
| 8252 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8253 | |
| 8254 | Address ArgAddr = Address::invalid(); |
| 8255 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8256 | switch (AI.getKind()) { |
| 8257 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8258 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8259 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8260 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8261 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8262 | case ABIArgInfo::Extend: { |
| 8263 | Stride = SlotSize; |
| 8264 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8265 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8266 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8267 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8268 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8269 | case ABIArgInfo::Direct: { |
| 8270 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8271 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8272 | ArgAddr = Addr; |
| 8273 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8274 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8275 | |
| 8276 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8277 | Stride = SlotSize; |
| 8278 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8279 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8280 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8281 | break; |
| 8282 | |
| 8283 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8284 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8285 | } |
| 8286 | |
| 8287 | // Update VAList. |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8288 | Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); |
| 8289 | Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8290 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8291 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8292 | } |
| 8293 | |
| 8294 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8295 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8296 | for (auto &I : FI.arguments()) |
| 8297 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8298 | } |
| 8299 | |
| 8300 | namespace { |
| 8301 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8302 | public: |
| 8303 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8304 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8305 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8306 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8307 | return 14; |
| 8308 | } |
| 8309 | |
| 8310 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8311 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8312 | }; |
| 8313 | } // end anonymous namespace |
| 8314 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8315 | bool |
| 8316 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8317 | llvm::Value *Address) const { |
| 8318 | // This is calculated from the LLVM and GCC tables and verified |
| 8319 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8320 | |
| 8321 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8322 | |
| 8323 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8324 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8325 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8326 | |
| 8327 | // 0-31: the 8-byte general-purpose registers |
| 8328 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8329 | |
| 8330 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8331 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8332 | |
| 8333 | // Y = 64 |
| 8334 | // PSR = 65 |
| 8335 | // WIM = 66 |
| 8336 | // TBR = 67 |
| 8337 | // PC = 68 |
| 8338 | // NPC = 69 |
| 8339 | // FSR = 70 |
| 8340 | // CSR = 71 |
| 8341 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8342 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8343 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8344 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8345 | |
| 8346 | return false; |
| 8347 | } |
| 8348 | |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8349 | // ARC ABI implementation. |
| 8350 | namespace { |
| 8351 | |
| 8352 | class ARCABIInfo : public DefaultABIInfo { |
| 8353 | public: |
| 8354 | using DefaultABIInfo::DefaultABIInfo; |
| 8355 | |
| 8356 | private: |
| 8357 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8358 | QualType Ty) const override; |
| 8359 | |
| 8360 | void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { |
| 8361 | if (!State.FreeRegs) |
| 8362 | return; |
| 8363 | if (Info.isIndirect() && Info.getInReg()) |
| 8364 | State.FreeRegs--; |
| 8365 | else if (Info.isDirect() && Info.getInReg()) { |
| 8366 | unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; |
| 8367 | if (sz < State.FreeRegs) |
| 8368 | State.FreeRegs -= sz; |
| 8369 | else |
| 8370 | State.FreeRegs = 0; |
| 8371 | } |
| 8372 | } |
| 8373 | |
| 8374 | void computeInfo(CGFunctionInfo &FI) const override { |
| 8375 | CCState State(FI.getCallingConvention()); |
| 8376 | // ARC uses 8 registers to pass arguments. |
| 8377 | State.FreeRegs = 8; |
| 8378 | |
| 8379 | if (!getCXXABI().classifyReturnType(FI)) |
| 8380 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8381 | updateState(FI.getReturnInfo(), FI.getReturnType(), State); |
| 8382 | for (auto &I : FI.arguments()) { |
| 8383 | I.info = classifyArgumentType(I.type, State.FreeRegs); |
| 8384 | updateState(I.info, I.type, State); |
| 8385 | } |
| 8386 | } |
| 8387 | |
| 8388 | ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; |
| 8389 | ABIArgInfo getIndirectByValue(QualType Ty) const; |
| 8390 | ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; |
| 8391 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8392 | }; |
| 8393 | |
| 8394 | class ARCTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8395 | public: |
| 8396 | ARCTargetCodeGenInfo(CodeGenTypes &CGT) |
| 8397 | : TargetCodeGenInfo(new ARCABIInfo(CGT)) {} |
| 8398 | }; |
| 8399 | |
| 8400 | |
| 8401 | ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { |
| 8402 | return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : |
| 8403 | getNaturalAlignIndirect(Ty, false); |
| 8404 | } |
| 8405 | |
| 8406 | ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8407 | // Compute the byval alignment. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8408 | const unsigned MinABIStackAlignInBytes = 4; |
| 8409 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 8410 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 8411 | TypeAlign > MinABIStackAlignInBytes); |
| 8412 | } |
| 8413 | |
| 8414 | Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8415 | QualType Ty) const { |
| 8416 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 8417 | getContext().getTypeInfoInChars(Ty), |
| 8418 | CharUnits::fromQuantity(4), true); |
| 8419 | } |
| 8420 | |
| 8421 | ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, |
| 8422 | uint8_t FreeRegs) const { |
| 8423 | // Handle the generic C++ ABI. |
| 8424 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 8425 | if (RT) { |
| 8426 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 8427 | if (RAA == CGCXXABI::RAA_Indirect) |
| 8428 | return getIndirectByRef(Ty, FreeRegs > 0); |
| 8429 | |
| 8430 | if (RAA == CGCXXABI::RAA_DirectInMemory) |
| 8431 | return getIndirectByValue(Ty); |
| 8432 | } |
| 8433 | |
| 8434 | // Treat an enum type as its underlying type. |
| 8435 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8436 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8437 | |
| 8438 | auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; |
| 8439 | |
| 8440 | if (isAggregateTypeForABI(Ty)) { |
| 8441 | // Structures with flexible arrays are always indirect. |
| 8442 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 8443 | return getIndirectByValue(Ty); |
| 8444 | |
| 8445 | // Ignore empty structs/unions. |
| 8446 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8447 | return ABIArgInfo::getIgnore(); |
| 8448 | |
| 8449 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 8450 | |
| 8451 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 8452 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 8453 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 8454 | |
| 8455 | return FreeRegs >= SizeInRegs ? |
| 8456 | ABIArgInfo::getDirectInReg(Result) : |
| 8457 | ABIArgInfo::getDirect(Result, 0, nullptr, false); |
| 8458 | } |
| 8459 | |
| 8460 | return Ty->isPromotableIntegerType() ? |
| 8461 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) : |
| 8462 | ABIArgInfo::getExtend(Ty)) : |
| 8463 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() : |
| 8464 | ABIArgInfo::getDirect()); |
| 8465 | } |
| 8466 | |
| 8467 | ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { |
| 8468 | if (RetTy->isAnyComplexType()) |
| 8469 | return ABIArgInfo::getDirectInReg(); |
| 8470 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8471 | // Arguments of size > 4 registers are indirect. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8472 | auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; |
| 8473 | if (RetSize > 4) |
| 8474 | return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); |
| 8475 | |
| 8476 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 8477 | } |
| 8478 | |
| 8479 | } // End anonymous namespace. |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8480 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8481 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8482 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8483 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8484 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8485 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8486 | |
| 8487 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8488 | /// it by reference between functions that append to it. |
| 8489 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8490 | |
| 8491 | /// TypeStringCache caches the meta encodings of Types. |
| 8492 | /// |
| 8493 | /// The reason for caching TypeStrings is two fold: |
| 8494 | /// 1. To cache a type's encoding for later uses; |
| 8495 | /// 2. As a means to break recursive member type inclusion. |
| 8496 | /// |
| 8497 | /// A cache Entry can have a Status of: |
| 8498 | /// NonRecursive: The type encoding is not recursive; |
| 8499 | /// Recursive: The type encoding is recursive; |
| 8500 | /// Incomplete: An incomplete TypeString; |
| 8501 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8502 | /// Recursive type encoding. |
| 8503 | /// |
| 8504 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8505 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8506 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8507 | /// the type is encountered. |
| 8508 | /// |
| 8509 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8510 | /// possible. The type itself is recursive and it may contain other types which |
| 8511 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8512 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8513 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8514 | /// |
| 8515 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8516 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8517 | /// are placed into the cache during type expansion as a means to identify and |
| 8518 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8519 | /// the entry becomes IncompleteUsed. |
| 8520 | /// |
| 8521 | /// During the expansion of a RecordType's members: |
| 8522 | /// |
| 8523 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8524 | /// cached encoding is used; |
| 8525 | /// |
| 8526 | /// If the cache contains a Recursive encoding for the member type, the |
| 8527 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8528 | /// |
| 8529 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8530 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8531 | /// |
| 8532 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8533 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8534 | /// it is swapped back in; |
| 8535 | /// |
| 8536 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8537 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8538 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8539 | /// |
| 8540 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8541 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8542 | /// Else the member is part of a recursive type and thus the recursion has |
| 8543 | /// been exited too soon for the encoding to be correct for the member. |
| 8544 | /// |
| 8545 | class TypeStringCache { |
| 8546 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8547 | struct Entry { |
| 8548 | std::string Str; // The encoded TypeString for the type. |
| 8549 | enum Status State; // Information about the encoding in 'Str'. |
| 8550 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8551 | // during the expansion of RecordType's members. |
| 8552 | }; |
| 8553 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8554 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8555 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8556 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8557 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8558 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8559 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8560 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8561 | bool IsRecursive); |
| 8562 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8563 | }; |
| 8564 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8565 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8566 | /// FieldEncoding is a helper for this ordering process. |
| 8567 | class FieldEncoding { |
| 8568 | bool HasName; |
| 8569 | std::string Enc; |
| 8570 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8571 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8572 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8573 | bool operator<(const FieldEncoding &rhs) const { |
| 8574 | if (HasName != rhs.HasName) return HasName; |
| 8575 | return Enc < rhs.Enc; |
| 8576 | } |
| 8577 | }; |
| 8578 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8579 | class XCoreABIInfo : public DefaultABIInfo { |
| 8580 | public: |
| 8581 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8582 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8583 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8584 | }; |
| 8585 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8586 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8587 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8588 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8589 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8590 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8591 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8592 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8593 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8594 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8595 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8596 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8597 | // TODO: this implementation is likely now redundant with the default |
| 8598 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8599 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8600 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8601 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8602 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8603 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8604 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8605 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8606 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8607 | // Handle the argument. |
| 8608 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8609 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8610 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8611 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8612 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8613 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8614 | |
| 8615 | Address Val = Address::invalid(); |
| 8616 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8617 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8618 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8619 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8620 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8621 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8622 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8623 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8624 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8625 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8626 | case ABIArgInfo::Extend: |
| 8627 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8628 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8629 | ArgSize = CharUnits::fromQuantity( |
| 8630 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8631 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8632 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8633 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8634 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8635 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8636 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8637 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8638 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8639 | |
| 8640 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8641 | if (!ArgSize.isZero()) { |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8642 | Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize); |
| 8643 | Builder.CreateStore(APN.getPointer(), VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8644 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8645 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8646 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8647 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8648 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8649 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8650 | /// into the cache as a means to identify and break recursion. |
| 8651 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8652 | /// be reinserted by removeIncomplete(). |
| 8653 | /// All other types of encoding should have been used rather than arriving here. |
| 8654 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8655 | std::string StubEnc) { |
| 8656 | if (!ID) |
| 8657 | return; |
| 8658 | Entry &E = Map[ID]; |
| 8659 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8660 | "Incorrectly use of addIncomplete"); |
| 8661 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8662 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8663 | E.Str.swap(StubEnc); |
| 8664 | E.State = Incomplete; |
| 8665 | ++IncompleteCount; |
| 8666 | } |
| 8667 | |
| 8668 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8669 | /// must be removed from the cache. |
| 8670 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8671 | /// Returns true if the RecordType was defined recursively. |
| 8672 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8673 | if (!ID) |
| 8674 | return false; |
| 8675 | auto I = Map.find(ID); |
| 8676 | assert(I != Map.end() && "Entry not present"); |
| 8677 | Entry &E = I->second; |
| 8678 | assert( (E.State == Incomplete || |
| 8679 | E.State == IncompleteUsed) && |
| 8680 | "Entry must be an incomplete type"); |
| 8681 | bool IsRecursive = false; |
| 8682 | if (E.State == IncompleteUsed) { |
| 8683 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8684 | IsRecursive = true; |
| 8685 | --IncompleteUsedCount; |
| 8686 | } |
| 8687 | if (E.Swapped.empty()) |
| 8688 | Map.erase(I); |
| 8689 | else { |
| 8690 | // Swap the Recursive back. |
| 8691 | E.Swapped.swap(E.Str); |
| 8692 | E.Swapped.clear(); |
| 8693 | E.State = Recursive; |
| 8694 | } |
| 8695 | --IncompleteCount; |
| 8696 | return IsRecursive; |
| 8697 | } |
| 8698 | |
| 8699 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8700 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8701 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8702 | bool IsRecursive) { |
| 8703 | if (!ID || IncompleteUsedCount) |
| 8704 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8705 | Entry &E = Map[ID]; |
| 8706 | if (IsRecursive && !E.Str.empty()) { |
| 8707 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8708 | "This is not the same Recursive entry"); |
| 8709 | // The parent container was not recursive after all, so we could have used |
| 8710 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8711 | // we started viz: IncompleteCount!=0. |
| 8712 | return; |
| 8713 | } |
| 8714 | assert(E.Str.empty() && "Entry already present"); |
| 8715 | E.Str = Str.str(); |
| 8716 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8717 | } |
| 8718 | |
| 8719 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8720 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8721 | /// encoding is Recursive, return an empty StringRef. |
| 8722 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8723 | if (!ID) |
| 8724 | return StringRef(); // We have no key. |
| 8725 | auto I = Map.find(ID); |
| 8726 | if (I == Map.end()) |
| 8727 | return StringRef(); // We have no encoding. |
| 8728 | Entry &E = I->second; |
| 8729 | if (E.State == Recursive && IncompleteCount) |
| 8730 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8731 | |
| 8732 | if (E.State == Incomplete) { |
| 8733 | // The incomplete type is being used to break out of recursion. |
| 8734 | E.State = IncompleteUsed; |
| 8735 | ++IncompleteUsedCount; |
| 8736 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8737 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8738 | } |
| 8739 | |
| 8740 | /// The XCore ABI includes a type information section that communicates symbol |
| 8741 | /// type information to the linker. The linker uses this information to verify |
| 8742 | /// safety/correctness of things such as array bound and pointers et al. |
| 8743 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8744 | /// This type information (TypeString) is emitted into meta data for all global |
| 8745 | /// symbols: definitions, declarations, functions & variables. |
| 8746 | /// |
| 8747 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8748 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8749 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8750 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8751 | /// |
| 8752 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8753 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8754 | |
| 8755 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8756 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8757 | CodeGen::CodeGenModule &CGM) const { |
| 8758 | SmallStringEnc Enc; |
| 8759 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8760 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8761 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8762 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8763 | llvm::NamedMDNode *MD = |
| 8764 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8765 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8766 | } |
| 8767 | } |
| 8768 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8769 | //===----------------------------------------------------------------------===// |
| 8770 | // SPIR ABI Implementation |
| 8771 | //===----------------------------------------------------------------------===// |
| 8772 | |
| 8773 | namespace { |
| 8774 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8775 | public: |
| 8776 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8777 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8778 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8779 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8780 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8781 | } // End anonymous namespace. |
| 8782 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8783 | namespace clang { |
| 8784 | namespace CodeGen { |
| 8785 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8786 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8787 | SPIRABI.computeInfo(FI); |
| 8788 | } |
| 8789 | } |
| 8790 | } |
| 8791 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8792 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8793 | return llvm::CallingConv::SPIR_KERNEL; |
| 8794 | } |
| 8795 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8796 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8797 | const CodeGen::CodeGenModule &CGM, |
| 8798 | TypeStringCache &TSC); |
| 8799 | |
| 8800 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8801 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8802 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8803 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8804 | const RecordDecl *RD, |
| 8805 | const CodeGen::CodeGenModule &CGM, |
| 8806 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8807 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8808 | SmallStringEnc Enc; |
| 8809 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8810 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8811 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8812 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8813 | Enc += "b("; |
| 8814 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8815 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8816 | Enc += ':'; |
| 8817 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8818 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8819 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8820 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8821 | Enc += ')'; |
| 8822 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8823 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8824 | } |
| 8825 | return true; |
| 8826 | } |
| 8827 | |
| 8828 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8829 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8830 | /// Union types have their fields ordered according to the ABI. |
| 8831 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8832 | const CodeGen::CodeGenModule &CGM, |
| 8833 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8834 | // Append the cached TypeString if we have one. |
| 8835 | StringRef TypeString = TSC.lookupStr(ID); |
| 8836 | if (!TypeString.empty()) { |
| 8837 | Enc += TypeString; |
| 8838 | return true; |
| 8839 | } |
| 8840 | |
| 8841 | // Start to emit an incomplete TypeString. |
| 8842 | size_t Start = Enc.size(); |
| 8843 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8844 | Enc += '('; |
| 8845 | if (ID) |
| 8846 | Enc += ID->getName(); |
| 8847 | Enc += "){"; |
| 8848 | |
| 8849 | // We collect all encoded fields and order as necessary. |
| 8850 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8851 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8852 | if (RD && !RD->field_empty()) { |
| 8853 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8854 | // so that recursive calls to this RecordType will use it whilst building a |
| 8855 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8856 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8857 | std::string StubEnc(Enc.substr(Start).str()); |
| 8858 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8859 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8860 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8861 | (void) TSC.removeIncomplete(ID); |
| 8862 | return false; |
| 8863 | } |
| 8864 | IsRecursive = TSC.removeIncomplete(ID); |
| 8865 | // The ABI requires unions to be sorted but not structures. |
| 8866 | // See FieldEncoding::operator< for sort algorithm. |
| 8867 | if (RT->isUnionType()) |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8868 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8869 | // We can now complete the TypeString. |
| 8870 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8871 | for (unsigned I = 0; I != E; ++I) { |
| 8872 | if (I) |
| 8873 | Enc += ','; |
| 8874 | Enc += FE[I].str(); |
| 8875 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8876 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8877 | Enc += '}'; |
| 8878 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8879 | return true; |
| 8880 | } |
| 8881 | |
| 8882 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8883 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8884 | TypeStringCache &TSC, |
| 8885 | const IdentifierInfo *ID) { |
| 8886 | // Append the cached TypeString if we have one. |
| 8887 | StringRef TypeString = TSC.lookupStr(ID); |
| 8888 | if (!TypeString.empty()) { |
| 8889 | Enc += TypeString; |
| 8890 | return true; |
| 8891 | } |
| 8892 | |
| 8893 | size_t Start = Enc.size(); |
| 8894 | Enc += "e("; |
| 8895 | if (ID) |
| 8896 | Enc += ID->getName(); |
| 8897 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8898 | |
| 8899 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8900 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8901 | SmallVector<FieldEncoding, 16> FE; |
| 8902 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8903 | ++I) { |
| 8904 | SmallStringEnc EnumEnc; |
| 8905 | EnumEnc += "m("; |
| 8906 | EnumEnc += I->getName(); |
| 8907 | EnumEnc += "){"; |
| 8908 | I->getInitVal().toString(EnumEnc); |
| 8909 | EnumEnc += '}'; |
| 8910 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8911 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8912 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8913 | unsigned E = FE.size(); |
| 8914 | for (unsigned I = 0; I != E; ++I) { |
| 8915 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8916 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8917 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8918 | } |
| 8919 | } |
| 8920 | Enc += '}'; |
| 8921 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8922 | return true; |
| 8923 | } |
| 8924 | |
| 8925 | /// Appends type's qualifier to Enc. |
| 8926 | /// This is done prior to appending the type's encoding. |
| 8927 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8928 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8929 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8930 | int Lookup = 0; |
| 8931 | if (QT.isConstQualified()) |
| 8932 | Lookup += 1<<0; |
| 8933 | if (QT.isRestrictQualified()) |
| 8934 | Lookup += 1<<1; |
| 8935 | if (QT.isVolatileQualified()) |
| 8936 | Lookup += 1<<2; |
| 8937 | Enc += Table[Lookup]; |
| 8938 | } |
| 8939 | |
| 8940 | /// Appends built-in types to Enc. |
| 8941 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8942 | const char *EncType; |
| 8943 | switch (BT->getKind()) { |
| 8944 | case BuiltinType::Void: |
| 8945 | EncType = "0"; |
| 8946 | break; |
| 8947 | case BuiltinType::Bool: |
| 8948 | EncType = "b"; |
| 8949 | break; |
| 8950 | case BuiltinType::Char_U: |
| 8951 | EncType = "uc"; |
| 8952 | break; |
| 8953 | case BuiltinType::UChar: |
| 8954 | EncType = "uc"; |
| 8955 | break; |
| 8956 | case BuiltinType::SChar: |
| 8957 | EncType = "sc"; |
| 8958 | break; |
| 8959 | case BuiltinType::UShort: |
| 8960 | EncType = "us"; |
| 8961 | break; |
| 8962 | case BuiltinType::Short: |
| 8963 | EncType = "ss"; |
| 8964 | break; |
| 8965 | case BuiltinType::UInt: |
| 8966 | EncType = "ui"; |
| 8967 | break; |
| 8968 | case BuiltinType::Int: |
| 8969 | EncType = "si"; |
| 8970 | break; |
| 8971 | case BuiltinType::ULong: |
| 8972 | EncType = "ul"; |
| 8973 | break; |
| 8974 | case BuiltinType::Long: |
| 8975 | EncType = "sl"; |
| 8976 | break; |
| 8977 | case BuiltinType::ULongLong: |
| 8978 | EncType = "ull"; |
| 8979 | break; |
| 8980 | case BuiltinType::LongLong: |
| 8981 | EncType = "sll"; |
| 8982 | break; |
| 8983 | case BuiltinType::Float: |
| 8984 | EncType = "ft"; |
| 8985 | break; |
| 8986 | case BuiltinType::Double: |
| 8987 | EncType = "d"; |
| 8988 | break; |
| 8989 | case BuiltinType::LongDouble: |
| 8990 | EncType = "ld"; |
| 8991 | break; |
| 8992 | default: |
| 8993 | return false; |
| 8994 | } |
| 8995 | Enc += EncType; |
| 8996 | return true; |
| 8997 | } |
| 8998 | |
| 8999 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 9000 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 9001 | const CodeGen::CodeGenModule &CGM, |
| 9002 | TypeStringCache &TSC) { |
| 9003 | Enc += "p("; |
| 9004 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 9005 | return false; |
| 9006 | Enc += ')'; |
| 9007 | return true; |
| 9008 | } |
| 9009 | |
| 9010 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9011 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 9012 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9013 | const CodeGen::CodeGenModule &CGM, |
| 9014 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 9015 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 9016 | return false; |
| 9017 | Enc += "a("; |
| 9018 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 9019 | CAT->getSize().toStringUnsigned(Enc); |
| 9020 | else |
| 9021 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 9022 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9023 | // The Qualifiers should be attached to the type rather than the array. |
| 9024 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9025 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 9026 | return false; |
| 9027 | Enc += ')'; |
| 9028 | return true; |
| 9029 | } |
| 9030 | |
| 9031 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 9032 | /// and the arguments. |
| 9033 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 9034 | const CodeGen::CodeGenModule &CGM, |
| 9035 | TypeStringCache &TSC) { |
| 9036 | Enc += "f{"; |
| 9037 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 9038 | return false; |
| 9039 | Enc += "}("; |
| 9040 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 9041 | // N.B. we are only interested in the adjusted param types. |
| 9042 | auto I = FPT->param_type_begin(); |
| 9043 | auto E = FPT->param_type_end(); |
| 9044 | if (I != E) { |
| 9045 | do { |
| 9046 | if (!appendType(Enc, *I, CGM, TSC)) |
| 9047 | return false; |
| 9048 | ++I; |
| 9049 | if (I != E) |
| 9050 | Enc += ','; |
| 9051 | } while (I != E); |
| 9052 | if (FPT->isVariadic()) |
| 9053 | Enc += ",va"; |
| 9054 | } else { |
| 9055 | if (FPT->isVariadic()) |
| 9056 | Enc += "va"; |
| 9057 | else |
| 9058 | Enc += '0'; |
| 9059 | } |
| 9060 | } |
| 9061 | Enc += ')'; |
| 9062 | return true; |
| 9063 | } |
| 9064 | |
| 9065 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 9066 | /// type encodings. |
| 9067 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 9068 | const CodeGen::CodeGenModule &CGM, |
| 9069 | TypeStringCache &TSC) { |
| 9070 | |
| 9071 | QualType QT = QType.getCanonicalType(); |
| 9072 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9073 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 9074 | // The Qualifiers should be attached to the type rather than the array. |
| 9075 | // Thus we don't call appendQualifier() here. |
| 9076 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 9077 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9078 | appendQualifier(Enc, QT); |
| 9079 | |
| 9080 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 9081 | return appendBuiltinType(Enc, BT); |
| 9082 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9083 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 9084 | return appendPointerType(Enc, PT, CGM, TSC); |
| 9085 | |
| 9086 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 9087 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 9088 | |
| 9089 | if (const RecordType *RT = QT->getAsStructureType()) |
| 9090 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9091 | |
| 9092 | if (const RecordType *RT = QT->getAsUnionType()) |
| 9093 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9094 | |
| 9095 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 9096 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 9097 | |
| 9098 | return false; |
| 9099 | } |
| 9100 | |
| 9101 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 9102 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 9103 | if (!D) |
| 9104 | return false; |
| 9105 | |
| 9106 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9107 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 9108 | return false; |
| 9109 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 9110 | } |
| 9111 | |
| 9112 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 9113 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 9114 | return false; |
| 9115 | QualType QT = VD->getType().getCanonicalType(); |
| 9116 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 9117 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9118 | // The Qualifiers should be attached to the type rather than the array. |
| 9119 | // Thus we don't call appendQualifier() here. |
| 9120 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9121 | } |
| 9122 | return appendType(Enc, QT, CGM, TSC); |
| 9123 | } |
| 9124 | return false; |
| 9125 | } |
| 9126 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9127 | //===----------------------------------------------------------------------===// |
| 9128 | // RISCV ABI Implementation |
| 9129 | //===----------------------------------------------------------------------===// |
| 9130 | |
| 9131 | namespace { |
| 9132 | class RISCVABIInfo : public DefaultABIInfo { |
| 9133 | private: |
| 9134 | unsigned XLen; // Size of the integer ('x') registers in bits. |
| 9135 | static const int NumArgGPRs = 8; |
| 9136 | |
| 9137 | public: |
| 9138 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9139 | : DefaultABIInfo(CGT), XLen(XLen) {} |
| 9140 | |
| 9141 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 9142 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 9143 | void computeInfo(CGFunctionInfo &FI) const override; |
| 9144 | |
| 9145 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, |
| 9146 | int &ArgGPRsLeft) const; |
| 9147 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 9148 | |
| 9149 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9150 | QualType Ty) const override; |
| 9151 | |
| 9152 | ABIArgInfo extendType(QualType Ty) const; |
| 9153 | }; |
| 9154 | } // end anonymous namespace |
| 9155 | |
| 9156 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 9157 | QualType RetTy = FI.getReturnType(); |
| 9158 | if (!getCXXABI().classifyReturnType(FI)) |
| 9159 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 9160 | |
| 9161 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 9162 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 9163 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 9164 | // rewrite the argument list and pass indirectly on RV32. |
| 9165 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 9166 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 9167 | |
| 9168 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 9169 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 9170 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 9171 | // different for variadic arguments, we must also track whether we are |
| 9172 | // examining a vararg or not. |
| 9173 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
| 9174 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 9175 | |
| 9176 | int ArgNum = 0; |
| 9177 | for (auto &ArgInfo : FI.arguments()) { |
| 9178 | bool IsFixed = ArgNum < NumFixedArgs; |
| 9179 | ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft); |
| 9180 | ArgNum++; |
| 9181 | } |
| 9182 | } |
| 9183 | |
| 9184 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
| 9185 | int &ArgGPRsLeft) const { |
| 9186 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 9187 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 9188 | |
| 9189 | // Structures with either a non-trivial destructor or a non-trivial |
| 9190 | // copy constructor are always passed indirectly. |
| 9191 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 9192 | if (ArgGPRsLeft) |
| 9193 | ArgGPRsLeft -= 1; |
| 9194 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 9195 | CGCXXABI::RAA_DirectInMemory); |
| 9196 | } |
| 9197 | |
| 9198 | // Ignore empty structs/unions. |
| 9199 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9200 | return ABIArgInfo::getIgnore(); |
| 9201 | |
| 9202 | uint64_t Size = getContext().getTypeSize(Ty); |
| 9203 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 9204 | bool MustUseStack = false; |
| 9205 | // Determine the number of GPRs needed to pass the current argument |
| 9206 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 9207 | // register pairs, so may consume 3 registers. |
| 9208 | int NeededArgGPRs = 1; |
| 9209 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 9210 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 9211 | else if (Size > XLen && Size <= 2 * XLen) |
| 9212 | NeededArgGPRs = 2; |
| 9213 | |
| 9214 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 9215 | MustUseStack = true; |
| 9216 | NeededArgGPRs = ArgGPRsLeft; |
| 9217 | } |
| 9218 | |
| 9219 | ArgGPRsLeft -= NeededArgGPRs; |
| 9220 | |
| 9221 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 9222 | // Treat an enum type as its underlying type. |
| 9223 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 9224 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 9225 | |
| 9226 | // All integral types are promoted to XLen width, unless passed on the |
| 9227 | // stack. |
| 9228 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 9229 | return extendType(Ty); |
| 9230 | } |
| 9231 | |
| 9232 | return ABIArgInfo::getDirect(); |
| 9233 | } |
| 9234 | |
| 9235 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 9236 | // so coerce to integers. |
| 9237 | if (Size <= 2 * XLen) { |
| 9238 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 9239 | |
| 9240 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 9241 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 9242 | if (Size <= XLen) { |
| 9243 | return ABIArgInfo::getDirect( |
| 9244 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 9245 | } else if (Alignment == 2 * XLen) { |
| 9246 | return ABIArgInfo::getDirect( |
| 9247 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 9248 | } else { |
| 9249 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 9250 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 9251 | } |
| 9252 | } |
| 9253 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 9254 | } |
| 9255 | |
| 9256 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 9257 | if (RetTy->isVoidType()) |
| 9258 | return ABIArgInfo::getIgnore(); |
| 9259 | |
| 9260 | int ArgGPRsLeft = 2; |
| 9261 | |
| 9262 | // The rules for return and argument types are the same, so defer to |
| 9263 | // classifyArgumentType. |
| 9264 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft); |
| 9265 | } |
| 9266 | |
| 9267 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9268 | QualType Ty) const { |
| 9269 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 9270 | |
| 9271 | // Empty records are ignored for parameter passing purposes. |
| 9272 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 9273 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 9274 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 9275 | return Addr; |
| 9276 | } |
| 9277 | |
| 9278 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 9279 | getContext().getTypeInfoInChars(Ty); |
| 9280 | |
| 9281 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 9282 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 9283 | |
| 9284 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 9285 | SlotSize, /*AllowHigherAlign=*/true); |
| 9286 | } |
| 9287 | |
| 9288 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 9289 | int TySize = getContext().getTypeSize(Ty); |
| 9290 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 9291 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 9292 | return ABIArgInfo::getSignExtend(Ty); |
| 9293 | return ABIArgInfo::getExtend(Ty); |
| 9294 | } |
| 9295 | |
| 9296 | namespace { |
| 9297 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 9298 | public: |
| 9299 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9300 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen)) {} |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 9301 | |
| 9302 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 9303 | CodeGen::CodeGenModule &CGM) const override { |
| 9304 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 9305 | if (!FD) return; |
| 9306 | |
| 9307 | const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); |
| 9308 | if (!Attr) |
| 9309 | return; |
| 9310 | |
| 9311 | const char *Kind; |
| 9312 | switch (Attr->getInterrupt()) { |
| 9313 | case RISCVInterruptAttr::user: Kind = "user"; break; |
| 9314 | case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; |
| 9315 | case RISCVInterruptAttr::machine: Kind = "machine"; break; |
| 9316 | } |
| 9317 | |
| 9318 | auto *Fn = cast<llvm::Function>(GV); |
| 9319 | |
| 9320 | Fn->addFnAttr("interrupt", Kind); |
| 9321 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9322 | }; |
| 9323 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9324 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9325 | //===----------------------------------------------------------------------===// |
| 9326 | // Driver code |
| 9327 | //===----------------------------------------------------------------------===// |
| 9328 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9329 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 9330 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9331 | } |
| 9332 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 9333 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9334 | if (TheTargetCodeGenInfo) |
| 9335 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9336 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9337 | // Helper to set the unique_ptr while still keeping the return value. |
| 9338 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 9339 | this->TheTargetCodeGenInfo.reset(P); |
| 9340 | return *P; |
| 9341 | }; |
| 9342 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 9343 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 9344 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9345 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9346 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9347 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 9348 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9349 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 9350 | case llvm::Triple::mips: |
| 9351 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 9352 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9353 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 9354 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9355 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 9356 | case llvm::Triple::mips64: |
| 9357 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9358 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9359 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 9360 | case llvm::Triple::avr: |
| 9361 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 9362 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 9363 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 9364 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9365 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 9366 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9367 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 9368 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 9369 | return SetCGInfo( |
| 9370 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9371 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9372 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9373 | } |
| 9374 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9375 | case llvm::Triple::wasm32: |
| 9376 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9377 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9378 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9379 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 9380 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9381 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9382 | case llvm::Triple::thumbeb: { |
| 9383 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9384 | return SetCGInfo( |
| 9385 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9386 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9387 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9388 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9389 | StringRef ABIStr = getTarget().getABI(); |
| 9390 | if (ABIStr == "apcs-gnu") |
| 9391 | Kind = ARMABIInfo::APCS; |
| 9392 | else if (ABIStr == "aapcs16") |
| 9393 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9394 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9395 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9396 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9397 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9398 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9399 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9400 | |
| 9401 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9402 | } |
| 9403 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9404 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9405 | return SetCGInfo( |
| 9406 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9407 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9408 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9409 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9410 | if (getTarget().getABI() == "elfv2") |
| 9411 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9412 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9413 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9414 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9415 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9416 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9417 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9418 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9419 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9420 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9421 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9422 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9423 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9424 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9425 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9426 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9427 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9428 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9429 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9430 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9431 | case llvm::Triple::nvptx: |
| 9432 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9433 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9434 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9435 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9436 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9437 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9438 | case llvm::Triple::riscv32: |
| 9439 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 32)); |
| 9440 | case llvm::Triple::riscv64: |
| 9441 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 64)); |
| 9442 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9443 | case llvm::Triple::systemz: { |
| 9444 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9445 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9446 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9447 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9448 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9449 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9450 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9451 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9452 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9453 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9454 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9455 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9456 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9457 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9458 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9459 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9460 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9461 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9462 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9463 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9464 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9465 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 9466 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9467 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9468 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9469 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9470 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9471 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9472 | X86AVXABILevel AVXLevel = |
| 9473 | (ABI == "avx512" |
| 9474 | ? X86AVXABILevel::AVX512 |
| 9475 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9476 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9477 | switch (Triple.getOS()) { |
| 9478 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9479 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9480 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9481 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9482 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9483 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9484 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9485 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9486 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9487 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9488 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9489 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9490 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9491 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9492 | case llvm::Triple::sparc: |
| 9493 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9494 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9495 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9496 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9497 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 9498 | case llvm::Triple::arc: |
| 9499 | return SetCGInfo(new ARCTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9500 | case llvm::Triple::spir: |
| 9501 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9502 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9503 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9504 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9505 | |
| 9506 | /// Create an OpenCL kernel for an enqueued block. |
| 9507 | /// |
| 9508 | /// The kernel has the same function type as the block invoke function. Its |
| 9509 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9510 | /// It simply calls the block invoke function then returns. |
| 9511 | llvm::Function * |
| 9512 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9513 | llvm::Function *Invoke, |
| 9514 | llvm::Value *BlockLiteral) const { |
| 9515 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9516 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9517 | for (auto &P : InvokeFT->params()) |
| 9518 | ArgTys.push_back(P); |
| 9519 | auto &C = CGF.getLLVMContext(); |
| 9520 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9521 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9522 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9523 | &CGF.CGM.getModule()); |
| 9524 | auto IP = CGF.Builder.saveIP(); |
| 9525 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9526 | auto &Builder = CGF.Builder; |
| 9527 | Builder.SetInsertPoint(BB); |
| 9528 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9529 | for (auto &A : F->args()) |
| 9530 | Args.push_back(&A); |
| 9531 | Builder.CreateCall(Invoke, Args); |
| 9532 | Builder.CreateRetVoid(); |
| 9533 | Builder.restoreIP(IP); |
| 9534 | return F; |
| 9535 | } |
| 9536 | |
| 9537 | /// Create an OpenCL kernel for an enqueued block. |
| 9538 | /// |
| 9539 | /// The type of the first argument (the block literal) is the struct type |
| 9540 | /// of the block literal instead of a pointer type. The first argument |
| 9541 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9542 | /// allocates the same type of struct on stack and stores the block literal |
| 9543 | /// to it and passes its pointer to the block invoke function. The kernel |
| 9544 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 9545 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 9546 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 9547 | llvm::Value *BlockLiteral) const { |
| 9548 | auto &Builder = CGF.Builder; |
| 9549 | auto &C = CGF.getLLVMContext(); |
| 9550 | |
| 9551 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 9552 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9553 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9554 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 9555 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 9556 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 9557 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 9558 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 9559 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 9560 | |
| 9561 | ArgTys.push_back(BlockTy); |
| 9562 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9563 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 9564 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9565 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9566 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9567 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 9568 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 9569 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9570 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9571 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 9572 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9573 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9574 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9575 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 9576 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9577 | } |
| 9578 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9579 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9580 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9581 | &CGF.CGM.getModule()); |
| 9582 | F->addFnAttr("enqueued-block"); |
| 9583 | auto IP = CGF.Builder.saveIP(); |
| 9584 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9585 | Builder.SetInsertPoint(BB); |
| 9586 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 9587 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
| 9588 | BlockPtr->setAlignment(BlockAlign); |
| 9589 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 9590 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 9591 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9592 | Args.push_back(Cast); |
| 9593 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 9594 | Args.push_back(I); |
| 9595 | Builder.CreateCall(Invoke, Args); |
| 9596 | Builder.CreateRetVoid(); |
| 9597 | Builder.restoreIP(IP); |
| 9598 | |
| 9599 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 9600 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 9601 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 9602 | F->setMetadata("kernel_arg_base_type", |
| 9603 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 9604 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 9605 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 9606 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 9607 | |
| 9608 | return F; |
| 9609 | } |