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 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 133 | 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. |
| 220 | CharUnits FullDirectSize = DirectSize.RoundUpToAlignment(SlotSize); |
| 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) |
| 367 | return 0; |
| 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; |
| 844 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 845 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 846 | /// \brief Rewrite the function info so that all memory arguments use |
| 847 | /// inalloca. |
| 848 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 849 | |
| 850 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 851 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 852 | QualType Type) const; |
| 853 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 854 | public: |
| 855 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 856 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 857 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 858 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 859 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 860 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 861 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 862 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 863 | : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
| 864 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
| 865 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 866 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 867 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 868 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 869 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 870 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 871 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 872 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 873 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 874 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 875 | unsigned NumRegisterParameters, bool SoftFloatABI) |
| 876 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 877 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
| 878 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 879 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 880 | static bool isStructReturnInRegABI( |
| 881 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 882 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 883 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 884 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 885 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 886 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 887 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 888 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 889 | return 4; |
| 890 | } |
| 891 | |
| 892 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 893 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 894 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 895 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 896 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 897 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 898 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 899 | } |
| 900 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 901 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 902 | std::string &Constraints, |
| 903 | std::vector<llvm::Type *> &ResultRegTypes, |
| 904 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 905 | std::vector<LValue> &ResultRegDests, |
| 906 | std::string &AsmString, |
| 907 | unsigned NumOutputs) const override; |
| 908 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 909 | llvm::Constant * |
| 910 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 911 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 912 | (0x06 << 8) | // .+0x08 |
| 913 | ('F' << 16) | |
| 914 | ('T' << 24); |
| 915 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 916 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 917 | }; |
| 918 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 919 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 920 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 921 | /// Rewrite input constraint references after adding some output constraints. |
| 922 | /// In the case where there is one output and one input and we add one output, |
| 923 | /// we need to replace all operand references greater than or equal to 1: |
| 924 | /// mov $0, $1 |
| 925 | /// mov eax, $1 |
| 926 | /// The result will be: |
| 927 | /// mov $0, $2 |
| 928 | /// mov eax, $2 |
| 929 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 930 | unsigned NumNewOuts, |
| 931 | std::string &AsmString) { |
| 932 | std::string Buf; |
| 933 | llvm::raw_string_ostream OS(Buf); |
| 934 | size_t Pos = 0; |
| 935 | while (Pos < AsmString.size()) { |
| 936 | size_t DollarStart = AsmString.find('$', Pos); |
| 937 | if (DollarStart == std::string::npos) |
| 938 | DollarStart = AsmString.size(); |
| 939 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 940 | if (DollarEnd == std::string::npos) |
| 941 | DollarEnd = AsmString.size(); |
| 942 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 943 | Pos = DollarEnd; |
| 944 | size_t NumDollars = DollarEnd - DollarStart; |
| 945 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 946 | // We have an operand reference. |
| 947 | size_t DigitStart = Pos; |
| 948 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 949 | if (DigitEnd == std::string::npos) |
| 950 | DigitEnd = AsmString.size(); |
| 951 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 952 | unsigned OperandIndex; |
| 953 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 954 | if (OperandIndex >= FirstIn) |
| 955 | OperandIndex += NumNewOuts; |
| 956 | OS << OperandIndex; |
| 957 | } else { |
| 958 | OS << OperandStr; |
| 959 | } |
| 960 | Pos = DigitEnd; |
| 961 | } |
| 962 | } |
| 963 | AsmString = std::move(OS.str()); |
| 964 | } |
| 965 | |
| 966 | /// Add output constraints for EAX:EDX because they are return registers. |
| 967 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 968 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 969 | std::vector<llvm::Type *> &ResultRegTypes, |
| 970 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 971 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 972 | unsigned NumOutputs) const { |
| 973 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 974 | |
| 975 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 976 | // larger. |
| 977 | if (!Constraints.empty()) |
| 978 | Constraints += ','; |
| 979 | if (RetWidth <= 32) { |
| 980 | Constraints += "={eax}"; |
| 981 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 982 | } else { |
| 983 | // Use the 'A' constraint for EAX:EDX. |
| 984 | Constraints += "=A"; |
| 985 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 986 | } |
| 987 | |
| 988 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 989 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 990 | ResultTruncRegTypes.push_back(CoerceTy); |
| 991 | |
| 992 | // Coerce the integer by bitcasting the return slot pointer. |
| 993 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 994 | CoerceTy->getPointerTo())); |
| 995 | ResultRegDests.push_back(ReturnSlot); |
| 996 | |
| 997 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 998 | } |
| 999 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1000 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1001 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1002 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1003 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1004 | uint64_t Size = Context.getTypeSize(Ty); |
| 1005 | |
| 1006 | // Type must be register sized. |
| 1007 | if (!isRegisterSize(Size)) |
| 1008 | return false; |
| 1009 | |
| 1010 | if (Ty->isVectorType()) { |
| 1011 | // 64- and 128- bit vectors inside structures are not returned in |
| 1012 | // registers. |
| 1013 | if (Size == 64 || Size == 128) |
| 1014 | return false; |
| 1015 | |
| 1016 | return true; |
| 1017 | } |
| 1018 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1019 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1020 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1021 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1022 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1023 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1024 | return true; |
| 1025 | |
| 1026 | // Arrays are treated like records. |
| 1027 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1028 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1031 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1032 | if (!RT) return false; |
| 1033 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1034 | // FIXME: Traverse bases here too. |
| 1035 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1036 | // Structure types are passed in register if all fields would be |
| 1037 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1038 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1039 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1040 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1041 | continue; |
| 1042 | |
| 1043 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1044 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1045 | return false; |
| 1046 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1047 | return true; |
| 1048 | } |
| 1049 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1050 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1051 | // If the return value is indirect, then the hidden argument is consuming one |
| 1052 | // integer register. |
| 1053 | if (State.FreeRegs) { |
| 1054 | --State.FreeRegs; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1055 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1056 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1057 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1060 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1061 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1062 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1063 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1064 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1065 | const Type *Base = nullptr; |
| 1066 | uint64_t NumElts = 0; |
| 1067 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1068 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1069 | // The LLVM struct type for such an aggregate should lower properly. |
| 1070 | return ABIArgInfo::getDirect(); |
| 1071 | } |
| 1072 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1073 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1074 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1075 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1076 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1077 | |
| 1078 | // 128-bit vectors are a special case; they are returned in |
| 1079 | // registers and we need to make sure to pick a type the LLVM |
| 1080 | // backend will like. |
| 1081 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1082 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1083 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1084 | |
| 1085 | // Always return in register if it fits in a general purpose |
| 1086 | // register, or if it is 64 bits and has a single element. |
| 1087 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1088 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1089 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1090 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1091 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1092 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1096 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1097 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1098 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1099 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1100 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1101 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1102 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1103 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1104 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1105 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1106 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1107 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1108 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1109 | // Small structures which are register sized are generally returned |
| 1110 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1111 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1112 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1113 | |
| 1114 | // As a special-case, if the struct is a "single-element" struct, and |
| 1115 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1116 | // floating-point register. (MSVC does not apply this special case.) |
| 1117 | // We apply a similar transformation for pointer types to improve the |
| 1118 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1119 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1120 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1121 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1122 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1123 | |
| 1124 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1125 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1126 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1129 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1130 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1131 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1132 | // Treat an enum type as its underlying type. |
| 1133 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1134 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1135 | |
| 1136 | return (RetTy->isPromotableIntegerType() ? |
| 1137 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1140 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1141 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1142 | } |
| 1143 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1144 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1145 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1146 | if (!RT) |
| 1147 | return 0; |
| 1148 | const RecordDecl *RD = RT->getDecl(); |
| 1149 | |
| 1150 | // If this is a C++ record, check the bases first. |
| 1151 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1152 | for (const auto &I : CXXRD->bases()) |
| 1153 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1154 | return false; |
| 1155 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1156 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1157 | QualType FT = i->getType(); |
| 1158 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1159 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1160 | return true; |
| 1161 | |
| 1162 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1163 | return true; |
| 1164 | } |
| 1165 | |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1169 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1170 | unsigned Align) const { |
| 1171 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1172 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1173 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1174 | return 0; // Use default alignment. |
| 1175 | |
| 1176 | // On non-Darwin, the stack type alignment is always 4. |
| 1177 | if (!IsDarwinVectorABI) { |
| 1178 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1179 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1180 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1181 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1182 | // 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] | 1183 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1184 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1185 | return 16; |
| 1186 | |
| 1187 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1190 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1191 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1192 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1193 | if (State.FreeRegs) { |
| 1194 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1195 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1196 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1197 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1198 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1199 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1200 | // Compute the byval alignment. |
| 1201 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1202 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1203 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1204 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1205 | |
| 1206 | // If the stack alignment is less than the type alignment, realign the |
| 1207 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1208 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1209 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1210 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1213 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1214 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1215 | if (!T) |
| 1216 | T = Ty.getTypePtr(); |
| 1217 | |
| 1218 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1219 | BuiltinType::Kind K = BT->getKind(); |
| 1220 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1221 | return Float; |
| 1222 | } |
| 1223 | return Integer; |
| 1224 | } |
| 1225 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1226 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 1227 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1228 | NeedsPadding = false; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1229 | if (!IsSoftFloatABI) { |
| 1230 | Class C = classify(Ty); |
| 1231 | if (C == Float) |
| 1232 | return false; |
| 1233 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1234 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1235 | unsigned Size = getContext().getTypeSize(Ty); |
| 1236 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1237 | |
| 1238 | if (SizeInRegs == 0) |
| 1239 | return false; |
| 1240 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1241 | if (!IsMCUABI) { |
| 1242 | if (SizeInRegs > State.FreeRegs) { |
| 1243 | State.FreeRegs = 0; |
| 1244 | return false; |
| 1245 | } |
| 1246 | } else { |
| 1247 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1248 | // earlier parameters that are passed on the stack. Also, |
| 1249 | // it does not allow passing >8-byte structs in-register, |
| 1250 | // even if there are 3 free registers available. |
| 1251 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1252 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1253 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1254 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1255 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1256 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1257 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1258 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1259 | if (Size > 32) |
| 1260 | return false; |
| 1261 | |
| 1262 | if (Ty->isIntegralOrEnumerationType()) |
| 1263 | return true; |
| 1264 | |
| 1265 | if (Ty->isPointerType()) |
| 1266 | return true; |
| 1267 | |
| 1268 | if (Ty->isReferenceType()) |
| 1269 | return true; |
| 1270 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1271 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1272 | NeedsPadding = true; |
| 1273 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1274 | return false; |
| 1275 | } |
| 1276 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1277 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1280 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1281 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1282 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1283 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1284 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1285 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1286 | // Check with the C++ ABI first. |
| 1287 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1288 | if (RT) { |
| 1289 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1290 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1291 | return getIndirectResult(Ty, false, State); |
| 1292 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1293 | // The field index doesn't matter, we'll fix it up later. |
| 1294 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1299 | // to other targets. |
| 1300 | const Type *Base = nullptr; |
| 1301 | uint64_t NumElts = 0; |
| 1302 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1303 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1304 | if (State.FreeSSERegs >= NumElts) { |
| 1305 | State.FreeSSERegs -= NumElts; |
| 1306 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1307 | return ABIArgInfo::getDirect(); |
| 1308 | return ABIArgInfo::getExpand(); |
| 1309 | } |
| 1310 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1311 | } |
| 1312 | |
| 1313 | if (isAggregateTypeForABI(Ty)) { |
| 1314 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1315 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1316 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1317 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1318 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1319 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1320 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1321 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1322 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1323 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1324 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1325 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1326 | return ABIArgInfo::getIgnore(); |
| 1327 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1328 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1329 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 1330 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1331 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1332 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1333 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1334 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 1335 | return ABIArgInfo::getDirectInReg(Result); |
| 1336 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1337 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1338 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1339 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1340 | // of those arguments will match the struct. This is important because the |
| 1341 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1342 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1343 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 1344 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1345 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1346 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1347 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1348 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1349 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1350 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1353 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1354 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1355 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1356 | if (IsDarwinVectorABI) { |
| 1357 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1358 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1359 | (Size == 64 && VT->getNumElements() == 1)) |
| 1360 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1361 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1362 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1363 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1364 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1365 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1367 | return ABIArgInfo::getDirect(); |
| 1368 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1369 | |
| 1370 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1371 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1372 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1373 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1374 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1375 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1376 | |
| 1377 | if (Ty->isPromotableIntegerType()) { |
| 1378 | if (InReg) |
| 1379 | return ABIArgInfo::getExtendInReg(); |
| 1380 | return ABIArgInfo::getExtend(); |
| 1381 | } |
| 1382 | if (InReg) |
| 1383 | return ABIArgInfo::getDirectInReg(); |
| 1384 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1387 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1388 | CCState State(FI.getCallingConvention()); |
| 1389 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1390 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1391 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1392 | State.FreeRegs = 2; |
| 1393 | State.FreeSSERegs = 6; |
| 1394 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1395 | State.FreeRegs = FI.getRegParm(); |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1396 | else if (IsMCUABI) |
| 1397 | State.FreeRegs = 3; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1398 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1399 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1400 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1401 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1402 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1403 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1404 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1405 | // return value was sret and put it in a register ourselves if appropriate. |
| 1406 | if (State.FreeRegs) { |
| 1407 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1408 | FI.getReturnInfo().setInReg(true); |
| 1409 | } |
| 1410 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1411 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1412 | // The chain argument effectively gives us another free register. |
| 1413 | if (FI.isChainCall()) |
| 1414 | ++State.FreeRegs; |
| 1415 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1416 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1417 | for (auto &I : FI.arguments()) { |
| 1418 | I.info = classifyArgumentType(I.type, State); |
| 1419 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1423 | // all the memory arguments to use inalloca. |
| 1424 | if (UsedInAlloca) |
| 1425 | rewriteWithInAlloca(FI); |
| 1426 | } |
| 1427 | |
| 1428 | void |
| 1429 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1430 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1431 | QualType Type) const { |
| 1432 | // Arguments are always 4-byte-aligned. |
| 1433 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1434 | |
| 1435 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1436 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1437 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1438 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1439 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1440 | // Insert padding bytes to respect alignment. |
| 1441 | CharUnits FieldEnd = StackOffset; |
| 1442 | StackOffset = FieldEnd.RoundUpToAlignment(FieldAlign); |
| 1443 | if (StackOffset != FieldEnd) { |
| 1444 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1445 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1446 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1447 | FrameFields.push_back(Ty); |
| 1448 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1451 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1452 | // Leave ignored and inreg arguments alone. |
| 1453 | switch (Info.getKind()) { |
| 1454 | case ABIArgInfo::InAlloca: |
| 1455 | return true; |
| 1456 | case ABIArgInfo::Indirect: |
| 1457 | assert(Info.getIndirectByVal()); |
| 1458 | return true; |
| 1459 | case ABIArgInfo::Ignore: |
| 1460 | return false; |
| 1461 | case ABIArgInfo::Direct: |
| 1462 | case ABIArgInfo::Extend: |
| 1463 | case ABIArgInfo::Expand: |
| 1464 | if (Info.getInReg()) |
| 1465 | return false; |
| 1466 | return true; |
| 1467 | } |
| 1468 | llvm_unreachable("invalid enum"); |
| 1469 | } |
| 1470 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1471 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1472 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1473 | |
| 1474 | // Build a packed struct type for all of the arguments in memory. |
| 1475 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1476 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1477 | // The stack alignment is always 4. |
| 1478 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1479 | |
| 1480 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1481 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1482 | |
| 1483 | // Put 'this' into the struct before 'sret', if necessary. |
| 1484 | bool IsThisCall = |
| 1485 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1486 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1487 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1488 | isArgInAlloca(I->info)) { |
| 1489 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1490 | ++I; |
| 1491 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1492 | |
| 1493 | // 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] | 1494 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1495 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1496 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1497 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1498 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1502 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1503 | ++I; |
| 1504 | |
| 1505 | // Put arguments passed in memory into the struct. |
| 1506 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1507 | if (isArgInAlloca(I->info)) |
| 1508 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1512 | /*isPacked=*/true), |
| 1513 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1516 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1517 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1518 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1519 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1520 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1521 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1522 | // |
| 1523 | // Just messing with TypeInfo like this works because we never pass |
| 1524 | // anything indirectly. |
| 1525 | TypeInfo.second = CharUnits::fromQuantity( |
| 1526 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1527 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1528 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1529 | TypeInfo, CharUnits::fromQuantity(4), |
| 1530 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1533 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1534 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1535 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1536 | |
| 1537 | switch (Opts.getStructReturnConvention()) { |
| 1538 | case CodeGenOptions::SRCK_Default: |
| 1539 | break; |
| 1540 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1541 | return false; |
| 1542 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1543 | return true; |
| 1544 | } |
| 1545 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1546 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1547 | return true; |
| 1548 | |
| 1549 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1550 | case llvm::Triple::DragonFly: |
| 1551 | case llvm::Triple::FreeBSD: |
| 1552 | case llvm::Triple::OpenBSD: |
| 1553 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1554 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1555 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1556 | default: |
| 1557 | return false; |
| 1558 | } |
| 1559 | } |
| 1560 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1561 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1562 | llvm::GlobalValue *GV, |
| 1563 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1564 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1565 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1566 | // Get the LLVM function. |
| 1567 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1568 | |
| 1569 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1570 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1571 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1572 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1573 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1574 | llvm::AttributeSet::FunctionIndex, |
| 1575 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1580 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1581 | CodeGen::CodeGenFunction &CGF, |
| 1582 | llvm::Value *Address) const { |
| 1583 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1585 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1586 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1587 | // 0-7 are the eight integer registers; the order is different |
| 1588 | // on Darwin (for EH), but the range is the same. |
| 1589 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1590 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1591 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1592 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1593 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1594 | // These have size 16, which is sizeof(long double) on |
| 1595 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1596 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1597 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1598 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1599 | } else { |
| 1600 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1601 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1602 | Builder.CreateAlignedStore( |
| 1603 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1604 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1605 | |
| 1606 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1607 | // These have size 12, which is sizeof(long double) on |
| 1608 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1609 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1610 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1611 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1612 | |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1616 | //===----------------------------------------------------------------------===// |
| 1617 | // X86-64 ABI Implementation |
| 1618 | //===----------------------------------------------------------------------===// |
| 1619 | |
| 1620 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1621 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1622 | /// The AVX ABI level for X86 targets. |
| 1623 | enum class X86AVXABILevel { |
| 1624 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1625 | AVX, |
| 1626 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1627 | }; |
| 1628 | |
| 1629 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1630 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1631 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1632 | case X86AVXABILevel::AVX512: |
| 1633 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1634 | case X86AVXABILevel::AVX: |
| 1635 | return 256; |
| 1636 | case X86AVXABILevel::None: |
| 1637 | return 128; |
| 1638 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1639 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1642 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1643 | class X86_64ABIInfo : public ABIInfo { |
| 1644 | enum Class { |
| 1645 | Integer = 0, |
| 1646 | SSE, |
| 1647 | SSEUp, |
| 1648 | X87, |
| 1649 | X87Up, |
| 1650 | ComplexX87, |
| 1651 | NoClass, |
| 1652 | Memory |
| 1653 | }; |
| 1654 | |
| 1655 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1656 | /// |
| 1657 | /// Merge an accumulating classification \arg Accum with a field |
| 1658 | /// classification \arg Field. |
| 1659 | /// |
| 1660 | /// \param Accum - The accumulating classification. This should |
| 1661 | /// always be either NoClass or the result of a previous merge |
| 1662 | /// call. In addition, this should never be Memory (the caller |
| 1663 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1664 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1665 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1666 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1667 | /// |
| 1668 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1669 | /// final MEMORY or SSE classes when necessary. |
| 1670 | /// |
| 1671 | /// \param AggregateSize - The size of the current aggregate in |
| 1672 | /// the classification process. |
| 1673 | /// |
| 1674 | /// \param Lo - The classification for the parts of the type |
| 1675 | /// residing in the low word of the containing object. |
| 1676 | /// |
| 1677 | /// \param Hi - The classification for the parts of the type |
| 1678 | /// residing in the higher words of the containing object. |
| 1679 | /// |
| 1680 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1681 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1682 | /// classify - Determine the x86_64 register classes in which the |
| 1683 | /// given type T should be passed. |
| 1684 | /// |
| 1685 | /// \param Lo - The classification for the parts of the type |
| 1686 | /// residing in the low word of the containing object. |
| 1687 | /// |
| 1688 | /// \param Hi - The classification for the parts of the type |
| 1689 | /// residing in the high word of the containing object. |
| 1690 | /// |
| 1691 | /// \param OffsetBase - The bit offset of this type in the |
| 1692 | /// containing object. Some parameters are classified different |
| 1693 | /// depending on whether they straddle an eightbyte boundary. |
| 1694 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1695 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1696 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1697 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1698 | /// If a word is unused its result will be NoClass; if a type should |
| 1699 | /// be passed in Memory then at least the classification of \arg Lo |
| 1700 | /// will be Memory. |
| 1701 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1702 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1703 | /// |
| 1704 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1705 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1706 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1707 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1708 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1709 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1710 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1711 | unsigned IROffset, QualType SourceTy, |
| 1712 | unsigned SourceOffset) const; |
| 1713 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1714 | unsigned IROffset, QualType SourceTy, |
| 1715 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1716 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1717 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1718 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1719 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1720 | |
| 1721 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1722 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1723 | /// |
| 1724 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1725 | /// available. |
| 1726 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1728 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1729 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1730 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1731 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1732 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1733 | unsigned &neededSSE, |
| 1734 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1735 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1736 | bool IsIllegalVectorType(QualType Ty) const; |
| 1737 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1738 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1739 | /// unfortunately in ways that were not always consistent with |
| 1740 | /// certain previous compilers. In particular, platforms which |
| 1741 | /// required strict binary compatibility with older versions of GCC |
| 1742 | /// may need to exempt themselves. |
| 1743 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1744 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1747 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1748 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1749 | // 64-bit hardware. |
| 1750 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1751 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1752 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1753 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| 1754 | ABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1755 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1756 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1757 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1758 | bool isPassedUsingAVXType(QualType type) const { |
| 1759 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1760 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1761 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1762 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1763 | if (info.isDirect()) { |
| 1764 | llvm::Type *ty = info.getCoerceToType(); |
| 1765 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1766 | return (vectorTy->getBitWidth() > 128); |
| 1767 | } |
| 1768 | return false; |
| 1769 | } |
| 1770 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1771 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1772 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1773 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1774 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1775 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1776 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1777 | |
| 1778 | bool has64BitPointers() const { |
| 1779 | return Has64BitPointers; |
| 1780 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1781 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1782 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1783 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1784 | class WinX86_64ABIInfo : public ABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1785 | public: |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1786 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) |
| 1787 | : ABIInfo(CGT), |
| 1788 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1789 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1790 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1791 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1792 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1793 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1794 | |
| 1795 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1796 | // FIXME: Assumes vectorcall is in use. |
| 1797 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1798 | } |
| 1799 | |
| 1800 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1801 | uint64_t NumMembers) const override { |
| 1802 | // FIXME: Assumes vectorcall is in use. |
| 1803 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1804 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 1805 | |
| 1806 | private: |
| 1807 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1808 | bool IsReturnType) const; |
| 1809 | |
| 1810 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1811 | }; |
| 1812 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1813 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1814 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1815 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1816 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1817 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1818 | const X86_64ABIInfo &getABIInfo() const { |
| 1819 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1820 | } |
| 1821 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1822 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1823 | return 7; |
| 1824 | } |
| 1825 | |
| 1826 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1827 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1828 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1829 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1830 | // 0-15 are the 16 integer registers. |
| 1831 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1832 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1833 | return false; |
| 1834 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1835 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1836 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1837 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1838 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1839 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1840 | } |
| 1841 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1842 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1843 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1844 | // The default CC on x86-64 sets %al to the number of SSA |
| 1845 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1846 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1847 | // that when AVX types are involved: the ABI explicitly states it is |
| 1848 | // undefined, and it doesn't work in practice because of how the ABI |
| 1849 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1850 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1851 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1852 | for (CallArgList::const_iterator |
| 1853 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1854 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1855 | HasAVXType = true; |
| 1856 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1857 | } |
| 1858 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1859 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1860 | if (!HasAVXType) |
| 1861 | return true; |
| 1862 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1863 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1864 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1867 | llvm::Constant * |
| 1868 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1869 | unsigned Sig; |
| 1870 | if (getABIInfo().has64BitPointers()) |
| 1871 | Sig = (0xeb << 0) | // jmp rel8 |
| 1872 | (0x0a << 8) | // .+0x0c |
| 1873 | ('F' << 16) | |
| 1874 | ('T' << 24); |
| 1875 | else |
| 1876 | Sig = (0xeb << 0) | // jmp rel8 |
| 1877 | (0x06 << 8) | // .+0x08 |
| 1878 | ('F' << 16) | |
| 1879 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1880 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1881 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1882 | }; |
| 1883 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1884 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1885 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1886 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1887 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1888 | |
| 1889 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1890 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1891 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 1892 | // If the argument contains a space, enclose it in quotes. |
| 1893 | if (Lib.find(" ") != StringRef::npos) |
| 1894 | Opt += "\"" + Lib.str() + "\""; |
| 1895 | else |
| 1896 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1897 | } |
| 1898 | }; |
| 1899 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1900 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1901 | // If the argument does not end in .lib, automatically add the suffix. |
| 1902 | // If the argument contains a space, enclose it in quotes. |
| 1903 | // This matches the behavior of MSVC. |
| 1904 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1905 | std::string ArgStr = Quote ? "\"" : ""; |
| 1906 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1907 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1908 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1909 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1910 | return ArgStr; |
| 1911 | } |
| 1912 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1913 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1914 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1915 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1916 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 1917 | unsigned NumRegisterParameters) |
| 1918 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1919 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1920 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1921 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1922 | CodeGen::CodeGenModule &CGM) const override; |
| 1923 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1924 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1925 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1926 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1927 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1928 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1929 | |
| 1930 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1931 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1932 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1933 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1934 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1935 | }; |
| 1936 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1937 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1938 | llvm::GlobalValue *GV, |
| 1939 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1940 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1941 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1942 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1943 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1944 | Fn->addFnAttr("stack-probe-size", |
| 1945 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1950 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1951 | llvm::GlobalValue *GV, |
| 1952 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1953 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1954 | |
| 1955 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1956 | } |
| 1957 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1958 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1959 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1960 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1961 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1962 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1963 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1964 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1965 | CodeGen::CodeGenModule &CGM) const override; |
| 1966 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1967 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1968 | return 7; |
| 1969 | } |
| 1970 | |
| 1971 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1972 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1973 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1975 | // 0-15 are the 16 integer registers. |
| 1976 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1977 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1978 | return false; |
| 1979 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1980 | |
| 1981 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1982 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1983 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1984 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1985 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1986 | |
| 1987 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1988 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1989 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1990 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1991 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1992 | }; |
| 1993 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1994 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1995 | llvm::GlobalValue *GV, |
| 1996 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1997 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1998 | |
| 1999 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 2000 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2001 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2002 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2003 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2004 | Class &Hi) const { |
| 2005 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2006 | // |
| 2007 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2008 | // memory. |
| 2009 | // |
| 2010 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2011 | // memory. |
| 2012 | // |
| 2013 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2014 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2015 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2016 | // ABI working for processors that don't support the __m256 type. |
| 2017 | // |
| 2018 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2019 | // |
| 2020 | // Some of these are enforced by the merging logic. Others can arise |
| 2021 | // only with unions; for example: |
| 2022 | // union { _Complex double; unsigned; } |
| 2023 | // |
| 2024 | // Note that clauses (b) and (c) were added in 0.98. |
| 2025 | // |
| 2026 | if (Hi == Memory) |
| 2027 | Lo = Memory; |
| 2028 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2029 | Lo = Memory; |
| 2030 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2031 | Lo = Memory; |
| 2032 | if (Hi == SSEUp && Lo != SSE) |
| 2033 | Hi = SSE; |
| 2034 | } |
| 2035 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2036 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2037 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2038 | // classified recursively so that always two fields are |
| 2039 | // considered. The resulting class is calculated according to |
| 2040 | // the classes of the fields in the eightbyte: |
| 2041 | // |
| 2042 | // (a) If both classes are equal, this is the resulting class. |
| 2043 | // |
| 2044 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2045 | // the other class. |
| 2046 | // |
| 2047 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2048 | // class. |
| 2049 | // |
| 2050 | // (d) If one of the classes is INTEGER, the result is the |
| 2051 | // INTEGER. |
| 2052 | // |
| 2053 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2054 | // MEMORY is used as class. |
| 2055 | // |
| 2056 | // (f) Otherwise class SSE is used. |
| 2057 | |
| 2058 | // Accum should never be memory (we should have returned) or |
| 2059 | // ComplexX87 (because this cannot be passed in a structure). |
| 2060 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2061 | "Invalid accumulated classification during merge."); |
| 2062 | if (Accum == Field || Field == NoClass) |
| 2063 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2064 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2065 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2066 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2067 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2068 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2069 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2070 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2071 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2072 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2073 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2076 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2077 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2078 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2079 | // Class pairs with appropriate constructor methods for the various |
| 2080 | // situations. |
| 2081 | |
| 2082 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2083 | // shouldn't be passed in registers for example, so there is no chance they |
| 2084 | // can straddle an eightbyte. Verify & simplify. |
| 2085 | |
| 2086 | Lo = Hi = NoClass; |
| 2087 | |
| 2088 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2089 | Current = Memory; |
| 2090 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2091 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2092 | BuiltinType::Kind k = BT->getKind(); |
| 2093 | |
| 2094 | if (k == BuiltinType::Void) { |
| 2095 | Current = NoClass; |
| 2096 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2097 | Lo = Integer; |
| 2098 | Hi = Integer; |
| 2099 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2100 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2101 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2102 | Current = SSE; |
| 2103 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2104 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2105 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2106 | Lo = SSE; |
| 2107 | Hi = SSEUp; |
| 2108 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2109 | Lo = X87; |
| 2110 | Hi = X87Up; |
| 2111 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2112 | Current = SSE; |
| 2113 | } else |
| 2114 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2115 | } |
| 2116 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2117 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2118 | return; |
| 2119 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2120 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2121 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2122 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2123 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2124 | return; |
| 2125 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2126 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2127 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2128 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2129 | return; |
| 2130 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2131 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2132 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2133 | if (Ty->isMemberFunctionPointerType()) { |
| 2134 | if (Has64BitPointers) { |
| 2135 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2136 | // Lo and Hi now. |
| 2137 | Lo = Hi = Integer; |
| 2138 | } else { |
| 2139 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2140 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2141 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2142 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2143 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2144 | Lo = Hi = Integer; |
| 2145 | } else { |
| 2146 | Current = Integer; |
| 2147 | } |
| 2148 | } |
| 2149 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2150 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2151 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2152 | return; |
| 2153 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2154 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2155 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2156 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2157 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2158 | // gcc passes the following as integer: |
| 2159 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2160 | // 2 bytes - <2 x char>, <1 x short> |
| 2161 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2162 | Current = Integer; |
| 2163 | |
| 2164 | // If this type crosses an eightbyte boundary, it should be |
| 2165 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2166 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2167 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2168 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2169 | Hi = Lo; |
| 2170 | } else if (Size == 64) { |
| 2171 | // gcc passes <1 x double> in memory. :( |
| 2172 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 2173 | return; |
| 2174 | |
| 2175 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 2176 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 2177 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2178 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 2179 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2180 | Current = Integer; |
| 2181 | else |
| 2182 | Current = SSE; |
| 2183 | |
| 2184 | // If this type crosses an eightbyte boundary, it should be |
| 2185 | // split. |
| 2186 | if (OffsetBase && OffsetBase != 64) |
| 2187 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2188 | } else if (Size == 128 || |
| 2189 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2190 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2191 | // least significant one belongs to class SSE and all the others to class |
| 2192 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2193 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2194 | // This design isn't correct for 256-bits, but since there're no cases |
| 2195 | // where the upper parts would need to be inspected, avoid adding |
| 2196 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2197 | // |
| 2198 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2199 | // registers if they are "named", i.e. not part of the "..." of a |
| 2200 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2201 | // |
| 2202 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2203 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2204 | Lo = SSE; |
| 2205 | Hi = SSEUp; |
| 2206 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2207 | return; |
| 2208 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2209 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2210 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2211 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2212 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2213 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2214 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2215 | if (Size <= 64) |
| 2216 | Current = Integer; |
| 2217 | else if (Size <= 128) |
| 2218 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2219 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2220 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2221 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2222 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2223 | } else if (ET == getContext().LongDoubleTy) { |
| 2224 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2225 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2226 | Current = Memory; |
| 2227 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2228 | Current = ComplexX87; |
| 2229 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2230 | Lo = Hi = SSE; |
| 2231 | else |
| 2232 | llvm_unreachable("unexpected long double representation!"); |
| 2233 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2234 | |
| 2235 | // If this complex type crosses an eightbyte boundary then it |
| 2236 | // should be split. |
| 2237 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2238 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2239 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2240 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2241 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2242 | return; |
| 2243 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2244 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2245 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2246 | // Arrays are treated like structures. |
| 2247 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2248 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2249 | |
| 2250 | // 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] | 2251 | // than four eightbytes, ..., it has class MEMORY. |
| 2252 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2253 | return; |
| 2254 | |
| 2255 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2256 | // fields, it has class MEMORY. |
| 2257 | // |
| 2258 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2259 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2260 | return; |
| 2261 | |
| 2262 | // Otherwise implement simplified merge. We could be smarter about |
| 2263 | // this, but it isn't worth it and would be harder to verify. |
| 2264 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2265 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2266 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2267 | |
| 2268 | // The only case a 256-bit wide vector could be used is when the array |
| 2269 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2270 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2271 | if (Size > 128 && EltSize != 256) |
| 2272 | return; |
| 2273 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2274 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2275 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2276 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2277 | Lo = merge(Lo, FieldLo); |
| 2278 | Hi = merge(Hi, FieldHi); |
| 2279 | if (Lo == Memory || Hi == Memory) |
| 2280 | break; |
| 2281 | } |
| 2282 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2283 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2284 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2285 | return; |
| 2286 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2287 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2288 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2289 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2290 | |
| 2291 | // 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] | 2292 | // than four eightbytes, ..., it has class MEMORY. |
| 2293 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2294 | return; |
| 2295 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2296 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2297 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2298 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2299 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2300 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2301 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2302 | const RecordDecl *RD = RT->getDecl(); |
| 2303 | |
| 2304 | // Assume variable sized types are passed in memory. |
| 2305 | if (RD->hasFlexibleArrayMember()) |
| 2306 | return; |
| 2307 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2308 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2309 | |
| 2310 | // Reset Lo class, this will be recomputed. |
| 2311 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2312 | |
| 2313 | // If this is a C++ record, classify the bases first. |
| 2314 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2315 | for (const auto &I : CXXRD->bases()) { |
| 2316 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2317 | "Unexpected base class!"); |
| 2318 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2319 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2320 | |
| 2321 | // Classify this field. |
| 2322 | // |
| 2323 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2324 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2325 | // initialized to class NO_CLASS. |
| 2326 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2327 | uint64_t Offset = |
| 2328 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2329 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2330 | Lo = merge(Lo, FieldLo); |
| 2331 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2332 | if (Lo == Memory || Hi == Memory) { |
| 2333 | postMerge(Size, Lo, Hi); |
| 2334 | return; |
| 2335 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2340 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2341 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2342 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2343 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2344 | bool BitField = i->isBitField(); |
| 2345 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2346 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2347 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2348 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2349 | // The only case a 256-bit wide vector could be used is when the struct |
| 2350 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2351 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2352 | // |
| 2353 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2354 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2355 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2356 | return; |
| 2357 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2358 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2359 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2360 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2361 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2362 | return; |
| 2363 | } |
| 2364 | |
| 2365 | // Classify this field. |
| 2366 | // |
| 2367 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2368 | // exceeds a single eightbyte, each is classified |
| 2369 | // separately. Each eightbyte gets initialized to class |
| 2370 | // NO_CLASS. |
| 2371 | Class FieldLo, FieldHi; |
| 2372 | |
| 2373 | // Bit-fields require special handling, they do not force the |
| 2374 | // structure to be passed in memory even if unaligned, and |
| 2375 | // therefore they can straddle an eightbyte. |
| 2376 | if (BitField) { |
| 2377 | // Ignore padding bit-fields. |
| 2378 | if (i->isUnnamedBitfield()) |
| 2379 | continue; |
| 2380 | |
| 2381 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2382 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2383 | |
| 2384 | uint64_t EB_Lo = Offset / 64; |
| 2385 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2386 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2387 | if (EB_Lo) { |
| 2388 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2389 | FieldLo = NoClass; |
| 2390 | FieldHi = Integer; |
| 2391 | } else { |
| 2392 | FieldLo = Integer; |
| 2393 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2394 | } |
| 2395 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2396 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2397 | Lo = merge(Lo, FieldLo); |
| 2398 | Hi = merge(Hi, FieldHi); |
| 2399 | if (Lo == Memory || Hi == Memory) |
| 2400 | break; |
| 2401 | } |
| 2402 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2403 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2407 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2408 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2409 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2410 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2411 | // Treat an enum type as its underlying type. |
| 2412 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2413 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2414 | |
| 2415 | return (Ty->isPromotableIntegerType() ? |
| 2416 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2417 | } |
| 2418 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2419 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2422 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2423 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2424 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2425 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2426 | if (Size <= 64 || Size > LargestVector) |
| 2427 | return true; |
| 2428 | } |
| 2429 | |
| 2430 | return false; |
| 2431 | } |
| 2432 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2433 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2434 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2435 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2436 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2437 | // |
| 2438 | // This assumption is optimistic, as there could be free registers available |
| 2439 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2440 | // the argument in the free register. This does not seem to happen currently, |
| 2441 | // but this code would be much safer if we could mark the argument with |
| 2442 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2443 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2444 | // Treat an enum type as its underlying type. |
| 2445 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2446 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2447 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2448 | return (Ty->isPromotableIntegerType() ? |
| 2449 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2450 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2451 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2452 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2453 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2455 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2456 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2457 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2458 | |
| 2459 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2460 | // is important for good codegen. |
| 2461 | // |
| 2462 | // We do this by coercing the value into a scalar type which the backend can |
| 2463 | // handle naturally (i.e., without using byval). |
| 2464 | // |
| 2465 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2466 | // free integer registers. Doing this when there are free integer registers |
| 2467 | // would require more care, as we would have to ensure that the coerced value |
| 2468 | // did not claim the unused register. That would require either reording the |
| 2469 | // arguments to the function (so that any subsequent inreg values came first), |
| 2470 | // or only doing this optimization when there were no following arguments that |
| 2471 | // might be inreg. |
| 2472 | // |
| 2473 | // We currently expect it to be rare (particularly in well written code) for |
| 2474 | // arguments to be passed on the stack when there are still free integer |
| 2475 | // registers available (this would typically imply large structs being passed |
| 2476 | // by value), so this seems like a fair tradeoff for now. |
| 2477 | // |
| 2478 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2479 | // attributes. See PR12193. |
| 2480 | if (freeIntRegs == 0) { |
| 2481 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2482 | |
| 2483 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2484 | // type, which will end up on the stack (with alignment 8). |
| 2485 | if (Align == 8 && Size <= 64) |
| 2486 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2487 | Size)); |
| 2488 | } |
| 2489 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2490 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2493 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2494 | /// 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] | 2495 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2496 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2497 | // vectors; strip them off if present. |
| 2498 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2499 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2500 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2501 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2502 | if (isa<llvm::VectorType>(IRType) || |
| 2503 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2504 | return IRType; |
| 2505 | |
| 2506 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2507 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2508 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2509 | |
| 2510 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2511 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2512 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2515 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2516 | /// is known to either be off the end of the specified type or being in |
| 2517 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2518 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2519 | /// classification that put one of the two halves in the INTEGER class. |
| 2520 | /// |
| 2521 | /// It is conservatively correct to return false. |
| 2522 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2523 | unsigned EndBit, ASTContext &Context) { |
| 2524 | // If the bytes being queried are off the end of the type, there is no user |
| 2525 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2526 | // types that don't contain interesting padding. |
| 2527 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2528 | if (TySize <= StartBit) |
| 2529 | return true; |
| 2530 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2531 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2532 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2533 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2534 | |
| 2535 | // Check each element to see if the element overlaps with the queried range. |
| 2536 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2537 | // If the element is after the span we care about, then we're done.. |
| 2538 | unsigned EltOffset = i*EltSize; |
| 2539 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2540 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2541 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2542 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2543 | EndBit-EltOffset, Context)) |
| 2544 | return false; |
| 2545 | } |
| 2546 | // If it overlaps no elements, then it is safe to process as padding. |
| 2547 | return true; |
| 2548 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2549 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2550 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2551 | const RecordDecl *RD = RT->getDecl(); |
| 2552 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2553 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2554 | // If this is a C++ record, check the bases first. |
| 2555 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2556 | for (const auto &I : CXXRD->bases()) { |
| 2557 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2558 | "Unexpected base class!"); |
| 2559 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2560 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2562 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2563 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2564 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2565 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2566 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2567 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2568 | EndBit-BaseOffset, Context)) |
| 2569 | return false; |
| 2570 | } |
| 2571 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2572 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2573 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2574 | // this could be sped up a lot by being smarter about queried fields, |
| 2575 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2576 | // much. |
| 2577 | unsigned idx = 0; |
| 2578 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2579 | i != e; ++i, ++idx) { |
| 2580 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2582 | // If we found a field after the region we care about, then we're done. |
| 2583 | if (FieldOffset >= EndBit) break; |
| 2584 | |
| 2585 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2586 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2587 | Context)) |
| 2588 | return false; |
| 2589 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2590 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2591 | // If nothing in this record overlapped the area of interest, then we're |
| 2592 | // clean. |
| 2593 | return true; |
| 2594 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2596 | return false; |
| 2597 | } |
| 2598 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2599 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2600 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2601 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2602 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2603 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2604 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2605 | // Base case if we find a float. |
| 2606 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2607 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2608 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2609 | // 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] | 2610 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2611 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2612 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2613 | IROffset -= SL->getElementOffset(Elt); |
| 2614 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2615 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2616 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2617 | // 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] | 2618 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2619 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2620 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2621 | IROffset -= IROffset/EltSize*EltSize; |
| 2622 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2623 | } |
| 2624 | |
| 2625 | return false; |
| 2626 | } |
| 2627 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2628 | |
| 2629 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2630 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2631 | llvm::Type *X86_64ABIInfo:: |
| 2632 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2633 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2634 | // 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] | 2635 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2636 | // structs that contain 3 floats. |
| 2637 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2638 | SourceOffset*8+64, getContext())) |
| 2639 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2640 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2641 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2642 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2643 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2644 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2645 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2646 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2648 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2649 | } |
| 2650 | |
| 2651 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2652 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2653 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2654 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2655 | /// 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] | 2656 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2657 | /// etc). |
| 2658 | /// |
| 2659 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2660 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2661 | /// the 8-byte value references. PrefType may be null. |
| 2662 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2663 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2664 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2665 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2666 | llvm::Type *X86_64ABIInfo:: |
| 2667 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2668 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2669 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2670 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2671 | if (IROffset == 0) { |
| 2672 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2673 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2674 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2675 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2676 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2677 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2678 | // goodness in the source type is just tail padding. This is allowed to |
| 2679 | // kick in for struct {double,int} on the int, but not on |
| 2680 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2681 | // have to do this analysis on the source type because we can't depend on |
| 2682 | // unions being lowered a specific way etc. |
| 2683 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2684 | IRType->isIntegerTy(32) || |
| 2685 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2686 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2687 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2688 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2689 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2690 | SourceOffset*8+64, getContext())) |
| 2691 | return IRType; |
| 2692 | } |
| 2693 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2694 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2695 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2696 | // 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] | 2697 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2698 | if (IROffset < SL->getSizeInBytes()) { |
| 2699 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2700 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2702 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2703 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2704 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2705 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2706 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2707 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2708 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2709 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2710 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2711 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2712 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2713 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2715 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2716 | // 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] | 2717 | unsigned TySizeInBytes = |
| 2718 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2719 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2720 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2721 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2722 | // It is always safe to classify this as an integer type up to i64 that |
| 2723 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2724 | return llvm::IntegerType::get(getVMContext(), |
| 2725 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2728 | |
| 2729 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2730 | /// be used as elements of a two register pair to pass or return, return a |
| 2731 | /// first class aggregate to represent them. For example, if the low part of |
| 2732 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2733 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2734 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2735 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2736 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2737 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2738 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2739 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2740 | // the second element at offset 8. Check for this: |
| 2741 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2742 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2743 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2744 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2745 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2746 | // To handle this, we have to increase the size of the low part so that the |
| 2747 | // second element will start at an 8 byte offset. We can't increase the size |
| 2748 | // of the second element because it might make us access off the end of the |
| 2749 | // struct. |
| 2750 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 2751 | // There are usually two sorts of types the ABI generation code can produce |
| 2752 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 2753 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 2754 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2755 | // Promote these to a larger type. |
| 2756 | if (Lo->isFloatTy()) |
| 2757 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2758 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 2759 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 2760 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2761 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2762 | } |
| 2763 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2764 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2765 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2766 | |
| 2767 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2768 | // Verify that the second element is at an 8-byte offset. |
| 2769 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2770 | "Invalid x86-64 argument pair!"); |
| 2771 | return Result; |
| 2772 | } |
| 2773 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2774 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2775 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2776 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2777 | // classification algorithm. |
| 2778 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2779 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2780 | |
| 2781 | // Check some invariants. |
| 2782 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2783 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2784 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2785 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2786 | switch (Lo) { |
| 2787 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2788 | if (Hi == NoClass) |
| 2789 | return ABIArgInfo::getIgnore(); |
| 2790 | // If the low part is just padding, it takes no register, leave ResType |
| 2791 | // null. |
| 2792 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2793 | "Unknown missing lo part"); |
| 2794 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2795 | |
| 2796 | case SSEUp: |
| 2797 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2798 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2799 | |
| 2800 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2801 | // hidden argument. |
| 2802 | case Memory: |
| 2803 | return getIndirectReturnResult(RetTy); |
| 2804 | |
| 2805 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2806 | // available register of the sequence %rax, %rdx is used. |
| 2807 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2808 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2809 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2810 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2811 | // so that the parameter gets the right LLVM IR attributes. |
| 2812 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2813 | // Treat an enum type as its underlying type. |
| 2814 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2815 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2816 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2817 | if (RetTy->isIntegralOrEnumerationType() && |
| 2818 | RetTy->isPromotableIntegerType()) |
| 2819 | return ABIArgInfo::getExtend(); |
| 2820 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2821 | break; |
| 2822 | |
| 2823 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2824 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2825 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2826 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2827 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2828 | |
| 2829 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2830 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2831 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2832 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2833 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2834 | |
| 2835 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2836 | // part of the value is returned in %st0 and the imaginary part in |
| 2837 | // %st1. |
| 2838 | case ComplexX87: |
| 2839 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2840 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2841 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2842 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2843 | break; |
| 2844 | } |
| 2845 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2846 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2847 | switch (Hi) { |
| 2848 | // Memory was handled previously and X87 should |
| 2849 | // never occur as a hi class. |
| 2850 | case Memory: |
| 2851 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2852 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2853 | |
| 2854 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2855 | case NoClass: |
| 2856 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2857 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2858 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2859 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2860 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2861 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2862 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2863 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2864 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2865 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2866 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2867 | break; |
| 2868 | |
| 2869 | // 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] | 2870 | // is passed in the next available eightbyte chunk if the last used |
| 2871 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2872 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2873 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2874 | case SSEUp: |
| 2875 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2876 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2877 | break; |
| 2878 | |
| 2879 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2880 | // returned together with the previous X87 value in %st0. |
| 2881 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2882 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2883 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2884 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2885 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2886 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2887 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2888 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2889 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2890 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2891 | break; |
| 2892 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2893 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2894 | // 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] | 2895 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2896 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2897 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2898 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2899 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2900 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2903 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2904 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2905 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2906 | const |
| 2907 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2908 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2909 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2910 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2911 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2912 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2913 | // Check some invariants. |
| 2914 | // FIXME: Enforce these by construction. |
| 2915 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2916 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2917 | |
| 2918 | neededInt = 0; |
| 2919 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2920 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2921 | switch (Lo) { |
| 2922 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2923 | if (Hi == NoClass) |
| 2924 | return ABIArgInfo::getIgnore(); |
| 2925 | // If the low part is just padding, it takes no register, leave ResType |
| 2926 | // null. |
| 2927 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2928 | "Unknown missing lo part"); |
| 2929 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2930 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2931 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2932 | // on the stack. |
| 2933 | case Memory: |
| 2934 | |
| 2935 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2936 | // COMPLEX_X87, it is passed in memory. |
| 2937 | case X87: |
| 2938 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2939 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2940 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2941 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2942 | |
| 2943 | case SSEUp: |
| 2944 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2945 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2946 | |
| 2947 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2948 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2949 | // and %r9 is used. |
| 2950 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2951 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2952 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2953 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2954 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2955 | |
| 2956 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2957 | // so that the parameter gets the right LLVM IR attributes. |
| 2958 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2959 | // Treat an enum type as its underlying type. |
| 2960 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2961 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2962 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2963 | if (Ty->isIntegralOrEnumerationType() && |
| 2964 | Ty->isPromotableIntegerType()) |
| 2965 | return ABIArgInfo::getExtend(); |
| 2966 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2967 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2968 | break; |
| 2969 | |
| 2970 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2971 | // available SSE register is used, the registers are taken in the |
| 2972 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2973 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2974 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2975 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2976 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2977 | break; |
| 2978 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2979 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2980 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2981 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2982 | switch (Hi) { |
| 2983 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2984 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2985 | // which is passed in memory. |
| 2986 | case Memory: |
| 2987 | case X87: |
| 2988 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2989 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2990 | |
| 2991 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2992 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2993 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2994 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2995 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2996 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2997 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2998 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2999 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3000 | break; |
| 3001 | |
| 3002 | // X87Up generally doesn't occur here (long double is passed in |
| 3003 | // memory), except in situations involving unions. |
| 3004 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3005 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3006 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3007 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3008 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3009 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3010 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3011 | ++neededSSE; |
| 3012 | break; |
| 3013 | |
| 3014 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3015 | // 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] | 3016 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3017 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3018 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3019 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3020 | break; |
| 3021 | } |
| 3022 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3023 | // If a high part was specified, merge it together with the low part. It is |
| 3024 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3025 | // first class struct aggregate with the high and low part: {low, high} |
| 3026 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3027 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3028 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3029 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3032 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3033 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3034 | if (!getCXXABI().classifyReturnType(FI)) |
| 3035 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3036 | |
| 3037 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3038 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3039 | |
| 3040 | // If the return value is indirect, then the hidden argument is consuming one |
| 3041 | // integer register. |
| 3042 | if (FI.getReturnInfo().isIndirect()) |
| 3043 | --freeIntRegs; |
| 3044 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3045 | // The chain argument effectively gives us another free register. |
| 3046 | if (FI.isChainCall()) |
| 3047 | ++freeIntRegs; |
| 3048 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3049 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3050 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3051 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3052 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3053 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3054 | it != ie; ++it, ++ArgNo) { |
| 3055 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3056 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3057 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3058 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3059 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3060 | |
| 3061 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3062 | // eightbyte of an argument, the whole argument is passed on the |
| 3063 | // stack. If registers have already been assigned for some |
| 3064 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3065 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3066 | freeIntRegs -= neededInt; |
| 3067 | freeSSERegs -= neededSSE; |
| 3068 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3069 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3070 | } |
| 3071 | } |
| 3072 | } |
| 3073 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3074 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3075 | Address VAListAddr, QualType Ty) { |
| 3076 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3077 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3078 | llvm::Value *overflow_arg_area = |
| 3079 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3080 | |
| 3081 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3082 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3083 | // It isn't stated explicitly in the standard, but in practice we use |
| 3084 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3085 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3086 | if (Align > CharUnits::fromQuantity(8)) { |
| 3087 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3088 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | // 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] | 3092 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3093 | llvm::Value *Res = |
| 3094 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3095 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3096 | |
| 3097 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3098 | // l->overflow_arg_area + sizeof(type). |
| 3099 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3100 | // an 8 byte boundary. |
| 3101 | |
| 3102 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3103 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3104 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3105 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3106 | "overflow_arg_area.next"); |
| 3107 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3108 | |
| 3109 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3110 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3113 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3114 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3115 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3116 | // struct { |
| 3117 | // i32 gp_offset; |
| 3118 | // i32 fp_offset; |
| 3119 | // i8* overflow_arg_area; |
| 3120 | // i8* reg_save_area; |
| 3121 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3122 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3123 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3124 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3125 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3126 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3127 | |
| 3128 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3129 | // in the registers. If not go to step 7. |
| 3130 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3131 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3132 | |
| 3133 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3134 | // general purpose registers needed to pass type and num_fp to hold |
| 3135 | // the number of floating point registers needed. |
| 3136 | |
| 3137 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3138 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3139 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3140 | // |
| 3141 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3142 | // register save space). |
| 3143 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3144 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3145 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3146 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3147 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3148 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3149 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3150 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3151 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3152 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3153 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3157 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3158 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3159 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3160 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3161 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3162 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3163 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3164 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3165 | } |
| 3166 | |
| 3167 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3168 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3169 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3170 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3171 | |
| 3172 | // Emit code to load the value if it was passed in registers. |
| 3173 | |
| 3174 | CGF.EmitBlock(InRegBlock); |
| 3175 | |
| 3176 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3177 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3178 | // copying to a temporary location in case the parameter is passed |
| 3179 | // in different register classes or requires an alignment greater |
| 3180 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3181 | // |
| 3182 | // FIXME: This really results in shameful code when we end up needing to |
| 3183 | // collect arguments from different places; often what should result in a |
| 3184 | // simple assembling of a structure from scattered addresses has many more |
| 3185 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3186 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3187 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3188 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3189 | "reg_save_area"); |
| 3190 | |
| 3191 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3192 | if (neededInt && neededSSE) { |
| 3193 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3194 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3195 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3196 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3197 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3198 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3199 | llvm::Type *TyLo = ST->getElementType(0); |
| 3200 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3201 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3202 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3203 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3204 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3205 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3206 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3207 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3208 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3209 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3210 | // Copy the first element. |
| 3211 | llvm::Value *V = |
| 3212 | CGF.Builder.CreateDefaultAlignedLoad( |
| 3213 | CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 3214 | CGF.Builder.CreateStore(V, |
| 3215 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3216 | |
| 3217 | // Copy the second element. |
| 3218 | V = CGF.Builder.CreateDefaultAlignedLoad( |
| 3219 | CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 3220 | CharUnits Offset = CharUnits::fromQuantity( |
| 3221 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3222 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3223 | |
| 3224 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3225 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3226 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3227 | CharUnits::fromQuantity(8)); |
| 3228 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3229 | |
| 3230 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3231 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3232 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3233 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3234 | CharUnits TyAlign = SizeAlign.second; |
| 3235 | |
| 3236 | // Copy into a temporary if the type is more aligned than the |
| 3237 | // register save area. |
| 3238 | if (TyAlign.getQuantity() > 8) { |
| 3239 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3240 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3241 | RegAddr = Tmp; |
| 3242 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3243 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3244 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3245 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3246 | CharUnits::fromQuantity(16)); |
| 3247 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3248 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3249 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3250 | // SSE registers are spaced 16 bytes apart in the register save |
| 3251 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3252 | // The ABI isn't explicit about this, but it seems reasonable |
| 3253 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3254 | // naturally 16-byte aligned and the prologue is expected to store |
| 3255 | // all the SSE registers to the RSA. |
| 3256 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3257 | CharUnits::fromQuantity(16)); |
| 3258 | Address RegAddrHi = |
| 3259 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3260 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3261 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3262 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3263 | llvm::Value *V; |
| 3264 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3265 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3266 | V = CGF.Builder.CreateLoad( |
| 3267 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3268 | CGF.Builder.CreateStore(V, |
| 3269 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3270 | V = CGF.Builder.CreateLoad( |
| 3271 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3272 | CGF.Builder.CreateStore(V, |
| 3273 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3274 | |
| 3275 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3279 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3280 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3281 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3282 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3283 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3284 | gp_offset_p); |
| 3285 | } |
| 3286 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3287 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3288 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3289 | fp_offset_p); |
| 3290 | } |
| 3291 | CGF.EmitBranch(ContBlock); |
| 3292 | |
| 3293 | // Emit code to load the value if it was passed in memory. |
| 3294 | |
| 3295 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3296 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Return the appropriate result. |
| 3299 | |
| 3300 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3301 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3302 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3303 | return ResAddr; |
| 3304 | } |
| 3305 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3306 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3307 | QualType Ty) const { |
| 3308 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3309 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3310 | CharUnits::fromQuantity(8), |
| 3311 | /*allowHigherAlign*/ false); |
| 3312 | } |
| 3313 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3314 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3315 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3316 | |
| 3317 | if (Ty->isVoidType()) |
| 3318 | return ABIArgInfo::getIgnore(); |
| 3319 | |
| 3320 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3321 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3322 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3323 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3324 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3325 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3326 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3327 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3328 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3329 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3330 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3331 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3335 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3336 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3337 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3338 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3339 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3340 | // other targets. |
| 3341 | const Type *Base = nullptr; |
| 3342 | uint64_t NumElts = 0; |
| 3343 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3344 | if (FreeSSERegs >= NumElts) { |
| 3345 | FreeSSERegs -= NumElts; |
| 3346 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3347 | return ABIArgInfo::getDirect(); |
| 3348 | return ABIArgInfo::getExpand(); |
| 3349 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3350 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3354 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3355 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3356 | // directly. |
| 3357 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3358 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3359 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3362 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3363 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3364 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3365 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3366 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3367 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3368 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3369 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3372 | // Bool type is always extended to the ABI, other builtin types are not |
| 3373 | // extended. |
| 3374 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3375 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3376 | return ABIArgInfo::getExtend(); |
| 3377 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3378 | // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It |
| 3379 | // passes them indirectly through memory. |
| 3380 | if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { |
| 3381 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 3382 | if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 3383 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3384 | } |
| 3385 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3386 | return ABIArgInfo::getDirect(); |
| 3387 | } |
| 3388 | |
| 3389 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3390 | bool IsVectorCall = |
| 3391 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3392 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3393 | // We can use up to 4 SSE return registers with vectorcall. |
| 3394 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3395 | if (!getCXXABI().classifyReturnType(FI)) |
| 3396 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3397 | |
| 3398 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3399 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3400 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3401 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3404 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3405 | QualType Ty) const { |
| 3406 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3407 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3408 | CharUnits::fromQuantity(8), |
| 3409 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3410 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3411 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3412 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3413 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3414 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3415 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3416 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3417 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3418 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3419 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3420 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3421 | }; |
| 3422 | |
| 3423 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3424 | public: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3425 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3426 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3427 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3428 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3429 | // This is recovered from gcc output. |
| 3430 | return 1; // r1 is the dedicated stack pointer |
| 3431 | } |
| 3432 | |
| 3433 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3434 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3435 | }; |
| 3436 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3437 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3438 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3439 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3440 | QualType Ty) const { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3441 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3442 | // TODO: Implement this. For now ignore. |
| 3443 | (void)CTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3444 | return Address::invalid(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3447 | // struct __va_list_tag { |
| 3448 | // unsigned char gpr; |
| 3449 | // unsigned char fpr; |
| 3450 | // unsigned short reserved; |
| 3451 | // void *overflow_arg_area; |
| 3452 | // void *reg_save_area; |
| 3453 | // }; |
| 3454 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3455 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3456 | bool isInt = |
| 3457 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3458 | |
| 3459 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3460 | // with the argument-lowering code. |
| 3461 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3462 | |
| 3463 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3464 | |
| 3465 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3466 | Address NumRegsAddr = Address::invalid(); |
| 3467 | if (isInt) { |
| 3468 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3469 | } else { |
| 3470 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3471 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3472 | |
| 3473 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3474 | |
| 3475 | // "Align" the register count when TY is i64. |
| 3476 | if (isI64) { |
| 3477 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3478 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3479 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3480 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3481 | llvm::Value *CC = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3482 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(8), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3483 | |
| 3484 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3485 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3486 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3487 | |
| 3488 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3489 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3490 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3491 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3492 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3493 | // Case 1: consume registers. |
| 3494 | Address RegAddr = Address::invalid(); |
| 3495 | { |
| 3496 | CGF.EmitBlock(UsingRegs); |
| 3497 | |
| 3498 | Address RegSaveAreaPtr = |
| 3499 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3500 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3501 | CharUnits::fromQuantity(8)); |
| 3502 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3503 | |
| 3504 | // Floating-point registers start after the general-purpose registers. |
| 3505 | if (!isInt) { |
| 3506 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3507 | CharUnits::fromQuantity(32)); |
| 3508 | } |
| 3509 | |
| 3510 | // Get the address of the saved value by scaling the number of |
| 3511 | // registers we've used by the number of |
| 3512 | CharUnits RegSize = CharUnits::fromQuantity(isInt ? 4 : 8); |
| 3513 | llvm::Value *RegOffset = |
| 3514 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3515 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3516 | RegAddr.getPointer(), RegOffset), |
| 3517 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3518 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3519 | |
| 3520 | // Increase the used-register count. |
| 3521 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(isI64 ? 2 : 1)); |
| 3522 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3523 | |
| 3524 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3525 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3526 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3527 | // Case 2: consume space in the overflow area. |
| 3528 | Address MemAddr = Address::invalid(); |
| 3529 | { |
| 3530 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3531 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3532 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3533 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3534 | |
| 3535 | CharUnits Size; |
| 3536 | if (!isIndirect) { |
| 3537 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 3538 | Size = TypeInfo.first.RoundUpToAlignment(OverflowAreaAlign); |
| 3539 | } else { |
| 3540 | Size = CGF.getPointerSize(); |
| 3541 | } |
| 3542 | |
| 3543 | Address OverflowAreaAddr = |
| 3544 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3545 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3546 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3547 | // Round up address of argument to alignment |
| 3548 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3549 | if (Align > OverflowAreaAlign) { |
| 3550 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 3551 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 3552 | Align); |
| 3553 | } |
| 3554 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3555 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3556 | |
| 3557 | // Increase the overflow area. |
| 3558 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3559 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3560 | CGF.EmitBranch(Cont); |
| 3561 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3562 | |
| 3563 | CGF.EmitBlock(Cont); |
| 3564 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3565 | // Merge the cases with a phi. |
| 3566 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3567 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3568 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3569 | // Load the pointer if the argument was passed indirectly. |
| 3570 | if (isIndirect) { |
| 3571 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3572 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | return Result; |
| 3576 | } |
| 3577 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3578 | bool |
| 3579 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3580 | llvm::Value *Address) const { |
| 3581 | // This is calculated from the LLVM and GCC tables and verified |
| 3582 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3583 | |
| 3584 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3585 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3586 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3587 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3588 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3589 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3590 | |
| 3591 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3592 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3593 | |
| 3594 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3595 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3596 | |
| 3597 | // 64-76 are various 4-byte special-purpose registers: |
| 3598 | // 64: mq |
| 3599 | // 65: lr |
| 3600 | // 66: ctr |
| 3601 | // 67: ap |
| 3602 | // 68-75 cr0-7 |
| 3603 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3604 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3605 | |
| 3606 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3607 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3608 | |
| 3609 | // 109: vrsave |
| 3610 | // 110: vscr |
| 3611 | // 111: spe_acc |
| 3612 | // 112: spefscr |
| 3613 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3614 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3615 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3616 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3619 | // PowerPC-64 |
| 3620 | |
| 3621 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3622 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3623 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3624 | public: |
| 3625 | enum ABIKind { |
| 3626 | ELFv1 = 0, |
| 3627 | ELFv2 |
| 3628 | }; |
| 3629 | |
| 3630 | private: |
| 3631 | static const unsigned GPRBits = 64; |
| 3632 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3633 | bool HasQPX; |
| 3634 | |
| 3635 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3636 | // will be passed in a QPX register. |
| 3637 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3638 | if (!HasQPX) |
| 3639 | return false; |
| 3640 | |
| 3641 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3642 | unsigned NumElements = VT->getNumElements(); |
| 3643 | if (NumElements == 1) |
| 3644 | return false; |
| 3645 | |
| 3646 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3647 | if (getContext().getTypeSize(Ty) <= 256) |
| 3648 | return true; |
| 3649 | } else if (VT->getElementType()-> |
| 3650 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3651 | if (getContext().getTypeSize(Ty) <= 128) |
| 3652 | return true; |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | return false; |
| 3657 | } |
| 3658 | |
| 3659 | bool IsQPXVectorTy(QualType Ty) const { |
| 3660 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3661 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3662 | |
| 3663 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3664 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3665 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3666 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3667 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3668 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3669 | |
| 3670 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3671 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3672 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3673 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3674 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3675 | uint64_t Members) const override; |
| 3676 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3677 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3678 | // Example: For aggregate arguments that fit in a register, we could |
| 3679 | // use getDirectInReg (as is done below for structs containing a single |
| 3680 | // floating-point value) to avoid pushing them to memory on function |
| 3681 | // entry. This would require changing the logic in PPCISelLowering |
| 3682 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3683 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3684 | if (!getCXXABI().classifyReturnType(FI)) |
| 3685 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3686 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3687 | // We rely on the default argument classification for the most part. |
| 3688 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3689 | // 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] | 3690 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3691 | if (T) { |
| 3692 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3693 | if (IsQPXVectorTy(T) || |
| 3694 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3695 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3696 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3697 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3698 | continue; |
| 3699 | } |
| 3700 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3701 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3702 | } |
| 3703 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3704 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3705 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3706 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3707 | }; |
| 3708 | |
| 3709 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3710 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3711 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3712 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3713 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3714 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3715 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3716 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3717 | // This is recovered from gcc output. |
| 3718 | return 1; // r1 is the dedicated stack pointer |
| 3719 | } |
| 3720 | |
| 3721 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3722 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3723 | }; |
| 3724 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3725 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3726 | public: |
| 3727 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3728 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3729 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3730 | // This is recovered from gcc output. |
| 3731 | return 1; // r1 is the dedicated stack pointer |
| 3732 | } |
| 3733 | |
| 3734 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3735 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3736 | }; |
| 3737 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3738 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3739 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3740 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3741 | // extended to 64 bits. |
| 3742 | bool |
| 3743 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3744 | // Treat an enum type as its underlying type. |
| 3745 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3746 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3747 | |
| 3748 | // Promotable integer types are required to be promoted by the ABI. |
| 3749 | if (Ty->isPromotableIntegerType()) |
| 3750 | return true; |
| 3751 | |
| 3752 | // In addition to the usual promotable integer types, we also need to |
| 3753 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3754 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3755 | switch (BT->getKind()) { |
| 3756 | case BuiltinType::Int: |
| 3757 | case BuiltinType::UInt: |
| 3758 | return true; |
| 3759 | default: |
| 3760 | break; |
| 3761 | } |
| 3762 | |
| 3763 | return false; |
| 3764 | } |
| 3765 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3766 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 3767 | /// higher alignment in the parameter area. Always returns at least 8. |
| 3768 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3769 | // Complex types are passed just like their elements. |
| 3770 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3771 | Ty = CTy->getElementType(); |
| 3772 | |
| 3773 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3774 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3775 | if (IsQPXVectorTy(Ty)) { |
| 3776 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3777 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3778 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3779 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3780 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3781 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3782 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3783 | |
| 3784 | // For single-element float/vector structs, we consider the whole type |
| 3785 | // to have the same alignment requirements as its single element. |
| 3786 | const Type *AlignAsType = nullptr; |
| 3787 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3788 | if (EltType) { |
| 3789 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3790 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3791 | getContext().getTypeSize(EltType) == 128) || |
| 3792 | (BT && BT->isFloatingPoint())) |
| 3793 | AlignAsType = EltType; |
| 3794 | } |
| 3795 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3796 | // Likewise for ELFv2 homogeneous aggregates. |
| 3797 | const Type *Base = nullptr; |
| 3798 | uint64_t Members = 0; |
| 3799 | if (!AlignAsType && Kind == ELFv2 && |
| 3800 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3801 | AlignAsType = Base; |
| 3802 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3803 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3804 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3805 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3806 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3807 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3808 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3809 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3810 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3811 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3812 | |
| 3813 | // Otherwise, we only need alignment for any aggregate type that |
| 3814 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3815 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3816 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3817 | return CharUnits::fromQuantity(32); |
| 3818 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3819 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3820 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3821 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3824 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3825 | /// aggregate. Base is set to the base element type, and Members is set |
| 3826 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3827 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3828 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3829 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3830 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3831 | if (NElements == 0) |
| 3832 | return false; |
| 3833 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3834 | return false; |
| 3835 | Members *= NElements; |
| 3836 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3837 | const RecordDecl *RD = RT->getDecl(); |
| 3838 | if (RD->hasFlexibleArrayMember()) |
| 3839 | return false; |
| 3840 | |
| 3841 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3842 | |
| 3843 | // If this is a C++ record, check the bases first. |
| 3844 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3845 | for (const auto &I : CXXRD->bases()) { |
| 3846 | // Ignore empty records. |
| 3847 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3848 | continue; |
| 3849 | |
| 3850 | uint64_t FldMembers; |
| 3851 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3852 | return false; |
| 3853 | |
| 3854 | Members += FldMembers; |
| 3855 | } |
| 3856 | } |
| 3857 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3858 | for (const auto *FD : RD->fields()) { |
| 3859 | // Ignore (non-zero arrays of) empty records. |
| 3860 | QualType FT = FD->getType(); |
| 3861 | while (const ConstantArrayType *AT = |
| 3862 | getContext().getAsConstantArrayType(FT)) { |
| 3863 | if (AT->getSize().getZExtValue() == 0) |
| 3864 | return false; |
| 3865 | FT = AT->getElementType(); |
| 3866 | } |
| 3867 | if (isEmptyRecord(getContext(), FT, true)) |
| 3868 | continue; |
| 3869 | |
| 3870 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3871 | if (getContext().getLangOpts().CPlusPlus && |
| 3872 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3873 | continue; |
| 3874 | |
| 3875 | uint64_t FldMembers; |
| 3876 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3877 | return false; |
| 3878 | |
| 3879 | Members = (RD->isUnion() ? |
| 3880 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3881 | } |
| 3882 | |
| 3883 | if (!Base) |
| 3884 | return false; |
| 3885 | |
| 3886 | // Ensure there is no padding. |
| 3887 | if (getContext().getTypeSize(Base) * Members != |
| 3888 | getContext().getTypeSize(Ty)) |
| 3889 | return false; |
| 3890 | } else { |
| 3891 | Members = 1; |
| 3892 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3893 | Members = 2; |
| 3894 | Ty = CT->getElementType(); |
| 3895 | } |
| 3896 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3897 | // Most ABIs only support float, double, and some vector type widths. |
| 3898 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3899 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3900 | |
| 3901 | // The base type must be the same for all members. Types that |
| 3902 | // agree in both total size and mode (float vs. vector) are |
| 3903 | // treated as being equivalent here. |
| 3904 | const Type *TyPtr = Ty.getTypePtr(); |
| 3905 | if (!Base) |
| 3906 | Base = TyPtr; |
| 3907 | |
| 3908 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3909 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3910 | return false; |
| 3911 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3912 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3913 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3914 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3915 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3916 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3917 | // double, long double, or 128-bit vectors. |
| 3918 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3919 | if (BT->getKind() == BuiltinType::Float || |
| 3920 | BT->getKind() == BuiltinType::Double || |
| 3921 | BT->getKind() == BuiltinType::LongDouble) |
| 3922 | return true; |
| 3923 | } |
| 3924 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3925 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3926 | return true; |
| 3927 | } |
| 3928 | return false; |
| 3929 | } |
| 3930 | |
| 3931 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3932 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3933 | // Vector types require one register, floating point types require one |
| 3934 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3935 | uint32_t NumRegs = |
| 3936 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3937 | |
| 3938 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3939 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3942 | ABIArgInfo |
| 3943 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3944 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3945 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3946 | if (Ty->isAnyComplexType()) |
| 3947 | return ABIArgInfo::getDirect(); |
| 3948 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3949 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3950 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3951 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3952 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3953 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3954 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3955 | else if (Size < 128) { |
| 3956 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3957 | return ABIArgInfo::getDirect(CoerceTy); |
| 3958 | } |
| 3959 | } |
| 3960 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3961 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3962 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3963 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3964 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3965 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 3966 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3967 | |
| 3968 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3969 | const Type *Base = nullptr; |
| 3970 | uint64_t Members = 0; |
| 3971 | if (Kind == ELFv2 && |
| 3972 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3973 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3974 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3975 | return ABIArgInfo::getDirect(CoerceTy); |
| 3976 | } |
| 3977 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3978 | // If an aggregate may end up fully in registers, we do not |
| 3979 | // use the ByVal method, but pass the aggregate as array. |
| 3980 | // This is usually beneficial since we avoid forcing the |
| 3981 | // back-end to store the argument to memory. |
| 3982 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3983 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3984 | llvm::Type *CoerceTy; |
| 3985 | |
| 3986 | // Types up to 8 bytes are passed as integer type (which will be |
| 3987 | // properly aligned in the argument save area doubleword). |
| 3988 | if (Bits <= GPRBits) |
| 3989 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3990 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3991 | // Larger types are passed as arrays, with the base type selected |
| 3992 | // according to the required alignment in the save area. |
| 3993 | else { |
| 3994 | uint64_t RegBits = ABIAlign * 8; |
| 3995 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3996 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3997 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3998 | } |
| 3999 | |
| 4000 | return ABIArgInfo::getDirect(CoerceTy); |
| 4001 | } |
| 4002 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4003 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4004 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4005 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4006 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | return (isPromotableTypeForABI(Ty) ? |
| 4010 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4011 | } |
| 4012 | |
| 4013 | ABIArgInfo |
| 4014 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4015 | if (RetTy->isVoidType()) |
| 4016 | return ABIArgInfo::getIgnore(); |
| 4017 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4018 | if (RetTy->isAnyComplexType()) |
| 4019 | return ABIArgInfo::getDirect(); |
| 4020 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4021 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4022 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4023 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4024 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4025 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4026 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4027 | else if (Size < 128) { |
| 4028 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4029 | return ABIArgInfo::getDirect(CoerceTy); |
| 4030 | } |
| 4031 | } |
| 4032 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4033 | if (isAggregateTypeForABI(RetTy)) { |
| 4034 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4035 | const Type *Base = nullptr; |
| 4036 | uint64_t Members = 0; |
| 4037 | if (Kind == ELFv2 && |
| 4038 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4039 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4040 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4041 | return ABIArgInfo::getDirect(CoerceTy); |
| 4042 | } |
| 4043 | |
| 4044 | // ELFv2 small aggregates are returned in up to two registers. |
| 4045 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4046 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4047 | if (Bits == 0) |
| 4048 | return ABIArgInfo::getIgnore(); |
| 4049 | |
| 4050 | llvm::Type *CoerceTy; |
| 4051 | if (Bits > GPRBits) { |
| 4052 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 4053 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4054 | } else |
| 4055 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 4056 | llvm::RoundUpToAlignment(Bits, 8)); |
| 4057 | return ABIArgInfo::getDirect(CoerceTy); |
| 4058 | } |
| 4059 | |
| 4060 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4061 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4062 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4063 | |
| 4064 | return (isPromotableTypeForABI(RetTy) ? |
| 4065 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4066 | } |
| 4067 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4068 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4069 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4070 | QualType Ty) const { |
| 4071 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4072 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4073 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4074 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4075 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4076 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4077 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4078 | // in separate doublewords. However, Clang expects us to produce a |
| 4079 | // pointer to a structure with the two parts packed tightly. So generate |
| 4080 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4081 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4082 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4083 | CharUnits EltSize = TypeInfo.first / 2; |
| 4084 | if (EltSize < SlotSize) { |
| 4085 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4086 | SlotSize * 2, SlotSize, |
| 4087 | SlotSize, /*AllowHigher*/ true); |
| 4088 | |
| 4089 | Address RealAddr = Addr; |
| 4090 | Address ImagAddr = RealAddr; |
| 4091 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4092 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4093 | SlotSize - EltSize); |
| 4094 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4095 | 2 * SlotSize - EltSize); |
| 4096 | } else { |
| 4097 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4098 | } |
| 4099 | |
| 4100 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4101 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4102 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4103 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4104 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4105 | |
| 4106 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4107 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4108 | /*init*/ true); |
| 4109 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4110 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4113 | // Otherwise, just use the general rule. |
| 4114 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4115 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | static bool |
| 4119 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4120 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4121 | // This is calculated from the LLVM and GCC tables and verified |
| 4122 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4123 | |
| 4124 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4125 | |
| 4126 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4127 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4128 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4129 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4130 | |
| 4131 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4132 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4133 | |
| 4134 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4135 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4136 | |
| 4137 | // 64-76 are various 4-byte special-purpose registers: |
| 4138 | // 64: mq |
| 4139 | // 65: lr |
| 4140 | // 66: ctr |
| 4141 | // 67: ap |
| 4142 | // 68-75 cr0-7 |
| 4143 | // 76: xer |
| 4144 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 4145 | |
| 4146 | // 77-108: v0-31, the 16-byte vector registers |
| 4147 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4148 | |
| 4149 | // 109: vrsave |
| 4150 | // 110: vscr |
| 4151 | // 111: spe_acc |
| 4152 | // 112: spefscr |
| 4153 | // 113: sfp |
| 4154 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 4155 | |
| 4156 | return false; |
| 4157 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4158 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4159 | bool |
| 4160 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4161 | CodeGen::CodeGenFunction &CGF, |
| 4162 | llvm::Value *Address) const { |
| 4163 | |
| 4164 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4165 | } |
| 4166 | |
| 4167 | bool |
| 4168 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4169 | llvm::Value *Address) const { |
| 4170 | |
| 4171 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4172 | } |
| 4173 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4174 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4175 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4176 | //===----------------------------------------------------------------------===// |
| 4177 | |
| 4178 | namespace { |
| 4179 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4180 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4181 | public: |
| 4182 | enum ABIKind { |
| 4183 | AAPCS = 0, |
| 4184 | DarwinPCS |
| 4185 | }; |
| 4186 | |
| 4187 | private: |
| 4188 | ABIKind Kind; |
| 4189 | |
| 4190 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4191 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4192 | |
| 4193 | private: |
| 4194 | ABIKind getABIKind() const { return Kind; } |
| 4195 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4196 | |
| 4197 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4198 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4199 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4200 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4201 | uint64_t Members) const override; |
| 4202 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4203 | bool isIllegalVectorType(QualType Ty) const; |
| 4204 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4205 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4206 | if (!getCXXABI().classifyReturnType(FI)) |
| 4207 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4208 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4209 | for (auto &it : FI.arguments()) |
| 4210 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4213 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4214 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4215 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4216 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4217 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4218 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4219 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4220 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4221 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4222 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4223 | } |
| 4224 | }; |
| 4225 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4226 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4227 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4228 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4229 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4230 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4231 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4232 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4233 | } |
| 4234 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4235 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4236 | return 31; |
| 4237 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4238 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4239 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4240 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4241 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4242 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4243 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4244 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4245 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4246 | // Handle illegal vector types here. |
| 4247 | if (isIllegalVectorType(Ty)) { |
| 4248 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4249 | if (Size <= 32) { |
| 4250 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4251 | return ABIArgInfo::getDirect(ResType); |
| 4252 | } |
| 4253 | if (Size == 64) { |
| 4254 | llvm::Type *ResType = |
| 4255 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4256 | return ABIArgInfo::getDirect(ResType); |
| 4257 | } |
| 4258 | if (Size == 128) { |
| 4259 | llvm::Type *ResType = |
| 4260 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4261 | return ABIArgInfo::getDirect(ResType); |
| 4262 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4263 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4264 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4265 | |
| 4266 | if (!isAggregateTypeForABI(Ty)) { |
| 4267 | // Treat an enum type as its underlying type. |
| 4268 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4269 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4270 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4271 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4272 | ? ABIArgInfo::getExtend() |
| 4273 | : ABIArgInfo::getDirect()); |
| 4274 | } |
| 4275 | |
| 4276 | // Structures with either a non-trivial destructor or a non-trivial |
| 4277 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4278 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4279 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4280 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4284 | // elsewhere for GNU compatibility. |
| 4285 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4286 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4287 | return ABIArgInfo::getIgnore(); |
| 4288 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4289 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4290 | } |
| 4291 | |
| 4292 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4293 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4294 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4295 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4296 | return ABIArgInfo::getDirect( |
| 4297 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4301 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4302 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4303 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4304 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4305 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4306 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4307 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4308 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4309 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4310 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4311 | } |
| 4312 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4313 | } |
| 4314 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4315 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4318 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4319 | if (RetTy->isVoidType()) |
| 4320 | return ABIArgInfo::getIgnore(); |
| 4321 | |
| 4322 | // Large vector types should be returned via memory. |
| 4323 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4324 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4325 | |
| 4326 | if (!isAggregateTypeForABI(RetTy)) { |
| 4327 | // Treat an enum type as its underlying type. |
| 4328 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4329 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4330 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4331 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4332 | ? ABIArgInfo::getExtend() |
| 4333 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4336 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4337 | return ABIArgInfo::getIgnore(); |
| 4338 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4339 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4340 | uint64_t Members = 0; |
| 4341 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4342 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4343 | return ABIArgInfo::getDirect(); |
| 4344 | |
| 4345 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4346 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4347 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4348 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4349 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4350 | |
| 4351 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4352 | // For aggregates with 16-byte alignment, we use i128. |
| 4353 | if (Alignment < 128 && Size == 128) { |
| 4354 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4355 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4356 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4357 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4358 | } |
| 4359 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4360 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4363 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4364 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4365 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4366 | // Check whether VT is legal. |
| 4367 | unsigned NumElements = VT->getNumElements(); |
| 4368 | uint64_t Size = getContext().getTypeSize(VT); |
| 4369 | // NumElements should be power of 2 between 1 and 16. |
| 4370 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4371 | return true; |
| 4372 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4373 | } |
| 4374 | return false; |
| 4375 | } |
| 4376 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4377 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4378 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4379 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4380 | // but with the difference that any floating-point type is allowed, |
| 4381 | // including __fp16. |
| 4382 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4383 | if (BT->isFloatingPoint()) |
| 4384 | return true; |
| 4385 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4386 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4387 | if (VecSize == 64 || VecSize == 128) |
| 4388 | return true; |
| 4389 | } |
| 4390 | return false; |
| 4391 | } |
| 4392 | |
| 4393 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4394 | uint64_t Members) const { |
| 4395 | return Members <= 4; |
| 4396 | } |
| 4397 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4398 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4399 | QualType Ty, |
| 4400 | CodeGenFunction &CGF) const { |
| 4401 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4402 | bool IsIndirect = AI.isIndirect(); |
| 4403 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4404 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4405 | if (IsIndirect) |
| 4406 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4407 | else if (AI.getCoerceToType()) |
| 4408 | BaseTy = AI.getCoerceToType(); |
| 4409 | |
| 4410 | unsigned NumRegs = 1; |
| 4411 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4412 | BaseTy = ArrTy->getElementType(); |
| 4413 | NumRegs = ArrTy->getNumElements(); |
| 4414 | } |
| 4415 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4416 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4417 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4418 | // Standard, section B.4: |
| 4419 | // |
| 4420 | // struct { |
| 4421 | // void *__stack; |
| 4422 | // void *__gr_top; |
| 4423 | // void *__vr_top; |
| 4424 | // int __gr_offs; |
| 4425 | // int __vr_offs; |
| 4426 | // }; |
| 4427 | |
| 4428 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4429 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4430 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4431 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4432 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4433 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4434 | CharUnits TyAlign = TyInfo.second; |
| 4435 | |
| 4436 | Address reg_offs_p = Address::invalid(); |
| 4437 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4438 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4439 | CharUnits reg_top_offset; |
| 4440 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4441 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4442 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4443 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4444 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4445 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4446 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4447 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4448 | reg_top_offset = CharUnits::fromQuantity(8); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4449 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4450 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4451 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4452 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4453 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4454 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4455 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4456 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4457 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4458 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | //======================================= |
| 4462 | // Find out where argument was passed |
| 4463 | //======================================= |
| 4464 | |
| 4465 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4466 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4467 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4468 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4469 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4470 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4471 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4472 | |
| 4473 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4474 | |
| 4475 | // 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] | 4476 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4477 | CGF.EmitBlock(MaybeRegBlock); |
| 4478 | |
| 4479 | // Integer arguments may need to correct register alignment (for example a |
| 4480 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4481 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4482 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4483 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4484 | |
| 4485 | reg_offs = CGF.Builder.CreateAdd( |
| 4486 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4487 | "align_regoffs"); |
| 4488 | reg_offs = CGF.Builder.CreateAnd( |
| 4489 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4490 | "aligned_regoffs"); |
| 4491 | } |
| 4492 | |
| 4493 | // 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] | 4494 | // The fact that this is done unconditionally reflects the fact that |
| 4495 | // allocating an argument to the stack also uses up all the remaining |
| 4496 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4497 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4498 | NewOffset = CGF.Builder.CreateAdd( |
| 4499 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4500 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4501 | |
| 4502 | // Now we're in a position to decide whether this argument really was in |
| 4503 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4504 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4505 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4506 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4507 | |
| 4508 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4509 | |
| 4510 | //======================================= |
| 4511 | // Argument was in registers |
| 4512 | //======================================= |
| 4513 | |
| 4514 | // Now we emit the code for if the argument was originally passed in |
| 4515 | // registers. First start the appropriate block: |
| 4516 | CGF.EmitBlock(InRegBlock); |
| 4517 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4518 | llvm::Value *reg_top = nullptr; |
| 4519 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4520 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4521 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4522 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4523 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4524 | Address RegAddr = Address::invalid(); |
| 4525 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4526 | |
| 4527 | if (IsIndirect) { |
| 4528 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4529 | // stored registers or on the stack will actually be a struct **. |
| 4530 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4531 | } |
| 4532 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4533 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4534 | uint64_t NumMembers = 0; |
| 4535 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4536 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4537 | // Homogeneous aggregates passed in registers will have their elements split |
| 4538 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4539 | // qN+1, ...). We reload and store into a temporary local variable |
| 4540 | // contiguously. |
| 4541 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4542 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4543 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4544 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4545 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4546 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4547 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4548 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4549 | int Offset = 0; |
| 4550 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4551 | BaseTyInfo.first.getQuantity() < 16) |
| 4552 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4553 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4554 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4555 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4556 | Address LoadAddr = |
| 4557 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4558 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4559 | |
| 4560 | Address StoreAddr = |
| 4561 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4562 | |
| 4563 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4564 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4565 | } |
| 4566 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4567 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4568 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4569 | // Otherwise the object is contiguous in memory. |
| 4570 | |
| 4571 | // It might be right-aligned in its slot. |
| 4572 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4573 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4574 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4575 | TyInfo.first < SlotSize) { |
| 4576 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4577 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4580 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | CGF.EmitBranch(ContBlock); |
| 4584 | |
| 4585 | //======================================= |
| 4586 | // Argument was on the stack |
| 4587 | //======================================= |
| 4588 | CGF.EmitBlock(OnStackBlock); |
| 4589 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4590 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4591 | CharUnits::Zero(), "stack_p"); |
| 4592 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4593 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4594 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4595 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4596 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4597 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4598 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4599 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4600 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4601 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4602 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4603 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4604 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4605 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4606 | "align_stack"); |
| 4607 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4608 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4609 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4610 | Address OnStackAddr(OnStackPtr, |
| 4611 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4612 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4613 | // All stack slots are multiples of 8 bytes. |
| 4614 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4615 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4616 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4617 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4618 | else |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4619 | StackSize = TyInfo.first.RoundUpToAlignment(StackSlotSize); |
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 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4622 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4623 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4624 | |
| 4625 | // Write the new value of __stack for the next call to va_arg |
| 4626 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4627 | |
| 4628 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4629 | TyInfo.first < StackSlotSize) { |
| 4630 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4631 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4634 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4635 | |
| 4636 | CGF.EmitBranch(ContBlock); |
| 4637 | |
| 4638 | //======================================= |
| 4639 | // Tidy up |
| 4640 | //======================================= |
| 4641 | CGF.EmitBlock(ContBlock); |
| 4642 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4643 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4644 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4645 | |
| 4646 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4647 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4648 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4649 | |
| 4650 | return ResAddr; |
| 4651 | } |
| 4652 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4653 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4654 | CodeGenFunction &CGF) const { |
| 4655 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4656 | // illegal vector types. Lower VAArg here for these cases and use |
| 4657 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4658 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4659 | return Address::invalid(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4660 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4661 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4662 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4663 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4664 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4665 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4666 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4667 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4670 | // The size of the actual thing passed, which might end up just |
| 4671 | // being a pointer for indirect types. |
| 4672 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4673 | |
| 4674 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4675 | // aggregates should be passed indirectly. |
| 4676 | bool IsIndirect = false; |
| 4677 | if (TyInfo.first.getQuantity() > 16) { |
| 4678 | const Type *Base = nullptr; |
| 4679 | uint64_t Members = 0; |
| 4680 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4683 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4684 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4688 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4689 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4690 | |
| 4691 | namespace { |
| 4692 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4693 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4694 | public: |
| 4695 | enum ABIKind { |
| 4696 | APCS = 0, |
| 4697 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4698 | AAPCS_VFP = 2, |
| 4699 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4700 | }; |
| 4701 | |
| 4702 | private: |
| 4703 | ABIKind Kind; |
| 4704 | |
| 4705 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4706 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4707 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4708 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4709 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4710 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4711 | switch (getTarget().getTriple().getEnvironment()) { |
| 4712 | case llvm::Triple::Android: |
| 4713 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4714 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4715 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4716 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4717 | return true; |
| 4718 | default: |
| 4719 | return false; |
| 4720 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4723 | bool isEABIHF() const { |
| 4724 | switch (getTarget().getTriple().getEnvironment()) { |
| 4725 | case llvm::Triple::EABIHF: |
| 4726 | case llvm::Triple::GNUEABIHF: |
| 4727 | return true; |
| 4728 | default: |
| 4729 | return false; |
| 4730 | } |
| 4731 | } |
| 4732 | |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame^] | 4733 | bool isAndroid() const { |
| 4734 | return (getTarget().getTriple().getEnvironment() == |
| 4735 | llvm::Triple::Android); |
| 4736 | } |
| 4737 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4738 | ABIKind getABIKind() const { return Kind; } |
| 4739 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4740 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4741 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4742 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4743 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4744 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4745 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4746 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4747 | uint64_t Members) const override; |
| 4748 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4749 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4750 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4751 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4752 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4753 | |
| 4754 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4755 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4756 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4757 | }; |
| 4758 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4759 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4760 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4761 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4762 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4763 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4764 | const ARMABIInfo &getABIInfo() const { |
| 4765 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4766 | } |
| 4767 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4768 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4769 | return 13; |
| 4770 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4771 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4772 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4773 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4774 | } |
| 4775 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4776 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4777 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4778 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4779 | |
| 4780 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4781 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4782 | return false; |
| 4783 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4784 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4785 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4786 | if (getABIInfo().isEABI()) return 88; |
| 4787 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4788 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4789 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4790 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4791 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 4792 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4793 | if (!FD) |
| 4794 | return; |
| 4795 | |
| 4796 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4797 | if (!Attr) |
| 4798 | return; |
| 4799 | |
| 4800 | const char *Kind; |
| 4801 | switch (Attr->getInterrupt()) { |
| 4802 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4803 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4804 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4805 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4806 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4807 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4808 | } |
| 4809 | |
| 4810 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4811 | |
| 4812 | Fn->addFnAttr("interrupt", Kind); |
| 4813 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4814 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 4815 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4816 | return; |
| 4817 | |
| 4818 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4819 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4820 | // the backend to perform a realignment as part of the function prologue. |
| 4821 | llvm::AttrBuilder B; |
| 4822 | B.addStackAlignmentAttr(8); |
| 4823 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4824 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4825 | llvm::AttributeSet::FunctionIndex, |
| 4826 | B)); |
| 4827 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4828 | }; |
| 4829 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4830 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4831 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4832 | CodeGen::CodeGenModule &CGM) const; |
| 4833 | |
| 4834 | public: |
| 4835 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4836 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4837 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4838 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4839 | CodeGen::CodeGenModule &CGM) const override; |
| 4840 | }; |
| 4841 | |
| 4842 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4843 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4844 | if (!isa<FunctionDecl>(D)) |
| 4845 | return; |
| 4846 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4847 | return; |
| 4848 | |
| 4849 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4850 | F->addFnAttr("stack-probe-size", |
| 4851 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4852 | } |
| 4853 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4854 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4855 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4856 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4857 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4858 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4859 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4860 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4861 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4862 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4863 | FI.getReturnInfo() = |
| 4864 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4865 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4866 | for (auto &I : FI.arguments()) |
| 4867 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4868 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4869 | // Always honor user-specified calling convention. |
| 4870 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4871 | return; |
| 4872 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4873 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4874 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4875 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4876 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4877 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4878 | /// Return the default calling convention that LLVM will use. |
| 4879 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4880 | // The default calling convention that LLVM will infer. |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4881 | if (isEABIHF() || getTarget().getTriple().isWatchOS()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4882 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4883 | else if (isEABI()) |
| 4884 | return llvm::CallingConv::ARM_AAPCS; |
| 4885 | else |
| 4886 | return llvm::CallingConv::ARM_APCS; |
| 4887 | } |
| 4888 | |
| 4889 | /// Return the calling convention that our ABI would like us to use |
| 4890 | /// as the C calling convention. |
| 4891 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4892 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4893 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4894 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4895 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4896 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4897 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4898 | llvm_unreachable("bad ABI kind"); |
| 4899 | } |
| 4900 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4901 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4902 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4903 | |
| 4904 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4905 | // they'd just match what LLVM will infer from the triple. |
| 4906 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4907 | if (abiCC != getLLVMDefaultCC()) |
| 4908 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4909 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4910 | // AAPCS apparently requires runtime support functions to be soft-float, but |
| 4911 | // that's almost certainly for historic reasons (Thumb1 not supporting VFP |
| 4912 | // most likely). It's more convenient for AAPCS16_VFP to be hard-float. |
| 4913 | switch (getABIKind()) { |
| 4914 | case APCS: |
| 4915 | case AAPCS16_VFP: |
| 4916 | if (abiCC != getLLVMDefaultCC()) |
| 4917 | BuiltinCC = abiCC; |
| 4918 | break; |
| 4919 | case AAPCS: |
| 4920 | case AAPCS_VFP: |
| 4921 | BuiltinCC = llvm::CallingConv::ARM_AAPCS; |
| 4922 | break; |
| 4923 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4926 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4927 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4928 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4929 | // A single-precision floating-point type (including promoted |
| 4930 | // half-precision types); A double-precision floating-point type; |
| 4931 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4932 | // with a Base Type of a single- or double-precision floating-point type, |
| 4933 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4934 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4935 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4936 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4937 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4938 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4939 | // Handle illegal vector types here. |
| 4940 | if (isIllegalVectorType(Ty)) { |
| 4941 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4942 | if (Size <= 32) { |
| 4943 | llvm::Type *ResType = |
| 4944 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4945 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4946 | } |
| 4947 | if (Size == 64) { |
| 4948 | llvm::Type *ResType = llvm::VectorType::get( |
| 4949 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4950 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4951 | } |
| 4952 | if (Size == 128) { |
| 4953 | llvm::Type *ResType = llvm::VectorType::get( |
| 4954 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4955 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4956 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4957 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 4960 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 4961 | // unspecified. This is not done for OpenCL as it handles the half type |
| 4962 | // natively, and does not need to interwork with AAPCS code. |
| 4963 | if (Ty->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 4964 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 4965 | llvm::Type::getFloatTy(getVMContext()) : |
| 4966 | llvm::Type::getInt32Ty(getVMContext()); |
| 4967 | return ABIArgInfo::getDirect(ResType); |
| 4968 | } |
| 4969 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4970 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4971 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4972 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4973 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4974 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4975 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4976 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4977 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4978 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4979 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4980 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4981 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4982 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4983 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4984 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4985 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4986 | return ABIArgInfo::getIgnore(); |
| 4987 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4988 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4989 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4990 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4991 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4992 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4993 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4994 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4995 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4996 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4997 | } |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 4998 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 4999 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5000 | // this convention even for a variadic function: the backend will use GPRs |
| 5001 | // if needed. |
| 5002 | const Type *Base = nullptr; |
| 5003 | uint64_t Members = 0; |
| 5004 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5005 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5006 | llvm::Type *Ty = |
| 5007 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5008 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5013 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5014 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5015 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5016 | // and a pointer is passed. |
| 5017 | return ABIArgInfo::getIndirect( |
| 5018 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5021 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5022 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5023 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5024 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5025 | uint64_t ABIAlign = 4; |
| 5026 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 5027 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5028 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5029 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 5030 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5031 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5032 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5033 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5034 | /*ByVal=*/true, |
| 5035 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5038 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5039 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5040 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5041 | // FIXME: Try to match the types of the arguments more accurately where |
| 5042 | // we can. |
| 5043 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 5044 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 5045 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5046 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 5047 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5048 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 5049 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 5050 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5051 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5054 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5055 | llvm::LLVMContext &VMContext) { |
| 5056 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 5057 | // is called integer-like if its size is less than or equal to one word, and |
| 5058 | // the offset of each of its addressable sub-fields is zero. |
| 5059 | |
| 5060 | uint64_t Size = Context.getTypeSize(Ty); |
| 5061 | |
| 5062 | // Check that the type fits in a word. |
| 5063 | if (Size > 32) |
| 5064 | return false; |
| 5065 | |
| 5066 | // FIXME: Handle vector types! |
| 5067 | if (Ty->isVectorType()) |
| 5068 | return false; |
| 5069 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 5070 | // Float types are never treated as "integer like". |
| 5071 | if (Ty->isRealFloatingType()) |
| 5072 | return false; |
| 5073 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5074 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5075 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5076 | return true; |
| 5077 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 5078 | // Small complex integer types are "integer like". |
| 5079 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5080 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5081 | |
| 5082 | // Single element and zero sized arrays should be allowed, by the definition |
| 5083 | // above, but they are not. |
| 5084 | |
| 5085 | // Otherwise, it must be a record type. |
| 5086 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5087 | if (!RT) return false; |
| 5088 | |
| 5089 | // Ignore records with flexible arrays. |
| 5090 | const RecordDecl *RD = RT->getDecl(); |
| 5091 | if (RD->hasFlexibleArrayMember()) |
| 5092 | return false; |
| 5093 | |
| 5094 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5095 | // like". |
| 5096 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5097 | |
| 5098 | bool HadField = false; |
| 5099 | unsigned idx = 0; |
| 5100 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5101 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5102 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5103 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5104 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5105 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5106 | // struct { int : 0; int x } |
| 5107 | // is non-integer like according to gcc. |
| 5108 | if (FD->isBitField()) { |
| 5109 | if (!RD->isUnion()) |
| 5110 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5111 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5112 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5113 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5114 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5115 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5118 | // Check if this field is at offset 0. |
| 5119 | if (Layout.getFieldOffset(idx) != 0) |
| 5120 | return false; |
| 5121 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5122 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5123 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5124 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5125 | // Only allow at most one field in a structure. This doesn't match the |
| 5126 | // wording above, but follows gcc in situations with a field following an |
| 5127 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5128 | if (!RD->isUnion()) { |
| 5129 | if (HadField) |
| 5130 | return false; |
| 5131 | |
| 5132 | HadField = true; |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | return true; |
| 5137 | } |
| 5138 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5139 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5140 | bool isVariadic) const { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5141 | bool IsEffectivelyAAPCS_VFP = |
| 5142 | (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5143 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5144 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5145 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5146 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5147 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5148 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5149 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5150 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5151 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5152 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5153 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5154 | // natively, and does not need to interwork with AAPCS code. |
| 5155 | if (RetTy->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 5156 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5157 | llvm::Type::getFloatTy(getVMContext()) : |
| 5158 | llvm::Type::getInt32Ty(getVMContext()); |
| 5159 | return ABIArgInfo::getDirect(ResType); |
| 5160 | } |
| 5161 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5162 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5163 | // Treat an enum type as its underlying type. |
| 5164 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5165 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5166 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5167 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5168 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5169 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5170 | |
| 5171 | // Are we following APCS? |
| 5172 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5173 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5174 | return ABIArgInfo::getIgnore(); |
| 5175 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5176 | // Complex types are all returned as packed integers. |
| 5177 | // |
| 5178 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5179 | // correctly. |
| 5180 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5181 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5182 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5183 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5184 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5185 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5186 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5187 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5188 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5189 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5190 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5191 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5192 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5196 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5197 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5198 | |
| 5199 | // Otherwise this is an AAPCS variant. |
| 5200 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5201 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5202 | return ABIArgInfo::getIgnore(); |
| 5203 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5204 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5205 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5206 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5207 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5208 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5209 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5210 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5211 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5212 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5215 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5216 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5217 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5218 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5219 | if (getDataLayout().isBigEndian()) |
| 5220 | // 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] | 5221 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5222 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5223 | // Return in the smallest viable integer type. |
| 5224 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5225 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5226 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5227 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5228 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5229 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 5230 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 5231 | llvm::Type *CoerceTy = |
| 5232 | llvm::ArrayType::get(Int32Ty, llvm::RoundUpToAlignment(Size, 32) / 32); |
| 5233 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5236 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5239 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5240 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame^] | 5241 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
| 5242 | if (isAndroid()) { |
| 5243 | // Android shipped using Clang 3.1, which supported a slightly different |
| 5244 | // vector ABI. The primary differences were that 3-element vector types |
| 5245 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 5246 | // accepts that legacy behavior for Android only. |
| 5247 | // Check whether VT is legal. |
| 5248 | unsigned NumElements = VT->getNumElements(); |
| 5249 | // NumElements should be power of 2 or equal to 3. |
| 5250 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 5251 | return true; |
| 5252 | } else { |
| 5253 | // Check whether VT is legal. |
| 5254 | unsigned NumElements = VT->getNumElements(); |
| 5255 | uint64_t Size = getContext().getTypeSize(VT); |
| 5256 | // NumElements should be power of 2. |
| 5257 | if (!llvm::isPowerOf2_32(NumElements)) |
| 5258 | return true; |
| 5259 | // Size should be greater than 32 bits. |
| 5260 | return Size <= 32; |
| 5261 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5262 | } |
| 5263 | return false; |
| 5264 | } |
| 5265 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5266 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5267 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5268 | // double, or 64-bit or 128-bit vectors. |
| 5269 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5270 | if (BT->getKind() == BuiltinType::Float || |
| 5271 | BT->getKind() == BuiltinType::Double || |
| 5272 | BT->getKind() == BuiltinType::LongDouble) |
| 5273 | return true; |
| 5274 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5275 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5276 | if (VecSize == 64 || VecSize == 128) |
| 5277 | return true; |
| 5278 | } |
| 5279 | return false; |
| 5280 | } |
| 5281 | |
| 5282 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5283 | uint64_t Members) const { |
| 5284 | return Members <= 4; |
| 5285 | } |
| 5286 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5287 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5288 | QualType Ty) const { |
| 5289 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5290 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5291 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5292 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5293 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5294 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5295 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5298 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5299 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5300 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5301 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5302 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5303 | const Type *Base = nullptr; |
| 5304 | uint64_t Members = 0; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5305 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5306 | IsIndirect = true; |
| 5307 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5308 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 5309 | // allocated by the caller. |
| 5310 | } else if (TyInfo.first > CharUnits::fromQuantity(16) && |
| 5311 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5312 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 5313 | IsIndirect = true; |
| 5314 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5315 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5316 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5317 | // 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] | 5318 | // Our callers should be prepared to handle an under-aligned address. |
| 5319 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5320 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5321 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5322 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 5323 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5324 | // ARMv7k allows type alignment up to 16 bytes. |
| 5325 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5326 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5327 | } else { |
| 5328 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5329 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5330 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5332 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5333 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5336 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5337 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5338 | //===----------------------------------------------------------------------===// |
| 5339 | |
| 5340 | namespace { |
| 5341 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5342 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5343 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5344 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5345 | |
| 5346 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5347 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5348 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5349 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5350 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5351 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5352 | }; |
| 5353 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5354 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5355 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5356 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5357 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5358 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5359 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5360 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5361 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5362 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5363 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5364 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5365 | }; |
| 5366 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5367 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5368 | if (RetTy->isVoidType()) |
| 5369 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5370 | |
| 5371 | // note: this is different from default ABI |
| 5372 | if (!RetTy->isScalarType()) |
| 5373 | return ABIArgInfo::getDirect(); |
| 5374 | |
| 5375 | // Treat an enum type as its underlying type. |
| 5376 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5377 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5378 | |
| 5379 | return (RetTy->isPromotableIntegerType() ? |
| 5380 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5383 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5384 | // Treat an enum type as its underlying type. |
| 5385 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5386 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5387 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5388 | // Return aggregates type as indirect by value |
| 5389 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5390 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5391 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5392 | return (Ty->isPromotableIntegerType() ? |
| 5393 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5396 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5397 | if (!getCXXABI().classifyReturnType(FI)) |
| 5398 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5399 | for (auto &I : FI.arguments()) |
| 5400 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5401 | |
| 5402 | // Always honor user-specified calling convention. |
| 5403 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5404 | return; |
| 5405 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5406 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5407 | } |
| 5408 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5409 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5410 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5411 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5414 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5415 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5416 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5417 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5418 | if (!FD) return; |
| 5419 | |
| 5420 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5421 | |
| 5422 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5423 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5424 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5425 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5426 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5427 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5428 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5429 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5430 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5431 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5432 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5433 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5434 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5435 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5436 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5437 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5438 | // __global__ functions cannot be called from the device, we do not |
| 5439 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5440 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5441 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5442 | addNVVMMetadata(F, "kernel", 1); |
| 5443 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5444 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5445 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5446 | llvm::APSInt MaxThreads(32); |
| 5447 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5448 | if (MaxThreads > 0) |
| 5449 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5450 | |
| 5451 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5452 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5453 | // we don't have to add a PTX directive. |
| 5454 | if (Attr->getMinBlocks()) { |
| 5455 | llvm::APSInt MinBlocks(32); |
| 5456 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5457 | if (MinBlocks > 0) |
| 5458 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5459 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5460 | } |
| 5461 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5465 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5466 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5467 | llvm::Module *M = F->getParent(); |
| 5468 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5469 | |
| 5470 | // Get "nvvm.annotations" metadata node |
| 5471 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5472 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5473 | llvm::Metadata *MDVals[] = { |
| 5474 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5475 | llvm::ConstantAsMetadata::get( |
| 5476 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5477 | // Append metadata to nvvm.annotations |
| 5478 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5479 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5480 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5481 | |
| 5482 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5483 | // SystemZ ABI Implementation |
| 5484 | //===----------------------------------------------------------------------===// |
| 5485 | |
| 5486 | namespace { |
| 5487 | |
| 5488 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5489 | bool HasVector; |
| 5490 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5491 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5492 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5493 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5494 | |
| 5495 | bool isPromotableIntegerType(QualType Ty) const; |
| 5496 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5497 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5498 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5499 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5500 | |
| 5501 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5502 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5503 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5504 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5505 | if (!getCXXABI().classifyReturnType(FI)) |
| 5506 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5507 | for (auto &I : FI.arguments()) |
| 5508 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5509 | } |
| 5510 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5511 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5512 | QualType Ty) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5513 | }; |
| 5514 | |
| 5515 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5516 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5517 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5518 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5519 | }; |
| 5520 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5521 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5522 | |
| 5523 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5524 | // Treat an enum type as its underlying type. |
| 5525 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5526 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5527 | |
| 5528 | // Promotable integer types are required to be promoted by the ABI. |
| 5529 | if (Ty->isPromotableIntegerType()) |
| 5530 | return true; |
| 5531 | |
| 5532 | // 32-bit values must also be promoted. |
| 5533 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5534 | switch (BT->getKind()) { |
| 5535 | case BuiltinType::Int: |
| 5536 | case BuiltinType::UInt: |
| 5537 | return true; |
| 5538 | default: |
| 5539 | return false; |
| 5540 | } |
| 5541 | return false; |
| 5542 | } |
| 5543 | |
| 5544 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5545 | return (Ty->isAnyComplexType() || |
| 5546 | Ty->isVectorType() || |
| 5547 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5550 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5551 | return (HasVector && |
| 5552 | Ty->isVectorType() && |
| 5553 | getContext().getTypeSize(Ty) <= 128); |
| 5554 | } |
| 5555 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5556 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5557 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5558 | switch (BT->getKind()) { |
| 5559 | case BuiltinType::Float: |
| 5560 | case BuiltinType::Double: |
| 5561 | return true; |
| 5562 | default: |
| 5563 | return false; |
| 5564 | } |
| 5565 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5566 | return false; |
| 5567 | } |
| 5568 | |
| 5569 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5570 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5571 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5572 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5573 | |
| 5574 | // If this is a C++ record, check the bases first. |
| 5575 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5576 | for (const auto &I : CXXRD->bases()) { |
| 5577 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5578 | |
| 5579 | // Empty bases don't affect things either way. |
| 5580 | if (isEmptyRecord(getContext(), Base, true)) |
| 5581 | continue; |
| 5582 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5583 | if (!Found.isNull()) |
| 5584 | return Ty; |
| 5585 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5586 | } |
| 5587 | |
| 5588 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5589 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5590 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5591 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5592 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5593 | if (getContext().getLangOpts().CPlusPlus && |
| 5594 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5595 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5596 | |
| 5597 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5598 | // Nested structures still do though. |
| 5599 | if (!Found.isNull()) |
| 5600 | return Ty; |
| 5601 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5605 | // 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] | 5606 | if (!Found.isNull()) |
| 5607 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5610 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5613 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5614 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5615 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5616 | // struct { |
| 5617 | // i64 __gpr; |
| 5618 | // i64 __fpr; |
| 5619 | // i8 *__overflow_arg_area; |
| 5620 | // i8 *__reg_save_area; |
| 5621 | // }; |
| 5622 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5623 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5624 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5625 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5626 | Ty = getContext().getCanonicalType(Ty); |
| 5627 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5628 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5629 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5630 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5631 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5632 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5633 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5634 | CharUnits UnpaddedSize; |
| 5635 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5636 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5637 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5638 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5639 | } else { |
| 5640 | if (AI.getCoerceToType()) |
| 5641 | ArgTy = AI.getCoerceToType(); |
| 5642 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5643 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5644 | UnpaddedSize = TyInfo.first; |
| 5645 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5646 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5647 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5648 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5649 | PaddedSize = CharUnits::fromQuantity(16); |
| 5650 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5651 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5652 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5653 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5654 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5655 | llvm::Value *PaddedSizeV = |
| 5656 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5657 | |
| 5658 | if (IsVector) { |
| 5659 | // Work out the address of a vector argument on the stack. |
| 5660 | // Vector arguments are always passed in the high bits of a |
| 5661 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5662 | Address OverflowArgAreaPtr = |
| 5663 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5664 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5665 | Address OverflowArgArea = |
| 5666 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5667 | TyInfo.second); |
| 5668 | Address MemAddr = |
| 5669 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5670 | |
| 5671 | // Update overflow_arg_area_ptr pointer |
| 5672 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5673 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5674 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5675 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5676 | |
| 5677 | return MemAddr; |
| 5678 | } |
| 5679 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5680 | assert(PaddedSize.getQuantity() == 8); |
| 5681 | |
| 5682 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 5683 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5684 | if (InFPRs) { |
| 5685 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5686 | RegCountField = 1; // __fpr |
| 5687 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5688 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5689 | } else { |
| 5690 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5691 | RegCountField = 0; // __gpr |
| 5692 | RegSaveIndex = 2; // save offset for r2 |
| 5693 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5694 | } |
| 5695 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5696 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5697 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 5698 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5699 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5700 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5701 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5702 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5703 | |
| 5704 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5705 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5706 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5707 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5708 | |
| 5709 | // Emit code to load the value if it was passed in registers. |
| 5710 | CGF.EmitBlock(InRegBlock); |
| 5711 | |
| 5712 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5713 | llvm::Value *ScaledRegCount = |
| 5714 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5715 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5716 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 5717 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5718 | llvm::Value *RegOffset = |
| 5719 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5720 | Address RegSaveAreaPtr = |
| 5721 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5722 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5723 | llvm::Value *RegSaveArea = |
| 5724 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5725 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 5726 | "raw_reg_addr"), |
| 5727 | PaddedSize); |
| 5728 | Address RegAddr = |
| 5729 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5730 | |
| 5731 | // Update the register count |
| 5732 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5733 | llvm::Value *NewRegCount = |
| 5734 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5735 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5736 | CGF.EmitBranch(ContBlock); |
| 5737 | |
| 5738 | // Emit code to load the value if it was passed in memory. |
| 5739 | CGF.EmitBlock(InMemBlock); |
| 5740 | |
| 5741 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5742 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5743 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 5744 | Address OverflowArgArea = |
| 5745 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5746 | PaddedSize); |
| 5747 | Address RawMemAddr = |
| 5748 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 5749 | Address MemAddr = |
| 5750 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5751 | |
| 5752 | // Update overflow_arg_area_ptr pointer |
| 5753 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5754 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5755 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5756 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5757 | CGF.EmitBranch(ContBlock); |
| 5758 | |
| 5759 | // Return the appropriate result. |
| 5760 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5761 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5762 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5763 | |
| 5764 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5765 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 5766 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5767 | |
| 5768 | return ResAddr; |
| 5769 | } |
| 5770 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5771 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5772 | if (RetTy->isVoidType()) |
| 5773 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5774 | if (isVectorArgumentType(RetTy)) |
| 5775 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5776 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5777 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5778 | return (isPromotableIntegerType(RetTy) ? |
| 5779 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5780 | } |
| 5781 | |
| 5782 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5783 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5784 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5785 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5786 | |
| 5787 | // Integers and enums are extended to full register width. |
| 5788 | if (isPromotableIntegerType(Ty)) |
| 5789 | return ABIArgInfo::getExtend(); |
| 5790 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5791 | // Handle vector types and vector-like structure types. Note that |
| 5792 | // as opposed to float-like structure types, we do not allow any |
| 5793 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5794 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5795 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5796 | if (isVectorArgumentType(SingleElementTy) && |
| 5797 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5798 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5799 | |
| 5800 | // 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] | 5801 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5802 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5803 | |
| 5804 | // Handle small structures. |
| 5805 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5806 | // Structures with flexible arrays have variable length, so really |
| 5807 | // fail the size test above. |
| 5808 | const RecordDecl *RD = RT->getDecl(); |
| 5809 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5810 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5811 | |
| 5812 | // The structure is passed as an unextended integer, a float, or a double. |
| 5813 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5814 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5815 | assert(Size == 32 || Size == 64); |
| 5816 | if (Size == 32) |
| 5817 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5818 | else |
| 5819 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5820 | } else |
| 5821 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5822 | return ABIArgInfo::getDirect(PassTy); |
| 5823 | } |
| 5824 | |
| 5825 | // Non-structure compounds are passed indirectly. |
| 5826 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5827 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5828 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5829 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5833 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5834 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5835 | |
| 5836 | namespace { |
| 5837 | |
| 5838 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5839 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5840 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5841 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5842 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5843 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5844 | }; |
| 5845 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5846 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5847 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5848 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5849 | llvm::GlobalValue *GV, |
| 5850 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5851 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5852 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5853 | // Handle 'interrupt' attribute: |
| 5854 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5855 | |
| 5856 | // Step 1: Set ISR calling convention. |
| 5857 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5858 | |
| 5859 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5860 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5861 | |
| 5862 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5863 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5864 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5865 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5866 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5867 | } |
| 5868 | } |
| 5869 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5870 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5871 | // MIPS ABI Implementation. This works for both little-endian and |
| 5872 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5873 | //===----------------------------------------------------------------------===// |
| 5874 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5875 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5876 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5877 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5878 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5879 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5880 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5881 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5882 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5883 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5884 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5885 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5886 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5887 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5888 | |
| 5889 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5890 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5891 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5892 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5893 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5894 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5895 | }; |
| 5896 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5897 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5898 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5899 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5900 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5901 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5902 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5903 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5904 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5905 | return 29; |
| 5906 | } |
| 5907 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5908 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5909 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5910 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5911 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5912 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5913 | if (FD->hasAttr<Mips16Attr>()) { |
| 5914 | Fn->addFnAttr("mips16"); |
| 5915 | } |
| 5916 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5917 | Fn->addFnAttr("nomips16"); |
| 5918 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5919 | |
| 5920 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 5921 | if (!Attr) |
| 5922 | return; |
| 5923 | |
| 5924 | const char *Kind; |
| 5925 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5926 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 5927 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 5928 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 5929 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 5930 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 5931 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 5932 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 5933 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 5934 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 5935 | } |
| 5936 | |
| 5937 | Fn->addFnAttr("interrupt", Kind); |
| 5938 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5939 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5940 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5941 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5942 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5943 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5944 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5945 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5946 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5947 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5948 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5949 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5950 | void MipsABIInfo::CoerceToIntArgs( |
| 5951 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5952 | llvm::IntegerType *IntTy = |
| 5953 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5954 | |
| 5955 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5956 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5957 | ArgList.push_back(IntTy); |
| 5958 | |
| 5959 | // If necessary, add one more integer type to ArgList. |
| 5960 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5961 | |
| 5962 | if (R) |
| 5963 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5964 | } |
| 5965 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5966 | // In N32/64, an aligned double precision floating point field is passed in |
| 5967 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5968 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5969 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5970 | |
| 5971 | if (IsO32) { |
| 5972 | CoerceToIntArgs(TySize, ArgList); |
| 5973 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5974 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5975 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5976 | if (Ty->isComplexType()) |
| 5977 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5978 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5979 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5980 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5981 | // Unions/vectors are passed in integer registers. |
| 5982 | if (!RT || !RT->isStructureOrClassType()) { |
| 5983 | CoerceToIntArgs(TySize, ArgList); |
| 5984 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5985 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5986 | |
| 5987 | const RecordDecl *RD = RT->getDecl(); |
| 5988 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5989 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5990 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5991 | uint64_t LastOffset = 0; |
| 5992 | unsigned idx = 0; |
| 5993 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5994 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5995 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5996 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5997 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5998 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5999 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6000 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 6001 | |
| 6002 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 6003 | continue; |
| 6004 | |
| 6005 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 6006 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 6007 | continue; |
| 6008 | |
| 6009 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 6010 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 6011 | ArgList.push_back(I64); |
| 6012 | |
| 6013 | // Add double type. |
| 6014 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 6015 | LastOffset = Offset + 64; |
| 6016 | } |
| 6017 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6018 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 6019 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 6020 | |
| 6021 | return llvm::StructType::get(getVMContext(), ArgList); |
| 6022 | } |
| 6023 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6024 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 6025 | uint64_t Offset) const { |
| 6026 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6027 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6028 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6029 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6030 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 6031 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6032 | ABIArgInfo |
| 6033 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 6034 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 6035 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6036 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6037 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6038 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6039 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6040 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 6041 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6042 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 6043 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6044 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6045 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6046 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6047 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6048 | return ABIArgInfo::getIgnore(); |
| 6049 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6050 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6051 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6052 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6053 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 6054 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6055 | // If we have reached here, aggregates are passed directly by coercing to |
| 6056 | // another structure type. Padding is inserted if the offset of the |
| 6057 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 6058 | ABIArgInfo ArgInfo = |
| 6059 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 6060 | getPaddingType(OrigOffset, CurrOffset)); |
| 6061 | ArgInfo.setInReg(true); |
| 6062 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6063 | } |
| 6064 | |
| 6065 | // Treat an enum type as its underlying type. |
| 6066 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6067 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6068 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 6069 | // All integral types are promoted to the GPR width. |
| 6070 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6071 | return ABIArgInfo::getExtend(); |
| 6072 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 6073 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6074 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6075 | } |
| 6076 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6077 | llvm::Type* |
| 6078 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6079 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6080 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6081 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6082 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6083 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6084 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 6085 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6086 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6087 | // N32/64 returns struct/classes in floating point registers if the |
| 6088 | // following conditions are met: |
| 6089 | // 1. The size of the struct/class is no larger than 128-bit. |
| 6090 | // 2. The struct/class has one or two fields all of which are floating |
| 6091 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6092 | // 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] | 6093 | // |
| 6094 | // Any other composite results are returned in integer registers. |
| 6095 | // |
| 6096 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 6097 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 6098 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6099 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6100 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6101 | if (!BT || !BT->isFloatingPoint()) |
| 6102 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6103 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6104 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | if (b == e) |
| 6108 | return llvm::StructType::get(getVMContext(), RTList, |
| 6109 | RD->hasAttr<PackedAttr>()); |
| 6110 | |
| 6111 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6112 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6113 | } |
| 6114 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6115 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6116 | return llvm::StructType::get(getVMContext(), RTList); |
| 6117 | } |
| 6118 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6119 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 6120 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6121 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 6122 | if (RetTy->isVoidType()) |
| 6123 | return ABIArgInfo::getIgnore(); |
| 6124 | |
| 6125 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 6126 | // However, N32/N64 ignores zero sized return values. |
| 6127 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6128 | return ABIArgInfo::getIgnore(); |
| 6129 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 6130 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6131 | if (Size <= 128) { |
| 6132 | if (RetTy->isAnyComplexType()) |
| 6133 | return ABIArgInfo::getDirect(); |
| 6134 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6135 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6136 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6137 | if (!IsO32 || |
| 6138 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6139 | ABIArgInfo ArgInfo = |
| 6140 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6141 | ArgInfo.setInReg(true); |
| 6142 | return ArgInfo; |
| 6143 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6144 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6145 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6146 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6147 | } |
| 6148 | |
| 6149 | // Treat an enum type as its underlying type. |
| 6150 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6151 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6152 | |
| 6153 | return (RetTy->isPromotableIntegerType() ? |
| 6154 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6155 | } |
| 6156 | |
| 6157 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6158 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6159 | if (!getCXXABI().classifyReturnType(FI)) |
| 6160 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6161 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6162 | // 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] | 6163 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6164 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6165 | for (auto &I : FI.arguments()) |
| 6166 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6167 | } |
| 6168 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6169 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6170 | QualType OrigTy) const { |
| 6171 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6172 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6173 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6174 | // 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] | 6175 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6176 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6177 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6178 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6179 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6180 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6181 | DidPromote = true; |
| 6182 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6183 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6184 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6185 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6186 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6187 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6188 | // The alignment of things in the argument area is never larger than |
| 6189 | // StackAlignInBytes. |
| 6190 | TyInfo.second = |
| 6191 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6192 | |
| 6193 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6194 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6195 | |
| 6196 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6197 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6198 | |
| 6199 | |
| 6200 | // If there was a promotion, "unpromote" into a temporary. |
| 6201 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6202 | if (DidPromote) { |
| 6203 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6204 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6205 | |
| 6206 | // Truncate down to the right width. |
| 6207 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6208 | : CGF.IntPtrTy); |
| 6209 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6210 | if (OrigTy->isPointerType()) |
| 6211 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6212 | |
| 6213 | CGF.Builder.CreateStore(V, Temp); |
| 6214 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6215 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6216 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6217 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6218 | } |
| 6219 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6220 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6221 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6222 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6223 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6224 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6225 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6226 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6227 | return false; |
| 6228 | } |
| 6229 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6230 | bool |
| 6231 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6232 | llvm::Value *Address) const { |
| 6233 | // This information comes from gcc's implementation, which seems to |
| 6234 | // as canonical as it gets. |
| 6235 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6236 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6237 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6238 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6239 | |
| 6240 | // 0-31 are the general purpose registers, $0 - $31. |
| 6241 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6242 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6243 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6244 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6245 | |
| 6246 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6247 | // They are one bit wide and ignored here. |
| 6248 | |
| 6249 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6250 | // (coprocessor 1 is the FP unit) |
| 6251 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6252 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6253 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6254 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6255 | return false; |
| 6256 | } |
| 6257 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6258 | //===----------------------------------------------------------------------===// |
| 6259 | // 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] | 6260 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6261 | // handling. |
| 6262 | //===----------------------------------------------------------------------===// |
| 6263 | |
| 6264 | namespace { |
| 6265 | |
| 6266 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6267 | public: |
| 6268 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6269 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6270 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6271 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6272 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6273 | }; |
| 6274 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6275 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6276 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6277 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6278 | if (!FD) return; |
| 6279 | |
| 6280 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6281 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6282 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6283 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6284 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6285 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6286 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6287 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6288 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6289 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6290 | llvm::NamedMDNode *OpenCLMetadata = |
| 6291 | M.getModule().getOrInsertNamedMetadata( |
| 6292 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6293 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6294 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6295 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6296 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6297 | Operands.push_back( |
| 6298 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6299 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6300 | Operands.push_back( |
| 6301 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6302 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6303 | Operands.push_back( |
| 6304 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6305 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6306 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6307 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6308 | // (false) for implementing the work_group_size_hint attr later. |
| 6309 | // 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] | 6310 | Operands.push_back( |
| 6311 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6312 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6313 | } |
| 6314 | } |
| 6315 | } |
| 6316 | } |
| 6317 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6318 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6319 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6320 | //===----------------------------------------------------------------------===// |
| 6321 | // Hexagon ABI Implementation |
| 6322 | //===----------------------------------------------------------------------===// |
| 6323 | |
| 6324 | namespace { |
| 6325 | |
| 6326 | class HexagonABIInfo : public ABIInfo { |
| 6327 | |
| 6328 | |
| 6329 | public: |
| 6330 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6331 | |
| 6332 | private: |
| 6333 | |
| 6334 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6335 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6336 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6337 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6338 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6339 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6340 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6341 | }; |
| 6342 | |
| 6343 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6344 | public: |
| 6345 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6346 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6347 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6348 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6349 | return 29; |
| 6350 | } |
| 6351 | }; |
| 6352 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6353 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6354 | |
| 6355 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6356 | if (!getCXXABI().classifyReturnType(FI)) |
| 6357 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6358 | for (auto &I : FI.arguments()) |
| 6359 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
| 6362 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6363 | if (!isAggregateTypeForABI(Ty)) { |
| 6364 | // Treat an enum type as its underlying type. |
| 6365 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6366 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6367 | |
| 6368 | return (Ty->isPromotableIntegerType() ? |
| 6369 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6370 | } |
| 6371 | |
| 6372 | // Ignore empty records. |
| 6373 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6374 | return ABIArgInfo::getIgnore(); |
| 6375 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6376 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6377 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6378 | |
| 6379 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6380 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6381 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6382 | // Pass in the smallest viable integer type. |
| 6383 | else if (Size > 32) |
| 6384 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6385 | else if (Size > 16) |
| 6386 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6387 | else if (Size > 8) |
| 6388 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6389 | else |
| 6390 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6391 | } |
| 6392 | |
| 6393 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6394 | if (RetTy->isVoidType()) |
| 6395 | return ABIArgInfo::getIgnore(); |
| 6396 | |
| 6397 | // Large vector types should be returned via memory. |
| 6398 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6399 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6400 | |
| 6401 | if (!isAggregateTypeForABI(RetTy)) { |
| 6402 | // Treat an enum type as its underlying type. |
| 6403 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6404 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6405 | |
| 6406 | return (RetTy->isPromotableIntegerType() ? |
| 6407 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6408 | } |
| 6409 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6410 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6411 | return ABIArgInfo::getIgnore(); |
| 6412 | |
| 6413 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6414 | // are returned indirectly. |
| 6415 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6416 | if (Size <= 64) { |
| 6417 | // Return in the smallest viable integer type. |
| 6418 | if (Size <= 8) |
| 6419 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6420 | if (Size <= 16) |
| 6421 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6422 | if (Size <= 32) |
| 6423 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6424 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6425 | } |
| 6426 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6427 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6430 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6431 | QualType Ty) const { |
| 6432 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6433 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6434 | getContext().getTypeInfoInChars(Ty), |
| 6435 | CharUnits::fromQuantity(4), |
| 6436 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6439 | //===----------------------------------------------------------------------===// |
| 6440 | // AMDGPU ABI Implementation |
| 6441 | //===----------------------------------------------------------------------===// |
| 6442 | |
| 6443 | namespace { |
| 6444 | |
| 6445 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6446 | public: |
| 6447 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6448 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6449 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6450 | CodeGen::CodeGenModule &M) const override; |
| 6451 | }; |
| 6452 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6453 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6454 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6455 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6456 | const Decl *D, |
| 6457 | llvm::GlobalValue *GV, |
| 6458 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6459 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6460 | if (!FD) |
| 6461 | return; |
| 6462 | |
| 6463 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6464 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6465 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6466 | if (NumVGPR != 0) |
| 6467 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6468 | } |
| 6469 | |
| 6470 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6471 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6472 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6473 | if (NumSGPR != 0) |
| 6474 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6475 | } |
| 6476 | } |
| 6477 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6478 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6479 | //===----------------------------------------------------------------------===// |
| 6480 | // SPARC v9 ABI Implementation. |
| 6481 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6482 | // |
| 6483 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6484 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6485 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6486 | // |
| 6487 | // One case requires special care: |
| 6488 | // |
| 6489 | // struct mixed { |
| 6490 | // int i; |
| 6491 | // float f; |
| 6492 | // }; |
| 6493 | // |
| 6494 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6495 | // parameter array, but the int is passed in an integer register, and the float |
| 6496 | // is passed in a floating point register. This is represented as two arguments |
| 6497 | // with the LLVM IR inreg attribute: |
| 6498 | // |
| 6499 | // declare void f(i32 inreg %i, float inreg %f) |
| 6500 | // |
| 6501 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6502 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6503 | // bytes. |
| 6504 | // |
| 6505 | namespace { |
| 6506 | class SparcV9ABIInfo : public ABIInfo { |
| 6507 | public: |
| 6508 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6509 | |
| 6510 | private: |
| 6511 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6512 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6513 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6514 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6515 | |
| 6516 | // Coercion type builder for structs passed in registers. The coercion type |
| 6517 | // serves two purposes: |
| 6518 | // |
| 6519 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6520 | // in registers. |
| 6521 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6522 | // code generator knows to pass them in floating point registers. |
| 6523 | // |
| 6524 | // We also compute the InReg flag which indicates that the struct contains |
| 6525 | // aligned 32-bit floats. |
| 6526 | // |
| 6527 | struct CoerceBuilder { |
| 6528 | llvm::LLVMContext &Context; |
| 6529 | const llvm::DataLayout &DL; |
| 6530 | SmallVector<llvm::Type*, 8> Elems; |
| 6531 | uint64_t Size; |
| 6532 | bool InReg; |
| 6533 | |
| 6534 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6535 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6536 | |
| 6537 | // Pad Elems with integers until Size is ToSize. |
| 6538 | void pad(uint64_t ToSize) { |
| 6539 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6540 | if (ToSize == Size) |
| 6541 | return; |
| 6542 | |
| 6543 | // Finish the current 64-bit word. |
| 6544 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6545 | if (Aligned > Size && Aligned <= ToSize) { |
| 6546 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6547 | Size = Aligned; |
| 6548 | } |
| 6549 | |
| 6550 | // Add whole 64-bit words. |
| 6551 | while (Size + 64 <= ToSize) { |
| 6552 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6553 | Size += 64; |
| 6554 | } |
| 6555 | |
| 6556 | // Final in-word padding. |
| 6557 | if (Size < ToSize) { |
| 6558 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6559 | Size = ToSize; |
| 6560 | } |
| 6561 | } |
| 6562 | |
| 6563 | // Add a floating point element at Offset. |
| 6564 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6565 | // Unaligned floats are treated as integers. |
| 6566 | if (Offset % Bits) |
| 6567 | return; |
| 6568 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6569 | if (Bits < 64) |
| 6570 | InReg = true; |
| 6571 | pad(Offset); |
| 6572 | Elems.push_back(Ty); |
| 6573 | Size = Offset + Bits; |
| 6574 | } |
| 6575 | |
| 6576 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6577 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6578 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6579 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6580 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6581 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6582 | switch (ElemTy->getTypeID()) { |
| 6583 | case llvm::Type::StructTyID: |
| 6584 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6585 | break; |
| 6586 | case llvm::Type::FloatTyID: |
| 6587 | addFloat(ElemOffset, ElemTy, 32); |
| 6588 | break; |
| 6589 | case llvm::Type::DoubleTyID: |
| 6590 | addFloat(ElemOffset, ElemTy, 64); |
| 6591 | break; |
| 6592 | case llvm::Type::FP128TyID: |
| 6593 | addFloat(ElemOffset, ElemTy, 128); |
| 6594 | break; |
| 6595 | case llvm::Type::PointerTyID: |
| 6596 | if (ElemOffset % 64 == 0) { |
| 6597 | pad(ElemOffset); |
| 6598 | Elems.push_back(ElemTy); |
| 6599 | Size += 64; |
| 6600 | } |
| 6601 | break; |
| 6602 | default: |
| 6603 | break; |
| 6604 | } |
| 6605 | } |
| 6606 | } |
| 6607 | |
| 6608 | // Check if Ty is a usable substitute for the coercion type. |
| 6609 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6610 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
| 6613 | // Get the coercion type as a literal struct type. |
| 6614 | llvm::Type *getType() const { |
| 6615 | if (Elems.size() == 1) |
| 6616 | return Elems.front(); |
| 6617 | else |
| 6618 | return llvm::StructType::get(Context, Elems); |
| 6619 | } |
| 6620 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6621 | }; |
| 6622 | } // end anonymous namespace |
| 6623 | |
| 6624 | ABIArgInfo |
| 6625 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6626 | if (Ty->isVoidType()) |
| 6627 | return ABIArgInfo::getIgnore(); |
| 6628 | |
| 6629 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6630 | |
| 6631 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6632 | // pointer / sret pointer. |
| 6633 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6634 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6635 | |
| 6636 | // Treat an enum type as its underlying type. |
| 6637 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6638 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6639 | |
| 6640 | // Integer types smaller than a register are extended. |
| 6641 | if (Size < 64 && Ty->isIntegerType()) |
| 6642 | return ABIArgInfo::getExtend(); |
| 6643 | |
| 6644 | // Other non-aggregates go in registers. |
| 6645 | if (!isAggregateTypeForABI(Ty)) |
| 6646 | return ABIArgInfo::getDirect(); |
| 6647 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6648 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6649 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6650 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6651 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6652 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6653 | // 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] | 6654 | // Build a coercion type from the LLVM struct type. |
| 6655 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6656 | if (!StrTy) |
| 6657 | return ABIArgInfo::getDirect(); |
| 6658 | |
| 6659 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6660 | CB.addStruct(0, StrTy); |
| 6661 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6662 | |
| 6663 | // Try to use the original type for coercion. |
| 6664 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6665 | |
| 6666 | if (CB.InReg) |
| 6667 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6668 | else |
| 6669 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6672 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6673 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6674 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6675 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6676 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6677 | AI.setCoerceToType(ArgTy); |
| 6678 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6679 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6680 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6681 | CGBuilderTy &Builder = CGF.Builder; |
| 6682 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 6683 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6684 | |
| 6685 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 6686 | |
| 6687 | Address ArgAddr = Address::invalid(); |
| 6688 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6689 | switch (AI.getKind()) { |
| 6690 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6691 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6692 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6693 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6694 | case ABIArgInfo::Extend: { |
| 6695 | Stride = SlotSize; |
| 6696 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 6697 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6698 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6699 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6700 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6701 | case ABIArgInfo::Direct: { |
| 6702 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6703 | Stride = CharUnits::fromQuantity(AllocSize).RoundUpToAlignment(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6704 | ArgAddr = Addr; |
| 6705 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6706 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6707 | |
| 6708 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6709 | Stride = SlotSize; |
| 6710 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 6711 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 6712 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6713 | break; |
| 6714 | |
| 6715 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6716 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6717 | } |
| 6718 | |
| 6719 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6720 | llvm::Value *NextPtr = |
| 6721 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 6722 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6723 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6724 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6725 | } |
| 6726 | |
| 6727 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6728 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6729 | for (auto &I : FI.arguments()) |
| 6730 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
| 6733 | namespace { |
| 6734 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6735 | public: |
| 6736 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6737 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6738 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6739 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6740 | return 14; |
| 6741 | } |
| 6742 | |
| 6743 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6744 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6745 | }; |
| 6746 | } // end anonymous namespace |
| 6747 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6748 | bool |
| 6749 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6750 | llvm::Value *Address) const { |
| 6751 | // This is calculated from the LLVM and GCC tables and verified |
| 6752 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6753 | |
| 6754 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6755 | |
| 6756 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6757 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6758 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6759 | |
| 6760 | // 0-31: the 8-byte general-purpose registers |
| 6761 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6762 | |
| 6763 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6764 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6765 | |
| 6766 | // Y = 64 |
| 6767 | // PSR = 65 |
| 6768 | // WIM = 66 |
| 6769 | // TBR = 67 |
| 6770 | // PC = 68 |
| 6771 | // NPC = 69 |
| 6772 | // FSR = 70 |
| 6773 | // CSR = 71 |
| 6774 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6775 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6776 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6777 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6778 | |
| 6779 | return false; |
| 6780 | } |
| 6781 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6782 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6783 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6784 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6785 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6786 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6787 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6788 | |
| 6789 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6790 | /// it by reference between functions that append to it. |
| 6791 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6792 | |
| 6793 | /// TypeStringCache caches the meta encodings of Types. |
| 6794 | /// |
| 6795 | /// The reason for caching TypeStrings is two fold: |
| 6796 | /// 1. To cache a type's encoding for later uses; |
| 6797 | /// 2. As a means to break recursive member type inclusion. |
| 6798 | /// |
| 6799 | /// A cache Entry can have a Status of: |
| 6800 | /// NonRecursive: The type encoding is not recursive; |
| 6801 | /// Recursive: The type encoding is recursive; |
| 6802 | /// Incomplete: An incomplete TypeString; |
| 6803 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6804 | /// Recursive type encoding. |
| 6805 | /// |
| 6806 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6807 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6808 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6809 | /// the type is encountered. |
| 6810 | /// |
| 6811 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6812 | /// possible. The type itself is recursive and it may contain other types which |
| 6813 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6814 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6815 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6816 | /// |
| 6817 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6818 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6819 | /// are placed into the cache during type expansion as a means to identify and |
| 6820 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6821 | /// the entry becomes IncompleteUsed. |
| 6822 | /// |
| 6823 | /// During the expansion of a RecordType's members: |
| 6824 | /// |
| 6825 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6826 | /// cached encoding is used; |
| 6827 | /// |
| 6828 | /// If the cache contains a Recursive encoding for the member type, the |
| 6829 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6830 | /// |
| 6831 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6832 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6833 | /// |
| 6834 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6835 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6836 | /// it is swapped back in; |
| 6837 | /// |
| 6838 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6839 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6840 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6841 | /// |
| 6842 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6843 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6844 | /// Else the member is part of a recursive type and thus the recursion has |
| 6845 | /// been exited too soon for the encoding to be correct for the member. |
| 6846 | /// |
| 6847 | class TypeStringCache { |
| 6848 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6849 | struct Entry { |
| 6850 | std::string Str; // The encoded TypeString for the type. |
| 6851 | enum Status State; // Information about the encoding in 'Str'. |
| 6852 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6853 | // during the expansion of RecordType's members. |
| 6854 | }; |
| 6855 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6856 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6857 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6858 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6859 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6860 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6861 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6862 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6863 | bool IsRecursive); |
| 6864 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6865 | }; |
| 6866 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6867 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6868 | /// FieldEncoding is a helper for this ordering process. |
| 6869 | class FieldEncoding { |
| 6870 | bool HasName; |
| 6871 | std::string Enc; |
| 6872 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6873 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 6874 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6875 | bool operator<(const FieldEncoding &rhs) const { |
| 6876 | if (HasName != rhs.HasName) return HasName; |
| 6877 | return Enc < rhs.Enc; |
| 6878 | } |
| 6879 | }; |
| 6880 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6881 | class XCoreABIInfo : public DefaultABIInfo { |
| 6882 | public: |
| 6883 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6884 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6885 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6886 | }; |
| 6887 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6888 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6889 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6890 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6891 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6892 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6893 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6894 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6895 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6896 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6897 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6898 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6899 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6900 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6901 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6902 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6903 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6904 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 6905 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6906 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6907 | // Handle the argument. |
| 6908 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6909 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6910 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6911 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6912 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6913 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6914 | |
| 6915 | Address Val = Address::invalid(); |
| 6916 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6917 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6918 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6919 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6920 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6921 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6922 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 6923 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6924 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6925 | case ABIArgInfo::Extend: |
| 6926 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6927 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 6928 | ArgSize = CharUnits::fromQuantity( |
| 6929 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
| 6930 | ArgSize = ArgSize.RoundUpToAlignment(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6931 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6932 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6933 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 6934 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 6935 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6936 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6937 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6938 | |
| 6939 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6940 | if (!ArgSize.isZero()) { |
| 6941 | llvm::Value *APN = |
| 6942 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 6943 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6944 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6945 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6946 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6947 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6948 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6949 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6950 | /// into the cache as a means to identify and break recursion. |
| 6951 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6952 | /// be reinserted by removeIncomplete(). |
| 6953 | /// All other types of encoding should have been used rather than arriving here. |
| 6954 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6955 | std::string StubEnc) { |
| 6956 | if (!ID) |
| 6957 | return; |
| 6958 | Entry &E = Map[ID]; |
| 6959 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6960 | "Incorrectly use of addIncomplete"); |
| 6961 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6962 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6963 | E.Str.swap(StubEnc); |
| 6964 | E.State = Incomplete; |
| 6965 | ++IncompleteCount; |
| 6966 | } |
| 6967 | |
| 6968 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6969 | /// must be removed from the cache. |
| 6970 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6971 | /// Returns true if the RecordType was defined recursively. |
| 6972 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6973 | if (!ID) |
| 6974 | return false; |
| 6975 | auto I = Map.find(ID); |
| 6976 | assert(I != Map.end() && "Entry not present"); |
| 6977 | Entry &E = I->second; |
| 6978 | assert( (E.State == Incomplete || |
| 6979 | E.State == IncompleteUsed) && |
| 6980 | "Entry must be an incomplete type"); |
| 6981 | bool IsRecursive = false; |
| 6982 | if (E.State == IncompleteUsed) { |
| 6983 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6984 | IsRecursive = true; |
| 6985 | --IncompleteUsedCount; |
| 6986 | } |
| 6987 | if (E.Swapped.empty()) |
| 6988 | Map.erase(I); |
| 6989 | else { |
| 6990 | // Swap the Recursive back. |
| 6991 | E.Swapped.swap(E.Str); |
| 6992 | E.Swapped.clear(); |
| 6993 | E.State = Recursive; |
| 6994 | } |
| 6995 | --IncompleteCount; |
| 6996 | return IsRecursive; |
| 6997 | } |
| 6998 | |
| 6999 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 7000 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 7001 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 7002 | bool IsRecursive) { |
| 7003 | if (!ID || IncompleteUsedCount) |
| 7004 | return; // No key or it is is an incomplete sub-type so don't add. |
| 7005 | Entry &E = Map[ID]; |
| 7006 | if (IsRecursive && !E.Str.empty()) { |
| 7007 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 7008 | "This is not the same Recursive entry"); |
| 7009 | // The parent container was not recursive after all, so we could have used |
| 7010 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 7011 | // we started viz: IncompleteCount!=0. |
| 7012 | return; |
| 7013 | } |
| 7014 | assert(E.Str.empty() && "Entry already present"); |
| 7015 | E.Str = Str.str(); |
| 7016 | E.State = IsRecursive? Recursive : NonRecursive; |
| 7017 | } |
| 7018 | |
| 7019 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 7020 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 7021 | /// encoding is Recursive, return an empty StringRef. |
| 7022 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 7023 | if (!ID) |
| 7024 | return StringRef(); // We have no key. |
| 7025 | auto I = Map.find(ID); |
| 7026 | if (I == Map.end()) |
| 7027 | return StringRef(); // We have no encoding. |
| 7028 | Entry &E = I->second; |
| 7029 | if (E.State == Recursive && IncompleteCount) |
| 7030 | return StringRef(); // We don't use Recursive encodings for member types. |
| 7031 | |
| 7032 | if (E.State == Incomplete) { |
| 7033 | // The incomplete type is being used to break out of recursion. |
| 7034 | E.State = IncompleteUsed; |
| 7035 | ++IncompleteUsedCount; |
| 7036 | } |
| 7037 | return E.Str.c_str(); |
| 7038 | } |
| 7039 | |
| 7040 | /// The XCore ABI includes a type information section that communicates symbol |
| 7041 | /// type information to the linker. The linker uses this information to verify |
| 7042 | /// safety/correctness of things such as array bound and pointers et al. |
| 7043 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 7044 | /// This type information (TypeString) is emitted into meta data for all global |
| 7045 | /// symbols: definitions, declarations, functions & variables. |
| 7046 | /// |
| 7047 | /// The TypeString carries type, qualifier, name, size & value details. |
| 7048 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7049 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7050 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 7051 | /// |
| 7052 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7053 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 7054 | |
| 7055 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 7056 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 7057 | CodeGen::CodeGenModule &CGM) const { |
| 7058 | SmallStringEnc Enc; |
| 7059 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 7060 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7061 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 7062 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7063 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 7064 | llvm::NamedMDNode *MD = |
| 7065 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 7066 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 7067 | } |
| 7068 | } |
| 7069 | |
| 7070 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7071 | const CodeGen::CodeGenModule &CGM, |
| 7072 | TypeStringCache &TSC); |
| 7073 | |
| 7074 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7075 | /// Builds a SmallVector containing the encoded field types in declaration |
| 7076 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7077 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 7078 | const RecordDecl *RD, |
| 7079 | const CodeGen::CodeGenModule &CGM, |
| 7080 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7081 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7082 | SmallStringEnc Enc; |
| 7083 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7084 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7085 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7086 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7087 | Enc += "b("; |
| 7088 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7089 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7090 | Enc += ':'; |
| 7091 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7092 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7093 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7094 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7095 | Enc += ')'; |
| 7096 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 7097 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7098 | } |
| 7099 | return true; |
| 7100 | } |
| 7101 | |
| 7102 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 7103 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 7104 | /// Union types have their fields ordered according to the ABI. |
| 7105 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 7106 | const CodeGen::CodeGenModule &CGM, |
| 7107 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 7108 | // Append the cached TypeString if we have one. |
| 7109 | StringRef TypeString = TSC.lookupStr(ID); |
| 7110 | if (!TypeString.empty()) { |
| 7111 | Enc += TypeString; |
| 7112 | return true; |
| 7113 | } |
| 7114 | |
| 7115 | // Start to emit an incomplete TypeString. |
| 7116 | size_t Start = Enc.size(); |
| 7117 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 7118 | Enc += '('; |
| 7119 | if (ID) |
| 7120 | Enc += ID->getName(); |
| 7121 | Enc += "){"; |
| 7122 | |
| 7123 | // We collect all encoded fields and order as necessary. |
| 7124 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7125 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 7126 | if (RD && !RD->field_empty()) { |
| 7127 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 7128 | // so that recursive calls to this RecordType will use it whilst building a |
| 7129 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7130 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7131 | std::string StubEnc(Enc.substr(Start).str()); |
| 7132 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7133 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7134 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7135 | (void) TSC.removeIncomplete(ID); |
| 7136 | return false; |
| 7137 | } |
| 7138 | IsRecursive = TSC.removeIncomplete(ID); |
| 7139 | // The ABI requires unions to be sorted but not structures. |
| 7140 | // See FieldEncoding::operator< for sort algorithm. |
| 7141 | if (RT->isUnionType()) |
| 7142 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7143 | // We can now complete the TypeString. |
| 7144 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7145 | for (unsigned I = 0; I != E; ++I) { |
| 7146 | if (I) |
| 7147 | Enc += ','; |
| 7148 | Enc += FE[I].str(); |
| 7149 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7150 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7151 | Enc += '}'; |
| 7152 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7153 | return true; |
| 7154 | } |
| 7155 | |
| 7156 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7157 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7158 | TypeStringCache &TSC, |
| 7159 | const IdentifierInfo *ID) { |
| 7160 | // Append the cached TypeString if we have one. |
| 7161 | StringRef TypeString = TSC.lookupStr(ID); |
| 7162 | if (!TypeString.empty()) { |
| 7163 | Enc += TypeString; |
| 7164 | return true; |
| 7165 | } |
| 7166 | |
| 7167 | size_t Start = Enc.size(); |
| 7168 | Enc += "e("; |
| 7169 | if (ID) |
| 7170 | Enc += ID->getName(); |
| 7171 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7172 | |
| 7173 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7174 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7175 | SmallVector<FieldEncoding, 16> FE; |
| 7176 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7177 | ++I) { |
| 7178 | SmallStringEnc EnumEnc; |
| 7179 | EnumEnc += "m("; |
| 7180 | EnumEnc += I->getName(); |
| 7181 | EnumEnc += "){"; |
| 7182 | I->getInitVal().toString(EnumEnc); |
| 7183 | EnumEnc += '}'; |
| 7184 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7185 | } |
| 7186 | std::sort(FE.begin(), FE.end()); |
| 7187 | unsigned E = FE.size(); |
| 7188 | for (unsigned I = 0; I != E; ++I) { |
| 7189 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7190 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7191 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7192 | } |
| 7193 | } |
| 7194 | Enc += '}'; |
| 7195 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7196 | return true; |
| 7197 | } |
| 7198 | |
| 7199 | /// Appends type's qualifier to Enc. |
| 7200 | /// This is done prior to appending the type's encoding. |
| 7201 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7202 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 7203 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7204 | int Lookup = 0; |
| 7205 | if (QT.isConstQualified()) |
| 7206 | Lookup += 1<<0; |
| 7207 | if (QT.isRestrictQualified()) |
| 7208 | Lookup += 1<<1; |
| 7209 | if (QT.isVolatileQualified()) |
| 7210 | Lookup += 1<<2; |
| 7211 | Enc += Table[Lookup]; |
| 7212 | } |
| 7213 | |
| 7214 | /// Appends built-in types to Enc. |
| 7215 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7216 | const char *EncType; |
| 7217 | switch (BT->getKind()) { |
| 7218 | case BuiltinType::Void: |
| 7219 | EncType = "0"; |
| 7220 | break; |
| 7221 | case BuiltinType::Bool: |
| 7222 | EncType = "b"; |
| 7223 | break; |
| 7224 | case BuiltinType::Char_U: |
| 7225 | EncType = "uc"; |
| 7226 | break; |
| 7227 | case BuiltinType::UChar: |
| 7228 | EncType = "uc"; |
| 7229 | break; |
| 7230 | case BuiltinType::SChar: |
| 7231 | EncType = "sc"; |
| 7232 | break; |
| 7233 | case BuiltinType::UShort: |
| 7234 | EncType = "us"; |
| 7235 | break; |
| 7236 | case BuiltinType::Short: |
| 7237 | EncType = "ss"; |
| 7238 | break; |
| 7239 | case BuiltinType::UInt: |
| 7240 | EncType = "ui"; |
| 7241 | break; |
| 7242 | case BuiltinType::Int: |
| 7243 | EncType = "si"; |
| 7244 | break; |
| 7245 | case BuiltinType::ULong: |
| 7246 | EncType = "ul"; |
| 7247 | break; |
| 7248 | case BuiltinType::Long: |
| 7249 | EncType = "sl"; |
| 7250 | break; |
| 7251 | case BuiltinType::ULongLong: |
| 7252 | EncType = "ull"; |
| 7253 | break; |
| 7254 | case BuiltinType::LongLong: |
| 7255 | EncType = "sll"; |
| 7256 | break; |
| 7257 | case BuiltinType::Float: |
| 7258 | EncType = "ft"; |
| 7259 | break; |
| 7260 | case BuiltinType::Double: |
| 7261 | EncType = "d"; |
| 7262 | break; |
| 7263 | case BuiltinType::LongDouble: |
| 7264 | EncType = "ld"; |
| 7265 | break; |
| 7266 | default: |
| 7267 | return false; |
| 7268 | } |
| 7269 | Enc += EncType; |
| 7270 | return true; |
| 7271 | } |
| 7272 | |
| 7273 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7274 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7275 | const CodeGen::CodeGenModule &CGM, |
| 7276 | TypeStringCache &TSC) { |
| 7277 | Enc += "p("; |
| 7278 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7279 | return false; |
| 7280 | Enc += ')'; |
| 7281 | return true; |
| 7282 | } |
| 7283 | |
| 7284 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7285 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7286 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7287 | const CodeGen::CodeGenModule &CGM, |
| 7288 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7289 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7290 | return false; |
| 7291 | Enc += "a("; |
| 7292 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7293 | CAT->getSize().toStringUnsigned(Enc); |
| 7294 | else |
| 7295 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7296 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7297 | // The Qualifiers should be attached to the type rather than the array. |
| 7298 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7299 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7300 | return false; |
| 7301 | Enc += ')'; |
| 7302 | return true; |
| 7303 | } |
| 7304 | |
| 7305 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7306 | /// and the arguments. |
| 7307 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7308 | const CodeGen::CodeGenModule &CGM, |
| 7309 | TypeStringCache &TSC) { |
| 7310 | Enc += "f{"; |
| 7311 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7312 | return false; |
| 7313 | Enc += "}("; |
| 7314 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7315 | // N.B. we are only interested in the adjusted param types. |
| 7316 | auto I = FPT->param_type_begin(); |
| 7317 | auto E = FPT->param_type_end(); |
| 7318 | if (I != E) { |
| 7319 | do { |
| 7320 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7321 | return false; |
| 7322 | ++I; |
| 7323 | if (I != E) |
| 7324 | Enc += ','; |
| 7325 | } while (I != E); |
| 7326 | if (FPT->isVariadic()) |
| 7327 | Enc += ",va"; |
| 7328 | } else { |
| 7329 | if (FPT->isVariadic()) |
| 7330 | Enc += "va"; |
| 7331 | else |
| 7332 | Enc += '0'; |
| 7333 | } |
| 7334 | } |
| 7335 | Enc += ')'; |
| 7336 | return true; |
| 7337 | } |
| 7338 | |
| 7339 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7340 | /// type encodings. |
| 7341 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7342 | const CodeGen::CodeGenModule &CGM, |
| 7343 | TypeStringCache &TSC) { |
| 7344 | |
| 7345 | QualType QT = QType.getCanonicalType(); |
| 7346 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7347 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7348 | // The Qualifiers should be attached to the type rather than the array. |
| 7349 | // Thus we don't call appendQualifier() here. |
| 7350 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7351 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7352 | appendQualifier(Enc, QT); |
| 7353 | |
| 7354 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7355 | return appendBuiltinType(Enc, BT); |
| 7356 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7357 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7358 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7359 | |
| 7360 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7361 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7362 | |
| 7363 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7364 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7365 | |
| 7366 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7367 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7368 | |
| 7369 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7370 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7371 | |
| 7372 | return false; |
| 7373 | } |
| 7374 | |
| 7375 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7376 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7377 | if (!D) |
| 7378 | return false; |
| 7379 | |
| 7380 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7381 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7382 | return false; |
| 7383 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7384 | } |
| 7385 | |
| 7386 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7387 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7388 | return false; |
| 7389 | QualType QT = VD->getType().getCanonicalType(); |
| 7390 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7391 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7392 | // The Qualifiers should be attached to the type rather than the array. |
| 7393 | // Thus we don't call appendQualifier() here. |
| 7394 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7395 | } |
| 7396 | return appendType(Enc, QT, CGM, TSC); |
| 7397 | } |
| 7398 | return false; |
| 7399 | } |
| 7400 | |
| 7401 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7402 | //===----------------------------------------------------------------------===// |
| 7403 | // Driver code |
| 7404 | //===----------------------------------------------------------------------===// |
| 7405 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7406 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7407 | return getTarget().getTriple(); |
| 7408 | } |
| 7409 | |
| 7410 | bool CodeGenModule::supportsCOMDAT() const { |
| 7411 | return !getTriple().isOSBinFormatMachO(); |
| 7412 | } |
| 7413 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7414 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7415 | if (TheTargetCodeGenInfo) |
| 7416 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7417 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7418 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7419 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7420 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7421 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7422 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7423 | case llvm::Triple::le32: |
| 7424 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7425 | case llvm::Triple::mips: |
| 7426 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 7427 | if (Triple.getOS() == llvm::Triple::NaCl) |
| 7428 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7429 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7430 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7431 | case llvm::Triple::mips64: |
| 7432 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7433 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7434 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7435 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7436 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7437 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7438 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7439 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7440 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7441 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7442 | } |
| 7443 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 7444 | case llvm::Triple::wasm32: |
| 7445 | case llvm::Triple::wasm64: |
| 7446 | return *(TheTargetCodeGenInfo = new WebAssemblyTargetCodeGenInfo(Types)); |
| 7447 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7448 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7449 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7450 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7451 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7452 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7453 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7454 | TheTargetCodeGenInfo = |
| 7455 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7456 | return *TheTargetCodeGenInfo; |
| 7457 | } |
| 7458 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7459 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7460 | StringRef ABIStr = getTarget().getABI(); |
| 7461 | if (ABIStr == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7462 | Kind = ARMABIInfo::APCS; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 7463 | else if (ABIStr == "aapcs16") |
| 7464 | Kind = ARMABIInfo::AAPCS16_VFP; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7465 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7466 | (CodeGenOpts.FloatABI != "soft" && |
| 7467 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7468 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7469 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7470 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7471 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7472 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7473 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7474 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7475 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7476 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7477 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7478 | if (getTarget().getABI() == "elfv2") |
| 7479 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7480 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7481 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7482 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7483 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7484 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7485 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7486 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7487 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7488 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7489 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7490 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7491 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7492 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7493 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7494 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7495 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7496 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7497 | case llvm::Triple::nvptx: |
| 7498 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7499 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7500 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7501 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7502 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7503 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7504 | case llvm::Triple::systemz: { |
| 7505 | bool HasVector = getTarget().getABI() == "vector"; |
| 7506 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7507 | HasVector)); |
| 7508 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7509 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7510 | case llvm::Triple::tce: |
| 7511 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7512 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7513 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7514 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7515 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7516 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7517 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7518 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7519 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7520 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7521 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7522 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7523 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7524 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 7525 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 7526 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
| 7527 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7528 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7529 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7530 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7531 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7532 | StringRef ABI = getTarget().getABI(); |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 7533 | X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 : |
| 7534 | ABI == "avx" ? X86AVXABILevel::AVX : |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7535 | X86AVXABILevel::None); |
| 7536 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7537 | switch (Triple.getOS()) { |
| 7538 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7539 | return *(TheTargetCodeGenInfo = |
| 7540 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7541 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7542 | return *(TheTargetCodeGenInfo = |
| 7543 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7544 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7545 | return *(TheTargetCodeGenInfo = |
| 7546 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7547 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7548 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7549 | case llvm::Triple::hexagon: |
| 7550 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7551 | case llvm::Triple::r600: |
| 7552 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7553 | case llvm::Triple::amdgcn: |
| 7554 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7555 | case llvm::Triple::sparcv9: |
| 7556 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7557 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7558 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7559 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7560 | } |