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 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 69 | ABIInfo::~ABIInfo() {} |
| 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 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 165 | /// Emit va_arg for a platform using the common void* representation, |
| 166 | /// where arguments are simply emitted in an array of slots on the stack. |
| 167 | /// |
| 168 | /// This version implements the core direct-value passing rules. |
| 169 | /// |
| 170 | /// \param SlotSize - The size and alignment of a stack slot. |
| 171 | /// Each argument will be allocated to a multiple of this number of |
| 172 | /// slots, and all the slots will be aligned to this value. |
| 173 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 174 | /// an argument type with an alignment greater than the slot size |
| 175 | /// will be emitted on a higher-alignment address, potentially |
| 176 | /// leaving one or more empty slots behind as padding. If this |
| 177 | /// is false, the returned address might be less-aligned than |
| 178 | /// DirectAlign. |
| 179 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 180 | Address VAListAddr, |
| 181 | llvm::Type *DirectTy, |
| 182 | CharUnits DirectSize, |
| 183 | CharUnits DirectAlign, |
| 184 | CharUnits SlotSize, |
| 185 | bool AllowHigherAlign) { |
| 186 | // Cast the element type to i8* if necessary. Some platforms define |
| 187 | // va_list as a struct containing an i8* instead of just an i8*. |
| 188 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 189 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 190 | |
| 191 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 192 | |
| 193 | // If the CC aligns values higher than the slot size, do so if needed. |
| 194 | Address Addr = Address::invalid(); |
| 195 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
| 196 | llvm::Value *PtrAsInt = Ptr; |
| 197 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 198 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 199 | llvm::ConstantInt::get(CGF.IntPtrTy, DirectAlign.getQuantity() - 1)); |
| 200 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 201 | llvm::ConstantInt::get(CGF.IntPtrTy, -DirectAlign.getQuantity())); |
| 202 | Addr = Address(CGF.Builder.CreateIntToPtr(PtrAsInt, Ptr->getType(), |
| 203 | "argp.cur.aligned"), |
| 204 | DirectAlign); |
| 205 | } else { |
| 206 | Addr = Address(Ptr, SlotSize); |
| 207 | } |
| 208 | |
| 209 | // Advance the pointer past the argument, then store that back. |
| 210 | CharUnits FullDirectSize = DirectSize.RoundUpToAlignment(SlotSize); |
| 211 | llvm::Value *NextPtr = |
| 212 | CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, |
| 213 | "argp.next"); |
| 214 | CGF.Builder.CreateStore(NextPtr, VAListAddr); |
| 215 | |
| 216 | // If the argument is smaller than a slot, and this is a big-endian |
| 217 | // target, the argument will be right-adjusted in its slot. |
| 218 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian()) { |
| 219 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 220 | } |
| 221 | |
| 222 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 223 | return Addr; |
| 224 | } |
| 225 | |
| 226 | /// Emit va_arg for a platform using the common void* representation, |
| 227 | /// where arguments are simply emitted in an array of slots on the stack. |
| 228 | /// |
| 229 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 230 | /// \param ValueInfo - The size and alignment of this type, generally |
| 231 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 232 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 233 | /// Each argument will be allocated to a multiple of this number of |
| 234 | /// slots, and all the slots will be aligned to this value. |
| 235 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 236 | /// an argument type with an alignment greater than the slot size |
| 237 | /// will be emitted on a higher-alignment address, potentially |
| 238 | /// leaving one or more empty slots behind as padding. |
| 239 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 240 | QualType ValueTy, bool IsIndirect, |
| 241 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 242 | CharUnits SlotSizeAndAlign, |
| 243 | bool AllowHigherAlign) { |
| 244 | // The size and alignment of the value that was passed directly. |
| 245 | CharUnits DirectSize, DirectAlign; |
| 246 | if (IsIndirect) { |
| 247 | DirectSize = CGF.getPointerSize(); |
| 248 | DirectAlign = CGF.getPointerAlign(); |
| 249 | } else { |
| 250 | DirectSize = ValueInfo.first; |
| 251 | DirectAlign = ValueInfo.second; |
| 252 | } |
| 253 | |
| 254 | // Cast the address we've calculated to the right type. |
| 255 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 256 | if (IsIndirect) |
| 257 | DirectTy = DirectTy->getPointerTo(0); |
| 258 | |
| 259 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 260 | DirectSize, DirectAlign, |
| 261 | SlotSizeAndAlign, |
| 262 | AllowHigherAlign); |
| 263 | |
| 264 | if (IsIndirect) { |
| 265 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 266 | } |
| 267 | |
| 268 | return Addr; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 273 | Address Addr1, llvm::BasicBlock *Block1, |
| 274 | Address Addr2, llvm::BasicBlock *Block2, |
| 275 | const llvm::Twine &Name = "") { |
| 276 | assert(Addr1.getType() == Addr2.getType()); |
| 277 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 278 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 279 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 280 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 281 | return Address(PHI, Align); |
| 282 | } |
| 283 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 284 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 285 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 286 | // If someone can figure out a general rule for this, that would be great. |
| 287 | // It's probably just doomed to be platform-dependent, though. |
| 288 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 289 | // Verified for: |
| 290 | // x86-64 FreeBSD, Linux, Darwin |
| 291 | // x86-32 FreeBSD, Linux, Darwin |
| 292 | // PowerPC Linux, Darwin |
| 293 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 294 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 295 | return 32; |
| 296 | } |
| 297 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 298 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 299 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 300 | // The following conventions are known to require this to be false: |
| 301 | // x86_stdcall |
| 302 | // MIPS |
| 303 | // For everything else, we just prefer false unless we opt out. |
| 304 | return false; |
| 305 | } |
| 306 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 307 | void |
| 308 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 309 | llvm::SmallString<24> &Opt) const { |
| 310 | // This assumes the user is passing a library name like "rt" instead of a |
| 311 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 312 | // dynamic. |
| 313 | Opt = "-l"; |
| 314 | Opt += Lib; |
| 315 | } |
| 316 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 317 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 318 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 319 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 320 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 321 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 322 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 323 | if (FD->isUnnamedBitfield()) |
| 324 | return true; |
| 325 | |
| 326 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 327 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 328 | // Constant arrays of empty records count as empty, strip them off. |
| 329 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 330 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 331 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 332 | if (AT->getSize() == 0) |
| 333 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 334 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 335 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 336 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 337 | const RecordType *RT = FT->getAs<RecordType>(); |
| 338 | if (!RT) |
| 339 | return false; |
| 340 | |
| 341 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 342 | // |
| 343 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 344 | // current ABI. |
| 345 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 346 | return false; |
| 347 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 348 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 351 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 352 | /// fields. Note that a structure with a flexible array member is not |
| 353 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 354 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 355 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 356 | if (!RT) |
| 357 | return 0; |
| 358 | const RecordDecl *RD = RT->getDecl(); |
| 359 | if (RD->hasFlexibleArrayMember()) |
| 360 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 361 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 362 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 363 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 364 | for (const auto &I : CXXRD->bases()) |
| 365 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 366 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 367 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 368 | for (const auto *I : RD->fields()) |
| 369 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 370 | return false; |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | /// isSingleElementStruct - Determine if a structure is a "single |
| 375 | /// element struct", i.e. it has exactly one non-empty field or |
| 376 | /// exactly one field which is itself a single element |
| 377 | /// struct. Structures with flexible array members are never |
| 378 | /// considered single element structs. |
| 379 | /// |
| 380 | /// \return The field declaration for the single non-empty field, if |
| 381 | /// it exists. |
| 382 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 383 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 384 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 385 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 386 | |
| 387 | const RecordDecl *RD = RT->getDecl(); |
| 388 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 389 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 390 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 391 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 392 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 393 | // If this is a C++ record, check the bases first. |
| 394 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 395 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 396 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 397 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 398 | continue; |
| 399 | |
| 400 | // If we already found an element then this isn't a single-element struct. |
| 401 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 402 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 403 | |
| 404 | // If this is non-empty and not a single element struct, the composite |
| 405 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 406 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 407 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 408 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 413 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 414 | QualType FT = FD->getType(); |
| 415 | |
| 416 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 417 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 418 | continue; |
| 419 | |
| 420 | // If we already found an element then this isn't a single-element |
| 421 | // struct. |
| 422 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 423 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 424 | |
| 425 | // Treat single element arrays as the element. |
| 426 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 427 | if (AT->getSize().getZExtValue() != 1) |
| 428 | break; |
| 429 | FT = AT->getElementType(); |
| 430 | } |
| 431 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 432 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 433 | Found = FT.getTypePtr(); |
| 434 | } else { |
| 435 | Found = isSingleElementStruct(FT, Context); |
| 436 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 437 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 441 | // We don't consider a struct a single-element struct if it has |
| 442 | // padding beyond the element type. |
| 443 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 444 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 445 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 446 | return Found; |
| 447 | } |
| 448 | |
| 449 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 450 | // Treat complex types as the element type. |
| 451 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 452 | Ty = CTy->getElementType(); |
| 453 | |
| 454 | // Check for a type which we know has a simple scalar argument-passing |
| 455 | // convention without any padding. (We're specifically looking for 32 |
| 456 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 457 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 458 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 459 | return false; |
| 460 | |
| 461 | uint64_t Size = Context.getTypeSize(Ty); |
| 462 | return Size == 32 || Size == 64; |
| 463 | } |
| 464 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 465 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 466 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 467 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 468 | /// inhibiting optimizations. |
| 469 | /// |
| 470 | // FIXME: This predicate is missing many cases, currently it just follows |
| 471 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 472 | // should probably make this smarter, or better yet make the LLVM backend |
| 473 | // capable of handling it. |
| 474 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 475 | // We can only expand structure types. |
| 476 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 477 | if (!RT) |
| 478 | return false; |
| 479 | |
| 480 | // We can only expand (C) structures. |
| 481 | // |
| 482 | // FIXME: This needs to be generalized to handle classes as well. |
| 483 | const RecordDecl *RD = RT->getDecl(); |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 484 | if (!RD->isStruct()) |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 485 | return false; |
| 486 | |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 487 | // We try to expand CLike CXXRecordDecl. |
| 488 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 489 | if (!CXXRD->isCLike()) |
| 490 | return false; |
| 491 | } |
| 492 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 493 | uint64_t Size = 0; |
| 494 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 495 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 496 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 497 | return false; |
| 498 | |
| 499 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 500 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 501 | // counts as "basic" is more complicated than what we were doing previously. |
| 502 | if (FD->isBitField()) |
| 503 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 504 | |
| 505 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 508 | // Make sure there are not any holes in the struct. |
| 509 | if (Size != Context.getTypeSize(Ty)) |
| 510 | return false; |
| 511 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 512 | return true; |
| 513 | } |
| 514 | |
| 515 | namespace { |
| 516 | /// DefaultABIInfo - The default implementation for ABI specific |
| 517 | /// details. This implementation provides information which results in |
| 518 | /// self-consistent and sensible LLVM IR generation, but does not |
| 519 | /// conform to any particular ABI. |
| 520 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 521 | public: |
| 522 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 524 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 525 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 526 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 527 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 528 | if (!getCXXABI().classifyReturnType(FI)) |
| 529 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 530 | for (auto &I : FI.arguments()) |
| 531 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 534 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 535 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 538 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 539 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 540 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 541 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 544 | Address DefaultABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 545 | QualType Ty) const { |
| 546 | return Address::invalid(); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 549 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 550 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 551 | |
| 552 | if (isAggregateTypeForABI(Ty)) { |
| 553 | // Records with non-trivial destructors/copy-constructors should not be |
| 554 | // passed by value. |
| 555 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 556 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 557 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 558 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 559 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 561 | // Treat an enum type as its underlying type. |
| 562 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 563 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 564 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 565 | return (Ty->isPromotableIntegerType() ? |
| 566 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 569 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 570 | if (RetTy->isVoidType()) |
| 571 | return ABIArgInfo::getIgnore(); |
| 572 | |
| 573 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 574 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 575 | |
| 576 | // Treat an enum type as its underlying type. |
| 577 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 578 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 579 | |
| 580 | return (RetTy->isPromotableIntegerType() ? |
| 581 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 582 | } |
| 583 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 584 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 585 | // WebAssembly ABI Implementation |
| 586 | // |
| 587 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | |
| 590 | class WebAssemblyABIInfo final : public DefaultABIInfo { |
| 591 | public: |
| 592 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
| 593 | : DefaultABIInfo(CGT) {} |
| 594 | |
| 595 | private: |
| 596 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 597 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 598 | |
| 599 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 600 | // non-virtual, but computeInfo is virtual, so we overload that. |
| 601 | void computeInfo(CGFunctionInfo &FI) const override { |
| 602 | if (!getCXXABI().classifyReturnType(FI)) |
| 603 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 604 | for (auto &Arg : FI.arguments()) |
| 605 | Arg.info = classifyArgumentType(Arg.type); |
| 606 | } |
| 607 | }; |
| 608 | |
| 609 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 610 | public: |
| 611 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 612 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
| 613 | }; |
| 614 | |
| 615 | /// \brief Classify argument of given type \p Ty. |
| 616 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 617 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 618 | |
| 619 | if (isAggregateTypeForABI(Ty)) { |
| 620 | // Records with non-trivial destructors/copy-constructors should not be |
| 621 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 622 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 623 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 624 | // Ignore empty structs/unions. |
| 625 | if (isEmptyRecord(getContext(), Ty, true)) |
| 626 | return ABIArgInfo::getIgnore(); |
| 627 | // Lower single-element structs to just pass a regular value. TODO: We |
| 628 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 629 | // though watch out for things like bitfields. |
| 630 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 631 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Otherwise just do the default thing. |
| 635 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 636 | } |
| 637 | |
| 638 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 639 | if (isAggregateTypeForABI(RetTy)) { |
| 640 | // Records with non-trivial destructors/copy-constructors should not be |
| 641 | // returned by value. |
| 642 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 643 | // Ignore empty structs/unions. |
| 644 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 645 | return ABIArgInfo::getIgnore(); |
| 646 | // Lower single-element structs to just return a regular value. TODO: We |
| 647 | // could do reasonable-size multiple-element structs too, using |
| 648 | // ABIArgInfo::getDirect(). |
| 649 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 650 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | // Otherwise just do the default thing. |
| 655 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 656 | } |
| 657 | |
| 658 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 659 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 660 | // |
| 661 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 662 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 663 | //===----------------------------------------------------------------------===// |
| 664 | |
| 665 | class PNaClABIInfo : public ABIInfo { |
| 666 | public: |
| 667 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 668 | |
| 669 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 670 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 671 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 672 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 673 | Address EmitVAArg(CodeGenFunction &CGF, |
| 674 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 678 | public: |
| 679 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 680 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 681 | }; |
| 682 | |
| 683 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 684 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 685 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 686 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 687 | for (auto &I : FI.arguments()) |
| 688 | I.info = classifyArgumentType(I.type); |
| 689 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 690 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 691 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 692 | QualType Ty) const { |
| 693 | return Address::invalid(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 696 | /// \brief Classify argument of given type \p Ty. |
| 697 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 698 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 699 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 700 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 701 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 702 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 703 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 704 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 705 | } else if (Ty->isFloatingType()) { |
| 706 | // Floating-point types don't go inreg. |
| 707 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 708 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 709 | |
| 710 | return (Ty->isPromotableIntegerType() ? |
| 711 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 715 | if (RetTy->isVoidType()) |
| 716 | return ABIArgInfo::getIgnore(); |
| 717 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 718 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 719 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 720 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 721 | |
| 722 | // Treat an enum type as its underlying type. |
| 723 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 724 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 725 | |
| 726 | return (RetTy->isPromotableIntegerType() ? |
| 727 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 728 | } |
| 729 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 730 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 731 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 732 | // 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] | 733 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 734 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 735 | IRType->getScalarSizeInBits() != 64; |
| 736 | } |
| 737 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 738 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 739 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 740 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 741 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 742 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 743 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 744 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 747 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 751 | return Ty; |
| 752 | } |
| 753 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 754 | /// Returns true if this type can be passed in SSE registers with the |
| 755 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 756 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 757 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 758 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 759 | return true; |
| 760 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 761 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 762 | // registers specially. |
| 763 | unsigned VecSize = Context.getTypeSize(VT); |
| 764 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 765 | return true; |
| 766 | } |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 771 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 772 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 773 | return NumMembers <= 4; |
| 774 | } |
| 775 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 776 | //===----------------------------------------------------------------------===// |
| 777 | // X86-32 ABI Implementation |
| 778 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 779 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 780 | /// \brief Similar to llvm::CCState, but for Clang. |
| 781 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 782 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 783 | |
| 784 | unsigned CC; |
| 785 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 786 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 787 | }; |
| 788 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 789 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 790 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 791 | enum Class { |
| 792 | Integer, |
| 793 | Float |
| 794 | }; |
| 795 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 796 | static const unsigned MinABIStackAlignInBytes = 4; |
| 797 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 798 | bool IsDarwinVectorABI; |
| 799 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 800 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 801 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 802 | |
| 803 | static bool isRegisterSize(unsigned Size) { |
| 804 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 805 | } |
| 806 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 807 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 808 | // FIXME: Assumes vectorcall is in use. |
| 809 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 810 | } |
| 811 | |
| 812 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 813 | uint64_t NumMembers) const override { |
| 814 | // FIXME: Assumes vectorcall is in use. |
| 815 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 816 | } |
| 817 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 818 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 819 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 820 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 821 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 822 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 823 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 824 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 825 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 826 | /// \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] | 827 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 828 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 829 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 830 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 831 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 832 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 833 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 834 | /// \brief Rewrite the function info so that all memory arguments use |
| 835 | /// inalloca. |
| 836 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 837 | |
| 838 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 839 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 840 | QualType Type) const; |
| 841 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 842 | public: |
| 843 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 844 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 845 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 846 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 847 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 848 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 849 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 850 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 851 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 852 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 853 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 854 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 855 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 856 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 857 | bool d, bool p, bool w, unsigned r) |
| 858 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 859 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 860 | static bool isStructReturnInRegABI( |
| 861 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 862 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 863 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 864 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 865 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 866 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 867 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 868 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 869 | return 4; |
| 870 | } |
| 871 | |
| 872 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 873 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 874 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 875 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 876 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 877 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 878 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 879 | } |
| 880 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 881 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 882 | std::string &Constraints, |
| 883 | std::vector<llvm::Type *> &ResultRegTypes, |
| 884 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 885 | std::vector<LValue> &ResultRegDests, |
| 886 | std::string &AsmString, |
| 887 | unsigned NumOutputs) const override; |
| 888 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 889 | llvm::Constant * |
| 890 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 891 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 892 | (0x06 << 8) | // .+0x08 |
| 893 | ('F' << 16) | |
| 894 | ('T' << 24); |
| 895 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 896 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 897 | }; |
| 898 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 899 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 900 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 901 | /// Rewrite input constraint references after adding some output constraints. |
| 902 | /// In the case where there is one output and one input and we add one output, |
| 903 | /// we need to replace all operand references greater than or equal to 1: |
| 904 | /// mov $0, $1 |
| 905 | /// mov eax, $1 |
| 906 | /// The result will be: |
| 907 | /// mov $0, $2 |
| 908 | /// mov eax, $2 |
| 909 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 910 | unsigned NumNewOuts, |
| 911 | std::string &AsmString) { |
| 912 | std::string Buf; |
| 913 | llvm::raw_string_ostream OS(Buf); |
| 914 | size_t Pos = 0; |
| 915 | while (Pos < AsmString.size()) { |
| 916 | size_t DollarStart = AsmString.find('$', Pos); |
| 917 | if (DollarStart == std::string::npos) |
| 918 | DollarStart = AsmString.size(); |
| 919 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 920 | if (DollarEnd == std::string::npos) |
| 921 | DollarEnd = AsmString.size(); |
| 922 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 923 | Pos = DollarEnd; |
| 924 | size_t NumDollars = DollarEnd - DollarStart; |
| 925 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 926 | // We have an operand reference. |
| 927 | size_t DigitStart = Pos; |
| 928 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 929 | if (DigitEnd == std::string::npos) |
| 930 | DigitEnd = AsmString.size(); |
| 931 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 932 | unsigned OperandIndex; |
| 933 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 934 | if (OperandIndex >= FirstIn) |
| 935 | OperandIndex += NumNewOuts; |
| 936 | OS << OperandIndex; |
| 937 | } else { |
| 938 | OS << OperandStr; |
| 939 | } |
| 940 | Pos = DigitEnd; |
| 941 | } |
| 942 | } |
| 943 | AsmString = std::move(OS.str()); |
| 944 | } |
| 945 | |
| 946 | /// Add output constraints for EAX:EDX because they are return registers. |
| 947 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 948 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 949 | std::vector<llvm::Type *> &ResultRegTypes, |
| 950 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 951 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 952 | unsigned NumOutputs) const { |
| 953 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 954 | |
| 955 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 956 | // larger. |
| 957 | if (!Constraints.empty()) |
| 958 | Constraints += ','; |
| 959 | if (RetWidth <= 32) { |
| 960 | Constraints += "={eax}"; |
| 961 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 962 | } else { |
| 963 | // Use the 'A' constraint for EAX:EDX. |
| 964 | Constraints += "=A"; |
| 965 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 966 | } |
| 967 | |
| 968 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 969 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 970 | ResultTruncRegTypes.push_back(CoerceTy); |
| 971 | |
| 972 | // Coerce the integer by bitcasting the return slot pointer. |
| 973 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 974 | CoerceTy->getPointerTo())); |
| 975 | ResultRegDests.push_back(ReturnSlot); |
| 976 | |
| 977 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 978 | } |
| 979 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 980 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 981 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 982 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 983 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 984 | uint64_t Size = Context.getTypeSize(Ty); |
| 985 | |
| 986 | // Type must be register sized. |
| 987 | if (!isRegisterSize(Size)) |
| 988 | return false; |
| 989 | |
| 990 | if (Ty->isVectorType()) { |
| 991 | // 64- and 128- bit vectors inside structures are not returned in |
| 992 | // registers. |
| 993 | if (Size == 64 || Size == 128) |
| 994 | return false; |
| 995 | |
| 996 | return true; |
| 997 | } |
| 998 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 999 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1000 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1001 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1002 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1003 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1004 | return true; |
| 1005 | |
| 1006 | // Arrays are treated like records. |
| 1007 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1008 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1009 | |
| 1010 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1011 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1012 | if (!RT) return false; |
| 1013 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1014 | // FIXME: Traverse bases here too. |
| 1015 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1016 | // Structure types are passed in register if all fields would be |
| 1017 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1018 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1019 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1020 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1021 | continue; |
| 1022 | |
| 1023 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1024 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1025 | return false; |
| 1026 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1027 | return true; |
| 1028 | } |
| 1029 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1030 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1031 | // If the return value is indirect, then the hidden argument is consuming one |
| 1032 | // integer register. |
| 1033 | if (State.FreeRegs) { |
| 1034 | --State.FreeRegs; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1035 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1036 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1037 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1040 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1041 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1042 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1043 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1044 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1045 | const Type *Base = nullptr; |
| 1046 | uint64_t NumElts = 0; |
| 1047 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1048 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1049 | // The LLVM struct type for such an aggregate should lower properly. |
| 1050 | return ABIArgInfo::getDirect(); |
| 1051 | } |
| 1052 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1053 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1054 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1055 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1056 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1057 | |
| 1058 | // 128-bit vectors are a special case; they are returned in |
| 1059 | // registers and we need to make sure to pick a type the LLVM |
| 1060 | // backend will like. |
| 1061 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1062 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1063 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1064 | |
| 1065 | // Always return in register if it fits in a general purpose |
| 1066 | // register, or if it is 64 bits and has a single element. |
| 1067 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1068 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1069 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1070 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1071 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1072 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1076 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1077 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1078 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1079 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1080 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1081 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1082 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1083 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1084 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1085 | // If specified, structs and unions are always indirect. |
| 1086 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1087 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1088 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1089 | // Small structures which are register sized are generally returned |
| 1090 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1091 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1092 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1093 | |
| 1094 | // As a special-case, if the struct is a "single-element" struct, and |
| 1095 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1096 | // floating-point register. (MSVC does not apply this special case.) |
| 1097 | // We apply a similar transformation for pointer types to improve the |
| 1098 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1099 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1100 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1101 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1102 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1103 | |
| 1104 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1105 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1106 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1109 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1110 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1112 | // Treat an enum type as its underlying type. |
| 1113 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1114 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1115 | |
| 1116 | return (RetTy->isPromotableIntegerType() ? |
| 1117 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1120 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1121 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1122 | } |
| 1123 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1124 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1125 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1126 | if (!RT) |
| 1127 | return 0; |
| 1128 | const RecordDecl *RD = RT->getDecl(); |
| 1129 | |
| 1130 | // If this is a C++ record, check the bases first. |
| 1131 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1132 | for (const auto &I : CXXRD->bases()) |
| 1133 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1134 | return false; |
| 1135 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1136 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1137 | QualType FT = i->getType(); |
| 1138 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1139 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1140 | return true; |
| 1141 | |
| 1142 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
| 1146 | return false; |
| 1147 | } |
| 1148 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1149 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1150 | unsigned Align) const { |
| 1151 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1152 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1153 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1154 | return 0; // Use default alignment. |
| 1155 | |
| 1156 | // On non-Darwin, the stack type alignment is always 4. |
| 1157 | if (!IsDarwinVectorABI) { |
| 1158 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1159 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1160 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1161 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1162 | // 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] | 1163 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1164 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1165 | return 16; |
| 1166 | |
| 1167 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1170 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1171 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1172 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1173 | if (State.FreeRegs) { |
| 1174 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1175 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1176 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1177 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1178 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1179 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1180 | // Compute the byval alignment. |
| 1181 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1182 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1183 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1184 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1185 | |
| 1186 | // If the stack alignment is less than the type alignment, realign the |
| 1187 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1188 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1189 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1190 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1193 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1194 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1195 | if (!T) |
| 1196 | T = Ty.getTypePtr(); |
| 1197 | |
| 1198 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1199 | BuiltinType::Kind K = BT->getKind(); |
| 1200 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1201 | return Float; |
| 1202 | } |
| 1203 | return Integer; |
| 1204 | } |
| 1205 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1206 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 1207 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1208 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1209 | Class C = classify(Ty); |
| 1210 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1211 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1212 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1213 | unsigned Size = getContext().getTypeSize(Ty); |
| 1214 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1215 | |
| 1216 | if (SizeInRegs == 0) |
| 1217 | return false; |
| 1218 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1219 | if (SizeInRegs > State.FreeRegs) { |
| 1220 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1221 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1222 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1223 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1224 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1225 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1226 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1227 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1228 | if (Size > 32) |
| 1229 | return false; |
| 1230 | |
| 1231 | if (Ty->isIntegralOrEnumerationType()) |
| 1232 | return true; |
| 1233 | |
| 1234 | if (Ty->isPointerType()) |
| 1235 | return true; |
| 1236 | |
| 1237 | if (Ty->isReferenceType()) |
| 1238 | return true; |
| 1239 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1240 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1241 | NeedsPadding = true; |
| 1242 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1243 | return false; |
| 1244 | } |
| 1245 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1246 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1249 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1250 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1251 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1252 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1253 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1254 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1255 | // Check with the C++ ABI first. |
| 1256 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1257 | if (RT) { |
| 1258 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1259 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1260 | return getIndirectResult(Ty, false, State); |
| 1261 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1262 | // The field index doesn't matter, we'll fix it up later. |
| 1263 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1268 | // to other targets. |
| 1269 | const Type *Base = nullptr; |
| 1270 | uint64_t NumElts = 0; |
| 1271 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1272 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1273 | if (State.FreeSSERegs >= NumElts) { |
| 1274 | State.FreeSSERegs -= NumElts; |
| 1275 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1276 | return ABIArgInfo::getDirect(); |
| 1277 | return ABIArgInfo::getExpand(); |
| 1278 | } |
| 1279 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1280 | } |
| 1281 | |
| 1282 | if (isAggregateTypeForABI(Ty)) { |
| 1283 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1284 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1285 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1286 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1287 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1288 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1289 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1290 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1291 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1292 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1293 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1294 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1295 | return ABIArgInfo::getIgnore(); |
| 1296 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1297 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1298 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 1299 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1300 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1301 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1302 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1303 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 1304 | return ABIArgInfo::getDirectInReg(Result); |
| 1305 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1306 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1307 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1308 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1309 | // of those arguments will match the struct. This is important because the |
| 1310 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1311 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1312 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 1313 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1314 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1315 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1316 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1317 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1318 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1319 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1322 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1323 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1324 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1325 | if (IsDarwinVectorABI) { |
| 1326 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1327 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1328 | (Size == 64 && VT->getNumElements() == 1)) |
| 1329 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1330 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1331 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1332 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1333 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1334 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1336 | return ABIArgInfo::getDirect(); |
| 1337 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1338 | |
| 1339 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1340 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1341 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1342 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1343 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1344 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1345 | |
| 1346 | if (Ty->isPromotableIntegerType()) { |
| 1347 | if (InReg) |
| 1348 | return ABIArgInfo::getExtendInReg(); |
| 1349 | return ABIArgInfo::getExtend(); |
| 1350 | } |
| 1351 | if (InReg) |
| 1352 | return ABIArgInfo::getDirectInReg(); |
| 1353 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1356 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1357 | CCState State(FI.getCallingConvention()); |
| 1358 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1359 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1360 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1361 | State.FreeRegs = 2; |
| 1362 | State.FreeSSERegs = 6; |
| 1363 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1364 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1365 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1366 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1367 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1368 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1369 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1370 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1371 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1372 | // return value was sret and put it in a register ourselves if appropriate. |
| 1373 | if (State.FreeRegs) { |
| 1374 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1375 | FI.getReturnInfo().setInReg(true); |
| 1376 | } |
| 1377 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1378 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1379 | // The chain argument effectively gives us another free register. |
| 1380 | if (FI.isChainCall()) |
| 1381 | ++State.FreeRegs; |
| 1382 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1383 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1384 | for (auto &I : FI.arguments()) { |
| 1385 | I.info = classifyArgumentType(I.type, State); |
| 1386 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1390 | // all the memory arguments to use inalloca. |
| 1391 | if (UsedInAlloca) |
| 1392 | rewriteWithInAlloca(FI); |
| 1393 | } |
| 1394 | |
| 1395 | void |
| 1396 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1397 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1398 | QualType Type) const { |
| 1399 | // Arguments are always 4-byte-aligned. |
| 1400 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1401 | |
| 1402 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1403 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1404 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1405 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1406 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1407 | // Insert padding bytes to respect alignment. |
| 1408 | CharUnits FieldEnd = StackOffset; |
| 1409 | StackOffset = FieldEnd.RoundUpToAlignment(FieldAlign); |
| 1410 | if (StackOffset != FieldEnd) { |
| 1411 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1412 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1413 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1414 | FrameFields.push_back(Ty); |
| 1415 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1418 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1419 | // Leave ignored and inreg arguments alone. |
| 1420 | switch (Info.getKind()) { |
| 1421 | case ABIArgInfo::InAlloca: |
| 1422 | return true; |
| 1423 | case ABIArgInfo::Indirect: |
| 1424 | assert(Info.getIndirectByVal()); |
| 1425 | return true; |
| 1426 | case ABIArgInfo::Ignore: |
| 1427 | return false; |
| 1428 | case ABIArgInfo::Direct: |
| 1429 | case ABIArgInfo::Extend: |
| 1430 | case ABIArgInfo::Expand: |
| 1431 | if (Info.getInReg()) |
| 1432 | return false; |
| 1433 | return true; |
| 1434 | } |
| 1435 | llvm_unreachable("invalid enum"); |
| 1436 | } |
| 1437 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1438 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1439 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1440 | |
| 1441 | // Build a packed struct type for all of the arguments in memory. |
| 1442 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1443 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1444 | // The stack alignment is always 4. |
| 1445 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1446 | |
| 1447 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1448 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1449 | |
| 1450 | // Put 'this' into the struct before 'sret', if necessary. |
| 1451 | bool IsThisCall = |
| 1452 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1453 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1454 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1455 | isArgInAlloca(I->info)) { |
| 1456 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1457 | ++I; |
| 1458 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1459 | |
| 1460 | // 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] | 1461 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1462 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1463 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1464 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1465 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1469 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1470 | ++I; |
| 1471 | |
| 1472 | // Put arguments passed in memory into the struct. |
| 1473 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1474 | if (isArgInAlloca(I->info)) |
| 1475 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1479 | /*isPacked=*/true), |
| 1480 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1483 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1484 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1485 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1486 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1487 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1488 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1489 | // |
| 1490 | // Just messing with TypeInfo like this works because we never pass |
| 1491 | // anything indirectly. |
| 1492 | TypeInfo.second = CharUnits::fromQuantity( |
| 1493 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1494 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1495 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1496 | TypeInfo, CharUnits::fromQuantity(4), |
| 1497 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1500 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1501 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1502 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1503 | |
| 1504 | switch (Opts.getStructReturnConvention()) { |
| 1505 | case CodeGenOptions::SRCK_Default: |
| 1506 | break; |
| 1507 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1508 | return false; |
| 1509 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1510 | return true; |
| 1511 | } |
| 1512 | |
| 1513 | if (Triple.isOSDarwin()) |
| 1514 | return true; |
| 1515 | |
| 1516 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1517 | case llvm::Triple::DragonFly: |
| 1518 | case llvm::Triple::FreeBSD: |
| 1519 | case llvm::Triple::OpenBSD: |
| 1520 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1521 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1522 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1523 | default: |
| 1524 | return false; |
| 1525 | } |
| 1526 | } |
| 1527 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1528 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1529 | llvm::GlobalValue *GV, |
| 1530 | CodeGen::CodeGenModule &CGM) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 1531 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1532 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1533 | // Get the LLVM function. |
| 1534 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1535 | |
| 1536 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1537 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1538 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1539 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1540 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1541 | llvm::AttributeSet::FunctionIndex, |
| 1542 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1547 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1548 | CodeGen::CodeGenFunction &CGF, |
| 1549 | llvm::Value *Address) const { |
| 1550 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1552 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1553 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1554 | // 0-7 are the eight integer registers; the order is different |
| 1555 | // on Darwin (for EH), but the range is the same. |
| 1556 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1557 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1558 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1559 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1560 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1561 | // These have size 16, which is sizeof(long double) on |
| 1562 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1563 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1564 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1565 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1566 | } else { |
| 1567 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1568 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1569 | Builder.CreateAlignedStore( |
| 1570 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 1571 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1572 | |
| 1573 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1574 | // These have size 12, which is sizeof(long double) on |
| 1575 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1576 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1577 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1578 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1579 | |
| 1580 | return false; |
| 1581 | } |
| 1582 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1583 | //===----------------------------------------------------------------------===// |
| 1584 | // X86-64 ABI Implementation |
| 1585 | //===----------------------------------------------------------------------===// |
| 1586 | |
| 1587 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1588 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1589 | /// The AVX ABI level for X86 targets. |
| 1590 | enum class X86AVXABILevel { |
| 1591 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1592 | AVX, |
| 1593 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1594 | }; |
| 1595 | |
| 1596 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1597 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1598 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1599 | case X86AVXABILevel::AVX512: |
| 1600 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1601 | case X86AVXABILevel::AVX: |
| 1602 | return 256; |
| 1603 | case X86AVXABILevel::None: |
| 1604 | return 128; |
| 1605 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1606 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1609 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1610 | class X86_64ABIInfo : public ABIInfo { |
| 1611 | enum Class { |
| 1612 | Integer = 0, |
| 1613 | SSE, |
| 1614 | SSEUp, |
| 1615 | X87, |
| 1616 | X87Up, |
| 1617 | ComplexX87, |
| 1618 | NoClass, |
| 1619 | Memory |
| 1620 | }; |
| 1621 | |
| 1622 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1623 | /// |
| 1624 | /// Merge an accumulating classification \arg Accum with a field |
| 1625 | /// classification \arg Field. |
| 1626 | /// |
| 1627 | /// \param Accum - The accumulating classification. This should |
| 1628 | /// always be either NoClass or the result of a previous merge |
| 1629 | /// call. In addition, this should never be Memory (the caller |
| 1630 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1631 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1632 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1633 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1634 | /// |
| 1635 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1636 | /// final MEMORY or SSE classes when necessary. |
| 1637 | /// |
| 1638 | /// \param AggregateSize - The size of the current aggregate in |
| 1639 | /// the classification process. |
| 1640 | /// |
| 1641 | /// \param Lo - The classification for the parts of the type |
| 1642 | /// residing in the low word of the containing object. |
| 1643 | /// |
| 1644 | /// \param Hi - The classification for the parts of the type |
| 1645 | /// residing in the higher words of the containing object. |
| 1646 | /// |
| 1647 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1648 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1649 | /// classify - Determine the x86_64 register classes in which the |
| 1650 | /// given type T should be passed. |
| 1651 | /// |
| 1652 | /// \param Lo - The classification for the parts of the type |
| 1653 | /// residing in the low word of the containing object. |
| 1654 | /// |
| 1655 | /// \param Hi - The classification for the parts of the type |
| 1656 | /// residing in the high word of the containing object. |
| 1657 | /// |
| 1658 | /// \param OffsetBase - The bit offset of this type in the |
| 1659 | /// containing object. Some parameters are classified different |
| 1660 | /// depending on whether they straddle an eightbyte boundary. |
| 1661 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1662 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1663 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1664 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1665 | /// If a word is unused its result will be NoClass; if a type should |
| 1666 | /// be passed in Memory then at least the classification of \arg Lo |
| 1667 | /// will be Memory. |
| 1668 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1669 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1670 | /// |
| 1671 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1672 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1673 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1674 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1675 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1676 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1677 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1678 | unsigned IROffset, QualType SourceTy, |
| 1679 | unsigned SourceOffset) const; |
| 1680 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1681 | unsigned IROffset, QualType SourceTy, |
| 1682 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1683 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1684 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1685 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1686 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1687 | |
| 1688 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1689 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1690 | /// |
| 1691 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1692 | /// available. |
| 1693 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1694 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1695 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1696 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1697 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1698 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1699 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1700 | unsigned &neededSSE, |
| 1701 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1702 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1703 | bool IsIllegalVectorType(QualType Ty) const; |
| 1704 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1705 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1706 | /// unfortunately in ways that were not always consistent with |
| 1707 | /// certain previous compilers. In particular, platforms which |
| 1708 | /// required strict binary compatibility with older versions of GCC |
| 1709 | /// may need to exempt themselves. |
| 1710 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1711 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1714 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1715 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1716 | // 64-bit hardware. |
| 1717 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1718 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1719 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1720 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| 1721 | ABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1722 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1723 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1724 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1725 | bool isPassedUsingAVXType(QualType type) const { |
| 1726 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1727 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1728 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1729 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1730 | if (info.isDirect()) { |
| 1731 | llvm::Type *ty = info.getCoerceToType(); |
| 1732 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1733 | return (vectorTy->getBitWidth() > 128); |
| 1734 | } |
| 1735 | return false; |
| 1736 | } |
| 1737 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1738 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1739 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1740 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1741 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 1742 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1743 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1744 | |
| 1745 | bool has64BitPointers() const { |
| 1746 | return Has64BitPointers; |
| 1747 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1748 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1749 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1750 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1751 | class WinX86_64ABIInfo : public ABIInfo { |
| 1752 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1753 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1754 | bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1755 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1756 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1757 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1758 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1759 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1760 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1761 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1762 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1763 | |
| 1764 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1765 | // FIXME: Assumes vectorcall is in use. |
| 1766 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1767 | } |
| 1768 | |
| 1769 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1770 | uint64_t NumMembers) const override { |
| 1771 | // FIXME: Assumes vectorcall is in use. |
| 1772 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1773 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1774 | }; |
| 1775 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1776 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1777 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1778 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1779 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1780 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1781 | const X86_64ABIInfo &getABIInfo() const { |
| 1782 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1783 | } |
| 1784 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1785 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1786 | return 7; |
| 1787 | } |
| 1788 | |
| 1789 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1790 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1791 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1792 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1793 | // 0-15 are the 16 integer registers. |
| 1794 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1795 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1796 | return false; |
| 1797 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1798 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1799 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1800 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1801 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1802 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1803 | } |
| 1804 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1805 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1806 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1807 | // The default CC on x86-64 sets %al to the number of SSA |
| 1808 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1809 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1810 | // that when AVX types are involved: the ABI explicitly states it is |
| 1811 | // undefined, and it doesn't work in practice because of how the ABI |
| 1812 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1813 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1814 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1815 | for (CallArgList::const_iterator |
| 1816 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1817 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1818 | HasAVXType = true; |
| 1819 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1820 | } |
| 1821 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1822 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1823 | if (!HasAVXType) |
| 1824 | return true; |
| 1825 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1826 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1827 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1830 | llvm::Constant * |
| 1831 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1832 | unsigned Sig; |
| 1833 | if (getABIInfo().has64BitPointers()) |
| 1834 | Sig = (0xeb << 0) | // jmp rel8 |
| 1835 | (0x0a << 8) | // .+0x0c |
| 1836 | ('F' << 16) | |
| 1837 | ('T' << 24); |
| 1838 | else |
| 1839 | Sig = (0xeb << 0) | // jmp rel8 |
| 1840 | (0x06 << 8) | // .+0x08 |
| 1841 | ('F' << 16) | |
| 1842 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1843 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1844 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1845 | }; |
| 1846 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1847 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1848 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1849 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1850 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1851 | |
| 1852 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1853 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1854 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 1855 | // If the argument contains a space, enclose it in quotes. |
| 1856 | if (Lib.find(" ") != StringRef::npos) |
| 1857 | Opt += "\"" + Lib.str() + "\""; |
| 1858 | else |
| 1859 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1860 | } |
| 1861 | }; |
| 1862 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1863 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1864 | // If the argument does not end in .lib, automatically add the suffix. |
| 1865 | // If the argument contains a space, enclose it in quotes. |
| 1866 | // This matches the behavior of MSVC. |
| 1867 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1868 | std::string ArgStr = Quote ? "\"" : ""; |
| 1869 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1870 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1871 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1872 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1873 | return ArgStr; |
| 1874 | } |
| 1875 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1876 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1877 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1878 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1879 | bool d, bool p, bool w, unsigned RegParms) |
| 1880 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1881 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1882 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1883 | CodeGen::CodeGenModule &CGM) const override; |
| 1884 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1885 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1886 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1887 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1888 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1889 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1890 | |
| 1891 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1892 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1893 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1894 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1895 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1896 | }; |
| 1897 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1898 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1899 | llvm::GlobalValue *GV, |
| 1900 | CodeGen::CodeGenModule &CGM) { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 1901 | if (D && isa<FunctionDecl>(D)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1902 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1903 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1904 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1905 | Fn->addFnAttr("stack-probe-size", |
| 1906 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1911 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1912 | llvm::GlobalValue *GV, |
| 1913 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1914 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1915 | |
| 1916 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1917 | } |
| 1918 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1919 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1920 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1921 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1922 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1923 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1924 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1925 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1926 | CodeGen::CodeGenModule &CGM) const override; |
| 1927 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1928 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1929 | return 7; |
| 1930 | } |
| 1931 | |
| 1932 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1933 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1934 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1936 | // 0-15 are the 16 integer registers. |
| 1937 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1938 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1939 | return false; |
| 1940 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1941 | |
| 1942 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1943 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1944 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1945 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1946 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1947 | |
| 1948 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1949 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1950 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1951 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1952 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1953 | }; |
| 1954 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1955 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1956 | llvm::GlobalValue *GV, |
| 1957 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1958 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1959 | |
| 1960 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1961 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1962 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1963 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1964 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1965 | Class &Hi) const { |
| 1966 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1967 | // |
| 1968 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1969 | // memory. |
| 1970 | // |
| 1971 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1972 | // memory. |
| 1973 | // |
| 1974 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1975 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1976 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1977 | // ABI working for processors that don't support the __m256 type. |
| 1978 | // |
| 1979 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1980 | // |
| 1981 | // Some of these are enforced by the merging logic. Others can arise |
| 1982 | // only with unions; for example: |
| 1983 | // union { _Complex double; unsigned; } |
| 1984 | // |
| 1985 | // Note that clauses (b) and (c) were added in 0.98. |
| 1986 | // |
| 1987 | if (Hi == Memory) |
| 1988 | Lo = Memory; |
| 1989 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1990 | Lo = Memory; |
| 1991 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1992 | Lo = Memory; |
| 1993 | if (Hi == SSEUp && Lo != SSE) |
| 1994 | Hi = SSE; |
| 1995 | } |
| 1996 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1997 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1998 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1999 | // classified recursively so that always two fields are |
| 2000 | // considered. The resulting class is calculated according to |
| 2001 | // the classes of the fields in the eightbyte: |
| 2002 | // |
| 2003 | // (a) If both classes are equal, this is the resulting class. |
| 2004 | // |
| 2005 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2006 | // the other class. |
| 2007 | // |
| 2008 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2009 | // class. |
| 2010 | // |
| 2011 | // (d) If one of the classes is INTEGER, the result is the |
| 2012 | // INTEGER. |
| 2013 | // |
| 2014 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2015 | // MEMORY is used as class. |
| 2016 | // |
| 2017 | // (f) Otherwise class SSE is used. |
| 2018 | |
| 2019 | // Accum should never be memory (we should have returned) or |
| 2020 | // ComplexX87 (because this cannot be passed in a structure). |
| 2021 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2022 | "Invalid accumulated classification during merge."); |
| 2023 | if (Accum == Field || Field == NoClass) |
| 2024 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2025 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2026 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2027 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2028 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2029 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2030 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2031 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2032 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2033 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2034 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2037 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2038 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2039 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2040 | // Class pairs with appropriate constructor methods for the various |
| 2041 | // situations. |
| 2042 | |
| 2043 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2044 | // shouldn't be passed in registers for example, so there is no chance they |
| 2045 | // can straddle an eightbyte. Verify & simplify. |
| 2046 | |
| 2047 | Lo = Hi = NoClass; |
| 2048 | |
| 2049 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2050 | Current = Memory; |
| 2051 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2052 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2053 | BuiltinType::Kind k = BT->getKind(); |
| 2054 | |
| 2055 | if (k == BuiltinType::Void) { |
| 2056 | Current = NoClass; |
| 2057 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2058 | Lo = Integer; |
| 2059 | Hi = Integer; |
| 2060 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2061 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2062 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2063 | Current = SSE; |
| 2064 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2065 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2066 | if (LDF == &llvm::APFloat::IEEEquad) { |
| 2067 | Lo = SSE; |
| 2068 | Hi = SSEUp; |
| 2069 | } else if (LDF == &llvm::APFloat::x87DoubleExtended) { |
| 2070 | Lo = X87; |
| 2071 | Hi = X87Up; |
| 2072 | } else if (LDF == &llvm::APFloat::IEEEdouble) { |
| 2073 | Current = SSE; |
| 2074 | } else |
| 2075 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2076 | } |
| 2077 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2078 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2079 | return; |
| 2080 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2081 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2082 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2083 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2084 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2085 | return; |
| 2086 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2087 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2088 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2089 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2090 | return; |
| 2091 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2092 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2093 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2094 | if (Ty->isMemberFunctionPointerType()) { |
| 2095 | if (Has64BitPointers) { |
| 2096 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2097 | // Lo and Hi now. |
| 2098 | Lo = Hi = Integer; |
| 2099 | } else { |
| 2100 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2101 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2102 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2103 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2104 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2105 | Lo = Hi = Integer; |
| 2106 | } else { |
| 2107 | Current = Integer; |
| 2108 | } |
| 2109 | } |
| 2110 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2111 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2112 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2113 | return; |
| 2114 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2115 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2116 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2117 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2118 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2119 | // gcc passes the following as integer: |
| 2120 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2121 | // 2 bytes - <2 x char>, <1 x short> |
| 2122 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2123 | Current = Integer; |
| 2124 | |
| 2125 | // If this type crosses an eightbyte boundary, it should be |
| 2126 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2127 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2128 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2129 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2130 | Hi = Lo; |
| 2131 | } else if (Size == 64) { |
| 2132 | // gcc passes <1 x double> in memory. :( |
| 2133 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 2134 | return; |
| 2135 | |
| 2136 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 2137 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 2138 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2139 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 2140 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2141 | Current = Integer; |
| 2142 | else |
| 2143 | Current = SSE; |
| 2144 | |
| 2145 | // If this type crosses an eightbyte boundary, it should be |
| 2146 | // split. |
| 2147 | if (OffsetBase && OffsetBase != 64) |
| 2148 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2149 | } else if (Size == 128 || |
| 2150 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2151 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2152 | // least significant one belongs to class SSE and all the others to class |
| 2153 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2154 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2155 | // This design isn't correct for 256-bits, but since there're no cases |
| 2156 | // where the upper parts would need to be inspected, avoid adding |
| 2157 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2158 | // |
| 2159 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2160 | // registers if they are "named", i.e. not part of the "..." of a |
| 2161 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2162 | // |
| 2163 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2164 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2165 | Lo = SSE; |
| 2166 | Hi = SSEUp; |
| 2167 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2168 | return; |
| 2169 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2170 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2171 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2172 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2173 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2174 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2175 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2176 | if (Size <= 64) |
| 2177 | Current = Integer; |
| 2178 | else if (Size <= 128) |
| 2179 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2180 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2181 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2182 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2183 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2184 | } else if (ET == getContext().LongDoubleTy) { |
| 2185 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 2186 | if (LDF == &llvm::APFloat::IEEEquad) |
| 2187 | Current = Memory; |
| 2188 | else if (LDF == &llvm::APFloat::x87DoubleExtended) |
| 2189 | Current = ComplexX87; |
| 2190 | else if (LDF == &llvm::APFloat::IEEEdouble) |
| 2191 | Lo = Hi = SSE; |
| 2192 | else |
| 2193 | llvm_unreachable("unexpected long double representation!"); |
| 2194 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2195 | |
| 2196 | // If this complex type crosses an eightbyte boundary then it |
| 2197 | // should be split. |
| 2198 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2199 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2200 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2201 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2202 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2203 | return; |
| 2204 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2205 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2206 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2207 | // Arrays are treated like structures. |
| 2208 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2209 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2210 | |
| 2211 | // 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] | 2212 | // than four eightbytes, ..., it has class MEMORY. |
| 2213 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2214 | return; |
| 2215 | |
| 2216 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2217 | // fields, it has class MEMORY. |
| 2218 | // |
| 2219 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2220 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2221 | return; |
| 2222 | |
| 2223 | // Otherwise implement simplified merge. We could be smarter about |
| 2224 | // this, but it isn't worth it and would be harder to verify. |
| 2225 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2226 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2227 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2228 | |
| 2229 | // The only case a 256-bit wide vector could be used is when the array |
| 2230 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2231 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2232 | if (Size > 128 && EltSize != 256) |
| 2233 | return; |
| 2234 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2235 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2236 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2237 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2238 | Lo = merge(Lo, FieldLo); |
| 2239 | Hi = merge(Hi, FieldHi); |
| 2240 | if (Lo == Memory || Hi == Memory) |
| 2241 | break; |
| 2242 | } |
| 2243 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2244 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2245 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2246 | return; |
| 2247 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2248 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2249 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2250 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2251 | |
| 2252 | // 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] | 2253 | // than four eightbytes, ..., it has class MEMORY. |
| 2254 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2255 | return; |
| 2256 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2257 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2258 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2259 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2260 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2261 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2262 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2263 | const RecordDecl *RD = RT->getDecl(); |
| 2264 | |
| 2265 | // Assume variable sized types are passed in memory. |
| 2266 | if (RD->hasFlexibleArrayMember()) |
| 2267 | return; |
| 2268 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2269 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Reset Lo class, this will be recomputed. |
| 2272 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2273 | |
| 2274 | // If this is a C++ record, classify the bases first. |
| 2275 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2276 | for (const auto &I : CXXRD->bases()) { |
| 2277 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2278 | "Unexpected base class!"); |
| 2279 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2280 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2281 | |
| 2282 | // Classify this field. |
| 2283 | // |
| 2284 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2285 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2286 | // initialized to class NO_CLASS. |
| 2287 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2288 | uint64_t Offset = |
| 2289 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2290 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2291 | Lo = merge(Lo, FieldLo); |
| 2292 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2293 | if (Lo == Memory || Hi == Memory) { |
| 2294 | postMerge(Size, Lo, Hi); |
| 2295 | return; |
| 2296 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2301 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2302 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2303 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2304 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2305 | bool BitField = i->isBitField(); |
| 2306 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2307 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2308 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2309 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2310 | // The only case a 256-bit wide vector could be used is when the struct |
| 2311 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2312 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2313 | // |
| 2314 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2315 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2316 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2317 | return; |
| 2318 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2319 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2320 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2321 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2322 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2323 | return; |
| 2324 | } |
| 2325 | |
| 2326 | // Classify this field. |
| 2327 | // |
| 2328 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2329 | // exceeds a single eightbyte, each is classified |
| 2330 | // separately. Each eightbyte gets initialized to class |
| 2331 | // NO_CLASS. |
| 2332 | Class FieldLo, FieldHi; |
| 2333 | |
| 2334 | // Bit-fields require special handling, they do not force the |
| 2335 | // structure to be passed in memory even if unaligned, and |
| 2336 | // therefore they can straddle an eightbyte. |
| 2337 | if (BitField) { |
| 2338 | // Ignore padding bit-fields. |
| 2339 | if (i->isUnnamedBitfield()) |
| 2340 | continue; |
| 2341 | |
| 2342 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2343 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2344 | |
| 2345 | uint64_t EB_Lo = Offset / 64; |
| 2346 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2347 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2348 | if (EB_Lo) { |
| 2349 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2350 | FieldLo = NoClass; |
| 2351 | FieldHi = Integer; |
| 2352 | } else { |
| 2353 | FieldLo = Integer; |
| 2354 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2355 | } |
| 2356 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2357 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2358 | Lo = merge(Lo, FieldLo); |
| 2359 | Hi = merge(Hi, FieldHi); |
| 2360 | if (Lo == Memory || Hi == Memory) |
| 2361 | break; |
| 2362 | } |
| 2363 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2364 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2368 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2369 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2370 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2371 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2372 | // Treat an enum type as its underlying type. |
| 2373 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2374 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2375 | |
| 2376 | return (Ty->isPromotableIntegerType() ? |
| 2377 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2378 | } |
| 2379 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2380 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2383 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2384 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2385 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2386 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2387 | if (Size <= 64 || Size > LargestVector) |
| 2388 | return true; |
| 2389 | } |
| 2390 | |
| 2391 | return false; |
| 2392 | } |
| 2393 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2394 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2395 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2396 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2397 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2398 | // |
| 2399 | // This assumption is optimistic, as there could be free registers available |
| 2400 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2401 | // the argument in the free register. This does not seem to happen currently, |
| 2402 | // but this code would be much safer if we could mark the argument with |
| 2403 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2404 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2405 | // Treat an enum type as its underlying type. |
| 2406 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2407 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2408 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2409 | return (Ty->isPromotableIntegerType() ? |
| 2410 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2411 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2412 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2413 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2414 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2415 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2416 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2417 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2418 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2419 | |
| 2420 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2421 | // is important for good codegen. |
| 2422 | // |
| 2423 | // We do this by coercing the value into a scalar type which the backend can |
| 2424 | // handle naturally (i.e., without using byval). |
| 2425 | // |
| 2426 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2427 | // free integer registers. Doing this when there are free integer registers |
| 2428 | // would require more care, as we would have to ensure that the coerced value |
| 2429 | // did not claim the unused register. That would require either reording the |
| 2430 | // arguments to the function (so that any subsequent inreg values came first), |
| 2431 | // or only doing this optimization when there were no following arguments that |
| 2432 | // might be inreg. |
| 2433 | // |
| 2434 | // We currently expect it to be rare (particularly in well written code) for |
| 2435 | // arguments to be passed on the stack when there are still free integer |
| 2436 | // registers available (this would typically imply large structs being passed |
| 2437 | // by value), so this seems like a fair tradeoff for now. |
| 2438 | // |
| 2439 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2440 | // attributes. See PR12193. |
| 2441 | if (freeIntRegs == 0) { |
| 2442 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2443 | |
| 2444 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2445 | // type, which will end up on the stack (with alignment 8). |
| 2446 | if (Align == 8 && Size <= 64) |
| 2447 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2448 | Size)); |
| 2449 | } |
| 2450 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2451 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2454 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2455 | /// 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] | 2456 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2457 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2458 | // vectors; strip them off if present. |
| 2459 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2460 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2461 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2462 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2463 | if (isa<llvm::VectorType>(IRType) || |
| 2464 | IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2465 | return IRType; |
| 2466 | |
| 2467 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2468 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2469 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2470 | |
| 2471 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2472 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2473 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2476 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2477 | /// is known to either be off the end of the specified type or being in |
| 2478 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2479 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2480 | /// classification that put one of the two halves in the INTEGER class. |
| 2481 | /// |
| 2482 | /// It is conservatively correct to return false. |
| 2483 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2484 | unsigned EndBit, ASTContext &Context) { |
| 2485 | // If the bytes being queried are off the end of the type, there is no user |
| 2486 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2487 | // types that don't contain interesting padding. |
| 2488 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2489 | if (TySize <= StartBit) |
| 2490 | return true; |
| 2491 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2492 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2493 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2494 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2495 | |
| 2496 | // Check each element to see if the element overlaps with the queried range. |
| 2497 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2498 | // If the element is after the span we care about, then we're done.. |
| 2499 | unsigned EltOffset = i*EltSize; |
| 2500 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2501 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2502 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2503 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2504 | EndBit-EltOffset, Context)) |
| 2505 | return false; |
| 2506 | } |
| 2507 | // If it overlaps no elements, then it is safe to process as padding. |
| 2508 | return true; |
| 2509 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2510 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2511 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2512 | const RecordDecl *RD = RT->getDecl(); |
| 2513 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2514 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2515 | // If this is a C++ record, check the bases first. |
| 2516 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2517 | for (const auto &I : CXXRD->bases()) { |
| 2518 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2519 | "Unexpected base class!"); |
| 2520 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2521 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2523 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2524 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2525 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2526 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2527 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2528 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2529 | EndBit-BaseOffset, Context)) |
| 2530 | return false; |
| 2531 | } |
| 2532 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2533 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2534 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2535 | // this could be sped up a lot by being smarter about queried fields, |
| 2536 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2537 | // much. |
| 2538 | unsigned idx = 0; |
| 2539 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2540 | i != e; ++i, ++idx) { |
| 2541 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2542 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2543 | // If we found a field after the region we care about, then we're done. |
| 2544 | if (FieldOffset >= EndBit) break; |
| 2545 | |
| 2546 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2547 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2548 | Context)) |
| 2549 | return false; |
| 2550 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2551 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2552 | // If nothing in this record overlapped the area of interest, then we're |
| 2553 | // clean. |
| 2554 | return true; |
| 2555 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2556 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2557 | return false; |
| 2558 | } |
| 2559 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2560 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2561 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2562 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2563 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2564 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2565 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2566 | // Base case if we find a float. |
| 2567 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2568 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2569 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2570 | // 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] | 2571 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2572 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2573 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2574 | IROffset -= SL->getElementOffset(Elt); |
| 2575 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2576 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2577 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2578 | // 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] | 2579 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2580 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2581 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2582 | IROffset -= IROffset/EltSize*EltSize; |
| 2583 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2584 | } |
| 2585 | |
| 2586 | return false; |
| 2587 | } |
| 2588 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2589 | |
| 2590 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2591 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2592 | llvm::Type *X86_64ABIInfo:: |
| 2593 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2594 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2595 | // 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] | 2596 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2597 | // structs that contain 3 floats. |
| 2598 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2599 | SourceOffset*8+64, getContext())) |
| 2600 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2601 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2602 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2603 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2604 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2605 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2606 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2607 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2608 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2609 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2610 | } |
| 2611 | |
| 2612 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2613 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2614 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2615 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2616 | /// 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] | 2617 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2618 | /// etc). |
| 2619 | /// |
| 2620 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2621 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2622 | /// the 8-byte value references. PrefType may be null. |
| 2623 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2624 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2625 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2626 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2627 | llvm::Type *X86_64ABIInfo:: |
| 2628 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2629 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2630 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2631 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2632 | if (IROffset == 0) { |
| 2633 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2634 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2635 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2636 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2637 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2638 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2639 | // goodness in the source type is just tail padding. This is allowed to |
| 2640 | // kick in for struct {double,int} on the int, but not on |
| 2641 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2642 | // have to do this analysis on the source type because we can't depend on |
| 2643 | // unions being lowered a specific way etc. |
| 2644 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2645 | IRType->isIntegerTy(32) || |
| 2646 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2647 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2648 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2649 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2650 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2651 | SourceOffset*8+64, getContext())) |
| 2652 | return IRType; |
| 2653 | } |
| 2654 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2655 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2656 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2657 | // 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] | 2658 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2659 | if (IROffset < SL->getSizeInBytes()) { |
| 2660 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2661 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2663 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2664 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2665 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2666 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2667 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2668 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2669 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2670 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2671 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2672 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2673 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2674 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2675 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2676 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2677 | // 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] | 2678 | unsigned TySizeInBytes = |
| 2679 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2680 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2681 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2682 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2683 | // It is always safe to classify this as an integer type up to i64 that |
| 2684 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2685 | return llvm::IntegerType::get(getVMContext(), |
| 2686 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2689 | |
| 2690 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2691 | /// be used as elements of a two register pair to pass or return, return a |
| 2692 | /// first class aggregate to represent them. For example, if the low part of |
| 2693 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2694 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2695 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2696 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2697 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2698 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2699 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2700 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2701 | // the second element at offset 8. Check for this: |
| 2702 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2703 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2704 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2705 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2706 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2707 | // To handle this, we have to increase the size of the low part so that the |
| 2708 | // second element will start at an 8 byte offset. We can't increase the size |
| 2709 | // of the second element because it might make us access off the end of the |
| 2710 | // struct. |
| 2711 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 2712 | // There are usually two sorts of types the ABI generation code can produce |
| 2713 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 2714 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 2715 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2716 | // Promote these to a larger type. |
| 2717 | if (Lo->isFloatTy()) |
| 2718 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2719 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 2720 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 2721 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2722 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2723 | } |
| 2724 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2725 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2726 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2727 | |
| 2728 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2729 | // Verify that the second element is at an 8-byte offset. |
| 2730 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2731 | "Invalid x86-64 argument pair!"); |
| 2732 | return Result; |
| 2733 | } |
| 2734 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2735 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2736 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2737 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2738 | // classification algorithm. |
| 2739 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2740 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2741 | |
| 2742 | // Check some invariants. |
| 2743 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2744 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2745 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2746 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2747 | switch (Lo) { |
| 2748 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2749 | if (Hi == NoClass) |
| 2750 | return ABIArgInfo::getIgnore(); |
| 2751 | // If the low part is just padding, it takes no register, leave ResType |
| 2752 | // null. |
| 2753 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2754 | "Unknown missing lo part"); |
| 2755 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2756 | |
| 2757 | case SSEUp: |
| 2758 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2759 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2760 | |
| 2761 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2762 | // hidden argument. |
| 2763 | case Memory: |
| 2764 | return getIndirectReturnResult(RetTy); |
| 2765 | |
| 2766 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2767 | // available register of the sequence %rax, %rdx is used. |
| 2768 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2769 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2770 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2771 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2772 | // so that the parameter gets the right LLVM IR attributes. |
| 2773 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2774 | // Treat an enum type as its underlying type. |
| 2775 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2776 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2777 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2778 | if (RetTy->isIntegralOrEnumerationType() && |
| 2779 | RetTy->isPromotableIntegerType()) |
| 2780 | return ABIArgInfo::getExtend(); |
| 2781 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2782 | break; |
| 2783 | |
| 2784 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2785 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2786 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2787 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2788 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2789 | |
| 2790 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2791 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2792 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2793 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2794 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2795 | |
| 2796 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2797 | // part of the value is returned in %st0 and the imaginary part in |
| 2798 | // %st1. |
| 2799 | case ComplexX87: |
| 2800 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2801 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2802 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2803 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2804 | break; |
| 2805 | } |
| 2806 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2807 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2808 | switch (Hi) { |
| 2809 | // Memory was handled previously and X87 should |
| 2810 | // never occur as a hi class. |
| 2811 | case Memory: |
| 2812 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2813 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2814 | |
| 2815 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2816 | case NoClass: |
| 2817 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2818 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2819 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2820 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2821 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2822 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2823 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2824 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2825 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2826 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2827 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2828 | break; |
| 2829 | |
| 2830 | // 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] | 2831 | // is passed in the next available eightbyte chunk if the last used |
| 2832 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2833 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2834 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2835 | case SSEUp: |
| 2836 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2837 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2838 | break; |
| 2839 | |
| 2840 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2841 | // returned together with the previous X87 value in %st0. |
| 2842 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2843 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2844 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2845 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2846 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2847 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2848 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2849 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2850 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2851 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2852 | break; |
| 2853 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2854 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2855 | // 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] | 2856 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2857 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2858 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2859 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2860 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2861 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2864 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2865 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2866 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2867 | const |
| 2868 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2869 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2870 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2871 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2872 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2873 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2874 | // Check some invariants. |
| 2875 | // FIXME: Enforce these by construction. |
| 2876 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2877 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2878 | |
| 2879 | neededInt = 0; |
| 2880 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2881 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2882 | switch (Lo) { |
| 2883 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2884 | if (Hi == NoClass) |
| 2885 | return ABIArgInfo::getIgnore(); |
| 2886 | // If the low part is just padding, it takes no register, leave ResType |
| 2887 | // null. |
| 2888 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2889 | "Unknown missing lo part"); |
| 2890 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2891 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2892 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2893 | // on the stack. |
| 2894 | case Memory: |
| 2895 | |
| 2896 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2897 | // COMPLEX_X87, it is passed in memory. |
| 2898 | case X87: |
| 2899 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2900 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2901 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2902 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2903 | |
| 2904 | case SSEUp: |
| 2905 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2906 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2907 | |
| 2908 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2909 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2910 | // and %r9 is used. |
| 2911 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2912 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2913 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2914 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2915 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2916 | |
| 2917 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2918 | // so that the parameter gets the right LLVM IR attributes. |
| 2919 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2920 | // Treat an enum type as its underlying type. |
| 2921 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2922 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2923 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2924 | if (Ty->isIntegralOrEnumerationType() && |
| 2925 | Ty->isPromotableIntegerType()) |
| 2926 | return ABIArgInfo::getExtend(); |
| 2927 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2928 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2929 | break; |
| 2930 | |
| 2931 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2932 | // available SSE register is used, the registers are taken in the |
| 2933 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2934 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2935 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2936 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2937 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2938 | break; |
| 2939 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2940 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2941 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2942 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2943 | switch (Hi) { |
| 2944 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2945 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2946 | // which is passed in memory. |
| 2947 | case Memory: |
| 2948 | case X87: |
| 2949 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2950 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2951 | |
| 2952 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2953 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2954 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2955 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2956 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2957 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2958 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2959 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2960 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2961 | break; |
| 2962 | |
| 2963 | // X87Up generally doesn't occur here (long double is passed in |
| 2964 | // memory), except in situations involving unions. |
| 2965 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2966 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2967 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2968 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2969 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2970 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2971 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2972 | ++neededSSE; |
| 2973 | break; |
| 2974 | |
| 2975 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2976 | // 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] | 2977 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2978 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2979 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2980 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2981 | break; |
| 2982 | } |
| 2983 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2984 | // If a high part was specified, merge it together with the low part. It is |
| 2985 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2986 | // first class struct aggregate with the high and low part: {low, high} |
| 2987 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2988 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2989 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2990 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2993 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2994 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2995 | if (!getCXXABI().classifyReturnType(FI)) |
| 2996 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2997 | |
| 2998 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2999 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3000 | |
| 3001 | // If the return value is indirect, then the hidden argument is consuming one |
| 3002 | // integer register. |
| 3003 | if (FI.getReturnInfo().isIndirect()) |
| 3004 | --freeIntRegs; |
| 3005 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3006 | // The chain argument effectively gives us another free register. |
| 3007 | if (FI.isChainCall()) |
| 3008 | ++freeIntRegs; |
| 3009 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3010 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3011 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3012 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3013 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3014 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3015 | it != ie; ++it, ++ArgNo) { |
| 3016 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3017 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3018 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3019 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3020 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3021 | |
| 3022 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3023 | // eightbyte of an argument, the whole argument is passed on the |
| 3024 | // stack. If registers have already been assigned for some |
| 3025 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3026 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3027 | freeIntRegs -= neededInt; |
| 3028 | freeSSERegs -= neededSSE; |
| 3029 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3030 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | } |
| 3034 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3035 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3036 | Address VAListAddr, QualType Ty) { |
| 3037 | Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 3038 | VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3039 | llvm::Value *overflow_arg_area = |
| 3040 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3041 | |
| 3042 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3043 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3044 | // It isn't stated explicitly in the standard, but in practice we use |
| 3045 | // alignment greater than 16 where necessary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3046 | uint64_t Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3047 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3048 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3049 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3050 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3051 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 3052 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3053 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3054 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3055 | overflow_arg_area = |
| 3056 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 3057 | overflow_arg_area->getType(), |
| 3058 | "overflow_arg_area.align"); |
| 3059 | } |
| 3060 | |
| 3061 | // 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] | 3062 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3063 | llvm::Value *Res = |
| 3064 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3065 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3066 | |
| 3067 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3068 | // l->overflow_arg_area + sizeof(type). |
| 3069 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3070 | // an 8 byte boundary. |
| 3071 | |
| 3072 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3073 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3074 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3075 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3076 | "overflow_arg_area.next"); |
| 3077 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3078 | |
| 3079 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3080 | return Address(Res, CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3083 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3084 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3085 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3086 | // struct { |
| 3087 | // i32 gp_offset; |
| 3088 | // i32 fp_offset; |
| 3089 | // i8* overflow_arg_area; |
| 3090 | // i8* reg_save_area; |
| 3091 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3092 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3093 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3094 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3095 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3096 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3097 | |
| 3098 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3099 | // in the registers. If not go to step 7. |
| 3100 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3101 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3102 | |
| 3103 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3104 | // general purpose registers needed to pass type and num_fp to hold |
| 3105 | // the number of floating point registers needed. |
| 3106 | |
| 3107 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3108 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3109 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3110 | // |
| 3111 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3112 | // register save space). |
| 3113 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3114 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3115 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3116 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3117 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3118 | gp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3119 | CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), |
| 3120 | "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3121 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3122 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3123 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3127 | fp_offset_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3128 | CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), |
| 3129 | "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3130 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3131 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3132 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3133 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3134 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3135 | } |
| 3136 | |
| 3137 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3138 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3139 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3140 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3141 | |
| 3142 | // Emit code to load the value if it was passed in registers. |
| 3143 | |
| 3144 | CGF.EmitBlock(InRegBlock); |
| 3145 | |
| 3146 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3147 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3148 | // copying to a temporary location in case the parameter is passed |
| 3149 | // in different register classes or requires an alignment greater |
| 3150 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3151 | // |
| 3152 | // FIXME: This really results in shameful code when we end up needing to |
| 3153 | // collect arguments from different places; often what should result in a |
| 3154 | // simple assembling of a structure from scattered addresses has many more |
| 3155 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3156 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3157 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
| 3158 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), |
| 3159 | "reg_save_area"); |
| 3160 | |
| 3161 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3162 | if (neededInt && neededSSE) { |
| 3163 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3164 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3165 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3166 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3167 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3168 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3169 | llvm::Type *TyLo = ST->getElementType(0); |
| 3170 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3171 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3172 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3173 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3174 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3175 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3176 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3177 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3178 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3179 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3180 | // Copy the first element. |
| 3181 | llvm::Value *V = |
| 3182 | CGF.Builder.CreateDefaultAlignedLoad( |
| 3183 | CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 3184 | CGF.Builder.CreateStore(V, |
| 3185 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3186 | |
| 3187 | // Copy the second element. |
| 3188 | V = CGF.Builder.CreateDefaultAlignedLoad( |
| 3189 | CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 3190 | CharUnits Offset = CharUnits::fromQuantity( |
| 3191 | getDataLayout().getStructLayout(ST)->getElementOffset(1)); |
| 3192 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); |
| 3193 | |
| 3194 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3195 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3196 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3197 | CharUnits::fromQuantity(8)); |
| 3198 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3199 | |
| 3200 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3201 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3202 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3203 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3204 | CharUnits TyAlign = SizeAlign.second; |
| 3205 | |
| 3206 | // Copy into a temporary if the type is more aligned than the |
| 3207 | // register save area. |
| 3208 | if (TyAlign.getQuantity() > 8) { |
| 3209 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3210 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3211 | RegAddr = Tmp; |
| 3212 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3213 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3214 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3215 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3216 | CharUnits::fromQuantity(16)); |
| 3217 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3218 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3219 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3220 | // SSE registers are spaced 16 bytes apart in the register save |
| 3221 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3222 | // The ABI isn't explicit about this, but it seems reasonable |
| 3223 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3224 | // naturally 16-byte aligned and the prologue is expected to store |
| 3225 | // all the SSE registers to the RSA. |
| 3226 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3227 | CharUnits::fromQuantity(16)); |
| 3228 | Address RegAddrHi = |
| 3229 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3230 | CharUnits::fromQuantity(16)); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3231 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3232 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3233 | llvm::Value *V; |
| 3234 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3235 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
| 3236 | V = CGF.Builder.CreateLoad( |
| 3237 | CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); |
| 3238 | CGF.Builder.CreateStore(V, |
| 3239 | CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); |
| 3240 | V = CGF.Builder.CreateLoad( |
| 3241 | CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); |
| 3242 | CGF.Builder.CreateStore(V, |
| 3243 | CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); |
| 3244 | |
| 3245 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3249 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3250 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3251 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3252 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3253 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3254 | gp_offset_p); |
| 3255 | } |
| 3256 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3257 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3258 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3259 | fp_offset_p); |
| 3260 | } |
| 3261 | CGF.EmitBranch(ContBlock); |
| 3262 | |
| 3263 | // Emit code to load the value if it was passed in memory. |
| 3264 | |
| 3265 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3266 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3267 | |
| 3268 | // Return the appropriate result. |
| 3269 | |
| 3270 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3271 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3272 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3273 | return ResAddr; |
| 3274 | } |
| 3275 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3276 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3277 | QualType Ty) const { |
| 3278 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3279 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3280 | CharUnits::fromQuantity(8), |
| 3281 | /*allowHigherAlign*/ false); |
| 3282 | } |
| 3283 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3284 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3285 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3286 | |
| 3287 | if (Ty->isVoidType()) |
| 3288 | return ABIArgInfo::getIgnore(); |
| 3289 | |
| 3290 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3291 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3292 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3293 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3294 | uint64_t Width = Info.Width; |
| 3295 | unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3296 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3297 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3298 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3299 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3300 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3301 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3305 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3306 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3307 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3308 | if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3309 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3310 | Width)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3311 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3312 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3313 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3314 | // other targets. |
| 3315 | const Type *Base = nullptr; |
| 3316 | uint64_t NumElts = 0; |
| 3317 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3318 | if (FreeSSERegs >= NumElts) { |
| 3319 | FreeSSERegs -= NumElts; |
| 3320 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3321 | return ABIArgInfo::getDirect(); |
| 3322 | return ABIArgInfo::getExpand(); |
| 3323 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3324 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align), |
| 3325 | /*ByVal=*/false); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3329 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3330 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3331 | // directly. |
| 3332 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3333 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3334 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3337 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3338 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3339 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3340 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3341 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3342 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3343 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3344 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3347 | // Bool type is always extended to the ABI, other builtin types are not |
| 3348 | // extended. |
| 3349 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3350 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3351 | return ABIArgInfo::getExtend(); |
| 3352 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3353 | return ABIArgInfo::getDirect(); |
| 3354 | } |
| 3355 | |
| 3356 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3357 | bool IsVectorCall = |
| 3358 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3359 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3360 | // We can use up to 4 SSE return registers with vectorcall. |
| 3361 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3362 | if (!getCXXABI().classifyReturnType(FI)) |
| 3363 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3364 | |
| 3365 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3366 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3367 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3368 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3371 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3372 | QualType Ty) const { |
| 3373 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3374 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3375 | CharUnits::fromQuantity(8), |
| 3376 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3377 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3378 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3379 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3380 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3381 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3382 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3383 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3384 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3385 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3386 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3387 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3388 | }; |
| 3389 | |
| 3390 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3391 | public: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3392 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3393 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3394 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3395 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3396 | // This is recovered from gcc output. |
| 3397 | return 1; // r1 is the dedicated stack pointer |
| 3398 | } |
| 3399 | |
| 3400 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3401 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3402 | }; |
| 3403 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3404 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3405 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3406 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 3407 | QualType Ty) const { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3408 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3409 | // TODO: Implement this. For now ignore. |
| 3410 | (void)CTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3411 | return Address::invalid(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3414 | // struct __va_list_tag { |
| 3415 | // unsigned char gpr; |
| 3416 | // unsigned char fpr; |
| 3417 | // unsigned short reserved; |
| 3418 | // void *overflow_arg_area; |
| 3419 | // void *reg_save_area; |
| 3420 | // }; |
| 3421 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3422 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3423 | bool isInt = |
| 3424 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3425 | |
| 3426 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 3427 | // with the argument-lowering code. |
| 3428 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3429 | |
| 3430 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3431 | |
| 3432 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 3433 | Address NumRegsAddr = Address::invalid(); |
| 3434 | if (isInt) { |
| 3435 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); |
| 3436 | } else { |
| 3437 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3438 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3439 | |
| 3440 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 3441 | |
| 3442 | // "Align" the register count when TY is i64. |
| 3443 | if (isI64) { |
| 3444 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 3445 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 3446 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3447 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3448 | llvm::Value *CC = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3449 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(8), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3450 | |
| 3451 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3452 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3453 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3454 | |
| 3455 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3456 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3457 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 3458 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3460 | // Case 1: consume registers. |
| 3461 | Address RegAddr = Address::invalid(); |
| 3462 | { |
| 3463 | CGF.EmitBlock(UsingRegs); |
| 3464 | |
| 3465 | Address RegSaveAreaPtr = |
| 3466 | Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); |
| 3467 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 3468 | CharUnits::fromQuantity(8)); |
| 3469 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 3470 | |
| 3471 | // Floating-point registers start after the general-purpose registers. |
| 3472 | if (!isInt) { |
| 3473 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 3474 | CharUnits::fromQuantity(32)); |
| 3475 | } |
| 3476 | |
| 3477 | // Get the address of the saved value by scaling the number of |
| 3478 | // registers we've used by the number of |
| 3479 | CharUnits RegSize = CharUnits::fromQuantity(isInt ? 4 : 8); |
| 3480 | llvm::Value *RegOffset = |
| 3481 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 3482 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 3483 | RegAddr.getPointer(), RegOffset), |
| 3484 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 3485 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 3486 | |
| 3487 | // Increase the used-register count. |
| 3488 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(isI64 ? 2 : 1)); |
| 3489 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 3490 | |
| 3491 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3492 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3493 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3494 | // Case 2: consume space in the overflow area. |
| 3495 | Address MemAddr = Address::invalid(); |
| 3496 | { |
| 3497 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3498 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3499 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 3500 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 3501 | |
| 3502 | CharUnits Size; |
| 3503 | if (!isIndirect) { |
| 3504 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 3505 | Size = TypeInfo.first.RoundUpToAlignment(OverflowAreaAlign); |
| 3506 | } else { |
| 3507 | Size = CGF.getPointerSize(); |
| 3508 | } |
| 3509 | |
| 3510 | Address OverflowAreaAddr = |
| 3511 | Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); |
| 3512 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr), |
| 3513 | OverflowAreaAlign); |
| 3514 | |
| 3515 | // The current address is the address of the varargs element. |
| 3516 | // FIXME: do we not need to round up to alignment? |
| 3517 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 3518 | |
| 3519 | // Increase the overflow area. |
| 3520 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 3521 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 3522 | CGF.EmitBranch(Cont); |
| 3523 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3524 | |
| 3525 | CGF.EmitBlock(Cont); |
| 3526 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3527 | // Merge the cases with a phi. |
| 3528 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 3529 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3530 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3531 | // Load the pointer if the argument was passed indirectly. |
| 3532 | if (isIndirect) { |
| 3533 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 3534 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | return Result; |
| 3538 | } |
| 3539 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3540 | bool |
| 3541 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3542 | llvm::Value *Address) const { |
| 3543 | // This is calculated from the LLVM and GCC tables and verified |
| 3544 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3545 | |
| 3546 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3547 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3548 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3549 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3550 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3551 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3552 | |
| 3553 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3554 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3555 | |
| 3556 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3557 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3558 | |
| 3559 | // 64-76 are various 4-byte special-purpose registers: |
| 3560 | // 64: mq |
| 3561 | // 65: lr |
| 3562 | // 66: ctr |
| 3563 | // 67: ap |
| 3564 | // 68-75 cr0-7 |
| 3565 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3566 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3567 | |
| 3568 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3569 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3570 | |
| 3571 | // 109: vrsave |
| 3572 | // 110: vscr |
| 3573 | // 111: spe_acc |
| 3574 | // 112: spefscr |
| 3575 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3576 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3577 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3578 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3581 | // PowerPC-64 |
| 3582 | |
| 3583 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3584 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3585 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3586 | public: |
| 3587 | enum ABIKind { |
| 3588 | ELFv1 = 0, |
| 3589 | ELFv2 |
| 3590 | }; |
| 3591 | |
| 3592 | private: |
| 3593 | static const unsigned GPRBits = 64; |
| 3594 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3595 | bool HasQPX; |
| 3596 | |
| 3597 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3598 | // will be passed in a QPX register. |
| 3599 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3600 | if (!HasQPX) |
| 3601 | return false; |
| 3602 | |
| 3603 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3604 | unsigned NumElements = VT->getNumElements(); |
| 3605 | if (NumElements == 1) |
| 3606 | return false; |
| 3607 | |
| 3608 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3609 | if (getContext().getTypeSize(Ty) <= 256) |
| 3610 | return true; |
| 3611 | } else if (VT->getElementType()-> |
| 3612 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3613 | if (getContext().getTypeSize(Ty) <= 128) |
| 3614 | return true; |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | return false; |
| 3619 | } |
| 3620 | |
| 3621 | bool IsQPXVectorTy(QualType Ty) const { |
| 3622 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3623 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3624 | |
| 3625 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3626 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3627 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3628 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3629 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3630 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3631 | |
| 3632 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3633 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3634 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3635 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3636 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3637 | uint64_t Members) const override; |
| 3638 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3639 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3640 | // Example: For aggregate arguments that fit in a register, we could |
| 3641 | // use getDirectInReg (as is done below for structs containing a single |
| 3642 | // floating-point value) to avoid pushing them to memory on function |
| 3643 | // entry. This would require changing the logic in PPCISelLowering |
| 3644 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3645 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3646 | if (!getCXXABI().classifyReturnType(FI)) |
| 3647 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3648 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3649 | // We rely on the default argument classification for the most part. |
| 3650 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3651 | // 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] | 3652 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3653 | if (T) { |
| 3654 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3655 | if (IsQPXVectorTy(T) || |
| 3656 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3657 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3658 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3659 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3660 | continue; |
| 3661 | } |
| 3662 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3663 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3664 | } |
| 3665 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3666 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3667 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3668 | QualType Ty) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3669 | }; |
| 3670 | |
| 3671 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3672 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3673 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3674 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3675 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3676 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3677 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3678 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3679 | // This is recovered from gcc output. |
| 3680 | return 1; // r1 is the dedicated stack pointer |
| 3681 | } |
| 3682 | |
| 3683 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3684 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3685 | }; |
| 3686 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3687 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3688 | public: |
| 3689 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3690 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3691 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3692 | // This is recovered from gcc output. |
| 3693 | return 1; // r1 is the dedicated stack pointer |
| 3694 | } |
| 3695 | |
| 3696 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3697 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3698 | }; |
| 3699 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3700 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3701 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3702 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3703 | // extended to 64 bits. |
| 3704 | bool |
| 3705 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3706 | // Treat an enum type as its underlying type. |
| 3707 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3708 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3709 | |
| 3710 | // Promotable integer types are required to be promoted by the ABI. |
| 3711 | if (Ty->isPromotableIntegerType()) |
| 3712 | return true; |
| 3713 | |
| 3714 | // In addition to the usual promotable integer types, we also need to |
| 3715 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3716 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3717 | switch (BT->getKind()) { |
| 3718 | case BuiltinType::Int: |
| 3719 | case BuiltinType::UInt: |
| 3720 | return true; |
| 3721 | default: |
| 3722 | break; |
| 3723 | } |
| 3724 | |
| 3725 | return false; |
| 3726 | } |
| 3727 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3728 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 3729 | /// higher alignment in the parameter area. Always returns at least 8. |
| 3730 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3731 | // Complex types are passed just like their elements. |
| 3732 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3733 | Ty = CTy->getElementType(); |
| 3734 | |
| 3735 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3736 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3737 | if (IsQPXVectorTy(Ty)) { |
| 3738 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3739 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3740 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3741 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3742 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3743 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3744 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3745 | |
| 3746 | // For single-element float/vector structs, we consider the whole type |
| 3747 | // to have the same alignment requirements as its single element. |
| 3748 | const Type *AlignAsType = nullptr; |
| 3749 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3750 | if (EltType) { |
| 3751 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3752 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3753 | getContext().getTypeSize(EltType) == 128) || |
| 3754 | (BT && BT->isFloatingPoint())) |
| 3755 | AlignAsType = EltType; |
| 3756 | } |
| 3757 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3758 | // Likewise for ELFv2 homogeneous aggregates. |
| 3759 | const Type *Base = nullptr; |
| 3760 | uint64_t Members = 0; |
| 3761 | if (!AlignAsType && Kind == ELFv2 && |
| 3762 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3763 | AlignAsType = Base; |
| 3764 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3765 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3766 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3767 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3768 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3769 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3770 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3771 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3772 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3773 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3774 | |
| 3775 | // Otherwise, we only need alignment for any aggregate type that |
| 3776 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3777 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3778 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3779 | return CharUnits::fromQuantity(32); |
| 3780 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3781 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3782 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3783 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3786 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3787 | /// aggregate. Base is set to the base element type, and Members is set |
| 3788 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3789 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3790 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3791 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3792 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3793 | if (NElements == 0) |
| 3794 | return false; |
| 3795 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3796 | return false; |
| 3797 | Members *= NElements; |
| 3798 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3799 | const RecordDecl *RD = RT->getDecl(); |
| 3800 | if (RD->hasFlexibleArrayMember()) |
| 3801 | return false; |
| 3802 | |
| 3803 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3804 | |
| 3805 | // If this is a C++ record, check the bases first. |
| 3806 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3807 | for (const auto &I : CXXRD->bases()) { |
| 3808 | // Ignore empty records. |
| 3809 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3810 | continue; |
| 3811 | |
| 3812 | uint64_t FldMembers; |
| 3813 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3814 | return false; |
| 3815 | |
| 3816 | Members += FldMembers; |
| 3817 | } |
| 3818 | } |
| 3819 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3820 | for (const auto *FD : RD->fields()) { |
| 3821 | // Ignore (non-zero arrays of) empty records. |
| 3822 | QualType FT = FD->getType(); |
| 3823 | while (const ConstantArrayType *AT = |
| 3824 | getContext().getAsConstantArrayType(FT)) { |
| 3825 | if (AT->getSize().getZExtValue() == 0) |
| 3826 | return false; |
| 3827 | FT = AT->getElementType(); |
| 3828 | } |
| 3829 | if (isEmptyRecord(getContext(), FT, true)) |
| 3830 | continue; |
| 3831 | |
| 3832 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3833 | if (getContext().getLangOpts().CPlusPlus && |
| 3834 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3835 | continue; |
| 3836 | |
| 3837 | uint64_t FldMembers; |
| 3838 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3839 | return false; |
| 3840 | |
| 3841 | Members = (RD->isUnion() ? |
| 3842 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3843 | } |
| 3844 | |
| 3845 | if (!Base) |
| 3846 | return false; |
| 3847 | |
| 3848 | // Ensure there is no padding. |
| 3849 | if (getContext().getTypeSize(Base) * Members != |
| 3850 | getContext().getTypeSize(Ty)) |
| 3851 | return false; |
| 3852 | } else { |
| 3853 | Members = 1; |
| 3854 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3855 | Members = 2; |
| 3856 | Ty = CT->getElementType(); |
| 3857 | } |
| 3858 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3859 | // Most ABIs only support float, double, and some vector type widths. |
| 3860 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3861 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3862 | |
| 3863 | // The base type must be the same for all members. Types that |
| 3864 | // agree in both total size and mode (float vs. vector) are |
| 3865 | // treated as being equivalent here. |
| 3866 | const Type *TyPtr = Ty.getTypePtr(); |
| 3867 | if (!Base) |
| 3868 | Base = TyPtr; |
| 3869 | |
| 3870 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3871 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3872 | return false; |
| 3873 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3874 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3875 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3876 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3877 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3878 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3879 | // double, long double, or 128-bit vectors. |
| 3880 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3881 | if (BT->getKind() == BuiltinType::Float || |
| 3882 | BT->getKind() == BuiltinType::Double || |
| 3883 | BT->getKind() == BuiltinType::LongDouble) |
| 3884 | return true; |
| 3885 | } |
| 3886 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3887 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3888 | return true; |
| 3889 | } |
| 3890 | return false; |
| 3891 | } |
| 3892 | |
| 3893 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3894 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3895 | // Vector types require one register, floating point types require one |
| 3896 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3897 | uint32_t NumRegs = |
| 3898 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3899 | |
| 3900 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3901 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3904 | ABIArgInfo |
| 3905 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3906 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3907 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3908 | if (Ty->isAnyComplexType()) |
| 3909 | return ABIArgInfo::getDirect(); |
| 3910 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3911 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3912 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3913 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3914 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3915 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3916 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3917 | else if (Size < 128) { |
| 3918 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3919 | return ABIArgInfo::getDirect(CoerceTy); |
| 3920 | } |
| 3921 | } |
| 3922 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3923 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3924 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3925 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3926 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3927 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 3928 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3929 | |
| 3930 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3931 | const Type *Base = nullptr; |
| 3932 | uint64_t Members = 0; |
| 3933 | if (Kind == ELFv2 && |
| 3934 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3935 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3936 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3937 | return ABIArgInfo::getDirect(CoerceTy); |
| 3938 | } |
| 3939 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3940 | // If an aggregate may end up fully in registers, we do not |
| 3941 | // use the ByVal method, but pass the aggregate as array. |
| 3942 | // This is usually beneficial since we avoid forcing the |
| 3943 | // back-end to store the argument to memory. |
| 3944 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3945 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3946 | llvm::Type *CoerceTy; |
| 3947 | |
| 3948 | // Types up to 8 bytes are passed as integer type (which will be |
| 3949 | // properly aligned in the argument save area doubleword). |
| 3950 | if (Bits <= GPRBits) |
| 3951 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3952 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3953 | // Larger types are passed as arrays, with the base type selected |
| 3954 | // according to the required alignment in the save area. |
| 3955 | else { |
| 3956 | uint64_t RegBits = ABIAlign * 8; |
| 3957 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3958 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3959 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3960 | } |
| 3961 | |
| 3962 | return ABIArgInfo::getDirect(CoerceTy); |
| 3963 | } |
| 3964 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3965 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3966 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 3967 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3968 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | return (isPromotableTypeForABI(Ty) ? |
| 3972 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3973 | } |
| 3974 | |
| 3975 | ABIArgInfo |
| 3976 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3977 | if (RetTy->isVoidType()) |
| 3978 | return ABIArgInfo::getIgnore(); |
| 3979 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3980 | if (RetTy->isAnyComplexType()) |
| 3981 | return ABIArgInfo::getDirect(); |
| 3982 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3983 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3984 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3985 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3986 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3987 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3988 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3989 | else if (Size < 128) { |
| 3990 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3991 | return ABIArgInfo::getDirect(CoerceTy); |
| 3992 | } |
| 3993 | } |
| 3994 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3995 | if (isAggregateTypeForABI(RetTy)) { |
| 3996 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3997 | const Type *Base = nullptr; |
| 3998 | uint64_t Members = 0; |
| 3999 | if (Kind == ELFv2 && |
| 4000 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4001 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4002 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4003 | return ABIArgInfo::getDirect(CoerceTy); |
| 4004 | } |
| 4005 | |
| 4006 | // ELFv2 small aggregates are returned in up to two registers. |
| 4007 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4008 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4009 | if (Bits == 0) |
| 4010 | return ABIArgInfo::getIgnore(); |
| 4011 | |
| 4012 | llvm::Type *CoerceTy; |
| 4013 | if (Bits > GPRBits) { |
| 4014 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 4015 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4016 | } else |
| 4017 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 4018 | llvm::RoundUpToAlignment(Bits, 8)); |
| 4019 | return ABIArgInfo::getDirect(CoerceTy); |
| 4020 | } |
| 4021 | |
| 4022 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4023 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4024 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4025 | |
| 4026 | return (isPromotableTypeForABI(RetTy) ? |
| 4027 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4028 | } |
| 4029 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4030 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4031 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4032 | QualType Ty) const { |
| 4033 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4034 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4035 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4036 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4037 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4038 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4039 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4040 | // in separate doublewords. However, Clang expects us to produce a |
| 4041 | // pointer to a structure with the two parts packed tightly. So generate |
| 4042 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4043 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4044 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4045 | CharUnits EltSize = TypeInfo.first / 2; |
| 4046 | if (EltSize < SlotSize) { |
| 4047 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4048 | SlotSize * 2, SlotSize, |
| 4049 | SlotSize, /*AllowHigher*/ true); |
| 4050 | |
| 4051 | Address RealAddr = Addr; |
| 4052 | Address ImagAddr = RealAddr; |
| 4053 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4054 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4055 | SlotSize - EltSize); |
| 4056 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4057 | 2 * SlotSize - EltSize); |
| 4058 | } else { |
| 4059 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4060 | } |
| 4061 | |
| 4062 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4063 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4064 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4065 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4066 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4067 | |
| 4068 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4069 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4070 | /*init*/ true); |
| 4071 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4072 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4075 | // Otherwise, just use the general rule. |
| 4076 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4077 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | static bool |
| 4081 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4082 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4083 | // This is calculated from the LLVM and GCC tables and verified |
| 4084 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4085 | |
| 4086 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4087 | |
| 4088 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4089 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4090 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4091 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4092 | |
| 4093 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4094 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4095 | |
| 4096 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4097 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4098 | |
| 4099 | // 64-76 are various 4-byte special-purpose registers: |
| 4100 | // 64: mq |
| 4101 | // 65: lr |
| 4102 | // 66: ctr |
| 4103 | // 67: ap |
| 4104 | // 68-75 cr0-7 |
| 4105 | // 76: xer |
| 4106 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 4107 | |
| 4108 | // 77-108: v0-31, the 16-byte vector registers |
| 4109 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4110 | |
| 4111 | // 109: vrsave |
| 4112 | // 110: vscr |
| 4113 | // 111: spe_acc |
| 4114 | // 112: spefscr |
| 4115 | // 113: sfp |
| 4116 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 4117 | |
| 4118 | return false; |
| 4119 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4120 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4121 | bool |
| 4122 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4123 | CodeGen::CodeGenFunction &CGF, |
| 4124 | llvm::Value *Address) const { |
| 4125 | |
| 4126 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4127 | } |
| 4128 | |
| 4129 | bool |
| 4130 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4131 | llvm::Value *Address) const { |
| 4132 | |
| 4133 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4134 | } |
| 4135 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4136 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4137 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4138 | //===----------------------------------------------------------------------===// |
| 4139 | |
| 4140 | namespace { |
| 4141 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4142 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4143 | public: |
| 4144 | enum ABIKind { |
| 4145 | AAPCS = 0, |
| 4146 | DarwinPCS |
| 4147 | }; |
| 4148 | |
| 4149 | private: |
| 4150 | ABIKind Kind; |
| 4151 | |
| 4152 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4153 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4154 | |
| 4155 | private: |
| 4156 | ABIKind getABIKind() const { return Kind; } |
| 4157 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 4158 | |
| 4159 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4160 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4161 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4162 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4163 | uint64_t Members) const override; |
| 4164 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4165 | bool isIllegalVectorType(QualType Ty) const; |
| 4166 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 4167 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4168 | if (!getCXXABI().classifyReturnType(FI)) |
| 4169 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 4170 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4171 | for (auto &it : FI.arguments()) |
| 4172 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4175 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4176 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4177 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4178 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 4179 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4180 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4181 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4182 | QualType Ty) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4183 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 4184 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 4185 | } |
| 4186 | }; |
| 4187 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4188 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4189 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4190 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 4191 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4192 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4193 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4194 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 4195 | } |
| 4196 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4197 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 4198 | return 31; |
| 4199 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4200 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4201 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4202 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4203 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4204 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4205 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4206 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4207 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4208 | // Handle illegal vector types here. |
| 4209 | if (isIllegalVectorType(Ty)) { |
| 4210 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4211 | if (Size <= 32) { |
| 4212 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4213 | return ABIArgInfo::getDirect(ResType); |
| 4214 | } |
| 4215 | if (Size == 64) { |
| 4216 | llvm::Type *ResType = |
| 4217 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4218 | return ABIArgInfo::getDirect(ResType); |
| 4219 | } |
| 4220 | if (Size == 128) { |
| 4221 | llvm::Type *ResType = |
| 4222 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4223 | return ABIArgInfo::getDirect(ResType); |
| 4224 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4225 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4226 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4227 | |
| 4228 | if (!isAggregateTypeForABI(Ty)) { |
| 4229 | // Treat an enum type as its underlying type. |
| 4230 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4231 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4232 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4233 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4234 | ? ABIArgInfo::getExtend() |
| 4235 | : ABIArgInfo::getDirect()); |
| 4236 | } |
| 4237 | |
| 4238 | // Structures with either a non-trivial destructor or a non-trivial |
| 4239 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4240 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4241 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 4242 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4246 | // elsewhere for GNU compatibility. |
| 4247 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4248 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4249 | return ABIArgInfo::getIgnore(); |
| 4250 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4251 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4252 | } |
| 4253 | |
| 4254 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4255 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4256 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4257 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4258 | return ABIArgInfo::getDirect( |
| 4259 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4263 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4264 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4265 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4266 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4267 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4268 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4269 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4270 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4271 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4272 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4273 | } |
| 4274 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4275 | } |
| 4276 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4277 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4280 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4281 | if (RetTy->isVoidType()) |
| 4282 | return ABIArgInfo::getIgnore(); |
| 4283 | |
| 4284 | // Large vector types should be returned via memory. |
| 4285 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4286 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4287 | |
| 4288 | if (!isAggregateTypeForABI(RetTy)) { |
| 4289 | // Treat an enum type as its underlying type. |
| 4290 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4291 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4292 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4293 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4294 | ? ABIArgInfo::getExtend() |
| 4295 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4298 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4299 | return ABIArgInfo::getIgnore(); |
| 4300 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4301 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4302 | uint64_t Members = 0; |
| 4303 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4304 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4305 | return ABIArgInfo::getDirect(); |
| 4306 | |
| 4307 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4308 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4309 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4310 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4311 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4312 | |
| 4313 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4314 | // For aggregates with 16-byte alignment, we use i128. |
| 4315 | if (Alignment < 128 && Size == 128) { |
| 4316 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4317 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4318 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4319 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4320 | } |
| 4321 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4322 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4325 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4326 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4327 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4328 | // Check whether VT is legal. |
| 4329 | unsigned NumElements = VT->getNumElements(); |
| 4330 | uint64_t Size = getContext().getTypeSize(VT); |
| 4331 | // NumElements should be power of 2 between 1 and 16. |
| 4332 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4333 | return true; |
| 4334 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4335 | } |
| 4336 | return false; |
| 4337 | } |
| 4338 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4339 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4340 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4341 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4342 | // but with the difference that any floating-point type is allowed, |
| 4343 | // including __fp16. |
| 4344 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4345 | if (BT->isFloatingPoint()) |
| 4346 | return true; |
| 4347 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4348 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4349 | if (VecSize == 64 || VecSize == 128) |
| 4350 | return true; |
| 4351 | } |
| 4352 | return false; |
| 4353 | } |
| 4354 | |
| 4355 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4356 | uint64_t Members) const { |
| 4357 | return Members <= 4; |
| 4358 | } |
| 4359 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4360 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4361 | QualType Ty, |
| 4362 | CodeGenFunction &CGF) const { |
| 4363 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4364 | bool IsIndirect = AI.isIndirect(); |
| 4365 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4366 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4367 | if (IsIndirect) |
| 4368 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4369 | else if (AI.getCoerceToType()) |
| 4370 | BaseTy = AI.getCoerceToType(); |
| 4371 | |
| 4372 | unsigned NumRegs = 1; |
| 4373 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4374 | BaseTy = ArrTy->getElementType(); |
| 4375 | NumRegs = ArrTy->getNumElements(); |
| 4376 | } |
| 4377 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4378 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4379 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4380 | // Standard, section B.4: |
| 4381 | // |
| 4382 | // struct { |
| 4383 | // void *__stack; |
| 4384 | // void *__gr_top; |
| 4385 | // void *__vr_top; |
| 4386 | // int __gr_offs; |
| 4387 | // int __vr_offs; |
| 4388 | // }; |
| 4389 | |
| 4390 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4391 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4392 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4393 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4394 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4395 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4396 | CharUnits TyAlign = TyInfo.second; |
| 4397 | |
| 4398 | Address reg_offs_p = Address::invalid(); |
| 4399 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4400 | int reg_top_index; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4401 | CharUnits reg_top_offset; |
| 4402 | int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4403 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4404 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4405 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4406 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 4407 | "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4408 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4409 | reg_top_index = 1; // field number for __gr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4410 | reg_top_offset = CharUnits::fromQuantity(8); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4411 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4412 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4413 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4414 | reg_offs_p = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4415 | CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), |
| 4416 | "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4417 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4418 | reg_top_index = 2; // field number for __vr_top |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4419 | reg_top_offset = CharUnits::fromQuantity(16); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4420 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | //======================================= |
| 4424 | // Find out where argument was passed |
| 4425 | //======================================= |
| 4426 | |
| 4427 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4428 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4429 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4430 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4431 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4432 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4433 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4434 | |
| 4435 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4436 | |
| 4437 | // 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] | 4438 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4439 | CGF.EmitBlock(MaybeRegBlock); |
| 4440 | |
| 4441 | // Integer arguments may need to correct register alignment (for example a |
| 4442 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4443 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4444 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 4445 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4446 | |
| 4447 | reg_offs = CGF.Builder.CreateAdd( |
| 4448 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4449 | "align_regoffs"); |
| 4450 | reg_offs = CGF.Builder.CreateAnd( |
| 4451 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4452 | "aligned_regoffs"); |
| 4453 | } |
| 4454 | |
| 4455 | // 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] | 4456 | // The fact that this is done unconditionally reflects the fact that |
| 4457 | // allocating an argument to the stack also uses up all the remaining |
| 4458 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4459 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4460 | NewOffset = CGF.Builder.CreateAdd( |
| 4461 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4462 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4463 | |
| 4464 | // Now we're in a position to decide whether this argument really was in |
| 4465 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4466 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4467 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4468 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4469 | |
| 4470 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4471 | |
| 4472 | //======================================= |
| 4473 | // Argument was in registers |
| 4474 | //======================================= |
| 4475 | |
| 4476 | // Now we emit the code for if the argument was originally passed in |
| 4477 | // registers. First start the appropriate block: |
| 4478 | CGF.EmitBlock(InRegBlock); |
| 4479 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4480 | llvm::Value *reg_top = nullptr; |
| 4481 | Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, |
| 4482 | reg_top_offset, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4483 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4484 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 4485 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 4486 | Address RegAddr = Address::invalid(); |
| 4487 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4488 | |
| 4489 | if (IsIndirect) { |
| 4490 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4491 | // stored registers or on the stack will actually be a struct **. |
| 4492 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4493 | } |
| 4494 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4495 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4496 | uint64_t NumMembers = 0; |
| 4497 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4498 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4499 | // Homogeneous aggregates passed in registers will have their elements split |
| 4500 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4501 | // qN+1, ...). We reload and store into a temporary local variable |
| 4502 | // contiguously. |
| 4503 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4504 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4505 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4506 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4507 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 4508 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4509 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4510 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 4511 | int Offset = 0; |
| 4512 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4513 | BaseTyInfo.first.getQuantity() < 16) |
| 4514 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 4515 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4516 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4517 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 4518 | Address LoadAddr = |
| 4519 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 4520 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 4521 | |
| 4522 | Address StoreAddr = |
| 4523 | CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4524 | |
| 4525 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4526 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4527 | } |
| 4528 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4529 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4530 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4531 | // Otherwise the object is contiguous in memory. |
| 4532 | |
| 4533 | // It might be right-aligned in its slot. |
| 4534 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 4535 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4536 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4537 | TyInfo.first < SlotSize) { |
| 4538 | CharUnits Offset = SlotSize - TyInfo.first; |
| 4539 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4542 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | CGF.EmitBranch(ContBlock); |
| 4546 | |
| 4547 | //======================================= |
| 4548 | // Argument was on the stack |
| 4549 | //======================================= |
| 4550 | CGF.EmitBlock(OnStackBlock); |
| 4551 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4552 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, |
| 4553 | CharUnits::Zero(), "stack_p"); |
| 4554 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4555 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4556 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4557 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4558 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 4559 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4560 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4561 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4562 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4563 | OnStackPtr = CGF.Builder.CreateAdd( |
| 4564 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4565 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4566 | OnStackPtr = CGF.Builder.CreateAnd( |
| 4567 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4568 | "align_stack"); |
| 4569 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4570 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4571 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4572 | Address OnStackAddr(OnStackPtr, |
| 4573 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4574 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4575 | // All stack slots are multiples of 8 bytes. |
| 4576 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 4577 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4578 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4579 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4580 | else |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4581 | StackSize = TyInfo.first.RoundUpToAlignment(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4582 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4583 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4584 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4585 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4586 | |
| 4587 | // Write the new value of __stack for the next call to va_arg |
| 4588 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4589 | |
| 4590 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4591 | TyInfo.first < StackSlotSize) { |
| 4592 | CharUnits Offset = StackSlotSize - TyInfo.first; |
| 4593 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4596 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4597 | |
| 4598 | CGF.EmitBranch(ContBlock); |
| 4599 | |
| 4600 | //======================================= |
| 4601 | // Tidy up |
| 4602 | //======================================= |
| 4603 | CGF.EmitBlock(ContBlock); |
| 4604 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4605 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 4606 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4607 | |
| 4608 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4609 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
| 4610 | TyInfo.second); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4611 | |
| 4612 | return ResAddr; |
| 4613 | } |
| 4614 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4615 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 4616 | CodeGenFunction &CGF) const { |
| 4617 | // The backend's lowering doesn't support va_arg for aggregates or |
| 4618 | // illegal vector types. Lower VAArg here for these cases and use |
| 4619 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4620 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4621 | return Address::invalid(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4622 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4623 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4624 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4625 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4626 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4627 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 4628 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 4629 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4632 | // The size of the actual thing passed, which might end up just |
| 4633 | // being a pointer for indirect types. |
| 4634 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 4635 | |
| 4636 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 4637 | // aggregates should be passed indirectly. |
| 4638 | bool IsIndirect = false; |
| 4639 | if (TyInfo.first.getQuantity() > 16) { |
| 4640 | const Type *Base = nullptr; |
| 4641 | uint64_t Members = 0; |
| 4642 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4645 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 4646 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4650 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4651 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4652 | |
| 4653 | namespace { |
| 4654 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4655 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4656 | public: |
| 4657 | enum ABIKind { |
| 4658 | APCS = 0, |
| 4659 | AAPCS = 1, |
| 4660 | AAPCS_VFP |
| 4661 | }; |
| 4662 | |
| 4663 | private: |
| 4664 | ABIKind Kind; |
| 4665 | |
| 4666 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4667 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4668 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4669 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4670 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4671 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4672 | switch (getTarget().getTriple().getEnvironment()) { |
| 4673 | case llvm::Triple::Android: |
| 4674 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4675 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4676 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4677 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4678 | return true; |
| 4679 | default: |
| 4680 | return false; |
| 4681 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4684 | bool isEABIHF() const { |
| 4685 | switch (getTarget().getTriple().getEnvironment()) { |
| 4686 | case llvm::Triple::EABIHF: |
| 4687 | case llvm::Triple::GNUEABIHF: |
| 4688 | return true; |
| 4689 | default: |
| 4690 | return false; |
| 4691 | } |
| 4692 | } |
| 4693 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4694 | ABIKind getABIKind() const { return Kind; } |
| 4695 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4696 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4697 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4698 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4699 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4700 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4701 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4702 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4703 | uint64_t Members) const override; |
| 4704 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4705 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4706 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4707 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4708 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4709 | |
| 4710 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4711 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4712 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4713 | }; |
| 4714 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4715 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4716 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4717 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4718 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4719 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4720 | const ARMABIInfo &getABIInfo() const { |
| 4721 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4722 | } |
| 4723 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4724 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4725 | return 13; |
| 4726 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4727 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4728 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4729 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4730 | } |
| 4731 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4732 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4733 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4734 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4735 | |
| 4736 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4737 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4738 | return false; |
| 4739 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4740 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4741 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4742 | if (getABIInfo().isEABI()) return 88; |
| 4743 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4744 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4745 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4746 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4747 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 4748 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4749 | if (!FD) |
| 4750 | return; |
| 4751 | |
| 4752 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4753 | if (!Attr) |
| 4754 | return; |
| 4755 | |
| 4756 | const char *Kind; |
| 4757 | switch (Attr->getInterrupt()) { |
| 4758 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4759 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4760 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4761 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4762 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4763 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4764 | } |
| 4765 | |
| 4766 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4767 | |
| 4768 | Fn->addFnAttr("interrupt", Kind); |
| 4769 | |
| 4770 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4771 | return; |
| 4772 | |
| 4773 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4774 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4775 | // the backend to perform a realignment as part of the function prologue. |
| 4776 | llvm::AttrBuilder B; |
| 4777 | B.addStackAlignmentAttr(8); |
| 4778 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4779 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4780 | llvm::AttributeSet::FunctionIndex, |
| 4781 | B)); |
| 4782 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4783 | }; |
| 4784 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4785 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4786 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4787 | CodeGen::CodeGenModule &CGM) const; |
| 4788 | |
| 4789 | public: |
| 4790 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4791 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4792 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4793 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4794 | CodeGen::CodeGenModule &CGM) const override; |
| 4795 | }; |
| 4796 | |
| 4797 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4798 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4799 | if (!isa<FunctionDecl>(D)) |
| 4800 | return; |
| 4801 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4802 | return; |
| 4803 | |
| 4804 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4805 | F->addFnAttr("stack-probe-size", |
| 4806 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4807 | } |
| 4808 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4809 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4810 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4811 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4812 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4813 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4814 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4815 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4816 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4817 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4818 | FI.getReturnInfo() = |
| 4819 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4820 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4821 | for (auto &I : FI.arguments()) |
| 4822 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4823 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4824 | // Always honor user-specified calling convention. |
| 4825 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4826 | return; |
| 4827 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4828 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4829 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4830 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4831 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4832 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4833 | /// Return the default calling convention that LLVM will use. |
| 4834 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4835 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4836 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4837 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4838 | else if (isEABI()) |
| 4839 | return llvm::CallingConv::ARM_AAPCS; |
| 4840 | else |
| 4841 | return llvm::CallingConv::ARM_APCS; |
| 4842 | } |
| 4843 | |
| 4844 | /// Return the calling convention that our ABI would like us to use |
| 4845 | /// as the C calling convention. |
| 4846 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4847 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4848 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4849 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4850 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4851 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4852 | llvm_unreachable("bad ABI kind"); |
| 4853 | } |
| 4854 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4855 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4856 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4857 | |
| 4858 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4859 | // they'd just match what LLVM will infer from the triple. |
| 4860 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4861 | if (abiCC != getLLVMDefaultCC()) |
| 4862 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4863 | |
| 4864 | BuiltinCC = (getABIKind() == APCS ? |
| 4865 | llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4866 | } |
| 4867 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4868 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4869 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4870 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4871 | // A single-precision floating-point type (including promoted |
| 4872 | // half-precision types); A double-precision floating-point type; |
| 4873 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4874 | // with a Base Type of a single- or double-precision floating-point type, |
| 4875 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4876 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4877 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4878 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4879 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4880 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4881 | // Handle illegal vector types here. |
| 4882 | if (isIllegalVectorType(Ty)) { |
| 4883 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4884 | if (Size <= 32) { |
| 4885 | llvm::Type *ResType = |
| 4886 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4887 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4888 | } |
| 4889 | if (Size == 64) { |
| 4890 | llvm::Type *ResType = llvm::VectorType::get( |
| 4891 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4892 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4893 | } |
| 4894 | if (Size == 128) { |
| 4895 | llvm::Type *ResType = llvm::VectorType::get( |
| 4896 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4897 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4898 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4899 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 4902 | // __fp16 gets passed as if it were an int or float, but with the top 16 bits |
| 4903 | // unspecified. This is not done for OpenCL as it handles the half type |
| 4904 | // natively, and does not need to interwork with AAPCS code. |
| 4905 | if (Ty->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 4906 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 4907 | llvm::Type::getFloatTy(getVMContext()) : |
| 4908 | llvm::Type::getInt32Ty(getVMContext()); |
| 4909 | return ABIArgInfo::getDirect(ResType); |
| 4910 | } |
| 4911 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4912 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4913 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4914 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4915 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4916 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4917 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4918 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4919 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4920 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4921 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4922 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4923 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4924 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4925 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4926 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4927 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4928 | return ABIArgInfo::getIgnore(); |
| 4929 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4930 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4931 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4932 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4933 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4934 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4935 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4936 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4937 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4938 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4939 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4942 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4943 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4944 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4945 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4946 | uint64_t ABIAlign = 4; |
| 4947 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4948 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4949 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4950 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4951 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4952 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4953 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4954 | /*ByVal=*/true, |
| 4955 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4958 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4959 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4960 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4961 | // FIXME: Try to match the types of the arguments more accurately where |
| 4962 | // we can. |
| 4963 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4964 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4965 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4966 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4967 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4968 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4969 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4970 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4971 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4974 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4975 | llvm::LLVMContext &VMContext) { |
| 4976 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4977 | // is called integer-like if its size is less than or equal to one word, and |
| 4978 | // the offset of each of its addressable sub-fields is zero. |
| 4979 | |
| 4980 | uint64_t Size = Context.getTypeSize(Ty); |
| 4981 | |
| 4982 | // Check that the type fits in a word. |
| 4983 | if (Size > 32) |
| 4984 | return false; |
| 4985 | |
| 4986 | // FIXME: Handle vector types! |
| 4987 | if (Ty->isVectorType()) |
| 4988 | return false; |
| 4989 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4990 | // Float types are never treated as "integer like". |
| 4991 | if (Ty->isRealFloatingType()) |
| 4992 | return false; |
| 4993 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4994 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4995 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4996 | return true; |
| 4997 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4998 | // Small complex integer types are "integer like". |
| 4999 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 5000 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5001 | |
| 5002 | // Single element and zero sized arrays should be allowed, by the definition |
| 5003 | // above, but they are not. |
| 5004 | |
| 5005 | // Otherwise, it must be a record type. |
| 5006 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 5007 | if (!RT) return false; |
| 5008 | |
| 5009 | // Ignore records with flexible arrays. |
| 5010 | const RecordDecl *RD = RT->getDecl(); |
| 5011 | if (RD->hasFlexibleArrayMember()) |
| 5012 | return false; |
| 5013 | |
| 5014 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 5015 | // like". |
| 5016 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 5017 | |
| 5018 | bool HadField = false; |
| 5019 | unsigned idx = 0; |
| 5020 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5021 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5022 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5023 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5024 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 5025 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 5026 | // struct { int : 0; int x } |
| 5027 | // is non-integer like according to gcc. |
| 5028 | if (FD->isBitField()) { |
| 5029 | if (!RD->isUnion()) |
| 5030 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5031 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5032 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5033 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5034 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5035 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5038 | // Check if this field is at offset 0. |
| 5039 | if (Layout.getFieldOffset(idx) != 0) |
| 5040 | return false; |
| 5041 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5042 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 5043 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 5044 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 5045 | // Only allow at most one field in a structure. This doesn't match the |
| 5046 | // wording above, but follows gcc in situations with a field following an |
| 5047 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5048 | if (!RD->isUnion()) { |
| 5049 | if (HadField) |
| 5050 | return false; |
| 5051 | |
| 5052 | HadField = true; |
| 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | return true; |
| 5057 | } |
| 5058 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5059 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 5060 | bool isVariadic) const { |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5061 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5062 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5063 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5064 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5065 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5066 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5067 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5068 | return getNaturalAlignIndirect(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5069 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 5070 | |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5071 | // __fp16 gets returned as if it were an int or float, but with the top 16 |
| 5072 | // bits unspecified. This is not done for OpenCL as it handles the half type |
| 5073 | // natively, and does not need to interwork with AAPCS code. |
| 5074 | if (RetTy->isHalfType() && !getContext().getLangOpts().OpenCL) { |
| 5075 | llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? |
| 5076 | llvm::Type::getFloatTy(getVMContext()) : |
| 5077 | llvm::Type::getInt32Ty(getVMContext()); |
| 5078 | return ABIArgInfo::getDirect(ResType); |
| 5079 | } |
| 5080 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5081 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5082 | // Treat an enum type as its underlying type. |
| 5083 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5084 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5085 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5086 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 5087 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5088 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5089 | |
| 5090 | // Are we following APCS? |
| 5091 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5092 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5093 | return ABIArgInfo::getIgnore(); |
| 5094 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5095 | // Complex types are all returned as packed integers. |
| 5096 | // |
| 5097 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 5098 | // correctly. |
| 5099 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5100 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 5101 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 5102 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5103 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5104 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5105 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5106 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5107 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5108 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5109 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 5110 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5111 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5115 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5116 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5117 | |
| 5118 | // Otherwise this is an AAPCS variant. |
| 5119 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5120 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5121 | return ABIArgInfo::getIgnore(); |
| 5122 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5123 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5124 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5125 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5126 | uint64_t Members; |
| 5127 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5128 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5129 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5130 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 5131 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5134 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 5135 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5136 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5137 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5138 | if (getDataLayout().isBigEndian()) |
| 5139 | // 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] | 5140 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 5141 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5142 | // Return in the smallest viable integer type. |
| 5143 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5144 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5145 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5146 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5147 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5150 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5153 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 5154 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 5155 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5156 | // Check whether VT is legal. |
| 5157 | unsigned NumElements = VT->getNumElements(); |
| 5158 | uint64_t Size = getContext().getTypeSize(VT); |
| 5159 | // NumElements should be power of 2. |
| 5160 | if ((NumElements & (NumElements - 1)) != 0) |
| 5161 | return true; |
| 5162 | // Size should be greater than 32 bits. |
| 5163 | return Size <= 32; |
| 5164 | } |
| 5165 | return false; |
| 5166 | } |
| 5167 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5168 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5169 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 5170 | // double, or 64-bit or 128-bit vectors. |
| 5171 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5172 | if (BT->getKind() == BuiltinType::Float || |
| 5173 | BT->getKind() == BuiltinType::Double || |
| 5174 | BT->getKind() == BuiltinType::LongDouble) |
| 5175 | return true; |
| 5176 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5177 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5178 | if (VecSize == 64 || VecSize == 128) |
| 5179 | return true; |
| 5180 | } |
| 5181 | return false; |
| 5182 | } |
| 5183 | |
| 5184 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5185 | uint64_t Members) const { |
| 5186 | return Members <= 4; |
| 5187 | } |
| 5188 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5189 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5190 | QualType Ty) const { |
| 5191 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5192 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5193 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5194 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5195 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 5196 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5197 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 5198 | } |
| 5199 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5200 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5201 | CharUnits TyAlignForABI = TyInfo.second; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5202 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5203 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5204 | bool IsIndirect = false; |
| 5205 | if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
| 5206 | IsIndirect = true; |
| 5207 | |
| 5208 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5209 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5210 | // 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] | 5211 | // Our callers should be prepared to handle an under-aligned address. |
| 5212 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5213 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5214 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 5215 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
| 5216 | } else { |
| 5217 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5218 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5219 | TyInfo.second = TyAlignForABI; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5220 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5221 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 5222 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5225 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5226 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5227 | //===----------------------------------------------------------------------===// |
| 5228 | |
| 5229 | namespace { |
| 5230 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5231 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5232 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5233 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5234 | |
| 5235 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5236 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5237 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5238 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5239 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5240 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5241 | }; |
| 5242 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5243 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5244 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5245 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5246 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5247 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5248 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5249 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5250 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5251 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5252 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5253 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5254 | }; |
| 5255 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5256 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5257 | if (RetTy->isVoidType()) |
| 5258 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5259 | |
| 5260 | // note: this is different from default ABI |
| 5261 | if (!RetTy->isScalarType()) |
| 5262 | return ABIArgInfo::getDirect(); |
| 5263 | |
| 5264 | // Treat an enum type as its underlying type. |
| 5265 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5266 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5267 | |
| 5268 | return (RetTy->isPromotableIntegerType() ? |
| 5269 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5272 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5273 | // Treat an enum type as its underlying type. |
| 5274 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5275 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5276 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5277 | // Return aggregates type as indirect by value |
| 5278 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5279 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5280 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5281 | return (Ty->isPromotableIntegerType() ? |
| 5282 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5285 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5286 | if (!getCXXABI().classifyReturnType(FI)) |
| 5287 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5288 | for (auto &I : FI.arguments()) |
| 5289 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5290 | |
| 5291 | // Always honor user-specified calling convention. |
| 5292 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5293 | return; |
| 5294 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5295 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5296 | } |
| 5297 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5298 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5299 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5300 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5303 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5304 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5305 | CodeGen::CodeGenModule &M) const{ |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 5306 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5307 | if (!FD) return; |
| 5308 | |
| 5309 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5310 | |
| 5311 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5312 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5313 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5314 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5315 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5316 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5317 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5318 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5319 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5320 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5321 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5322 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5323 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5324 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5325 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5326 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5327 | // __global__ functions cannot be called from the device, we do not |
| 5328 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5329 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5330 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5331 | addNVVMMetadata(F, "kernel", 1); |
| 5332 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5333 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5334 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5335 | llvm::APSInt MaxThreads(32); |
| 5336 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5337 | if (MaxThreads > 0) |
| 5338 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5339 | |
| 5340 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5341 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5342 | // we don't have to add a PTX directive. |
| 5343 | if (Attr->getMinBlocks()) { |
| 5344 | llvm::APSInt MinBlocks(32); |
| 5345 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5346 | if (MinBlocks > 0) |
| 5347 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5348 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5349 | } |
| 5350 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5351 | } |
| 5352 | } |
| 5353 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5354 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5355 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5356 | llvm::Module *M = F->getParent(); |
| 5357 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5358 | |
| 5359 | // Get "nvvm.annotations" metadata node |
| 5360 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5361 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5362 | llvm::Metadata *MDVals[] = { |
| 5363 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5364 | llvm::ConstantAsMetadata::get( |
| 5365 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5366 | // Append metadata to nvvm.annotations |
| 5367 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5368 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5369 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5370 | |
| 5371 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5372 | // SystemZ ABI Implementation |
| 5373 | //===----------------------------------------------------------------------===// |
| 5374 | |
| 5375 | namespace { |
| 5376 | |
| 5377 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5378 | bool HasVector; |
| 5379 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5380 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5381 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5382 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5383 | |
| 5384 | bool isPromotableIntegerType(QualType Ty) const; |
| 5385 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5386 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5387 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5388 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5389 | |
| 5390 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5391 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5392 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5393 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5394 | if (!getCXXABI().classifyReturnType(FI)) |
| 5395 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5396 | for (auto &I : FI.arguments()) |
| 5397 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5400 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5401 | QualType Ty) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5402 | }; |
| 5403 | |
| 5404 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5405 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5406 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5407 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5408 | }; |
| 5409 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5410 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5411 | |
| 5412 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5413 | // Treat an enum type as its underlying type. |
| 5414 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5415 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5416 | |
| 5417 | // Promotable integer types are required to be promoted by the ABI. |
| 5418 | if (Ty->isPromotableIntegerType()) |
| 5419 | return true; |
| 5420 | |
| 5421 | // 32-bit values must also be promoted. |
| 5422 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5423 | switch (BT->getKind()) { |
| 5424 | case BuiltinType::Int: |
| 5425 | case BuiltinType::UInt: |
| 5426 | return true; |
| 5427 | default: |
| 5428 | return false; |
| 5429 | } |
| 5430 | return false; |
| 5431 | } |
| 5432 | |
| 5433 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5434 | return (Ty->isAnyComplexType() || |
| 5435 | Ty->isVectorType() || |
| 5436 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5439 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5440 | return (HasVector && |
| 5441 | Ty->isVectorType() && |
| 5442 | getContext().getTypeSize(Ty) <= 128); |
| 5443 | } |
| 5444 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5445 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5446 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5447 | switch (BT->getKind()) { |
| 5448 | case BuiltinType::Float: |
| 5449 | case BuiltinType::Double: |
| 5450 | return true; |
| 5451 | default: |
| 5452 | return false; |
| 5453 | } |
| 5454 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5455 | return false; |
| 5456 | } |
| 5457 | |
| 5458 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5459 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5460 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5461 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5462 | |
| 5463 | // If this is a C++ record, check the bases first. |
| 5464 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5465 | for (const auto &I : CXXRD->bases()) { |
| 5466 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5467 | |
| 5468 | // Empty bases don't affect things either way. |
| 5469 | if (isEmptyRecord(getContext(), Base, true)) |
| 5470 | continue; |
| 5471 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5472 | if (!Found.isNull()) |
| 5473 | return Ty; |
| 5474 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5478 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5479 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5480 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5481 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5482 | if (getContext().getLangOpts().CPlusPlus && |
| 5483 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5484 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5485 | |
| 5486 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5487 | // Nested structures still do though. |
| 5488 | if (!Found.isNull()) |
| 5489 | return Ty; |
| 5490 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5494 | // 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] | 5495 | if (!Found.isNull()) |
| 5496 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5499 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5502 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5503 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5504 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5505 | // struct { |
| 5506 | // i64 __gpr; |
| 5507 | // i64 __fpr; |
| 5508 | // i8 *__overflow_arg_area; |
| 5509 | // i8 *__reg_save_area; |
| 5510 | // }; |
| 5511 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5512 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5513 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5514 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5515 | Ty = getContext().getCanonicalType(Ty); |
| 5516 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5517 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5518 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5519 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5520 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5521 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5522 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5523 | CharUnits UnpaddedSize; |
| 5524 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5525 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5526 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 5527 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5528 | } else { |
| 5529 | if (AI.getCoerceToType()) |
| 5530 | ArgTy = AI.getCoerceToType(); |
| 5531 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5532 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5533 | UnpaddedSize = TyInfo.first; |
| 5534 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5535 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5536 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 5537 | if (IsVector && UnpaddedSize > PaddedSize) |
| 5538 | PaddedSize = CharUnits::fromQuantity(16); |
| 5539 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5540 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5541 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5542 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5543 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5544 | llvm::Value *PaddedSizeV = |
| 5545 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5546 | |
| 5547 | if (IsVector) { |
| 5548 | // Work out the address of a vector argument on the stack. |
| 5549 | // Vector arguments are always passed in the high bits of a |
| 5550 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5551 | Address OverflowArgAreaPtr = |
| 5552 | CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5553 | "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5554 | Address OverflowArgArea = |
| 5555 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5556 | TyInfo.second); |
| 5557 | Address MemAddr = |
| 5558 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5559 | |
| 5560 | // Update overflow_arg_area_ptr pointer |
| 5561 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5562 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5563 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5564 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5565 | |
| 5566 | return MemAddr; |
| 5567 | } |
| 5568 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5569 | assert(PaddedSize.getQuantity() == 8); |
| 5570 | |
| 5571 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 5572 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5573 | if (InFPRs) { |
| 5574 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5575 | RegCountField = 1; // __fpr |
| 5576 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5577 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5578 | } else { |
| 5579 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5580 | RegCountField = 0; // __gpr |
| 5581 | RegSaveIndex = 2; // save offset for r2 |
| 5582 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5583 | } |
| 5584 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5585 | Address RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5586 | VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), |
| 5587 | "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5588 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5589 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5590 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5591 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5592 | |
| 5593 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5594 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5595 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5596 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5597 | |
| 5598 | // Emit code to load the value if it was passed in registers. |
| 5599 | CGF.EmitBlock(InRegBlock); |
| 5600 | |
| 5601 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5602 | llvm::Value *ScaledRegCount = |
| 5603 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5604 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5605 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 5606 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5607 | llvm::Value *RegOffset = |
| 5608 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5609 | Address RegSaveAreaPtr = |
| 5610 | CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), |
| 5611 | "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5612 | llvm::Value *RegSaveArea = |
| 5613 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5614 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 5615 | "raw_reg_addr"), |
| 5616 | PaddedSize); |
| 5617 | Address RegAddr = |
| 5618 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5619 | |
| 5620 | // Update the register count |
| 5621 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5622 | llvm::Value *NewRegCount = |
| 5623 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5624 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5625 | CGF.EmitBranch(ContBlock); |
| 5626 | |
| 5627 | // Emit code to load the value if it was passed in memory. |
| 5628 | CGF.EmitBlock(InMemBlock); |
| 5629 | |
| 5630 | // Work out the address of a stack argument. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5631 | Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5632 | VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); |
| 5633 | Address OverflowArgArea = |
| 5634 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 5635 | PaddedSize); |
| 5636 | Address RawMemAddr = |
| 5637 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 5638 | Address MemAddr = |
| 5639 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5640 | |
| 5641 | // Update overflow_arg_area_ptr pointer |
| 5642 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5643 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 5644 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5645 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5646 | CGF.EmitBranch(ContBlock); |
| 5647 | |
| 5648 | // Return the appropriate result. |
| 5649 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5650 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5651 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5652 | |
| 5653 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5654 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 5655 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5656 | |
| 5657 | return ResAddr; |
| 5658 | } |
| 5659 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5660 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5661 | if (RetTy->isVoidType()) |
| 5662 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5663 | if (isVectorArgumentType(RetTy)) |
| 5664 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5665 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5666 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5667 | return (isPromotableIntegerType(RetTy) ? |
| 5668 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5669 | } |
| 5670 | |
| 5671 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5672 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5673 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5674 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5675 | |
| 5676 | // Integers and enums are extended to full register width. |
| 5677 | if (isPromotableIntegerType(Ty)) |
| 5678 | return ABIArgInfo::getExtend(); |
| 5679 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5680 | // Handle vector types and vector-like structure types. Note that |
| 5681 | // as opposed to float-like structure types, we do not allow any |
| 5682 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5683 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5684 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5685 | if (isVectorArgumentType(SingleElementTy) && |
| 5686 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5687 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5688 | |
| 5689 | // 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] | 5690 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5691 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5692 | |
| 5693 | // Handle small structures. |
| 5694 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5695 | // Structures with flexible arrays have variable length, so really |
| 5696 | // fail the size test above. |
| 5697 | const RecordDecl *RD = RT->getDecl(); |
| 5698 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5699 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5700 | |
| 5701 | // The structure is passed as an unextended integer, a float, or a double. |
| 5702 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5703 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5704 | assert(Size == 32 || Size == 64); |
| 5705 | if (Size == 32) |
| 5706 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5707 | else |
| 5708 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5709 | } else |
| 5710 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5711 | return ABIArgInfo::getDirect(PassTy); |
| 5712 | } |
| 5713 | |
| 5714 | // Non-structure compounds are passed indirectly. |
| 5715 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5716 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5717 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5718 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
| 5721 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5722 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5723 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5724 | |
| 5725 | namespace { |
| 5726 | |
| 5727 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5728 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5729 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5730 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5731 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5732 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5733 | }; |
| 5734 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5735 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5736 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5737 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5738 | llvm::GlobalValue *GV, |
| 5739 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 5740 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5741 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5742 | // Handle 'interrupt' attribute: |
| 5743 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5744 | |
| 5745 | // Step 1: Set ISR calling convention. |
| 5746 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5747 | |
| 5748 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5749 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5750 | |
| 5751 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5752 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5753 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5754 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5755 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5756 | } |
| 5757 | } |
| 5758 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5759 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5760 | // MIPS ABI Implementation. This works for both little-endian and |
| 5761 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5762 | //===----------------------------------------------------------------------===// |
| 5763 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5764 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5765 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5766 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5767 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5768 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5769 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5770 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5771 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5772 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5773 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5774 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5775 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5776 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5777 | |
| 5778 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5779 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5780 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5781 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5782 | QualType Ty) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5783 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5784 | }; |
| 5785 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5786 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5787 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5788 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5789 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5790 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5791 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5792 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5793 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5794 | return 29; |
| 5795 | } |
| 5796 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5797 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5798 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 5799 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5800 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5801 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5802 | if (FD->hasAttr<Mips16Attr>()) { |
| 5803 | Fn->addFnAttr("mips16"); |
| 5804 | } |
| 5805 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5806 | Fn->addFnAttr("nomips16"); |
| 5807 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5808 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5809 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5810 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5811 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5812 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5813 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5814 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5815 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5816 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5817 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5818 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5819 | void MipsABIInfo::CoerceToIntArgs( |
| 5820 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5821 | llvm::IntegerType *IntTy = |
| 5822 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5823 | |
| 5824 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5825 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5826 | ArgList.push_back(IntTy); |
| 5827 | |
| 5828 | // If necessary, add one more integer type to ArgList. |
| 5829 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5830 | |
| 5831 | if (R) |
| 5832 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5835 | // In N32/64, an aligned double precision floating point field is passed in |
| 5836 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5837 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5838 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5839 | |
| 5840 | if (IsO32) { |
| 5841 | CoerceToIntArgs(TySize, ArgList); |
| 5842 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5843 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5844 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5845 | if (Ty->isComplexType()) |
| 5846 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5847 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5848 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5849 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5850 | // Unions/vectors are passed in integer registers. |
| 5851 | if (!RT || !RT->isStructureOrClassType()) { |
| 5852 | CoerceToIntArgs(TySize, ArgList); |
| 5853 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5854 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5855 | |
| 5856 | const RecordDecl *RD = RT->getDecl(); |
| 5857 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5858 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5859 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5860 | uint64_t LastOffset = 0; |
| 5861 | unsigned idx = 0; |
| 5862 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5863 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5864 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5865 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5866 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5867 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5868 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5869 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5870 | |
| 5871 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5872 | continue; |
| 5873 | |
| 5874 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5875 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5876 | continue; |
| 5877 | |
| 5878 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5879 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5880 | ArgList.push_back(I64); |
| 5881 | |
| 5882 | // Add double type. |
| 5883 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5884 | LastOffset = Offset + 64; |
| 5885 | } |
| 5886 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5887 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5888 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5889 | |
| 5890 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5891 | } |
| 5892 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5893 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5894 | uint64_t Offset) const { |
| 5895 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5896 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5897 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5898 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5899 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5900 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5901 | ABIArgInfo |
| 5902 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5903 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5904 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5905 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5906 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5907 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5908 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5909 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5910 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5911 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5912 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5913 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5914 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5915 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5916 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5917 | return ABIArgInfo::getIgnore(); |
| 5918 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5919 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5920 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5921 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5922 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5923 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5924 | // If we have reached here, aggregates are passed directly by coercing to |
| 5925 | // another structure type. Padding is inserted if the offset of the |
| 5926 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 5927 | ABIArgInfo ArgInfo = |
| 5928 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 5929 | getPaddingType(OrigOffset, CurrOffset)); |
| 5930 | ArgInfo.setInReg(true); |
| 5931 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | // Treat an enum type as its underlying type. |
| 5935 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5936 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5937 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 5938 | // All integral types are promoted to the GPR width. |
| 5939 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5940 | return ABIArgInfo::getExtend(); |
| 5941 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5942 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5943 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5944 | } |
| 5945 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5946 | llvm::Type* |
| 5947 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5948 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5949 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5950 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5951 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5952 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5953 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5954 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5955 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5956 | // N32/64 returns struct/classes in floating point registers if the |
| 5957 | // following conditions are met: |
| 5958 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5959 | // 2. The struct/class has one or two fields all of which are floating |
| 5960 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5961 | // 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] | 5962 | // |
| 5963 | // Any other composite results are returned in integer registers. |
| 5964 | // |
| 5965 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5966 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5967 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5968 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5969 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5970 | if (!BT || !BT->isFloatingPoint()) |
| 5971 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5972 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5973 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | if (b == e) |
| 5977 | return llvm::StructType::get(getVMContext(), RTList, |
| 5978 | RD->hasAttr<PackedAttr>()); |
| 5979 | |
| 5980 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5981 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5984 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5985 | return llvm::StructType::get(getVMContext(), RTList); |
| 5986 | } |
| 5987 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5988 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5989 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5990 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5991 | if (RetTy->isVoidType()) |
| 5992 | return ABIArgInfo::getIgnore(); |
| 5993 | |
| 5994 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5995 | // However, N32/N64 ignores zero sized return values. |
| 5996 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5997 | return ABIArgInfo::getIgnore(); |
| 5998 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5999 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6000 | if (Size <= 128) { |
| 6001 | if (RetTy->isAnyComplexType()) |
| 6002 | return ABIArgInfo::getDirect(); |
| 6003 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6004 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 6005 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 6006 | if (!IsO32 || |
| 6007 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 6008 | ABIArgInfo ArgInfo = |
| 6009 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 6010 | ArgInfo.setInReg(true); |
| 6011 | return ArgInfo; |
| 6012 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6013 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6014 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6015 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
| 6018 | // Treat an enum type as its underlying type. |
| 6019 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6020 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6021 | |
| 6022 | return (RetTy->isPromotableIntegerType() ? |
| 6023 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6024 | } |
| 6025 | |
| 6026 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6027 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6028 | if (!getCXXABI().classifyReturnType(FI)) |
| 6029 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6030 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6031 | // 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] | 6032 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 6033 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6034 | for (auto &I : FI.arguments()) |
| 6035 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6038 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6039 | QualType OrigTy) const { |
| 6040 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6041 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6042 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 6043 | // 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] | 6044 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6045 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6046 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6047 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6048 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 6049 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6050 | DidPromote = true; |
| 6051 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 6052 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 6053 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6054 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6055 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6056 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6057 | // The alignment of things in the argument area is never larger than |
| 6058 | // StackAlignInBytes. |
| 6059 | TyInfo.second = |
| 6060 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 6061 | |
| 6062 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 6063 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 6064 | |
| 6065 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6066 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 6067 | |
| 6068 | |
| 6069 | // If there was a promotion, "unpromote" into a temporary. |
| 6070 | // TODO: can we just use a pointer into a subset of the original slot? |
| 6071 | if (DidPromote) { |
| 6072 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 6073 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 6074 | |
| 6075 | // Truncate down to the right width. |
| 6076 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 6077 | : CGF.IntPtrTy); |
| 6078 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 6079 | if (OrigTy->isPointerType()) |
| 6080 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 6081 | |
| 6082 | CGF.Builder.CreateStore(V, Temp); |
| 6083 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6084 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 6085 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6086 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6089 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 6090 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6091 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6092 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 6093 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 6094 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6095 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 6096 | return false; |
| 6097 | } |
| 6098 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6099 | bool |
| 6100 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6101 | llvm::Value *Address) const { |
| 6102 | // This information comes from gcc's implementation, which seems to |
| 6103 | // as canonical as it gets. |
| 6104 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6105 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 6106 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6107 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6108 | |
| 6109 | // 0-31 are the general purpose registers, $0 - $31. |
| 6110 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 6111 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 6112 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6113 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6114 | |
| 6115 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 6116 | // They are one bit wide and ignored here. |
| 6117 | |
| 6118 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 6119 | // (coprocessor 1 is the FP unit) |
| 6120 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 6121 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 6122 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6123 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6124 | return false; |
| 6125 | } |
| 6126 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6127 | //===----------------------------------------------------------------------===// |
| 6128 | // 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] | 6129 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6130 | // handling. |
| 6131 | //===----------------------------------------------------------------------===// |
| 6132 | |
| 6133 | namespace { |
| 6134 | |
| 6135 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 6136 | public: |
| 6137 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 6138 | : DefaultTargetCodeGenInfo(CGT) {} |
| 6139 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6140 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6141 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6142 | }; |
| 6143 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6144 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6145 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 6146 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6147 | if (!FD) return; |
| 6148 | |
| 6149 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6150 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6151 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6152 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 6153 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6154 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 6155 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 6156 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6157 | // Convert the reqd_work_group_size() attributes to metadata. |
| 6158 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6159 | llvm::NamedMDNode *OpenCLMetadata = |
| 6160 | M.getModule().getOrInsertNamedMetadata( |
| 6161 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6162 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6163 | SmallVector<llvm::Metadata *, 5> Operands; |
| 6164 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6165 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6166 | Operands.push_back( |
| 6167 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6168 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 6169 | Operands.push_back( |
| 6170 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6171 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 6172 | Operands.push_back( |
| 6173 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 6174 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6175 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6176 | // Add a boolean constant operand for "required" (true) or "hint" |
| 6177 | // (false) for implementing the work_group_size_hint attr later. |
| 6178 | // 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] | 6179 | Operands.push_back( |
| 6180 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6181 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6182 | } |
| 6183 | } |
| 6184 | } |
| 6185 | } |
| 6186 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6187 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6188 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6189 | //===----------------------------------------------------------------------===// |
| 6190 | // Hexagon ABI Implementation |
| 6191 | //===----------------------------------------------------------------------===// |
| 6192 | |
| 6193 | namespace { |
| 6194 | |
| 6195 | class HexagonABIInfo : public ABIInfo { |
| 6196 | |
| 6197 | |
| 6198 | public: |
| 6199 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6200 | |
| 6201 | private: |
| 6202 | |
| 6203 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6204 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6205 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6206 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6207 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6208 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6209 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6210 | }; |
| 6211 | |
| 6212 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6213 | public: |
| 6214 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6215 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6216 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6217 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6218 | return 29; |
| 6219 | } |
| 6220 | }; |
| 6221 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6222 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6223 | |
| 6224 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6225 | if (!getCXXABI().classifyReturnType(FI)) |
| 6226 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6227 | for (auto &I : FI.arguments()) |
| 6228 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6232 | if (!isAggregateTypeForABI(Ty)) { |
| 6233 | // Treat an enum type as its underlying type. |
| 6234 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6235 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6236 | |
| 6237 | return (Ty->isPromotableIntegerType() ? |
| 6238 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6239 | } |
| 6240 | |
| 6241 | // Ignore empty records. |
| 6242 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6243 | return ABIArgInfo::getIgnore(); |
| 6244 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6245 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6246 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6247 | |
| 6248 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6249 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6250 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6251 | // Pass in the smallest viable integer type. |
| 6252 | else if (Size > 32) |
| 6253 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6254 | else if (Size > 16) |
| 6255 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6256 | else if (Size > 8) |
| 6257 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6258 | else |
| 6259 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6260 | } |
| 6261 | |
| 6262 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6263 | if (RetTy->isVoidType()) |
| 6264 | return ABIArgInfo::getIgnore(); |
| 6265 | |
| 6266 | // Large vector types should be returned via memory. |
| 6267 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6268 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6269 | |
| 6270 | if (!isAggregateTypeForABI(RetTy)) { |
| 6271 | // Treat an enum type as its underlying type. |
| 6272 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6273 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6274 | |
| 6275 | return (RetTy->isPromotableIntegerType() ? |
| 6276 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6277 | } |
| 6278 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6279 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6280 | return ABIArgInfo::getIgnore(); |
| 6281 | |
| 6282 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6283 | // are returned indirectly. |
| 6284 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6285 | if (Size <= 64) { |
| 6286 | // Return in the smallest viable integer type. |
| 6287 | if (Size <= 8) |
| 6288 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6289 | if (Size <= 16) |
| 6290 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6291 | if (Size <= 32) |
| 6292 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6293 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6294 | } |
| 6295 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6296 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6297 | } |
| 6298 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6299 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6300 | QualType Ty) const { |
| 6301 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 6302 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 6303 | getContext().getTypeInfoInChars(Ty), |
| 6304 | CharUnits::fromQuantity(4), |
| 6305 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6308 | //===----------------------------------------------------------------------===// |
| 6309 | // AMDGPU ABI Implementation |
| 6310 | //===----------------------------------------------------------------------===// |
| 6311 | |
| 6312 | namespace { |
| 6313 | |
| 6314 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6315 | public: |
| 6316 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6317 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6318 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6319 | CodeGen::CodeGenModule &M) const override; |
| 6320 | }; |
| 6321 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6322 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6323 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6324 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6325 | const Decl *D, |
| 6326 | llvm::GlobalValue *GV, |
| 6327 | CodeGen::CodeGenModule &M) const { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame^] | 6328 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6329 | if (!FD) |
| 6330 | return; |
| 6331 | |
| 6332 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6333 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6334 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6335 | if (NumVGPR != 0) |
| 6336 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6337 | } |
| 6338 | |
| 6339 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6340 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6341 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6342 | if (NumSGPR != 0) |
| 6343 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6344 | } |
| 6345 | } |
| 6346 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6347 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6348 | //===----------------------------------------------------------------------===// |
| 6349 | // SPARC v9 ABI Implementation. |
| 6350 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6351 | // |
| 6352 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6353 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6354 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6355 | // |
| 6356 | // One case requires special care: |
| 6357 | // |
| 6358 | // struct mixed { |
| 6359 | // int i; |
| 6360 | // float f; |
| 6361 | // }; |
| 6362 | // |
| 6363 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6364 | // parameter array, but the int is passed in an integer register, and the float |
| 6365 | // is passed in a floating point register. This is represented as two arguments |
| 6366 | // with the LLVM IR inreg attribute: |
| 6367 | // |
| 6368 | // declare void f(i32 inreg %i, float inreg %f) |
| 6369 | // |
| 6370 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6371 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6372 | // bytes. |
| 6373 | // |
| 6374 | namespace { |
| 6375 | class SparcV9ABIInfo : public ABIInfo { |
| 6376 | public: |
| 6377 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6378 | |
| 6379 | private: |
| 6380 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6381 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6382 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6383 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6384 | |
| 6385 | // Coercion type builder for structs passed in registers. The coercion type |
| 6386 | // serves two purposes: |
| 6387 | // |
| 6388 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6389 | // in registers. |
| 6390 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6391 | // code generator knows to pass them in floating point registers. |
| 6392 | // |
| 6393 | // We also compute the InReg flag which indicates that the struct contains |
| 6394 | // aligned 32-bit floats. |
| 6395 | // |
| 6396 | struct CoerceBuilder { |
| 6397 | llvm::LLVMContext &Context; |
| 6398 | const llvm::DataLayout &DL; |
| 6399 | SmallVector<llvm::Type*, 8> Elems; |
| 6400 | uint64_t Size; |
| 6401 | bool InReg; |
| 6402 | |
| 6403 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6404 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6405 | |
| 6406 | // Pad Elems with integers until Size is ToSize. |
| 6407 | void pad(uint64_t ToSize) { |
| 6408 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6409 | if (ToSize == Size) |
| 6410 | return; |
| 6411 | |
| 6412 | // Finish the current 64-bit word. |
| 6413 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6414 | if (Aligned > Size && Aligned <= ToSize) { |
| 6415 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6416 | Size = Aligned; |
| 6417 | } |
| 6418 | |
| 6419 | // Add whole 64-bit words. |
| 6420 | while (Size + 64 <= ToSize) { |
| 6421 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6422 | Size += 64; |
| 6423 | } |
| 6424 | |
| 6425 | // Final in-word padding. |
| 6426 | if (Size < ToSize) { |
| 6427 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6428 | Size = ToSize; |
| 6429 | } |
| 6430 | } |
| 6431 | |
| 6432 | // Add a floating point element at Offset. |
| 6433 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6434 | // Unaligned floats are treated as integers. |
| 6435 | if (Offset % Bits) |
| 6436 | return; |
| 6437 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6438 | if (Bits < 64) |
| 6439 | InReg = true; |
| 6440 | pad(Offset); |
| 6441 | Elems.push_back(Ty); |
| 6442 | Size = Offset + Bits; |
| 6443 | } |
| 6444 | |
| 6445 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6446 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6447 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6448 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6449 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6450 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6451 | switch (ElemTy->getTypeID()) { |
| 6452 | case llvm::Type::StructTyID: |
| 6453 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6454 | break; |
| 6455 | case llvm::Type::FloatTyID: |
| 6456 | addFloat(ElemOffset, ElemTy, 32); |
| 6457 | break; |
| 6458 | case llvm::Type::DoubleTyID: |
| 6459 | addFloat(ElemOffset, ElemTy, 64); |
| 6460 | break; |
| 6461 | case llvm::Type::FP128TyID: |
| 6462 | addFloat(ElemOffset, ElemTy, 128); |
| 6463 | break; |
| 6464 | case llvm::Type::PointerTyID: |
| 6465 | if (ElemOffset % 64 == 0) { |
| 6466 | pad(ElemOffset); |
| 6467 | Elems.push_back(ElemTy); |
| 6468 | Size += 64; |
| 6469 | } |
| 6470 | break; |
| 6471 | default: |
| 6472 | break; |
| 6473 | } |
| 6474 | } |
| 6475 | } |
| 6476 | |
| 6477 | // Check if Ty is a usable substitute for the coercion type. |
| 6478 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6479 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | // Get the coercion type as a literal struct type. |
| 6483 | llvm::Type *getType() const { |
| 6484 | if (Elems.size() == 1) |
| 6485 | return Elems.front(); |
| 6486 | else |
| 6487 | return llvm::StructType::get(Context, Elems); |
| 6488 | } |
| 6489 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6490 | }; |
| 6491 | } // end anonymous namespace |
| 6492 | |
| 6493 | ABIArgInfo |
| 6494 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6495 | if (Ty->isVoidType()) |
| 6496 | return ABIArgInfo::getIgnore(); |
| 6497 | |
| 6498 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6499 | |
| 6500 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6501 | // pointer / sret pointer. |
| 6502 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6503 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6504 | |
| 6505 | // Treat an enum type as its underlying type. |
| 6506 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6507 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6508 | |
| 6509 | // Integer types smaller than a register are extended. |
| 6510 | if (Size < 64 && Ty->isIntegerType()) |
| 6511 | return ABIArgInfo::getExtend(); |
| 6512 | |
| 6513 | // Other non-aggregates go in registers. |
| 6514 | if (!isAggregateTypeForABI(Ty)) |
| 6515 | return ABIArgInfo::getDirect(); |
| 6516 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6517 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6518 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6519 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6520 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6521 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6522 | // 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] | 6523 | // Build a coercion type from the LLVM struct type. |
| 6524 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6525 | if (!StrTy) |
| 6526 | return ABIArgInfo::getDirect(); |
| 6527 | |
| 6528 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6529 | CB.addStruct(0, StrTy); |
| 6530 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6531 | |
| 6532 | // Try to use the original type for coercion. |
| 6533 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6534 | |
| 6535 | if (CB.InReg) |
| 6536 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6537 | else |
| 6538 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6541 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6542 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6543 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6544 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6545 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6546 | AI.setCoerceToType(ArgTy); |
| 6547 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6548 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6549 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6550 | CGBuilderTy &Builder = CGF.Builder; |
| 6551 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 6552 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6553 | |
| 6554 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 6555 | |
| 6556 | Address ArgAddr = Address::invalid(); |
| 6557 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6558 | switch (AI.getKind()) { |
| 6559 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6560 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6561 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6562 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6563 | case ABIArgInfo::Extend: { |
| 6564 | Stride = SlotSize; |
| 6565 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 6566 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6567 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6568 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6569 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6570 | case ABIArgInfo::Direct: { |
| 6571 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6572 | Stride = CharUnits::fromQuantity(AllocSize).RoundUpToAlignment(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6573 | ArgAddr = Addr; |
| 6574 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6575 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6576 | |
| 6577 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6578 | Stride = SlotSize; |
| 6579 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 6580 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 6581 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6582 | break; |
| 6583 | |
| 6584 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6585 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
| 6588 | // Update VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6589 | llvm::Value *NextPtr = |
| 6590 | Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); |
| 6591 | Builder.CreateStore(NextPtr, VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6592 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6593 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6597 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6598 | for (auto &I : FI.arguments()) |
| 6599 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | namespace { |
| 6603 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6604 | public: |
| 6605 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6606 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6607 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6608 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6609 | return 14; |
| 6610 | } |
| 6611 | |
| 6612 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6613 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6614 | }; |
| 6615 | } // end anonymous namespace |
| 6616 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6617 | bool |
| 6618 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6619 | llvm::Value *Address) const { |
| 6620 | // This is calculated from the LLVM and GCC tables and verified |
| 6621 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6622 | |
| 6623 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6624 | |
| 6625 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6626 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6627 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6628 | |
| 6629 | // 0-31: the 8-byte general-purpose registers |
| 6630 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6631 | |
| 6632 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6633 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6634 | |
| 6635 | // Y = 64 |
| 6636 | // PSR = 65 |
| 6637 | // WIM = 66 |
| 6638 | // TBR = 67 |
| 6639 | // PC = 68 |
| 6640 | // NPC = 69 |
| 6641 | // FSR = 70 |
| 6642 | // CSR = 71 |
| 6643 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6644 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6645 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6646 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6647 | |
| 6648 | return false; |
| 6649 | } |
| 6650 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6651 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6652 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6653 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6654 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6655 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6656 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6657 | |
| 6658 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6659 | /// it by reference between functions that append to it. |
| 6660 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6661 | |
| 6662 | /// TypeStringCache caches the meta encodings of Types. |
| 6663 | /// |
| 6664 | /// The reason for caching TypeStrings is two fold: |
| 6665 | /// 1. To cache a type's encoding for later uses; |
| 6666 | /// 2. As a means to break recursive member type inclusion. |
| 6667 | /// |
| 6668 | /// A cache Entry can have a Status of: |
| 6669 | /// NonRecursive: The type encoding is not recursive; |
| 6670 | /// Recursive: The type encoding is recursive; |
| 6671 | /// Incomplete: An incomplete TypeString; |
| 6672 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6673 | /// Recursive type encoding. |
| 6674 | /// |
| 6675 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6676 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6677 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6678 | /// the type is encountered. |
| 6679 | /// |
| 6680 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6681 | /// possible. The type itself is recursive and it may contain other types which |
| 6682 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6683 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6684 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6685 | /// |
| 6686 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6687 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6688 | /// are placed into the cache during type expansion as a means to identify and |
| 6689 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6690 | /// the entry becomes IncompleteUsed. |
| 6691 | /// |
| 6692 | /// During the expansion of a RecordType's members: |
| 6693 | /// |
| 6694 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6695 | /// cached encoding is used; |
| 6696 | /// |
| 6697 | /// If the cache contains a Recursive encoding for the member type, the |
| 6698 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6699 | /// |
| 6700 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6701 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6702 | /// |
| 6703 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6704 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6705 | /// it is swapped back in; |
| 6706 | /// |
| 6707 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6708 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6709 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6710 | /// |
| 6711 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6712 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6713 | /// Else the member is part of a recursive type and thus the recursion has |
| 6714 | /// been exited too soon for the encoding to be correct for the member. |
| 6715 | /// |
| 6716 | class TypeStringCache { |
| 6717 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6718 | struct Entry { |
| 6719 | std::string Str; // The encoded TypeString for the type. |
| 6720 | enum Status State; // Information about the encoding in 'Str'. |
| 6721 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6722 | // during the expansion of RecordType's members. |
| 6723 | }; |
| 6724 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6725 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6726 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6727 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6728 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6729 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6730 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6731 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6732 | bool IsRecursive); |
| 6733 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6734 | }; |
| 6735 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6736 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6737 | /// FieldEncoding is a helper for this ordering process. |
| 6738 | class FieldEncoding { |
| 6739 | bool HasName; |
| 6740 | std::string Enc; |
| 6741 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 6742 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 6743 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6744 | bool operator<(const FieldEncoding &rhs) const { |
| 6745 | if (HasName != rhs.HasName) return HasName; |
| 6746 | return Enc < rhs.Enc; |
| 6747 | } |
| 6748 | }; |
| 6749 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6750 | class XCoreABIInfo : public DefaultABIInfo { |
| 6751 | public: |
| 6752 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6753 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6754 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6755 | }; |
| 6756 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6757 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6758 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6759 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6760 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6761 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6762 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6763 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6764 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6765 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6766 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6767 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6768 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6769 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6770 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6771 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6772 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6773 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 6774 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6775 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6776 | // Handle the argument. |
| 6777 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6778 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6779 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6780 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6781 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6782 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6783 | |
| 6784 | Address Val = Address::invalid(); |
| 6785 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6786 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6787 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6788 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6789 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6790 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6791 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 6792 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6793 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6794 | case ABIArgInfo::Extend: |
| 6795 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6796 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 6797 | ArgSize = CharUnits::fromQuantity( |
| 6798 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
| 6799 | ArgSize = ArgSize.RoundUpToAlignment(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6800 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6801 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6802 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 6803 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 6804 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6805 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6806 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6807 | |
| 6808 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6809 | if (!ArgSize.isZero()) { |
| 6810 | llvm::Value *APN = |
| 6811 | Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); |
| 6812 | Builder.CreateStore(APN, VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6813 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6814 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6815 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6816 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6817 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6818 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6819 | /// into the cache as a means to identify and break recursion. |
| 6820 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6821 | /// be reinserted by removeIncomplete(). |
| 6822 | /// All other types of encoding should have been used rather than arriving here. |
| 6823 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6824 | std::string StubEnc) { |
| 6825 | if (!ID) |
| 6826 | return; |
| 6827 | Entry &E = Map[ID]; |
| 6828 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6829 | "Incorrectly use of addIncomplete"); |
| 6830 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6831 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6832 | E.Str.swap(StubEnc); |
| 6833 | E.State = Incomplete; |
| 6834 | ++IncompleteCount; |
| 6835 | } |
| 6836 | |
| 6837 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6838 | /// must be removed from the cache. |
| 6839 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6840 | /// Returns true if the RecordType was defined recursively. |
| 6841 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6842 | if (!ID) |
| 6843 | return false; |
| 6844 | auto I = Map.find(ID); |
| 6845 | assert(I != Map.end() && "Entry not present"); |
| 6846 | Entry &E = I->second; |
| 6847 | assert( (E.State == Incomplete || |
| 6848 | E.State == IncompleteUsed) && |
| 6849 | "Entry must be an incomplete type"); |
| 6850 | bool IsRecursive = false; |
| 6851 | if (E.State == IncompleteUsed) { |
| 6852 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6853 | IsRecursive = true; |
| 6854 | --IncompleteUsedCount; |
| 6855 | } |
| 6856 | if (E.Swapped.empty()) |
| 6857 | Map.erase(I); |
| 6858 | else { |
| 6859 | // Swap the Recursive back. |
| 6860 | E.Swapped.swap(E.Str); |
| 6861 | E.Swapped.clear(); |
| 6862 | E.State = Recursive; |
| 6863 | } |
| 6864 | --IncompleteCount; |
| 6865 | return IsRecursive; |
| 6866 | } |
| 6867 | |
| 6868 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6869 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6870 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6871 | bool IsRecursive) { |
| 6872 | if (!ID || IncompleteUsedCount) |
| 6873 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6874 | Entry &E = Map[ID]; |
| 6875 | if (IsRecursive && !E.Str.empty()) { |
| 6876 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6877 | "This is not the same Recursive entry"); |
| 6878 | // The parent container was not recursive after all, so we could have used |
| 6879 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6880 | // we started viz: IncompleteCount!=0. |
| 6881 | return; |
| 6882 | } |
| 6883 | assert(E.Str.empty() && "Entry already present"); |
| 6884 | E.Str = Str.str(); |
| 6885 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6886 | } |
| 6887 | |
| 6888 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6889 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6890 | /// encoding is Recursive, return an empty StringRef. |
| 6891 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6892 | if (!ID) |
| 6893 | return StringRef(); // We have no key. |
| 6894 | auto I = Map.find(ID); |
| 6895 | if (I == Map.end()) |
| 6896 | return StringRef(); // We have no encoding. |
| 6897 | Entry &E = I->second; |
| 6898 | if (E.State == Recursive && IncompleteCount) |
| 6899 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6900 | |
| 6901 | if (E.State == Incomplete) { |
| 6902 | // The incomplete type is being used to break out of recursion. |
| 6903 | E.State = IncompleteUsed; |
| 6904 | ++IncompleteUsedCount; |
| 6905 | } |
| 6906 | return E.Str.c_str(); |
| 6907 | } |
| 6908 | |
| 6909 | /// The XCore ABI includes a type information section that communicates symbol |
| 6910 | /// type information to the linker. The linker uses this information to verify |
| 6911 | /// safety/correctness of things such as array bound and pointers et al. |
| 6912 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6913 | /// This type information (TypeString) is emitted into meta data for all global |
| 6914 | /// symbols: definitions, declarations, functions & variables. |
| 6915 | /// |
| 6916 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6917 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6918 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6919 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6920 | /// |
| 6921 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6922 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6923 | |
| 6924 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6925 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6926 | CodeGen::CodeGenModule &CGM) const { |
| 6927 | SmallStringEnc Enc; |
| 6928 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6929 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6930 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 6931 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6932 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6933 | llvm::NamedMDNode *MD = |
| 6934 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6935 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6936 | } |
| 6937 | } |
| 6938 | |
| 6939 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6940 | const CodeGen::CodeGenModule &CGM, |
| 6941 | TypeStringCache &TSC); |
| 6942 | |
| 6943 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6944 | /// Builds a SmallVector containing the encoded field types in declaration |
| 6945 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6946 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6947 | const RecordDecl *RD, |
| 6948 | const CodeGen::CodeGenModule &CGM, |
| 6949 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6950 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6951 | SmallStringEnc Enc; |
| 6952 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6953 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6954 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6955 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6956 | Enc += "b("; |
| 6957 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6958 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6959 | Enc += ':'; |
| 6960 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6961 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6962 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6963 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6964 | Enc += ')'; |
| 6965 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 6966 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6967 | } |
| 6968 | return true; |
| 6969 | } |
| 6970 | |
| 6971 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6972 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6973 | /// Union types have their fields ordered according to the ABI. |
| 6974 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6975 | const CodeGen::CodeGenModule &CGM, |
| 6976 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6977 | // Append the cached TypeString if we have one. |
| 6978 | StringRef TypeString = TSC.lookupStr(ID); |
| 6979 | if (!TypeString.empty()) { |
| 6980 | Enc += TypeString; |
| 6981 | return true; |
| 6982 | } |
| 6983 | |
| 6984 | // Start to emit an incomplete TypeString. |
| 6985 | size_t Start = Enc.size(); |
| 6986 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6987 | Enc += '('; |
| 6988 | if (ID) |
| 6989 | Enc += ID->getName(); |
| 6990 | Enc += "){"; |
| 6991 | |
| 6992 | // We collect all encoded fields and order as necessary. |
| 6993 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6994 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6995 | if (RD && !RD->field_empty()) { |
| 6996 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6997 | // so that recursive calls to this RecordType will use it whilst building a |
| 6998 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6999 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7000 | std::string StubEnc(Enc.substr(Start).str()); |
| 7001 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 7002 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 7003 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 7004 | (void) TSC.removeIncomplete(ID); |
| 7005 | return false; |
| 7006 | } |
| 7007 | IsRecursive = TSC.removeIncomplete(ID); |
| 7008 | // The ABI requires unions to be sorted but not structures. |
| 7009 | // See FieldEncoding::operator< for sort algorithm. |
| 7010 | if (RT->isUnionType()) |
| 7011 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7012 | // We can now complete the TypeString. |
| 7013 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7014 | for (unsigned I = 0; I != E; ++I) { |
| 7015 | if (I) |
| 7016 | Enc += ','; |
| 7017 | Enc += FE[I].str(); |
| 7018 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7019 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7020 | Enc += '}'; |
| 7021 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 7022 | return true; |
| 7023 | } |
| 7024 | |
| 7025 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 7026 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 7027 | TypeStringCache &TSC, |
| 7028 | const IdentifierInfo *ID) { |
| 7029 | // Append the cached TypeString if we have one. |
| 7030 | StringRef TypeString = TSC.lookupStr(ID); |
| 7031 | if (!TypeString.empty()) { |
| 7032 | Enc += TypeString; |
| 7033 | return true; |
| 7034 | } |
| 7035 | |
| 7036 | size_t Start = Enc.size(); |
| 7037 | Enc += "e("; |
| 7038 | if (ID) |
| 7039 | Enc += ID->getName(); |
| 7040 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7041 | |
| 7042 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7043 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7044 | SmallVector<FieldEncoding, 16> FE; |
| 7045 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 7046 | ++I) { |
| 7047 | SmallStringEnc EnumEnc; |
| 7048 | EnumEnc += "m("; |
| 7049 | EnumEnc += I->getName(); |
| 7050 | EnumEnc += "){"; |
| 7051 | I->getInitVal().toString(EnumEnc); |
| 7052 | EnumEnc += '}'; |
| 7053 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 7054 | } |
| 7055 | std::sort(FE.begin(), FE.end()); |
| 7056 | unsigned E = FE.size(); |
| 7057 | for (unsigned I = 0; I != E; ++I) { |
| 7058 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7059 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 7060 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7061 | } |
| 7062 | } |
| 7063 | Enc += '}'; |
| 7064 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 7065 | return true; |
| 7066 | } |
| 7067 | |
| 7068 | /// Appends type's qualifier to Enc. |
| 7069 | /// This is done prior to appending the type's encoding. |
| 7070 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 7071 | // Qualifiers are emitted in alphabetical order. |
| 7072 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 7073 | int Lookup = 0; |
| 7074 | if (QT.isConstQualified()) |
| 7075 | Lookup += 1<<0; |
| 7076 | if (QT.isRestrictQualified()) |
| 7077 | Lookup += 1<<1; |
| 7078 | if (QT.isVolatileQualified()) |
| 7079 | Lookup += 1<<2; |
| 7080 | Enc += Table[Lookup]; |
| 7081 | } |
| 7082 | |
| 7083 | /// Appends built-in types to Enc. |
| 7084 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 7085 | const char *EncType; |
| 7086 | switch (BT->getKind()) { |
| 7087 | case BuiltinType::Void: |
| 7088 | EncType = "0"; |
| 7089 | break; |
| 7090 | case BuiltinType::Bool: |
| 7091 | EncType = "b"; |
| 7092 | break; |
| 7093 | case BuiltinType::Char_U: |
| 7094 | EncType = "uc"; |
| 7095 | break; |
| 7096 | case BuiltinType::UChar: |
| 7097 | EncType = "uc"; |
| 7098 | break; |
| 7099 | case BuiltinType::SChar: |
| 7100 | EncType = "sc"; |
| 7101 | break; |
| 7102 | case BuiltinType::UShort: |
| 7103 | EncType = "us"; |
| 7104 | break; |
| 7105 | case BuiltinType::Short: |
| 7106 | EncType = "ss"; |
| 7107 | break; |
| 7108 | case BuiltinType::UInt: |
| 7109 | EncType = "ui"; |
| 7110 | break; |
| 7111 | case BuiltinType::Int: |
| 7112 | EncType = "si"; |
| 7113 | break; |
| 7114 | case BuiltinType::ULong: |
| 7115 | EncType = "ul"; |
| 7116 | break; |
| 7117 | case BuiltinType::Long: |
| 7118 | EncType = "sl"; |
| 7119 | break; |
| 7120 | case BuiltinType::ULongLong: |
| 7121 | EncType = "ull"; |
| 7122 | break; |
| 7123 | case BuiltinType::LongLong: |
| 7124 | EncType = "sll"; |
| 7125 | break; |
| 7126 | case BuiltinType::Float: |
| 7127 | EncType = "ft"; |
| 7128 | break; |
| 7129 | case BuiltinType::Double: |
| 7130 | EncType = "d"; |
| 7131 | break; |
| 7132 | case BuiltinType::LongDouble: |
| 7133 | EncType = "ld"; |
| 7134 | break; |
| 7135 | default: |
| 7136 | return false; |
| 7137 | } |
| 7138 | Enc += EncType; |
| 7139 | return true; |
| 7140 | } |
| 7141 | |
| 7142 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 7143 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 7144 | const CodeGen::CodeGenModule &CGM, |
| 7145 | TypeStringCache &TSC) { |
| 7146 | Enc += "p("; |
| 7147 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 7148 | return false; |
| 7149 | Enc += ')'; |
| 7150 | return true; |
| 7151 | } |
| 7152 | |
| 7153 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7154 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 7155 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7156 | const CodeGen::CodeGenModule &CGM, |
| 7157 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 7158 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 7159 | return false; |
| 7160 | Enc += "a("; |
| 7161 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 7162 | CAT->getSize().toStringUnsigned(Enc); |
| 7163 | else |
| 7164 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 7165 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7166 | // The Qualifiers should be attached to the type rather than the array. |
| 7167 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7168 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 7169 | return false; |
| 7170 | Enc += ')'; |
| 7171 | return true; |
| 7172 | } |
| 7173 | |
| 7174 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7175 | /// and the arguments. |
| 7176 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7177 | const CodeGen::CodeGenModule &CGM, |
| 7178 | TypeStringCache &TSC) { |
| 7179 | Enc += "f{"; |
| 7180 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7181 | return false; |
| 7182 | Enc += "}("; |
| 7183 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7184 | // N.B. we are only interested in the adjusted param types. |
| 7185 | auto I = FPT->param_type_begin(); |
| 7186 | auto E = FPT->param_type_end(); |
| 7187 | if (I != E) { |
| 7188 | do { |
| 7189 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7190 | return false; |
| 7191 | ++I; |
| 7192 | if (I != E) |
| 7193 | Enc += ','; |
| 7194 | } while (I != E); |
| 7195 | if (FPT->isVariadic()) |
| 7196 | Enc += ",va"; |
| 7197 | } else { |
| 7198 | if (FPT->isVariadic()) |
| 7199 | Enc += "va"; |
| 7200 | else |
| 7201 | Enc += '0'; |
| 7202 | } |
| 7203 | } |
| 7204 | Enc += ')'; |
| 7205 | return true; |
| 7206 | } |
| 7207 | |
| 7208 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7209 | /// type encodings. |
| 7210 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7211 | const CodeGen::CodeGenModule &CGM, |
| 7212 | TypeStringCache &TSC) { |
| 7213 | |
| 7214 | QualType QT = QType.getCanonicalType(); |
| 7215 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7216 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7217 | // The Qualifiers should be attached to the type rather than the array. |
| 7218 | // Thus we don't call appendQualifier() here. |
| 7219 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7220 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7221 | appendQualifier(Enc, QT); |
| 7222 | |
| 7223 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7224 | return appendBuiltinType(Enc, BT); |
| 7225 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7226 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7227 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7228 | |
| 7229 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7230 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7231 | |
| 7232 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7233 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7234 | |
| 7235 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7236 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7237 | |
| 7238 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7239 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7240 | |
| 7241 | return false; |
| 7242 | } |
| 7243 | |
| 7244 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7245 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7246 | if (!D) |
| 7247 | return false; |
| 7248 | |
| 7249 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7250 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7251 | return false; |
| 7252 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7253 | } |
| 7254 | |
| 7255 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7256 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7257 | return false; |
| 7258 | QualType QT = VD->getType().getCanonicalType(); |
| 7259 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7260 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7261 | // The Qualifiers should be attached to the type rather than the array. |
| 7262 | // Thus we don't call appendQualifier() here. |
| 7263 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7264 | } |
| 7265 | return appendType(Enc, QT, CGM, TSC); |
| 7266 | } |
| 7267 | return false; |
| 7268 | } |
| 7269 | |
| 7270 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7271 | //===----------------------------------------------------------------------===// |
| 7272 | // Driver code |
| 7273 | //===----------------------------------------------------------------------===// |
| 7274 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7275 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7276 | return getTarget().getTriple(); |
| 7277 | } |
| 7278 | |
| 7279 | bool CodeGenModule::supportsCOMDAT() const { |
| 7280 | return !getTriple().isOSBinFormatMachO(); |
| 7281 | } |
| 7282 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7283 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7284 | if (TheTargetCodeGenInfo) |
| 7285 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7286 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7287 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7288 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7289 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7290 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7291 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7292 | case llvm::Triple::le32: |
| 7293 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7294 | case llvm::Triple::mips: |
| 7295 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 7296 | if (Triple.getOS() == llvm::Triple::NaCl) |
| 7297 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7298 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7299 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7300 | case llvm::Triple::mips64: |
| 7301 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7302 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7303 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7304 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7305 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7306 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7307 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7308 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7309 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7310 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 7313 | case llvm::Triple::wasm32: |
| 7314 | case llvm::Triple::wasm64: |
| 7315 | return *(TheTargetCodeGenInfo = new WebAssemblyTargetCodeGenInfo(Types)); |
| 7316 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7317 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7318 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7319 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7320 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7321 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7322 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7323 | TheTargetCodeGenInfo = |
| 7324 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7325 | return *TheTargetCodeGenInfo; |
| 7326 | } |
| 7327 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7328 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7329 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7330 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7331 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7332 | (CodeGenOpts.FloatABI != "soft" && |
| 7333 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7334 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7335 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7336 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7337 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7338 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7339 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7340 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7341 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7342 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7343 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7344 | if (getTarget().getABI() == "elfv2") |
| 7345 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7346 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7347 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7348 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7349 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7350 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7351 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7352 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7353 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7354 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7355 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7356 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7357 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7358 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7359 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7360 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7361 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7362 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7363 | case llvm::Triple::nvptx: |
| 7364 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7365 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7366 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7367 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7368 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7369 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7370 | case llvm::Triple::systemz: { |
| 7371 | bool HasVector = getTarget().getABI() == "vector"; |
| 7372 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7373 | HasVector)); |
| 7374 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7375 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7376 | case llvm::Triple::tce: |
| 7377 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7378 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7379 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7380 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 7381 | bool IsSmallStructInRegABI = |
| 7382 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7383 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7384 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7385 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7386 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
| 7387 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7388 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7389 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7390 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
| 7391 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7392 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7393 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7394 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7395 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7396 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7397 | StringRef ABI = getTarget().getABI(); |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 7398 | X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 : |
| 7399 | ABI == "avx" ? X86AVXABILevel::AVX : |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7400 | X86AVXABILevel::None); |
| 7401 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7402 | switch (Triple.getOS()) { |
| 7403 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7404 | return *(TheTargetCodeGenInfo = |
| 7405 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7406 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7407 | return *(TheTargetCodeGenInfo = |
| 7408 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7409 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7410 | return *(TheTargetCodeGenInfo = |
| 7411 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7412 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7413 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7414 | case llvm::Triple::hexagon: |
| 7415 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7416 | case llvm::Triple::r600: |
| 7417 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7418 | case llvm::Triple::amdgcn: |
| 7419 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7420 | case llvm::Triple::sparcv9: |
| 7421 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7422 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7423 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7424 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7425 | } |