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" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CGFunctionInfo.h" |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CodeGenOptions.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 28 | #include <algorithm> // std::sort |
| 29 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 33 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 34 | llvm::Value *Array, |
| 35 | llvm::Value *Value, |
| 36 | unsigned FirstIndex, |
| 37 | unsigned LastIndex) { |
| 38 | // Alternatively, we could emit this as a loop in the source. |
| 39 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 40 | llvm::Value *Cell = |
| 41 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 42 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 46 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 47 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 48 | T->isMemberFunctionPointerType(); |
| 49 | } |
| 50 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 51 | ABIArgInfo |
| 52 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 53 | llvm::Type *Padding) const { |
| 54 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 55 | ByRef, Realign, Padding); |
| 56 | } |
| 57 | |
| 58 | ABIArgInfo |
| 59 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 60 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 61 | /*ByRef*/ false, Realign); |
| 62 | } |
| 63 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 64 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 65 | QualType Ty) const { |
| 66 | return Address::invalid(); |
| 67 | } |
| 68 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 69 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 70 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 71 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 72 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 73 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 74 | if (!RD) |
| 75 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 76 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 80 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 81 | const RecordType *RT = T->getAs<RecordType>(); |
| 82 | if (!RT) |
| 83 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 84 | return getRecordArgABI(RT, CXXABI); |
| 85 | } |
| 86 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 87 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 88 | /// should ensure that all elements of the union have the same "machine type". |
| 89 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 90 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 91 | const RecordDecl *UD = UT->getDecl(); |
| 92 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 93 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 94 | return UD->field_begin()->getType(); |
| 95 | } |
| 96 | } |
| 97 | return Ty; |
| 98 | } |
| 99 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 100 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 101 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 104 | ASTContext &ABIInfo::getContext() const { |
| 105 | return CGT.getContext(); |
| 106 | } |
| 107 | |
| 108 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 109 | return CGT.getLLVMContext(); |
| 110 | } |
| 111 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 112 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 113 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 116 | const TargetInfo &ABIInfo::getTarget() const { |
| 117 | return CGT.getTarget(); |
| 118 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 119 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 120 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 125 | uint64_t Members) const { |
| 126 | return false; |
| 127 | } |
| 128 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 129 | bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 130 | return false; |
| 131 | } |
| 132 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 133 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 134 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 135 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 136 | switch (TheKind) { |
| 137 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 138 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 139 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 140 | Ty->print(OS); |
| 141 | else |
| 142 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 143 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 144 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 145 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 146 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 147 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 148 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 149 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 150 | case InAlloca: |
| 151 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 152 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 153 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 154 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 155 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 156 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 157 | break; |
| 158 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 159 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 162 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 165 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 166 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 167 | llvm::Value *Ptr, |
| 168 | CharUnits Align) { |
| 169 | llvm::Value *PtrAsInt = Ptr; |
| 170 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 171 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 172 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 173 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 174 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 175 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 176 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 177 | Ptr->getType(), |
| 178 | Ptr->getName() + ".aligned"); |
| 179 | return PtrAsInt; |
| 180 | } |
| 181 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 182 | /// Emit va_arg for a platform using the common void* representation, |
| 183 | /// where arguments are simply emitted in an array of slots on the stack. |
| 184 | /// |
| 185 | /// This version implements the core direct-value passing rules. |
| 186 | /// |
| 187 | /// \param SlotSize - The size and alignment of a stack slot. |
| 188 | /// Each argument will be allocated to a multiple of this number of |
| 189 | /// slots, and all the slots will be aligned to this value. |
| 190 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 191 | /// an argument type with an alignment greater than the slot size |
| 192 | /// will be emitted on a higher-alignment address, potentially |
| 193 | /// leaving one or more empty slots behind as padding. If this |
| 194 | /// is false, the returned address might be less-aligned than |
| 195 | /// DirectAlign. |
| 196 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 197 | Address VAListAddr, |
| 198 | llvm::Type *DirectTy, |
| 199 | CharUnits DirectSize, |
| 200 | CharUnits DirectAlign, |
| 201 | CharUnits SlotSize, |
| 202 | bool AllowHigherAlign) { |
| 203 | // Cast the element type to i8* if necessary. Some platforms define |
| 204 | // va_list as a struct containing an i8* instead of just an i8*. |
| 205 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 206 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 207 | |
| 208 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 209 | |
| 210 | // If the CC aligns values higher than the slot size, do so if needed. |
| 211 | Address Addr = Address::invalid(); |
| 212 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 213 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 214 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 215 | } else { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 216 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 220 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 221 | llvm::Value *NextPtr = |
| 222 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 223 | "argp.next"); |
| 224 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 225 | |
| 226 | // If the argument is smaller than a slot, and this is a big-endian |
| 227 | // target, the argument will be right-adjusted in its slot. |
| 228 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian()) { |
| 229 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 230 | } |
| 231 | |
| 232 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 233 | return Addr; |
| 234 | } |
| 235 | |
| 236 | /// Emit va_arg for a platform using the common void* representation, |
| 237 | /// where arguments are simply emitted in an array of slots on the stack. |
| 238 | /// |
| 239 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 240 | /// \param ValueInfo - The size and alignment of this type, generally |
| 241 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 242 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 243 | /// Each argument will be allocated to a multiple of this number of |
| 244 | /// slots, and all the slots will be aligned to this value. |
| 245 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 246 | /// an argument type with an alignment greater than the slot size |
| 247 | /// will be emitted on a higher-alignment address, potentially |
| 248 | /// leaving one or more empty slots behind as padding. |
| 249 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 250 | QualType ValueTy, bool IsIndirect, |
| 251 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 252 | CharUnits SlotSizeAndAlign, |
| 253 | bool AllowHigherAlign) { |
| 254 | // The size and alignment of the value that was passed directly. |
| 255 | CharUnits DirectSize, DirectAlign; |
| 256 | if (IsIndirect) { |
| 257 | DirectSize = CGF.getPointerSize(); |
| 258 | DirectAlign = CGF.getPointerAlign(); |
| 259 | } else { |
| 260 | DirectSize = ValueInfo.first; |
| 261 | DirectAlign = ValueInfo.second; |
| 262 | } |
| 263 | |
| 264 | // Cast the address we've calculated to the right type. |
| 265 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 266 | if (IsIndirect) |
| 267 | DirectTy = DirectTy->getPointerTo(0); |
| 268 | |
| 269 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 270 | DirectSize, DirectAlign, |
| 271 | SlotSizeAndAlign, |
| 272 | AllowHigherAlign); |
| 273 | |
| 274 | if (IsIndirect) { |
| 275 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 276 | } |
| 277 | |
| 278 | return Addr; |
| 279 | |
| 280 | } |
| 281 | |
| 282 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 283 | Address Addr1, llvm::BasicBlock *Block1, |
| 284 | Address Addr2, llvm::BasicBlock *Block2, |
| 285 | const llvm::Twine &Name = "") { |
| 286 | assert(Addr1.getType() == Addr2.getType()); |
| 287 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 288 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 289 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 290 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 291 | return Address(PHI, Align); |
| 292 | } |
| 293 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 294 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 295 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 296 | // If someone can figure out a general rule for this, that would be great. |
| 297 | // It's probably just doomed to be platform-dependent, though. |
| 298 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 299 | // Verified for: |
| 300 | // x86-64 FreeBSD, Linux, Darwin |
| 301 | // x86-32 FreeBSD, Linux, Darwin |
| 302 | // PowerPC Linux, Darwin |
| 303 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 304 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 305 | return 32; |
| 306 | } |
| 307 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 308 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 309 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 310 | // The following conventions are known to require this to be false: |
| 311 | // x86_stdcall |
| 312 | // MIPS |
| 313 | // For everything else, we just prefer false unless we opt out. |
| 314 | return false; |
| 315 | } |
| 316 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 317 | void |
| 318 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 319 | llvm::SmallString<24> &Opt) const { |
| 320 | // This assumes the user is passing a library name like "rt" instead of a |
| 321 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 322 | // dynamic. |
| 323 | Opt = "-l"; |
| 324 | Opt += Lib; |
| 325 | } |
| 326 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 327 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 328 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 329 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 330 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 331 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 332 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 333 | if (FD->isUnnamedBitfield()) |
| 334 | return true; |
| 335 | |
| 336 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 337 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 338 | // Constant arrays of empty records count as empty, strip them off. |
| 339 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 340 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 341 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 342 | if (AT->getSize() == 0) |
| 343 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 344 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 345 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 346 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 347 | const RecordType *RT = FT->getAs<RecordType>(); |
| 348 | if (!RT) |
| 349 | return false; |
| 350 | |
| 351 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 352 | // |
| 353 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 354 | // current ABI. |
| 355 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 356 | return false; |
| 357 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 358 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 361 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 362 | /// fields. Note that a structure with a flexible array member is not |
| 363 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 364 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 365 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 366 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 367 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 368 | const RecordDecl *RD = RT->getDecl(); |
| 369 | if (RD->hasFlexibleArrayMember()) |
| 370 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 371 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 372 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 373 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 374 | for (const auto &I : CXXRD->bases()) |
| 375 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 376 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 377 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 378 | for (const auto *I : RD->fields()) |
| 379 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 380 | return false; |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | /// isSingleElementStruct - Determine if a structure is a "single |
| 385 | /// element struct", i.e. it has exactly one non-empty field or |
| 386 | /// exactly one field which is itself a single element |
| 387 | /// struct. Structures with flexible array members are never |
| 388 | /// considered single element structs. |
| 389 | /// |
| 390 | /// \return The field declaration for the single non-empty field, if |
| 391 | /// it exists. |
| 392 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 393 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 394 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 395 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 396 | |
| 397 | const RecordDecl *RD = RT->getDecl(); |
| 398 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 399 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 400 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 401 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 402 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 403 | // If this is a C++ record, check the bases first. |
| 404 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 405 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 406 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 407 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 408 | continue; |
| 409 | |
| 410 | // If we already found an element then this isn't a single-element struct. |
| 411 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 412 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 413 | |
| 414 | // If this is non-empty and not a single element struct, the composite |
| 415 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 416 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 417 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 418 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 423 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 424 | QualType FT = FD->getType(); |
| 425 | |
| 426 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 427 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 428 | continue; |
| 429 | |
| 430 | // If we already found an element then this isn't a single-element |
| 431 | // struct. |
| 432 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 433 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 434 | |
| 435 | // Treat single element arrays as the element. |
| 436 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 437 | if (AT->getSize().getZExtValue() != 1) |
| 438 | break; |
| 439 | FT = AT->getElementType(); |
| 440 | } |
| 441 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 442 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 443 | Found = FT.getTypePtr(); |
| 444 | } else { |
| 445 | Found = isSingleElementStruct(FT, Context); |
| 446 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 447 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 451 | // We don't consider a struct a single-element struct if it has |
| 452 | // padding beyond the element type. |
| 453 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 454 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 455 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 456 | return Found; |
| 457 | } |
| 458 | |
| 459 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 460 | // Treat complex types as the element type. |
| 461 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 462 | Ty = CTy->getElementType(); |
| 463 | |
| 464 | // Check for a type which we know has a simple scalar argument-passing |
| 465 | // convention without any padding. (We're specifically looking for 32 |
| 466 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 467 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 468 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 469 | return false; |
| 470 | |
| 471 | uint64_t Size = Context.getTypeSize(Ty); |
| 472 | return Size == 32 || Size == 64; |
| 473 | } |
| 474 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 475 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 476 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 477 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 478 | /// inhibiting optimizations. |
| 479 | /// |
| 480 | // FIXME: This predicate is missing many cases, currently it just follows |
| 481 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 482 | // should probably make this smarter, or better yet make the LLVM backend |
| 483 | // capable of handling it. |
| 484 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 485 | // We can only expand structure types. |
| 486 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 487 | if (!RT) |
| 488 | return false; |
| 489 | |
| 490 | // We can only expand (C) structures. |
| 491 | // |
| 492 | // FIXME: This needs to be generalized to handle classes as well. |
| 493 | const RecordDecl *RD = RT->getDecl(); |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 494 | if (!RD->isStruct()) |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 495 | return false; |
| 496 | |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 497 | // We try to expand CLike CXXRecordDecl. |
| 498 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 499 | if (!CXXRD->isCLike()) |
| 500 | return false; |
| 501 | } |
| 502 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 503 | uint64_t Size = 0; |
| 504 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 505 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 506 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 507 | return false; |
| 508 | |
| 509 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 510 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 511 | // counts as "basic" is more complicated than what we were doing previously. |
| 512 | if (FD->isBitField()) |
| 513 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 514 | |
| 515 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 518 | // Make sure there are not any holes in the struct. |
| 519 | if (Size != Context.getTypeSize(Ty)) |
| 520 | return false; |
| 521 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | |
| 525 | namespace { |
| 526 | /// DefaultABIInfo - The default implementation for ABI specific |
| 527 | /// details. This implementation provides information which results in |
| 528 | /// self-consistent and sensible LLVM IR generation, but does not |
| 529 | /// conform to any particular ABI. |
| 530 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 531 | public: |
| 532 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 534 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 535 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 536 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 537 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 538 | if (!getCXXABI().classifyReturnType(FI)) |
| 539 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 540 | for (auto &I : FI.arguments()) |
| 541 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 542 | } |
| 543 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 544 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 545 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 546 | }; |
| 547 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 548 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 549 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 550 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 551 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 554 | Address DefaultABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 555 | QualType Ty) const { |
| 556 | return Address::invalid(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 559 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 560 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 561 | |
| 562 | if (isAggregateTypeForABI(Ty)) { |
| 563 | // Records with non-trivial destructors/copy-constructors should not be |
| 564 | // passed by value. |
| 565 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 566 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 567 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 568 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 569 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 570 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 571 | // Treat an enum type as its underlying type. |
| 572 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 573 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 575 | return (Ty->isPromotableIntegerType() ? |
| 576 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 579 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 580 | if (RetTy->isVoidType()) |
| 581 | return ABIArgInfo::getIgnore(); |
| 582 | |
| 583 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 584 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 585 | |
| 586 | // Treat an enum type as its underlying type. |
| 587 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 588 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 589 | |
| 590 | return (RetTy->isPromotableIntegerType() ? |
| 591 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 592 | } |
| 593 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 594 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 595 | // WebAssembly ABI Implementation |
| 596 | // |
| 597 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 598 | //===----------------------------------------------------------------------===// |
| 599 | |
| 600 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 601 | public: |
| 602 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 603 | : DefaultABIInfo(CGT) {} |
| 604 | |
| 605 | private: |
| 606 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 607 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 608 | |
| 609 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 610 | // non-virtual, but computeInfo is virtual, so we overload that. |
| 611 | void computeInfo(CGFunctionInfo &FI) const override { |
| 612 | if (!getCXXABI().classifyReturnType(FI)) |
| 613 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 614 | for (auto &Arg : FI.arguments()) |
| 615 | Arg.info = classifyArgumentType(Arg.type); |
| 616 | } |
| 617 | }; |
| 618 | |
| 619 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 620 | public: |
| 621 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 622 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 623 | }; |
| 624 | |
| 625 | /// \brief Classify argument of given type \p Ty. |
| 626 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 627 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 628 | |
| 629 | if (isAggregateTypeForABI(Ty)) { |
| 630 | // Records with non-trivial destructors/copy-constructors should not be |
| 631 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 632 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 633 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 634 | // Ignore empty structs/unions. |
| 635 | if (isEmptyRecord(getContext(), Ty, true)) |
| 636 | return ABIArgInfo::getIgnore(); |
| 637 | // Lower single-element structs to just pass a regular value. TODO: We |
| 638 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 639 | // though watch out for things like bitfields. |
| 640 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 641 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // Otherwise just do the default thing. |
| 645 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 646 | } |
| 647 | |
| 648 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 649 | if (isAggregateTypeForABI(RetTy)) { |
| 650 | // Records with non-trivial destructors/copy-constructors should not be |
| 651 | // returned by value. |
| 652 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 653 | // Ignore empty structs/unions. |
| 654 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 655 | return ABIArgInfo::getIgnore(); |
| 656 | // Lower single-element structs to just return a regular value. TODO: We |
| 657 | // could do reasonable-size multiple-element structs too, using |
| 658 | // ABIArgInfo::getDirect(). |
| 659 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 660 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // Otherwise just do the default thing. |
| 665 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 666 | } |
| 667 | |
| 668 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 669 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 670 | // |
| 671 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 672 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 673 | //===----------------------------------------------------------------------===// |
| 674 | |
| 675 | class PNaClABIInfo : public ABIInfo { |
| 676 | public: |
| 677 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 678 | |
| 679 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 680 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 681 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 682 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 683 | Address EmitVAArg(CodeGenFunction &CGF, |
| 684 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
| 687 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 688 | public: |
| 689 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 690 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 691 | }; |
| 692 | |
| 693 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 694 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 695 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 696 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 697 | for (auto &I : FI.arguments()) |
| 698 | I.info = classifyArgumentType(I.type); |
| 699 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 700 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 701 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 702 | QualType Ty) const { |
| 703 | return Address::invalid(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 706 | /// \brief Classify argument of given type \p Ty. |
| 707 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 708 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 709 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 710 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 711 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 712 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 713 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 714 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 715 | } else if (Ty->isFloatingType()) { |
| 716 | // Floating-point types don't go inreg. |
| 717 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 718 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 719 | |
| 720 | return (Ty->isPromotableIntegerType() ? |
| 721 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 725 | if (RetTy->isVoidType()) |
| 726 | return ABIArgInfo::getIgnore(); |
| 727 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 728 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 729 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 730 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 731 | |
| 732 | // Treat an enum type as its underlying type. |
| 733 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 734 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 735 | |
| 736 | return (RetTy->isPromotableIntegerType() ? |
| 737 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 738 | } |
| 739 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 740 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 741 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 742 | // 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] | 743 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 744 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 745 | IRType->getScalarSizeInBits() != 64; |
| 746 | } |
| 747 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 748 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 749 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 750 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 751 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 752 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 753 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 754 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 757 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 761 | return Ty; |
| 762 | } |
| 763 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 764 | /// Returns true if this type can be passed in SSE registers with the |
| 765 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 766 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 767 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 768 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 769 | return true; |
| 770 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 771 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 772 | // registers specially. |
| 773 | unsigned VecSize = Context.getTypeSize(VT); |
| 774 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 775 | return true; |
| 776 | } |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 781 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 782 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 783 | return NumMembers <= 4; |
| 784 | } |
| 785 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 786 | //===----------------------------------------------------------------------===// |
| 787 | // X86-32 ABI Implementation |
| 788 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 789 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 790 | /// \brief Similar to llvm::CCState, but for Clang. |
| 791 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 792 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 793 | |
| 794 | unsigned CC; |
| 795 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 796 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 797 | }; |
| 798 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 799 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 800 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 801 | enum Class { |
| 802 | Integer, |
| 803 | Float |
| 804 | }; |
| 805 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 806 | static const unsigned MinABIStackAlignInBytes = 4; |
| 807 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 808 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 809 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 810 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 811 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 812 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 813 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 814 | |
| 815 | static bool isRegisterSize(unsigned Size) { |
| 816 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 817 | } |
| 818 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 819 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 820 | // FIXME: Assumes vectorcall is in use. |
| 821 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 822 | } |
| 823 | |
| 824 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 825 | uint64_t NumMembers) const override { |
| 826 | // FIXME: Assumes vectorcall is in use. |
| 827 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 828 | } |
| 829 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 830 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 832 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 833 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 834 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 835 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 836 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 837 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 838 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 839 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 840 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 841 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 842 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 843 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 844 | /// \brief Updates the number of available free registers, returns |
| 845 | /// true if any registers were allocated. |
| 846 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 847 | |
| 848 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 849 | bool &NeedsPadding) const; |
| 850 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 851 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 852 | /// \brief Rewrite the function info so that all memory arguments use |
| 853 | /// inalloca. |
| 854 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 855 | |
| 856 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 857 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 858 | QualType Type) const; |
| 859 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 860 | public: |
| 861 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 862 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 863 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 864 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 865 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 866 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 867 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 868 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 869 | : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
| 870 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 871 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 872 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 873 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 874 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 875 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 876 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 877 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 878 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 879 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 880 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 881 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 882 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 883 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 884 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 885 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 886 | static bool isStructReturnInRegABI( |
| 887 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 888 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 889 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 890 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 891 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 892 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 893 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 894 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 895 | return 4; |
| 896 | } |
| 897 | |
| 898 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 899 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 900 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 901 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 902 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 903 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 904 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 905 | } |
| 906 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 907 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 908 | std::string &Constraints, |
| 909 | std::vector<llvm::Type *> &ResultRegTypes, |
| 910 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 911 | std::vector<LValue> &ResultRegDests, |
| 912 | std::string &AsmString, |
| 913 | unsigned NumOutputs) const override; |
| 914 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 915 | llvm::Constant * |
| 916 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 917 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 918 | (0x06 << 8) | // .+0x08 |
| 919 | ('F' << 16) | |
| 920 | ('T' << 24); |
| 921 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 922 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 923 | |
| 924 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 925 | return "movl\t%ebp, %ebp" |
| 926 | "\t\t## marker for objc_retainAutoreleaseReturnValue"; |
| 927 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 928 | }; |
| 929 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 930 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 931 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 932 | /// Rewrite input constraint references after adding some output constraints. |
| 933 | /// In the case where there is one output and one input and we add one output, |
| 934 | /// we need to replace all operand references greater than or equal to 1: |
| 935 | /// mov $0, $1 |
| 936 | /// mov eax, $1 |
| 937 | /// The result will be: |
| 938 | /// mov $0, $2 |
| 939 | /// mov eax, $2 |
| 940 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 941 | unsigned NumNewOuts, |
| 942 | std::string &AsmString) { |
| 943 | std::string Buf; |
| 944 | llvm::raw_string_ostream OS(Buf); |
| 945 | size_t Pos = 0; |
| 946 | while (Pos < AsmString.size()) { |
| 947 | size_t DollarStart = AsmString.find('$', Pos); |
| 948 | if (DollarStart == std::string::npos) |
| 949 | DollarStart = AsmString.size(); |
| 950 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 951 | if (DollarEnd == std::string::npos) |
| 952 | DollarEnd = AsmString.size(); |
| 953 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 954 | Pos = DollarEnd; |
| 955 | size_t NumDollars = DollarEnd - DollarStart; |
| 956 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 957 | // We have an operand reference. |
| 958 | size_t DigitStart = Pos; |
| 959 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 960 | if (DigitEnd == std::string::npos) |
| 961 | DigitEnd = AsmString.size(); |
| 962 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 963 | unsigned OperandIndex; |
| 964 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 965 | if (OperandIndex >= FirstIn) |
| 966 | OperandIndex += NumNewOuts; |
| 967 | OS << OperandIndex; |
| 968 | } else { |
| 969 | OS << OperandStr; |
| 970 | } |
| 971 | Pos = DigitEnd; |
| 972 | } |
| 973 | } |
| 974 | AsmString = std::move(OS.str()); |
| 975 | } |
| 976 | |
| 977 | /// Add output constraints for EAX:EDX because they are return registers. |
| 978 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 979 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 980 | std::vector<llvm::Type *> &ResultRegTypes, |
| 981 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 982 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 983 | unsigned NumOutputs) const { |
| 984 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 985 | |
| 986 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 987 | // larger. |
| 988 | if (!Constraints.empty()) |
| 989 | Constraints += ','; |
| 990 | if (RetWidth <= 32) { |
| 991 | Constraints += "={eax}"; |
| 992 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 993 | } else { |
| 994 | // Use the 'A' constraint for EAX:EDX. |
| 995 | Constraints += "=A"; |
| 996 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 997 | } |
| 998 | |
| 999 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1000 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1001 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1002 | |
| 1003 | // Coerce the integer by bitcasting the return slot pointer. |
| 1004 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 1005 | CoerceTy->getPointerTo())); |
| 1006 | ResultRegDests.push_back(ReturnSlot); |
| 1007 | |
| 1008 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1009 | } |
| 1010 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1011 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1012 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1013 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1014 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1015 | uint64_t Size = Context.getTypeSize(Ty); |
| 1016 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1017 | // For i386, type must be register sized. |
| 1018 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1019 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1020 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1021 | |
| 1022 | if (Ty->isVectorType()) { |
| 1023 | // 64- and 128- bit vectors inside structures are not returned in |
| 1024 | // registers. |
| 1025 | if (Size == 64 || Size == 128) |
| 1026 | return false; |
| 1027 | |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1031 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1032 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1033 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1034 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1035 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1036 | return true; |
| 1037 | |
| 1038 | // Arrays are treated like records. |
| 1039 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1040 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1043 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1044 | if (!RT) return false; |
| 1045 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1046 | // FIXME: Traverse bases here too. |
| 1047 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1048 | // Structure types are passed in register if all fields would be |
| 1049 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1050 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1051 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1052 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1053 | continue; |
| 1054 | |
| 1055 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1056 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1057 | return false; |
| 1058 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1059 | return true; |
| 1060 | } |
| 1061 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1062 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1063 | // If the return value is indirect, then the hidden argument is consuming one |
| 1064 | // integer register. |
| 1065 | if (State.FreeRegs) { |
| 1066 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1067 | if (!IsMCUABI) |
| 1068 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1069 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1070 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1073 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1074 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1075 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1076 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1077 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1078 | const Type *Base = nullptr; |
| 1079 | uint64_t NumElts = 0; |
| 1080 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1081 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1082 | // The LLVM struct type for such an aggregate should lower properly. |
| 1083 | return ABIArgInfo::getDirect(); |
| 1084 | } |
| 1085 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1086 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1087 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1088 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1089 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1090 | |
| 1091 | // 128-bit vectors are a special case; they are returned in |
| 1092 | // registers and we need to make sure to pick a type the LLVM |
| 1093 | // backend will like. |
| 1094 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1095 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1096 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1097 | |
| 1098 | // Always return in register if it fits in a general purpose |
| 1099 | // register, or if it is 64 bits and has a single element. |
| 1100 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1101 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1102 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1103 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1104 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1105 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1109 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1110 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1111 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1112 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1113 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1114 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1115 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1116 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1117 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1118 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1119 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1120 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1121 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1122 | // Ignore empty structs/unions. |
| 1123 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1124 | return ABIArgInfo::getIgnore(); |
| 1125 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1126 | // Small structures which are register sized are generally returned |
| 1127 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1128 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1129 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1130 | |
| 1131 | // As a special-case, if the struct is a "single-element" struct, and |
| 1132 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1133 | // floating-point register. (MSVC does not apply this special case.) |
| 1134 | // We apply a similar transformation for pointer types to improve the |
| 1135 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1136 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1137 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1138 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1139 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1140 | |
| 1141 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1142 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1143 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1146 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1147 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1148 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1149 | // Treat an enum type as its underlying type. |
| 1150 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1151 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1152 | |
| 1153 | return (RetTy->isPromotableIntegerType() ? |
| 1154 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1157 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1158 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1159 | } |
| 1160 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1161 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1162 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1163 | if (!RT) |
| 1164 | return 0; |
| 1165 | const RecordDecl *RD = RT->getDecl(); |
| 1166 | |
| 1167 | // If this is a C++ record, check the bases first. |
| 1168 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1169 | for (const auto &I : CXXRD->bases()) |
| 1170 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1173 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1174 | QualType FT = i->getType(); |
| 1175 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1176 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1177 | return true; |
| 1178 | |
| 1179 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1186 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1187 | unsigned Align) const { |
| 1188 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1189 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1190 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1191 | return 0; // Use default alignment. |
| 1192 | |
| 1193 | // On non-Darwin, the stack type alignment is always 4. |
| 1194 | if (!IsDarwinVectorABI) { |
| 1195 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1196 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1197 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1198 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1199 | // 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] | 1200 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1201 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1202 | return 16; |
| 1203 | |
| 1204 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1207 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1208 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1209 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1210 | if (State.FreeRegs) { |
| 1211 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1212 | if (!IsMCUABI) |
| 1213 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1214 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1215 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1216 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1217 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1218 | // Compute the byval alignment. |
| 1219 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1220 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1221 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1222 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1223 | |
| 1224 | // If the stack alignment is less than the type alignment, realign the |
| 1225 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1226 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1227 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1228 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1231 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1232 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1233 | if (!T) |
| 1234 | T = Ty.getTypePtr(); |
| 1235 | |
| 1236 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1237 | BuiltinType::Kind K = BT->getKind(); |
| 1238 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1239 | return Float; |
| 1240 | } |
| 1241 | return Integer; |
| 1242 | } |
| 1243 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1244 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1245 | if (!IsSoftFloatABI) { |
| 1246 | Class C = classify(Ty); |
| 1247 | if (C == Float) |
| 1248 | return false; |
| 1249 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1250 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1251 | unsigned Size = getContext().getTypeSize(Ty); |
| 1252 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1253 | |
| 1254 | if (SizeInRegs == 0) |
| 1255 | return false; |
| 1256 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1257 | if (!IsMCUABI) { |
| 1258 | if (SizeInRegs > State.FreeRegs) { |
| 1259 | State.FreeRegs = 0; |
| 1260 | return false; |
| 1261 | } |
| 1262 | } else { |
| 1263 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1264 | // earlier parameters that are passed on the stack. Also, |
| 1265 | // it does not allow passing >8-byte structs in-register, |
| 1266 | // even if there are 3 free registers available. |
| 1267 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1268 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1269 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1270 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1271 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1272 | return true; |
| 1273 | } |
| 1274 | |
| 1275 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
| 1276 | bool &InReg, |
| 1277 | bool &NeedsPadding) const { |
| 1278 | NeedsPadding = false; |
| 1279 | InReg = !IsMCUABI; |
| 1280 | |
| 1281 | if (!updateFreeRegs(Ty, State)) |
| 1282 | return false; |
| 1283 | |
| 1284 | if (IsMCUABI) |
| 1285 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1286 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1287 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1288 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1289 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1290 | NeedsPadding = true; |
| 1291 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1292 | return false; |
| 1293 | } |
| 1294 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1295 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1298 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1299 | if (!updateFreeRegs(Ty, State)) |
| 1300 | return false; |
| 1301 | |
| 1302 | if (IsMCUABI) |
| 1303 | return false; |
| 1304 | |
| 1305 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1306 | State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1307 | if (getContext().getTypeSize(Ty) > 32) |
| 1308 | return false; |
| 1309 | |
| 1310 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
| 1311 | Ty->isReferenceType()); |
| 1312 | } |
| 1313 | |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1317 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1318 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1319 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1320 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1321 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1322 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1323 | // Check with the C++ ABI first. |
| 1324 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1325 | if (RT) { |
| 1326 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1327 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1328 | return getIndirectResult(Ty, false, State); |
| 1329 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1330 | // The field index doesn't matter, we'll fix it up later. |
| 1331 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1336 | // to other targets. |
| 1337 | const Type *Base = nullptr; |
| 1338 | uint64_t NumElts = 0; |
| 1339 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1340 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1341 | if (State.FreeSSERegs >= NumElts) { |
| 1342 | State.FreeSSERegs -= NumElts; |
| 1343 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1344 | return ABIArgInfo::getDirect(); |
| 1345 | return ABIArgInfo::getExpand(); |
| 1346 | } |
| 1347 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1348 | } |
| 1349 | |
| 1350 | if (isAggregateTypeForABI(Ty)) { |
| 1351 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1352 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1353 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1354 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1355 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1356 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1357 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1358 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1359 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1360 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1361 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1362 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1363 | return ABIArgInfo::getIgnore(); |
| 1364 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1365 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1366 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1367 | bool NeedsPadding, InReg; |
| 1368 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1369 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1370 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1371 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1372 | if (InReg) |
| 1373 | return ABIArgInfo::getDirectInReg(Result); |
| 1374 | else |
| 1375 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1376 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1377 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1378 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1379 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1380 | // of those arguments will match the struct. This is important because the |
| 1381 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1382 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1383 | // Don't do this for the MCU if there are still free integer registers |
| 1384 | // (see X86_64 ABI for full explanation). |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1385 | if (getContext().getTypeSize(Ty) <= 4*32 && |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1386 | canExpandIndirectArgument(Ty, getContext()) && |
| 1387 | (!IsMCUABI || State.FreeRegs == 0)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1388 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1389 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1390 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1391 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1392 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1393 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1396 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1397 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1398 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1399 | if (IsDarwinVectorABI) { |
| 1400 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1401 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1402 | (Size == 64 && VT->getNumElements() == 1)) |
| 1403 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1404 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1405 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1406 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1407 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1408 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1409 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1410 | return ABIArgInfo::getDirect(); |
| 1411 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1412 | |
| 1413 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1414 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1415 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1416 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1417 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1418 | |
| 1419 | if (Ty->isPromotableIntegerType()) { |
| 1420 | if (InReg) |
| 1421 | return ABIArgInfo::getExtendInReg(); |
| 1422 | return ABIArgInfo::getExtend(); |
| 1423 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1424 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1425 | if (InReg) |
| 1426 | return ABIArgInfo::getDirectInReg(); |
| 1427 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1430 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1431 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1432 | if (IsMCUABI) |
| 1433 | State.FreeRegs = 3; |
| 1434 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1435 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1436 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1437 | State.FreeRegs = 2; |
| 1438 | State.FreeSSERegs = 6; |
| 1439 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1440 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1441 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1442 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1443 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1444 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1445 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1446 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1447 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1448 | // return value was sret and put it in a register ourselves if appropriate. |
| 1449 | if (State.FreeRegs) { |
| 1450 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1451 | if (!IsMCUABI) |
| 1452 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1453 | } |
| 1454 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1455 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1456 | // The chain argument effectively gives us another free register. |
| 1457 | if (FI.isChainCall()) |
| 1458 | ++State.FreeRegs; |
| 1459 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1460 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1461 | for (auto &I : FI.arguments()) { |
| 1462 | I.info = classifyArgumentType(I.type, State); |
| 1463 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1467 | // all the memory arguments to use inalloca. |
| 1468 | if (UsedInAlloca) |
| 1469 | rewriteWithInAlloca(FI); |
| 1470 | } |
| 1471 | |
| 1472 | void |
| 1473 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1474 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1475 | QualType Type) const { |
| 1476 | // Arguments are always 4-byte-aligned. |
| 1477 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1478 | |
| 1479 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1480 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1481 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1482 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1483 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1484 | // Insert padding bytes to respect alignment. |
| 1485 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1486 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1487 | if (StackOffset != FieldEnd) { |
| 1488 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1489 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1490 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1491 | FrameFields.push_back(Ty); |
| 1492 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1495 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1496 | // Leave ignored and inreg arguments alone. |
| 1497 | switch (Info.getKind()) { |
| 1498 | case ABIArgInfo::InAlloca: |
| 1499 | return true; |
| 1500 | case ABIArgInfo::Indirect: |
| 1501 | assert(Info.getIndirectByVal()); |
| 1502 | return true; |
| 1503 | case ABIArgInfo::Ignore: |
| 1504 | return false; |
| 1505 | case ABIArgInfo::Direct: |
| 1506 | case ABIArgInfo::Extend: |
| 1507 | case ABIArgInfo::Expand: |
| 1508 | if (Info.getInReg()) |
| 1509 | return false; |
| 1510 | return true; |
| 1511 | } |
| 1512 | llvm_unreachable("invalid enum"); |
| 1513 | } |
| 1514 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1515 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1516 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1517 | |
| 1518 | // Build a packed struct type for all of the arguments in memory. |
| 1519 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1520 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1521 | // The stack alignment is always 4. |
| 1522 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1523 | |
| 1524 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1525 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1526 | |
| 1527 | // Put 'this' into the struct before 'sret', if necessary. |
| 1528 | bool IsThisCall = |
| 1529 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1530 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1531 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1532 | isArgInAlloca(I->info)) { |
| 1533 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1534 | ++I; |
| 1535 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1536 | |
| 1537 | // 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] | 1538 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1539 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1540 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1541 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1542 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1546 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1547 | ++I; |
| 1548 | |
| 1549 | // Put arguments passed in memory into the struct. |
| 1550 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1551 | if (isArgInAlloca(I->info)) |
| 1552 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1556 | /*isPacked=*/true), |
| 1557 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1560 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1561 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1562 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1563 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1564 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1565 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1566 | // |
| 1567 | // Just messing with TypeInfo like this works because we never pass |
| 1568 | // anything indirectly. |
| 1569 | TypeInfo.second = CharUnits::fromQuantity( |
| 1570 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1571 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1572 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1573 | TypeInfo, CharUnits::fromQuantity(4), |
| 1574 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1577 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1578 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1579 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1580 | |
| 1581 | switch (Opts.getStructReturnConvention()) { |
| 1582 | case CodeGenOptions::SRCK_Default: |
| 1583 | break; |
| 1584 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1585 | return false; |
| 1586 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1587 | return true; |
| 1588 | } |
| 1589 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1590 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1591 | return true; |
| 1592 | |
| 1593 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1594 | case llvm::Triple::DragonFly: |
| 1595 | case llvm::Triple::FreeBSD: |
| 1596 | case llvm::Triple::OpenBSD: |
| 1597 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1598 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1599 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1600 | default: |
| 1601 | return false; |
| 1602 | } |
| 1603 | } |
| 1604 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1605 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1606 | llvm::GlobalValue *GV, |
| 1607 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1608 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1609 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1610 | // Get the LLVM function. |
| 1611 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1612 | |
| 1613 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1614 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1615 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1616 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1617 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1618 | llvm::AttributeSet::FunctionIndex, |
| 1619 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1620 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1621 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1622 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1623 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1624 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1628 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1629 | CodeGen::CodeGenFunction &CGF, |
| 1630 | llvm::Value *Address) const { |
| 1631 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1632 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1633 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1634 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1635 | // 0-7 are the eight integer registers; the order is different |
| 1636 | // on Darwin (for EH), but the range is the same. |
| 1637 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1638 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1639 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1640 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1641 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1642 | // These have size 16, which is sizeof(long double) on |
| 1643 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1644 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1645 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1646 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1647 | } else { |
| 1648 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1649 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1650 | Builder.CreateAlignedStore( |
| 1651 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1652 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1653 | |
| 1654 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1655 | // These have size 12, which is sizeof(long double) on |
| 1656 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1657 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1658 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1659 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1660 | |
| 1661 | return false; |
| 1662 | } |
| 1663 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1664 | //===----------------------------------------------------------------------===// |
| 1665 | // X86-64 ABI Implementation |
| 1666 | //===----------------------------------------------------------------------===// |
| 1667 | |
| 1668 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1669 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1670 | /// The AVX ABI level for X86 targets. |
| 1671 | enum class X86AVXABILevel { |
| 1672 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1673 | AVX, |
| 1674 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1675 | }; |
| 1676 | |
| 1677 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1678 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1679 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1680 | case X86AVXABILevel::AVX512: |
| 1681 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1682 | case X86AVXABILevel::AVX: |
| 1683 | return 256; |
| 1684 | case X86AVXABILevel::None: |
| 1685 | return 128; |
| 1686 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1687 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1690 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1691 | class X86_64ABIInfo : public ABIInfo { |
| 1692 | enum Class { |
| 1693 | Integer = 0, |
| 1694 | SSE, |
| 1695 | SSEUp, |
| 1696 | X87, |
| 1697 | X87Up, |
| 1698 | ComplexX87, |
| 1699 | NoClass, |
| 1700 | Memory |
| 1701 | }; |
| 1702 | |
| 1703 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1704 | /// |
| 1705 | /// Merge an accumulating classification \arg Accum with a field |
| 1706 | /// classification \arg Field. |
| 1707 | /// |
| 1708 | /// \param Accum - The accumulating classification. This should |
| 1709 | /// always be either NoClass or the result of a previous merge |
| 1710 | /// call. In addition, this should never be Memory (the caller |
| 1711 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1712 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1713 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1714 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1715 | /// |
| 1716 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1717 | /// final MEMORY or SSE classes when necessary. |
| 1718 | /// |
| 1719 | /// \param AggregateSize - The size of the current aggregate in |
| 1720 | /// the classification process. |
| 1721 | /// |
| 1722 | /// \param Lo - The classification for the parts of the type |
| 1723 | /// residing in the low word of the containing object. |
| 1724 | /// |
| 1725 | /// \param Hi - The classification for the parts of the type |
| 1726 | /// residing in the higher words of the containing object. |
| 1727 | /// |
| 1728 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1729 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1730 | /// classify - Determine the x86_64 register classes in which the |
| 1731 | /// given type T should be passed. |
| 1732 | /// |
| 1733 | /// \param Lo - The classification for the parts of the type |
| 1734 | /// residing in the low word of the containing object. |
| 1735 | /// |
| 1736 | /// \param Hi - The classification for the parts of the type |
| 1737 | /// residing in the high word of the containing object. |
| 1738 | /// |
| 1739 | /// \param OffsetBase - The bit offset of this type in the |
| 1740 | /// containing object. Some parameters are classified different |
| 1741 | /// depending on whether they straddle an eightbyte boundary. |
| 1742 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1743 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1744 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1745 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1746 | /// If a word is unused its result will be NoClass; if a type should |
| 1747 | /// be passed in Memory then at least the classification of \arg Lo |
| 1748 | /// will be Memory. |
| 1749 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1750 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1751 | /// |
| 1752 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1753 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1754 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1755 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1756 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1757 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1758 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1759 | unsigned IROffset, QualType SourceTy, |
| 1760 | unsigned SourceOffset) const; |
| 1761 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1762 | unsigned IROffset, QualType SourceTy, |
| 1763 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1764 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1765 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1766 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1767 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1768 | |
| 1769 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1770 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1771 | /// |
| 1772 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1773 | /// available. |
| 1774 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1776 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1777 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1778 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1779 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1780 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1781 | unsigned &neededSSE, |
| 1782 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1783 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1784 | bool IsIllegalVectorType(QualType Ty) const; |
| 1785 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1786 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1787 | /// unfortunately in ways that were not always consistent with |
| 1788 | /// certain previous compilers. In particular, platforms which |
| 1789 | /// required strict binary compatibility with older versions of GCC |
| 1790 | /// may need to exempt themselves. |
| 1791 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1792 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1795 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1796 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1797 | // 64-bit hardware. |
| 1798 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1799 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1800 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1801 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| 1802 | ABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1803 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1804 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1805 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1806 | bool isPassedUsingAVXType(QualType type) const { |
| 1807 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1808 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1809 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1810 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1811 | if (info.isDirect()) { |
| 1812 | llvm::Type *ty = info.getCoerceToType(); |
| 1813 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1814 | return (vectorTy->getBitWidth() > 128); |
| 1815 | } |
| 1816 | return false; |
| 1817 | } |
| 1818 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1819 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1820 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1821 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1822 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1823 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1824 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1825 | |
| 1826 | bool has64BitPointers() const { |
| 1827 | return Has64BitPointers; |
| 1828 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1829 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1830 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1831 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1832 | class WinX86_64ABIInfo : public ABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1833 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1834 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
| 1835 | : ABIInfo(CGT), |
| 1836 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1837 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1838 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1839 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1840 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1841 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1842 | |
| 1843 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1844 | // FIXME: Assumes vectorcall is in use. |
| 1845 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1846 | } |
| 1847 | |
| 1848 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1849 | uint64_t NumMembers) const override { |
| 1850 | // FIXME: Assumes vectorcall is in use. |
| 1851 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1852 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1853 | |
| 1854 | private: |
| 1855 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1856 | bool IsReturnType) const; |
| 1857 | |
| 1858 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1859 | }; |
| 1860 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1861 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1862 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1863 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1864 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1865 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1866 | const X86_64ABIInfo &getABIInfo() const { |
| 1867 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1868 | } |
| 1869 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1870 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1871 | return 7; |
| 1872 | } |
| 1873 | |
| 1874 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1875 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1876 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1877 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1878 | // 0-15 are the 16 integer registers. |
| 1879 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1880 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1881 | return false; |
| 1882 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1883 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1884 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1885 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1886 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1887 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1888 | } |
| 1889 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1890 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1891 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1892 | // The default CC on x86-64 sets %al to the number of SSA |
| 1893 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1894 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1895 | // that when AVX types are involved: the ABI explicitly states it is |
| 1896 | // undefined, and it doesn't work in practice because of how the ABI |
| 1897 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1898 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1899 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1900 | for (CallArgList::const_iterator |
| 1901 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1902 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1903 | HasAVXType = true; |
| 1904 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1905 | } |
| 1906 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1907 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1908 | if (!HasAVXType) |
| 1909 | return true; |
| 1910 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1911 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1912 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1915 | llvm::Constant * |
| 1916 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1917 | unsigned Sig; |
| 1918 | if (getABIInfo().has64BitPointers()) |
| 1919 | Sig = (0xeb << 0) | // jmp rel8 |
| 1920 | (0x0a << 8) | // .+0x0c |
| 1921 | ('F' << 16) | |
| 1922 | ('T' << 24); |
| 1923 | else |
| 1924 | Sig = (0xeb << 0) | // jmp rel8 |
| 1925 | (0x06 << 8) | // .+0x08 |
| 1926 | ('F' << 16) | |
| 1927 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1928 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1929 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1930 | |
| 1931 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1932 | CodeGen::CodeGenModule &CGM) const override { |
| 1933 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 1934 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1935 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1936 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1937 | } |
| 1938 | } |
| 1939 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1940 | }; |
| 1941 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1942 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1943 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1944 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1945 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1946 | |
| 1947 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1948 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1949 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 1950 | // If the argument contains a space, enclose it in quotes. |
| 1951 | if (Lib.find(" ") != StringRef::npos) |
| 1952 | Opt += "\"" + Lib.str() + "\""; |
| 1953 | else |
| 1954 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1955 | } |
| 1956 | }; |
| 1957 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1958 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1959 | // If the argument does not end in .lib, automatically add the suffix. |
| 1960 | // If the argument contains a space, enclose it in quotes. |
| 1961 | // This matches the behavior of MSVC. |
| 1962 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1963 | std::string ArgStr = Quote ? "\"" : ""; |
| 1964 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1965 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1966 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1967 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1968 | return ArgStr; |
| 1969 | } |
| 1970 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1971 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1972 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1973 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1974 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 1975 | unsigned NumRegisterParameters) |
| 1976 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1977 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1978 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1979 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1980 | CodeGen::CodeGenModule &CGM) const override; |
| 1981 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1982 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1983 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1984 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1985 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1986 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1987 | |
| 1988 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1989 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1990 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1991 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1992 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1993 | }; |
| 1994 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1995 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1996 | llvm::GlobalValue *GV, |
| 1997 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1998 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1999 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 2000 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2001 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2002 | Fn->addFnAttr("stack-probe-size", |
| 2003 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2008 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2009 | llvm::GlobalValue *GV, |
| 2010 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2011 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2012 | |
| 2013 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2014 | } |
| 2015 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2016 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2017 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2018 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2019 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2020 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2021 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2022 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2023 | CodeGen::CodeGenModule &CGM) const override; |
| 2024 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2025 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2026 | return 7; |
| 2027 | } |
| 2028 | |
| 2029 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2030 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2031 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2032 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2033 | // 0-15 are the 16 integer registers. |
| 2034 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2035 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2036 | return false; |
| 2037 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2038 | |
| 2039 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2040 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2041 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2042 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2043 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2044 | |
| 2045 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2046 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2047 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2048 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2049 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2050 | }; |
| 2051 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2052 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2053 | llvm::GlobalValue *GV, |
| 2054 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2055 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2056 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2057 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 2058 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2059 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2060 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2061 | } |
| 2062 | } |
| 2063 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2064 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2065 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2066 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2067 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2068 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2069 | Class &Hi) const { |
| 2070 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2071 | // |
| 2072 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2073 | // memory. |
| 2074 | // |
| 2075 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2076 | // memory. |
| 2077 | // |
| 2078 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2079 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2080 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2081 | // ABI working for processors that don't support the __m256 type. |
| 2082 | // |
| 2083 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2084 | // |
| 2085 | // Some of these are enforced by the merging logic. Others can arise |
| 2086 | // only with unions; for example: |
| 2087 | // union { _Complex double; unsigned; } |
| 2088 | // |
| 2089 | // Note that clauses (b) and (c) were added in 0.98. |
| 2090 | // |
| 2091 | if (Hi == Memory) |
| 2092 | Lo = Memory; |
| 2093 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2094 | Lo = Memory; |
| 2095 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2096 | Lo = Memory; |
| 2097 | if (Hi == SSEUp && Lo != SSE) |
| 2098 | Hi = SSE; |
| 2099 | } |
| 2100 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2101 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2102 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2103 | // classified recursively so that always two fields are |
| 2104 | // considered. The resulting class is calculated according to |
| 2105 | // the classes of the fields in the eightbyte: |
| 2106 | // |
| 2107 | // (a) If both classes are equal, this is the resulting class. |
| 2108 | // |
| 2109 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2110 | // the other class. |
| 2111 | // |
| 2112 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2113 | // class. |
| 2114 | // |
| 2115 | // (d) If one of the classes is INTEGER, the result is the |
| 2116 | // INTEGER. |
| 2117 | // |
| 2118 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2119 | // MEMORY is used as class. |
| 2120 | // |
| 2121 | // (f) Otherwise class SSE is used. |
| 2122 | |
| 2123 | // Accum should never be memory (we should have returned) or |
| 2124 | // ComplexX87 (because this cannot be passed in a structure). |
| 2125 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2126 | "Invalid accumulated classification during merge."); |
| 2127 | if (Accum == Field || Field == NoClass) |
| 2128 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2129 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2130 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2131 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2132 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2133 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2134 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2135 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2136 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2137 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2138 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2141 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2142 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2143 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2144 | // Class pairs with appropriate constructor methods for the various |
| 2145 | // situations. |
| 2146 | |
| 2147 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2148 | // shouldn't be passed in registers for example, so there is no chance they |
| 2149 | // can straddle an eightbyte. Verify & simplify. |
| 2150 | |
| 2151 | Lo = Hi = NoClass; |
| 2152 | |
| 2153 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2154 | Current = Memory; |
| 2155 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2156 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2157 | BuiltinType::Kind k = BT->getKind(); |
| 2158 | |
| 2159 | if (k == BuiltinType::Void) { |
| 2160 | Current = NoClass; |
| 2161 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2162 | Lo = Integer; |
| 2163 | Hi = Integer; |
| 2164 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2165 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2166 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2167 | Current = SSE; |
| 2168 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2169 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2170 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2171 | Lo = SSE; |
| 2172 | Hi = SSEUp; |
| 2173 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2174 | Lo = X87; |
| 2175 | Hi = X87Up; |
| 2176 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2177 | Current = SSE; |
| 2178 | } else |
| 2179 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2180 | } |
| 2181 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2182 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2183 | return; |
| 2184 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2185 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2186 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2187 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2188 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2189 | return; |
| 2190 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2191 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2192 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2193 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2194 | return; |
| 2195 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2196 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2197 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2198 | if (Ty->isMemberFunctionPointerType()) { |
| 2199 | if (Has64BitPointers) { |
| 2200 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2201 | // Lo and Hi now. |
| 2202 | Lo = Hi = Integer; |
| 2203 | } else { |
| 2204 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2205 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2206 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2207 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2208 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2209 | Lo = Hi = Integer; |
| 2210 | } else { |
| 2211 | Current = Integer; |
| 2212 | } |
| 2213 | } |
| 2214 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2215 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2216 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2217 | return; |
| 2218 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2219 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2220 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2221 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2222 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2223 | // gcc passes the following as integer: |
| 2224 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2225 | // 2 bytes - <2 x char>, <1 x short> |
| 2226 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2227 | Current = Integer; |
| 2228 | |
| 2229 | // If this type crosses an eightbyte boundary, it should be |
| 2230 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2231 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2232 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2233 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2234 | Hi = Lo; |
| 2235 | } else if (Size == 64) { |
| 2236 | // gcc passes <1 x double> in memory. :( |
| 2237 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 2238 | return; |
| 2239 | |
| 2240 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 2241 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 2242 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2243 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 2244 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2245 | Current = Integer; |
| 2246 | else |
| 2247 | Current = SSE; |
| 2248 | |
| 2249 | // If this type crosses an eightbyte boundary, it should be |
| 2250 | // split. |
| 2251 | if (OffsetBase && OffsetBase != 64) |
| 2252 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2253 | } else if (Size == 128 || |
| 2254 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2255 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2256 | // least significant one belongs to class SSE and all the others to class |
| 2257 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2258 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2259 | // This design isn't correct for 256-bits, but since there're no cases |
| 2260 | // where the upper parts would need to be inspected, avoid adding |
| 2261 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2262 | // |
| 2263 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2264 | // registers if they are "named", i.e. not part of the "..." of a |
| 2265 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2266 | // |
| 2267 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2268 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2269 | Lo = SSE; |
| 2270 | Hi = SSEUp; |
| 2271 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2272 | return; |
| 2273 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2274 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2275 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2276 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2277 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2278 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2279 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2280 | if (Size <= 64) |
| 2281 | Current = Integer; |
| 2282 | else if (Size <= 128) |
| 2283 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2284 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2285 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2286 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2287 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2288 | } else if (ET == getContext().LongDoubleTy) { |
| 2289 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2290 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2291 | Current = Memory; |
| 2292 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2293 | Current = ComplexX87; |
| 2294 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2295 | Lo = Hi = SSE; |
| 2296 | else |
| 2297 | llvm_unreachable("unexpected long double representation!"); |
| 2298 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2299 | |
| 2300 | // If this complex type crosses an eightbyte boundary then it |
| 2301 | // should be split. |
| 2302 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2303 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2304 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2305 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2306 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2307 | return; |
| 2308 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2309 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2310 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2311 | // Arrays are treated like structures. |
| 2312 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2313 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2314 | |
| 2315 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2316 | // than four eightbytes, ..., it has class MEMORY. |
| 2317 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2318 | return; |
| 2319 | |
| 2320 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2321 | // fields, it has class MEMORY. |
| 2322 | // |
| 2323 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2324 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2325 | return; |
| 2326 | |
| 2327 | // Otherwise implement simplified merge. We could be smarter about |
| 2328 | // this, but it isn't worth it and would be harder to verify. |
| 2329 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2330 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2331 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2332 | |
| 2333 | // The only case a 256-bit wide vector could be used is when the array |
| 2334 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2335 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2336 | if (Size > 128 && EltSize != 256) |
| 2337 | return; |
| 2338 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2339 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2340 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2341 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2342 | Lo = merge(Lo, FieldLo); |
| 2343 | Hi = merge(Hi, FieldHi); |
| 2344 | if (Lo == Memory || Hi == Memory) |
| 2345 | break; |
| 2346 | } |
| 2347 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2348 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2349 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2352 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2353 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2354 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2355 | |
| 2356 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2357 | // than four eightbytes, ..., it has class MEMORY. |
| 2358 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2359 | return; |
| 2360 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2361 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2362 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2363 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2364 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2365 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2366 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2367 | const RecordDecl *RD = RT->getDecl(); |
| 2368 | |
| 2369 | // Assume variable sized types are passed in memory. |
| 2370 | if (RD->hasFlexibleArrayMember()) |
| 2371 | return; |
| 2372 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2373 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Reset Lo class, this will be recomputed. |
| 2376 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2377 | |
| 2378 | // If this is a C++ record, classify the bases first. |
| 2379 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2380 | for (const auto &I : CXXRD->bases()) { |
| 2381 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2382 | "Unexpected base class!"); |
| 2383 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2384 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2385 | |
| 2386 | // Classify this field. |
| 2387 | // |
| 2388 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2389 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2390 | // initialized to class NO_CLASS. |
| 2391 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2392 | uint64_t Offset = |
| 2393 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2394 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2395 | Lo = merge(Lo, FieldLo); |
| 2396 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2397 | if (Lo == Memory || Hi == Memory) { |
| 2398 | postMerge(Size, Lo, Hi); |
| 2399 | return; |
| 2400 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2405 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2406 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2407 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2408 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2409 | bool BitField = i->isBitField(); |
| 2410 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2411 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2412 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2413 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2414 | // The only case a 256-bit wide vector could be used is when the struct |
| 2415 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2416 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2417 | // |
| 2418 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2419 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2420 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2421 | return; |
| 2422 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2423 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2424 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2425 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2426 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2427 | return; |
| 2428 | } |
| 2429 | |
| 2430 | // Classify this field. |
| 2431 | // |
| 2432 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2433 | // exceeds a single eightbyte, each is classified |
| 2434 | // separately. Each eightbyte gets initialized to class |
| 2435 | // NO_CLASS. |
| 2436 | Class FieldLo, FieldHi; |
| 2437 | |
| 2438 | // Bit-fields require special handling, they do not force the |
| 2439 | // structure to be passed in memory even if unaligned, and |
| 2440 | // therefore they can straddle an eightbyte. |
| 2441 | if (BitField) { |
| 2442 | // Ignore padding bit-fields. |
| 2443 | if (i->isUnnamedBitfield()) |
| 2444 | continue; |
| 2445 | |
| 2446 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2447 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2448 | |
| 2449 | uint64_t EB_Lo = Offset / 64; |
| 2450 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2451 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2452 | if (EB_Lo) { |
| 2453 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2454 | FieldLo = NoClass; |
| 2455 | FieldHi = Integer; |
| 2456 | } else { |
| 2457 | FieldLo = Integer; |
| 2458 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2459 | } |
| 2460 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2461 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2462 | Lo = merge(Lo, FieldLo); |
| 2463 | Hi = merge(Hi, FieldHi); |
| 2464 | if (Lo == Memory || Hi == Memory) |
| 2465 | break; |
| 2466 | } |
| 2467 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2468 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2469 | } |
| 2470 | } |
| 2471 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2472 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2473 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2474 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2475 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2476 | // Treat an enum type as its underlying type. |
| 2477 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2478 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2479 | |
| 2480 | return (Ty->isPromotableIntegerType() ? |
| 2481 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2482 | } |
| 2483 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2484 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2487 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2488 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2489 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2490 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2491 | if (Size <= 64 || Size > LargestVector) |
| 2492 | return true; |
| 2493 | } |
| 2494 | |
| 2495 | return false; |
| 2496 | } |
| 2497 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2498 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2499 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2500 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2501 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2502 | // |
| 2503 | // This assumption is optimistic, as there could be free registers available |
| 2504 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2505 | // the argument in the free register. This does not seem to happen currently, |
| 2506 | // but this code would be much safer if we could mark the argument with |
| 2507 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2508 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2509 | // Treat an enum type as its underlying type. |
| 2510 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2511 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2512 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2513 | return (Ty->isPromotableIntegerType() ? |
| 2514 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2515 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2516 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2517 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2518 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2519 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2520 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2521 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2522 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2523 | |
| 2524 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2525 | // is important for good codegen. |
| 2526 | // |
| 2527 | // We do this by coercing the value into a scalar type which the backend can |
| 2528 | // handle naturally (i.e., without using byval). |
| 2529 | // |
| 2530 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2531 | // free integer registers. Doing this when there are free integer registers |
| 2532 | // would require more care, as we would have to ensure that the coerced value |
| 2533 | // did not claim the unused register. That would require either reording the |
| 2534 | // arguments to the function (so that any subsequent inreg values came first), |
| 2535 | // or only doing this optimization when there were no following arguments that |
| 2536 | // might be inreg. |
| 2537 | // |
| 2538 | // We currently expect it to be rare (particularly in well written code) for |
| 2539 | // arguments to be passed on the stack when there are still free integer |
| 2540 | // registers available (this would typically imply large structs being passed |
| 2541 | // by value), so this seems like a fair tradeoff for now. |
| 2542 | // |
| 2543 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2544 | // attributes. See PR12193. |
| 2545 | if (freeIntRegs == 0) { |
| 2546 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2547 | |
| 2548 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2549 | // type, which will end up on the stack (with alignment 8). |
| 2550 | if (Align == 8 && Size <= 64) |
| 2551 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2552 | Size)); |
| 2553 | } |
| 2554 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2555 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2558 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2559 | /// 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] | 2560 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2561 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2562 | // vectors; strip them off if present. |
| 2563 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2564 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2565 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2566 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2567 | if (isa<llvm::VectorType>(IRType) || |
| 2568 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2569 | return IRType; |
| 2570 | |
| 2571 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2572 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2573 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2574 | |
| 2575 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2576 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2577 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2580 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2581 | /// is known to either be off the end of the specified type or being in |
| 2582 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2583 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2584 | /// classification that put one of the two halves in the INTEGER class. |
| 2585 | /// |
| 2586 | /// It is conservatively correct to return false. |
| 2587 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2588 | unsigned EndBit, ASTContext &Context) { |
| 2589 | // If the bytes being queried are off the end of the type, there is no user |
| 2590 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2591 | // types that don't contain interesting padding. |
| 2592 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2593 | if (TySize <= StartBit) |
| 2594 | return true; |
| 2595 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2596 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2597 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2598 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2599 | |
| 2600 | // Check each element to see if the element overlaps with the queried range. |
| 2601 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2602 | // If the element is after the span we care about, then we're done.. |
| 2603 | unsigned EltOffset = i*EltSize; |
| 2604 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2606 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2607 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2608 | EndBit-EltOffset, Context)) |
| 2609 | return false; |
| 2610 | } |
| 2611 | // If it overlaps no elements, then it is safe to process as padding. |
| 2612 | return true; |
| 2613 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2614 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2615 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2616 | const RecordDecl *RD = RT->getDecl(); |
| 2617 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2618 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2619 | // If this is a C++ record, check the bases first. |
| 2620 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2621 | for (const auto &I : CXXRD->bases()) { |
| 2622 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2623 | "Unexpected base class!"); |
| 2624 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2625 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2626 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2627 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2628 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2629 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2630 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2631 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2632 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2633 | EndBit-BaseOffset, Context)) |
| 2634 | return false; |
| 2635 | } |
| 2636 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2638 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2639 | // this could be sped up a lot by being smarter about queried fields, |
| 2640 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2641 | // much. |
| 2642 | unsigned idx = 0; |
| 2643 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2644 | i != e; ++i, ++idx) { |
| 2645 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2646 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2647 | // If we found a field after the region we care about, then we're done. |
| 2648 | if (FieldOffset >= EndBit) break; |
| 2649 | |
| 2650 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2651 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2652 | Context)) |
| 2653 | return false; |
| 2654 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2655 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2656 | // If nothing in this record overlapped the area of interest, then we're |
| 2657 | // clean. |
| 2658 | return true; |
| 2659 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2660 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2661 | return false; |
| 2662 | } |
| 2663 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2664 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2665 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2666 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2667 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2668 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2669 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2670 | // Base case if we find a float. |
| 2671 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2672 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2674 | // 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] | 2675 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2676 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2677 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2678 | IROffset -= SL->getElementOffset(Elt); |
| 2679 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2680 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2681 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2682 | // 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] | 2683 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2684 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2685 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2686 | IROffset -= IROffset/EltSize*EltSize; |
| 2687 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2688 | } |
| 2689 | |
| 2690 | return false; |
| 2691 | } |
| 2692 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2693 | |
| 2694 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2695 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2696 | llvm::Type *X86_64ABIInfo:: |
| 2697 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2698 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2699 | // 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] | 2700 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2701 | // structs that contain 3 floats. |
| 2702 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2703 | SourceOffset*8+64, getContext())) |
| 2704 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2705 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2706 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2707 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2708 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2709 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2710 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2711 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2712 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2713 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2714 | } |
| 2715 | |
| 2716 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2717 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2718 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2719 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2720 | /// 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] | 2721 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2722 | /// etc). |
| 2723 | /// |
| 2724 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2725 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2726 | /// the 8-byte value references. PrefType may be null. |
| 2727 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2728 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2729 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2730 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2731 | llvm::Type *X86_64ABIInfo:: |
| 2732 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2733 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2734 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2735 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2736 | if (IROffset == 0) { |
| 2737 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2738 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2739 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2740 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2741 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2742 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2743 | // goodness in the source type is just tail padding. This is allowed to |
| 2744 | // kick in for struct {double,int} on the int, but not on |
| 2745 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2746 | // have to do this analysis on the source type because we can't depend on |
| 2747 | // unions being lowered a specific way etc. |
| 2748 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2749 | IRType->isIntegerTy(32) || |
| 2750 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2751 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2752 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2753 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2754 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2755 | SourceOffset*8+64, getContext())) |
| 2756 | return IRType; |
| 2757 | } |
| 2758 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2760 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2761 | // 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] | 2762 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2763 | if (IROffset < SL->getSizeInBytes()) { |
| 2764 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2765 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2766 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2767 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2768 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2769 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2770 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2771 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2772 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2773 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2774 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2775 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2776 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2777 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2778 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2779 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2780 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2781 | // 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] | 2782 | unsigned TySizeInBytes = |
| 2783 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2784 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2785 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2787 | // It is always safe to classify this as an integer type up to i64 that |
| 2788 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2789 | return llvm::IntegerType::get(getVMContext(), |
| 2790 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2793 | |
| 2794 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2795 | /// be used as elements of a two register pair to pass or return, return a |
| 2796 | /// first class aggregate to represent them. For example, if the low part of |
| 2797 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2798 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2799 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2800 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2801 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2802 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2803 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2804 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2805 | // the second element at offset 8. Check for this: |
| 2806 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2807 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 2808 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2809 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2810 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2811 | // To handle this, we have to increase the size of the low part so that the |
| 2812 | // second element will start at an 8 byte offset. We can't increase the size |
| 2813 | // of the second element because it might make us access off the end of the |
| 2814 | // struct. |
| 2815 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 2816 | // There are usually two sorts of types the ABI generation code can produce |
| 2817 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 2818 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 2819 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2820 | // Promote these to a larger type. |
| 2821 | if (Lo->isFloatTy()) |
| 2822 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2823 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 2824 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 2825 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2826 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2827 | } |
| 2828 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2829 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2830 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2831 | |
| 2832 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2833 | // Verify that the second element is at an 8-byte offset. |
| 2834 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2835 | "Invalid x86-64 argument pair!"); |
| 2836 | return Result; |
| 2837 | } |
| 2838 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2839 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2840 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2841 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2842 | // classification algorithm. |
| 2843 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2844 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2845 | |
| 2846 | // Check some invariants. |
| 2847 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2848 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2849 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2850 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2851 | switch (Lo) { |
| 2852 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2853 | if (Hi == NoClass) |
| 2854 | return ABIArgInfo::getIgnore(); |
| 2855 | // If the low part is just padding, it takes no register, leave ResType |
| 2856 | // null. |
| 2857 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2858 | "Unknown missing lo part"); |
| 2859 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2860 | |
| 2861 | case SSEUp: |
| 2862 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2863 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2864 | |
| 2865 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2866 | // hidden argument. |
| 2867 | case Memory: |
| 2868 | return getIndirectReturnResult(RetTy); |
| 2869 | |
| 2870 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2871 | // available register of the sequence %rax, %rdx is used. |
| 2872 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2873 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2874 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2875 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2876 | // so that the parameter gets the right LLVM IR attributes. |
| 2877 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2878 | // Treat an enum type as its underlying type. |
| 2879 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2880 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2881 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2882 | if (RetTy->isIntegralOrEnumerationType() && |
| 2883 | RetTy->isPromotableIntegerType()) |
| 2884 | return ABIArgInfo::getExtend(); |
| 2885 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2886 | break; |
| 2887 | |
| 2888 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2889 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2890 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2891 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2892 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2893 | |
| 2894 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2895 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2896 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2897 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2898 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2899 | |
| 2900 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2901 | // part of the value is returned in %st0 and the imaginary part in |
| 2902 | // %st1. |
| 2903 | case ComplexX87: |
| 2904 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2905 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2906 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2907 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2908 | break; |
| 2909 | } |
| 2910 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2911 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2912 | switch (Hi) { |
| 2913 | // Memory was handled previously and X87 should |
| 2914 | // never occur as a hi class. |
| 2915 | case Memory: |
| 2916 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2917 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2918 | |
| 2919 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2920 | case NoClass: |
| 2921 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2922 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2923 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2924 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2925 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2926 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2927 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2928 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2929 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2930 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2931 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2932 | break; |
| 2933 | |
| 2934 | // 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] | 2935 | // is passed in the next available eightbyte chunk if the last used |
| 2936 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2937 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2938 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2939 | case SSEUp: |
| 2940 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2941 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2942 | break; |
| 2943 | |
| 2944 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2945 | // returned together with the previous X87 value in %st0. |
| 2946 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2947 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2948 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2949 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2950 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2951 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2952 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2953 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2954 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2955 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2956 | break; |
| 2957 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2958 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2959 | // 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] | 2960 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2961 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2962 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2963 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2964 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2965 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2968 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2969 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2970 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2971 | const |
| 2972 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2973 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2974 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2975 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2976 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2977 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2978 | // Check some invariants. |
| 2979 | // FIXME: Enforce these by construction. |
| 2980 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2981 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2982 | |
| 2983 | neededInt = 0; |
| 2984 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2985 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2986 | switch (Lo) { |
| 2987 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2988 | if (Hi == NoClass) |
| 2989 | return ABIArgInfo::getIgnore(); |
| 2990 | // If the low part is just padding, it takes no register, leave ResType |
| 2991 | // null. |
| 2992 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2993 | "Unknown missing lo part"); |
| 2994 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2995 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2996 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2997 | // on the stack. |
| 2998 | case Memory: |
| 2999 | |
| 3000 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3001 | // COMPLEX_X87, it is passed in memory. |
| 3002 | case X87: |
| 3003 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3004 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3005 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3006 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3007 | |
| 3008 | case SSEUp: |
| 3009 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3010 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3011 | |
| 3012 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3013 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3014 | // and %r9 is used. |
| 3015 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3016 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3017 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3018 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3019 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3020 | |
| 3021 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3022 | // so that the parameter gets the right LLVM IR attributes. |
| 3023 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3024 | // Treat an enum type as its underlying type. |
| 3025 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3026 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3027 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3028 | if (Ty->isIntegralOrEnumerationType() && |
| 3029 | Ty->isPromotableIntegerType()) |
| 3030 | return ABIArgInfo::getExtend(); |
| 3031 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3032 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3033 | break; |
| 3034 | |
| 3035 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3036 | // available SSE register is used, the registers are taken in the |
| 3037 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3038 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3039 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3040 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3041 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3042 | break; |
| 3043 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3044 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3045 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3046 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3047 | switch (Hi) { |
| 3048 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3049 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3050 | // which is passed in memory. |
| 3051 | case Memory: |
| 3052 | case X87: |
| 3053 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3054 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3055 | |
| 3056 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3057 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3058 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3059 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3060 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3061 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3062 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3063 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3064 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3065 | break; |
| 3066 | |
| 3067 | // X87Up generally doesn't occur here (long double is passed in |
| 3068 | // memory), except in situations involving unions. |
| 3069 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3070 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3071 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3072 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3073 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3074 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3075 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3076 | ++neededSSE; |
| 3077 | break; |
| 3078 | |
| 3079 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3080 | // 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] | 3081 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3082 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3083 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3084 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3085 | break; |
| 3086 | } |
| 3087 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3088 | // If a high part was specified, merge it together with the low part. It is |
| 3089 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3090 | // first class struct aggregate with the high and low part: {low, high} |
| 3091 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3092 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3093 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3094 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3097 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3098 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3099 | if (!getCXXABI().classifyReturnType(FI)) |
| 3100 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3101 | |
| 3102 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3103 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3104 | |
| 3105 | // If the return value is indirect, then the hidden argument is consuming one |
| 3106 | // integer register. |
| 3107 | if (FI.getReturnInfo().isIndirect()) |
| 3108 | --freeIntRegs; |
| 3109 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3110 | // The chain argument effectively gives us another free register. |
| 3111 | if (FI.isChainCall()) |
| 3112 | ++freeIntRegs; |
| 3113 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3114 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3115 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3116 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3117 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3118 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3119 | it != ie; ++it, ++ArgNo) { |
| 3120 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3121 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3122 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3123 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3124 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3125 | |
| 3126 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3127 | // eightbyte of an argument, the whole argument is passed on the |
| 3128 | // stack. If registers have already been assigned for some |
| 3129 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3130 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3131 | freeIntRegs -= neededInt; |
| 3132 | freeSSERegs -= neededSSE; |
| 3133 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3134 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3135 | } |
| 3136 | } |
| 3137 | } |
| 3138 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3139 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3140 | Address VAListAddr, QualType Ty) { |
| 3141 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3142 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3143 | llvm::Value *overflow_arg_area = |
| 3144 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3145 | |
| 3146 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3147 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3148 | // It isn't stated explicitly in the standard, but in practice we use |
| 3149 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3150 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3151 | if (Align > CharUnits::fromQuantity(8)) { |
| 3152 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3153 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | // 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] | 3157 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3158 | llvm::Value *Res = |
| 3159 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3160 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3161 | |
| 3162 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3163 | // l->overflow_arg_area + sizeof(type). |
| 3164 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3165 | // an 8 byte boundary. |
| 3166 | |
| 3167 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3168 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3169 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3170 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3171 | "overflow_arg_area.next"); |
| 3172 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3173 | |
| 3174 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3175 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3178 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3179 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3180 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3181 | // struct { |
| 3182 | // i32 gp_offset; |
| 3183 | // i32 fp_offset; |
| 3184 | // i8* overflow_arg_area; |
| 3185 | // i8* reg_save_area; |
| 3186 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3187 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3188 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3189 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3190 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3191 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3192 | |
| 3193 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3194 | // in the registers. If not go to step 7. |
| 3195 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3196 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3197 | |
| 3198 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3199 | // general purpose registers needed to pass type and num_fp to hold |
| 3200 | // the number of floating point registers needed. |
| 3201 | |
| 3202 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3203 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3204 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3205 | // |
| 3206 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3207 | // register save space). |
| 3208 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3209 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3210 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3211 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3212 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3213 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3214 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3215 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3216 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3217 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3218 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3222 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3223 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3224 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3225 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3226 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3227 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3228 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3229 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3230 | } |
| 3231 | |
| 3232 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3233 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3234 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3235 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3236 | |
| 3237 | // Emit code to load the value if it was passed in registers. |
| 3238 | |
| 3239 | CGF.EmitBlock(InRegBlock); |
| 3240 | |
| 3241 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3242 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3243 | // copying to a temporary location in case the parameter is passed |
| 3244 | // in different register classes or requires an alignment greater |
| 3245 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3246 | // |
| 3247 | // FIXME: This really results in shameful code when we end up needing to |
| 3248 | // collect arguments from different places; often what should result in a |
| 3249 | // simple assembling of a structure from scattered addresses has many more |
| 3250 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3251 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3252 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3253 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3254 | "reg_save_area"); |
| 3255 | |
| 3256 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3257 | if (neededInt && neededSSE) { |
| 3258 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3259 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3260 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3261 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3262 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3263 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3264 | llvm::Type *TyLo = ST->getElementType(0); |
| 3265 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3266 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3267 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3268 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3269 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3270 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3271 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3272 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3273 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3274 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3275 | // Copy the first element. |
| 3276 | llvm::Value *V = |
| 3277 | CGF.Builder.CreateDefaultAlignedLoad( |
| 3278 | CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 3279 | CGF.Builder.CreateStore(V, |
| 3280 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3281 | |
| 3282 | // Copy the second element. |
| 3283 | V = CGF.Builder.CreateDefaultAlignedLoad( |
| 3284 | CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 3285 | CharUnits Offset = CharUnits::fromQuantity( |
| 3286 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3287 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3288 | |
| 3289 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3290 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3291 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3292 | CharUnits::fromQuantity(8)); |
| 3293 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3294 | |
| 3295 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3296 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3297 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3298 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3299 | CharUnits TyAlign = SizeAlign.second; |
| 3300 | |
| 3301 | // Copy into a temporary if the type is more aligned than the |
| 3302 | // register save area. |
| 3303 | if (TyAlign.getQuantity() > 8) { |
| 3304 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3305 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3306 | RegAddr = Tmp; |
| 3307 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3308 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3309 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3310 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3311 | CharUnits::fromQuantity(16)); |
| 3312 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3313 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3314 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3315 | // SSE registers are spaced 16 bytes apart in the register save |
| 3316 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3317 | // The ABI isn't explicit about this, but it seems reasonable |
| 3318 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3319 | // naturally 16-byte aligned and the prologue is expected to store |
| 3320 | // all the SSE registers to the RSA. |
| 3321 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3322 | CharUnits::fromQuantity(16)); |
| 3323 | Address RegAddrHi = |
| 3324 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3325 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3326 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3327 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3328 | llvm::Value *V; |
| 3329 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3330 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3331 | V = CGF.Builder.CreateLoad( |
| 3332 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3333 | CGF.Builder.CreateStore(V, |
| 3334 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3335 | V = CGF.Builder.CreateLoad( |
| 3336 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3337 | CGF.Builder.CreateStore(V, |
| 3338 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3339 | |
| 3340 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3344 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3345 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3346 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3347 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3348 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3349 | gp_offset_p); |
| 3350 | } |
| 3351 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3352 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3353 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3354 | fp_offset_p); |
| 3355 | } |
| 3356 | CGF.EmitBranch(ContBlock); |
| 3357 | |
| 3358 | // Emit code to load the value if it was passed in memory. |
| 3359 | |
| 3360 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3361 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3362 | |
| 3363 | // Return the appropriate result. |
| 3364 | |
| 3365 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3366 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3367 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3368 | return ResAddr; |
| 3369 | } |
| 3370 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3371 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3372 | QualType Ty) const { |
| 3373 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3374 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3375 | CharUnits::fromQuantity(8), |
| 3376 | /*allowHigherAlign*/ false); |
| 3377 | } |
| 3378 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3379 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3380 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3381 | |
| 3382 | if (Ty->isVoidType()) |
| 3383 | return ABIArgInfo::getIgnore(); |
| 3384 | |
| 3385 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3386 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3387 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3388 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3389 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3390 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3391 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3392 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3393 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3394 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3395 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3396 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3400 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3401 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3402 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3403 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3404 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3405 | // other targets. |
| 3406 | const Type *Base = nullptr; |
| 3407 | uint64_t NumElts = 0; |
| 3408 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3409 | if (FreeSSERegs >= NumElts) { |
| 3410 | FreeSSERegs -= NumElts; |
| 3411 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3412 | return ABIArgInfo::getDirect(); |
| 3413 | return ABIArgInfo::getExpand(); |
| 3414 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3415 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3419 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3420 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3421 | // directly. |
| 3422 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3423 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3424 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3427 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3428 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3429 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3430 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3431 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3432 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3433 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3434 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3437 | // Bool type is always extended to the ABI, other builtin types are not |
| 3438 | // extended. |
| 3439 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3440 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3441 | return ABIArgInfo::getExtend(); |
| 3442 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3443 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3444 | // passes them indirectly through memory. |
| 3445 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3446 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3447 | if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 3448 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3449 | } |
| 3450 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3451 | return ABIArgInfo::getDirect(); |
| 3452 | } |
| 3453 | |
| 3454 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3455 | bool IsVectorCall = |
| 3456 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3457 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3458 | // We can use up to 4 SSE return registers with vectorcall. |
| 3459 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3460 | if (!getCXXABI().classifyReturnType(FI)) |
| 3461 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3462 | |
| 3463 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3464 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3465 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3466 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3469 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3470 | QualType Ty) const { |
| 3471 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3472 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3473 | CharUnits::fromQuantity(8), |
| 3474 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3475 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3476 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3477 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3478 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3479 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3480 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3481 | bool IsSoftFloatABI; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3482 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3483 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 3484 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3485 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3486 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3487 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3488 | }; |
| 3489 | |
| 3490 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3491 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3492 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 3493 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3494 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3495 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3496 | // This is recovered from gcc output. |
| 3497 | return 1; // r1 is the dedicated stack pointer |
| 3498 | } |
| 3499 | |
| 3500 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3501 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3502 | }; |
| 3503 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3504 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3505 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3506 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3507 | QualType Ty) const { |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame^] | 3508 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3509 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3510 | // TODO: Implement this. For now ignore. |
| 3511 | (void)CTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3512 | return Address::invalid(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3515 | // struct __va_list_tag { |
| 3516 | // unsigned char gpr; |
| 3517 | // unsigned char fpr; |
| 3518 | // unsigned short reserved; |
| 3519 | // void *overflow_arg_area; |
| 3520 | // void *reg_save_area; |
| 3521 | // }; |
| 3522 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3523 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3524 | bool isInt = |
| 3525 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3526 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3527 | |
| 3528 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3529 | // with the argument-lowering code. |
| 3530 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3531 | |
| 3532 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3533 | |
| 3534 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3535 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3536 | if (isInt || IsSoftFloatABI) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3537 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3538 | } else { |
| 3539 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3540 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3541 | |
| 3542 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3543 | |
| 3544 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3545 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3546 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3547 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3548 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3549 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3550 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame^] | 3551 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3552 | |
| 3553 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3554 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3555 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3556 | |
| 3557 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3558 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3559 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3560 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3561 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3562 | // Case 1: consume registers. |
| 3563 | Address RegAddr = Address::invalid(); |
| 3564 | { |
| 3565 | CGF.EmitBlock(UsingRegs); |
| 3566 | |
| 3567 | Address RegSaveAreaPtr = |
| 3568 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3569 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3570 | CharUnits::fromQuantity(8)); |
| 3571 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3572 | |
| 3573 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3574 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3575 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3576 | CharUnits::fromQuantity(32)); |
| 3577 | } |
| 3578 | |
| 3579 | // Get the address of the saved value by scaling the number of |
| 3580 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3581 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3582 | llvm::Value *RegOffset = |
| 3583 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3584 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3585 | RegAddr.getPointer(), RegOffset), |
| 3586 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3587 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3588 | |
| 3589 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 3590 | NumRegs = |
| 3591 | Builder.CreateAdd(NumRegs, |
| 3592 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3593 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3594 | |
| 3595 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3596 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3597 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3598 | // Case 2: consume space in the overflow area. |
| 3599 | Address MemAddr = Address::invalid(); |
| 3600 | { |
| 3601 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3602 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame^] | 3603 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 3604 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3605 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3606 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3607 | |
| 3608 | CharUnits Size; |
| 3609 | if (!isIndirect) { |
| 3610 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3611 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3612 | } else { |
| 3613 | Size = CGF.getPointerSize(); |
| 3614 | } |
| 3615 | |
| 3616 | Address OverflowAreaAddr = |
| 3617 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3618 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3619 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3620 | // Round up address of argument to alignment |
| 3621 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3622 | if (Align > OverflowAreaAlign) { |
| 3623 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 3624 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 3625 | Align); |
| 3626 | } |
| 3627 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3628 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3629 | |
| 3630 | // Increase the overflow area. |
| 3631 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3632 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3633 | CGF.EmitBranch(Cont); |
| 3634 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3635 | |
| 3636 | CGF.EmitBlock(Cont); |
| 3637 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3638 | // Merge the cases with a phi. |
| 3639 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3640 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3641 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3642 | // Load the pointer if the argument was passed indirectly. |
| 3643 | if (isIndirect) { |
| 3644 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3645 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | return Result; |
| 3649 | } |
| 3650 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3651 | bool |
| 3652 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3653 | llvm::Value *Address) const { |
| 3654 | // This is calculated from the LLVM and GCC tables and verified |
| 3655 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3656 | |
| 3657 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3658 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3659 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3660 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3661 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3662 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3663 | |
| 3664 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3665 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3666 | |
| 3667 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3668 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3669 | |
| 3670 | // 64-76 are various 4-byte special-purpose registers: |
| 3671 | // 64: mq |
| 3672 | // 65: lr |
| 3673 | // 66: ctr |
| 3674 | // 67: ap |
| 3675 | // 68-75 cr0-7 |
| 3676 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3677 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3678 | |
| 3679 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3680 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3681 | |
| 3682 | // 109: vrsave |
| 3683 | // 110: vscr |
| 3684 | // 111: spe_acc |
| 3685 | // 112: spefscr |
| 3686 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3687 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3688 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3689 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3692 | // PowerPC-64 |
| 3693 | |
| 3694 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3695 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3696 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3697 | public: |
| 3698 | enum ABIKind { |
| 3699 | ELFv1 = 0, |
| 3700 | ELFv2 |
| 3701 | }; |
| 3702 | |
| 3703 | private: |
| 3704 | static const unsigned GPRBits = 64; |
| 3705 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3706 | bool HasQPX; |
| 3707 | |
| 3708 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3709 | // will be passed in a QPX register. |
| 3710 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3711 | if (!HasQPX) |
| 3712 | return false; |
| 3713 | |
| 3714 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3715 | unsigned NumElements = VT->getNumElements(); |
| 3716 | if (NumElements == 1) |
| 3717 | return false; |
| 3718 | |
| 3719 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3720 | if (getContext().getTypeSize(Ty) <= 256) |
| 3721 | return true; |
| 3722 | } else if (VT->getElementType()-> |
| 3723 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3724 | if (getContext().getTypeSize(Ty) <= 128) |
| 3725 | return true; |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | return false; |
| 3730 | } |
| 3731 | |
| 3732 | bool IsQPXVectorTy(QualType Ty) const { |
| 3733 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3734 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3735 | |
| 3736 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3737 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3738 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3739 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3740 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3741 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3742 | |
| 3743 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3744 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3745 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3746 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3747 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3748 | uint64_t Members) const override; |
| 3749 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3750 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3751 | // Example: For aggregate arguments that fit in a register, we could |
| 3752 | // use getDirectInReg (as is done below for structs containing a single |
| 3753 | // floating-point value) to avoid pushing them to memory on function |
| 3754 | // entry. This would require changing the logic in PPCISelLowering |
| 3755 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3756 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3757 | if (!getCXXABI().classifyReturnType(FI)) |
| 3758 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3759 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3760 | // We rely on the default argument classification for the most part. |
| 3761 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3762 | // 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] | 3763 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3764 | if (T) { |
| 3765 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3766 | if (IsQPXVectorTy(T) || |
| 3767 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3768 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3769 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3770 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3771 | continue; |
| 3772 | } |
| 3773 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3774 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3775 | } |
| 3776 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3777 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3778 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3779 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3780 | }; |
| 3781 | |
| 3782 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3783 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3784 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3785 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3786 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3787 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3788 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3789 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3790 | // This is recovered from gcc output. |
| 3791 | return 1; // r1 is the dedicated stack pointer |
| 3792 | } |
| 3793 | |
| 3794 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3795 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3796 | }; |
| 3797 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3798 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3799 | public: |
| 3800 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3801 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3802 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3803 | // This is recovered from gcc output. |
| 3804 | return 1; // r1 is the dedicated stack pointer |
| 3805 | } |
| 3806 | |
| 3807 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3808 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3809 | }; |
| 3810 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3811 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3812 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3813 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3814 | // extended to 64 bits. |
| 3815 | bool |
| 3816 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3817 | // Treat an enum type as its underlying type. |
| 3818 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3819 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3820 | |
| 3821 | // Promotable integer types are required to be promoted by the ABI. |
| 3822 | if (Ty->isPromotableIntegerType()) |
| 3823 | return true; |
| 3824 | |
| 3825 | // In addition to the usual promotable integer types, we also need to |
| 3826 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3827 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3828 | switch (BT->getKind()) { |
| 3829 | case BuiltinType::Int: |
| 3830 | case BuiltinType::UInt: |
| 3831 | return true; |
| 3832 | default: |
| 3833 | break; |
| 3834 | } |
| 3835 | |
| 3836 | return false; |
| 3837 | } |
| 3838 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3839 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 3840 | /// higher alignment in the parameter area. Always returns at least 8. |
| 3841 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3842 | // Complex types are passed just like their elements. |
| 3843 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3844 | Ty = CTy->getElementType(); |
| 3845 | |
| 3846 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3847 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3848 | if (IsQPXVectorTy(Ty)) { |
| 3849 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3850 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3851 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3852 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3853 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3854 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3855 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3856 | |
| 3857 | // For single-element float/vector structs, we consider the whole type |
| 3858 | // to have the same alignment requirements as its single element. |
| 3859 | const Type *AlignAsType = nullptr; |
| 3860 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3861 | if (EltType) { |
| 3862 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3863 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3864 | getContext().getTypeSize(EltType) == 128) || |
| 3865 | (BT && BT->isFloatingPoint())) |
| 3866 | AlignAsType = EltType; |
| 3867 | } |
| 3868 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3869 | // Likewise for ELFv2 homogeneous aggregates. |
| 3870 | const Type *Base = nullptr; |
| 3871 | uint64_t Members = 0; |
| 3872 | if (!AlignAsType && Kind == ELFv2 && |
| 3873 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3874 | AlignAsType = Base; |
| 3875 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3876 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3877 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3878 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3879 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3880 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3881 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3882 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3883 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3884 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3885 | |
| 3886 | // Otherwise, we only need alignment for any aggregate type that |
| 3887 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3888 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3889 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3890 | return CharUnits::fromQuantity(32); |
| 3891 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3892 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3893 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3894 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3897 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3898 | /// aggregate. Base is set to the base element type, and Members is set |
| 3899 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3900 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3901 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3902 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3903 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3904 | if (NElements == 0) |
| 3905 | return false; |
| 3906 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3907 | return false; |
| 3908 | Members *= NElements; |
| 3909 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3910 | const RecordDecl *RD = RT->getDecl(); |
| 3911 | if (RD->hasFlexibleArrayMember()) |
| 3912 | return false; |
| 3913 | |
| 3914 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3915 | |
| 3916 | // If this is a C++ record, check the bases first. |
| 3917 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3918 | for (const auto &I : CXXRD->bases()) { |
| 3919 | // Ignore empty records. |
| 3920 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3921 | continue; |
| 3922 | |
| 3923 | uint64_t FldMembers; |
| 3924 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3925 | return false; |
| 3926 | |
| 3927 | Members += FldMembers; |
| 3928 | } |
| 3929 | } |
| 3930 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3931 | for (const auto *FD : RD->fields()) { |
| 3932 | // Ignore (non-zero arrays of) empty records. |
| 3933 | QualType FT = FD->getType(); |
| 3934 | while (const ConstantArrayType *AT = |
| 3935 | getContext().getAsConstantArrayType(FT)) { |
| 3936 | if (AT->getSize().getZExtValue() == 0) |
| 3937 | return false; |
| 3938 | FT = AT->getElementType(); |
| 3939 | } |
| 3940 | if (isEmptyRecord(getContext(), FT, true)) |
| 3941 | continue; |
| 3942 | |
| 3943 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3944 | if (getContext().getLangOpts().CPlusPlus && |
| 3945 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3946 | continue; |
| 3947 | |
| 3948 | uint64_t FldMembers; |
| 3949 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3950 | return false; |
| 3951 | |
| 3952 | Members = (RD->isUnion() ? |
| 3953 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3954 | } |
| 3955 | |
| 3956 | if (!Base) |
| 3957 | return false; |
| 3958 | |
| 3959 | // Ensure there is no padding. |
| 3960 | if (getContext().getTypeSize(Base) * Members != |
| 3961 | getContext().getTypeSize(Ty)) |
| 3962 | return false; |
| 3963 | } else { |
| 3964 | Members = 1; |
| 3965 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3966 | Members = 2; |
| 3967 | Ty = CT->getElementType(); |
| 3968 | } |
| 3969 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3970 | // Most ABIs only support float, double, and some vector type widths. |
| 3971 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3972 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3973 | |
| 3974 | // The base type must be the same for all members. Types that |
| 3975 | // agree in both total size and mode (float vs. vector) are |
| 3976 | // treated as being equivalent here. |
| 3977 | const Type *TyPtr = Ty.getTypePtr(); |
| 3978 | if (!Base) |
| 3979 | Base = TyPtr; |
| 3980 | |
| 3981 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3982 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3983 | return false; |
| 3984 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3985 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3986 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3987 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3988 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3989 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3990 | // double, long double, or 128-bit vectors. |
| 3991 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3992 | if (BT->getKind() == BuiltinType::Float || |
| 3993 | BT->getKind() == BuiltinType::Double || |
| 3994 | BT->getKind() == BuiltinType::LongDouble) |
| 3995 | return true; |
| 3996 | } |
| 3997 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3998 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3999 | return true; |
| 4000 | } |
| 4001 | return false; |
| 4002 | } |
| 4003 | |
| 4004 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4005 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4006 | // Vector types require one register, floating point types require one |
| 4007 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4008 | uint32_t NumRegs = |
| 4009 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4010 | |
| 4011 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4012 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4015 | ABIArgInfo |
| 4016 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4017 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4018 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4019 | if (Ty->isAnyComplexType()) |
| 4020 | return ABIArgInfo::getDirect(); |
| 4021 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4022 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4023 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4024 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4025 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4026 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4027 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4028 | else if (Size < 128) { |
| 4029 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4030 | return ABIArgInfo::getDirect(CoerceTy); |
| 4031 | } |
| 4032 | } |
| 4033 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4034 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4035 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4036 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4037 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4038 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4039 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4040 | |
| 4041 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4042 | const Type *Base = nullptr; |
| 4043 | uint64_t Members = 0; |
| 4044 | if (Kind == ELFv2 && |
| 4045 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4046 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4047 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4048 | return ABIArgInfo::getDirect(CoerceTy); |
| 4049 | } |
| 4050 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4051 | // If an aggregate may end up fully in registers, we do not |
| 4052 | // use the ByVal method, but pass the aggregate as array. |
| 4053 | // This is usually beneficial since we avoid forcing the |
| 4054 | // back-end to store the argument to memory. |
| 4055 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4056 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4057 | llvm::Type *CoerceTy; |
| 4058 | |
| 4059 | // Types up to 8 bytes are passed as integer type (which will be |
| 4060 | // properly aligned in the argument save area doubleword). |
| 4061 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4062 | CoerceTy = |
| 4063 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4064 | // Larger types are passed as arrays, with the base type selected |
| 4065 | // according to the required alignment in the save area. |
| 4066 | else { |
| 4067 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4068 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4069 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4070 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4071 | } |
| 4072 | |
| 4073 | return ABIArgInfo::getDirect(CoerceTy); |
| 4074 | } |
| 4075 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4076 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4077 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4078 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4079 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | return (isPromotableTypeForABI(Ty) ? |
| 4083 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4084 | } |
| 4085 | |
| 4086 | ABIArgInfo |
| 4087 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4088 | if (RetTy->isVoidType()) |
| 4089 | return ABIArgInfo::getIgnore(); |
| 4090 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4091 | if (RetTy->isAnyComplexType()) |
| 4092 | return ABIArgInfo::getDirect(); |
| 4093 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4094 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4095 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4096 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4097 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4098 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4099 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4100 | else if (Size < 128) { |
| 4101 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4102 | return ABIArgInfo::getDirect(CoerceTy); |
| 4103 | } |
| 4104 | } |
| 4105 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4106 | if (isAggregateTypeForABI(RetTy)) { |
| 4107 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4108 | const Type *Base = nullptr; |
| 4109 | uint64_t Members = 0; |
| 4110 | if (Kind == ELFv2 && |
| 4111 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4112 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4113 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4114 | return ABIArgInfo::getDirect(CoerceTy); |
| 4115 | } |
| 4116 | |
| 4117 | // ELFv2 small aggregates are returned in up to two registers. |
| 4118 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4119 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4120 | if (Bits == 0) |
| 4121 | return ABIArgInfo::getIgnore(); |
| 4122 | |
| 4123 | llvm::Type *CoerceTy; |
| 4124 | if (Bits > GPRBits) { |
| 4125 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 4126 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4127 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4128 | CoerceTy = |
| 4129 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4130 | return ABIArgInfo::getDirect(CoerceTy); |
| 4131 | } |
| 4132 | |
| 4133 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4134 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4135 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4136 | |
| 4137 | return (isPromotableTypeForABI(RetTy) ? |
| 4138 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4139 | } |
| 4140 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4141 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4142 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4143 | QualType Ty) const { |
| 4144 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4145 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4146 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4147 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4148 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4149 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4150 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4151 | // in separate doublewords. However, Clang expects us to produce a |
| 4152 | // pointer to a structure with the two parts packed tightly. So generate |
| 4153 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4154 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4155 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4156 | CharUnits EltSize = TypeInfo.first / 2; |
| 4157 | if (EltSize < SlotSize) { |
| 4158 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4159 | SlotSize * 2, SlotSize, |
| 4160 | SlotSize, /*AllowHigher*/ true); |
| 4161 | |
| 4162 | Address RealAddr = Addr; |
| 4163 | Address ImagAddr = RealAddr; |
| 4164 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4165 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4166 | SlotSize - EltSize); |
| 4167 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4168 | 2 * SlotSize - EltSize); |
| 4169 | } else { |
| 4170 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4171 | } |
| 4172 | |
| 4173 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4174 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4175 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4176 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4177 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4178 | |
| 4179 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4180 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4181 | /*init*/ true); |
| 4182 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4183 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4186 | // Otherwise, just use the general rule. |
| 4187 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4188 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | static bool |
| 4192 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4193 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4194 | // This is calculated from the LLVM and GCC tables and verified |
| 4195 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4196 | |
| 4197 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4198 | |
| 4199 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4200 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4201 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4202 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4203 | |
| 4204 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4205 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4206 | |
| 4207 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4208 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4209 | |
| 4210 | // 64-76 are various 4-byte special-purpose registers: |
| 4211 | // 64: mq |
| 4212 | // 65: lr |
| 4213 | // 66: ctr |
| 4214 | // 67: ap |
| 4215 | // 68-75 cr0-7 |
| 4216 | // 76: xer |
| 4217 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 4218 | |
| 4219 | // 77-108: v0-31, the 16-byte vector registers |
| 4220 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4221 | |
| 4222 | // 109: vrsave |
| 4223 | // 110: vscr |
| 4224 | // 111: spe_acc |
| 4225 | // 112: spefscr |
| 4226 | // 113: sfp |
| 4227 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 4228 | |
| 4229 | return false; |
| 4230 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4231 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4232 | bool |
| 4233 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4234 | CodeGen::CodeGenFunction &CGF, |
| 4235 | llvm::Value *Address) const { |
| 4236 | |
| 4237 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4238 | } |
| 4239 | |
| 4240 | bool |
| 4241 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4242 | llvm::Value *Address) const { |
| 4243 | |
| 4244 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4245 | } |
| 4246 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4247 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4248 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4249 | //===----------------------------------------------------------------------===// |
| 4250 | |
| 4251 | namespace { |
| 4252 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4253 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4254 | public: |
| 4255 | enum ABIKind { |
| 4256 | AAPCS = 0, |
| 4257 | DarwinPCS |
| 4258 | }; |
| 4259 | |
| 4260 | private: |
| 4261 | ABIKind Kind; |
| 4262 | |
| 4263 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4264 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4265 | |
| 4266 | private: |
| 4267 | ABIKind getABIKind() const { return Kind; } |
| 4268 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4269 | |
| 4270 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4271 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4272 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4273 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4274 | uint64_t Members) const override; |
| 4275 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4276 | bool isIllegalVectorType(QualType Ty) const; |
| 4277 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4278 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4279 | if (!getCXXABI().classifyReturnType(FI)) |
| 4280 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4281 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4282 | for (auto &it : FI.arguments()) |
| 4283 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4286 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4287 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4288 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4289 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4290 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4291 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4292 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4293 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4294 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4295 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4296 | } |
| 4297 | }; |
| 4298 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4299 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4300 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4301 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4302 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4303 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4304 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4305 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4306 | } |
| 4307 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4308 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4309 | return 31; |
| 4310 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4311 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4312 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4313 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4314 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4315 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4316 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4317 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4318 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4319 | // Handle illegal vector types here. |
| 4320 | if (isIllegalVectorType(Ty)) { |
| 4321 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4322 | if (Size <= 32) { |
| 4323 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4324 | return ABIArgInfo::getDirect(ResType); |
| 4325 | } |
| 4326 | if (Size == 64) { |
| 4327 | llvm::Type *ResType = |
| 4328 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4329 | return ABIArgInfo::getDirect(ResType); |
| 4330 | } |
| 4331 | if (Size == 128) { |
| 4332 | llvm::Type *ResType = |
| 4333 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4334 | return ABIArgInfo::getDirect(ResType); |
| 4335 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4336 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4337 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4338 | |
| 4339 | if (!isAggregateTypeForABI(Ty)) { |
| 4340 | // Treat an enum type as its underlying type. |
| 4341 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4342 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4343 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4344 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4345 | ? ABIArgInfo::getExtend() |
| 4346 | : ABIArgInfo::getDirect()); |
| 4347 | } |
| 4348 | |
| 4349 | // Structures with either a non-trivial destructor or a non-trivial |
| 4350 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4351 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4352 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4353 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4357 | // elsewhere for GNU compatibility. |
| 4358 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4359 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4360 | return ABIArgInfo::getIgnore(); |
| 4361 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4362 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4363 | } |
| 4364 | |
| 4365 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4366 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4367 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4368 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4369 | return ABIArgInfo::getDirect( |
| 4370 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4374 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4375 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4376 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4377 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4378 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4379 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4380 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4381 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4382 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4383 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4384 | } |
| 4385 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4386 | } |
| 4387 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4388 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4391 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4392 | if (RetTy->isVoidType()) |
| 4393 | return ABIArgInfo::getIgnore(); |
| 4394 | |
| 4395 | // Large vector types should be returned via memory. |
| 4396 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4397 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4398 | |
| 4399 | if (!isAggregateTypeForABI(RetTy)) { |
| 4400 | // Treat an enum type as its underlying type. |
| 4401 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4402 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4403 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4404 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4405 | ? ABIArgInfo::getExtend() |
| 4406 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4409 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4410 | return ABIArgInfo::getIgnore(); |
| 4411 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4412 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4413 | uint64_t Members = 0; |
| 4414 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4415 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4416 | return ABIArgInfo::getDirect(); |
| 4417 | |
| 4418 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4419 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4420 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4421 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4422 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4423 | |
| 4424 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4425 | // For aggregates with 16-byte alignment, we use i128. |
| 4426 | if (Alignment < 128 && Size == 128) { |
| 4427 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4428 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4429 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4430 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4431 | } |
| 4432 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4433 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4436 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4437 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4438 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4439 | // Check whether VT is legal. |
| 4440 | unsigned NumElements = VT->getNumElements(); |
| 4441 | uint64_t Size = getContext().getTypeSize(VT); |
| 4442 | // NumElements should be power of 2 between 1 and 16. |
| 4443 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4444 | return true; |
| 4445 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4446 | } |
| 4447 | return false; |
| 4448 | } |
| 4449 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4450 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4451 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4452 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4453 | // but with the difference that any floating-point type is allowed, |
| 4454 | // including __fp16. |
| 4455 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4456 | if (BT->isFloatingPoint()) |
| 4457 | return true; |
| 4458 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4459 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4460 | if (VecSize == 64 || VecSize == 128) |
| 4461 | return true; |
| 4462 | } |
| 4463 | return false; |
| 4464 | } |
| 4465 | |
| 4466 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4467 | uint64_t Members) const { |
| 4468 | return Members <= 4; |
| 4469 | } |
| 4470 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4471 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4472 | QualType Ty, |
| 4473 | CodeGenFunction &CGF) const { |
| 4474 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4475 | bool IsIndirect = AI.isIndirect(); |
| 4476 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4477 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4478 | if (IsIndirect) |
| 4479 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4480 | else if (AI.getCoerceToType()) |
| 4481 | BaseTy = AI.getCoerceToType(); |
| 4482 | |
| 4483 | unsigned NumRegs = 1; |
| 4484 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4485 | BaseTy = ArrTy->getElementType(); |
| 4486 | NumRegs = ArrTy->getNumElements(); |
| 4487 | } |
| 4488 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4489 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4490 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4491 | // Standard, section B.4: |
| 4492 | // |
| 4493 | // struct { |
| 4494 | // void *__stack; |
| 4495 | // void *__gr_top; |
| 4496 | // void *__vr_top; |
| 4497 | // int __gr_offs; |
| 4498 | // int __vr_offs; |
| 4499 | // }; |
| 4500 | |
| 4501 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4502 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4503 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4504 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4505 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4506 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4507 | CharUnits TyAlign = TyInfo.second; |
| 4508 | |
| 4509 | Address reg_offs_p = Address::invalid(); |
| 4510 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4511 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4512 | CharUnits reg_top_offset; |
| 4513 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4514 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4515 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4516 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4517 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4518 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4519 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4520 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4521 | reg_top_offset = CharUnits::fromQuantity(8); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4522 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4523 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4524 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4525 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4526 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4527 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4528 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4529 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4530 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4531 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | //======================================= |
| 4535 | // Find out where argument was passed |
| 4536 | //======================================= |
| 4537 | |
| 4538 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4539 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4540 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4541 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4542 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4543 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4544 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4545 | |
| 4546 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4547 | |
| 4548 | // 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] | 4549 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4550 | CGF.EmitBlock(MaybeRegBlock); |
| 4551 | |
| 4552 | // Integer arguments may need to correct register alignment (for example a |
| 4553 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4554 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4555 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4556 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4557 | |
| 4558 | reg_offs = CGF.Builder.CreateAdd( |
| 4559 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4560 | "align_regoffs"); |
| 4561 | reg_offs = CGF.Builder.CreateAnd( |
| 4562 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4563 | "aligned_regoffs"); |
| 4564 | } |
| 4565 | |
| 4566 | // 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] | 4567 | // The fact that this is done unconditionally reflects the fact that |
| 4568 | // allocating an argument to the stack also uses up all the remaining |
| 4569 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4570 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4571 | NewOffset = CGF.Builder.CreateAdd( |
| 4572 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4573 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4574 | |
| 4575 | // Now we're in a position to decide whether this argument really was in |
| 4576 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4577 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4578 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4579 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4580 | |
| 4581 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4582 | |
| 4583 | //======================================= |
| 4584 | // Argument was in registers |
| 4585 | //======================================= |
| 4586 | |
| 4587 | // Now we emit the code for if the argument was originally passed in |
| 4588 | // registers. First start the appropriate block: |
| 4589 | CGF.EmitBlock(InRegBlock); |
| 4590 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4591 | llvm::Value *reg_top = nullptr; |
| 4592 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4593 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4594 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4595 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4596 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4597 | Address RegAddr = Address::invalid(); |
| 4598 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4599 | |
| 4600 | if (IsIndirect) { |
| 4601 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4602 | // stored registers or on the stack will actually be a struct **. |
| 4603 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4604 | } |
| 4605 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4606 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4607 | uint64_t NumMembers = 0; |
| 4608 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4609 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4610 | // Homogeneous aggregates passed in registers will have their elements split |
| 4611 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4612 | // qN+1, ...). We reload and store into a temporary local variable |
| 4613 | // contiguously. |
| 4614 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4615 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4616 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4617 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4618 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4619 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4620 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4621 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4622 | int Offset = 0; |
| 4623 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4624 | BaseTyInfo.first.getQuantity() < 16) |
| 4625 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4626 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4627 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4628 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4629 | Address LoadAddr = |
| 4630 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4631 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4632 | |
| 4633 | Address StoreAddr = |
| 4634 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4635 | |
| 4636 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4637 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4638 | } |
| 4639 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4640 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4641 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4642 | // Otherwise the object is contiguous in memory. |
| 4643 | |
| 4644 | // It might be right-aligned in its slot. |
| 4645 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4646 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4647 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4648 | TyInfo.first < SlotSize) { |
| 4649 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4650 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4653 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | CGF.EmitBranch(ContBlock); |
| 4657 | |
| 4658 | //======================================= |
| 4659 | // Argument was on the stack |
| 4660 | //======================================= |
| 4661 | CGF.EmitBlock(OnStackBlock); |
| 4662 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4663 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4664 | CharUnits::Zero(), "stack_p"); |
| 4665 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4666 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4667 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4668 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4669 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4670 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4671 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4672 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4673 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4674 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4675 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4676 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4677 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4678 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4679 | "align_stack"); |
| 4680 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4681 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4682 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4683 | Address OnStackAddr(OnStackPtr, |
| 4684 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4685 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4686 | // All stack slots are multiples of 8 bytes. |
| 4687 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4688 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4689 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4690 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4691 | else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4692 | StackSize = TyInfo.first.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4693 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4694 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4695 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4696 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4697 | |
| 4698 | // Write the new value of __stack for the next call to va_arg |
| 4699 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4700 | |
| 4701 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4702 | TyInfo.first < StackSlotSize) { |
| 4703 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4704 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4707 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4708 | |
| 4709 | CGF.EmitBranch(ContBlock); |
| 4710 | |
| 4711 | //======================================= |
| 4712 | // Tidy up |
| 4713 | //======================================= |
| 4714 | CGF.EmitBlock(ContBlock); |
| 4715 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4716 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4717 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4718 | |
| 4719 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4720 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4721 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4722 | |
| 4723 | return ResAddr; |
| 4724 | } |
| 4725 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4726 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4727 | CodeGenFunction &CGF) const { |
| 4728 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4729 | // illegal vector types. Lower VAArg here for these cases and use |
| 4730 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4731 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4732 | return Address::invalid(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4733 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4734 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4735 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4736 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4737 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4738 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4739 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4740 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4743 | // The size of the actual thing passed, which might end up just |
| 4744 | // being a pointer for indirect types. |
| 4745 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4746 | |
| 4747 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4748 | // aggregates should be passed indirectly. |
| 4749 | bool IsIndirect = false; |
| 4750 | if (TyInfo.first.getQuantity() > 16) { |
| 4751 | const Type *Base = nullptr; |
| 4752 | uint64_t Members = 0; |
| 4753 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4756 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4757 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4761 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4762 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4763 | |
| 4764 | namespace { |
| 4765 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4766 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4767 | public: |
| 4768 | enum ABIKind { |
| 4769 | APCS = 0, |
| 4770 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4771 | AAPCS_VFP = 2, |
| 4772 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4773 | }; |
| 4774 | |
| 4775 | private: |
| 4776 | ABIKind Kind; |
| 4777 | |
| 4778 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4779 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4780 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4781 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4782 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4783 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4784 | switch (getTarget().getTriple().getEnvironment()) { |
| 4785 | case llvm::Triple::Android: |
| 4786 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4787 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4788 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4789 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4790 | return true; |
| 4791 | default: |
| 4792 | return false; |
| 4793 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4796 | bool isEABIHF() const { |
| 4797 | switch (getTarget().getTriple().getEnvironment()) { |
| 4798 | case llvm::Triple::EABIHF: |
| 4799 | case llvm::Triple::GNUEABIHF: |
| 4800 | return true; |
| 4801 | default: |
| 4802 | return false; |
| 4803 | } |
| 4804 | } |
| 4805 | |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 4806 | bool isAndroid() const { |
| 4807 | return (getTarget().getTriple().getEnvironment() == |
| 4808 | llvm::Triple::Android); |
| 4809 | } |
| 4810 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4811 | ABIKind getABIKind() const { return Kind; } |
| 4812 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4813 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4814 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4815 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4816 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4817 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4818 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4819 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4820 | uint64_t Members) const override; |
| 4821 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4822 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4823 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4824 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4825 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4826 | |
| 4827 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4828 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4829 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4830 | }; |
| 4831 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4832 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4833 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4834 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4835 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4836 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4837 | const ARMABIInfo &getABIInfo() const { |
| 4838 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4839 | } |
| 4840 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4841 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4842 | return 13; |
| 4843 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4844 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4845 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4846 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4847 | } |
| 4848 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4849 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4850 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4851 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4852 | |
| 4853 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4854 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4855 | return false; |
| 4856 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4857 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4858 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4859 | if (getABIInfo().isEABI()) return 88; |
| 4860 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4861 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4862 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4863 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4864 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 4865 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4866 | if (!FD) |
| 4867 | return; |
| 4868 | |
| 4869 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4870 | if (!Attr) |
| 4871 | return; |
| 4872 | |
| 4873 | const char *Kind; |
| 4874 | switch (Attr->getInterrupt()) { |
| 4875 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4876 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4877 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4878 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4879 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4880 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4881 | } |
| 4882 | |
| 4883 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4884 | |
| 4885 | Fn->addFnAttr("interrupt", Kind); |
| 4886 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4887 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 4888 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4889 | return; |
| 4890 | |
| 4891 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4892 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4893 | // the backend to perform a realignment as part of the function prologue. |
| 4894 | llvm::AttrBuilder B; |
| 4895 | B.addStackAlignmentAttr(8); |
| 4896 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4897 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4898 | llvm::AttributeSet::FunctionIndex, |
| 4899 | B)); |
| 4900 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4901 | }; |
| 4902 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4903 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4904 | public: |
| 4905 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4906 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4907 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4908 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4909 | CodeGen::CodeGenModule &CGM) const override; |
| 4910 | }; |
| 4911 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4912 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4913 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4914 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4915 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4916 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4917 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4918 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4919 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4920 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4921 | FI.getReturnInfo() = |
| 4922 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4923 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4924 | for (auto &I : FI.arguments()) |
| 4925 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4926 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4927 | // Always honor user-specified calling convention. |
| 4928 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4929 | return; |
| 4930 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4931 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4932 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4933 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4934 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4935 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4936 | /// Return the default calling convention that LLVM will use. |
| 4937 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4938 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 4939 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4940 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4941 | else if (isEABI()) |
| 4942 | return llvm::CallingConv::ARM_AAPCS; |
| 4943 | else |
| 4944 | return llvm::CallingConv::ARM_APCS; |
| 4945 | } |
| 4946 | |
| 4947 | /// Return the calling convention that our ABI would like us to use |
| 4948 | /// as the C calling convention. |
| 4949 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4950 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4951 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4952 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4953 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4954 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4955 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4956 | llvm_unreachable("bad ABI kind"); |
| 4957 | } |
| 4958 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4959 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4960 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4961 | |
| 4962 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4963 | // they'd just match what LLVM will infer from the triple. |
| 4964 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4965 | if (abiCC != getLLVMDefaultCC()) |
| 4966 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4967 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4968 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 4969 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 4970 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 4971 | switch (getABIKind()) { |
| 4972 | case APCS: |
| 4973 | case AAPCS16_VFP: |
| 4974 | if (abiCC != getLLVMDefaultCC()) |
| 4975 | BuiltinCC = abiCC; |
| 4976 | break; |
| 4977 | case AAPCS: |
| 4978 | case AAPCS_VFP: |
| 4979 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 4980 | break; |
| 4981 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4984 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4985 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4986 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4987 | // A single-precision floating-point type (including promoted |
| 4988 | // half-precision types); A double-precision floating-point type; |
| 4989 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4990 | // with a Base Type of a single- or double-precision floating-point type, |
| 4991 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4992 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4993 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4994 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4995 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4996 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4997 | // Handle illegal vector types here. |
| 4998 | if (isIllegalVectorType(Ty)) { |
| 4999 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5000 | if (Size <= 32) { |
| 5001 | llvm::Type *ResType = |
| 5002 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5003 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5004 | } |
| 5005 | if (Size == 64) { |
| 5006 | llvm::Type *ResType = llvm::VectorType::get( |
| 5007 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5008 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5009 | } |
| 5010 | if (Size == 128) { |
| 5011 | llvm::Type *ResType = llvm::VectorType::get( |
| 5012 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5013 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5014 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5015 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5018 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 5019 | // unspecified. This is not done for OpenCL as it handles the half type |
| 5020 | // natively, and does not need to interwork with AAPCS code. |
| 5021 | if (Ty->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 5022 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5023 | llvm::Type::getFloatTy(getVMContext()) : |
| 5024 | llvm::Type::getInt32Ty(getVMContext()); |
| 5025 | return ABIArgInfo::getDirect(ResType); |
| 5026 | } |
| 5027 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5028 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5029 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5030 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5031 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5032 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5033 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5034 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5035 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5036 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5037 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5038 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5039 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5040 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5041 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5042 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5043 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5044 | return ABIArgInfo::getIgnore(); |
| 5045 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5046 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5047 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5048 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5049 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5050 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5051 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5052 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5053 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5054 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5055 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5056 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5057 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5058 | // this convention even for a variadic function: the backend will use GPRs |
| 5059 | // if needed. |
| 5060 | const Type *Base = nullptr; |
| 5061 | uint64_t Members = 0; |
| 5062 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5063 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5064 | llvm::Type *Ty = |
| 5065 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5066 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5071 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5072 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5073 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5074 | // and a pointer is passed. |
| 5075 | return ABIArgInfo::getIndirect( |
| 5076 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5079 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5080 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5081 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5082 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5083 | uint64_t ABIAlign = 4; |
| 5084 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5085 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5086 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5087 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5088 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5089 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5090 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5091 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5092 | /*ByVal=*/true, |
| 5093 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5096 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5097 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5098 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5099 | // FIXME: Try to match the types of the arguments more accurately where |
| 5100 | // we can. |
| 5101 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5102 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5103 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5104 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5105 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5106 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5107 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5108 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5109 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5112 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5113 | llvm::LLVMContext &VMContext) { |
| 5114 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5115 | // is called integer-like if its size is less than or equal to one word, and |
| 5116 | // the offset of each of its addressable sub-fields is zero. |
| 5117 | |
| 5118 | uint64_t Size = Context.getTypeSize(Ty); |
| 5119 | |
| 5120 | // Check that the type fits in a word. |
| 5121 | if (Size > 32) |
| 5122 | return false; |
| 5123 | |
| 5124 | // FIXME: Handle vector types! |
| 5125 | if (Ty->isVectorType()) |
| 5126 | return false; |
| 5127 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5128 | // Float types are never treated as "integer like". |
| 5129 | if (Ty->isRealFloatingType()) |
| 5130 | return false; |
| 5131 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5132 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5133 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5134 | return true; |
| 5135 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5136 | // Small complex integer types are "integer like". |
| 5137 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5138 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5139 | |
| 5140 | // Single element and zero sized arrays should be allowed, by the definition |
| 5141 | // above, but they are not. |
| 5142 | |
| 5143 | // Otherwise, it must be a record type. |
| 5144 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5145 | if (!RT) return false; |
| 5146 | |
| 5147 | // Ignore records with flexible arrays. |
| 5148 | const RecordDecl *RD = RT->getDecl(); |
| 5149 | if (RD->hasFlexibleArrayMember()) |
| 5150 | return false; |
| 5151 | |
| 5152 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5153 | // like". |
| 5154 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5155 | |
| 5156 | bool HadField = false; |
| 5157 | unsigned idx = 0; |
| 5158 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5159 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5160 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5161 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5162 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5163 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5164 | // struct { int : 0; int x } |
| 5165 | // is non-integer like according to gcc. |
| 5166 | if (FD->isBitField()) { |
| 5167 | if (!RD->isUnion()) |
| 5168 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5169 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5170 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5171 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5172 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5173 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5176 | // Check if this field is at offset 0. |
| 5177 | if (Layout.getFieldOffset(idx) != 0) |
| 5178 | return false; |
| 5179 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5180 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5181 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5182 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5183 | // Only allow at most one field in a structure. This doesn't match the |
| 5184 | // wording above, but follows gcc in situations with a field following an |
| 5185 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5186 | if (!RD->isUnion()) { |
| 5187 | if (HadField) |
| 5188 | return false; |
| 5189 | |
| 5190 | HadField = true; |
| 5191 | } |
| 5192 | } |
| 5193 | |
| 5194 | return true; |
| 5195 | } |
| 5196 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5197 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5198 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5199 | bool IsEffectivelyAAPCS_VFP = |
| 5200 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5201 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5202 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5203 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5204 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5205 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5206 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5207 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5208 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5209 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5210 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5211 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5212 | // natively, and does not need to interwork with AAPCS code. |
| 5213 | if (RetTy->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 5214 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5215 | llvm::Type::getFloatTy(getVMContext()) : |
| 5216 | llvm::Type::getInt32Ty(getVMContext()); |
| 5217 | return ABIArgInfo::getDirect(ResType); |
| 5218 | } |
| 5219 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5220 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5221 | // Treat an enum type as its underlying type. |
| 5222 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5223 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5224 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5225 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5226 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5227 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5228 | |
| 5229 | // Are we following APCS? |
| 5230 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5231 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5232 | return ABIArgInfo::getIgnore(); |
| 5233 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5234 | // Complex types are all returned as packed integers. |
| 5235 | // |
| 5236 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5237 | // correctly. |
| 5238 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5239 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5240 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5241 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5242 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5243 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5244 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5245 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5246 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5247 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5248 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5249 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5250 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5254 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5255 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5256 | |
| 5257 | // Otherwise this is an AAPCS variant. |
| 5258 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5259 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5260 | return ABIArgInfo::getIgnore(); |
| 5261 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5262 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5263 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5264 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5265 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5266 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5267 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5268 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5269 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5270 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5273 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5274 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5275 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5276 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5277 | if (getDataLayout().isBigEndian()) |
| 5278 | // 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] | 5279 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5280 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5281 | // Return in the smallest viable integer type. |
| 5282 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5283 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5284 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5285 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5286 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5287 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5288 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5289 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5290 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5291 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5294 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5297 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5298 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 5299 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5300 | if (isAndroid()) { |
| 5301 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5302 | // vector ABI. The primary differences were that 3-element vector types |
| 5303 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5304 | // accepts that legacy behavior for Android only. |
| 5305 | // Check whether VT is legal. |
| 5306 | unsigned NumElements = VT->getNumElements(); |
| 5307 | // NumElements should be power of 2 or equal to 3. |
| 5308 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5309 | return true; |
| 5310 | } else { |
| 5311 | // Check whether VT is legal. |
| 5312 | unsigned NumElements = VT->getNumElements(); |
| 5313 | uint64_t Size = getContext().getTypeSize(VT); |
| 5314 | // NumElements should be power of 2. |
| 5315 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5316 | return true; |
| 5317 | // Size should be greater than 32 bits. |
| 5318 | return Size <= 32; |
| 5319 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5320 | } |
| 5321 | return false; |
| 5322 | } |
| 5323 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5324 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5325 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5326 | // double, or 64-bit or 128-bit vectors. |
| 5327 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5328 | if (BT->getKind() == BuiltinType::Float || |
| 5329 | BT->getKind() == BuiltinType::Double || |
| 5330 | BT->getKind() == BuiltinType::LongDouble) |
| 5331 | return true; |
| 5332 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5333 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5334 | if (VecSize == 64 || VecSize == 128) |
| 5335 | return true; |
| 5336 | } |
| 5337 | return false; |
| 5338 | } |
| 5339 | |
| 5340 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5341 | uint64_t Members) const { |
| 5342 | return Members <= 4; |
| 5343 | } |
| 5344 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5345 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5346 | QualType Ty) const { |
| 5347 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5348 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5349 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5350 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5351 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5352 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5353 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5356 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5357 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5358 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5359 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5360 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5361 | const Type *Base = nullptr; |
| 5362 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5363 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5364 | IsIndirect = true; |
| 5365 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5366 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5367 | // allocated by the caller. |
| 5368 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5369 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5370 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5371 | IsIndirect = true; |
| 5372 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5373 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5374 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5375 | // 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] | 5376 | // Our callers should be prepared to handle an under-aligned address. |
| 5377 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5378 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5379 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5380 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 5381 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5382 | // ARMv7k allows type alignment up to 16 bytes. |
| 5383 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5384 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5385 | } else { |
| 5386 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5387 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5388 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5389 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5390 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5391 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5394 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5395 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5396 | //===----------------------------------------------------------------------===// |
| 5397 | |
| 5398 | namespace { |
| 5399 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5400 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5401 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5402 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5403 | |
| 5404 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5405 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5406 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5407 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5408 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5409 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5410 | }; |
| 5411 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5412 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5413 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5414 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5415 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5416 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5417 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5418 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5419 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5420 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5421 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5422 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5423 | }; |
| 5424 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5425 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5426 | if (RetTy->isVoidType()) |
| 5427 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5428 | |
| 5429 | // note: this is different from default ABI |
| 5430 | if (!RetTy->isScalarType()) |
| 5431 | return ABIArgInfo::getDirect(); |
| 5432 | |
| 5433 | // Treat an enum type as its underlying type. |
| 5434 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5435 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5436 | |
| 5437 | return (RetTy->isPromotableIntegerType() ? |
| 5438 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5441 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5442 | // Treat an enum type as its underlying type. |
| 5443 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5444 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5445 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5446 | // Return aggregates type as indirect by value |
| 5447 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5448 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5449 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5450 | return (Ty->isPromotableIntegerType() ? |
| 5451 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5454 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5455 | if (!getCXXABI().classifyReturnType(FI)) |
| 5456 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5457 | for (auto &I : FI.arguments()) |
| 5458 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5459 | |
| 5460 | // Always honor user-specified calling convention. |
| 5461 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5462 | return; |
| 5463 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5464 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5465 | } |
| 5466 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5467 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5468 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5469 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5472 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5473 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5474 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5475 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5476 | if (!FD) return; |
| 5477 | |
| 5478 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5479 | |
| 5480 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5481 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5482 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5483 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5484 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5485 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5486 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5487 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5488 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5489 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5490 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5491 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5492 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5493 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5494 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5495 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5496 | // __global__ functions cannot be called from the device, we do not |
| 5497 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5498 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5499 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5500 | addNVVMMetadata(F, "kernel", 1); |
| 5501 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5502 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5503 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5504 | llvm::APSInt MaxThreads(32); |
| 5505 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5506 | if (MaxThreads > 0) |
| 5507 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5508 | |
| 5509 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5510 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5511 | // we don't have to add a PTX directive. |
| 5512 | if (Attr->getMinBlocks()) { |
| 5513 | llvm::APSInt MinBlocks(32); |
| 5514 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5515 | if (MinBlocks > 0) |
| 5516 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5517 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5518 | } |
| 5519 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5523 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5524 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5525 | llvm::Module *M = F->getParent(); |
| 5526 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5527 | |
| 5528 | // Get "nvvm.annotations" metadata node |
| 5529 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5530 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5531 | llvm::Metadata *MDVals[] = { |
| 5532 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5533 | llvm::ConstantAsMetadata::get( |
| 5534 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5535 | // Append metadata to nvvm.annotations |
| 5536 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5537 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5538 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5539 | |
| 5540 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5541 | // SystemZ ABI Implementation |
| 5542 | //===----------------------------------------------------------------------===// |
| 5543 | |
| 5544 | namespace { |
| 5545 | |
| 5546 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5547 | bool HasVector; |
| 5548 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5549 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5550 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5551 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5552 | |
| 5553 | bool isPromotableIntegerType(QualType Ty) const; |
| 5554 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5555 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5556 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5557 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5558 | |
| 5559 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5560 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5561 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5562 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5563 | if (!getCXXABI().classifyReturnType(FI)) |
| 5564 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5565 | for (auto &I : FI.arguments()) |
| 5566 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5567 | } |
| 5568 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5569 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5570 | QualType Ty) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5571 | }; |
| 5572 | |
| 5573 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5574 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5575 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5576 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5577 | }; |
| 5578 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5579 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5580 | |
| 5581 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5582 | // Treat an enum type as its underlying type. |
| 5583 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5584 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5585 | |
| 5586 | // Promotable integer types are required to be promoted by the ABI. |
| 5587 | if (Ty->isPromotableIntegerType()) |
| 5588 | return true; |
| 5589 | |
| 5590 | // 32-bit values must also be promoted. |
| 5591 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5592 | switch (BT->getKind()) { |
| 5593 | case BuiltinType::Int: |
| 5594 | case BuiltinType::UInt: |
| 5595 | return true; |
| 5596 | default: |
| 5597 | return false; |
| 5598 | } |
| 5599 | return false; |
| 5600 | } |
| 5601 | |
| 5602 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5603 | return (Ty->isAnyComplexType() || |
| 5604 | Ty->isVectorType() || |
| 5605 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5608 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5609 | return (HasVector && |
| 5610 | Ty->isVectorType() && |
| 5611 | getContext().getTypeSize(Ty) <= 128); |
| 5612 | } |
| 5613 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5614 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5615 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5616 | switch (BT->getKind()) { |
| 5617 | case BuiltinType::Float: |
| 5618 | case BuiltinType::Double: |
| 5619 | return true; |
| 5620 | default: |
| 5621 | return false; |
| 5622 | } |
| 5623 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5624 | return false; |
| 5625 | } |
| 5626 | |
| 5627 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5628 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5629 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5630 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5631 | |
| 5632 | // If this is a C++ record, check the bases first. |
| 5633 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5634 | for (const auto &I : CXXRD->bases()) { |
| 5635 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5636 | |
| 5637 | // Empty bases don't affect things either way. |
| 5638 | if (isEmptyRecord(getContext(), Base, true)) |
| 5639 | continue; |
| 5640 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5641 | if (!Found.isNull()) |
| 5642 | return Ty; |
| 5643 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
| 5646 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5647 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5648 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5649 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5650 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5651 | if (getContext().getLangOpts().CPlusPlus && |
| 5652 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5653 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5654 | |
| 5655 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5656 | // Nested structures still do though. |
| 5657 | if (!Found.isNull()) |
| 5658 | return Ty; |
| 5659 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5663 | // 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] | 5664 | if (!Found.isNull()) |
| 5665 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5666 | } |
| 5667 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5668 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5671 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5672 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5673 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5674 | // struct { |
| 5675 | // i64 __gpr; |
| 5676 | // i64 __fpr; |
| 5677 | // i8 *__overflow_arg_area; |
| 5678 | // i8 *__reg_save_area; |
| 5679 | // }; |
| 5680 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5681 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5682 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5683 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5684 | Ty = getContext().getCanonicalType(Ty); |
| 5685 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5686 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5687 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5688 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5689 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5690 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5691 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5692 | CharUnits UnpaddedSize; |
| 5693 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5694 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5695 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5696 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5697 | } else { |
| 5698 | if (AI.getCoerceToType()) |
| 5699 | ArgTy = AI.getCoerceToType(); |
| 5700 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5701 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5702 | UnpaddedSize = TyInfo.first; |
| 5703 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5704 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5705 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5706 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5707 | PaddedSize = CharUnits::fromQuantity(16); |
| 5708 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5709 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5710 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5711 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5712 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5713 | llvm::Value *PaddedSizeV = |
| 5714 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5715 | |
| 5716 | if (IsVector) { |
| 5717 | // Work out the address of a vector argument on the stack. |
| 5718 | // Vector arguments are always passed in the high bits of a |
| 5719 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5720 | Address OverflowArgAreaPtr = |
| 5721 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5722 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5723 | Address OverflowArgArea = |
| 5724 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5725 | TyInfo.second); |
| 5726 | Address MemAddr = |
| 5727 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5728 | |
| 5729 | // Update overflow_arg_area_ptr pointer |
| 5730 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5731 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5732 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5733 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5734 | |
| 5735 | return MemAddr; |
| 5736 | } |
| 5737 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5738 | assert(PaddedSize.getQuantity() == 8); |
| 5739 | |
| 5740 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 5741 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5742 | if (InFPRs) { |
| 5743 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5744 | RegCountField = 1; // __fpr |
| 5745 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5746 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5747 | } else { |
| 5748 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5749 | RegCountField = 0; // __gpr |
| 5750 | RegSaveIndex = 2; // save offset for r2 |
| 5751 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5752 | } |
| 5753 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5754 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5755 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 5756 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5757 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5758 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5759 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5760 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5761 | |
| 5762 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5763 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5764 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5765 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5766 | |
| 5767 | // Emit code to load the value if it was passed in registers. |
| 5768 | CGF.EmitBlock(InRegBlock); |
| 5769 | |
| 5770 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5771 | llvm::Value *ScaledRegCount = |
| 5772 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5773 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5774 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 5775 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5776 | llvm::Value *RegOffset = |
| 5777 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5778 | Address RegSaveAreaPtr = |
| 5779 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5780 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5781 | llvm::Value *RegSaveArea = |
| 5782 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5783 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 5784 | "raw_reg_addr"), |
| 5785 | PaddedSize); |
| 5786 | Address RegAddr = |
| 5787 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5788 | |
| 5789 | // Update the register count |
| 5790 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5791 | llvm::Value *NewRegCount = |
| 5792 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5793 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5794 | CGF.EmitBranch(ContBlock); |
| 5795 | |
| 5796 | // Emit code to load the value if it was passed in memory. |
| 5797 | CGF.EmitBlock(InMemBlock); |
| 5798 | |
| 5799 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5800 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5801 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 5802 | Address OverflowArgArea = |
| 5803 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5804 | PaddedSize); |
| 5805 | Address RawMemAddr = |
| 5806 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 5807 | Address MemAddr = |
| 5808 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5809 | |
| 5810 | // Update overflow_arg_area_ptr pointer |
| 5811 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5812 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5813 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5814 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5815 | CGF.EmitBranch(ContBlock); |
| 5816 | |
| 5817 | // Return the appropriate result. |
| 5818 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5819 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5820 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5821 | |
| 5822 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5823 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 5824 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5825 | |
| 5826 | return ResAddr; |
| 5827 | } |
| 5828 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5829 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5830 | if (RetTy->isVoidType()) |
| 5831 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5832 | if (isVectorArgumentType(RetTy)) |
| 5833 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5834 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5835 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5836 | return (isPromotableIntegerType(RetTy) ? |
| 5837 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5838 | } |
| 5839 | |
| 5840 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5841 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5842 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5843 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5844 | |
| 5845 | // Integers and enums are extended to full register width. |
| 5846 | if (isPromotableIntegerType(Ty)) |
| 5847 | return ABIArgInfo::getExtend(); |
| 5848 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5849 | // Handle vector types and vector-like structure types. Note that |
| 5850 | // as opposed to float-like structure types, we do not allow any |
| 5851 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5852 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5853 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5854 | if (isVectorArgumentType(SingleElementTy) && |
| 5855 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5856 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5857 | |
| 5858 | // 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] | 5859 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5860 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5861 | |
| 5862 | // Handle small structures. |
| 5863 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5864 | // Structures with flexible arrays have variable length, so really |
| 5865 | // fail the size test above. |
| 5866 | const RecordDecl *RD = RT->getDecl(); |
| 5867 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5868 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5869 | |
| 5870 | // The structure is passed as an unextended integer, a float, or a double. |
| 5871 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5872 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5873 | assert(Size == 32 || Size == 64); |
| 5874 | if (Size == 32) |
| 5875 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5876 | else |
| 5877 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5878 | } else |
| 5879 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5880 | return ABIArgInfo::getDirect(PassTy); |
| 5881 | } |
| 5882 | |
| 5883 | // Non-structure compounds are passed indirectly. |
| 5884 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5885 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5886 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5887 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
| 5890 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5891 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5892 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5893 | |
| 5894 | namespace { |
| 5895 | |
| 5896 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5897 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5898 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5899 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5900 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5901 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5902 | }; |
| 5903 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5904 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5905 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5906 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5907 | llvm::GlobalValue *GV, |
| 5908 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5909 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5910 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5911 | // Handle 'interrupt' attribute: |
| 5912 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5913 | |
| 5914 | // Step 1: Set ISR calling convention. |
| 5915 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5916 | |
| 5917 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5918 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5919 | |
| 5920 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5921 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5922 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5923 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5924 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5925 | } |
| 5926 | } |
| 5927 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5928 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5929 | // MIPS ABI Implementation. This works for both little-endian and |
| 5930 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5931 | //===----------------------------------------------------------------------===// |
| 5932 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5933 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5934 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5935 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5936 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5937 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5938 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5939 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5940 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5941 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5942 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5943 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5944 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5945 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5946 | |
| 5947 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5948 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5949 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5950 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5951 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5952 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5953 | }; |
| 5954 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5955 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5956 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5957 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5958 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5959 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5960 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5961 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5962 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5963 | return 29; |
| 5964 | } |
| 5965 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5966 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5967 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5968 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5969 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5970 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5971 | if (FD->hasAttr<Mips16Attr>()) { |
| 5972 | Fn->addFnAttr("mips16"); |
| 5973 | } |
| 5974 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5975 | Fn->addFnAttr("nomips16"); |
| 5976 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5977 | |
| 5978 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 5979 | if (!Attr) |
| 5980 | return; |
| 5981 | |
| 5982 | const char *Kind; |
| 5983 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5984 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 5985 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 5986 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 5987 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 5988 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 5989 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 5990 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 5991 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 5992 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 5993 | } |
| 5994 | |
| 5995 | Fn->addFnAttr("interrupt", Kind); |
| 5996 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5997 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5998 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5999 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6000 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6001 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6002 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6003 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 6004 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6005 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6006 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6007 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6008 | void MipsABIInfo::CoerceToIntArgs( |
| 6009 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6010 | llvm::IntegerType *IntTy = |
| 6011 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6012 | |
| 6013 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 6014 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 6015 | ArgList.push_back(IntTy); |
| 6016 | |
| 6017 | // If necessary, add one more integer type to ArgList. |
| 6018 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 6019 | |
| 6020 | if (R) |
| 6021 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6024 | // In N32/64, an aligned double precision floating point field is passed in |
| 6025 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6026 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6027 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 6028 | |
| 6029 | if (IsO32) { |
| 6030 | CoerceToIntArgs(TySize, ArgList); |
| 6031 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6032 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6033 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 6034 | if (Ty->isComplexType()) |
| 6035 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 6036 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6037 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6038 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6039 | // Unions/vectors are passed in integer registers. |
| 6040 | if (!RT || !RT->isStructureOrClassType()) { |
| 6041 | CoerceToIntArgs(TySize, ArgList); |
| 6042 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6043 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6044 | |
| 6045 | const RecordDecl *RD = RT->getDecl(); |
| 6046 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6047 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6048 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6049 | uint64_t LastOffset = 0; |
| 6050 | unsigned idx = 0; |
| 6051 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 6052 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 6053 | // Iterate over fields in the struct/class and check if there are any aligned |
| 6054 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6055 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6056 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6057 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6058 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6059 | |
| 6060 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6061 | continue; |
| 6062 | |
| 6063 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6064 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6065 | continue; |
| 6066 | |
| 6067 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6068 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6069 | ArgList.push_back(I64); |
| 6070 | |
| 6071 | // Add double type. |
| 6072 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6073 | LastOffset = Offset + 64; |
| 6074 | } |
| 6075 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6076 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6077 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6078 | |
| 6079 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6080 | } |
| 6081 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6082 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6083 | uint64_t Offset) const { |
| 6084 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6085 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6086 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6087 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6088 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6089 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6090 | ABIArgInfo |
| 6091 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6092 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6093 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6094 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6095 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6096 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6097 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6098 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6099 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6100 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 6101 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6102 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6103 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6104 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6105 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6106 | return ABIArgInfo::getIgnore(); |
| 6107 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6108 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6109 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6110 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6111 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6112 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6113 | // If we have reached here, aggregates are passed directly by coercing to |
| 6114 | // another structure type. Padding is inserted if the offset of the |
| 6115 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6116 | ABIArgInfo ArgInfo = |
| 6117 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6118 | getPaddingType(OrigOffset, CurrOffset)); |
| 6119 | ArgInfo.setInReg(true); |
| 6120 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
| 6123 | // Treat an enum type as its underlying type. |
| 6124 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6125 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6126 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6127 | // All integral types are promoted to the GPR width. |
| 6128 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6129 | return ABIArgInfo::getExtend(); |
| 6130 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6131 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6132 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6135 | llvm::Type* |
| 6136 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6137 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6138 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6139 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6140 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6141 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6142 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6143 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6144 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6145 | // N32/64 returns struct/classes in floating point registers if the |
| 6146 | // following conditions are met: |
| 6147 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6148 | // 2. The struct/class has one or two fields all of which are floating |
| 6149 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6150 | // 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] | 6151 | // |
| 6152 | // Any other composite results are returned in integer registers. |
| 6153 | // |
| 6154 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6155 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6156 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6157 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6158 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6159 | if (!BT || !BT->isFloatingPoint()) |
| 6160 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6161 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6162 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | if (b == e) |
| 6166 | return llvm::StructType::get(getVMContext(), RTList, |
| 6167 | RD->hasAttr<PackedAttr>()); |
| 6168 | |
| 6169 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6170 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6173 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6174 | return llvm::StructType::get(getVMContext(), RTList); |
| 6175 | } |
| 6176 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6177 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6178 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6179 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6180 | if (RetTy->isVoidType()) |
| 6181 | return ABIArgInfo::getIgnore(); |
| 6182 | |
| 6183 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6184 | // However, N32/N64 ignores zero sized return values. |
| 6185 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6186 | return ABIArgInfo::getIgnore(); |
| 6187 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6188 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6189 | if (Size <= 128) { |
| 6190 | if (RetTy->isAnyComplexType()) |
| 6191 | return ABIArgInfo::getDirect(); |
| 6192 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6193 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6194 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6195 | if (!IsO32 || |
| 6196 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6197 | ABIArgInfo ArgInfo = |
| 6198 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6199 | ArgInfo.setInReg(true); |
| 6200 | return ArgInfo; |
| 6201 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6202 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6203 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6204 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | // Treat an enum type as its underlying type. |
| 6208 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6209 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6210 | |
| 6211 | return (RetTy->isPromotableIntegerType() ? |
| 6212 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6213 | } |
| 6214 | |
| 6215 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6216 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6217 | if (!getCXXABI().classifyReturnType(FI)) |
| 6218 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6219 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6220 | // 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] | 6221 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6222 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6223 | for (auto &I : FI.arguments()) |
| 6224 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6227 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6228 | QualType OrigTy) const { |
| 6229 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6230 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6231 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6232 | // 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] | 6233 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6234 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6235 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6236 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6237 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6238 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6239 | DidPromote = true; |
| 6240 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6241 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6242 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6243 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6244 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6245 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6246 | // The alignment of things in the argument area is never larger than |
| 6247 | // StackAlignInBytes. |
| 6248 | TyInfo.second = |
| 6249 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6250 | |
| 6251 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6252 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6253 | |
| 6254 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6255 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6256 | |
| 6257 | |
| 6258 | // If there was a promotion, "unpromote" into a temporary. |
| 6259 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6260 | if (DidPromote) { |
| 6261 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6262 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6263 | |
| 6264 | // Truncate down to the right width. |
| 6265 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6266 | : CGF.IntPtrTy); |
| 6267 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6268 | if (OrigTy->isPointerType()) |
| 6269 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6270 | |
| 6271 | CGF.Builder.CreateStore(V, Temp); |
| 6272 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6273 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6274 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6275 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6278 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6279 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6280 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6281 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6282 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6283 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6284 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6285 | return false; |
| 6286 | } |
| 6287 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6288 | bool |
| 6289 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6290 | llvm::Value *Address) const { |
| 6291 | // This information comes from gcc's implementation, which seems to |
| 6292 | // as canonical as it gets. |
| 6293 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6294 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6295 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6296 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6297 | |
| 6298 | // 0-31 are the general purpose registers, $0 - $31. |
| 6299 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6300 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6301 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6302 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6303 | |
| 6304 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6305 | // They are one bit wide and ignored here. |
| 6306 | |
| 6307 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6308 | // (coprocessor 1 is the FP unit) |
| 6309 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6310 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6311 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6312 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6313 | return false; |
| 6314 | } |
| 6315 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6316 | //===----------------------------------------------------------------------===// |
| 6317 | // 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] | 6318 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6319 | // handling. |
| 6320 | //===----------------------------------------------------------------------===// |
| 6321 | |
| 6322 | namespace { |
| 6323 | |
| 6324 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6325 | public: |
| 6326 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6327 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6328 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6329 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6330 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6331 | }; |
| 6332 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6333 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6334 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6335 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6336 | if (!FD) return; |
| 6337 | |
| 6338 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6339 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6340 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6341 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6342 | // OpenCL C 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); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6344 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6345 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6346 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6347 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6348 | llvm::NamedMDNode *OpenCLMetadata = |
| 6349 | M.getModule().getOrInsertNamedMetadata( |
| 6350 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6351 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6352 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6353 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6354 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6355 | Operands.push_back( |
| 6356 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6357 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6358 | Operands.push_back( |
| 6359 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6360 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6361 | Operands.push_back( |
| 6362 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6363 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6364 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6365 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6366 | // (false) for implementing the work_group_size_hint attr later. |
| 6367 | // 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] | 6368 | Operands.push_back( |
| 6369 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6370 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6371 | } |
| 6372 | } |
| 6373 | } |
| 6374 | } |
| 6375 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6376 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6377 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6378 | //===----------------------------------------------------------------------===// |
| 6379 | // Hexagon ABI Implementation |
| 6380 | //===----------------------------------------------------------------------===// |
| 6381 | |
| 6382 | namespace { |
| 6383 | |
| 6384 | class HexagonABIInfo : public ABIInfo { |
| 6385 | |
| 6386 | |
| 6387 | public: |
| 6388 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6389 | |
| 6390 | private: |
| 6391 | |
| 6392 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6393 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6394 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6395 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6396 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6397 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6398 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6399 | }; |
| 6400 | |
| 6401 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6402 | public: |
| 6403 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6404 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6405 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6406 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6407 | return 29; |
| 6408 | } |
| 6409 | }; |
| 6410 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6411 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6412 | |
| 6413 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6414 | if (!getCXXABI().classifyReturnType(FI)) |
| 6415 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6416 | for (auto &I : FI.arguments()) |
| 6417 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6421 | if (!isAggregateTypeForABI(Ty)) { |
| 6422 | // Treat an enum type as its underlying type. |
| 6423 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6424 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6425 | |
| 6426 | return (Ty->isPromotableIntegerType() ? |
| 6427 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6428 | } |
| 6429 | |
| 6430 | // Ignore empty records. |
| 6431 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6432 | return ABIArgInfo::getIgnore(); |
| 6433 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6434 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6435 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6436 | |
| 6437 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6438 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6439 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6440 | // Pass in the smallest viable integer type. |
| 6441 | else if (Size > 32) |
| 6442 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6443 | else if (Size > 16) |
| 6444 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6445 | else if (Size > 8) |
| 6446 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6447 | else |
| 6448 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6449 | } |
| 6450 | |
| 6451 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6452 | if (RetTy->isVoidType()) |
| 6453 | return ABIArgInfo::getIgnore(); |
| 6454 | |
| 6455 | // Large vector types should be returned via memory. |
| 6456 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6457 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6458 | |
| 6459 | if (!isAggregateTypeForABI(RetTy)) { |
| 6460 | // Treat an enum type as its underlying type. |
| 6461 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6462 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6463 | |
| 6464 | return (RetTy->isPromotableIntegerType() ? |
| 6465 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6466 | } |
| 6467 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6468 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6469 | return ABIArgInfo::getIgnore(); |
| 6470 | |
| 6471 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6472 | // are returned indirectly. |
| 6473 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6474 | if (Size <= 64) { |
| 6475 | // Return in the smallest viable integer type. |
| 6476 | if (Size <= 8) |
| 6477 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6478 | if (Size <= 16) |
| 6479 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6480 | if (Size <= 32) |
| 6481 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6482 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6483 | } |
| 6484 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6485 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6486 | } |
| 6487 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6488 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6489 | QualType Ty) const { |
| 6490 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6491 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6492 | getContext().getTypeInfoInChars(Ty), |
| 6493 | CharUnits::fromQuantity(4), |
| 6494 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6495 | } |
| 6496 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6497 | //===----------------------------------------------------------------------===// |
| 6498 | // AMDGPU ABI Implementation |
| 6499 | //===----------------------------------------------------------------------===// |
| 6500 | |
| 6501 | namespace { |
| 6502 | |
| 6503 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6504 | public: |
| 6505 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6506 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6507 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6508 | CodeGen::CodeGenModule &M) const override; |
| 6509 | }; |
| 6510 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6511 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6512 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6513 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6514 | const Decl *D, |
| 6515 | llvm::GlobalValue *GV, |
| 6516 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6517 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6518 | if (!FD) |
| 6519 | return; |
| 6520 | |
| 6521 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6522 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6523 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6524 | if (NumVGPR != 0) |
| 6525 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6526 | } |
| 6527 | |
| 6528 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6529 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6530 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6531 | if (NumSGPR != 0) |
| 6532 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6533 | } |
| 6534 | } |
| 6535 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6536 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6537 | //===----------------------------------------------------------------------===// |
| 6538 | // SPARC v9 ABI Implementation. |
| 6539 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6540 | // |
| 6541 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6542 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6543 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6544 | // |
| 6545 | // One case requires special care: |
| 6546 | // |
| 6547 | // struct mixed { |
| 6548 | // int i; |
| 6549 | // float f; |
| 6550 | // }; |
| 6551 | // |
| 6552 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6553 | // parameter array, but the int is passed in an integer register, and the float |
| 6554 | // is passed in a floating point register. This is represented as two arguments |
| 6555 | // with the LLVM IR inreg attribute: |
| 6556 | // |
| 6557 | // declare void f(i32 inreg %i, float inreg %f) |
| 6558 | // |
| 6559 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6560 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6561 | // bytes. |
| 6562 | // |
| 6563 | namespace { |
| 6564 | class SparcV9ABIInfo : public ABIInfo { |
| 6565 | public: |
| 6566 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6567 | |
| 6568 | private: |
| 6569 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6570 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6571 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6572 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6573 | |
| 6574 | // Coercion type builder for structs passed in registers. The coercion type |
| 6575 | // serves two purposes: |
| 6576 | // |
| 6577 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6578 | // in registers. |
| 6579 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6580 | // code generator knows to pass them in floating point registers. |
| 6581 | // |
| 6582 | // We also compute the InReg flag which indicates that the struct contains |
| 6583 | // aligned 32-bit floats. |
| 6584 | // |
| 6585 | struct CoerceBuilder { |
| 6586 | llvm::LLVMContext &Context; |
| 6587 | const llvm::DataLayout &DL; |
| 6588 | SmallVector<llvm::Type*, 8> Elems; |
| 6589 | uint64_t Size; |
| 6590 | bool InReg; |
| 6591 | |
| 6592 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6593 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6594 | |
| 6595 | // Pad Elems with integers until Size is ToSize. |
| 6596 | void pad(uint64_t ToSize) { |
| 6597 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6598 | if (ToSize == Size) |
| 6599 | return; |
| 6600 | |
| 6601 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6602 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6603 | if (Aligned > Size && Aligned <= ToSize) { |
| 6604 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6605 | Size = Aligned; |
| 6606 | } |
| 6607 | |
| 6608 | // Add whole 64-bit words. |
| 6609 | while (Size + 64 <= ToSize) { |
| 6610 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6611 | Size += 64; |
| 6612 | } |
| 6613 | |
| 6614 | // Final in-word padding. |
| 6615 | if (Size < ToSize) { |
| 6616 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6617 | Size = ToSize; |
| 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | // Add a floating point element at Offset. |
| 6622 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6623 | // Unaligned floats are treated as integers. |
| 6624 | if (Offset % Bits) |
| 6625 | return; |
| 6626 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6627 | if (Bits < 64) |
| 6628 | InReg = true; |
| 6629 | pad(Offset); |
| 6630 | Elems.push_back(Ty); |
| 6631 | Size = Offset + Bits; |
| 6632 | } |
| 6633 | |
| 6634 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6635 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6636 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6637 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6638 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6639 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6640 | switch (ElemTy->getTypeID()) { |
| 6641 | case llvm::Type::StructTyID: |
| 6642 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6643 | break; |
| 6644 | case llvm::Type::FloatTyID: |
| 6645 | addFloat(ElemOffset, ElemTy, 32); |
| 6646 | break; |
| 6647 | case llvm::Type::DoubleTyID: |
| 6648 | addFloat(ElemOffset, ElemTy, 64); |
| 6649 | break; |
| 6650 | case llvm::Type::FP128TyID: |
| 6651 | addFloat(ElemOffset, ElemTy, 128); |
| 6652 | break; |
| 6653 | case llvm::Type::PointerTyID: |
| 6654 | if (ElemOffset % 64 == 0) { |
| 6655 | pad(ElemOffset); |
| 6656 | Elems.push_back(ElemTy); |
| 6657 | Size += 64; |
| 6658 | } |
| 6659 | break; |
| 6660 | default: |
| 6661 | break; |
| 6662 | } |
| 6663 | } |
| 6664 | } |
| 6665 | |
| 6666 | // Check if Ty is a usable substitute for the coercion type. |
| 6667 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6668 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6669 | } |
| 6670 | |
| 6671 | // Get the coercion type as a literal struct type. |
| 6672 | llvm::Type *getType() const { |
| 6673 | if (Elems.size() == 1) |
| 6674 | return Elems.front(); |
| 6675 | else |
| 6676 | return llvm::StructType::get(Context, Elems); |
| 6677 | } |
| 6678 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6679 | }; |
| 6680 | } // end anonymous namespace |
| 6681 | |
| 6682 | ABIArgInfo |
| 6683 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6684 | if (Ty->isVoidType()) |
| 6685 | return ABIArgInfo::getIgnore(); |
| 6686 | |
| 6687 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6688 | |
| 6689 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6690 | // pointer / sret pointer. |
| 6691 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6692 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6693 | |
| 6694 | // Treat an enum type as its underlying type. |
| 6695 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6696 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6697 | |
| 6698 | // Integer types smaller than a register are extended. |
| 6699 | if (Size < 64 && Ty->isIntegerType()) |
| 6700 | return ABIArgInfo::getExtend(); |
| 6701 | |
| 6702 | // Other non-aggregates go in registers. |
| 6703 | if (!isAggregateTypeForABI(Ty)) |
| 6704 | return ABIArgInfo::getDirect(); |
| 6705 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6706 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6707 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 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); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6710 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6711 | // 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] | 6712 | // Build a coercion type from the LLVM struct type. |
| 6713 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6714 | if (!StrTy) |
| 6715 | return ABIArgInfo::getDirect(); |
| 6716 | |
| 6717 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6718 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6719 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6720 | |
| 6721 | // Try to use the original type for coercion. |
| 6722 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6723 | |
| 6724 | if (CB.InReg) |
| 6725 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6726 | else |
| 6727 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6728 | } |
| 6729 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6730 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6731 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6732 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6733 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6734 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6735 | AI.setCoerceToType(ArgTy); |
| 6736 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6737 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6738 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6739 | CGBuilderTy &Builder = CGF.Builder; |
| 6740 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 6741 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6742 | |
| 6743 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 6744 | |
| 6745 | Address ArgAddr = Address::invalid(); |
| 6746 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6747 | switch (AI.getKind()) { |
| 6748 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6749 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6750 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6751 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6752 | case ABIArgInfo::Extend: { |
| 6753 | Stride = SlotSize; |
| 6754 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 6755 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6756 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6757 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6758 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6759 | case ABIArgInfo::Direct: { |
| 6760 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6761 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6762 | ArgAddr = Addr; |
| 6763 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6764 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6765 | |
| 6766 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6767 | Stride = SlotSize; |
| 6768 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 6769 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 6770 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6771 | break; |
| 6772 | |
| 6773 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6774 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
| 6777 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6778 | llvm::Value *NextPtr = |
| 6779 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 6780 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6781 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6782 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6783 | } |
| 6784 | |
| 6785 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6786 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6787 | for (auto &I : FI.arguments()) |
| 6788 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | namespace { |
| 6792 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6793 | public: |
| 6794 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6795 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6796 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6797 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6798 | return 14; |
| 6799 | } |
| 6800 | |
| 6801 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6802 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6803 | }; |
| 6804 | } // end anonymous namespace |
| 6805 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6806 | bool |
| 6807 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6808 | llvm::Value *Address) const { |
| 6809 | // This is calculated from the LLVM and GCC tables and verified |
| 6810 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6811 | |
| 6812 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6813 | |
| 6814 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6815 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6816 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6817 | |
| 6818 | // 0-31: the 8-byte general-purpose registers |
| 6819 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6820 | |
| 6821 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6822 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6823 | |
| 6824 | // Y = 64 |
| 6825 | // PSR = 65 |
| 6826 | // WIM = 66 |
| 6827 | // TBR = 67 |
| 6828 | // PC = 68 |
| 6829 | // NPC = 69 |
| 6830 | // FSR = 70 |
| 6831 | // CSR = 71 |
| 6832 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6833 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6834 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6835 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6836 | |
| 6837 | return false; |
| 6838 | } |
| 6839 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6840 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6841 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6842 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6843 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6844 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6845 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6846 | |
| 6847 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6848 | /// it by reference between functions that append to it. |
| 6849 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6850 | |
| 6851 | /// TypeStringCache caches the meta encodings of Types. |
| 6852 | /// |
| 6853 | /// The reason for caching TypeStrings is two fold: |
| 6854 | /// 1. To cache a type's encoding for later uses; |
| 6855 | /// 2. As a means to break recursive member type inclusion. |
| 6856 | /// |
| 6857 | /// A cache Entry can have a Status of: |
| 6858 | /// NonRecursive: The type encoding is not recursive; |
| 6859 | /// Recursive: The type encoding is recursive; |
| 6860 | /// Incomplete: An incomplete TypeString; |
| 6861 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6862 | /// Recursive type encoding. |
| 6863 | /// |
| 6864 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6865 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6866 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6867 | /// the type is encountered. |
| 6868 | /// |
| 6869 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6870 | /// possible. The type itself is recursive and it may contain other types which |
| 6871 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6872 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6873 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6874 | /// |
| 6875 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6876 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6877 | /// are placed into the cache during type expansion as a means to identify and |
| 6878 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6879 | /// the entry becomes IncompleteUsed. |
| 6880 | /// |
| 6881 | /// During the expansion of a RecordType's members: |
| 6882 | /// |
| 6883 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6884 | /// cached encoding is used; |
| 6885 | /// |
| 6886 | /// If the cache contains a Recursive encoding for the member type, the |
| 6887 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6888 | /// |
| 6889 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6890 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6891 | /// |
| 6892 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6893 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6894 | /// it is swapped back in; |
| 6895 | /// |
| 6896 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6897 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6898 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6899 | /// |
| 6900 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6901 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6902 | /// Else the member is part of a recursive type and thus the recursion has |
| 6903 | /// been exited too soon for the encoding to be correct for the member. |
| 6904 | /// |
| 6905 | class TypeStringCache { |
| 6906 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6907 | struct Entry { |
| 6908 | std::string Str; // The encoded TypeString for the type. |
| 6909 | enum Status State; // Information about the encoding in 'Str'. |
| 6910 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6911 | // during the expansion of RecordType's members. |
| 6912 | }; |
| 6913 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6914 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6915 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6916 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6917 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6918 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6919 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6920 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6921 | bool IsRecursive); |
| 6922 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6923 | }; |
| 6924 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6925 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6926 | /// FieldEncoding is a helper for this ordering process. |
| 6927 | class FieldEncoding { |
| 6928 | bool HasName; |
| 6929 | std::string Enc; |
| 6930 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6931 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 6932 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6933 | bool operator<(const FieldEncoding &rhs) const { |
| 6934 | if (HasName != rhs.HasName) return HasName; |
| 6935 | return Enc < rhs.Enc; |
| 6936 | } |
| 6937 | }; |
| 6938 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6939 | class XCoreABIInfo : public DefaultABIInfo { |
| 6940 | public: |
| 6941 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6942 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6943 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6944 | }; |
| 6945 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6946 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6947 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6948 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6949 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6950 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6951 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6952 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6953 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6954 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6955 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6956 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6957 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6958 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6959 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6960 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6961 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6962 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 6963 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6964 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6965 | // Handle the argument. |
| 6966 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6967 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6968 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6969 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6970 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6971 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6972 | |
| 6973 | Address Val = Address::invalid(); |
| 6974 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6975 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6976 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6977 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6978 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6979 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6980 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 6981 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6982 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6983 | case ABIArgInfo::Extend: |
| 6984 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6985 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 6986 | ArgSize = CharUnits::fromQuantity( |
| 6987 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6988 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6989 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6990 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6991 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 6992 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 6993 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6994 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6995 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6996 | |
| 6997 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6998 | if (!ArgSize.isZero()) { |
| 6999 | llvm::Value *APN = |
| 7000 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 7001 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7002 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7003 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 7004 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 7005 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7006 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7007 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 7008 | /// into the cache as a means to identify and break recursion. |
| 7009 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 7010 | /// be reinserted by removeIncomplete(). |
| 7011 | /// All other types of encoding should have been used rather than arriving here. |
| 7012 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 7013 | std::string StubEnc) { |
| 7014 | if (!ID) |
| 7015 | return; |
| 7016 | Entry &E = Map[ID]; |
| 7017 | assert( (E.Str.empty() || E.State == Recursive) && |
| 7018 | "Incorrectly use of addIncomplete"); |
| 7019 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 7020 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 7021 | E.Str.swap(StubEnc); |
| 7022 | E.State = Incomplete; |
| 7023 | ++IncompleteCount; |
| 7024 | } |
| 7025 | |
| 7026 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 7027 | /// must be removed from the cache. |
| 7028 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 7029 | /// Returns true if the RecordType was defined recursively. |
| 7030 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 7031 | if (!ID) |
| 7032 | return false; |
| 7033 | auto I = Map.find(ID); |
| 7034 | assert(I != Map.end() && "Entry not present"); |
| 7035 | Entry &E = I->second; |
| 7036 | assert( (E.State == Incomplete || |
| 7037 | E.State == IncompleteUsed) && |
| 7038 | "Entry must be an incomplete type"); |
| 7039 | bool IsRecursive = false; |
| 7040 | if (E.State == IncompleteUsed) { |
| 7041 | // We made use of our Incomplete encoding, thus we are recursive. |
| 7042 | IsRecursive = true; |
| 7043 | --IncompleteUsedCount; |
| 7044 | } |
| 7045 | if (E.Swapped.empty()) |
| 7046 | Map.erase(I); |
| 7047 | else { |
| 7048 | // Swap the Recursive back. |
| 7049 | E.Swapped.swap(E.Str); |
| 7050 | E.Swapped.clear(); |
| 7051 | E.State = Recursive; |
| 7052 | } |
| 7053 | --IncompleteCount; |
| 7054 | return IsRecursive; |
| 7055 | } |
| 7056 | |
| 7057 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 7058 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 7059 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7060 | bool IsRecursive) { |
| 7061 | if (!ID || IncompleteUsedCount) |
| 7062 | return; // No key or it is is an incomplete sub-type so don't add. |
| 7063 | Entry &E = Map[ID]; |
| 7064 | if (IsRecursive && !E.Str.empty()) { |
| 7065 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 7066 | "This is not the same Recursive entry"); |
| 7067 | // The parent container was not recursive after all, so we could have used |
| 7068 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 7069 | // we started viz: IncompleteCount!=0. |
| 7070 | return; |
| 7071 | } |
| 7072 | assert(E.Str.empty() && "Entry already present"); |
| 7073 | E.Str = Str.str(); |
| 7074 | E.State = IsRecursive? Recursive : NonRecursive; |
| 7075 | } |
| 7076 | |
| 7077 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 7078 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 7079 | /// encoding is Recursive, return an empty StringRef. |
| 7080 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 7081 | if (!ID) |
| 7082 | return StringRef(); // We have no key. |
| 7083 | auto I = Map.find(ID); |
| 7084 | if (I == Map.end()) |
| 7085 | return StringRef(); // We have no encoding. |
| 7086 | Entry &E = I->second; |
| 7087 | if (E.State == Recursive && IncompleteCount) |
| 7088 | return StringRef(); // We don't use Recursive encodings for member types. |
| 7089 | |
| 7090 | if (E.State == Incomplete) { |
| 7091 | // The incomplete type is being used to break out of recursion. |
| 7092 | E.State = IncompleteUsed; |
| 7093 | ++IncompleteUsedCount; |
| 7094 | } |
| 7095 | return E.Str.c_str(); |
| 7096 | } |
| 7097 | |
| 7098 | /// The XCore ABI includes a type information section that communicates symbol |
| 7099 | /// type information to the linker. The linker uses this information to verify |
| 7100 | /// safety/correctness of things such as array bound and pointers et al. |
| 7101 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7102 | /// This type information (TypeString) is emitted into meta data for all global |
| 7103 | /// symbols: definitions, declarations, functions & variables. |
| 7104 | /// |
| 7105 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7106 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7107 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7108 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 7109 | /// |
| 7110 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7111 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 7112 | |
| 7113 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 7114 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7115 | CodeGen::CodeGenModule &CGM) const { |
| 7116 | SmallStringEnc Enc; |
| 7117 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 7118 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7119 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 7120 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7121 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 7122 | llvm::NamedMDNode *MD = |
| 7123 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 7124 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 7125 | } |
| 7126 | } |
| 7127 | |
| 7128 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7129 | const CodeGen::CodeGenModule &CGM, |
| 7130 | TypeStringCache &TSC); |
| 7131 | |
| 7132 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7133 | /// Builds a SmallVector containing the encoded field types in declaration |
| 7134 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7135 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 7136 | const RecordDecl *RD, |
| 7137 | const CodeGen::CodeGenModule &CGM, |
| 7138 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7139 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7140 | SmallStringEnc Enc; |
| 7141 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7142 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7143 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7144 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7145 | Enc += "b("; |
| 7146 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7147 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7148 | Enc += ':'; |
| 7149 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7150 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7151 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7152 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7153 | Enc += ')'; |
| 7154 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 7155 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7156 | } |
| 7157 | return true; |
| 7158 | } |
| 7159 | |
| 7160 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 7161 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 7162 | /// Union types have their fields ordered according to the ABI. |
| 7163 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 7164 | const CodeGen::CodeGenModule &CGM, |
| 7165 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 7166 | // Append the cached TypeString if we have one. |
| 7167 | StringRef TypeString = TSC.lookupStr(ID); |
| 7168 | if (!TypeString.empty()) { |
| 7169 | Enc += TypeString; |
| 7170 | return true; |
| 7171 | } |
| 7172 | |
| 7173 | // Start to emit an incomplete TypeString. |
| 7174 | size_t Start = Enc.size(); |
| 7175 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 7176 | Enc += '('; |
| 7177 | if (ID) |
| 7178 | Enc += ID->getName(); |
| 7179 | Enc += "){"; |
| 7180 | |
| 7181 | // We collect all encoded fields and order as necessary. |
| 7182 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7183 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 7184 | if (RD && !RD->field_empty()) { |
| 7185 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 7186 | // so that recursive calls to this RecordType will use it whilst building a |
| 7187 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7188 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7189 | std::string StubEnc(Enc.substr(Start).str()); |
| 7190 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7191 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7192 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7193 | (void) TSC.removeIncomplete(ID); |
| 7194 | return false; |
| 7195 | } |
| 7196 | IsRecursive = TSC.removeIncomplete(ID); |
| 7197 | // The ABI requires unions to be sorted but not structures. |
| 7198 | // See FieldEncoding::operator< for sort algorithm. |
| 7199 | if (RT->isUnionType()) |
| 7200 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7201 | // We can now complete the TypeString. |
| 7202 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7203 | for (unsigned I = 0; I != E; ++I) { |
| 7204 | if (I) |
| 7205 | Enc += ','; |
| 7206 | Enc += FE[I].str(); |
| 7207 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7208 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7209 | Enc += '}'; |
| 7210 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7211 | return true; |
| 7212 | } |
| 7213 | |
| 7214 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7215 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7216 | TypeStringCache &TSC, |
| 7217 | const IdentifierInfo *ID) { |
| 7218 | // Append the cached TypeString if we have one. |
| 7219 | StringRef TypeString = TSC.lookupStr(ID); |
| 7220 | if (!TypeString.empty()) { |
| 7221 | Enc += TypeString; |
| 7222 | return true; |
| 7223 | } |
| 7224 | |
| 7225 | size_t Start = Enc.size(); |
| 7226 | Enc += "e("; |
| 7227 | if (ID) |
| 7228 | Enc += ID->getName(); |
| 7229 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7230 | |
| 7231 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7232 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7233 | SmallVector<FieldEncoding, 16> FE; |
| 7234 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7235 | ++I) { |
| 7236 | SmallStringEnc EnumEnc; |
| 7237 | EnumEnc += "m("; |
| 7238 | EnumEnc += I->getName(); |
| 7239 | EnumEnc += "){"; |
| 7240 | I->getInitVal().toString(EnumEnc); |
| 7241 | EnumEnc += '}'; |
| 7242 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7243 | } |
| 7244 | std::sort(FE.begin(), FE.end()); |
| 7245 | unsigned E = FE.size(); |
| 7246 | for (unsigned I = 0; I != E; ++I) { |
| 7247 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7248 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7249 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7250 | } |
| 7251 | } |
| 7252 | Enc += '}'; |
| 7253 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7254 | return true; |
| 7255 | } |
| 7256 | |
| 7257 | /// Appends type's qualifier to Enc. |
| 7258 | /// This is done prior to appending the type's encoding. |
| 7259 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7260 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 7261 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7262 | int Lookup = 0; |
| 7263 | if (QT.isConstQualified()) |
| 7264 | Lookup += 1<<0; |
| 7265 | if (QT.isRestrictQualified()) |
| 7266 | Lookup += 1<<1; |
| 7267 | if (QT.isVolatileQualified()) |
| 7268 | Lookup += 1<<2; |
| 7269 | Enc += Table[Lookup]; |
| 7270 | } |
| 7271 | |
| 7272 | /// Appends built-in types to Enc. |
| 7273 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7274 | const char *EncType; |
| 7275 | switch (BT->getKind()) { |
| 7276 | case BuiltinType::Void: |
| 7277 | EncType = "0"; |
| 7278 | break; |
| 7279 | case BuiltinType::Bool: |
| 7280 | EncType = "b"; |
| 7281 | break; |
| 7282 | case BuiltinType::Char_U: |
| 7283 | EncType = "uc"; |
| 7284 | break; |
| 7285 | case BuiltinType::UChar: |
| 7286 | EncType = "uc"; |
| 7287 | break; |
| 7288 | case BuiltinType::SChar: |
| 7289 | EncType = "sc"; |
| 7290 | break; |
| 7291 | case BuiltinType::UShort: |
| 7292 | EncType = "us"; |
| 7293 | break; |
| 7294 | case BuiltinType::Short: |
| 7295 | EncType = "ss"; |
| 7296 | break; |
| 7297 | case BuiltinType::UInt: |
| 7298 | EncType = "ui"; |
| 7299 | break; |
| 7300 | case BuiltinType::Int: |
| 7301 | EncType = "si"; |
| 7302 | break; |
| 7303 | case BuiltinType::ULong: |
| 7304 | EncType = "ul"; |
| 7305 | break; |
| 7306 | case BuiltinType::Long: |
| 7307 | EncType = "sl"; |
| 7308 | break; |
| 7309 | case BuiltinType::ULongLong: |
| 7310 | EncType = "ull"; |
| 7311 | break; |
| 7312 | case BuiltinType::LongLong: |
| 7313 | EncType = "sll"; |
| 7314 | break; |
| 7315 | case BuiltinType::Float: |
| 7316 | EncType = "ft"; |
| 7317 | break; |
| 7318 | case BuiltinType::Double: |
| 7319 | EncType = "d"; |
| 7320 | break; |
| 7321 | case BuiltinType::LongDouble: |
| 7322 | EncType = "ld"; |
| 7323 | break; |
| 7324 | default: |
| 7325 | return false; |
| 7326 | } |
| 7327 | Enc += EncType; |
| 7328 | return true; |
| 7329 | } |
| 7330 | |
| 7331 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7332 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7333 | const CodeGen::CodeGenModule &CGM, |
| 7334 | TypeStringCache &TSC) { |
| 7335 | Enc += "p("; |
| 7336 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7337 | return false; |
| 7338 | Enc += ')'; |
| 7339 | return true; |
| 7340 | } |
| 7341 | |
| 7342 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7343 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7344 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7345 | const CodeGen::CodeGenModule &CGM, |
| 7346 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7347 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7348 | return false; |
| 7349 | Enc += "a("; |
| 7350 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7351 | CAT->getSize().toStringUnsigned(Enc); |
| 7352 | else |
| 7353 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7354 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7355 | // The Qualifiers should be attached to the type rather than the array. |
| 7356 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7357 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7358 | return false; |
| 7359 | Enc += ')'; |
| 7360 | return true; |
| 7361 | } |
| 7362 | |
| 7363 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7364 | /// and the arguments. |
| 7365 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7366 | const CodeGen::CodeGenModule &CGM, |
| 7367 | TypeStringCache &TSC) { |
| 7368 | Enc += "f{"; |
| 7369 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7370 | return false; |
| 7371 | Enc += "}("; |
| 7372 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7373 | // N.B. we are only interested in the adjusted param types. |
| 7374 | auto I = FPT->param_type_begin(); |
| 7375 | auto E = FPT->param_type_end(); |
| 7376 | if (I != E) { |
| 7377 | do { |
| 7378 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7379 | return false; |
| 7380 | ++I; |
| 7381 | if (I != E) |
| 7382 | Enc += ','; |
| 7383 | } while (I != E); |
| 7384 | if (FPT->isVariadic()) |
| 7385 | Enc += ",va"; |
| 7386 | } else { |
| 7387 | if (FPT->isVariadic()) |
| 7388 | Enc += "va"; |
| 7389 | else |
| 7390 | Enc += '0'; |
| 7391 | } |
| 7392 | } |
| 7393 | Enc += ')'; |
| 7394 | return true; |
| 7395 | } |
| 7396 | |
| 7397 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7398 | /// type encodings. |
| 7399 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7400 | const CodeGen::CodeGenModule &CGM, |
| 7401 | TypeStringCache &TSC) { |
| 7402 | |
| 7403 | QualType QT = QType.getCanonicalType(); |
| 7404 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7405 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7406 | // The Qualifiers should be attached to the type rather than the array. |
| 7407 | // Thus we don't call appendQualifier() here. |
| 7408 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7409 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7410 | appendQualifier(Enc, QT); |
| 7411 | |
| 7412 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7413 | return appendBuiltinType(Enc, BT); |
| 7414 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7415 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7416 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7417 | |
| 7418 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7419 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7420 | |
| 7421 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7422 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7423 | |
| 7424 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7425 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7426 | |
| 7427 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7428 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7429 | |
| 7430 | return false; |
| 7431 | } |
| 7432 | |
| 7433 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7434 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7435 | if (!D) |
| 7436 | return false; |
| 7437 | |
| 7438 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7439 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7440 | return false; |
| 7441 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7442 | } |
| 7443 | |
| 7444 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7445 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7446 | return false; |
| 7447 | QualType QT = VD->getType().getCanonicalType(); |
| 7448 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7449 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7450 | // The Qualifiers should be attached to the type rather than the array. |
| 7451 | // Thus we don't call appendQualifier() here. |
| 7452 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7453 | } |
| 7454 | return appendType(Enc, QT, CGM, TSC); |
| 7455 | } |
| 7456 | return false; |
| 7457 | } |
| 7458 | |
| 7459 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7460 | //===----------------------------------------------------------------------===// |
| 7461 | // Driver code |
| 7462 | //===----------------------------------------------------------------------===// |
| 7463 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7464 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7465 | return getTarget().getTriple(); |
| 7466 | } |
| 7467 | |
| 7468 | bool CodeGenModule::supportsCOMDAT() const { |
| 7469 | return !getTriple().isOSBinFormatMachO(); |
| 7470 | } |
| 7471 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7472 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7473 | if (TheTargetCodeGenInfo) |
| 7474 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7475 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7476 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7477 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7478 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7479 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7480 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7481 | case llvm::Triple::le32: |
| 7482 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7483 | case llvm::Triple::mips: |
| 7484 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 7485 | if (Triple.getOS() == llvm::Triple::NaCl) |
| 7486 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7487 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7488 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7489 | case llvm::Triple::mips64: |
| 7490 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7491 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7492 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7493 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7494 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7495 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7496 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7497 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7498 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7499 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7500 | } |
| 7501 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 7502 | case llvm::Triple::wasm32: |
| 7503 | case llvm::Triple::wasm64: |
| 7504 | return *(TheTargetCodeGenInfo = new WebAssemblyTargetCodeGenInfo(Types)); |
| 7505 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7506 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7507 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7508 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7509 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7510 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7511 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7512 | TheTargetCodeGenInfo = |
| 7513 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7514 | return *TheTargetCodeGenInfo; |
| 7515 | } |
| 7516 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7517 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7518 | StringRef ABIStr = getTarget().getABI(); |
| 7519 | if (ABIStr == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7520 | Kind = ARMABIInfo::APCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7521 | else if (ABIStr == "aapcs16") |
| 7522 | Kind = ARMABIInfo::AAPCS16_VFP; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7523 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7524 | (CodeGenOpts.FloatABI != "soft" && |
| 7525 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7526 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7527 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7528 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7529 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7530 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7531 | case llvm::Triple::ppc: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 7532 | return *(TheTargetCodeGenInfo = |
| 7533 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7534 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7535 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7536 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7537 | if (getTarget().getABI() == "elfv2") |
| 7538 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7539 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7540 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7541 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7542 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7543 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7544 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7545 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7546 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7547 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7548 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7549 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7550 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7551 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7552 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7553 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7554 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7555 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7556 | case llvm::Triple::nvptx: |
| 7557 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7558 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7559 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7560 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7561 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7562 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7563 | case llvm::Triple::systemz: { |
| 7564 | bool HasVector = getTarget().getABI() == "vector"; |
| 7565 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7566 | HasVector)); |
| 7567 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7568 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7569 | case llvm::Triple::tce: |
| 7570 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7571 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7572 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7573 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7574 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7575 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7576 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7577 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7578 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7579 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7580 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7581 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7582 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7583 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7584 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 7585 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 7586 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7587 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7588 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7589 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7590 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7591 | StringRef ABI = getTarget().getABI(); |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 7592 | X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 : |
| 7593 | ABI == "avx" ? X86AVXABILevel::AVX : |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7594 | X86AVXABILevel::None); |
| 7595 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7596 | switch (Triple.getOS()) { |
| 7597 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7598 | return *(TheTargetCodeGenInfo = |
| 7599 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7600 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7601 | return *(TheTargetCodeGenInfo = |
| 7602 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7603 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7604 | return *(TheTargetCodeGenInfo = |
| 7605 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7606 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7607 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7608 | case llvm::Triple::hexagon: |
| 7609 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7610 | case llvm::Triple::r600: |
| 7611 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7612 | case llvm::Triple::amdgcn: |
| 7613 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7614 | case llvm::Triple::sparcv9: |
| 7615 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7616 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7617 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7618 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7619 | } |