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 |
| 465 | TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const { |
| 466 | return C.getOrInsertSyncScopeID(""); /* default sync scope */ |
| 467 | } |
| 468 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 469 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 470 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 471 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 472 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 473 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 474 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 475 | if (FD->isUnnamedBitfield()) |
| 476 | return true; |
| 477 | |
| 478 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 479 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 480 | // Constant arrays of empty records count as empty, strip them off. |
| 481 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 482 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 483 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 484 | if (AT->getSize() == 0) |
| 485 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 486 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 487 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 488 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 489 | const RecordType *RT = FT->getAs<RecordType>(); |
| 490 | if (!RT) |
| 491 | return false; |
| 492 | |
| 493 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 494 | // |
| 495 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 496 | // current ABI. |
| 497 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 498 | return false; |
| 499 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 500 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 503 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 504 | /// fields. Note that a structure with a flexible array member is not |
| 505 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 506 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 507 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 508 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 509 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 510 | const RecordDecl *RD = RT->getDecl(); |
| 511 | if (RD->hasFlexibleArrayMember()) |
| 512 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 513 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 514 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 515 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 516 | for (const auto &I : CXXRD->bases()) |
| 517 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 518 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 519 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 520 | for (const auto *I : RD->fields()) |
| 521 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 522 | return false; |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | /// isSingleElementStruct - Determine if a structure is a "single |
| 527 | /// element struct", i.e. it has exactly one non-empty field or |
| 528 | /// exactly one field which is itself a single element |
| 529 | /// struct. Structures with flexible array members are never |
| 530 | /// considered single element structs. |
| 531 | /// |
| 532 | /// \return The field declaration for the single non-empty field, if |
| 533 | /// it exists. |
| 534 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 535 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 536 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 537 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 538 | |
| 539 | const RecordDecl *RD = RT->getDecl(); |
| 540 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 541 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 542 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 543 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 544 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 545 | // If this is a C++ record, check the bases first. |
| 546 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 547 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 548 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 549 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 550 | continue; |
| 551 | |
| 552 | // If we already found an element then this isn't a single-element struct. |
| 553 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 554 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 555 | |
| 556 | // If this is non-empty and not a single element struct, the composite |
| 557 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 558 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 559 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 560 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 565 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 566 | QualType FT = FD->getType(); |
| 567 | |
| 568 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 569 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 570 | continue; |
| 571 | |
| 572 | // If we already found an element then this isn't a single-element |
| 573 | // struct. |
| 574 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 575 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 576 | |
| 577 | // Treat single element arrays as the element. |
| 578 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 579 | if (AT->getSize().getZExtValue() != 1) |
| 580 | break; |
| 581 | FT = AT->getElementType(); |
| 582 | } |
| 583 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 584 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 585 | Found = FT.getTypePtr(); |
| 586 | } else { |
| 587 | Found = isSingleElementStruct(FT, Context); |
| 588 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 589 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 593 | // We don't consider a struct a single-element struct if it has |
| 594 | // padding beyond the element type. |
| 595 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 596 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 597 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 598 | return Found; |
| 599 | } |
| 600 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 601 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 602 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 603 | const ABIArgInfo &AI) { |
| 604 | // This default implementation defers to the llvm backend's va_arg |
| 605 | // instruction. It can handle only passing arguments directly |
| 606 | // (typically only handled in the backend for primitive types), or |
| 607 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 608 | // flag has ABI impact in the callee, this implementation cannot |
| 609 | // work.) |
| 610 | |
| 611 | // Only a few cases are covered here at the moment -- those needed |
| 612 | // by the default abi. |
| 613 | llvm::Value *Val; |
| 614 | |
| 615 | if (AI.isIndirect()) { |
| 616 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 617 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 618 | assert( |
| 619 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 620 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 621 | |
| 622 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 623 | CharUnits TyAlignForABI = TyInfo.second; |
| 624 | |
| 625 | llvm::Type *BaseTy = |
| 626 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 627 | llvm::Value *Addr = |
| 628 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 629 | return Address(Addr, TyAlignForABI); |
| 630 | } else { |
| 631 | assert((AI.isDirect() || AI.isExtend()) && |
| 632 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 633 | |
| 634 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 635 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 636 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 637 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 638 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 639 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 640 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 641 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 642 | |
| 643 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 644 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 645 | CGF.Builder.CreateStore(Val, Temp); |
| 646 | return Temp; |
| 647 | } |
| 648 | } |
| 649 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 650 | /// DefaultABIInfo - The default implementation for ABI specific |
| 651 | /// details. This implementation provides information which results in |
| 652 | /// self-consistent and sensible LLVM IR generation, but does not |
| 653 | /// conform to any particular ABI. |
| 654 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 655 | public: |
| 656 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 658 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 659 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 660 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 661 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 662 | if (!getCXXABI().classifyReturnType(FI)) |
| 663 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 664 | for (auto &I : FI.arguments()) |
| 665 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 666 | } |
| 667 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 668 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 669 | QualType Ty) const override { |
| 670 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 671 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 672 | }; |
| 673 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 674 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 675 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 676 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 677 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 678 | }; |
| 679 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 680 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 681 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 682 | |
| 683 | if (isAggregateTypeForABI(Ty)) { |
| 684 | // Records with non-trivial destructors/copy-constructors should not be |
| 685 | // passed by value. |
| 686 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 687 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 688 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 689 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 690 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 692 | // Treat an enum type as its underlying type. |
| 693 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 694 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 695 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 696 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 697 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 700 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 701 | if (RetTy->isVoidType()) |
| 702 | return ABIArgInfo::getIgnore(); |
| 703 | |
| 704 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 705 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 706 | |
| 707 | // Treat an enum type as its underlying type. |
| 708 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 709 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 710 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 711 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 712 | : ABIArgInfo::getDirect()); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 715 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 716 | // WebAssembly ABI Implementation |
| 717 | // |
| 718 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 721 | class WebAssemblyABIInfo final : public SwiftABIInfo { |
| 722 | DefaultABIInfo defaultInfo; |
| 723 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 724 | public: |
| 725 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 726 | : SwiftABIInfo(CGT), defaultInfo(CGT) {} |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 727 | |
| 728 | private: |
| 729 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 730 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 731 | |
| 732 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 733 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 734 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 735 | void computeInfo(CGFunctionInfo &FI) const override { |
| 736 | if (!getCXXABI().classifyReturnType(FI)) |
| 737 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 738 | for (auto &Arg : FI.arguments()) |
| 739 | Arg.info = classifyArgumentType(Arg.type); |
| 740 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 741 | |
| 742 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 743 | QualType Ty) const override; |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 744 | |
| 745 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 746 | bool asReturnValue) const override { |
| 747 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 748 | } |
| 749 | |
| 750 | bool isSwiftErrorInRegister() const override { |
| 751 | return false; |
| 752 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 756 | public: |
| 757 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 758 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 759 | |
| 760 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 761 | CodeGen::CodeGenModule &CGM) const override { |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 762 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 763 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 764 | if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) { |
| 765 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 766 | llvm::AttrBuilder B; |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 767 | B.addAttribute("wasm-import-module", Attr->getImportModule()); |
| 768 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 769 | } |
| 770 | if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) { |
| 771 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 772 | llvm::AttrBuilder B; |
| 773 | B.addAttribute("wasm-import-name", Attr->getImportName()); |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 774 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 775 | } |
| 776 | } |
| 777 | |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 778 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 779 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 780 | if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) |
| 781 | Fn->addFnAttr("no-prototype"); |
| 782 | } |
| 783 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 784 | }; |
| 785 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 786 | /// Classify argument of given type \p Ty. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 787 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 788 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 789 | |
| 790 | if (isAggregateTypeForABI(Ty)) { |
| 791 | // Records with non-trivial destructors/copy-constructors should not be |
| 792 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 793 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 794 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 795 | // Ignore empty structs/unions. |
| 796 | if (isEmptyRecord(getContext(), Ty, true)) |
| 797 | return ABIArgInfo::getIgnore(); |
| 798 | // Lower single-element structs to just pass a regular value. TODO: We |
| 799 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 800 | // though watch out for things like bitfields. |
| 801 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 802 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 806 | return defaultInfo.classifyArgumentType(Ty); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 810 | if (isAggregateTypeForABI(RetTy)) { |
| 811 | // Records with non-trivial destructors/copy-constructors should not be |
| 812 | // returned by value. |
| 813 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 814 | // Ignore empty structs/unions. |
| 815 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 816 | return ABIArgInfo::getIgnore(); |
| 817 | // Lower single-element structs to just return a regular value. TODO: We |
| 818 | // could do reasonable-size multiple-element structs too, using |
| 819 | // ABIArgInfo::getDirect(). |
| 820 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 821 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 826 | return defaultInfo.classifyReturnType(RetTy); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 829 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 830 | QualType Ty) const { |
| 831 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 832 | getContext().getTypeInfoInChars(Ty), |
| 833 | CharUnits::fromQuantity(4), |
| 834 | /*AllowHigherAlign=*/ true); |
| 835 | } |
| 836 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 837 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 838 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 839 | // |
| 840 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 841 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 842 | //===----------------------------------------------------------------------===// |
| 843 | |
| 844 | class PNaClABIInfo : public ABIInfo { |
| 845 | public: |
| 846 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 847 | |
| 848 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 849 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 850 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 851 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 852 | Address EmitVAArg(CodeGenFunction &CGF, |
| 853 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 857 | public: |
| 858 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 859 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 860 | }; |
| 861 | |
| 862 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 863 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 864 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 865 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 866 | for (auto &I : FI.arguments()) |
| 867 | I.info = classifyArgumentType(I.type); |
| 868 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 869 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 870 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 871 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 872 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 873 | // function classification. Structs get passed directly for varargs |
| 874 | // functions, through a rewriting transform in |
| 875 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 876 | // this target to actually support a va_arg instructions with an |
| 877 | // aggregate type, unlike other targets. |
| 878 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 881 | /// Classify argument of given type \p Ty. |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 882 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 883 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 884 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 885 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 886 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 887 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 888 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 889 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 890 | } else if (Ty->isFloatingType()) { |
| 891 | // Floating-point types don't go inreg. |
| 892 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 893 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 894 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 895 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 896 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 900 | if (RetTy->isVoidType()) |
| 901 | return ABIArgInfo::getIgnore(); |
| 902 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 903 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 904 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 905 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 906 | |
| 907 | // Treat an enum type as its underlying type. |
| 908 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 909 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 910 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 911 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 912 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 915 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 916 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 917 | // 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] | 918 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 919 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 920 | IRType->getScalarSizeInBits() != 64; |
| 921 | } |
| 922 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 923 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 924 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 925 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 926 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 927 | .Cases("y", "&y", "^Ym", true) |
| 928 | .Default(false); |
| 929 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 930 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 931 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 932 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 935 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 939 | return Ty; |
| 940 | } |
| 941 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 942 | /// Returns true if this type can be passed in SSE registers with the |
| 943 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 944 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 945 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 946 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 947 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 948 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 949 | &llvm::APFloat::x87DoubleExtended()) |
| 950 | return false; |
| 951 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 952 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 953 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 954 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 955 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 956 | // registers specially. |
| 957 | unsigned VecSize = Context.getTypeSize(VT); |
| 958 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 959 | return true; |
| 960 | } |
| 961 | return false; |
| 962 | } |
| 963 | |
| 964 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 965 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 966 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 967 | return NumMembers <= 4; |
| 968 | } |
| 969 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 970 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 971 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 972 | auto AI = ABIArgInfo::getDirect(T); |
| 973 | AI.setInReg(true); |
| 974 | AI.setCanBeFlattened(false); |
| 975 | return AI; |
| 976 | } |
| 977 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 978 | //===----------------------------------------------------------------------===// |
| 979 | // X86-32 ABI Implementation |
| 980 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 981 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 982 | /// Similar to llvm::CCState, but for Clang. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 983 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 984 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 985 | |
| 986 | unsigned CC; |
| 987 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 988 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 989 | }; |
| 990 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 991 | enum { |
| 992 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 993 | VectorcallMaxParamNumAsReg = 6 |
| 994 | }; |
| 995 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 996 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 997 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 998 | enum Class { |
| 999 | Integer, |
| 1000 | Float |
| 1001 | }; |
| 1002 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1003 | static const unsigned MinABIStackAlignInBytes = 4; |
| 1004 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1005 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1006 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1007 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1008 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1009 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1010 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1011 | |
| 1012 | static bool isRegisterSize(unsigned Size) { |
| 1013 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 1014 | } |
| 1015 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1016 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1017 | // FIXME: Assumes vectorcall is in use. |
| 1018 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1019 | } |
| 1020 | |
| 1021 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1022 | uint64_t NumMembers) const override { |
| 1023 | // FIXME: Assumes vectorcall is in use. |
| 1024 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1025 | } |
| 1026 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1027 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1028 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1029 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 1030 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1031 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 1032 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1033 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1034 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1035 | /// Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1036 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1037 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1038 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1039 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1040 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1041 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1042 | /// Updates the number of available free registers, returns |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1043 | /// true if any registers were allocated. |
| 1044 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 1045 | |
| 1046 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1047 | bool &NeedsPadding) const; |
| 1048 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1049 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1050 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1051 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1052 | /// Rewrite the function info so that all memory arguments use |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1053 | /// inalloca. |
| 1054 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1055 | |
| 1056 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1057 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1058 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1059 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1060 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1061 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1062 | public: |
| 1063 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1064 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1065 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1066 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1067 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1068 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1069 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1070 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1071 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1072 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1073 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1074 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1075 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1076 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1077 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1078 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1079 | bool asReturnValue) const override { |
| 1080 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1081 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1082 | // four vector registers for vectors, but those can overlap with the |
| 1083 | // scalar registers. |
| 1084 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1085 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1086 | |
| 1087 | bool isSwiftErrorInRegister() const override { |
| 1088 | // x86-32 lowering does not support passing swifterror in a register. |
| 1089 | return false; |
| 1090 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1091 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1092 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1093 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1094 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1095 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1096 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1097 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1098 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1099 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1100 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1101 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1102 | static bool isStructReturnInRegABI( |
| 1103 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1104 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1105 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1106 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1107 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1108 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1109 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1110 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1111 | return 4; |
| 1112 | } |
| 1113 | |
| 1114 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1115 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1116 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1117 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1118 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1119 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1120 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1121 | } |
| 1122 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1123 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1124 | std::string &Constraints, |
| 1125 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1126 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1127 | std::vector<LValue> &ResultRegDests, |
| 1128 | std::string &AsmString, |
| 1129 | unsigned NumOutputs) const override; |
| 1130 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1131 | llvm::Constant * |
| 1132 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1133 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1134 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1135 | ('v' << 16) | |
| 1136 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1137 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1138 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1139 | |
| 1140 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1141 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1142 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1143 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1144 | }; |
| 1145 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1146 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1147 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1148 | /// Rewrite input constraint references after adding some output constraints. |
| 1149 | /// In the case where there is one output and one input and we add one output, |
| 1150 | /// we need to replace all operand references greater than or equal to 1: |
| 1151 | /// mov $0, $1 |
| 1152 | /// mov eax, $1 |
| 1153 | /// The result will be: |
| 1154 | /// mov $0, $2 |
| 1155 | /// mov eax, $2 |
| 1156 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1157 | unsigned NumNewOuts, |
| 1158 | std::string &AsmString) { |
| 1159 | std::string Buf; |
| 1160 | llvm::raw_string_ostream OS(Buf); |
| 1161 | size_t Pos = 0; |
| 1162 | while (Pos < AsmString.size()) { |
| 1163 | size_t DollarStart = AsmString.find('$', Pos); |
| 1164 | if (DollarStart == std::string::npos) |
| 1165 | DollarStart = AsmString.size(); |
| 1166 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1167 | if (DollarEnd == std::string::npos) |
| 1168 | DollarEnd = AsmString.size(); |
| 1169 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1170 | Pos = DollarEnd; |
| 1171 | size_t NumDollars = DollarEnd - DollarStart; |
| 1172 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1173 | // We have an operand reference. |
| 1174 | size_t DigitStart = Pos; |
| 1175 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1176 | if (DigitEnd == std::string::npos) |
| 1177 | DigitEnd = AsmString.size(); |
| 1178 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1179 | unsigned OperandIndex; |
| 1180 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1181 | if (OperandIndex >= FirstIn) |
| 1182 | OperandIndex += NumNewOuts; |
| 1183 | OS << OperandIndex; |
| 1184 | } else { |
| 1185 | OS << OperandStr; |
| 1186 | } |
| 1187 | Pos = DigitEnd; |
| 1188 | } |
| 1189 | } |
| 1190 | AsmString = std::move(OS.str()); |
| 1191 | } |
| 1192 | |
| 1193 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1194 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1195 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1196 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1197 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1198 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1199 | unsigned NumOutputs) const { |
| 1200 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1201 | |
| 1202 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1203 | // larger. |
| 1204 | if (!Constraints.empty()) |
| 1205 | Constraints += ','; |
| 1206 | if (RetWidth <= 32) { |
| 1207 | Constraints += "={eax}"; |
| 1208 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1209 | } else { |
| 1210 | // Use the 'A' constraint for EAX:EDX. |
| 1211 | Constraints += "=A"; |
| 1212 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1213 | } |
| 1214 | |
| 1215 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1216 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1217 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1218 | |
| 1219 | // Coerce the integer by bitcasting the return slot pointer. |
| 1220 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1221 | CoerceTy->getPointerTo())); |
| 1222 | ResultRegDests.push_back(ReturnSlot); |
| 1223 | |
| 1224 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1225 | } |
| 1226 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1227 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1228 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1229 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1230 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1231 | uint64_t Size = Context.getTypeSize(Ty); |
| 1232 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1233 | // For i386, type must be register sized. |
| 1234 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1235 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1236 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1237 | |
| 1238 | if (Ty->isVectorType()) { |
| 1239 | // 64- and 128- bit vectors inside structures are not returned in |
| 1240 | // registers. |
| 1241 | if (Size == 64 || Size == 128) |
| 1242 | return false; |
| 1243 | |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1247 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1248 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1249 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1250 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1251 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1252 | return true; |
| 1253 | |
| 1254 | // Arrays are treated like records. |
| 1255 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1256 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1257 | |
| 1258 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1259 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1260 | if (!RT) return false; |
| 1261 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1262 | // FIXME: Traverse bases here too. |
| 1263 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1264 | // Structure types are passed in register if all fields would be |
| 1265 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1266 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1267 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1268 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1269 | continue; |
| 1270 | |
| 1271 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1272 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1273 | return false; |
| 1274 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1275 | return true; |
| 1276 | } |
| 1277 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1278 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1279 | // Treat complex types as the element type. |
| 1280 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1281 | Ty = CTy->getElementType(); |
| 1282 | |
| 1283 | // Check for a type which we know has a simple scalar argument-passing |
| 1284 | // convention without any padding. (We're specifically looking for 32 |
| 1285 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1286 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1287 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1288 | return false; |
| 1289 | |
| 1290 | uint64_t Size = Context.getTypeSize(Ty); |
| 1291 | return Size == 32 || Size == 64; |
| 1292 | } |
| 1293 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1294 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1295 | uint64_t &Size) { |
| 1296 | for (const auto *FD : RD->fields()) { |
| 1297 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1298 | // argument is smaller than 32-bits, expanding the struct will create |
| 1299 | // alignment padding. |
| 1300 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1301 | return false; |
| 1302 | |
| 1303 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1304 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1305 | // counts as "basic" is more complicated than what we were doing previously. |
| 1306 | if (FD->isBitField()) |
| 1307 | return false; |
| 1308 | |
| 1309 | Size += Context.getTypeSize(FD->getType()); |
| 1310 | } |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1315 | uint64_t &Size) { |
| 1316 | // Don't do this if there are any non-empty bases. |
| 1317 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1318 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1319 | Size)) |
| 1320 | return false; |
| 1321 | } |
| 1322 | if (!addFieldSizes(Context, RD, Size)) |
| 1323 | return false; |
| 1324 | return true; |
| 1325 | } |
| 1326 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1327 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1328 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1329 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1330 | /// optimizations. |
| 1331 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1332 | // We can only expand structure types. |
| 1333 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1334 | if (!RT) |
| 1335 | return false; |
| 1336 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1337 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1338 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1339 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1340 | // On non-Windows, we have to conservatively match our old bitcode |
| 1341 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1342 | if (!CXXRD->isCLike()) |
| 1343 | return false; |
| 1344 | } else { |
| 1345 | // Don't do this for dynamic classes. |
| 1346 | if (CXXRD->isDynamicClass()) |
| 1347 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1348 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1349 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1350 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1351 | } else { |
| 1352 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1353 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | // We can do this if there was no alignment padding. |
| 1357 | return Size == getContext().getTypeSize(Ty); |
| 1358 | } |
| 1359 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1360 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1361 | // If the return value is indirect, then the hidden argument is consuming one |
| 1362 | // integer register. |
| 1363 | if (State.FreeRegs) { |
| 1364 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1365 | if (!IsMCUABI) |
| 1366 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1367 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1368 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1371 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1372 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1373 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1374 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1375 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1376 | const Type *Base = nullptr; |
| 1377 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1378 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1379 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1380 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1381 | // The LLVM struct type for such an aggregate should lower properly. |
| 1382 | return ABIArgInfo::getDirect(); |
| 1383 | } |
| 1384 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1385 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1386 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1387 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1388 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1389 | |
| 1390 | // 128-bit vectors are a special case; they are returned in |
| 1391 | // registers and we need to make sure to pick a type the LLVM |
| 1392 | // backend will like. |
| 1393 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1394 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1395 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1396 | |
| 1397 | // Always return in register if it fits in a general purpose |
| 1398 | // register, or if it is 64 bits and has a single element. |
| 1399 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1400 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1401 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1402 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1403 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1404 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1408 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1409 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1410 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1411 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1412 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1413 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1414 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1415 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1416 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1417 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1418 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1419 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1420 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1421 | // Ignore empty structs/unions. |
| 1422 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1423 | return ABIArgInfo::getIgnore(); |
| 1424 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1425 | // Small structures which are register sized are generally returned |
| 1426 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1427 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1428 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1429 | |
| 1430 | // As a special-case, if the struct is a "single-element" struct, and |
| 1431 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1432 | // floating-point register. (MSVC does not apply this special case.) |
| 1433 | // We apply a similar transformation for pointer types to improve the |
| 1434 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1435 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1436 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1437 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1438 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1439 | |
| 1440 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1441 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1442 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1445 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1446 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1448 | // Treat an enum type as its underlying type. |
| 1449 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1450 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1451 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1452 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1453 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1456 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1457 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1458 | } |
| 1459 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1460 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1461 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1462 | if (!RT) |
| 1463 | return 0; |
| 1464 | const RecordDecl *RD = RT->getDecl(); |
| 1465 | |
| 1466 | // If this is a C++ record, check the bases first. |
| 1467 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1468 | for (const auto &I : CXXRD->bases()) |
| 1469 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1470 | return false; |
| 1471 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1472 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1473 | QualType FT = i->getType(); |
| 1474 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1475 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1476 | return true; |
| 1477 | |
| 1478 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1485 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1486 | unsigned Align) const { |
| 1487 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1488 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1489 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1490 | return 0; // Use default alignment. |
| 1491 | |
| 1492 | // On non-Darwin, the stack type alignment is always 4. |
| 1493 | if (!IsDarwinVectorABI) { |
| 1494 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1495 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1496 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1497 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1498 | // 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] | 1499 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1500 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1501 | return 16; |
| 1502 | |
| 1503 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1506 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1507 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1508 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1509 | if (State.FreeRegs) { |
| 1510 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1511 | if (!IsMCUABI) |
| 1512 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1513 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1514 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1515 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1516 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1517 | // Compute the byval alignment. |
| 1518 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1519 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1520 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1521 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1522 | |
| 1523 | // If the stack alignment is less than the type alignment, realign the |
| 1524 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1525 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1526 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1527 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1530 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1531 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1532 | if (!T) |
| 1533 | T = Ty.getTypePtr(); |
| 1534 | |
| 1535 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1536 | BuiltinType::Kind K = BT->getKind(); |
| 1537 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1538 | return Float; |
| 1539 | } |
| 1540 | return Integer; |
| 1541 | } |
| 1542 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1543 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1544 | if (!IsSoftFloatABI) { |
| 1545 | Class C = classify(Ty); |
| 1546 | if (C == Float) |
| 1547 | return false; |
| 1548 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1549 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1550 | unsigned Size = getContext().getTypeSize(Ty); |
| 1551 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1552 | |
| 1553 | if (SizeInRegs == 0) |
| 1554 | return false; |
| 1555 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1556 | if (!IsMCUABI) { |
| 1557 | if (SizeInRegs > State.FreeRegs) { |
| 1558 | State.FreeRegs = 0; |
| 1559 | return false; |
| 1560 | } |
| 1561 | } else { |
| 1562 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1563 | // earlier parameters that are passed on the stack. Also, |
| 1564 | // it does not allow passing >8-byte structs in-register, |
| 1565 | // even if there are 3 free registers available. |
| 1566 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1567 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1568 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1569 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1570 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1571 | return true; |
| 1572 | } |
| 1573 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1574 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1575 | bool &InReg, |
| 1576 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1577 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1578 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1579 | // (HFAs) have already been dealt with at this point. |
| 1580 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1581 | return false; |
| 1582 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1583 | NeedsPadding = false; |
| 1584 | InReg = !IsMCUABI; |
| 1585 | |
| 1586 | if (!updateFreeRegs(Ty, State)) |
| 1587 | return false; |
| 1588 | |
| 1589 | if (IsMCUABI) |
| 1590 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1591 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1592 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1593 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1594 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1595 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1596 | NeedsPadding = true; |
| 1597 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1598 | return false; |
| 1599 | } |
| 1600 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1601 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1604 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1605 | if (!updateFreeRegs(Ty, State)) |
| 1606 | return false; |
| 1607 | |
| 1608 | if (IsMCUABI) |
| 1609 | return false; |
| 1610 | |
| 1611 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1612 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1613 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1614 | if (getContext().getTypeSize(Ty) > 32) |
| 1615 | return false; |
| 1616 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1617 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1618 | Ty->isReferenceType()); |
| 1619 | } |
| 1620 | |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1624 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1625 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1626 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1627 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1628 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1629 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1630 | // Check with the C++ ABI first. |
| 1631 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1632 | if (RT) { |
| 1633 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1634 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1635 | return getIndirectResult(Ty, false, State); |
| 1636 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1637 | // The field index doesn't matter, we'll fix it up later. |
| 1638 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1639 | } |
| 1640 | } |
| 1641 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1642 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1643 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1644 | const Type *Base = nullptr; |
| 1645 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1646 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1647 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1648 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1649 | if (State.FreeSSERegs >= NumElts) { |
| 1650 | State.FreeSSERegs -= NumElts; |
| 1651 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1652 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1653 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1654 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1655 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1659 | // Structures with flexible arrays are always indirect. |
| 1660 | // FIXME: This should not be byval! |
| 1661 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1662 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1663 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1664 | // Ignore empty structs/unions on non-Windows. |
| 1665 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1666 | return ABIArgInfo::getIgnore(); |
| 1667 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1668 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1669 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1670 | bool NeedsPadding = false; |
| 1671 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1672 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1673 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1674 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1675 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1676 | if (InReg) |
| 1677 | return ABIArgInfo::getDirectInReg(Result); |
| 1678 | else |
| 1679 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1680 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1681 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1682 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1683 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1684 | // of those arguments will match the struct. This is important because the |
| 1685 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1686 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1687 | // Don't do this for the MCU if there are still free integer registers |
| 1688 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1689 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1690 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1691 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1692 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1693 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1694 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1695 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1696 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1697 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1700 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1701 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1702 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1703 | if (IsDarwinVectorABI) { |
| 1704 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1705 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1706 | (Size == 64 && VT->getNumElements() == 1)) |
| 1707 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1708 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1709 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1710 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1711 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1712 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1713 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1714 | return ABIArgInfo::getDirect(); |
| 1715 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1716 | |
| 1717 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1718 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1719 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1720 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1721 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1722 | |
| 1723 | if (Ty->isPromotableIntegerType()) { |
| 1724 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1725 | return ABIArgInfo::getExtendInReg(Ty); |
| 1726 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1727 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1728 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1729 | if (InReg) |
| 1730 | return ABIArgInfo::getDirectInReg(); |
| 1731 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1734 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1735 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1736 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1737 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1738 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1739 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1740 | // registers. |
| 1741 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1742 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1743 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1744 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1745 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1746 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1747 | // First pass do all the vector types. |
| 1748 | const Type *Base = nullptr; |
| 1749 | uint64_t NumElts = 0; |
| 1750 | const QualType& Ty = I.type; |
| 1751 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1752 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1753 | if (State.FreeSSERegs >= NumElts) { |
| 1754 | State.FreeSSERegs -= NumElts; |
| 1755 | I.info = ABIArgInfo::getDirect(); |
| 1756 | } else { |
| 1757 | I.info = classifyArgumentType(Ty, State); |
| 1758 | } |
| 1759 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1760 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1761 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1762 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1763 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1764 | // Second pass, do the rest! |
| 1765 | const Type *Base = nullptr; |
| 1766 | uint64_t NumElts = 0; |
| 1767 | const QualType& Ty = I.type; |
| 1768 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1769 | |
| 1770 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1771 | // Assign true HVAs (non vector/native FP types). |
| 1772 | if (State.FreeSSERegs >= NumElts) { |
| 1773 | State.FreeSSERegs -= NumElts; |
| 1774 | I.info = getDirectX86Hva(); |
| 1775 | } else { |
| 1776 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1777 | } |
| 1778 | } else if (!IsHva) { |
| 1779 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1780 | I.info = classifyArgumentType(Ty, State); |
| 1781 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1782 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |
| 1785 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1786 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1787 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1788 | if (IsMCUABI) |
| 1789 | State.FreeRegs = 3; |
| 1790 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1791 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1792 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1793 | State.FreeRegs = 2; |
| 1794 | State.FreeSSERegs = 6; |
| 1795 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1796 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1797 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1798 | State.FreeRegs = 5; |
| 1799 | State.FreeSSERegs = 8; |
| 1800 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1801 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1802 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 1803 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1804 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1805 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1806 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1807 | // return value was sret and put it in a register ourselves if appropriate. |
| 1808 | if (State.FreeRegs) { |
| 1809 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1810 | if (!IsMCUABI) |
| 1811 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1812 | } |
| 1813 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1814 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1815 | // The chain argument effectively gives us another free register. |
| 1816 | if (FI.isChainCall()) |
| 1817 | ++State.FreeRegs; |
| 1818 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1819 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1820 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1821 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1822 | } else { |
| 1823 | // If not vectorcall, revert to normal behavior. |
| 1824 | for (auto &I : FI.arguments()) { |
| 1825 | I.info = classifyArgumentType(I.type, State); |
| 1826 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1827 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1831 | // all the memory arguments to use inalloca. |
| 1832 | if (UsedInAlloca) |
| 1833 | rewriteWithInAlloca(FI); |
| 1834 | } |
| 1835 | |
| 1836 | void |
| 1837 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1838 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1839 | QualType Type) const { |
| 1840 | // Arguments are always 4-byte-aligned. |
| 1841 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1842 | |
| 1843 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1844 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1845 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1846 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1847 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1848 | // Insert padding bytes to respect alignment. |
| 1849 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1850 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1851 | if (StackOffset != FieldEnd) { |
| 1852 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1853 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1854 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1855 | FrameFields.push_back(Ty); |
| 1856 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1859 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1860 | // Leave ignored and inreg arguments alone. |
| 1861 | switch (Info.getKind()) { |
| 1862 | case ABIArgInfo::InAlloca: |
| 1863 | return true; |
| 1864 | case ABIArgInfo::Indirect: |
| 1865 | assert(Info.getIndirectByVal()); |
| 1866 | return true; |
| 1867 | case ABIArgInfo::Ignore: |
| 1868 | return false; |
| 1869 | case ABIArgInfo::Direct: |
| 1870 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1871 | if (Info.getInReg()) |
| 1872 | return false; |
| 1873 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1874 | case ABIArgInfo::Expand: |
| 1875 | case ABIArgInfo::CoerceAndExpand: |
| 1876 | // These are aggregate types which are never passed in registers when |
| 1877 | // inalloca is involved. |
| 1878 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1879 | } |
| 1880 | llvm_unreachable("invalid enum"); |
| 1881 | } |
| 1882 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1883 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1884 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1885 | |
| 1886 | // Build a packed struct type for all of the arguments in memory. |
| 1887 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1888 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1889 | // The stack alignment is always 4. |
| 1890 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1891 | |
| 1892 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1893 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1894 | |
| 1895 | // Put 'this' into the struct before 'sret', if necessary. |
| 1896 | bool IsThisCall = |
| 1897 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1898 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1899 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1900 | isArgInAlloca(I->info)) { |
| 1901 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1902 | ++I; |
| 1903 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1904 | |
| 1905 | // 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] | 1906 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1907 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1908 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1909 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1910 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1914 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1915 | ++I; |
| 1916 | |
| 1917 | // Put arguments passed in memory into the struct. |
| 1918 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1919 | if (isArgInAlloca(I->info)) |
| 1920 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1924 | /*isPacked=*/true), |
| 1925 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1928 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1929 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1930 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1931 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1932 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1933 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1934 | // |
| 1935 | // Just messing with TypeInfo like this works because we never pass |
| 1936 | // anything indirectly. |
| 1937 | TypeInfo.second = CharUnits::fromQuantity( |
| 1938 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1939 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1940 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1941 | TypeInfo, CharUnits::fromQuantity(4), |
| 1942 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1945 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1946 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1947 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1948 | |
| 1949 | switch (Opts.getStructReturnConvention()) { |
| 1950 | case CodeGenOptions::SRCK_Default: |
| 1951 | break; |
| 1952 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1953 | return false; |
| 1954 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1955 | return true; |
| 1956 | } |
| 1957 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1958 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1959 | return true; |
| 1960 | |
| 1961 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1962 | case llvm::Triple::DragonFly: |
| 1963 | case llvm::Triple::FreeBSD: |
| 1964 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1965 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1966 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1967 | default: |
| 1968 | return false; |
| 1969 | } |
| 1970 | } |
| 1971 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1972 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1973 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1974 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1975 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1976 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1977 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1978 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 1979 | Fn->addFnAttr("stackrealign"); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1980 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1981 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1982 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1983 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1984 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1988 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1989 | CodeGen::CodeGenFunction &CGF, |
| 1990 | llvm::Value *Address) const { |
| 1991 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1993 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1994 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1995 | // 0-7 are the eight integer registers; the order is different |
| 1996 | // on Darwin (for EH), but the range is the same. |
| 1997 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1998 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1999 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2000 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2001 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 2002 | // These have size 16, which is sizeof(long double) on |
| 2003 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2004 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2005 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2006 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2007 | } else { |
| 2008 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 2009 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2010 | Builder.CreateAlignedStore( |
| 2011 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 2012 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2013 | |
| 2014 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 2015 | // These have size 12, which is sizeof(long double) on |
| 2016 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2017 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2018 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 2019 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2020 | |
| 2021 | return false; |
| 2022 | } |
| 2023 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2024 | //===----------------------------------------------------------------------===// |
| 2025 | // X86-64 ABI Implementation |
| 2026 | //===----------------------------------------------------------------------===// |
| 2027 | |
| 2028 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2029 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2030 | /// The AVX ABI level for X86 targets. |
| 2031 | enum class X86AVXABILevel { |
| 2032 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2033 | AVX, |
| 2034 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2035 | }; |
| 2036 | |
| 2037 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2038 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2039 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2040 | case X86AVXABILevel::AVX512: |
| 2041 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2042 | case X86AVXABILevel::AVX: |
| 2043 | return 256; |
| 2044 | case X86AVXABILevel::None: |
| 2045 | return 128; |
| 2046 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2047 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2050 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2051 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2052 | enum Class { |
| 2053 | Integer = 0, |
| 2054 | SSE, |
| 2055 | SSEUp, |
| 2056 | X87, |
| 2057 | X87Up, |
| 2058 | ComplexX87, |
| 2059 | NoClass, |
| 2060 | Memory |
| 2061 | }; |
| 2062 | |
| 2063 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2064 | /// |
| 2065 | /// Merge an accumulating classification \arg Accum with a field |
| 2066 | /// classification \arg Field. |
| 2067 | /// |
| 2068 | /// \param Accum - The accumulating classification. This should |
| 2069 | /// always be either NoClass or the result of a previous merge |
| 2070 | /// call. In addition, this should never be Memory (the caller |
| 2071 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2072 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2073 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2074 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2075 | /// |
| 2076 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2077 | /// final MEMORY or SSE classes when necessary. |
| 2078 | /// |
| 2079 | /// \param AggregateSize - The size of the current aggregate in |
| 2080 | /// the classification process. |
| 2081 | /// |
| 2082 | /// \param Lo - The classification for the parts of the type |
| 2083 | /// residing in the low word of the containing object. |
| 2084 | /// |
| 2085 | /// \param Hi - The classification for the parts of the type |
| 2086 | /// residing in the higher words of the containing object. |
| 2087 | /// |
| 2088 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2089 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2090 | /// classify - Determine the x86_64 register classes in which the |
| 2091 | /// given type T should be passed. |
| 2092 | /// |
| 2093 | /// \param Lo - The classification for the parts of the type |
| 2094 | /// residing in the low word of the containing object. |
| 2095 | /// |
| 2096 | /// \param Hi - The classification for the parts of the type |
| 2097 | /// residing in the high word of the containing object. |
| 2098 | /// |
| 2099 | /// \param OffsetBase - The bit offset of this type in the |
| 2100 | /// containing object. Some parameters are classified different |
| 2101 | /// depending on whether they straddle an eightbyte boundary. |
| 2102 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2103 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2104 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2105 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2106 | /// If a word is unused its result will be NoClass; if a type should |
| 2107 | /// be passed in Memory then at least the classification of \arg Lo |
| 2108 | /// will be Memory. |
| 2109 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2110 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2111 | /// |
| 2112 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2113 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2114 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2115 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2116 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2117 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2118 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2119 | unsigned IROffset, QualType SourceTy, |
| 2120 | unsigned SourceOffset) const; |
| 2121 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2122 | unsigned IROffset, QualType SourceTy, |
| 2123 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2124 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2125 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2126 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2127 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2128 | |
| 2129 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2130 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2131 | /// |
| 2132 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2133 | /// available. |
| 2134 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2135 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2136 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2137 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2138 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2139 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2140 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2141 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2142 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2143 | unsigned &NeededSSE) const; |
| 2144 | |
| 2145 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2146 | unsigned &NeededSSE) const; |
| 2147 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2148 | bool IsIllegalVectorType(QualType Ty) const; |
| 2149 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2150 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2151 | /// unfortunately in ways that were not always consistent with |
| 2152 | /// certain previous compilers. In particular, platforms which |
| 2153 | /// required strict binary compatibility with older versions of GCC |
| 2154 | /// may need to exempt themselves. |
| 2155 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2156 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2159 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2160 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2161 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2162 | // Clang <= 3.8 did not do this. |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 2163 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2164 | LangOptions::ClangABI::Ver3_8) |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2165 | return false; |
| 2166 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2167 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2168 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2169 | return false; |
| 2170 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2171 | return false; |
| 2172 | return true; |
| 2173 | } |
| 2174 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2175 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2176 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2177 | // 64-bit hardware. |
| 2178 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2179 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2180 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2181 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2182 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2183 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2184 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2185 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2186 | bool isPassedUsingAVXType(QualType type) const { |
| 2187 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2188 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2189 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2190 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2191 | if (info.isDirect()) { |
| 2192 | llvm::Type *ty = info.getCoerceToType(); |
| 2193 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2194 | return (vectorTy->getBitWidth() > 128); |
| 2195 | } |
| 2196 | return false; |
| 2197 | } |
| 2198 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2199 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2200 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2201 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2202 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2203 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2204 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2205 | |
| 2206 | bool has64BitPointers() const { |
| 2207 | return Has64BitPointers; |
| 2208 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2209 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2210 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2211 | bool asReturnValue) const override { |
| 2212 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2213 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2214 | bool isSwiftErrorInRegister() const override { |
| 2215 | return true; |
| 2216 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2217 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2218 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2219 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2220 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2221 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2222 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2223 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2224 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2225 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2226 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2227 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2228 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2229 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2230 | |
| 2231 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2232 | // FIXME: Assumes vectorcall is in use. |
| 2233 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2234 | } |
| 2235 | |
| 2236 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2237 | uint64_t NumMembers) const override { |
| 2238 | // FIXME: Assumes vectorcall is in use. |
| 2239 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2240 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2241 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2242 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2243 | bool asReturnValue) const override { |
| 2244 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2245 | } |
| 2246 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2247 | bool isSwiftErrorInRegister() const override { |
| 2248 | return true; |
| 2249 | } |
| 2250 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2251 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2252 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2253 | bool IsVectorCall, bool IsRegCall) const; |
| 2254 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2255 | const ABIArgInfo ¤t) const; |
| 2256 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2257 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2258 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2259 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2260 | }; |
| 2261 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2262 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2263 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2264 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2265 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2266 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2267 | const X86_64ABIInfo &getABIInfo() const { |
| 2268 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2269 | } |
| 2270 | |
Akira Hatanaka | 65bb3f9 | 2019-03-21 19:59:49 +0000 | [diff] [blame^] | 2271 | /// Disable tail call on x86-64. The epilogue code before the tail jump blocks |
| 2272 | /// the autoreleaseRV/retainRV optimization. |
| 2273 | bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override { |
| 2274 | return true; |
| 2275 | } |
| 2276 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2277 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2278 | return 7; |
| 2279 | } |
| 2280 | |
| 2281 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2282 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2283 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2284 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2285 | // 0-15 are the 16 integer registers. |
| 2286 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2287 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2288 | return false; |
| 2289 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2290 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2291 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2292 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2293 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2294 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2295 | } |
| 2296 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2297 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2298 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2299 | // The default CC on x86-64 sets %al to the number of SSA |
| 2300 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2301 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2302 | // that when AVX types are involved: the ABI explicitly states it is |
| 2303 | // undefined, and it doesn't work in practice because of how the ABI |
| 2304 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2305 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2306 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2307 | for (CallArgList::const_iterator |
| 2308 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2309 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2310 | HasAVXType = true; |
| 2311 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2314 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2315 | if (!HasAVXType) |
| 2316 | return true; |
| 2317 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2318 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2319 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2322 | llvm::Constant * |
| 2323 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2324 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2325 | (0x06 << 8) | // .+0x08 |
| 2326 | ('v' << 16) | |
| 2327 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2328 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2329 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2330 | |
| 2331 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2332 | CodeGen::CodeGenModule &CGM) const override { |
| 2333 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2334 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2335 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2336 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2337 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2338 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2339 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2340 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2341 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2342 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2343 | } |
| 2344 | } |
| 2345 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2346 | }; |
| 2347 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2348 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2349 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2350 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2351 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2352 | |
| 2353 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2354 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2355 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2356 | // If the argument contains a space, enclose it in quotes. |
| 2357 | if (Lib.find(" ") != StringRef::npos) |
| 2358 | Opt += "\"" + Lib.str() + "\""; |
| 2359 | else |
| 2360 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2361 | } |
| 2362 | }; |
| 2363 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2364 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2365 | // If the argument does not end in .lib, automatically add the suffix. |
| 2366 | // If the argument contains a space, enclose it in quotes. |
| 2367 | // This matches the behavior of MSVC. |
| 2368 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2369 | std::string ArgStr = Quote ? "\"" : ""; |
| 2370 | ArgStr += Lib; |
Martin Storsjo | 3cd67c9 | 2018-10-10 09:01:00 +0000 | [diff] [blame] | 2371 | if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2372 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2373 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2374 | return ArgStr; |
| 2375 | } |
| 2376 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2377 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2378 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2379 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2380 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2381 | unsigned NumRegisterParameters) |
| 2382 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2383 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2384 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2385 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2386 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2387 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2388 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2389 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2390 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2391 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2392 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2393 | |
| 2394 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2395 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2396 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2397 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2398 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2399 | }; |
| 2400 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2401 | static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2402 | CodeGen::CodeGenModule &CGM) { |
| 2403 | if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2404 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2405 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2406 | Fn->addFnAttr("stack-probe-size", |
| 2407 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2408 | if (CGM.getCodeGenOpts().NoStackArgProbe) |
| 2409 | Fn->addFnAttr("no-stack-arg-probe"); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2410 | } |
| 2411 | } |
| 2412 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2413 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2414 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2415 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2416 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2417 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2418 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2421 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2422 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2423 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2424 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2425 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2426 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2427 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2428 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2429 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2430 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2431 | return 7; |
| 2432 | } |
| 2433 | |
| 2434 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2435 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2436 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2438 | // 0-15 are the 16 integer registers. |
| 2439 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2440 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2441 | return false; |
| 2442 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2443 | |
| 2444 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2445 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2446 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2447 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2448 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2449 | |
| 2450 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2451 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2452 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2453 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2454 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2455 | }; |
| 2456 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2457 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2458 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2459 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2460 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2461 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2462 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2463 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2464 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2465 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2466 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2467 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2468 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2469 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2470 | } |
| 2471 | } |
| 2472 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2473 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2474 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2475 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2476 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2477 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2478 | Class &Hi) const { |
| 2479 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2480 | // |
| 2481 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2482 | // memory. |
| 2483 | // |
| 2484 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2485 | // memory. |
| 2486 | // |
| 2487 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2488 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2489 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2490 | // ABI working for processors that don't support the __m256 type. |
| 2491 | // |
| 2492 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2493 | // |
| 2494 | // Some of these are enforced by the merging logic. Others can arise |
| 2495 | // only with unions; for example: |
| 2496 | // union { _Complex double; unsigned; } |
| 2497 | // |
| 2498 | // Note that clauses (b) and (c) were added in 0.98. |
| 2499 | // |
| 2500 | if (Hi == Memory) |
| 2501 | Lo = Memory; |
| 2502 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2503 | Lo = Memory; |
| 2504 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2505 | Lo = Memory; |
| 2506 | if (Hi == SSEUp && Lo != SSE) |
| 2507 | Hi = SSE; |
| 2508 | } |
| 2509 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2510 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2511 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2512 | // classified recursively so that always two fields are |
| 2513 | // considered. The resulting class is calculated according to |
| 2514 | // the classes of the fields in the eightbyte: |
| 2515 | // |
| 2516 | // (a) If both classes are equal, this is the resulting class. |
| 2517 | // |
| 2518 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2519 | // the other class. |
| 2520 | // |
| 2521 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2522 | // class. |
| 2523 | // |
| 2524 | // (d) If one of the classes is INTEGER, the result is the |
| 2525 | // INTEGER. |
| 2526 | // |
| 2527 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2528 | // MEMORY is used as class. |
| 2529 | // |
| 2530 | // (f) Otherwise class SSE is used. |
| 2531 | |
| 2532 | // Accum should never be memory (we should have returned) or |
| 2533 | // ComplexX87 (because this cannot be passed in a structure). |
| 2534 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2535 | "Invalid accumulated classification during merge."); |
| 2536 | if (Accum == Field || Field == NoClass) |
| 2537 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2538 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2539 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2540 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2541 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2542 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2543 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2544 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2545 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2546 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2547 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2550 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2551 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2552 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2553 | // Class pairs with appropriate constructor methods for the various |
| 2554 | // situations. |
| 2555 | |
| 2556 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2557 | // shouldn't be passed in registers for example, so there is no chance they |
| 2558 | // can straddle an eightbyte. Verify & simplify. |
| 2559 | |
| 2560 | Lo = Hi = NoClass; |
| 2561 | |
| 2562 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2563 | Current = Memory; |
| 2564 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2565 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2566 | BuiltinType::Kind k = BT->getKind(); |
| 2567 | |
| 2568 | if (k == BuiltinType::Void) { |
| 2569 | Current = NoClass; |
| 2570 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2571 | Lo = Integer; |
| 2572 | Hi = Integer; |
| 2573 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2574 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2575 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2576 | Current = SSE; |
| 2577 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2578 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2579 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2580 | Lo = SSE; |
| 2581 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2582 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2583 | Lo = X87; |
| 2584 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2585 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2586 | Current = SSE; |
| 2587 | } else |
| 2588 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2589 | } |
| 2590 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2591 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2592 | return; |
| 2593 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2594 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2595 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2596 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2597 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2598 | return; |
| 2599 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2601 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2602 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2603 | return; |
| 2604 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2606 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2607 | if (Ty->isMemberFunctionPointerType()) { |
| 2608 | if (Has64BitPointers) { |
| 2609 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2610 | // Lo and Hi now. |
| 2611 | Lo = Hi = Integer; |
| 2612 | } else { |
| 2613 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2614 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2615 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2616 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2617 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2618 | Lo = Hi = Integer; |
| 2619 | } else { |
| 2620 | Current = Integer; |
| 2621 | } |
| 2622 | } |
| 2623 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2624 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2625 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2626 | return; |
| 2627 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2629 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2630 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2631 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2632 | // gcc passes the following as integer: |
| 2633 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2634 | // 2 bytes - <2 x char>, <1 x short> |
| 2635 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2636 | Current = Integer; |
| 2637 | |
| 2638 | // If this type crosses an eightbyte boundary, it should be |
| 2639 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2640 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2641 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2642 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2643 | Hi = Lo; |
| 2644 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2645 | QualType ElementType = VT->getElementType(); |
| 2646 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2647 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2648 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2649 | return; |
| 2650 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2651 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2652 | // pass them as integer. For platforms where clang is the de facto |
| 2653 | // platform compiler, we must continue to use integer. |
| 2654 | if (!classifyIntegerMMXAsSSE() && |
| 2655 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2656 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2657 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2658 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2659 | Current = Integer; |
| 2660 | else |
| 2661 | Current = SSE; |
| 2662 | |
| 2663 | // If this type crosses an eightbyte boundary, it should be |
| 2664 | // split. |
| 2665 | if (OffsetBase && OffsetBase != 64) |
| 2666 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2667 | } else if (Size == 128 || |
| 2668 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2669 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2670 | // least significant one belongs to class SSE and all the others to class |
| 2671 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2672 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2673 | // This design isn't correct for 256-bits, but since there're no cases |
| 2674 | // where the upper parts would need to be inspected, avoid adding |
| 2675 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2676 | // |
| 2677 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2678 | // registers if they are "named", i.e. not part of the "..." of a |
| 2679 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2680 | // |
| 2681 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2682 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2683 | Lo = SSE; |
| 2684 | Hi = SSEUp; |
| 2685 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2686 | return; |
| 2687 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2688 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2689 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2690 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2691 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2692 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2693 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2694 | if (Size <= 64) |
| 2695 | Current = Integer; |
| 2696 | else if (Size <= 128) |
| 2697 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2698 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2699 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2700 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2701 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2702 | } else if (ET == getContext().LongDoubleTy) { |
| 2703 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2704 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2705 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2706 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2707 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2708 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2709 | Lo = Hi = SSE; |
| 2710 | else |
| 2711 | llvm_unreachable("unexpected long double representation!"); |
| 2712 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2713 | |
| 2714 | // If this complex type crosses an eightbyte boundary then it |
| 2715 | // should be split. |
| 2716 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2717 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2718 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2719 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2723 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2724 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2725 | // Arrays are treated like structures. |
| 2726 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2727 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2728 | |
| 2729 | // 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] | 2730 | // than eight eightbytes, ..., it has class MEMORY. |
| 2731 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2732 | return; |
| 2733 | |
| 2734 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2735 | // fields, it has class MEMORY. |
| 2736 | // |
| 2737 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2738 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2739 | return; |
| 2740 | |
| 2741 | // Otherwise implement simplified merge. We could be smarter about |
| 2742 | // this, but it isn't worth it and would be harder to verify. |
| 2743 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2744 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2745 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2746 | |
| 2747 | // The only case a 256-bit wide vector could be used is when the array |
| 2748 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2749 | // 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] | 2750 | // |
| 2751 | if (Size > 128 && |
| 2752 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2753 | return; |
| 2754 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2755 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2756 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2757 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2758 | Lo = merge(Lo, FieldLo); |
| 2759 | Hi = merge(Hi, FieldHi); |
| 2760 | if (Lo == Memory || Hi == Memory) |
| 2761 | break; |
| 2762 | } |
| 2763 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2764 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2765 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2766 | return; |
| 2767 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2768 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2769 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2770 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2771 | |
| 2772 | // 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] | 2773 | // than eight eightbytes, ..., it has class MEMORY. |
| 2774 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2775 | return; |
| 2776 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2777 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2778 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2779 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2780 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2781 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2782 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2783 | const RecordDecl *RD = RT->getDecl(); |
| 2784 | |
| 2785 | // Assume variable sized types are passed in memory. |
| 2786 | if (RD->hasFlexibleArrayMember()) |
| 2787 | return; |
| 2788 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2789 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2790 | |
| 2791 | // Reset Lo class, this will be recomputed. |
| 2792 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2793 | |
| 2794 | // If this is a C++ record, classify the bases first. |
| 2795 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2796 | for (const auto &I : CXXRD->bases()) { |
| 2797 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2798 | "Unexpected base class!"); |
| 2799 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2800 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2801 | |
| 2802 | // Classify this field. |
| 2803 | // |
| 2804 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2805 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2806 | // initialized to class NO_CLASS. |
| 2807 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2808 | uint64_t Offset = |
| 2809 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2810 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2811 | Lo = merge(Lo, FieldLo); |
| 2812 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2813 | if (Lo == Memory || Hi == Memory) { |
| 2814 | postMerge(Size, Lo, Hi); |
| 2815 | return; |
| 2816 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2821 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2822 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2823 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2824 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2825 | bool BitField = i->isBitField(); |
| 2826 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2827 | // Ignore padding bit-fields. |
| 2828 | if (BitField && i->isUnnamedBitfield()) |
| 2829 | continue; |
| 2830 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2831 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2832 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2833 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2834 | // The only case a 256-bit wide vector could be used is when the struct |
| 2835 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2836 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2837 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2838 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2839 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2840 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2841 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2842 | return; |
| 2843 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2844 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2845 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2846 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2847 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2848 | return; |
| 2849 | } |
| 2850 | |
| 2851 | // Classify this field. |
| 2852 | // |
| 2853 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2854 | // exceeds a single eightbyte, each is classified |
| 2855 | // separately. Each eightbyte gets initialized to class |
| 2856 | // NO_CLASS. |
| 2857 | Class FieldLo, FieldHi; |
| 2858 | |
| 2859 | // Bit-fields require special handling, they do not force the |
| 2860 | // structure to be passed in memory even if unaligned, and |
| 2861 | // therefore they can straddle an eightbyte. |
| 2862 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2863 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2864 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2865 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2866 | |
| 2867 | uint64_t EB_Lo = Offset / 64; |
| 2868 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2869 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2870 | if (EB_Lo) { |
| 2871 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2872 | FieldLo = NoClass; |
| 2873 | FieldHi = Integer; |
| 2874 | } else { |
| 2875 | FieldLo = Integer; |
| 2876 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2877 | } |
| 2878 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2879 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2880 | Lo = merge(Lo, FieldLo); |
| 2881 | Hi = merge(Hi, FieldHi); |
| 2882 | if (Lo == Memory || Hi == Memory) |
| 2883 | break; |
| 2884 | } |
| 2885 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2886 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2890 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2891 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2892 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2893 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2894 | // Treat an enum type as its underlying type. |
| 2895 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2896 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2897 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2898 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2899 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2902 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2905 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2906 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2907 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2908 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2909 | if (Size <= 64 || Size > LargestVector) |
| 2910 | return true; |
| 2911 | } |
| 2912 | |
| 2913 | return false; |
| 2914 | } |
| 2915 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2916 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2917 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2918 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2919 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2920 | // |
| 2921 | // This assumption is optimistic, as there could be free registers available |
| 2922 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2923 | // the argument in the free register. This does not seem to happen currently, |
| 2924 | // but this code would be much safer if we could mark the argument with |
| 2925 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2926 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2927 | // Treat an enum type as its underlying type. |
| 2928 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2929 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2930 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2931 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2932 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2933 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2934 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2935 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2936 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2937 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2938 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2939 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2940 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2941 | |
| 2942 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2943 | // is important for good codegen. |
| 2944 | // |
| 2945 | // We do this by coercing the value into a scalar type which the backend can |
| 2946 | // handle naturally (i.e., without using byval). |
| 2947 | // |
| 2948 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2949 | // free integer registers. Doing this when there are free integer registers |
| 2950 | // would require more care, as we would have to ensure that the coerced value |
| 2951 | // did not claim the unused register. That would require either reording the |
| 2952 | // arguments to the function (so that any subsequent inreg values came first), |
| 2953 | // or only doing this optimization when there were no following arguments that |
| 2954 | // might be inreg. |
| 2955 | // |
| 2956 | // We currently expect it to be rare (particularly in well written code) for |
| 2957 | // arguments to be passed on the stack when there are still free integer |
| 2958 | // registers available (this would typically imply large structs being passed |
| 2959 | // by value), so this seems like a fair tradeoff for now. |
| 2960 | // |
| 2961 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2962 | // attributes. See PR12193. |
| 2963 | if (freeIntRegs == 0) { |
| 2964 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2965 | |
| 2966 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2967 | // type, which will end up on the stack (with alignment 8). |
| 2968 | if (Align == 8 && Size <= 64) |
| 2969 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2970 | Size)); |
| 2971 | } |
| 2972 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2973 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2976 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2977 | /// 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] | 2978 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2979 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2980 | // vectors; strip them off if present. |
| 2981 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2982 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2983 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2984 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2985 | if (isa<llvm::VectorType>(IRType) || |
| 2986 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2987 | return IRType; |
| 2988 | |
| 2989 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2990 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2991 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2992 | |
| 2993 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2994 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2995 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2998 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2999 | /// is known to either be off the end of the specified type or being in |
| 3000 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 3001 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 3002 | /// classification that put one of the two halves in the INTEGER class. |
| 3003 | /// |
| 3004 | /// It is conservatively correct to return false. |
| 3005 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 3006 | unsigned EndBit, ASTContext &Context) { |
| 3007 | // If the bytes being queried are off the end of the type, there is no user |
| 3008 | // data hiding here. This handles analysis of builtins, vectors and other |
| 3009 | // types that don't contain interesting padding. |
| 3010 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 3011 | if (TySize <= StartBit) |
| 3012 | return true; |
| 3013 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3014 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3015 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 3016 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 3017 | |
| 3018 | // Check each element to see if the element overlaps with the queried range. |
| 3019 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3020 | // If the element is after the span we care about, then we're done.. |
| 3021 | unsigned EltOffset = i*EltSize; |
| 3022 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3024 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 3025 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3026 | EndBit-EltOffset, Context)) |
| 3027 | return false; |
| 3028 | } |
| 3029 | // If it overlaps no elements, then it is safe to process as padding. |
| 3030 | return true; |
| 3031 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3032 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3033 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3034 | const RecordDecl *RD = RT->getDecl(); |
| 3035 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3036 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3037 | // If this is a C++ record, check the bases first. |
| 3038 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3039 | for (const auto &I : CXXRD->bases()) { |
| 3040 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3041 | "Unexpected base class!"); |
| 3042 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3043 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3044 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3045 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3046 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3047 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3048 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3049 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3050 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3051 | EndBit-BaseOffset, Context)) |
| 3052 | return false; |
| 3053 | } |
| 3054 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3055 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3056 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3057 | // this could be sped up a lot by being smarter about queried fields, |
| 3058 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3059 | // much. |
| 3060 | unsigned idx = 0; |
| 3061 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3062 | i != e; ++i, ++idx) { |
| 3063 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3064 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3065 | // If we found a field after the region we care about, then we're done. |
| 3066 | if (FieldOffset >= EndBit) break; |
| 3067 | |
| 3068 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3069 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3070 | Context)) |
| 3071 | return false; |
| 3072 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3073 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3074 | // If nothing in this record overlapped the area of interest, then we're |
| 3075 | // clean. |
| 3076 | return true; |
| 3077 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3078 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3079 | return false; |
| 3080 | } |
| 3081 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3082 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3083 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3084 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3085 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3086 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3087 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3088 | // Base case if we find a float. |
| 3089 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3090 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3092 | // 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] | 3093 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3094 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3095 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3096 | IROffset -= SL->getElementOffset(Elt); |
| 3097 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3098 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3099 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3100 | // 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] | 3101 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3102 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3103 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3104 | IROffset -= IROffset/EltSize*EltSize; |
| 3105 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3106 | } |
| 3107 | |
| 3108 | return false; |
| 3109 | } |
| 3110 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3111 | |
| 3112 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3113 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3114 | llvm::Type *X86_64ABIInfo:: |
| 3115 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3116 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3117 | // 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] | 3118 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3119 | // structs that contain 3 floats. |
| 3120 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3121 | SourceOffset*8+64, getContext())) |
| 3122 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3123 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3124 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3125 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3126 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3127 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3128 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3129 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3131 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3132 | } |
| 3133 | |
| 3134 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3135 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3136 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3137 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3138 | /// 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] | 3139 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3140 | /// etc). |
| 3141 | /// |
| 3142 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3143 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3144 | /// the 8-byte value references. PrefType may be null. |
| 3145 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3146 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3147 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3148 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3149 | llvm::Type *X86_64ABIInfo:: |
| 3150 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3151 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3152 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3153 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3154 | if (IROffset == 0) { |
| 3155 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3156 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3157 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3158 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3159 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3160 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3161 | // goodness in the source type is just tail padding. This is allowed to |
| 3162 | // kick in for struct {double,int} on the int, but not on |
| 3163 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3164 | // have to do this analysis on the source type because we can't depend on |
| 3165 | // unions being lowered a specific way etc. |
| 3166 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3167 | IRType->isIntegerTy(32) || |
| 3168 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3169 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3170 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3171 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3172 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3173 | SourceOffset*8+64, getContext())) |
| 3174 | return IRType; |
| 3175 | } |
| 3176 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3177 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3178 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3179 | // 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] | 3180 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3181 | if (IROffset < SL->getSizeInBytes()) { |
| 3182 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3183 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3184 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3185 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3186 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3187 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3188 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3190 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3191 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3192 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3193 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3194 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3195 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3196 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3198 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3199 | // 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] | 3200 | unsigned TySizeInBytes = |
| 3201 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3202 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3203 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3204 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3205 | // It is always safe to classify this as an integer type up to i64 that |
| 3206 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3207 | return llvm::IntegerType::get(getVMContext(), |
| 3208 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3211 | |
| 3212 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3213 | /// be used as elements of a two register pair to pass or return, return a |
| 3214 | /// first class aggregate to represent them. For example, if the low part of |
| 3215 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3216 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3217 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3218 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3219 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3220 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3221 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3222 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3223 | // the second element at offset 8. Check for this: |
| 3224 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3225 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3226 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3227 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3228 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3229 | // To handle this, we have to increase the size of the low part so that the |
| 3230 | // second element will start at an 8 byte offset. We can't increase the size |
| 3231 | // of the second element because it might make us access off the end of the |
| 3232 | // struct. |
| 3233 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3234 | // There are usually two sorts of types the ABI generation code can produce |
| 3235 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3236 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3237 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3238 | // Promote these to a larger type. |
| 3239 | if (Lo->isFloatTy()) |
| 3240 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3241 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3242 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3243 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3244 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3245 | } |
| 3246 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3247 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3248 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3249 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3250 | // Verify that the second element is at an 8-byte offset. |
| 3251 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3252 | "Invalid x86-64 argument pair!"); |
| 3253 | return Result; |
| 3254 | } |
| 3255 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3256 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3257 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3258 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3259 | // classification algorithm. |
| 3260 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3261 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3262 | |
| 3263 | // Check some invariants. |
| 3264 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3265 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3266 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3267 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3268 | switch (Lo) { |
| 3269 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3270 | if (Hi == NoClass) |
| 3271 | return ABIArgInfo::getIgnore(); |
| 3272 | // If the low part is just padding, it takes no register, leave ResType |
| 3273 | // null. |
| 3274 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3275 | "Unknown missing lo part"); |
| 3276 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3277 | |
| 3278 | case SSEUp: |
| 3279 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3280 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3281 | |
| 3282 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3283 | // hidden argument. |
| 3284 | case Memory: |
| 3285 | return getIndirectReturnResult(RetTy); |
| 3286 | |
| 3287 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3288 | // available register of the sequence %rax, %rdx is used. |
| 3289 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3290 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3291 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3292 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3293 | // so that the parameter gets the right LLVM IR attributes. |
| 3294 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3295 | // Treat an enum type as its underlying type. |
| 3296 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3297 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3298 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3299 | if (RetTy->isIntegralOrEnumerationType() && |
| 3300 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3301 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3302 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3303 | break; |
| 3304 | |
| 3305 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3306 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3307 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3308 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3309 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3310 | |
| 3311 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3312 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3313 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3314 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3315 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3316 | |
| 3317 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3318 | // part of the value is returned in %st0 and the imaginary part in |
| 3319 | // %st1. |
| 3320 | case ComplexX87: |
| 3321 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3322 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3323 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3324 | break; |
| 3325 | } |
| 3326 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3327 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3328 | switch (Hi) { |
| 3329 | // Memory was handled previously and X87 should |
| 3330 | // never occur as a hi class. |
| 3331 | case Memory: |
| 3332 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3333 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3334 | |
| 3335 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3336 | case NoClass: |
| 3337 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3338 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3339 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3340 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3341 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3342 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3343 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3344 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3345 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3346 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3347 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3348 | break; |
| 3349 | |
| 3350 | // 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] | 3351 | // is passed in the next available eightbyte chunk if the last used |
| 3352 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3353 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3354 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3355 | case SSEUp: |
| 3356 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3357 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3358 | break; |
| 3359 | |
| 3360 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3361 | // returned together with the previous X87 value in %st0. |
| 3362 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3363 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3364 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3365 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3366 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3367 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3368 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3369 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3370 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3371 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3372 | break; |
| 3373 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3374 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3375 | // 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] | 3376 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3377 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3378 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3379 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3380 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3381 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3384 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3385 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3386 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3387 | const |
| 3388 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3389 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3390 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3391 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3392 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3393 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3394 | // Check some invariants. |
| 3395 | // FIXME: Enforce these by construction. |
| 3396 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3397 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3398 | |
| 3399 | neededInt = 0; |
| 3400 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3401 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3402 | switch (Lo) { |
| 3403 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3404 | if (Hi == NoClass) |
| 3405 | return ABIArgInfo::getIgnore(); |
| 3406 | // If the low part is just padding, it takes no register, leave ResType |
| 3407 | // null. |
| 3408 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3409 | "Unknown missing lo part"); |
| 3410 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3411 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3412 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3413 | // on the stack. |
| 3414 | case Memory: |
| 3415 | |
| 3416 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3417 | // COMPLEX_X87, it is passed in memory. |
| 3418 | case X87: |
| 3419 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3420 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3421 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3422 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3423 | |
| 3424 | case SSEUp: |
| 3425 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3426 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3427 | |
| 3428 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3429 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3430 | // and %r9 is used. |
| 3431 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3432 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3433 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3434 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3435 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3436 | |
| 3437 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3438 | // so that the parameter gets the right LLVM IR attributes. |
| 3439 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3440 | // Treat an enum type as its underlying type. |
| 3441 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3442 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3443 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3444 | if (Ty->isIntegralOrEnumerationType() && |
| 3445 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3446 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3447 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3448 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3449 | break; |
| 3450 | |
| 3451 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3452 | // available SSE register is used, the registers are taken in the |
| 3453 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3454 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3455 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3456 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3457 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3458 | break; |
| 3459 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3460 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3461 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3462 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3463 | switch (Hi) { |
| 3464 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3465 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3466 | // which is passed in memory. |
| 3467 | case Memory: |
| 3468 | case X87: |
| 3469 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3470 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3471 | |
| 3472 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3473 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3474 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3475 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3476 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3477 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3478 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3479 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3480 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3481 | break; |
| 3482 | |
| 3483 | // X87Up generally doesn't occur here (long double is passed in |
| 3484 | // memory), except in situations involving unions. |
| 3485 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3486 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3487 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3488 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3489 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3490 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3491 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3492 | ++neededSSE; |
| 3493 | break; |
| 3494 | |
| 3495 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3496 | // 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] | 3497 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3498 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3499 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3500 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3501 | break; |
| 3502 | } |
| 3503 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3504 | // If a high part was specified, merge it together with the low part. It is |
| 3505 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3506 | // first class struct aggregate with the high and low part: {low, high} |
| 3507 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3508 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3509 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3510 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3513 | ABIArgInfo |
| 3514 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3515 | unsigned &NeededSSE) const { |
| 3516 | auto RT = Ty->getAs<RecordType>(); |
| 3517 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3518 | |
| 3519 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3520 | return getIndirectReturnResult(Ty); |
| 3521 | |
| 3522 | // Sum up bases |
| 3523 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3524 | if (CXXRD->isDynamicClass()) { |
| 3525 | NeededInt = NeededSSE = 0; |
| 3526 | return getIndirectReturnResult(Ty); |
| 3527 | } |
| 3528 | |
| 3529 | for (const auto &I : CXXRD->bases()) |
| 3530 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3531 | .isIndirect()) { |
| 3532 | NeededInt = NeededSSE = 0; |
| 3533 | return getIndirectReturnResult(Ty); |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | // Sum up members |
| 3538 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3539 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3540 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3541 | .isIndirect()) { |
| 3542 | NeededInt = NeededSSE = 0; |
| 3543 | return getIndirectReturnResult(Ty); |
| 3544 | } |
| 3545 | } else { |
| 3546 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3547 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3548 | LocalNeededSSE, true) |
| 3549 | .isIndirect()) { |
| 3550 | NeededInt = NeededSSE = 0; |
| 3551 | return getIndirectReturnResult(Ty); |
| 3552 | } |
| 3553 | NeededInt += LocalNeededInt; |
| 3554 | NeededSSE += LocalNeededSSE; |
| 3555 | } |
| 3556 | } |
| 3557 | |
| 3558 | return ABIArgInfo::getDirect(); |
| 3559 | } |
| 3560 | |
| 3561 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3562 | unsigned &NeededInt, |
| 3563 | unsigned &NeededSSE) const { |
| 3564 | |
| 3565 | NeededInt = 0; |
| 3566 | NeededSSE = 0; |
| 3567 | |
| 3568 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3569 | } |
| 3570 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3571 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3572 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3573 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3574 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3575 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3576 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3577 | if (CallingConv == llvm::CallingConv::Win64) { |
| 3578 | WinX86_64ABIInfo Win64ABIInfo(CGT); |
| 3579 | Win64ABIInfo.computeInfo(FI); |
| 3580 | return; |
| 3581 | } |
| 3582 | |
| 3583 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3584 | |
| 3585 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3586 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3587 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3588 | unsigned NeededInt, NeededSSE; |
| 3589 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 3590 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3591 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3592 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3593 | FI.getReturnInfo() = |
| 3594 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3595 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3596 | FreeIntRegs -= NeededInt; |
| 3597 | FreeSSERegs -= NeededSSE; |
| 3598 | } else { |
| 3599 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3600 | } |
| 3601 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3602 | // Complex Long Double Type is passed in Memory when Regcall |
| 3603 | // calling convention is used. |
| 3604 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3605 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3606 | getContext().LongDoubleTy) |
| 3607 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3608 | } else |
| 3609 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3610 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3611 | |
| 3612 | // If the return value is indirect, then the hidden argument is consuming one |
| 3613 | // integer register. |
| 3614 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3615 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3616 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3617 | // The chain argument effectively gives us another free register. |
| 3618 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3619 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3620 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3621 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3622 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3623 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3624 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3625 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3626 | it != ie; ++it, ++ArgNo) { |
| 3627 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3628 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3629 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3630 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3631 | else |
| 3632 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3633 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3634 | |
| 3635 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3636 | // eightbyte of an argument, the whole argument is passed on the |
| 3637 | // stack. If registers have already been assigned for some |
| 3638 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3639 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3640 | FreeIntRegs -= NeededInt; |
| 3641 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3642 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3643 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3648 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3649 | Address VAListAddr, QualType Ty) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3650 | Address overflow_arg_area_p = |
| 3651 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3652 | llvm::Value *overflow_arg_area = |
| 3653 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3654 | |
| 3655 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3656 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3657 | // It isn't stated explicitly in the standard, but in practice we use |
| 3658 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3659 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3660 | if (Align > CharUnits::fromQuantity(8)) { |
| 3661 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3662 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | // 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] | 3666 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3667 | llvm::Value *Res = |
| 3668 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3669 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3670 | |
| 3671 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3672 | // l->overflow_arg_area + sizeof(type). |
| 3673 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3674 | // an 8 byte boundary. |
| 3675 | |
| 3676 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3677 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3678 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3679 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3680 | "overflow_arg_area.next"); |
| 3681 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3682 | |
| 3683 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3684 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3687 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3688 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3689 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3690 | // struct { |
| 3691 | // i32 gp_offset; |
| 3692 | // i32 fp_offset; |
| 3693 | // i8* overflow_arg_area; |
| 3694 | // i8* reg_save_area; |
| 3695 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3696 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3697 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3698 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3699 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3700 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3701 | |
| 3702 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3703 | // in the registers. If not go to step 7. |
| 3704 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3705 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3706 | |
| 3707 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3708 | // general purpose registers needed to pass type and num_fp to hold |
| 3709 | // the number of floating point registers needed. |
| 3710 | |
| 3711 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3712 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3713 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3714 | // |
| 3715 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3716 | // register save space). |
| 3717 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3718 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3719 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3720 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3721 | if (neededInt) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3722 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3723 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3724 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3725 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | if (neededSSE) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3729 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3730 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3731 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3732 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3733 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3734 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3735 | } |
| 3736 | |
| 3737 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3738 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3739 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3740 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3741 | |
| 3742 | // Emit code to load the value if it was passed in registers. |
| 3743 | |
| 3744 | CGF.EmitBlock(InRegBlock); |
| 3745 | |
| 3746 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3747 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3748 | // copying to a temporary location in case the parameter is passed |
| 3749 | // in different register classes or requires an alignment greater |
| 3750 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3751 | // |
| 3752 | // FIXME: This really results in shameful code when we end up needing to |
| 3753 | // collect arguments from different places; often what should result in a |
| 3754 | // simple assembling of a structure from scattered addresses has many more |
| 3755 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3756 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3757 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3758 | CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3759 | |
| 3760 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3761 | if (neededInt && neededSSE) { |
| 3762 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3763 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3764 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3765 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3766 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3767 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3768 | llvm::Type *TyLo = ST->getElementType(0); |
| 3769 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3770 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3771 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3772 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3773 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3774 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3775 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3776 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3777 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3778 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3779 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3780 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3781 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3782 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3783 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3784 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3785 | |
| 3786 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3787 | V = CGF.Builder.CreateAlignedLoad( |
| 3788 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3789 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3790 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3791 | |
| 3792 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3793 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3794 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3795 | CharUnits::fromQuantity(8)); |
| 3796 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3797 | |
| 3798 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3799 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3800 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3801 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3802 | CharUnits TyAlign = SizeAlign.second; |
| 3803 | |
| 3804 | // Copy into a temporary if the type is more aligned than the |
| 3805 | // register save area. |
| 3806 | if (TyAlign.getQuantity() > 8) { |
| 3807 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3808 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3809 | RegAddr = Tmp; |
| 3810 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3811 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3812 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3813 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3814 | CharUnits::fromQuantity(16)); |
| 3815 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3816 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3817 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3818 | // SSE registers are spaced 16 bytes apart in the register save |
| 3819 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3820 | // The ABI isn't explicit about this, but it seems reasonable |
| 3821 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3822 | // naturally 16-byte aligned and the prologue is expected to store |
| 3823 | // all the SSE registers to the RSA. |
| 3824 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3825 | CharUnits::fromQuantity(16)); |
| 3826 | Address RegAddrHi = |
| 3827 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3828 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3829 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3830 | ? AI.getCoerceToType() |
| 3831 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3832 | llvm::Value *V; |
| 3833 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3834 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3835 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3836 | RegAddrLo, ST->getStructElementType(0))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3837 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3838 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3839 | RegAddrHi, ST->getStructElementType(1))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3840 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3841 | |
| 3842 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3846 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3847 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3848 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3849 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3850 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3851 | gp_offset_p); |
| 3852 | } |
| 3853 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3854 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3855 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3856 | fp_offset_p); |
| 3857 | } |
| 3858 | CGF.EmitBranch(ContBlock); |
| 3859 | |
| 3860 | // Emit code to load the value if it was passed in memory. |
| 3861 | |
| 3862 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3863 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3864 | |
| 3865 | // Return the appropriate result. |
| 3866 | |
| 3867 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3868 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3869 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3870 | return ResAddr; |
| 3871 | } |
| 3872 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3873 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3874 | QualType Ty) const { |
| 3875 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3876 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3877 | CharUnits::fromQuantity(8), |
| 3878 | /*allowHigherAlign*/ false); |
| 3879 | } |
| 3880 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3881 | ABIArgInfo |
| 3882 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3883 | const ABIArgInfo ¤t) const { |
| 3884 | // Assumes vectorCall calling convention. |
| 3885 | const Type *Base = nullptr; |
| 3886 | uint64_t NumElts = 0; |
| 3887 | |
| 3888 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3889 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3890 | FreeSSERegs -= NumElts; |
| 3891 | return getDirectX86Hva(); |
| 3892 | } |
| 3893 | return current; |
| 3894 | } |
| 3895 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3896 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3897 | bool IsReturnType, bool IsVectorCall, |
| 3898 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3899 | |
| 3900 | if (Ty->isVoidType()) |
| 3901 | return ABIArgInfo::getIgnore(); |
| 3902 | |
| 3903 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3904 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3905 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3906 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3907 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3908 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3909 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3910 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3911 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3912 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3913 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3914 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3918 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3919 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3920 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3921 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3922 | const Type *Base = nullptr; |
| 3923 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3924 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3925 | // other targets. |
| 3926 | if ((IsVectorCall || IsRegCall) && |
| 3927 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3928 | if (IsRegCall) { |
| 3929 | if (FreeSSERegs >= NumElts) { |
| 3930 | FreeSSERegs -= NumElts; |
| 3931 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3932 | return ABIArgInfo::getDirect(); |
| 3933 | return ABIArgInfo::getExpand(); |
| 3934 | } |
| 3935 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3936 | } else if (IsVectorCall) { |
| 3937 | if (FreeSSERegs >= NumElts && |
| 3938 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3939 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3940 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3941 | } else if (IsReturnType) { |
| 3942 | return ABIArgInfo::getExpand(); |
| 3943 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3944 | // HVAs are delayed and reclassified in the 2nd step. |
| 3945 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3946 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3947 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3950 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3951 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3952 | // directly. |
| 3953 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3954 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3955 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3958 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3959 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3960 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3961 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3962 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3963 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3964 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3965 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Reid Kleckner | 08f64e9 | 2018-10-31 17:43:55 +0000 | [diff] [blame] | 3968 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3969 | switch (BT->getKind()) { |
| 3970 | case BuiltinType::Bool: |
| 3971 | // Bool type is always extended to the ABI, other builtin types are not |
| 3972 | // extended. |
| 3973 | return ABIArgInfo::getExtend(Ty); |
| 3974 | |
| 3975 | case BuiltinType::LongDouble: |
| 3976 | // Mingw64 GCC uses the old 80 bit extended precision floating point |
| 3977 | // unit. It passes them indirectly through memory. |
| 3978 | if (IsMingw64) { |
| 3979 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3980 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
| 3981 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3982 | } |
| 3983 | break; |
| 3984 | |
| 3985 | case BuiltinType::Int128: |
| 3986 | case BuiltinType::UInt128: |
| 3987 | // If it's a parameter type, the normal ABI rule is that arguments larger |
| 3988 | // than 8 bytes are passed indirectly. GCC follows it. We follow it too, |
| 3989 | // even though it isn't particularly efficient. |
| 3990 | if (!IsReturnType) |
| 3991 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3992 | |
| 3993 | // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. |
| 3994 | // Clang matches them for compatibility. |
| 3995 | return ABIArgInfo::getDirect( |
| 3996 | llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2)); |
| 3997 | |
| 3998 | default: |
| 3999 | break; |
| 4000 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4003 | return ABIArgInfo::getDirect(); |
| 4004 | } |
| 4005 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4006 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 4007 | unsigned FreeSSERegs, |
| 4008 | bool IsVectorCall, |
| 4009 | bool IsRegCall) const { |
| 4010 | unsigned Count = 0; |
| 4011 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4012 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 4013 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4014 | if (Count < VectorcallMaxParamNumAsReg) |
| 4015 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4016 | else { |
| 4017 | // Since these cannot be passed in registers, pretend no registers |
| 4018 | // are left. |
| 4019 | unsigned ZeroSSERegsAvail = 0; |
| 4020 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 4021 | IsVectorCall, IsRegCall); |
| 4022 | } |
| 4023 | ++Count; |
| 4024 | } |
| 4025 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4026 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4027 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4028 | } |
| 4029 | } |
| 4030 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4031 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4032 | bool IsVectorCall = |
| 4033 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4034 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 4035 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4036 | unsigned FreeSSERegs = 0; |
| 4037 | if (IsVectorCall) { |
| 4038 | // We can use up to 4 SSE return registers with vectorcall. |
| 4039 | FreeSSERegs = 4; |
| 4040 | } else if (IsRegCall) { |
| 4041 | // RegCall gives us 16 SSE registers. |
| 4042 | FreeSSERegs = 16; |
| 4043 | } |
| 4044 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4045 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4046 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 4047 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4048 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4049 | if (IsVectorCall) { |
| 4050 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4051 | FreeSSERegs = 6; |
| 4052 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4053 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4054 | FreeSSERegs = 16; |
| 4055 | } |
| 4056 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4057 | if (IsVectorCall) { |
| 4058 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4059 | } else { |
| 4060 | for (auto &I : FI.arguments()) |
| 4061 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4062 | } |
| 4063 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4066 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4067 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4068 | |
| 4069 | bool IsIndirect = false; |
| 4070 | |
| 4071 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4072 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4073 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4074 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4075 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4076 | } |
| 4077 | |
| 4078 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4079 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4080 | CharUnits::fromQuantity(8), |
| 4081 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4082 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4083 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4084 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4085 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4086 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4087 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4088 | bool IsSoftFloatABI; |
| 4089 | |
| 4090 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4091 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4092 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4093 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4094 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4095 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4096 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4097 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4098 | }; |
| 4099 | |
| 4100 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4101 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4102 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4103 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4104 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4105 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4106 | // This is recovered from gcc output. |
| 4107 | return 1; // r1 is the dedicated stack pointer |
| 4108 | } |
| 4109 | |
| 4110 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4111 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4112 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4113 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4114 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4115 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4116 | // Complex types are passed just like their elements |
| 4117 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4118 | Ty = CTy->getElementType(); |
| 4119 | |
| 4120 | if (Ty->isVectorType()) |
| 4121 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4122 | : 4); |
| 4123 | |
| 4124 | // For single-element float/vector structs, we consider the whole type |
| 4125 | // to have the same alignment requirements as its single element. |
| 4126 | const Type *AlignTy = nullptr; |
| 4127 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4128 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4129 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4130 | (BT && BT->isFloatingPoint())) |
| 4131 | AlignTy = EltType; |
| 4132 | } |
| 4133 | |
| 4134 | if (AlignTy) |
| 4135 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4136 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4137 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4138 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4139 | // TODO: this implementation is now likely redundant with |
| 4140 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4141 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4142 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4143 | if (getTarget().getTriple().isOSDarwin()) { |
| 4144 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4145 | TI.second = getParamTypeAlignment(Ty); |
| 4146 | |
| 4147 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4148 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4149 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4150 | /*AllowHigherAlign=*/true); |
| 4151 | } |
| 4152 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4153 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4154 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4155 | // TODO: Implement this. For now ignore. |
| 4156 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4157 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4160 | // struct __va_list_tag { |
| 4161 | // unsigned char gpr; |
| 4162 | // unsigned char fpr; |
| 4163 | // unsigned short reserved; |
| 4164 | // void *overflow_arg_area; |
| 4165 | // void *reg_save_area; |
| 4166 | // }; |
| 4167 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4168 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4169 | bool isInt = |
| 4170 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4171 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4172 | |
| 4173 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4174 | // with the argument-lowering code. |
| 4175 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4176 | |
| 4177 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4178 | |
| 4179 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4180 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4181 | if (isInt || IsSoftFloatABI) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4182 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4183 | } else { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4184 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4185 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4186 | |
| 4187 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4188 | |
| 4189 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4190 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4191 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4192 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4193 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4194 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4195 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4196 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4197 | |
| 4198 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4199 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4200 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4201 | |
| 4202 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4203 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4204 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4205 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4206 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4207 | // Case 1: consume registers. |
| 4208 | Address RegAddr = Address::invalid(); |
| 4209 | { |
| 4210 | CGF.EmitBlock(UsingRegs); |
| 4211 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4212 | Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4213 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4214 | CharUnits::fromQuantity(8)); |
| 4215 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4216 | |
| 4217 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4218 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4219 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4220 | CharUnits::fromQuantity(32)); |
| 4221 | } |
| 4222 | |
| 4223 | // Get the address of the saved value by scaling the number of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4224 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4225 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4226 | llvm::Value *RegOffset = |
| 4227 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4228 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4229 | RegAddr.getPointer(), RegOffset), |
| 4230 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4231 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4232 | |
| 4233 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4234 | NumRegs = |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4235 | Builder.CreateAdd(NumRegs, |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4236 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4237 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4238 | |
| 4239 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4240 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4241 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4242 | // Case 2: consume space in the overflow area. |
| 4243 | Address MemAddr = Address::invalid(); |
| 4244 | { |
| 4245 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4246 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4247 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4248 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4249 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4250 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4251 | |
| 4252 | CharUnits Size; |
| 4253 | if (!isIndirect) { |
| 4254 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4255 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4256 | } else { |
| 4257 | Size = CGF.getPointerSize(); |
| 4258 | } |
| 4259 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4260 | Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4261 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4262 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4263 | // Round up address of argument to alignment |
| 4264 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4265 | if (Align > OverflowAreaAlign) { |
| 4266 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4267 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4268 | Align); |
| 4269 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4270 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4271 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4272 | |
| 4273 | // Increase the overflow area. |
| 4274 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4275 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4276 | CGF.EmitBranch(Cont); |
| 4277 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4278 | |
| 4279 | CGF.EmitBlock(Cont); |
| 4280 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4281 | // Merge the cases with a phi. |
| 4282 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4283 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4284 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4285 | // Load the pointer if the argument was passed indirectly. |
| 4286 | if (isIndirect) { |
| 4287 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4288 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | return Result; |
| 4292 | } |
| 4293 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4294 | bool |
| 4295 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4296 | llvm::Value *Address) const { |
| 4297 | // This is calculated from the LLVM and GCC tables and verified |
| 4298 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4299 | |
| 4300 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4301 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4302 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4303 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4304 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4305 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4306 | |
| 4307 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4308 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4309 | |
| 4310 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4311 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4312 | |
| 4313 | // 64-76 are various 4-byte special-purpose registers: |
| 4314 | // 64: mq |
| 4315 | // 65: lr |
| 4316 | // 66: ctr |
| 4317 | // 67: ap |
| 4318 | // 68-75 cr0-7 |
| 4319 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4320 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4321 | |
| 4322 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4323 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4324 | |
| 4325 | // 109: vrsave |
| 4326 | // 110: vscr |
| 4327 | // 111: spe_acc |
| 4328 | // 112: spefscr |
| 4329 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4330 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4331 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4332 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4335 | // PowerPC-64 |
| 4336 | |
| 4337 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4338 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4339 | class PPC64_SVR4_ABIInfo : public SwiftABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4340 | public: |
| 4341 | enum ABIKind { |
| 4342 | ELFv1 = 0, |
| 4343 | ELFv2 |
| 4344 | }; |
| 4345 | |
| 4346 | private: |
| 4347 | static const unsigned GPRBits = 64; |
| 4348 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4349 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4350 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4351 | |
| 4352 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4353 | // will be passed in a QPX register. |
| 4354 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4355 | if (!HasQPX) |
| 4356 | return false; |
| 4357 | |
| 4358 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4359 | unsigned NumElements = VT->getNumElements(); |
| 4360 | if (NumElements == 1) |
| 4361 | return false; |
| 4362 | |
| 4363 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4364 | if (getContext().getTypeSize(Ty) <= 256) |
| 4365 | return true; |
| 4366 | } else if (VT->getElementType()-> |
| 4367 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4368 | if (getContext().getTypeSize(Ty) <= 128) |
| 4369 | return true; |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | return false; |
| 4374 | } |
| 4375 | |
| 4376 | bool IsQPXVectorTy(QualType Ty) const { |
| 4377 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4378 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4379 | |
| 4380 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4381 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4382 | bool SoftFloatABI) |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4383 | : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4384 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4385 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4386 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4387 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4388 | |
| 4389 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4390 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4391 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4392 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4393 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4394 | uint64_t Members) const override; |
| 4395 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4396 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4397 | // Example: For aggregate arguments that fit in a register, we could |
| 4398 | // use getDirectInReg (as is done below for structs containing a single |
| 4399 | // floating-point value) to avoid pushing them to memory on function |
| 4400 | // entry. This would require changing the logic in PPCISelLowering |
| 4401 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4402 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4403 | if (!getCXXABI().classifyReturnType(FI)) |
| 4404 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4405 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4406 | // We rely on the default argument classification for the most part. |
| 4407 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4408 | // 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] | 4409 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4410 | if (T) { |
| 4411 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4412 | if (IsQPXVectorTy(T) || |
| 4413 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4414 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4415 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4416 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4417 | continue; |
| 4418 | } |
| 4419 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4420 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4421 | } |
| 4422 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4423 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4424 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4425 | QualType Ty) const override; |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4426 | |
| 4427 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 4428 | bool asReturnValue) const override { |
| 4429 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4430 | } |
| 4431 | |
| 4432 | bool isSwiftErrorInRegister() const override { |
| 4433 | return false; |
| 4434 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4435 | }; |
| 4436 | |
| 4437 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4438 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4439 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4440 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4441 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4442 | bool SoftFloatABI) |
| 4443 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4444 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4445 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4446 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +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; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4453 | }; |
| 4454 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4455 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4456 | public: |
| 4457 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4458 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4459 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4460 | // This is recovered from gcc output. |
| 4461 | return 1; // r1 is the dedicated stack pointer |
| 4462 | } |
| 4463 | |
| 4464 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4465 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4466 | }; |
| 4467 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4468 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4469 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4470 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4471 | // extended to 64 bits. |
| 4472 | bool |
| 4473 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4474 | // Treat an enum type as its underlying type. |
| 4475 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4476 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4477 | |
| 4478 | // Promotable integer types are required to be promoted by the ABI. |
| 4479 | if (Ty->isPromotableIntegerType()) |
| 4480 | return true; |
| 4481 | |
| 4482 | // In addition to the usual promotable integer types, we also need to |
| 4483 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4484 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4485 | switch (BT->getKind()) { |
| 4486 | case BuiltinType::Int: |
| 4487 | case BuiltinType::UInt: |
| 4488 | return true; |
| 4489 | default: |
| 4490 | break; |
| 4491 | } |
| 4492 | |
| 4493 | return false; |
| 4494 | } |
| 4495 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4496 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4497 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4498 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4499 | // Complex types are passed just like their elements. |
| 4500 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4501 | Ty = CTy->getElementType(); |
| 4502 | |
| 4503 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4504 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4505 | if (IsQPXVectorTy(Ty)) { |
| 4506 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4507 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4508 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4509 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4510 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4511 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4512 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4513 | |
| 4514 | // For single-element float/vector structs, we consider the whole type |
| 4515 | // to have the same alignment requirements as its single element. |
| 4516 | const Type *AlignAsType = nullptr; |
| 4517 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4518 | if (EltType) { |
| 4519 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4520 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4521 | getContext().getTypeSize(EltType) == 128) || |
| 4522 | (BT && BT->isFloatingPoint())) |
| 4523 | AlignAsType = EltType; |
| 4524 | } |
| 4525 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4526 | // Likewise for ELFv2 homogeneous aggregates. |
| 4527 | const Type *Base = nullptr; |
| 4528 | uint64_t Members = 0; |
| 4529 | if (!AlignAsType && Kind == ELFv2 && |
| 4530 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4531 | AlignAsType = Base; |
| 4532 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4533 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4534 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4535 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4536 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4537 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4538 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4539 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4540 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4541 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4542 | |
| 4543 | // Otherwise, we only need alignment for any aggregate type that |
| 4544 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4545 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4546 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4547 | return CharUnits::fromQuantity(32); |
| 4548 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4549 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4550 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4551 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4554 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4555 | /// aggregate. Base is set to the base element type, and Members is set |
| 4556 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4557 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4558 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4559 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4560 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4561 | if (NElements == 0) |
| 4562 | return false; |
| 4563 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4564 | return false; |
| 4565 | Members *= NElements; |
| 4566 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4567 | const RecordDecl *RD = RT->getDecl(); |
| 4568 | if (RD->hasFlexibleArrayMember()) |
| 4569 | return false; |
| 4570 | |
| 4571 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4572 | |
| 4573 | // If this is a C++ record, check the bases first. |
| 4574 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4575 | for (const auto &I : CXXRD->bases()) { |
| 4576 | // Ignore empty records. |
| 4577 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4578 | continue; |
| 4579 | |
| 4580 | uint64_t FldMembers; |
| 4581 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4582 | return false; |
| 4583 | |
| 4584 | Members += FldMembers; |
| 4585 | } |
| 4586 | } |
| 4587 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4588 | for (const auto *FD : RD->fields()) { |
| 4589 | // Ignore (non-zero arrays of) empty records. |
| 4590 | QualType FT = FD->getType(); |
| 4591 | while (const ConstantArrayType *AT = |
| 4592 | getContext().getAsConstantArrayType(FT)) { |
| 4593 | if (AT->getSize().getZExtValue() == 0) |
| 4594 | return false; |
| 4595 | FT = AT->getElementType(); |
| 4596 | } |
| 4597 | if (isEmptyRecord(getContext(), FT, true)) |
| 4598 | continue; |
| 4599 | |
| 4600 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4601 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4602 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4603 | continue; |
| 4604 | |
| 4605 | uint64_t FldMembers; |
| 4606 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4607 | return false; |
| 4608 | |
| 4609 | Members = (RD->isUnion() ? |
| 4610 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4611 | } |
| 4612 | |
| 4613 | if (!Base) |
| 4614 | return false; |
| 4615 | |
| 4616 | // Ensure there is no padding. |
| 4617 | if (getContext().getTypeSize(Base) * Members != |
| 4618 | getContext().getTypeSize(Ty)) |
| 4619 | return false; |
| 4620 | } else { |
| 4621 | Members = 1; |
| 4622 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4623 | Members = 2; |
| 4624 | Ty = CT->getElementType(); |
| 4625 | } |
| 4626 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4627 | // Most ABIs only support float, double, and some vector type widths. |
| 4628 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4629 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4630 | |
| 4631 | // The base type must be the same for all members. Types that |
| 4632 | // agree in both total size and mode (float vs. vector) are |
| 4633 | // treated as being equivalent here. |
| 4634 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4635 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4636 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4637 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4638 | // so make sure to widen it explicitly. |
| 4639 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4640 | QualType EltTy = VT->getElementType(); |
| 4641 | unsigned NumElements = |
| 4642 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4643 | Base = getContext() |
| 4644 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4645 | .getTypePtr(); |
| 4646 | } |
| 4647 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4648 | |
| 4649 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4650 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4651 | return false; |
| 4652 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4653 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4654 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4655 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4656 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4657 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4658 | // double, long double, or 128-bit vectors. |
| 4659 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4660 | if (BT->getKind() == BuiltinType::Float || |
| 4661 | BT->getKind() == BuiltinType::Double || |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4662 | BT->getKind() == BuiltinType::LongDouble || |
| 4663 | (getContext().getTargetInfo().hasFloat128Type() && |
| 4664 | (BT->getKind() == BuiltinType::Float128))) { |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4665 | if (IsSoftFloatABI) |
| 4666 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4667 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4668 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4669 | } |
| 4670 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4671 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4672 | return true; |
| 4673 | } |
| 4674 | return false; |
| 4675 | } |
| 4676 | |
| 4677 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4678 | const Type *Base, uint64_t Members) const { |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4679 | // Vector and fp128 types require one register, other floating point types |
| 4680 | // require one or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4681 | uint32_t NumRegs = |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4682 | ((getContext().getTargetInfo().hasFloat128Type() && |
| 4683 | Base->isFloat128Type()) || |
| 4684 | Base->isVectorType()) ? 1 |
| 4685 | : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4686 | |
| 4687 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4688 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4691 | ABIArgInfo |
| 4692 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4693 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4694 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4695 | if (Ty->isAnyComplexType()) |
| 4696 | return ABIArgInfo::getDirect(); |
| 4697 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4698 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4699 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4700 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4701 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4702 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4703 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4704 | else if (Size < 128) { |
| 4705 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4706 | return ABIArgInfo::getDirect(CoerceTy); |
| 4707 | } |
| 4708 | } |
| 4709 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4710 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4711 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4712 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4713 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4714 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4715 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4716 | |
| 4717 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4718 | const Type *Base = nullptr; |
| 4719 | uint64_t Members = 0; |
| 4720 | if (Kind == ELFv2 && |
| 4721 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4722 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4723 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4724 | return ABIArgInfo::getDirect(CoerceTy); |
| 4725 | } |
| 4726 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4727 | // If an aggregate may end up fully in registers, we do not |
| 4728 | // use the ByVal method, but pass the aggregate as array. |
| 4729 | // This is usually beneficial since we avoid forcing the |
| 4730 | // back-end to store the argument to memory. |
| 4731 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4732 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4733 | llvm::Type *CoerceTy; |
| 4734 | |
| 4735 | // Types up to 8 bytes are passed as integer type (which will be |
| 4736 | // properly aligned in the argument save area doubleword). |
| 4737 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4738 | CoerceTy = |
| 4739 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4740 | // Larger types are passed as arrays, with the base type selected |
| 4741 | // according to the required alignment in the save area. |
| 4742 | else { |
| 4743 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4744 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4745 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4746 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4747 | } |
| 4748 | |
| 4749 | return ABIArgInfo::getDirect(CoerceTy); |
| 4750 | } |
| 4751 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4752 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4753 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4754 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4755 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4758 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4759 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | ABIArgInfo |
| 4763 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4764 | if (RetTy->isVoidType()) |
| 4765 | return ABIArgInfo::getIgnore(); |
| 4766 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4767 | if (RetTy->isAnyComplexType()) |
| 4768 | return ABIArgInfo::getDirect(); |
| 4769 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4770 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4771 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4772 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4773 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4774 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4775 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4776 | else if (Size < 128) { |
| 4777 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4778 | return ABIArgInfo::getDirect(CoerceTy); |
| 4779 | } |
| 4780 | } |
| 4781 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4782 | if (isAggregateTypeForABI(RetTy)) { |
| 4783 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4784 | const Type *Base = nullptr; |
| 4785 | uint64_t Members = 0; |
| 4786 | if (Kind == ELFv2 && |
| 4787 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4788 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4789 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4790 | return ABIArgInfo::getDirect(CoerceTy); |
| 4791 | } |
| 4792 | |
| 4793 | // ELFv2 small aggregates are returned in up to two registers. |
| 4794 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4795 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4796 | if (Bits == 0) |
| 4797 | return ABIArgInfo::getIgnore(); |
| 4798 | |
| 4799 | llvm::Type *CoerceTy; |
| 4800 | if (Bits > GPRBits) { |
| 4801 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4802 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4803 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4804 | CoerceTy = |
| 4805 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4806 | return ABIArgInfo::getDirect(CoerceTy); |
| 4807 | } |
| 4808 | |
| 4809 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4810 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4811 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4812 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4813 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4814 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4817 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4818 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4819 | QualType Ty) const { |
| 4820 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4821 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4822 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4823 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4824 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4825 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4826 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4827 | // in separate doublewords. However, Clang expects us to produce a |
| 4828 | // pointer to a structure with the two parts packed tightly. So generate |
| 4829 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4830 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4831 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4832 | CharUnits EltSize = TypeInfo.first / 2; |
| 4833 | if (EltSize < SlotSize) { |
| 4834 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4835 | SlotSize * 2, SlotSize, |
| 4836 | SlotSize, /*AllowHigher*/ true); |
| 4837 | |
| 4838 | Address RealAddr = Addr; |
| 4839 | Address ImagAddr = RealAddr; |
| 4840 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4841 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4842 | SlotSize - EltSize); |
| 4843 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4844 | 2 * SlotSize - EltSize); |
| 4845 | } else { |
| 4846 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4847 | } |
| 4848 | |
| 4849 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4850 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4851 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4852 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4853 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4854 | |
| 4855 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4856 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4857 | /*init*/ true); |
| 4858 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4859 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4862 | // Otherwise, just use the general rule. |
| 4863 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4864 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | static bool |
| 4868 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4869 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4870 | // This is calculated from the LLVM and GCC tables and verified |
| 4871 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4872 | |
| 4873 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4874 | |
| 4875 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4876 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4877 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4878 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4879 | |
| 4880 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4881 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4882 | |
| 4883 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4884 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4885 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4886 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4887 | // 64: mq |
| 4888 | // 65: lr |
| 4889 | // 66: ctr |
| 4890 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4891 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4892 | |
| 4893 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4894 | // 68-75 cr0-7 |
| 4895 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4896 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4897 | |
| 4898 | // 77-108: v0-31, the 16-byte vector registers |
| 4899 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4900 | |
| 4901 | // 109: vrsave |
| 4902 | // 110: vscr |
| 4903 | // 111: spe_acc |
| 4904 | // 112: spefscr |
| 4905 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4906 | // 114: tfhar |
| 4907 | // 115: tfiar |
| 4908 | // 116: texasr |
| 4909 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4910 | |
| 4911 | return false; |
| 4912 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4913 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4914 | bool |
| 4915 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4916 | CodeGen::CodeGenFunction &CGF, |
| 4917 | llvm::Value *Address) const { |
| 4918 | |
| 4919 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4920 | } |
| 4921 | |
| 4922 | bool |
| 4923 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4924 | llvm::Value *Address) const { |
| 4925 | |
| 4926 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4927 | } |
| 4928 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4929 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4930 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4931 | //===----------------------------------------------------------------------===// |
| 4932 | |
| 4933 | namespace { |
| 4934 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4935 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4936 | public: |
| 4937 | enum ABIKind { |
| 4938 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4939 | DarwinPCS, |
| 4940 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4941 | }; |
| 4942 | |
| 4943 | private: |
| 4944 | ABIKind Kind; |
| 4945 | |
| 4946 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4947 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4948 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4949 | |
| 4950 | private: |
| 4951 | ABIKind getABIKind() const { return Kind; } |
| 4952 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4953 | |
| 4954 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4955 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4956 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4957 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4958 | uint64_t Members) const override; |
| 4959 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4960 | bool isIllegalVectorType(QualType Ty) const; |
| 4961 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4962 | void computeInfo(CGFunctionInfo &FI) const override { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4963 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4964 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4965 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4966 | for (auto &it : FI.arguments()) |
| 4967 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4970 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4971 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4972 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4973 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4974 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4975 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4976 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4977 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4978 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4979 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4980 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4981 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4982 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4983 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4984 | QualType Ty) const override; |
| 4985 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 4986 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4987 | bool asReturnValue) const override { |
| 4988 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4989 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4990 | bool isSwiftErrorInRegister() const override { |
| 4991 | return true; |
| 4992 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4993 | |
| 4994 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4995 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4996 | }; |
| 4997 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4998 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4999 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5000 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 5001 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5002 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5003 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5004 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5007 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 5008 | return 31; |
| 5009 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5010 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5011 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5012 | |
| 5013 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5014 | CodeGen::CodeGenModule &CGM) const override { |
| 5015 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 5016 | if (!FD) |
| 5017 | return; |
| 5018 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5019 | |
| 5020 | auto Kind = CGM.getCodeGenOpts().getSignReturnAddress(); |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5021 | if (Kind != CodeGenOptions::SignReturnAddressScope::None) { |
| 5022 | Fn->addFnAttr("sign-return-address", |
| 5023 | Kind == CodeGenOptions::SignReturnAddressScope::All |
| 5024 | ? "all" |
| 5025 | : "non-leaf"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5026 | |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5027 | auto Key = CGM.getCodeGenOpts().getSignReturnAddressKey(); |
| 5028 | Fn->addFnAttr("sign-return-address-key", |
| 5029 | Key == CodeGenOptions::SignReturnAddressKeyValue::AKey |
| 5030 | ? "a_key" |
| 5031 | : "b_key"); |
| 5032 | } |
| 5033 | |
| 5034 | if (CGM.getCodeGenOpts().BranchTargetEnforcement) |
| 5035 | Fn->addFnAttr("branch-target-enforcement"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5036 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5037 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5038 | |
| 5039 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 5040 | public: |
| 5041 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 5042 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 5043 | |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5044 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5045 | CodeGen::CodeGenModule &CGM) const override; |
| 5046 | |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5047 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5048 | llvm::SmallString<24> &Opt) const override { |
| 5049 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5050 | } |
| 5051 | |
| 5052 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5053 | llvm::SmallString<32> &Opt) const override { |
| 5054 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5055 | } |
| 5056 | }; |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5057 | |
| 5058 | void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( |
| 5059 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5060 | AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5061 | if (GV->isDeclaration()) |
| 5062 | return; |
| 5063 | addStackProbeTargetAttributes(D, GV, CGM); |
| 5064 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5065 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5066 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5067 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5068 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5069 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5070 | // Handle illegal vector types here. |
| 5071 | if (isIllegalVectorType(Ty)) { |
| 5072 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5073 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 5074 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5075 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 5076 | return ABIArgInfo::getDirect(ResType); |
| 5077 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5078 | if (Size <= 32) { |
| 5079 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5080 | return ABIArgInfo::getDirect(ResType); |
| 5081 | } |
| 5082 | if (Size == 64) { |
| 5083 | llvm::Type *ResType = |
| 5084 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5085 | return ABIArgInfo::getDirect(ResType); |
| 5086 | } |
| 5087 | if (Size == 128) { |
| 5088 | llvm::Type *ResType = |
| 5089 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5090 | return ABIArgInfo::getDirect(ResType); |
| 5091 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5092 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5093 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5094 | |
| 5095 | if (!isAggregateTypeForABI(Ty)) { |
| 5096 | // Treat an enum type as its underlying type. |
| 5097 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5098 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5099 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5100 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5101 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5102 | : ABIArgInfo::getDirect()); |
| 5103 | } |
| 5104 | |
| 5105 | // Structures with either a non-trivial destructor or a non-trivial |
| 5106 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5107 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5108 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5109 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5113 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5114 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5115 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5116 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5117 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5118 | return ABIArgInfo::getIgnore(); |
| 5119 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5120 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5121 | // 0. |
| 5122 | if (IsEmpty && Size == 0) |
| 5123 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5124 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5125 | } |
| 5126 | |
| 5127 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5128 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5129 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5130 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5131 | return ABIArgInfo::getDirect( |
| 5132 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5136 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5137 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5138 | // same size and alignment. |
| 5139 | if (getTarget().isRenderScriptTarget()) { |
| 5140 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5141 | } |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5142 | unsigned Alignment; |
| 5143 | if (Kind == AArch64ABIInfo::AAPCS) { |
| 5144 | Alignment = getContext().getTypeUnadjustedAlign(Ty); |
| 5145 | Alignment = Alignment < 128 ? 64 : 128; |
| 5146 | } else { |
| 5147 | Alignment = getContext().getTypeAlign(Ty); |
| 5148 | } |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5149 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5150 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5151 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5152 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5153 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5154 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5155 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5156 | } |
| 5157 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5158 | } |
| 5159 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5160 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5161 | } |
| 5162 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5163 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5164 | if (RetTy->isVoidType()) |
| 5165 | return ABIArgInfo::getIgnore(); |
| 5166 | |
| 5167 | // Large vector types should be returned via memory. |
| 5168 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5169 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5170 | |
| 5171 | if (!isAggregateTypeForABI(RetTy)) { |
| 5172 | // Treat an enum type as its underlying type. |
| 5173 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5174 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5175 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5176 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5177 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5178 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5181 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5182 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5183 | return ABIArgInfo::getIgnore(); |
| 5184 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5185 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5186 | uint64_t Members = 0; |
| 5187 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5188 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5189 | return ABIArgInfo::getDirect(); |
| 5190 | |
| 5191 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5192 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5193 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5194 | // same size and alignment. |
| 5195 | if (getTarget().isRenderScriptTarget()) { |
| 5196 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5197 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5198 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5199 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5200 | |
| 5201 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5202 | // For aggregates with 16-byte alignment, we use i128. |
| 5203 | if (Alignment < 128 && Size == 128) { |
| 5204 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5205 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5206 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5207 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5208 | } |
| 5209 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5210 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5213 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5214 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5215 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5216 | // Check whether VT is legal. |
| 5217 | unsigned NumElements = VT->getNumElements(); |
| 5218 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5219 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5220 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5221 | return true; |
| 5222 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5223 | } |
| 5224 | return false; |
| 5225 | } |
| 5226 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5227 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5228 | llvm::Type *eltTy, |
| 5229 | unsigned elts) const { |
| 5230 | if (!llvm::isPowerOf2_32(elts)) |
| 5231 | return false; |
| 5232 | if (totalSize.getQuantity() != 8 && |
| 5233 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5234 | return false; |
| 5235 | return true; |
| 5236 | } |
| 5237 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5238 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5239 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5240 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5241 | // but with the difference that any floating-point type is allowed, |
| 5242 | // including __fp16. |
| 5243 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5244 | if (BT->isFloatingPoint()) |
| 5245 | return true; |
| 5246 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5247 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5248 | if (VecSize == 64 || VecSize == 128) |
| 5249 | return true; |
| 5250 | } |
| 5251 | return false; |
| 5252 | } |
| 5253 | |
| 5254 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5255 | uint64_t Members) const { |
| 5256 | return Members <= 4; |
| 5257 | } |
| 5258 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5259 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5260 | QualType Ty, |
| 5261 | CodeGenFunction &CGF) const { |
| 5262 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5263 | bool IsIndirect = AI.isIndirect(); |
| 5264 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5265 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5266 | if (IsIndirect) |
| 5267 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5268 | else if (AI.getCoerceToType()) |
| 5269 | BaseTy = AI.getCoerceToType(); |
| 5270 | |
| 5271 | unsigned NumRegs = 1; |
| 5272 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5273 | BaseTy = ArrTy->getElementType(); |
| 5274 | NumRegs = ArrTy->getNumElements(); |
| 5275 | } |
| 5276 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5277 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5278 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5279 | // Standard, section B.4: |
| 5280 | // |
| 5281 | // struct { |
| 5282 | // void *__stack; |
| 5283 | // void *__gr_top; |
| 5284 | // void *__vr_top; |
| 5285 | // int __gr_offs; |
| 5286 | // int __vr_offs; |
| 5287 | // }; |
| 5288 | |
| 5289 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5290 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5291 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5292 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5293 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5294 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5295 | CharUnits TyAlign = TyInfo.second; |
| 5296 | |
| 5297 | Address reg_offs_p = Address::invalid(); |
| 5298 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5299 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5300 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5301 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5302 | // 3 is the field number of __gr_offs |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5303 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5304 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5305 | reg_top_index = 1; // field number for __gr_top |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5306 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5307 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5308 | // 4 is the field number of __vr_offs. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5309 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5310 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5311 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5312 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | //======================================= |
| 5316 | // Find out where argument was passed |
| 5317 | //======================================= |
| 5318 | |
| 5319 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5320 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5321 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5322 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5323 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5324 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5325 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5326 | |
| 5327 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5328 | |
| 5329 | // 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] | 5330 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5331 | CGF.EmitBlock(MaybeRegBlock); |
| 5332 | |
| 5333 | // Integer arguments may need to correct register alignment (for example a |
| 5334 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5335 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5336 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5337 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5338 | |
| 5339 | reg_offs = CGF.Builder.CreateAdd( |
| 5340 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5341 | "align_regoffs"); |
| 5342 | reg_offs = CGF.Builder.CreateAnd( |
| 5343 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5344 | "aligned_regoffs"); |
| 5345 | } |
| 5346 | |
| 5347 | // 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] | 5348 | // The fact that this is done unconditionally reflects the fact that |
| 5349 | // allocating an argument to the stack also uses up all the remaining |
| 5350 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5351 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5352 | NewOffset = CGF.Builder.CreateAdd( |
| 5353 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5354 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5355 | |
| 5356 | // Now we're in a position to decide whether this argument really was in |
| 5357 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5358 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5359 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5360 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5361 | |
| 5362 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5363 | |
| 5364 | //======================================= |
| 5365 | // Argument was in registers |
| 5366 | //======================================= |
| 5367 | |
| 5368 | // Now we emit the code for if the argument was originally passed in |
| 5369 | // registers. First start the appropriate block: |
| 5370 | CGF.EmitBlock(InRegBlock); |
| 5371 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5372 | llvm::Value *reg_top = nullptr; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5373 | Address reg_top_p = |
| 5374 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5375 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5376 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5377 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5378 | Address RegAddr = Address::invalid(); |
| 5379 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5380 | |
| 5381 | if (IsIndirect) { |
| 5382 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5383 | // stored registers or on the stack will actually be a struct **. |
| 5384 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5385 | } |
| 5386 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5387 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5388 | uint64_t NumMembers = 0; |
| 5389 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5390 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5391 | // Homogeneous aggregates passed in registers will have their elements split |
| 5392 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5393 | // qN+1, ...). We reload and store into a temporary local variable |
| 5394 | // contiguously. |
| 5395 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5396 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5397 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5398 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5399 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5400 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5401 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5402 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5403 | int Offset = 0; |
| 5404 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5405 | BaseTyInfo.first.getQuantity() < 16) |
| 5406 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5407 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5408 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5409 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5410 | Address LoadAddr = |
| 5411 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5412 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5413 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5414 | Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5415 | |
| 5416 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5417 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5418 | } |
| 5419 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5420 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5421 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5422 | // Otherwise the object is contiguous in memory. |
| 5423 | |
| 5424 | // It might be right-aligned in its slot. |
| 5425 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5426 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5427 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5428 | TyInfo.first < SlotSize) { |
| 5429 | CharUnits Offset = SlotSize - TyInfo.first; |
| 5430 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5433 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | CGF.EmitBranch(ContBlock); |
| 5437 | |
| 5438 | //======================================= |
| 5439 | // Argument was on the stack |
| 5440 | //======================================= |
| 5441 | CGF.EmitBlock(OnStackBlock); |
| 5442 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5443 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5444 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5445 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5446 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5447 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5448 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5449 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5450 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5451 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5452 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5453 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5454 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5455 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5456 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5457 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5458 | "align_stack"); |
| 5459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5460 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5461 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5462 | Address OnStackAddr(OnStackPtr, |
| 5463 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5464 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5465 | // All stack slots are multiples of 8 bytes. |
| 5466 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5467 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5468 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5469 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5470 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5471 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5472 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5473 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5474 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5475 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5476 | |
| 5477 | // Write the new value of __stack for the next call to va_arg |
| 5478 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5479 | |
| 5480 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5481 | TyInfo.first < StackSlotSize) { |
| 5482 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5483 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5486 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5487 | |
| 5488 | CGF.EmitBranch(ContBlock); |
| 5489 | |
| 5490 | //======================================= |
| 5491 | // Tidy up |
| 5492 | //======================================= |
| 5493 | CGF.EmitBlock(ContBlock); |
| 5494 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5495 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5496 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5497 | |
| 5498 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5499 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5500 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5501 | |
| 5502 | return ResAddr; |
| 5503 | } |
| 5504 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5505 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5506 | CodeGenFunction &CGF) const { |
| 5507 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5508 | // illegal vector types. Lower VAArg here for these cases and use |
| 5509 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5510 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5511 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5512 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5513 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5514 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5515 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5516 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5517 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5518 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5519 | return Addr; |
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 | // The size of the actual thing passed, which might end up just |
| 5523 | // being a pointer for indirect types. |
| 5524 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5525 | |
| 5526 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5527 | // aggregates should be passed indirectly. |
| 5528 | bool IsIndirect = false; |
| 5529 | if (TyInfo.first.getQuantity() > 16) { |
| 5530 | const Type *Base = nullptr; |
| 5531 | uint64_t Members = 0; |
| 5532 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5535 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5536 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5539 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5540 | QualType Ty) const { |
| 5541 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5542 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5543 | CharUnits::fromQuantity(8), |
| 5544 | /*allowHigherAlign*/ false); |
| 5545 | } |
| 5546 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5547 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5548 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5549 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5550 | |
| 5551 | namespace { |
| 5552 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5553 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5554 | public: |
| 5555 | enum ABIKind { |
| 5556 | APCS = 0, |
| 5557 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5558 | AAPCS_VFP = 2, |
| 5559 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5560 | }; |
| 5561 | |
| 5562 | private: |
| 5563 | ABIKind Kind; |
| 5564 | |
| 5565 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5566 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5567 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5568 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5569 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5570 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5571 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5572 | switch (getTarget().getTriple().getEnvironment()) { |
| 5573 | case llvm::Triple::Android: |
| 5574 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5575 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5576 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5577 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5578 | case llvm::Triple::MuslEABI: |
| 5579 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5580 | return true; |
| 5581 | default: |
| 5582 | return false; |
| 5583 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5586 | bool isEABIHF() const { |
| 5587 | switch (getTarget().getTriple().getEnvironment()) { |
| 5588 | case llvm::Triple::EABIHF: |
| 5589 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5590 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5591 | return true; |
| 5592 | default: |
| 5593 | return false; |
| 5594 | } |
| 5595 | } |
| 5596 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5597 | ABIKind getABIKind() const { return Kind; } |
| 5598 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5599 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5600 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5601 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5602 | ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, |
| 5603 | uint64_t Members) const; |
| 5604 | ABIArgInfo coerceIllegalVector(QualType Ty) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5605 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5606 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5607 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5608 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5609 | uint64_t Members) const override; |
| 5610 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5611 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5612 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5613 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5614 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5615 | |
| 5616 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5617 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5618 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5619 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5620 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5621 | bool asReturnValue) const override { |
| 5622 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5623 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5624 | bool isSwiftErrorInRegister() const override { |
| 5625 | return true; |
| 5626 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5627 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5628 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5629 | }; |
| 5630 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5631 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5632 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5633 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5634 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5635 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5636 | const ARMABIInfo &getABIInfo() const { |
| 5637 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5638 | } |
| 5639 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5640 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5641 | return 13; |
| 5642 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5643 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5644 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5645 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5648 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5649 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5650 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5651 | |
| 5652 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5653 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5654 | return false; |
| 5655 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5656 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5657 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5658 | if (getABIInfo().isEABI()) return 88; |
| 5659 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5660 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5661 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5662 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5663 | CodeGen::CodeGenModule &CGM) const override { |
| 5664 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5665 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5666 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5667 | if (!FD) |
| 5668 | return; |
| 5669 | |
| 5670 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5671 | if (!Attr) |
| 5672 | return; |
| 5673 | |
| 5674 | const char *Kind; |
| 5675 | switch (Attr->getInterrupt()) { |
| 5676 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5677 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5678 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5679 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5680 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5681 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5682 | } |
| 5683 | |
| 5684 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5685 | |
| 5686 | Fn->addFnAttr("interrupt", Kind); |
| 5687 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5688 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5689 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5690 | return; |
| 5691 | |
| 5692 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5693 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5694 | // the backend to perform a realignment as part of the function prologue. |
| 5695 | llvm::AttrBuilder B; |
| 5696 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5697 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5698 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5699 | }; |
| 5700 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5701 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5702 | public: |
| 5703 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5704 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5705 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5706 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5707 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5708 | |
| 5709 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5710 | llvm::SmallString<24> &Opt) const override { |
| 5711 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5712 | } |
| 5713 | |
| 5714 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5715 | llvm::SmallString<32> &Opt) const override { |
| 5716 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5717 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5718 | }; |
| 5719 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5720 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5721 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5722 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5723 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5724 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 5725 | addStackProbeTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5726 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5727 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5728 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5729 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5730 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5731 | FI.getReturnInfo() = |
| 5732 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5733 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5734 | for (auto &I : FI.arguments()) |
| 5735 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5736 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5737 | // Always honor user-specified calling convention. |
| 5738 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5739 | return; |
| 5740 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5741 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5742 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5743 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5744 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5745 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5746 | /// Return the default calling convention that LLVM will use. |
| 5747 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5748 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5749 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5750 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5751 | else if (isEABI()) |
| 5752 | return llvm::CallingConv::ARM_AAPCS; |
| 5753 | else |
| 5754 | return llvm::CallingConv::ARM_APCS; |
| 5755 | } |
| 5756 | |
| 5757 | /// Return the calling convention that our ABI would like us to use |
| 5758 | /// as the C calling convention. |
| 5759 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5760 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5761 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5762 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5763 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5764 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5765 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5766 | llvm_unreachable("bad ABI kind"); |
| 5767 | } |
| 5768 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5769 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5770 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5771 | |
| 5772 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5773 | // they'd just match what LLVM will infer from the triple. |
| 5774 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5775 | if (abiCC != getLLVMDefaultCC()) |
| 5776 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5779 | ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { |
| 5780 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5781 | if (Size <= 32) { |
| 5782 | llvm::Type *ResType = |
| 5783 | llvm::Type::getInt32Ty(getVMContext()); |
| 5784 | return ABIArgInfo::getDirect(ResType); |
| 5785 | } |
| 5786 | if (Size == 64 || Size == 128) { |
| 5787 | llvm::Type *ResType = llvm::VectorType::get( |
| 5788 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5789 | return ABIArgInfo::getDirect(ResType); |
| 5790 | } |
| 5791 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5792 | } |
| 5793 | |
| 5794 | ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, |
| 5795 | const Type *Base, |
| 5796 | uint64_t Members) const { |
| 5797 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 5798 | // Base can be a floating-point or a vector. |
| 5799 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 5800 | // FP16 vectors should be converted to integer vectors |
| 5801 | if (!getTarget().hasLegalHalfType() && |
| 5802 | (VT->getElementType()->isFloat16Type() || |
| 5803 | VT->getElementType()->isHalfType())) { |
| 5804 | uint64_t Size = getContext().getTypeSize(VT); |
| 5805 | llvm::Type *NewVecTy = llvm::VectorType::get( |
| 5806 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5807 | llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); |
| 5808 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5809 | } |
| 5810 | } |
| 5811 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 5812 | } |
| 5813 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5814 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5815 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5816 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5817 | // A single-precision floating-point type (including promoted |
| 5818 | // half-precision types); A double-precision floating-point type; |
| 5819 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5820 | // with a Base Type of a single- or double-precision floating-point type, |
| 5821 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5822 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5823 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5824 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5825 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5826 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5827 | // Handle illegal vector types here. |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5828 | if (isIllegalVectorType(Ty)) |
| 5829 | return coerceIllegalVector(Ty); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5830 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5831 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5832 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5833 | // half type natively, and does not need to interwork with AAPCS code. |
| 5834 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5835 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5836 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5837 | llvm::Type::getFloatTy(getVMContext()) : |
| 5838 | llvm::Type::getInt32Ty(getVMContext()); |
| 5839 | return ABIArgInfo::getDirect(ResType); |
| 5840 | } |
| 5841 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5842 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5843 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5844 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5845 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5846 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5847 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5848 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5849 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5850 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5851 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5852 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5853 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5854 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5855 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5856 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5857 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5858 | return ABIArgInfo::getIgnore(); |
| 5859 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5860 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5861 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5862 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5863 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5864 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5865 | if (isHomogeneousAggregate(Ty, Base, Members)) |
| 5866 | return classifyHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5867 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5868 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5869 | // this convention even for a variadic function: the backend will use GPRs |
| 5870 | // if needed. |
| 5871 | const Type *Base = nullptr; |
| 5872 | uint64_t Members = 0; |
| 5873 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5874 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5875 | llvm::Type *Ty = |
| 5876 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5877 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5882 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5883 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5884 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5885 | // and a pointer is passed. |
| 5886 | return ABIArgInfo::getIndirect( |
| 5887 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5890 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5891 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5892 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5893 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5894 | uint64_t ABIAlign = 4; |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5895 | uint64_t TyAlign; |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5896 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5897 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5898 | TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5899 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5900 | } else { |
| 5901 | TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 5902 | } |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5903 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5904 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5905 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5906 | /*ByVal=*/true, |
| 5907 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5908 | } |
| 5909 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5910 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5911 | // same size and alignment. |
| 5912 | if (getTarget().isRenderScriptTarget()) { |
| 5913 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5914 | } |
| 5915 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5916 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5917 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5918 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5919 | // FIXME: Try to match the types of the arguments more accurately where |
| 5920 | // we can. |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5921 | if (TyAlign <= 4) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5922 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5923 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5924 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5925 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5926 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5927 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5928 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5929 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5930 | } |
| 5931 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5932 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5933 | llvm::LLVMContext &VMContext) { |
| 5934 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5935 | // is called integer-like if its size is less than or equal to one word, and |
| 5936 | // the offset of each of its addressable sub-fields is zero. |
| 5937 | |
| 5938 | uint64_t Size = Context.getTypeSize(Ty); |
| 5939 | |
| 5940 | // Check that the type fits in a word. |
| 5941 | if (Size > 32) |
| 5942 | return false; |
| 5943 | |
| 5944 | // FIXME: Handle vector types! |
| 5945 | if (Ty->isVectorType()) |
| 5946 | return false; |
| 5947 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5948 | // Float types are never treated as "integer like". |
| 5949 | if (Ty->isRealFloatingType()) |
| 5950 | return false; |
| 5951 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5952 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5953 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5954 | return true; |
| 5955 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5956 | // Small complex integer types are "integer like". |
| 5957 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5958 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5959 | |
| 5960 | // Single element and zero sized arrays should be allowed, by the definition |
| 5961 | // above, but they are not. |
| 5962 | |
| 5963 | // Otherwise, it must be a record type. |
| 5964 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5965 | if (!RT) return false; |
| 5966 | |
| 5967 | // Ignore records with flexible arrays. |
| 5968 | const RecordDecl *RD = RT->getDecl(); |
| 5969 | if (RD->hasFlexibleArrayMember()) |
| 5970 | return false; |
| 5971 | |
| 5972 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5973 | // like". |
| 5974 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5975 | |
| 5976 | bool HadField = false; |
| 5977 | unsigned idx = 0; |
| 5978 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5979 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5980 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5981 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5982 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5983 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5984 | // struct { int : 0; int x } |
| 5985 | // is non-integer like according to gcc. |
| 5986 | if (FD->isBitField()) { |
| 5987 | if (!RD->isUnion()) |
| 5988 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5989 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5990 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5991 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5992 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5993 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5996 | // Check if this field is at offset 0. |
| 5997 | if (Layout.getFieldOffset(idx) != 0) |
| 5998 | return false; |
| 5999 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6000 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6001 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 6002 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6003 | // Only allow at most one field in a structure. This doesn't match the |
| 6004 | // wording above, but follows gcc in situations with a field following an |
| 6005 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6006 | if (!RD->isUnion()) { |
| 6007 | if (HadField) |
| 6008 | return false; |
| 6009 | |
| 6010 | HadField = true; |
| 6011 | } |
| 6012 | } |
| 6013 | |
| 6014 | return true; |
| 6015 | } |
| 6016 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6017 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 6018 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6019 | bool IsEffectivelyAAPCS_VFP = |
| 6020 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6021 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6022 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6023 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6024 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6025 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| 6026 | // Large vector types should be returned via memory. |
| 6027 | if (getContext().getTypeSize(RetTy) > 128) |
| 6028 | return getNaturalAlignIndirect(RetTy); |
| 6029 | // FP16 vectors should be converted to integer vectors |
| 6030 | if (!getTarget().hasLegalHalfType() && |
| 6031 | (VT->getElementType()->isFloat16Type() || |
| 6032 | VT->getElementType()->isHalfType())) |
| 6033 | return coerceIllegalVector(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6034 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 6035 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 6036 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 6037 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 6038 | // half type natively, and does not need to interwork with AAPCS code. |
| 6039 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 6040 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 6041 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 6042 | llvm::Type::getFloatTy(getVMContext()) : |
| 6043 | llvm::Type::getInt32Ty(getVMContext()); |
| 6044 | return ABIArgInfo::getDirect(ResType); |
| 6045 | } |
| 6046 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 6047 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6048 | // Treat an enum type as its underlying type. |
| 6049 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6050 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6051 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6052 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6053 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6054 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6055 | |
| 6056 | // Are we following APCS? |
| 6057 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6058 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6059 | return ABIArgInfo::getIgnore(); |
| 6060 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6061 | // Complex types are all returned as packed integers. |
| 6062 | // |
| 6063 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 6064 | // correctly. |
| 6065 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6066 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 6067 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6068 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6069 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6070 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6071 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6072 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6073 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6074 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6075 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6076 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6077 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6081 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6082 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6083 | |
| 6084 | // Otherwise this is an AAPCS variant. |
| 6085 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6086 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6087 | return ABIArgInfo::getIgnore(); |
| 6088 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6089 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6090 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6091 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6092 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6093 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
| 6094 | return classifyHomogeneousAggregate(RetTy, Base, Members); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6097 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 6098 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6099 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6100 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 6101 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 6102 | // same size and alignment. |
| 6103 | if (getTarget().isRenderScriptTarget()) { |
| 6104 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 6105 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6106 | if (getDataLayout().isBigEndian()) |
| 6107 | // 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] | 6108 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6109 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6110 | // Return in the smallest viable integer type. |
| 6111 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6112 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6113 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6114 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6115 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6116 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6117 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6118 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6119 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6120 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6123 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6126 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6127 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6128 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6129 | // On targets that don't support FP16, FP16 is expanded into float, and we |
| 6130 | // don't want the ABI to depend on whether or not FP16 is supported in |
| 6131 | // hardware. Thus return false to coerce FP16 vectors into integer vectors. |
| 6132 | if (!getTarget().hasLegalHalfType() && |
| 6133 | (VT->getElementType()->isFloat16Type() || |
| 6134 | VT->getElementType()->isHalfType())) |
| 6135 | return true; |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6136 | if (isAndroid()) { |
| 6137 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6138 | // vector ABI. The primary differences were that 3-element vector types |
| 6139 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6140 | // accepts that legacy behavior for Android only. |
| 6141 | // Check whether VT is legal. |
| 6142 | unsigned NumElements = VT->getNumElements(); |
| 6143 | // NumElements should be power of 2 or equal to 3. |
| 6144 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6145 | return true; |
| 6146 | } else { |
| 6147 | // Check whether VT is legal. |
| 6148 | unsigned NumElements = VT->getNumElements(); |
| 6149 | uint64_t Size = getContext().getTypeSize(VT); |
| 6150 | // NumElements should be power of 2. |
| 6151 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6152 | return true; |
| 6153 | // Size should be greater than 32 bits. |
| 6154 | return Size <= 32; |
| 6155 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6156 | } |
| 6157 | return false; |
| 6158 | } |
| 6159 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6160 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6161 | llvm::Type *eltTy, |
| 6162 | unsigned numElts) const { |
| 6163 | if (!llvm::isPowerOf2_32(numElts)) |
| 6164 | return false; |
| 6165 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6166 | if (size > 64) |
| 6167 | return false; |
| 6168 | if (vectorSize.getQuantity() != 8 && |
| 6169 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6170 | return false; |
| 6171 | return true; |
| 6172 | } |
| 6173 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6174 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6175 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6176 | // double, or 64-bit or 128-bit vectors. |
| 6177 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6178 | if (BT->getKind() == BuiltinType::Float || |
| 6179 | BT->getKind() == BuiltinType::Double || |
| 6180 | BT->getKind() == BuiltinType::LongDouble) |
| 6181 | return true; |
| 6182 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6183 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6184 | if (VecSize == 64 || VecSize == 128) |
| 6185 | return true; |
| 6186 | } |
| 6187 | return false; |
| 6188 | } |
| 6189 | |
| 6190 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6191 | uint64_t Members) const { |
| 6192 | return Members <= 4; |
| 6193 | } |
| 6194 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6195 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6196 | QualType Ty) const { |
| 6197 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6198 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6199 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6200 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6201 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6202 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6203 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6206 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6207 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6208 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6209 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6210 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6211 | const Type *Base = nullptr; |
| 6212 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6213 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6214 | IsIndirect = true; |
| 6215 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6216 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6217 | // allocated by the caller. |
| 6218 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6219 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6220 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6221 | IsIndirect = true; |
| 6222 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6223 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6224 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6225 | // 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] | 6226 | // Our callers should be prepared to handle an under-aligned address. |
| 6227 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6228 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6229 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6230 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6231 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6232 | // ARMv7k allows type alignment up to 16 bytes. |
| 6233 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6234 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6235 | } else { |
| 6236 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6237 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6238 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6239 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6240 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6241 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6242 | } |
| 6243 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6244 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6245 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6246 | //===----------------------------------------------------------------------===// |
| 6247 | |
| 6248 | namespace { |
| 6249 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6250 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6251 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6252 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6253 | |
| 6254 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6255 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6256 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6257 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6258 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6259 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6260 | }; |
| 6261 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6262 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6263 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6264 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6265 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6266 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6267 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6268 | CodeGen::CodeGenModule &M) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6269 | bool shouldEmitStaticExternCAliases() const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6270 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6271 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6272 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6273 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6274 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6275 | }; |
| 6276 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6277 | /// Checks if the type is unsupported directly by the current target. |
| 6278 | static bool isUnsupportedType(ASTContext &Context, QualType T) { |
| 6279 | if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type()) |
| 6280 | return true; |
| 6281 | if (!Context.getTargetInfo().hasFloat128Type() && T->isFloat128Type()) |
| 6282 | return true; |
| 6283 | if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() && |
| 6284 | Context.getTypeSize(T) > 64) |
| 6285 | return true; |
| 6286 | if (const auto *AT = T->getAsArrayTypeUnsafe()) |
| 6287 | return isUnsupportedType(Context, AT->getElementType()); |
| 6288 | const auto *RT = T->getAs<RecordType>(); |
| 6289 | if (!RT) |
| 6290 | return false; |
| 6291 | const RecordDecl *RD = RT->getDecl(); |
| 6292 | |
| 6293 | // If this is a C++ record, check the bases first. |
| 6294 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6295 | for (const CXXBaseSpecifier &I : CXXRD->bases()) |
| 6296 | if (isUnsupportedType(Context, I.getType())) |
| 6297 | return true; |
| 6298 | |
| 6299 | for (const FieldDecl *I : RD->fields()) |
| 6300 | if (isUnsupportedType(Context, I->getType())) |
| 6301 | return true; |
| 6302 | return false; |
| 6303 | } |
| 6304 | |
| 6305 | /// Coerce the given type into an array with maximum allowed size of elements. |
| 6306 | static ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, ASTContext &Context, |
| 6307 | llvm::LLVMContext &LLVMContext, |
| 6308 | unsigned MaxSize) { |
| 6309 | // Alignment and Size are measured in bits. |
| 6310 | const uint64_t Size = Context.getTypeSize(Ty); |
| 6311 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 6312 | const unsigned Div = std::min<unsigned>(MaxSize, Alignment); |
| 6313 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Div); |
| 6314 | const uint64_t NumElements = (Size + Div - 1) / Div; |
| 6315 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 6316 | } |
| 6317 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6318 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6319 | if (RetTy->isVoidType()) |
| 6320 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6321 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6322 | if (getContext().getLangOpts().OpenMP && |
| 6323 | getContext().getLangOpts().OpenMPIsDevice && |
| 6324 | isUnsupportedType(getContext(), RetTy)) |
| 6325 | return coerceToIntArrayWithLimit(RetTy, getContext(), getVMContext(), 64); |
| 6326 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6327 | // note: this is different from default ABI |
| 6328 | if (!RetTy->isScalarType()) |
| 6329 | return ABIArgInfo::getDirect(); |
| 6330 | |
| 6331 | // Treat an enum type as its underlying type. |
| 6332 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6333 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6334 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6335 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6336 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6339 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6340 | // Treat an enum type as its underlying type. |
| 6341 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6342 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6343 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6344 | // Return aggregates type as indirect by value |
| 6345 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6346 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6347 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6348 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6349 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6350 | } |
| 6351 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6352 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6353 | if (!getCXXABI().classifyReturnType(FI)) |
| 6354 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6355 | for (auto &I : FI.arguments()) |
| 6356 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6357 | |
| 6358 | // Always honor user-specified calling convention. |
| 6359 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6360 | return; |
| 6361 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6362 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6363 | } |
| 6364 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6365 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6366 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6367 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6368 | } |
| 6369 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6370 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6371 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6372 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6373 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6374 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6375 | if (!FD) return; |
| 6376 | |
| 6377 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6378 | |
| 6379 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6380 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6381 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6382 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6383 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6384 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6385 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6386 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6387 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6388 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6389 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6390 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6391 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6392 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6393 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6394 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6395 | // __global__ functions cannot be called from the device, we do not |
| 6396 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6397 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6398 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6399 | addNVVMMetadata(F, "kernel", 1); |
| 6400 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6401 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6402 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6403 | llvm::APSInt MaxThreads(32); |
| 6404 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6405 | if (MaxThreads > 0) |
| 6406 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6407 | |
| 6408 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6409 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6410 | // we don't have to add a PTX directive. |
| 6411 | if (Attr->getMinBlocks()) { |
| 6412 | llvm::APSInt MinBlocks(32); |
| 6413 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6414 | if (MinBlocks > 0) |
| 6415 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6416 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6417 | } |
| 6418 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6419 | } |
| 6420 | } |
| 6421 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6422 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6423 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6424 | llvm::Module *M = F->getParent(); |
| 6425 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6426 | |
| 6427 | // Get "nvvm.annotations" metadata node |
| 6428 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6429 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6430 | llvm::Metadata *MDVals[] = { |
| 6431 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6432 | llvm::ConstantAsMetadata::get( |
| 6433 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6434 | // Append metadata to nvvm.annotations |
| 6435 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6436 | } |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6437 | |
| 6438 | bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 6439 | return false; |
| 6440 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6441 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6442 | |
| 6443 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6444 | // SystemZ ABI Implementation |
| 6445 | //===----------------------------------------------------------------------===// |
| 6446 | |
| 6447 | namespace { |
| 6448 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6449 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6450 | bool HasVector; |
| 6451 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6452 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6453 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6454 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6455 | |
| 6456 | bool isPromotableIntegerType(QualType Ty) const; |
| 6457 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6458 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6459 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6460 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6461 | |
| 6462 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6463 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6464 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6465 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6466 | if (!getCXXABI().classifyReturnType(FI)) |
| 6467 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6468 | for (auto &I : FI.arguments()) |
| 6469 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6472 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6473 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6474 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6475 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6476 | bool asReturnValue) const override { |
| 6477 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6478 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6479 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6480 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6481 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6482 | }; |
| 6483 | |
| 6484 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6485 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6486 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6487 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6488 | }; |
| 6489 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6490 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6491 | |
| 6492 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6493 | // Treat an enum type as its underlying type. |
| 6494 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6495 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6496 | |
| 6497 | // Promotable integer types are required to be promoted by the ABI. |
| 6498 | if (Ty->isPromotableIntegerType()) |
| 6499 | return true; |
| 6500 | |
| 6501 | // 32-bit values must also be promoted. |
| 6502 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6503 | switch (BT->getKind()) { |
| 6504 | case BuiltinType::Int: |
| 6505 | case BuiltinType::UInt: |
| 6506 | return true; |
| 6507 | default: |
| 6508 | return false; |
| 6509 | } |
| 6510 | return false; |
| 6511 | } |
| 6512 | |
| 6513 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6514 | return (Ty->isAnyComplexType() || |
| 6515 | Ty->isVectorType() || |
| 6516 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6517 | } |
| 6518 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6519 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6520 | return (HasVector && |
| 6521 | Ty->isVectorType() && |
| 6522 | getContext().getTypeSize(Ty) <= 128); |
| 6523 | } |
| 6524 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6525 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6526 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6527 | switch (BT->getKind()) { |
| 6528 | case BuiltinType::Float: |
| 6529 | case BuiltinType::Double: |
| 6530 | return true; |
| 6531 | default: |
| 6532 | return false; |
| 6533 | } |
| 6534 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6535 | return false; |
| 6536 | } |
| 6537 | |
| 6538 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6539 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6540 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6541 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6542 | |
| 6543 | // If this is a C++ record, check the bases first. |
| 6544 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6545 | for (const auto &I : CXXRD->bases()) { |
| 6546 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6547 | |
| 6548 | // Empty bases don't affect things either way. |
| 6549 | if (isEmptyRecord(getContext(), Base, true)) |
| 6550 | continue; |
| 6551 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6552 | if (!Found.isNull()) |
| 6553 | return Ty; |
| 6554 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6558 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6559 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6560 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6561 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6562 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 6563 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6564 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6565 | |
| 6566 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6567 | // Nested structures still do though. |
| 6568 | if (!Found.isNull()) |
| 6569 | return Ty; |
| 6570 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6571 | } |
| 6572 | |
| 6573 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6574 | // 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] | 6575 | if (!Found.isNull()) |
| 6576 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6579 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6582 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6583 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6584 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6585 | // struct { |
| 6586 | // i64 __gpr; |
| 6587 | // i64 __fpr; |
| 6588 | // i8 *__overflow_arg_area; |
| 6589 | // i8 *__reg_save_area; |
| 6590 | // }; |
| 6591 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6592 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6593 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6594 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6595 | Ty = getContext().getCanonicalType(Ty); |
| 6596 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6597 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6598 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6599 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6600 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6601 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6602 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6603 | CharUnits UnpaddedSize; |
| 6604 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6605 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6606 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6607 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6608 | } else { |
| 6609 | if (AI.getCoerceToType()) |
| 6610 | ArgTy = AI.getCoerceToType(); |
| 6611 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6612 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6613 | UnpaddedSize = TyInfo.first; |
| 6614 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6615 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6616 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6617 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6618 | PaddedSize = CharUnits::fromQuantity(16); |
| 6619 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6620 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6621 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6622 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6623 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6624 | llvm::Value *PaddedSizeV = |
| 6625 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6626 | |
| 6627 | if (IsVector) { |
| 6628 | // Work out the address of a vector argument on the stack. |
| 6629 | // Vector arguments are always passed in the high bits of a |
| 6630 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6631 | Address OverflowArgAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6632 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6633 | Address OverflowArgArea = |
| 6634 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6635 | TyInfo.second); |
| 6636 | Address MemAddr = |
| 6637 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6638 | |
| 6639 | // Update overflow_arg_area_ptr pointer |
| 6640 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6641 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6642 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6643 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6644 | |
| 6645 | return MemAddr; |
| 6646 | } |
| 6647 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6648 | assert(PaddedSize.getQuantity() == 8); |
| 6649 | |
| 6650 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6651 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6652 | if (InFPRs) { |
| 6653 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6654 | RegCountField = 1; // __fpr |
| 6655 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6656 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6657 | } else { |
| 6658 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6659 | RegCountField = 0; // __gpr |
| 6660 | RegSaveIndex = 2; // save offset for r2 |
| 6661 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6662 | } |
| 6663 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6664 | Address RegCountPtr = |
| 6665 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6666 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6667 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6668 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6669 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6670 | |
| 6671 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6672 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6673 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6674 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6675 | |
| 6676 | // Emit code to load the value if it was passed in registers. |
| 6677 | CGF.EmitBlock(InRegBlock); |
| 6678 | |
| 6679 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6680 | llvm::Value *ScaledRegCount = |
| 6681 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6682 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6683 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6684 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6685 | llvm::Value *RegOffset = |
| 6686 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6687 | Address RegSaveAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6688 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6689 | llvm::Value *RegSaveArea = |
| 6690 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6691 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6692 | "raw_reg_addr"), |
| 6693 | PaddedSize); |
| 6694 | Address RegAddr = |
| 6695 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6696 | |
| 6697 | // Update the register count |
| 6698 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6699 | llvm::Value *NewRegCount = |
| 6700 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6701 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6702 | CGF.EmitBranch(ContBlock); |
| 6703 | |
| 6704 | // Emit code to load the value if it was passed in memory. |
| 6705 | CGF.EmitBlock(InMemBlock); |
| 6706 | |
| 6707 | // Work out the address of a stack argument. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6708 | Address OverflowArgAreaPtr = |
| 6709 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6710 | Address OverflowArgArea = |
| 6711 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6712 | PaddedSize); |
| 6713 | Address RawMemAddr = |
| 6714 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6715 | Address MemAddr = |
| 6716 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6717 | |
| 6718 | // Update overflow_arg_area_ptr pointer |
| 6719 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6720 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6721 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6722 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6723 | CGF.EmitBranch(ContBlock); |
| 6724 | |
| 6725 | // Return the appropriate result. |
| 6726 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6727 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6728 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6729 | |
| 6730 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6731 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6732 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6733 | |
| 6734 | return ResAddr; |
| 6735 | } |
| 6736 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6737 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6738 | if (RetTy->isVoidType()) |
| 6739 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6740 | if (isVectorArgumentType(RetTy)) |
| 6741 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6742 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6743 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6744 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6745 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
| 6748 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6749 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6750 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6751 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6752 | |
| 6753 | // Integers and enums are extended to full register width. |
| 6754 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6755 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6756 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6757 | // Handle vector types and vector-like structure types. Note that |
| 6758 | // as opposed to float-like structure types, we do not allow any |
| 6759 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6760 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6761 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6762 | if (isVectorArgumentType(SingleElementTy) && |
| 6763 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6764 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6765 | |
| 6766 | // 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] | 6767 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6768 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6769 | |
| 6770 | // Handle small structures. |
| 6771 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6772 | // Structures with flexible arrays have variable length, so really |
| 6773 | // fail the size test above. |
| 6774 | const RecordDecl *RD = RT->getDecl(); |
| 6775 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6776 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6777 | |
| 6778 | // The structure is passed as an unextended integer, a float, or a double. |
| 6779 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6780 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6781 | assert(Size == 32 || Size == 64); |
| 6782 | if (Size == 32) |
| 6783 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6784 | else |
| 6785 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6786 | } else |
| 6787 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6788 | return ABIArgInfo::getDirect(PassTy); |
| 6789 | } |
| 6790 | |
| 6791 | // Non-structure compounds are passed indirectly. |
| 6792 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6793 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6794 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6795 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6796 | } |
| 6797 | |
| 6798 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6799 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6800 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6801 | |
| 6802 | namespace { |
| 6803 | |
| 6804 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6805 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6806 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6807 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6808 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6809 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6810 | }; |
| 6811 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6812 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6813 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6814 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6815 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6816 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6817 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6818 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6819 | const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); |
| 6820 | if (!InterruptAttr) |
| 6821 | return; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6822 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6823 | // Handle 'interrupt' attribute: |
| 6824 | llvm::Function *F = cast<llvm::Function>(GV); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6825 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6826 | // Step 1: Set ISR calling convention. |
| 6827 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6828 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6829 | // Step 2: Add attributes goodness. |
| 6830 | F->addFnAttr(llvm::Attribute::NoInline); |
| 6831 | F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6832 | } |
| 6833 | } |
| 6834 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6835 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6836 | // MIPS ABI Implementation. This works for both little-endian and |
| 6837 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6838 | //===----------------------------------------------------------------------===// |
| 6839 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6840 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6841 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6842 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6843 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6844 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6845 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6846 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6847 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6848 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6849 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6850 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6851 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6852 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6853 | |
| 6854 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6855 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6856 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6857 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6858 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6859 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6860 | }; |
| 6861 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6862 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6863 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6864 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6865 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6866 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6867 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6868 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6869 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6870 | return 29; |
| 6871 | } |
| 6872 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6873 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6874 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6875 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6876 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6877 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6878 | |
| 6879 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6880 | Fn->addFnAttr("long-call"); |
| 6881 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6882 | Fn->addFnAttr("short-call"); |
| 6883 | |
| 6884 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6885 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6886 | return; |
| 6887 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6888 | if (FD->hasAttr<Mips16Attr>()) { |
| 6889 | Fn->addFnAttr("mips16"); |
| 6890 | } |
| 6891 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6892 | Fn->addFnAttr("nomips16"); |
| 6893 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6894 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6895 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6896 | Fn->addFnAttr("micromips"); |
| 6897 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6898 | Fn->addFnAttr("nomicromips"); |
| 6899 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6900 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6901 | if (!Attr) |
| 6902 | return; |
| 6903 | |
| 6904 | const char *Kind; |
| 6905 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6906 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6907 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6908 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6909 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6910 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6911 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6912 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6913 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6914 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6915 | } |
| 6916 | |
| 6917 | Fn->addFnAttr("interrupt", Kind); |
| 6918 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6919 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6920 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6921 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6922 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6923 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6924 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6925 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6926 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6927 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6928 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6929 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6930 | void MipsABIInfo::CoerceToIntArgs( |
| 6931 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6932 | llvm::IntegerType *IntTy = |
| 6933 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6934 | |
| 6935 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6936 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6937 | ArgList.push_back(IntTy); |
| 6938 | |
| 6939 | // If necessary, add one more integer type to ArgList. |
| 6940 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6941 | |
| 6942 | if (R) |
| 6943 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6946 | // In N32/64, an aligned double precision floating point field is passed in |
| 6947 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6948 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6949 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6950 | |
| 6951 | if (IsO32) { |
| 6952 | CoerceToIntArgs(TySize, ArgList); |
| 6953 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6954 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6955 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6956 | if (Ty->isComplexType()) |
| 6957 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6958 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6959 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6960 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6961 | // Unions/vectors are passed in integer registers. |
| 6962 | if (!RT || !RT->isStructureOrClassType()) { |
| 6963 | CoerceToIntArgs(TySize, ArgList); |
| 6964 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6965 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6966 | |
| 6967 | const RecordDecl *RD = RT->getDecl(); |
| 6968 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6969 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6970 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6971 | uint64_t LastOffset = 0; |
| 6972 | unsigned idx = 0; |
| 6973 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6974 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6975 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6976 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6977 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6978 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6979 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6980 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6981 | |
| 6982 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6983 | continue; |
| 6984 | |
| 6985 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6986 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6987 | continue; |
| 6988 | |
| 6989 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6990 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6991 | ArgList.push_back(I64); |
| 6992 | |
| 6993 | // Add double type. |
| 6994 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6995 | LastOffset = Offset + 64; |
| 6996 | } |
| 6997 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6998 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6999 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7000 | |
| 7001 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7002 | } |
| 7003 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7004 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 7005 | uint64_t Offset) const { |
| 7006 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7007 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7008 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7009 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7010 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 7011 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7012 | ABIArgInfo |
| 7013 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 7014 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7015 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7016 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7017 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7018 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7019 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7020 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 7021 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7022 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 7023 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7024 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7025 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7026 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7027 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7028 | return ABIArgInfo::getIgnore(); |
| 7029 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 7030 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7031 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7032 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7033 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 7034 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7035 | // If we have reached here, aggregates are passed directly by coercing to |
| 7036 | // another structure type. Padding is inserted if the offset of the |
| 7037 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 7038 | ABIArgInfo ArgInfo = |
| 7039 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 7040 | getPaddingType(OrigOffset, CurrOffset)); |
| 7041 | ArgInfo.setInReg(true); |
| 7042 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
| 7045 | // Treat an enum type as its underlying type. |
| 7046 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7047 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7048 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 7049 | // All integral types are promoted to the GPR width. |
| 7050 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7051 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7052 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7053 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7054 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7055 | } |
| 7056 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7057 | llvm::Type* |
| 7058 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7059 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7060 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7061 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7062 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7063 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7064 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 7065 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7066 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7067 | // N32/64 returns struct/classes in floating point registers if the |
| 7068 | // following conditions are met: |
| 7069 | // 1. The size of the struct/class is no larger than 128-bit. |
| 7070 | // 2. The struct/class has one or two fields all of which are floating |
| 7071 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7072 | // 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] | 7073 | // |
| 7074 | // Any other composite results are returned in integer registers. |
| 7075 | // |
| 7076 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 7077 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 7078 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7079 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7080 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7081 | if (!BT || !BT->isFloatingPoint()) |
| 7082 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7083 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7084 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7085 | } |
| 7086 | |
| 7087 | if (b == e) |
| 7088 | return llvm::StructType::get(getVMContext(), RTList, |
| 7089 | RD->hasAttr<PackedAttr>()); |
| 7090 | |
| 7091 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7092 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7093 | } |
| 7094 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7095 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7096 | return llvm::StructType::get(getVMContext(), RTList); |
| 7097 | } |
| 7098 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7099 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 7100 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7101 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 7102 | if (RetTy->isVoidType()) |
| 7103 | return ABIArgInfo::getIgnore(); |
| 7104 | |
| 7105 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 7106 | // However, N32/N64 ignores zero sized return values. |
| 7107 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7108 | return ABIArgInfo::getIgnore(); |
| 7109 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 7110 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7111 | if (Size <= 128) { |
| 7112 | if (RetTy->isAnyComplexType()) |
| 7113 | return ABIArgInfo::getDirect(); |
| 7114 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7115 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 7116 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7117 | if (!IsO32 || |
| 7118 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 7119 | ABIArgInfo ArgInfo = |
| 7120 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 7121 | ArgInfo.setInReg(true); |
| 7122 | return ArgInfo; |
| 7123 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7124 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7125 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7126 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
| 7129 | // Treat an enum type as its underlying type. |
| 7130 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7131 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7132 | |
Stefan Maksimovic | b9da8a5 | 2018-07-30 10:44:46 +0000 | [diff] [blame] | 7133 | if (RetTy->isPromotableIntegerType()) |
| 7134 | return ABIArgInfo::getExtend(RetTy); |
| 7135 | |
| 7136 | if ((RetTy->isUnsignedIntegerOrEnumerationType() || |
| 7137 | RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) |
| 7138 | return ABIArgInfo::getSignExtend(RetTy); |
| 7139 | |
| 7140 | return ABIArgInfo::getDirect(); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7141 | } |
| 7142 | |
| 7143 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7144 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7145 | if (!getCXXABI().classifyReturnType(FI)) |
| 7146 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7147 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7148 | // 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] | 7149 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7150 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7151 | for (auto &I : FI.arguments()) |
| 7152 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7153 | } |
| 7154 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7155 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7156 | QualType OrigTy) const { |
| 7157 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7158 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7159 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 7160 | // 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] | 7161 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7162 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7163 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7164 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7165 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7166 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7167 | DidPromote = true; |
| 7168 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7169 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7170 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7171 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7172 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7173 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7174 | // The alignment of things in the argument area is never larger than |
| 7175 | // StackAlignInBytes. |
| 7176 | TyInfo.second = |
| 7177 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7178 | |
| 7179 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7180 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7181 | |
| 7182 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7183 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7184 | |
| 7185 | |
| 7186 | // If there was a promotion, "unpromote" into a temporary. |
| 7187 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7188 | if (DidPromote) { |
| 7189 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7190 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7191 | |
| 7192 | // Truncate down to the right width. |
| 7193 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7194 | : CGF.IntPtrTy); |
| 7195 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7196 | if (OrigTy->isPointerType()) |
| 7197 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7198 | |
| 7199 | CGF.Builder.CreateStore(V, Temp); |
| 7200 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7201 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7202 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7203 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7204 | } |
| 7205 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7206 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7207 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7208 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7209 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7210 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7211 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7212 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7213 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7216 | bool |
| 7217 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7218 | llvm::Value *Address) const { |
| 7219 | // This information comes from gcc's implementation, which seems to |
| 7220 | // as canonical as it gets. |
| 7221 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7222 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7223 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7224 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7225 | |
| 7226 | // 0-31 are the general purpose registers, $0 - $31. |
| 7227 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7228 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7229 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7230 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7231 | |
| 7232 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7233 | // They are one bit wide and ignored here. |
| 7234 | |
| 7235 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7236 | // (coprocessor 1 is the FP unit) |
| 7237 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7238 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7239 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7240 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7241 | return false; |
| 7242 | } |
| 7243 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7244 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7245 | // AVR ABI Implementation. |
| 7246 | //===----------------------------------------------------------------------===// |
| 7247 | |
| 7248 | namespace { |
| 7249 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7250 | public: |
| 7251 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7252 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7253 | |
| 7254 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7255 | CodeGen::CodeGenModule &CGM) const override { |
| 7256 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7257 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7258 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7259 | if (!FD) return; |
| 7260 | auto *Fn = cast<llvm::Function>(GV); |
| 7261 | |
| 7262 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7263 | Fn->addFnAttr("interrupt"); |
| 7264 | |
| 7265 | if (FD->getAttr<AVRSignalAttr>()) |
| 7266 | Fn->addFnAttr("signal"); |
| 7267 | } |
| 7268 | }; |
| 7269 | } |
| 7270 | |
| 7271 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7272 | // 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] | 7273 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7274 | // handling. |
| 7275 | //===----------------------------------------------------------------------===// |
| 7276 | |
| 7277 | namespace { |
| 7278 | |
| 7279 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7280 | public: |
| 7281 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7282 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7283 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7284 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7285 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7286 | }; |
| 7287 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7288 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7289 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7290 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7291 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7292 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7293 | if (!FD) return; |
| 7294 | |
| 7295 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7296 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7297 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7298 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7299 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7300 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7301 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7302 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7303 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7304 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7305 | llvm::NamedMDNode *OpenCLMetadata = |
| 7306 | M.getModule().getOrInsertNamedMetadata( |
| 7307 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7308 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7309 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7310 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7311 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7312 | Operands.push_back( |
| 7313 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7314 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7315 | Operands.push_back( |
| 7316 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7317 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7318 | Operands.push_back( |
| 7319 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7320 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7321 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7322 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7323 | // (false) for implementing the work_group_size_hint attr later. |
| 7324 | // 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] | 7325 | Operands.push_back( |
| 7326 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7327 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7328 | } |
| 7329 | } |
| 7330 | } |
| 7331 | } |
| 7332 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7333 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7334 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7335 | //===----------------------------------------------------------------------===// |
| 7336 | // Hexagon ABI Implementation |
| 7337 | //===----------------------------------------------------------------------===// |
| 7338 | |
| 7339 | namespace { |
| 7340 | |
| 7341 | class HexagonABIInfo : public ABIInfo { |
| 7342 | |
| 7343 | |
| 7344 | public: |
| 7345 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7346 | |
| 7347 | private: |
| 7348 | |
| 7349 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7350 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7351 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7352 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7353 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7354 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7355 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7356 | }; |
| 7357 | |
| 7358 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7359 | public: |
| 7360 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7361 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7362 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7363 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7364 | return 29; |
| 7365 | } |
| 7366 | }; |
| 7367 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7368 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7369 | |
| 7370 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7371 | if (!getCXXABI().classifyReturnType(FI)) |
| 7372 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7373 | for (auto &I : FI.arguments()) |
| 7374 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
| 7377 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7378 | if (!isAggregateTypeForABI(Ty)) { |
| 7379 | // Treat an enum type as its underlying type. |
| 7380 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7381 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7382 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7383 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7384 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7385 | } |
| 7386 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7387 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7388 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7389 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7390 | // Ignore empty records. |
| 7391 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7392 | return ABIArgInfo::getIgnore(); |
| 7393 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7394 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7395 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7396 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7397 | // Pass in the smallest viable integer type. |
| 7398 | else if (Size > 32) |
| 7399 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7400 | else if (Size > 16) |
| 7401 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7402 | else if (Size > 8) |
| 7403 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7404 | else |
| 7405 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7406 | } |
| 7407 | |
| 7408 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7409 | if (RetTy->isVoidType()) |
| 7410 | return ABIArgInfo::getIgnore(); |
| 7411 | |
| 7412 | // Large vector types should be returned via memory. |
| 7413 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7414 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7415 | |
| 7416 | if (!isAggregateTypeForABI(RetTy)) { |
| 7417 | // Treat an enum type as its underlying type. |
| 7418 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7419 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7420 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7421 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7422 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7423 | } |
| 7424 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7425 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7426 | return ABIArgInfo::getIgnore(); |
| 7427 | |
| 7428 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7429 | // are returned indirectly. |
| 7430 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7431 | if (Size <= 64) { |
| 7432 | // Return in the smallest viable integer type. |
| 7433 | if (Size <= 8) |
| 7434 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7435 | if (Size <= 16) |
| 7436 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7437 | if (Size <= 32) |
| 7438 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7439 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7440 | } |
| 7441 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7442 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7443 | } |
| 7444 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7445 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7446 | QualType Ty) const { |
| 7447 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7448 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7449 | getContext().getTypeInfoInChars(Ty), |
| 7450 | CharUnits::fromQuantity(4), |
| 7451 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7452 | } |
| 7453 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7454 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7455 | // Lanai ABI Implementation |
| 7456 | //===----------------------------------------------------------------------===// |
| 7457 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7458 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7459 | class LanaiABIInfo : public DefaultABIInfo { |
| 7460 | public: |
| 7461 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7462 | |
| 7463 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7464 | |
| 7465 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7466 | CCState State(FI.getCallingConvention()); |
| 7467 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7468 | // regparm attribute set. |
| 7469 | if (FI.getHasRegParm()) { |
| 7470 | State.FreeRegs = FI.getRegParm(); |
| 7471 | } else { |
| 7472 | State.FreeRegs = 4; |
| 7473 | } |
| 7474 | |
| 7475 | if (!getCXXABI().classifyReturnType(FI)) |
| 7476 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7477 | for (auto &I : FI.arguments()) |
| 7478 | I.info = classifyArgumentType(I.type, State); |
| 7479 | } |
| 7480 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7481 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7482 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7483 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7484 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7485 | |
| 7486 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7487 | unsigned Size = getContext().getTypeSize(Ty); |
| 7488 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7489 | |
| 7490 | if (SizeInRegs == 0) |
| 7491 | return false; |
| 7492 | |
| 7493 | if (SizeInRegs > State.FreeRegs) { |
| 7494 | State.FreeRegs = 0; |
| 7495 | return false; |
| 7496 | } |
| 7497 | |
| 7498 | State.FreeRegs -= SizeInRegs; |
| 7499 | |
| 7500 | return true; |
| 7501 | } |
| 7502 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7503 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7504 | CCState &State) const { |
| 7505 | if (!ByVal) { |
| 7506 | if (State.FreeRegs) { |
| 7507 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7508 | return getNaturalAlignIndirectInReg(Ty); |
| 7509 | } |
| 7510 | return getNaturalAlignIndirect(Ty, false); |
| 7511 | } |
| 7512 | |
| 7513 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7514 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7515 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7516 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7517 | /*Realign=*/TypeAlign > |
| 7518 | MinABIStackAlignInBytes); |
| 7519 | } |
| 7520 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7521 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7522 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7523 | // Check with the C++ ABI first. |
| 7524 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7525 | if (RT) { |
| 7526 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7527 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7528 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7529 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7530 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7531 | } |
| 7532 | } |
| 7533 | |
| 7534 | if (isAggregateTypeForABI(Ty)) { |
| 7535 | // Structures with flexible arrays are always indirect. |
| 7536 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7537 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7538 | |
| 7539 | // Ignore empty structs/unions. |
| 7540 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7541 | return ABIArgInfo::getIgnore(); |
| 7542 | |
| 7543 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7544 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7545 | if (SizeInRegs <= State.FreeRegs) { |
| 7546 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7547 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7548 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7549 | State.FreeRegs -= SizeInRegs; |
| 7550 | return ABIArgInfo::getDirectInReg(Result); |
| 7551 | } else { |
| 7552 | State.FreeRegs = 0; |
| 7553 | } |
| 7554 | return getIndirectResult(Ty, true, State); |
| 7555 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7556 | |
| 7557 | // Treat an enum type as its underlying type. |
| 7558 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7559 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7560 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7561 | bool InReg = shouldUseInReg(Ty, State); |
| 7562 | if (Ty->isPromotableIntegerType()) { |
| 7563 | if (InReg) |
| 7564 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7565 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7566 | } |
| 7567 | if (InReg) |
| 7568 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7569 | return ABIArgInfo::getDirect(); |
| 7570 | } |
| 7571 | |
| 7572 | namespace { |
| 7573 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7574 | public: |
| 7575 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7576 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7577 | }; |
| 7578 | } |
| 7579 | |
| 7580 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7581 | // AMDGPU ABI Implementation |
| 7582 | //===----------------------------------------------------------------------===// |
| 7583 | |
| 7584 | namespace { |
| 7585 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7586 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7587 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7588 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7589 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7590 | unsigned numRegsForType(QualType Ty) const; |
| 7591 | |
| 7592 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7593 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7594 | uint64_t Members) const override; |
| 7595 | |
| 7596 | public: |
| 7597 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7598 | DefaultABIInfo(CGT) {} |
| 7599 | |
| 7600 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7601 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7602 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7603 | |
| 7604 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7605 | }; |
| 7606 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7607 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7608 | return true; |
| 7609 | } |
| 7610 | |
| 7611 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7612 | const Type *Base, uint64_t Members) const { |
| 7613 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7614 | |
| 7615 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7616 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7617 | } |
| 7618 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7619 | /// Estimate number of registers the type will use when passed in registers. |
| 7620 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7621 | unsigned NumRegs = 0; |
| 7622 | |
| 7623 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7624 | // Compute from the number of elements. The reported size is based on the |
| 7625 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7626 | QualType EltTy = VT->getElementType(); |
| 7627 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7628 | |
| 7629 | // 16-bit element vectors should be passed as packed. |
| 7630 | if (EltSize == 16) |
| 7631 | return (VT->getNumElements() + 1) / 2; |
| 7632 | |
| 7633 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7634 | return EltNumRegs * VT->getNumElements(); |
| 7635 | } |
| 7636 | |
| 7637 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7638 | const RecordDecl *RD = RT->getDecl(); |
| 7639 | assert(!RD->hasFlexibleArrayMember()); |
| 7640 | |
| 7641 | for (const FieldDecl *Field : RD->fields()) { |
| 7642 | QualType FieldTy = Field->getType(); |
| 7643 | NumRegs += numRegsForType(FieldTy); |
| 7644 | } |
| 7645 | |
| 7646 | return NumRegs; |
| 7647 | } |
| 7648 | |
| 7649 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7650 | } |
| 7651 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7652 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7653 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7654 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7655 | if (!getCXXABI().classifyReturnType(FI)) |
| 7656 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7657 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7658 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7659 | for (auto &Arg : FI.arguments()) { |
| 7660 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7661 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7662 | } else { |
| 7663 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7664 | } |
| 7665 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7666 | } |
| 7667 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7668 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7669 | if (isAggregateTypeForABI(RetTy)) { |
| 7670 | // Records with non-trivial destructors/copy-constructors should not be |
| 7671 | // returned by value. |
| 7672 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7673 | // Ignore empty structs/unions. |
| 7674 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7675 | return ABIArgInfo::getIgnore(); |
| 7676 | |
| 7677 | // Lower single-element structs to just return a regular value. |
| 7678 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7679 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7680 | |
| 7681 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7682 | const RecordDecl *RD = RT->getDecl(); |
| 7683 | if (RD->hasFlexibleArrayMember()) |
| 7684 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7685 | } |
| 7686 | |
| 7687 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7688 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7689 | if (Size <= 16) |
| 7690 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7691 | |
| 7692 | if (Size <= 32) |
| 7693 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7694 | |
| 7695 | if (Size <= 64) { |
| 7696 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7697 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7698 | } |
| 7699 | |
| 7700 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7701 | return ABIArgInfo::getDirect(); |
| 7702 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7703 | } |
| 7704 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7705 | // Otherwise just do the default thing. |
| 7706 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7707 | } |
| 7708 | |
| 7709 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7710 | /// make sense to pass anything byval, so everything must be direct. |
| 7711 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7712 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7713 | |
| 7714 | // TODO: Can we omit empty structs? |
| 7715 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7716 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7717 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7718 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7719 | |
| 7720 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7721 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7722 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7723 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7724 | } |
| 7725 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7726 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7727 | unsigned &NumRegsLeft) const { |
| 7728 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7729 | |
| 7730 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7731 | |
| 7732 | if (isAggregateTypeForABI(Ty)) { |
| 7733 | // Records with non-trivial destructors/copy-constructors should not be |
| 7734 | // passed by value. |
| 7735 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7736 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7737 | |
| 7738 | // Ignore empty structs/unions. |
| 7739 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7740 | return ABIArgInfo::getIgnore(); |
| 7741 | |
| 7742 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7743 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7744 | // though watch out for things like bitfields. |
| 7745 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7746 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7747 | |
| 7748 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7749 | const RecordDecl *RD = RT->getDecl(); |
| 7750 | if (RD->hasFlexibleArrayMember()) |
| 7751 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7752 | } |
| 7753 | |
| 7754 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7755 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7756 | if (Size <= 64) { |
| 7757 | unsigned NumRegs = (Size + 31) / 32; |
| 7758 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7759 | |
| 7760 | if (Size <= 16) |
| 7761 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7762 | |
| 7763 | if (Size <= 32) |
| 7764 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7765 | |
| 7766 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7767 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7768 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7769 | } |
| 7770 | |
| 7771 | if (NumRegsLeft > 0) { |
| 7772 | unsigned NumRegs = numRegsForType(Ty); |
| 7773 | if (NumRegsLeft >= NumRegs) { |
| 7774 | NumRegsLeft -= NumRegs; |
| 7775 | return ABIArgInfo::getDirect(); |
| 7776 | } |
| 7777 | } |
| 7778 | } |
| 7779 | |
| 7780 | // Otherwise just do the default thing. |
| 7781 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7782 | if (!ArgInfo.isIndirect()) { |
| 7783 | unsigned NumRegs = numRegsForType(Ty); |
| 7784 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7785 | } |
| 7786 | |
| 7787 | return ArgInfo; |
| 7788 | } |
| 7789 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7790 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7791 | public: |
| 7792 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7793 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7794 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7795 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7796 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7797 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7798 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7799 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7800 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7801 | LangAS getASTAllocaAddressSpace() const override { |
| 7802 | return getLangASFromTargetAS( |
| 7803 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7804 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7805 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7806 | const VarDecl *D) const override; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7807 | llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, |
| 7808 | llvm::LLVMContext &C) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7809 | llvm::Function * |
| 7810 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7811 | llvm::Function *BlockInvokeFunc, |
| 7812 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7813 | bool shouldEmitStaticExternCAliases() const override; |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7814 | void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7815 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7816 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7817 | |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7818 | static bool requiresAMDGPUProtectedVisibility(const Decl *D, |
| 7819 | llvm::GlobalValue *GV) { |
| 7820 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 7821 | return false; |
| 7822 | |
| 7823 | return D->hasAttr<OpenCLKernelAttr>() || |
| 7824 | (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) || |
| 7825 | (isa<VarDecl>(D) && D->hasAttr<CUDADeviceAttr>()); |
| 7826 | } |
| 7827 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7828 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7829 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7830 | if (requiresAMDGPUProtectedVisibility(D, GV)) { |
| 7831 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 7832 | GV->setDSOLocal(true); |
| 7833 | } |
| 7834 | |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7835 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7836 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7837 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7838 | if (!FD) |
| 7839 | return; |
| 7840 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7841 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7842 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7843 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7844 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7845 | |
| 7846 | if (M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>() && |
| 7847 | (M.getTriple().getOS() == llvm::Triple::AMDHSA)) |
Tony Tye | 68e11a6 | 2018-03-23 18:51:45 +0000 | [diff] [blame] | 7848 | F->addFnAttr("amdgpu-implicitarg-num-bytes", "48"); |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7849 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7850 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7851 | if (ReqdWGS || FlatWGS) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 7852 | unsigned Min = 0; |
| 7853 | unsigned Max = 0; |
| 7854 | if (FlatWGS) { |
| 7855 | Min = FlatWGS->getMin() |
| 7856 | ->EvaluateKnownConstInt(M.getContext()) |
| 7857 | .getExtValue(); |
| 7858 | Max = FlatWGS->getMax() |
| 7859 | ->EvaluateKnownConstInt(M.getContext()) |
| 7860 | .getExtValue(); |
| 7861 | } |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7862 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7863 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7864 | |
| 7865 | if (Min != 0) { |
| 7866 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7867 | |
| 7868 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7869 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7870 | } else |
| 7871 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7872 | } |
| 7873 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7874 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 7875 | unsigned Min = |
| 7876 | Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue(); |
| 7877 | unsigned Max = Attr->getMax() ? Attr->getMax() |
| 7878 | ->EvaluateKnownConstInt(M.getContext()) |
| 7879 | .getExtValue() |
| 7880 | : 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7881 | |
| 7882 | if (Min != 0) { |
| 7883 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7884 | |
| 7885 | std::string AttrVal = llvm::utostr(Min); |
| 7886 | if (Max != 0) |
| 7887 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7888 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7889 | } else |
| 7890 | assert(Max == 0 && "Max must be zero"); |
| 7891 | } |
| 7892 | |
| 7893 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7894 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7895 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7896 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7897 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7898 | } |
| 7899 | |
| 7900 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7901 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7902 | |
| 7903 | if (NumVGPR != 0) |
| 7904 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7905 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7906 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7907 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7908 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7909 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7910 | } |
| 7911 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7912 | // Currently LLVM assumes null pointers always have value 0, |
| 7913 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7914 | // emitting null pointers in private and local address spaces, a null |
| 7915 | // pointer in generic address space is emitted which is casted to a |
| 7916 | // pointer in local or private address space. |
| 7917 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7918 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7919 | QualType QT) const { |
| 7920 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7921 | return llvm::ConstantPointerNull::get(PT); |
| 7922 | |
| 7923 | auto &Ctx = CGM.getContext(); |
| 7924 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7925 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7926 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7927 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7928 | } |
| 7929 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7930 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7931 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7932 | const VarDecl *D) const { |
| 7933 | assert(!CGM.getLangOpts().OpenCL && |
| 7934 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7935 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7936 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 7937 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7938 | if (!D) |
| 7939 | return DefaultGlobalAS; |
| 7940 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7941 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 7942 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7943 | if (AddrSpace != LangAS::Default) |
| 7944 | return AddrSpace; |
| 7945 | |
| 7946 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7947 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7948 | return ConstAS.getValue(); |
| 7949 | } |
| 7950 | return DefaultGlobalAS; |
| 7951 | } |
| 7952 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7953 | llvm::SyncScope::ID |
| 7954 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, |
| 7955 | llvm::LLVMContext &C) const { |
| 7956 | StringRef Name; |
| 7957 | switch (S) { |
| 7958 | case SyncScope::OpenCLWorkGroup: |
| 7959 | Name = "workgroup"; |
| 7960 | break; |
| 7961 | case SyncScope::OpenCLDevice: |
| 7962 | Name = "agent"; |
| 7963 | break; |
| 7964 | case SyncScope::OpenCLAllSVMDevices: |
| 7965 | Name = ""; |
| 7966 | break; |
| 7967 | case SyncScope::OpenCLSubGroup: |
Konstantin Zhuravlyov | 3161c89 | 2019-03-06 20:54:48 +0000 | [diff] [blame] | 7968 | Name = "wavefront"; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7969 | } |
| 7970 | return C.getOrInsertSyncScopeID(Name); |
| 7971 | } |
| 7972 | |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7973 | bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 7974 | return false; |
| 7975 | } |
| 7976 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 7977 | void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7978 | const FunctionType *&FT) const { |
| 7979 | FT = getABIInfo().getContext().adjustFunctionType( |
| 7980 | FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 7981 | } |
| 7982 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7983 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7984 | // SPARC v8 ABI Implementation. |
| 7985 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7986 | // |
| 7987 | // Ensures that complex values are passed in registers. |
| 7988 | // |
| 7989 | namespace { |
| 7990 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7991 | public: |
| 7992 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7993 | |
| 7994 | private: |
| 7995 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7996 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7997 | }; |
| 7998 | } // end anonymous namespace |
| 7999 | |
| 8000 | |
| 8001 | ABIArgInfo |
| 8002 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 8003 | if (Ty->isAnyComplexType()) { |
| 8004 | return ABIArgInfo::getDirect(); |
| 8005 | } |
| 8006 | else { |
| 8007 | return DefaultABIInfo::classifyReturnType(Ty); |
| 8008 | } |
| 8009 | } |
| 8010 | |
| 8011 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8012 | |
| 8013 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8014 | for (auto &Arg : FI.arguments()) |
| 8015 | Arg.info = classifyArgumentType(Arg.type); |
| 8016 | } |
| 8017 | |
| 8018 | namespace { |
| 8019 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8020 | public: |
| 8021 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8022 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 8023 | }; |
| 8024 | } // end anonymous namespace |
| 8025 | |
| 8026 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8027 | // SPARC v9 ABI Implementation. |
| 8028 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8029 | // |
| 8030 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 8031 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 8032 | // the array, structs larger than 16 bytes are passed indirectly. |
| 8033 | // |
| 8034 | // One case requires special care: |
| 8035 | // |
| 8036 | // struct mixed { |
| 8037 | // int i; |
| 8038 | // float f; |
| 8039 | // }; |
| 8040 | // |
| 8041 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 8042 | // parameter array, but the int is passed in an integer register, and the float |
| 8043 | // is passed in a floating point register. This is represented as two arguments |
| 8044 | // with the LLVM IR inreg attribute: |
| 8045 | // |
| 8046 | // declare void f(i32 inreg %i, float inreg %f) |
| 8047 | // |
| 8048 | // The code generator will only allocate 4 bytes from the parameter array for |
| 8049 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 8050 | // bytes. |
| 8051 | // |
| 8052 | namespace { |
| 8053 | class SparcV9ABIInfo : public ABIInfo { |
| 8054 | public: |
| 8055 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 8056 | |
| 8057 | private: |
| 8058 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8059 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8060 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8061 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8062 | |
| 8063 | // Coercion type builder for structs passed in registers. The coercion type |
| 8064 | // serves two purposes: |
| 8065 | // |
| 8066 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 8067 | // in registers. |
| 8068 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 8069 | // code generator knows to pass them in floating point registers. |
| 8070 | // |
| 8071 | // We also compute the InReg flag which indicates that the struct contains |
| 8072 | // aligned 32-bit floats. |
| 8073 | // |
| 8074 | struct CoerceBuilder { |
| 8075 | llvm::LLVMContext &Context; |
| 8076 | const llvm::DataLayout &DL; |
| 8077 | SmallVector<llvm::Type*, 8> Elems; |
| 8078 | uint64_t Size; |
| 8079 | bool InReg; |
| 8080 | |
| 8081 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 8082 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 8083 | |
| 8084 | // Pad Elems with integers until Size is ToSize. |
| 8085 | void pad(uint64_t ToSize) { |
| 8086 | assert(ToSize >= Size && "Cannot remove elements"); |
| 8087 | if (ToSize == Size) |
| 8088 | return; |
| 8089 | |
| 8090 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8091 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8092 | if (Aligned > Size && Aligned <= ToSize) { |
| 8093 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 8094 | Size = Aligned; |
| 8095 | } |
| 8096 | |
| 8097 | // Add whole 64-bit words. |
| 8098 | while (Size + 64 <= ToSize) { |
| 8099 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 8100 | Size += 64; |
| 8101 | } |
| 8102 | |
| 8103 | // Final in-word padding. |
| 8104 | if (Size < ToSize) { |
| 8105 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 8106 | Size = ToSize; |
| 8107 | } |
| 8108 | } |
| 8109 | |
| 8110 | // Add a floating point element at Offset. |
| 8111 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 8112 | // Unaligned floats are treated as integers. |
| 8113 | if (Offset % Bits) |
| 8114 | return; |
| 8115 | // The InReg flag is only required if there are any floats < 64 bits. |
| 8116 | if (Bits < 64) |
| 8117 | InReg = true; |
| 8118 | pad(Offset); |
| 8119 | Elems.push_back(Ty); |
| 8120 | Size = Offset + Bits; |
| 8121 | } |
| 8122 | |
| 8123 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 8124 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 8125 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 8126 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 8127 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 8128 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 8129 | switch (ElemTy->getTypeID()) { |
| 8130 | case llvm::Type::StructTyID: |
| 8131 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 8132 | break; |
| 8133 | case llvm::Type::FloatTyID: |
| 8134 | addFloat(ElemOffset, ElemTy, 32); |
| 8135 | break; |
| 8136 | case llvm::Type::DoubleTyID: |
| 8137 | addFloat(ElemOffset, ElemTy, 64); |
| 8138 | break; |
| 8139 | case llvm::Type::FP128TyID: |
| 8140 | addFloat(ElemOffset, ElemTy, 128); |
| 8141 | break; |
| 8142 | case llvm::Type::PointerTyID: |
| 8143 | if (ElemOffset % 64 == 0) { |
| 8144 | pad(ElemOffset); |
| 8145 | Elems.push_back(ElemTy); |
| 8146 | Size += 64; |
| 8147 | } |
| 8148 | break; |
| 8149 | default: |
| 8150 | break; |
| 8151 | } |
| 8152 | } |
| 8153 | } |
| 8154 | |
| 8155 | // Check if Ty is a usable substitute for the coercion type. |
| 8156 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 8157 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8158 | } |
| 8159 | |
| 8160 | // Get the coercion type as a literal struct type. |
| 8161 | llvm::Type *getType() const { |
| 8162 | if (Elems.size() == 1) |
| 8163 | return Elems.front(); |
| 8164 | else |
| 8165 | return llvm::StructType::get(Context, Elems); |
| 8166 | } |
| 8167 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8168 | }; |
| 8169 | } // end anonymous namespace |
| 8170 | |
| 8171 | ABIArgInfo |
| 8172 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 8173 | if (Ty->isVoidType()) |
| 8174 | return ABIArgInfo::getIgnore(); |
| 8175 | |
| 8176 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8177 | |
| 8178 | // Anything too big to fit in registers is passed with an explicit indirect |
| 8179 | // pointer / sret pointer. |
| 8180 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8181 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8182 | |
| 8183 | // Treat an enum type as its underlying type. |
| 8184 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8185 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8186 | |
| 8187 | // Integer types smaller than a register are extended. |
| 8188 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 8189 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8190 | |
| 8191 | // Other non-aggregates go in registers. |
| 8192 | if (!isAggregateTypeForABI(Ty)) |
| 8193 | return ABIArgInfo::getDirect(); |
| 8194 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8195 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 8196 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 8197 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8198 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8199 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8200 | // 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] | 8201 | // Build a coercion type from the LLVM struct type. |
| 8202 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 8203 | if (!StrTy) |
| 8204 | return ABIArgInfo::getDirect(); |
| 8205 | |
| 8206 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 8207 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8208 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8209 | |
| 8210 | // Try to use the original type for coercion. |
| 8211 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8212 | |
| 8213 | if (CB.InReg) |
| 8214 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8215 | else |
| 8216 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8217 | } |
| 8218 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8219 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8220 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8221 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8222 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8223 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8224 | AI.setCoerceToType(ArgTy); |
| 8225 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8226 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8227 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8228 | CGBuilderTy &Builder = CGF.Builder; |
| 8229 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8230 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8231 | |
| 8232 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8233 | |
| 8234 | Address ArgAddr = Address::invalid(); |
| 8235 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8236 | switch (AI.getKind()) { |
| 8237 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8238 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8239 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8240 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8241 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8242 | case ABIArgInfo::Extend: { |
| 8243 | Stride = SlotSize; |
| 8244 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8245 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8246 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8247 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8248 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8249 | case ABIArgInfo::Direct: { |
| 8250 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8251 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8252 | ArgAddr = Addr; |
| 8253 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8254 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8255 | |
| 8256 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8257 | Stride = SlotSize; |
| 8258 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8259 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8260 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8261 | break; |
| 8262 | |
| 8263 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8264 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8265 | } |
| 8266 | |
| 8267 | // Update VAList. |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8268 | Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); |
| 8269 | Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8270 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8271 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8272 | } |
| 8273 | |
| 8274 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8275 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8276 | for (auto &I : FI.arguments()) |
| 8277 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8278 | } |
| 8279 | |
| 8280 | namespace { |
| 8281 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8282 | public: |
| 8283 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8284 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8285 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8286 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8287 | return 14; |
| 8288 | } |
| 8289 | |
| 8290 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8291 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8292 | }; |
| 8293 | } // end anonymous namespace |
| 8294 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8295 | bool |
| 8296 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8297 | llvm::Value *Address) const { |
| 8298 | // This is calculated from the LLVM and GCC tables and verified |
| 8299 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8300 | |
| 8301 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8302 | |
| 8303 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8304 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8305 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8306 | |
| 8307 | // 0-31: the 8-byte general-purpose registers |
| 8308 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8309 | |
| 8310 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8311 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8312 | |
| 8313 | // Y = 64 |
| 8314 | // PSR = 65 |
| 8315 | // WIM = 66 |
| 8316 | // TBR = 67 |
| 8317 | // PC = 68 |
| 8318 | // NPC = 69 |
| 8319 | // FSR = 70 |
| 8320 | // CSR = 71 |
| 8321 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8322 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8323 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8324 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8325 | |
| 8326 | return false; |
| 8327 | } |
| 8328 | |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8329 | // ARC ABI implementation. |
| 8330 | namespace { |
| 8331 | |
| 8332 | class ARCABIInfo : public DefaultABIInfo { |
| 8333 | public: |
| 8334 | using DefaultABIInfo::DefaultABIInfo; |
| 8335 | |
| 8336 | private: |
| 8337 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8338 | QualType Ty) const override; |
| 8339 | |
| 8340 | void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { |
| 8341 | if (!State.FreeRegs) |
| 8342 | return; |
| 8343 | if (Info.isIndirect() && Info.getInReg()) |
| 8344 | State.FreeRegs--; |
| 8345 | else if (Info.isDirect() && Info.getInReg()) { |
| 8346 | unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; |
| 8347 | if (sz < State.FreeRegs) |
| 8348 | State.FreeRegs -= sz; |
| 8349 | else |
| 8350 | State.FreeRegs = 0; |
| 8351 | } |
| 8352 | } |
| 8353 | |
| 8354 | void computeInfo(CGFunctionInfo &FI) const override { |
| 8355 | CCState State(FI.getCallingConvention()); |
| 8356 | // ARC uses 8 registers to pass arguments. |
| 8357 | State.FreeRegs = 8; |
| 8358 | |
| 8359 | if (!getCXXABI().classifyReturnType(FI)) |
| 8360 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8361 | updateState(FI.getReturnInfo(), FI.getReturnType(), State); |
| 8362 | for (auto &I : FI.arguments()) { |
| 8363 | I.info = classifyArgumentType(I.type, State.FreeRegs); |
| 8364 | updateState(I.info, I.type, State); |
| 8365 | } |
| 8366 | } |
| 8367 | |
| 8368 | ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; |
| 8369 | ABIArgInfo getIndirectByValue(QualType Ty) const; |
| 8370 | ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; |
| 8371 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8372 | }; |
| 8373 | |
| 8374 | class ARCTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8375 | public: |
| 8376 | ARCTargetCodeGenInfo(CodeGenTypes &CGT) |
| 8377 | : TargetCodeGenInfo(new ARCABIInfo(CGT)) {} |
| 8378 | }; |
| 8379 | |
| 8380 | |
| 8381 | ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { |
| 8382 | return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : |
| 8383 | getNaturalAlignIndirect(Ty, false); |
| 8384 | } |
| 8385 | |
| 8386 | ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8387 | // Compute the byval alignment. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8388 | const unsigned MinABIStackAlignInBytes = 4; |
| 8389 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 8390 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 8391 | TypeAlign > MinABIStackAlignInBytes); |
| 8392 | } |
| 8393 | |
| 8394 | Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8395 | QualType Ty) const { |
| 8396 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 8397 | getContext().getTypeInfoInChars(Ty), |
| 8398 | CharUnits::fromQuantity(4), true); |
| 8399 | } |
| 8400 | |
| 8401 | ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, |
| 8402 | uint8_t FreeRegs) const { |
| 8403 | // Handle the generic C++ ABI. |
| 8404 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 8405 | if (RT) { |
| 8406 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 8407 | if (RAA == CGCXXABI::RAA_Indirect) |
| 8408 | return getIndirectByRef(Ty, FreeRegs > 0); |
| 8409 | |
| 8410 | if (RAA == CGCXXABI::RAA_DirectInMemory) |
| 8411 | return getIndirectByValue(Ty); |
| 8412 | } |
| 8413 | |
| 8414 | // Treat an enum type as its underlying type. |
| 8415 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8416 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8417 | |
| 8418 | auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; |
| 8419 | |
| 8420 | if (isAggregateTypeForABI(Ty)) { |
| 8421 | // Structures with flexible arrays are always indirect. |
| 8422 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 8423 | return getIndirectByValue(Ty); |
| 8424 | |
| 8425 | // Ignore empty structs/unions. |
| 8426 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8427 | return ABIArgInfo::getIgnore(); |
| 8428 | |
| 8429 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 8430 | |
| 8431 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 8432 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 8433 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 8434 | |
| 8435 | return FreeRegs >= SizeInRegs ? |
| 8436 | ABIArgInfo::getDirectInReg(Result) : |
| 8437 | ABIArgInfo::getDirect(Result, 0, nullptr, false); |
| 8438 | } |
| 8439 | |
| 8440 | return Ty->isPromotableIntegerType() ? |
| 8441 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) : |
| 8442 | ABIArgInfo::getExtend(Ty)) : |
| 8443 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() : |
| 8444 | ABIArgInfo::getDirect()); |
| 8445 | } |
| 8446 | |
| 8447 | ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { |
| 8448 | if (RetTy->isAnyComplexType()) |
| 8449 | return ABIArgInfo::getDirectInReg(); |
| 8450 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8451 | // Arguments of size > 4 registers are indirect. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8452 | auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; |
| 8453 | if (RetSize > 4) |
| 8454 | return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); |
| 8455 | |
| 8456 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 8457 | } |
| 8458 | |
| 8459 | } // End anonymous namespace. |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8460 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8461 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8462 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8463 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8464 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8465 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8466 | |
| 8467 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8468 | /// it by reference between functions that append to it. |
| 8469 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8470 | |
| 8471 | /// TypeStringCache caches the meta encodings of Types. |
| 8472 | /// |
| 8473 | /// The reason for caching TypeStrings is two fold: |
| 8474 | /// 1. To cache a type's encoding for later uses; |
| 8475 | /// 2. As a means to break recursive member type inclusion. |
| 8476 | /// |
| 8477 | /// A cache Entry can have a Status of: |
| 8478 | /// NonRecursive: The type encoding is not recursive; |
| 8479 | /// Recursive: The type encoding is recursive; |
| 8480 | /// Incomplete: An incomplete TypeString; |
| 8481 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8482 | /// Recursive type encoding. |
| 8483 | /// |
| 8484 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8485 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8486 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8487 | /// the type is encountered. |
| 8488 | /// |
| 8489 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8490 | /// possible. The type itself is recursive and it may contain other types which |
| 8491 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8492 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8493 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8494 | /// |
| 8495 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8496 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8497 | /// are placed into the cache during type expansion as a means to identify and |
| 8498 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8499 | /// the entry becomes IncompleteUsed. |
| 8500 | /// |
| 8501 | /// During the expansion of a RecordType's members: |
| 8502 | /// |
| 8503 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8504 | /// cached encoding is used; |
| 8505 | /// |
| 8506 | /// If the cache contains a Recursive encoding for the member type, the |
| 8507 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8508 | /// |
| 8509 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8510 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8511 | /// |
| 8512 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8513 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8514 | /// it is swapped back in; |
| 8515 | /// |
| 8516 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8517 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8518 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8519 | /// |
| 8520 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8521 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8522 | /// Else the member is part of a recursive type and thus the recursion has |
| 8523 | /// been exited too soon for the encoding to be correct for the member. |
| 8524 | /// |
| 8525 | class TypeStringCache { |
| 8526 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8527 | struct Entry { |
| 8528 | std::string Str; // The encoded TypeString for the type. |
| 8529 | enum Status State; // Information about the encoding in 'Str'. |
| 8530 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8531 | // during the expansion of RecordType's members. |
| 8532 | }; |
| 8533 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8534 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8535 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8536 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8537 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8538 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8539 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8540 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8541 | bool IsRecursive); |
| 8542 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8543 | }; |
| 8544 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8545 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8546 | /// FieldEncoding is a helper for this ordering process. |
| 8547 | class FieldEncoding { |
| 8548 | bool HasName; |
| 8549 | std::string Enc; |
| 8550 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8551 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8552 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8553 | bool operator<(const FieldEncoding &rhs) const { |
| 8554 | if (HasName != rhs.HasName) return HasName; |
| 8555 | return Enc < rhs.Enc; |
| 8556 | } |
| 8557 | }; |
| 8558 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8559 | class XCoreABIInfo : public DefaultABIInfo { |
| 8560 | public: |
| 8561 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8562 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8563 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8564 | }; |
| 8565 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8566 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8567 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8568 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8569 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8570 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8571 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8572 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8573 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8574 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8575 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8576 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8577 | // TODO: this implementation is likely now redundant with the default |
| 8578 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8579 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8580 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8581 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8582 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8583 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8584 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8585 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8586 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8587 | // Handle the argument. |
| 8588 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8589 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8590 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8591 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8592 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8593 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8594 | |
| 8595 | Address Val = Address::invalid(); |
| 8596 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8597 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8598 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8599 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8600 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8601 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8602 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8603 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8604 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8605 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8606 | case ABIArgInfo::Extend: |
| 8607 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8608 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8609 | ArgSize = CharUnits::fromQuantity( |
| 8610 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8611 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8612 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8613 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8614 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8615 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8616 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8617 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8618 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8619 | |
| 8620 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8621 | if (!ArgSize.isZero()) { |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8622 | Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize); |
| 8623 | Builder.CreateStore(APN.getPointer(), VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8624 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8625 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8626 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8627 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8628 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8629 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8630 | /// into the cache as a means to identify and break recursion. |
| 8631 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8632 | /// be reinserted by removeIncomplete(). |
| 8633 | /// All other types of encoding should have been used rather than arriving here. |
| 8634 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8635 | std::string StubEnc) { |
| 8636 | if (!ID) |
| 8637 | return; |
| 8638 | Entry &E = Map[ID]; |
| 8639 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8640 | "Incorrectly use of addIncomplete"); |
| 8641 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8642 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8643 | E.Str.swap(StubEnc); |
| 8644 | E.State = Incomplete; |
| 8645 | ++IncompleteCount; |
| 8646 | } |
| 8647 | |
| 8648 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8649 | /// must be removed from the cache. |
| 8650 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8651 | /// Returns true if the RecordType was defined recursively. |
| 8652 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8653 | if (!ID) |
| 8654 | return false; |
| 8655 | auto I = Map.find(ID); |
| 8656 | assert(I != Map.end() && "Entry not present"); |
| 8657 | Entry &E = I->second; |
| 8658 | assert( (E.State == Incomplete || |
| 8659 | E.State == IncompleteUsed) && |
| 8660 | "Entry must be an incomplete type"); |
| 8661 | bool IsRecursive = false; |
| 8662 | if (E.State == IncompleteUsed) { |
| 8663 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8664 | IsRecursive = true; |
| 8665 | --IncompleteUsedCount; |
| 8666 | } |
| 8667 | if (E.Swapped.empty()) |
| 8668 | Map.erase(I); |
| 8669 | else { |
| 8670 | // Swap the Recursive back. |
| 8671 | E.Swapped.swap(E.Str); |
| 8672 | E.Swapped.clear(); |
| 8673 | E.State = Recursive; |
| 8674 | } |
| 8675 | --IncompleteCount; |
| 8676 | return IsRecursive; |
| 8677 | } |
| 8678 | |
| 8679 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8680 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8681 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8682 | bool IsRecursive) { |
| 8683 | if (!ID || IncompleteUsedCount) |
| 8684 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8685 | Entry &E = Map[ID]; |
| 8686 | if (IsRecursive && !E.Str.empty()) { |
| 8687 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8688 | "This is not the same Recursive entry"); |
| 8689 | // The parent container was not recursive after all, so we could have used |
| 8690 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8691 | // we started viz: IncompleteCount!=0. |
| 8692 | return; |
| 8693 | } |
| 8694 | assert(E.Str.empty() && "Entry already present"); |
| 8695 | E.Str = Str.str(); |
| 8696 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8697 | } |
| 8698 | |
| 8699 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8700 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8701 | /// encoding is Recursive, return an empty StringRef. |
| 8702 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8703 | if (!ID) |
| 8704 | return StringRef(); // We have no key. |
| 8705 | auto I = Map.find(ID); |
| 8706 | if (I == Map.end()) |
| 8707 | return StringRef(); // We have no encoding. |
| 8708 | Entry &E = I->second; |
| 8709 | if (E.State == Recursive && IncompleteCount) |
| 8710 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8711 | |
| 8712 | if (E.State == Incomplete) { |
| 8713 | // The incomplete type is being used to break out of recursion. |
| 8714 | E.State = IncompleteUsed; |
| 8715 | ++IncompleteUsedCount; |
| 8716 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8717 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8718 | } |
| 8719 | |
| 8720 | /// The XCore ABI includes a type information section that communicates symbol |
| 8721 | /// type information to the linker. The linker uses this information to verify |
| 8722 | /// safety/correctness of things such as array bound and pointers et al. |
| 8723 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8724 | /// This type information (TypeString) is emitted into meta data for all global |
| 8725 | /// symbols: definitions, declarations, functions & variables. |
| 8726 | /// |
| 8727 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8728 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8729 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8730 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8731 | /// |
| 8732 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8733 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8734 | |
| 8735 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8736 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8737 | CodeGen::CodeGenModule &CGM) const { |
| 8738 | SmallStringEnc Enc; |
| 8739 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8740 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8741 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8742 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8743 | llvm::NamedMDNode *MD = |
| 8744 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8745 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8746 | } |
| 8747 | } |
| 8748 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8749 | //===----------------------------------------------------------------------===// |
| 8750 | // SPIR ABI Implementation |
| 8751 | //===----------------------------------------------------------------------===// |
| 8752 | |
| 8753 | namespace { |
| 8754 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8755 | public: |
| 8756 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8757 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8758 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8759 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8760 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8761 | } // End anonymous namespace. |
| 8762 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8763 | namespace clang { |
| 8764 | namespace CodeGen { |
| 8765 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8766 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8767 | SPIRABI.computeInfo(FI); |
| 8768 | } |
| 8769 | } |
| 8770 | } |
| 8771 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8772 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8773 | return llvm::CallingConv::SPIR_KERNEL; |
| 8774 | } |
| 8775 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8776 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8777 | const CodeGen::CodeGenModule &CGM, |
| 8778 | TypeStringCache &TSC); |
| 8779 | |
| 8780 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8781 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8782 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8783 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8784 | const RecordDecl *RD, |
| 8785 | const CodeGen::CodeGenModule &CGM, |
| 8786 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8787 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8788 | SmallStringEnc Enc; |
| 8789 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8790 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8791 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8792 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8793 | Enc += "b("; |
| 8794 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8795 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8796 | Enc += ':'; |
| 8797 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8798 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8799 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8800 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8801 | Enc += ')'; |
| 8802 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8803 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8804 | } |
| 8805 | return true; |
| 8806 | } |
| 8807 | |
| 8808 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8809 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8810 | /// Union types have their fields ordered according to the ABI. |
| 8811 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8812 | const CodeGen::CodeGenModule &CGM, |
| 8813 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8814 | // Append the cached TypeString if we have one. |
| 8815 | StringRef TypeString = TSC.lookupStr(ID); |
| 8816 | if (!TypeString.empty()) { |
| 8817 | Enc += TypeString; |
| 8818 | return true; |
| 8819 | } |
| 8820 | |
| 8821 | // Start to emit an incomplete TypeString. |
| 8822 | size_t Start = Enc.size(); |
| 8823 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8824 | Enc += '('; |
| 8825 | if (ID) |
| 8826 | Enc += ID->getName(); |
| 8827 | Enc += "){"; |
| 8828 | |
| 8829 | // We collect all encoded fields and order as necessary. |
| 8830 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8831 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8832 | if (RD && !RD->field_empty()) { |
| 8833 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8834 | // so that recursive calls to this RecordType will use it whilst building a |
| 8835 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8836 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8837 | std::string StubEnc(Enc.substr(Start).str()); |
| 8838 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8839 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8840 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8841 | (void) TSC.removeIncomplete(ID); |
| 8842 | return false; |
| 8843 | } |
| 8844 | IsRecursive = TSC.removeIncomplete(ID); |
| 8845 | // The ABI requires unions to be sorted but not structures. |
| 8846 | // See FieldEncoding::operator< for sort algorithm. |
| 8847 | if (RT->isUnionType()) |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8848 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8849 | // We can now complete the TypeString. |
| 8850 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8851 | for (unsigned I = 0; I != E; ++I) { |
| 8852 | if (I) |
| 8853 | Enc += ','; |
| 8854 | Enc += FE[I].str(); |
| 8855 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8856 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8857 | Enc += '}'; |
| 8858 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8859 | return true; |
| 8860 | } |
| 8861 | |
| 8862 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8863 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8864 | TypeStringCache &TSC, |
| 8865 | const IdentifierInfo *ID) { |
| 8866 | // Append the cached TypeString if we have one. |
| 8867 | StringRef TypeString = TSC.lookupStr(ID); |
| 8868 | if (!TypeString.empty()) { |
| 8869 | Enc += TypeString; |
| 8870 | return true; |
| 8871 | } |
| 8872 | |
| 8873 | size_t Start = Enc.size(); |
| 8874 | Enc += "e("; |
| 8875 | if (ID) |
| 8876 | Enc += ID->getName(); |
| 8877 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8878 | |
| 8879 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8880 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8881 | SmallVector<FieldEncoding, 16> FE; |
| 8882 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8883 | ++I) { |
| 8884 | SmallStringEnc EnumEnc; |
| 8885 | EnumEnc += "m("; |
| 8886 | EnumEnc += I->getName(); |
| 8887 | EnumEnc += "){"; |
| 8888 | I->getInitVal().toString(EnumEnc); |
| 8889 | EnumEnc += '}'; |
| 8890 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8891 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8892 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8893 | unsigned E = FE.size(); |
| 8894 | for (unsigned I = 0; I != E; ++I) { |
| 8895 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8896 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8897 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8898 | } |
| 8899 | } |
| 8900 | Enc += '}'; |
| 8901 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8902 | return true; |
| 8903 | } |
| 8904 | |
| 8905 | /// Appends type's qualifier to Enc. |
| 8906 | /// This is done prior to appending the type's encoding. |
| 8907 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8908 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8909 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8910 | int Lookup = 0; |
| 8911 | if (QT.isConstQualified()) |
| 8912 | Lookup += 1<<0; |
| 8913 | if (QT.isRestrictQualified()) |
| 8914 | Lookup += 1<<1; |
| 8915 | if (QT.isVolatileQualified()) |
| 8916 | Lookup += 1<<2; |
| 8917 | Enc += Table[Lookup]; |
| 8918 | } |
| 8919 | |
| 8920 | /// Appends built-in types to Enc. |
| 8921 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8922 | const char *EncType; |
| 8923 | switch (BT->getKind()) { |
| 8924 | case BuiltinType::Void: |
| 8925 | EncType = "0"; |
| 8926 | break; |
| 8927 | case BuiltinType::Bool: |
| 8928 | EncType = "b"; |
| 8929 | break; |
| 8930 | case BuiltinType::Char_U: |
| 8931 | EncType = "uc"; |
| 8932 | break; |
| 8933 | case BuiltinType::UChar: |
| 8934 | EncType = "uc"; |
| 8935 | break; |
| 8936 | case BuiltinType::SChar: |
| 8937 | EncType = "sc"; |
| 8938 | break; |
| 8939 | case BuiltinType::UShort: |
| 8940 | EncType = "us"; |
| 8941 | break; |
| 8942 | case BuiltinType::Short: |
| 8943 | EncType = "ss"; |
| 8944 | break; |
| 8945 | case BuiltinType::UInt: |
| 8946 | EncType = "ui"; |
| 8947 | break; |
| 8948 | case BuiltinType::Int: |
| 8949 | EncType = "si"; |
| 8950 | break; |
| 8951 | case BuiltinType::ULong: |
| 8952 | EncType = "ul"; |
| 8953 | break; |
| 8954 | case BuiltinType::Long: |
| 8955 | EncType = "sl"; |
| 8956 | break; |
| 8957 | case BuiltinType::ULongLong: |
| 8958 | EncType = "ull"; |
| 8959 | break; |
| 8960 | case BuiltinType::LongLong: |
| 8961 | EncType = "sll"; |
| 8962 | break; |
| 8963 | case BuiltinType::Float: |
| 8964 | EncType = "ft"; |
| 8965 | break; |
| 8966 | case BuiltinType::Double: |
| 8967 | EncType = "d"; |
| 8968 | break; |
| 8969 | case BuiltinType::LongDouble: |
| 8970 | EncType = "ld"; |
| 8971 | break; |
| 8972 | default: |
| 8973 | return false; |
| 8974 | } |
| 8975 | Enc += EncType; |
| 8976 | return true; |
| 8977 | } |
| 8978 | |
| 8979 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8980 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8981 | const CodeGen::CodeGenModule &CGM, |
| 8982 | TypeStringCache &TSC) { |
| 8983 | Enc += "p("; |
| 8984 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8985 | return false; |
| 8986 | Enc += ')'; |
| 8987 | return true; |
| 8988 | } |
| 8989 | |
| 8990 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8991 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8992 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8993 | const CodeGen::CodeGenModule &CGM, |
| 8994 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8995 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8996 | return false; |
| 8997 | Enc += "a("; |
| 8998 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8999 | CAT->getSize().toStringUnsigned(Enc); |
| 9000 | else |
| 9001 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 9002 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9003 | // The Qualifiers should be attached to the type rather than the array. |
| 9004 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9005 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 9006 | return false; |
| 9007 | Enc += ')'; |
| 9008 | return true; |
| 9009 | } |
| 9010 | |
| 9011 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 9012 | /// and the arguments. |
| 9013 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 9014 | const CodeGen::CodeGenModule &CGM, |
| 9015 | TypeStringCache &TSC) { |
| 9016 | Enc += "f{"; |
| 9017 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 9018 | return false; |
| 9019 | Enc += "}("; |
| 9020 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 9021 | // N.B. we are only interested in the adjusted param types. |
| 9022 | auto I = FPT->param_type_begin(); |
| 9023 | auto E = FPT->param_type_end(); |
| 9024 | if (I != E) { |
| 9025 | do { |
| 9026 | if (!appendType(Enc, *I, CGM, TSC)) |
| 9027 | return false; |
| 9028 | ++I; |
| 9029 | if (I != E) |
| 9030 | Enc += ','; |
| 9031 | } while (I != E); |
| 9032 | if (FPT->isVariadic()) |
| 9033 | Enc += ",va"; |
| 9034 | } else { |
| 9035 | if (FPT->isVariadic()) |
| 9036 | Enc += "va"; |
| 9037 | else |
| 9038 | Enc += '0'; |
| 9039 | } |
| 9040 | } |
| 9041 | Enc += ')'; |
| 9042 | return true; |
| 9043 | } |
| 9044 | |
| 9045 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 9046 | /// type encodings. |
| 9047 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 9048 | const CodeGen::CodeGenModule &CGM, |
| 9049 | TypeStringCache &TSC) { |
| 9050 | |
| 9051 | QualType QT = QType.getCanonicalType(); |
| 9052 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9053 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 9054 | // The Qualifiers should be attached to the type rather than the array. |
| 9055 | // Thus we don't call appendQualifier() here. |
| 9056 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 9057 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9058 | appendQualifier(Enc, QT); |
| 9059 | |
| 9060 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 9061 | return appendBuiltinType(Enc, BT); |
| 9062 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9063 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 9064 | return appendPointerType(Enc, PT, CGM, TSC); |
| 9065 | |
| 9066 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 9067 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 9068 | |
| 9069 | if (const RecordType *RT = QT->getAsStructureType()) |
| 9070 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9071 | |
| 9072 | if (const RecordType *RT = QT->getAsUnionType()) |
| 9073 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9074 | |
| 9075 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 9076 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 9077 | |
| 9078 | return false; |
| 9079 | } |
| 9080 | |
| 9081 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 9082 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 9083 | if (!D) |
| 9084 | return false; |
| 9085 | |
| 9086 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9087 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 9088 | return false; |
| 9089 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 9090 | } |
| 9091 | |
| 9092 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 9093 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 9094 | return false; |
| 9095 | QualType QT = VD->getType().getCanonicalType(); |
| 9096 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 9097 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9098 | // The Qualifiers should be attached to the type rather than the array. |
| 9099 | // Thus we don't call appendQualifier() here. |
| 9100 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9101 | } |
| 9102 | return appendType(Enc, QT, CGM, TSC); |
| 9103 | } |
| 9104 | return false; |
| 9105 | } |
| 9106 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9107 | //===----------------------------------------------------------------------===// |
| 9108 | // RISCV ABI Implementation |
| 9109 | //===----------------------------------------------------------------------===// |
| 9110 | |
| 9111 | namespace { |
| 9112 | class RISCVABIInfo : public DefaultABIInfo { |
| 9113 | private: |
| 9114 | unsigned XLen; // Size of the integer ('x') registers in bits. |
| 9115 | static const int NumArgGPRs = 8; |
| 9116 | |
| 9117 | public: |
| 9118 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9119 | : DefaultABIInfo(CGT), XLen(XLen) {} |
| 9120 | |
| 9121 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 9122 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 9123 | void computeInfo(CGFunctionInfo &FI) const override; |
| 9124 | |
| 9125 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, |
| 9126 | int &ArgGPRsLeft) const; |
| 9127 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 9128 | |
| 9129 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9130 | QualType Ty) const override; |
| 9131 | |
| 9132 | ABIArgInfo extendType(QualType Ty) const; |
| 9133 | }; |
| 9134 | } // end anonymous namespace |
| 9135 | |
| 9136 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 9137 | QualType RetTy = FI.getReturnType(); |
| 9138 | if (!getCXXABI().classifyReturnType(FI)) |
| 9139 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 9140 | |
| 9141 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 9142 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 9143 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 9144 | // rewrite the argument list and pass indirectly on RV32. |
| 9145 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 9146 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 9147 | |
| 9148 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 9149 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 9150 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 9151 | // different for variadic arguments, we must also track whether we are |
| 9152 | // examining a vararg or not. |
| 9153 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
| 9154 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 9155 | |
| 9156 | int ArgNum = 0; |
| 9157 | for (auto &ArgInfo : FI.arguments()) { |
| 9158 | bool IsFixed = ArgNum < NumFixedArgs; |
| 9159 | ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft); |
| 9160 | ArgNum++; |
| 9161 | } |
| 9162 | } |
| 9163 | |
| 9164 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
| 9165 | int &ArgGPRsLeft) const { |
| 9166 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 9167 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 9168 | |
| 9169 | // Structures with either a non-trivial destructor or a non-trivial |
| 9170 | // copy constructor are always passed indirectly. |
| 9171 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 9172 | if (ArgGPRsLeft) |
| 9173 | ArgGPRsLeft -= 1; |
| 9174 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 9175 | CGCXXABI::RAA_DirectInMemory); |
| 9176 | } |
| 9177 | |
| 9178 | // Ignore empty structs/unions. |
| 9179 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9180 | return ABIArgInfo::getIgnore(); |
| 9181 | |
| 9182 | uint64_t Size = getContext().getTypeSize(Ty); |
| 9183 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 9184 | bool MustUseStack = false; |
| 9185 | // Determine the number of GPRs needed to pass the current argument |
| 9186 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 9187 | // register pairs, so may consume 3 registers. |
| 9188 | int NeededArgGPRs = 1; |
| 9189 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 9190 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 9191 | else if (Size > XLen && Size <= 2 * XLen) |
| 9192 | NeededArgGPRs = 2; |
| 9193 | |
| 9194 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 9195 | MustUseStack = true; |
| 9196 | NeededArgGPRs = ArgGPRsLeft; |
| 9197 | } |
| 9198 | |
| 9199 | ArgGPRsLeft -= NeededArgGPRs; |
| 9200 | |
| 9201 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 9202 | // Treat an enum type as its underlying type. |
| 9203 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 9204 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 9205 | |
| 9206 | // All integral types are promoted to XLen width, unless passed on the |
| 9207 | // stack. |
| 9208 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 9209 | return extendType(Ty); |
| 9210 | } |
| 9211 | |
| 9212 | return ABIArgInfo::getDirect(); |
| 9213 | } |
| 9214 | |
| 9215 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 9216 | // so coerce to integers. |
| 9217 | if (Size <= 2 * XLen) { |
| 9218 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 9219 | |
| 9220 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 9221 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 9222 | if (Size <= XLen) { |
| 9223 | return ABIArgInfo::getDirect( |
| 9224 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 9225 | } else if (Alignment == 2 * XLen) { |
| 9226 | return ABIArgInfo::getDirect( |
| 9227 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 9228 | } else { |
| 9229 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 9230 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 9231 | } |
| 9232 | } |
| 9233 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 9234 | } |
| 9235 | |
| 9236 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 9237 | if (RetTy->isVoidType()) |
| 9238 | return ABIArgInfo::getIgnore(); |
| 9239 | |
| 9240 | int ArgGPRsLeft = 2; |
| 9241 | |
| 9242 | // The rules for return and argument types are the same, so defer to |
| 9243 | // classifyArgumentType. |
| 9244 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft); |
| 9245 | } |
| 9246 | |
| 9247 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9248 | QualType Ty) const { |
| 9249 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 9250 | |
| 9251 | // Empty records are ignored for parameter passing purposes. |
| 9252 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 9253 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 9254 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 9255 | return Addr; |
| 9256 | } |
| 9257 | |
| 9258 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 9259 | getContext().getTypeInfoInChars(Ty); |
| 9260 | |
| 9261 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 9262 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 9263 | |
| 9264 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 9265 | SlotSize, /*AllowHigherAlign=*/true); |
| 9266 | } |
| 9267 | |
| 9268 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 9269 | int TySize = getContext().getTypeSize(Ty); |
| 9270 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 9271 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 9272 | return ABIArgInfo::getSignExtend(Ty); |
| 9273 | return ABIArgInfo::getExtend(Ty); |
| 9274 | } |
| 9275 | |
| 9276 | namespace { |
| 9277 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 9278 | public: |
| 9279 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9280 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen)) {} |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 9281 | |
| 9282 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 9283 | CodeGen::CodeGenModule &CGM) const override { |
| 9284 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 9285 | if (!FD) return; |
| 9286 | |
| 9287 | const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); |
| 9288 | if (!Attr) |
| 9289 | return; |
| 9290 | |
| 9291 | const char *Kind; |
| 9292 | switch (Attr->getInterrupt()) { |
| 9293 | case RISCVInterruptAttr::user: Kind = "user"; break; |
| 9294 | case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; |
| 9295 | case RISCVInterruptAttr::machine: Kind = "machine"; break; |
| 9296 | } |
| 9297 | |
| 9298 | auto *Fn = cast<llvm::Function>(GV); |
| 9299 | |
| 9300 | Fn->addFnAttr("interrupt", Kind); |
| 9301 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9302 | }; |
| 9303 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9304 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9305 | //===----------------------------------------------------------------------===// |
| 9306 | // Driver code |
| 9307 | //===----------------------------------------------------------------------===// |
| 9308 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9309 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 9310 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9311 | } |
| 9312 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 9313 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9314 | if (TheTargetCodeGenInfo) |
| 9315 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9316 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9317 | // Helper to set the unique_ptr while still keeping the return value. |
| 9318 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 9319 | this->TheTargetCodeGenInfo.reset(P); |
| 9320 | return *P; |
| 9321 | }; |
| 9322 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 9323 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 9324 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9325 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9326 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9327 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 9328 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9329 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 9330 | case llvm::Triple::mips: |
| 9331 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 9332 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9333 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 9334 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9335 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 9336 | case llvm::Triple::mips64: |
| 9337 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9338 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9339 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 9340 | case llvm::Triple::avr: |
| 9341 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 9342 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 9343 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 9344 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9345 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 9346 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9347 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 9348 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 9349 | return SetCGInfo( |
| 9350 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9351 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9352 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9353 | } |
| 9354 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9355 | case llvm::Triple::wasm32: |
| 9356 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9357 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9358 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9359 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 9360 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9361 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9362 | case llvm::Triple::thumbeb: { |
| 9363 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9364 | return SetCGInfo( |
| 9365 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9366 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9367 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9368 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9369 | StringRef ABIStr = getTarget().getABI(); |
| 9370 | if (ABIStr == "apcs-gnu") |
| 9371 | Kind = ARMABIInfo::APCS; |
| 9372 | else if (ABIStr == "aapcs16") |
| 9373 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9374 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9375 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9376 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9377 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9378 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9379 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9380 | |
| 9381 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9382 | } |
| 9383 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9384 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9385 | return SetCGInfo( |
| 9386 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9387 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9388 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9389 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9390 | if (getTarget().getABI() == "elfv2") |
| 9391 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9392 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9393 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9394 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9395 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9396 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9397 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9398 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9399 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9400 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9401 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9402 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9403 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9404 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9405 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9406 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9407 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9408 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9409 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9410 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9411 | case llvm::Triple::nvptx: |
| 9412 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9413 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9414 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9415 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9416 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9417 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9418 | case llvm::Triple::riscv32: |
| 9419 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 32)); |
| 9420 | case llvm::Triple::riscv64: |
| 9421 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 64)); |
| 9422 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9423 | case llvm::Triple::systemz: { |
| 9424 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9425 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9426 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9427 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9428 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9429 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9430 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9431 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9432 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9433 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9434 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9435 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9436 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9437 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9438 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9439 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9440 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9441 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9442 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9443 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9444 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9445 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 9446 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9447 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9448 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9449 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9450 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9451 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9452 | X86AVXABILevel AVXLevel = |
| 9453 | (ABI == "avx512" |
| 9454 | ? X86AVXABILevel::AVX512 |
| 9455 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9456 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9457 | switch (Triple.getOS()) { |
| 9458 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9459 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 9460 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9461 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9462 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9463 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9464 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9465 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9466 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9467 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9468 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9469 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9470 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9471 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9472 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9473 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9474 | case llvm::Triple::sparc: |
| 9475 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9476 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9477 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9478 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9479 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 9480 | case llvm::Triple::arc: |
| 9481 | return SetCGInfo(new ARCTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9482 | case llvm::Triple::spir: |
| 9483 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9484 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9485 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9486 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9487 | |
| 9488 | /// Create an OpenCL kernel for an enqueued block. |
| 9489 | /// |
| 9490 | /// The kernel has the same function type as the block invoke function. Its |
| 9491 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9492 | /// It simply calls the block invoke function then returns. |
| 9493 | llvm::Function * |
| 9494 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9495 | llvm::Function *Invoke, |
| 9496 | llvm::Value *BlockLiteral) const { |
| 9497 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9498 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9499 | for (auto &P : InvokeFT->params()) |
| 9500 | ArgTys.push_back(P); |
| 9501 | auto &C = CGF.getLLVMContext(); |
| 9502 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9503 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9504 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9505 | &CGF.CGM.getModule()); |
| 9506 | auto IP = CGF.Builder.saveIP(); |
| 9507 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9508 | auto &Builder = CGF.Builder; |
| 9509 | Builder.SetInsertPoint(BB); |
| 9510 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9511 | for (auto &A : F->args()) |
| 9512 | Args.push_back(&A); |
| 9513 | Builder.CreateCall(Invoke, Args); |
| 9514 | Builder.CreateRetVoid(); |
| 9515 | Builder.restoreIP(IP); |
| 9516 | return F; |
| 9517 | } |
| 9518 | |
| 9519 | /// Create an OpenCL kernel for an enqueued block. |
| 9520 | /// |
| 9521 | /// The type of the first argument (the block literal) is the struct type |
| 9522 | /// of the block literal instead of a pointer type. The first argument |
| 9523 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9524 | /// allocates the same type of struct on stack and stores the block literal |
| 9525 | /// to it and passes its pointer to the block invoke function. The kernel |
| 9526 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 9527 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 9528 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 9529 | llvm::Value *BlockLiteral) const { |
| 9530 | auto &Builder = CGF.Builder; |
| 9531 | auto &C = CGF.getLLVMContext(); |
| 9532 | |
| 9533 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 9534 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9535 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9536 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 9537 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 9538 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 9539 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 9540 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 9541 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 9542 | |
| 9543 | ArgTys.push_back(BlockTy); |
| 9544 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9545 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 9546 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9547 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9548 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9549 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 9550 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 9551 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9552 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9553 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 9554 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9555 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9556 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9557 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 9558 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9559 | } |
| 9560 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9561 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9562 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9563 | &CGF.CGM.getModule()); |
| 9564 | F->addFnAttr("enqueued-block"); |
| 9565 | auto IP = CGF.Builder.saveIP(); |
| 9566 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9567 | Builder.SetInsertPoint(BB); |
| 9568 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 9569 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
| 9570 | BlockPtr->setAlignment(BlockAlign); |
| 9571 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 9572 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 9573 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9574 | Args.push_back(Cast); |
| 9575 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 9576 | Args.push_back(I); |
| 9577 | Builder.CreateCall(Invoke, Args); |
| 9578 | Builder.CreateRetVoid(); |
| 9579 | Builder.restoreIP(IP); |
| 9580 | |
| 9581 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 9582 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 9583 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 9584 | F->setMetadata("kernel_arg_base_type", |
| 9585 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 9586 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 9587 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 9588 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 9589 | |
| 9590 | return F; |
| 9591 | } |