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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 17 | #include "CGBlocks.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 18 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 19 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 20 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 22 | #include "clang/Basic/CodeGenOptions.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 23 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 24 | #include "clang/CodeGen/SwiftCallingConv.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Triple.h" |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 32 | #include <algorithm> // std::sort |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 33 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace CodeGen; |
| 36 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 37 | // Helper for coercing an aggregate argument or return value into an integer |
| 38 | // array of the same size (including padding) and alignment. This alternate |
| 39 | // coercion happens only for the RenderScript ABI and can be removed after |
| 40 | // runtimes that rely on it are no longer supported. |
| 41 | // |
| 42 | // RenderScript assumes that the size of the argument / return value in the IR |
| 43 | // is the same as the size of the corresponding qualified type. This helper |
| 44 | // coerces the aggregate type into an array of the same size (including |
| 45 | // padding). This coercion is used in lieu of expansion of struct members or |
| 46 | // other canonical coercions that return a coerced-type of larger size. |
| 47 | // |
| 48 | // Ty - The argument / return value type |
| 49 | // Context - The associated ASTContext |
| 50 | // LLVMContext - The associated LLVMContext |
| 51 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 52 | ASTContext &Context, |
| 53 | llvm::LLVMContext &LLVMContext) { |
| 54 | // Alignment and Size are measured in bits. |
| 55 | const uint64_t Size = Context.getTypeSize(Ty); |
| 56 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 57 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 58 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 59 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 60 | } |
| 61 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 62 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 63 | llvm::Value *Array, |
| 64 | llvm::Value *Value, |
| 65 | unsigned FirstIndex, |
| 66 | unsigned LastIndex) { |
| 67 | // Alternatively, we could emit this as a loop in the source. |
| 68 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 69 | llvm::Value *Cell = |
| 70 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 71 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 75 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 76 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 77 | T->isMemberFunctionPointerType(); |
| 78 | } |
| 79 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 80 | ABIArgInfo |
| 81 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 82 | llvm::Type *Padding) const { |
| 83 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 84 | ByRef, Realign, Padding); |
| 85 | } |
| 86 | |
| 87 | ABIArgInfo |
| 88 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 89 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 90 | /*ByRef*/ false, Realign); |
| 91 | } |
| 92 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 93 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 94 | QualType Ty) const { |
| 95 | return Address::invalid(); |
| 96 | } |
| 97 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 98 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 99 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 100 | /// Does the given lowering require more than the given number of |
| 101 | /// registers when expanded? |
| 102 | /// |
| 103 | /// This is intended to be the basis of a reasonable basic implementation |
| 104 | /// of should{Pass,Return}IndirectlyForSwift. |
| 105 | /// |
| 106 | /// For most targets, a limit of four total registers is reasonable; this |
| 107 | /// limits the amount of code required in order to move around the value |
| 108 | /// in case it wasn't produced immediately prior to the call by the caller |
| 109 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 110 | /// immediately within the callee. But some targets may need to further |
| 111 | /// limit the register count due to an inability to support that many |
| 112 | /// return registers. |
| 113 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 114 | ArrayRef<llvm::Type*> scalarTypes, |
| 115 | unsigned maxAllRegisters) { |
| 116 | unsigned intCount = 0, fpCount = 0; |
| 117 | for (llvm::Type *type : scalarTypes) { |
| 118 | if (type->isPointerTy()) { |
| 119 | intCount++; |
| 120 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 121 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 122 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 123 | } else { |
| 124 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 125 | fpCount++; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return (intCount + fpCount > maxAllRegisters); |
| 130 | } |
| 131 | |
| 132 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 133 | llvm::Type *eltTy, |
| 134 | unsigned numElts) const { |
| 135 | // The default implementation of this assumes that the target guarantees |
| 136 | // 128-bit SIMD support but nothing more. |
| 137 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 138 | } |
| 139 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 140 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 141 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 142 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 143 | if (!RD) { |
| 144 | if (!RT->getDecl()->canPassInRegisters()) |
| 145 | return CGCXXABI::RAA_Indirect; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 146 | return CGCXXABI::RAA_Default; |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 147 | } |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 148 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 152 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 153 | const RecordType *RT = T->getAs<RecordType>(); |
| 154 | if (!RT) |
| 155 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 156 | return getRecordArgABI(RT, CXXABI); |
| 157 | } |
| 158 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 159 | static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, |
| 160 | const ABIInfo &Info) { |
| 161 | QualType Ty = FI.getReturnType(); |
| 162 | |
| 163 | if (const auto *RT = Ty->getAs<RecordType>()) |
| 164 | if (!isa<CXXRecordDecl>(RT->getDecl()) && |
| 165 | !RT->getDecl()->canPassInRegisters()) { |
| 166 | FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | return CXXABI.classifyReturnType(FI); |
| 171 | } |
| 172 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 173 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 174 | /// should ensure that all elements of the union have the same "machine type". |
| 175 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 176 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 177 | const RecordDecl *UD = UT->getDecl(); |
| 178 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 179 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 180 | return UD->field_begin()->getType(); |
| 181 | } |
| 182 | } |
| 183 | return Ty; |
| 184 | } |
| 185 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 186 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 187 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 190 | ASTContext &ABIInfo::getContext() const { |
| 191 | return CGT.getContext(); |
| 192 | } |
| 193 | |
| 194 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 195 | return CGT.getLLVMContext(); |
| 196 | } |
| 197 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 198 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 199 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 200 | } |
| 201 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 202 | const TargetInfo &ABIInfo::getTarget() const { |
| 203 | return CGT.getTarget(); |
| 204 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 205 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 206 | const CodeGenOptions &ABIInfo::getCodeGenOpts() const { |
| 207 | return CGT.getCodeGenOpts(); |
| 208 | } |
| 209 | |
| 210 | bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); } |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 211 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 212 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 217 | uint64_t Members) const { |
| 218 | return false; |
| 219 | } |
| 220 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 221 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 222 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 223 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 224 | switch (TheKind) { |
| 225 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 226 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 227 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 228 | Ty->print(OS); |
| 229 | else |
| 230 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 231 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 232 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 233 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 234 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 235 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 236 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 237 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 238 | case InAlloca: |
| 239 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 240 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 241 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 242 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 243 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 244 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 245 | break; |
| 246 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 247 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 248 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 249 | case CoerceAndExpand: |
| 250 | OS << "CoerceAndExpand Type="; |
| 251 | getCoerceAndExpandType()->print(OS); |
| 252 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 253 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 254 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 257 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 258 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 259 | llvm::Value *Ptr, |
| 260 | CharUnits Align) { |
| 261 | llvm::Value *PtrAsInt = Ptr; |
| 262 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 263 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 264 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 265 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 266 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 267 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 268 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 269 | Ptr->getType(), |
| 270 | Ptr->getName() + ".aligned"); |
| 271 | return PtrAsInt; |
| 272 | } |
| 273 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 274 | /// Emit va_arg for a platform using the common void* representation, |
| 275 | /// where arguments are simply emitted in an array of slots on the stack. |
| 276 | /// |
| 277 | /// This version implements the core direct-value passing rules. |
| 278 | /// |
| 279 | /// \param SlotSize - The size and alignment of a stack slot. |
| 280 | /// Each argument will be allocated to a multiple of this number of |
| 281 | /// slots, and all the slots will be aligned to this value. |
| 282 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 283 | /// an argument type with an alignment greater than the slot size |
| 284 | /// will be emitted on a higher-alignment address, potentially |
| 285 | /// leaving one or more empty slots behind as padding. If this |
| 286 | /// is false, the returned address might be less-aligned than |
| 287 | /// DirectAlign. |
| 288 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 289 | Address VAListAddr, |
| 290 | llvm::Type *DirectTy, |
| 291 | CharUnits DirectSize, |
| 292 | CharUnits DirectAlign, |
| 293 | CharUnits SlotSize, |
| 294 | bool AllowHigherAlign) { |
| 295 | // Cast the element type to i8* if necessary. Some platforms define |
| 296 | // va_list as a struct containing an i8* instead of just an i8*. |
| 297 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 298 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 299 | |
| 300 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 301 | |
| 302 | // If the CC aligns values higher than the slot size, do so if needed. |
| 303 | Address Addr = Address::invalid(); |
| 304 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 305 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 306 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 307 | } else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 308 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 312 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 313 | llvm::Value *NextPtr = |
| 314 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 315 | "argp.next"); |
| 316 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 317 | |
| 318 | // If the argument is smaller than a slot, and this is a big-endian |
| 319 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 320 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 321 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 322 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 323 | } |
| 324 | |
| 325 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 326 | return Addr; |
| 327 | } |
| 328 | |
| 329 | /// Emit va_arg for a platform using the common void* representation, |
| 330 | /// where arguments are simply emitted in an array of slots on the stack. |
| 331 | /// |
| 332 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 333 | /// \param ValueInfo - The size and alignment of this type, generally |
| 334 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 335 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 336 | /// Each argument will be allocated to a multiple of this number of |
| 337 | /// slots, and all the slots will be aligned to this value. |
| 338 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 339 | /// an argument type with an alignment greater than the slot size |
| 340 | /// will be emitted on a higher-alignment address, potentially |
| 341 | /// leaving one or more empty slots behind as padding. |
| 342 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 343 | QualType ValueTy, bool IsIndirect, |
| 344 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 345 | CharUnits SlotSizeAndAlign, |
| 346 | bool AllowHigherAlign) { |
| 347 | // The size and alignment of the value that was passed directly. |
| 348 | CharUnits DirectSize, DirectAlign; |
| 349 | if (IsIndirect) { |
| 350 | DirectSize = CGF.getPointerSize(); |
| 351 | DirectAlign = CGF.getPointerAlign(); |
| 352 | } else { |
| 353 | DirectSize = ValueInfo.first; |
| 354 | DirectAlign = ValueInfo.second; |
| 355 | } |
| 356 | |
| 357 | // Cast the address we've calculated to the right type. |
| 358 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 359 | if (IsIndirect) |
| 360 | DirectTy = DirectTy->getPointerTo(0); |
| 361 | |
| 362 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 363 | DirectSize, DirectAlign, |
| 364 | SlotSizeAndAlign, |
| 365 | AllowHigherAlign); |
| 366 | |
| 367 | if (IsIndirect) { |
| 368 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 369 | } |
| 370 | |
| 371 | return Addr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 372 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 376 | Address Addr1, llvm::BasicBlock *Block1, |
| 377 | Address Addr2, llvm::BasicBlock *Block2, |
| 378 | const llvm::Twine &Name = "") { |
| 379 | assert(Addr1.getType() == Addr2.getType()); |
| 380 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 381 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 382 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 383 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 384 | return Address(PHI, Align); |
| 385 | } |
| 386 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 387 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 388 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 389 | // If someone can figure out a general rule for this, that would be great. |
| 390 | // It's probably just doomed to be platform-dependent, though. |
| 391 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 392 | // Verified for: |
| 393 | // x86-64 FreeBSD, Linux, Darwin |
| 394 | // x86-32 FreeBSD, Linux, Darwin |
| 395 | // PowerPC Linux, Darwin |
| 396 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 397 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 398 | return 32; |
| 399 | } |
| 400 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 401 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 402 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 403 | // The following conventions are known to require this to be false: |
| 404 | // x86_stdcall |
| 405 | // MIPS |
| 406 | // For everything else, we just prefer false unless we opt out. |
| 407 | return false; |
| 408 | } |
| 409 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 410 | void |
| 411 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 412 | llvm::SmallString<24> &Opt) const { |
| 413 | // This assumes the user is passing a library name like "rt" instead of a |
| 414 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 415 | // dynamic. |
| 416 | Opt = "-l"; |
| 417 | Opt += Lib; |
| 418 | } |
| 419 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 420 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 421 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 422 | // set with clSetKernelArg(), not as normal sub-functions. |
| 423 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 424 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 425 | // gets one matching argument in the produced kernel function argument |
| 426 | // list to enable feasible implementation of clSetKernelArg() with |
| 427 | // aggregates etc. In case we would use the default C calling conv here, |
| 428 | // clSetKernelArg() might break depending on the target-specific |
| 429 | // conventions; different targets might split structs passed as values |
| 430 | // to multiple function arguments etc. |
| 431 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 432 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 433 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 434 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 435 | llvm::PointerType *T, QualType QT) const { |
| 436 | return llvm::ConstantPointerNull::get(T); |
| 437 | } |
| 438 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 439 | LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 440 | const VarDecl *D) const { |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 441 | assert(!CGM.getLangOpts().OpenCL && |
| 442 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 443 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 444 | return D ? D->getType().getAddressSpace() : LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 447 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 448 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, |
| 449 | LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 450 | // Since target may map different address spaces in AST to the same address |
| 451 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 452 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 453 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 454 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DestTy); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 457 | llvm::Constant * |
| 458 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 459 | LangAS SrcAddr, LangAS DestAddr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 460 | llvm::Type *DestTy) const { |
| 461 | // Since target may map different address spaces in AST to the same address |
| 462 | // space, an address space conversion may end up as a bitcast. |
| 463 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 464 | } |
| 465 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 466 | llvm::SyncScope::ID |
| 467 | TargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, llvm::LLVMContext &C) const { |
| 468 | return C.getOrInsertSyncScopeID(""); /* default sync scope */ |
| 469 | } |
| 470 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 471 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 472 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 473 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 474 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 475 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 476 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 477 | if (FD->isUnnamedBitfield()) |
| 478 | return true; |
| 479 | |
| 480 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 481 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 482 | // Constant arrays of empty records count as empty, strip them off. |
| 483 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 484 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 485 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 486 | if (AT->getSize() == 0) |
| 487 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 488 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 489 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 490 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 491 | const RecordType *RT = FT->getAs<RecordType>(); |
| 492 | if (!RT) |
| 493 | return false; |
| 494 | |
| 495 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 496 | // |
| 497 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 498 | // current ABI. |
| 499 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 500 | return false; |
| 501 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 502 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 505 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 506 | /// fields. Note that a structure with a flexible array member is not |
| 507 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 508 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 509 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 510 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 511 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 512 | const RecordDecl *RD = RT->getDecl(); |
| 513 | if (RD->hasFlexibleArrayMember()) |
| 514 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 515 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 516 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 517 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 518 | for (const auto &I : CXXRD->bases()) |
| 519 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 520 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 521 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 522 | for (const auto *I : RD->fields()) |
| 523 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 524 | return false; |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | /// isSingleElementStruct - Determine if a structure is a "single |
| 529 | /// element struct", i.e. it has exactly one non-empty field or |
| 530 | /// exactly one field which is itself a single element |
| 531 | /// struct. Structures with flexible array members are never |
| 532 | /// considered single element structs. |
| 533 | /// |
| 534 | /// \return The field declaration for the single non-empty field, if |
| 535 | /// it exists. |
| 536 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 537 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 538 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 539 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 540 | |
| 541 | const RecordDecl *RD = RT->getDecl(); |
| 542 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 543 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 544 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 545 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 546 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 547 | // If this is a C++ record, check the bases first. |
| 548 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 549 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 550 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 551 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 552 | continue; |
| 553 | |
| 554 | // If we already found an element then this isn't a single-element struct. |
| 555 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 556 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 557 | |
| 558 | // If this is non-empty and not a single element struct, the composite |
| 559 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 560 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 561 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 562 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 567 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 568 | QualType FT = FD->getType(); |
| 569 | |
| 570 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 571 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 572 | continue; |
| 573 | |
| 574 | // If we already found an element then this isn't a single-element |
| 575 | // struct. |
| 576 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 577 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 578 | |
| 579 | // Treat single element arrays as the element. |
| 580 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 581 | if (AT->getSize().getZExtValue() != 1) |
| 582 | break; |
| 583 | FT = AT->getElementType(); |
| 584 | } |
| 585 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 586 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 587 | Found = FT.getTypePtr(); |
| 588 | } else { |
| 589 | Found = isSingleElementStruct(FT, Context); |
| 590 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 591 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 595 | // We don't consider a struct a single-element struct if it has |
| 596 | // padding beyond the element type. |
| 597 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 598 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 599 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 600 | return Found; |
| 601 | } |
| 602 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 603 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 604 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 605 | const ABIArgInfo &AI) { |
| 606 | // This default implementation defers to the llvm backend's va_arg |
| 607 | // instruction. It can handle only passing arguments directly |
| 608 | // (typically only handled in the backend for primitive types), or |
| 609 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 610 | // flag has ABI impact in the callee, this implementation cannot |
| 611 | // work.) |
| 612 | |
| 613 | // Only a few cases are covered here at the moment -- those needed |
| 614 | // by the default abi. |
| 615 | llvm::Value *Val; |
| 616 | |
| 617 | if (AI.isIndirect()) { |
| 618 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 619 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 620 | assert( |
| 621 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 622 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 623 | |
| 624 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 625 | CharUnits TyAlignForABI = TyInfo.second; |
| 626 | |
| 627 | llvm::Type *BaseTy = |
| 628 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 629 | llvm::Value *Addr = |
| 630 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 631 | return Address(Addr, TyAlignForABI); |
| 632 | } else { |
| 633 | assert((AI.isDirect() || AI.isExtend()) && |
| 634 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 635 | |
| 636 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 637 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 638 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 639 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 640 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 641 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 642 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 643 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 644 | |
| 645 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 646 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 647 | CGF.Builder.CreateStore(Val, Temp); |
| 648 | return Temp; |
| 649 | } |
| 650 | } |
| 651 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 652 | /// DefaultABIInfo - The default implementation for ABI specific |
| 653 | /// details. This implementation provides information which results in |
| 654 | /// self-consistent and sensible LLVM IR generation, but does not |
| 655 | /// conform to any particular ABI. |
| 656 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 657 | public: |
| 658 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 660 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 661 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 662 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 663 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 664 | if (!getCXXABI().classifyReturnType(FI)) |
| 665 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 666 | for (auto &I : FI.arguments()) |
| 667 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 668 | } |
| 669 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 670 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 671 | QualType Ty) const override { |
| 672 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 673 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 676 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 677 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 678 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 679 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 680 | }; |
| 681 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 682 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 683 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 684 | |
| 685 | if (isAggregateTypeForABI(Ty)) { |
| 686 | // Records with non-trivial destructors/copy-constructors should not be |
| 687 | // passed by value. |
| 688 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 689 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 690 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 691 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 692 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 693 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 694 | // Treat an enum type as its underlying type. |
| 695 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 696 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 697 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 698 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 699 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 702 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 703 | if (RetTy->isVoidType()) |
| 704 | return ABIArgInfo::getIgnore(); |
| 705 | |
| 706 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 707 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 708 | |
| 709 | // Treat an enum type as its underlying type. |
| 710 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 711 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 712 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 713 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 714 | : ABIArgInfo::getDirect()); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 717 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 718 | // WebAssembly ABI Implementation |
| 719 | // |
| 720 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 721 | //===----------------------------------------------------------------------===// |
| 722 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 723 | class WebAssemblyABIInfo final : public SwiftABIInfo { |
| 724 | DefaultABIInfo defaultInfo; |
| 725 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 726 | public: |
| 727 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 728 | : SwiftABIInfo(CGT), defaultInfo(CGT) {} |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 729 | |
| 730 | private: |
| 731 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 732 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 733 | |
| 734 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 735 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 736 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 737 | void computeInfo(CGFunctionInfo &FI) const override { |
| 738 | if (!getCXXABI().classifyReturnType(FI)) |
| 739 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 740 | for (auto &Arg : FI.arguments()) |
| 741 | Arg.info = classifyArgumentType(Arg.type); |
| 742 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 743 | |
| 744 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 745 | QualType Ty) const override; |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 746 | |
| 747 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 748 | bool asReturnValue) const override { |
| 749 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 750 | } |
| 751 | |
| 752 | bool isSwiftErrorInRegister() const override { |
| 753 | return false; |
| 754 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 755 | }; |
| 756 | |
| 757 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 758 | public: |
| 759 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 760 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 761 | |
| 762 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 763 | CodeGen::CodeGenModule &CGM) const override { |
| 764 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 765 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 766 | if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) |
| 767 | Fn->addFnAttr("no-prototype"); |
| 768 | } |
| 769 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 770 | }; |
| 771 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 772 | /// Classify argument of given type \p Ty. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 773 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 774 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 775 | |
| 776 | if (isAggregateTypeForABI(Ty)) { |
| 777 | // Records with non-trivial destructors/copy-constructors should not be |
| 778 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 779 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 780 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 781 | // Ignore empty structs/unions. |
| 782 | if (isEmptyRecord(getContext(), Ty, true)) |
| 783 | return ABIArgInfo::getIgnore(); |
| 784 | // Lower single-element structs to just pass a regular value. TODO: We |
| 785 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 786 | // though watch out for things like bitfields. |
| 787 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 788 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 792 | return defaultInfo.classifyArgumentType(Ty); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 796 | if (isAggregateTypeForABI(RetTy)) { |
| 797 | // Records with non-trivial destructors/copy-constructors should not be |
| 798 | // returned by value. |
| 799 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 800 | // Ignore empty structs/unions. |
| 801 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 802 | return ABIArgInfo::getIgnore(); |
| 803 | // Lower single-element structs to just return a regular value. TODO: We |
| 804 | // could do reasonable-size multiple-element structs too, using |
| 805 | // ABIArgInfo::getDirect(). |
| 806 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 807 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 812 | return defaultInfo.classifyReturnType(RetTy); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 815 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 816 | QualType Ty) const { |
| 817 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, |
| 818 | getContext().getTypeInfoInChars(Ty), |
| 819 | CharUnits::fromQuantity(4), |
| 820 | /*AllowHigherAlign=*/ true); |
| 821 | } |
| 822 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 823 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 824 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 825 | // |
| 826 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 827 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 828 | //===----------------------------------------------------------------------===// |
| 829 | |
| 830 | class PNaClABIInfo : public ABIInfo { |
| 831 | public: |
| 832 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 833 | |
| 834 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 835 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 836 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 837 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 838 | Address EmitVAArg(CodeGenFunction &CGF, |
| 839 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 840 | }; |
| 841 | |
| 842 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 843 | public: |
| 844 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 845 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 846 | }; |
| 847 | |
| 848 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 849 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 850 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 851 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 852 | for (auto &I : FI.arguments()) |
| 853 | I.info = classifyArgumentType(I.type); |
| 854 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 855 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 856 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 857 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 858 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 859 | // function classification. Structs get passed directly for varargs |
| 860 | // functions, through a rewriting transform in |
| 861 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 862 | // this target to actually support a va_arg instructions with an |
| 863 | // aggregate type, unlike other targets. |
| 864 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 867 | /// Classify argument of given type \p Ty. |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 868 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 869 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 870 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 871 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 872 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 873 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 874 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 875 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 876 | } else if (Ty->isFloatingType()) { |
| 877 | // Floating-point types don't go inreg. |
| 878 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 879 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 880 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 881 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 882 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 886 | if (RetTy->isVoidType()) |
| 887 | return ABIArgInfo::getIgnore(); |
| 888 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 889 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 890 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 891 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 892 | |
| 893 | // Treat an enum type as its underlying type. |
| 894 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 895 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 896 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 897 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 898 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 901 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 902 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 903 | // 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] | 904 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 905 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 906 | IRType->getScalarSizeInBits() != 64; |
| 907 | } |
| 908 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 909 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 910 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 911 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 912 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 913 | .Cases("y", "&y", "^Ym", true) |
| 914 | .Default(false); |
| 915 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 916 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 917 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 918 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 921 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 925 | return Ty; |
| 926 | } |
| 927 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 928 | /// Returns true if this type can be passed in SSE registers with the |
| 929 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 930 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 931 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 932 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 933 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 934 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 935 | &llvm::APFloat::x87DoubleExtended()) |
| 936 | return false; |
| 937 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 938 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 939 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 940 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 941 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 942 | // registers specially. |
| 943 | unsigned VecSize = Context.getTypeSize(VT); |
| 944 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 945 | return true; |
| 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 951 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 952 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 953 | return NumMembers <= 4; |
| 954 | } |
| 955 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 956 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 957 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 958 | auto AI = ABIArgInfo::getDirect(T); |
| 959 | AI.setInReg(true); |
| 960 | AI.setCanBeFlattened(false); |
| 961 | return AI; |
| 962 | } |
| 963 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 964 | //===----------------------------------------------------------------------===// |
| 965 | // X86-32 ABI Implementation |
| 966 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 967 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 968 | /// Similar to llvm::CCState, but for Clang. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 969 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 970 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 971 | |
| 972 | unsigned CC; |
| 973 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 974 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 975 | }; |
| 976 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 977 | enum { |
| 978 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 979 | VectorcallMaxParamNumAsReg = 6 |
| 980 | }; |
| 981 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 982 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 983 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 984 | enum Class { |
| 985 | Integer, |
| 986 | Float |
| 987 | }; |
| 988 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 989 | static const unsigned MinABIStackAlignInBytes = 4; |
| 990 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 991 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 992 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 993 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 994 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 995 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 996 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 997 | |
| 998 | static bool isRegisterSize(unsigned Size) { |
| 999 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 1000 | } |
| 1001 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1002 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1003 | // FIXME: Assumes vectorcall is in use. |
| 1004 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1005 | } |
| 1006 | |
| 1007 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1008 | uint64_t NumMembers) const override { |
| 1009 | // FIXME: Assumes vectorcall is in use. |
| 1010 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1011 | } |
| 1012 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1013 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1014 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1015 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 1016 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1017 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 1018 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1019 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1020 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1021 | /// Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1022 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1023 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1024 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1025 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1026 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1027 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1028 | /// Updates the number of available free registers, returns |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1029 | /// true if any registers were allocated. |
| 1030 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 1031 | |
| 1032 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1033 | bool &NeedsPadding) const; |
| 1034 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1035 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1036 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1037 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1038 | /// Rewrite the function info so that all memory arguments use |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1039 | /// inalloca. |
| 1040 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1041 | |
| 1042 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1043 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1044 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1045 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1046 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1047 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1048 | public: |
| 1049 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1050 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1051 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1052 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1053 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1054 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1055 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1056 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1057 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1058 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1059 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1060 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1061 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1062 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1063 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1064 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1065 | bool asReturnValue) const override { |
| 1066 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1067 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1068 | // four vector registers for vectors, but those can overlap with the |
| 1069 | // scalar registers. |
| 1070 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1071 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1072 | |
| 1073 | bool isSwiftErrorInRegister() const override { |
| 1074 | // x86-32 lowering does not support passing swifterror in a register. |
| 1075 | return false; |
| 1076 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1077 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1078 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1079 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1080 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1081 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1082 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1083 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 1084 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1085 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 1086 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1087 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1088 | static bool isStructReturnInRegABI( |
| 1089 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1090 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1091 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1092 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1093 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1094 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1095 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1096 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1097 | return 4; |
| 1098 | } |
| 1099 | |
| 1100 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1101 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1102 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1103 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1104 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1105 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1106 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1107 | } |
| 1108 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1109 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1110 | std::string &Constraints, |
| 1111 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1112 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1113 | std::vector<LValue> &ResultRegDests, |
| 1114 | std::string &AsmString, |
| 1115 | unsigned NumOutputs) const override; |
| 1116 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1117 | llvm::Constant * |
| 1118 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1119 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1120 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1121 | ('v' << 16) | |
| 1122 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1123 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1124 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1125 | |
| 1126 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1127 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1128 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1129 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1130 | }; |
| 1131 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1132 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1133 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1134 | /// Rewrite input constraint references after adding some output constraints. |
| 1135 | /// In the case where there is one output and one input and we add one output, |
| 1136 | /// we need to replace all operand references greater than or equal to 1: |
| 1137 | /// mov $0, $1 |
| 1138 | /// mov eax, $1 |
| 1139 | /// The result will be: |
| 1140 | /// mov $0, $2 |
| 1141 | /// mov eax, $2 |
| 1142 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1143 | unsigned NumNewOuts, |
| 1144 | std::string &AsmString) { |
| 1145 | std::string Buf; |
| 1146 | llvm::raw_string_ostream OS(Buf); |
| 1147 | size_t Pos = 0; |
| 1148 | while (Pos < AsmString.size()) { |
| 1149 | size_t DollarStart = AsmString.find('$', Pos); |
| 1150 | if (DollarStart == std::string::npos) |
| 1151 | DollarStart = AsmString.size(); |
| 1152 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1153 | if (DollarEnd == std::string::npos) |
| 1154 | DollarEnd = AsmString.size(); |
| 1155 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1156 | Pos = DollarEnd; |
| 1157 | size_t NumDollars = DollarEnd - DollarStart; |
| 1158 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1159 | // We have an operand reference. |
| 1160 | size_t DigitStart = Pos; |
| 1161 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1162 | if (DigitEnd == std::string::npos) |
| 1163 | DigitEnd = AsmString.size(); |
| 1164 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1165 | unsigned OperandIndex; |
| 1166 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1167 | if (OperandIndex >= FirstIn) |
| 1168 | OperandIndex += NumNewOuts; |
| 1169 | OS << OperandIndex; |
| 1170 | } else { |
| 1171 | OS << OperandStr; |
| 1172 | } |
| 1173 | Pos = DigitEnd; |
| 1174 | } |
| 1175 | } |
| 1176 | AsmString = std::move(OS.str()); |
| 1177 | } |
| 1178 | |
| 1179 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1180 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1181 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1182 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1183 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1184 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1185 | unsigned NumOutputs) const { |
| 1186 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1187 | |
| 1188 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1189 | // larger. |
| 1190 | if (!Constraints.empty()) |
| 1191 | Constraints += ','; |
| 1192 | if (RetWidth <= 32) { |
| 1193 | Constraints += "={eax}"; |
| 1194 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1195 | } else { |
| 1196 | // Use the 'A' constraint for EAX:EDX. |
| 1197 | Constraints += "=A"; |
| 1198 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1199 | } |
| 1200 | |
| 1201 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1202 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1203 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1204 | |
| 1205 | // Coerce the integer by bitcasting the return slot pointer. |
| 1206 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1207 | CoerceTy->getPointerTo())); |
| 1208 | ResultRegDests.push_back(ReturnSlot); |
| 1209 | |
| 1210 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1211 | } |
| 1212 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1213 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1214 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1215 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1216 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1217 | uint64_t Size = Context.getTypeSize(Ty); |
| 1218 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1219 | // For i386, type must be register sized. |
| 1220 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1221 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1222 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1223 | |
| 1224 | if (Ty->isVectorType()) { |
| 1225 | // 64- and 128- bit vectors inside structures are not returned in |
| 1226 | // registers. |
| 1227 | if (Size == 64 || Size == 128) |
| 1228 | return false; |
| 1229 | |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1233 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1234 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1235 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1236 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1237 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1238 | return true; |
| 1239 | |
| 1240 | // Arrays are treated like records. |
| 1241 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1242 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1245 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1246 | if (!RT) return false; |
| 1247 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1248 | // FIXME: Traverse bases here too. |
| 1249 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1250 | // Structure types are passed in register if all fields would be |
| 1251 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1252 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1253 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1254 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1255 | continue; |
| 1256 | |
| 1257 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1258 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1259 | return false; |
| 1260 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1261 | return true; |
| 1262 | } |
| 1263 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1264 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1265 | // Treat complex types as the element type. |
| 1266 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1267 | Ty = CTy->getElementType(); |
| 1268 | |
| 1269 | // Check for a type which we know has a simple scalar argument-passing |
| 1270 | // convention without any padding. (We're specifically looking for 32 |
| 1271 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1272 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1273 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1274 | return false; |
| 1275 | |
| 1276 | uint64_t Size = Context.getTypeSize(Ty); |
| 1277 | return Size == 32 || Size == 64; |
| 1278 | } |
| 1279 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1280 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1281 | uint64_t &Size) { |
| 1282 | for (const auto *FD : RD->fields()) { |
| 1283 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1284 | // argument is smaller than 32-bits, expanding the struct will create |
| 1285 | // alignment padding. |
| 1286 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1287 | return false; |
| 1288 | |
| 1289 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1290 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1291 | // counts as "basic" is more complicated than what we were doing previously. |
| 1292 | if (FD->isBitField()) |
| 1293 | return false; |
| 1294 | |
| 1295 | Size += Context.getTypeSize(FD->getType()); |
| 1296 | } |
| 1297 | return true; |
| 1298 | } |
| 1299 | |
| 1300 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1301 | uint64_t &Size) { |
| 1302 | // Don't do this if there are any non-empty bases. |
| 1303 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1304 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1305 | Size)) |
| 1306 | return false; |
| 1307 | } |
| 1308 | if (!addFieldSizes(Context, RD, Size)) |
| 1309 | return false; |
| 1310 | return true; |
| 1311 | } |
| 1312 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1313 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1314 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1315 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1316 | /// optimizations. |
| 1317 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1318 | // We can only expand structure types. |
| 1319 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1320 | if (!RT) |
| 1321 | return false; |
| 1322 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1323 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1324 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1325 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1326 | // On non-Windows, we have to conservatively match our old bitcode |
| 1327 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1328 | if (!CXXRD->isCLike()) |
| 1329 | return false; |
| 1330 | } else { |
| 1331 | // Don't do this for dynamic classes. |
| 1332 | if (CXXRD->isDynamicClass()) |
| 1333 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1334 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1335 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1336 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1337 | } else { |
| 1338 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1339 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | // We can do this if there was no alignment padding. |
| 1343 | return Size == getContext().getTypeSize(Ty); |
| 1344 | } |
| 1345 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1346 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1347 | // If the return value is indirect, then the hidden argument is consuming one |
| 1348 | // integer register. |
| 1349 | if (State.FreeRegs) { |
| 1350 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1351 | if (!IsMCUABI) |
| 1352 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1353 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1354 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1357 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1358 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1359 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1360 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1361 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1362 | const Type *Base = nullptr; |
| 1363 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1364 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1365 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1366 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1367 | // The LLVM struct type for such an aggregate should lower properly. |
| 1368 | return ABIArgInfo::getDirect(); |
| 1369 | } |
| 1370 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1371 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1372 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1373 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1374 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1375 | |
| 1376 | // 128-bit vectors are a special case; they are returned in |
| 1377 | // registers and we need to make sure to pick a type the LLVM |
| 1378 | // backend will like. |
| 1379 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1380 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1381 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1382 | |
| 1383 | // Always return in register if it fits in a general purpose |
| 1384 | // register, or if it is 64 bits and has a single element. |
| 1385 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1386 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1387 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1388 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1389 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1390 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1394 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1395 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1396 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1397 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1398 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1399 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1400 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1401 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1402 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1403 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1404 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1405 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1406 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1407 | // Ignore empty structs/unions. |
| 1408 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1409 | return ABIArgInfo::getIgnore(); |
| 1410 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1411 | // Small structures which are register sized are generally returned |
| 1412 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1413 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1414 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1415 | |
| 1416 | // As a special-case, if the struct is a "single-element" struct, and |
| 1417 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1418 | // floating-point register. (MSVC does not apply this special case.) |
| 1419 | // We apply a similar transformation for pointer types to improve the |
| 1420 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1421 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1422 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1423 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1424 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1425 | |
| 1426 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1427 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1428 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1431 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1432 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1433 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1434 | // Treat an enum type as its underlying type. |
| 1435 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1436 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1437 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1438 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1439 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1442 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1443 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1444 | } |
| 1445 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1446 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1447 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1448 | if (!RT) |
| 1449 | return 0; |
| 1450 | const RecordDecl *RD = RT->getDecl(); |
| 1451 | |
| 1452 | // If this is a C++ record, check the bases first. |
| 1453 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1454 | for (const auto &I : CXXRD->bases()) |
| 1455 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1458 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1459 | QualType FT = i->getType(); |
| 1460 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1461 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1462 | return true; |
| 1463 | |
| 1464 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1465 | return true; |
| 1466 | } |
| 1467 | |
| 1468 | return false; |
| 1469 | } |
| 1470 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1471 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1472 | unsigned Align) const { |
| 1473 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1474 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1475 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1476 | return 0; // Use default alignment. |
| 1477 | |
| 1478 | // On non-Darwin, the stack type alignment is always 4. |
| 1479 | if (!IsDarwinVectorABI) { |
| 1480 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1481 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1482 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1483 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1484 | // 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] | 1485 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1486 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1487 | return 16; |
| 1488 | |
| 1489 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1492 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1493 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1494 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1495 | if (State.FreeRegs) { |
| 1496 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1497 | if (!IsMCUABI) |
| 1498 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1499 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1500 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1501 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1502 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1503 | // Compute the byval alignment. |
| 1504 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1505 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1506 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1507 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1508 | |
| 1509 | // If the stack alignment is less than the type alignment, realign the |
| 1510 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1511 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1512 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1513 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1516 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1517 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1518 | if (!T) |
| 1519 | T = Ty.getTypePtr(); |
| 1520 | |
| 1521 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1522 | BuiltinType::Kind K = BT->getKind(); |
| 1523 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1524 | return Float; |
| 1525 | } |
| 1526 | return Integer; |
| 1527 | } |
| 1528 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1529 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1530 | if (!IsSoftFloatABI) { |
| 1531 | Class C = classify(Ty); |
| 1532 | if (C == Float) |
| 1533 | return false; |
| 1534 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1535 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1536 | unsigned Size = getContext().getTypeSize(Ty); |
| 1537 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (SizeInRegs == 0) |
| 1540 | return false; |
| 1541 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1542 | if (!IsMCUABI) { |
| 1543 | if (SizeInRegs > State.FreeRegs) { |
| 1544 | State.FreeRegs = 0; |
| 1545 | return false; |
| 1546 | } |
| 1547 | } else { |
| 1548 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1549 | // earlier parameters that are passed on the stack. Also, |
| 1550 | // it does not allow passing >8-byte structs in-register, |
| 1551 | // even if there are 3 free registers available. |
| 1552 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1553 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1554 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1555 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1556 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1557 | return true; |
| 1558 | } |
| 1559 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1560 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1561 | bool &InReg, |
| 1562 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1563 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1564 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1565 | // (HFAs) have already been dealt with at this point. |
| 1566 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1567 | return false; |
| 1568 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1569 | NeedsPadding = false; |
| 1570 | InReg = !IsMCUABI; |
| 1571 | |
| 1572 | if (!updateFreeRegs(Ty, State)) |
| 1573 | return false; |
| 1574 | |
| 1575 | if (IsMCUABI) |
| 1576 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1577 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1578 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1579 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1580 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1581 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1582 | NeedsPadding = true; |
| 1583 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1584 | return false; |
| 1585 | } |
| 1586 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1587 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1590 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1591 | if (!updateFreeRegs(Ty, State)) |
| 1592 | return false; |
| 1593 | |
| 1594 | if (IsMCUABI) |
| 1595 | return false; |
| 1596 | |
| 1597 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1598 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1599 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1600 | if (getContext().getTypeSize(Ty) > 32) |
| 1601 | return false; |
| 1602 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1603 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1604 | Ty->isReferenceType()); |
| 1605 | } |
| 1606 | |
| 1607 | return true; |
| 1608 | } |
| 1609 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1610 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1611 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1612 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1613 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1614 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1615 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1616 | // Check with the C++ ABI first. |
| 1617 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1618 | if (RT) { |
| 1619 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1620 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1621 | return getIndirectResult(Ty, false, State); |
| 1622 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1623 | // The field index doesn't matter, we'll fix it up later. |
| 1624 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1625 | } |
| 1626 | } |
| 1627 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1628 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1629 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1630 | const Type *Base = nullptr; |
| 1631 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1632 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1633 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1634 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1635 | if (State.FreeSSERegs >= NumElts) { |
| 1636 | State.FreeSSERegs -= NumElts; |
| 1637 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1638 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1639 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1640 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1641 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1645 | // Structures with flexible arrays are always indirect. |
| 1646 | // FIXME: This should not be byval! |
| 1647 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1648 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1649 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1650 | // Ignore empty structs/unions on non-Windows. |
| 1651 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1652 | return ABIArgInfo::getIgnore(); |
| 1653 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1654 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1655 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1656 | bool NeedsPadding = false; |
| 1657 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1658 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1659 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1660 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1661 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1662 | if (InReg) |
| 1663 | return ABIArgInfo::getDirectInReg(Result); |
| 1664 | else |
| 1665 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1666 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1667 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1668 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1669 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1670 | // of those arguments will match the struct. This is important because the |
| 1671 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1672 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1673 | // Don't do this for the MCU if there are still free integer registers |
| 1674 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1675 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1676 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1677 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1678 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1679 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1680 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1681 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1682 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1683 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1686 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1687 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1688 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1689 | if (IsDarwinVectorABI) { |
| 1690 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1691 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1692 | (Size == 64 && VT->getNumElements() == 1)) |
| 1693 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1694 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1695 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1696 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1697 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1698 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1699 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1700 | return ABIArgInfo::getDirect(); |
| 1701 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1702 | |
| 1703 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1704 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1705 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1706 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1707 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1708 | |
| 1709 | if (Ty->isPromotableIntegerType()) { |
| 1710 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1711 | return ABIArgInfo::getExtendInReg(Ty); |
| 1712 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1713 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1714 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1715 | if (InReg) |
| 1716 | return ABIArgInfo::getDirectInReg(); |
| 1717 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1720 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1721 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1722 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1723 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1724 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1725 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1726 | // registers. |
| 1727 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1728 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1729 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1730 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1731 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1732 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1733 | // First pass do all the vector types. |
| 1734 | const Type *Base = nullptr; |
| 1735 | uint64_t NumElts = 0; |
| 1736 | const QualType& Ty = I.type; |
| 1737 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1738 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1739 | if (State.FreeSSERegs >= NumElts) { |
| 1740 | State.FreeSSERegs -= NumElts; |
| 1741 | I.info = ABIArgInfo::getDirect(); |
| 1742 | } else { |
| 1743 | I.info = classifyArgumentType(Ty, State); |
| 1744 | } |
| 1745 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1746 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1747 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1748 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1749 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1750 | // Second pass, do the rest! |
| 1751 | const Type *Base = nullptr; |
| 1752 | uint64_t NumElts = 0; |
| 1753 | const QualType& Ty = I.type; |
| 1754 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1755 | |
| 1756 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1757 | // Assign true HVAs (non vector/native FP types). |
| 1758 | if (State.FreeSSERegs >= NumElts) { |
| 1759 | State.FreeSSERegs -= NumElts; |
| 1760 | I.info = getDirectX86Hva(); |
| 1761 | } else { |
| 1762 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1763 | } |
| 1764 | } else if (!IsHva) { |
| 1765 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1766 | I.info = classifyArgumentType(Ty, State); |
| 1767 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1768 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1769 | } |
| 1770 | } |
| 1771 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1772 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1773 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1774 | if (IsMCUABI) |
| 1775 | State.FreeRegs = 3; |
| 1776 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1777 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1778 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1779 | State.FreeRegs = 2; |
| 1780 | State.FreeSSERegs = 6; |
| 1781 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1782 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1783 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1784 | State.FreeRegs = 5; |
| 1785 | State.FreeSSERegs = 8; |
| 1786 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1787 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1788 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 1789 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1790 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1791 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1792 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1793 | // return value was sret and put it in a register ourselves if appropriate. |
| 1794 | if (State.FreeRegs) { |
| 1795 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1796 | if (!IsMCUABI) |
| 1797 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1800 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1801 | // The chain argument effectively gives us another free register. |
| 1802 | if (FI.isChainCall()) |
| 1803 | ++State.FreeRegs; |
| 1804 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1805 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1806 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1807 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1808 | } else { |
| 1809 | // If not vectorcall, revert to normal behavior. |
| 1810 | for (auto &I : FI.arguments()) { |
| 1811 | I.info = classifyArgumentType(I.type, State); |
| 1812 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1813 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1817 | // all the memory arguments to use inalloca. |
| 1818 | if (UsedInAlloca) |
| 1819 | rewriteWithInAlloca(FI); |
| 1820 | } |
| 1821 | |
| 1822 | void |
| 1823 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1824 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1825 | QualType Type) const { |
| 1826 | // Arguments are always 4-byte-aligned. |
| 1827 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1828 | |
| 1829 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1830 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1831 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1832 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1833 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1834 | // Insert padding bytes to respect alignment. |
| 1835 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1836 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1837 | if (StackOffset != FieldEnd) { |
| 1838 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1839 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1840 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1841 | FrameFields.push_back(Ty); |
| 1842 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1845 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1846 | // Leave ignored and inreg arguments alone. |
| 1847 | switch (Info.getKind()) { |
| 1848 | case ABIArgInfo::InAlloca: |
| 1849 | return true; |
| 1850 | case ABIArgInfo::Indirect: |
| 1851 | assert(Info.getIndirectByVal()); |
| 1852 | return true; |
| 1853 | case ABIArgInfo::Ignore: |
| 1854 | return false; |
| 1855 | case ABIArgInfo::Direct: |
| 1856 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1857 | if (Info.getInReg()) |
| 1858 | return false; |
| 1859 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1860 | case ABIArgInfo::Expand: |
| 1861 | case ABIArgInfo::CoerceAndExpand: |
| 1862 | // These are aggregate types which are never passed in registers when |
| 1863 | // inalloca is involved. |
| 1864 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1865 | } |
| 1866 | llvm_unreachable("invalid enum"); |
| 1867 | } |
| 1868 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1869 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1870 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1871 | |
| 1872 | // Build a packed struct type for all of the arguments in memory. |
| 1873 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1874 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1875 | // The stack alignment is always 4. |
| 1876 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1877 | |
| 1878 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1879 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1880 | |
| 1881 | // Put 'this' into the struct before 'sret', if necessary. |
| 1882 | bool IsThisCall = |
| 1883 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1884 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1885 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1886 | isArgInAlloca(I->info)) { |
| 1887 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1888 | ++I; |
| 1889 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1890 | |
| 1891 | // 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] | 1892 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1893 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1894 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1895 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1896 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1900 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1901 | ++I; |
| 1902 | |
| 1903 | // Put arguments passed in memory into the struct. |
| 1904 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1905 | if (isArgInAlloca(I->info)) |
| 1906 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1910 | /*isPacked=*/true), |
| 1911 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1914 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1915 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1916 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1917 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1918 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1919 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1920 | // |
| 1921 | // Just messing with TypeInfo like this works because we never pass |
| 1922 | // anything indirectly. |
| 1923 | TypeInfo.second = CharUnits::fromQuantity( |
| 1924 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1925 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1926 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1927 | TypeInfo, CharUnits::fromQuantity(4), |
| 1928 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1931 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1932 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1933 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1934 | |
| 1935 | switch (Opts.getStructReturnConvention()) { |
| 1936 | case CodeGenOptions::SRCK_Default: |
| 1937 | break; |
| 1938 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1939 | return false; |
| 1940 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1941 | return true; |
| 1942 | } |
| 1943 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1944 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1945 | return true; |
| 1946 | |
| 1947 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1948 | case llvm::Triple::DragonFly: |
| 1949 | case llvm::Triple::FreeBSD: |
| 1950 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1951 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1952 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1953 | default: |
| 1954 | return false; |
| 1955 | } |
| 1956 | } |
| 1957 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1958 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1959 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1960 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1961 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1962 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1963 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1964 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 1965 | Fn->addFnAttr("stackrealign"); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1966 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1967 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1968 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1969 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1970 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1971 | } |
| 1972 | } |
| 1973 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1974 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1975 | CodeGen::CodeGenFunction &CGF, |
| 1976 | llvm::Value *Address) const { |
| 1977 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1979 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1980 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1981 | // 0-7 are the eight integer registers; the order is different |
| 1982 | // on Darwin (for EH), but the range is the same. |
| 1983 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1984 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1985 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1986 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1987 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1988 | // These have size 16, which is sizeof(long double) on |
| 1989 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1990 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1991 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1992 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1993 | } else { |
| 1994 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1995 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1996 | Builder.CreateAlignedStore( |
| 1997 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1998 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1999 | |
| 2000 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 2001 | // These have size 12, which is sizeof(long double) on |
| 2002 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2003 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2004 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 2005 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2006 | |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2010 | //===----------------------------------------------------------------------===// |
| 2011 | // X86-64 ABI Implementation |
| 2012 | //===----------------------------------------------------------------------===// |
| 2013 | |
| 2014 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2015 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2016 | /// The AVX ABI level for X86 targets. |
| 2017 | enum class X86AVXABILevel { |
| 2018 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2019 | AVX, |
| 2020 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2021 | }; |
| 2022 | |
| 2023 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2024 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2025 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2026 | case X86AVXABILevel::AVX512: |
| 2027 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2028 | case X86AVXABILevel::AVX: |
| 2029 | return 256; |
| 2030 | case X86AVXABILevel::None: |
| 2031 | return 128; |
| 2032 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2033 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2036 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2037 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2038 | enum Class { |
| 2039 | Integer = 0, |
| 2040 | SSE, |
| 2041 | SSEUp, |
| 2042 | X87, |
| 2043 | X87Up, |
| 2044 | ComplexX87, |
| 2045 | NoClass, |
| 2046 | Memory |
| 2047 | }; |
| 2048 | |
| 2049 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2050 | /// |
| 2051 | /// Merge an accumulating classification \arg Accum with a field |
| 2052 | /// classification \arg Field. |
| 2053 | /// |
| 2054 | /// \param Accum - The accumulating classification. This should |
| 2055 | /// always be either NoClass or the result of a previous merge |
| 2056 | /// call. In addition, this should never be Memory (the caller |
| 2057 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2058 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2059 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2060 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2061 | /// |
| 2062 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2063 | /// final MEMORY or SSE classes when necessary. |
| 2064 | /// |
| 2065 | /// \param AggregateSize - The size of the current aggregate in |
| 2066 | /// the classification process. |
| 2067 | /// |
| 2068 | /// \param Lo - The classification for the parts of the type |
| 2069 | /// residing in the low word of the containing object. |
| 2070 | /// |
| 2071 | /// \param Hi - The classification for the parts of the type |
| 2072 | /// residing in the higher words of the containing object. |
| 2073 | /// |
| 2074 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2075 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2076 | /// classify - Determine the x86_64 register classes in which the |
| 2077 | /// given type T should be passed. |
| 2078 | /// |
| 2079 | /// \param Lo - The classification for the parts of the type |
| 2080 | /// residing in the low word of the containing object. |
| 2081 | /// |
| 2082 | /// \param Hi - The classification for the parts of the type |
| 2083 | /// residing in the high word of the containing object. |
| 2084 | /// |
| 2085 | /// \param OffsetBase - The bit offset of this type in the |
| 2086 | /// containing object. Some parameters are classified different |
| 2087 | /// depending on whether they straddle an eightbyte boundary. |
| 2088 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2089 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2090 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2091 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2092 | /// If a word is unused its result will be NoClass; if a type should |
| 2093 | /// be passed in Memory then at least the classification of \arg Lo |
| 2094 | /// will be Memory. |
| 2095 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2096 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2097 | /// |
| 2098 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2099 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2100 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2101 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2102 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2103 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2104 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2105 | unsigned IROffset, QualType SourceTy, |
| 2106 | unsigned SourceOffset) const; |
| 2107 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2108 | unsigned IROffset, QualType SourceTy, |
| 2109 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2110 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2111 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2112 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2113 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2114 | |
| 2115 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2116 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2117 | /// |
| 2118 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2119 | /// available. |
| 2120 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2122 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2123 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2124 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2125 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2126 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2127 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2128 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2129 | unsigned &NeededSSE) const; |
| 2130 | |
| 2131 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2132 | unsigned &NeededSSE) const; |
| 2133 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2134 | bool IsIllegalVectorType(QualType Ty) const; |
| 2135 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2136 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2137 | /// unfortunately in ways that were not always consistent with |
| 2138 | /// certain previous compilers. In particular, platforms which |
| 2139 | /// required strict binary compatibility with older versions of GCC |
| 2140 | /// may need to exempt themselves. |
| 2141 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2142 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2145 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2146 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2147 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2148 | // Clang <= 3.8 did not do this. |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 2149 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2150 | LangOptions::ClangABI::Ver3_8) |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2151 | return false; |
| 2152 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2153 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2154 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2155 | return false; |
| 2156 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2157 | return false; |
| 2158 | return true; |
| 2159 | } |
| 2160 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2161 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2162 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2163 | // 64-bit hardware. |
| 2164 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2165 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2166 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2167 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2168 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2169 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2170 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2171 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2172 | bool isPassedUsingAVXType(QualType type) const { |
| 2173 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2174 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2175 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2176 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2177 | if (info.isDirect()) { |
| 2178 | llvm::Type *ty = info.getCoerceToType(); |
| 2179 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2180 | return (vectorTy->getBitWidth() > 128); |
| 2181 | } |
| 2182 | return false; |
| 2183 | } |
| 2184 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2185 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2186 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2187 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2188 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2189 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2190 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2191 | |
| 2192 | bool has64BitPointers() const { |
| 2193 | return Has64BitPointers; |
| 2194 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2195 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2196 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2197 | bool asReturnValue) const override { |
| 2198 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2199 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2200 | bool isSwiftErrorInRegister() const override { |
| 2201 | return true; |
| 2202 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2203 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2204 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2205 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2206 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2207 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2208 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2209 | : SwiftABIInfo(CGT), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2210 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2211 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2212 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2213 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2214 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2215 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2216 | |
| 2217 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2218 | // FIXME: Assumes vectorcall is in use. |
| 2219 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2220 | } |
| 2221 | |
| 2222 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2223 | uint64_t NumMembers) const override { |
| 2224 | // FIXME: Assumes vectorcall is in use. |
| 2225 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2226 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2227 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2228 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2229 | bool asReturnValue) const override { |
| 2230 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2231 | } |
| 2232 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2233 | bool isSwiftErrorInRegister() const override { |
| 2234 | return true; |
| 2235 | } |
| 2236 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2237 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2238 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2239 | bool IsVectorCall, bool IsRegCall) const; |
| 2240 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2241 | const ABIArgInfo ¤t) const; |
| 2242 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2243 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2244 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2245 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2246 | }; |
| 2247 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2248 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2249 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2250 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2251 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2252 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2253 | const X86_64ABIInfo &getABIInfo() const { |
| 2254 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2255 | } |
| 2256 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2257 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2258 | return 7; |
| 2259 | } |
| 2260 | |
| 2261 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2262 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2263 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2264 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2265 | // 0-15 are the 16 integer registers. |
| 2266 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2267 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2268 | return false; |
| 2269 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2270 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2271 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2272 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2273 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2274 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2275 | } |
| 2276 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2277 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2278 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2279 | // The default CC on x86-64 sets %al to the number of SSA |
| 2280 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2281 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2282 | // that when AVX types are involved: the ABI explicitly states it is |
| 2283 | // undefined, and it doesn't work in practice because of how the ABI |
| 2284 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2285 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2286 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2287 | for (CallArgList::const_iterator |
| 2288 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2289 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2290 | HasAVXType = true; |
| 2291 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2292 | } |
| 2293 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2294 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2295 | if (!HasAVXType) |
| 2296 | return true; |
| 2297 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2298 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2299 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2302 | llvm::Constant * |
| 2303 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2304 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2305 | (0x06 << 8) | // .+0x08 |
| 2306 | ('v' << 16) | |
| 2307 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2308 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2309 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2310 | |
| 2311 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2312 | CodeGen::CodeGenModule &CGM) const override { |
| 2313 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2314 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2315 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2316 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2317 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2318 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2319 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2320 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2321 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2322 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2323 | } |
| 2324 | } |
| 2325 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2326 | }; |
| 2327 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2328 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 2329 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2330 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2331 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2332 | |
| 2333 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 2334 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2335 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 2336 | // If the argument contains a space, enclose it in quotes. |
| 2337 | if (Lib.find(" ") != StringRef::npos) |
| 2338 | Opt += "\"" + Lib.str() + "\""; |
| 2339 | else |
| 2340 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 2341 | } |
| 2342 | }; |
| 2343 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2344 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2345 | // If the argument does not end in .lib, automatically add the suffix. |
| 2346 | // If the argument contains a space, enclose it in quotes. |
| 2347 | // This matches the behavior of MSVC. |
| 2348 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2349 | std::string ArgStr = Quote ? "\"" : ""; |
| 2350 | ArgStr += Lib; |
Martin Storsjo | 3cd67c9 | 2018-10-10 09:01:00 +0000 | [diff] [blame] | 2351 | if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2352 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2353 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2354 | return ArgStr; |
| 2355 | } |
| 2356 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2357 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2358 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2359 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2360 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2361 | unsigned NumRegisterParameters) |
| 2362 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2363 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2364 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2365 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2366 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2367 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2368 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2369 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2370 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2371 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2372 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2373 | |
| 2374 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2375 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2376 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2377 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2378 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2379 | }; |
| 2380 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2381 | static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2382 | CodeGen::CodeGenModule &CGM) { |
| 2383 | if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2384 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2385 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2386 | Fn->addFnAttr("stack-probe-size", |
| 2387 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2388 | if (CGM.getCodeGenOpts().NoStackArgProbe) |
| 2389 | Fn->addFnAttr("no-stack-arg-probe"); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2393 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2394 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2395 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2396 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2397 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2398 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2401 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2402 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2403 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2404 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2405 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2406 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2407 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2408 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2409 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2410 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2411 | return 7; |
| 2412 | } |
| 2413 | |
| 2414 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2415 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2416 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2417 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2418 | // 0-15 are the 16 integer registers. |
| 2419 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2420 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2421 | return false; |
| 2422 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2423 | |
| 2424 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2425 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2426 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2427 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2428 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2429 | |
| 2430 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2431 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2432 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2433 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2434 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2435 | }; |
| 2436 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2437 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2438 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2439 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2440 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2441 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2442 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2443 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2444 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2445 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2446 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2447 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2448 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2449 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2450 | } |
| 2451 | } |
| 2452 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2453 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2454 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2455 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2456 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2457 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2458 | Class &Hi) const { |
| 2459 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2460 | // |
| 2461 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2462 | // memory. |
| 2463 | // |
| 2464 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2465 | // memory. |
| 2466 | // |
| 2467 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2468 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2469 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2470 | // ABI working for processors that don't support the __m256 type. |
| 2471 | // |
| 2472 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2473 | // |
| 2474 | // Some of these are enforced by the merging logic. Others can arise |
| 2475 | // only with unions; for example: |
| 2476 | // union { _Complex double; unsigned; } |
| 2477 | // |
| 2478 | // Note that clauses (b) and (c) were added in 0.98. |
| 2479 | // |
| 2480 | if (Hi == Memory) |
| 2481 | Lo = Memory; |
| 2482 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2483 | Lo = Memory; |
| 2484 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2485 | Lo = Memory; |
| 2486 | if (Hi == SSEUp && Lo != SSE) |
| 2487 | Hi = SSE; |
| 2488 | } |
| 2489 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2490 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2491 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2492 | // classified recursively so that always two fields are |
| 2493 | // considered. The resulting class is calculated according to |
| 2494 | // the classes of the fields in the eightbyte: |
| 2495 | // |
| 2496 | // (a) If both classes are equal, this is the resulting class. |
| 2497 | // |
| 2498 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2499 | // the other class. |
| 2500 | // |
| 2501 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2502 | // class. |
| 2503 | // |
| 2504 | // (d) If one of the classes is INTEGER, the result is the |
| 2505 | // INTEGER. |
| 2506 | // |
| 2507 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2508 | // MEMORY is used as class. |
| 2509 | // |
| 2510 | // (f) Otherwise class SSE is used. |
| 2511 | |
| 2512 | // Accum should never be memory (we should have returned) or |
| 2513 | // ComplexX87 (because this cannot be passed in a structure). |
| 2514 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2515 | "Invalid accumulated classification during merge."); |
| 2516 | if (Accum == Field || Field == NoClass) |
| 2517 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2518 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2519 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2520 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2521 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2522 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2523 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2524 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2525 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2526 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2527 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2530 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2531 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2532 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2533 | // Class pairs with appropriate constructor methods for the various |
| 2534 | // situations. |
| 2535 | |
| 2536 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2537 | // shouldn't be passed in registers for example, so there is no chance they |
| 2538 | // can straddle an eightbyte. Verify & simplify. |
| 2539 | |
| 2540 | Lo = Hi = NoClass; |
| 2541 | |
| 2542 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2543 | Current = Memory; |
| 2544 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2545 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2546 | BuiltinType::Kind k = BT->getKind(); |
| 2547 | |
| 2548 | if (k == BuiltinType::Void) { |
| 2549 | Current = NoClass; |
| 2550 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2551 | Lo = Integer; |
| 2552 | Hi = Integer; |
| 2553 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2554 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2555 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2556 | Current = SSE; |
| 2557 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2558 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2559 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2560 | Lo = SSE; |
| 2561 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2562 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2563 | Lo = X87; |
| 2564 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2565 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2566 | Current = SSE; |
| 2567 | } else |
| 2568 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2569 | } |
| 2570 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2571 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2575 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2576 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2577 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2578 | return; |
| 2579 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2580 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2581 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2582 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2583 | return; |
| 2584 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2585 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2586 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2587 | if (Ty->isMemberFunctionPointerType()) { |
| 2588 | if (Has64BitPointers) { |
| 2589 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2590 | // Lo and Hi now. |
| 2591 | Lo = Hi = Integer; |
| 2592 | } else { |
| 2593 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2594 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2595 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2596 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2597 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2598 | Lo = Hi = Integer; |
| 2599 | } else { |
| 2600 | Current = Integer; |
| 2601 | } |
| 2602 | } |
| 2603 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2604 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2605 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2608 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2609 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2610 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2611 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2612 | // gcc passes the following as integer: |
| 2613 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2614 | // 2 bytes - <2 x char>, <1 x short> |
| 2615 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2616 | Current = Integer; |
| 2617 | |
| 2618 | // If this type crosses an eightbyte boundary, it should be |
| 2619 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2620 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2621 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2622 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2623 | Hi = Lo; |
| 2624 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2625 | QualType ElementType = VT->getElementType(); |
| 2626 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2627 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2628 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2629 | return; |
| 2630 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2631 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2632 | // pass them as integer. For platforms where clang is the de facto |
| 2633 | // platform compiler, we must continue to use integer. |
| 2634 | if (!classifyIntegerMMXAsSSE() && |
| 2635 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2636 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2637 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2638 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2639 | Current = Integer; |
| 2640 | else |
| 2641 | Current = SSE; |
| 2642 | |
| 2643 | // If this type crosses an eightbyte boundary, it should be |
| 2644 | // split. |
| 2645 | if (OffsetBase && OffsetBase != 64) |
| 2646 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2647 | } else if (Size == 128 || |
| 2648 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2649 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2650 | // least significant one belongs to class SSE and all the others to class |
| 2651 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2652 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2653 | // This design isn't correct for 256-bits, but since there're no cases |
| 2654 | // where the upper parts would need to be inspected, avoid adding |
| 2655 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2656 | // |
| 2657 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2658 | // registers if they are "named", i.e. not part of the "..." of a |
| 2659 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2660 | // |
| 2661 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2662 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2663 | Lo = SSE; |
| 2664 | Hi = SSEUp; |
| 2665 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2666 | return; |
| 2667 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2668 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2669 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2670 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2671 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2672 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2673 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2674 | if (Size <= 64) |
| 2675 | Current = Integer; |
| 2676 | else if (Size <= 128) |
| 2677 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2678 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2679 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2680 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2681 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2682 | } else if (ET == getContext().LongDoubleTy) { |
| 2683 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2684 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2685 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2686 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2687 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2688 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2689 | Lo = Hi = SSE; |
| 2690 | else |
| 2691 | llvm_unreachable("unexpected long double representation!"); |
| 2692 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2693 | |
| 2694 | // If this complex type crosses an eightbyte boundary then it |
| 2695 | // should be split. |
| 2696 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2697 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2698 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2699 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2700 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2701 | return; |
| 2702 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2703 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2704 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2705 | // Arrays are treated like structures. |
| 2706 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2707 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2708 | |
| 2709 | // 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] | 2710 | // than eight eightbytes, ..., it has class MEMORY. |
| 2711 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2712 | return; |
| 2713 | |
| 2714 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2715 | // fields, it has class MEMORY. |
| 2716 | // |
| 2717 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2718 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2719 | return; |
| 2720 | |
| 2721 | // Otherwise implement simplified merge. We could be smarter about |
| 2722 | // this, but it isn't worth it and would be harder to verify. |
| 2723 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2724 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2725 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2726 | |
| 2727 | // The only case a 256-bit wide vector could be used is when the array |
| 2728 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2729 | // 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] | 2730 | // |
| 2731 | if (Size > 128 && |
| 2732 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2733 | return; |
| 2734 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2735 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2736 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2737 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2738 | Lo = merge(Lo, FieldLo); |
| 2739 | Hi = merge(Hi, FieldHi); |
| 2740 | if (Lo == Memory || Hi == Memory) |
| 2741 | break; |
| 2742 | } |
| 2743 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2744 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2745 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2746 | return; |
| 2747 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2748 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2749 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2750 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2751 | |
| 2752 | // 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] | 2753 | // than eight eightbytes, ..., it has class MEMORY. |
| 2754 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2755 | return; |
| 2756 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2757 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2758 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2759 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2760 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2761 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2762 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2763 | const RecordDecl *RD = RT->getDecl(); |
| 2764 | |
| 2765 | // Assume variable sized types are passed in memory. |
| 2766 | if (RD->hasFlexibleArrayMember()) |
| 2767 | return; |
| 2768 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2769 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2770 | |
| 2771 | // Reset Lo class, this will be recomputed. |
| 2772 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2773 | |
| 2774 | // If this is a C++ record, classify the bases first. |
| 2775 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2776 | for (const auto &I : CXXRD->bases()) { |
| 2777 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2778 | "Unexpected base class!"); |
| 2779 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2780 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2781 | |
| 2782 | // Classify this field. |
| 2783 | // |
| 2784 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2785 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2786 | // initialized to class NO_CLASS. |
| 2787 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2788 | uint64_t Offset = |
| 2789 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2790 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2791 | Lo = merge(Lo, FieldLo); |
| 2792 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2793 | if (Lo == Memory || Hi == Memory) { |
| 2794 | postMerge(Size, Lo, Hi); |
| 2795 | return; |
| 2796 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2801 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2802 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2803 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2804 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2805 | bool BitField = i->isBitField(); |
| 2806 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2807 | // Ignore padding bit-fields. |
| 2808 | if (BitField && i->isUnnamedBitfield()) |
| 2809 | continue; |
| 2810 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2811 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2812 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2813 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2814 | // The only case a 256-bit wide vector could be used is when the struct |
| 2815 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2816 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2817 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2818 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2819 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2820 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2821 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2822 | return; |
| 2823 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2824 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2825 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2826 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2827 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2828 | return; |
| 2829 | } |
| 2830 | |
| 2831 | // Classify this field. |
| 2832 | // |
| 2833 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2834 | // exceeds a single eightbyte, each is classified |
| 2835 | // separately. Each eightbyte gets initialized to class |
| 2836 | // NO_CLASS. |
| 2837 | Class FieldLo, FieldHi; |
| 2838 | |
| 2839 | // Bit-fields require special handling, they do not force the |
| 2840 | // structure to be passed in memory even if unaligned, and |
| 2841 | // therefore they can straddle an eightbyte. |
| 2842 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2843 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2844 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2845 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2846 | |
| 2847 | uint64_t EB_Lo = Offset / 64; |
| 2848 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2849 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2850 | if (EB_Lo) { |
| 2851 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2852 | FieldLo = NoClass; |
| 2853 | FieldHi = Integer; |
| 2854 | } else { |
| 2855 | FieldLo = Integer; |
| 2856 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2857 | } |
| 2858 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2859 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2860 | Lo = merge(Lo, FieldLo); |
| 2861 | Hi = merge(Hi, FieldHi); |
| 2862 | if (Lo == Memory || Hi == Memory) |
| 2863 | break; |
| 2864 | } |
| 2865 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2866 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2870 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2871 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2872 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2873 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2874 | // Treat an enum type as its underlying type. |
| 2875 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2876 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2877 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2878 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2879 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2882 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2885 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2886 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2887 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2888 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2889 | if (Size <= 64 || Size > LargestVector) |
| 2890 | return true; |
| 2891 | } |
| 2892 | |
| 2893 | return false; |
| 2894 | } |
| 2895 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2896 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2897 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2898 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2899 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2900 | // |
| 2901 | // This assumption is optimistic, as there could be free registers available |
| 2902 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2903 | // the argument in the free register. This does not seem to happen currently, |
| 2904 | // but this code would be much safer if we could mark the argument with |
| 2905 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2906 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2907 | // Treat an enum type as its underlying type. |
| 2908 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2909 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2910 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2911 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2912 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2913 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2914 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2915 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2916 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2917 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2918 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2919 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2920 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2921 | |
| 2922 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2923 | // is important for good codegen. |
| 2924 | // |
| 2925 | // We do this by coercing the value into a scalar type which the backend can |
| 2926 | // handle naturally (i.e., without using byval). |
| 2927 | // |
| 2928 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2929 | // free integer registers. Doing this when there are free integer registers |
| 2930 | // would require more care, as we would have to ensure that the coerced value |
| 2931 | // did not claim the unused register. That would require either reording the |
| 2932 | // arguments to the function (so that any subsequent inreg values came first), |
| 2933 | // or only doing this optimization when there were no following arguments that |
| 2934 | // might be inreg. |
| 2935 | // |
| 2936 | // We currently expect it to be rare (particularly in well written code) for |
| 2937 | // arguments to be passed on the stack when there are still free integer |
| 2938 | // registers available (this would typically imply large structs being passed |
| 2939 | // by value), so this seems like a fair tradeoff for now. |
| 2940 | // |
| 2941 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2942 | // attributes. See PR12193. |
| 2943 | if (freeIntRegs == 0) { |
| 2944 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2945 | |
| 2946 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2947 | // type, which will end up on the stack (with alignment 8). |
| 2948 | if (Align == 8 && Size <= 64) |
| 2949 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2950 | Size)); |
| 2951 | } |
| 2952 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2953 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2956 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2957 | /// 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] | 2958 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2959 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2960 | // vectors; strip them off if present. |
| 2961 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2962 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2963 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2964 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2965 | if (isa<llvm::VectorType>(IRType) || |
| 2966 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2967 | return IRType; |
| 2968 | |
| 2969 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2970 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2971 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2972 | |
| 2973 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2974 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2975 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2978 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2979 | /// is known to either be off the end of the specified type or being in |
| 2980 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2981 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2982 | /// classification that put one of the two halves in the INTEGER class. |
| 2983 | /// |
| 2984 | /// It is conservatively correct to return false. |
| 2985 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2986 | unsigned EndBit, ASTContext &Context) { |
| 2987 | // If the bytes being queried are off the end of the type, there is no user |
| 2988 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2989 | // types that don't contain interesting padding. |
| 2990 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2991 | if (TySize <= StartBit) |
| 2992 | return true; |
| 2993 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2994 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2995 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2996 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2997 | |
| 2998 | // Check each element to see if the element overlaps with the queried range. |
| 2999 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3000 | // If the element is after the span we care about, then we're done.. |
| 3001 | unsigned EltOffset = i*EltSize; |
| 3002 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3003 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3004 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 3005 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3006 | EndBit-EltOffset, Context)) |
| 3007 | return false; |
| 3008 | } |
| 3009 | // If it overlaps no elements, then it is safe to process as padding. |
| 3010 | return true; |
| 3011 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3012 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3013 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3014 | const RecordDecl *RD = RT->getDecl(); |
| 3015 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3016 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3017 | // If this is a C++ record, check the bases first. |
| 3018 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3019 | for (const auto &I : CXXRD->bases()) { |
| 3020 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3021 | "Unexpected base class!"); |
| 3022 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3023 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3024 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3025 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3026 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3027 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3028 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3029 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3030 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3031 | EndBit-BaseOffset, Context)) |
| 3032 | return false; |
| 3033 | } |
| 3034 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3035 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3036 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3037 | // this could be sped up a lot by being smarter about queried fields, |
| 3038 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3039 | // much. |
| 3040 | unsigned idx = 0; |
| 3041 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3042 | i != e; ++i, ++idx) { |
| 3043 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
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 we found a field after the region we care about, then we're done. |
| 3046 | if (FieldOffset >= EndBit) break; |
| 3047 | |
| 3048 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3049 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3050 | Context)) |
| 3051 | return false; |
| 3052 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3053 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3054 | // If nothing in this record overlapped the area of interest, then we're |
| 3055 | // clean. |
| 3056 | return true; |
| 3057 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3058 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3059 | return false; |
| 3060 | } |
| 3061 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3062 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3063 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3064 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3065 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3066 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3067 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3068 | // Base case if we find a float. |
| 3069 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3070 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3072 | // 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] | 3073 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3074 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3075 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3076 | IROffset -= SL->getElementOffset(Elt); |
| 3077 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3078 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3079 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3080 | // 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] | 3081 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3082 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3083 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3084 | IROffset -= IROffset/EltSize*EltSize; |
| 3085 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3086 | } |
| 3087 | |
| 3088 | return false; |
| 3089 | } |
| 3090 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3091 | |
| 3092 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3093 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3094 | llvm::Type *X86_64ABIInfo:: |
| 3095 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3096 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3097 | // 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] | 3098 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3099 | // structs that contain 3 floats. |
| 3100 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3101 | SourceOffset*8+64, getContext())) |
| 3102 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3103 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3104 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3105 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3106 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3107 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3108 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3109 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3111 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3112 | } |
| 3113 | |
| 3114 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3115 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3116 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3117 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3118 | /// 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] | 3119 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3120 | /// etc). |
| 3121 | /// |
| 3122 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3123 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3124 | /// the 8-byte value references. PrefType may be null. |
| 3125 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3126 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3127 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3128 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3129 | llvm::Type *X86_64ABIInfo:: |
| 3130 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3131 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3132 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3133 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3134 | if (IROffset == 0) { |
| 3135 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3136 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3137 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3138 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3139 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3140 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3141 | // goodness in the source type is just tail padding. This is allowed to |
| 3142 | // kick in for struct {double,int} on the int, but not on |
| 3143 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3144 | // have to do this analysis on the source type because we can't depend on |
| 3145 | // unions being lowered a specific way etc. |
| 3146 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3147 | IRType->isIntegerTy(32) || |
| 3148 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3149 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3150 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3151 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3152 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3153 | SourceOffset*8+64, getContext())) |
| 3154 | return IRType; |
| 3155 | } |
| 3156 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3157 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3158 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3159 | // 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] | 3160 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3161 | if (IROffset < SL->getSizeInBytes()) { |
| 3162 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3163 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3164 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3165 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3166 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3167 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3168 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3169 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3170 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3171 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3172 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3173 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3174 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3175 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3176 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3177 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3178 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3179 | // 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] | 3180 | unsigned TySizeInBytes = |
| 3181 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3182 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3183 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3184 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3185 | // It is always safe to classify this as an integer type up to i64 that |
| 3186 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3187 | return llvm::IntegerType::get(getVMContext(), |
| 3188 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3191 | |
| 3192 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3193 | /// be used as elements of a two register pair to pass or return, return a |
| 3194 | /// first class aggregate to represent them. For example, if the low part of |
| 3195 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3196 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3197 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3198 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3199 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3200 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3201 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3202 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3203 | // the second element at offset 8. Check for this: |
| 3204 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3205 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3206 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3207 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3208 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3209 | // To handle this, we have to increase the size of the low part so that the |
| 3210 | // second element will start at an 8 byte offset. We can't increase the size |
| 3211 | // of the second element because it might make us access off the end of the |
| 3212 | // struct. |
| 3213 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3214 | // There are usually two sorts of types the ABI generation code can produce |
| 3215 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3216 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3217 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3218 | // Promote these to a larger type. |
| 3219 | if (Lo->isFloatTy()) |
| 3220 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3221 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3222 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3223 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3224 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3225 | } |
| 3226 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3227 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3228 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3229 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3230 | // Verify that the second element is at an 8-byte offset. |
| 3231 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3232 | "Invalid x86-64 argument pair!"); |
| 3233 | return Result; |
| 3234 | } |
| 3235 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3236 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3237 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3238 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3239 | // classification algorithm. |
| 3240 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3241 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3242 | |
| 3243 | // Check some invariants. |
| 3244 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3245 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3246 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3247 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3248 | switch (Lo) { |
| 3249 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3250 | if (Hi == NoClass) |
| 3251 | return ABIArgInfo::getIgnore(); |
| 3252 | // If the low part is just padding, it takes no register, leave ResType |
| 3253 | // null. |
| 3254 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3255 | "Unknown missing lo part"); |
| 3256 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3257 | |
| 3258 | case SSEUp: |
| 3259 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3260 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3261 | |
| 3262 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3263 | // hidden argument. |
| 3264 | case Memory: |
| 3265 | return getIndirectReturnResult(RetTy); |
| 3266 | |
| 3267 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3268 | // available register of the sequence %rax, %rdx is used. |
| 3269 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3270 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3271 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3272 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3273 | // so that the parameter gets the right LLVM IR attributes. |
| 3274 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3275 | // Treat an enum type as its underlying type. |
| 3276 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3277 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3278 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3279 | if (RetTy->isIntegralOrEnumerationType() && |
| 3280 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3281 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3282 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3283 | break; |
| 3284 | |
| 3285 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3286 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3287 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3288 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3289 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3290 | |
| 3291 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3292 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3293 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3294 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3295 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3296 | |
| 3297 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3298 | // part of the value is returned in %st0 and the imaginary part in |
| 3299 | // %st1. |
| 3300 | case ComplexX87: |
| 3301 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3302 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3303 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3304 | break; |
| 3305 | } |
| 3306 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3307 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3308 | switch (Hi) { |
| 3309 | // Memory was handled previously and X87 should |
| 3310 | // never occur as a hi class. |
| 3311 | case Memory: |
| 3312 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3313 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3314 | |
| 3315 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3316 | case NoClass: |
| 3317 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3318 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3319 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3320 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3321 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3322 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3323 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3324 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3325 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3326 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3327 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3328 | break; |
| 3329 | |
| 3330 | // 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] | 3331 | // is passed in the next available eightbyte chunk if the last used |
| 3332 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3333 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3334 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3335 | case SSEUp: |
| 3336 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3337 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3338 | break; |
| 3339 | |
| 3340 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3341 | // returned together with the previous X87 value in %st0. |
| 3342 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3343 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3344 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3345 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3346 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3347 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3348 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3349 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3350 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3351 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3352 | break; |
| 3353 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3354 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3355 | // 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] | 3356 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3357 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3358 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3359 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3360 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3361 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3364 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3365 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3366 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3367 | const |
| 3368 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3369 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3370 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3371 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3372 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3373 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3374 | // Check some invariants. |
| 3375 | // FIXME: Enforce these by construction. |
| 3376 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3377 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3378 | |
| 3379 | neededInt = 0; |
| 3380 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3381 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3382 | switch (Lo) { |
| 3383 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3384 | if (Hi == NoClass) |
| 3385 | return ABIArgInfo::getIgnore(); |
| 3386 | // If the low part is just padding, it takes no register, leave ResType |
| 3387 | // null. |
| 3388 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3389 | "Unknown missing lo part"); |
| 3390 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3391 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3392 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3393 | // on the stack. |
| 3394 | case Memory: |
| 3395 | |
| 3396 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3397 | // COMPLEX_X87, it is passed in memory. |
| 3398 | case X87: |
| 3399 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3400 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3401 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3402 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3403 | |
| 3404 | case SSEUp: |
| 3405 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3406 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3407 | |
| 3408 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3409 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3410 | // and %r9 is used. |
| 3411 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3412 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3413 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3414 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3415 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3416 | |
| 3417 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3418 | // so that the parameter gets the right LLVM IR attributes. |
| 3419 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3420 | // Treat an enum type as its underlying type. |
| 3421 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3422 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3423 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3424 | if (Ty->isIntegralOrEnumerationType() && |
| 3425 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3426 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3427 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3428 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3429 | break; |
| 3430 | |
| 3431 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3432 | // available SSE register is used, the registers are taken in the |
| 3433 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3434 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3435 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3436 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3437 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3438 | break; |
| 3439 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3440 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3441 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3442 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3443 | switch (Hi) { |
| 3444 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3445 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3446 | // which is passed in memory. |
| 3447 | case Memory: |
| 3448 | case X87: |
| 3449 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3450 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3451 | |
| 3452 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3453 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3454 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3455 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3456 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3457 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3458 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3459 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3460 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3461 | break; |
| 3462 | |
| 3463 | // X87Up generally doesn't occur here (long double is passed in |
| 3464 | // memory), except in situations involving unions. |
| 3465 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3466 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3467 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3468 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3469 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3470 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3471 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3472 | ++neededSSE; |
| 3473 | break; |
| 3474 | |
| 3475 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3476 | // 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] | 3477 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3478 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3479 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3480 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3481 | break; |
| 3482 | } |
| 3483 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3484 | // If a high part was specified, merge it together with the low part. It is |
| 3485 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3486 | // first class struct aggregate with the high and low part: {low, high} |
| 3487 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3488 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3489 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3490 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3493 | ABIArgInfo |
| 3494 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3495 | unsigned &NeededSSE) const { |
| 3496 | auto RT = Ty->getAs<RecordType>(); |
| 3497 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3498 | |
| 3499 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3500 | return getIndirectReturnResult(Ty); |
| 3501 | |
| 3502 | // Sum up bases |
| 3503 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3504 | if (CXXRD->isDynamicClass()) { |
| 3505 | NeededInt = NeededSSE = 0; |
| 3506 | return getIndirectReturnResult(Ty); |
| 3507 | } |
| 3508 | |
| 3509 | for (const auto &I : CXXRD->bases()) |
| 3510 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3511 | .isIndirect()) { |
| 3512 | NeededInt = NeededSSE = 0; |
| 3513 | return getIndirectReturnResult(Ty); |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | // Sum up members |
| 3518 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3519 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3520 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3521 | .isIndirect()) { |
| 3522 | NeededInt = NeededSSE = 0; |
| 3523 | return getIndirectReturnResult(Ty); |
| 3524 | } |
| 3525 | } else { |
| 3526 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3527 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3528 | LocalNeededSSE, true) |
| 3529 | .isIndirect()) { |
| 3530 | NeededInt = NeededSSE = 0; |
| 3531 | return getIndirectReturnResult(Ty); |
| 3532 | } |
| 3533 | NeededInt += LocalNeededInt; |
| 3534 | NeededSSE += LocalNeededSSE; |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | return ABIArgInfo::getDirect(); |
| 3539 | } |
| 3540 | |
| 3541 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3542 | unsigned &NeededInt, |
| 3543 | unsigned &NeededSSE) const { |
| 3544 | |
| 3545 | NeededInt = 0; |
| 3546 | NeededSSE = 0; |
| 3547 | |
| 3548 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3549 | } |
| 3550 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3551 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3552 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3553 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3554 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3555 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3556 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3557 | if (CallingConv == llvm::CallingConv::Win64) { |
| 3558 | WinX86_64ABIInfo Win64ABIInfo(CGT); |
| 3559 | Win64ABIInfo.computeInfo(FI); |
| 3560 | return; |
| 3561 | } |
| 3562 | |
| 3563 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3564 | |
| 3565 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3566 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3567 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3568 | unsigned NeededInt, NeededSSE; |
| 3569 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 3570 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3571 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3572 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3573 | FI.getReturnInfo() = |
| 3574 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3575 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3576 | FreeIntRegs -= NeededInt; |
| 3577 | FreeSSERegs -= NeededSSE; |
| 3578 | } else { |
| 3579 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3580 | } |
| 3581 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3582 | // Complex Long Double Type is passed in Memory when Regcall |
| 3583 | // calling convention is used. |
| 3584 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3585 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3586 | getContext().LongDoubleTy) |
| 3587 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3588 | } else |
| 3589 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3590 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3591 | |
| 3592 | // If the return value is indirect, then the hidden argument is consuming one |
| 3593 | // integer register. |
| 3594 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3595 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3596 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3597 | // The chain argument effectively gives us another free register. |
| 3598 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3599 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3600 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3601 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3602 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3603 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3604 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3605 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3606 | it != ie; ++it, ++ArgNo) { |
| 3607 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3608 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3609 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3610 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3611 | else |
| 3612 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3613 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3614 | |
| 3615 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3616 | // eightbyte of an argument, the whole argument is passed on the |
| 3617 | // stack. If registers have already been assigned for some |
| 3618 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3619 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3620 | FreeIntRegs -= NeededInt; |
| 3621 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3622 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3623 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3624 | } |
| 3625 | } |
| 3626 | } |
| 3627 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3628 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3629 | Address VAListAddr, QualType Ty) { |
| 3630 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3631 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3632 | llvm::Value *overflow_arg_area = |
| 3633 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3634 | |
| 3635 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3636 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3637 | // It isn't stated explicitly in the standard, but in practice we use |
| 3638 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3639 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3640 | if (Align > CharUnits::fromQuantity(8)) { |
| 3641 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3642 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | // 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] | 3646 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3647 | llvm::Value *Res = |
| 3648 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3649 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3650 | |
| 3651 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3652 | // l->overflow_arg_area + sizeof(type). |
| 3653 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3654 | // an 8 byte boundary. |
| 3655 | |
| 3656 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3657 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3658 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3659 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3660 | "overflow_arg_area.next"); |
| 3661 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3662 | |
| 3663 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3664 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3667 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3668 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3669 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3670 | // struct { |
| 3671 | // i32 gp_offset; |
| 3672 | // i32 fp_offset; |
| 3673 | // i8* overflow_arg_area; |
| 3674 | // i8* reg_save_area; |
| 3675 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3676 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3677 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3678 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3679 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3680 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3681 | |
| 3682 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3683 | // in the registers. If not go to step 7. |
| 3684 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3685 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3686 | |
| 3687 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3688 | // general purpose registers needed to pass type and num_fp to hold |
| 3689 | // the number of floating point registers needed. |
| 3690 | |
| 3691 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3692 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3693 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3694 | // |
| 3695 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3696 | // register save space). |
| 3697 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3698 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3699 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3700 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3701 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3702 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3703 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3704 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3705 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3706 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3707 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3711 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3712 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3713 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3714 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3715 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3716 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3717 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3718 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3719 | } |
| 3720 | |
| 3721 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3722 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3723 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3724 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3725 | |
| 3726 | // Emit code to load the value if it was passed in registers. |
| 3727 | |
| 3728 | CGF.EmitBlock(InRegBlock); |
| 3729 | |
| 3730 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3731 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3732 | // copying to a temporary location in case the parameter is passed |
| 3733 | // in different register classes or requires an alignment greater |
| 3734 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3735 | // |
| 3736 | // FIXME: This really results in shameful code when we end up needing to |
| 3737 | // collect arguments from different places; often what should result in a |
| 3738 | // simple assembling of a structure from scattered addresses has many more |
| 3739 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3740 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3741 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3742 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3743 | "reg_save_area"); |
| 3744 | |
| 3745 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3746 | if (neededInt && neededSSE) { |
| 3747 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3748 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3749 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3750 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3751 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3752 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3753 | llvm::Type *TyLo = ST->getElementType(0); |
| 3754 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3755 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3756 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3757 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3758 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3759 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3760 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3761 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3762 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3763 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3764 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3765 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3766 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3767 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3768 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3769 | CGF.Builder.CreateStore(V, |
| 3770 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3771 | |
| 3772 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3773 | V = CGF.Builder.CreateAlignedLoad( |
| 3774 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3775 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3776 | CharUnits Offset = CharUnits::fromQuantity( |
| 3777 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3778 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3779 | |
| 3780 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3781 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3782 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3783 | CharUnits::fromQuantity(8)); |
| 3784 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3785 | |
| 3786 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3787 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3788 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3789 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3790 | CharUnits TyAlign = SizeAlign.second; |
| 3791 | |
| 3792 | // Copy into a temporary if the type is more aligned than the |
| 3793 | // register save area. |
| 3794 | if (TyAlign.getQuantity() > 8) { |
| 3795 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3796 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3797 | RegAddr = Tmp; |
| 3798 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3799 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3800 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3801 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3802 | CharUnits::fromQuantity(16)); |
| 3803 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3804 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3805 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3806 | // SSE registers are spaced 16 bytes apart in the register save |
| 3807 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3808 | // The ABI isn't explicit about this, but it seems reasonable |
| 3809 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3810 | // naturally 16-byte aligned and the prologue is expected to store |
| 3811 | // all the SSE registers to the RSA. |
| 3812 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3813 | CharUnits::fromQuantity(16)); |
| 3814 | Address RegAddrHi = |
| 3815 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3816 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3817 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3818 | ? AI.getCoerceToType() |
| 3819 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3820 | llvm::Value *V; |
| 3821 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3822 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3823 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3824 | RegAddrLo, ST->getStructElementType(0))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3825 | CGF.Builder.CreateStore(V, |
| 3826 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3827 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3828 | RegAddrHi, ST->getStructElementType(1))); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3829 | CGF.Builder.CreateStore(V, |
| 3830 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3831 | |
| 3832 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3836 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3837 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3838 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3839 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3840 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3841 | gp_offset_p); |
| 3842 | } |
| 3843 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3844 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3845 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3846 | fp_offset_p); |
| 3847 | } |
| 3848 | CGF.EmitBranch(ContBlock); |
| 3849 | |
| 3850 | // Emit code to load the value if it was passed in memory. |
| 3851 | |
| 3852 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3853 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3854 | |
| 3855 | // Return the appropriate result. |
| 3856 | |
| 3857 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3858 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3859 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3860 | return ResAddr; |
| 3861 | } |
| 3862 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3863 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3864 | QualType Ty) const { |
| 3865 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3866 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3867 | CharUnits::fromQuantity(8), |
| 3868 | /*allowHigherAlign*/ false); |
| 3869 | } |
| 3870 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3871 | ABIArgInfo |
| 3872 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3873 | const ABIArgInfo ¤t) const { |
| 3874 | // Assumes vectorCall calling convention. |
| 3875 | const Type *Base = nullptr; |
| 3876 | uint64_t NumElts = 0; |
| 3877 | |
| 3878 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3879 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3880 | FreeSSERegs -= NumElts; |
| 3881 | return getDirectX86Hva(); |
| 3882 | } |
| 3883 | return current; |
| 3884 | } |
| 3885 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3886 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3887 | bool IsReturnType, bool IsVectorCall, |
| 3888 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3889 | |
| 3890 | if (Ty->isVoidType()) |
| 3891 | return ABIArgInfo::getIgnore(); |
| 3892 | |
| 3893 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3894 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3895 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3896 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3897 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3898 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3899 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3900 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3901 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3902 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3903 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3904 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3908 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
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 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3911 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3912 | const Type *Base = nullptr; |
| 3913 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3914 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3915 | // other targets. |
| 3916 | if ((IsVectorCall || IsRegCall) && |
| 3917 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3918 | if (IsRegCall) { |
| 3919 | if (FreeSSERegs >= NumElts) { |
| 3920 | FreeSSERegs -= NumElts; |
| 3921 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3922 | return ABIArgInfo::getDirect(); |
| 3923 | return ABIArgInfo::getExpand(); |
| 3924 | } |
| 3925 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3926 | } else if (IsVectorCall) { |
| 3927 | if (FreeSSERegs >= NumElts && |
| 3928 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3929 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3930 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3931 | } else if (IsReturnType) { |
| 3932 | return ABIArgInfo::getExpand(); |
| 3933 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3934 | // HVAs are delayed and reclassified in the 2nd step. |
| 3935 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3936 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3937 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3940 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3941 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3942 | // directly. |
| 3943 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3944 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3945 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3948 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3949 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3950 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3951 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3952 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3953 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3954 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3955 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Reid Kleckner | 08f64e9 | 2018-10-31 17:43:55 +0000 | [diff] [blame] | 3958 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3959 | switch (BT->getKind()) { |
| 3960 | case BuiltinType::Bool: |
| 3961 | // Bool type is always extended to the ABI, other builtin types are not |
| 3962 | // extended. |
| 3963 | return ABIArgInfo::getExtend(Ty); |
| 3964 | |
| 3965 | case BuiltinType::LongDouble: |
| 3966 | // Mingw64 GCC uses the old 80 bit extended precision floating point |
| 3967 | // unit. It passes them indirectly through memory. |
| 3968 | if (IsMingw64) { |
| 3969 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3970 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
| 3971 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3972 | } |
| 3973 | break; |
| 3974 | |
| 3975 | case BuiltinType::Int128: |
| 3976 | case BuiltinType::UInt128: |
| 3977 | // If it's a parameter type, the normal ABI rule is that arguments larger |
| 3978 | // than 8 bytes are passed indirectly. GCC follows it. We follow it too, |
| 3979 | // even though it isn't particularly efficient. |
| 3980 | if (!IsReturnType) |
| 3981 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3982 | |
| 3983 | // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. |
| 3984 | // Clang matches them for compatibility. |
| 3985 | return ABIArgInfo::getDirect( |
| 3986 | llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2)); |
| 3987 | |
| 3988 | default: |
| 3989 | break; |
| 3990 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3993 | return ABIArgInfo::getDirect(); |
| 3994 | } |
| 3995 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3996 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 3997 | unsigned FreeSSERegs, |
| 3998 | bool IsVectorCall, |
| 3999 | bool IsRegCall) const { |
| 4000 | unsigned Count = 0; |
| 4001 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4002 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 4003 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4004 | if (Count < VectorcallMaxParamNumAsReg) |
| 4005 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4006 | else { |
| 4007 | // Since these cannot be passed in registers, pretend no registers |
| 4008 | // are left. |
| 4009 | unsigned ZeroSSERegsAvail = 0; |
| 4010 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 4011 | IsVectorCall, IsRegCall); |
| 4012 | } |
| 4013 | ++Count; |
| 4014 | } |
| 4015 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4016 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4017 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4018 | } |
| 4019 | } |
| 4020 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4021 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4022 | bool IsVectorCall = |
| 4023 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4024 | bool IsRegCall = FI.getCallingConvention() == llvm::CallingConv::X86_RegCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 4025 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4026 | unsigned FreeSSERegs = 0; |
| 4027 | if (IsVectorCall) { |
| 4028 | // We can use up to 4 SSE return registers with vectorcall. |
| 4029 | FreeSSERegs = 4; |
| 4030 | } else if (IsRegCall) { |
| 4031 | // RegCall gives us 16 SSE registers. |
| 4032 | FreeSSERegs = 16; |
| 4033 | } |
| 4034 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4035 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4036 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 4037 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4038 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4039 | if (IsVectorCall) { |
| 4040 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4041 | FreeSSERegs = 6; |
| 4042 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4043 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4044 | FreeSSERegs = 16; |
| 4045 | } |
| 4046 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4047 | if (IsVectorCall) { |
| 4048 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4049 | } else { |
| 4050 | for (auto &I : FI.arguments()) |
| 4051 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4052 | } |
| 4053 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4056 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4057 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4058 | |
| 4059 | bool IsIndirect = false; |
| 4060 | |
| 4061 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4062 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4063 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4064 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4065 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4066 | } |
| 4067 | |
| 4068 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4069 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4070 | CharUnits::fromQuantity(8), |
| 4071 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4072 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4073 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4074 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4075 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4076 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4077 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4078 | bool IsSoftFloatABI; |
| 4079 | |
| 4080 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4081 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4082 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4083 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4084 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4085 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4086 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4087 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4088 | }; |
| 4089 | |
| 4090 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4091 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4092 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4093 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4094 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4095 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4096 | // This is recovered from gcc output. |
| 4097 | return 1; // r1 is the dedicated stack pointer |
| 4098 | } |
| 4099 | |
| 4100 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4101 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4102 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4103 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4104 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4105 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4106 | // Complex types are passed just like their elements |
| 4107 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4108 | Ty = CTy->getElementType(); |
| 4109 | |
| 4110 | if (Ty->isVectorType()) |
| 4111 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4112 | : 4); |
| 4113 | |
| 4114 | // For single-element float/vector structs, we consider the whole type |
| 4115 | // to have the same alignment requirements as its single element. |
| 4116 | const Type *AlignTy = nullptr; |
| 4117 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4118 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4119 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4120 | (BT && BT->isFloatingPoint())) |
| 4121 | AlignTy = EltType; |
| 4122 | } |
| 4123 | |
| 4124 | if (AlignTy) |
| 4125 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4126 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4127 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4128 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4129 | // TODO: this implementation is now likely redundant with |
| 4130 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4131 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4132 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4133 | if (getTarget().getTriple().isOSDarwin()) { |
| 4134 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4135 | TI.second = getParamTypeAlignment(Ty); |
| 4136 | |
| 4137 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4138 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4139 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4140 | /*AllowHigherAlign=*/true); |
| 4141 | } |
| 4142 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4143 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4144 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4145 | // TODO: Implement this. For now ignore. |
| 4146 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4147 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4150 | // struct __va_list_tag { |
| 4151 | // unsigned char gpr; |
| 4152 | // unsigned char fpr; |
| 4153 | // unsigned short reserved; |
| 4154 | // void *overflow_arg_area; |
| 4155 | // void *reg_save_area; |
| 4156 | // }; |
| 4157 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4158 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4159 | bool isInt = |
| 4160 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4161 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4162 | |
| 4163 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4164 | // with the argument-lowering code. |
| 4165 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4166 | |
| 4167 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4168 | |
| 4169 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4170 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4171 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4172 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 4173 | } else { |
| 4174 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4175 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4176 | |
| 4177 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4178 | |
| 4179 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4180 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4181 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4182 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4183 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4184 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4185 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4186 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4187 | |
| 4188 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4189 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4190 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4191 | |
| 4192 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4193 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4194 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4195 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4196 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4197 | // Case 1: consume registers. |
| 4198 | Address RegAddr = Address::invalid(); |
| 4199 | { |
| 4200 | CGF.EmitBlock(UsingRegs); |
| 4201 | |
| 4202 | Address RegSaveAreaPtr = |
| 4203 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 4204 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4205 | CharUnits::fromQuantity(8)); |
| 4206 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4207 | |
| 4208 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4209 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4210 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4211 | CharUnits::fromQuantity(32)); |
| 4212 | } |
| 4213 | |
| 4214 | // Get the address of the saved value by scaling the number of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4215 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4216 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4217 | llvm::Value *RegOffset = |
| 4218 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4219 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4220 | RegAddr.getPointer(), RegOffset), |
| 4221 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4222 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4223 | |
| 4224 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4225 | NumRegs = |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4226 | Builder.CreateAdd(NumRegs, |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4227 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4228 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4229 | |
| 4230 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4231 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4232 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4233 | // Case 2: consume space in the overflow area. |
| 4234 | Address MemAddr = Address::invalid(); |
| 4235 | { |
| 4236 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4237 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4238 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4239 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4240 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4241 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4242 | |
| 4243 | CharUnits Size; |
| 4244 | if (!isIndirect) { |
| 4245 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4246 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4247 | } else { |
| 4248 | Size = CGF.getPointerSize(); |
| 4249 | } |
| 4250 | |
| 4251 | Address OverflowAreaAddr = |
| 4252 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4253 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4254 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4255 | // Round up address of argument to alignment |
| 4256 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4257 | if (Align > OverflowAreaAlign) { |
| 4258 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4259 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4260 | Align); |
| 4261 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4262 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4263 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4264 | |
| 4265 | // Increase the overflow area. |
| 4266 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4267 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4268 | CGF.EmitBranch(Cont); |
| 4269 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4270 | |
| 4271 | CGF.EmitBlock(Cont); |
| 4272 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4273 | // Merge the cases with a phi. |
| 4274 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4275 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4276 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4277 | // Load the pointer if the argument was passed indirectly. |
| 4278 | if (isIndirect) { |
| 4279 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4280 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | return Result; |
| 4284 | } |
| 4285 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4286 | bool |
| 4287 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4288 | llvm::Value *Address) const { |
| 4289 | // This is calculated from the LLVM and GCC tables and verified |
| 4290 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4291 | |
| 4292 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4293 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4294 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4295 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4296 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4297 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4298 | |
| 4299 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4300 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4301 | |
| 4302 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4303 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4304 | |
| 4305 | // 64-76 are various 4-byte special-purpose registers: |
| 4306 | // 64: mq |
| 4307 | // 65: lr |
| 4308 | // 66: ctr |
| 4309 | // 67: ap |
| 4310 | // 68-75 cr0-7 |
| 4311 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4312 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4313 | |
| 4314 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4315 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4316 | |
| 4317 | // 109: vrsave |
| 4318 | // 110: vscr |
| 4319 | // 111: spe_acc |
| 4320 | // 112: spefscr |
| 4321 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4322 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4323 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4324 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4327 | // PowerPC-64 |
| 4328 | |
| 4329 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4330 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4331 | class PPC64_SVR4_ABIInfo : public SwiftABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4332 | public: |
| 4333 | enum ABIKind { |
| 4334 | ELFv1 = 0, |
| 4335 | ELFv2 |
| 4336 | }; |
| 4337 | |
| 4338 | private: |
| 4339 | static const unsigned GPRBits = 64; |
| 4340 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4341 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4342 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4343 | |
| 4344 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4345 | // will be passed in a QPX register. |
| 4346 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4347 | if (!HasQPX) |
| 4348 | return false; |
| 4349 | |
| 4350 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4351 | unsigned NumElements = VT->getNumElements(); |
| 4352 | if (NumElements == 1) |
| 4353 | return false; |
| 4354 | |
| 4355 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4356 | if (getContext().getTypeSize(Ty) <= 256) |
| 4357 | return true; |
| 4358 | } else if (VT->getElementType()-> |
| 4359 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4360 | if (getContext().getTypeSize(Ty) <= 128) |
| 4361 | return true; |
| 4362 | } |
| 4363 | } |
| 4364 | |
| 4365 | return false; |
| 4366 | } |
| 4367 | |
| 4368 | bool IsQPXVectorTy(QualType Ty) const { |
| 4369 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4370 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4371 | |
| 4372 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4373 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4374 | bool SoftFloatABI) |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4375 | : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4376 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4377 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4378 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4379 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4380 | |
| 4381 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4382 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4383 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4384 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4385 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4386 | uint64_t Members) const override; |
| 4387 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4388 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4389 | // Example: For aggregate arguments that fit in a register, we could |
| 4390 | // use getDirectInReg (as is done below for structs containing a single |
| 4391 | // floating-point value) to avoid pushing them to memory on function |
| 4392 | // entry. This would require changing the logic in PPCISelLowering |
| 4393 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4394 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4395 | if (!getCXXABI().classifyReturnType(FI)) |
| 4396 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4397 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4398 | // We rely on the default argument classification for the most part. |
| 4399 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4400 | // 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] | 4401 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4402 | if (T) { |
| 4403 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4404 | if (IsQPXVectorTy(T) || |
| 4405 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4406 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4407 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4408 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4409 | continue; |
| 4410 | } |
| 4411 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4412 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4413 | } |
| 4414 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4415 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4416 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4417 | QualType Ty) const override; |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4418 | |
| 4419 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 4420 | bool asReturnValue) const override { |
| 4421 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4422 | } |
| 4423 | |
| 4424 | bool isSwiftErrorInRegister() const override { |
| 4425 | return false; |
| 4426 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4427 | }; |
| 4428 | |
| 4429 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4430 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4431 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4432 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4433 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4434 | bool SoftFloatABI) |
| 4435 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4436 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4437 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4438 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4439 | // This is recovered from gcc output. |
| 4440 | return 1; // r1 is the dedicated stack pointer |
| 4441 | } |
| 4442 | |
| 4443 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4444 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4445 | }; |
| 4446 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4447 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4448 | public: |
| 4449 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4450 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4451 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4452 | // This is recovered from gcc output. |
| 4453 | return 1; // r1 is the dedicated stack pointer |
| 4454 | } |
| 4455 | |
| 4456 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4457 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4458 | }; |
| 4459 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4460 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4461 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4462 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4463 | // extended to 64 bits. |
| 4464 | bool |
| 4465 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4466 | // Treat an enum type as its underlying type. |
| 4467 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4468 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4469 | |
| 4470 | // Promotable integer types are required to be promoted by the ABI. |
| 4471 | if (Ty->isPromotableIntegerType()) |
| 4472 | return true; |
| 4473 | |
| 4474 | // In addition to the usual promotable integer types, we also need to |
| 4475 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4476 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4477 | switch (BT->getKind()) { |
| 4478 | case BuiltinType::Int: |
| 4479 | case BuiltinType::UInt: |
| 4480 | return true; |
| 4481 | default: |
| 4482 | break; |
| 4483 | } |
| 4484 | |
| 4485 | return false; |
| 4486 | } |
| 4487 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4488 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4489 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4490 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4491 | // Complex types are passed just like their elements. |
| 4492 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4493 | Ty = CTy->getElementType(); |
| 4494 | |
| 4495 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4496 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4497 | if (IsQPXVectorTy(Ty)) { |
| 4498 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4499 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4500 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4501 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4502 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4503 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4504 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4505 | |
| 4506 | // For single-element float/vector structs, we consider the whole type |
| 4507 | // to have the same alignment requirements as its single element. |
| 4508 | const Type *AlignAsType = nullptr; |
| 4509 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4510 | if (EltType) { |
| 4511 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4512 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4513 | getContext().getTypeSize(EltType) == 128) || |
| 4514 | (BT && BT->isFloatingPoint())) |
| 4515 | AlignAsType = EltType; |
| 4516 | } |
| 4517 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4518 | // Likewise for ELFv2 homogeneous aggregates. |
| 4519 | const Type *Base = nullptr; |
| 4520 | uint64_t Members = 0; |
| 4521 | if (!AlignAsType && Kind == ELFv2 && |
| 4522 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4523 | AlignAsType = Base; |
| 4524 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4525 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4526 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4527 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4528 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4529 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4530 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4531 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4532 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4533 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4534 | |
| 4535 | // Otherwise, we only need alignment for any aggregate type that |
| 4536 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4537 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4538 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4539 | return CharUnits::fromQuantity(32); |
| 4540 | return CharUnits::fromQuantity(16); |
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 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4543 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4546 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4547 | /// aggregate. Base is set to the base element type, and Members is set |
| 4548 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4549 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4550 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4551 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4552 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4553 | if (NElements == 0) |
| 4554 | return false; |
| 4555 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4556 | return false; |
| 4557 | Members *= NElements; |
| 4558 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4559 | const RecordDecl *RD = RT->getDecl(); |
| 4560 | if (RD->hasFlexibleArrayMember()) |
| 4561 | return false; |
| 4562 | |
| 4563 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4564 | |
| 4565 | // If this is a C++ record, check the bases first. |
| 4566 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4567 | for (const auto &I : CXXRD->bases()) { |
| 4568 | // Ignore empty records. |
| 4569 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4570 | continue; |
| 4571 | |
| 4572 | uint64_t FldMembers; |
| 4573 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4574 | return false; |
| 4575 | |
| 4576 | Members += FldMembers; |
| 4577 | } |
| 4578 | } |
| 4579 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4580 | for (const auto *FD : RD->fields()) { |
| 4581 | // Ignore (non-zero arrays of) empty records. |
| 4582 | QualType FT = FD->getType(); |
| 4583 | while (const ConstantArrayType *AT = |
| 4584 | getContext().getAsConstantArrayType(FT)) { |
| 4585 | if (AT->getSize().getZExtValue() == 0) |
| 4586 | return false; |
| 4587 | FT = AT->getElementType(); |
| 4588 | } |
| 4589 | if (isEmptyRecord(getContext(), FT, true)) |
| 4590 | continue; |
| 4591 | |
| 4592 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4593 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4594 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4595 | continue; |
| 4596 | |
| 4597 | uint64_t FldMembers; |
| 4598 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4599 | return false; |
| 4600 | |
| 4601 | Members = (RD->isUnion() ? |
| 4602 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4603 | } |
| 4604 | |
| 4605 | if (!Base) |
| 4606 | return false; |
| 4607 | |
| 4608 | // Ensure there is no padding. |
| 4609 | if (getContext().getTypeSize(Base) * Members != |
| 4610 | getContext().getTypeSize(Ty)) |
| 4611 | return false; |
| 4612 | } else { |
| 4613 | Members = 1; |
| 4614 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4615 | Members = 2; |
| 4616 | Ty = CT->getElementType(); |
| 4617 | } |
| 4618 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4619 | // Most ABIs only support float, double, and some vector type widths. |
| 4620 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4621 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4622 | |
| 4623 | // The base type must be the same for all members. Types that |
| 4624 | // agree in both total size and mode (float vs. vector) are |
| 4625 | // treated as being equivalent here. |
| 4626 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4627 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4628 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4629 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4630 | // so make sure to widen it explicitly. |
| 4631 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4632 | QualType EltTy = VT->getElementType(); |
| 4633 | unsigned NumElements = |
| 4634 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4635 | Base = getContext() |
| 4636 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4637 | .getTypePtr(); |
| 4638 | } |
| 4639 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4640 | |
| 4641 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4642 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4643 | return false; |
| 4644 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4645 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4646 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4647 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4648 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4649 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4650 | // double, long double, or 128-bit vectors. |
| 4651 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4652 | if (BT->getKind() == BuiltinType::Float || |
| 4653 | BT->getKind() == BuiltinType::Double || |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4654 | BT->getKind() == BuiltinType::LongDouble || |
| 4655 | (getContext().getTargetInfo().hasFloat128Type() && |
| 4656 | (BT->getKind() == BuiltinType::Float128))) { |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4657 | if (IsSoftFloatABI) |
| 4658 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4659 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4660 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4661 | } |
| 4662 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4663 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4664 | return true; |
| 4665 | } |
| 4666 | return false; |
| 4667 | } |
| 4668 | |
| 4669 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4670 | const Type *Base, uint64_t Members) const { |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4671 | // Vector and fp128 types require one register, other floating point types |
| 4672 | // require one or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4673 | uint32_t NumRegs = |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4674 | ((getContext().getTargetInfo().hasFloat128Type() && |
| 4675 | Base->isFloat128Type()) || |
| 4676 | Base->isVectorType()) ? 1 |
| 4677 | : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4678 | |
| 4679 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4680 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4683 | ABIArgInfo |
| 4684 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4685 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4686 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4687 | if (Ty->isAnyComplexType()) |
| 4688 | return ABIArgInfo::getDirect(); |
| 4689 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4690 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4691 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4692 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4693 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4694 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4695 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4696 | else if (Size < 128) { |
| 4697 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4698 | return ABIArgInfo::getDirect(CoerceTy); |
| 4699 | } |
| 4700 | } |
| 4701 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4702 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4703 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4704 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4705 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4706 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4707 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4708 | |
| 4709 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4710 | const Type *Base = nullptr; |
| 4711 | uint64_t Members = 0; |
| 4712 | if (Kind == ELFv2 && |
| 4713 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4714 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4715 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4716 | return ABIArgInfo::getDirect(CoerceTy); |
| 4717 | } |
| 4718 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4719 | // If an aggregate may end up fully in registers, we do not |
| 4720 | // use the ByVal method, but pass the aggregate as array. |
| 4721 | // This is usually beneficial since we avoid forcing the |
| 4722 | // back-end to store the argument to memory. |
| 4723 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4724 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4725 | llvm::Type *CoerceTy; |
| 4726 | |
| 4727 | // Types up to 8 bytes are passed as integer type (which will be |
| 4728 | // properly aligned in the argument save area doubleword). |
| 4729 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4730 | CoerceTy = |
| 4731 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4732 | // Larger types are passed as arrays, with the base type selected |
| 4733 | // according to the required alignment in the save area. |
| 4734 | else { |
| 4735 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4736 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4737 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4738 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4739 | } |
| 4740 | |
| 4741 | return ABIArgInfo::getDirect(CoerceTy); |
| 4742 | } |
| 4743 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4744 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4745 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4746 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4747 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4750 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4751 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | ABIArgInfo |
| 4755 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4756 | if (RetTy->isVoidType()) |
| 4757 | return ABIArgInfo::getIgnore(); |
| 4758 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4759 | if (RetTy->isAnyComplexType()) |
| 4760 | return ABIArgInfo::getDirect(); |
| 4761 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4762 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4763 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4764 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4765 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4766 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4767 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4768 | else if (Size < 128) { |
| 4769 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4770 | return ABIArgInfo::getDirect(CoerceTy); |
| 4771 | } |
| 4772 | } |
| 4773 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4774 | if (isAggregateTypeForABI(RetTy)) { |
| 4775 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4776 | const Type *Base = nullptr; |
| 4777 | uint64_t Members = 0; |
| 4778 | if (Kind == ELFv2 && |
| 4779 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4780 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4781 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4782 | return ABIArgInfo::getDirect(CoerceTy); |
| 4783 | } |
| 4784 | |
| 4785 | // ELFv2 small aggregates are returned in up to two registers. |
| 4786 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4787 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4788 | if (Bits == 0) |
| 4789 | return ABIArgInfo::getIgnore(); |
| 4790 | |
| 4791 | llvm::Type *CoerceTy; |
| 4792 | if (Bits > GPRBits) { |
| 4793 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4794 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4795 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4796 | CoerceTy = |
| 4797 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4798 | return ABIArgInfo::getDirect(CoerceTy); |
| 4799 | } |
| 4800 | |
| 4801 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4802 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4803 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4804 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4805 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4806 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4809 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4810 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4811 | QualType Ty) const { |
| 4812 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4813 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4814 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4815 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4816 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4817 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4818 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4819 | // in separate doublewords. However, Clang expects us to produce a |
| 4820 | // pointer to a structure with the two parts packed tightly. So generate |
| 4821 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4822 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4823 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4824 | CharUnits EltSize = TypeInfo.first / 2; |
| 4825 | if (EltSize < SlotSize) { |
| 4826 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4827 | SlotSize * 2, SlotSize, |
| 4828 | SlotSize, /*AllowHigher*/ true); |
| 4829 | |
| 4830 | Address RealAddr = Addr; |
| 4831 | Address ImagAddr = RealAddr; |
| 4832 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4833 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4834 | SlotSize - EltSize); |
| 4835 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4836 | 2 * SlotSize - EltSize); |
| 4837 | } else { |
| 4838 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4839 | } |
| 4840 | |
| 4841 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4842 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4843 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4844 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4845 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4846 | |
| 4847 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4848 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4849 | /*init*/ true); |
| 4850 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4851 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4854 | // Otherwise, just use the general rule. |
| 4855 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4856 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | static bool |
| 4860 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4861 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4862 | // This is calculated from the LLVM and GCC tables and verified |
| 4863 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4864 | |
| 4865 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4866 | |
| 4867 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4868 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4869 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4870 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4871 | |
| 4872 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4873 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4874 | |
| 4875 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4876 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4877 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4878 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4879 | // 64: mq |
| 4880 | // 65: lr |
| 4881 | // 66: ctr |
| 4882 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4883 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4884 | |
| 4885 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4886 | // 68-75 cr0-7 |
| 4887 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4888 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4889 | |
| 4890 | // 77-108: v0-31, the 16-byte vector registers |
| 4891 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4892 | |
| 4893 | // 109: vrsave |
| 4894 | // 110: vscr |
| 4895 | // 111: spe_acc |
| 4896 | // 112: spefscr |
| 4897 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4898 | // 114: tfhar |
| 4899 | // 115: tfiar |
| 4900 | // 116: texasr |
| 4901 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4902 | |
| 4903 | return false; |
| 4904 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4905 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4906 | bool |
| 4907 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4908 | CodeGen::CodeGenFunction &CGF, |
| 4909 | llvm::Value *Address) const { |
| 4910 | |
| 4911 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4912 | } |
| 4913 | |
| 4914 | bool |
| 4915 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4916 | llvm::Value *Address) const { |
| 4917 | |
| 4918 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4919 | } |
| 4920 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4921 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4922 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4923 | //===----------------------------------------------------------------------===// |
| 4924 | |
| 4925 | namespace { |
| 4926 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4927 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4928 | public: |
| 4929 | enum ABIKind { |
| 4930 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4931 | DarwinPCS, |
| 4932 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4933 | }; |
| 4934 | |
| 4935 | private: |
| 4936 | ABIKind Kind; |
| 4937 | |
| 4938 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4939 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4940 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4941 | |
| 4942 | private: |
| 4943 | ABIKind getABIKind() const { return Kind; } |
| 4944 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4945 | |
| 4946 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4947 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4948 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4949 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4950 | uint64_t Members) const override; |
| 4951 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4952 | bool isIllegalVectorType(QualType Ty) const; |
| 4953 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4954 | void computeInfo(CGFunctionInfo &FI) const override { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 4955 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4956 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4957 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4958 | for (auto &it : FI.arguments()) |
| 4959 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4962 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4963 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4964 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4965 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4966 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4967 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4968 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4969 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4970 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 4971 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4972 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4973 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4974 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4975 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4976 | QualType Ty) const override; |
| 4977 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 4978 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4979 | bool asReturnValue) const override { |
| 4980 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4981 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 4982 | bool isSwiftErrorInRegister() const override { |
| 4983 | return true; |
| 4984 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 4985 | |
| 4986 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 4987 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4988 | }; |
| 4989 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4990 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4991 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4992 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4993 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4994 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4995 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 4996 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4997 | } |
| 4998 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4999 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 5000 | return 31; |
| 5001 | } |
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 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5004 | |
| 5005 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5006 | CodeGen::CodeGenModule &CGM) const override { |
| 5007 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 5008 | if (!FD) |
| 5009 | return; |
| 5010 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5011 | |
| 5012 | auto Kind = CGM.getCodeGenOpts().getSignReturnAddress(); |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5013 | if (Kind != CodeGenOptions::SignReturnAddressScope::None) { |
| 5014 | Fn->addFnAttr("sign-return-address", |
| 5015 | Kind == CodeGenOptions::SignReturnAddressScope::All |
| 5016 | ? "all" |
| 5017 | : "non-leaf"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5018 | |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5019 | auto Key = CGM.getCodeGenOpts().getSignReturnAddressKey(); |
| 5020 | Fn->addFnAttr("sign-return-address-key", |
| 5021 | Key == CodeGenOptions::SignReturnAddressKeyValue::AKey |
| 5022 | ? "a_key" |
| 5023 | : "b_key"); |
| 5024 | } |
| 5025 | |
| 5026 | if (CGM.getCodeGenOpts().BranchTargetEnforcement) |
| 5027 | Fn->addFnAttr("branch-target-enforcement"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5028 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5029 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5030 | |
| 5031 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 5032 | public: |
| 5033 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 5034 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 5035 | |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5036 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5037 | CodeGen::CodeGenModule &CGM) const override; |
| 5038 | |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5039 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5040 | llvm::SmallString<24> &Opt) const override { |
| 5041 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5042 | } |
| 5043 | |
| 5044 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5045 | llvm::SmallString<32> &Opt) const override { |
| 5046 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5047 | } |
| 5048 | }; |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5049 | |
| 5050 | void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( |
| 5051 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5052 | AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5053 | if (GV->isDeclaration()) |
| 5054 | return; |
| 5055 | addStackProbeTargetAttributes(D, GV, CGM); |
| 5056 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5057 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5058 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5059 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5060 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5061 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5062 | // Handle illegal vector types here. |
| 5063 | if (isIllegalVectorType(Ty)) { |
| 5064 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5065 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 5066 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5067 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 5068 | return ABIArgInfo::getDirect(ResType); |
| 5069 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5070 | if (Size <= 32) { |
| 5071 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5072 | return ABIArgInfo::getDirect(ResType); |
| 5073 | } |
| 5074 | if (Size == 64) { |
| 5075 | llvm::Type *ResType = |
| 5076 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5077 | return ABIArgInfo::getDirect(ResType); |
| 5078 | } |
| 5079 | if (Size == 128) { |
| 5080 | llvm::Type *ResType = |
| 5081 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5082 | return ABIArgInfo::getDirect(ResType); |
| 5083 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5084 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5085 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5086 | |
| 5087 | if (!isAggregateTypeForABI(Ty)) { |
| 5088 | // Treat an enum type as its underlying type. |
| 5089 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5090 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5091 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5092 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5093 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5094 | : ABIArgInfo::getDirect()); |
| 5095 | } |
| 5096 | |
| 5097 | // Structures with either a non-trivial destructor or a non-trivial |
| 5098 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5099 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5100 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5101 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5105 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5106 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5107 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5108 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5109 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5110 | return ABIArgInfo::getIgnore(); |
| 5111 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5112 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5113 | // 0. |
| 5114 | if (IsEmpty && Size == 0) |
| 5115 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5116 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5117 | } |
| 5118 | |
| 5119 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5120 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5121 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5122 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5123 | return ABIArgInfo::getDirect( |
| 5124 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5128 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5129 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5130 | // same size and alignment. |
| 5131 | if (getTarget().isRenderScriptTarget()) { |
| 5132 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5133 | } |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5134 | unsigned Alignment; |
| 5135 | if (Kind == AArch64ABIInfo::AAPCS) { |
| 5136 | Alignment = getContext().getTypeUnadjustedAlign(Ty); |
| 5137 | Alignment = Alignment < 128 ? 64 : 128; |
| 5138 | } else { |
| 5139 | Alignment = getContext().getTypeAlign(Ty); |
| 5140 | } |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5141 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5142 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5143 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5144 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 5145 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5146 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5147 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5148 | } |
| 5149 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5150 | } |
| 5151 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5152 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5155 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5156 | if (RetTy->isVoidType()) |
| 5157 | return ABIArgInfo::getIgnore(); |
| 5158 | |
| 5159 | // Large vector types should be returned via memory. |
| 5160 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5161 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5162 | |
| 5163 | if (!isAggregateTypeForABI(RetTy)) { |
| 5164 | // Treat an enum type as its underlying type. |
| 5165 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5166 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5167 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5168 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5169 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5170 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5173 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5174 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5175 | return ABIArgInfo::getIgnore(); |
| 5176 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5177 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5178 | uint64_t Members = 0; |
| 5179 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5180 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5181 | return ABIArgInfo::getDirect(); |
| 5182 | |
| 5183 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5184 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5185 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5186 | // same size and alignment. |
| 5187 | if (getTarget().isRenderScriptTarget()) { |
| 5188 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5189 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5190 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5191 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5192 | |
| 5193 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5194 | // For aggregates with 16-byte alignment, we use i128. |
| 5195 | if (Alignment < 128 && Size == 128) { |
| 5196 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5197 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5198 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5199 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5200 | } |
| 5201 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5202 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5205 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5206 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5207 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5208 | // Check whether VT is legal. |
| 5209 | unsigned NumElements = VT->getNumElements(); |
| 5210 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5211 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5212 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5213 | return true; |
| 5214 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5215 | } |
| 5216 | return false; |
| 5217 | } |
| 5218 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5219 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5220 | llvm::Type *eltTy, |
| 5221 | unsigned elts) const { |
| 5222 | if (!llvm::isPowerOf2_32(elts)) |
| 5223 | return false; |
| 5224 | if (totalSize.getQuantity() != 8 && |
| 5225 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5226 | return false; |
| 5227 | return true; |
| 5228 | } |
| 5229 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5230 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5231 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5232 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5233 | // but with the difference that any floating-point type is allowed, |
| 5234 | // including __fp16. |
| 5235 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5236 | if (BT->isFloatingPoint()) |
| 5237 | return true; |
| 5238 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5239 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5240 | if (VecSize == 64 || VecSize == 128) |
| 5241 | return true; |
| 5242 | } |
| 5243 | return false; |
| 5244 | } |
| 5245 | |
| 5246 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5247 | uint64_t Members) const { |
| 5248 | return Members <= 4; |
| 5249 | } |
| 5250 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5251 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5252 | QualType Ty, |
| 5253 | CodeGenFunction &CGF) const { |
| 5254 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5255 | bool IsIndirect = AI.isIndirect(); |
| 5256 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5257 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5258 | if (IsIndirect) |
| 5259 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5260 | else if (AI.getCoerceToType()) |
| 5261 | BaseTy = AI.getCoerceToType(); |
| 5262 | |
| 5263 | unsigned NumRegs = 1; |
| 5264 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5265 | BaseTy = ArrTy->getElementType(); |
| 5266 | NumRegs = ArrTy->getNumElements(); |
| 5267 | } |
| 5268 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5269 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5270 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5271 | // Standard, section B.4: |
| 5272 | // |
| 5273 | // struct { |
| 5274 | // void *__stack; |
| 5275 | // void *__gr_top; |
| 5276 | // void *__vr_top; |
| 5277 | // int __gr_offs; |
| 5278 | // int __vr_offs; |
| 5279 | // }; |
| 5280 | |
| 5281 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5282 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5283 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5284 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5285 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5286 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5287 | CharUnits TyAlign = TyInfo.second; |
| 5288 | |
| 5289 | Address reg_offs_p = Address::invalid(); |
| 5290 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5291 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5292 | CharUnits reg_top_offset; |
| 5293 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5294 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5295 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5296 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5297 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5298 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5299 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5300 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5301 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5302 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5303 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5304 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5305 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5306 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 5307 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5308 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5309 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5310 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5311 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | //======================================= |
| 5315 | // Find out where argument was passed |
| 5316 | //======================================= |
| 5317 | |
| 5318 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5319 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5320 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5321 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5322 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5323 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5324 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5325 | |
| 5326 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5327 | |
| 5328 | // Otherwise, at least some kind of argument could go in these registers, the |
Bob Wilson | 3abf169 | 2014-04-21 01:23:36 +0000 | [diff] [blame] | 5329 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5330 | CGF.EmitBlock(MaybeRegBlock); |
| 5331 | |
| 5332 | // Integer arguments may need to correct register alignment (for example a |
| 5333 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5334 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5335 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5336 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5337 | |
| 5338 | reg_offs = CGF.Builder.CreateAdd( |
| 5339 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5340 | "align_regoffs"); |
| 5341 | reg_offs = CGF.Builder.CreateAnd( |
| 5342 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5343 | "aligned_regoffs"); |
| 5344 | } |
| 5345 | |
| 5346 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5347 | // The fact that this is done unconditionally reflects the fact that |
| 5348 | // allocating an argument to the stack also uses up all the remaining |
| 5349 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5350 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5351 | NewOffset = CGF.Builder.CreateAdd( |
| 5352 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5353 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5354 | |
| 5355 | // Now we're in a position to decide whether this argument really was in |
| 5356 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5357 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5358 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5359 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5360 | |
| 5361 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5362 | |
| 5363 | //======================================= |
| 5364 | // Argument was in registers |
| 5365 | //======================================= |
| 5366 | |
| 5367 | // Now we emit the code for if the argument was originally passed in |
| 5368 | // registers. First start the appropriate block: |
| 5369 | CGF.EmitBlock(InRegBlock); |
| 5370 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5371 | llvm::Value *reg_top = nullptr; |
| 5372 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 5373 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5374 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5375 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5376 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5377 | Address RegAddr = Address::invalid(); |
| 5378 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5379 | |
| 5380 | if (IsIndirect) { |
| 5381 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5382 | // stored registers or on the stack will actually be a struct **. |
| 5383 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5384 | } |
| 5385 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5386 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5387 | uint64_t NumMembers = 0; |
| 5388 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5389 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5390 | // Homogeneous aggregates passed in registers will have their elements split |
| 5391 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5392 | // qN+1, ...). We reload and store into a temporary local variable |
| 5393 | // contiguously. |
| 5394 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5395 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5396 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5397 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5398 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5399 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5400 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5401 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5402 | int Offset = 0; |
| 5403 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5404 | BaseTyInfo.first.getQuantity() < 16) |
| 5405 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5406 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5407 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5408 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5409 | Address LoadAddr = |
| 5410 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5411 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5412 | |
| 5413 | Address StoreAddr = |
| 5414 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
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 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5443 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 5444 | CharUnits::Zero(), "stack_p"); |
| 5445 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5446 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5447 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5448 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5449 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5450 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5451 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5452 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5453 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5454 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5455 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5456 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5457 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5458 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5459 | "align_stack"); |
| 5460 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5461 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5462 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5463 | Address OnStackAddr(OnStackPtr, |
| 5464 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5465 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5466 | // All stack slots are multiples of 8 bytes. |
| 5467 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5468 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5469 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5470 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5471 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5472 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5473 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5474 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5475 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5476 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5477 | |
| 5478 | // Write the new value of __stack for the next call to va_arg |
| 5479 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5480 | |
| 5481 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5482 | TyInfo.first < StackSlotSize) { |
| 5483 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 5484 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5487 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5488 | |
| 5489 | CGF.EmitBranch(ContBlock); |
| 5490 | |
| 5491 | //======================================= |
| 5492 | // Tidy up |
| 5493 | //======================================= |
| 5494 | CGF.EmitBlock(ContBlock); |
| 5495 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5496 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5497 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5498 | |
| 5499 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5500 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 5501 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5502 | |
| 5503 | return ResAddr; |
| 5504 | } |
| 5505 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5506 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5507 | CodeGenFunction &CGF) const { |
| 5508 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5509 | // illegal vector types. Lower VAArg here for these cases and use |
| 5510 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5511 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5512 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5513 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5514 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5515 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5516 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5517 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5518 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5519 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5520 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5523 | // The size of the actual thing passed, which might end up just |
| 5524 | // being a pointer for indirect types. |
| 5525 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5526 | |
| 5527 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5528 | // aggregates should be passed indirectly. |
| 5529 | bool IsIndirect = false; |
| 5530 | if (TyInfo.first.getQuantity() > 16) { |
| 5531 | const Type *Base = nullptr; |
| 5532 | uint64_t Members = 0; |
| 5533 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5536 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5537 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5540 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5541 | QualType Ty) const { |
| 5542 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5543 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5544 | CharUnits::fromQuantity(8), |
| 5545 | /*allowHigherAlign*/ false); |
| 5546 | } |
| 5547 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5548 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5549 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5550 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5551 | |
| 5552 | namespace { |
| 5553 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5554 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5555 | public: |
| 5556 | enum ABIKind { |
| 5557 | APCS = 0, |
| 5558 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5559 | AAPCS_VFP = 2, |
| 5560 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5561 | }; |
| 5562 | |
| 5563 | private: |
| 5564 | ABIKind Kind; |
| 5565 | |
| 5566 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5567 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5568 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5569 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5570 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5571 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5572 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5573 | switch (getTarget().getTriple().getEnvironment()) { |
| 5574 | case llvm::Triple::Android: |
| 5575 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5576 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5577 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5578 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5579 | case llvm::Triple::MuslEABI: |
| 5580 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5581 | return true; |
| 5582 | default: |
| 5583 | return false; |
| 5584 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5585 | } |
| 5586 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5587 | bool isEABIHF() const { |
| 5588 | switch (getTarget().getTriple().getEnvironment()) { |
| 5589 | case llvm::Triple::EABIHF: |
| 5590 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5591 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5592 | return true; |
| 5593 | default: |
| 5594 | return false; |
| 5595 | } |
| 5596 | } |
| 5597 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5598 | ABIKind getABIKind() const { return Kind; } |
| 5599 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5600 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 5601 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5602 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5603 | ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, |
| 5604 | uint64_t Members) const; |
| 5605 | ABIArgInfo coerceIllegalVector(QualType Ty) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5606 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5607 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5608 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5609 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5610 | uint64_t Members) const override; |
| 5611 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5612 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5613 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5614 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5615 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5616 | |
| 5617 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5618 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5619 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5620 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5621 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5622 | bool asReturnValue) const override { |
| 5623 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5624 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5625 | bool isSwiftErrorInRegister() const override { |
| 5626 | return true; |
| 5627 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5628 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5629 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5630 | }; |
| 5631 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5632 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5633 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5634 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5635 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5636 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5637 | const ARMABIInfo &getABIInfo() const { |
| 5638 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5639 | } |
| 5640 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5641 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5642 | return 13; |
| 5643 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5644 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5645 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5646 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5649 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5650 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5651 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5652 | |
| 5653 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5654 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5655 | return false; |
| 5656 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5657 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5658 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5659 | if (getABIInfo().isEABI()) return 88; |
| 5660 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5661 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5662 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5663 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5664 | CodeGen::CodeGenModule &CGM) const override { |
| 5665 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5666 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5667 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5668 | if (!FD) |
| 5669 | return; |
| 5670 | |
| 5671 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5672 | if (!Attr) |
| 5673 | return; |
| 5674 | |
| 5675 | const char *Kind; |
| 5676 | switch (Attr->getInterrupt()) { |
| 5677 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5678 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5679 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5680 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5681 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5682 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5683 | } |
| 5684 | |
| 5685 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5686 | |
| 5687 | Fn->addFnAttr("interrupt", Kind); |
| 5688 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5689 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5690 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5691 | return; |
| 5692 | |
| 5693 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5694 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5695 | // the backend to perform a realignment as part of the function prologue. |
| 5696 | llvm::AttrBuilder B; |
| 5697 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5698 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5699 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5700 | }; |
| 5701 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5702 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5703 | public: |
| 5704 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5705 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5706 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5707 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5708 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5709 | |
| 5710 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5711 | llvm::SmallString<24> &Opt) const override { |
| 5712 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5713 | } |
| 5714 | |
| 5715 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5716 | llvm::SmallString<32> &Opt) const override { |
| 5717 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5718 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5719 | }; |
| 5720 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5721 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5722 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5723 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5724 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5725 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 5726 | addStackProbeTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5727 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5728 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5729 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5730 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5731 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5732 | FI.getReturnInfo() = |
| 5733 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5734 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5735 | for (auto &I : FI.arguments()) |
| 5736 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5737 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5738 | // Always honor user-specified calling convention. |
| 5739 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5740 | return; |
| 5741 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5742 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5743 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5744 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5745 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5746 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5747 | /// Return the default calling convention that LLVM will use. |
| 5748 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5749 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5750 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5751 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5752 | else if (isEABI()) |
| 5753 | return llvm::CallingConv::ARM_AAPCS; |
| 5754 | else |
| 5755 | return llvm::CallingConv::ARM_APCS; |
| 5756 | } |
| 5757 | |
| 5758 | /// Return the calling convention that our ABI would like us to use |
| 5759 | /// as the C calling convention. |
| 5760 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5761 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5762 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5763 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5764 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5765 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5766 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5767 | llvm_unreachable("bad ABI kind"); |
| 5768 | } |
| 5769 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5770 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5771 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5772 | |
| 5773 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5774 | // they'd just match what LLVM will infer from the triple. |
| 5775 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5776 | if (abiCC != getLLVMDefaultCC()) |
| 5777 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5778 | } |
| 5779 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5780 | ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { |
| 5781 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5782 | if (Size <= 32) { |
| 5783 | llvm::Type *ResType = |
| 5784 | llvm::Type::getInt32Ty(getVMContext()); |
| 5785 | return ABIArgInfo::getDirect(ResType); |
| 5786 | } |
| 5787 | if (Size == 64 || Size == 128) { |
| 5788 | llvm::Type *ResType = llvm::VectorType::get( |
| 5789 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5790 | return ABIArgInfo::getDirect(ResType); |
| 5791 | } |
| 5792 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5793 | } |
| 5794 | |
| 5795 | ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, |
| 5796 | const Type *Base, |
| 5797 | uint64_t Members) const { |
| 5798 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 5799 | // Base can be a floating-point or a vector. |
| 5800 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 5801 | // FP16 vectors should be converted to integer vectors |
| 5802 | if (!getTarget().hasLegalHalfType() && |
| 5803 | (VT->getElementType()->isFloat16Type() || |
| 5804 | VT->getElementType()->isHalfType())) { |
| 5805 | uint64_t Size = getContext().getTypeSize(VT); |
| 5806 | llvm::Type *NewVecTy = llvm::VectorType::get( |
| 5807 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5808 | llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); |
| 5809 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5810 | } |
| 5811 | } |
| 5812 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 5813 | } |
| 5814 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5815 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 5816 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5817 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5818 | // A single-precision floating-point type (including promoted |
| 5819 | // half-precision types); A double-precision floating-point type; |
| 5820 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5821 | // with a Base Type of a single- or double-precision floating-point type, |
| 5822 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5823 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5824 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5825 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5826 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5827 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5828 | // Handle illegal vector types here. |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5829 | if (isIllegalVectorType(Ty)) |
| 5830 | return coerceIllegalVector(Ty); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5831 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5832 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5833 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5834 | // half type natively, and does not need to interwork with AAPCS code. |
| 5835 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5836 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5837 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5838 | llvm::Type::getFloatTy(getVMContext()) : |
| 5839 | llvm::Type::getInt32Ty(getVMContext()); |
| 5840 | return ABIArgInfo::getDirect(ResType); |
| 5841 | } |
| 5842 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5843 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5844 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5845 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5846 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5847 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5848 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5849 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5850 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5851 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5852 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5853 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5854 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5855 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5856 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5857 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5858 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5859 | return ABIArgInfo::getIgnore(); |
| 5860 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5861 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5862 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5863 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5864 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5865 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5866 | if (isHomogeneousAggregate(Ty, Base, Members)) |
| 5867 | return classifyHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5868 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5869 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5870 | // this convention even for a variadic function: the backend will use GPRs |
| 5871 | // if needed. |
| 5872 | const Type *Base = nullptr; |
| 5873 | uint64_t Members = 0; |
| 5874 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5875 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5876 | llvm::Type *Ty = |
| 5877 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5878 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5879 | } |
| 5880 | } |
| 5881 | |
| 5882 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5883 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5884 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5885 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5886 | // and a pointer is passed. |
| 5887 | return ABIArgInfo::getIndirect( |
| 5888 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5891 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5892 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5893 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5894 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5895 | uint64_t ABIAlign = 4; |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5896 | uint64_t TyAlign; |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5897 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5898 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5899 | TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5900 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5901 | } else { |
| 5902 | TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 5903 | } |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5904 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5905 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5906 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5907 | /*ByVal=*/true, |
| 5908 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5911 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5912 | // same size and alignment. |
| 5913 | if (getTarget().isRenderScriptTarget()) { |
| 5914 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5915 | } |
| 5916 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5917 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5918 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5919 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5920 | // FIXME: Try to match the types of the arguments more accurately where |
| 5921 | // we can. |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5922 | if (TyAlign <= 4) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5923 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5924 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5925 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5926 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5927 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5928 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5929 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5930 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5933 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5934 | llvm::LLVMContext &VMContext) { |
| 5935 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5936 | // is called integer-like if its size is less than or equal to one word, and |
| 5937 | // the offset of each of its addressable sub-fields is zero. |
| 5938 | |
| 5939 | uint64_t Size = Context.getTypeSize(Ty); |
| 5940 | |
| 5941 | // Check that the type fits in a word. |
| 5942 | if (Size > 32) |
| 5943 | return false; |
| 5944 | |
| 5945 | // FIXME: Handle vector types! |
| 5946 | if (Ty->isVectorType()) |
| 5947 | return false; |
| 5948 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5949 | // Float types are never treated as "integer like". |
| 5950 | if (Ty->isRealFloatingType()) |
| 5951 | return false; |
| 5952 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5953 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5954 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5955 | return true; |
| 5956 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5957 | // Small complex integer types are "integer like". |
| 5958 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5959 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5960 | |
| 5961 | // Single element and zero sized arrays should be allowed, by the definition |
| 5962 | // above, but they are not. |
| 5963 | |
| 5964 | // Otherwise, it must be a record type. |
| 5965 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5966 | if (!RT) return false; |
| 5967 | |
| 5968 | // Ignore records with flexible arrays. |
| 5969 | const RecordDecl *RD = RT->getDecl(); |
| 5970 | if (RD->hasFlexibleArrayMember()) |
| 5971 | return false; |
| 5972 | |
| 5973 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5974 | // like". |
| 5975 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5976 | |
| 5977 | bool HadField = false; |
| 5978 | unsigned idx = 0; |
| 5979 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5980 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5981 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5982 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5983 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5984 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5985 | // struct { int : 0; int x } |
| 5986 | // is non-integer like according to gcc. |
| 5987 | if (FD->isBitField()) { |
| 5988 | if (!RD->isUnion()) |
| 5989 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5990 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5991 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5992 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5993 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5994 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5997 | // Check if this field is at offset 0. |
| 5998 | if (Layout.getFieldOffset(idx) != 0) |
| 5999 | return false; |
| 6000 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6001 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6002 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 6003 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6004 | // Only allow at most one field in a structure. This doesn't match the |
| 6005 | // wording above, but follows gcc in situations with a field following an |
| 6006 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6007 | if (!RD->isUnion()) { |
| 6008 | if (HadField) |
| 6009 | return false; |
| 6010 | |
| 6011 | HadField = true; |
| 6012 | } |
| 6013 | } |
| 6014 | |
| 6015 | return true; |
| 6016 | } |
| 6017 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6018 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 6019 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6020 | bool IsEffectivelyAAPCS_VFP = |
| 6021 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6022 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6023 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6024 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6025 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6026 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| 6027 | // Large vector types should be returned via memory. |
| 6028 | if (getContext().getTypeSize(RetTy) > 128) |
| 6029 | return getNaturalAlignIndirect(RetTy); |
| 6030 | // FP16 vectors should be converted to integer vectors |
| 6031 | if (!getTarget().hasLegalHalfType() && |
| 6032 | (VT->getElementType()->isFloat16Type() || |
| 6033 | VT->getElementType()->isHalfType())) |
| 6034 | return coerceIllegalVector(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6035 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 6036 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 6037 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 6038 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 6039 | // half type natively, and does not need to interwork with AAPCS code. |
| 6040 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 6041 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 6042 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 6043 | llvm::Type::getFloatTy(getVMContext()) : |
| 6044 | llvm::Type::getInt32Ty(getVMContext()); |
| 6045 | return ABIArgInfo::getDirect(ResType); |
| 6046 | } |
| 6047 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 6048 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6049 | // Treat an enum type as its underlying type. |
| 6050 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6051 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6052 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6053 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6054 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6055 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6056 | |
| 6057 | // Are we following APCS? |
| 6058 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6059 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6060 | return ABIArgInfo::getIgnore(); |
| 6061 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6062 | // Complex types are all returned as packed integers. |
| 6063 | // |
| 6064 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 6065 | // correctly. |
| 6066 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6067 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 6068 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6069 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6070 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6071 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6072 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6073 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6074 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6075 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6076 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6077 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6078 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
| 6081 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6082 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6083 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6084 | |
| 6085 | // Otherwise this is an AAPCS variant. |
| 6086 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6087 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6088 | return ABIArgInfo::getIgnore(); |
| 6089 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6090 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6091 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6092 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6093 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6094 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
| 6095 | return classifyHomogeneousAggregate(RetTy, Base, Members); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6098 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 6099 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6100 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6101 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 6102 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 6103 | // same size and alignment. |
| 6104 | if (getTarget().isRenderScriptTarget()) { |
| 6105 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 6106 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6107 | if (getDataLayout().isBigEndian()) |
| 6108 | // 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] | 6109 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6110 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6111 | // Return in the smallest viable integer type. |
| 6112 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6113 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6114 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6115 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6116 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6117 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6118 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6119 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6120 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6121 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6124 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6125 | } |
| 6126 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6127 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6128 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6129 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6130 | // On targets that don't support FP16, FP16 is expanded into float, and we |
| 6131 | // don't want the ABI to depend on whether or not FP16 is supported in |
| 6132 | // hardware. Thus return false to coerce FP16 vectors into integer vectors. |
| 6133 | if (!getTarget().hasLegalHalfType() && |
| 6134 | (VT->getElementType()->isFloat16Type() || |
| 6135 | VT->getElementType()->isHalfType())) |
| 6136 | return true; |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6137 | if (isAndroid()) { |
| 6138 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6139 | // vector ABI. The primary differences were that 3-element vector types |
| 6140 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6141 | // accepts that legacy behavior for Android only. |
| 6142 | // Check whether VT is legal. |
| 6143 | unsigned NumElements = VT->getNumElements(); |
| 6144 | // NumElements should be power of 2 or equal to 3. |
| 6145 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6146 | return true; |
| 6147 | } else { |
| 6148 | // Check whether VT is legal. |
| 6149 | unsigned NumElements = VT->getNumElements(); |
| 6150 | uint64_t Size = getContext().getTypeSize(VT); |
| 6151 | // NumElements should be power of 2. |
| 6152 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6153 | return true; |
| 6154 | // Size should be greater than 32 bits. |
| 6155 | return Size <= 32; |
| 6156 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6157 | } |
| 6158 | return false; |
| 6159 | } |
| 6160 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6161 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6162 | llvm::Type *eltTy, |
| 6163 | unsigned numElts) const { |
| 6164 | if (!llvm::isPowerOf2_32(numElts)) |
| 6165 | return false; |
| 6166 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6167 | if (size > 64) |
| 6168 | return false; |
| 6169 | if (vectorSize.getQuantity() != 8 && |
| 6170 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6171 | return false; |
| 6172 | return true; |
| 6173 | } |
| 6174 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6175 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6176 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6177 | // double, or 64-bit or 128-bit vectors. |
| 6178 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6179 | if (BT->getKind() == BuiltinType::Float || |
| 6180 | BT->getKind() == BuiltinType::Double || |
| 6181 | BT->getKind() == BuiltinType::LongDouble) |
| 6182 | return true; |
| 6183 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6184 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6185 | if (VecSize == 64 || VecSize == 128) |
| 6186 | return true; |
| 6187 | } |
| 6188 | return false; |
| 6189 | } |
| 6190 | |
| 6191 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6192 | uint64_t Members) const { |
| 6193 | return Members <= 4; |
| 6194 | } |
| 6195 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6196 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6197 | QualType Ty) const { |
| 6198 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6199 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6200 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6201 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6202 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6203 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6204 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6207 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 6208 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6209 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6210 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6211 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6212 | const Type *Base = nullptr; |
| 6213 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6214 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 6215 | IsIndirect = true; |
| 6216 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6217 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6218 | // allocated by the caller. |
| 6219 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 6220 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6221 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6222 | IsIndirect = true; |
| 6223 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6224 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6225 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6226 | // 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] | 6227 | // Our callers should be prepared to handle an under-aligned address. |
| 6228 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6229 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6230 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6231 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6232 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6233 | // ARMv7k allows type alignment up to 16 bytes. |
| 6234 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6235 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6236 | } else { |
| 6237 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6238 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6239 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6240 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6241 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6242 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6243 | } |
| 6244 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6245 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6246 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6247 | //===----------------------------------------------------------------------===// |
| 6248 | |
| 6249 | namespace { |
| 6250 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6251 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6252 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6253 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6254 | |
| 6255 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6256 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6257 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6258 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6259 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6260 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6261 | }; |
| 6262 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6263 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6264 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6265 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6266 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6267 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6268 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6269 | CodeGen::CodeGenModule &M) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6270 | bool shouldEmitStaticExternCAliases() const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6271 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6272 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6273 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6274 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6275 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6276 | }; |
| 6277 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6278 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6279 | if (RetTy->isVoidType()) |
| 6280 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6281 | |
| 6282 | // note: this is different from default ABI |
| 6283 | if (!RetTy->isScalarType()) |
| 6284 | return ABIArgInfo::getDirect(); |
| 6285 | |
| 6286 | // Treat an enum type as its underlying type. |
| 6287 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6288 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6289 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6290 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6291 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6294 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6295 | // Treat an enum type as its underlying type. |
| 6296 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6297 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6298 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6299 | // Return aggregates type as indirect by value |
| 6300 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6301 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6302 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6303 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6304 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6307 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6308 | if (!getCXXABI().classifyReturnType(FI)) |
| 6309 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6310 | for (auto &I : FI.arguments()) |
| 6311 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6312 | |
| 6313 | // Always honor user-specified calling convention. |
| 6314 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6315 | return; |
| 6316 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6317 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6318 | } |
| 6319 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6320 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6321 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6322 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6323 | } |
| 6324 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6325 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6326 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6327 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6328 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6329 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6330 | if (!FD) return; |
| 6331 | |
| 6332 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6333 | |
| 6334 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6335 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6336 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6337 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6338 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6339 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6340 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6341 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6342 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6343 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6344 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6345 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6346 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6347 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6348 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6349 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6350 | // __global__ functions cannot be called from the device, we do not |
| 6351 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6352 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6353 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6354 | addNVVMMetadata(F, "kernel", 1); |
| 6355 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6356 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6357 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6358 | llvm::APSInt MaxThreads(32); |
| 6359 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6360 | if (MaxThreads > 0) |
| 6361 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6362 | |
| 6363 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6364 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6365 | // we don't have to add a PTX directive. |
| 6366 | if (Attr->getMinBlocks()) { |
| 6367 | llvm::APSInt MinBlocks(32); |
| 6368 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6369 | if (MinBlocks > 0) |
| 6370 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6371 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6372 | } |
| 6373 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6374 | } |
| 6375 | } |
| 6376 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6377 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6378 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6379 | llvm::Module *M = F->getParent(); |
| 6380 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6381 | |
| 6382 | // Get "nvvm.annotations" metadata node |
| 6383 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6384 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6385 | llvm::Metadata *MDVals[] = { |
| 6386 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6387 | llvm::ConstantAsMetadata::get( |
| 6388 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6389 | // Append metadata to nvvm.annotations |
| 6390 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6391 | } |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6392 | |
| 6393 | bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 6394 | return false; |
| 6395 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6396 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6397 | |
| 6398 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6399 | // SystemZ ABI Implementation |
| 6400 | //===----------------------------------------------------------------------===// |
| 6401 | |
| 6402 | namespace { |
| 6403 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6404 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6405 | bool HasVector; |
| 6406 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6407 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6408 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6409 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6410 | |
| 6411 | bool isPromotableIntegerType(QualType Ty) const; |
| 6412 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6413 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6414 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6415 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6416 | |
| 6417 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6418 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6419 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6420 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6421 | if (!getCXXABI().classifyReturnType(FI)) |
| 6422 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6423 | for (auto &I : FI.arguments()) |
| 6424 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6427 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6428 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6429 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6430 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6431 | bool asReturnValue) const override { |
| 6432 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6433 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6434 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6435 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6436 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6437 | }; |
| 6438 | |
| 6439 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6440 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6441 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6442 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6443 | }; |
| 6444 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6445 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6446 | |
| 6447 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6448 | // Treat an enum type as its underlying type. |
| 6449 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6450 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6451 | |
| 6452 | // Promotable integer types are required to be promoted by the ABI. |
| 6453 | if (Ty->isPromotableIntegerType()) |
| 6454 | return true; |
| 6455 | |
| 6456 | // 32-bit values must also be promoted. |
| 6457 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6458 | switch (BT->getKind()) { |
| 6459 | case BuiltinType::Int: |
| 6460 | case BuiltinType::UInt: |
| 6461 | return true; |
| 6462 | default: |
| 6463 | return false; |
| 6464 | } |
| 6465 | return false; |
| 6466 | } |
| 6467 | |
| 6468 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6469 | return (Ty->isAnyComplexType() || |
| 6470 | Ty->isVectorType() || |
| 6471 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6472 | } |
| 6473 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6474 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6475 | return (HasVector && |
| 6476 | Ty->isVectorType() && |
| 6477 | getContext().getTypeSize(Ty) <= 128); |
| 6478 | } |
| 6479 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6480 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6481 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6482 | switch (BT->getKind()) { |
| 6483 | case BuiltinType::Float: |
| 6484 | case BuiltinType::Double: |
| 6485 | return true; |
| 6486 | default: |
| 6487 | return false; |
| 6488 | } |
| 6489 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6490 | return false; |
| 6491 | } |
| 6492 | |
| 6493 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6494 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6495 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6496 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6497 | |
| 6498 | // If this is a C++ record, check the bases first. |
| 6499 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6500 | for (const auto &I : CXXRD->bases()) { |
| 6501 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6502 | |
| 6503 | // Empty bases don't affect things either way. |
| 6504 | if (isEmptyRecord(getContext(), Base, true)) |
| 6505 | continue; |
| 6506 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6507 | if (!Found.isNull()) |
| 6508 | return Ty; |
| 6509 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6513 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6514 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6515 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6516 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6517 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 6518 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6519 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6520 | |
| 6521 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6522 | // Nested structures still do though. |
| 6523 | if (!Found.isNull()) |
| 6524 | return Ty; |
| 6525 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
| 6528 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6529 | // 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] | 6530 | if (!Found.isNull()) |
| 6531 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6532 | } |
| 6533 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6534 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6537 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6538 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6539 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6540 | // struct { |
| 6541 | // i64 __gpr; |
| 6542 | // i64 __fpr; |
| 6543 | // i8 *__overflow_arg_area; |
| 6544 | // i8 *__reg_save_area; |
| 6545 | // }; |
| 6546 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6547 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6548 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6549 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6550 | Ty = getContext().getCanonicalType(Ty); |
| 6551 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6552 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6553 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6554 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6555 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6556 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6557 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6558 | CharUnits UnpaddedSize; |
| 6559 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6560 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6561 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6562 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6563 | } else { |
| 6564 | if (AI.getCoerceToType()) |
| 6565 | ArgTy = AI.getCoerceToType(); |
| 6566 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6567 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6568 | UnpaddedSize = TyInfo.first; |
| 6569 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6570 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6571 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6572 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6573 | PaddedSize = CharUnits::fromQuantity(16); |
| 6574 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6575 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6576 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6577 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6578 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6579 | llvm::Value *PaddedSizeV = |
| 6580 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6581 | |
| 6582 | if (IsVector) { |
| 6583 | // Work out the address of a vector argument on the stack. |
| 6584 | // Vector arguments are always passed in the high bits of a |
| 6585 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6586 | Address OverflowArgAreaPtr = |
| 6587 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6588 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6589 | Address OverflowArgArea = |
| 6590 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6591 | TyInfo.second); |
| 6592 | Address MemAddr = |
| 6593 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6594 | |
| 6595 | // Update overflow_arg_area_ptr pointer |
| 6596 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6597 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6598 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6599 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6600 | |
| 6601 | return MemAddr; |
| 6602 | } |
| 6603 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6604 | assert(PaddedSize.getQuantity() == 8); |
| 6605 | |
| 6606 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6607 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6608 | if (InFPRs) { |
| 6609 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6610 | RegCountField = 1; // __fpr |
| 6611 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6612 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6613 | } else { |
| 6614 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6615 | RegCountField = 0; // __gpr |
| 6616 | RegSaveIndex = 2; // save offset for r2 |
| 6617 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6618 | } |
| 6619 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6620 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 6621 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 6622 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6623 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6624 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6625 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6626 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6627 | |
| 6628 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6629 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6630 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6631 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6632 | |
| 6633 | // Emit code to load the value if it was passed in registers. |
| 6634 | CGF.EmitBlock(InRegBlock); |
| 6635 | |
| 6636 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6637 | llvm::Value *ScaledRegCount = |
| 6638 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6639 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6640 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6641 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6642 | llvm::Value *RegOffset = |
| 6643 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6644 | Address RegSaveAreaPtr = |
| 6645 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 6646 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6647 | llvm::Value *RegSaveArea = |
| 6648 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6649 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6650 | "raw_reg_addr"), |
| 6651 | PaddedSize); |
| 6652 | Address RegAddr = |
| 6653 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6654 | |
| 6655 | // Update the register count |
| 6656 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6657 | llvm::Value *NewRegCount = |
| 6658 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6659 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6660 | CGF.EmitBranch(ContBlock); |
| 6661 | |
| 6662 | // Emit code to load the value if it was passed in memory. |
| 6663 | CGF.EmitBlock(InMemBlock); |
| 6664 | |
| 6665 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6666 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 6667 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 6668 | Address OverflowArgArea = |
| 6669 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6670 | PaddedSize); |
| 6671 | Address RawMemAddr = |
| 6672 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6673 | Address MemAddr = |
| 6674 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6675 | |
| 6676 | // Update overflow_arg_area_ptr pointer |
| 6677 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6678 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6679 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6680 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6681 | CGF.EmitBranch(ContBlock); |
| 6682 | |
| 6683 | // Return the appropriate result. |
| 6684 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6685 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6686 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6687 | |
| 6688 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6689 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6690 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6691 | |
| 6692 | return ResAddr; |
| 6693 | } |
| 6694 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6695 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6696 | if (RetTy->isVoidType()) |
| 6697 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6698 | if (isVectorArgumentType(RetTy)) |
| 6699 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6700 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6701 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6702 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6703 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6707 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6708 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6709 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6710 | |
| 6711 | // Integers and enums are extended to full register width. |
| 6712 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6713 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6714 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6715 | // Handle vector types and vector-like structure types. Note that |
| 6716 | // as opposed to float-like structure types, we do not allow any |
| 6717 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6718 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6719 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6720 | if (isVectorArgumentType(SingleElementTy) && |
| 6721 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6722 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6723 | |
| 6724 | // 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] | 6725 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6726 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6727 | |
| 6728 | // Handle small structures. |
| 6729 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6730 | // Structures with flexible arrays have variable length, so really |
| 6731 | // fail the size test above. |
| 6732 | const RecordDecl *RD = RT->getDecl(); |
| 6733 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6734 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6735 | |
| 6736 | // The structure is passed as an unextended integer, a float, or a double. |
| 6737 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6738 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6739 | assert(Size == 32 || Size == 64); |
| 6740 | if (Size == 32) |
| 6741 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6742 | else |
| 6743 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6744 | } else |
| 6745 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6746 | return ABIArgInfo::getDirect(PassTy); |
| 6747 | } |
| 6748 | |
| 6749 | // Non-structure compounds are passed indirectly. |
| 6750 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6751 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6752 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6753 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6754 | } |
| 6755 | |
| 6756 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6757 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6758 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6759 | |
| 6760 | namespace { |
| 6761 | |
| 6762 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6763 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6764 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6765 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6766 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6767 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6768 | }; |
| 6769 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6770 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6771 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6772 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6773 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6774 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6775 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6776 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6777 | const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); |
| 6778 | if (!InterruptAttr) |
| 6779 | return; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6780 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6781 | // Handle 'interrupt' attribute: |
| 6782 | llvm::Function *F = cast<llvm::Function>(GV); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6783 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6784 | // Step 1: Set ISR calling convention. |
| 6785 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6786 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6787 | // Step 2: Add attributes goodness. |
| 6788 | F->addFnAttr(llvm::Attribute::NoInline); |
| 6789 | F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6790 | } |
| 6791 | } |
| 6792 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6793 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6794 | // MIPS ABI Implementation. This works for both little-endian and |
| 6795 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6796 | //===----------------------------------------------------------------------===// |
| 6797 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6798 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6799 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6800 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6801 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6802 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6803 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6804 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6805 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6806 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6807 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6808 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6809 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6810 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6811 | |
| 6812 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6813 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6814 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6815 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6816 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6817 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6818 | }; |
| 6819 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6820 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6821 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6822 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6823 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6824 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6825 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6826 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6827 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6828 | return 29; |
| 6829 | } |
| 6830 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6831 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6832 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6833 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6834 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 6835 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6836 | |
| 6837 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 6838 | Fn->addFnAttr("long-call"); |
| 6839 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 6840 | Fn->addFnAttr("short-call"); |
| 6841 | |
| 6842 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6843 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6844 | return; |
| 6845 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6846 | if (FD->hasAttr<Mips16Attr>()) { |
| 6847 | Fn->addFnAttr("mips16"); |
| 6848 | } |
| 6849 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 6850 | Fn->addFnAttr("nomips16"); |
| 6851 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6852 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6853 | if (FD->hasAttr<MicroMipsAttr>()) |
| 6854 | Fn->addFnAttr("micromips"); |
| 6855 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 6856 | Fn->addFnAttr("nomicromips"); |
| 6857 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6858 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 6859 | if (!Attr) |
| 6860 | return; |
| 6861 | |
| 6862 | const char *Kind; |
| 6863 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 6864 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 6865 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 6866 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 6867 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 6868 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 6869 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 6870 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 6871 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 6872 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 6873 | } |
| 6874 | |
| 6875 | Fn->addFnAttr("interrupt", Kind); |
| 6876 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 6877 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 6878 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6879 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6880 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6881 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6882 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6883 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6884 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6885 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6886 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6887 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6888 | void MipsABIInfo::CoerceToIntArgs( |
| 6889 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6890 | llvm::IntegerType *IntTy = |
| 6891 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6892 | |
| 6893 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6894 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6895 | ArgList.push_back(IntTy); |
| 6896 | |
| 6897 | // If necessary, add one more integer type to ArgList. |
| 6898 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6899 | |
| 6900 | if (R) |
| 6901 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6902 | } |
| 6903 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6904 | // In N32/64, an aligned double precision floating point field is passed in |
| 6905 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6906 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6907 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6908 | |
| 6909 | if (IsO32) { |
| 6910 | CoerceToIntArgs(TySize, ArgList); |
| 6911 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6912 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6913 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6914 | if (Ty->isComplexType()) |
| 6915 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6916 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6917 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6918 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6919 | // Unions/vectors are passed in integer registers. |
| 6920 | if (!RT || !RT->isStructureOrClassType()) { |
| 6921 | CoerceToIntArgs(TySize, ArgList); |
| 6922 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6923 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6924 | |
| 6925 | const RecordDecl *RD = RT->getDecl(); |
| 6926 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6927 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6928 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6929 | uint64_t LastOffset = 0; |
| 6930 | unsigned idx = 0; |
| 6931 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6932 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6933 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6934 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6935 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6936 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6937 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6938 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6939 | |
| 6940 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6941 | continue; |
| 6942 | |
| 6943 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6944 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6945 | continue; |
| 6946 | |
| 6947 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6948 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6949 | ArgList.push_back(I64); |
| 6950 | |
| 6951 | // Add double type. |
| 6952 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6953 | LastOffset = Offset + 64; |
| 6954 | } |
| 6955 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6956 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6957 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6958 | |
| 6959 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6960 | } |
| 6961 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6962 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6963 | uint64_t Offset) const { |
| 6964 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6965 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6966 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6967 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6968 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6969 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6970 | ABIArgInfo |
| 6971 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6972 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6973 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6974 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6975 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6976 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6977 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6978 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6979 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6980 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6981 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6982 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6983 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6984 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6985 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6986 | return ABIArgInfo::getIgnore(); |
| 6987 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6988 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6989 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6990 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6991 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6992 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6993 | // If we have reached here, aggregates are passed directly by coercing to |
| 6994 | // another structure type. Padding is inserted if the offset of the |
| 6995 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6996 | ABIArgInfo ArgInfo = |
| 6997 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6998 | getPaddingType(OrigOffset, CurrOffset)); |
| 6999 | ArgInfo.setInReg(true); |
| 7000 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
| 7003 | // Treat an enum type as its underlying type. |
| 7004 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7005 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7006 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 7007 | // All integral types are promoted to the GPR width. |
| 7008 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7009 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7010 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7011 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7012 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7013 | } |
| 7014 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7015 | llvm::Type* |
| 7016 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7017 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7018 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7019 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7020 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7021 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7022 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 7023 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7024 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7025 | // N32/64 returns struct/classes in floating point registers if the |
| 7026 | // following conditions are met: |
| 7027 | // 1. The size of the struct/class is no larger than 128-bit. |
| 7028 | // 2. The struct/class has one or two fields all of which are floating |
| 7029 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7030 | // 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] | 7031 | // |
| 7032 | // Any other composite results are returned in integer registers. |
| 7033 | // |
| 7034 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 7035 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 7036 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7037 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7038 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7039 | if (!BT || !BT->isFloatingPoint()) |
| 7040 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7041 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7042 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
| 7045 | if (b == e) |
| 7046 | return llvm::StructType::get(getVMContext(), RTList, |
| 7047 | RD->hasAttr<PackedAttr>()); |
| 7048 | |
| 7049 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7050 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7051 | } |
| 7052 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7053 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7054 | return llvm::StructType::get(getVMContext(), RTList); |
| 7055 | } |
| 7056 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7057 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 7058 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7059 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 7060 | if (RetTy->isVoidType()) |
| 7061 | return ABIArgInfo::getIgnore(); |
| 7062 | |
| 7063 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 7064 | // However, N32/N64 ignores zero sized return values. |
| 7065 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7066 | return ABIArgInfo::getIgnore(); |
| 7067 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 7068 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7069 | if (Size <= 128) { |
| 7070 | if (RetTy->isAnyComplexType()) |
| 7071 | return ABIArgInfo::getDirect(); |
| 7072 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7073 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 7074 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7075 | if (!IsO32 || |
| 7076 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 7077 | ABIArgInfo ArgInfo = |
| 7078 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 7079 | ArgInfo.setInReg(true); |
| 7080 | return ArgInfo; |
| 7081 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7082 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7083 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7084 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7085 | } |
| 7086 | |
| 7087 | // Treat an enum type as its underlying type. |
| 7088 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7089 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7090 | |
Stefan Maksimovic | b9da8a5 | 2018-07-30 10:44:46 +0000 | [diff] [blame] | 7091 | if (RetTy->isPromotableIntegerType()) |
| 7092 | return ABIArgInfo::getExtend(RetTy); |
| 7093 | |
| 7094 | if ((RetTy->isUnsignedIntegerOrEnumerationType() || |
| 7095 | RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) |
| 7096 | return ABIArgInfo::getSignExtend(RetTy); |
| 7097 | |
| 7098 | return ABIArgInfo::getDirect(); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7102 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7103 | if (!getCXXABI().classifyReturnType(FI)) |
| 7104 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7105 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7106 | // 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] | 7107 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7108 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7109 | for (auto &I : FI.arguments()) |
| 7110 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7113 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7114 | QualType OrigTy) const { |
| 7115 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7116 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7117 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 7118 | // 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] | 7119 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7120 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7121 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7122 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7123 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7124 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7125 | DidPromote = true; |
| 7126 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7127 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7128 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7129 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7130 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7131 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7132 | // The alignment of things in the argument area is never larger than |
| 7133 | // StackAlignInBytes. |
| 7134 | TyInfo.second = |
| 7135 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7136 | |
| 7137 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7138 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7139 | |
| 7140 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7141 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7142 | |
| 7143 | |
| 7144 | // If there was a promotion, "unpromote" into a temporary. |
| 7145 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7146 | if (DidPromote) { |
| 7147 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7148 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7149 | |
| 7150 | // Truncate down to the right width. |
| 7151 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7152 | : CGF.IntPtrTy); |
| 7153 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7154 | if (OrigTy->isPointerType()) |
| 7155 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7156 | |
| 7157 | CGF.Builder.CreateStore(V, Temp); |
| 7158 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7159 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7160 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7161 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7162 | } |
| 7163 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7164 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7165 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7166 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7167 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7168 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7169 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7170 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7171 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7172 | } |
| 7173 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7174 | bool |
| 7175 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7176 | llvm::Value *Address) const { |
| 7177 | // This information comes from gcc's implementation, which seems to |
| 7178 | // as canonical as it gets. |
| 7179 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7180 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7181 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7182 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7183 | |
| 7184 | // 0-31 are the general purpose registers, $0 - $31. |
| 7185 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7186 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7187 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7188 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7189 | |
| 7190 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7191 | // They are one bit wide and ignored here. |
| 7192 | |
| 7193 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7194 | // (coprocessor 1 is the FP unit) |
| 7195 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7196 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7197 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7198 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7199 | return false; |
| 7200 | } |
| 7201 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7202 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7203 | // AVR ABI Implementation. |
| 7204 | //===----------------------------------------------------------------------===// |
| 7205 | |
| 7206 | namespace { |
| 7207 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7208 | public: |
| 7209 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7210 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7211 | |
| 7212 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7213 | CodeGen::CodeGenModule &CGM) const override { |
| 7214 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7215 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7216 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7217 | if (!FD) return; |
| 7218 | auto *Fn = cast<llvm::Function>(GV); |
| 7219 | |
| 7220 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7221 | Fn->addFnAttr("interrupt"); |
| 7222 | |
| 7223 | if (FD->getAttr<AVRSignalAttr>()) |
| 7224 | Fn->addFnAttr("signal"); |
| 7225 | } |
| 7226 | }; |
| 7227 | } |
| 7228 | |
| 7229 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7230 | // 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] | 7231 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7232 | // handling. |
| 7233 | //===----------------------------------------------------------------------===// |
| 7234 | |
| 7235 | namespace { |
| 7236 | |
| 7237 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7238 | public: |
| 7239 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7240 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7241 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7242 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7243 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7244 | }; |
| 7245 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7246 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7247 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7248 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7249 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7250 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7251 | if (!FD) return; |
| 7252 | |
| 7253 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7254 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7255 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7256 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7257 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7258 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7259 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7260 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7261 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7262 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7263 | llvm::NamedMDNode *OpenCLMetadata = |
| 7264 | M.getModule().getOrInsertNamedMetadata( |
| 7265 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7266 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7267 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7268 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7269 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7270 | Operands.push_back( |
| 7271 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7272 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7273 | Operands.push_back( |
| 7274 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7275 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7276 | Operands.push_back( |
| 7277 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7278 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7279 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7280 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7281 | // (false) for implementing the work_group_size_hint attr later. |
| 7282 | // 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] | 7283 | Operands.push_back( |
| 7284 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7285 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7286 | } |
| 7287 | } |
| 7288 | } |
| 7289 | } |
| 7290 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7291 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7292 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7293 | //===----------------------------------------------------------------------===// |
| 7294 | // Hexagon ABI Implementation |
| 7295 | //===----------------------------------------------------------------------===// |
| 7296 | |
| 7297 | namespace { |
| 7298 | |
| 7299 | class HexagonABIInfo : public ABIInfo { |
| 7300 | |
| 7301 | |
| 7302 | public: |
| 7303 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7304 | |
| 7305 | private: |
| 7306 | |
| 7307 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7308 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7309 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7310 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7311 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7312 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7313 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7314 | }; |
| 7315 | |
| 7316 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7317 | public: |
| 7318 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7319 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7320 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7321 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7322 | return 29; |
| 7323 | } |
| 7324 | }; |
| 7325 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7326 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7327 | |
| 7328 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7329 | if (!getCXXABI().classifyReturnType(FI)) |
| 7330 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7331 | for (auto &I : FI.arguments()) |
| 7332 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7333 | } |
| 7334 | |
| 7335 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7336 | if (!isAggregateTypeForABI(Ty)) { |
| 7337 | // Treat an enum type as its underlying type. |
| 7338 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7339 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7340 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7341 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7342 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7343 | } |
| 7344 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7345 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7346 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7347 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7348 | // Ignore empty records. |
| 7349 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7350 | return ABIArgInfo::getIgnore(); |
| 7351 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7352 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7353 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7354 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7355 | // Pass in the smallest viable integer type. |
| 7356 | else if (Size > 32) |
| 7357 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7358 | else if (Size > 16) |
| 7359 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7360 | else if (Size > 8) |
| 7361 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7362 | else |
| 7363 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7364 | } |
| 7365 | |
| 7366 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7367 | if (RetTy->isVoidType()) |
| 7368 | return ABIArgInfo::getIgnore(); |
| 7369 | |
| 7370 | // Large vector types should be returned via memory. |
| 7371 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7372 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7373 | |
| 7374 | if (!isAggregateTypeForABI(RetTy)) { |
| 7375 | // Treat an enum type as its underlying type. |
| 7376 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7377 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7378 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7379 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7380 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7383 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7384 | return ABIArgInfo::getIgnore(); |
| 7385 | |
| 7386 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7387 | // are returned indirectly. |
| 7388 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7389 | if (Size <= 64) { |
| 7390 | // Return in the smallest viable integer type. |
| 7391 | if (Size <= 8) |
| 7392 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7393 | if (Size <= 16) |
| 7394 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7395 | if (Size <= 32) |
| 7396 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7397 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7398 | } |
| 7399 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7400 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7401 | } |
| 7402 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7403 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7404 | QualType Ty) const { |
| 7405 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7406 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7407 | getContext().getTypeInfoInChars(Ty), |
| 7408 | CharUnits::fromQuantity(4), |
| 7409 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7412 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7413 | // Lanai ABI Implementation |
| 7414 | //===----------------------------------------------------------------------===// |
| 7415 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7416 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7417 | class LanaiABIInfo : public DefaultABIInfo { |
| 7418 | public: |
| 7419 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7420 | |
| 7421 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7422 | |
| 7423 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7424 | CCState State(FI.getCallingConvention()); |
| 7425 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7426 | // regparm attribute set. |
| 7427 | if (FI.getHasRegParm()) { |
| 7428 | State.FreeRegs = FI.getRegParm(); |
| 7429 | } else { |
| 7430 | State.FreeRegs = 4; |
| 7431 | } |
| 7432 | |
| 7433 | if (!getCXXABI().classifyReturnType(FI)) |
| 7434 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7435 | for (auto &I : FI.arguments()) |
| 7436 | I.info = classifyArgumentType(I.type, State); |
| 7437 | } |
| 7438 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7439 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7440 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7441 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7442 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7443 | |
| 7444 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7445 | unsigned Size = getContext().getTypeSize(Ty); |
| 7446 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7447 | |
| 7448 | if (SizeInRegs == 0) |
| 7449 | return false; |
| 7450 | |
| 7451 | if (SizeInRegs > State.FreeRegs) { |
| 7452 | State.FreeRegs = 0; |
| 7453 | return false; |
| 7454 | } |
| 7455 | |
| 7456 | State.FreeRegs -= SizeInRegs; |
| 7457 | |
| 7458 | return true; |
| 7459 | } |
| 7460 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7461 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7462 | CCState &State) const { |
| 7463 | if (!ByVal) { |
| 7464 | if (State.FreeRegs) { |
| 7465 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7466 | return getNaturalAlignIndirectInReg(Ty); |
| 7467 | } |
| 7468 | return getNaturalAlignIndirect(Ty, false); |
| 7469 | } |
| 7470 | |
| 7471 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7472 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7473 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7474 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7475 | /*Realign=*/TypeAlign > |
| 7476 | MinABIStackAlignInBytes); |
| 7477 | } |
| 7478 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7479 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7480 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7481 | // Check with the C++ ABI first. |
| 7482 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7483 | if (RT) { |
| 7484 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7485 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7486 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7487 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7488 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7489 | } |
| 7490 | } |
| 7491 | |
| 7492 | if (isAggregateTypeForABI(Ty)) { |
| 7493 | // Structures with flexible arrays are always indirect. |
| 7494 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7495 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7496 | |
| 7497 | // Ignore empty structs/unions. |
| 7498 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7499 | return ABIArgInfo::getIgnore(); |
| 7500 | |
| 7501 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7502 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7503 | if (SizeInRegs <= State.FreeRegs) { |
| 7504 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7505 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7506 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7507 | State.FreeRegs -= SizeInRegs; |
| 7508 | return ABIArgInfo::getDirectInReg(Result); |
| 7509 | } else { |
| 7510 | State.FreeRegs = 0; |
| 7511 | } |
| 7512 | return getIndirectResult(Ty, true, State); |
| 7513 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7514 | |
| 7515 | // Treat an enum type as its underlying type. |
| 7516 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7517 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7518 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7519 | bool InReg = shouldUseInReg(Ty, State); |
| 7520 | if (Ty->isPromotableIntegerType()) { |
| 7521 | if (InReg) |
| 7522 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7523 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7524 | } |
| 7525 | if (InReg) |
| 7526 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7527 | return ABIArgInfo::getDirect(); |
| 7528 | } |
| 7529 | |
| 7530 | namespace { |
| 7531 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7532 | public: |
| 7533 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7534 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7535 | }; |
| 7536 | } |
| 7537 | |
| 7538 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7539 | // AMDGPU ABI Implementation |
| 7540 | //===----------------------------------------------------------------------===// |
| 7541 | |
| 7542 | namespace { |
| 7543 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7544 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7545 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7546 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7547 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7548 | unsigned numRegsForType(QualType Ty) const; |
| 7549 | |
| 7550 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7551 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7552 | uint64_t Members) const override; |
| 7553 | |
| 7554 | public: |
| 7555 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7556 | DefaultABIInfo(CGT) {} |
| 7557 | |
| 7558 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7559 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7560 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7561 | |
| 7562 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7563 | }; |
| 7564 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7565 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7566 | return true; |
| 7567 | } |
| 7568 | |
| 7569 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7570 | const Type *Base, uint64_t Members) const { |
| 7571 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7572 | |
| 7573 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7574 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7575 | } |
| 7576 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7577 | /// Estimate number of registers the type will use when passed in registers. |
| 7578 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7579 | unsigned NumRegs = 0; |
| 7580 | |
| 7581 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7582 | // Compute from the number of elements. The reported size is based on the |
| 7583 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7584 | QualType EltTy = VT->getElementType(); |
| 7585 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7586 | |
| 7587 | // 16-bit element vectors should be passed as packed. |
| 7588 | if (EltSize == 16) |
| 7589 | return (VT->getNumElements() + 1) / 2; |
| 7590 | |
| 7591 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7592 | return EltNumRegs * VT->getNumElements(); |
| 7593 | } |
| 7594 | |
| 7595 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7596 | const RecordDecl *RD = RT->getDecl(); |
| 7597 | assert(!RD->hasFlexibleArrayMember()); |
| 7598 | |
| 7599 | for (const FieldDecl *Field : RD->fields()) { |
| 7600 | QualType FieldTy = Field->getType(); |
| 7601 | NumRegs += numRegsForType(FieldTy); |
| 7602 | } |
| 7603 | |
| 7604 | return NumRegs; |
| 7605 | } |
| 7606 | |
| 7607 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7608 | } |
| 7609 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7610 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7611 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7612 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7613 | if (!getCXXABI().classifyReturnType(FI)) |
| 7614 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7615 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7616 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7617 | for (auto &Arg : FI.arguments()) { |
| 7618 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7619 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7620 | } else { |
| 7621 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7622 | } |
| 7623 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7624 | } |
| 7625 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7626 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7627 | if (isAggregateTypeForABI(RetTy)) { |
| 7628 | // Records with non-trivial destructors/copy-constructors should not be |
| 7629 | // returned by value. |
| 7630 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7631 | // Ignore empty structs/unions. |
| 7632 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7633 | return ABIArgInfo::getIgnore(); |
| 7634 | |
| 7635 | // Lower single-element structs to just return a regular value. |
| 7636 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7637 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7638 | |
| 7639 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7640 | const RecordDecl *RD = RT->getDecl(); |
| 7641 | if (RD->hasFlexibleArrayMember()) |
| 7642 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7643 | } |
| 7644 | |
| 7645 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7646 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7647 | if (Size <= 16) |
| 7648 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7649 | |
| 7650 | if (Size <= 32) |
| 7651 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7652 | |
| 7653 | if (Size <= 64) { |
| 7654 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7655 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7656 | } |
| 7657 | |
| 7658 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7659 | return ABIArgInfo::getDirect(); |
| 7660 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7661 | } |
| 7662 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7663 | // Otherwise just do the default thing. |
| 7664 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7665 | } |
| 7666 | |
| 7667 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7668 | /// make sense to pass anything byval, so everything must be direct. |
| 7669 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7670 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7671 | |
| 7672 | // TODO: Can we omit empty structs? |
| 7673 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7674 | // Coerce single element structs to its element. |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7675 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7676 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7677 | |
| 7678 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7679 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7680 | // have to set it to false here. Other args of getDirect() are just defaults. |
| 7681 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 7682 | } |
| 7683 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7684 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7685 | unsigned &NumRegsLeft) const { |
| 7686 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7687 | |
| 7688 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7689 | |
| 7690 | if (isAggregateTypeForABI(Ty)) { |
| 7691 | // Records with non-trivial destructors/copy-constructors should not be |
| 7692 | // passed by value. |
| 7693 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7694 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7695 | |
| 7696 | // Ignore empty structs/unions. |
| 7697 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7698 | return ABIArgInfo::getIgnore(); |
| 7699 | |
| 7700 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7701 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7702 | // though watch out for things like bitfields. |
| 7703 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7704 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7705 | |
| 7706 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7707 | const RecordDecl *RD = RT->getDecl(); |
| 7708 | if (RD->hasFlexibleArrayMember()) |
| 7709 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7710 | } |
| 7711 | |
| 7712 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7713 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7714 | if (Size <= 64) { |
| 7715 | unsigned NumRegs = (Size + 31) / 32; |
| 7716 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7717 | |
| 7718 | if (Size <= 16) |
| 7719 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7720 | |
| 7721 | if (Size <= 32) |
| 7722 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7723 | |
| 7724 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7725 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7726 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7727 | } |
| 7728 | |
| 7729 | if (NumRegsLeft > 0) { |
| 7730 | unsigned NumRegs = numRegsForType(Ty); |
| 7731 | if (NumRegsLeft >= NumRegs) { |
| 7732 | NumRegsLeft -= NumRegs; |
| 7733 | return ABIArgInfo::getDirect(); |
| 7734 | } |
| 7735 | } |
| 7736 | } |
| 7737 | |
| 7738 | // Otherwise just do the default thing. |
| 7739 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7740 | if (!ArgInfo.isIndirect()) { |
| 7741 | unsigned NumRegs = numRegsForType(Ty); |
| 7742 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7743 | } |
| 7744 | |
| 7745 | return ArgInfo; |
| 7746 | } |
| 7747 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7748 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7749 | public: |
| 7750 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7751 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7752 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7753 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7754 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7755 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7756 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7757 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7758 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7759 | LangAS getASTAllocaAddressSpace() const override { |
| 7760 | return getLangASFromTargetAS( |
| 7761 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7762 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7763 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7764 | const VarDecl *D) const override; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7765 | llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, |
| 7766 | llvm::LLVMContext &C) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7767 | llvm::Function * |
| 7768 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7769 | llvm::Function *BlockInvokeFunc, |
| 7770 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7771 | bool shouldEmitStaticExternCAliases() const override; |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7772 | void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7773 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7774 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7775 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7776 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7777 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7778 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7779 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7780 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7781 | if (!FD) |
| 7782 | return; |
| 7783 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7784 | llvm::Function *F = cast<llvm::Function>(GV); |
| 7785 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7786 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 7787 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7788 | |
| 7789 | if (M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>() && |
| 7790 | (M.getTriple().getOS() == llvm::Triple::AMDHSA)) |
Tony Tye | 68e11a6 | 2018-03-23 18:51:45 +0000 | [diff] [blame] | 7791 | F->addFnAttr("amdgpu-implicitarg-num-bytes", "48"); |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 7792 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 7793 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 7794 | if (ReqdWGS || FlatWGS) { |
| 7795 | unsigned Min = FlatWGS ? FlatWGS->getMin() : 0; |
| 7796 | unsigned Max = FlatWGS ? FlatWGS->getMax() : 0; |
| 7797 | if (ReqdWGS && Min == 0 && Max == 0) |
| 7798 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7799 | |
| 7800 | if (Min != 0) { |
| 7801 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 7802 | |
| 7803 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 7804 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 7805 | } else |
| 7806 | assert(Max == 0 && "Max must be zero"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7807 | } |
| 7808 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7809 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
| 7810 | unsigned Min = Attr->getMin(); |
| 7811 | unsigned Max = Attr->getMax(); |
| 7812 | |
| 7813 | if (Min != 0) { |
| 7814 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 7815 | |
| 7816 | std::string AttrVal = llvm::utostr(Min); |
| 7817 | if (Max != 0) |
| 7818 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 7819 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 7820 | } else |
| 7821 | assert(Max == 0 && "Max must be zero"); |
| 7822 | } |
| 7823 | |
| 7824 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7825 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7826 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7827 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 7828 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 7829 | } |
| 7830 | |
| 7831 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 7832 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 7833 | |
| 7834 | if (NumVGPR != 0) |
| 7835 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7836 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 7837 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7838 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7839 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 7840 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 7841 | } |
| 7842 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7843 | // Currently LLVM assumes null pointers always have value 0, |
| 7844 | // which results in incorrectly transformed IR. Therefore, instead of |
| 7845 | // emitting null pointers in private and local address spaces, a null |
| 7846 | // pointer in generic address space is emitted which is casted to a |
| 7847 | // pointer in local or private address space. |
| 7848 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 7849 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 7850 | QualType QT) const { |
| 7851 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 7852 | return llvm::ConstantPointerNull::get(PT); |
| 7853 | |
| 7854 | auto &Ctx = CGM.getContext(); |
| 7855 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 7856 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 7857 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 7858 | llvm::ConstantPointerNull::get(NPT), PT); |
| 7859 | } |
| 7860 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7861 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7862 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7863 | const VarDecl *D) const { |
| 7864 | assert(!CGM.getLangOpts().OpenCL && |
| 7865 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 7866 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7867 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 7868 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7869 | if (!D) |
| 7870 | return DefaultGlobalAS; |
| 7871 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7872 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 7873 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 7874 | if (AddrSpace != LangAS::Default) |
| 7875 | return AddrSpace; |
| 7876 | |
| 7877 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 7878 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 7879 | return ConstAS.getValue(); |
| 7880 | } |
| 7881 | return DefaultGlobalAS; |
| 7882 | } |
| 7883 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 7884 | llvm::SyncScope::ID |
| 7885 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(SyncScope S, |
| 7886 | llvm::LLVMContext &C) const { |
| 7887 | StringRef Name; |
| 7888 | switch (S) { |
| 7889 | case SyncScope::OpenCLWorkGroup: |
| 7890 | Name = "workgroup"; |
| 7891 | break; |
| 7892 | case SyncScope::OpenCLDevice: |
| 7893 | Name = "agent"; |
| 7894 | break; |
| 7895 | case SyncScope::OpenCLAllSVMDevices: |
| 7896 | Name = ""; |
| 7897 | break; |
| 7898 | case SyncScope::OpenCLSubGroup: |
| 7899 | Name = "subgroup"; |
| 7900 | } |
| 7901 | return C.getOrInsertSyncScopeID(Name); |
| 7902 | } |
| 7903 | |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7904 | bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 7905 | return false; |
| 7906 | } |
| 7907 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 7908 | void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7909 | const FunctionType *&FT) const { |
| 7910 | FT = getABIInfo().getContext().adjustFunctionType( |
| 7911 | FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 7912 | } |
| 7913 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7914 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 7915 | // SPARC v8 ABI Implementation. |
| 7916 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7917 | // |
| 7918 | // Ensures that complex values are passed in registers. |
| 7919 | // |
| 7920 | namespace { |
| 7921 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 7922 | public: |
| 7923 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7924 | |
| 7925 | private: |
| 7926 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7927 | void computeInfo(CGFunctionInfo &FI) const override; |
| 7928 | }; |
| 7929 | } // end anonymous namespace |
| 7930 | |
| 7931 | |
| 7932 | ABIArgInfo |
| 7933 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 7934 | if (Ty->isAnyComplexType()) { |
| 7935 | return ABIArgInfo::getDirect(); |
| 7936 | } |
| 7937 | else { |
| 7938 | return DefaultABIInfo::classifyReturnType(Ty); |
| 7939 | } |
| 7940 | } |
| 7941 | |
| 7942 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 7943 | |
| 7944 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7945 | for (auto &Arg : FI.arguments()) |
| 7946 | Arg.info = classifyArgumentType(Arg.type); |
| 7947 | } |
| 7948 | |
| 7949 | namespace { |
| 7950 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 7951 | public: |
| 7952 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 7953 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 7954 | }; |
| 7955 | } // end anonymous namespace |
| 7956 | |
| 7957 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7958 | // SPARC v9 ABI Implementation. |
| 7959 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 7960 | // |
| 7961 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 7962 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 7963 | // the array, structs larger than 16 bytes are passed indirectly. |
| 7964 | // |
| 7965 | // One case requires special care: |
| 7966 | // |
| 7967 | // struct mixed { |
| 7968 | // int i; |
| 7969 | // float f; |
| 7970 | // }; |
| 7971 | // |
| 7972 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 7973 | // parameter array, but the int is passed in an integer register, and the float |
| 7974 | // is passed in a floating point register. This is represented as two arguments |
| 7975 | // with the LLVM IR inreg attribute: |
| 7976 | // |
| 7977 | // declare void f(i32 inreg %i, float inreg %f) |
| 7978 | // |
| 7979 | // The code generator will only allocate 4 bytes from the parameter array for |
| 7980 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 7981 | // bytes. |
| 7982 | // |
| 7983 | namespace { |
| 7984 | class SparcV9ABIInfo : public ABIInfo { |
| 7985 | public: |
| 7986 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7987 | |
| 7988 | private: |
| 7989 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7990 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7991 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7992 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 7993 | |
| 7994 | // Coercion type builder for structs passed in registers. The coercion type |
| 7995 | // serves two purposes: |
| 7996 | // |
| 7997 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 7998 | // in registers. |
| 7999 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 8000 | // code generator knows to pass them in floating point registers. |
| 8001 | // |
| 8002 | // We also compute the InReg flag which indicates that the struct contains |
| 8003 | // aligned 32-bit floats. |
| 8004 | // |
| 8005 | struct CoerceBuilder { |
| 8006 | llvm::LLVMContext &Context; |
| 8007 | const llvm::DataLayout &DL; |
| 8008 | SmallVector<llvm::Type*, 8> Elems; |
| 8009 | uint64_t Size; |
| 8010 | bool InReg; |
| 8011 | |
| 8012 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 8013 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 8014 | |
| 8015 | // Pad Elems with integers until Size is ToSize. |
| 8016 | void pad(uint64_t ToSize) { |
| 8017 | assert(ToSize >= Size && "Cannot remove elements"); |
| 8018 | if (ToSize == Size) |
| 8019 | return; |
| 8020 | |
| 8021 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8022 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8023 | if (Aligned > Size && Aligned <= ToSize) { |
| 8024 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 8025 | Size = Aligned; |
| 8026 | } |
| 8027 | |
| 8028 | // Add whole 64-bit words. |
| 8029 | while (Size + 64 <= ToSize) { |
| 8030 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 8031 | Size += 64; |
| 8032 | } |
| 8033 | |
| 8034 | // Final in-word padding. |
| 8035 | if (Size < ToSize) { |
| 8036 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 8037 | Size = ToSize; |
| 8038 | } |
| 8039 | } |
| 8040 | |
| 8041 | // Add a floating point element at Offset. |
| 8042 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 8043 | // Unaligned floats are treated as integers. |
| 8044 | if (Offset % Bits) |
| 8045 | return; |
| 8046 | // The InReg flag is only required if there are any floats < 64 bits. |
| 8047 | if (Bits < 64) |
| 8048 | InReg = true; |
| 8049 | pad(Offset); |
| 8050 | Elems.push_back(Ty); |
| 8051 | Size = Offset + Bits; |
| 8052 | } |
| 8053 | |
| 8054 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 8055 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 8056 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 8057 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 8058 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 8059 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 8060 | switch (ElemTy->getTypeID()) { |
| 8061 | case llvm::Type::StructTyID: |
| 8062 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 8063 | break; |
| 8064 | case llvm::Type::FloatTyID: |
| 8065 | addFloat(ElemOffset, ElemTy, 32); |
| 8066 | break; |
| 8067 | case llvm::Type::DoubleTyID: |
| 8068 | addFloat(ElemOffset, ElemTy, 64); |
| 8069 | break; |
| 8070 | case llvm::Type::FP128TyID: |
| 8071 | addFloat(ElemOffset, ElemTy, 128); |
| 8072 | break; |
| 8073 | case llvm::Type::PointerTyID: |
| 8074 | if (ElemOffset % 64 == 0) { |
| 8075 | pad(ElemOffset); |
| 8076 | Elems.push_back(ElemTy); |
| 8077 | Size += 64; |
| 8078 | } |
| 8079 | break; |
| 8080 | default: |
| 8081 | break; |
| 8082 | } |
| 8083 | } |
| 8084 | } |
| 8085 | |
| 8086 | // Check if Ty is a usable substitute for the coercion type. |
| 8087 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 8088 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8089 | } |
| 8090 | |
| 8091 | // Get the coercion type as a literal struct type. |
| 8092 | llvm::Type *getType() const { |
| 8093 | if (Elems.size() == 1) |
| 8094 | return Elems.front(); |
| 8095 | else |
| 8096 | return llvm::StructType::get(Context, Elems); |
| 8097 | } |
| 8098 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8099 | }; |
| 8100 | } // end anonymous namespace |
| 8101 | |
| 8102 | ABIArgInfo |
| 8103 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 8104 | if (Ty->isVoidType()) |
| 8105 | return ABIArgInfo::getIgnore(); |
| 8106 | |
| 8107 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8108 | |
| 8109 | // Anything too big to fit in registers is passed with an explicit indirect |
| 8110 | // pointer / sret pointer. |
| 8111 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8112 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8113 | |
| 8114 | // Treat an enum type as its underlying type. |
| 8115 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8116 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8117 | |
| 8118 | // Integer types smaller than a register are extended. |
| 8119 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 8120 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8121 | |
| 8122 | // Other non-aggregates go in registers. |
| 8123 | if (!isAggregateTypeForABI(Ty)) |
| 8124 | return ABIArgInfo::getDirect(); |
| 8125 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8126 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 8127 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 8128 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8129 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8130 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8131 | // 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] | 8132 | // Build a coercion type from the LLVM struct type. |
| 8133 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 8134 | if (!StrTy) |
| 8135 | return ABIArgInfo::getDirect(); |
| 8136 | |
| 8137 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 8138 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8139 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8140 | |
| 8141 | // Try to use the original type for coercion. |
| 8142 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8143 | |
| 8144 | if (CB.InReg) |
| 8145 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8146 | else |
| 8147 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8148 | } |
| 8149 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8150 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8151 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8152 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8153 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8154 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8155 | AI.setCoerceToType(ArgTy); |
| 8156 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8157 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8158 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8159 | CGBuilderTy &Builder = CGF.Builder; |
| 8160 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8161 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8162 | |
| 8163 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8164 | |
| 8165 | Address ArgAddr = Address::invalid(); |
| 8166 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8167 | switch (AI.getKind()) { |
| 8168 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8169 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8170 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8171 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8172 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8173 | case ABIArgInfo::Extend: { |
| 8174 | Stride = SlotSize; |
| 8175 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8176 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8177 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8178 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8179 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8180 | case ABIArgInfo::Direct: { |
| 8181 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8182 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8183 | ArgAddr = Addr; |
| 8184 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8185 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8186 | |
| 8187 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8188 | Stride = SlotSize; |
| 8189 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8190 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8191 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8192 | break; |
| 8193 | |
| 8194 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8195 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8196 | } |
| 8197 | |
| 8198 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8199 | llvm::Value *NextPtr = |
| 8200 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 8201 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8202 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8203 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8204 | } |
| 8205 | |
| 8206 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8207 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8208 | for (auto &I : FI.arguments()) |
| 8209 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8210 | } |
| 8211 | |
| 8212 | namespace { |
| 8213 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8214 | public: |
| 8215 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8216 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8217 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8218 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8219 | return 14; |
| 8220 | } |
| 8221 | |
| 8222 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8223 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8224 | }; |
| 8225 | } // end anonymous namespace |
| 8226 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8227 | bool |
| 8228 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8229 | llvm::Value *Address) const { |
| 8230 | // This is calculated from the LLVM and GCC tables and verified |
| 8231 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8232 | |
| 8233 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8234 | |
| 8235 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8236 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8237 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8238 | |
| 8239 | // 0-31: the 8-byte general-purpose registers |
| 8240 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8241 | |
| 8242 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8243 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8244 | |
| 8245 | // Y = 64 |
| 8246 | // PSR = 65 |
| 8247 | // WIM = 66 |
| 8248 | // TBR = 67 |
| 8249 | // PC = 68 |
| 8250 | // NPC = 69 |
| 8251 | // FSR = 70 |
| 8252 | // CSR = 71 |
| 8253 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8254 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8255 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8256 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8257 | |
| 8258 | return false; |
| 8259 | } |
| 8260 | |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8261 | // ARC ABI implementation. |
| 8262 | namespace { |
| 8263 | |
| 8264 | class ARCABIInfo : public DefaultABIInfo { |
| 8265 | public: |
| 8266 | using DefaultABIInfo::DefaultABIInfo; |
| 8267 | |
| 8268 | private: |
| 8269 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8270 | QualType Ty) const override; |
| 8271 | |
| 8272 | void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { |
| 8273 | if (!State.FreeRegs) |
| 8274 | return; |
| 8275 | if (Info.isIndirect() && Info.getInReg()) |
| 8276 | State.FreeRegs--; |
| 8277 | else if (Info.isDirect() && Info.getInReg()) { |
| 8278 | unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; |
| 8279 | if (sz < State.FreeRegs) |
| 8280 | State.FreeRegs -= sz; |
| 8281 | else |
| 8282 | State.FreeRegs = 0; |
| 8283 | } |
| 8284 | } |
| 8285 | |
| 8286 | void computeInfo(CGFunctionInfo &FI) const override { |
| 8287 | CCState State(FI.getCallingConvention()); |
| 8288 | // ARC uses 8 registers to pass arguments. |
| 8289 | State.FreeRegs = 8; |
| 8290 | |
| 8291 | if (!getCXXABI().classifyReturnType(FI)) |
| 8292 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8293 | updateState(FI.getReturnInfo(), FI.getReturnType(), State); |
| 8294 | for (auto &I : FI.arguments()) { |
| 8295 | I.info = classifyArgumentType(I.type, State.FreeRegs); |
| 8296 | updateState(I.info, I.type, State); |
| 8297 | } |
| 8298 | } |
| 8299 | |
| 8300 | ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; |
| 8301 | ABIArgInfo getIndirectByValue(QualType Ty) const; |
| 8302 | ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; |
| 8303 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8304 | }; |
| 8305 | |
| 8306 | class ARCTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8307 | public: |
| 8308 | ARCTargetCodeGenInfo(CodeGenTypes &CGT) |
| 8309 | : TargetCodeGenInfo(new ARCABIInfo(CGT)) {} |
| 8310 | }; |
| 8311 | |
| 8312 | |
| 8313 | ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { |
| 8314 | return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : |
| 8315 | getNaturalAlignIndirect(Ty, false); |
| 8316 | } |
| 8317 | |
| 8318 | ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8319 | // Compute the byval alignment. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8320 | const unsigned MinABIStackAlignInBytes = 4; |
| 8321 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 8322 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 8323 | TypeAlign > MinABIStackAlignInBytes); |
| 8324 | } |
| 8325 | |
| 8326 | Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8327 | QualType Ty) const { |
| 8328 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 8329 | getContext().getTypeInfoInChars(Ty), |
| 8330 | CharUnits::fromQuantity(4), true); |
| 8331 | } |
| 8332 | |
| 8333 | ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, |
| 8334 | uint8_t FreeRegs) const { |
| 8335 | // Handle the generic C++ ABI. |
| 8336 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 8337 | if (RT) { |
| 8338 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 8339 | if (RAA == CGCXXABI::RAA_Indirect) |
| 8340 | return getIndirectByRef(Ty, FreeRegs > 0); |
| 8341 | |
| 8342 | if (RAA == CGCXXABI::RAA_DirectInMemory) |
| 8343 | return getIndirectByValue(Ty); |
| 8344 | } |
| 8345 | |
| 8346 | // Treat an enum type as its underlying type. |
| 8347 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8348 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8349 | |
| 8350 | auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; |
| 8351 | |
| 8352 | if (isAggregateTypeForABI(Ty)) { |
| 8353 | // Structures with flexible arrays are always indirect. |
| 8354 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 8355 | return getIndirectByValue(Ty); |
| 8356 | |
| 8357 | // Ignore empty structs/unions. |
| 8358 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8359 | return ABIArgInfo::getIgnore(); |
| 8360 | |
| 8361 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 8362 | |
| 8363 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 8364 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 8365 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 8366 | |
| 8367 | return FreeRegs >= SizeInRegs ? |
| 8368 | ABIArgInfo::getDirectInReg(Result) : |
| 8369 | ABIArgInfo::getDirect(Result, 0, nullptr, false); |
| 8370 | } |
| 8371 | |
| 8372 | return Ty->isPromotableIntegerType() ? |
| 8373 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) : |
| 8374 | ABIArgInfo::getExtend(Ty)) : |
| 8375 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() : |
| 8376 | ABIArgInfo::getDirect()); |
| 8377 | } |
| 8378 | |
| 8379 | ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { |
| 8380 | if (RetTy->isAnyComplexType()) |
| 8381 | return ABIArgInfo::getDirectInReg(); |
| 8382 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8383 | // Arguments of size > 4 registers are indirect. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8384 | auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; |
| 8385 | if (RetSize > 4) |
| 8386 | return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); |
| 8387 | |
| 8388 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 8389 | } |
| 8390 | |
| 8391 | } // End anonymous namespace. |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8392 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8393 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8394 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8395 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8396 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8397 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8398 | |
| 8399 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8400 | /// it by reference between functions that append to it. |
| 8401 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8402 | |
| 8403 | /// TypeStringCache caches the meta encodings of Types. |
| 8404 | /// |
| 8405 | /// The reason for caching TypeStrings is two fold: |
| 8406 | /// 1. To cache a type's encoding for later uses; |
| 8407 | /// 2. As a means to break recursive member type inclusion. |
| 8408 | /// |
| 8409 | /// A cache Entry can have a Status of: |
| 8410 | /// NonRecursive: The type encoding is not recursive; |
| 8411 | /// Recursive: The type encoding is recursive; |
| 8412 | /// Incomplete: An incomplete TypeString; |
| 8413 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8414 | /// Recursive type encoding. |
| 8415 | /// |
| 8416 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8417 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8418 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8419 | /// the type is encountered. |
| 8420 | /// |
| 8421 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8422 | /// possible. The type itself is recursive and it may contain other types which |
| 8423 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8424 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8425 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8426 | /// |
| 8427 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8428 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8429 | /// are placed into the cache during type expansion as a means to identify and |
| 8430 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8431 | /// the entry becomes IncompleteUsed. |
| 8432 | /// |
| 8433 | /// During the expansion of a RecordType's members: |
| 8434 | /// |
| 8435 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8436 | /// cached encoding is used; |
| 8437 | /// |
| 8438 | /// If the cache contains a Recursive encoding for the member type, the |
| 8439 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8440 | /// |
| 8441 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8442 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8443 | /// |
| 8444 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8445 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8446 | /// it is swapped back in; |
| 8447 | /// |
| 8448 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8449 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8450 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8451 | /// |
| 8452 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8453 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8454 | /// Else the member is part of a recursive type and thus the recursion has |
| 8455 | /// been exited too soon for the encoding to be correct for the member. |
| 8456 | /// |
| 8457 | class TypeStringCache { |
| 8458 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8459 | struct Entry { |
| 8460 | std::string Str; // The encoded TypeString for the type. |
| 8461 | enum Status State; // Information about the encoding in 'Str'. |
| 8462 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8463 | // during the expansion of RecordType's members. |
| 8464 | }; |
| 8465 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8466 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8467 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8468 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8469 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8470 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8471 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8472 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8473 | bool IsRecursive); |
| 8474 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8475 | }; |
| 8476 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8477 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8478 | /// FieldEncoding is a helper for this ordering process. |
| 8479 | class FieldEncoding { |
| 8480 | bool HasName; |
| 8481 | std::string Enc; |
| 8482 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8483 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8484 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8485 | bool operator<(const FieldEncoding &rhs) const { |
| 8486 | if (HasName != rhs.HasName) return HasName; |
| 8487 | return Enc < rhs.Enc; |
| 8488 | } |
| 8489 | }; |
| 8490 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8491 | class XCoreABIInfo : public DefaultABIInfo { |
| 8492 | public: |
| 8493 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8494 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8495 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8496 | }; |
| 8497 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8498 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8499 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8500 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8501 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8502 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8503 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8504 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8505 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8506 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8507 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8508 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8509 | // TODO: this implementation is likely now redundant with the default |
| 8510 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8511 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8512 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8513 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8514 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8515 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8516 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8517 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8518 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8519 | // Handle the argument. |
| 8520 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8521 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8522 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8523 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8524 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8525 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8526 | |
| 8527 | Address Val = Address::invalid(); |
| 8528 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8529 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8530 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8531 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8532 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8533 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8534 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8535 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8536 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8537 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8538 | case ABIArgInfo::Extend: |
| 8539 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8540 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8541 | ArgSize = CharUnits::fromQuantity( |
| 8542 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8543 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8544 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8545 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8546 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8547 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8548 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8549 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8550 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8551 | |
| 8552 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8553 | if (!ArgSize.isZero()) { |
| 8554 | llvm::Value *APN = |
| 8555 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 8556 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8557 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8558 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8559 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8560 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8561 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8562 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8563 | /// into the cache as a means to identify and break recursion. |
| 8564 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8565 | /// be reinserted by removeIncomplete(). |
| 8566 | /// All other types of encoding should have been used rather than arriving here. |
| 8567 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8568 | std::string StubEnc) { |
| 8569 | if (!ID) |
| 8570 | return; |
| 8571 | Entry &E = Map[ID]; |
| 8572 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8573 | "Incorrectly use of addIncomplete"); |
| 8574 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8575 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8576 | E.Str.swap(StubEnc); |
| 8577 | E.State = Incomplete; |
| 8578 | ++IncompleteCount; |
| 8579 | } |
| 8580 | |
| 8581 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8582 | /// must be removed from the cache. |
| 8583 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8584 | /// Returns true if the RecordType was defined recursively. |
| 8585 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8586 | if (!ID) |
| 8587 | return false; |
| 8588 | auto I = Map.find(ID); |
| 8589 | assert(I != Map.end() && "Entry not present"); |
| 8590 | Entry &E = I->second; |
| 8591 | assert( (E.State == Incomplete || |
| 8592 | E.State == IncompleteUsed) && |
| 8593 | "Entry must be an incomplete type"); |
| 8594 | bool IsRecursive = false; |
| 8595 | if (E.State == IncompleteUsed) { |
| 8596 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8597 | IsRecursive = true; |
| 8598 | --IncompleteUsedCount; |
| 8599 | } |
| 8600 | if (E.Swapped.empty()) |
| 8601 | Map.erase(I); |
| 8602 | else { |
| 8603 | // Swap the Recursive back. |
| 8604 | E.Swapped.swap(E.Str); |
| 8605 | E.Swapped.clear(); |
| 8606 | E.State = Recursive; |
| 8607 | } |
| 8608 | --IncompleteCount; |
| 8609 | return IsRecursive; |
| 8610 | } |
| 8611 | |
| 8612 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8613 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8614 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8615 | bool IsRecursive) { |
| 8616 | if (!ID || IncompleteUsedCount) |
| 8617 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8618 | Entry &E = Map[ID]; |
| 8619 | if (IsRecursive && !E.Str.empty()) { |
| 8620 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8621 | "This is not the same Recursive entry"); |
| 8622 | // The parent container was not recursive after all, so we could have used |
| 8623 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8624 | // we started viz: IncompleteCount!=0. |
| 8625 | return; |
| 8626 | } |
| 8627 | assert(E.Str.empty() && "Entry already present"); |
| 8628 | E.Str = Str.str(); |
| 8629 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8630 | } |
| 8631 | |
| 8632 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8633 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8634 | /// encoding is Recursive, return an empty StringRef. |
| 8635 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8636 | if (!ID) |
| 8637 | return StringRef(); // We have no key. |
| 8638 | auto I = Map.find(ID); |
| 8639 | if (I == Map.end()) |
| 8640 | return StringRef(); // We have no encoding. |
| 8641 | Entry &E = I->second; |
| 8642 | if (E.State == Recursive && IncompleteCount) |
| 8643 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8644 | |
| 8645 | if (E.State == Incomplete) { |
| 8646 | // The incomplete type is being used to break out of recursion. |
| 8647 | E.State = IncompleteUsed; |
| 8648 | ++IncompleteUsedCount; |
| 8649 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8650 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8651 | } |
| 8652 | |
| 8653 | /// The XCore ABI includes a type information section that communicates symbol |
| 8654 | /// type information to the linker. The linker uses this information to verify |
| 8655 | /// safety/correctness of things such as array bound and pointers et al. |
| 8656 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8657 | /// This type information (TypeString) is emitted into meta data for all global |
| 8658 | /// symbols: definitions, declarations, functions & variables. |
| 8659 | /// |
| 8660 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8661 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8662 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8663 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8664 | /// |
| 8665 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8666 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8667 | |
| 8668 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8669 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8670 | CodeGen::CodeGenModule &CGM) const { |
| 8671 | SmallStringEnc Enc; |
| 8672 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8673 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8674 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8675 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8676 | llvm::NamedMDNode *MD = |
| 8677 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8678 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8679 | } |
| 8680 | } |
| 8681 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8682 | //===----------------------------------------------------------------------===// |
| 8683 | // SPIR ABI Implementation |
| 8684 | //===----------------------------------------------------------------------===// |
| 8685 | |
| 8686 | namespace { |
| 8687 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8688 | public: |
| 8689 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8690 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8691 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8692 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8693 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8694 | } // End anonymous namespace. |
| 8695 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8696 | namespace clang { |
| 8697 | namespace CodeGen { |
| 8698 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8699 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8700 | SPIRABI.computeInfo(FI); |
| 8701 | } |
| 8702 | } |
| 8703 | } |
| 8704 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8705 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8706 | return llvm::CallingConv::SPIR_KERNEL; |
| 8707 | } |
| 8708 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8709 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8710 | const CodeGen::CodeGenModule &CGM, |
| 8711 | TypeStringCache &TSC); |
| 8712 | |
| 8713 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8714 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8715 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8716 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8717 | const RecordDecl *RD, |
| 8718 | const CodeGen::CodeGenModule &CGM, |
| 8719 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8720 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8721 | SmallStringEnc Enc; |
| 8722 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8723 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8724 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8725 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8726 | Enc += "b("; |
| 8727 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8728 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8729 | Enc += ':'; |
| 8730 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8731 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8732 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8733 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8734 | Enc += ')'; |
| 8735 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 8736 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8737 | } |
| 8738 | return true; |
| 8739 | } |
| 8740 | |
| 8741 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 8742 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 8743 | /// Union types have their fields ordered according to the ABI. |
| 8744 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 8745 | const CodeGen::CodeGenModule &CGM, |
| 8746 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 8747 | // Append the cached TypeString if we have one. |
| 8748 | StringRef TypeString = TSC.lookupStr(ID); |
| 8749 | if (!TypeString.empty()) { |
| 8750 | Enc += TypeString; |
| 8751 | return true; |
| 8752 | } |
| 8753 | |
| 8754 | // Start to emit an incomplete TypeString. |
| 8755 | size_t Start = Enc.size(); |
| 8756 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 8757 | Enc += '('; |
| 8758 | if (ID) |
| 8759 | Enc += ID->getName(); |
| 8760 | Enc += "){"; |
| 8761 | |
| 8762 | // We collect all encoded fields and order as necessary. |
| 8763 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8764 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 8765 | if (RD && !RD->field_empty()) { |
| 8766 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 8767 | // so that recursive calls to this RecordType will use it whilst building a |
| 8768 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8769 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8770 | std::string StubEnc(Enc.substr(Start).str()); |
| 8771 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 8772 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 8773 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 8774 | (void) TSC.removeIncomplete(ID); |
| 8775 | return false; |
| 8776 | } |
| 8777 | IsRecursive = TSC.removeIncomplete(ID); |
| 8778 | // The ABI requires unions to be sorted but not structures. |
| 8779 | // See FieldEncoding::operator< for sort algorithm. |
| 8780 | if (RT->isUnionType()) |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8781 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8782 | // We can now complete the TypeString. |
| 8783 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8784 | for (unsigned I = 0; I != E; ++I) { |
| 8785 | if (I) |
| 8786 | Enc += ','; |
| 8787 | Enc += FE[I].str(); |
| 8788 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8789 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8790 | Enc += '}'; |
| 8791 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 8792 | return true; |
| 8793 | } |
| 8794 | |
| 8795 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 8796 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 8797 | TypeStringCache &TSC, |
| 8798 | const IdentifierInfo *ID) { |
| 8799 | // Append the cached TypeString if we have one. |
| 8800 | StringRef TypeString = TSC.lookupStr(ID); |
| 8801 | if (!TypeString.empty()) { |
| 8802 | Enc += TypeString; |
| 8803 | return true; |
| 8804 | } |
| 8805 | |
| 8806 | size_t Start = Enc.size(); |
| 8807 | Enc += "e("; |
| 8808 | if (ID) |
| 8809 | Enc += ID->getName(); |
| 8810 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8811 | |
| 8812 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8813 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8814 | SmallVector<FieldEncoding, 16> FE; |
| 8815 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 8816 | ++I) { |
| 8817 | SmallStringEnc EnumEnc; |
| 8818 | EnumEnc += "m("; |
| 8819 | EnumEnc += I->getName(); |
| 8820 | EnumEnc += "){"; |
| 8821 | I->getInitVal().toString(EnumEnc); |
| 8822 | EnumEnc += '}'; |
| 8823 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 8824 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 8825 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8826 | unsigned E = FE.size(); |
| 8827 | for (unsigned I = 0; I != E; ++I) { |
| 8828 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8829 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8830 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8831 | } |
| 8832 | } |
| 8833 | Enc += '}'; |
| 8834 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 8835 | return true; |
| 8836 | } |
| 8837 | |
| 8838 | /// Appends type's qualifier to Enc. |
| 8839 | /// This is done prior to appending the type's encoding. |
| 8840 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 8841 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 8842 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8843 | int Lookup = 0; |
| 8844 | if (QT.isConstQualified()) |
| 8845 | Lookup += 1<<0; |
| 8846 | if (QT.isRestrictQualified()) |
| 8847 | Lookup += 1<<1; |
| 8848 | if (QT.isVolatileQualified()) |
| 8849 | Lookup += 1<<2; |
| 8850 | Enc += Table[Lookup]; |
| 8851 | } |
| 8852 | |
| 8853 | /// Appends built-in types to Enc. |
| 8854 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 8855 | const char *EncType; |
| 8856 | switch (BT->getKind()) { |
| 8857 | case BuiltinType::Void: |
| 8858 | EncType = "0"; |
| 8859 | break; |
| 8860 | case BuiltinType::Bool: |
| 8861 | EncType = "b"; |
| 8862 | break; |
| 8863 | case BuiltinType::Char_U: |
| 8864 | EncType = "uc"; |
| 8865 | break; |
| 8866 | case BuiltinType::UChar: |
| 8867 | EncType = "uc"; |
| 8868 | break; |
| 8869 | case BuiltinType::SChar: |
| 8870 | EncType = "sc"; |
| 8871 | break; |
| 8872 | case BuiltinType::UShort: |
| 8873 | EncType = "us"; |
| 8874 | break; |
| 8875 | case BuiltinType::Short: |
| 8876 | EncType = "ss"; |
| 8877 | break; |
| 8878 | case BuiltinType::UInt: |
| 8879 | EncType = "ui"; |
| 8880 | break; |
| 8881 | case BuiltinType::Int: |
| 8882 | EncType = "si"; |
| 8883 | break; |
| 8884 | case BuiltinType::ULong: |
| 8885 | EncType = "ul"; |
| 8886 | break; |
| 8887 | case BuiltinType::Long: |
| 8888 | EncType = "sl"; |
| 8889 | break; |
| 8890 | case BuiltinType::ULongLong: |
| 8891 | EncType = "ull"; |
| 8892 | break; |
| 8893 | case BuiltinType::LongLong: |
| 8894 | EncType = "sll"; |
| 8895 | break; |
| 8896 | case BuiltinType::Float: |
| 8897 | EncType = "ft"; |
| 8898 | break; |
| 8899 | case BuiltinType::Double: |
| 8900 | EncType = "d"; |
| 8901 | break; |
| 8902 | case BuiltinType::LongDouble: |
| 8903 | EncType = "ld"; |
| 8904 | break; |
| 8905 | default: |
| 8906 | return false; |
| 8907 | } |
| 8908 | Enc += EncType; |
| 8909 | return true; |
| 8910 | } |
| 8911 | |
| 8912 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 8913 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 8914 | const CodeGen::CodeGenModule &CGM, |
| 8915 | TypeStringCache &TSC) { |
| 8916 | Enc += "p("; |
| 8917 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 8918 | return false; |
| 8919 | Enc += ')'; |
| 8920 | return true; |
| 8921 | } |
| 8922 | |
| 8923 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8924 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 8925 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8926 | const CodeGen::CodeGenModule &CGM, |
| 8927 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 8928 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 8929 | return false; |
| 8930 | Enc += "a("; |
| 8931 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 8932 | CAT->getSize().toStringUnsigned(Enc); |
| 8933 | else |
| 8934 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 8935 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8936 | // The Qualifiers should be attached to the type rather than the array. |
| 8937 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8938 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 8939 | return false; |
| 8940 | Enc += ')'; |
| 8941 | return true; |
| 8942 | } |
| 8943 | |
| 8944 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 8945 | /// and the arguments. |
| 8946 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 8947 | const CodeGen::CodeGenModule &CGM, |
| 8948 | TypeStringCache &TSC) { |
| 8949 | Enc += "f{"; |
| 8950 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 8951 | return false; |
| 8952 | Enc += "}("; |
| 8953 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 8954 | // N.B. we are only interested in the adjusted param types. |
| 8955 | auto I = FPT->param_type_begin(); |
| 8956 | auto E = FPT->param_type_end(); |
| 8957 | if (I != E) { |
| 8958 | do { |
| 8959 | if (!appendType(Enc, *I, CGM, TSC)) |
| 8960 | return false; |
| 8961 | ++I; |
| 8962 | if (I != E) |
| 8963 | Enc += ','; |
| 8964 | } while (I != E); |
| 8965 | if (FPT->isVariadic()) |
| 8966 | Enc += ",va"; |
| 8967 | } else { |
| 8968 | if (FPT->isVariadic()) |
| 8969 | Enc += "va"; |
| 8970 | else |
| 8971 | Enc += '0'; |
| 8972 | } |
| 8973 | } |
| 8974 | Enc += ')'; |
| 8975 | return true; |
| 8976 | } |
| 8977 | |
| 8978 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 8979 | /// type encodings. |
| 8980 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8981 | const CodeGen::CodeGenModule &CGM, |
| 8982 | TypeStringCache &TSC) { |
| 8983 | |
| 8984 | QualType QT = QType.getCanonicalType(); |
| 8985 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 8986 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 8987 | // The Qualifiers should be attached to the type rather than the array. |
| 8988 | // Thus we don't call appendQualifier() here. |
| 8989 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 8990 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8991 | appendQualifier(Enc, QT); |
| 8992 | |
| 8993 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 8994 | return appendBuiltinType(Enc, BT); |
| 8995 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8996 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 8997 | return appendPointerType(Enc, PT, CGM, TSC); |
| 8998 | |
| 8999 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 9000 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 9001 | |
| 9002 | if (const RecordType *RT = QT->getAsStructureType()) |
| 9003 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9004 | |
| 9005 | if (const RecordType *RT = QT->getAsUnionType()) |
| 9006 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9007 | |
| 9008 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 9009 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 9010 | |
| 9011 | return false; |
| 9012 | } |
| 9013 | |
| 9014 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 9015 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 9016 | if (!D) |
| 9017 | return false; |
| 9018 | |
| 9019 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9020 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 9021 | return false; |
| 9022 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 9023 | } |
| 9024 | |
| 9025 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 9026 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 9027 | return false; |
| 9028 | QualType QT = VD->getType().getCanonicalType(); |
| 9029 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 9030 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9031 | // The Qualifiers should be attached to the type rather than the array. |
| 9032 | // Thus we don't call appendQualifier() here. |
| 9033 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9034 | } |
| 9035 | return appendType(Enc, QT, CGM, TSC); |
| 9036 | } |
| 9037 | return false; |
| 9038 | } |
| 9039 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9040 | //===----------------------------------------------------------------------===// |
| 9041 | // RISCV ABI Implementation |
| 9042 | //===----------------------------------------------------------------------===// |
| 9043 | |
| 9044 | namespace { |
| 9045 | class RISCVABIInfo : public DefaultABIInfo { |
| 9046 | private: |
| 9047 | unsigned XLen; // Size of the integer ('x') registers in bits. |
| 9048 | static const int NumArgGPRs = 8; |
| 9049 | |
| 9050 | public: |
| 9051 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9052 | : DefaultABIInfo(CGT), XLen(XLen) {} |
| 9053 | |
| 9054 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 9055 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 9056 | void computeInfo(CGFunctionInfo &FI) const override; |
| 9057 | |
| 9058 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, |
| 9059 | int &ArgGPRsLeft) const; |
| 9060 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 9061 | |
| 9062 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9063 | QualType Ty) const override; |
| 9064 | |
| 9065 | ABIArgInfo extendType(QualType Ty) const; |
| 9066 | }; |
| 9067 | } // end anonymous namespace |
| 9068 | |
| 9069 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 9070 | QualType RetTy = FI.getReturnType(); |
| 9071 | if (!getCXXABI().classifyReturnType(FI)) |
| 9072 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 9073 | |
| 9074 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 9075 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 9076 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 9077 | // rewrite the argument list and pass indirectly on RV32. |
| 9078 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 9079 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 9080 | |
| 9081 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 9082 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 9083 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 9084 | // different for variadic arguments, we must also track whether we are |
| 9085 | // examining a vararg or not. |
| 9086 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
| 9087 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 9088 | |
| 9089 | int ArgNum = 0; |
| 9090 | for (auto &ArgInfo : FI.arguments()) { |
| 9091 | bool IsFixed = ArgNum < NumFixedArgs; |
| 9092 | ArgInfo.info = classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft); |
| 9093 | ArgNum++; |
| 9094 | } |
| 9095 | } |
| 9096 | |
| 9097 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
| 9098 | int &ArgGPRsLeft) const { |
| 9099 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 9100 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 9101 | |
| 9102 | // Structures with either a non-trivial destructor or a non-trivial |
| 9103 | // copy constructor are always passed indirectly. |
| 9104 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 9105 | if (ArgGPRsLeft) |
| 9106 | ArgGPRsLeft -= 1; |
| 9107 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 9108 | CGCXXABI::RAA_DirectInMemory); |
| 9109 | } |
| 9110 | |
| 9111 | // Ignore empty structs/unions. |
| 9112 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9113 | return ABIArgInfo::getIgnore(); |
| 9114 | |
| 9115 | uint64_t Size = getContext().getTypeSize(Ty); |
| 9116 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 9117 | bool MustUseStack = false; |
| 9118 | // Determine the number of GPRs needed to pass the current argument |
| 9119 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 9120 | // register pairs, so may consume 3 registers. |
| 9121 | int NeededArgGPRs = 1; |
| 9122 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 9123 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 9124 | else if (Size > XLen && Size <= 2 * XLen) |
| 9125 | NeededArgGPRs = 2; |
| 9126 | |
| 9127 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 9128 | MustUseStack = true; |
| 9129 | NeededArgGPRs = ArgGPRsLeft; |
| 9130 | } |
| 9131 | |
| 9132 | ArgGPRsLeft -= NeededArgGPRs; |
| 9133 | |
| 9134 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 9135 | // Treat an enum type as its underlying type. |
| 9136 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 9137 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 9138 | |
| 9139 | // All integral types are promoted to XLen width, unless passed on the |
| 9140 | // stack. |
| 9141 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 9142 | return extendType(Ty); |
| 9143 | } |
| 9144 | |
| 9145 | return ABIArgInfo::getDirect(); |
| 9146 | } |
| 9147 | |
| 9148 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 9149 | // so coerce to integers. |
| 9150 | if (Size <= 2 * XLen) { |
| 9151 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 9152 | |
| 9153 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 9154 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 9155 | if (Size <= XLen) { |
| 9156 | return ABIArgInfo::getDirect( |
| 9157 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 9158 | } else if (Alignment == 2 * XLen) { |
| 9159 | return ABIArgInfo::getDirect( |
| 9160 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 9161 | } else { |
| 9162 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 9163 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 9164 | } |
| 9165 | } |
| 9166 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 9167 | } |
| 9168 | |
| 9169 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 9170 | if (RetTy->isVoidType()) |
| 9171 | return ABIArgInfo::getIgnore(); |
| 9172 | |
| 9173 | int ArgGPRsLeft = 2; |
| 9174 | |
| 9175 | // The rules for return and argument types are the same, so defer to |
| 9176 | // classifyArgumentType. |
| 9177 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft); |
| 9178 | } |
| 9179 | |
| 9180 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9181 | QualType Ty) const { |
| 9182 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 9183 | |
| 9184 | // Empty records are ignored for parameter passing purposes. |
| 9185 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 9186 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 9187 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 9188 | return Addr; |
| 9189 | } |
| 9190 | |
| 9191 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 9192 | getContext().getTypeInfoInChars(Ty); |
| 9193 | |
| 9194 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 9195 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 9196 | |
| 9197 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 9198 | SlotSize, /*AllowHigherAlign=*/true); |
| 9199 | } |
| 9200 | |
| 9201 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 9202 | int TySize = getContext().getTypeSize(Ty); |
| 9203 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 9204 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 9205 | return ABIArgInfo::getSignExtend(Ty); |
| 9206 | return ABIArgInfo::getExtend(Ty); |
| 9207 | } |
| 9208 | |
| 9209 | namespace { |
| 9210 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 9211 | public: |
| 9212 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen) |
| 9213 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen)) {} |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 9214 | |
| 9215 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 9216 | CodeGen::CodeGenModule &CGM) const override { |
| 9217 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 9218 | if (!FD) return; |
| 9219 | |
| 9220 | const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); |
| 9221 | if (!Attr) |
| 9222 | return; |
| 9223 | |
| 9224 | const char *Kind; |
| 9225 | switch (Attr->getInterrupt()) { |
| 9226 | case RISCVInterruptAttr::user: Kind = "user"; break; |
| 9227 | case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; |
| 9228 | case RISCVInterruptAttr::machine: Kind = "machine"; break; |
| 9229 | } |
| 9230 | |
| 9231 | auto *Fn = cast<llvm::Function>(GV); |
| 9232 | |
| 9233 | Fn->addFnAttr("interrupt", Kind); |
| 9234 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9235 | }; |
| 9236 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9237 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9238 | //===----------------------------------------------------------------------===// |
| 9239 | // Driver code |
| 9240 | //===----------------------------------------------------------------------===// |
| 9241 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9242 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 9243 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9244 | } |
| 9245 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 9246 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9247 | if (TheTargetCodeGenInfo) |
| 9248 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9249 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9250 | // Helper to set the unique_ptr while still keeping the return value. |
| 9251 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 9252 | this->TheTargetCodeGenInfo.reset(P); |
| 9253 | return *P; |
| 9254 | }; |
| 9255 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 9256 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 9257 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9258 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9259 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9260 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 9261 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9262 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 9263 | case llvm::Triple::mips: |
| 9264 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 9265 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9266 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 9267 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9268 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 9269 | case llvm::Triple::mips64: |
| 9270 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9271 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9272 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 9273 | case llvm::Triple::avr: |
| 9274 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 9275 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 9276 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 9277 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9278 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 9279 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9280 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 9281 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 9282 | return SetCGInfo( |
| 9283 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9284 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9285 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9286 | } |
| 9287 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9288 | case llvm::Triple::wasm32: |
| 9289 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9290 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9291 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9292 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 9293 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9294 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9295 | case llvm::Triple::thumbeb: { |
| 9296 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9297 | return SetCGInfo( |
| 9298 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9299 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9300 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9301 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9302 | StringRef ABIStr = getTarget().getABI(); |
| 9303 | if (ABIStr == "apcs-gnu") |
| 9304 | Kind = ARMABIInfo::APCS; |
| 9305 | else if (ABIStr == "aapcs16") |
| 9306 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9307 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9308 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9309 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9310 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9311 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9312 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9313 | |
| 9314 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9315 | } |
| 9316 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9317 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9318 | return SetCGInfo( |
| 9319 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9320 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9321 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9322 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9323 | if (getTarget().getABI() == "elfv2") |
| 9324 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9325 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9326 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9327 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9328 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9329 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9330 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9331 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9332 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9333 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9334 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9335 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9336 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9337 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9338 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9339 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9340 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9341 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9342 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9343 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9344 | case llvm::Triple::nvptx: |
| 9345 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9346 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9347 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9348 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9349 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9350 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9351 | case llvm::Triple::riscv32: |
| 9352 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 32)); |
| 9353 | case llvm::Triple::riscv64: |
| 9354 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, 64)); |
| 9355 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9356 | case llvm::Triple::systemz: { |
| 9357 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9358 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9359 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9360 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9361 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9362 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9363 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9364 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9365 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9366 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9367 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9368 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9369 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9370 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9371 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9372 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9373 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9374 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9375 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9376 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9377 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9378 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 9379 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9380 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9381 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9382 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9383 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9384 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9385 | X86AVXABILevel AVXLevel = |
| 9386 | (ABI == "avx512" |
| 9387 | ? X86AVXABILevel::AVX512 |
| 9388 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9389 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9390 | switch (Triple.getOS()) { |
| 9391 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9392 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 9393 | case llvm::Triple::PS4: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9394 | return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9395 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9396 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9397 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9398 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9399 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9400 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9401 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9402 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9403 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9404 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9405 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9406 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9407 | case llvm::Triple::sparc: |
| 9408 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9409 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9410 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9411 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9412 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 9413 | case llvm::Triple::arc: |
| 9414 | return SetCGInfo(new ARCTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9415 | case llvm::Triple::spir: |
| 9416 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9417 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9418 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9419 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9420 | |
| 9421 | /// Create an OpenCL kernel for an enqueued block. |
| 9422 | /// |
| 9423 | /// The kernel has the same function type as the block invoke function. Its |
| 9424 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9425 | /// It simply calls the block invoke function then returns. |
| 9426 | llvm::Function * |
| 9427 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9428 | llvm::Function *Invoke, |
| 9429 | llvm::Value *BlockLiteral) const { |
| 9430 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9431 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9432 | for (auto &P : InvokeFT->params()) |
| 9433 | ArgTys.push_back(P); |
| 9434 | auto &C = CGF.getLLVMContext(); |
| 9435 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9436 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9437 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9438 | &CGF.CGM.getModule()); |
| 9439 | auto IP = CGF.Builder.saveIP(); |
| 9440 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9441 | auto &Builder = CGF.Builder; |
| 9442 | Builder.SetInsertPoint(BB); |
| 9443 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9444 | for (auto &A : F->args()) |
| 9445 | Args.push_back(&A); |
| 9446 | Builder.CreateCall(Invoke, Args); |
| 9447 | Builder.CreateRetVoid(); |
| 9448 | Builder.restoreIP(IP); |
| 9449 | return F; |
| 9450 | } |
| 9451 | |
| 9452 | /// Create an OpenCL kernel for an enqueued block. |
| 9453 | /// |
| 9454 | /// The type of the first argument (the block literal) is the struct type |
| 9455 | /// of the block literal instead of a pointer type. The first argument |
| 9456 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9457 | /// allocates the same type of struct on stack and stores the block literal |
| 9458 | /// to it and passes its pointer to the block invoke function. The kernel |
| 9459 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 9460 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 9461 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 9462 | llvm::Value *BlockLiteral) const { |
| 9463 | auto &Builder = CGF.Builder; |
| 9464 | auto &C = CGF.getLLVMContext(); |
| 9465 | |
| 9466 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 9467 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9468 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9469 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 9470 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 9471 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 9472 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 9473 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 9474 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 9475 | |
| 9476 | ArgTys.push_back(BlockTy); |
| 9477 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9478 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 9479 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 9480 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9481 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9482 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 9483 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 9484 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9485 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9486 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 9487 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 9488 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 9489 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 9490 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 9491 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9492 | } |
| 9493 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9494 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9495 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9496 | &CGF.CGM.getModule()); |
| 9497 | F->addFnAttr("enqueued-block"); |
| 9498 | auto IP = CGF.Builder.saveIP(); |
| 9499 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9500 | Builder.SetInsertPoint(BB); |
| 9501 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 9502 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
| 9503 | BlockPtr->setAlignment(BlockAlign); |
| 9504 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 9505 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 9506 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9507 | Args.push_back(Cast); |
| 9508 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 9509 | Args.push_back(I); |
| 9510 | Builder.CreateCall(Invoke, Args); |
| 9511 | Builder.CreateRetVoid(); |
| 9512 | Builder.restoreIP(IP); |
| 9513 | |
| 9514 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 9515 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 9516 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 9517 | F->setMetadata("kernel_arg_base_type", |
| 9518 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 9519 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 9520 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 9521 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 9522 | |
| 9523 | return F; |
| 9524 | } |