1 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // These classes wrap the information about a call or function 10 // definition used to handle ABI compliancy. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "TargetInfo.h" 15 #include "ABIInfo.h" 16 #include "CGBlocks.h" 17 #include "CGCXXABI.h" 18 #include "CGValue.h" 19 #include "CodeGenFunction.h" 20 #include "clang/AST/Attr.h" 21 #include "clang/AST/RecordLayout.h" 22 #include "clang/Basic/CodeGenOptions.h" 23 #include "clang/Basic/DiagnosticFrontend.h" 24 #include "clang/CodeGen/CGFunctionInfo.h" 25 #include "clang/CodeGen/SwiftCallingConv.h" 26 #include "llvm/ADT/SmallBitVector.h" 27 #include "llvm/ADT/StringExtras.h" 28 #include "llvm/ADT/StringSwitch.h" 29 #include "llvm/ADT/Triple.h" 30 #include "llvm/ADT/Twine.h" 31 #include "llvm/IR/DataLayout.h" 32 #include "llvm/IR/IntrinsicsNVPTX.h" 33 #include "llvm/IR/Type.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include <algorithm> // std::sort 36 37 using namespace clang; 38 using namespace CodeGen; 39 40 // Helper for coercing an aggregate argument or return value into an integer 41 // array of the same size (including padding) and alignment. This alternate 42 // coercion happens only for the RenderScript ABI and can be removed after 43 // runtimes that rely on it are no longer supported. 44 // 45 // RenderScript assumes that the size of the argument / return value in the IR 46 // is the same as the size of the corresponding qualified type. This helper 47 // coerces the aggregate type into an array of the same size (including 48 // padding). This coercion is used in lieu of expansion of struct members or 49 // other canonical coercions that return a coerced-type of larger size. 50 // 51 // Ty - The argument / return value type 52 // Context - The associated ASTContext 53 // LLVMContext - The associated LLVMContext 54 static ABIArgInfo coerceToIntArray(QualType Ty, 55 ASTContext &Context, 56 llvm::LLVMContext &LLVMContext) { 57 // Alignment and Size are measured in bits. 58 const uint64_t Size = Context.getTypeSize(Ty); 59 const uint64_t Alignment = Context.getTypeAlign(Ty); 60 llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); 61 const uint64_t NumElements = (Size + Alignment - 1) / Alignment; 62 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); 63 } 64 65 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, 66 llvm::Value *Array, 67 llvm::Value *Value, 68 unsigned FirstIndex, 69 unsigned LastIndex) { 70 // Alternatively, we could emit this as a loop in the source. 71 for (unsigned I = FirstIndex; I <= LastIndex; ++I) { 72 llvm::Value *Cell = 73 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); 74 Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); 75 } 76 } 77 78 static bool isAggregateTypeForABI(QualType T) { 79 return !CodeGenFunction::hasScalarEvaluationKind(T) || 80 T->isMemberFunctionPointerType(); 81 } 82 83 ABIArgInfo 84 ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, 85 llvm::Type *Padding) const { 86 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), 87 ByRef, Realign, Padding); 88 } 89 90 ABIArgInfo 91 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { 92 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), 93 /*ByRef*/ false, Realign); 94 } 95 96 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 97 QualType Ty) const { 98 return Address::invalid(); 99 } 100 101 bool ABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const { 102 if (Ty->isPromotableIntegerType()) 103 return true; 104 105 if (const auto *EIT = Ty->getAs<ExtIntType>()) 106 if (EIT->getNumBits() < getContext().getTypeSize(getContext().IntTy)) 107 return true; 108 109 return false; 110 } 111 112 ABIInfo::~ABIInfo() {} 113 114 /// Does the given lowering require more than the given number of 115 /// registers when expanded? 116 /// 117 /// This is intended to be the basis of a reasonable basic implementation 118 /// of should{Pass,Return}IndirectlyForSwift. 119 /// 120 /// For most targets, a limit of four total registers is reasonable; this 121 /// limits the amount of code required in order to move around the value 122 /// in case it wasn't produced immediately prior to the call by the caller 123 /// (or wasn't produced in exactly the right registers) or isn't used 124 /// immediately within the callee. But some targets may need to further 125 /// limit the register count due to an inability to support that many 126 /// return registers. 127 static bool occupiesMoreThan(CodeGenTypes &cgt, 128 ArrayRef<llvm::Type*> scalarTypes, 129 unsigned maxAllRegisters) { 130 unsigned intCount = 0, fpCount = 0; 131 for (llvm::Type *type : scalarTypes) { 132 if (type->isPointerTy()) { 133 intCount++; 134 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { 135 auto ptrWidth = cgt.getTarget().getPointerWidth(0); 136 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; 137 } else { 138 assert(type->isVectorTy() || type->isFloatingPointTy()); 139 fpCount++; 140 } 141 } 142 143 return (intCount + fpCount > maxAllRegisters); 144 } 145 146 bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, 147 llvm::Type *eltTy, 148 unsigned numElts) const { 149 // The default implementation of this assumes that the target guarantees 150 // 128-bit SIMD support but nothing more. 151 return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); 152 } 153 154 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, 155 CGCXXABI &CXXABI) { 156 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 157 if (!RD) { 158 if (!RT->getDecl()->canPassInRegisters()) 159 return CGCXXABI::RAA_Indirect; 160 return CGCXXABI::RAA_Default; 161 } 162 return CXXABI.getRecordArgABI(RD); 163 } 164 165 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, 166 CGCXXABI &CXXABI) { 167 const RecordType *RT = T->getAs<RecordType>(); 168 if (!RT) 169 return CGCXXABI::RAA_Default; 170 return getRecordArgABI(RT, CXXABI); 171 } 172 173 static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, 174 const ABIInfo &Info) { 175 QualType Ty = FI.getReturnType(); 176 177 if (const auto *RT = Ty->getAs<RecordType>()) 178 if (!isa<CXXRecordDecl>(RT->getDecl()) && 179 !RT->getDecl()->canPassInRegisters()) { 180 FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty); 181 return true; 182 } 183 184 return CXXABI.classifyReturnType(FI); 185 } 186 187 /// Pass transparent unions as if they were the type of the first element. Sema 188 /// should ensure that all elements of the union have the same "machine type". 189 static QualType useFirstFieldIfTransparentUnion(QualType Ty) { 190 if (const RecordType *UT = Ty->getAsUnionType()) { 191 const RecordDecl *UD = UT->getDecl(); 192 if (UD->hasAttr<TransparentUnionAttr>()) { 193 assert(!UD->field_empty() && "sema created an empty transparent union"); 194 return UD->field_begin()->getType(); 195 } 196 } 197 return Ty; 198 } 199 200 CGCXXABI &ABIInfo::getCXXABI() const { 201 return CGT.getCXXABI(); 202 } 203 204 ASTContext &ABIInfo::getContext() const { 205 return CGT.getContext(); 206 } 207 208 llvm::LLVMContext &ABIInfo::getVMContext() const { 209 return CGT.getLLVMContext(); 210 } 211 212 const llvm::DataLayout &ABIInfo::getDataLayout() const { 213 return CGT.getDataLayout(); 214 } 215 216 const TargetInfo &ABIInfo::getTarget() const { 217 return CGT.getTarget(); 218 } 219 220 const CodeGenOptions &ABIInfo::getCodeGenOpts() const { 221 return CGT.getCodeGenOpts(); 222 } 223 224 bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); } 225 226 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 227 return false; 228 } 229 230 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 231 uint64_t Members) const { 232 return false; 233 } 234 235 LLVM_DUMP_METHOD void ABIArgInfo::dump() const { 236 raw_ostream &OS = llvm::errs(); 237 OS << "(ABIArgInfo Kind="; 238 switch (TheKind) { 239 case Direct: 240 OS << "Direct Type="; 241 if (llvm::Type *Ty = getCoerceToType()) 242 Ty->print(OS); 243 else 244 OS << "null"; 245 break; 246 case Extend: 247 OS << "Extend"; 248 break; 249 case Ignore: 250 OS << "Ignore"; 251 break; 252 case InAlloca: 253 OS << "InAlloca Offset=" << getInAllocaFieldIndex(); 254 break; 255 case Indirect: 256 OS << "Indirect Align=" << getIndirectAlign().getQuantity() 257 << " ByVal=" << getIndirectByVal() 258 << " Realign=" << getIndirectRealign(); 259 break; 260 case Expand: 261 OS << "Expand"; 262 break; 263 case CoerceAndExpand: 264 OS << "CoerceAndExpand Type="; 265 getCoerceAndExpandType()->print(OS); 266 break; 267 } 268 OS << ")\n"; 269 } 270 271 // Dynamically round a pointer up to a multiple of the given alignment. 272 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, 273 llvm::Value *Ptr, 274 CharUnits Align) { 275 llvm::Value *PtrAsInt = Ptr; 276 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; 277 PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); 278 PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, 279 llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); 280 PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, 281 llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); 282 PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, 283 Ptr->getType(), 284 Ptr->getName() + ".aligned"); 285 return PtrAsInt; 286 } 287 288 /// Emit va_arg for a platform using the common void* representation, 289 /// where arguments are simply emitted in an array of slots on the stack. 290 /// 291 /// This version implements the core direct-value passing rules. 292 /// 293 /// \param SlotSize - The size and alignment of a stack slot. 294 /// Each argument will be allocated to a multiple of this number of 295 /// slots, and all the slots will be aligned to this value. 296 /// \param AllowHigherAlign - The slot alignment is not a cap; 297 /// an argument type with an alignment greater than the slot size 298 /// will be emitted on a higher-alignment address, potentially 299 /// leaving one or more empty slots behind as padding. If this 300 /// is false, the returned address might be less-aligned than 301 /// DirectAlign. 302 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, 303 Address VAListAddr, 304 llvm::Type *DirectTy, 305 CharUnits DirectSize, 306 CharUnits DirectAlign, 307 CharUnits SlotSize, 308 bool AllowHigherAlign) { 309 // Cast the element type to i8* if necessary. Some platforms define 310 // va_list as a struct containing an i8* instead of just an i8*. 311 if (VAListAddr.getElementType() != CGF.Int8PtrTy) 312 VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); 313 314 llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); 315 316 // If the CC aligns values higher than the slot size, do so if needed. 317 Address Addr = Address::invalid(); 318 if (AllowHigherAlign && DirectAlign > SlotSize) { 319 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), 320 DirectAlign); 321 } else { 322 Addr = Address(Ptr, SlotSize); 323 } 324 325 // Advance the pointer past the argument, then store that back. 326 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); 327 Address NextPtr = 328 CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next"); 329 CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr); 330 331 // If the argument is smaller than a slot, and this is a big-endian 332 // target, the argument will be right-adjusted in its slot. 333 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && 334 !DirectTy->isStructTy()) { 335 Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); 336 } 337 338 Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); 339 return Addr; 340 } 341 342 /// Emit va_arg for a platform using the common void* representation, 343 /// where arguments are simply emitted in an array of slots on the stack. 344 /// 345 /// \param IsIndirect - Values of this type are passed indirectly. 346 /// \param ValueInfo - The size and alignment of this type, generally 347 /// computed with getContext().getTypeInfoInChars(ValueTy). 348 /// \param SlotSizeAndAlign - The size and alignment of a stack slot. 349 /// Each argument will be allocated to a multiple of this number of 350 /// slots, and all the slots will be aligned to this value. 351 /// \param AllowHigherAlign - The slot alignment is not a cap; 352 /// an argument type with an alignment greater than the slot size 353 /// will be emitted on a higher-alignment address, potentially 354 /// leaving one or more empty slots behind as padding. 355 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, 356 QualType ValueTy, bool IsIndirect, 357 std::pair<CharUnits, CharUnits> ValueInfo, 358 CharUnits SlotSizeAndAlign, 359 bool AllowHigherAlign) { 360 // The size and alignment of the value that was passed directly. 361 CharUnits DirectSize, DirectAlign; 362 if (IsIndirect) { 363 DirectSize = CGF.getPointerSize(); 364 DirectAlign = CGF.getPointerAlign(); 365 } else { 366 DirectSize = ValueInfo.first; 367 DirectAlign = ValueInfo.second; 368 } 369 370 // Cast the address we've calculated to the right type. 371 llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); 372 if (IsIndirect) 373 DirectTy = DirectTy->getPointerTo(0); 374 375 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, 376 DirectSize, DirectAlign, 377 SlotSizeAndAlign, 378 AllowHigherAlign); 379 380 if (IsIndirect) { 381 Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); 382 } 383 384 return Addr; 385 386 } 387 388 static Address emitMergePHI(CodeGenFunction &CGF, 389 Address Addr1, llvm::BasicBlock *Block1, 390 Address Addr2, llvm::BasicBlock *Block2, 391 const llvm::Twine &Name = "") { 392 assert(Addr1.getType() == Addr2.getType()); 393 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); 394 PHI->addIncoming(Addr1.getPointer(), Block1); 395 PHI->addIncoming(Addr2.getPointer(), Block2); 396 CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); 397 return Address(PHI, Align); 398 } 399 400 TargetCodeGenInfo::~TargetCodeGenInfo() = default; 401 402 // If someone can figure out a general rule for this, that would be great. 403 // It's probably just doomed to be platform-dependent, though. 404 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { 405 // Verified for: 406 // x86-64 FreeBSD, Linux, Darwin 407 // x86-32 FreeBSD, Linux, Darwin 408 // PowerPC Linux, Darwin 409 // ARM Darwin (*not* EABI) 410 // AArch64 Linux 411 return 32; 412 } 413 414 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, 415 const FunctionNoProtoType *fnType) const { 416 // The following conventions are known to require this to be false: 417 // x86_stdcall 418 // MIPS 419 // For everything else, we just prefer false unless we opt out. 420 return false; 421 } 422 423 void 424 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, 425 llvm::SmallString<24> &Opt) const { 426 // This assumes the user is passing a library name like "rt" instead of a 427 // filename like "librt.a/so", and that they don't care whether it's static or 428 // dynamic. 429 Opt = "-l"; 430 Opt += Lib; 431 } 432 433 unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { 434 // OpenCL kernels are called via an explicit runtime API with arguments 435 // set with clSetKernelArg(), not as normal sub-functions. 436 // Return SPIR_KERNEL by default as the kernel calling convention to 437 // ensure the fingerprint is fixed such way that each OpenCL argument 438 // gets one matching argument in the produced kernel function argument 439 // list to enable feasible implementation of clSetKernelArg() with 440 // aggregates etc. In case we would use the default C calling conv here, 441 // clSetKernelArg() might break depending on the target-specific 442 // conventions; different targets might split structs passed as values 443 // to multiple function arguments etc. 444 return llvm::CallingConv::SPIR_KERNEL; 445 } 446 447 llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, 448 llvm::PointerType *T, QualType QT) const { 449 return llvm::ConstantPointerNull::get(T); 450 } 451 452 LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, 453 const VarDecl *D) const { 454 assert(!CGM.getLangOpts().OpenCL && 455 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && 456 "Address space agnostic languages only"); 457 return D ? D->getType().getAddressSpace() : LangAS::Default; 458 } 459 460 llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( 461 CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, 462 LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const { 463 // Since target may map different address spaces in AST to the same address 464 // space, an address space conversion may end up as a bitcast. 465 if (auto *C = dyn_cast<llvm::Constant>(Src)) 466 return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); 467 // Try to preserve the source's name to make IR more readable. 468 return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( 469 Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : ""); 470 } 471 472 llvm::Constant * 473 TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, 474 LangAS SrcAddr, LangAS DestAddr, 475 llvm::Type *DestTy) const { 476 // Since target may map different address spaces in AST to the same address 477 // space, an address space conversion may end up as a bitcast. 478 return llvm::ConstantExpr::getPointerCast(Src, DestTy); 479 } 480 481 llvm::SyncScope::ID 482 TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, 483 SyncScope Scope, 484 llvm::AtomicOrdering Ordering, 485 llvm::LLVMContext &Ctx) const { 486 return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */ 487 } 488 489 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); 490 491 /// isEmptyField - Return true iff a the field is "empty", that is it 492 /// is an unnamed bit-field or an (array of) empty record(s). 493 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, 494 bool AllowArrays) { 495 if (FD->isUnnamedBitfield()) 496 return true; 497 498 QualType FT = FD->getType(); 499 500 // Constant arrays of empty records count as empty, strip them off. 501 // Constant arrays of zero length always count as empty. 502 if (AllowArrays) 503 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 504 if (AT->getSize() == 0) 505 return true; 506 FT = AT->getElementType(); 507 } 508 509 const RecordType *RT = FT->getAs<RecordType>(); 510 if (!RT) 511 return false; 512 513 // C++ record fields are never empty, at least in the Itanium ABI. 514 // 515 // FIXME: We should use a predicate for whether this behavior is true in the 516 // current ABI. 517 if (isa<CXXRecordDecl>(RT->getDecl())) 518 return false; 519 520 return isEmptyRecord(Context, FT, AllowArrays); 521 } 522 523 /// isEmptyRecord - Return true iff a structure contains only empty 524 /// fields. Note that a structure with a flexible array member is not 525 /// considered empty. 526 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { 527 const RecordType *RT = T->getAs<RecordType>(); 528 if (!RT) 529 return false; 530 const RecordDecl *RD = RT->getDecl(); 531 if (RD->hasFlexibleArrayMember()) 532 return false; 533 534 // If this is a C++ record, check the bases first. 535 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 536 for (const auto &I : CXXRD->bases()) 537 if (!isEmptyRecord(Context, I.getType(), true)) 538 return false; 539 540 for (const auto *I : RD->fields()) 541 if (!isEmptyField(Context, I, AllowArrays)) 542 return false; 543 return true; 544 } 545 546 /// isSingleElementStruct - Determine if a structure is a "single 547 /// element struct", i.e. it has exactly one non-empty field or 548 /// exactly one field which is itself a single element 549 /// struct. Structures with flexible array members are never 550 /// considered single element structs. 551 /// 552 /// \return The field declaration for the single non-empty field, if 553 /// it exists. 554 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { 555 const RecordType *RT = T->getAs<RecordType>(); 556 if (!RT) 557 return nullptr; 558 559 const RecordDecl *RD = RT->getDecl(); 560 if (RD->hasFlexibleArrayMember()) 561 return nullptr; 562 563 const Type *Found = nullptr; 564 565 // If this is a C++ record, check the bases first. 566 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 567 for (const auto &I : CXXRD->bases()) { 568 // Ignore empty records. 569 if (isEmptyRecord(Context, I.getType(), true)) 570 continue; 571 572 // If we already found an element then this isn't a single-element struct. 573 if (Found) 574 return nullptr; 575 576 // If this is non-empty and not a single element struct, the composite 577 // cannot be a single element struct. 578 Found = isSingleElementStruct(I.getType(), Context); 579 if (!Found) 580 return nullptr; 581 } 582 } 583 584 // Check for single element. 585 for (const auto *FD : RD->fields()) { 586 QualType FT = FD->getType(); 587 588 // Ignore empty fields. 589 if (isEmptyField(Context, FD, true)) 590 continue; 591 592 // If we already found an element then this isn't a single-element 593 // struct. 594 if (Found) 595 return nullptr; 596 597 // Treat single element arrays as the element. 598 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 599 if (AT->getSize().getZExtValue() != 1) 600 break; 601 FT = AT->getElementType(); 602 } 603 604 if (!isAggregateTypeForABI(FT)) { 605 Found = FT.getTypePtr(); 606 } else { 607 Found = isSingleElementStruct(FT, Context); 608 if (!Found) 609 return nullptr; 610 } 611 } 612 613 // We don't consider a struct a single-element struct if it has 614 // padding beyond the element type. 615 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) 616 return nullptr; 617 618 return Found; 619 } 620 621 namespace { 622 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, 623 const ABIArgInfo &AI) { 624 // This default implementation defers to the llvm backend's va_arg 625 // instruction. It can handle only passing arguments directly 626 // (typically only handled in the backend for primitive types), or 627 // aggregates passed indirectly by pointer (NOTE: if the "byval" 628 // flag has ABI impact in the callee, this implementation cannot 629 // work.) 630 631 // Only a few cases are covered here at the moment -- those needed 632 // by the default abi. 633 llvm::Value *Val; 634 635 if (AI.isIndirect()) { 636 assert(!AI.getPaddingType() && 637 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); 638 assert( 639 !AI.getIndirectRealign() && 640 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); 641 642 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); 643 CharUnits TyAlignForABI = TyInfo.second; 644 645 llvm::Type *BaseTy = 646 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 647 llvm::Value *Addr = 648 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); 649 return Address(Addr, TyAlignForABI); 650 } else { 651 assert((AI.isDirect() || AI.isExtend()) && 652 "Unexpected ArgInfo Kind in generic VAArg emitter!"); 653 654 assert(!AI.getInReg() && 655 "Unexpected InReg seen in arginfo in generic VAArg emitter!"); 656 assert(!AI.getPaddingType() && 657 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); 658 assert(!AI.getDirectOffset() && 659 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); 660 assert(!AI.getCoerceToType() && 661 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); 662 663 Address Temp = CGF.CreateMemTemp(Ty, "varet"); 664 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); 665 CGF.Builder.CreateStore(Val, Temp); 666 return Temp; 667 } 668 } 669 670 /// DefaultABIInfo - The default implementation for ABI specific 671 /// details. This implementation provides information which results in 672 /// self-consistent and sensible LLVM IR generation, but does not 673 /// conform to any particular ABI. 674 class DefaultABIInfo : public ABIInfo { 675 public: 676 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 677 678 ABIArgInfo classifyReturnType(QualType RetTy) const; 679 ABIArgInfo classifyArgumentType(QualType RetTy) const; 680 681 void computeInfo(CGFunctionInfo &FI) const override { 682 if (!getCXXABI().classifyReturnType(FI)) 683 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 684 for (auto &I : FI.arguments()) 685 I.info = classifyArgumentType(I.type); 686 } 687 688 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 689 QualType Ty) const override { 690 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); 691 } 692 }; 693 694 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { 695 public: 696 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 697 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} 698 }; 699 700 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { 701 Ty = useFirstFieldIfTransparentUnion(Ty); 702 703 if (isAggregateTypeForABI(Ty)) { 704 // Records with non-trivial destructors/copy-constructors should not be 705 // passed by value. 706 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 707 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 708 709 return getNaturalAlignIndirect(Ty); 710 } 711 712 // Treat an enum type as its underlying type. 713 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 714 Ty = EnumTy->getDecl()->getIntegerType(); 715 716 ASTContext &Context = getContext(); 717 if (const auto *EIT = Ty->getAs<ExtIntType>()) 718 if (EIT->getNumBits() > 719 Context.getTypeSize(Context.getTargetInfo().hasInt128Type() 720 ? Context.Int128Ty 721 : Context.LongLongTy)) 722 return getNaturalAlignIndirect(Ty); 723 724 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 725 : ABIArgInfo::getDirect()); 726 } 727 728 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { 729 if (RetTy->isVoidType()) 730 return ABIArgInfo::getIgnore(); 731 732 if (isAggregateTypeForABI(RetTy)) 733 return getNaturalAlignIndirect(RetTy); 734 735 // Treat an enum type as its underlying type. 736 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 737 RetTy = EnumTy->getDecl()->getIntegerType(); 738 739 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 740 if (EIT->getNumBits() > 741 getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type() 742 ? getContext().Int128Ty 743 : getContext().LongLongTy)) 744 return getNaturalAlignIndirect(RetTy); 745 746 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 747 : ABIArgInfo::getDirect()); 748 } 749 750 //===----------------------------------------------------------------------===// 751 // WebAssembly ABI Implementation 752 // 753 // This is a very simple ABI that relies a lot on DefaultABIInfo. 754 //===----------------------------------------------------------------------===// 755 756 class WebAssemblyABIInfo final : public SwiftABIInfo { 757 public: 758 enum ABIKind { 759 MVP = 0, 760 ExperimentalMV = 1, 761 }; 762 763 private: 764 DefaultABIInfo defaultInfo; 765 ABIKind Kind; 766 767 public: 768 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind) 769 : SwiftABIInfo(CGT), defaultInfo(CGT), Kind(Kind) {} 770 771 private: 772 ABIArgInfo classifyReturnType(QualType RetTy) const; 773 ABIArgInfo classifyArgumentType(QualType Ty) const; 774 775 // DefaultABIInfo's classifyReturnType and classifyArgumentType are 776 // non-virtual, but computeInfo and EmitVAArg are virtual, so we 777 // overload them. 778 void computeInfo(CGFunctionInfo &FI) const override { 779 if (!getCXXABI().classifyReturnType(FI)) 780 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 781 for (auto &Arg : FI.arguments()) 782 Arg.info = classifyArgumentType(Arg.type); 783 } 784 785 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 786 QualType Ty) const override; 787 788 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 789 bool asReturnValue) const override { 790 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 791 } 792 793 bool isSwiftErrorInRegister() const override { 794 return false; 795 } 796 }; 797 798 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { 799 public: 800 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 801 WebAssemblyABIInfo::ABIKind K) 802 : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {} 803 804 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 805 CodeGen::CodeGenModule &CGM) const override { 806 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 807 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { 808 if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) { 809 llvm::Function *Fn = cast<llvm::Function>(GV); 810 llvm::AttrBuilder B; 811 B.addAttribute("wasm-import-module", Attr->getImportModule()); 812 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 813 } 814 if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) { 815 llvm::Function *Fn = cast<llvm::Function>(GV); 816 llvm::AttrBuilder B; 817 B.addAttribute("wasm-import-name", Attr->getImportName()); 818 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 819 } 820 if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) { 821 llvm::Function *Fn = cast<llvm::Function>(GV); 822 llvm::AttrBuilder B; 823 B.addAttribute("wasm-export-name", Attr->getExportName()); 824 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 825 } 826 } 827 828 if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { 829 llvm::Function *Fn = cast<llvm::Function>(GV); 830 if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) 831 Fn->addFnAttr("no-prototype"); 832 } 833 } 834 }; 835 836 /// Classify argument of given type \p Ty. 837 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { 838 Ty = useFirstFieldIfTransparentUnion(Ty); 839 840 if (isAggregateTypeForABI(Ty)) { 841 // Records with non-trivial destructors/copy-constructors should not be 842 // passed by value. 843 if (auto RAA = getRecordArgABI(Ty, getCXXABI())) 844 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 845 // Ignore empty structs/unions. 846 if (isEmptyRecord(getContext(), Ty, true)) 847 return ABIArgInfo::getIgnore(); 848 // Lower single-element structs to just pass a regular value. TODO: We 849 // could do reasonable-size multiple-element structs too, using getExpand(), 850 // though watch out for things like bitfields. 851 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) 852 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 853 // For the experimental multivalue ABI, fully expand all other aggregates 854 if (Kind == ABIKind::ExperimentalMV) { 855 const RecordType *RT = Ty->getAs<RecordType>(); 856 assert(RT); 857 bool HasBitField = false; 858 for (auto *Field : RT->getDecl()->fields()) { 859 if (Field->isBitField()) { 860 HasBitField = true; 861 break; 862 } 863 } 864 if (!HasBitField) 865 return ABIArgInfo::getExpand(); 866 } 867 } 868 869 // Otherwise just do the default thing. 870 return defaultInfo.classifyArgumentType(Ty); 871 } 872 873 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { 874 if (isAggregateTypeForABI(RetTy)) { 875 // Records with non-trivial destructors/copy-constructors should not be 876 // returned by value. 877 if (!getRecordArgABI(RetTy, getCXXABI())) { 878 // Ignore empty structs/unions. 879 if (isEmptyRecord(getContext(), RetTy, true)) 880 return ABIArgInfo::getIgnore(); 881 // Lower single-element structs to just return a regular value. TODO: We 882 // could do reasonable-size multiple-element structs too, using 883 // ABIArgInfo::getDirect(). 884 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 885 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 886 // For the experimental multivalue ABI, return all other aggregates 887 if (Kind == ABIKind::ExperimentalMV) 888 return ABIArgInfo::getDirect(); 889 } 890 } 891 892 // Otherwise just do the default thing. 893 return defaultInfo.classifyReturnType(RetTy); 894 } 895 896 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 897 QualType Ty) const { 898 bool IsIndirect = isAggregateTypeForABI(Ty) && 899 !isEmptyRecord(getContext(), Ty, true) && 900 !isSingleElementStruct(Ty, getContext()); 901 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, 902 getContext().getTypeInfoInChars(Ty), 903 CharUnits::fromQuantity(4), 904 /*AllowHigherAlign=*/true); 905 } 906 907 //===----------------------------------------------------------------------===// 908 // le32/PNaCl bitcode ABI Implementation 909 // 910 // This is a simplified version of the x86_32 ABI. Arguments and return values 911 // are always passed on the stack. 912 //===----------------------------------------------------------------------===// 913 914 class PNaClABIInfo : public ABIInfo { 915 public: 916 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 917 918 ABIArgInfo classifyReturnType(QualType RetTy) const; 919 ABIArgInfo classifyArgumentType(QualType RetTy) const; 920 921 void computeInfo(CGFunctionInfo &FI) const override; 922 Address EmitVAArg(CodeGenFunction &CGF, 923 Address VAListAddr, QualType Ty) const override; 924 }; 925 926 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { 927 public: 928 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 929 : TargetCodeGenInfo(std::make_unique<PNaClABIInfo>(CGT)) {} 930 }; 931 932 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { 933 if (!getCXXABI().classifyReturnType(FI)) 934 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 935 936 for (auto &I : FI.arguments()) 937 I.info = classifyArgumentType(I.type); 938 } 939 940 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 941 QualType Ty) const { 942 // The PNaCL ABI is a bit odd, in that varargs don't use normal 943 // function classification. Structs get passed directly for varargs 944 // functions, through a rewriting transform in 945 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows 946 // this target to actually support a va_arg instructions with an 947 // aggregate type, unlike other targets. 948 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); 949 } 950 951 /// Classify argument of given type \p Ty. 952 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { 953 if (isAggregateTypeForABI(Ty)) { 954 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 955 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 956 return getNaturalAlignIndirect(Ty); 957 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 958 // Treat an enum type as its underlying type. 959 Ty = EnumTy->getDecl()->getIntegerType(); 960 } else if (Ty->isFloatingType()) { 961 // Floating-point types don't go inreg. 962 return ABIArgInfo::getDirect(); 963 } else if (const auto *EIT = Ty->getAs<ExtIntType>()) { 964 // Treat extended integers as integers if <=64, otherwise pass indirectly. 965 if (EIT->getNumBits() > 64) 966 return getNaturalAlignIndirect(Ty); 967 return ABIArgInfo::getDirect(); 968 } 969 970 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 971 : ABIArgInfo::getDirect()); 972 } 973 974 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { 975 if (RetTy->isVoidType()) 976 return ABIArgInfo::getIgnore(); 977 978 // In the PNaCl ABI we always return records/structures on the stack. 979 if (isAggregateTypeForABI(RetTy)) 980 return getNaturalAlignIndirect(RetTy); 981 982 // Treat extended integers as integers if <=64, otherwise pass indirectly. 983 if (const auto *EIT = RetTy->getAs<ExtIntType>()) { 984 if (EIT->getNumBits() > 64) 985 return getNaturalAlignIndirect(RetTy); 986 return ABIArgInfo::getDirect(); 987 } 988 989 // Treat an enum type as its underlying type. 990 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 991 RetTy = EnumTy->getDecl()->getIntegerType(); 992 993 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 994 : ABIArgInfo::getDirect()); 995 } 996 997 /// IsX86_MMXType - Return true if this is an MMX type. 998 bool IsX86_MMXType(llvm::Type *IRType) { 999 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. 1000 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && 1001 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && 1002 IRType->getScalarSizeInBits() != 64; 1003 } 1004 1005 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 1006 StringRef Constraint, 1007 llvm::Type* Ty) { 1008 bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) 1009 .Cases("y", "&y", "^Ym", true) 1010 .Default(false); 1011 if (IsMMXCons && Ty->isVectorTy()) { 1012 if (cast<llvm::VectorType>(Ty)->getPrimitiveSizeInBits().getFixedSize() != 1013 64) { 1014 // Invalid MMX constraint 1015 return nullptr; 1016 } 1017 1018 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); 1019 } 1020 1021 // No operation needed 1022 return Ty; 1023 } 1024 1025 /// Returns true if this type can be passed in SSE registers with the 1026 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. 1027 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { 1028 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 1029 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { 1030 if (BT->getKind() == BuiltinType::LongDouble) { 1031 if (&Context.getTargetInfo().getLongDoubleFormat() == 1032 &llvm::APFloat::x87DoubleExtended()) 1033 return false; 1034 } 1035 return true; 1036 } 1037 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 1038 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX 1039 // registers specially. 1040 unsigned VecSize = Context.getTypeSize(VT); 1041 if (VecSize == 128 || VecSize == 256 || VecSize == 512) 1042 return true; 1043 } 1044 return false; 1045 } 1046 1047 /// Returns true if this aggregate is small enough to be passed in SSE registers 1048 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. 1049 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { 1050 return NumMembers <= 4; 1051 } 1052 1053 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. 1054 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { 1055 auto AI = ABIArgInfo::getDirect(T); 1056 AI.setInReg(true); 1057 AI.setCanBeFlattened(false); 1058 return AI; 1059 } 1060 1061 //===----------------------------------------------------------------------===// 1062 // X86-32 ABI Implementation 1063 //===----------------------------------------------------------------------===// 1064 1065 /// Similar to llvm::CCState, but for Clang. 1066 struct CCState { 1067 CCState(CGFunctionInfo &FI) 1068 : IsPreassigned(FI.arg_size()), CC(FI.getCallingConvention()) {} 1069 1070 llvm::SmallBitVector IsPreassigned; 1071 unsigned CC = CallingConv::CC_C; 1072 unsigned FreeRegs = 0; 1073 unsigned FreeSSERegs = 0; 1074 }; 1075 1076 enum { 1077 // Vectorcall only allows the first 6 parameters to be passed in registers. 1078 VectorcallMaxParamNumAsReg = 6 1079 }; 1080 1081 /// X86_32ABIInfo - The X86-32 ABI information. 1082 class X86_32ABIInfo : public SwiftABIInfo { 1083 enum Class { 1084 Integer, 1085 Float 1086 }; 1087 1088 static const unsigned MinABIStackAlignInBytes = 4; 1089 1090 bool IsDarwinVectorABI; 1091 bool IsRetSmallStructInRegABI; 1092 bool IsWin32StructABI; 1093 bool IsSoftFloatABI; 1094 bool IsMCUABI; 1095 unsigned DefaultNumRegisterParameters; 1096 1097 static bool isRegisterSize(unsigned Size) { 1098 return (Size == 8 || Size == 16 || Size == 32 || Size == 64); 1099 } 1100 1101 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 1102 // FIXME: Assumes vectorcall is in use. 1103 return isX86VectorTypeForVectorCall(getContext(), Ty); 1104 } 1105 1106 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 1107 uint64_t NumMembers) const override { 1108 // FIXME: Assumes vectorcall is in use. 1109 return isX86VectorCallAggregateSmallEnough(NumMembers); 1110 } 1111 1112 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; 1113 1114 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1115 /// such that the argument will be passed in memory. 1116 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; 1117 1118 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; 1119 1120 /// Return the alignment to use for the given type on the stack. 1121 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; 1122 1123 Class classify(QualType Ty) const; 1124 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; 1125 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; 1126 1127 /// Updates the number of available free registers, returns 1128 /// true if any registers were allocated. 1129 bool updateFreeRegs(QualType Ty, CCState &State) const; 1130 1131 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, 1132 bool &NeedsPadding) const; 1133 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; 1134 1135 bool canExpandIndirectArgument(QualType Ty) const; 1136 1137 /// Rewrite the function info so that all memory arguments use 1138 /// inalloca. 1139 void rewriteWithInAlloca(CGFunctionInfo &FI) const; 1140 1141 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 1142 CharUnits &StackOffset, ABIArgInfo &Info, 1143 QualType Type) const; 1144 void runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const; 1145 1146 public: 1147 1148 void computeInfo(CGFunctionInfo &FI) const override; 1149 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 1150 QualType Ty) const override; 1151 1152 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, 1153 bool RetSmallStructInRegABI, bool Win32StructABI, 1154 unsigned NumRegisterParameters, bool SoftFloatABI) 1155 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), 1156 IsRetSmallStructInRegABI(RetSmallStructInRegABI), 1157 IsWin32StructABI(Win32StructABI), 1158 IsSoftFloatABI(SoftFloatABI), 1159 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), 1160 DefaultNumRegisterParameters(NumRegisterParameters) {} 1161 1162 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 1163 bool asReturnValue) const override { 1164 // LLVM's x86-32 lowering currently only assigns up to three 1165 // integer registers and three fp registers. Oddly, it'll use up to 1166 // four vector registers for vectors, but those can overlap with the 1167 // scalar registers. 1168 return occupiesMoreThan(CGT, scalars, /*total*/ 3); 1169 } 1170 1171 bool isSwiftErrorInRegister() const override { 1172 // x86-32 lowering does not support passing swifterror in a register. 1173 return false; 1174 } 1175 }; 1176 1177 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { 1178 public: 1179 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, 1180 bool RetSmallStructInRegABI, bool Win32StructABI, 1181 unsigned NumRegisterParameters, bool SoftFloatABI) 1182 : TargetCodeGenInfo(std::make_unique<X86_32ABIInfo>( 1183 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, 1184 NumRegisterParameters, SoftFloatABI)) {} 1185 1186 static bool isStructReturnInRegABI( 1187 const llvm::Triple &Triple, const CodeGenOptions &Opts); 1188 1189 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 1190 CodeGen::CodeGenModule &CGM) const override; 1191 1192 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 1193 // Darwin uses different dwarf register numbers for EH. 1194 if (CGM.getTarget().getTriple().isOSDarwin()) return 5; 1195 return 4; 1196 } 1197 1198 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 1199 llvm::Value *Address) const override; 1200 1201 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 1202 StringRef Constraint, 1203 llvm::Type* Ty) const override { 1204 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 1205 } 1206 1207 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, 1208 std::string &Constraints, 1209 std::vector<llvm::Type *> &ResultRegTypes, 1210 std::vector<llvm::Type *> &ResultTruncRegTypes, 1211 std::vector<LValue> &ResultRegDests, 1212 std::string &AsmString, 1213 unsigned NumOutputs) const override; 1214 1215 llvm::Constant * 1216 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 1217 unsigned Sig = (0xeb << 0) | // jmp rel8 1218 (0x06 << 8) | // .+0x08 1219 ('v' << 16) | 1220 ('2' << 24); 1221 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 1222 } 1223 1224 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 1225 return "movl\t%ebp, %ebp" 1226 "\t\t// marker for objc_retainAutoreleaseReturnValue"; 1227 } 1228 }; 1229 1230 } 1231 1232 /// Rewrite input constraint references after adding some output constraints. 1233 /// In the case where there is one output and one input and we add one output, 1234 /// we need to replace all operand references greater than or equal to 1: 1235 /// mov $0, $1 1236 /// mov eax, $1 1237 /// The result will be: 1238 /// mov $0, $2 1239 /// mov eax, $2 1240 static void rewriteInputConstraintReferences(unsigned FirstIn, 1241 unsigned NumNewOuts, 1242 std::string &AsmString) { 1243 std::string Buf; 1244 llvm::raw_string_ostream OS(Buf); 1245 size_t Pos = 0; 1246 while (Pos < AsmString.size()) { 1247 size_t DollarStart = AsmString.find('$', Pos); 1248 if (DollarStart == std::string::npos) 1249 DollarStart = AsmString.size(); 1250 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); 1251 if (DollarEnd == std::string::npos) 1252 DollarEnd = AsmString.size(); 1253 OS << StringRef(&AsmString[Pos], DollarEnd - Pos); 1254 Pos = DollarEnd; 1255 size_t NumDollars = DollarEnd - DollarStart; 1256 if (NumDollars % 2 != 0 && Pos < AsmString.size()) { 1257 // We have an operand reference. 1258 size_t DigitStart = Pos; 1259 if (AsmString[DigitStart] == '{') { 1260 OS << '{'; 1261 ++DigitStart; 1262 } 1263 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); 1264 if (DigitEnd == std::string::npos) 1265 DigitEnd = AsmString.size(); 1266 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); 1267 unsigned OperandIndex; 1268 if (!OperandStr.getAsInteger(10, OperandIndex)) { 1269 if (OperandIndex >= FirstIn) 1270 OperandIndex += NumNewOuts; 1271 OS << OperandIndex; 1272 } else { 1273 OS << OperandStr; 1274 } 1275 Pos = DigitEnd; 1276 } 1277 } 1278 AsmString = std::move(OS.str()); 1279 } 1280 1281 /// Add output constraints for EAX:EDX because they are return registers. 1282 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( 1283 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, 1284 std::vector<llvm::Type *> &ResultRegTypes, 1285 std::vector<llvm::Type *> &ResultTruncRegTypes, 1286 std::vector<LValue> &ResultRegDests, std::string &AsmString, 1287 unsigned NumOutputs) const { 1288 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); 1289 1290 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is 1291 // larger. 1292 if (!Constraints.empty()) 1293 Constraints += ','; 1294 if (RetWidth <= 32) { 1295 Constraints += "={eax}"; 1296 ResultRegTypes.push_back(CGF.Int32Ty); 1297 } else { 1298 // Use the 'A' constraint for EAX:EDX. 1299 Constraints += "=A"; 1300 ResultRegTypes.push_back(CGF.Int64Ty); 1301 } 1302 1303 // Truncate EAX or EAX:EDX to an integer of the appropriate size. 1304 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); 1305 ResultTruncRegTypes.push_back(CoerceTy); 1306 1307 // Coerce the integer by bitcasting the return slot pointer. 1308 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(CGF), 1309 CoerceTy->getPointerTo())); 1310 ResultRegDests.push_back(ReturnSlot); 1311 1312 rewriteInputConstraintReferences(NumOutputs, 1, AsmString); 1313 } 1314 1315 /// shouldReturnTypeInRegister - Determine if the given type should be 1316 /// returned in a register (for the Darwin and MCU ABI). 1317 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, 1318 ASTContext &Context) const { 1319 uint64_t Size = Context.getTypeSize(Ty); 1320 1321 // For i386, type must be register sized. 1322 // For the MCU ABI, it only needs to be <= 8-byte 1323 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) 1324 return false; 1325 1326 if (Ty->isVectorType()) { 1327 // 64- and 128- bit vectors inside structures are not returned in 1328 // registers. 1329 if (Size == 64 || Size == 128) 1330 return false; 1331 1332 return true; 1333 } 1334 1335 // If this is a builtin, pointer, enum, complex type, member pointer, or 1336 // member function pointer it is ok. 1337 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || 1338 Ty->isAnyComplexType() || Ty->isEnumeralType() || 1339 Ty->isBlockPointerType() || Ty->isMemberPointerType()) 1340 return true; 1341 1342 // Arrays are treated like records. 1343 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) 1344 return shouldReturnTypeInRegister(AT->getElementType(), Context); 1345 1346 // Otherwise, it must be a record type. 1347 const RecordType *RT = Ty->getAs<RecordType>(); 1348 if (!RT) return false; 1349 1350 // FIXME: Traverse bases here too. 1351 1352 // Structure types are passed in register if all fields would be 1353 // passed in a register. 1354 for (const auto *FD : RT->getDecl()->fields()) { 1355 // Empty fields are ignored. 1356 if (isEmptyField(Context, FD, true)) 1357 continue; 1358 1359 // Check fields recursively. 1360 if (!shouldReturnTypeInRegister(FD->getType(), Context)) 1361 return false; 1362 } 1363 return true; 1364 } 1365 1366 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { 1367 // Treat complex types as the element type. 1368 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 1369 Ty = CTy->getElementType(); 1370 1371 // Check for a type which we know has a simple scalar argument-passing 1372 // convention without any padding. (We're specifically looking for 32 1373 // and 64-bit integer and integer-equivalents, float, and double.) 1374 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && 1375 !Ty->isEnumeralType() && !Ty->isBlockPointerType()) 1376 return false; 1377 1378 uint64_t Size = Context.getTypeSize(Ty); 1379 return Size == 32 || Size == 64; 1380 } 1381 1382 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, 1383 uint64_t &Size) { 1384 for (const auto *FD : RD->fields()) { 1385 // Scalar arguments on the stack get 4 byte alignment on x86. If the 1386 // argument is smaller than 32-bits, expanding the struct will create 1387 // alignment padding. 1388 if (!is32Or64BitBasicType(FD->getType(), Context)) 1389 return false; 1390 1391 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know 1392 // how to expand them yet, and the predicate for telling if a bitfield still 1393 // counts as "basic" is more complicated than what we were doing previously. 1394 if (FD->isBitField()) 1395 return false; 1396 1397 Size += Context.getTypeSize(FD->getType()); 1398 } 1399 return true; 1400 } 1401 1402 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, 1403 uint64_t &Size) { 1404 // Don't do this if there are any non-empty bases. 1405 for (const CXXBaseSpecifier &Base : RD->bases()) { 1406 if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), 1407 Size)) 1408 return false; 1409 } 1410 if (!addFieldSizes(Context, RD, Size)) 1411 return false; 1412 return true; 1413 } 1414 1415 /// Test whether an argument type which is to be passed indirectly (on the 1416 /// stack) would have the equivalent layout if it was expanded into separate 1417 /// arguments. If so, we prefer to do the latter to avoid inhibiting 1418 /// optimizations. 1419 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { 1420 // We can only expand structure types. 1421 const RecordType *RT = Ty->getAs<RecordType>(); 1422 if (!RT) 1423 return false; 1424 const RecordDecl *RD = RT->getDecl(); 1425 uint64_t Size = 0; 1426 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 1427 if (!IsWin32StructABI) { 1428 // On non-Windows, we have to conservatively match our old bitcode 1429 // prototypes in order to be ABI-compatible at the bitcode level. 1430 if (!CXXRD->isCLike()) 1431 return false; 1432 } else { 1433 // Don't do this for dynamic classes. 1434 if (CXXRD->isDynamicClass()) 1435 return false; 1436 } 1437 if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) 1438 return false; 1439 } else { 1440 if (!addFieldSizes(getContext(), RD, Size)) 1441 return false; 1442 } 1443 1444 // We can do this if there was no alignment padding. 1445 return Size == getContext().getTypeSize(Ty); 1446 } 1447 1448 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { 1449 // If the return value is indirect, then the hidden argument is consuming one 1450 // integer register. 1451 if (State.FreeRegs) { 1452 --State.FreeRegs; 1453 if (!IsMCUABI) 1454 return getNaturalAlignIndirectInReg(RetTy); 1455 } 1456 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 1457 } 1458 1459 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, 1460 CCState &State) const { 1461 if (RetTy->isVoidType()) 1462 return ABIArgInfo::getIgnore(); 1463 1464 const Type *Base = nullptr; 1465 uint64_t NumElts = 0; 1466 if ((State.CC == llvm::CallingConv::X86_VectorCall || 1467 State.CC == llvm::CallingConv::X86_RegCall) && 1468 isHomogeneousAggregate(RetTy, Base, NumElts)) { 1469 // The LLVM struct type for such an aggregate should lower properly. 1470 return ABIArgInfo::getDirect(); 1471 } 1472 1473 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 1474 // On Darwin, some vectors are returned in registers. 1475 if (IsDarwinVectorABI) { 1476 uint64_t Size = getContext().getTypeSize(RetTy); 1477 1478 // 128-bit vectors are a special case; they are returned in 1479 // registers and we need to make sure to pick a type the LLVM 1480 // backend will like. 1481 if (Size == 128) 1482 return ABIArgInfo::getDirect(llvm::FixedVectorType::get( 1483 llvm::Type::getInt64Ty(getVMContext()), 2)); 1484 1485 // Always return in register if it fits in a general purpose 1486 // register, or if it is 64 bits and has a single element. 1487 if ((Size == 8 || Size == 16 || Size == 32) || 1488 (Size == 64 && VT->getNumElements() == 1)) 1489 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 1490 Size)); 1491 1492 return getIndirectReturnResult(RetTy, State); 1493 } 1494 1495 return ABIArgInfo::getDirect(); 1496 } 1497 1498 if (isAggregateTypeForABI(RetTy)) { 1499 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 1500 // Structures with flexible arrays are always indirect. 1501 if (RT->getDecl()->hasFlexibleArrayMember()) 1502 return getIndirectReturnResult(RetTy, State); 1503 } 1504 1505 // If specified, structs and unions are always indirect. 1506 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) 1507 return getIndirectReturnResult(RetTy, State); 1508 1509 // Ignore empty structs/unions. 1510 if (isEmptyRecord(getContext(), RetTy, true)) 1511 return ABIArgInfo::getIgnore(); 1512 1513 // Small structures which are register sized are generally returned 1514 // in a register. 1515 if (shouldReturnTypeInRegister(RetTy, getContext())) { 1516 uint64_t Size = getContext().getTypeSize(RetTy); 1517 1518 // As a special-case, if the struct is a "single-element" struct, and 1519 // the field is of type "float" or "double", return it in a 1520 // floating-point register. (MSVC does not apply this special case.) 1521 // We apply a similar transformation for pointer types to improve the 1522 // quality of the generated IR. 1523 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 1524 if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) 1525 || SeltTy->hasPointerRepresentation()) 1526 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 1527 1528 // FIXME: We should be able to narrow this integer in cases with dead 1529 // padding. 1530 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); 1531 } 1532 1533 return getIndirectReturnResult(RetTy, State); 1534 } 1535 1536 // Treat an enum type as its underlying type. 1537 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 1538 RetTy = EnumTy->getDecl()->getIntegerType(); 1539 1540 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 1541 if (EIT->getNumBits() > 64) 1542 return getIndirectReturnResult(RetTy, State); 1543 1544 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 1545 : ABIArgInfo::getDirect()); 1546 } 1547 1548 static bool isSIMDVectorType(ASTContext &Context, QualType Ty) { 1549 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; 1550 } 1551 1552 static bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty) { 1553 const RecordType *RT = Ty->getAs<RecordType>(); 1554 if (!RT) 1555 return 0; 1556 const RecordDecl *RD = RT->getDecl(); 1557 1558 // If this is a C++ record, check the bases first. 1559 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 1560 for (const auto &I : CXXRD->bases()) 1561 if (!isRecordWithSIMDVectorType(Context, I.getType())) 1562 return false; 1563 1564 for (const auto *i : RD->fields()) { 1565 QualType FT = i->getType(); 1566 1567 if (isSIMDVectorType(Context, FT)) 1568 return true; 1569 1570 if (isRecordWithSIMDVectorType(Context, FT)) 1571 return true; 1572 } 1573 1574 return false; 1575 } 1576 1577 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, 1578 unsigned Align) const { 1579 // Otherwise, if the alignment is less than or equal to the minimum ABI 1580 // alignment, just use the default; the backend will handle this. 1581 if (Align <= MinABIStackAlignInBytes) 1582 return 0; // Use default alignment. 1583 1584 // On non-Darwin, the stack type alignment is always 4. 1585 if (!IsDarwinVectorABI) { 1586 // Set explicit alignment, since we may need to realign the top. 1587 return MinABIStackAlignInBytes; 1588 } 1589 1590 // Otherwise, if the type contains an SSE vector type, the alignment is 16. 1591 if (Align >= 16 && (isSIMDVectorType(getContext(), Ty) || 1592 isRecordWithSIMDVectorType(getContext(), Ty))) 1593 return 16; 1594 1595 return MinABIStackAlignInBytes; 1596 } 1597 1598 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, 1599 CCState &State) const { 1600 if (!ByVal) { 1601 if (State.FreeRegs) { 1602 --State.FreeRegs; // Non-byval indirects just use one pointer. 1603 if (!IsMCUABI) 1604 return getNaturalAlignIndirectInReg(Ty); 1605 } 1606 return getNaturalAlignIndirect(Ty, false); 1607 } 1608 1609 // Compute the byval alignment. 1610 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 1611 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); 1612 if (StackAlign == 0) 1613 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); 1614 1615 // If the stack alignment is less than the type alignment, realign the 1616 // argument. 1617 bool Realign = TypeAlign > StackAlign; 1618 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), 1619 /*ByVal=*/true, Realign); 1620 } 1621 1622 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { 1623 const Type *T = isSingleElementStruct(Ty, getContext()); 1624 if (!T) 1625 T = Ty.getTypePtr(); 1626 1627 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 1628 BuiltinType::Kind K = BT->getKind(); 1629 if (K == BuiltinType::Float || K == BuiltinType::Double) 1630 return Float; 1631 } 1632 return Integer; 1633 } 1634 1635 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { 1636 if (!IsSoftFloatABI) { 1637 Class C = classify(Ty); 1638 if (C == Float) 1639 return false; 1640 } 1641 1642 unsigned Size = getContext().getTypeSize(Ty); 1643 unsigned SizeInRegs = (Size + 31) / 32; 1644 1645 if (SizeInRegs == 0) 1646 return false; 1647 1648 if (!IsMCUABI) { 1649 if (SizeInRegs > State.FreeRegs) { 1650 State.FreeRegs = 0; 1651 return false; 1652 } 1653 } else { 1654 // The MCU psABI allows passing parameters in-reg even if there are 1655 // earlier parameters that are passed on the stack. Also, 1656 // it does not allow passing >8-byte structs in-register, 1657 // even if there are 3 free registers available. 1658 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) 1659 return false; 1660 } 1661 1662 State.FreeRegs -= SizeInRegs; 1663 return true; 1664 } 1665 1666 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, 1667 bool &InReg, 1668 bool &NeedsPadding) const { 1669 // On Windows, aggregates other than HFAs are never passed in registers, and 1670 // they do not consume register slots. Homogenous floating-point aggregates 1671 // (HFAs) have already been dealt with at this point. 1672 if (IsWin32StructABI && isAggregateTypeForABI(Ty)) 1673 return false; 1674 1675 NeedsPadding = false; 1676 InReg = !IsMCUABI; 1677 1678 if (!updateFreeRegs(Ty, State)) 1679 return false; 1680 1681 if (IsMCUABI) 1682 return true; 1683 1684 if (State.CC == llvm::CallingConv::X86_FastCall || 1685 State.CC == llvm::CallingConv::X86_VectorCall || 1686 State.CC == llvm::CallingConv::X86_RegCall) { 1687 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) 1688 NeedsPadding = true; 1689 1690 return false; 1691 } 1692 1693 return true; 1694 } 1695 1696 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { 1697 if (!updateFreeRegs(Ty, State)) 1698 return false; 1699 1700 if (IsMCUABI) 1701 return false; 1702 1703 if (State.CC == llvm::CallingConv::X86_FastCall || 1704 State.CC == llvm::CallingConv::X86_VectorCall || 1705 State.CC == llvm::CallingConv::X86_RegCall) { 1706 if (getContext().getTypeSize(Ty) > 32) 1707 return false; 1708 1709 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || 1710 Ty->isReferenceType()); 1711 } 1712 1713 return true; 1714 } 1715 1716 void X86_32ABIInfo::runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const { 1717 // Vectorcall x86 works subtly different than in x64, so the format is 1718 // a bit different than the x64 version. First, all vector types (not HVAs) 1719 // are assigned, with the first 6 ending up in the [XYZ]MM0-5 registers. 1720 // This differs from the x64 implementation, where the first 6 by INDEX get 1721 // registers. 1722 // In the second pass over the arguments, HVAs are passed in the remaining 1723 // vector registers if possible, or indirectly by address. The address will be 1724 // passed in ECX/EDX if available. Any other arguments are passed according to 1725 // the usual fastcall rules. 1726 MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments(); 1727 for (int I = 0, E = Args.size(); I < E; ++I) { 1728 const Type *Base = nullptr; 1729 uint64_t NumElts = 0; 1730 const QualType &Ty = Args[I].type; 1731 if ((Ty->isVectorType() || Ty->isBuiltinType()) && 1732 isHomogeneousAggregate(Ty, Base, NumElts)) { 1733 if (State.FreeSSERegs >= NumElts) { 1734 State.FreeSSERegs -= NumElts; 1735 Args[I].info = ABIArgInfo::getDirectInReg(); 1736 State.IsPreassigned.set(I); 1737 } 1738 } 1739 } 1740 } 1741 1742 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, 1743 CCState &State) const { 1744 // FIXME: Set alignment on indirect arguments. 1745 bool IsFastCall = State.CC == llvm::CallingConv::X86_FastCall; 1746 bool IsRegCall = State.CC == llvm::CallingConv::X86_RegCall; 1747 bool IsVectorCall = State.CC == llvm::CallingConv::X86_VectorCall; 1748 1749 Ty = useFirstFieldIfTransparentUnion(Ty); 1750 TypeInfo TI = getContext().getTypeInfo(Ty); 1751 1752 // Check with the C++ ABI first. 1753 const RecordType *RT = Ty->getAs<RecordType>(); 1754 if (RT) { 1755 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 1756 if (RAA == CGCXXABI::RAA_Indirect) { 1757 return getIndirectResult(Ty, false, State); 1758 } else if (RAA == CGCXXABI::RAA_DirectInMemory) { 1759 // The field index doesn't matter, we'll fix it up later. 1760 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); 1761 } 1762 } 1763 1764 // Regcall uses the concept of a homogenous vector aggregate, similar 1765 // to other targets. 1766 const Type *Base = nullptr; 1767 uint64_t NumElts = 0; 1768 if ((IsRegCall || IsVectorCall) && 1769 isHomogeneousAggregate(Ty, Base, NumElts)) { 1770 if (State.FreeSSERegs >= NumElts) { 1771 State.FreeSSERegs -= NumElts; 1772 1773 // Vectorcall passes HVAs directly and does not flatten them, but regcall 1774 // does. 1775 if (IsVectorCall) 1776 return getDirectX86Hva(); 1777 1778 if (Ty->isBuiltinType() || Ty->isVectorType()) 1779 return ABIArgInfo::getDirect(); 1780 return ABIArgInfo::getExpand(); 1781 } 1782 return getIndirectResult(Ty, /*ByVal=*/false, State); 1783 } 1784 1785 if (isAggregateTypeForABI(Ty)) { 1786 // Structures with flexible arrays are always indirect. 1787 // FIXME: This should not be byval! 1788 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 1789 return getIndirectResult(Ty, true, State); 1790 1791 // Ignore empty structs/unions on non-Windows. 1792 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) 1793 return ABIArgInfo::getIgnore(); 1794 1795 llvm::LLVMContext &LLVMContext = getVMContext(); 1796 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 1797 bool NeedsPadding = false; 1798 bool InReg; 1799 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { 1800 unsigned SizeInRegs = (TI.Width + 31) / 32; 1801 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); 1802 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 1803 if (InReg) 1804 return ABIArgInfo::getDirectInReg(Result); 1805 else 1806 return ABIArgInfo::getDirect(Result); 1807 } 1808 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; 1809 1810 // Pass over-aligned aggregates on Windows indirectly. This behavior was 1811 // added in MSVC 2015. 1812 if (IsWin32StructABI && TI.AlignIsRequired && TI.Align > 32) 1813 return getIndirectResult(Ty, /*ByVal=*/false, State); 1814 1815 // Expand small (<= 128-bit) record types when we know that the stack layout 1816 // of those arguments will match the struct. This is important because the 1817 // LLVM backend isn't smart enough to remove byval, which inhibits many 1818 // optimizations. 1819 // Don't do this for the MCU if there are still free integer registers 1820 // (see X86_64 ABI for full explanation). 1821 if (TI.Width <= 4 * 32 && (!IsMCUABI || State.FreeRegs == 0) && 1822 canExpandIndirectArgument(Ty)) 1823 return ABIArgInfo::getExpandWithPadding( 1824 IsFastCall || IsVectorCall || IsRegCall, PaddingType); 1825 1826 return getIndirectResult(Ty, true, State); 1827 } 1828 1829 if (const VectorType *VT = Ty->getAs<VectorType>()) { 1830 // On Windows, vectors are passed directly if registers are available, or 1831 // indirectly if not. This avoids the need to align argument memory. Pass 1832 // user-defined vector types larger than 512 bits indirectly for simplicity. 1833 if (IsWin32StructABI) { 1834 if (TI.Width <= 512 && State.FreeSSERegs > 0) { 1835 --State.FreeSSERegs; 1836 return ABIArgInfo::getDirectInReg(); 1837 } 1838 return getIndirectResult(Ty, /*ByVal=*/false, State); 1839 } 1840 1841 // On Darwin, some vectors are passed in memory, we handle this by passing 1842 // it as an i8/i16/i32/i64. 1843 if (IsDarwinVectorABI) { 1844 if ((TI.Width == 8 || TI.Width == 16 || TI.Width == 32) || 1845 (TI.Width == 64 && VT->getNumElements() == 1)) 1846 return ABIArgInfo::getDirect( 1847 llvm::IntegerType::get(getVMContext(), TI.Width)); 1848 } 1849 1850 if (IsX86_MMXType(CGT.ConvertType(Ty))) 1851 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); 1852 1853 return ABIArgInfo::getDirect(); 1854 } 1855 1856 1857 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 1858 Ty = EnumTy->getDecl()->getIntegerType(); 1859 1860 bool InReg = shouldPrimitiveUseInReg(Ty, State); 1861 1862 if (isPromotableIntegerTypeForABI(Ty)) { 1863 if (InReg) 1864 return ABIArgInfo::getExtendInReg(Ty); 1865 return ABIArgInfo::getExtend(Ty); 1866 } 1867 1868 if (const auto * EIT = Ty->getAs<ExtIntType>()) { 1869 if (EIT->getNumBits() <= 64) { 1870 if (InReg) 1871 return ABIArgInfo::getDirectInReg(); 1872 return ABIArgInfo::getDirect(); 1873 } 1874 return getIndirectResult(Ty, /*ByVal=*/false, State); 1875 } 1876 1877 if (InReg) 1878 return ABIArgInfo::getDirectInReg(); 1879 return ABIArgInfo::getDirect(); 1880 } 1881 1882 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { 1883 CCState State(FI); 1884 if (IsMCUABI) 1885 State.FreeRegs = 3; 1886 else if (State.CC == llvm::CallingConv::X86_FastCall) { 1887 State.FreeRegs = 2; 1888 State.FreeSSERegs = 3; 1889 } else if (State.CC == llvm::CallingConv::X86_VectorCall) { 1890 State.FreeRegs = 2; 1891 State.FreeSSERegs = 6; 1892 } else if (FI.getHasRegParm()) 1893 State.FreeRegs = FI.getRegParm(); 1894 else if (State.CC == llvm::CallingConv::X86_RegCall) { 1895 State.FreeRegs = 5; 1896 State.FreeSSERegs = 8; 1897 } else if (IsWin32StructABI) { 1898 // Since MSVC 2015, the first three SSE vectors have been passed in 1899 // registers. The rest are passed indirectly. 1900 State.FreeRegs = DefaultNumRegisterParameters; 1901 State.FreeSSERegs = 3; 1902 } else 1903 State.FreeRegs = DefaultNumRegisterParameters; 1904 1905 if (!::classifyReturnType(getCXXABI(), FI, *this)) { 1906 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); 1907 } else if (FI.getReturnInfo().isIndirect()) { 1908 // The C++ ABI is not aware of register usage, so we have to check if the 1909 // return value was sret and put it in a register ourselves if appropriate. 1910 if (State.FreeRegs) { 1911 --State.FreeRegs; // The sret parameter consumes a register. 1912 if (!IsMCUABI) 1913 FI.getReturnInfo().setInReg(true); 1914 } 1915 } 1916 1917 // The chain argument effectively gives us another free register. 1918 if (FI.isChainCall()) 1919 ++State.FreeRegs; 1920 1921 // For vectorcall, do a first pass over the arguments, assigning FP and vector 1922 // arguments to XMM registers as available. 1923 if (State.CC == llvm::CallingConv::X86_VectorCall) 1924 runVectorCallFirstPass(FI, State); 1925 1926 bool UsedInAlloca = false; 1927 MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments(); 1928 for (int I = 0, E = Args.size(); I < E; ++I) { 1929 // Skip arguments that have already been assigned. 1930 if (State.IsPreassigned.test(I)) 1931 continue; 1932 1933 Args[I].info = classifyArgumentType(Args[I].type, State); 1934 UsedInAlloca |= (Args[I].info.getKind() == ABIArgInfo::InAlloca); 1935 } 1936 1937 // If we needed to use inalloca for any argument, do a second pass and rewrite 1938 // all the memory arguments to use inalloca. 1939 if (UsedInAlloca) 1940 rewriteWithInAlloca(FI); 1941 } 1942 1943 void 1944 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 1945 CharUnits &StackOffset, ABIArgInfo &Info, 1946 QualType Type) const { 1947 // Arguments are always 4-byte-aligned. 1948 CharUnits WordSize = CharUnits::fromQuantity(4); 1949 assert(StackOffset.isMultipleOf(WordSize) && "unaligned inalloca struct"); 1950 1951 // sret pointers and indirect things will require an extra pointer 1952 // indirection, unless they are byval. Most things are byval, and will not 1953 // require this indirection. 1954 bool IsIndirect = false; 1955 if (Info.isIndirect() && !Info.getIndirectByVal()) 1956 IsIndirect = true; 1957 Info = ABIArgInfo::getInAlloca(FrameFields.size(), IsIndirect); 1958 llvm::Type *LLTy = CGT.ConvertTypeForMem(Type); 1959 if (IsIndirect) 1960 LLTy = LLTy->getPointerTo(0); 1961 FrameFields.push_back(LLTy); 1962 StackOffset += IsIndirect ? WordSize : getContext().getTypeSizeInChars(Type); 1963 1964 // Insert padding bytes to respect alignment. 1965 CharUnits FieldEnd = StackOffset; 1966 StackOffset = FieldEnd.alignTo(WordSize); 1967 if (StackOffset != FieldEnd) { 1968 CharUnits NumBytes = StackOffset - FieldEnd; 1969 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); 1970 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); 1971 FrameFields.push_back(Ty); 1972 } 1973 } 1974 1975 static bool isArgInAlloca(const ABIArgInfo &Info) { 1976 // Leave ignored and inreg arguments alone. 1977 switch (Info.getKind()) { 1978 case ABIArgInfo::InAlloca: 1979 return true; 1980 case ABIArgInfo::Ignore: 1981 return false; 1982 case ABIArgInfo::Indirect: 1983 case ABIArgInfo::Direct: 1984 case ABIArgInfo::Extend: 1985 return !Info.getInReg(); 1986 case ABIArgInfo::Expand: 1987 case ABIArgInfo::CoerceAndExpand: 1988 // These are aggregate types which are never passed in registers when 1989 // inalloca is involved. 1990 return true; 1991 } 1992 llvm_unreachable("invalid enum"); 1993 } 1994 1995 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { 1996 assert(IsWin32StructABI && "inalloca only supported on win32"); 1997 1998 // Build a packed struct type for all of the arguments in memory. 1999 SmallVector<llvm::Type *, 6> FrameFields; 2000 2001 // The stack alignment is always 4. 2002 CharUnits StackAlign = CharUnits::fromQuantity(4); 2003 2004 CharUnits StackOffset; 2005 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); 2006 2007 // Put 'this' into the struct before 'sret', if necessary. 2008 bool IsThisCall = 2009 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; 2010 ABIArgInfo &Ret = FI.getReturnInfo(); 2011 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && 2012 isArgInAlloca(I->info)) { 2013 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 2014 ++I; 2015 } 2016 2017 // Put the sret parameter into the inalloca struct if it's in memory. 2018 if (Ret.isIndirect() && !Ret.getInReg()) { 2019 addFieldToArgStruct(FrameFields, StackOffset, Ret, FI.getReturnType()); 2020 // On Windows, the hidden sret parameter is always returned in eax. 2021 Ret.setInAllocaSRet(IsWin32StructABI); 2022 } 2023 2024 // Skip the 'this' parameter in ecx. 2025 if (IsThisCall) 2026 ++I; 2027 2028 // Put arguments passed in memory into the struct. 2029 for (; I != E; ++I) { 2030 if (isArgInAlloca(I->info)) 2031 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 2032 } 2033 2034 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, 2035 /*isPacked=*/true), 2036 StackAlign); 2037 } 2038 2039 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, 2040 Address VAListAddr, QualType Ty) const { 2041 2042 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 2043 2044 // x86-32 changes the alignment of certain arguments on the stack. 2045 // 2046 // Just messing with TypeInfo like this works because we never pass 2047 // anything indirectly. 2048 TypeInfo.second = CharUnits::fromQuantity( 2049 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); 2050 2051 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, 2052 TypeInfo, CharUnits::fromQuantity(4), 2053 /*AllowHigherAlign*/ true); 2054 } 2055 2056 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( 2057 const llvm::Triple &Triple, const CodeGenOptions &Opts) { 2058 assert(Triple.getArch() == llvm::Triple::x86); 2059 2060 switch (Opts.getStructReturnConvention()) { 2061 case CodeGenOptions::SRCK_Default: 2062 break; 2063 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return 2064 return false; 2065 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return 2066 return true; 2067 } 2068 2069 if (Triple.isOSDarwin() || Triple.isOSIAMCU()) 2070 return true; 2071 2072 switch (Triple.getOS()) { 2073 case llvm::Triple::DragonFly: 2074 case llvm::Triple::FreeBSD: 2075 case llvm::Triple::OpenBSD: 2076 case llvm::Triple::Win32: 2077 return true; 2078 default: 2079 return false; 2080 } 2081 } 2082 2083 void X86_32TargetCodeGenInfo::setTargetAttributes( 2084 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 2085 if (GV->isDeclaration()) 2086 return; 2087 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2088 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 2089 llvm::Function *Fn = cast<llvm::Function>(GV); 2090 Fn->addFnAttr("stackrealign"); 2091 } 2092 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2093 llvm::Function *Fn = cast<llvm::Function>(GV); 2094 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2095 } 2096 } 2097 } 2098 2099 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( 2100 CodeGen::CodeGenFunction &CGF, 2101 llvm::Value *Address) const { 2102 CodeGen::CGBuilderTy &Builder = CGF.Builder; 2103 2104 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 2105 2106 // 0-7 are the eight integer registers; the order is different 2107 // on Darwin (for EH), but the range is the same. 2108 // 8 is %eip. 2109 AssignToArrayRange(Builder, Address, Four8, 0, 8); 2110 2111 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { 2112 // 12-16 are st(0..4). Not sure why we stop at 4. 2113 // These have size 16, which is sizeof(long double) on 2114 // platforms with 8-byte alignment for that type. 2115 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); 2116 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); 2117 2118 } else { 2119 // 9 is %eflags, which doesn't get a size on Darwin for some 2120 // reason. 2121 Builder.CreateAlignedStore( 2122 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), 2123 CharUnits::One()); 2124 2125 // 11-16 are st(0..5). Not sure why we stop at 5. 2126 // These have size 12, which is sizeof(long double) on 2127 // platforms with 4-byte alignment for that type. 2128 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); 2129 AssignToArrayRange(Builder, Address, Twelve8, 11, 16); 2130 } 2131 2132 return false; 2133 } 2134 2135 //===----------------------------------------------------------------------===// 2136 // X86-64 ABI Implementation 2137 //===----------------------------------------------------------------------===// 2138 2139 2140 namespace { 2141 /// The AVX ABI level for X86 targets. 2142 enum class X86AVXABILevel { 2143 None, 2144 AVX, 2145 AVX512 2146 }; 2147 2148 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. 2149 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { 2150 switch (AVXLevel) { 2151 case X86AVXABILevel::AVX512: 2152 return 512; 2153 case X86AVXABILevel::AVX: 2154 return 256; 2155 case X86AVXABILevel::None: 2156 return 128; 2157 } 2158 llvm_unreachable("Unknown AVXLevel"); 2159 } 2160 2161 /// X86_64ABIInfo - The X86_64 ABI information. 2162 class X86_64ABIInfo : public SwiftABIInfo { 2163 enum Class { 2164 Integer = 0, 2165 SSE, 2166 SSEUp, 2167 X87, 2168 X87Up, 2169 ComplexX87, 2170 NoClass, 2171 Memory 2172 }; 2173 2174 /// merge - Implement the X86_64 ABI merging algorithm. 2175 /// 2176 /// Merge an accumulating classification \arg Accum with a field 2177 /// classification \arg Field. 2178 /// 2179 /// \param Accum - The accumulating classification. This should 2180 /// always be either NoClass or the result of a previous merge 2181 /// call. In addition, this should never be Memory (the caller 2182 /// should just return Memory for the aggregate). 2183 static Class merge(Class Accum, Class Field); 2184 2185 /// postMerge - Implement the X86_64 ABI post merging algorithm. 2186 /// 2187 /// Post merger cleanup, reduces a malformed Hi and Lo pair to 2188 /// final MEMORY or SSE classes when necessary. 2189 /// 2190 /// \param AggregateSize - The size of the current aggregate in 2191 /// the classification process. 2192 /// 2193 /// \param Lo - The classification for the parts of the type 2194 /// residing in the low word of the containing object. 2195 /// 2196 /// \param Hi - The classification for the parts of the type 2197 /// residing in the higher words of the containing object. 2198 /// 2199 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; 2200 2201 /// classify - Determine the x86_64 register classes in which the 2202 /// given type T should be passed. 2203 /// 2204 /// \param Lo - The classification for the parts of the type 2205 /// residing in the low word of the containing object. 2206 /// 2207 /// \param Hi - The classification for the parts of the type 2208 /// residing in the high word of the containing object. 2209 /// 2210 /// \param OffsetBase - The bit offset of this type in the 2211 /// containing object. Some parameters are classified different 2212 /// depending on whether they straddle an eightbyte boundary. 2213 /// 2214 /// \param isNamedArg - Whether the argument in question is a "named" 2215 /// argument, as used in AMD64-ABI 3.5.7. 2216 /// 2217 /// If a word is unused its result will be NoClass; if a type should 2218 /// be passed in Memory then at least the classification of \arg Lo 2219 /// will be Memory. 2220 /// 2221 /// The \arg Lo class will be NoClass iff the argument is ignored. 2222 /// 2223 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will 2224 /// also be ComplexX87. 2225 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, 2226 bool isNamedArg) const; 2227 2228 llvm::Type *GetByteVectorType(QualType Ty) const; 2229 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, 2230 unsigned IROffset, QualType SourceTy, 2231 unsigned SourceOffset) const; 2232 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, 2233 unsigned IROffset, QualType SourceTy, 2234 unsigned SourceOffset) const; 2235 2236 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 2237 /// such that the argument will be returned in memory. 2238 ABIArgInfo getIndirectReturnResult(QualType Ty) const; 2239 2240 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 2241 /// such that the argument will be passed in memory. 2242 /// 2243 /// \param freeIntRegs - The number of free integer registers remaining 2244 /// available. 2245 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; 2246 2247 ABIArgInfo classifyReturnType(QualType RetTy) const; 2248 2249 ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, 2250 unsigned &neededInt, unsigned &neededSSE, 2251 bool isNamedArg) const; 2252 2253 ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, 2254 unsigned &NeededSSE) const; 2255 2256 ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, 2257 unsigned &NeededSSE) const; 2258 2259 bool IsIllegalVectorType(QualType Ty) const; 2260 2261 /// The 0.98 ABI revision clarified a lot of ambiguities, 2262 /// unfortunately in ways that were not always consistent with 2263 /// certain previous compilers. In particular, platforms which 2264 /// required strict binary compatibility with older versions of GCC 2265 /// may need to exempt themselves. 2266 bool honorsRevision0_98() const { 2267 return !getTarget().getTriple().isOSDarwin(); 2268 } 2269 2270 /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to 2271 /// classify it as INTEGER (for compatibility with older clang compilers). 2272 bool classifyIntegerMMXAsSSE() const { 2273 // Clang <= 3.8 did not do this. 2274 if (getContext().getLangOpts().getClangABICompat() <= 2275 LangOptions::ClangABI::Ver3_8) 2276 return false; 2277 2278 const llvm::Triple &Triple = getTarget().getTriple(); 2279 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) 2280 return false; 2281 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) 2282 return false; 2283 return true; 2284 } 2285 2286 // GCC classifies vectors of __int128 as memory. 2287 bool passInt128VectorsInMem() const { 2288 // Clang <= 9.0 did not do this. 2289 if (getContext().getLangOpts().getClangABICompat() <= 2290 LangOptions::ClangABI::Ver9) 2291 return false; 2292 2293 const llvm::Triple &T = getTarget().getTriple(); 2294 return T.isOSLinux() || T.isOSNetBSD(); 2295 } 2296 2297 X86AVXABILevel AVXLevel; 2298 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on 2299 // 64-bit hardware. 2300 bool Has64BitPointers; 2301 2302 public: 2303 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : 2304 SwiftABIInfo(CGT), AVXLevel(AVXLevel), 2305 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { 2306 } 2307 2308 bool isPassedUsingAVXType(QualType type) const { 2309 unsigned neededInt, neededSSE; 2310 // The freeIntRegs argument doesn't matter here. 2311 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, 2312 /*isNamedArg*/true); 2313 if (info.isDirect()) { 2314 llvm::Type *ty = info.getCoerceToType(); 2315 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) 2316 return vectorTy->getPrimitiveSizeInBits().getFixedSize() > 128; 2317 } 2318 return false; 2319 } 2320 2321 void computeInfo(CGFunctionInfo &FI) const override; 2322 2323 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 2324 QualType Ty) const override; 2325 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 2326 QualType Ty) const override; 2327 2328 bool has64BitPointers() const { 2329 return Has64BitPointers; 2330 } 2331 2332 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 2333 bool asReturnValue) const override { 2334 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 2335 } 2336 bool isSwiftErrorInRegister() const override { 2337 return true; 2338 } 2339 }; 2340 2341 /// WinX86_64ABIInfo - The Windows X86_64 ABI information. 2342 class WinX86_64ABIInfo : public SwiftABIInfo { 2343 public: 2344 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) 2345 : SwiftABIInfo(CGT), AVXLevel(AVXLevel), 2346 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} 2347 2348 void computeInfo(CGFunctionInfo &FI) const override; 2349 2350 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 2351 QualType Ty) const override; 2352 2353 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 2354 // FIXME: Assumes vectorcall is in use. 2355 return isX86VectorTypeForVectorCall(getContext(), Ty); 2356 } 2357 2358 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 2359 uint64_t NumMembers) const override { 2360 // FIXME: Assumes vectorcall is in use. 2361 return isX86VectorCallAggregateSmallEnough(NumMembers); 2362 } 2363 2364 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, 2365 bool asReturnValue) const override { 2366 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 2367 } 2368 2369 bool isSwiftErrorInRegister() const override { 2370 return true; 2371 } 2372 2373 private: 2374 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, 2375 bool IsVectorCall, bool IsRegCall) const; 2376 ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, 2377 const ABIArgInfo ¤t) const; 2378 void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, 2379 bool IsVectorCall, bool IsRegCall) const; 2380 2381 X86AVXABILevel AVXLevel; 2382 2383 bool IsMingw64; 2384 }; 2385 2386 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { 2387 public: 2388 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) 2389 : TargetCodeGenInfo(std::make_unique<X86_64ABIInfo>(CGT, AVXLevel)) {} 2390 2391 const X86_64ABIInfo &getABIInfo() const { 2392 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); 2393 } 2394 2395 /// Disable tail call on x86-64. The epilogue code before the tail jump blocks 2396 /// the autoreleaseRV/retainRV optimization. 2397 bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override { 2398 return true; 2399 } 2400 2401 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 2402 return 7; 2403 } 2404 2405 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2406 llvm::Value *Address) const override { 2407 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 2408 2409 // 0-15 are the 16 integer registers. 2410 // 16 is %rip. 2411 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 2412 return false; 2413 } 2414 2415 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 2416 StringRef Constraint, 2417 llvm::Type* Ty) const override { 2418 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 2419 } 2420 2421 bool isNoProtoCallVariadic(const CallArgList &args, 2422 const FunctionNoProtoType *fnType) const override { 2423 // The default CC on x86-64 sets %al to the number of SSA 2424 // registers used, and GCC sets this when calling an unprototyped 2425 // function, so we override the default behavior. However, don't do 2426 // that when AVX types are involved: the ABI explicitly states it is 2427 // undefined, and it doesn't work in practice because of how the ABI 2428 // defines varargs anyway. 2429 if (fnType->getCallConv() == CC_C) { 2430 bool HasAVXType = false; 2431 for (CallArgList::const_iterator 2432 it = args.begin(), ie = args.end(); it != ie; ++it) { 2433 if (getABIInfo().isPassedUsingAVXType(it->Ty)) { 2434 HasAVXType = true; 2435 break; 2436 } 2437 } 2438 2439 if (!HasAVXType) 2440 return true; 2441 } 2442 2443 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); 2444 } 2445 2446 llvm::Constant * 2447 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 2448 unsigned Sig = (0xeb << 0) | // jmp rel8 2449 (0x06 << 8) | // .+0x08 2450 ('v' << 16) | 2451 ('2' << 24); 2452 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 2453 } 2454 2455 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2456 CodeGen::CodeGenModule &CGM) const override { 2457 if (GV->isDeclaration()) 2458 return; 2459 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2460 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 2461 llvm::Function *Fn = cast<llvm::Function>(GV); 2462 Fn->addFnAttr("stackrealign"); 2463 } 2464 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2465 llvm::Function *Fn = cast<llvm::Function>(GV); 2466 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2467 } 2468 } 2469 } 2470 2471 void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc, 2472 const FunctionDecl *Caller, 2473 const FunctionDecl *Callee, 2474 const CallArgList &Args) const override; 2475 }; 2476 2477 static void initFeatureMaps(const ASTContext &Ctx, 2478 llvm::StringMap<bool> &CallerMap, 2479 const FunctionDecl *Caller, 2480 llvm::StringMap<bool> &CalleeMap, 2481 const FunctionDecl *Callee) { 2482 if (CalleeMap.empty() && CallerMap.empty()) { 2483 // The caller is potentially nullptr in the case where the call isn't in a 2484 // function. In this case, the getFunctionFeatureMap ensures we just get 2485 // the TU level setting (since it cannot be modified by 'target'.. 2486 Ctx.getFunctionFeatureMap(CallerMap, Caller); 2487 Ctx.getFunctionFeatureMap(CalleeMap, Callee); 2488 } 2489 } 2490 2491 static bool checkAVXParamFeature(DiagnosticsEngine &Diag, 2492 SourceLocation CallLoc, 2493 const llvm::StringMap<bool> &CallerMap, 2494 const llvm::StringMap<bool> &CalleeMap, 2495 QualType Ty, StringRef Feature, 2496 bool IsArgument) { 2497 bool CallerHasFeat = CallerMap.lookup(Feature); 2498 bool CalleeHasFeat = CalleeMap.lookup(Feature); 2499 if (!CallerHasFeat && !CalleeHasFeat) 2500 return Diag.Report(CallLoc, diag::warn_avx_calling_convention) 2501 << IsArgument << Ty << Feature; 2502 2503 // Mixing calling conventions here is very clearly an error. 2504 if (!CallerHasFeat || !CalleeHasFeat) 2505 return Diag.Report(CallLoc, diag::err_avx_calling_convention) 2506 << IsArgument << Ty << Feature; 2507 2508 // Else, both caller and callee have the required feature, so there is no need 2509 // to diagnose. 2510 return false; 2511 } 2512 2513 static bool checkAVXParam(DiagnosticsEngine &Diag, ASTContext &Ctx, 2514 SourceLocation CallLoc, 2515 const llvm::StringMap<bool> &CallerMap, 2516 const llvm::StringMap<bool> &CalleeMap, QualType Ty, 2517 bool IsArgument) { 2518 uint64_t Size = Ctx.getTypeSize(Ty); 2519 if (Size > 256) 2520 return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, 2521 "avx512f", IsArgument); 2522 2523 if (Size > 128) 2524 return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, "avx", 2525 IsArgument); 2526 2527 return false; 2528 } 2529 2530 void X86_64TargetCodeGenInfo::checkFunctionCallABI( 2531 CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller, 2532 const FunctionDecl *Callee, const CallArgList &Args) const { 2533 llvm::StringMap<bool> CallerMap; 2534 llvm::StringMap<bool> CalleeMap; 2535 unsigned ArgIndex = 0; 2536 2537 // We need to loop through the actual call arguments rather than the the 2538 // function's parameters, in case this variadic. 2539 for (const CallArg &Arg : Args) { 2540 // The "avx" feature changes how vectors >128 in size are passed. "avx512f" 2541 // additionally changes how vectors >256 in size are passed. Like GCC, we 2542 // warn when a function is called with an argument where this will change. 2543 // Unlike GCC, we also error when it is an obvious ABI mismatch, that is, 2544 // the caller and callee features are mismatched. 2545 // Unfortunately, we cannot do this diagnostic in SEMA, since the callee can 2546 // change its ABI with attribute-target after this call. 2547 if (Arg.getType()->isVectorType() && 2548 CGM.getContext().getTypeSize(Arg.getType()) > 128) { 2549 initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee); 2550 QualType Ty = Arg.getType(); 2551 // The CallArg seems to have desugared the type already, so for clearer 2552 // diagnostics, replace it with the type in the FunctionDecl if possible. 2553 if (ArgIndex < Callee->getNumParams()) 2554 Ty = Callee->getParamDecl(ArgIndex)->getType(); 2555 2556 if (checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap, 2557 CalleeMap, Ty, /*IsArgument*/ true)) 2558 return; 2559 } 2560 ++ArgIndex; 2561 } 2562 2563 // Check return always, as we don't have a good way of knowing in codegen 2564 // whether this value is used, tail-called, etc. 2565 if (Callee->getReturnType()->isVectorType() && 2566 CGM.getContext().getTypeSize(Callee->getReturnType()) > 128) { 2567 initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee); 2568 checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap, 2569 CalleeMap, Callee->getReturnType(), 2570 /*IsArgument*/ false); 2571 } 2572 } 2573 2574 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { 2575 // If the argument does not end in .lib, automatically add the suffix. 2576 // If the argument contains a space, enclose it in quotes. 2577 // This matches the behavior of MSVC. 2578 bool Quote = (Lib.find(" ") != StringRef::npos); 2579 std::string ArgStr = Quote ? "\"" : ""; 2580 ArgStr += Lib; 2581 if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) 2582 ArgStr += ".lib"; 2583 ArgStr += Quote ? "\"" : ""; 2584 return ArgStr; 2585 } 2586 2587 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { 2588 public: 2589 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 2590 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, 2591 unsigned NumRegisterParameters) 2592 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, 2593 Win32StructABI, NumRegisterParameters, false) {} 2594 2595 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2596 CodeGen::CodeGenModule &CGM) const override; 2597 2598 void getDependentLibraryOption(llvm::StringRef Lib, 2599 llvm::SmallString<24> &Opt) const override { 2600 Opt = "/DEFAULTLIB:"; 2601 Opt += qualifyWindowsLibrary(Lib); 2602 } 2603 2604 void getDetectMismatchOption(llvm::StringRef Name, 2605 llvm::StringRef Value, 2606 llvm::SmallString<32> &Opt) const override { 2607 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 2608 } 2609 }; 2610 2611 static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2612 CodeGen::CodeGenModule &CGM) { 2613 if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { 2614 2615 if (CGM.getCodeGenOpts().StackProbeSize != 4096) 2616 Fn->addFnAttr("stack-probe-size", 2617 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); 2618 if (CGM.getCodeGenOpts().NoStackArgProbe) 2619 Fn->addFnAttr("no-stack-arg-probe"); 2620 } 2621 } 2622 2623 void WinX86_32TargetCodeGenInfo::setTargetAttributes( 2624 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 2625 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 2626 if (GV->isDeclaration()) 2627 return; 2628 addStackProbeTargetAttributes(D, GV, CGM); 2629 } 2630 2631 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 2632 public: 2633 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 2634 X86AVXABILevel AVXLevel) 2635 : TargetCodeGenInfo(std::make_unique<WinX86_64ABIInfo>(CGT, AVXLevel)) {} 2636 2637 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2638 CodeGen::CodeGenModule &CGM) const override; 2639 2640 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 2641 return 7; 2642 } 2643 2644 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2645 llvm::Value *Address) const override { 2646 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 2647 2648 // 0-15 are the 16 integer registers. 2649 // 16 is %rip. 2650 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 2651 return false; 2652 } 2653 2654 void getDependentLibraryOption(llvm::StringRef Lib, 2655 llvm::SmallString<24> &Opt) const override { 2656 Opt = "/DEFAULTLIB:"; 2657 Opt += qualifyWindowsLibrary(Lib); 2658 } 2659 2660 void getDetectMismatchOption(llvm::StringRef Name, 2661 llvm::StringRef Value, 2662 llvm::SmallString<32> &Opt) const override { 2663 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 2664 } 2665 }; 2666 2667 void WinX86_64TargetCodeGenInfo::setTargetAttributes( 2668 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 2669 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 2670 if (GV->isDeclaration()) 2671 return; 2672 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2673 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 2674 llvm::Function *Fn = cast<llvm::Function>(GV); 2675 Fn->addFnAttr("stackrealign"); 2676 } 2677 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2678 llvm::Function *Fn = cast<llvm::Function>(GV); 2679 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2680 } 2681 } 2682 2683 addStackProbeTargetAttributes(D, GV, CGM); 2684 } 2685 } 2686 2687 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, 2688 Class &Hi) const { 2689 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: 2690 // 2691 // (a) If one of the classes is Memory, the whole argument is passed in 2692 // memory. 2693 // 2694 // (b) If X87UP is not preceded by X87, the whole argument is passed in 2695 // memory. 2696 // 2697 // (c) If the size of the aggregate exceeds two eightbytes and the first 2698 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole 2699 // argument is passed in memory. NOTE: This is necessary to keep the 2700 // ABI working for processors that don't support the __m256 type. 2701 // 2702 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. 2703 // 2704 // Some of these are enforced by the merging logic. Others can arise 2705 // only with unions; for example: 2706 // union { _Complex double; unsigned; } 2707 // 2708 // Note that clauses (b) and (c) were added in 0.98. 2709 // 2710 if (Hi == Memory) 2711 Lo = Memory; 2712 if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) 2713 Lo = Memory; 2714 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) 2715 Lo = Memory; 2716 if (Hi == SSEUp && Lo != SSE) 2717 Hi = SSE; 2718 } 2719 2720 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { 2721 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is 2722 // classified recursively so that always two fields are 2723 // considered. The resulting class is calculated according to 2724 // the classes of the fields in the eightbyte: 2725 // 2726 // (a) If both classes are equal, this is the resulting class. 2727 // 2728 // (b) If one of the classes is NO_CLASS, the resulting class is 2729 // the other class. 2730 // 2731 // (c) If one of the classes is MEMORY, the result is the MEMORY 2732 // class. 2733 // 2734 // (d) If one of the classes is INTEGER, the result is the 2735 // INTEGER. 2736 // 2737 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, 2738 // MEMORY is used as class. 2739 // 2740 // (f) Otherwise class SSE is used. 2741 2742 // Accum should never be memory (we should have returned) or 2743 // ComplexX87 (because this cannot be passed in a structure). 2744 assert((Accum != Memory && Accum != ComplexX87) && 2745 "Invalid accumulated classification during merge."); 2746 if (Accum == Field || Field == NoClass) 2747 return Accum; 2748 if (Field == Memory) 2749 return Memory; 2750 if (Accum == NoClass) 2751 return Field; 2752 if (Accum == Integer || Field == Integer) 2753 return Integer; 2754 if (Field == X87 || Field == X87Up || Field == ComplexX87 || 2755 Accum == X87 || Accum == X87Up) 2756 return Memory; 2757 return SSE; 2758 } 2759 2760 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, 2761 Class &Lo, Class &Hi, bool isNamedArg) const { 2762 // FIXME: This code can be simplified by introducing a simple value class for 2763 // Class pairs with appropriate constructor methods for the various 2764 // situations. 2765 2766 // FIXME: Some of the split computations are wrong; unaligned vectors 2767 // shouldn't be passed in registers for example, so there is no chance they 2768 // can straddle an eightbyte. Verify & simplify. 2769 2770 Lo = Hi = NoClass; 2771 2772 Class &Current = OffsetBase < 64 ? Lo : Hi; 2773 Current = Memory; 2774 2775 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 2776 BuiltinType::Kind k = BT->getKind(); 2777 2778 if (k == BuiltinType::Void) { 2779 Current = NoClass; 2780 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { 2781 Lo = Integer; 2782 Hi = Integer; 2783 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { 2784 Current = Integer; 2785 } else if (k == BuiltinType::Float || k == BuiltinType::Double) { 2786 Current = SSE; 2787 } else if (k == BuiltinType::LongDouble) { 2788 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 2789 if (LDF == &llvm::APFloat::IEEEquad()) { 2790 Lo = SSE; 2791 Hi = SSEUp; 2792 } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { 2793 Lo = X87; 2794 Hi = X87Up; 2795 } else if (LDF == &llvm::APFloat::IEEEdouble()) { 2796 Current = SSE; 2797 } else 2798 llvm_unreachable("unexpected long double representation!"); 2799 } 2800 // FIXME: _Decimal32 and _Decimal64 are SSE. 2801 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). 2802 return; 2803 } 2804 2805 if (const EnumType *ET = Ty->getAs<EnumType>()) { 2806 // Classify the underlying integer type. 2807 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); 2808 return; 2809 } 2810 2811 if (Ty->hasPointerRepresentation()) { 2812 Current = Integer; 2813 return; 2814 } 2815 2816 if (Ty->isMemberPointerType()) { 2817 if (Ty->isMemberFunctionPointerType()) { 2818 if (Has64BitPointers) { 2819 // If Has64BitPointers, this is an {i64, i64}, so classify both 2820 // Lo and Hi now. 2821 Lo = Hi = Integer; 2822 } else { 2823 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that 2824 // straddles an eightbyte boundary, Hi should be classified as well. 2825 uint64_t EB_FuncPtr = (OffsetBase) / 64; 2826 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; 2827 if (EB_FuncPtr != EB_ThisAdj) { 2828 Lo = Hi = Integer; 2829 } else { 2830 Current = Integer; 2831 } 2832 } 2833 } else { 2834 Current = Integer; 2835 } 2836 return; 2837 } 2838 2839 if (const VectorType *VT = Ty->getAs<VectorType>()) { 2840 uint64_t Size = getContext().getTypeSize(VT); 2841 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { 2842 // gcc passes the following as integer: 2843 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> 2844 // 2 bytes - <2 x char>, <1 x short> 2845 // 1 byte - <1 x char> 2846 Current = Integer; 2847 2848 // If this type crosses an eightbyte boundary, it should be 2849 // split. 2850 uint64_t EB_Lo = (OffsetBase) / 64; 2851 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; 2852 if (EB_Lo != EB_Hi) 2853 Hi = Lo; 2854 } else if (Size == 64) { 2855 QualType ElementType = VT->getElementType(); 2856 2857 // gcc passes <1 x double> in memory. :( 2858 if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) 2859 return; 2860 2861 // gcc passes <1 x long long> as SSE but clang used to unconditionally 2862 // pass them as integer. For platforms where clang is the de facto 2863 // platform compiler, we must continue to use integer. 2864 if (!classifyIntegerMMXAsSSE() && 2865 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || 2866 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || 2867 ElementType->isSpecificBuiltinType(BuiltinType::Long) || 2868 ElementType->isSpecificBuiltinType(BuiltinType::ULong))) 2869 Current = Integer; 2870 else 2871 Current = SSE; 2872 2873 // If this type crosses an eightbyte boundary, it should be 2874 // split. 2875 if (OffsetBase && OffsetBase != 64) 2876 Hi = Lo; 2877 } else if (Size == 128 || 2878 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { 2879 QualType ElementType = VT->getElementType(); 2880 2881 // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :( 2882 if (passInt128VectorsInMem() && Size != 128 && 2883 (ElementType->isSpecificBuiltinType(BuiltinType::Int128) || 2884 ElementType->isSpecificBuiltinType(BuiltinType::UInt128))) 2885 return; 2886 2887 // Arguments of 256-bits are split into four eightbyte chunks. The 2888 // least significant one belongs to class SSE and all the others to class 2889 // SSEUP. The original Lo and Hi design considers that types can't be 2890 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. 2891 // This design isn't correct for 256-bits, but since there're no cases 2892 // where the upper parts would need to be inspected, avoid adding 2893 // complexity and just consider Hi to match the 64-256 part. 2894 // 2895 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in 2896 // registers if they are "named", i.e. not part of the "..." of a 2897 // variadic function. 2898 // 2899 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are 2900 // split into eight eightbyte chunks, one SSE and seven SSEUP. 2901 Lo = SSE; 2902 Hi = SSEUp; 2903 } 2904 return; 2905 } 2906 2907 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 2908 QualType ET = getContext().getCanonicalType(CT->getElementType()); 2909 2910 uint64_t Size = getContext().getTypeSize(Ty); 2911 if (ET->isIntegralOrEnumerationType()) { 2912 if (Size <= 64) 2913 Current = Integer; 2914 else if (Size <= 128) 2915 Lo = Hi = Integer; 2916 } else if (ET == getContext().FloatTy) { 2917 Current = SSE; 2918 } else if (ET == getContext().DoubleTy) { 2919 Lo = Hi = SSE; 2920 } else if (ET == getContext().LongDoubleTy) { 2921 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 2922 if (LDF == &llvm::APFloat::IEEEquad()) 2923 Current = Memory; 2924 else if (LDF == &llvm::APFloat::x87DoubleExtended()) 2925 Current = ComplexX87; 2926 else if (LDF == &llvm::APFloat::IEEEdouble()) 2927 Lo = Hi = SSE; 2928 else 2929 llvm_unreachable("unexpected long double representation!"); 2930 } 2931 2932 // If this complex type crosses an eightbyte boundary then it 2933 // should be split. 2934 uint64_t EB_Real = (OffsetBase) / 64; 2935 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; 2936 if (Hi == NoClass && EB_Real != EB_Imag) 2937 Hi = Lo; 2938 2939 return; 2940 } 2941 2942 if (const auto *EITy = Ty->getAs<ExtIntType>()) { 2943 if (EITy->getNumBits() <= 64) 2944 Current = Integer; 2945 else if (EITy->getNumBits() <= 128) 2946 Lo = Hi = Integer; 2947 // Larger values need to get passed in memory. 2948 return; 2949 } 2950 2951 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 2952 // Arrays are treated like structures. 2953 2954 uint64_t Size = getContext().getTypeSize(Ty); 2955 2956 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 2957 // than eight eightbytes, ..., it has class MEMORY. 2958 if (Size > 512) 2959 return; 2960 2961 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned 2962 // fields, it has class MEMORY. 2963 // 2964 // Only need to check alignment of array base. 2965 if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) 2966 return; 2967 2968 // Otherwise implement simplified merge. We could be smarter about 2969 // this, but it isn't worth it and would be harder to verify. 2970 Current = NoClass; 2971 uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); 2972 uint64_t ArraySize = AT->getSize().getZExtValue(); 2973 2974 // The only case a 256-bit wide vector could be used is when the array 2975 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 2976 // to work for sizes wider than 128, early check and fallback to memory. 2977 // 2978 if (Size > 128 && 2979 (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) 2980 return; 2981 2982 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { 2983 Class FieldLo, FieldHi; 2984 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); 2985 Lo = merge(Lo, FieldLo); 2986 Hi = merge(Hi, FieldHi); 2987 if (Lo == Memory || Hi == Memory) 2988 break; 2989 } 2990 2991 postMerge(Size, Lo, Hi); 2992 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); 2993 return; 2994 } 2995 2996 if (const RecordType *RT = Ty->getAs<RecordType>()) { 2997 uint64_t Size = getContext().getTypeSize(Ty); 2998 2999 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 3000 // than eight eightbytes, ..., it has class MEMORY. 3001 if (Size > 512) 3002 return; 3003 3004 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial 3005 // copy constructor or a non-trivial destructor, it is passed by invisible 3006 // reference. 3007 if (getRecordArgABI(RT, getCXXABI())) 3008 return; 3009 3010 const RecordDecl *RD = RT->getDecl(); 3011 3012 // Assume variable sized types are passed in memory. 3013 if (RD->hasFlexibleArrayMember()) 3014 return; 3015 3016 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 3017 3018 // Reset Lo class, this will be recomputed. 3019 Current = NoClass; 3020 3021 // If this is a C++ record, classify the bases first. 3022 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 3023 for (const auto &I : CXXRD->bases()) { 3024 assert(!I.isVirtual() && !I.getType()->isDependentType() && 3025 "Unexpected base class!"); 3026 const auto *Base = 3027 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); 3028 3029 // Classify this field. 3030 // 3031 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a 3032 // single eightbyte, each is classified separately. Each eightbyte gets 3033 // initialized to class NO_CLASS. 3034 Class FieldLo, FieldHi; 3035 uint64_t Offset = 3036 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); 3037 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); 3038 Lo = merge(Lo, FieldLo); 3039 Hi = merge(Hi, FieldHi); 3040 if (Lo == Memory || Hi == Memory) { 3041 postMerge(Size, Lo, Hi); 3042 return; 3043 } 3044 } 3045 } 3046 3047 // Classify the fields one at a time, merging the results. 3048 unsigned idx = 0; 3049 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 3050 i != e; ++i, ++idx) { 3051 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 3052 bool BitField = i->isBitField(); 3053 3054 // Ignore padding bit-fields. 3055 if (BitField && i->isUnnamedBitfield()) 3056 continue; 3057 3058 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than 3059 // four eightbytes, or it contains unaligned fields, it has class MEMORY. 3060 // 3061 // The only case a 256-bit wide vector could be used is when the struct 3062 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 3063 // to work for sizes wider than 128, early check and fallback to memory. 3064 // 3065 if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || 3066 Size > getNativeVectorSizeForAVXABI(AVXLevel))) { 3067 Lo = Memory; 3068 postMerge(Size, Lo, Hi); 3069 return; 3070 } 3071 // Note, skip this test for bit-fields, see below. 3072 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { 3073 Lo = Memory; 3074 postMerge(Size, Lo, Hi); 3075 return; 3076 } 3077 3078 // Classify this field. 3079 // 3080 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate 3081 // exceeds a single eightbyte, each is classified 3082 // separately. Each eightbyte gets initialized to class 3083 // NO_CLASS. 3084 Class FieldLo, FieldHi; 3085 3086 // Bit-fields require special handling, they do not force the 3087 // structure to be passed in memory even if unaligned, and 3088 // therefore they can straddle an eightbyte. 3089 if (BitField) { 3090 assert(!i->isUnnamedBitfield()); 3091 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 3092 uint64_t Size = i->getBitWidthValue(getContext()); 3093 3094 uint64_t EB_Lo = Offset / 64; 3095 uint64_t EB_Hi = (Offset + Size - 1) / 64; 3096 3097 if (EB_Lo) { 3098 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); 3099 FieldLo = NoClass; 3100 FieldHi = Integer; 3101 } else { 3102 FieldLo = Integer; 3103 FieldHi = EB_Hi ? Integer : NoClass; 3104 } 3105 } else 3106 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); 3107 Lo = merge(Lo, FieldLo); 3108 Hi = merge(Hi, FieldHi); 3109 if (Lo == Memory || Hi == Memory) 3110 break; 3111 } 3112 3113 postMerge(Size, Lo, Hi); 3114 } 3115 } 3116 3117 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { 3118 // If this is a scalar LLVM value then assume LLVM will pass it in the right 3119 // place naturally. 3120 if (!isAggregateTypeForABI(Ty)) { 3121 // Treat an enum type as its underlying type. 3122 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3123 Ty = EnumTy->getDecl()->getIntegerType(); 3124 3125 if (Ty->isExtIntType()) 3126 return getNaturalAlignIndirect(Ty); 3127 3128 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 3129 : ABIArgInfo::getDirect()); 3130 } 3131 3132 return getNaturalAlignIndirect(Ty); 3133 } 3134 3135 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { 3136 if (const VectorType *VecTy = Ty->getAs<VectorType>()) { 3137 uint64_t Size = getContext().getTypeSize(VecTy); 3138 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); 3139 if (Size <= 64 || Size > LargestVector) 3140 return true; 3141 QualType EltTy = VecTy->getElementType(); 3142 if (passInt128VectorsInMem() && 3143 (EltTy->isSpecificBuiltinType(BuiltinType::Int128) || 3144 EltTy->isSpecificBuiltinType(BuiltinType::UInt128))) 3145 return true; 3146 } 3147 3148 return false; 3149 } 3150 3151 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, 3152 unsigned freeIntRegs) const { 3153 // If this is a scalar LLVM value then assume LLVM will pass it in the right 3154 // place naturally. 3155 // 3156 // This assumption is optimistic, as there could be free registers available 3157 // when we need to pass this argument in memory, and LLVM could try to pass 3158 // the argument in the free register. This does not seem to happen currently, 3159 // but this code would be much safer if we could mark the argument with 3160 // 'onstack'. See PR12193. 3161 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) && 3162 !Ty->isExtIntType()) { 3163 // Treat an enum type as its underlying type. 3164 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3165 Ty = EnumTy->getDecl()->getIntegerType(); 3166 3167 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 3168 : ABIArgInfo::getDirect()); 3169 } 3170 3171 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 3172 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 3173 3174 // Compute the byval alignment. We specify the alignment of the byval in all 3175 // cases so that the mid-level optimizer knows the alignment of the byval. 3176 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); 3177 3178 // Attempt to avoid passing indirect results using byval when possible. This 3179 // is important for good codegen. 3180 // 3181 // We do this by coercing the value into a scalar type which the backend can 3182 // handle naturally (i.e., without using byval). 3183 // 3184 // For simplicity, we currently only do this when we have exhausted all of the 3185 // free integer registers. Doing this when there are free integer registers 3186 // would require more care, as we would have to ensure that the coerced value 3187 // did not claim the unused register. That would require either reording the 3188 // arguments to the function (so that any subsequent inreg values came first), 3189 // or only doing this optimization when there were no following arguments that 3190 // might be inreg. 3191 // 3192 // We currently expect it to be rare (particularly in well written code) for 3193 // arguments to be passed on the stack when there are still free integer 3194 // registers available (this would typically imply large structs being passed 3195 // by value), so this seems like a fair tradeoff for now. 3196 // 3197 // We can revisit this if the backend grows support for 'onstack' parameter 3198 // attributes. See PR12193. 3199 if (freeIntRegs == 0) { 3200 uint64_t Size = getContext().getTypeSize(Ty); 3201 3202 // If this type fits in an eightbyte, coerce it into the matching integral 3203 // type, which will end up on the stack (with alignment 8). 3204 if (Align == 8 && Size <= 64) 3205 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 3206 Size)); 3207 } 3208 3209 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); 3210 } 3211 3212 /// The ABI specifies that a value should be passed in a full vector XMM/YMM 3213 /// register. Pick an LLVM IR type that will be passed as a vector register. 3214 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { 3215 // Wrapper structs/arrays that only contain vectors are passed just like 3216 // vectors; strip them off if present. 3217 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) 3218 Ty = QualType(InnerTy, 0); 3219 3220 llvm::Type *IRType = CGT.ConvertType(Ty); 3221 if (isa<llvm::VectorType>(IRType)) { 3222 // Don't pass vXi128 vectors in their native type, the backend can't 3223 // legalize them. 3224 if (passInt128VectorsInMem() && 3225 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) { 3226 // Use a vXi64 vector. 3227 uint64_t Size = getContext().getTypeSize(Ty); 3228 return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()), 3229 Size / 64); 3230 } 3231 3232 return IRType; 3233 } 3234 3235 if (IRType->getTypeID() == llvm::Type::FP128TyID) 3236 return IRType; 3237 3238 // We couldn't find the preferred IR vector type for 'Ty'. 3239 uint64_t Size = getContext().getTypeSize(Ty); 3240 assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); 3241 3242 3243 // Return a LLVM IR vector type based on the size of 'Ty'. 3244 return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()), 3245 Size / 64); 3246 } 3247 3248 /// BitsContainNoUserData - Return true if the specified [start,end) bit range 3249 /// is known to either be off the end of the specified type or being in 3250 /// alignment padding. The user type specified is known to be at most 128 bits 3251 /// in size, and have passed through X86_64ABIInfo::classify with a successful 3252 /// classification that put one of the two halves in the INTEGER class. 3253 /// 3254 /// It is conservatively correct to return false. 3255 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, 3256 unsigned EndBit, ASTContext &Context) { 3257 // If the bytes being queried are off the end of the type, there is no user 3258 // data hiding here. This handles analysis of builtins, vectors and other 3259 // types that don't contain interesting padding. 3260 unsigned TySize = (unsigned)Context.getTypeSize(Ty); 3261 if (TySize <= StartBit) 3262 return true; 3263 3264 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 3265 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); 3266 unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); 3267 3268 // Check each element to see if the element overlaps with the queried range. 3269 for (unsigned i = 0; i != NumElts; ++i) { 3270 // If the element is after the span we care about, then we're done.. 3271 unsigned EltOffset = i*EltSize; 3272 if (EltOffset >= EndBit) break; 3273 3274 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; 3275 if (!BitsContainNoUserData(AT->getElementType(), EltStart, 3276 EndBit-EltOffset, Context)) 3277 return false; 3278 } 3279 // If it overlaps no elements, then it is safe to process as padding. 3280 return true; 3281 } 3282 3283 if (const RecordType *RT = Ty->getAs<RecordType>()) { 3284 const RecordDecl *RD = RT->getDecl(); 3285 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 3286 3287 // If this is a C++ record, check the bases first. 3288 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 3289 for (const auto &I : CXXRD->bases()) { 3290 assert(!I.isVirtual() && !I.getType()->isDependentType() && 3291 "Unexpected base class!"); 3292 const auto *Base = 3293 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); 3294 3295 // If the base is after the span we care about, ignore it. 3296 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); 3297 if (BaseOffset >= EndBit) continue; 3298 3299 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; 3300 if (!BitsContainNoUserData(I.getType(), BaseStart, 3301 EndBit-BaseOffset, Context)) 3302 return false; 3303 } 3304 } 3305 3306 // Verify that no field has data that overlaps the region of interest. Yes 3307 // this could be sped up a lot by being smarter about queried fields, 3308 // however we're only looking at structs up to 16 bytes, so we don't care 3309 // much. 3310 unsigned idx = 0; 3311 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 3312 i != e; ++i, ++idx) { 3313 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); 3314 3315 // If we found a field after the region we care about, then we're done. 3316 if (FieldOffset >= EndBit) break; 3317 3318 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; 3319 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, 3320 Context)) 3321 return false; 3322 } 3323 3324 // If nothing in this record overlapped the area of interest, then we're 3325 // clean. 3326 return true; 3327 } 3328 3329 return false; 3330 } 3331 3332 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a 3333 /// float member at the specified offset. For example, {int,{float}} has a 3334 /// float at offset 4. It is conservatively correct for this routine to return 3335 /// false. 3336 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, 3337 const llvm::DataLayout &TD) { 3338 // Base case if we find a float. 3339 if (IROffset == 0 && IRType->isFloatTy()) 3340 return true; 3341 3342 // If this is a struct, recurse into the field at the specified offset. 3343 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 3344 const llvm::StructLayout *SL = TD.getStructLayout(STy); 3345 unsigned Elt = SL->getElementContainingOffset(IROffset); 3346 IROffset -= SL->getElementOffset(Elt); 3347 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); 3348 } 3349 3350 // If this is an array, recurse into the field at the specified offset. 3351 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 3352 llvm::Type *EltTy = ATy->getElementType(); 3353 unsigned EltSize = TD.getTypeAllocSize(EltTy); 3354 IROffset -= IROffset/EltSize*EltSize; 3355 return ContainsFloatAtOffset(EltTy, IROffset, TD); 3356 } 3357 3358 return false; 3359 } 3360 3361 3362 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the 3363 /// low 8 bytes of an XMM register, corresponding to the SSE class. 3364 llvm::Type *X86_64ABIInfo:: 3365 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, 3366 QualType SourceTy, unsigned SourceOffset) const { 3367 // The only three choices we have are either double, <2 x float>, or float. We 3368 // pass as float if the last 4 bytes is just padding. This happens for 3369 // structs that contain 3 floats. 3370 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, 3371 SourceOffset*8+64, getContext())) 3372 return llvm::Type::getFloatTy(getVMContext()); 3373 3374 // We want to pass as <2 x float> if the LLVM IR type contains a float at 3375 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the 3376 // case. 3377 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && 3378 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) 3379 return llvm::FixedVectorType::get(llvm::Type::getFloatTy(getVMContext()), 3380 2); 3381 3382 return llvm::Type::getDoubleTy(getVMContext()); 3383 } 3384 3385 3386 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in 3387 /// an 8-byte GPR. This means that we either have a scalar or we are talking 3388 /// about the high or low part of an up-to-16-byte struct. This routine picks 3389 /// the best LLVM IR type to represent this, which may be i64 or may be anything 3390 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, 3391 /// etc). 3392 /// 3393 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for 3394 /// the source type. IROffset is an offset in bytes into the LLVM IR type that 3395 /// the 8-byte value references. PrefType may be null. 3396 /// 3397 /// SourceTy is the source-level type for the entire argument. SourceOffset is 3398 /// an offset into this that we're processing (which is always either 0 or 8). 3399 /// 3400 llvm::Type *X86_64ABIInfo:: 3401 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, 3402 QualType SourceTy, unsigned SourceOffset) const { 3403 // If we're dealing with an un-offset LLVM IR type, then it means that we're 3404 // returning an 8-byte unit starting with it. See if we can safely use it. 3405 if (IROffset == 0) { 3406 // Pointers and int64's always fill the 8-byte unit. 3407 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || 3408 IRType->isIntegerTy(64)) 3409 return IRType; 3410 3411 // If we have a 1/2/4-byte integer, we can use it only if the rest of the 3412 // goodness in the source type is just tail padding. This is allowed to 3413 // kick in for struct {double,int} on the int, but not on 3414 // struct{double,int,int} because we wouldn't return the second int. We 3415 // have to do this analysis on the source type because we can't depend on 3416 // unions being lowered a specific way etc. 3417 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || 3418 IRType->isIntegerTy(32) || 3419 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { 3420 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : 3421 cast<llvm::IntegerType>(IRType)->getBitWidth(); 3422 3423 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, 3424 SourceOffset*8+64, getContext())) 3425 return IRType; 3426 } 3427 } 3428 3429 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 3430 // If this is a struct, recurse into the field at the specified offset. 3431 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); 3432 if (IROffset < SL->getSizeInBytes()) { 3433 unsigned FieldIdx = SL->getElementContainingOffset(IROffset); 3434 IROffset -= SL->getElementOffset(FieldIdx); 3435 3436 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, 3437 SourceTy, SourceOffset); 3438 } 3439 } 3440 3441 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 3442 llvm::Type *EltTy = ATy->getElementType(); 3443 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); 3444 unsigned EltOffset = IROffset/EltSize*EltSize; 3445 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, 3446 SourceOffset); 3447 } 3448 3449 // Okay, we don't have any better idea of what to pass, so we pass this in an 3450 // integer register that isn't too big to fit the rest of the struct. 3451 unsigned TySizeInBytes = 3452 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); 3453 3454 assert(TySizeInBytes != SourceOffset && "Empty field?"); 3455 3456 // It is always safe to classify this as an integer type up to i64 that 3457 // isn't larger than the structure. 3458 return llvm::IntegerType::get(getVMContext(), 3459 std::min(TySizeInBytes-SourceOffset, 8U)*8); 3460 } 3461 3462 3463 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally 3464 /// be used as elements of a two register pair to pass or return, return a 3465 /// first class aggregate to represent them. For example, if the low part of 3466 /// a by-value argument should be passed as i32* and the high part as float, 3467 /// return {i32*, float}. 3468 static llvm::Type * 3469 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, 3470 const llvm::DataLayout &TD) { 3471 // In order to correctly satisfy the ABI, we need to the high part to start 3472 // at offset 8. If the high and low parts we inferred are both 4-byte types 3473 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have 3474 // the second element at offset 8. Check for this: 3475 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); 3476 unsigned HiAlign = TD.getABITypeAlignment(Hi); 3477 unsigned HiStart = llvm::alignTo(LoSize, HiAlign); 3478 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); 3479 3480 // To handle this, we have to increase the size of the low part so that the 3481 // second element will start at an 8 byte offset. We can't increase the size 3482 // of the second element because it might make us access off the end of the 3483 // struct. 3484 if (HiStart != 8) { 3485 // There are usually two sorts of types the ABI generation code can produce 3486 // for the low part of a pair that aren't 8 bytes in size: float or 3487 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and 3488 // NaCl). 3489 // Promote these to a larger type. 3490 if (Lo->isFloatTy()) 3491 Lo = llvm::Type::getDoubleTy(Lo->getContext()); 3492 else { 3493 assert((Lo->isIntegerTy() || Lo->isPointerTy()) 3494 && "Invalid/unknown lo type"); 3495 Lo = llvm::Type::getInt64Ty(Lo->getContext()); 3496 } 3497 } 3498 3499 llvm::StructType *Result = llvm::StructType::get(Lo, Hi); 3500 3501 // Verify that the second element is at an 8-byte offset. 3502 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && 3503 "Invalid x86-64 argument pair!"); 3504 return Result; 3505 } 3506 3507 ABIArgInfo X86_64ABIInfo:: 3508 classifyReturnType(QualType RetTy) const { 3509 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the 3510 // classification algorithm. 3511 X86_64ABIInfo::Class Lo, Hi; 3512 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); 3513 3514 // Check some invariants. 3515 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 3516 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 3517 3518 llvm::Type *ResType = nullptr; 3519 switch (Lo) { 3520 case NoClass: 3521 if (Hi == NoClass) 3522 return ABIArgInfo::getIgnore(); 3523 // If the low part is just padding, it takes no register, leave ResType 3524 // null. 3525 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 3526 "Unknown missing lo part"); 3527 break; 3528 3529 case SSEUp: 3530 case X87Up: 3531 llvm_unreachable("Invalid classification for lo word."); 3532 3533 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via 3534 // hidden argument. 3535 case Memory: 3536 return getIndirectReturnResult(RetTy); 3537 3538 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next 3539 // available register of the sequence %rax, %rdx is used. 3540 case Integer: 3541 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 3542 3543 // If we have a sign or zero extended integer, make sure to return Extend 3544 // so that the parameter gets the right LLVM IR attributes. 3545 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 3546 // Treat an enum type as its underlying type. 3547 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 3548 RetTy = EnumTy->getDecl()->getIntegerType(); 3549 3550 if (RetTy->isIntegralOrEnumerationType() && 3551 isPromotableIntegerTypeForABI(RetTy)) 3552 return ABIArgInfo::getExtend(RetTy); 3553 } 3554 break; 3555 3556 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next 3557 // available SSE register of the sequence %xmm0, %xmm1 is used. 3558 case SSE: 3559 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 3560 break; 3561 3562 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is 3563 // returned on the X87 stack in %st0 as 80-bit x87 number. 3564 case X87: 3565 ResType = llvm::Type::getX86_FP80Ty(getVMContext()); 3566 break; 3567 3568 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real 3569 // part of the value is returned in %st0 and the imaginary part in 3570 // %st1. 3571 case ComplexX87: 3572 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); 3573 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), 3574 llvm::Type::getX86_FP80Ty(getVMContext())); 3575 break; 3576 } 3577 3578 llvm::Type *HighPart = nullptr; 3579 switch (Hi) { 3580 // Memory was handled previously and X87 should 3581 // never occur as a hi class. 3582 case Memory: 3583 case X87: 3584 llvm_unreachable("Invalid classification for hi word."); 3585 3586 case ComplexX87: // Previously handled. 3587 case NoClass: 3588 break; 3589 3590 case Integer: 3591 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3592 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3593 return ABIArgInfo::getDirect(HighPart, 8); 3594 break; 3595 case SSE: 3596 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3597 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3598 return ABIArgInfo::getDirect(HighPart, 8); 3599 break; 3600 3601 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte 3602 // is passed in the next available eightbyte chunk if the last used 3603 // vector register. 3604 // 3605 // SSEUP should always be preceded by SSE, just widen. 3606 case SSEUp: 3607 assert(Lo == SSE && "Unexpected SSEUp classification."); 3608 ResType = GetByteVectorType(RetTy); 3609 break; 3610 3611 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is 3612 // returned together with the previous X87 value in %st0. 3613 case X87Up: 3614 // If X87Up is preceded by X87, we don't need to do 3615 // anything. However, in some cases with unions it may not be 3616 // preceded by X87. In such situations we follow gcc and pass the 3617 // extra bits in an SSE reg. 3618 if (Lo != X87) { 3619 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3620 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3621 return ABIArgInfo::getDirect(HighPart, 8); 3622 } 3623 break; 3624 } 3625 3626 // If a high part was specified, merge it together with the low part. It is 3627 // known to pass in the high eightbyte of the result. We do this by forming a 3628 // first class struct aggregate with the high and low part: {low, high} 3629 if (HighPart) 3630 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 3631 3632 return ABIArgInfo::getDirect(ResType); 3633 } 3634 3635 ABIArgInfo X86_64ABIInfo::classifyArgumentType( 3636 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, 3637 bool isNamedArg) 3638 const 3639 { 3640 Ty = useFirstFieldIfTransparentUnion(Ty); 3641 3642 X86_64ABIInfo::Class Lo, Hi; 3643 classify(Ty, 0, Lo, Hi, isNamedArg); 3644 3645 // Check some invariants. 3646 // FIXME: Enforce these by construction. 3647 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 3648 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 3649 3650 neededInt = 0; 3651 neededSSE = 0; 3652 llvm::Type *ResType = nullptr; 3653 switch (Lo) { 3654 case NoClass: 3655 if (Hi == NoClass) 3656 return ABIArgInfo::getIgnore(); 3657 // If the low part is just padding, it takes no register, leave ResType 3658 // null. 3659 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 3660 "Unknown missing lo part"); 3661 break; 3662 3663 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument 3664 // on the stack. 3665 case Memory: 3666 3667 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or 3668 // COMPLEX_X87, it is passed in memory. 3669 case X87: 3670 case ComplexX87: 3671 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) 3672 ++neededInt; 3673 return getIndirectResult(Ty, freeIntRegs); 3674 3675 case SSEUp: 3676 case X87Up: 3677 llvm_unreachable("Invalid classification for lo word."); 3678 3679 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next 3680 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 3681 // and %r9 is used. 3682 case Integer: 3683 ++neededInt; 3684 3685 // Pick an 8-byte type based on the preferred type. 3686 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); 3687 3688 // If we have a sign or zero extended integer, make sure to return Extend 3689 // so that the parameter gets the right LLVM IR attributes. 3690 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 3691 // Treat an enum type as its underlying type. 3692 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3693 Ty = EnumTy->getDecl()->getIntegerType(); 3694 3695 if (Ty->isIntegralOrEnumerationType() && 3696 isPromotableIntegerTypeForABI(Ty)) 3697 return ABIArgInfo::getExtend(Ty); 3698 } 3699 3700 break; 3701 3702 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next 3703 // available SSE register is used, the registers are taken in the 3704 // order from %xmm0 to %xmm7. 3705 case SSE: { 3706 llvm::Type *IRType = CGT.ConvertType(Ty); 3707 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); 3708 ++neededSSE; 3709 break; 3710 } 3711 } 3712 3713 llvm::Type *HighPart = nullptr; 3714 switch (Hi) { 3715 // Memory was handled previously, ComplexX87 and X87 should 3716 // never occur as hi classes, and X87Up must be preceded by X87, 3717 // which is passed in memory. 3718 case Memory: 3719 case X87: 3720 case ComplexX87: 3721 llvm_unreachable("Invalid classification for hi word."); 3722 3723 case NoClass: break; 3724 3725 case Integer: 3726 ++neededInt; 3727 // Pick an 8-byte type based on the preferred type. 3728 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 3729 3730 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 3731 return ABIArgInfo::getDirect(HighPart, 8); 3732 break; 3733 3734 // X87Up generally doesn't occur here (long double is passed in 3735 // memory), except in situations involving unions. 3736 case X87Up: 3737 case SSE: 3738 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 3739 3740 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 3741 return ABIArgInfo::getDirect(HighPart, 8); 3742 3743 ++neededSSE; 3744 break; 3745 3746 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the 3747 // eightbyte is passed in the upper half of the last used SSE 3748 // register. This only happens when 128-bit vectors are passed. 3749 case SSEUp: 3750 assert(Lo == SSE && "Unexpected SSEUp classification"); 3751 ResType = GetByteVectorType(Ty); 3752 break; 3753 } 3754 3755 // If a high part was specified, merge it together with the low part. It is 3756 // known to pass in the high eightbyte of the result. We do this by forming a 3757 // first class struct aggregate with the high and low part: {low, high} 3758 if (HighPart) 3759 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 3760 3761 return ABIArgInfo::getDirect(ResType); 3762 } 3763 3764 ABIArgInfo 3765 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, 3766 unsigned &NeededSSE) const { 3767 auto RT = Ty->getAs<RecordType>(); 3768 assert(RT && "classifyRegCallStructType only valid with struct types"); 3769 3770 if (RT->getDecl()->hasFlexibleArrayMember()) 3771 return getIndirectReturnResult(Ty); 3772 3773 // Sum up bases 3774 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 3775 if (CXXRD->isDynamicClass()) { 3776 NeededInt = NeededSSE = 0; 3777 return getIndirectReturnResult(Ty); 3778 } 3779 3780 for (const auto &I : CXXRD->bases()) 3781 if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) 3782 .isIndirect()) { 3783 NeededInt = NeededSSE = 0; 3784 return getIndirectReturnResult(Ty); 3785 } 3786 } 3787 3788 // Sum up members 3789 for (const auto *FD : RT->getDecl()->fields()) { 3790 if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { 3791 if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) 3792 .isIndirect()) { 3793 NeededInt = NeededSSE = 0; 3794 return getIndirectReturnResult(Ty); 3795 } 3796 } else { 3797 unsigned LocalNeededInt, LocalNeededSSE; 3798 if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, 3799 LocalNeededSSE, true) 3800 .isIndirect()) { 3801 NeededInt = NeededSSE = 0; 3802 return getIndirectReturnResult(Ty); 3803 } 3804 NeededInt += LocalNeededInt; 3805 NeededSSE += LocalNeededSSE; 3806 } 3807 } 3808 3809 return ABIArgInfo::getDirect(); 3810 } 3811 3812 ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, 3813 unsigned &NeededInt, 3814 unsigned &NeededSSE) const { 3815 3816 NeededInt = 0; 3817 NeededSSE = 0; 3818 3819 return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); 3820 } 3821 3822 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 3823 3824 const unsigned CallingConv = FI.getCallingConvention(); 3825 // It is possible to force Win64 calling convention on any x86_64 target by 3826 // using __attribute__((ms_abi)). In such case to correctly emit Win64 3827 // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. 3828 if (CallingConv == llvm::CallingConv::Win64) { 3829 WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel); 3830 Win64ABIInfo.computeInfo(FI); 3831 return; 3832 } 3833 3834 bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; 3835 3836 // Keep track of the number of assigned registers. 3837 unsigned FreeIntRegs = IsRegCall ? 11 : 6; 3838 unsigned FreeSSERegs = IsRegCall ? 16 : 8; 3839 unsigned NeededInt, NeededSSE; 3840 3841 if (!::classifyReturnType(getCXXABI(), FI, *this)) { 3842 if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && 3843 !FI.getReturnType()->getTypePtr()->isUnionType()) { 3844 FI.getReturnInfo() = 3845 classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); 3846 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { 3847 FreeIntRegs -= NeededInt; 3848 FreeSSERegs -= NeededSSE; 3849 } else { 3850 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); 3851 } 3852 } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() && 3853 getContext().getCanonicalType(FI.getReturnType() 3854 ->getAs<ComplexType>() 3855 ->getElementType()) == 3856 getContext().LongDoubleTy) 3857 // Complex Long Double Type is passed in Memory when Regcall 3858 // calling convention is used. 3859 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); 3860 else 3861 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3862 } 3863 3864 // If the return value is indirect, then the hidden argument is consuming one 3865 // integer register. 3866 if (FI.getReturnInfo().isIndirect()) 3867 --FreeIntRegs; 3868 3869 // The chain argument effectively gives us another free register. 3870 if (FI.isChainCall()) 3871 ++FreeIntRegs; 3872 3873 unsigned NumRequiredArgs = FI.getNumRequiredArgs(); 3874 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers 3875 // get assigned (in left-to-right order) for passing as follows... 3876 unsigned ArgNo = 0; 3877 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3878 it != ie; ++it, ++ArgNo) { 3879 bool IsNamedArg = ArgNo < NumRequiredArgs; 3880 3881 if (IsRegCall && it->type->isStructureOrClassType()) 3882 it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); 3883 else 3884 it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, 3885 NeededSSE, IsNamedArg); 3886 3887 // AMD64-ABI 3.2.3p3: If there are no registers available for any 3888 // eightbyte of an argument, the whole argument is passed on the 3889 // stack. If registers have already been assigned for some 3890 // eightbytes of such an argument, the assignments get reverted. 3891 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { 3892 FreeIntRegs -= NeededInt; 3893 FreeSSERegs -= NeededSSE; 3894 } else { 3895 it->info = getIndirectResult(it->type, FreeIntRegs); 3896 } 3897 } 3898 } 3899 3900 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, 3901 Address VAListAddr, QualType Ty) { 3902 Address overflow_arg_area_p = 3903 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); 3904 llvm::Value *overflow_arg_area = 3905 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); 3906 3907 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 3908 // byte boundary if alignment needed by type exceeds 8 byte boundary. 3909 // It isn't stated explicitly in the standard, but in practice we use 3910 // alignment greater than 16 where necessary. 3911 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); 3912 if (Align > CharUnits::fromQuantity(8)) { 3913 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, 3914 Align); 3915 } 3916 3917 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. 3918 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 3919 llvm::Value *Res = 3920 CGF.Builder.CreateBitCast(overflow_arg_area, 3921 llvm::PointerType::getUnqual(LTy)); 3922 3923 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: 3924 // l->overflow_arg_area + sizeof(type). 3925 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to 3926 // an 8 byte boundary. 3927 3928 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; 3929 llvm::Value *Offset = 3930 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); 3931 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, 3932 "overflow_arg_area.next"); 3933 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); 3934 3935 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. 3936 return Address(Res, Align); 3937 } 3938 3939 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 3940 QualType Ty) const { 3941 // Assume that va_list type is correct; should be pointer to LLVM type: 3942 // struct { 3943 // i32 gp_offset; 3944 // i32 fp_offset; 3945 // i8* overflow_arg_area; 3946 // i8* reg_save_area; 3947 // }; 3948 unsigned neededInt, neededSSE; 3949 3950 Ty = getContext().getCanonicalType(Ty); 3951 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, 3952 /*isNamedArg*/false); 3953 3954 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed 3955 // in the registers. If not go to step 7. 3956 if (!neededInt && !neededSSE) 3957 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); 3958 3959 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of 3960 // general purpose registers needed to pass type and num_fp to hold 3961 // the number of floating point registers needed. 3962 3963 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into 3964 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or 3965 // l->fp_offset > 304 - num_fp * 16 go to step 7. 3966 // 3967 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of 3968 // register save space). 3969 3970 llvm::Value *InRegs = nullptr; 3971 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); 3972 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; 3973 if (neededInt) { 3974 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); 3975 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); 3976 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); 3977 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); 3978 } 3979 3980 if (neededSSE) { 3981 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); 3982 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); 3983 llvm::Value *FitsInFP = 3984 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); 3985 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); 3986 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; 3987 } 3988 3989 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 3990 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 3991 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 3992 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 3993 3994 // Emit code to load the value if it was passed in registers. 3995 3996 CGF.EmitBlock(InRegBlock); 3997 3998 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with 3999 // an offset of l->gp_offset and/or l->fp_offset. This may require 4000 // copying to a temporary location in case the parameter is passed 4001 // in different register classes or requires an alignment greater 4002 // than 8 for general purpose registers and 16 for XMM registers. 4003 // 4004 // FIXME: This really results in shameful code when we end up needing to 4005 // collect arguments from different places; often what should result in a 4006 // simple assembling of a structure from scattered addresses has many more 4007 // loads than necessary. Can we clean this up? 4008 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 4009 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( 4010 CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area"); 4011 4012 Address RegAddr = Address::invalid(); 4013 if (neededInt && neededSSE) { 4014 // FIXME: Cleanup. 4015 assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); 4016 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); 4017 Address Tmp = CGF.CreateMemTemp(Ty); 4018 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); 4019 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); 4020 llvm::Type *TyLo = ST->getElementType(0); 4021 llvm::Type *TyHi = ST->getElementType(1); 4022 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && 4023 "Unexpected ABI info for mixed regs"); 4024 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); 4025 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); 4026 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); 4027 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); 4028 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; 4029 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; 4030 4031 // Copy the first element. 4032 // FIXME: Our choice of alignment here and below is probably pessimistic. 4033 llvm::Value *V = CGF.Builder.CreateAlignedLoad( 4034 TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), 4035 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); 4036 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 4037 4038 // Copy the second element. 4039 V = CGF.Builder.CreateAlignedLoad( 4040 TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), 4041 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); 4042 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 4043 4044 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); 4045 } else if (neededInt) { 4046 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), 4047 CharUnits::fromQuantity(8)); 4048 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); 4049 4050 // Copy to a temporary if necessary to ensure the appropriate alignment. 4051 std::pair<CharUnits, CharUnits> SizeAlign = 4052 getContext().getTypeInfoInChars(Ty); 4053 uint64_t TySize = SizeAlign.first.getQuantity(); 4054 CharUnits TyAlign = SizeAlign.second; 4055 4056 // Copy into a temporary if the type is more aligned than the 4057 // register save area. 4058 if (TyAlign.getQuantity() > 8) { 4059 Address Tmp = CGF.CreateMemTemp(Ty); 4060 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); 4061 RegAddr = Tmp; 4062 } 4063 4064 } else if (neededSSE == 1) { 4065 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), 4066 CharUnits::fromQuantity(16)); 4067 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); 4068 } else { 4069 assert(neededSSE == 2 && "Invalid number of needed registers!"); 4070 // SSE registers are spaced 16 bytes apart in the register save 4071 // area, we need to collect the two eightbytes together. 4072 // The ABI isn't explicit about this, but it seems reasonable 4073 // to assume that the slots are 16-byte aligned, since the stack is 4074 // naturally 16-byte aligned and the prologue is expected to store 4075 // all the SSE registers to the RSA. 4076 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), 4077 CharUnits::fromQuantity(16)); 4078 Address RegAddrHi = 4079 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, 4080 CharUnits::fromQuantity(16)); 4081 llvm::Type *ST = AI.canHaveCoerceToType() 4082 ? AI.getCoerceToType() 4083 : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); 4084 llvm::Value *V; 4085 Address Tmp = CGF.CreateMemTemp(Ty); 4086 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); 4087 V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( 4088 RegAddrLo, ST->getStructElementType(0))); 4089 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 4090 V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( 4091 RegAddrHi, ST->getStructElementType(1))); 4092 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 4093 4094 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); 4095 } 4096 4097 // AMD64-ABI 3.5.7p5: Step 5. Set: 4098 // l->gp_offset = l->gp_offset + num_gp * 8 4099 // l->fp_offset = l->fp_offset + num_fp * 16. 4100 if (neededInt) { 4101 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); 4102 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), 4103 gp_offset_p); 4104 } 4105 if (neededSSE) { 4106 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); 4107 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), 4108 fp_offset_p); 4109 } 4110 CGF.EmitBranch(ContBlock); 4111 4112 // Emit code to load the value if it was passed in memory. 4113 4114 CGF.EmitBlock(InMemBlock); 4115 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); 4116 4117 // Return the appropriate result. 4118 4119 CGF.EmitBlock(ContBlock); 4120 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, 4121 "vaarg.addr"); 4122 return ResAddr; 4123 } 4124 4125 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 4126 QualType Ty) const { 4127 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 4128 CGF.getContext().getTypeInfoInChars(Ty), 4129 CharUnits::fromQuantity(8), 4130 /*allowHigherAlign*/ false); 4131 } 4132 4133 ABIArgInfo 4134 WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, 4135 const ABIArgInfo ¤t) const { 4136 // Assumes vectorCall calling convention. 4137 const Type *Base = nullptr; 4138 uint64_t NumElts = 0; 4139 4140 if (!Ty->isBuiltinType() && !Ty->isVectorType() && 4141 isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { 4142 FreeSSERegs -= NumElts; 4143 return getDirectX86Hva(); 4144 } 4145 return current; 4146 } 4147 4148 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, 4149 bool IsReturnType, bool IsVectorCall, 4150 bool IsRegCall) const { 4151 4152 if (Ty->isVoidType()) 4153 return ABIArgInfo::getIgnore(); 4154 4155 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4156 Ty = EnumTy->getDecl()->getIntegerType(); 4157 4158 TypeInfo Info = getContext().getTypeInfo(Ty); 4159 uint64_t Width = Info.Width; 4160 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); 4161 4162 const RecordType *RT = Ty->getAs<RecordType>(); 4163 if (RT) { 4164 if (!IsReturnType) { 4165 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) 4166 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 4167 } 4168 4169 if (RT->getDecl()->hasFlexibleArrayMember()) 4170 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 4171 4172 } 4173 4174 const Type *Base = nullptr; 4175 uint64_t NumElts = 0; 4176 // vectorcall adds the concept of a homogenous vector aggregate, similar to 4177 // other targets. 4178 if ((IsVectorCall || IsRegCall) && 4179 isHomogeneousAggregate(Ty, Base, NumElts)) { 4180 if (IsRegCall) { 4181 if (FreeSSERegs >= NumElts) { 4182 FreeSSERegs -= NumElts; 4183 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) 4184 return ABIArgInfo::getDirect(); 4185 return ABIArgInfo::getExpand(); 4186 } 4187 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4188 } else if (IsVectorCall) { 4189 if (FreeSSERegs >= NumElts && 4190 (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { 4191 FreeSSERegs -= NumElts; 4192 return ABIArgInfo::getDirect(); 4193 } else if (IsReturnType) { 4194 return ABIArgInfo::getExpand(); 4195 } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { 4196 // HVAs are delayed and reclassified in the 2nd step. 4197 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4198 } 4199 } 4200 } 4201 4202 if (Ty->isMemberPointerType()) { 4203 // If the member pointer is represented by an LLVM int or ptr, pass it 4204 // directly. 4205 llvm::Type *LLTy = CGT.ConvertType(Ty); 4206 if (LLTy->isPointerTy() || LLTy->isIntegerTy()) 4207 return ABIArgInfo::getDirect(); 4208 } 4209 4210 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { 4211 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 4212 // not 1, 2, 4, or 8 bytes, must be passed by reference." 4213 if (Width > 64 || !llvm::isPowerOf2_64(Width)) 4214 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 4215 4216 // Otherwise, coerce it to a small integer. 4217 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); 4218 } 4219 4220 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 4221 switch (BT->getKind()) { 4222 case BuiltinType::Bool: 4223 // Bool type is always extended to the ABI, other builtin types are not 4224 // extended. 4225 return ABIArgInfo::getExtend(Ty); 4226 4227 case BuiltinType::LongDouble: 4228 // Mingw64 GCC uses the old 80 bit extended precision floating point 4229 // unit. It passes them indirectly through memory. 4230 if (IsMingw64) { 4231 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 4232 if (LDF == &llvm::APFloat::x87DoubleExtended()) 4233 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4234 } 4235 break; 4236 4237 case BuiltinType::Int128: 4238 case BuiltinType::UInt128: 4239 // If it's a parameter type, the normal ABI rule is that arguments larger 4240 // than 8 bytes are passed indirectly. GCC follows it. We follow it too, 4241 // even though it isn't particularly efficient. 4242 if (!IsReturnType) 4243 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4244 4245 // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. 4246 // Clang matches them for compatibility. 4247 return ABIArgInfo::getDirect(llvm::FixedVectorType::get( 4248 llvm::Type::getInt64Ty(getVMContext()), 2)); 4249 4250 default: 4251 break; 4252 } 4253 } 4254 4255 if (Ty->isExtIntType()) { 4256 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 4257 // not 1, 2, 4, or 8 bytes, must be passed by reference." 4258 // However, non-power-of-two _ExtInts will be passed as 1,2,4 or 8 bytes 4259 // anyway as long is it fits in them, so we don't have to check the power of 4260 // 2. 4261 if (Width <= 64) 4262 return ABIArgInfo::getDirect(); 4263 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4264 } 4265 4266 return ABIArgInfo::getDirect(); 4267 } 4268 4269 void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, 4270 unsigned FreeSSERegs, 4271 bool IsVectorCall, 4272 bool IsRegCall) const { 4273 unsigned Count = 0; 4274 for (auto &I : FI.arguments()) { 4275 // Vectorcall in x64 only permits the first 6 arguments to be passed 4276 // as XMM/YMM registers. 4277 if (Count < VectorcallMaxParamNumAsReg) 4278 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); 4279 else { 4280 // Since these cannot be passed in registers, pretend no registers 4281 // are left. 4282 unsigned ZeroSSERegsAvail = 0; 4283 I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, 4284 IsVectorCall, IsRegCall); 4285 } 4286 ++Count; 4287 } 4288 4289 for (auto &I : FI.arguments()) { 4290 I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); 4291 } 4292 } 4293 4294 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 4295 const unsigned CC = FI.getCallingConvention(); 4296 bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall; 4297 bool IsRegCall = CC == llvm::CallingConv::X86_RegCall; 4298 4299 // If __attribute__((sysv_abi)) is in use, use the SysV argument 4300 // classification rules. 4301 if (CC == llvm::CallingConv::X86_64_SysV) { 4302 X86_64ABIInfo SysVABIInfo(CGT, AVXLevel); 4303 SysVABIInfo.computeInfo(FI); 4304 return; 4305 } 4306 4307 unsigned FreeSSERegs = 0; 4308 if (IsVectorCall) { 4309 // We can use up to 4 SSE return registers with vectorcall. 4310 FreeSSERegs = 4; 4311 } else if (IsRegCall) { 4312 // RegCall gives us 16 SSE registers. 4313 FreeSSERegs = 16; 4314 } 4315 4316 if (!getCXXABI().classifyReturnType(FI)) 4317 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, 4318 IsVectorCall, IsRegCall); 4319 4320 if (IsVectorCall) { 4321 // We can use up to 6 SSE register parameters with vectorcall. 4322 FreeSSERegs = 6; 4323 } else if (IsRegCall) { 4324 // RegCall gives us 16 SSE registers, we can reuse the return registers. 4325 FreeSSERegs = 16; 4326 } 4327 4328 if (IsVectorCall) { 4329 computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); 4330 } else { 4331 for (auto &I : FI.arguments()) 4332 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); 4333 } 4334 4335 } 4336 4337 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4338 QualType Ty) const { 4339 4340 bool IsIndirect = false; 4341 4342 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 4343 // not 1, 2, 4, or 8 bytes, must be passed by reference." 4344 if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { 4345 uint64_t Width = getContext().getTypeSize(Ty); 4346 IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); 4347 } 4348 4349 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, 4350 CGF.getContext().getTypeInfoInChars(Ty), 4351 CharUnits::fromQuantity(8), 4352 /*allowHigherAlign*/ false); 4353 } 4354 4355 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4356 llvm::Value *Address, bool Is64Bit, 4357 bool IsAIX) { 4358 // This is calculated from the LLVM and GCC tables and verified 4359 // against gcc output. AFAIK all PPC ABIs use the same encoding. 4360 4361 CodeGen::CGBuilderTy &Builder = CGF.Builder; 4362 4363 llvm::IntegerType *i8 = CGF.Int8Ty; 4364 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 4365 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 4366 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 4367 4368 // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers 4369 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31); 4370 4371 // 32-63: fp0-31, the 8-byte floating-point registers 4372 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 4373 4374 // 64-67 are various 4-byte or 8-byte special-purpose registers: 4375 // 64: mq 4376 // 65: lr 4377 // 66: ctr 4378 // 67: ap 4379 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67); 4380 4381 // 68-76 are various 4-byte special-purpose registers: 4382 // 68-75 cr0-7 4383 // 76: xer 4384 AssignToArrayRange(Builder, Address, Four8, 68, 76); 4385 4386 // 77-108: v0-31, the 16-byte vector registers 4387 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 4388 4389 // 109: vrsave 4390 // 110: vscr 4391 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110); 4392 4393 // AIX does not utilize the rest of the registers. 4394 if (IsAIX) 4395 return false; 4396 4397 // 111: spe_acc 4398 // 112: spefscr 4399 // 113: sfp 4400 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113); 4401 4402 if (!Is64Bit) 4403 return false; 4404 4405 // TODO: Need to verify if these registers are used on 64 bit AIX with Power8 4406 // or above CPU. 4407 // 64-bit only registers: 4408 // 114: tfhar 4409 // 115: tfiar 4410 // 116: texasr 4411 AssignToArrayRange(Builder, Address, Eight8, 114, 116); 4412 4413 return false; 4414 } 4415 4416 // AIX 4417 namespace { 4418 /// AIXABIInfo - The AIX XCOFF ABI information. 4419 class AIXABIInfo : public ABIInfo { 4420 const bool Is64Bit; 4421 const unsigned PtrByteSize; 4422 CharUnits getParamTypeAlignment(QualType Ty) const; 4423 4424 public: 4425 AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit) 4426 : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {} 4427 4428 bool isPromotableTypeForABI(QualType Ty) const; 4429 4430 ABIArgInfo classifyReturnType(QualType RetTy) const; 4431 ABIArgInfo classifyArgumentType(QualType Ty) const; 4432 4433 void computeInfo(CGFunctionInfo &FI) const override { 4434 if (!getCXXABI().classifyReturnType(FI)) 4435 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4436 4437 for (auto &I : FI.arguments()) 4438 I.info = classifyArgumentType(I.type); 4439 } 4440 4441 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4442 QualType Ty) const override; 4443 }; 4444 4445 class AIXTargetCodeGenInfo : public TargetCodeGenInfo { 4446 const bool Is64Bit; 4447 4448 public: 4449 AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit) 4450 : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)), 4451 Is64Bit(Is64Bit) {} 4452 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4453 return 1; // r1 is the dedicated stack pointer 4454 } 4455 4456 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4457 llvm::Value *Address) const override; 4458 }; 4459 } // namespace 4460 4461 // Return true if the ABI requires Ty to be passed sign- or zero- 4462 // extended to 32/64 bits. 4463 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const { 4464 // Treat an enum type as its underlying type. 4465 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4466 Ty = EnumTy->getDecl()->getIntegerType(); 4467 4468 // Promotable integer types are required to be promoted by the ABI. 4469 if (Ty->isPromotableIntegerType()) 4470 return true; 4471 4472 if (!Is64Bit) 4473 return false; 4474 4475 // For 64 bit mode, in addition to the usual promotable integer types, we also 4476 // need to extend all 32-bit types, since the ABI requires promotion to 64 4477 // bits. 4478 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 4479 switch (BT->getKind()) { 4480 case BuiltinType::Int: 4481 case BuiltinType::UInt: 4482 return true; 4483 default: 4484 break; 4485 } 4486 4487 return false; 4488 } 4489 4490 ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const { 4491 if (RetTy->isAnyComplexType()) 4492 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4493 4494 if (RetTy->isVectorType()) 4495 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4496 4497 if (RetTy->isVoidType()) 4498 return ABIArgInfo::getIgnore(); 4499 4500 // TODO: Evaluate if AIX power alignment rule would have an impact on the 4501 // alignment here. 4502 if (isAggregateTypeForABI(RetTy)) 4503 return getNaturalAlignIndirect(RetTy); 4504 4505 return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 4506 : ABIArgInfo::getDirect()); 4507 } 4508 4509 ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const { 4510 Ty = useFirstFieldIfTransparentUnion(Ty); 4511 4512 if (Ty->isAnyComplexType()) 4513 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4514 4515 if (Ty->isVectorType()) 4516 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4517 4518 // TODO: Evaluate if AIX power alignment rule would have an impact on the 4519 // alignment here. 4520 if (isAggregateTypeForABI(Ty)) { 4521 // Records with non-trivial destructors/copy-constructors should not be 4522 // passed by value. 4523 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 4524 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 4525 4526 CharUnits CCAlign = getParamTypeAlignment(Ty); 4527 CharUnits TyAlign = getContext().getTypeAlignInChars(Ty); 4528 4529 return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true, 4530 /*Realign*/ TyAlign > CCAlign); 4531 } 4532 4533 return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 4534 : ABIArgInfo::getDirect()); 4535 } 4536 4537 CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const { 4538 if (Ty->isAnyComplexType()) 4539 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4540 4541 if (Ty->isVectorType()) 4542 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4543 4544 // If the structure contains a vector type, the alignment is 16. 4545 if (isRecordWithSIMDVectorType(getContext(), Ty)) 4546 return CharUnits::fromQuantity(16); 4547 4548 return CharUnits::fromQuantity(PtrByteSize); 4549 } 4550 4551 Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4552 QualType Ty) const { 4553 if (Ty->isAnyComplexType()) 4554 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4555 4556 if (Ty->isVectorType()) 4557 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4558 4559 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 4560 TypeInfo.second = getParamTypeAlignment(Ty); 4561 4562 CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize); 4563 4564 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo, 4565 SlotSize, /*AllowHigher*/ true); 4566 } 4567 4568 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable( 4569 CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const { 4570 return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true); 4571 } 4572 4573 // PowerPC-32 4574 namespace { 4575 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. 4576 class PPC32_SVR4_ABIInfo : public DefaultABIInfo { 4577 bool IsSoftFloatABI; 4578 bool IsRetSmallStructInRegABI; 4579 4580 CharUnits getParamTypeAlignment(QualType Ty) const; 4581 4582 public: 4583 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI, 4584 bool RetSmallStructInRegABI) 4585 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI), 4586 IsRetSmallStructInRegABI(RetSmallStructInRegABI) {} 4587 4588 ABIArgInfo classifyReturnType(QualType RetTy) const; 4589 4590 void computeInfo(CGFunctionInfo &FI) const override { 4591 if (!getCXXABI().classifyReturnType(FI)) 4592 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4593 for (auto &I : FI.arguments()) 4594 I.info = classifyArgumentType(I.type); 4595 } 4596 4597 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4598 QualType Ty) const override; 4599 }; 4600 4601 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { 4602 public: 4603 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI, 4604 bool RetSmallStructInRegABI) 4605 : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>( 4606 CGT, SoftFloatABI, RetSmallStructInRegABI)) {} 4607 4608 static bool isStructReturnInRegABI(const llvm::Triple &Triple, 4609 const CodeGenOptions &Opts); 4610 4611 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4612 // This is recovered from gcc output. 4613 return 1; // r1 is the dedicated stack pointer 4614 } 4615 4616 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4617 llvm::Value *Address) const override; 4618 }; 4619 } 4620 4621 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { 4622 // Complex types are passed just like their elements. 4623 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 4624 Ty = CTy->getElementType(); 4625 4626 if (Ty->isVectorType()) 4627 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 4628 : 4); 4629 4630 // For single-element float/vector structs, we consider the whole type 4631 // to have the same alignment requirements as its single element. 4632 const Type *AlignTy = nullptr; 4633 if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { 4634 const BuiltinType *BT = EltType->getAs<BuiltinType>(); 4635 if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || 4636 (BT && BT->isFloatingPoint())) 4637 AlignTy = EltType; 4638 } 4639 4640 if (AlignTy) 4641 return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); 4642 return CharUnits::fromQuantity(4); 4643 } 4644 4645 ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { 4646 uint64_t Size; 4647 4648 // -msvr4-struct-return puts small aggregates in GPR3 and GPR4. 4649 if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI && 4650 (Size = getContext().getTypeSize(RetTy)) <= 64) { 4651 // System V ABI (1995), page 3-22, specified: 4652 // > A structure or union whose size is less than or equal to 8 bytes 4653 // > shall be returned in r3 and r4, as if it were first stored in the 4654 // > 8-byte aligned memory area and then the low addressed word were 4655 // > loaded into r3 and the high-addressed word into r4. Bits beyond 4656 // > the last member of the structure or union are not defined. 4657 // 4658 // GCC for big-endian PPC32 inserts the pad before the first member, 4659 // not "beyond the last member" of the struct. To stay compatible 4660 // with GCC, we coerce the struct to an integer of the same size. 4661 // LLVM will extend it and return i32 in r3, or i64 in r3:r4. 4662 if (Size == 0) 4663 return ABIArgInfo::getIgnore(); 4664 else { 4665 llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size); 4666 return ABIArgInfo::getDirect(CoerceTy); 4667 } 4668 } 4669 4670 return DefaultABIInfo::classifyReturnType(RetTy); 4671 } 4672 4673 // TODO: this implementation is now likely redundant with 4674 // DefaultABIInfo::EmitVAArg. 4675 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, 4676 QualType Ty) const { 4677 if (getTarget().getTriple().isOSDarwin()) { 4678 auto TI = getContext().getTypeInfoInChars(Ty); 4679 TI.second = getParamTypeAlignment(Ty); 4680 4681 CharUnits SlotSize = CharUnits::fromQuantity(4); 4682 return emitVoidPtrVAArg(CGF, VAList, Ty, 4683 classifyArgumentType(Ty).isIndirect(), TI, SlotSize, 4684 /*AllowHigherAlign=*/true); 4685 } 4686 4687 const unsigned OverflowLimit = 8; 4688 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 4689 // TODO: Implement this. For now ignore. 4690 (void)CTy; 4691 return Address::invalid(); // FIXME? 4692 } 4693 4694 // struct __va_list_tag { 4695 // unsigned char gpr; 4696 // unsigned char fpr; 4697 // unsigned short reserved; 4698 // void *overflow_arg_area; 4699 // void *reg_save_area; 4700 // }; 4701 4702 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; 4703 bool isInt = 4704 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); 4705 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; 4706 4707 // All aggregates are passed indirectly? That doesn't seem consistent 4708 // with the argument-lowering code. 4709 bool isIndirect = Ty->isAggregateType(); 4710 4711 CGBuilderTy &Builder = CGF.Builder; 4712 4713 // The calling convention either uses 1-2 GPRs or 1 FPR. 4714 Address NumRegsAddr = Address::invalid(); 4715 if (isInt || IsSoftFloatABI) { 4716 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr"); 4717 } else { 4718 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr"); 4719 } 4720 4721 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); 4722 4723 // "Align" the register count when TY is i64. 4724 if (isI64 || (isF64 && IsSoftFloatABI)) { 4725 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); 4726 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); 4727 } 4728 4729 llvm::Value *CC = 4730 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); 4731 4732 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); 4733 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); 4734 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 4735 4736 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); 4737 4738 llvm::Type *DirectTy = CGF.ConvertType(Ty); 4739 if (isIndirect) DirectTy = DirectTy->getPointerTo(0); 4740 4741 // Case 1: consume registers. 4742 Address RegAddr = Address::invalid(); 4743 { 4744 CGF.EmitBlock(UsingRegs); 4745 4746 Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4); 4747 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), 4748 CharUnits::fromQuantity(8)); 4749 assert(RegAddr.getElementType() == CGF.Int8Ty); 4750 4751 // Floating-point registers start after the general-purpose registers. 4752 if (!(isInt || IsSoftFloatABI)) { 4753 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, 4754 CharUnits::fromQuantity(32)); 4755 } 4756 4757 // Get the address of the saved value by scaling the number of 4758 // registers we've used by the number of 4759 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); 4760 llvm::Value *RegOffset = 4761 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); 4762 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, 4763 RegAddr.getPointer(), RegOffset), 4764 RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); 4765 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); 4766 4767 // Increase the used-register count. 4768 NumRegs = 4769 Builder.CreateAdd(NumRegs, 4770 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); 4771 Builder.CreateStore(NumRegs, NumRegsAddr); 4772 4773 CGF.EmitBranch(Cont); 4774 } 4775 4776 // Case 2: consume space in the overflow area. 4777 Address MemAddr = Address::invalid(); 4778 { 4779 CGF.EmitBlock(UsingOverflow); 4780 4781 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); 4782 4783 // Everything in the overflow area is rounded up to a size of at least 4. 4784 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); 4785 4786 CharUnits Size; 4787 if (!isIndirect) { 4788 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); 4789 Size = TypeInfo.first.alignTo(OverflowAreaAlign); 4790 } else { 4791 Size = CGF.getPointerSize(); 4792 } 4793 4794 Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3); 4795 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), 4796 OverflowAreaAlign); 4797 // Round up address of argument to alignment 4798 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); 4799 if (Align > OverflowAreaAlign) { 4800 llvm::Value *Ptr = OverflowArea.getPointer(); 4801 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), 4802 Align); 4803 } 4804 4805 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); 4806 4807 // Increase the overflow area. 4808 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); 4809 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); 4810 CGF.EmitBranch(Cont); 4811 } 4812 4813 CGF.EmitBlock(Cont); 4814 4815 // Merge the cases with a phi. 4816 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, 4817 "vaarg.addr"); 4818 4819 // Load the pointer if the argument was passed indirectly. 4820 if (isIndirect) { 4821 Result = Address(Builder.CreateLoad(Result, "aggr"), 4822 getContext().getTypeAlignInChars(Ty)); 4823 } 4824 4825 return Result; 4826 } 4827 4828 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI( 4829 const llvm::Triple &Triple, const CodeGenOptions &Opts) { 4830 assert(Triple.getArch() == llvm::Triple::ppc); 4831 4832 switch (Opts.getStructReturnConvention()) { 4833 case CodeGenOptions::SRCK_Default: 4834 break; 4835 case CodeGenOptions::SRCK_OnStack: // -maix-struct-return 4836 return false; 4837 case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return 4838 return true; 4839 } 4840 4841 if (Triple.isOSBinFormatELF() && !Triple.isOSLinux()) 4842 return true; 4843 4844 return false; 4845 } 4846 4847 bool 4848 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4849 llvm::Value *Address) const { 4850 return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false, 4851 /*IsAIX*/ false); 4852 } 4853 4854 // PowerPC-64 4855 4856 namespace { 4857 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. 4858 class PPC64_SVR4_ABIInfo : public SwiftABIInfo { 4859 public: 4860 enum ABIKind { 4861 ELFv1 = 0, 4862 ELFv2 4863 }; 4864 4865 private: 4866 static const unsigned GPRBits = 64; 4867 ABIKind Kind; 4868 bool HasQPX; 4869 bool IsSoftFloatABI; 4870 4871 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and 4872 // will be passed in a QPX register. 4873 bool IsQPXVectorTy(const Type *Ty) const { 4874 if (!HasQPX) 4875 return false; 4876 4877 if (const VectorType *VT = Ty->getAs<VectorType>()) { 4878 unsigned NumElements = VT->getNumElements(); 4879 if (NumElements == 1) 4880 return false; 4881 4882 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { 4883 if (getContext().getTypeSize(Ty) <= 256) 4884 return true; 4885 } else if (VT->getElementType()-> 4886 isSpecificBuiltinType(BuiltinType::Float)) { 4887 if (getContext().getTypeSize(Ty) <= 128) 4888 return true; 4889 } 4890 } 4891 4892 return false; 4893 } 4894 4895 bool IsQPXVectorTy(QualType Ty) const { 4896 return IsQPXVectorTy(Ty.getTypePtr()); 4897 } 4898 4899 public: 4900 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, 4901 bool SoftFloatABI) 4902 : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), 4903 IsSoftFloatABI(SoftFloatABI) {} 4904 4905 bool isPromotableTypeForABI(QualType Ty) const; 4906 CharUnits getParamTypeAlignment(QualType Ty) const; 4907 4908 ABIArgInfo classifyReturnType(QualType RetTy) const; 4909 ABIArgInfo classifyArgumentType(QualType Ty) const; 4910 4911 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 4912 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 4913 uint64_t Members) const override; 4914 4915 // TODO: We can add more logic to computeInfo to improve performance. 4916 // Example: For aggregate arguments that fit in a register, we could 4917 // use getDirectInReg (as is done below for structs containing a single 4918 // floating-point value) to avoid pushing them to memory on function 4919 // entry. This would require changing the logic in PPCISelLowering 4920 // when lowering the parameters in the caller and args in the callee. 4921 void computeInfo(CGFunctionInfo &FI) const override { 4922 if (!getCXXABI().classifyReturnType(FI)) 4923 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4924 for (auto &I : FI.arguments()) { 4925 // We rely on the default argument classification for the most part. 4926 // One exception: An aggregate containing a single floating-point 4927 // or vector item must be passed in a register if one is available. 4928 const Type *T = isSingleElementStruct(I.type, getContext()); 4929 if (T) { 4930 const BuiltinType *BT = T->getAs<BuiltinType>(); 4931 if (IsQPXVectorTy(T) || 4932 (T->isVectorType() && getContext().getTypeSize(T) == 128) || 4933 (BT && BT->isFloatingPoint())) { 4934 QualType QT(T, 0); 4935 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); 4936 continue; 4937 } 4938 } 4939 I.info = classifyArgumentType(I.type); 4940 } 4941 } 4942 4943 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4944 QualType Ty) const override; 4945 4946 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 4947 bool asReturnValue) const override { 4948 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 4949 } 4950 4951 bool isSwiftErrorInRegister() const override { 4952 return false; 4953 } 4954 }; 4955 4956 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { 4957 4958 public: 4959 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, 4960 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, 4961 bool SoftFloatABI) 4962 : TargetCodeGenInfo(std::make_unique<PPC64_SVR4_ABIInfo>( 4963 CGT, Kind, HasQPX, SoftFloatABI)) {} 4964 4965 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4966 // This is recovered from gcc output. 4967 return 1; // r1 is the dedicated stack pointer 4968 } 4969 4970 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4971 llvm::Value *Address) const override; 4972 }; 4973 4974 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 4975 public: 4976 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 4977 4978 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4979 // This is recovered from gcc output. 4980 return 1; // r1 is the dedicated stack pointer 4981 } 4982 4983 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4984 llvm::Value *Address) const override; 4985 }; 4986 4987 } 4988 4989 // Return true if the ABI requires Ty to be passed sign- or zero- 4990 // extended to 64 bits. 4991 bool 4992 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { 4993 // Treat an enum type as its underlying type. 4994 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4995 Ty = EnumTy->getDecl()->getIntegerType(); 4996 4997 // Promotable integer types are required to be promoted by the ABI. 4998 if (isPromotableIntegerTypeForABI(Ty)) 4999 return true; 5000 5001 // In addition to the usual promotable integer types, we also need to 5002 // extend all 32-bit types, since the ABI requires promotion to 64 bits. 5003 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 5004 switch (BT->getKind()) { 5005 case BuiltinType::Int: 5006 case BuiltinType::UInt: 5007 return true; 5008 default: 5009 break; 5010 } 5011 5012 if (const auto *EIT = Ty->getAs<ExtIntType>()) 5013 if (EIT->getNumBits() < 64) 5014 return true; 5015 5016 return false; 5017 } 5018 5019 /// isAlignedParamType - Determine whether a type requires 16-byte or 5020 /// higher alignment in the parameter area. Always returns at least 8. 5021 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { 5022 // Complex types are passed just like their elements. 5023 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 5024 Ty = CTy->getElementType(); 5025 5026 // Only vector types of size 16 bytes need alignment (larger types are 5027 // passed via reference, smaller types are not aligned). 5028 if (IsQPXVectorTy(Ty)) { 5029 if (getContext().getTypeSize(Ty) > 128) 5030 return CharUnits::fromQuantity(32); 5031 5032 return CharUnits::fromQuantity(16); 5033 } else if (Ty->isVectorType()) { 5034 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); 5035 } 5036 5037 // For single-element float/vector structs, we consider the whole type 5038 // to have the same alignment requirements as its single element. 5039 const Type *AlignAsType = nullptr; 5040 const Type *EltType = isSingleElementStruct(Ty, getContext()); 5041 if (EltType) { 5042 const BuiltinType *BT = EltType->getAs<BuiltinType>(); 5043 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && 5044 getContext().getTypeSize(EltType) == 128) || 5045 (BT && BT->isFloatingPoint())) 5046 AlignAsType = EltType; 5047 } 5048 5049 // Likewise for ELFv2 homogeneous aggregates. 5050 const Type *Base = nullptr; 5051 uint64_t Members = 0; 5052 if (!AlignAsType && Kind == ELFv2 && 5053 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) 5054 AlignAsType = Base; 5055 5056 // With special case aggregates, only vector base types need alignment. 5057 if (AlignAsType && IsQPXVectorTy(AlignAsType)) { 5058 if (getContext().getTypeSize(AlignAsType) > 128) 5059 return CharUnits::fromQuantity(32); 5060 5061 return CharUnits::fromQuantity(16); 5062 } else if (AlignAsType) { 5063 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); 5064 } 5065 5066 // Otherwise, we only need alignment for any aggregate type that 5067 // has an alignment requirement of >= 16 bytes. 5068 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { 5069 if (HasQPX && getContext().getTypeAlign(Ty) >= 256) 5070 return CharUnits::fromQuantity(32); 5071 return CharUnits::fromQuantity(16); 5072 } 5073 5074 return CharUnits::fromQuantity(8); 5075 } 5076 5077 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous 5078 /// aggregate. Base is set to the base element type, and Members is set 5079 /// to the number of base elements. 5080 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, 5081 uint64_t &Members) const { 5082 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 5083 uint64_t NElements = AT->getSize().getZExtValue(); 5084 if (NElements == 0) 5085 return false; 5086 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) 5087 return false; 5088 Members *= NElements; 5089 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 5090 const RecordDecl *RD = RT->getDecl(); 5091 if (RD->hasFlexibleArrayMember()) 5092 return false; 5093 5094 Members = 0; 5095 5096 // If this is a C++ record, check the bases first. 5097 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 5098 for (const auto &I : CXXRD->bases()) { 5099 // Ignore empty records. 5100 if (isEmptyRecord(getContext(), I.getType(), true)) 5101 continue; 5102 5103 uint64_t FldMembers; 5104 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) 5105 return false; 5106 5107 Members += FldMembers; 5108 } 5109 } 5110 5111 for (const auto *FD : RD->fields()) { 5112 // Ignore (non-zero arrays of) empty records. 5113 QualType FT = FD->getType(); 5114 while (const ConstantArrayType *AT = 5115 getContext().getAsConstantArrayType(FT)) { 5116 if (AT->getSize().getZExtValue() == 0) 5117 return false; 5118 FT = AT->getElementType(); 5119 } 5120 if (isEmptyRecord(getContext(), FT, true)) 5121 continue; 5122 5123 // For compatibility with GCC, ignore empty bitfields in C++ mode. 5124 if (getContext().getLangOpts().CPlusPlus && 5125 FD->isZeroLengthBitField(getContext())) 5126 continue; 5127 5128 uint64_t FldMembers; 5129 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) 5130 return false; 5131 5132 Members = (RD->isUnion() ? 5133 std::max(Members, FldMembers) : Members + FldMembers); 5134 } 5135 5136 if (!Base) 5137 return false; 5138 5139 // Ensure there is no padding. 5140 if (getContext().getTypeSize(Base) * Members != 5141 getContext().getTypeSize(Ty)) 5142 return false; 5143 } else { 5144 Members = 1; 5145 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 5146 Members = 2; 5147 Ty = CT->getElementType(); 5148 } 5149 5150 // Most ABIs only support float, double, and some vector type widths. 5151 if (!isHomogeneousAggregateBaseType(Ty)) 5152 return false; 5153 5154 // The base type must be the same for all members. Types that 5155 // agree in both total size and mode (float vs. vector) are 5156 // treated as being equivalent here. 5157 const Type *TyPtr = Ty.getTypePtr(); 5158 if (!Base) { 5159 Base = TyPtr; 5160 // If it's a non-power-of-2 vector, its size is already a power-of-2, 5161 // so make sure to widen it explicitly. 5162 if (const VectorType *VT = Base->getAs<VectorType>()) { 5163 QualType EltTy = VT->getElementType(); 5164 unsigned NumElements = 5165 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); 5166 Base = getContext() 5167 .getVectorType(EltTy, NumElements, VT->getVectorKind()) 5168 .getTypePtr(); 5169 } 5170 } 5171 5172 if (Base->isVectorType() != TyPtr->isVectorType() || 5173 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) 5174 return false; 5175 } 5176 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); 5177 } 5178 5179 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 5180 // Homogeneous aggregates for ELFv2 must have base types of float, 5181 // double, long double, or 128-bit vectors. 5182 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 5183 if (BT->getKind() == BuiltinType::Float || 5184 BT->getKind() == BuiltinType::Double || 5185 BT->getKind() == BuiltinType::LongDouble || 5186 (getContext().getTargetInfo().hasFloat128Type() && 5187 (BT->getKind() == BuiltinType::Float128))) { 5188 if (IsSoftFloatABI) 5189 return false; 5190 return true; 5191 } 5192 } 5193 if (const VectorType *VT = Ty->getAs<VectorType>()) { 5194 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) 5195 return true; 5196 } 5197 return false; 5198 } 5199 5200 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( 5201 const Type *Base, uint64_t Members) const { 5202 // Vector and fp128 types require one register, other floating point types 5203 // require one or two registers depending on their size. 5204 uint32_t NumRegs = 5205 ((getContext().getTargetInfo().hasFloat128Type() && 5206 Base->isFloat128Type()) || 5207 Base->isVectorType()) ? 1 5208 : (getContext().getTypeSize(Base) + 63) / 64; 5209 5210 // Homogeneous Aggregates may occupy at most 8 registers. 5211 return Members * NumRegs <= 8; 5212 } 5213 5214 ABIArgInfo 5215 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { 5216 Ty = useFirstFieldIfTransparentUnion(Ty); 5217 5218 if (Ty->isAnyComplexType()) 5219 return ABIArgInfo::getDirect(); 5220 5221 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) 5222 // or via reference (larger than 16 bytes). 5223 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { 5224 uint64_t Size = getContext().getTypeSize(Ty); 5225 if (Size > 128) 5226 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 5227 else if (Size < 128) { 5228 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 5229 return ABIArgInfo::getDirect(CoerceTy); 5230 } 5231 } 5232 5233 if (const auto *EIT = Ty->getAs<ExtIntType>()) 5234 if (EIT->getNumBits() > 128) 5235 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 5236 5237 if (isAggregateTypeForABI(Ty)) { 5238 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 5239 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 5240 5241 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); 5242 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); 5243 5244 // ELFv2 homogeneous aggregates are passed as array types. 5245 const Type *Base = nullptr; 5246 uint64_t Members = 0; 5247 if (Kind == ELFv2 && 5248 isHomogeneousAggregate(Ty, Base, Members)) { 5249 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 5250 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 5251 return ABIArgInfo::getDirect(CoerceTy); 5252 } 5253 5254 // If an aggregate may end up fully in registers, we do not 5255 // use the ByVal method, but pass the aggregate as array. 5256 // This is usually beneficial since we avoid forcing the 5257 // back-end to store the argument to memory. 5258 uint64_t Bits = getContext().getTypeSize(Ty); 5259 if (Bits > 0 && Bits <= 8 * GPRBits) { 5260 llvm::Type *CoerceTy; 5261 5262 // Types up to 8 bytes are passed as integer type (which will be 5263 // properly aligned in the argument save area doubleword). 5264 if (Bits <= GPRBits) 5265 CoerceTy = 5266 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); 5267 // Larger types are passed as arrays, with the base type selected 5268 // according to the required alignment in the save area. 5269 else { 5270 uint64_t RegBits = ABIAlign * 8; 5271 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; 5272 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); 5273 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); 5274 } 5275 5276 return ABIArgInfo::getDirect(CoerceTy); 5277 } 5278 5279 // All other aggregates are passed ByVal. 5280 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), 5281 /*ByVal=*/true, 5282 /*Realign=*/TyAlign > ABIAlign); 5283 } 5284 5285 return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 5286 : ABIArgInfo::getDirect()); 5287 } 5288 5289 ABIArgInfo 5290 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { 5291 if (RetTy->isVoidType()) 5292 return ABIArgInfo::getIgnore(); 5293 5294 if (RetTy->isAnyComplexType()) 5295 return ABIArgInfo::getDirect(); 5296 5297 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) 5298 // or via reference (larger than 16 bytes). 5299 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { 5300 uint64_t Size = getContext().getTypeSize(RetTy); 5301 if (Size > 128) 5302 return getNaturalAlignIndirect(RetTy); 5303 else if (Size < 128) { 5304 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 5305 return ABIArgInfo::getDirect(CoerceTy); 5306 } 5307 } 5308 5309 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 5310 if (EIT->getNumBits() > 128) 5311 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 5312 5313 if (isAggregateTypeForABI(RetTy)) { 5314 // ELFv2 homogeneous aggregates are returned as array types. 5315 const Type *Base = nullptr; 5316 uint64_t Members = 0; 5317 if (Kind == ELFv2 && 5318 isHomogeneousAggregate(RetTy, Base, Members)) { 5319 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 5320 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 5321 return ABIArgInfo::getDirect(CoerceTy); 5322 } 5323 5324 // ELFv2 small aggregates are returned in up to two registers. 5325 uint64_t Bits = getContext().getTypeSize(RetTy); 5326 if (Kind == ELFv2 && Bits <= 2 * GPRBits) { 5327 if (Bits == 0) 5328 return ABIArgInfo::getIgnore(); 5329 5330 llvm::Type *CoerceTy; 5331 if (Bits > GPRBits) { 5332 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); 5333 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); 5334 } else 5335 CoerceTy = 5336 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); 5337 return ABIArgInfo::getDirect(CoerceTy); 5338 } 5339 5340 // All other aggregates are returned indirectly. 5341 return getNaturalAlignIndirect(RetTy); 5342 } 5343 5344 return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 5345 : ABIArgInfo::getDirect()); 5346 } 5347 5348 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. 5349 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5350 QualType Ty) const { 5351 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 5352 TypeInfo.second = getParamTypeAlignment(Ty); 5353 5354 CharUnits SlotSize = CharUnits::fromQuantity(8); 5355 5356 // If we have a complex type and the base type is smaller than 8 bytes, 5357 // the ABI calls for the real and imaginary parts to be right-adjusted 5358 // in separate doublewords. However, Clang expects us to produce a 5359 // pointer to a structure with the two parts packed tightly. So generate 5360 // loads of the real and imaginary parts relative to the va_list pointer, 5361 // and store them to a temporary structure. 5362 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 5363 CharUnits EltSize = TypeInfo.first / 2; 5364 if (EltSize < SlotSize) { 5365 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, 5366 SlotSize * 2, SlotSize, 5367 SlotSize, /*AllowHigher*/ true); 5368 5369 Address RealAddr = Addr; 5370 Address ImagAddr = RealAddr; 5371 if (CGF.CGM.getDataLayout().isBigEndian()) { 5372 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, 5373 SlotSize - EltSize); 5374 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, 5375 2 * SlotSize - EltSize); 5376 } else { 5377 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); 5378 } 5379 5380 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); 5381 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); 5382 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); 5383 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); 5384 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); 5385 5386 Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); 5387 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), 5388 /*init*/ true); 5389 return Temp; 5390 } 5391 } 5392 5393 // Otherwise, just use the general rule. 5394 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, 5395 TypeInfo, SlotSize, /*AllowHigher*/ true); 5396 } 5397 5398 bool 5399 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( 5400 CodeGen::CodeGenFunction &CGF, 5401 llvm::Value *Address) const { 5402 return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true, 5403 /*IsAIX*/ false); 5404 } 5405 5406 bool 5407 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 5408 llvm::Value *Address) const { 5409 return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true, 5410 /*IsAIX*/ false); 5411 } 5412 5413 //===----------------------------------------------------------------------===// 5414 // AArch64 ABI Implementation 5415 //===----------------------------------------------------------------------===// 5416 5417 namespace { 5418 5419 class AArch64ABIInfo : public SwiftABIInfo { 5420 public: 5421 enum ABIKind { 5422 AAPCS = 0, 5423 DarwinPCS, 5424 Win64 5425 }; 5426 5427 private: 5428 ABIKind Kind; 5429 5430 public: 5431 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) 5432 : SwiftABIInfo(CGT), Kind(Kind) {} 5433 5434 private: 5435 ABIKind getABIKind() const { return Kind; } 5436 bool isDarwinPCS() const { return Kind == DarwinPCS; } 5437 5438 ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const; 5439 ABIArgInfo classifyArgumentType(QualType RetTy) const; 5440 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 5441 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 5442 uint64_t Members) const override; 5443 5444 bool isIllegalVectorType(QualType Ty) const; 5445 5446 void computeInfo(CGFunctionInfo &FI) const override { 5447 if (!::classifyReturnType(getCXXABI(), FI, *this)) 5448 FI.getReturnInfo() = 5449 classifyReturnType(FI.getReturnType(), FI.isVariadic()); 5450 5451 for (auto &it : FI.arguments()) 5452 it.info = classifyArgumentType(it.type); 5453 } 5454 5455 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, 5456 CodeGenFunction &CGF) const; 5457 5458 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, 5459 CodeGenFunction &CGF) const; 5460 5461 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5462 QualType Ty) const override { 5463 return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) 5464 : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) 5465 : EmitAAPCSVAArg(VAListAddr, Ty, CGF); 5466 } 5467 5468 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 5469 QualType Ty) const override; 5470 5471 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 5472 bool asReturnValue) const override { 5473 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 5474 } 5475 bool isSwiftErrorInRegister() const override { 5476 return true; 5477 } 5478 5479 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, 5480 unsigned elts) const override; 5481 5482 bool allowBFloatArgsAndRet() const override { 5483 return getTarget().hasBFloat16Type(); 5484 } 5485 }; 5486 5487 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { 5488 public: 5489 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) 5490 : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {} 5491 5492 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 5493 return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; 5494 } 5495 5496 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 5497 return 31; 5498 } 5499 5500 bool doesReturnSlotInterfereWithArgs() const override { return false; } 5501 5502 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5503 CodeGen::CodeGenModule &CGM) const override { 5504 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 5505 if (!FD) 5506 return; 5507 5508 LangOptions::SignReturnAddressScopeKind Scope = 5509 CGM.getLangOpts().getSignReturnAddressScope(); 5510 LangOptions::SignReturnAddressKeyKind Key = 5511 CGM.getLangOpts().getSignReturnAddressKey(); 5512 bool BranchTargetEnforcement = CGM.getLangOpts().BranchTargetEnforcement; 5513 if (const auto *TA = FD->getAttr<TargetAttr>()) { 5514 ParsedTargetAttr Attr = TA->parse(); 5515 if (!Attr.BranchProtection.empty()) { 5516 TargetInfo::BranchProtectionInfo BPI; 5517 StringRef Error; 5518 (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection, 5519 BPI, Error); 5520 assert(Error.empty()); 5521 Scope = BPI.SignReturnAddr; 5522 Key = BPI.SignKey; 5523 BranchTargetEnforcement = BPI.BranchTargetEnforcement; 5524 } 5525 } 5526 5527 auto *Fn = cast<llvm::Function>(GV); 5528 if (Scope != LangOptions::SignReturnAddressScopeKind::None) { 5529 Fn->addFnAttr("sign-return-address", 5530 Scope == LangOptions::SignReturnAddressScopeKind::All 5531 ? "all" 5532 : "non-leaf"); 5533 5534 Fn->addFnAttr("sign-return-address-key", 5535 Key == LangOptions::SignReturnAddressKeyKind::AKey 5536 ? "a_key" 5537 : "b_key"); 5538 } 5539 5540 if (BranchTargetEnforcement) 5541 Fn->addFnAttr("branch-target-enforcement"); 5542 } 5543 }; 5544 5545 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { 5546 public: 5547 WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) 5548 : AArch64TargetCodeGenInfo(CGT, K) {} 5549 5550 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5551 CodeGen::CodeGenModule &CGM) const override; 5552 5553 void getDependentLibraryOption(llvm::StringRef Lib, 5554 llvm::SmallString<24> &Opt) const override { 5555 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); 5556 } 5557 5558 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, 5559 llvm::SmallString<32> &Opt) const override { 5560 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 5561 } 5562 }; 5563 5564 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( 5565 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 5566 AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 5567 if (GV->isDeclaration()) 5568 return; 5569 addStackProbeTargetAttributes(D, GV, CGM); 5570 } 5571 } 5572 5573 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { 5574 Ty = useFirstFieldIfTransparentUnion(Ty); 5575 5576 // Handle illegal vector types here. 5577 if (isIllegalVectorType(Ty)) { 5578 uint64_t Size = getContext().getTypeSize(Ty); 5579 // Android promotes <2 x i8> to i16, not i32 5580 if (isAndroid() && (Size <= 16)) { 5581 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); 5582 return ABIArgInfo::getDirect(ResType); 5583 } 5584 if (Size <= 32) { 5585 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); 5586 return ABIArgInfo::getDirect(ResType); 5587 } 5588 if (Size == 64) { 5589 auto *ResType = 5590 llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); 5591 return ABIArgInfo::getDirect(ResType); 5592 } 5593 if (Size == 128) { 5594 auto *ResType = 5595 llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); 5596 return ABIArgInfo::getDirect(ResType); 5597 } 5598 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 5599 } 5600 5601 if (!isAggregateTypeForABI(Ty)) { 5602 // Treat an enum type as its underlying type. 5603 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 5604 Ty = EnumTy->getDecl()->getIntegerType(); 5605 5606 if (const auto *EIT = Ty->getAs<ExtIntType>()) 5607 if (EIT->getNumBits() > 128) 5608 return getNaturalAlignIndirect(Ty); 5609 5610 return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS() 5611 ? ABIArgInfo::getExtend(Ty) 5612 : ABIArgInfo::getDirect()); 5613 } 5614 5615 // Structures with either a non-trivial destructor or a non-trivial 5616 // copy constructor are always indirect. 5617 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 5618 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == 5619 CGCXXABI::RAA_DirectInMemory); 5620 } 5621 5622 // Empty records are always ignored on Darwin, but actually passed in C++ mode 5623 // elsewhere for GNU compatibility. 5624 uint64_t Size = getContext().getTypeSize(Ty); 5625 bool IsEmpty = isEmptyRecord(getContext(), Ty, true); 5626 if (IsEmpty || Size == 0) { 5627 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) 5628 return ABIArgInfo::getIgnore(); 5629 5630 // GNU C mode. The only argument that gets ignored is an empty one with size 5631 // 0. 5632 if (IsEmpty && Size == 0) 5633 return ABIArgInfo::getIgnore(); 5634 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 5635 } 5636 5637 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. 5638 const Type *Base = nullptr; 5639 uint64_t Members = 0; 5640 if (isHomogeneousAggregate(Ty, Base, Members)) { 5641 return ABIArgInfo::getDirect( 5642 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); 5643 } 5644 5645 // Aggregates <= 16 bytes are passed directly in registers or on the stack. 5646 if (Size <= 128) { 5647 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of 5648 // same size and alignment. 5649 if (getTarget().isRenderScriptTarget()) { 5650 return coerceToIntArray(Ty, getContext(), getVMContext()); 5651 } 5652 unsigned Alignment; 5653 if (Kind == AArch64ABIInfo::AAPCS) { 5654 Alignment = getContext().getTypeUnadjustedAlign(Ty); 5655 Alignment = Alignment < 128 ? 64 : 128; 5656 } else { 5657 Alignment = std::max(getContext().getTypeAlign(Ty), 5658 (unsigned)getTarget().getPointerWidth(0)); 5659 } 5660 Size = llvm::alignTo(Size, Alignment); 5661 5662 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. 5663 // For aggregates with 16-byte alignment, we use i128. 5664 llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment); 5665 return ABIArgInfo::getDirect( 5666 Size == Alignment ? BaseTy 5667 : llvm::ArrayType::get(BaseTy, Size / Alignment)); 5668 } 5669 5670 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 5671 } 5672 5673 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy, 5674 bool IsVariadic) const { 5675 if (RetTy->isVoidType()) 5676 return ABIArgInfo::getIgnore(); 5677 5678 // Large vector types should be returned via memory. 5679 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) 5680 return getNaturalAlignIndirect(RetTy); 5681 5682 if (!isAggregateTypeForABI(RetTy)) { 5683 // Treat an enum type as its underlying type. 5684 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 5685 RetTy = EnumTy->getDecl()->getIntegerType(); 5686 5687 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 5688 if (EIT->getNumBits() > 128) 5689 return getNaturalAlignIndirect(RetTy); 5690 5691 return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS() 5692 ? ABIArgInfo::getExtend(RetTy) 5693 : ABIArgInfo::getDirect()); 5694 } 5695 5696 uint64_t Size = getContext().getTypeSize(RetTy); 5697 if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) 5698 return ABIArgInfo::getIgnore(); 5699 5700 const Type *Base = nullptr; 5701 uint64_t Members = 0; 5702 if (isHomogeneousAggregate(RetTy, Base, Members) && 5703 !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 && 5704 IsVariadic)) 5705 // Homogeneous Floating-point Aggregates (HFAs) are returned directly. 5706 return ABIArgInfo::getDirect(); 5707 5708 // Aggregates <= 16 bytes are returned directly in registers or on the stack. 5709 if (Size <= 128) { 5710 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of 5711 // same size and alignment. 5712 if (getTarget().isRenderScriptTarget()) { 5713 return coerceToIntArray(RetTy, getContext(), getVMContext()); 5714 } 5715 unsigned Alignment = getContext().getTypeAlign(RetTy); 5716 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes 5717 5718 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. 5719 // For aggregates with 16-byte alignment, we use i128. 5720 if (Alignment < 128 && Size == 128) { 5721 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); 5722 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); 5723 } 5724 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); 5725 } 5726 5727 return getNaturalAlignIndirect(RetTy); 5728 } 5729 5730 /// isIllegalVectorType - check whether the vector type is legal for AArch64. 5731 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { 5732 if (const VectorType *VT = Ty->getAs<VectorType>()) { 5733 // Check whether VT is legal. 5734 unsigned NumElements = VT->getNumElements(); 5735 uint64_t Size = getContext().getTypeSize(VT); 5736 // NumElements should be power of 2. 5737 if (!llvm::isPowerOf2_32(NumElements)) 5738 return true; 5739 5740 // arm64_32 has to be compatible with the ARM logic here, which allows huge 5741 // vectors for some reason. 5742 llvm::Triple Triple = getTarget().getTriple(); 5743 if (Triple.getArch() == llvm::Triple::aarch64_32 && 5744 Triple.isOSBinFormatMachO()) 5745 return Size <= 32; 5746 5747 return Size != 64 && (Size != 128 || NumElements == 1); 5748 } 5749 return false; 5750 } 5751 5752 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, 5753 llvm::Type *eltTy, 5754 unsigned elts) const { 5755 if (!llvm::isPowerOf2_32(elts)) 5756 return false; 5757 if (totalSize.getQuantity() != 8 && 5758 (totalSize.getQuantity() != 16 || elts == 1)) 5759 return false; 5760 return true; 5761 } 5762 5763 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 5764 // Homogeneous aggregates for AAPCS64 must have base types of a floating 5765 // point type or a short-vector type. This is the same as the 32-bit ABI, 5766 // but with the difference that any floating-point type is allowed, 5767 // including __fp16. 5768 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 5769 if (BT->isFloatingPoint()) 5770 return true; 5771 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 5772 unsigned VecSize = getContext().getTypeSize(VT); 5773 if (VecSize == 64 || VecSize == 128) 5774 return true; 5775 } 5776 return false; 5777 } 5778 5779 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 5780 uint64_t Members) const { 5781 return Members <= 4; 5782 } 5783 5784 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, 5785 QualType Ty, 5786 CodeGenFunction &CGF) const { 5787 ABIArgInfo AI = classifyArgumentType(Ty); 5788 bool IsIndirect = AI.isIndirect(); 5789 5790 llvm::Type *BaseTy = CGF.ConvertType(Ty); 5791 if (IsIndirect) 5792 BaseTy = llvm::PointerType::getUnqual(BaseTy); 5793 else if (AI.getCoerceToType()) 5794 BaseTy = AI.getCoerceToType(); 5795 5796 unsigned NumRegs = 1; 5797 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { 5798 BaseTy = ArrTy->getElementType(); 5799 NumRegs = ArrTy->getNumElements(); 5800 } 5801 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); 5802 5803 // The AArch64 va_list type and handling is specified in the Procedure Call 5804 // Standard, section B.4: 5805 // 5806 // struct { 5807 // void *__stack; 5808 // void *__gr_top; 5809 // void *__vr_top; 5810 // int __gr_offs; 5811 // int __vr_offs; 5812 // }; 5813 5814 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); 5815 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 5816 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); 5817 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 5818 5819 CharUnits TySize = getContext().getTypeSizeInChars(Ty); 5820 CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty); 5821 5822 Address reg_offs_p = Address::invalid(); 5823 llvm::Value *reg_offs = nullptr; 5824 int reg_top_index; 5825 int RegSize = IsIndirect ? 8 : TySize.getQuantity(); 5826 if (!IsFPR) { 5827 // 3 is the field number of __gr_offs 5828 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); 5829 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); 5830 reg_top_index = 1; // field number for __gr_top 5831 RegSize = llvm::alignTo(RegSize, 8); 5832 } else { 5833 // 4 is the field number of __vr_offs. 5834 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); 5835 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); 5836 reg_top_index = 2; // field number for __vr_top 5837 RegSize = 16 * NumRegs; 5838 } 5839 5840 //======================================= 5841 // Find out where argument was passed 5842 //======================================= 5843 5844 // If reg_offs >= 0 we're already using the stack for this type of 5845 // argument. We don't want to keep updating reg_offs (in case it overflows, 5846 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves 5847 // whatever they get). 5848 llvm::Value *UsingStack = nullptr; 5849 UsingStack = CGF.Builder.CreateICmpSGE( 5850 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); 5851 5852 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); 5853 5854 // Otherwise, at least some kind of argument could go in these registers, the 5855 // question is whether this particular type is too big. 5856 CGF.EmitBlock(MaybeRegBlock); 5857 5858 // Integer arguments may need to correct register alignment (for example a 5859 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we 5860 // align __gr_offs to calculate the potential address. 5861 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { 5862 int Align = TyAlign.getQuantity(); 5863 5864 reg_offs = CGF.Builder.CreateAdd( 5865 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), 5866 "align_regoffs"); 5867 reg_offs = CGF.Builder.CreateAnd( 5868 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), 5869 "aligned_regoffs"); 5870 } 5871 5872 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. 5873 // The fact that this is done unconditionally reflects the fact that 5874 // allocating an argument to the stack also uses up all the remaining 5875 // registers of the appropriate kind. 5876 llvm::Value *NewOffset = nullptr; 5877 NewOffset = CGF.Builder.CreateAdd( 5878 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); 5879 CGF.Builder.CreateStore(NewOffset, reg_offs_p); 5880 5881 // Now we're in a position to decide whether this argument really was in 5882 // registers or not. 5883 llvm::Value *InRegs = nullptr; 5884 InRegs = CGF.Builder.CreateICmpSLE( 5885 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); 5886 5887 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); 5888 5889 //======================================= 5890 // Argument was in registers 5891 //======================================= 5892 5893 // Now we emit the code for if the argument was originally passed in 5894 // registers. First start the appropriate block: 5895 CGF.EmitBlock(InRegBlock); 5896 5897 llvm::Value *reg_top = nullptr; 5898 Address reg_top_p = 5899 CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); 5900 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); 5901 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), 5902 CharUnits::fromQuantity(IsFPR ? 16 : 8)); 5903 Address RegAddr = Address::invalid(); 5904 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); 5905 5906 if (IsIndirect) { 5907 // If it's been passed indirectly (actually a struct), whatever we find from 5908 // stored registers or on the stack will actually be a struct **. 5909 MemTy = llvm::PointerType::getUnqual(MemTy); 5910 } 5911 5912 const Type *Base = nullptr; 5913 uint64_t NumMembers = 0; 5914 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); 5915 if (IsHFA && NumMembers > 1) { 5916 // Homogeneous aggregates passed in registers will have their elements split 5917 // and stored 16-bytes apart regardless of size (they're notionally in qN, 5918 // qN+1, ...). We reload and store into a temporary local variable 5919 // contiguously. 5920 assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); 5921 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); 5922 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); 5923 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); 5924 Address Tmp = CGF.CreateTempAlloca(HFATy, 5925 std::max(TyAlign, BaseTyInfo.second)); 5926 5927 // On big-endian platforms, the value will be right-aligned in its slot. 5928 int Offset = 0; 5929 if (CGF.CGM.getDataLayout().isBigEndian() && 5930 BaseTyInfo.first.getQuantity() < 16) 5931 Offset = 16 - BaseTyInfo.first.getQuantity(); 5932 5933 for (unsigned i = 0; i < NumMembers; ++i) { 5934 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); 5935 Address LoadAddr = 5936 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); 5937 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); 5938 5939 Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i); 5940 5941 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); 5942 CGF.Builder.CreateStore(Elem, StoreAddr); 5943 } 5944 5945 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); 5946 } else { 5947 // Otherwise the object is contiguous in memory. 5948 5949 // It might be right-aligned in its slot. 5950 CharUnits SlotSize = BaseAddr.getAlignment(); 5951 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && 5952 (IsHFA || !isAggregateTypeForABI(Ty)) && 5953 TySize < SlotSize) { 5954 CharUnits Offset = SlotSize - TySize; 5955 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); 5956 } 5957 5958 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); 5959 } 5960 5961 CGF.EmitBranch(ContBlock); 5962 5963 //======================================= 5964 // Argument was on the stack 5965 //======================================= 5966 CGF.EmitBlock(OnStackBlock); 5967 5968 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); 5969 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); 5970 5971 // Again, stack arguments may need realignment. In this case both integer and 5972 // floating-point ones might be affected. 5973 if (!IsIndirect && TyAlign.getQuantity() > 8) { 5974 int Align = TyAlign.getQuantity(); 5975 5976 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); 5977 5978 OnStackPtr = CGF.Builder.CreateAdd( 5979 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), 5980 "align_stack"); 5981 OnStackPtr = CGF.Builder.CreateAnd( 5982 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), 5983 "align_stack"); 5984 5985 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); 5986 } 5987 Address OnStackAddr(OnStackPtr, 5988 std::max(CharUnits::fromQuantity(8), TyAlign)); 5989 5990 // All stack slots are multiples of 8 bytes. 5991 CharUnits StackSlotSize = CharUnits::fromQuantity(8); 5992 CharUnits StackSize; 5993 if (IsIndirect) 5994 StackSize = StackSlotSize; 5995 else 5996 StackSize = TySize.alignTo(StackSlotSize); 5997 5998 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); 5999 llvm::Value *NewStack = 6000 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); 6001 6002 // Write the new value of __stack for the next call to va_arg 6003 CGF.Builder.CreateStore(NewStack, stack_p); 6004 6005 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && 6006 TySize < StackSlotSize) { 6007 CharUnits Offset = StackSlotSize - TySize; 6008 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); 6009 } 6010 6011 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); 6012 6013 CGF.EmitBranch(ContBlock); 6014 6015 //======================================= 6016 // Tidy up 6017 //======================================= 6018 CGF.EmitBlock(ContBlock); 6019 6020 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, 6021 OnStackAddr, OnStackBlock, "vaargs.addr"); 6022 6023 if (IsIndirect) 6024 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), 6025 TyAlign); 6026 6027 return ResAddr; 6028 } 6029 6030 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, 6031 CodeGenFunction &CGF) const { 6032 // The backend's lowering doesn't support va_arg for aggregates or 6033 // illegal vector types. Lower VAArg here for these cases and use 6034 // the LLVM va_arg instruction for everything else. 6035 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) 6036 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); 6037 6038 uint64_t PointerSize = getTarget().getPointerWidth(0) / 8; 6039 CharUnits SlotSize = CharUnits::fromQuantity(PointerSize); 6040 6041 // Empty records are ignored for parameter passing purposes. 6042 if (isEmptyRecord(getContext(), Ty, true)) { 6043 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); 6044 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 6045 return Addr; 6046 } 6047 6048 // The size of the actual thing passed, which might end up just 6049 // being a pointer for indirect types. 6050 auto TyInfo = getContext().getTypeInfoInChars(Ty); 6051 6052 // Arguments bigger than 16 bytes which aren't homogeneous 6053 // aggregates should be passed indirectly. 6054 bool IsIndirect = false; 6055 if (TyInfo.first.getQuantity() > 16) { 6056 const Type *Base = nullptr; 6057 uint64_t Members = 0; 6058 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); 6059 } 6060 6061 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, 6062 TyInfo, SlotSize, /*AllowHigherAlign*/ true); 6063 } 6064 6065 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 6066 QualType Ty) const { 6067 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 6068 CGF.getContext().getTypeInfoInChars(Ty), 6069 CharUnits::fromQuantity(8), 6070 /*allowHigherAlign*/ false); 6071 } 6072 6073 //===----------------------------------------------------------------------===// 6074 // ARM ABI Implementation 6075 //===----------------------------------------------------------------------===// 6076 6077 namespace { 6078 6079 class ARMABIInfo : public SwiftABIInfo { 6080 public: 6081 enum ABIKind { 6082 APCS = 0, 6083 AAPCS = 1, 6084 AAPCS_VFP = 2, 6085 AAPCS16_VFP = 3, 6086 }; 6087 6088 private: 6089 ABIKind Kind; 6090 bool IsFloatABISoftFP; 6091 6092 public: 6093 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) 6094 : SwiftABIInfo(CGT), Kind(_Kind) { 6095 setCCs(); 6096 IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" || 6097 CGT.getCodeGenOpts().FloatABI == ""; // default 6098 } 6099 6100 bool isEABI() const { 6101 switch (getTarget().getTriple().getEnvironment()) { 6102 case llvm::Triple::Android: 6103 case llvm::Triple::EABI: 6104 case llvm::Triple::EABIHF: 6105 case llvm::Triple::GNUEABI: 6106 case llvm::Triple::GNUEABIHF: 6107 case llvm::Triple::MuslEABI: 6108 case llvm::Triple::MuslEABIHF: 6109 return true; 6110 default: 6111 return false; 6112 } 6113 } 6114 6115 bool isEABIHF() const { 6116 switch (getTarget().getTriple().getEnvironment()) { 6117 case llvm::Triple::EABIHF: 6118 case llvm::Triple::GNUEABIHF: 6119 case llvm::Triple::MuslEABIHF: 6120 return true; 6121 default: 6122 return false; 6123 } 6124 } 6125 6126 ABIKind getABIKind() const { return Kind; } 6127 6128 bool allowBFloatArgsAndRet() const override { 6129 return !IsFloatABISoftFP && getTarget().hasBFloat16Type(); 6130 } 6131 6132 private: 6133 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic, 6134 unsigned functionCallConv) const; 6135 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, 6136 unsigned functionCallConv) const; 6137 ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, 6138 uint64_t Members) const; 6139 ABIArgInfo coerceIllegalVector(QualType Ty) const; 6140 bool isIllegalVectorType(QualType Ty) const; 6141 bool containsAnyFP16Vectors(QualType Ty) const; 6142 6143 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 6144 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 6145 uint64_t Members) const override; 6146 6147 bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const; 6148 6149 void computeInfo(CGFunctionInfo &FI) const override; 6150 6151 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6152 QualType Ty) const override; 6153 6154 llvm::CallingConv::ID getLLVMDefaultCC() const; 6155 llvm::CallingConv::ID getABIDefaultCC() const; 6156 void setCCs(); 6157 6158 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 6159 bool asReturnValue) const override { 6160 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 6161 } 6162 bool isSwiftErrorInRegister() const override { 6163 return true; 6164 } 6165 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, 6166 unsigned elts) const override; 6167 }; 6168 6169 class ARMTargetCodeGenInfo : public TargetCodeGenInfo { 6170 public: 6171 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 6172 : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {} 6173 6174 const ARMABIInfo &getABIInfo() const { 6175 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); 6176 } 6177 6178 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 6179 return 13; 6180 } 6181 6182 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 6183 return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; 6184 } 6185 6186 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 6187 llvm::Value *Address) const override { 6188 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 6189 6190 // 0-15 are the 16 integer registers. 6191 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); 6192 return false; 6193 } 6194 6195 unsigned getSizeOfUnwindException() const override { 6196 if (getABIInfo().isEABI()) return 88; 6197 return TargetCodeGenInfo::getSizeOfUnwindException(); 6198 } 6199 6200 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6201 CodeGen::CodeGenModule &CGM) const override { 6202 if (GV->isDeclaration()) 6203 return; 6204 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 6205 if (!FD) 6206 return; 6207 6208 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); 6209 if (!Attr) 6210 return; 6211 6212 const char *Kind; 6213 switch (Attr->getInterrupt()) { 6214 case ARMInterruptAttr::Generic: Kind = ""; break; 6215 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; 6216 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; 6217 case ARMInterruptAttr::SWI: Kind = "SWI"; break; 6218 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; 6219 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; 6220 } 6221 6222 llvm::Function *Fn = cast<llvm::Function>(GV); 6223 6224 Fn->addFnAttr("interrupt", Kind); 6225 6226 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); 6227 if (ABI == ARMABIInfo::APCS) 6228 return; 6229 6230 // AAPCS guarantees that sp will be 8-byte aligned on any public interface, 6231 // however this is not necessarily true on taking any interrupt. Instruct 6232 // the backend to perform a realignment as part of the function prologue. 6233 llvm::AttrBuilder B; 6234 B.addStackAlignmentAttr(8); 6235 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 6236 } 6237 }; 6238 6239 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { 6240 public: 6241 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 6242 : ARMTargetCodeGenInfo(CGT, K) {} 6243 6244 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6245 CodeGen::CodeGenModule &CGM) const override; 6246 6247 void getDependentLibraryOption(llvm::StringRef Lib, 6248 llvm::SmallString<24> &Opt) const override { 6249 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); 6250 } 6251 6252 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, 6253 llvm::SmallString<32> &Opt) const override { 6254 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 6255 } 6256 }; 6257 6258 void WindowsARMTargetCodeGenInfo::setTargetAttributes( 6259 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 6260 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 6261 if (GV->isDeclaration()) 6262 return; 6263 addStackProbeTargetAttributes(D, GV, CGM); 6264 } 6265 } 6266 6267 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 6268 if (!::classifyReturnType(getCXXABI(), FI, *this)) 6269 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(), 6270 FI.getCallingConvention()); 6271 6272 for (auto &I : FI.arguments()) 6273 I.info = classifyArgumentType(I.type, FI.isVariadic(), 6274 FI.getCallingConvention()); 6275 6276 6277 // Always honor user-specified calling convention. 6278 if (FI.getCallingConvention() != llvm::CallingConv::C) 6279 return; 6280 6281 llvm::CallingConv::ID cc = getRuntimeCC(); 6282 if (cc != llvm::CallingConv::C) 6283 FI.setEffectiveCallingConvention(cc); 6284 } 6285 6286 /// Return the default calling convention that LLVM will use. 6287 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { 6288 // The default calling convention that LLVM will infer. 6289 if (isEABIHF() || getTarget().getTriple().isWatchABI()) 6290 return llvm::CallingConv::ARM_AAPCS_VFP; 6291 else if (isEABI()) 6292 return llvm::CallingConv::ARM_AAPCS; 6293 else 6294 return llvm::CallingConv::ARM_APCS; 6295 } 6296 6297 /// Return the calling convention that our ABI would like us to use 6298 /// as the C calling convention. 6299 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { 6300 switch (getABIKind()) { 6301 case APCS: return llvm::CallingConv::ARM_APCS; 6302 case AAPCS: return llvm::CallingConv::ARM_AAPCS; 6303 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 6304 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 6305 } 6306 llvm_unreachable("bad ABI kind"); 6307 } 6308 6309 void ARMABIInfo::setCCs() { 6310 assert(getRuntimeCC() == llvm::CallingConv::C); 6311 6312 // Don't muddy up the IR with a ton of explicit annotations if 6313 // they'd just match what LLVM will infer from the triple. 6314 llvm::CallingConv::ID abiCC = getABIDefaultCC(); 6315 if (abiCC != getLLVMDefaultCC()) 6316 RuntimeCC = abiCC; 6317 } 6318 6319 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { 6320 uint64_t Size = getContext().getTypeSize(Ty); 6321 if (Size <= 32) { 6322 llvm::Type *ResType = 6323 llvm::Type::getInt32Ty(getVMContext()); 6324 return ABIArgInfo::getDirect(ResType); 6325 } 6326 if (Size == 64 || Size == 128) { 6327 auto *ResType = llvm::FixedVectorType::get( 6328 llvm::Type::getInt32Ty(getVMContext()), Size / 32); 6329 return ABIArgInfo::getDirect(ResType); 6330 } 6331 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 6332 } 6333 6334 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, 6335 const Type *Base, 6336 uint64_t Members) const { 6337 assert(Base && "Base class should be set for homogeneous aggregate"); 6338 // Base can be a floating-point or a vector. 6339 if (const VectorType *VT = Base->getAs<VectorType>()) { 6340 // FP16 vectors should be converted to integer vectors 6341 if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) { 6342 uint64_t Size = getContext().getTypeSize(VT); 6343 auto *NewVecTy = llvm::FixedVectorType::get( 6344 llvm::Type::getInt32Ty(getVMContext()), Size / 32); 6345 llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); 6346 return ABIArgInfo::getDirect(Ty, 0, nullptr, false); 6347 } 6348 } 6349 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); 6350 } 6351 6352 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, 6353 unsigned functionCallConv) const { 6354 // 6.1.2.1 The following argument types are VFP CPRCs: 6355 // A single-precision floating-point type (including promoted 6356 // half-precision types); A double-precision floating-point type; 6357 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate 6358 // with a Base Type of a single- or double-precision floating-point type, 6359 // 64-bit containerized vectors or 128-bit containerized vectors with one 6360 // to four Elements. 6361 // Variadic functions should always marshal to the base standard. 6362 bool IsAAPCS_VFP = 6363 !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false); 6364 6365 Ty = useFirstFieldIfTransparentUnion(Ty); 6366 6367 // Handle illegal vector types here. 6368 if (isIllegalVectorType(Ty)) 6369 return coerceIllegalVector(Ty); 6370 6371 if (!isAggregateTypeForABI(Ty)) { 6372 // Treat an enum type as its underlying type. 6373 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 6374 Ty = EnumTy->getDecl()->getIntegerType(); 6375 } 6376 6377 if (const auto *EIT = Ty->getAs<ExtIntType>()) 6378 if (EIT->getNumBits() > 64) 6379 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 6380 6381 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 6382 : ABIArgInfo::getDirect()); 6383 } 6384 6385 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 6386 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 6387 } 6388 6389 // Ignore empty records. 6390 if (isEmptyRecord(getContext(), Ty, true)) 6391 return ABIArgInfo::getIgnore(); 6392 6393 if (IsAAPCS_VFP) { 6394 // Homogeneous Aggregates need to be expanded when we can fit the aggregate 6395 // into VFP registers. 6396 const Type *Base = nullptr; 6397 uint64_t Members = 0; 6398 if (isHomogeneousAggregate(Ty, Base, Members)) 6399 return classifyHomogeneousAggregate(Ty, Base, Members); 6400 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { 6401 // WatchOS does have homogeneous aggregates. Note that we intentionally use 6402 // this convention even for a variadic function: the backend will use GPRs 6403 // if needed. 6404 const Type *Base = nullptr; 6405 uint64_t Members = 0; 6406 if (isHomogeneousAggregate(Ty, Base, Members)) { 6407 assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); 6408 llvm::Type *Ty = 6409 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); 6410 return ABIArgInfo::getDirect(Ty, 0, nullptr, false); 6411 } 6412 } 6413 6414 if (getABIKind() == ARMABIInfo::AAPCS16_VFP && 6415 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { 6416 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're 6417 // bigger than 128-bits, they get placed in space allocated by the caller, 6418 // and a pointer is passed. 6419 return ABIArgInfo::getIndirect( 6420 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); 6421 } 6422 6423 // Support byval for ARM. 6424 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at 6425 // most 8-byte. We realign the indirect argument if type alignment is bigger 6426 // than ABI alignment. 6427 uint64_t ABIAlign = 4; 6428 uint64_t TyAlign; 6429 if (getABIKind() == ARMABIInfo::AAPCS_VFP || 6430 getABIKind() == ARMABIInfo::AAPCS) { 6431 TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); 6432 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); 6433 } else { 6434 TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); 6435 } 6436 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { 6437 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); 6438 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), 6439 /*ByVal=*/true, 6440 /*Realign=*/TyAlign > ABIAlign); 6441 } 6442 6443 // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of 6444 // same size and alignment. 6445 if (getTarget().isRenderScriptTarget()) { 6446 return coerceToIntArray(Ty, getContext(), getVMContext()); 6447 } 6448 6449 // Otherwise, pass by coercing to a structure of the appropriate size. 6450 llvm::Type* ElemTy; 6451 unsigned SizeRegs; 6452 // FIXME: Try to match the types of the arguments more accurately where 6453 // we can. 6454 if (TyAlign <= 4) { 6455 ElemTy = llvm::Type::getInt32Ty(getVMContext()); 6456 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; 6457 } else { 6458 ElemTy = llvm::Type::getInt64Ty(getVMContext()); 6459 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; 6460 } 6461 6462 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); 6463 } 6464 6465 static bool isIntegerLikeType(QualType Ty, ASTContext &Context, 6466 llvm::LLVMContext &VMContext) { 6467 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure 6468 // is called integer-like if its size is less than or equal to one word, and 6469 // the offset of each of its addressable sub-fields is zero. 6470 6471 uint64_t Size = Context.getTypeSize(Ty); 6472 6473 // Check that the type fits in a word. 6474 if (Size > 32) 6475 return false; 6476 6477 // FIXME: Handle vector types! 6478 if (Ty->isVectorType()) 6479 return false; 6480 6481 // Float types are never treated as "integer like". 6482 if (Ty->isRealFloatingType()) 6483 return false; 6484 6485 // If this is a builtin or pointer type then it is ok. 6486 if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) 6487 return true; 6488 6489 // Small complex integer types are "integer like". 6490 if (const ComplexType *CT = Ty->getAs<ComplexType>()) 6491 return isIntegerLikeType(CT->getElementType(), Context, VMContext); 6492 6493 // Single element and zero sized arrays should be allowed, by the definition 6494 // above, but they are not. 6495 6496 // Otherwise, it must be a record type. 6497 const RecordType *RT = Ty->getAs<RecordType>(); 6498 if (!RT) return false; 6499 6500 // Ignore records with flexible arrays. 6501 const RecordDecl *RD = RT->getDecl(); 6502 if (RD->hasFlexibleArrayMember()) 6503 return false; 6504 6505 // Check that all sub-fields are at offset 0, and are themselves "integer 6506 // like". 6507 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 6508 6509 bool HadField = false; 6510 unsigned idx = 0; 6511 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 6512 i != e; ++i, ++idx) { 6513 const FieldDecl *FD = *i; 6514 6515 // Bit-fields are not addressable, we only need to verify they are "integer 6516 // like". We still have to disallow a subsequent non-bitfield, for example: 6517 // struct { int : 0; int x } 6518 // is non-integer like according to gcc. 6519 if (FD->isBitField()) { 6520 if (!RD->isUnion()) 6521 HadField = true; 6522 6523 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 6524 return false; 6525 6526 continue; 6527 } 6528 6529 // Check if this field is at offset 0. 6530 if (Layout.getFieldOffset(idx) != 0) 6531 return false; 6532 6533 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 6534 return false; 6535 6536 // Only allow at most one field in a structure. This doesn't match the 6537 // wording above, but follows gcc in situations with a field following an 6538 // empty structure. 6539 if (!RD->isUnion()) { 6540 if (HadField) 6541 return false; 6542 6543 HadField = true; 6544 } 6545 } 6546 6547 return true; 6548 } 6549 6550 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic, 6551 unsigned functionCallConv) const { 6552 6553 // Variadic functions should always marshal to the base standard. 6554 bool IsAAPCS_VFP = 6555 !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true); 6556 6557 if (RetTy->isVoidType()) 6558 return ABIArgInfo::getIgnore(); 6559 6560 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 6561 // Large vector types should be returned via memory. 6562 if (getContext().getTypeSize(RetTy) > 128) 6563 return getNaturalAlignIndirect(RetTy); 6564 // TODO: FP16/BF16 vectors should be converted to integer vectors 6565 // This check is similar to isIllegalVectorType - refactor? 6566 if ((!getTarget().hasLegalHalfType() && 6567 (VT->getElementType()->isFloat16Type() || 6568 VT->getElementType()->isHalfType())) || 6569 (IsFloatABISoftFP && 6570 VT->getElementType()->isBFloat16Type())) 6571 return coerceIllegalVector(RetTy); 6572 } 6573 6574 if (!isAggregateTypeForABI(RetTy)) { 6575 // Treat an enum type as its underlying type. 6576 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 6577 RetTy = EnumTy->getDecl()->getIntegerType(); 6578 6579 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 6580 if (EIT->getNumBits() > 64) 6581 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 6582 6583 return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 6584 : ABIArgInfo::getDirect(); 6585 } 6586 6587 // Are we following APCS? 6588 if (getABIKind() == APCS) { 6589 if (isEmptyRecord(getContext(), RetTy, false)) 6590 return ABIArgInfo::getIgnore(); 6591 6592 // Complex types are all returned as packed integers. 6593 // 6594 // FIXME: Consider using 2 x vector types if the back end handles them 6595 // correctly. 6596 if (RetTy->isAnyComplexType()) 6597 return ABIArgInfo::getDirect(llvm::IntegerType::get( 6598 getVMContext(), getContext().getTypeSize(RetTy))); 6599 6600 // Integer like structures are returned in r0. 6601 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { 6602 // Return in the smallest viable integer type. 6603 uint64_t Size = getContext().getTypeSize(RetTy); 6604 if (Size <= 8) 6605 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 6606 if (Size <= 16) 6607 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 6608 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6609 } 6610 6611 // Otherwise return in memory. 6612 return getNaturalAlignIndirect(RetTy); 6613 } 6614 6615 // Otherwise this is an AAPCS variant. 6616 6617 if (isEmptyRecord(getContext(), RetTy, true)) 6618 return ABIArgInfo::getIgnore(); 6619 6620 // Check for homogeneous aggregates with AAPCS-VFP. 6621 if (IsAAPCS_VFP) { 6622 const Type *Base = nullptr; 6623 uint64_t Members = 0; 6624 if (isHomogeneousAggregate(RetTy, Base, Members)) 6625 return classifyHomogeneousAggregate(RetTy, Base, Members); 6626 } 6627 6628 // Aggregates <= 4 bytes are returned in r0; other aggregates 6629 // are returned indirectly. 6630 uint64_t Size = getContext().getTypeSize(RetTy); 6631 if (Size <= 32) { 6632 // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of 6633 // same size and alignment. 6634 if (getTarget().isRenderScriptTarget()) { 6635 return coerceToIntArray(RetTy, getContext(), getVMContext()); 6636 } 6637 if (getDataLayout().isBigEndian()) 6638 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) 6639 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6640 6641 // Return in the smallest viable integer type. 6642 if (Size <= 8) 6643 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 6644 if (Size <= 16) 6645 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 6646 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6647 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { 6648 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); 6649 llvm::Type *CoerceTy = 6650 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); 6651 return ABIArgInfo::getDirect(CoerceTy); 6652 } 6653 6654 return getNaturalAlignIndirect(RetTy); 6655 } 6656 6657 /// isIllegalVector - check whether Ty is an illegal vector type. 6658 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { 6659 if (const VectorType *VT = Ty->getAs<VectorType> ()) { 6660 // On targets that don't support half, fp16 or bfloat, they are expanded 6661 // into float, and we don't want the ABI to depend on whether or not they 6662 // are supported in hardware. Thus return false to coerce vectors of these 6663 // types into integer vectors. 6664 // We do not depend on hasLegalHalfType for bfloat as it is a 6665 // separate IR type. 6666 if ((!getTarget().hasLegalHalfType() && 6667 (VT->getElementType()->isFloat16Type() || 6668 VT->getElementType()->isHalfType())) || 6669 (IsFloatABISoftFP && 6670 VT->getElementType()->isBFloat16Type())) 6671 return true; 6672 if (isAndroid()) { 6673 // Android shipped using Clang 3.1, which supported a slightly different 6674 // vector ABI. The primary differences were that 3-element vector types 6675 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path 6676 // accepts that legacy behavior for Android only. 6677 // Check whether VT is legal. 6678 unsigned NumElements = VT->getNumElements(); 6679 // NumElements should be power of 2 or equal to 3. 6680 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) 6681 return true; 6682 } else { 6683 // Check whether VT is legal. 6684 unsigned NumElements = VT->getNumElements(); 6685 uint64_t Size = getContext().getTypeSize(VT); 6686 // NumElements should be power of 2. 6687 if (!llvm::isPowerOf2_32(NumElements)) 6688 return true; 6689 // Size should be greater than 32 bits. 6690 return Size <= 32; 6691 } 6692 } 6693 return false; 6694 } 6695 6696 /// Return true if a type contains any 16-bit floating point vectors 6697 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const { 6698 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 6699 uint64_t NElements = AT->getSize().getZExtValue(); 6700 if (NElements == 0) 6701 return false; 6702 return containsAnyFP16Vectors(AT->getElementType()); 6703 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 6704 const RecordDecl *RD = RT->getDecl(); 6705 6706 // If this is a C++ record, check the bases first. 6707 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 6708 if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) { 6709 return containsAnyFP16Vectors(B.getType()); 6710 })) 6711 return true; 6712 6713 if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) { 6714 return FD && containsAnyFP16Vectors(FD->getType()); 6715 })) 6716 return true; 6717 6718 return false; 6719 } else { 6720 if (const VectorType *VT = Ty->getAs<VectorType>()) 6721 return (VT->getElementType()->isFloat16Type() || 6722 VT->getElementType()->isBFloat16Type() || 6723 VT->getElementType()->isHalfType()); 6724 return false; 6725 } 6726 } 6727 6728 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, 6729 llvm::Type *eltTy, 6730 unsigned numElts) const { 6731 if (!llvm::isPowerOf2_32(numElts)) 6732 return false; 6733 unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); 6734 if (size > 64) 6735 return false; 6736 if (vectorSize.getQuantity() != 8 && 6737 (vectorSize.getQuantity() != 16 || numElts == 1)) 6738 return false; 6739 return true; 6740 } 6741 6742 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 6743 // Homogeneous aggregates for AAPCS-VFP must have base types of float, 6744 // double, or 64-bit or 128-bit vectors. 6745 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 6746 if (BT->getKind() == BuiltinType::Float || 6747 BT->getKind() == BuiltinType::Double || 6748 BT->getKind() == BuiltinType::LongDouble) 6749 return true; 6750 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 6751 unsigned VecSize = getContext().getTypeSize(VT); 6752 if (VecSize == 64 || VecSize == 128) 6753 return true; 6754 } 6755 return false; 6756 } 6757 6758 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 6759 uint64_t Members) const { 6760 return Members <= 4; 6761 } 6762 6763 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention, 6764 bool acceptHalf) const { 6765 // Give precedence to user-specified calling conventions. 6766 if (callConvention != llvm::CallingConv::C) 6767 return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP); 6768 else 6769 return (getABIKind() == AAPCS_VFP) || 6770 (acceptHalf && (getABIKind() == AAPCS16_VFP)); 6771 } 6772 6773 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6774 QualType Ty) const { 6775 CharUnits SlotSize = CharUnits::fromQuantity(4); 6776 6777 // Empty records are ignored for parameter passing purposes. 6778 if (isEmptyRecord(getContext(), Ty, true)) { 6779 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); 6780 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 6781 return Addr; 6782 } 6783 6784 CharUnits TySize = getContext().getTypeSizeInChars(Ty); 6785 CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty); 6786 6787 // Use indirect if size of the illegal vector is bigger than 16 bytes. 6788 bool IsIndirect = false; 6789 const Type *Base = nullptr; 6790 uint64_t Members = 0; 6791 if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { 6792 IsIndirect = true; 6793 6794 // ARMv7k passes structs bigger than 16 bytes indirectly, in space 6795 // allocated by the caller. 6796 } else if (TySize > CharUnits::fromQuantity(16) && 6797 getABIKind() == ARMABIInfo::AAPCS16_VFP && 6798 !isHomogeneousAggregate(Ty, Base, Members)) { 6799 IsIndirect = true; 6800 6801 // Otherwise, bound the type's ABI alignment. 6802 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for 6803 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. 6804 // Our callers should be prepared to handle an under-aligned address. 6805 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || 6806 getABIKind() == ARMABIInfo::AAPCS) { 6807 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); 6808 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); 6809 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { 6810 // ARMv7k allows type alignment up to 16 bytes. 6811 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); 6812 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); 6813 } else { 6814 TyAlignForABI = CharUnits::fromQuantity(4); 6815 } 6816 6817 std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI }; 6818 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, 6819 SlotSize, /*AllowHigherAlign*/ true); 6820 } 6821 6822 //===----------------------------------------------------------------------===// 6823 // NVPTX ABI Implementation 6824 //===----------------------------------------------------------------------===// 6825 6826 namespace { 6827 6828 class NVPTXTargetCodeGenInfo; 6829 6830 class NVPTXABIInfo : public ABIInfo { 6831 NVPTXTargetCodeGenInfo &CGInfo; 6832 6833 public: 6834 NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info) 6835 : ABIInfo(CGT), CGInfo(Info) {} 6836 6837 ABIArgInfo classifyReturnType(QualType RetTy) const; 6838 ABIArgInfo classifyArgumentType(QualType Ty) const; 6839 6840 void computeInfo(CGFunctionInfo &FI) const override; 6841 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6842 QualType Ty) const override; 6843 bool isUnsupportedType(QualType T) const; 6844 ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const; 6845 }; 6846 6847 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { 6848 public: 6849 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) 6850 : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {} 6851 6852 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6853 CodeGen::CodeGenModule &M) const override; 6854 bool shouldEmitStaticExternCAliases() const override; 6855 6856 llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override { 6857 // On the device side, surface reference is represented as an object handle 6858 // in 64-bit integer. 6859 return llvm::Type::getInt64Ty(getABIInfo().getVMContext()); 6860 } 6861 6862 llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override { 6863 // On the device side, texture reference is represented as an object handle 6864 // in 64-bit integer. 6865 return llvm::Type::getInt64Ty(getABIInfo().getVMContext()); 6866 } 6867 6868 bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst, 6869 LValue Src) const override { 6870 emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src); 6871 return true; 6872 } 6873 6874 bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst, 6875 LValue Src) const override { 6876 emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src); 6877 return true; 6878 } 6879 6880 private: 6881 // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the 6882 // resulting MDNode to the nvvm.annotations MDNode. 6883 static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name, 6884 int Operand); 6885 6886 static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst, 6887 LValue Src) { 6888 llvm::Value *Handle = nullptr; 6889 llvm::Constant *C = 6890 llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer()); 6891 // Lookup `addrspacecast` through the constant pointer if any. 6892 if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C)) 6893 C = llvm::cast<llvm::Constant>(ASC->getPointerOperand()); 6894 if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) { 6895 // Load the handle from the specific global variable using 6896 // `nvvm.texsurf.handle.internal` intrinsic. 6897 Handle = CGF.EmitRuntimeCall( 6898 CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal, 6899 {GV->getType()}), 6900 {GV}, "texsurf_handle"); 6901 } else 6902 Handle = CGF.EmitLoadOfScalar(Src, SourceLocation()); 6903 CGF.EmitStoreOfScalar(Handle, Dst); 6904 } 6905 }; 6906 6907 /// Checks if the type is unsupported directly by the current target. 6908 bool NVPTXABIInfo::isUnsupportedType(QualType T) const { 6909 ASTContext &Context = getContext(); 6910 if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type()) 6911 return true; 6912 if (!Context.getTargetInfo().hasFloat128Type() && 6913 (T->isFloat128Type() || 6914 (T->isRealFloatingType() && Context.getTypeSize(T) == 128))) 6915 return true; 6916 if (const auto *EIT = T->getAs<ExtIntType>()) 6917 return EIT->getNumBits() > 6918 (Context.getTargetInfo().hasInt128Type() ? 128U : 64U); 6919 if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() && 6920 Context.getTypeSize(T) > 64U) 6921 return true; 6922 if (const auto *AT = T->getAsArrayTypeUnsafe()) 6923 return isUnsupportedType(AT->getElementType()); 6924 const auto *RT = T->getAs<RecordType>(); 6925 if (!RT) 6926 return false; 6927 const RecordDecl *RD = RT->getDecl(); 6928 6929 // If this is a C++ record, check the bases first. 6930 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 6931 for (const CXXBaseSpecifier &I : CXXRD->bases()) 6932 if (isUnsupportedType(I.getType())) 6933 return true; 6934 6935 for (const FieldDecl *I : RD->fields()) 6936 if (isUnsupportedType(I->getType())) 6937 return true; 6938 return false; 6939 } 6940 6941 /// Coerce the given type into an array with maximum allowed size of elements. 6942 ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty, 6943 unsigned MaxSize) const { 6944 // Alignment and Size are measured in bits. 6945 const uint64_t Size = getContext().getTypeSize(Ty); 6946 const uint64_t Alignment = getContext().getTypeAlign(Ty); 6947 const unsigned Div = std::min<unsigned>(MaxSize, Alignment); 6948 llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div); 6949 const uint64_t NumElements = (Size + Div - 1) / Div; 6950 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); 6951 } 6952 6953 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { 6954 if (RetTy->isVoidType()) 6955 return ABIArgInfo::getIgnore(); 6956 6957 if (getContext().getLangOpts().OpenMP && 6958 getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy)) 6959 return coerceToIntArrayWithLimit(RetTy, 64); 6960 6961 // note: this is different from default ABI 6962 if (!RetTy->isScalarType()) 6963 return ABIArgInfo::getDirect(); 6964 6965 // Treat an enum type as its underlying type. 6966 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 6967 RetTy = EnumTy->getDecl()->getIntegerType(); 6968 6969 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 6970 : ABIArgInfo::getDirect()); 6971 } 6972 6973 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { 6974 // Treat an enum type as its underlying type. 6975 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 6976 Ty = EnumTy->getDecl()->getIntegerType(); 6977 6978 // Return aggregates type as indirect by value 6979 if (isAggregateTypeForABI(Ty)) { 6980 // Under CUDA device compilation, tex/surf builtin types are replaced with 6981 // object types and passed directly. 6982 if (getContext().getLangOpts().CUDAIsDevice) { 6983 if (Ty->isCUDADeviceBuiltinSurfaceType()) 6984 return ABIArgInfo::getDirect( 6985 CGInfo.getCUDADeviceBuiltinSurfaceDeviceType()); 6986 if (Ty->isCUDADeviceBuiltinTextureType()) 6987 return ABIArgInfo::getDirect( 6988 CGInfo.getCUDADeviceBuiltinTextureDeviceType()); 6989 } 6990 return getNaturalAlignIndirect(Ty, /* byval */ true); 6991 } 6992 6993 if (const auto *EIT = Ty->getAs<ExtIntType>()) { 6994 if ((EIT->getNumBits() > 128) || 6995 (!getContext().getTargetInfo().hasInt128Type() && 6996 EIT->getNumBits() > 64)) 6997 return getNaturalAlignIndirect(Ty, /* byval */ true); 6998 } 6999 7000 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 7001 : ABIArgInfo::getDirect()); 7002 } 7003 7004 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { 7005 if (!getCXXABI().classifyReturnType(FI)) 7006 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 7007 for (auto &I : FI.arguments()) 7008 I.info = classifyArgumentType(I.type); 7009 7010 // Always honor user-specified calling convention. 7011 if (FI.getCallingConvention() != llvm::CallingConv::C) 7012 return; 7013 7014 FI.setEffectiveCallingConvention(getRuntimeCC()); 7015 } 7016 7017 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7018 QualType Ty) const { 7019 llvm_unreachable("NVPTX does not support varargs"); 7020 } 7021 7022 void NVPTXTargetCodeGenInfo::setTargetAttributes( 7023 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 7024 if (GV->isDeclaration()) 7025 return; 7026 const VarDecl *VD = dyn_cast_or_null<VarDecl>(D); 7027 if (VD) { 7028 if (M.getLangOpts().CUDA) { 7029 if (VD->getType()->isCUDADeviceBuiltinSurfaceType()) 7030 addNVVMMetadata(GV, "surface", 1); 7031 else if (VD->getType()->isCUDADeviceBuiltinTextureType()) 7032 addNVVMMetadata(GV, "texture", 1); 7033 return; 7034 } 7035 } 7036 7037 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 7038 if (!FD) return; 7039 7040 llvm::Function *F = cast<llvm::Function>(GV); 7041 7042 // Perform special handling in OpenCL mode 7043 if (M.getLangOpts().OpenCL) { 7044 // Use OpenCL function attributes to check for kernel functions 7045 // By default, all functions are device functions 7046 if (FD->hasAttr<OpenCLKernelAttr>()) { 7047 // OpenCL __kernel functions get kernel metadata 7048 // Create !{<func-ref>, metadata !"kernel", i32 1} node 7049 addNVVMMetadata(F, "kernel", 1); 7050 // And kernel functions are not subject to inlining 7051 F->addFnAttr(llvm::Attribute::NoInline); 7052 } 7053 } 7054 7055 // Perform special handling in CUDA mode. 7056 if (M.getLangOpts().CUDA) { 7057 // CUDA __global__ functions get a kernel metadata entry. Since 7058 // __global__ functions cannot be called from the device, we do not 7059 // need to set the noinline attribute. 7060 if (FD->hasAttr<CUDAGlobalAttr>()) { 7061 // Create !{<func-ref>, metadata !"kernel", i32 1} node 7062 addNVVMMetadata(F, "kernel", 1); 7063 } 7064 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { 7065 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node 7066 llvm::APSInt MaxThreads(32); 7067 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); 7068 if (MaxThreads > 0) 7069 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); 7070 7071 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was 7072 // not specified in __launch_bounds__ or if the user specified a 0 value, 7073 // we don't have to add a PTX directive. 7074 if (Attr->getMinBlocks()) { 7075 llvm::APSInt MinBlocks(32); 7076 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); 7077 if (MinBlocks > 0) 7078 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node 7079 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); 7080 } 7081 } 7082 } 7083 } 7084 7085 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV, 7086 StringRef Name, int Operand) { 7087 llvm::Module *M = GV->getParent(); 7088 llvm::LLVMContext &Ctx = M->getContext(); 7089 7090 // Get "nvvm.annotations" metadata node 7091 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); 7092 7093 llvm::Metadata *MDVals[] = { 7094 llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name), 7095 llvm::ConstantAsMetadata::get( 7096 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; 7097 // Append metadata to nvvm.annotations 7098 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 7099 } 7100 7101 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { 7102 return false; 7103 } 7104 } 7105 7106 //===----------------------------------------------------------------------===// 7107 // SystemZ ABI Implementation 7108 //===----------------------------------------------------------------------===// 7109 7110 namespace { 7111 7112 class SystemZABIInfo : public SwiftABIInfo { 7113 bool HasVector; 7114 bool IsSoftFloatABI; 7115 7116 public: 7117 SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF) 7118 : SwiftABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {} 7119 7120 bool isPromotableIntegerTypeForABI(QualType Ty) const; 7121 bool isCompoundType(QualType Ty) const; 7122 bool isVectorArgumentType(QualType Ty) const; 7123 bool isFPArgumentType(QualType Ty) const; 7124 QualType GetSingleElementType(QualType Ty) const; 7125 7126 ABIArgInfo classifyReturnType(QualType RetTy) const; 7127 ABIArgInfo classifyArgumentType(QualType ArgTy) const; 7128 7129 void computeInfo(CGFunctionInfo &FI) const override { 7130 if (!getCXXABI().classifyReturnType(FI)) 7131 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 7132 for (auto &I : FI.arguments()) 7133 I.info = classifyArgumentType(I.type); 7134 } 7135 7136 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7137 QualType Ty) const override; 7138 7139 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 7140 bool asReturnValue) const override { 7141 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 7142 } 7143 bool isSwiftErrorInRegister() const override { 7144 return false; 7145 } 7146 }; 7147 7148 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { 7149 public: 7150 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI) 7151 : TargetCodeGenInfo( 7152 std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)) {} 7153 }; 7154 7155 } 7156 7157 bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const { 7158 // Treat an enum type as its underlying type. 7159 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 7160 Ty = EnumTy->getDecl()->getIntegerType(); 7161 7162 // Promotable integer types are required to be promoted by the ABI. 7163 if (ABIInfo::isPromotableIntegerTypeForABI(Ty)) 7164 return true; 7165 7166 if (const auto *EIT = Ty->getAs<ExtIntType>()) 7167 if (EIT->getNumBits() < 64) 7168 return true; 7169 7170 // 32-bit values must also be promoted. 7171 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 7172 switch (BT->getKind()) { 7173 case BuiltinType::Int: 7174 case BuiltinType::UInt: 7175 return true; 7176 default: 7177 return false; 7178 } 7179 return false; 7180 } 7181 7182 bool SystemZABIInfo::isCompoundType(QualType Ty) const { 7183 return (Ty->isAnyComplexType() || 7184 Ty->isVectorType() || 7185 isAggregateTypeForABI(Ty)); 7186 } 7187 7188 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { 7189 return (HasVector && 7190 Ty->isVectorType() && 7191 getContext().getTypeSize(Ty) <= 128); 7192 } 7193 7194 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { 7195 if (IsSoftFloatABI) 7196 return false; 7197 7198 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 7199 switch (BT->getKind()) { 7200 case BuiltinType::Float: 7201 case BuiltinType::Double: 7202 return true; 7203 default: 7204 return false; 7205 } 7206 7207 return false; 7208 } 7209 7210 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { 7211 if (const RecordType *RT = Ty->getAsStructureType()) { 7212 const RecordDecl *RD = RT->getDecl(); 7213 QualType Found; 7214 7215 // If this is a C++ record, check the bases first. 7216 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 7217 for (const auto &I : CXXRD->bases()) { 7218 QualType Base = I.getType(); 7219 7220 // Empty bases don't affect things either way. 7221 if (isEmptyRecord(getContext(), Base, true)) 7222 continue; 7223 7224 if (!Found.isNull()) 7225 return Ty; 7226 Found = GetSingleElementType(Base); 7227 } 7228 7229 // Check the fields. 7230 for (const auto *FD : RD->fields()) { 7231 // For compatibility with GCC, ignore empty bitfields in C++ mode. 7232 // Unlike isSingleElementStruct(), empty structure and array fields 7233 // do count. So do anonymous bitfields that aren't zero-sized. 7234 if (getContext().getLangOpts().CPlusPlus && 7235 FD->isZeroLengthBitField(getContext())) 7236 continue; 7237 7238 // Unlike isSingleElementStruct(), arrays do not count. 7239 // Nested structures still do though. 7240 if (!Found.isNull()) 7241 return Ty; 7242 Found = GetSingleElementType(FD->getType()); 7243 } 7244 7245 // Unlike isSingleElementStruct(), trailing padding is allowed. 7246 // An 8-byte aligned struct s { float f; } is passed as a double. 7247 if (!Found.isNull()) 7248 return Found; 7249 } 7250 7251 return Ty; 7252 } 7253 7254 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7255 QualType Ty) const { 7256 // Assume that va_list type is correct; should be pointer to LLVM type: 7257 // struct { 7258 // i64 __gpr; 7259 // i64 __fpr; 7260 // i8 *__overflow_arg_area; 7261 // i8 *__reg_save_area; 7262 // }; 7263 7264 // Every non-vector argument occupies 8 bytes and is passed by preference 7265 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are 7266 // always passed on the stack. 7267 Ty = getContext().getCanonicalType(Ty); 7268 auto TyInfo = getContext().getTypeInfoInChars(Ty); 7269 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); 7270 llvm::Type *DirectTy = ArgTy; 7271 ABIArgInfo AI = classifyArgumentType(Ty); 7272 bool IsIndirect = AI.isIndirect(); 7273 bool InFPRs = false; 7274 bool IsVector = false; 7275 CharUnits UnpaddedSize; 7276 CharUnits DirectAlign; 7277 if (IsIndirect) { 7278 DirectTy = llvm::PointerType::getUnqual(DirectTy); 7279 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); 7280 } else { 7281 if (AI.getCoerceToType()) 7282 ArgTy = AI.getCoerceToType(); 7283 InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy())); 7284 IsVector = ArgTy->isVectorTy(); 7285 UnpaddedSize = TyInfo.first; 7286 DirectAlign = TyInfo.second; 7287 } 7288 CharUnits PaddedSize = CharUnits::fromQuantity(8); 7289 if (IsVector && UnpaddedSize > PaddedSize) 7290 PaddedSize = CharUnits::fromQuantity(16); 7291 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); 7292 7293 CharUnits Padding = (PaddedSize - UnpaddedSize); 7294 7295 llvm::Type *IndexTy = CGF.Int64Ty; 7296 llvm::Value *PaddedSizeV = 7297 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); 7298 7299 if (IsVector) { 7300 // Work out the address of a vector argument on the stack. 7301 // Vector arguments are always passed in the high bits of a 7302 // single (8 byte) or double (16 byte) stack slot. 7303 Address OverflowArgAreaPtr = 7304 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); 7305 Address OverflowArgArea = 7306 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), 7307 TyInfo.second); 7308 Address MemAddr = 7309 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); 7310 7311 // Update overflow_arg_area_ptr pointer 7312 llvm::Value *NewOverflowArgArea = 7313 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, 7314 "overflow_arg_area"); 7315 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 7316 7317 return MemAddr; 7318 } 7319 7320 assert(PaddedSize.getQuantity() == 8); 7321 7322 unsigned MaxRegs, RegCountField, RegSaveIndex; 7323 CharUnits RegPadding; 7324 if (InFPRs) { 7325 MaxRegs = 4; // Maximum of 4 FPR arguments 7326 RegCountField = 1; // __fpr 7327 RegSaveIndex = 16; // save offset for f0 7328 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR 7329 } else { 7330 MaxRegs = 5; // Maximum of 5 GPR arguments 7331 RegCountField = 0; // __gpr 7332 RegSaveIndex = 2; // save offset for r2 7333 RegPadding = Padding; // values are passed in the low bits of a GPR 7334 } 7335 7336 Address RegCountPtr = 7337 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); 7338 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); 7339 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); 7340 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, 7341 "fits_in_regs"); 7342 7343 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 7344 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 7345 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 7346 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 7347 7348 // Emit code to load the value if it was passed in registers. 7349 CGF.EmitBlock(InRegBlock); 7350 7351 // Work out the address of an argument register. 7352 llvm::Value *ScaledRegCount = 7353 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); 7354 llvm::Value *RegBase = 7355 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() 7356 + RegPadding.getQuantity()); 7357 llvm::Value *RegOffset = 7358 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); 7359 Address RegSaveAreaPtr = 7360 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); 7361 llvm::Value *RegSaveArea = 7362 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); 7363 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, 7364 "raw_reg_addr"), 7365 PaddedSize); 7366 Address RegAddr = 7367 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); 7368 7369 // Update the register count 7370 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); 7371 llvm::Value *NewRegCount = 7372 CGF.Builder.CreateAdd(RegCount, One, "reg_count"); 7373 CGF.Builder.CreateStore(NewRegCount, RegCountPtr); 7374 CGF.EmitBranch(ContBlock); 7375 7376 // Emit code to load the value if it was passed in memory. 7377 CGF.EmitBlock(InMemBlock); 7378 7379 // Work out the address of a stack argument. 7380 Address OverflowArgAreaPtr = 7381 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); 7382 Address OverflowArgArea = 7383 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), 7384 PaddedSize); 7385 Address RawMemAddr = 7386 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); 7387 Address MemAddr = 7388 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); 7389 7390 // Update overflow_arg_area_ptr pointer 7391 llvm::Value *NewOverflowArgArea = 7392 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, 7393 "overflow_arg_area"); 7394 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 7395 CGF.EmitBranch(ContBlock); 7396 7397 // Return the appropriate result. 7398 CGF.EmitBlock(ContBlock); 7399 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, 7400 MemAddr, InMemBlock, "va_arg.addr"); 7401 7402 if (IsIndirect) 7403 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), 7404 TyInfo.second); 7405 7406 return ResAddr; 7407 } 7408 7409 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { 7410 if (RetTy->isVoidType()) 7411 return ABIArgInfo::getIgnore(); 7412 if (isVectorArgumentType(RetTy)) 7413 return ABIArgInfo::getDirect(); 7414 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) 7415 return getNaturalAlignIndirect(RetTy); 7416 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 7417 : ABIArgInfo::getDirect()); 7418 } 7419 7420 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { 7421 // Handle the generic C++ ABI. 7422 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 7423 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 7424 7425 // Integers and enums are extended to full register width. 7426 if (isPromotableIntegerTypeForABI(Ty)) 7427 return ABIArgInfo::getExtend(Ty); 7428 7429 // Handle vector types and vector-like structure types. Note that 7430 // as opposed to float-like structure types, we do not allow any 7431 // padding for vector-like structures, so verify the sizes match. 7432 uint64_t Size = getContext().getTypeSize(Ty); 7433 QualType SingleElementTy = GetSingleElementType(Ty); 7434 if (isVectorArgumentType(SingleElementTy) && 7435 getContext().getTypeSize(SingleElementTy) == Size) 7436 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); 7437 7438 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. 7439 if (Size != 8 && Size != 16 && Size != 32 && Size != 64) 7440 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 7441 7442 // Handle small structures. 7443 if (const RecordType *RT = Ty->getAs<RecordType>()) { 7444 // Structures with flexible arrays have variable length, so really 7445 // fail the size test above. 7446 const RecordDecl *RD = RT->getDecl(); 7447 if (RD->hasFlexibleArrayMember()) 7448 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 7449 7450 // The structure is passed as an unextended integer, a float, or a double. 7451 llvm::Type *PassTy; 7452 if (isFPArgumentType(SingleElementTy)) { 7453 assert(Size == 32 || Size == 64); 7454 if (Size == 32) 7455 PassTy = llvm::Type::getFloatTy(getVMContext()); 7456 else 7457 PassTy = llvm::Type::getDoubleTy(getVMContext()); 7458 } else 7459 PassTy = llvm::IntegerType::get(getVMContext(), Size); 7460 return ABIArgInfo::getDirect(PassTy); 7461 } 7462 7463 // Non-structure compounds are passed indirectly. 7464 if (isCompoundType(Ty)) 7465 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 7466 7467 return ABIArgInfo::getDirect(nullptr); 7468 } 7469 7470 //===----------------------------------------------------------------------===// 7471 // MSP430 ABI Implementation 7472 //===----------------------------------------------------------------------===// 7473 7474 namespace { 7475 7476 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { 7477 public: 7478 MSP430TargetCodeGenInfo(CodeGenTypes &CGT) 7479 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} 7480 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 7481 CodeGen::CodeGenModule &M) const override; 7482 }; 7483 7484 } 7485 7486 void MSP430TargetCodeGenInfo::setTargetAttributes( 7487 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 7488 if (GV->isDeclaration()) 7489 return; 7490 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 7491 const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); 7492 if (!InterruptAttr) 7493 return; 7494 7495 // Handle 'interrupt' attribute: 7496 llvm::Function *F = cast<llvm::Function>(GV); 7497 7498 // Step 1: Set ISR calling convention. 7499 F->setCallingConv(llvm::CallingConv::MSP430_INTR); 7500 7501 // Step 2: Add attributes goodness. 7502 F->addFnAttr(llvm::Attribute::NoInline); 7503 F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); 7504 } 7505 } 7506 7507 //===----------------------------------------------------------------------===// 7508 // MIPS ABI Implementation. This works for both little-endian and 7509 // big-endian variants. 7510 //===----------------------------------------------------------------------===// 7511 7512 namespace { 7513 class MipsABIInfo : public ABIInfo { 7514 bool IsO32; 7515 unsigned MinABIStackAlignInBytes, StackAlignInBytes; 7516 void CoerceToIntArgs(uint64_t TySize, 7517 SmallVectorImpl<llvm::Type *> &ArgList) const; 7518 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; 7519 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; 7520 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; 7521 public: 7522 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : 7523 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), 7524 StackAlignInBytes(IsO32 ? 8 : 16) {} 7525 7526 ABIArgInfo classifyReturnType(QualType RetTy) const; 7527 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; 7528 void computeInfo(CGFunctionInfo &FI) const override; 7529 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7530 QualType Ty) const override; 7531 ABIArgInfo extendType(QualType Ty) const; 7532 }; 7533 7534 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { 7535 unsigned SizeOfUnwindException; 7536 public: 7537 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) 7538 : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)), 7539 SizeOfUnwindException(IsO32 ? 24 : 32) {} 7540 7541 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 7542 return 29; 7543 } 7544 7545 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 7546 CodeGen::CodeGenModule &CGM) const override { 7547 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 7548 if (!FD) return; 7549 llvm::Function *Fn = cast<llvm::Function>(GV); 7550 7551 if (FD->hasAttr<MipsLongCallAttr>()) 7552 Fn->addFnAttr("long-call"); 7553 else if (FD->hasAttr<MipsShortCallAttr>()) 7554 Fn->addFnAttr("short-call"); 7555 7556 // Other attributes do not have a meaning for declarations. 7557 if (GV->isDeclaration()) 7558 return; 7559 7560 if (FD->hasAttr<Mips16Attr>()) { 7561 Fn->addFnAttr("mips16"); 7562 } 7563 else if (FD->hasAttr<NoMips16Attr>()) { 7564 Fn->addFnAttr("nomips16"); 7565 } 7566 7567 if (FD->hasAttr<MicroMipsAttr>()) 7568 Fn->addFnAttr("micromips"); 7569 else if (FD->hasAttr<NoMicroMipsAttr>()) 7570 Fn->addFnAttr("nomicromips"); 7571 7572 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); 7573 if (!Attr) 7574 return; 7575 7576 const char *Kind; 7577 switch (Attr->getInterrupt()) { 7578 case MipsInterruptAttr::eic: Kind = "eic"; break; 7579 case MipsInterruptAttr::sw0: Kind = "sw0"; break; 7580 case MipsInterruptAttr::sw1: Kind = "sw1"; break; 7581 case MipsInterruptAttr::hw0: Kind = "hw0"; break; 7582 case MipsInterruptAttr::hw1: Kind = "hw1"; break; 7583 case MipsInterruptAttr::hw2: Kind = "hw2"; break; 7584 case MipsInterruptAttr::hw3: Kind = "hw3"; break; 7585 case MipsInterruptAttr::hw4: Kind = "hw4"; break; 7586 case MipsInterruptAttr::hw5: Kind = "hw5"; break; 7587 } 7588 7589 Fn->addFnAttr("interrupt", Kind); 7590 7591 } 7592 7593 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 7594 llvm::Value *Address) const override; 7595 7596 unsigned getSizeOfUnwindException() const override { 7597 return SizeOfUnwindException; 7598 } 7599 }; 7600 } 7601 7602 void MipsABIInfo::CoerceToIntArgs( 7603 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { 7604 llvm::IntegerType *IntTy = 7605 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 7606 7607 // Add (TySize / MinABIStackAlignInBytes) args of IntTy. 7608 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) 7609 ArgList.push_back(IntTy); 7610 7611 // If necessary, add one more integer type to ArgList. 7612 unsigned R = TySize % (MinABIStackAlignInBytes * 8); 7613 7614 if (R) 7615 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); 7616 } 7617 7618 // In N32/64, an aligned double precision floating point field is passed in 7619 // a register. 7620 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { 7621 SmallVector<llvm::Type*, 8> ArgList, IntArgList; 7622 7623 if (IsO32) { 7624 CoerceToIntArgs(TySize, ArgList); 7625 return llvm::StructType::get(getVMContext(), ArgList); 7626 } 7627 7628 if (Ty->isComplexType()) 7629 return CGT.ConvertType(Ty); 7630 7631 const RecordType *RT = Ty->getAs<RecordType>(); 7632 7633 // Unions/vectors are passed in integer registers. 7634 if (!RT || !RT->isStructureOrClassType()) { 7635 CoerceToIntArgs(TySize, ArgList); 7636 return llvm::StructType::get(getVMContext(), ArgList); 7637 } 7638 7639 const RecordDecl *RD = RT->getDecl(); 7640 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 7641 assert(!(TySize % 8) && "Size of structure must be multiple of 8."); 7642 7643 uint64_t LastOffset = 0; 7644 unsigned idx = 0; 7645 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); 7646 7647 // Iterate over fields in the struct/class and check if there are any aligned 7648 // double fields. 7649 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 7650 i != e; ++i, ++idx) { 7651 const QualType Ty = i->getType(); 7652 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 7653 7654 if (!BT || BT->getKind() != BuiltinType::Double) 7655 continue; 7656 7657 uint64_t Offset = Layout.getFieldOffset(idx); 7658 if (Offset % 64) // Ignore doubles that are not aligned. 7659 continue; 7660 7661 // Add ((Offset - LastOffset) / 64) args of type i64. 7662 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) 7663 ArgList.push_back(I64); 7664 7665 // Add double type. 7666 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); 7667 LastOffset = Offset + 64; 7668 } 7669 7670 CoerceToIntArgs(TySize - LastOffset, IntArgList); 7671 ArgList.append(IntArgList.begin(), IntArgList.end()); 7672 7673 return llvm::StructType::get(getVMContext(), ArgList); 7674 } 7675 7676 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, 7677 uint64_t Offset) const { 7678 if (OrigOffset + MinABIStackAlignInBytes > Offset) 7679 return nullptr; 7680 7681 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); 7682 } 7683 7684 ABIArgInfo 7685 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { 7686 Ty = useFirstFieldIfTransparentUnion(Ty); 7687 7688 uint64_t OrigOffset = Offset; 7689 uint64_t TySize = getContext().getTypeSize(Ty); 7690 uint64_t Align = getContext().getTypeAlign(Ty) / 8; 7691 7692 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), 7693 (uint64_t)StackAlignInBytes); 7694 unsigned CurrOffset = llvm::alignTo(Offset, Align); 7695 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; 7696 7697 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { 7698 // Ignore empty aggregates. 7699 if (TySize == 0) 7700 return ABIArgInfo::getIgnore(); 7701 7702 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 7703 Offset = OrigOffset + MinABIStackAlignInBytes; 7704 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 7705 } 7706 7707 // If we have reached here, aggregates are passed directly by coercing to 7708 // another structure type. Padding is inserted if the offset of the 7709 // aggregate is unaligned. 7710 ABIArgInfo ArgInfo = 7711 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, 7712 getPaddingType(OrigOffset, CurrOffset)); 7713 ArgInfo.setInReg(true); 7714 return ArgInfo; 7715 } 7716 7717 // Treat an enum type as its underlying type. 7718 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 7719 Ty = EnumTy->getDecl()->getIntegerType(); 7720 7721 // Make sure we pass indirectly things that are too large. 7722 if (const auto *EIT = Ty->getAs<ExtIntType>()) 7723 if (EIT->getNumBits() > 128 || 7724 (EIT->getNumBits() > 64 && 7725 !getContext().getTargetInfo().hasInt128Type())) 7726 return getNaturalAlignIndirect(Ty); 7727 7728 // All integral types are promoted to the GPR width. 7729 if (Ty->isIntegralOrEnumerationType()) 7730 return extendType(Ty); 7731 7732 return ABIArgInfo::getDirect( 7733 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); 7734 } 7735 7736 llvm::Type* 7737 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { 7738 const RecordType *RT = RetTy->getAs<RecordType>(); 7739 SmallVector<llvm::Type*, 8> RTList; 7740 7741 if (RT && RT->isStructureOrClassType()) { 7742 const RecordDecl *RD = RT->getDecl(); 7743 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 7744 unsigned FieldCnt = Layout.getFieldCount(); 7745 7746 // N32/64 returns struct/classes in floating point registers if the 7747 // following conditions are met: 7748 // 1. The size of the struct/class is no larger than 128-bit. 7749 // 2. The struct/class has one or two fields all of which are floating 7750 // point types. 7751 // 3. The offset of the first field is zero (this follows what gcc does). 7752 // 7753 // Any other composite results are returned in integer registers. 7754 // 7755 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { 7756 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); 7757 for (; b != e; ++b) { 7758 const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); 7759 7760 if (!BT || !BT->isFloatingPoint()) 7761 break; 7762 7763 RTList.push_back(CGT.ConvertType(b->getType())); 7764 } 7765 7766 if (b == e) 7767 return llvm::StructType::get(getVMContext(), RTList, 7768 RD->hasAttr<PackedAttr>()); 7769 7770 RTList.clear(); 7771 } 7772 } 7773 7774 CoerceToIntArgs(Size, RTList); 7775 return llvm::StructType::get(getVMContext(), RTList); 7776 } 7777 7778 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { 7779 uint64_t Size = getContext().getTypeSize(RetTy); 7780 7781 if (RetTy->isVoidType()) 7782 return ABIArgInfo::getIgnore(); 7783 7784 // O32 doesn't treat zero-sized structs differently from other structs. 7785 // However, N32/N64 ignores zero sized return values. 7786 if (!IsO32 && Size == 0) 7787 return ABIArgInfo::getIgnore(); 7788 7789 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { 7790 if (Size <= 128) { 7791 if (RetTy->isAnyComplexType()) 7792 return ABIArgInfo::getDirect(); 7793 7794 // O32 returns integer vectors in registers and N32/N64 returns all small 7795 // aggregates in registers. 7796 if (!IsO32 || 7797 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { 7798 ABIArgInfo ArgInfo = 7799 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 7800 ArgInfo.setInReg(true); 7801 return ArgInfo; 7802 } 7803 } 7804 7805 return getNaturalAlignIndirect(RetTy); 7806 } 7807 7808 // Treat an enum type as its underlying type. 7809 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 7810 RetTy = EnumTy->getDecl()->getIntegerType(); 7811 7812 // Make sure we pass indirectly things that are too large. 7813 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 7814 if (EIT->getNumBits() > 128 || 7815 (EIT->getNumBits() > 64 && 7816 !getContext().getTargetInfo().hasInt128Type())) 7817 return getNaturalAlignIndirect(RetTy); 7818 7819 if (isPromotableIntegerTypeForABI(RetTy)) 7820 return ABIArgInfo::getExtend(RetTy); 7821 7822 if ((RetTy->isUnsignedIntegerOrEnumerationType() || 7823 RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) 7824 return ABIArgInfo::getSignExtend(RetTy); 7825 7826 return ABIArgInfo::getDirect(); 7827 } 7828 7829 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { 7830 ABIArgInfo &RetInfo = FI.getReturnInfo(); 7831 if (!getCXXABI().classifyReturnType(FI)) 7832 RetInfo = classifyReturnType(FI.getReturnType()); 7833 7834 // Check if a pointer to an aggregate is passed as a hidden argument. 7835 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; 7836 7837 for (auto &I : FI.arguments()) 7838 I.info = classifyArgumentType(I.type, Offset); 7839 } 7840 7841 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7842 QualType OrigTy) const { 7843 QualType Ty = OrigTy; 7844 7845 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. 7846 // Pointers are also promoted in the same way but this only matters for N32. 7847 unsigned SlotSizeInBits = IsO32 ? 32 : 64; 7848 unsigned PtrWidth = getTarget().getPointerWidth(0); 7849 bool DidPromote = false; 7850 if ((Ty->isIntegerType() && 7851 getContext().getIntWidth(Ty) < SlotSizeInBits) || 7852 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { 7853 DidPromote = true; 7854 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, 7855 Ty->isSignedIntegerType()); 7856 } 7857 7858 auto TyInfo = getContext().getTypeInfoInChars(Ty); 7859 7860 // The alignment of things in the argument area is never larger than 7861 // StackAlignInBytes. 7862 TyInfo.second = 7863 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); 7864 7865 // MinABIStackAlignInBytes is the size of argument slots on the stack. 7866 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); 7867 7868 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 7869 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); 7870 7871 7872 // If there was a promotion, "unpromote" into a temporary. 7873 // TODO: can we just use a pointer into a subset of the original slot? 7874 if (DidPromote) { 7875 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); 7876 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); 7877 7878 // Truncate down to the right width. 7879 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() 7880 : CGF.IntPtrTy); 7881 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); 7882 if (OrigTy->isPointerType()) 7883 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); 7884 7885 CGF.Builder.CreateStore(V, Temp); 7886 Addr = Temp; 7887 } 7888 7889 return Addr; 7890 } 7891 7892 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { 7893 int TySize = getContext().getTypeSize(Ty); 7894 7895 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. 7896 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) 7897 return ABIArgInfo::getSignExtend(Ty); 7898 7899 return ABIArgInfo::getExtend(Ty); 7900 } 7901 7902 bool 7903 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 7904 llvm::Value *Address) const { 7905 // This information comes from gcc's implementation, which seems to 7906 // as canonical as it gets. 7907 7908 // Everything on MIPS is 4 bytes. Double-precision FP registers 7909 // are aliased to pairs of single-precision FP registers. 7910 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 7911 7912 // 0-31 are the general purpose registers, $0 - $31. 7913 // 32-63 are the floating-point registers, $f0 - $f31. 7914 // 64 and 65 are the multiply/divide registers, $hi and $lo. 7915 // 66 is the (notional, I think) register for signal-handler return. 7916 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); 7917 7918 // 67-74 are the floating-point status registers, $fcc0 - $fcc7. 7919 // They are one bit wide and ignored here. 7920 7921 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. 7922 // (coprocessor 1 is the FP unit) 7923 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. 7924 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. 7925 // 176-181 are the DSP accumulator registers. 7926 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); 7927 return false; 7928 } 7929 7930 //===----------------------------------------------------------------------===// 7931 // AVR ABI Implementation. 7932 //===----------------------------------------------------------------------===// 7933 7934 namespace { 7935 class AVRTargetCodeGenInfo : public TargetCodeGenInfo { 7936 public: 7937 AVRTargetCodeGenInfo(CodeGenTypes &CGT) 7938 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} 7939 7940 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 7941 CodeGen::CodeGenModule &CGM) const override { 7942 if (GV->isDeclaration()) 7943 return; 7944 const auto *FD = dyn_cast_or_null<FunctionDecl>(D); 7945 if (!FD) return; 7946 auto *Fn = cast<llvm::Function>(GV); 7947 7948 if (FD->getAttr<AVRInterruptAttr>()) 7949 Fn->addFnAttr("interrupt"); 7950 7951 if (FD->getAttr<AVRSignalAttr>()) 7952 Fn->addFnAttr("signal"); 7953 } 7954 }; 7955 } 7956 7957 //===----------------------------------------------------------------------===// 7958 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. 7959 // Currently subclassed only to implement custom OpenCL C function attribute 7960 // handling. 7961 //===----------------------------------------------------------------------===// 7962 7963 namespace { 7964 7965 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { 7966 public: 7967 TCETargetCodeGenInfo(CodeGenTypes &CGT) 7968 : DefaultTargetCodeGenInfo(CGT) {} 7969 7970 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 7971 CodeGen::CodeGenModule &M) const override; 7972 }; 7973 7974 void TCETargetCodeGenInfo::setTargetAttributes( 7975 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 7976 if (GV->isDeclaration()) 7977 return; 7978 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 7979 if (!FD) return; 7980 7981 llvm::Function *F = cast<llvm::Function>(GV); 7982 7983 if (M.getLangOpts().OpenCL) { 7984 if (FD->hasAttr<OpenCLKernelAttr>()) { 7985 // OpenCL C Kernel functions are not subject to inlining 7986 F->addFnAttr(llvm::Attribute::NoInline); 7987 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); 7988 if (Attr) { 7989 // Convert the reqd_work_group_size() attributes to metadata. 7990 llvm::LLVMContext &Context = F->getContext(); 7991 llvm::NamedMDNode *OpenCLMetadata = 7992 M.getModule().getOrInsertNamedMetadata( 7993 "opencl.kernel_wg_size_info"); 7994 7995 SmallVector<llvm::Metadata *, 5> Operands; 7996 Operands.push_back(llvm::ConstantAsMetadata::get(F)); 7997 7998 Operands.push_back( 7999 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 8000 M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); 8001 Operands.push_back( 8002 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 8003 M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); 8004 Operands.push_back( 8005 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 8006 M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); 8007 8008 // Add a boolean constant operand for "required" (true) or "hint" 8009 // (false) for implementing the work_group_size_hint attr later. 8010 // Currently always true as the hint is not yet implemented. 8011 Operands.push_back( 8012 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); 8013 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); 8014 } 8015 } 8016 } 8017 } 8018 8019 } 8020 8021 //===----------------------------------------------------------------------===// 8022 // Hexagon ABI Implementation 8023 //===----------------------------------------------------------------------===// 8024 8025 namespace { 8026 8027 class HexagonABIInfo : public DefaultABIInfo { 8028 public: 8029 HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 8030 8031 private: 8032 ABIArgInfo classifyReturnType(QualType RetTy) const; 8033 ABIArgInfo classifyArgumentType(QualType RetTy) const; 8034 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const; 8035 8036 void computeInfo(CGFunctionInfo &FI) const override; 8037 8038 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8039 QualType Ty) const override; 8040 Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr, 8041 QualType Ty) const; 8042 Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr, 8043 QualType Ty) const; 8044 Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr, 8045 QualType Ty) const; 8046 }; 8047 8048 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { 8049 public: 8050 HexagonTargetCodeGenInfo(CodeGenTypes &CGT) 8051 : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {} 8052 8053 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 8054 return 29; 8055 } 8056 8057 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 8058 CodeGen::CodeGenModule &GCM) const override { 8059 if (GV->isDeclaration()) 8060 return; 8061 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 8062 if (!FD) 8063 return; 8064 } 8065 }; 8066 8067 } // namespace 8068 8069 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { 8070 unsigned RegsLeft = 6; 8071 if (!getCXXABI().classifyReturnType(FI)) 8072 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 8073 for (auto &I : FI.arguments()) 8074 I.info = classifyArgumentType(I.type, &RegsLeft); 8075 } 8076 8077 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) { 8078 assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits" 8079 " through registers"); 8080 8081 if (*RegsLeft == 0) 8082 return false; 8083 8084 if (Size <= 32) { 8085 (*RegsLeft)--; 8086 return true; 8087 } 8088 8089 if (2 <= (*RegsLeft & (~1U))) { 8090 *RegsLeft = (*RegsLeft & (~1U)) - 2; 8091 return true; 8092 } 8093 8094 // Next available register was r5 but candidate was greater than 32-bits so it 8095 // has to go on the stack. However we still consume r5 8096 if (*RegsLeft == 1) 8097 *RegsLeft = 0; 8098 8099 return false; 8100 } 8101 8102 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty, 8103 unsigned *RegsLeft) const { 8104 if (!isAggregateTypeForABI(Ty)) { 8105 // Treat an enum type as its underlying type. 8106 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 8107 Ty = EnumTy->getDecl()->getIntegerType(); 8108 8109 uint64_t Size = getContext().getTypeSize(Ty); 8110 if (Size <= 64) 8111 HexagonAdjustRegsLeft(Size, RegsLeft); 8112 8113 if (Size > 64 && Ty->isExtIntType()) 8114 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 8115 8116 return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 8117 : ABIArgInfo::getDirect(); 8118 } 8119 8120 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 8121 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 8122 8123 // Ignore empty records. 8124 if (isEmptyRecord(getContext(), Ty, true)) 8125 return ABIArgInfo::getIgnore(); 8126 8127 uint64_t Size = getContext().getTypeSize(Ty); 8128 unsigned Align = getContext().getTypeAlign(Ty); 8129 8130 if (Size > 64) 8131 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 8132 8133 if (HexagonAdjustRegsLeft(Size, RegsLeft)) 8134 Align = Size <= 32 ? 32 : 64; 8135 if (Size <= Align) { 8136 // Pass in the smallest viable integer type. 8137 if (!llvm::isPowerOf2_64(Size)) 8138 Size = llvm::NextPowerOf2(Size); 8139 return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size)); 8140 } 8141 return DefaultABIInfo::classifyArgumentType(Ty); 8142 } 8143 8144 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { 8145 if (RetTy->isVoidType()) 8146 return ABIArgInfo::getIgnore(); 8147 8148 const TargetInfo &T = CGT.getTarget(); 8149 uint64_t Size = getContext().getTypeSize(RetTy); 8150 8151 if (RetTy->getAs<VectorType>()) { 8152 // HVX vectors are returned in vector registers or register pairs. 8153 if (T.hasFeature("hvx")) { 8154 assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b")); 8155 uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8; 8156 if (Size == VecSize || Size == 2*VecSize) 8157 return ABIArgInfo::getDirectInReg(); 8158 } 8159 // Large vector types should be returned via memory. 8160 if (Size > 64) 8161 return getNaturalAlignIndirect(RetTy); 8162 } 8163 8164 if (!isAggregateTypeForABI(RetTy)) { 8165 // Treat an enum type as its underlying type. 8166 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 8167 RetTy = EnumTy->getDecl()->getIntegerType(); 8168 8169 if (Size > 64 && RetTy->isExtIntType()) 8170 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 8171 8172 return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 8173 : ABIArgInfo::getDirect(); 8174 } 8175 8176 if (isEmptyRecord(getContext(), RetTy, true)) 8177 return ABIArgInfo::getIgnore(); 8178 8179 // Aggregates <= 8 bytes are returned in registers, other aggregates 8180 // are returned indirectly. 8181 if (Size <= 64) { 8182 // Return in the smallest viable integer type. 8183 if (!llvm::isPowerOf2_64(Size)) 8184 Size = llvm::NextPowerOf2(Size); 8185 return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size)); 8186 } 8187 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); 8188 } 8189 8190 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF, 8191 Address VAListAddr, 8192 QualType Ty) const { 8193 // Load the overflow area pointer. 8194 Address __overflow_area_pointer_p = 8195 CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p"); 8196 llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad( 8197 __overflow_area_pointer_p, "__overflow_area_pointer"); 8198 8199 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; 8200 if (Align > 4) { 8201 // Alignment should be a power of 2. 8202 assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!"); 8203 8204 // overflow_arg_area = (overflow_arg_area + align - 1) & -align; 8205 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); 8206 8207 // Add offset to the current pointer to access the argument. 8208 __overflow_area_pointer = 8209 CGF.Builder.CreateGEP(__overflow_area_pointer, Offset); 8210 llvm::Value *AsInt = 8211 CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty); 8212 8213 // Create a mask which should be "AND"ed 8214 // with (overflow_arg_area + align - 1) 8215 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align); 8216 __overflow_area_pointer = CGF.Builder.CreateIntToPtr( 8217 CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(), 8218 "__overflow_area_pointer.align"); 8219 } 8220 8221 // Get the type of the argument from memory and bitcast 8222 // overflow area pointer to the argument type. 8223 llvm::Type *PTy = CGF.ConvertTypeForMem(Ty); 8224 Address AddrTyped = CGF.Builder.CreateBitCast( 8225 Address(__overflow_area_pointer, CharUnits::fromQuantity(Align)), 8226 llvm::PointerType::getUnqual(PTy)); 8227 8228 // Round up to the minimum stack alignment for varargs which is 4 bytes. 8229 uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4); 8230 8231 __overflow_area_pointer = CGF.Builder.CreateGEP( 8232 __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 8233 "__overflow_area_pointer.next"); 8234 CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p); 8235 8236 return AddrTyped; 8237 } 8238 8239 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF, 8240 Address VAListAddr, 8241 QualType Ty) const { 8242 // FIXME: Need to handle alignment 8243 llvm::Type *BP = CGF.Int8PtrTy; 8244 llvm::Type *BPP = CGF.Int8PtrPtrTy; 8245 CGBuilderTy &Builder = CGF.Builder; 8246 Address VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 8247 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 8248 // Handle address alignment for type alignment > 32 bits 8249 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; 8250 if (TyAlign > 4) { 8251 assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!"); 8252 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); 8253 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); 8254 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); 8255 Addr = Builder.CreateIntToPtr(AddrAsInt, BP); 8256 } 8257 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 8258 Address AddrTyped = Builder.CreateBitCast( 8259 Address(Addr, CharUnits::fromQuantity(TyAlign)), PTy); 8260 8261 uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4); 8262 llvm::Value *NextAddr = Builder.CreateGEP( 8263 Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); 8264 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 8265 8266 return AddrTyped; 8267 } 8268 8269 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF, 8270 Address VAListAddr, 8271 QualType Ty) const { 8272 int ArgSize = CGF.getContext().getTypeSize(Ty) / 8; 8273 8274 if (ArgSize > 8) 8275 return EmitVAArgFromMemory(CGF, VAListAddr, Ty); 8276 8277 // Here we have check if the argument is in register area or 8278 // in overflow area. 8279 // If the saved register area pointer + argsize rounded up to alignment > 8280 // saved register area end pointer, argument is in overflow area. 8281 unsigned RegsLeft = 6; 8282 Ty = CGF.getContext().getCanonicalType(Ty); 8283 (void)classifyArgumentType(Ty, &RegsLeft); 8284 8285 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); 8286 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 8287 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); 8288 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 8289 8290 // Get rounded size of the argument.GCC does not allow vararg of 8291 // size < 4 bytes. We follow the same logic here. 8292 ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8; 8293 int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8; 8294 8295 // Argument may be in saved register area 8296 CGF.EmitBlock(MaybeRegBlock); 8297 8298 // Load the current saved register area pointer. 8299 Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP( 8300 VAListAddr, 0, "__current_saved_reg_area_pointer_p"); 8301 llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad( 8302 __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer"); 8303 8304 // Load the saved register area end pointer. 8305 Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP( 8306 VAListAddr, 1, "__saved_reg_area_end_pointer_p"); 8307 llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad( 8308 __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer"); 8309 8310 // If the size of argument is > 4 bytes, check if the stack 8311 // location is aligned to 8 bytes 8312 if (ArgAlign > 4) { 8313 8314 llvm::Value *__current_saved_reg_area_pointer_int = 8315 CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer, 8316 CGF.Int32Ty); 8317 8318 __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd( 8319 __current_saved_reg_area_pointer_int, 8320 llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)), 8321 "align_current_saved_reg_area_pointer"); 8322 8323 __current_saved_reg_area_pointer_int = 8324 CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int, 8325 llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign), 8326 "align_current_saved_reg_area_pointer"); 8327 8328 __current_saved_reg_area_pointer = 8329 CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int, 8330 __current_saved_reg_area_pointer->getType(), 8331 "align_current_saved_reg_area_pointer"); 8332 } 8333 8334 llvm::Value *__new_saved_reg_area_pointer = 8335 CGF.Builder.CreateGEP(__current_saved_reg_area_pointer, 8336 llvm::ConstantInt::get(CGF.Int32Ty, ArgSize), 8337 "__new_saved_reg_area_pointer"); 8338 8339 llvm::Value *UsingStack = 0; 8340 UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer, 8341 __saved_reg_area_end_pointer); 8342 8343 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock); 8344 8345 // Argument in saved register area 8346 // Implement the block where argument is in register saved area 8347 CGF.EmitBlock(InRegBlock); 8348 8349 llvm::Type *PTy = CGF.ConvertType(Ty); 8350 llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast( 8351 __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy)); 8352 8353 CGF.Builder.CreateStore(__new_saved_reg_area_pointer, 8354 __current_saved_reg_area_pointer_p); 8355 8356 CGF.EmitBranch(ContBlock); 8357 8358 // Argument in overflow area 8359 // Implement the block where the argument is in overflow area. 8360 CGF.EmitBlock(OnStackBlock); 8361 8362 // Load the overflow area pointer 8363 Address __overflow_area_pointer_p = 8364 CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p"); 8365 llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad( 8366 __overflow_area_pointer_p, "__overflow_area_pointer"); 8367 8368 // Align the overflow area pointer according to the alignment of the argument 8369 if (ArgAlign > 4) { 8370 llvm::Value *__overflow_area_pointer_int = 8371 CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty); 8372 8373 __overflow_area_pointer_int = 8374 CGF.Builder.CreateAdd(__overflow_area_pointer_int, 8375 llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1), 8376 "align_overflow_area_pointer"); 8377 8378 __overflow_area_pointer_int = 8379 CGF.Builder.CreateAnd(__overflow_area_pointer_int, 8380 llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign), 8381 "align_overflow_area_pointer"); 8382 8383 __overflow_area_pointer = CGF.Builder.CreateIntToPtr( 8384 __overflow_area_pointer_int, __overflow_area_pointer->getType(), 8385 "align_overflow_area_pointer"); 8386 } 8387 8388 // Get the pointer for next argument in overflow area and store it 8389 // to overflow area pointer. 8390 llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP( 8391 __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, ArgSize), 8392 "__overflow_area_pointer.next"); 8393 8394 CGF.Builder.CreateStore(__new_overflow_area_pointer, 8395 __overflow_area_pointer_p); 8396 8397 CGF.Builder.CreateStore(__new_overflow_area_pointer, 8398 __current_saved_reg_area_pointer_p); 8399 8400 // Bitcast the overflow area pointer to the type of argument. 8401 llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty); 8402 llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast( 8403 __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy)); 8404 8405 CGF.EmitBranch(ContBlock); 8406 8407 // Get the correct pointer to load the variable argument 8408 // Implement the ContBlock 8409 CGF.EmitBlock(ContBlock); 8410 8411 llvm::Type *MemPTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 8412 llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr"); 8413 ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock); 8414 ArgAddr->addIncoming(__overflow_area_p, OnStackBlock); 8415 8416 return Address(ArgAddr, CharUnits::fromQuantity(ArgAlign)); 8417 } 8418 8419 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8420 QualType Ty) const { 8421 8422 if (getTarget().getTriple().isMusl()) 8423 return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty); 8424 8425 return EmitVAArgForHexagon(CGF, VAListAddr, Ty); 8426 } 8427 8428 //===----------------------------------------------------------------------===// 8429 // Lanai ABI Implementation 8430 //===----------------------------------------------------------------------===// 8431 8432 namespace { 8433 class LanaiABIInfo : public DefaultABIInfo { 8434 public: 8435 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 8436 8437 bool shouldUseInReg(QualType Ty, CCState &State) const; 8438 8439 void computeInfo(CGFunctionInfo &FI) const override { 8440 CCState State(FI); 8441 // Lanai uses 4 registers to pass arguments unless the function has the 8442 // regparm attribute set. 8443 if (FI.getHasRegParm()) { 8444 State.FreeRegs = FI.getRegParm(); 8445 } else { 8446 State.FreeRegs = 4; 8447 } 8448 8449 if (!getCXXABI().classifyReturnType(FI)) 8450 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 8451 for (auto &I : FI.arguments()) 8452 I.info = classifyArgumentType(I.type, State); 8453 } 8454 8455 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; 8456 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; 8457 }; 8458 } // end anonymous namespace 8459 8460 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { 8461 unsigned Size = getContext().getTypeSize(Ty); 8462 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; 8463 8464 if (SizeInRegs == 0) 8465 return false; 8466 8467 if (SizeInRegs > State.FreeRegs) { 8468 State.FreeRegs = 0; 8469 return false; 8470 } 8471 8472 State.FreeRegs -= SizeInRegs; 8473 8474 return true; 8475 } 8476 8477 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, 8478 CCState &State) const { 8479 if (!ByVal) { 8480 if (State.FreeRegs) { 8481 --State.FreeRegs; // Non-byval indirects just use one pointer. 8482 return getNaturalAlignIndirectInReg(Ty); 8483 } 8484 return getNaturalAlignIndirect(Ty, false); 8485 } 8486 8487 // Compute the byval alignment. 8488 const unsigned MinABIStackAlignInBytes = 4; 8489 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 8490 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, 8491 /*Realign=*/TypeAlign > 8492 MinABIStackAlignInBytes); 8493 } 8494 8495 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, 8496 CCState &State) const { 8497 // Check with the C++ ABI first. 8498 const RecordType *RT = Ty->getAs<RecordType>(); 8499 if (RT) { 8500 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 8501 if (RAA == CGCXXABI::RAA_Indirect) { 8502 return getIndirectResult(Ty, /*ByVal=*/false, State); 8503 } else if (RAA == CGCXXABI::RAA_DirectInMemory) { 8504 return getNaturalAlignIndirect(Ty, /*ByRef=*/true); 8505 } 8506 } 8507 8508 if (isAggregateTypeForABI(Ty)) { 8509 // Structures with flexible arrays are always indirect. 8510 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 8511 return getIndirectResult(Ty, /*ByVal=*/true, State); 8512 8513 // Ignore empty structs/unions. 8514 if (isEmptyRecord(getContext(), Ty, true)) 8515 return ABIArgInfo::getIgnore(); 8516 8517 llvm::LLVMContext &LLVMContext = getVMContext(); 8518 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; 8519 if (SizeInRegs <= State.FreeRegs) { 8520 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 8521 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); 8522 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 8523 State.FreeRegs -= SizeInRegs; 8524 return ABIArgInfo::getDirectInReg(Result); 8525 } else { 8526 State.FreeRegs = 0; 8527 } 8528 return getIndirectResult(Ty, true, State); 8529 } 8530 8531 // Treat an enum type as its underlying type. 8532 if (const auto *EnumTy = Ty->getAs<EnumType>()) 8533 Ty = EnumTy->getDecl()->getIntegerType(); 8534 8535 bool InReg = shouldUseInReg(Ty, State); 8536 8537 // Don't pass >64 bit integers in registers. 8538 if (const auto *EIT = Ty->getAs<ExtIntType>()) 8539 if (EIT->getNumBits() > 64) 8540 return getIndirectResult(Ty, /*ByVal=*/true, State); 8541 8542 if (isPromotableIntegerTypeForABI(Ty)) { 8543 if (InReg) 8544 return ABIArgInfo::getDirectInReg(); 8545 return ABIArgInfo::getExtend(Ty); 8546 } 8547 if (InReg) 8548 return ABIArgInfo::getDirectInReg(); 8549 return ABIArgInfo::getDirect(); 8550 } 8551 8552 namespace { 8553 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { 8554 public: 8555 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 8556 : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {} 8557 }; 8558 } 8559 8560 //===----------------------------------------------------------------------===// 8561 // AMDGPU ABI Implementation 8562 //===----------------------------------------------------------------------===// 8563 8564 namespace { 8565 8566 class AMDGPUABIInfo final : public DefaultABIInfo { 8567 private: 8568 static const unsigned MaxNumRegsForArgsRet = 16; 8569 8570 unsigned numRegsForType(QualType Ty) const; 8571 8572 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 8573 bool isHomogeneousAggregateSmallEnough(const Type *Base, 8574 uint64_t Members) const override; 8575 8576 // Coerce HIP pointer arguments from generic pointers to global ones. 8577 llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS, 8578 unsigned ToAS) const { 8579 // Structure types. 8580 if (auto STy = dyn_cast<llvm::StructType>(Ty)) { 8581 SmallVector<llvm::Type *, 8> EltTys; 8582 bool Changed = false; 8583 for (auto T : STy->elements()) { 8584 auto NT = coerceKernelArgumentType(T, FromAS, ToAS); 8585 EltTys.push_back(NT); 8586 Changed |= (NT != T); 8587 } 8588 // Skip if there is no change in element types. 8589 if (!Changed) 8590 return STy; 8591 if (STy->hasName()) 8592 return llvm::StructType::create( 8593 EltTys, (STy->getName() + ".coerce").str(), STy->isPacked()); 8594 return llvm::StructType::get(getVMContext(), EltTys, STy->isPacked()); 8595 } 8596 // Array types. 8597 if (auto ATy = dyn_cast<llvm::ArrayType>(Ty)) { 8598 auto T = ATy->getElementType(); 8599 auto NT = coerceKernelArgumentType(T, FromAS, ToAS); 8600 // Skip if there is no change in that element type. 8601 if (NT == T) 8602 return ATy; 8603 return llvm::ArrayType::get(NT, ATy->getNumElements()); 8604 } 8605 // Single value types. 8606 if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS) 8607 return llvm::PointerType::get( 8608 cast<llvm::PointerType>(Ty)->getElementType(), ToAS); 8609 return Ty; 8610 } 8611 8612 public: 8613 explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : 8614 DefaultABIInfo(CGT) {} 8615 8616 ABIArgInfo classifyReturnType(QualType RetTy) const; 8617 ABIArgInfo classifyKernelArgumentType(QualType Ty) const; 8618 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; 8619 8620 void computeInfo(CGFunctionInfo &FI) const override; 8621 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8622 QualType Ty) const override; 8623 }; 8624 8625 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 8626 return true; 8627 } 8628 8629 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( 8630 const Type *Base, uint64_t Members) const { 8631 uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; 8632 8633 // Homogeneous Aggregates may occupy at most 16 registers. 8634 return Members * NumRegs <= MaxNumRegsForArgsRet; 8635 } 8636 8637 /// Estimate number of registers the type will use when passed in registers. 8638 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { 8639 unsigned NumRegs = 0; 8640 8641 if (const VectorType *VT = Ty->getAs<VectorType>()) { 8642 // Compute from the number of elements. The reported size is based on the 8643 // in-memory size, which includes the padding 4th element for 3-vectors. 8644 QualType EltTy = VT->getElementType(); 8645 unsigned EltSize = getContext().getTypeSize(EltTy); 8646 8647 // 16-bit element vectors should be passed as packed. 8648 if (EltSize == 16) 8649 return (VT->getNumElements() + 1) / 2; 8650 8651 unsigned EltNumRegs = (EltSize + 31) / 32; 8652 return EltNumRegs * VT->getNumElements(); 8653 } 8654 8655 if (const RecordType *RT = Ty->getAs<RecordType>()) { 8656 const RecordDecl *RD = RT->getDecl(); 8657 assert(!RD->hasFlexibleArrayMember()); 8658 8659 for (const FieldDecl *Field : RD->fields()) { 8660 QualType FieldTy = Field->getType(); 8661 NumRegs += numRegsForType(FieldTy); 8662 } 8663 8664 return NumRegs; 8665 } 8666 8667 return (getContext().getTypeSize(Ty) + 31) / 32; 8668 } 8669 8670 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { 8671 llvm::CallingConv::ID CC = FI.getCallingConvention(); 8672 8673 if (!getCXXABI().classifyReturnType(FI)) 8674 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 8675 8676 unsigned NumRegsLeft = MaxNumRegsForArgsRet; 8677 for (auto &Arg : FI.arguments()) { 8678 if (CC == llvm::CallingConv::AMDGPU_KERNEL) { 8679 Arg.info = classifyKernelArgumentType(Arg.type); 8680 } else { 8681 Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); 8682 } 8683 } 8684 } 8685 8686 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8687 QualType Ty) const { 8688 llvm_unreachable("AMDGPU does not support varargs"); 8689 } 8690 8691 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { 8692 if (isAggregateTypeForABI(RetTy)) { 8693 // Records with non-trivial destructors/copy-constructors should not be 8694 // returned by value. 8695 if (!getRecordArgABI(RetTy, getCXXABI())) { 8696 // Ignore empty structs/unions. 8697 if (isEmptyRecord(getContext(), RetTy, true)) 8698 return ABIArgInfo::getIgnore(); 8699 8700 // Lower single-element structs to just return a regular value. 8701 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 8702 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 8703 8704 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 8705 const RecordDecl *RD = RT->getDecl(); 8706 if (RD->hasFlexibleArrayMember()) 8707 return DefaultABIInfo::classifyReturnType(RetTy); 8708 } 8709 8710 // Pack aggregates <= 4 bytes into single VGPR or pair. 8711 uint64_t Size = getContext().getTypeSize(RetTy); 8712 if (Size <= 16) 8713 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 8714 8715 if (Size <= 32) 8716 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 8717 8718 if (Size <= 64) { 8719 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); 8720 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); 8721 } 8722 8723 if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) 8724 return ABIArgInfo::getDirect(); 8725 } 8726 } 8727 8728 // Otherwise just do the default thing. 8729 return DefaultABIInfo::classifyReturnType(RetTy); 8730 } 8731 8732 /// For kernels all parameters are really passed in a special buffer. It doesn't 8733 /// make sense to pass anything byval, so everything must be direct. 8734 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { 8735 Ty = useFirstFieldIfTransparentUnion(Ty); 8736 8737 // TODO: Can we omit empty structs? 8738 8739 llvm::Type *LTy = nullptr; 8740 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) 8741 LTy = CGT.ConvertType(QualType(SeltTy, 0)); 8742 8743 if (getContext().getLangOpts().HIP) { 8744 if (!LTy) 8745 LTy = CGT.ConvertType(Ty); 8746 LTy = coerceKernelArgumentType( 8747 LTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default), 8748 /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device)); 8749 } 8750 8751 // If we set CanBeFlattened to true, CodeGen will expand the struct to its 8752 // individual elements, which confuses the Clover OpenCL backend; therefore we 8753 // have to set it to false here. Other args of getDirect() are just defaults. 8754 return ABIArgInfo::getDirect(LTy, 0, nullptr, false); 8755 } 8756 8757 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, 8758 unsigned &NumRegsLeft) const { 8759 assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); 8760 8761 Ty = useFirstFieldIfTransparentUnion(Ty); 8762 8763 if (isAggregateTypeForABI(Ty)) { 8764 // Records with non-trivial destructors/copy-constructors should not be 8765 // passed by value. 8766 if (auto RAA = getRecordArgABI(Ty, getCXXABI())) 8767 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 8768 8769 // Ignore empty structs/unions. 8770 if (isEmptyRecord(getContext(), Ty, true)) 8771 return ABIArgInfo::getIgnore(); 8772 8773 // Lower single-element structs to just pass a regular value. TODO: We 8774 // could do reasonable-size multiple-element structs too, using getExpand(), 8775 // though watch out for things like bitfields. 8776 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) 8777 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 8778 8779 if (const RecordType *RT = Ty->getAs<RecordType>()) { 8780 const RecordDecl *RD = RT->getDecl(); 8781 if (RD->hasFlexibleArrayMember()) 8782 return DefaultABIInfo::classifyArgumentType(Ty); 8783 } 8784 8785 // Pack aggregates <= 8 bytes into single VGPR or pair. 8786 uint64_t Size = getContext().getTypeSize(Ty); 8787 if (Size <= 64) { 8788 unsigned NumRegs = (Size + 31) / 32; 8789 NumRegsLeft -= std::min(NumRegsLeft, NumRegs); 8790 8791 if (Size <= 16) 8792 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 8793 8794 if (Size <= 32) 8795 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 8796 8797 // XXX: Should this be i64 instead, and should the limit increase? 8798 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); 8799 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); 8800 } 8801 8802 if (NumRegsLeft > 0) { 8803 unsigned NumRegs = numRegsForType(Ty); 8804 if (NumRegsLeft >= NumRegs) { 8805 NumRegsLeft -= NumRegs; 8806 return ABIArgInfo::getDirect(); 8807 } 8808 } 8809 } 8810 8811 // Otherwise just do the default thing. 8812 ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); 8813 if (!ArgInfo.isIndirect()) { 8814 unsigned NumRegs = numRegsForType(Ty); 8815 NumRegsLeft -= std::min(NumRegs, NumRegsLeft); 8816 } 8817 8818 return ArgInfo; 8819 } 8820 8821 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { 8822 public: 8823 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) 8824 : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {} 8825 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 8826 CodeGen::CodeGenModule &M) const override; 8827 unsigned getOpenCLKernelCallingConv() const override; 8828 8829 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, 8830 llvm::PointerType *T, QualType QT) const override; 8831 8832 LangAS getASTAllocaAddressSpace() const override { 8833 return getLangASFromTargetAS( 8834 getABIInfo().getDataLayout().getAllocaAddrSpace()); 8835 } 8836 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, 8837 const VarDecl *D) const override; 8838 llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts, 8839 SyncScope Scope, 8840 llvm::AtomicOrdering Ordering, 8841 llvm::LLVMContext &Ctx) const override; 8842 llvm::Function * 8843 createEnqueuedBlockKernel(CodeGenFunction &CGF, 8844 llvm::Function *BlockInvokeFunc, 8845 llvm::Value *BlockLiteral) const override; 8846 bool shouldEmitStaticExternCAliases() const override; 8847 void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; 8848 }; 8849 } 8850 8851 static bool requiresAMDGPUProtectedVisibility(const Decl *D, 8852 llvm::GlobalValue *GV) { 8853 if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) 8854 return false; 8855 8856 return D->hasAttr<OpenCLKernelAttr>() || 8857 (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) || 8858 (isa<VarDecl>(D) && 8859 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || 8860 cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() || 8861 cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType())); 8862 } 8863 8864 void AMDGPUTargetCodeGenInfo::setTargetAttributes( 8865 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 8866 if (requiresAMDGPUProtectedVisibility(D, GV)) { 8867 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 8868 GV->setDSOLocal(true); 8869 } 8870 8871 if (GV->isDeclaration()) 8872 return; 8873 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 8874 if (!FD) 8875 return; 8876 8877 llvm::Function *F = cast<llvm::Function>(GV); 8878 8879 const auto *ReqdWGS = M.getLangOpts().OpenCL ? 8880 FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; 8881 8882 8883 const bool IsOpenCLKernel = M.getLangOpts().OpenCL && 8884 FD->hasAttr<OpenCLKernelAttr>(); 8885 const bool IsHIPKernel = M.getLangOpts().HIP && 8886 FD->hasAttr<CUDAGlobalAttr>(); 8887 if ((IsOpenCLKernel || IsHIPKernel) && 8888 (M.getTriple().getOS() == llvm::Triple::AMDHSA)) 8889 F->addFnAttr("amdgpu-implicitarg-num-bytes", "56"); 8890 8891 if (IsHIPKernel) 8892 F->addFnAttr("uniform-work-group-size", "true"); 8893 8894 8895 const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); 8896 if (ReqdWGS || FlatWGS) { 8897 unsigned Min = 0; 8898 unsigned Max = 0; 8899 if (FlatWGS) { 8900 Min = FlatWGS->getMin() 8901 ->EvaluateKnownConstInt(M.getContext()) 8902 .getExtValue(); 8903 Max = FlatWGS->getMax() 8904 ->EvaluateKnownConstInt(M.getContext()) 8905 .getExtValue(); 8906 } 8907 if (ReqdWGS && Min == 0 && Max == 0) 8908 Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); 8909 8910 if (Min != 0) { 8911 assert(Min <= Max && "Min must be less than or equal Max"); 8912 8913 std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); 8914 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); 8915 } else 8916 assert(Max == 0 && "Max must be zero"); 8917 } else if (IsOpenCLKernel || IsHIPKernel) { 8918 // By default, restrict the maximum size to a value specified by 8919 // --gpu-max-threads-per-block=n or its default value for HIP. 8920 const unsigned OpenCLDefaultMaxWorkGroupSize = 256; 8921 const unsigned DefaultMaxWorkGroupSize = 8922 IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize 8923 : M.getLangOpts().GPUMaxThreadsPerBlock; 8924 std::string AttrVal = 8925 std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize); 8926 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); 8927 } 8928 8929 if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { 8930 unsigned Min = 8931 Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue(); 8932 unsigned Max = Attr->getMax() ? Attr->getMax() 8933 ->EvaluateKnownConstInt(M.getContext()) 8934 .getExtValue() 8935 : 0; 8936 8937 if (Min != 0) { 8938 assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); 8939 8940 std::string AttrVal = llvm::utostr(Min); 8941 if (Max != 0) 8942 AttrVal = AttrVal + "," + llvm::utostr(Max); 8943 F->addFnAttr("amdgpu-waves-per-eu", AttrVal); 8944 } else 8945 assert(Max == 0 && "Max must be zero"); 8946 } 8947 8948 if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { 8949 unsigned NumSGPR = Attr->getNumSGPR(); 8950 8951 if (NumSGPR != 0) 8952 F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); 8953 } 8954 8955 if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { 8956 uint32_t NumVGPR = Attr->getNumVGPR(); 8957 8958 if (NumVGPR != 0) 8959 F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); 8960 } 8961 } 8962 8963 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { 8964 return llvm::CallingConv::AMDGPU_KERNEL; 8965 } 8966 8967 // Currently LLVM assumes null pointers always have value 0, 8968 // which results in incorrectly transformed IR. Therefore, instead of 8969 // emitting null pointers in private and local address spaces, a null 8970 // pointer in generic address space is emitted which is casted to a 8971 // pointer in local or private address space. 8972 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( 8973 const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, 8974 QualType QT) const { 8975 if (CGM.getContext().getTargetNullPointerValue(QT) == 0) 8976 return llvm::ConstantPointerNull::get(PT); 8977 8978 auto &Ctx = CGM.getContext(); 8979 auto NPT = llvm::PointerType::get(PT->getElementType(), 8980 Ctx.getTargetAddressSpace(LangAS::opencl_generic)); 8981 return llvm::ConstantExpr::getAddrSpaceCast( 8982 llvm::ConstantPointerNull::get(NPT), PT); 8983 } 8984 8985 LangAS 8986 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, 8987 const VarDecl *D) const { 8988 assert(!CGM.getLangOpts().OpenCL && 8989 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && 8990 "Address space agnostic languages only"); 8991 LangAS DefaultGlobalAS = getLangASFromTargetAS( 8992 CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); 8993 if (!D) 8994 return DefaultGlobalAS; 8995 8996 LangAS AddrSpace = D->getType().getAddressSpace(); 8997 assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); 8998 if (AddrSpace != LangAS::Default) 8999 return AddrSpace; 9000 9001 if (CGM.isTypeConstant(D->getType(), false)) { 9002 if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) 9003 return ConstAS.getValue(); 9004 } 9005 return DefaultGlobalAS; 9006 } 9007 9008 llvm::SyncScope::ID 9009 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, 9010 SyncScope Scope, 9011 llvm::AtomicOrdering Ordering, 9012 llvm::LLVMContext &Ctx) const { 9013 std::string Name; 9014 switch (Scope) { 9015 case SyncScope::OpenCLWorkGroup: 9016 Name = "workgroup"; 9017 break; 9018 case SyncScope::OpenCLDevice: 9019 Name = "agent"; 9020 break; 9021 case SyncScope::OpenCLAllSVMDevices: 9022 Name = ""; 9023 break; 9024 case SyncScope::OpenCLSubGroup: 9025 Name = "wavefront"; 9026 } 9027 9028 if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) { 9029 if (!Name.empty()) 9030 Name = Twine(Twine(Name) + Twine("-")).str(); 9031 9032 Name = Twine(Twine(Name) + Twine("one-as")).str(); 9033 } 9034 9035 return Ctx.getOrInsertSyncScopeID(Name); 9036 } 9037 9038 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { 9039 return false; 9040 } 9041 9042 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( 9043 const FunctionType *&FT) const { 9044 FT = getABIInfo().getContext().adjustFunctionType( 9045 FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); 9046 } 9047 9048 //===----------------------------------------------------------------------===// 9049 // SPARC v8 ABI Implementation. 9050 // Based on the SPARC Compliance Definition version 2.4.1. 9051 // 9052 // Ensures that complex values are passed in registers. 9053 // 9054 namespace { 9055 class SparcV8ABIInfo : public DefaultABIInfo { 9056 public: 9057 SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 9058 9059 private: 9060 ABIArgInfo classifyReturnType(QualType RetTy) const; 9061 void computeInfo(CGFunctionInfo &FI) const override; 9062 }; 9063 } // end anonymous namespace 9064 9065 9066 ABIArgInfo 9067 SparcV8ABIInfo::classifyReturnType(QualType Ty) const { 9068 if (Ty->isAnyComplexType()) { 9069 return ABIArgInfo::getDirect(); 9070 } 9071 else { 9072 return DefaultABIInfo::classifyReturnType(Ty); 9073 } 9074 } 9075 9076 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { 9077 9078 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 9079 for (auto &Arg : FI.arguments()) 9080 Arg.info = classifyArgumentType(Arg.type); 9081 } 9082 9083 namespace { 9084 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { 9085 public: 9086 SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) 9087 : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {} 9088 }; 9089 } // end anonymous namespace 9090 9091 //===----------------------------------------------------------------------===// 9092 // SPARC v9 ABI Implementation. 9093 // Based on the SPARC Compliance Definition version 2.4.1. 9094 // 9095 // Function arguments a mapped to a nominal "parameter array" and promoted to 9096 // registers depending on their type. Each argument occupies 8 or 16 bytes in 9097 // the array, structs larger than 16 bytes are passed indirectly. 9098 // 9099 // One case requires special care: 9100 // 9101 // struct mixed { 9102 // int i; 9103 // float f; 9104 // }; 9105 // 9106 // When a struct mixed is passed by value, it only occupies 8 bytes in the 9107 // parameter array, but the int is passed in an integer register, and the float 9108 // is passed in a floating point register. This is represented as two arguments 9109 // with the LLVM IR inreg attribute: 9110 // 9111 // declare void f(i32 inreg %i, float inreg %f) 9112 // 9113 // The code generator will only allocate 4 bytes from the parameter array for 9114 // the inreg arguments. All other arguments are allocated a multiple of 8 9115 // bytes. 9116 // 9117 namespace { 9118 class SparcV9ABIInfo : public ABIInfo { 9119 public: 9120 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 9121 9122 private: 9123 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; 9124 void computeInfo(CGFunctionInfo &FI) const override; 9125 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9126 QualType Ty) const override; 9127 9128 // Coercion type builder for structs passed in registers. The coercion type 9129 // serves two purposes: 9130 // 9131 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' 9132 // in registers. 9133 // 2. Expose aligned floating point elements as first-level elements, so the 9134 // code generator knows to pass them in floating point registers. 9135 // 9136 // We also compute the InReg flag which indicates that the struct contains 9137 // aligned 32-bit floats. 9138 // 9139 struct CoerceBuilder { 9140 llvm::LLVMContext &Context; 9141 const llvm::DataLayout &DL; 9142 SmallVector<llvm::Type*, 8> Elems; 9143 uint64_t Size; 9144 bool InReg; 9145 9146 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) 9147 : Context(c), DL(dl), Size(0), InReg(false) {} 9148 9149 // Pad Elems with integers until Size is ToSize. 9150 void pad(uint64_t ToSize) { 9151 assert(ToSize >= Size && "Cannot remove elements"); 9152 if (ToSize == Size) 9153 return; 9154 9155 // Finish the current 64-bit word. 9156 uint64_t Aligned = llvm::alignTo(Size, 64); 9157 if (Aligned > Size && Aligned <= ToSize) { 9158 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); 9159 Size = Aligned; 9160 } 9161 9162 // Add whole 64-bit words. 9163 while (Size + 64 <= ToSize) { 9164 Elems.push_back(llvm::Type::getInt64Ty(Context)); 9165 Size += 64; 9166 } 9167 9168 // Final in-word padding. 9169 if (Size < ToSize) { 9170 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); 9171 Size = ToSize; 9172 } 9173 } 9174 9175 // Add a floating point element at Offset. 9176 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { 9177 // Unaligned floats are treated as integers. 9178 if (Offset % Bits) 9179 return; 9180 // The InReg flag is only required if there are any floats < 64 bits. 9181 if (Bits < 64) 9182 InReg = true; 9183 pad(Offset); 9184 Elems.push_back(Ty); 9185 Size = Offset + Bits; 9186 } 9187 9188 // Add a struct type to the coercion type, starting at Offset (in bits). 9189 void addStruct(uint64_t Offset, llvm::StructType *StrTy) { 9190 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); 9191 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { 9192 llvm::Type *ElemTy = StrTy->getElementType(i); 9193 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); 9194 switch (ElemTy->getTypeID()) { 9195 case llvm::Type::StructTyID: 9196 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); 9197 break; 9198 case llvm::Type::FloatTyID: 9199 addFloat(ElemOffset, ElemTy, 32); 9200 break; 9201 case llvm::Type::DoubleTyID: 9202 addFloat(ElemOffset, ElemTy, 64); 9203 break; 9204 case llvm::Type::FP128TyID: 9205 addFloat(ElemOffset, ElemTy, 128); 9206 break; 9207 case llvm::Type::PointerTyID: 9208 if (ElemOffset % 64 == 0) { 9209 pad(ElemOffset); 9210 Elems.push_back(ElemTy); 9211 Size += 64; 9212 } 9213 break; 9214 default: 9215 break; 9216 } 9217 } 9218 } 9219 9220 // Check if Ty is a usable substitute for the coercion type. 9221 bool isUsableType(llvm::StructType *Ty) const { 9222 return llvm::makeArrayRef(Elems) == Ty->elements(); 9223 } 9224 9225 // Get the coercion type as a literal struct type. 9226 llvm::Type *getType() const { 9227 if (Elems.size() == 1) 9228 return Elems.front(); 9229 else 9230 return llvm::StructType::get(Context, Elems); 9231 } 9232 }; 9233 }; 9234 } // end anonymous namespace 9235 9236 ABIArgInfo 9237 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { 9238 if (Ty->isVoidType()) 9239 return ABIArgInfo::getIgnore(); 9240 9241 uint64_t Size = getContext().getTypeSize(Ty); 9242 9243 // Anything too big to fit in registers is passed with an explicit indirect 9244 // pointer / sret pointer. 9245 if (Size > SizeLimit) 9246 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 9247 9248 // Treat an enum type as its underlying type. 9249 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 9250 Ty = EnumTy->getDecl()->getIntegerType(); 9251 9252 // Integer types smaller than a register are extended. 9253 if (Size < 64 && Ty->isIntegerType()) 9254 return ABIArgInfo::getExtend(Ty); 9255 9256 if (const auto *EIT = Ty->getAs<ExtIntType>()) 9257 if (EIT->getNumBits() < 64) 9258 return ABIArgInfo::getExtend(Ty); 9259 9260 // Other non-aggregates go in registers. 9261 if (!isAggregateTypeForABI(Ty)) 9262 return ABIArgInfo::getDirect(); 9263 9264 // If a C++ object has either a non-trivial copy constructor or a non-trivial 9265 // destructor, it is passed with an explicit indirect pointer / sret pointer. 9266 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 9267 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 9268 9269 // This is a small aggregate type that should be passed in registers. 9270 // Build a coercion type from the LLVM struct type. 9271 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); 9272 if (!StrTy) 9273 return ABIArgInfo::getDirect(); 9274 9275 CoerceBuilder CB(getVMContext(), getDataLayout()); 9276 CB.addStruct(0, StrTy); 9277 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); 9278 9279 // Try to use the original type for coercion. 9280 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); 9281 9282 if (CB.InReg) 9283 return ABIArgInfo::getDirectInReg(CoerceTy); 9284 else 9285 return ABIArgInfo::getDirect(CoerceTy); 9286 } 9287 9288 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9289 QualType Ty) const { 9290 ABIArgInfo AI = classifyType(Ty, 16 * 8); 9291 llvm::Type *ArgTy = CGT.ConvertType(Ty); 9292 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 9293 AI.setCoerceToType(ArgTy); 9294 9295 CharUnits SlotSize = CharUnits::fromQuantity(8); 9296 9297 CGBuilderTy &Builder = CGF.Builder; 9298 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); 9299 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 9300 9301 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 9302 9303 Address ArgAddr = Address::invalid(); 9304 CharUnits Stride; 9305 switch (AI.getKind()) { 9306 case ABIArgInfo::Expand: 9307 case ABIArgInfo::CoerceAndExpand: 9308 case ABIArgInfo::InAlloca: 9309 llvm_unreachable("Unsupported ABI kind for va_arg"); 9310 9311 case ABIArgInfo::Extend: { 9312 Stride = SlotSize; 9313 CharUnits Offset = SlotSize - TypeInfo.first; 9314 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); 9315 break; 9316 } 9317 9318 case ABIArgInfo::Direct: { 9319 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); 9320 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); 9321 ArgAddr = Addr; 9322 break; 9323 } 9324 9325 case ABIArgInfo::Indirect: 9326 Stride = SlotSize; 9327 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); 9328 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), 9329 TypeInfo.second); 9330 break; 9331 9332 case ABIArgInfo::Ignore: 9333 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); 9334 } 9335 9336 // Update VAList. 9337 Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); 9338 Builder.CreateStore(NextPtr.getPointer(), VAListAddr); 9339 9340 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); 9341 } 9342 9343 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { 9344 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); 9345 for (auto &I : FI.arguments()) 9346 I.info = classifyType(I.type, 16 * 8); 9347 } 9348 9349 namespace { 9350 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { 9351 public: 9352 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) 9353 : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {} 9354 9355 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 9356 return 14; 9357 } 9358 9359 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 9360 llvm::Value *Address) const override; 9361 }; 9362 } // end anonymous namespace 9363 9364 bool 9365 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 9366 llvm::Value *Address) const { 9367 // This is calculated from the LLVM and GCC tables and verified 9368 // against gcc output. AFAIK all ABIs use the same encoding. 9369 9370 CodeGen::CGBuilderTy &Builder = CGF.Builder; 9371 9372 llvm::IntegerType *i8 = CGF.Int8Ty; 9373 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 9374 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 9375 9376 // 0-31: the 8-byte general-purpose registers 9377 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 9378 9379 // 32-63: f0-31, the 4-byte floating-point registers 9380 AssignToArrayRange(Builder, Address, Four8, 32, 63); 9381 9382 // Y = 64 9383 // PSR = 65 9384 // WIM = 66 9385 // TBR = 67 9386 // PC = 68 9387 // NPC = 69 9388 // FSR = 70 9389 // CSR = 71 9390 AssignToArrayRange(Builder, Address, Eight8, 64, 71); 9391 9392 // 72-87: d0-15, the 8-byte floating-point registers 9393 AssignToArrayRange(Builder, Address, Eight8, 72, 87); 9394 9395 return false; 9396 } 9397 9398 // ARC ABI implementation. 9399 namespace { 9400 9401 class ARCABIInfo : public DefaultABIInfo { 9402 public: 9403 using DefaultABIInfo::DefaultABIInfo; 9404 9405 private: 9406 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9407 QualType Ty) const override; 9408 9409 void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { 9410 if (!State.FreeRegs) 9411 return; 9412 if (Info.isIndirect() && Info.getInReg()) 9413 State.FreeRegs--; 9414 else if (Info.isDirect() && Info.getInReg()) { 9415 unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; 9416 if (sz < State.FreeRegs) 9417 State.FreeRegs -= sz; 9418 else 9419 State.FreeRegs = 0; 9420 } 9421 } 9422 9423 void computeInfo(CGFunctionInfo &FI) const override { 9424 CCState State(FI); 9425 // ARC uses 8 registers to pass arguments. 9426 State.FreeRegs = 8; 9427 9428 if (!getCXXABI().classifyReturnType(FI)) 9429 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 9430 updateState(FI.getReturnInfo(), FI.getReturnType(), State); 9431 for (auto &I : FI.arguments()) { 9432 I.info = classifyArgumentType(I.type, State.FreeRegs); 9433 updateState(I.info, I.type, State); 9434 } 9435 } 9436 9437 ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; 9438 ABIArgInfo getIndirectByValue(QualType Ty) const; 9439 ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; 9440 ABIArgInfo classifyReturnType(QualType RetTy) const; 9441 }; 9442 9443 class ARCTargetCodeGenInfo : public TargetCodeGenInfo { 9444 public: 9445 ARCTargetCodeGenInfo(CodeGenTypes &CGT) 9446 : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {} 9447 }; 9448 9449 9450 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { 9451 return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : 9452 getNaturalAlignIndirect(Ty, false); 9453 } 9454 9455 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { 9456 // Compute the byval alignment. 9457 const unsigned MinABIStackAlignInBytes = 4; 9458 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 9459 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, 9460 TypeAlign > MinABIStackAlignInBytes); 9461 } 9462 9463 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9464 QualType Ty) const { 9465 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 9466 getContext().getTypeInfoInChars(Ty), 9467 CharUnits::fromQuantity(4), true); 9468 } 9469 9470 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, 9471 uint8_t FreeRegs) const { 9472 // Handle the generic C++ ABI. 9473 const RecordType *RT = Ty->getAs<RecordType>(); 9474 if (RT) { 9475 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 9476 if (RAA == CGCXXABI::RAA_Indirect) 9477 return getIndirectByRef(Ty, FreeRegs > 0); 9478 9479 if (RAA == CGCXXABI::RAA_DirectInMemory) 9480 return getIndirectByValue(Ty); 9481 } 9482 9483 // Treat an enum type as its underlying type. 9484 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 9485 Ty = EnumTy->getDecl()->getIntegerType(); 9486 9487 auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; 9488 9489 if (isAggregateTypeForABI(Ty)) { 9490 // Structures with flexible arrays are always indirect. 9491 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 9492 return getIndirectByValue(Ty); 9493 9494 // Ignore empty structs/unions. 9495 if (isEmptyRecord(getContext(), Ty, true)) 9496 return ABIArgInfo::getIgnore(); 9497 9498 llvm::LLVMContext &LLVMContext = getVMContext(); 9499 9500 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 9501 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); 9502 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 9503 9504 return FreeRegs >= SizeInRegs ? 9505 ABIArgInfo::getDirectInReg(Result) : 9506 ABIArgInfo::getDirect(Result, 0, nullptr, false); 9507 } 9508 9509 if (const auto *EIT = Ty->getAs<ExtIntType>()) 9510 if (EIT->getNumBits() > 64) 9511 return getIndirectByValue(Ty); 9512 9513 return isPromotableIntegerTypeForABI(Ty) 9514 ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) 9515 : ABIArgInfo::getExtend(Ty)) 9516 : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() 9517 : ABIArgInfo::getDirect()); 9518 } 9519 9520 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { 9521 if (RetTy->isAnyComplexType()) 9522 return ABIArgInfo::getDirectInReg(); 9523 9524 // Arguments of size > 4 registers are indirect. 9525 auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; 9526 if (RetSize > 4) 9527 return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); 9528 9529 return DefaultABIInfo::classifyReturnType(RetTy); 9530 } 9531 9532 } // End anonymous namespace. 9533 9534 //===----------------------------------------------------------------------===// 9535 // XCore ABI Implementation 9536 //===----------------------------------------------------------------------===// 9537 9538 namespace { 9539 9540 /// A SmallStringEnc instance is used to build up the TypeString by passing 9541 /// it by reference between functions that append to it. 9542 typedef llvm::SmallString<128> SmallStringEnc; 9543 9544 /// TypeStringCache caches the meta encodings of Types. 9545 /// 9546 /// The reason for caching TypeStrings is two fold: 9547 /// 1. To cache a type's encoding for later uses; 9548 /// 2. As a means to break recursive member type inclusion. 9549 /// 9550 /// A cache Entry can have a Status of: 9551 /// NonRecursive: The type encoding is not recursive; 9552 /// Recursive: The type encoding is recursive; 9553 /// Incomplete: An incomplete TypeString; 9554 /// IncompleteUsed: An incomplete TypeString that has been used in a 9555 /// Recursive type encoding. 9556 /// 9557 /// A NonRecursive entry will have all of its sub-members expanded as fully 9558 /// as possible. Whilst it may contain types which are recursive, the type 9559 /// itself is not recursive and thus its encoding may be safely used whenever 9560 /// the type is encountered. 9561 /// 9562 /// A Recursive entry will have all of its sub-members expanded as fully as 9563 /// possible. The type itself is recursive and it may contain other types which 9564 /// are recursive. The Recursive encoding must not be used during the expansion 9565 /// of a recursive type's recursive branch. For simplicity the code uses 9566 /// IncompleteCount to reject all usage of Recursive encodings for member types. 9567 /// 9568 /// An Incomplete entry is always a RecordType and only encodes its 9569 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and 9570 /// are placed into the cache during type expansion as a means to identify and 9571 /// handle recursive inclusion of types as sub-members. If there is recursion 9572 /// the entry becomes IncompleteUsed. 9573 /// 9574 /// During the expansion of a RecordType's members: 9575 /// 9576 /// If the cache contains a NonRecursive encoding for the member type, the 9577 /// cached encoding is used; 9578 /// 9579 /// If the cache contains a Recursive encoding for the member type, the 9580 /// cached encoding is 'Swapped' out, as it may be incorrect, and... 9581 /// 9582 /// If the member is a RecordType, an Incomplete encoding is placed into the 9583 /// cache to break potential recursive inclusion of itself as a sub-member; 9584 /// 9585 /// Once a member RecordType has been expanded, its temporary incomplete 9586 /// entry is removed from the cache. If a Recursive encoding was swapped out 9587 /// it is swapped back in; 9588 /// 9589 /// If an incomplete entry is used to expand a sub-member, the incomplete 9590 /// entry is marked as IncompleteUsed. The cache keeps count of how many 9591 /// IncompleteUsed entries it currently contains in IncompleteUsedCount; 9592 /// 9593 /// If a member's encoding is found to be a NonRecursive or Recursive viz: 9594 /// IncompleteUsedCount==0, the member's encoding is added to the cache. 9595 /// Else the member is part of a recursive type and thus the recursion has 9596 /// been exited too soon for the encoding to be correct for the member. 9597 /// 9598 class TypeStringCache { 9599 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; 9600 struct Entry { 9601 std::string Str; // The encoded TypeString for the type. 9602 enum Status State; // Information about the encoding in 'Str'. 9603 std::string Swapped; // A temporary place holder for a Recursive encoding 9604 // during the expansion of RecordType's members. 9605 }; 9606 std::map<const IdentifierInfo *, struct Entry> Map; 9607 unsigned IncompleteCount; // Number of Incomplete entries in the Map. 9608 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. 9609 public: 9610 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} 9611 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); 9612 bool removeIncomplete(const IdentifierInfo *ID); 9613 void addIfComplete(const IdentifierInfo *ID, StringRef Str, 9614 bool IsRecursive); 9615 StringRef lookupStr(const IdentifierInfo *ID); 9616 }; 9617 9618 /// TypeString encodings for enum & union fields must be order. 9619 /// FieldEncoding is a helper for this ordering process. 9620 class FieldEncoding { 9621 bool HasName; 9622 std::string Enc; 9623 public: 9624 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} 9625 StringRef str() { return Enc; } 9626 bool operator<(const FieldEncoding &rhs) const { 9627 if (HasName != rhs.HasName) return HasName; 9628 return Enc < rhs.Enc; 9629 } 9630 }; 9631 9632 class XCoreABIInfo : public DefaultABIInfo { 9633 public: 9634 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 9635 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9636 QualType Ty) const override; 9637 }; 9638 9639 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { 9640 mutable TypeStringCache TSC; 9641 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 9642 const CodeGen::CodeGenModule &M) const; 9643 9644 public: 9645 XCoreTargetCodeGenInfo(CodeGenTypes &CGT) 9646 : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {} 9647 void emitTargetMetadata(CodeGen::CodeGenModule &CGM, 9648 const llvm::MapVector<GlobalDecl, StringRef> 9649 &MangledDeclNames) const override; 9650 }; 9651 9652 } // End anonymous namespace. 9653 9654 // TODO: this implementation is likely now redundant with the default 9655 // EmitVAArg. 9656 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9657 QualType Ty) const { 9658 CGBuilderTy &Builder = CGF.Builder; 9659 9660 // Get the VAList. 9661 CharUnits SlotSize = CharUnits::fromQuantity(4); 9662 Address AP(Builder.CreateLoad(VAListAddr), SlotSize); 9663 9664 // Handle the argument. 9665 ABIArgInfo AI = classifyArgumentType(Ty); 9666 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); 9667 llvm::Type *ArgTy = CGT.ConvertType(Ty); 9668 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 9669 AI.setCoerceToType(ArgTy); 9670 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 9671 9672 Address Val = Address::invalid(); 9673 CharUnits ArgSize = CharUnits::Zero(); 9674 switch (AI.getKind()) { 9675 case ABIArgInfo::Expand: 9676 case ABIArgInfo::CoerceAndExpand: 9677 case ABIArgInfo::InAlloca: 9678 llvm_unreachable("Unsupported ABI kind for va_arg"); 9679 case ABIArgInfo::Ignore: 9680 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); 9681 ArgSize = CharUnits::Zero(); 9682 break; 9683 case ABIArgInfo::Extend: 9684 case ABIArgInfo::Direct: 9685 Val = Builder.CreateBitCast(AP, ArgPtrTy); 9686 ArgSize = CharUnits::fromQuantity( 9687 getDataLayout().getTypeAllocSize(AI.getCoerceToType())); 9688 ArgSize = ArgSize.alignTo(SlotSize); 9689 break; 9690 case ABIArgInfo::Indirect: 9691 Val = Builder.CreateElementBitCast(AP, ArgPtrTy); 9692 Val = Address(Builder.CreateLoad(Val), TypeAlign); 9693 ArgSize = SlotSize; 9694 break; 9695 } 9696 9697 // Increment the VAList. 9698 if (!ArgSize.isZero()) { 9699 Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize); 9700 Builder.CreateStore(APN.getPointer(), VAListAddr); 9701 } 9702 9703 return Val; 9704 } 9705 9706 /// During the expansion of a RecordType, an incomplete TypeString is placed 9707 /// into the cache as a means to identify and break recursion. 9708 /// If there is a Recursive encoding in the cache, it is swapped out and will 9709 /// be reinserted by removeIncomplete(). 9710 /// All other types of encoding should have been used rather than arriving here. 9711 void TypeStringCache::addIncomplete(const IdentifierInfo *ID, 9712 std::string StubEnc) { 9713 if (!ID) 9714 return; 9715 Entry &E = Map[ID]; 9716 assert( (E.Str.empty() || E.State == Recursive) && 9717 "Incorrectly use of addIncomplete"); 9718 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); 9719 E.Swapped.swap(E.Str); // swap out the Recursive 9720 E.Str.swap(StubEnc); 9721 E.State = Incomplete; 9722 ++IncompleteCount; 9723 } 9724 9725 /// Once the RecordType has been expanded, the temporary incomplete TypeString 9726 /// must be removed from the cache. 9727 /// If a Recursive was swapped out by addIncomplete(), it will be replaced. 9728 /// Returns true if the RecordType was defined recursively. 9729 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { 9730 if (!ID) 9731 return false; 9732 auto I = Map.find(ID); 9733 assert(I != Map.end() && "Entry not present"); 9734 Entry &E = I->second; 9735 assert( (E.State == Incomplete || 9736 E.State == IncompleteUsed) && 9737 "Entry must be an incomplete type"); 9738 bool IsRecursive = false; 9739 if (E.State == IncompleteUsed) { 9740 // We made use of our Incomplete encoding, thus we are recursive. 9741 IsRecursive = true; 9742 --IncompleteUsedCount; 9743 } 9744 if (E.Swapped.empty()) 9745 Map.erase(I); 9746 else { 9747 // Swap the Recursive back. 9748 E.Swapped.swap(E.Str); 9749 E.Swapped.clear(); 9750 E.State = Recursive; 9751 } 9752 --IncompleteCount; 9753 return IsRecursive; 9754 } 9755 9756 /// Add the encoded TypeString to the cache only if it is NonRecursive or 9757 /// Recursive (viz: all sub-members were expanded as fully as possible). 9758 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, 9759 bool IsRecursive) { 9760 if (!ID || IncompleteUsedCount) 9761 return; // No key or it is is an incomplete sub-type so don't add. 9762 Entry &E = Map[ID]; 9763 if (IsRecursive && !E.Str.empty()) { 9764 assert(E.State==Recursive && E.Str.size() == Str.size() && 9765 "This is not the same Recursive entry"); 9766 // The parent container was not recursive after all, so we could have used 9767 // this Recursive sub-member entry after all, but we assumed the worse when 9768 // we started viz: IncompleteCount!=0. 9769 return; 9770 } 9771 assert(E.Str.empty() && "Entry already present"); 9772 E.Str = Str.str(); 9773 E.State = IsRecursive? Recursive : NonRecursive; 9774 } 9775 9776 /// Return a cached TypeString encoding for the ID. If there isn't one, or we 9777 /// are recursively expanding a type (IncompleteCount != 0) and the cached 9778 /// encoding is Recursive, return an empty StringRef. 9779 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { 9780 if (!ID) 9781 return StringRef(); // We have no key. 9782 auto I = Map.find(ID); 9783 if (I == Map.end()) 9784 return StringRef(); // We have no encoding. 9785 Entry &E = I->second; 9786 if (E.State == Recursive && IncompleteCount) 9787 return StringRef(); // We don't use Recursive encodings for member types. 9788 9789 if (E.State == Incomplete) { 9790 // The incomplete type is being used to break out of recursion. 9791 E.State = IncompleteUsed; 9792 ++IncompleteUsedCount; 9793 } 9794 return E.Str; 9795 } 9796 9797 /// The XCore ABI includes a type information section that communicates symbol 9798 /// type information to the linker. The linker uses this information to verify 9799 /// safety/correctness of things such as array bound and pointers et al. 9800 /// The ABI only requires C (and XC) language modules to emit TypeStrings. 9801 /// This type information (TypeString) is emitted into meta data for all global 9802 /// symbols: definitions, declarations, functions & variables. 9803 /// 9804 /// The TypeString carries type, qualifier, name, size & value details. 9805 /// Please see 'Tools Development Guide' section 2.16.2 for format details: 9806 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf 9807 /// The output is tested by test/CodeGen/xcore-stringtype.c. 9808 /// 9809 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 9810 const CodeGen::CodeGenModule &CGM, 9811 TypeStringCache &TSC); 9812 9813 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. 9814 void XCoreTargetCodeGenInfo::emitTargetMD( 9815 const Decl *D, llvm::GlobalValue *GV, 9816 const CodeGen::CodeGenModule &CGM) const { 9817 SmallStringEnc Enc; 9818 if (getTypeString(Enc, D, CGM, TSC)) { 9819 llvm::LLVMContext &Ctx = CGM.getModule().getContext(); 9820 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), 9821 llvm::MDString::get(Ctx, Enc.str())}; 9822 llvm::NamedMDNode *MD = 9823 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); 9824 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 9825 } 9826 } 9827 9828 void XCoreTargetCodeGenInfo::emitTargetMetadata( 9829 CodeGen::CodeGenModule &CGM, 9830 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const { 9831 // Warning, new MangledDeclNames may be appended within this loop. 9832 // We rely on MapVector insertions adding new elements to the end 9833 // of the container. 9834 for (unsigned I = 0; I != MangledDeclNames.size(); ++I) { 9835 auto Val = *(MangledDeclNames.begin() + I); 9836 llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second); 9837 if (GV) { 9838 const Decl *D = Val.first.getDecl()->getMostRecentDecl(); 9839 emitTargetMD(D, GV, CGM); 9840 } 9841 } 9842 } 9843 //===----------------------------------------------------------------------===// 9844 // SPIR ABI Implementation 9845 //===----------------------------------------------------------------------===// 9846 9847 namespace { 9848 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { 9849 public: 9850 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 9851 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} 9852 unsigned getOpenCLKernelCallingConv() const override; 9853 }; 9854 9855 } // End anonymous namespace. 9856 9857 namespace clang { 9858 namespace CodeGen { 9859 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { 9860 DefaultABIInfo SPIRABI(CGM.getTypes()); 9861 SPIRABI.computeInfo(FI); 9862 } 9863 } 9864 } 9865 9866 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { 9867 return llvm::CallingConv::SPIR_KERNEL; 9868 } 9869 9870 static bool appendType(SmallStringEnc &Enc, QualType QType, 9871 const CodeGen::CodeGenModule &CGM, 9872 TypeStringCache &TSC); 9873 9874 /// Helper function for appendRecordType(). 9875 /// Builds a SmallVector containing the encoded field types in declaration 9876 /// order. 9877 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, 9878 const RecordDecl *RD, 9879 const CodeGen::CodeGenModule &CGM, 9880 TypeStringCache &TSC) { 9881 for (const auto *Field : RD->fields()) { 9882 SmallStringEnc Enc; 9883 Enc += "m("; 9884 Enc += Field->getName(); 9885 Enc += "){"; 9886 if (Field->isBitField()) { 9887 Enc += "b("; 9888 llvm::raw_svector_ostream OS(Enc); 9889 OS << Field->getBitWidthValue(CGM.getContext()); 9890 Enc += ':'; 9891 } 9892 if (!appendType(Enc, Field->getType(), CGM, TSC)) 9893 return false; 9894 if (Field->isBitField()) 9895 Enc += ')'; 9896 Enc += '}'; 9897 FE.emplace_back(!Field->getName().empty(), Enc); 9898 } 9899 return true; 9900 } 9901 9902 /// Appends structure and union types to Enc and adds encoding to cache. 9903 /// Recursively calls appendType (via extractFieldType) for each field. 9904 /// Union types have their fields ordered according to the ABI. 9905 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, 9906 const CodeGen::CodeGenModule &CGM, 9907 TypeStringCache &TSC, const IdentifierInfo *ID) { 9908 // Append the cached TypeString if we have one. 9909 StringRef TypeString = TSC.lookupStr(ID); 9910 if (!TypeString.empty()) { 9911 Enc += TypeString; 9912 return true; 9913 } 9914 9915 // Start to emit an incomplete TypeString. 9916 size_t Start = Enc.size(); 9917 Enc += (RT->isUnionType()? 'u' : 's'); 9918 Enc += '('; 9919 if (ID) 9920 Enc += ID->getName(); 9921 Enc += "){"; 9922 9923 // We collect all encoded fields and order as necessary. 9924 bool IsRecursive = false; 9925 const RecordDecl *RD = RT->getDecl()->getDefinition(); 9926 if (RD && !RD->field_empty()) { 9927 // An incomplete TypeString stub is placed in the cache for this RecordType 9928 // so that recursive calls to this RecordType will use it whilst building a 9929 // complete TypeString for this RecordType. 9930 SmallVector<FieldEncoding, 16> FE; 9931 std::string StubEnc(Enc.substr(Start).str()); 9932 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. 9933 TSC.addIncomplete(ID, std::move(StubEnc)); 9934 if (!extractFieldType(FE, RD, CGM, TSC)) { 9935 (void) TSC.removeIncomplete(ID); 9936 return false; 9937 } 9938 IsRecursive = TSC.removeIncomplete(ID); 9939 // The ABI requires unions to be sorted but not structures. 9940 // See FieldEncoding::operator< for sort algorithm. 9941 if (RT->isUnionType()) 9942 llvm::sort(FE); 9943 // We can now complete the TypeString. 9944 unsigned E = FE.size(); 9945 for (unsigned I = 0; I != E; ++I) { 9946 if (I) 9947 Enc += ','; 9948 Enc += FE[I].str(); 9949 } 9950 } 9951 Enc += '}'; 9952 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); 9953 return true; 9954 } 9955 9956 /// Appends enum types to Enc and adds the encoding to the cache. 9957 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, 9958 TypeStringCache &TSC, 9959 const IdentifierInfo *ID) { 9960 // Append the cached TypeString if we have one. 9961 StringRef TypeString = TSC.lookupStr(ID); 9962 if (!TypeString.empty()) { 9963 Enc += TypeString; 9964 return true; 9965 } 9966 9967 size_t Start = Enc.size(); 9968 Enc += "e("; 9969 if (ID) 9970 Enc += ID->getName(); 9971 Enc += "){"; 9972 9973 // We collect all encoded enumerations and order them alphanumerically. 9974 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { 9975 SmallVector<FieldEncoding, 16> FE; 9976 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; 9977 ++I) { 9978 SmallStringEnc EnumEnc; 9979 EnumEnc += "m("; 9980 EnumEnc += I->getName(); 9981 EnumEnc += "){"; 9982 I->getInitVal().toString(EnumEnc); 9983 EnumEnc += '}'; 9984 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); 9985 } 9986 llvm::sort(FE); 9987 unsigned E = FE.size(); 9988 for (unsigned I = 0; I != E; ++I) { 9989 if (I) 9990 Enc += ','; 9991 Enc += FE[I].str(); 9992 } 9993 } 9994 Enc += '}'; 9995 TSC.addIfComplete(ID, Enc.substr(Start), false); 9996 return true; 9997 } 9998 9999 /// Appends type's qualifier to Enc. 10000 /// This is done prior to appending the type's encoding. 10001 static void appendQualifier(SmallStringEnc &Enc, QualType QT) { 10002 // Qualifiers are emitted in alphabetical order. 10003 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; 10004 int Lookup = 0; 10005 if (QT.isConstQualified()) 10006 Lookup += 1<<0; 10007 if (QT.isRestrictQualified()) 10008 Lookup += 1<<1; 10009 if (QT.isVolatileQualified()) 10010 Lookup += 1<<2; 10011 Enc += Table[Lookup]; 10012 } 10013 10014 /// Appends built-in types to Enc. 10015 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { 10016 const char *EncType; 10017 switch (BT->getKind()) { 10018 case BuiltinType::Void: 10019 EncType = "0"; 10020 break; 10021 case BuiltinType::Bool: 10022 EncType = "b"; 10023 break; 10024 case BuiltinType::Char_U: 10025 EncType = "uc"; 10026 break; 10027 case BuiltinType::UChar: 10028 EncType = "uc"; 10029 break; 10030 case BuiltinType::SChar: 10031 EncType = "sc"; 10032 break; 10033 case BuiltinType::UShort: 10034 EncType = "us"; 10035 break; 10036 case BuiltinType::Short: 10037 EncType = "ss"; 10038 break; 10039 case BuiltinType::UInt: 10040 EncType = "ui"; 10041 break; 10042 case BuiltinType::Int: 10043 EncType = "si"; 10044 break; 10045 case BuiltinType::ULong: 10046 EncType = "ul"; 10047 break; 10048 case BuiltinType::Long: 10049 EncType = "sl"; 10050 break; 10051 case BuiltinType::ULongLong: 10052 EncType = "ull"; 10053 break; 10054 case BuiltinType::LongLong: 10055 EncType = "sll"; 10056 break; 10057 case BuiltinType::Float: 10058 EncType = "ft"; 10059 break; 10060 case BuiltinType::Double: 10061 EncType = "d"; 10062 break; 10063 case BuiltinType::LongDouble: 10064 EncType = "ld"; 10065 break; 10066 default: 10067 return false; 10068 } 10069 Enc += EncType; 10070 return true; 10071 } 10072 10073 /// Appends a pointer encoding to Enc before calling appendType for the pointee. 10074 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, 10075 const CodeGen::CodeGenModule &CGM, 10076 TypeStringCache &TSC) { 10077 Enc += "p("; 10078 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) 10079 return false; 10080 Enc += ')'; 10081 return true; 10082 } 10083 10084 /// Appends array encoding to Enc before calling appendType for the element. 10085 static bool appendArrayType(SmallStringEnc &Enc, QualType QT, 10086 const ArrayType *AT, 10087 const CodeGen::CodeGenModule &CGM, 10088 TypeStringCache &TSC, StringRef NoSizeEnc) { 10089 if (AT->getSizeModifier() != ArrayType::Normal) 10090 return false; 10091 Enc += "a("; 10092 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 10093 CAT->getSize().toStringUnsigned(Enc); 10094 else 10095 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". 10096 Enc += ':'; 10097 // The Qualifiers should be attached to the type rather than the array. 10098 appendQualifier(Enc, QT); 10099 if (!appendType(Enc, AT->getElementType(), CGM, TSC)) 10100 return false; 10101 Enc += ')'; 10102 return true; 10103 } 10104 10105 /// Appends a function encoding to Enc, calling appendType for the return type 10106 /// and the arguments. 10107 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, 10108 const CodeGen::CodeGenModule &CGM, 10109 TypeStringCache &TSC) { 10110 Enc += "f{"; 10111 if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) 10112 return false; 10113 Enc += "}("; 10114 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { 10115 // N.B. we are only interested in the adjusted param types. 10116 auto I = FPT->param_type_begin(); 10117 auto E = FPT->param_type_end(); 10118 if (I != E) { 10119 do { 10120 if (!appendType(Enc, *I, CGM, TSC)) 10121 return false; 10122 ++I; 10123 if (I != E) 10124 Enc += ','; 10125 } while (I != E); 10126 if (FPT->isVariadic()) 10127 Enc += ",va"; 10128 } else { 10129 if (FPT->isVariadic()) 10130 Enc += "va"; 10131 else 10132 Enc += '0'; 10133 } 10134 } 10135 Enc += ')'; 10136 return true; 10137 } 10138 10139 /// Handles the type's qualifier before dispatching a call to handle specific 10140 /// type encodings. 10141 static bool appendType(SmallStringEnc &Enc, QualType QType, 10142 const CodeGen::CodeGenModule &CGM, 10143 TypeStringCache &TSC) { 10144 10145 QualType QT = QType.getCanonicalType(); 10146 10147 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) 10148 // The Qualifiers should be attached to the type rather than the array. 10149 // Thus we don't call appendQualifier() here. 10150 return appendArrayType(Enc, QT, AT, CGM, TSC, ""); 10151 10152 appendQualifier(Enc, QT); 10153 10154 if (const BuiltinType *BT = QT->getAs<BuiltinType>()) 10155 return appendBuiltinType(Enc, BT); 10156 10157 if (const PointerType *PT = QT->getAs<PointerType>()) 10158 return appendPointerType(Enc, PT, CGM, TSC); 10159 10160 if (const EnumType *ET = QT->getAs<EnumType>()) 10161 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); 10162 10163 if (const RecordType *RT = QT->getAsStructureType()) 10164 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 10165 10166 if (const RecordType *RT = QT->getAsUnionType()) 10167 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 10168 10169 if (const FunctionType *FT = QT->getAs<FunctionType>()) 10170 return appendFunctionType(Enc, FT, CGM, TSC); 10171 10172 return false; 10173 } 10174 10175 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 10176 const CodeGen::CodeGenModule &CGM, 10177 TypeStringCache &TSC) { 10178 if (!D) 10179 return false; 10180 10181 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 10182 if (FD->getLanguageLinkage() != CLanguageLinkage) 10183 return false; 10184 return appendType(Enc, FD->getType(), CGM, TSC); 10185 } 10186 10187 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 10188 if (VD->getLanguageLinkage() != CLanguageLinkage) 10189 return false; 10190 QualType QT = VD->getType().getCanonicalType(); 10191 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { 10192 // Global ArrayTypes are given a size of '*' if the size is unknown. 10193 // The Qualifiers should be attached to the type rather than the array. 10194 // Thus we don't call appendQualifier() here. 10195 return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); 10196 } 10197 return appendType(Enc, QT, CGM, TSC); 10198 } 10199 return false; 10200 } 10201 10202 //===----------------------------------------------------------------------===// 10203 // RISCV ABI Implementation 10204 //===----------------------------------------------------------------------===// 10205 10206 namespace { 10207 class RISCVABIInfo : public DefaultABIInfo { 10208 private: 10209 // Size of the integer ('x') registers in bits. 10210 unsigned XLen; 10211 // Size of the floating point ('f') registers in bits. Note that the target 10212 // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target 10213 // with soft float ABI has FLen==0). 10214 unsigned FLen; 10215 static const int NumArgGPRs = 8; 10216 static const int NumArgFPRs = 8; 10217 bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, 10218 llvm::Type *&Field1Ty, 10219 CharUnits &Field1Off, 10220 llvm::Type *&Field2Ty, 10221 CharUnits &Field2Off) const; 10222 10223 public: 10224 RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen) 10225 : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {} 10226 10227 // DefaultABIInfo's classifyReturnType and classifyArgumentType are 10228 // non-virtual, but computeInfo is virtual, so we overload it. 10229 void computeInfo(CGFunctionInfo &FI) const override; 10230 10231 ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft, 10232 int &ArgFPRsLeft) const; 10233 ABIArgInfo classifyReturnType(QualType RetTy) const; 10234 10235 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 10236 QualType Ty) const override; 10237 10238 ABIArgInfo extendType(QualType Ty) const; 10239 10240 bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, 10241 CharUnits &Field1Off, llvm::Type *&Field2Ty, 10242 CharUnits &Field2Off, int &NeededArgGPRs, 10243 int &NeededArgFPRs) const; 10244 ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty, 10245 CharUnits Field1Off, 10246 llvm::Type *Field2Ty, 10247 CharUnits Field2Off) const; 10248 }; 10249 } // end anonymous namespace 10250 10251 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { 10252 QualType RetTy = FI.getReturnType(); 10253 if (!getCXXABI().classifyReturnType(FI)) 10254 FI.getReturnInfo() = classifyReturnType(RetTy); 10255 10256 // IsRetIndirect is true if classifyArgumentType indicated the value should 10257 // be passed indirect, or if the type size is a scalar greater than 2*XLen 10258 // and not a complex type with elements <= FLen. e.g. fp128 is passed direct 10259 // in LLVM IR, relying on the backend lowering code to rewrite the argument 10260 // list and pass indirectly on RV32. 10261 bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect; 10262 if (!IsRetIndirect && RetTy->isScalarType() && 10263 getContext().getTypeSize(RetTy) > (2 * XLen)) { 10264 if (RetTy->isComplexType() && FLen) { 10265 QualType EltTy = RetTy->getAs<ComplexType>()->getElementType(); 10266 IsRetIndirect = getContext().getTypeSize(EltTy) > FLen; 10267 } else { 10268 // This is a normal scalar > 2*XLen, such as fp128 on RV32. 10269 IsRetIndirect = true; 10270 } 10271 } 10272 10273 // We must track the number of GPRs used in order to conform to the RISC-V 10274 // ABI, as integer scalars passed in registers should have signext/zeroext 10275 // when promoted, but are anyext if passed on the stack. As GPR usage is 10276 // different for variadic arguments, we must also track whether we are 10277 // examining a vararg or not. 10278 int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; 10279 int ArgFPRsLeft = FLen ? NumArgFPRs : 0; 10280 int NumFixedArgs = FI.getNumRequiredArgs(); 10281 10282 int ArgNum = 0; 10283 for (auto &ArgInfo : FI.arguments()) { 10284 bool IsFixed = ArgNum < NumFixedArgs; 10285 ArgInfo.info = 10286 classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft); 10287 ArgNum++; 10288 } 10289 } 10290 10291 // Returns true if the struct is a potential candidate for the floating point 10292 // calling convention. If this function returns true, the caller is 10293 // responsible for checking that if there is only a single field then that 10294 // field is a float. 10295 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, 10296 llvm::Type *&Field1Ty, 10297 CharUnits &Field1Off, 10298 llvm::Type *&Field2Ty, 10299 CharUnits &Field2Off) const { 10300 bool IsInt = Ty->isIntegralOrEnumerationType(); 10301 bool IsFloat = Ty->isRealFloatingType(); 10302 10303 if (IsInt || IsFloat) { 10304 uint64_t Size = getContext().getTypeSize(Ty); 10305 if (IsInt && Size > XLen) 10306 return false; 10307 // Can't be eligible if larger than the FP registers. Half precision isn't 10308 // currently supported on RISC-V and the ABI hasn't been confirmed, so 10309 // default to the integer ABI in that case. 10310 if (IsFloat && (Size > FLen || Size < 32)) 10311 return false; 10312 // Can't be eligible if an integer type was already found (int+int pairs 10313 // are not eligible). 10314 if (IsInt && Field1Ty && Field1Ty->isIntegerTy()) 10315 return false; 10316 if (!Field1Ty) { 10317 Field1Ty = CGT.ConvertType(Ty); 10318 Field1Off = CurOff; 10319 return true; 10320 } 10321 if (!Field2Ty) { 10322 Field2Ty = CGT.ConvertType(Ty); 10323 Field2Off = CurOff; 10324 return true; 10325 } 10326 return false; 10327 } 10328 10329 if (auto CTy = Ty->getAs<ComplexType>()) { 10330 if (Field1Ty) 10331 return false; 10332 QualType EltTy = CTy->getElementType(); 10333 if (getContext().getTypeSize(EltTy) > FLen) 10334 return false; 10335 Field1Ty = CGT.ConvertType(EltTy); 10336 Field1Off = CurOff; 10337 assert(CurOff.isZero() && "Unexpected offset for first field"); 10338 Field2Ty = Field1Ty; 10339 Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy); 10340 return true; 10341 } 10342 10343 if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) { 10344 uint64_t ArraySize = ATy->getSize().getZExtValue(); 10345 QualType EltTy = ATy->getElementType(); 10346 CharUnits EltSize = getContext().getTypeSizeInChars(EltTy); 10347 for (uint64_t i = 0; i < ArraySize; ++i) { 10348 bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty, 10349 Field1Off, Field2Ty, Field2Off); 10350 if (!Ret) 10351 return false; 10352 CurOff += EltSize; 10353 } 10354 return true; 10355 } 10356 10357 if (const auto *RTy = Ty->getAs<RecordType>()) { 10358 // Structures with either a non-trivial destructor or a non-trivial 10359 // copy constructor are not eligible for the FP calling convention. 10360 if (getRecordArgABI(Ty, CGT.getCXXABI())) 10361 return false; 10362 if (isEmptyRecord(getContext(), Ty, true)) 10363 return true; 10364 const RecordDecl *RD = RTy->getDecl(); 10365 // Unions aren't eligible unless they're empty (which is caught above). 10366 if (RD->isUnion()) 10367 return false; 10368 int ZeroWidthBitFieldCount = 0; 10369 for (const FieldDecl *FD : RD->fields()) { 10370 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 10371 uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex()); 10372 QualType QTy = FD->getType(); 10373 if (FD->isBitField()) { 10374 unsigned BitWidth = FD->getBitWidthValue(getContext()); 10375 // Allow a bitfield with a type greater than XLen as long as the 10376 // bitwidth is XLen or less. 10377 if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen) 10378 QTy = getContext().getIntTypeForBitwidth(XLen, false); 10379 if (BitWidth == 0) { 10380 ZeroWidthBitFieldCount++; 10381 continue; 10382 } 10383 } 10384 10385 bool Ret = detectFPCCEligibleStructHelper( 10386 QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits), 10387 Field1Ty, Field1Off, Field2Ty, Field2Off); 10388 if (!Ret) 10389 return false; 10390 10391 // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp 10392 // or int+fp structs, but are ignored for a struct with an fp field and 10393 // any number of zero-width bitfields. 10394 if (Field2Ty && ZeroWidthBitFieldCount > 0) 10395 return false; 10396 } 10397 return Field1Ty != nullptr; 10398 } 10399 10400 return false; 10401 } 10402 10403 // Determine if a struct is eligible for passing according to the floating 10404 // point calling convention (i.e., when flattened it contains a single fp 10405 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and 10406 // NeededArgGPRs are incremented appropriately. 10407 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, 10408 CharUnits &Field1Off, 10409 llvm::Type *&Field2Ty, 10410 CharUnits &Field2Off, 10411 int &NeededArgGPRs, 10412 int &NeededArgFPRs) const { 10413 Field1Ty = nullptr; 10414 Field2Ty = nullptr; 10415 NeededArgGPRs = 0; 10416 NeededArgFPRs = 0; 10417 bool IsCandidate = detectFPCCEligibleStructHelper( 10418 Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off); 10419 // Not really a candidate if we have a single int but no float. 10420 if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy()) 10421 return false; 10422 if (!IsCandidate) 10423 return false; 10424 if (Field1Ty && Field1Ty->isFloatingPointTy()) 10425 NeededArgFPRs++; 10426 else if (Field1Ty) 10427 NeededArgGPRs++; 10428 if (Field2Ty && Field2Ty->isFloatingPointTy()) 10429 NeededArgFPRs++; 10430 else if (Field2Ty) 10431 NeededArgGPRs++; 10432 return IsCandidate; 10433 } 10434 10435 // Call getCoerceAndExpand for the two-element flattened struct described by 10436 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an 10437 // appropriate coerceToType and unpaddedCoerceToType. 10438 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct( 10439 llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty, 10440 CharUnits Field2Off) const { 10441 SmallVector<llvm::Type *, 3> CoerceElts; 10442 SmallVector<llvm::Type *, 2> UnpaddedCoerceElts; 10443 if (!Field1Off.isZero()) 10444 CoerceElts.push_back(llvm::ArrayType::get( 10445 llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity())); 10446 10447 CoerceElts.push_back(Field1Ty); 10448 UnpaddedCoerceElts.push_back(Field1Ty); 10449 10450 if (!Field2Ty) { 10451 return ABIArgInfo::getCoerceAndExpand( 10452 llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()), 10453 UnpaddedCoerceElts[0]); 10454 } 10455 10456 CharUnits Field2Align = 10457 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty)); 10458 CharUnits Field1Size = 10459 CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty)); 10460 CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align); 10461 10462 CharUnits Padding = CharUnits::Zero(); 10463 if (Field2Off > Field2OffNoPadNoPack) 10464 Padding = Field2Off - Field2OffNoPadNoPack; 10465 else if (Field2Off != Field2Align && Field2Off > Field1Size) 10466 Padding = Field2Off - Field1Size; 10467 10468 bool IsPacked = !Field2Off.isMultipleOf(Field2Align); 10469 10470 if (!Padding.isZero()) 10471 CoerceElts.push_back(llvm::ArrayType::get( 10472 llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity())); 10473 10474 CoerceElts.push_back(Field2Ty); 10475 UnpaddedCoerceElts.push_back(Field2Ty); 10476 10477 auto CoerceToType = 10478 llvm::StructType::get(getVMContext(), CoerceElts, IsPacked); 10479 auto UnpaddedCoerceToType = 10480 llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked); 10481 10482 return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType); 10483 } 10484 10485 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, 10486 int &ArgGPRsLeft, 10487 int &ArgFPRsLeft) const { 10488 assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); 10489 Ty = useFirstFieldIfTransparentUnion(Ty); 10490 10491 // Structures with either a non-trivial destructor or a non-trivial 10492 // copy constructor are always passed indirectly. 10493 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 10494 if (ArgGPRsLeft) 10495 ArgGPRsLeft -= 1; 10496 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == 10497 CGCXXABI::RAA_DirectInMemory); 10498 } 10499 10500 // Ignore empty structs/unions. 10501 if (isEmptyRecord(getContext(), Ty, true)) 10502 return ABIArgInfo::getIgnore(); 10503 10504 uint64_t Size = getContext().getTypeSize(Ty); 10505 10506 // Pass floating point values via FPRs if possible. 10507 if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() && 10508 FLen >= Size && ArgFPRsLeft) { 10509 ArgFPRsLeft--; 10510 return ABIArgInfo::getDirect(); 10511 } 10512 10513 // Complex types for the hard float ABI must be passed direct rather than 10514 // using CoerceAndExpand. 10515 if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) { 10516 QualType EltTy = Ty->castAs<ComplexType>()->getElementType(); 10517 if (getContext().getTypeSize(EltTy) <= FLen) { 10518 ArgFPRsLeft -= 2; 10519 return ABIArgInfo::getDirect(); 10520 } 10521 } 10522 10523 if (IsFixed && FLen && Ty->isStructureOrClassType()) { 10524 llvm::Type *Field1Ty = nullptr; 10525 llvm::Type *Field2Ty = nullptr; 10526 CharUnits Field1Off = CharUnits::Zero(); 10527 CharUnits Field2Off = CharUnits::Zero(); 10528 int NeededArgGPRs; 10529 int NeededArgFPRs; 10530 bool IsCandidate = 10531 detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off, 10532 NeededArgGPRs, NeededArgFPRs); 10533 if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft && 10534 NeededArgFPRs <= ArgFPRsLeft) { 10535 ArgGPRsLeft -= NeededArgGPRs; 10536 ArgFPRsLeft -= NeededArgFPRs; 10537 return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty, 10538 Field2Off); 10539 } 10540 } 10541 10542 uint64_t NeededAlign = getContext().getTypeAlign(Ty); 10543 bool MustUseStack = false; 10544 // Determine the number of GPRs needed to pass the current argument 10545 // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" 10546 // register pairs, so may consume 3 registers. 10547 int NeededArgGPRs = 1; 10548 if (!IsFixed && NeededAlign == 2 * XLen) 10549 NeededArgGPRs = 2 + (ArgGPRsLeft % 2); 10550 else if (Size > XLen && Size <= 2 * XLen) 10551 NeededArgGPRs = 2; 10552 10553 if (NeededArgGPRs > ArgGPRsLeft) { 10554 MustUseStack = true; 10555 NeededArgGPRs = ArgGPRsLeft; 10556 } 10557 10558 ArgGPRsLeft -= NeededArgGPRs; 10559 10560 if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { 10561 // Treat an enum type as its underlying type. 10562 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 10563 Ty = EnumTy->getDecl()->getIntegerType(); 10564 10565 // All integral types are promoted to XLen width, unless passed on the 10566 // stack. 10567 if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { 10568 return extendType(Ty); 10569 } 10570 10571 if (const auto *EIT = Ty->getAs<ExtIntType>()) { 10572 if (EIT->getNumBits() < XLen && !MustUseStack) 10573 return extendType(Ty); 10574 if (EIT->getNumBits() > 128 || 10575 (!getContext().getTargetInfo().hasInt128Type() && 10576 EIT->getNumBits() > 64)) 10577 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 10578 } 10579 10580 return ABIArgInfo::getDirect(); 10581 } 10582 10583 // Aggregates which are <= 2*XLen will be passed in registers if possible, 10584 // so coerce to integers. 10585 if (Size <= 2 * XLen) { 10586 unsigned Alignment = getContext().getTypeAlign(Ty); 10587 10588 // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is 10589 // required, and a 2-element XLen array if only XLen alignment is required. 10590 if (Size <= XLen) { 10591 return ABIArgInfo::getDirect( 10592 llvm::IntegerType::get(getVMContext(), XLen)); 10593 } else if (Alignment == 2 * XLen) { 10594 return ABIArgInfo::getDirect( 10595 llvm::IntegerType::get(getVMContext(), 2 * XLen)); 10596 } else { 10597 return ABIArgInfo::getDirect(llvm::ArrayType::get( 10598 llvm::IntegerType::get(getVMContext(), XLen), 2)); 10599 } 10600 } 10601 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 10602 } 10603 10604 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { 10605 if (RetTy->isVoidType()) 10606 return ABIArgInfo::getIgnore(); 10607 10608 int ArgGPRsLeft = 2; 10609 int ArgFPRsLeft = FLen ? 2 : 0; 10610 10611 // The rules for return and argument types are the same, so defer to 10612 // classifyArgumentType. 10613 return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft, 10614 ArgFPRsLeft); 10615 } 10616 10617 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 10618 QualType Ty) const { 10619 CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); 10620 10621 // Empty records are ignored for parameter passing purposes. 10622 if (isEmptyRecord(getContext(), Ty, true)) { 10623 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); 10624 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 10625 return Addr; 10626 } 10627 10628 std::pair<CharUnits, CharUnits> SizeAndAlign = 10629 getContext().getTypeInfoInChars(Ty); 10630 10631 // Arguments bigger than 2*Xlen bytes are passed indirectly. 10632 bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; 10633 10634 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, 10635 SlotSize, /*AllowHigherAlign=*/true); 10636 } 10637 10638 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { 10639 int TySize = getContext().getTypeSize(Ty); 10640 // RV64 ABI requires unsigned 32 bit integers to be sign extended. 10641 if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) 10642 return ABIArgInfo::getSignExtend(Ty); 10643 return ABIArgInfo::getExtend(Ty); 10644 } 10645 10646 namespace { 10647 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { 10648 public: 10649 RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, 10650 unsigned FLen) 10651 : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {} 10652 10653 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 10654 CodeGen::CodeGenModule &CGM) const override { 10655 const auto *FD = dyn_cast_or_null<FunctionDecl>(D); 10656 if (!FD) return; 10657 10658 const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); 10659 if (!Attr) 10660 return; 10661 10662 const char *Kind; 10663 switch (Attr->getInterrupt()) { 10664 case RISCVInterruptAttr::user: Kind = "user"; break; 10665 case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; 10666 case RISCVInterruptAttr::machine: Kind = "machine"; break; 10667 } 10668 10669 auto *Fn = cast<llvm::Function>(GV); 10670 10671 Fn->addFnAttr("interrupt", Kind); 10672 } 10673 }; 10674 } // namespace 10675 10676 //===----------------------------------------------------------------------===// 10677 // VE ABI Implementation. 10678 // 10679 namespace { 10680 class VEABIInfo : public DefaultABIInfo { 10681 public: 10682 VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 10683 10684 private: 10685 ABIArgInfo classifyReturnType(QualType RetTy) const; 10686 ABIArgInfo classifyArgumentType(QualType RetTy) const; 10687 void computeInfo(CGFunctionInfo &FI) const override; 10688 }; 10689 } // end anonymous namespace 10690 10691 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const { 10692 if (Ty->isAnyComplexType()) { 10693 return ABIArgInfo::getDirect(); 10694 } 10695 return DefaultABIInfo::classifyReturnType(Ty); 10696 } 10697 10698 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const { 10699 if (Ty->isAnyComplexType()) { 10700 return ABIArgInfo::getDirect(); 10701 } 10702 return DefaultABIInfo::classifyArgumentType(Ty); 10703 } 10704 10705 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const { 10706 10707 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 10708 for (auto &Arg : FI.arguments()) 10709 Arg.info = classifyArgumentType(Arg.type); 10710 } 10711 10712 namespace { 10713 class VETargetCodeGenInfo : public TargetCodeGenInfo { 10714 public: 10715 VETargetCodeGenInfo(CodeGenTypes &CGT) 10716 : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {} 10717 // VE ABI requires the arguments of variadic and prototype-less functions 10718 // are passed in both registers and memory. 10719 bool isNoProtoCallVariadic(const CallArgList &args, 10720 const FunctionNoProtoType *fnType) const override { 10721 return true; 10722 } 10723 }; 10724 } // end anonymous namespace 10725 10726 //===----------------------------------------------------------------------===// 10727 // Driver code 10728 //===----------------------------------------------------------------------===// 10729 10730 bool CodeGenModule::supportsCOMDAT() const { 10731 return getTriple().supportsCOMDAT(); 10732 } 10733 10734 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { 10735 if (TheTargetCodeGenInfo) 10736 return *TheTargetCodeGenInfo; 10737 10738 // Helper to set the unique_ptr while still keeping the return value. 10739 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { 10740 this->TheTargetCodeGenInfo.reset(P); 10741 return *P; 10742 }; 10743 10744 const llvm::Triple &Triple = getTarget().getTriple(); 10745 switch (Triple.getArch()) { 10746 default: 10747 return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); 10748 10749 case llvm::Triple::le32: 10750 return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); 10751 case llvm::Triple::mips: 10752 case llvm::Triple::mipsel: 10753 if (Triple.getOS() == llvm::Triple::NaCl) 10754 return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); 10755 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); 10756 10757 case llvm::Triple::mips64: 10758 case llvm::Triple::mips64el: 10759 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); 10760 10761 case llvm::Triple::avr: 10762 return SetCGInfo(new AVRTargetCodeGenInfo(Types)); 10763 10764 case llvm::Triple::aarch64: 10765 case llvm::Triple::aarch64_32: 10766 case llvm::Triple::aarch64_be: { 10767 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; 10768 if (getTarget().getABI() == "darwinpcs") 10769 Kind = AArch64ABIInfo::DarwinPCS; 10770 else if (Triple.isOSWindows()) 10771 return SetCGInfo( 10772 new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); 10773 10774 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); 10775 } 10776 10777 case llvm::Triple::wasm32: 10778 case llvm::Triple::wasm64: { 10779 WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP; 10780 if (getTarget().getABI() == "experimental-mv") 10781 Kind = WebAssemblyABIInfo::ExperimentalMV; 10782 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind)); 10783 } 10784 10785 case llvm::Triple::arm: 10786 case llvm::Triple::armeb: 10787 case llvm::Triple::thumb: 10788 case llvm::Triple::thumbeb: { 10789 if (Triple.getOS() == llvm::Triple::Win32) { 10790 return SetCGInfo( 10791 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); 10792 } 10793 10794 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; 10795 StringRef ABIStr = getTarget().getABI(); 10796 if (ABIStr == "apcs-gnu") 10797 Kind = ARMABIInfo::APCS; 10798 else if (ABIStr == "aapcs16") 10799 Kind = ARMABIInfo::AAPCS16_VFP; 10800 else if (CodeGenOpts.FloatABI == "hard" || 10801 (CodeGenOpts.FloatABI != "soft" && 10802 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 10803 Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 10804 Triple.getEnvironment() == llvm::Triple::EABIHF))) 10805 Kind = ARMABIInfo::AAPCS_VFP; 10806 10807 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); 10808 } 10809 10810 case llvm::Triple::ppc: { 10811 if (Triple.isOSAIX()) 10812 return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false)); 10813 10814 bool IsSoftFloat = 10815 CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe"); 10816 bool RetSmallStructInRegABI = 10817 PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); 10818 return SetCGInfo( 10819 new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI)); 10820 } 10821 case llvm::Triple::ppc64: 10822 if (Triple.isOSAIX()) 10823 return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true)); 10824 10825 if (Triple.isOSBinFormatELF()) { 10826 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; 10827 if (getTarget().getABI() == "elfv2") 10828 Kind = PPC64_SVR4_ABIInfo::ELFv2; 10829 bool HasQPX = getTarget().getABI() == "elfv1-qpx"; 10830 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; 10831 10832 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, 10833 IsSoftFloat)); 10834 } 10835 return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); 10836 case llvm::Triple::ppc64le: { 10837 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); 10838 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; 10839 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") 10840 Kind = PPC64_SVR4_ABIInfo::ELFv1; 10841 bool HasQPX = getTarget().getABI() == "elfv1-qpx"; 10842 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; 10843 10844 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, 10845 IsSoftFloat)); 10846 } 10847 10848 case llvm::Triple::nvptx: 10849 case llvm::Triple::nvptx64: 10850 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); 10851 10852 case llvm::Triple::msp430: 10853 return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); 10854 10855 case llvm::Triple::riscv32: 10856 case llvm::Triple::riscv64: { 10857 StringRef ABIStr = getTarget().getABI(); 10858 unsigned XLen = getTarget().getPointerWidth(0); 10859 unsigned ABIFLen = 0; 10860 if (ABIStr.endswith("f")) 10861 ABIFLen = 32; 10862 else if (ABIStr.endswith("d")) 10863 ABIFLen = 64; 10864 return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen)); 10865 } 10866 10867 case llvm::Triple::systemz: { 10868 bool SoftFloat = CodeGenOpts.FloatABI == "soft"; 10869 bool HasVector = !SoftFloat && getTarget().getABI() == "vector"; 10870 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat)); 10871 } 10872 10873 case llvm::Triple::tce: 10874 case llvm::Triple::tcele: 10875 return SetCGInfo(new TCETargetCodeGenInfo(Types)); 10876 10877 case llvm::Triple::x86: { 10878 bool IsDarwinVectorABI = Triple.isOSDarwin(); 10879 bool RetSmallStructInRegABI = 10880 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); 10881 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); 10882 10883 if (Triple.getOS() == llvm::Triple::Win32) { 10884 return SetCGInfo(new WinX86_32TargetCodeGenInfo( 10885 Types, IsDarwinVectorABI, RetSmallStructInRegABI, 10886 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); 10887 } else { 10888 return SetCGInfo(new X86_32TargetCodeGenInfo( 10889 Types, IsDarwinVectorABI, RetSmallStructInRegABI, 10890 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, 10891 CodeGenOpts.FloatABI == "soft")); 10892 } 10893 } 10894 10895 case llvm::Triple::x86_64: { 10896 StringRef ABI = getTarget().getABI(); 10897 X86AVXABILevel AVXLevel = 10898 (ABI == "avx512" 10899 ? X86AVXABILevel::AVX512 10900 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); 10901 10902 switch (Triple.getOS()) { 10903 case llvm::Triple::Win32: 10904 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); 10905 default: 10906 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); 10907 } 10908 } 10909 case llvm::Triple::hexagon: 10910 return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); 10911 case llvm::Triple::lanai: 10912 return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); 10913 case llvm::Triple::r600: 10914 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); 10915 case llvm::Triple::amdgcn: 10916 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); 10917 case llvm::Triple::sparc: 10918 return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); 10919 case llvm::Triple::sparcv9: 10920 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); 10921 case llvm::Triple::xcore: 10922 return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); 10923 case llvm::Triple::arc: 10924 return SetCGInfo(new ARCTargetCodeGenInfo(Types)); 10925 case llvm::Triple::spir: 10926 case llvm::Triple::spir64: 10927 return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); 10928 case llvm::Triple::ve: 10929 return SetCGInfo(new VETargetCodeGenInfo(Types)); 10930 } 10931 } 10932 10933 /// Create an OpenCL kernel for an enqueued block. 10934 /// 10935 /// The kernel has the same function type as the block invoke function. Its 10936 /// name is the name of the block invoke function postfixed with "_kernel". 10937 /// It simply calls the block invoke function then returns. 10938 llvm::Function * 10939 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, 10940 llvm::Function *Invoke, 10941 llvm::Value *BlockLiteral) const { 10942 auto *InvokeFT = Invoke->getFunctionType(); 10943 llvm::SmallVector<llvm::Type *, 2> ArgTys; 10944 for (auto &P : InvokeFT->params()) 10945 ArgTys.push_back(P); 10946 auto &C = CGF.getLLVMContext(); 10947 std::string Name = Invoke->getName().str() + "_kernel"; 10948 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); 10949 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, 10950 &CGF.CGM.getModule()); 10951 auto IP = CGF.Builder.saveIP(); 10952 auto *BB = llvm::BasicBlock::Create(C, "entry", F); 10953 auto &Builder = CGF.Builder; 10954 Builder.SetInsertPoint(BB); 10955 llvm::SmallVector<llvm::Value *, 2> Args; 10956 for (auto &A : F->args()) 10957 Args.push_back(&A); 10958 Builder.CreateCall(Invoke, Args); 10959 Builder.CreateRetVoid(); 10960 Builder.restoreIP(IP); 10961 return F; 10962 } 10963 10964 /// Create an OpenCL kernel for an enqueued block. 10965 /// 10966 /// The type of the first argument (the block literal) is the struct type 10967 /// of the block literal instead of a pointer type. The first argument 10968 /// (block literal) is passed directly by value to the kernel. The kernel 10969 /// allocates the same type of struct on stack and stores the block literal 10970 /// to it and passes its pointer to the block invoke function. The kernel 10971 /// has "enqueued-block" function attribute and kernel argument metadata. 10972 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( 10973 CodeGenFunction &CGF, llvm::Function *Invoke, 10974 llvm::Value *BlockLiteral) const { 10975 auto &Builder = CGF.Builder; 10976 auto &C = CGF.getLLVMContext(); 10977 10978 auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); 10979 auto *InvokeFT = Invoke->getFunctionType(); 10980 llvm::SmallVector<llvm::Type *, 2> ArgTys; 10981 llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; 10982 llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; 10983 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; 10984 llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; 10985 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; 10986 llvm::SmallVector<llvm::Metadata *, 8> ArgNames; 10987 10988 ArgTys.push_back(BlockTy); 10989 ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); 10990 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); 10991 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); 10992 ArgTypeQuals.push_back(llvm::MDString::get(C, "")); 10993 AccessQuals.push_back(llvm::MDString::get(C, "none")); 10994 ArgNames.push_back(llvm::MDString::get(C, "block_literal")); 10995 for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { 10996 ArgTys.push_back(InvokeFT->getParamType(I)); 10997 ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); 10998 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); 10999 AccessQuals.push_back(llvm::MDString::get(C, "none")); 11000 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); 11001 ArgTypeQuals.push_back(llvm::MDString::get(C, "")); 11002 ArgNames.push_back( 11003 llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); 11004 } 11005 std::string Name = Invoke->getName().str() + "_kernel"; 11006 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); 11007 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, 11008 &CGF.CGM.getModule()); 11009 F->addFnAttr("enqueued-block"); 11010 auto IP = CGF.Builder.saveIP(); 11011 auto *BB = llvm::BasicBlock::Create(C, "entry", F); 11012 Builder.SetInsertPoint(BB); 11013 const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy); 11014 auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); 11015 BlockPtr->setAlignment(BlockAlign); 11016 Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); 11017 auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); 11018 llvm::SmallVector<llvm::Value *, 2> Args; 11019 Args.push_back(Cast); 11020 for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) 11021 Args.push_back(I); 11022 Builder.CreateCall(Invoke, Args); 11023 Builder.CreateRetVoid(); 11024 Builder.restoreIP(IP); 11025 11026 F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); 11027 F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); 11028 F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); 11029 F->setMetadata("kernel_arg_base_type", 11030 llvm::MDNode::get(C, ArgBaseTypeNames)); 11031 F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); 11032 if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) 11033 F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); 11034 11035 return F; 11036 } 11037