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 bool WasArray = false; 503 if (AllowArrays) 504 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 505 if (AT->getSize() == 0) 506 return true; 507 FT = AT->getElementType(); 508 // The [[no_unique_address]] special case below does not apply to 509 // arrays of C++ empty records, so we need to remember this fact. 510 WasArray = true; 511 } 512 513 const RecordType *RT = FT->getAs<RecordType>(); 514 if (!RT) 515 return false; 516 517 // C++ record fields are never empty, at least in the Itanium ABI. 518 // 519 // FIXME: We should use a predicate for whether this behavior is true in the 520 // current ABI. 521 // 522 // The exception to the above rule are fields marked with the 523 // [[no_unique_address]] attribute (since C++20). Those do count as empty 524 // according to the Itanium ABI. The exception applies only to records, 525 // not arrays of records, so we must also check whether we stripped off an 526 // array type above. 527 if (isa<CXXRecordDecl>(RT->getDecl()) && 528 (WasArray || !FD->hasAttr<NoUniqueAddressAttr>())) 529 return false; 530 531 return isEmptyRecord(Context, FT, AllowArrays); 532 } 533 534 /// isEmptyRecord - Return true iff a structure contains only empty 535 /// fields. Note that a structure with a flexible array member is not 536 /// considered empty. 537 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { 538 const RecordType *RT = T->getAs<RecordType>(); 539 if (!RT) 540 return false; 541 const RecordDecl *RD = RT->getDecl(); 542 if (RD->hasFlexibleArrayMember()) 543 return false; 544 545 // If this is a C++ record, check the bases first. 546 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 547 for (const auto &I : CXXRD->bases()) 548 if (!isEmptyRecord(Context, I.getType(), true)) 549 return false; 550 551 for (const auto *I : RD->fields()) 552 if (!isEmptyField(Context, I, AllowArrays)) 553 return false; 554 return true; 555 } 556 557 /// isSingleElementStruct - Determine if a structure is a "single 558 /// element struct", i.e. it has exactly one non-empty field or 559 /// exactly one field which is itself a single element 560 /// struct. Structures with flexible array members are never 561 /// considered single element structs. 562 /// 563 /// \return The field declaration for the single non-empty field, if 564 /// it exists. 565 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { 566 const RecordType *RT = T->getAs<RecordType>(); 567 if (!RT) 568 return nullptr; 569 570 const RecordDecl *RD = RT->getDecl(); 571 if (RD->hasFlexibleArrayMember()) 572 return nullptr; 573 574 const Type *Found = nullptr; 575 576 // If this is a C++ record, check the bases first. 577 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 578 for (const auto &I : CXXRD->bases()) { 579 // Ignore empty records. 580 if (isEmptyRecord(Context, I.getType(), true)) 581 continue; 582 583 // If we already found an element then this isn't a single-element struct. 584 if (Found) 585 return nullptr; 586 587 // If this is non-empty and not a single element struct, the composite 588 // cannot be a single element struct. 589 Found = isSingleElementStruct(I.getType(), Context); 590 if (!Found) 591 return nullptr; 592 } 593 } 594 595 // Check for single element. 596 for (const auto *FD : RD->fields()) { 597 QualType FT = FD->getType(); 598 599 // Ignore empty fields. 600 if (isEmptyField(Context, FD, true)) 601 continue; 602 603 // If we already found an element then this isn't a single-element 604 // struct. 605 if (Found) 606 return nullptr; 607 608 // Treat single element arrays as the element. 609 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 610 if (AT->getSize().getZExtValue() != 1) 611 break; 612 FT = AT->getElementType(); 613 } 614 615 if (!isAggregateTypeForABI(FT)) { 616 Found = FT.getTypePtr(); 617 } else { 618 Found = isSingleElementStruct(FT, Context); 619 if (!Found) 620 return nullptr; 621 } 622 } 623 624 // We don't consider a struct a single-element struct if it has 625 // padding beyond the element type. 626 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) 627 return nullptr; 628 629 return Found; 630 } 631 632 namespace { 633 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, 634 const ABIArgInfo &AI) { 635 // This default implementation defers to the llvm backend's va_arg 636 // instruction. It can handle only passing arguments directly 637 // (typically only handled in the backend for primitive types), or 638 // aggregates passed indirectly by pointer (NOTE: if the "byval" 639 // flag has ABI impact in the callee, this implementation cannot 640 // work.) 641 642 // Only a few cases are covered here at the moment -- those needed 643 // by the default abi. 644 llvm::Value *Val; 645 646 if (AI.isIndirect()) { 647 assert(!AI.getPaddingType() && 648 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); 649 assert( 650 !AI.getIndirectRealign() && 651 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); 652 653 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); 654 CharUnits TyAlignForABI = TyInfo.second; 655 656 llvm::Type *BaseTy = 657 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 658 llvm::Value *Addr = 659 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); 660 return Address(Addr, TyAlignForABI); 661 } else { 662 assert((AI.isDirect() || AI.isExtend()) && 663 "Unexpected ArgInfo Kind in generic VAArg emitter!"); 664 665 assert(!AI.getInReg() && 666 "Unexpected InReg seen in arginfo in generic VAArg emitter!"); 667 assert(!AI.getPaddingType() && 668 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); 669 assert(!AI.getDirectOffset() && 670 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); 671 assert(!AI.getCoerceToType() && 672 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); 673 674 Address Temp = CGF.CreateMemTemp(Ty, "varet"); 675 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); 676 CGF.Builder.CreateStore(Val, Temp); 677 return Temp; 678 } 679 } 680 681 /// DefaultABIInfo - The default implementation for ABI specific 682 /// details. This implementation provides information which results in 683 /// self-consistent and sensible LLVM IR generation, but does not 684 /// conform to any particular ABI. 685 class DefaultABIInfo : public ABIInfo { 686 public: 687 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 688 689 ABIArgInfo classifyReturnType(QualType RetTy) const; 690 ABIArgInfo classifyArgumentType(QualType RetTy) const; 691 692 void computeInfo(CGFunctionInfo &FI) const override { 693 if (!getCXXABI().classifyReturnType(FI)) 694 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 695 for (auto &I : FI.arguments()) 696 I.info = classifyArgumentType(I.type); 697 } 698 699 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 700 QualType Ty) const override { 701 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); 702 } 703 }; 704 705 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { 706 public: 707 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 708 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} 709 }; 710 711 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { 712 Ty = useFirstFieldIfTransparentUnion(Ty); 713 714 if (isAggregateTypeForABI(Ty)) { 715 // Records with non-trivial destructors/copy-constructors should not be 716 // passed by value. 717 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 718 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 719 720 return getNaturalAlignIndirect(Ty); 721 } 722 723 // Treat an enum type as its underlying type. 724 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 725 Ty = EnumTy->getDecl()->getIntegerType(); 726 727 ASTContext &Context = getContext(); 728 if (const auto *EIT = Ty->getAs<ExtIntType>()) 729 if (EIT->getNumBits() > 730 Context.getTypeSize(Context.getTargetInfo().hasInt128Type() 731 ? Context.Int128Ty 732 : Context.LongLongTy)) 733 return getNaturalAlignIndirect(Ty); 734 735 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 736 : ABIArgInfo::getDirect()); 737 } 738 739 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { 740 if (RetTy->isVoidType()) 741 return ABIArgInfo::getIgnore(); 742 743 if (isAggregateTypeForABI(RetTy)) 744 return getNaturalAlignIndirect(RetTy); 745 746 // Treat an enum type as its underlying type. 747 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 748 RetTy = EnumTy->getDecl()->getIntegerType(); 749 750 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 751 if (EIT->getNumBits() > 752 getContext().getTypeSize(getContext().getTargetInfo().hasInt128Type() 753 ? getContext().Int128Ty 754 : getContext().LongLongTy)) 755 return getNaturalAlignIndirect(RetTy); 756 757 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 758 : ABIArgInfo::getDirect()); 759 } 760 761 //===----------------------------------------------------------------------===// 762 // WebAssembly ABI Implementation 763 // 764 // This is a very simple ABI that relies a lot on DefaultABIInfo. 765 //===----------------------------------------------------------------------===// 766 767 class WebAssemblyABIInfo final : public SwiftABIInfo { 768 public: 769 enum ABIKind { 770 MVP = 0, 771 ExperimentalMV = 1, 772 }; 773 774 private: 775 DefaultABIInfo defaultInfo; 776 ABIKind Kind; 777 778 public: 779 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind) 780 : SwiftABIInfo(CGT), defaultInfo(CGT), Kind(Kind) {} 781 782 private: 783 ABIArgInfo classifyReturnType(QualType RetTy) const; 784 ABIArgInfo classifyArgumentType(QualType Ty) const; 785 786 // DefaultABIInfo's classifyReturnType and classifyArgumentType are 787 // non-virtual, but computeInfo and EmitVAArg are virtual, so we 788 // overload them. 789 void computeInfo(CGFunctionInfo &FI) const override { 790 if (!getCXXABI().classifyReturnType(FI)) 791 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 792 for (auto &Arg : FI.arguments()) 793 Arg.info = classifyArgumentType(Arg.type); 794 } 795 796 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 797 QualType Ty) const override; 798 799 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 800 bool asReturnValue) const override { 801 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 802 } 803 804 bool isSwiftErrorInRegister() const override { 805 return false; 806 } 807 }; 808 809 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { 810 public: 811 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 812 WebAssemblyABIInfo::ABIKind K) 813 : TargetCodeGenInfo(std::make_unique<WebAssemblyABIInfo>(CGT, K)) {} 814 815 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 816 CodeGen::CodeGenModule &CGM) const override { 817 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 818 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { 819 if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) { 820 llvm::Function *Fn = cast<llvm::Function>(GV); 821 llvm::AttrBuilder B; 822 B.addAttribute("wasm-import-module", Attr->getImportModule()); 823 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 824 } 825 if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) { 826 llvm::Function *Fn = cast<llvm::Function>(GV); 827 llvm::AttrBuilder B; 828 B.addAttribute("wasm-import-name", Attr->getImportName()); 829 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 830 } 831 if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) { 832 llvm::Function *Fn = cast<llvm::Function>(GV); 833 llvm::AttrBuilder B; 834 B.addAttribute("wasm-export-name", Attr->getExportName()); 835 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 836 } 837 } 838 839 if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { 840 llvm::Function *Fn = cast<llvm::Function>(GV); 841 if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) 842 Fn->addFnAttr("no-prototype"); 843 } 844 } 845 }; 846 847 /// Classify argument of given type \p Ty. 848 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { 849 Ty = useFirstFieldIfTransparentUnion(Ty); 850 851 if (isAggregateTypeForABI(Ty)) { 852 // Records with non-trivial destructors/copy-constructors should not be 853 // passed by value. 854 if (auto RAA = getRecordArgABI(Ty, getCXXABI())) 855 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 856 // Ignore empty structs/unions. 857 if (isEmptyRecord(getContext(), Ty, true)) 858 return ABIArgInfo::getIgnore(); 859 // Lower single-element structs to just pass a regular value. TODO: We 860 // could do reasonable-size multiple-element structs too, using getExpand(), 861 // though watch out for things like bitfields. 862 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) 863 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 864 // For the experimental multivalue ABI, fully expand all other aggregates 865 if (Kind == ABIKind::ExperimentalMV) { 866 const RecordType *RT = Ty->getAs<RecordType>(); 867 assert(RT); 868 bool HasBitField = false; 869 for (auto *Field : RT->getDecl()->fields()) { 870 if (Field->isBitField()) { 871 HasBitField = true; 872 break; 873 } 874 } 875 if (!HasBitField) 876 return ABIArgInfo::getExpand(); 877 } 878 } 879 880 // Otherwise just do the default thing. 881 return defaultInfo.classifyArgumentType(Ty); 882 } 883 884 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { 885 if (isAggregateTypeForABI(RetTy)) { 886 // Records with non-trivial destructors/copy-constructors should not be 887 // returned by value. 888 if (!getRecordArgABI(RetTy, getCXXABI())) { 889 // Ignore empty structs/unions. 890 if (isEmptyRecord(getContext(), RetTy, true)) 891 return ABIArgInfo::getIgnore(); 892 // Lower single-element structs to just return a regular value. TODO: We 893 // could do reasonable-size multiple-element structs too, using 894 // ABIArgInfo::getDirect(). 895 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 896 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 897 // For the experimental multivalue ABI, return all other aggregates 898 if (Kind == ABIKind::ExperimentalMV) 899 return ABIArgInfo::getDirect(); 900 } 901 } 902 903 // Otherwise just do the default thing. 904 return defaultInfo.classifyReturnType(RetTy); 905 } 906 907 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 908 QualType Ty) const { 909 bool IsIndirect = isAggregateTypeForABI(Ty) && 910 !isEmptyRecord(getContext(), Ty, true) && 911 !isSingleElementStruct(Ty, getContext()); 912 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, 913 getContext().getTypeInfoInChars(Ty), 914 CharUnits::fromQuantity(4), 915 /*AllowHigherAlign=*/true); 916 } 917 918 //===----------------------------------------------------------------------===// 919 // le32/PNaCl bitcode ABI Implementation 920 // 921 // This is a simplified version of the x86_32 ABI. Arguments and return values 922 // are always passed on the stack. 923 //===----------------------------------------------------------------------===// 924 925 class PNaClABIInfo : public ABIInfo { 926 public: 927 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 928 929 ABIArgInfo classifyReturnType(QualType RetTy) const; 930 ABIArgInfo classifyArgumentType(QualType RetTy) const; 931 932 void computeInfo(CGFunctionInfo &FI) const override; 933 Address EmitVAArg(CodeGenFunction &CGF, 934 Address VAListAddr, QualType Ty) const override; 935 }; 936 937 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { 938 public: 939 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 940 : TargetCodeGenInfo(std::make_unique<PNaClABIInfo>(CGT)) {} 941 }; 942 943 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { 944 if (!getCXXABI().classifyReturnType(FI)) 945 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 946 947 for (auto &I : FI.arguments()) 948 I.info = classifyArgumentType(I.type); 949 } 950 951 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 952 QualType Ty) const { 953 // The PNaCL ABI is a bit odd, in that varargs don't use normal 954 // function classification. Structs get passed directly for varargs 955 // functions, through a rewriting transform in 956 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows 957 // this target to actually support a va_arg instructions with an 958 // aggregate type, unlike other targets. 959 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); 960 } 961 962 /// Classify argument of given type \p Ty. 963 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { 964 if (isAggregateTypeForABI(Ty)) { 965 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 966 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 967 return getNaturalAlignIndirect(Ty); 968 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 969 // Treat an enum type as its underlying type. 970 Ty = EnumTy->getDecl()->getIntegerType(); 971 } else if (Ty->isFloatingType()) { 972 // Floating-point types don't go inreg. 973 return ABIArgInfo::getDirect(); 974 } else if (const auto *EIT = Ty->getAs<ExtIntType>()) { 975 // Treat extended integers as integers if <=64, otherwise pass indirectly. 976 if (EIT->getNumBits() > 64) 977 return getNaturalAlignIndirect(Ty); 978 return ABIArgInfo::getDirect(); 979 } 980 981 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 982 : ABIArgInfo::getDirect()); 983 } 984 985 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { 986 if (RetTy->isVoidType()) 987 return ABIArgInfo::getIgnore(); 988 989 // In the PNaCl ABI we always return records/structures on the stack. 990 if (isAggregateTypeForABI(RetTy)) 991 return getNaturalAlignIndirect(RetTy); 992 993 // Treat extended integers as integers if <=64, otherwise pass indirectly. 994 if (const auto *EIT = RetTy->getAs<ExtIntType>()) { 995 if (EIT->getNumBits() > 64) 996 return getNaturalAlignIndirect(RetTy); 997 return ABIArgInfo::getDirect(); 998 } 999 1000 // Treat an enum type as its underlying type. 1001 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 1002 RetTy = EnumTy->getDecl()->getIntegerType(); 1003 1004 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 1005 : ABIArgInfo::getDirect()); 1006 } 1007 1008 /// IsX86_MMXType - Return true if this is an MMX type. 1009 bool IsX86_MMXType(llvm::Type *IRType) { 1010 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. 1011 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && 1012 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && 1013 IRType->getScalarSizeInBits() != 64; 1014 } 1015 1016 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 1017 StringRef Constraint, 1018 llvm::Type* Ty) { 1019 bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) 1020 .Cases("y", "&y", "^Ym", true) 1021 .Default(false); 1022 if (IsMMXCons && Ty->isVectorTy()) { 1023 if (cast<llvm::VectorType>(Ty)->getPrimitiveSizeInBits().getFixedSize() != 1024 64) { 1025 // Invalid MMX constraint 1026 return nullptr; 1027 } 1028 1029 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); 1030 } 1031 1032 // No operation needed 1033 return Ty; 1034 } 1035 1036 /// Returns true if this type can be passed in SSE registers with the 1037 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. 1038 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { 1039 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 1040 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { 1041 if (BT->getKind() == BuiltinType::LongDouble) { 1042 if (&Context.getTargetInfo().getLongDoubleFormat() == 1043 &llvm::APFloat::x87DoubleExtended()) 1044 return false; 1045 } 1046 return true; 1047 } 1048 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 1049 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX 1050 // registers specially. 1051 unsigned VecSize = Context.getTypeSize(VT); 1052 if (VecSize == 128 || VecSize == 256 || VecSize == 512) 1053 return true; 1054 } 1055 return false; 1056 } 1057 1058 /// Returns true if this aggregate is small enough to be passed in SSE registers 1059 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. 1060 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { 1061 return NumMembers <= 4; 1062 } 1063 1064 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. 1065 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { 1066 auto AI = ABIArgInfo::getDirect(T); 1067 AI.setInReg(true); 1068 AI.setCanBeFlattened(false); 1069 return AI; 1070 } 1071 1072 //===----------------------------------------------------------------------===// 1073 // X86-32 ABI Implementation 1074 //===----------------------------------------------------------------------===// 1075 1076 /// Similar to llvm::CCState, but for Clang. 1077 struct CCState { 1078 CCState(CGFunctionInfo &FI) 1079 : IsPreassigned(FI.arg_size()), CC(FI.getCallingConvention()) {} 1080 1081 llvm::SmallBitVector IsPreassigned; 1082 unsigned CC = CallingConv::CC_C; 1083 unsigned FreeRegs = 0; 1084 unsigned FreeSSERegs = 0; 1085 }; 1086 1087 enum { 1088 // Vectorcall only allows the first 6 parameters to be passed in registers. 1089 VectorcallMaxParamNumAsReg = 6 1090 }; 1091 1092 /// X86_32ABIInfo - The X86-32 ABI information. 1093 class X86_32ABIInfo : public SwiftABIInfo { 1094 enum Class { 1095 Integer, 1096 Float 1097 }; 1098 1099 static const unsigned MinABIStackAlignInBytes = 4; 1100 1101 bool IsDarwinVectorABI; 1102 bool IsRetSmallStructInRegABI; 1103 bool IsWin32StructABI; 1104 bool IsSoftFloatABI; 1105 bool IsMCUABI; 1106 unsigned DefaultNumRegisterParameters; 1107 1108 static bool isRegisterSize(unsigned Size) { 1109 return (Size == 8 || Size == 16 || Size == 32 || Size == 64); 1110 } 1111 1112 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 1113 // FIXME: Assumes vectorcall is in use. 1114 return isX86VectorTypeForVectorCall(getContext(), Ty); 1115 } 1116 1117 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 1118 uint64_t NumMembers) const override { 1119 // FIXME: Assumes vectorcall is in use. 1120 return isX86VectorCallAggregateSmallEnough(NumMembers); 1121 } 1122 1123 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; 1124 1125 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1126 /// such that the argument will be passed in memory. 1127 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; 1128 1129 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; 1130 1131 /// Return the alignment to use for the given type on the stack. 1132 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; 1133 1134 Class classify(QualType Ty) const; 1135 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; 1136 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; 1137 1138 /// Updates the number of available free registers, returns 1139 /// true if any registers were allocated. 1140 bool updateFreeRegs(QualType Ty, CCState &State) const; 1141 1142 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, 1143 bool &NeedsPadding) const; 1144 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; 1145 1146 bool canExpandIndirectArgument(QualType Ty) const; 1147 1148 /// Rewrite the function info so that all memory arguments use 1149 /// inalloca. 1150 void rewriteWithInAlloca(CGFunctionInfo &FI) const; 1151 1152 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 1153 CharUnits &StackOffset, ABIArgInfo &Info, 1154 QualType Type) const; 1155 void runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const; 1156 1157 public: 1158 1159 void computeInfo(CGFunctionInfo &FI) const override; 1160 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 1161 QualType Ty) const override; 1162 1163 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, 1164 bool RetSmallStructInRegABI, bool Win32StructABI, 1165 unsigned NumRegisterParameters, bool SoftFloatABI) 1166 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), 1167 IsRetSmallStructInRegABI(RetSmallStructInRegABI), 1168 IsWin32StructABI(Win32StructABI), 1169 IsSoftFloatABI(SoftFloatABI), 1170 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), 1171 DefaultNumRegisterParameters(NumRegisterParameters) {} 1172 1173 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 1174 bool asReturnValue) const override { 1175 // LLVM's x86-32 lowering currently only assigns up to three 1176 // integer registers and three fp registers. Oddly, it'll use up to 1177 // four vector registers for vectors, but those can overlap with the 1178 // scalar registers. 1179 return occupiesMoreThan(CGT, scalars, /*total*/ 3); 1180 } 1181 1182 bool isSwiftErrorInRegister() const override { 1183 // x86-32 lowering does not support passing swifterror in a register. 1184 return false; 1185 } 1186 }; 1187 1188 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { 1189 public: 1190 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, 1191 bool RetSmallStructInRegABI, bool Win32StructABI, 1192 unsigned NumRegisterParameters, bool SoftFloatABI) 1193 : TargetCodeGenInfo(std::make_unique<X86_32ABIInfo>( 1194 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, 1195 NumRegisterParameters, SoftFloatABI)) {} 1196 1197 static bool isStructReturnInRegABI( 1198 const llvm::Triple &Triple, const CodeGenOptions &Opts); 1199 1200 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 1201 CodeGen::CodeGenModule &CGM) const override; 1202 1203 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 1204 // Darwin uses different dwarf register numbers for EH. 1205 if (CGM.getTarget().getTriple().isOSDarwin()) return 5; 1206 return 4; 1207 } 1208 1209 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 1210 llvm::Value *Address) const override; 1211 1212 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 1213 StringRef Constraint, 1214 llvm::Type* Ty) const override { 1215 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 1216 } 1217 1218 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, 1219 std::string &Constraints, 1220 std::vector<llvm::Type *> &ResultRegTypes, 1221 std::vector<llvm::Type *> &ResultTruncRegTypes, 1222 std::vector<LValue> &ResultRegDests, 1223 std::string &AsmString, 1224 unsigned NumOutputs) const override; 1225 1226 llvm::Constant * 1227 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 1228 unsigned Sig = (0xeb << 0) | // jmp rel8 1229 (0x06 << 8) | // .+0x08 1230 ('v' << 16) | 1231 ('2' << 24); 1232 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 1233 } 1234 1235 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 1236 return "movl\t%ebp, %ebp" 1237 "\t\t// marker for objc_retainAutoreleaseReturnValue"; 1238 } 1239 }; 1240 1241 } 1242 1243 /// Rewrite input constraint references after adding some output constraints. 1244 /// In the case where there is one output and one input and we add one output, 1245 /// we need to replace all operand references greater than or equal to 1: 1246 /// mov $0, $1 1247 /// mov eax, $1 1248 /// The result will be: 1249 /// mov $0, $2 1250 /// mov eax, $2 1251 static void rewriteInputConstraintReferences(unsigned FirstIn, 1252 unsigned NumNewOuts, 1253 std::string &AsmString) { 1254 std::string Buf; 1255 llvm::raw_string_ostream OS(Buf); 1256 size_t Pos = 0; 1257 while (Pos < AsmString.size()) { 1258 size_t DollarStart = AsmString.find('$', Pos); 1259 if (DollarStart == std::string::npos) 1260 DollarStart = AsmString.size(); 1261 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); 1262 if (DollarEnd == std::string::npos) 1263 DollarEnd = AsmString.size(); 1264 OS << StringRef(&AsmString[Pos], DollarEnd - Pos); 1265 Pos = DollarEnd; 1266 size_t NumDollars = DollarEnd - DollarStart; 1267 if (NumDollars % 2 != 0 && Pos < AsmString.size()) { 1268 // We have an operand reference. 1269 size_t DigitStart = Pos; 1270 if (AsmString[DigitStart] == '{') { 1271 OS << '{'; 1272 ++DigitStart; 1273 } 1274 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); 1275 if (DigitEnd == std::string::npos) 1276 DigitEnd = AsmString.size(); 1277 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); 1278 unsigned OperandIndex; 1279 if (!OperandStr.getAsInteger(10, OperandIndex)) { 1280 if (OperandIndex >= FirstIn) 1281 OperandIndex += NumNewOuts; 1282 OS << OperandIndex; 1283 } else { 1284 OS << OperandStr; 1285 } 1286 Pos = DigitEnd; 1287 } 1288 } 1289 AsmString = std::move(OS.str()); 1290 } 1291 1292 /// Add output constraints for EAX:EDX because they are return registers. 1293 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( 1294 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, 1295 std::vector<llvm::Type *> &ResultRegTypes, 1296 std::vector<llvm::Type *> &ResultTruncRegTypes, 1297 std::vector<LValue> &ResultRegDests, std::string &AsmString, 1298 unsigned NumOutputs) const { 1299 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); 1300 1301 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is 1302 // larger. 1303 if (!Constraints.empty()) 1304 Constraints += ','; 1305 if (RetWidth <= 32) { 1306 Constraints += "={eax}"; 1307 ResultRegTypes.push_back(CGF.Int32Ty); 1308 } else { 1309 // Use the 'A' constraint for EAX:EDX. 1310 Constraints += "=A"; 1311 ResultRegTypes.push_back(CGF.Int64Ty); 1312 } 1313 1314 // Truncate EAX or EAX:EDX to an integer of the appropriate size. 1315 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); 1316 ResultTruncRegTypes.push_back(CoerceTy); 1317 1318 // Coerce the integer by bitcasting the return slot pointer. 1319 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(CGF), 1320 CoerceTy->getPointerTo())); 1321 ResultRegDests.push_back(ReturnSlot); 1322 1323 rewriteInputConstraintReferences(NumOutputs, 1, AsmString); 1324 } 1325 1326 /// shouldReturnTypeInRegister - Determine if the given type should be 1327 /// returned in a register (for the Darwin and MCU ABI). 1328 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, 1329 ASTContext &Context) const { 1330 uint64_t Size = Context.getTypeSize(Ty); 1331 1332 // For i386, type must be register sized. 1333 // For the MCU ABI, it only needs to be <= 8-byte 1334 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) 1335 return false; 1336 1337 if (Ty->isVectorType()) { 1338 // 64- and 128- bit vectors inside structures are not returned in 1339 // registers. 1340 if (Size == 64 || Size == 128) 1341 return false; 1342 1343 return true; 1344 } 1345 1346 // If this is a builtin, pointer, enum, complex type, member pointer, or 1347 // member function pointer it is ok. 1348 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || 1349 Ty->isAnyComplexType() || Ty->isEnumeralType() || 1350 Ty->isBlockPointerType() || Ty->isMemberPointerType()) 1351 return true; 1352 1353 // Arrays are treated like records. 1354 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) 1355 return shouldReturnTypeInRegister(AT->getElementType(), Context); 1356 1357 // Otherwise, it must be a record type. 1358 const RecordType *RT = Ty->getAs<RecordType>(); 1359 if (!RT) return false; 1360 1361 // FIXME: Traverse bases here too. 1362 1363 // Structure types are passed in register if all fields would be 1364 // passed in a register. 1365 for (const auto *FD : RT->getDecl()->fields()) { 1366 // Empty fields are ignored. 1367 if (isEmptyField(Context, FD, true)) 1368 continue; 1369 1370 // Check fields recursively. 1371 if (!shouldReturnTypeInRegister(FD->getType(), Context)) 1372 return false; 1373 } 1374 return true; 1375 } 1376 1377 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { 1378 // Treat complex types as the element type. 1379 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 1380 Ty = CTy->getElementType(); 1381 1382 // Check for a type which we know has a simple scalar argument-passing 1383 // convention without any padding. (We're specifically looking for 32 1384 // and 64-bit integer and integer-equivalents, float, and double.) 1385 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && 1386 !Ty->isEnumeralType() && !Ty->isBlockPointerType()) 1387 return false; 1388 1389 uint64_t Size = Context.getTypeSize(Ty); 1390 return Size == 32 || Size == 64; 1391 } 1392 1393 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, 1394 uint64_t &Size) { 1395 for (const auto *FD : RD->fields()) { 1396 // Scalar arguments on the stack get 4 byte alignment on x86. If the 1397 // argument is smaller than 32-bits, expanding the struct will create 1398 // alignment padding. 1399 if (!is32Or64BitBasicType(FD->getType(), Context)) 1400 return false; 1401 1402 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know 1403 // how to expand them yet, and the predicate for telling if a bitfield still 1404 // counts as "basic" is more complicated than what we were doing previously. 1405 if (FD->isBitField()) 1406 return false; 1407 1408 Size += Context.getTypeSize(FD->getType()); 1409 } 1410 return true; 1411 } 1412 1413 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, 1414 uint64_t &Size) { 1415 // Don't do this if there are any non-empty bases. 1416 for (const CXXBaseSpecifier &Base : RD->bases()) { 1417 if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), 1418 Size)) 1419 return false; 1420 } 1421 if (!addFieldSizes(Context, RD, Size)) 1422 return false; 1423 return true; 1424 } 1425 1426 /// Test whether an argument type which is to be passed indirectly (on the 1427 /// stack) would have the equivalent layout if it was expanded into separate 1428 /// arguments. If so, we prefer to do the latter to avoid inhibiting 1429 /// optimizations. 1430 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { 1431 // We can only expand structure types. 1432 const RecordType *RT = Ty->getAs<RecordType>(); 1433 if (!RT) 1434 return false; 1435 const RecordDecl *RD = RT->getDecl(); 1436 uint64_t Size = 0; 1437 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 1438 if (!IsWin32StructABI) { 1439 // On non-Windows, we have to conservatively match our old bitcode 1440 // prototypes in order to be ABI-compatible at the bitcode level. 1441 if (!CXXRD->isCLike()) 1442 return false; 1443 } else { 1444 // Don't do this for dynamic classes. 1445 if (CXXRD->isDynamicClass()) 1446 return false; 1447 } 1448 if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) 1449 return false; 1450 } else { 1451 if (!addFieldSizes(getContext(), RD, Size)) 1452 return false; 1453 } 1454 1455 // We can do this if there was no alignment padding. 1456 return Size == getContext().getTypeSize(Ty); 1457 } 1458 1459 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { 1460 // If the return value is indirect, then the hidden argument is consuming one 1461 // integer register. 1462 if (State.FreeRegs) { 1463 --State.FreeRegs; 1464 if (!IsMCUABI) 1465 return getNaturalAlignIndirectInReg(RetTy); 1466 } 1467 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 1468 } 1469 1470 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, 1471 CCState &State) const { 1472 if (RetTy->isVoidType()) 1473 return ABIArgInfo::getIgnore(); 1474 1475 const Type *Base = nullptr; 1476 uint64_t NumElts = 0; 1477 if ((State.CC == llvm::CallingConv::X86_VectorCall || 1478 State.CC == llvm::CallingConv::X86_RegCall) && 1479 isHomogeneousAggregate(RetTy, Base, NumElts)) { 1480 // The LLVM struct type for such an aggregate should lower properly. 1481 return ABIArgInfo::getDirect(); 1482 } 1483 1484 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 1485 // On Darwin, some vectors are returned in registers. 1486 if (IsDarwinVectorABI) { 1487 uint64_t Size = getContext().getTypeSize(RetTy); 1488 1489 // 128-bit vectors are a special case; they are returned in 1490 // registers and we need to make sure to pick a type the LLVM 1491 // backend will like. 1492 if (Size == 128) 1493 return ABIArgInfo::getDirect(llvm::FixedVectorType::get( 1494 llvm::Type::getInt64Ty(getVMContext()), 2)); 1495 1496 // Always return in register if it fits in a general purpose 1497 // register, or if it is 64 bits and has a single element. 1498 if ((Size == 8 || Size == 16 || Size == 32) || 1499 (Size == 64 && VT->getNumElements() == 1)) 1500 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 1501 Size)); 1502 1503 return getIndirectReturnResult(RetTy, State); 1504 } 1505 1506 return ABIArgInfo::getDirect(); 1507 } 1508 1509 if (isAggregateTypeForABI(RetTy)) { 1510 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 1511 // Structures with flexible arrays are always indirect. 1512 if (RT->getDecl()->hasFlexibleArrayMember()) 1513 return getIndirectReturnResult(RetTy, State); 1514 } 1515 1516 // If specified, structs and unions are always indirect. 1517 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) 1518 return getIndirectReturnResult(RetTy, State); 1519 1520 // Ignore empty structs/unions. 1521 if (isEmptyRecord(getContext(), RetTy, true)) 1522 return ABIArgInfo::getIgnore(); 1523 1524 // Small structures which are register sized are generally returned 1525 // in a register. 1526 if (shouldReturnTypeInRegister(RetTy, getContext())) { 1527 uint64_t Size = getContext().getTypeSize(RetTy); 1528 1529 // As a special-case, if the struct is a "single-element" struct, and 1530 // the field is of type "float" or "double", return it in a 1531 // floating-point register. (MSVC does not apply this special case.) 1532 // We apply a similar transformation for pointer types to improve the 1533 // quality of the generated IR. 1534 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 1535 if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) 1536 || SeltTy->hasPointerRepresentation()) 1537 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 1538 1539 // FIXME: We should be able to narrow this integer in cases with dead 1540 // padding. 1541 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); 1542 } 1543 1544 return getIndirectReturnResult(RetTy, State); 1545 } 1546 1547 // Treat an enum type as its underlying type. 1548 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 1549 RetTy = EnumTy->getDecl()->getIntegerType(); 1550 1551 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 1552 if (EIT->getNumBits() > 64) 1553 return getIndirectReturnResult(RetTy, State); 1554 1555 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 1556 : ABIArgInfo::getDirect()); 1557 } 1558 1559 static bool isSIMDVectorType(ASTContext &Context, QualType Ty) { 1560 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; 1561 } 1562 1563 static bool isRecordWithSIMDVectorType(ASTContext &Context, QualType Ty) { 1564 const RecordType *RT = Ty->getAs<RecordType>(); 1565 if (!RT) 1566 return 0; 1567 const RecordDecl *RD = RT->getDecl(); 1568 1569 // If this is a C++ record, check the bases first. 1570 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 1571 for (const auto &I : CXXRD->bases()) 1572 if (!isRecordWithSIMDVectorType(Context, I.getType())) 1573 return false; 1574 1575 for (const auto *i : RD->fields()) { 1576 QualType FT = i->getType(); 1577 1578 if (isSIMDVectorType(Context, FT)) 1579 return true; 1580 1581 if (isRecordWithSIMDVectorType(Context, FT)) 1582 return true; 1583 } 1584 1585 return false; 1586 } 1587 1588 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, 1589 unsigned Align) const { 1590 // Otherwise, if the alignment is less than or equal to the minimum ABI 1591 // alignment, just use the default; the backend will handle this. 1592 if (Align <= MinABIStackAlignInBytes) 1593 return 0; // Use default alignment. 1594 1595 // On non-Darwin, the stack type alignment is always 4. 1596 if (!IsDarwinVectorABI) { 1597 // Set explicit alignment, since we may need to realign the top. 1598 return MinABIStackAlignInBytes; 1599 } 1600 1601 // Otherwise, if the type contains an SSE vector type, the alignment is 16. 1602 if (Align >= 16 && (isSIMDVectorType(getContext(), Ty) || 1603 isRecordWithSIMDVectorType(getContext(), Ty))) 1604 return 16; 1605 1606 return MinABIStackAlignInBytes; 1607 } 1608 1609 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, 1610 CCState &State) const { 1611 if (!ByVal) { 1612 if (State.FreeRegs) { 1613 --State.FreeRegs; // Non-byval indirects just use one pointer. 1614 if (!IsMCUABI) 1615 return getNaturalAlignIndirectInReg(Ty); 1616 } 1617 return getNaturalAlignIndirect(Ty, false); 1618 } 1619 1620 // Compute the byval alignment. 1621 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 1622 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); 1623 if (StackAlign == 0) 1624 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); 1625 1626 // If the stack alignment is less than the type alignment, realign the 1627 // argument. 1628 bool Realign = TypeAlign > StackAlign; 1629 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), 1630 /*ByVal=*/true, Realign); 1631 } 1632 1633 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { 1634 const Type *T = isSingleElementStruct(Ty, getContext()); 1635 if (!T) 1636 T = Ty.getTypePtr(); 1637 1638 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 1639 BuiltinType::Kind K = BT->getKind(); 1640 if (K == BuiltinType::Float || K == BuiltinType::Double) 1641 return Float; 1642 } 1643 return Integer; 1644 } 1645 1646 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { 1647 if (!IsSoftFloatABI) { 1648 Class C = classify(Ty); 1649 if (C == Float) 1650 return false; 1651 } 1652 1653 unsigned Size = getContext().getTypeSize(Ty); 1654 unsigned SizeInRegs = (Size + 31) / 32; 1655 1656 if (SizeInRegs == 0) 1657 return false; 1658 1659 if (!IsMCUABI) { 1660 if (SizeInRegs > State.FreeRegs) { 1661 State.FreeRegs = 0; 1662 return false; 1663 } 1664 } else { 1665 // The MCU psABI allows passing parameters in-reg even if there are 1666 // earlier parameters that are passed on the stack. Also, 1667 // it does not allow passing >8-byte structs in-register, 1668 // even if there are 3 free registers available. 1669 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) 1670 return false; 1671 } 1672 1673 State.FreeRegs -= SizeInRegs; 1674 return true; 1675 } 1676 1677 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, 1678 bool &InReg, 1679 bool &NeedsPadding) const { 1680 // On Windows, aggregates other than HFAs are never passed in registers, and 1681 // they do not consume register slots. Homogenous floating-point aggregates 1682 // (HFAs) have already been dealt with at this point. 1683 if (IsWin32StructABI && isAggregateTypeForABI(Ty)) 1684 return false; 1685 1686 NeedsPadding = false; 1687 InReg = !IsMCUABI; 1688 1689 if (!updateFreeRegs(Ty, State)) 1690 return false; 1691 1692 if (IsMCUABI) 1693 return true; 1694 1695 if (State.CC == llvm::CallingConv::X86_FastCall || 1696 State.CC == llvm::CallingConv::X86_VectorCall || 1697 State.CC == llvm::CallingConv::X86_RegCall) { 1698 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) 1699 NeedsPadding = true; 1700 1701 return false; 1702 } 1703 1704 return true; 1705 } 1706 1707 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { 1708 if (!updateFreeRegs(Ty, State)) 1709 return false; 1710 1711 if (IsMCUABI) 1712 return false; 1713 1714 if (State.CC == llvm::CallingConv::X86_FastCall || 1715 State.CC == llvm::CallingConv::X86_VectorCall || 1716 State.CC == llvm::CallingConv::X86_RegCall) { 1717 if (getContext().getTypeSize(Ty) > 32) 1718 return false; 1719 1720 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || 1721 Ty->isReferenceType()); 1722 } 1723 1724 return true; 1725 } 1726 1727 void X86_32ABIInfo::runVectorCallFirstPass(CGFunctionInfo &FI, CCState &State) const { 1728 // Vectorcall x86 works subtly different than in x64, so the format is 1729 // a bit different than the x64 version. First, all vector types (not HVAs) 1730 // are assigned, with the first 6 ending up in the [XYZ]MM0-5 registers. 1731 // This differs from the x64 implementation, where the first 6 by INDEX get 1732 // registers. 1733 // In the second pass over the arguments, HVAs are passed in the remaining 1734 // vector registers if possible, or indirectly by address. The address will be 1735 // passed in ECX/EDX if available. Any other arguments are passed according to 1736 // the usual fastcall rules. 1737 MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments(); 1738 for (int I = 0, E = Args.size(); I < E; ++I) { 1739 const Type *Base = nullptr; 1740 uint64_t NumElts = 0; 1741 const QualType &Ty = Args[I].type; 1742 if ((Ty->isVectorType() || Ty->isBuiltinType()) && 1743 isHomogeneousAggregate(Ty, Base, NumElts)) { 1744 if (State.FreeSSERegs >= NumElts) { 1745 State.FreeSSERegs -= NumElts; 1746 Args[I].info = ABIArgInfo::getDirectInReg(); 1747 State.IsPreassigned.set(I); 1748 } 1749 } 1750 } 1751 } 1752 1753 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, 1754 CCState &State) const { 1755 // FIXME: Set alignment on indirect arguments. 1756 bool IsFastCall = State.CC == llvm::CallingConv::X86_FastCall; 1757 bool IsRegCall = State.CC == llvm::CallingConv::X86_RegCall; 1758 bool IsVectorCall = State.CC == llvm::CallingConv::X86_VectorCall; 1759 1760 Ty = useFirstFieldIfTransparentUnion(Ty); 1761 TypeInfo TI = getContext().getTypeInfo(Ty); 1762 1763 // Check with the C++ ABI first. 1764 const RecordType *RT = Ty->getAs<RecordType>(); 1765 if (RT) { 1766 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 1767 if (RAA == CGCXXABI::RAA_Indirect) { 1768 return getIndirectResult(Ty, false, State); 1769 } else if (RAA == CGCXXABI::RAA_DirectInMemory) { 1770 // The field index doesn't matter, we'll fix it up later. 1771 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); 1772 } 1773 } 1774 1775 // Regcall uses the concept of a homogenous vector aggregate, similar 1776 // to other targets. 1777 const Type *Base = nullptr; 1778 uint64_t NumElts = 0; 1779 if ((IsRegCall || IsVectorCall) && 1780 isHomogeneousAggregate(Ty, Base, NumElts)) { 1781 if (State.FreeSSERegs >= NumElts) { 1782 State.FreeSSERegs -= NumElts; 1783 1784 // Vectorcall passes HVAs directly and does not flatten them, but regcall 1785 // does. 1786 if (IsVectorCall) 1787 return getDirectX86Hva(); 1788 1789 if (Ty->isBuiltinType() || Ty->isVectorType()) 1790 return ABIArgInfo::getDirect(); 1791 return ABIArgInfo::getExpand(); 1792 } 1793 return getIndirectResult(Ty, /*ByVal=*/false, State); 1794 } 1795 1796 if (isAggregateTypeForABI(Ty)) { 1797 // Structures with flexible arrays are always indirect. 1798 // FIXME: This should not be byval! 1799 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 1800 return getIndirectResult(Ty, true, State); 1801 1802 // Ignore empty structs/unions on non-Windows. 1803 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) 1804 return ABIArgInfo::getIgnore(); 1805 1806 llvm::LLVMContext &LLVMContext = getVMContext(); 1807 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 1808 bool NeedsPadding = false; 1809 bool InReg; 1810 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { 1811 unsigned SizeInRegs = (TI.Width + 31) / 32; 1812 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); 1813 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 1814 if (InReg) 1815 return ABIArgInfo::getDirectInReg(Result); 1816 else 1817 return ABIArgInfo::getDirect(Result); 1818 } 1819 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; 1820 1821 // Pass over-aligned aggregates on Windows indirectly. This behavior was 1822 // added in MSVC 2015. 1823 if (IsWin32StructABI && TI.AlignIsRequired && TI.Align > 32) 1824 return getIndirectResult(Ty, /*ByVal=*/false, State); 1825 1826 // Expand small (<= 128-bit) record types when we know that the stack layout 1827 // of those arguments will match the struct. This is important because the 1828 // LLVM backend isn't smart enough to remove byval, which inhibits many 1829 // optimizations. 1830 // Don't do this for the MCU if there are still free integer registers 1831 // (see X86_64 ABI for full explanation). 1832 if (TI.Width <= 4 * 32 && (!IsMCUABI || State.FreeRegs == 0) && 1833 canExpandIndirectArgument(Ty)) 1834 return ABIArgInfo::getExpandWithPadding( 1835 IsFastCall || IsVectorCall || IsRegCall, PaddingType); 1836 1837 return getIndirectResult(Ty, true, State); 1838 } 1839 1840 if (const VectorType *VT = Ty->getAs<VectorType>()) { 1841 // On Windows, vectors are passed directly if registers are available, or 1842 // indirectly if not. This avoids the need to align argument memory. Pass 1843 // user-defined vector types larger than 512 bits indirectly for simplicity. 1844 if (IsWin32StructABI) { 1845 if (TI.Width <= 512 && State.FreeSSERegs > 0) { 1846 --State.FreeSSERegs; 1847 return ABIArgInfo::getDirectInReg(); 1848 } 1849 return getIndirectResult(Ty, /*ByVal=*/false, State); 1850 } 1851 1852 // On Darwin, some vectors are passed in memory, we handle this by passing 1853 // it as an i8/i16/i32/i64. 1854 if (IsDarwinVectorABI) { 1855 if ((TI.Width == 8 || TI.Width == 16 || TI.Width == 32) || 1856 (TI.Width == 64 && VT->getNumElements() == 1)) 1857 return ABIArgInfo::getDirect( 1858 llvm::IntegerType::get(getVMContext(), TI.Width)); 1859 } 1860 1861 if (IsX86_MMXType(CGT.ConvertType(Ty))) 1862 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); 1863 1864 return ABIArgInfo::getDirect(); 1865 } 1866 1867 1868 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 1869 Ty = EnumTy->getDecl()->getIntegerType(); 1870 1871 bool InReg = shouldPrimitiveUseInReg(Ty, State); 1872 1873 if (isPromotableIntegerTypeForABI(Ty)) { 1874 if (InReg) 1875 return ABIArgInfo::getExtendInReg(Ty); 1876 return ABIArgInfo::getExtend(Ty); 1877 } 1878 1879 if (const auto * EIT = Ty->getAs<ExtIntType>()) { 1880 if (EIT->getNumBits() <= 64) { 1881 if (InReg) 1882 return ABIArgInfo::getDirectInReg(); 1883 return ABIArgInfo::getDirect(); 1884 } 1885 return getIndirectResult(Ty, /*ByVal=*/false, State); 1886 } 1887 1888 if (InReg) 1889 return ABIArgInfo::getDirectInReg(); 1890 return ABIArgInfo::getDirect(); 1891 } 1892 1893 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { 1894 CCState State(FI); 1895 if (IsMCUABI) 1896 State.FreeRegs = 3; 1897 else if (State.CC == llvm::CallingConv::X86_FastCall) { 1898 State.FreeRegs = 2; 1899 State.FreeSSERegs = 3; 1900 } else if (State.CC == llvm::CallingConv::X86_VectorCall) { 1901 State.FreeRegs = 2; 1902 State.FreeSSERegs = 6; 1903 } else if (FI.getHasRegParm()) 1904 State.FreeRegs = FI.getRegParm(); 1905 else if (State.CC == llvm::CallingConv::X86_RegCall) { 1906 State.FreeRegs = 5; 1907 State.FreeSSERegs = 8; 1908 } else if (IsWin32StructABI) { 1909 // Since MSVC 2015, the first three SSE vectors have been passed in 1910 // registers. The rest are passed indirectly. 1911 State.FreeRegs = DefaultNumRegisterParameters; 1912 State.FreeSSERegs = 3; 1913 } else 1914 State.FreeRegs = DefaultNumRegisterParameters; 1915 1916 if (!::classifyReturnType(getCXXABI(), FI, *this)) { 1917 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); 1918 } else if (FI.getReturnInfo().isIndirect()) { 1919 // The C++ ABI is not aware of register usage, so we have to check if the 1920 // return value was sret and put it in a register ourselves if appropriate. 1921 if (State.FreeRegs) { 1922 --State.FreeRegs; // The sret parameter consumes a register. 1923 if (!IsMCUABI) 1924 FI.getReturnInfo().setInReg(true); 1925 } 1926 } 1927 1928 // The chain argument effectively gives us another free register. 1929 if (FI.isChainCall()) 1930 ++State.FreeRegs; 1931 1932 // For vectorcall, do a first pass over the arguments, assigning FP and vector 1933 // arguments to XMM registers as available. 1934 if (State.CC == llvm::CallingConv::X86_VectorCall) 1935 runVectorCallFirstPass(FI, State); 1936 1937 bool UsedInAlloca = false; 1938 MutableArrayRef<CGFunctionInfoArgInfo> Args = FI.arguments(); 1939 for (int I = 0, E = Args.size(); I < E; ++I) { 1940 // Skip arguments that have already been assigned. 1941 if (State.IsPreassigned.test(I)) 1942 continue; 1943 1944 Args[I].info = classifyArgumentType(Args[I].type, State); 1945 UsedInAlloca |= (Args[I].info.getKind() == ABIArgInfo::InAlloca); 1946 } 1947 1948 // If we needed to use inalloca for any argument, do a second pass and rewrite 1949 // all the memory arguments to use inalloca. 1950 if (UsedInAlloca) 1951 rewriteWithInAlloca(FI); 1952 } 1953 1954 void 1955 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 1956 CharUnits &StackOffset, ABIArgInfo &Info, 1957 QualType Type) const { 1958 // Arguments are always 4-byte-aligned. 1959 CharUnits WordSize = CharUnits::fromQuantity(4); 1960 assert(StackOffset.isMultipleOf(WordSize) && "unaligned inalloca struct"); 1961 1962 // sret pointers and indirect things will require an extra pointer 1963 // indirection, unless they are byval. Most things are byval, and will not 1964 // require this indirection. 1965 bool IsIndirect = false; 1966 if (Info.isIndirect() && !Info.getIndirectByVal()) 1967 IsIndirect = true; 1968 Info = ABIArgInfo::getInAlloca(FrameFields.size(), IsIndirect); 1969 llvm::Type *LLTy = CGT.ConvertTypeForMem(Type); 1970 if (IsIndirect) 1971 LLTy = LLTy->getPointerTo(0); 1972 FrameFields.push_back(LLTy); 1973 StackOffset += IsIndirect ? WordSize : getContext().getTypeSizeInChars(Type); 1974 1975 // Insert padding bytes to respect alignment. 1976 CharUnits FieldEnd = StackOffset; 1977 StackOffset = FieldEnd.alignTo(WordSize); 1978 if (StackOffset != FieldEnd) { 1979 CharUnits NumBytes = StackOffset - FieldEnd; 1980 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); 1981 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); 1982 FrameFields.push_back(Ty); 1983 } 1984 } 1985 1986 static bool isArgInAlloca(const ABIArgInfo &Info) { 1987 // Leave ignored and inreg arguments alone. 1988 switch (Info.getKind()) { 1989 case ABIArgInfo::InAlloca: 1990 return true; 1991 case ABIArgInfo::Ignore: 1992 return false; 1993 case ABIArgInfo::Indirect: 1994 case ABIArgInfo::Direct: 1995 case ABIArgInfo::Extend: 1996 return !Info.getInReg(); 1997 case ABIArgInfo::Expand: 1998 case ABIArgInfo::CoerceAndExpand: 1999 // These are aggregate types which are never passed in registers when 2000 // inalloca is involved. 2001 return true; 2002 } 2003 llvm_unreachable("invalid enum"); 2004 } 2005 2006 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { 2007 assert(IsWin32StructABI && "inalloca only supported on win32"); 2008 2009 // Build a packed struct type for all of the arguments in memory. 2010 SmallVector<llvm::Type *, 6> FrameFields; 2011 2012 // The stack alignment is always 4. 2013 CharUnits StackAlign = CharUnits::fromQuantity(4); 2014 2015 CharUnits StackOffset; 2016 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); 2017 2018 // Put 'this' into the struct before 'sret', if necessary. 2019 bool IsThisCall = 2020 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; 2021 ABIArgInfo &Ret = FI.getReturnInfo(); 2022 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && 2023 isArgInAlloca(I->info)) { 2024 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 2025 ++I; 2026 } 2027 2028 // Put the sret parameter into the inalloca struct if it's in memory. 2029 if (Ret.isIndirect() && !Ret.getInReg()) { 2030 addFieldToArgStruct(FrameFields, StackOffset, Ret, FI.getReturnType()); 2031 // On Windows, the hidden sret parameter is always returned in eax. 2032 Ret.setInAllocaSRet(IsWin32StructABI); 2033 } 2034 2035 // Skip the 'this' parameter in ecx. 2036 if (IsThisCall) 2037 ++I; 2038 2039 // Put arguments passed in memory into the struct. 2040 for (; I != E; ++I) { 2041 if (isArgInAlloca(I->info)) 2042 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 2043 } 2044 2045 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, 2046 /*isPacked=*/true), 2047 StackAlign); 2048 } 2049 2050 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, 2051 Address VAListAddr, QualType Ty) const { 2052 2053 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 2054 2055 // x86-32 changes the alignment of certain arguments on the stack. 2056 // 2057 // Just messing with TypeInfo like this works because we never pass 2058 // anything indirectly. 2059 TypeInfo.second = CharUnits::fromQuantity( 2060 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); 2061 2062 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, 2063 TypeInfo, CharUnits::fromQuantity(4), 2064 /*AllowHigherAlign*/ true); 2065 } 2066 2067 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( 2068 const llvm::Triple &Triple, const CodeGenOptions &Opts) { 2069 assert(Triple.getArch() == llvm::Triple::x86); 2070 2071 switch (Opts.getStructReturnConvention()) { 2072 case CodeGenOptions::SRCK_Default: 2073 break; 2074 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return 2075 return false; 2076 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return 2077 return true; 2078 } 2079 2080 if (Triple.isOSDarwin() || Triple.isOSIAMCU()) 2081 return true; 2082 2083 switch (Triple.getOS()) { 2084 case llvm::Triple::DragonFly: 2085 case llvm::Triple::FreeBSD: 2086 case llvm::Triple::OpenBSD: 2087 case llvm::Triple::Win32: 2088 return true; 2089 default: 2090 return false; 2091 } 2092 } 2093 2094 void X86_32TargetCodeGenInfo::setTargetAttributes( 2095 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 2096 if (GV->isDeclaration()) 2097 return; 2098 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2099 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 2100 llvm::Function *Fn = cast<llvm::Function>(GV); 2101 Fn->addFnAttr("stackrealign"); 2102 } 2103 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2104 llvm::Function *Fn = cast<llvm::Function>(GV); 2105 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2106 } 2107 } 2108 } 2109 2110 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( 2111 CodeGen::CodeGenFunction &CGF, 2112 llvm::Value *Address) const { 2113 CodeGen::CGBuilderTy &Builder = CGF.Builder; 2114 2115 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 2116 2117 // 0-7 are the eight integer registers; the order is different 2118 // on Darwin (for EH), but the range is the same. 2119 // 8 is %eip. 2120 AssignToArrayRange(Builder, Address, Four8, 0, 8); 2121 2122 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { 2123 // 12-16 are st(0..4). Not sure why we stop at 4. 2124 // These have size 16, which is sizeof(long double) on 2125 // platforms with 8-byte alignment for that type. 2126 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); 2127 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); 2128 2129 } else { 2130 // 9 is %eflags, which doesn't get a size on Darwin for some 2131 // reason. 2132 Builder.CreateAlignedStore( 2133 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), 2134 CharUnits::One()); 2135 2136 // 11-16 are st(0..5). Not sure why we stop at 5. 2137 // These have size 12, which is sizeof(long double) on 2138 // platforms with 4-byte alignment for that type. 2139 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); 2140 AssignToArrayRange(Builder, Address, Twelve8, 11, 16); 2141 } 2142 2143 return false; 2144 } 2145 2146 //===----------------------------------------------------------------------===// 2147 // X86-64 ABI Implementation 2148 //===----------------------------------------------------------------------===// 2149 2150 2151 namespace { 2152 /// The AVX ABI level for X86 targets. 2153 enum class X86AVXABILevel { 2154 None, 2155 AVX, 2156 AVX512 2157 }; 2158 2159 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. 2160 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { 2161 switch (AVXLevel) { 2162 case X86AVXABILevel::AVX512: 2163 return 512; 2164 case X86AVXABILevel::AVX: 2165 return 256; 2166 case X86AVXABILevel::None: 2167 return 128; 2168 } 2169 llvm_unreachable("Unknown AVXLevel"); 2170 } 2171 2172 /// X86_64ABIInfo - The X86_64 ABI information. 2173 class X86_64ABIInfo : public SwiftABIInfo { 2174 enum Class { 2175 Integer = 0, 2176 SSE, 2177 SSEUp, 2178 X87, 2179 X87Up, 2180 ComplexX87, 2181 NoClass, 2182 Memory 2183 }; 2184 2185 /// merge - Implement the X86_64 ABI merging algorithm. 2186 /// 2187 /// Merge an accumulating classification \arg Accum with a field 2188 /// classification \arg Field. 2189 /// 2190 /// \param Accum - The accumulating classification. This should 2191 /// always be either NoClass or the result of a previous merge 2192 /// call. In addition, this should never be Memory (the caller 2193 /// should just return Memory for the aggregate). 2194 static Class merge(Class Accum, Class Field); 2195 2196 /// postMerge - Implement the X86_64 ABI post merging algorithm. 2197 /// 2198 /// Post merger cleanup, reduces a malformed Hi and Lo pair to 2199 /// final MEMORY or SSE classes when necessary. 2200 /// 2201 /// \param AggregateSize - The size of the current aggregate in 2202 /// the classification process. 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 higher words of the containing object. 2209 /// 2210 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; 2211 2212 /// classify - Determine the x86_64 register classes in which the 2213 /// given type T should be passed. 2214 /// 2215 /// \param Lo - The classification for the parts of the type 2216 /// residing in the low word of the containing object. 2217 /// 2218 /// \param Hi - The classification for the parts of the type 2219 /// residing in the high word of the containing object. 2220 /// 2221 /// \param OffsetBase - The bit offset of this type in the 2222 /// containing object. Some parameters are classified different 2223 /// depending on whether they straddle an eightbyte boundary. 2224 /// 2225 /// \param isNamedArg - Whether the argument in question is a "named" 2226 /// argument, as used in AMD64-ABI 3.5.7. 2227 /// 2228 /// If a word is unused its result will be NoClass; if a type should 2229 /// be passed in Memory then at least the classification of \arg Lo 2230 /// will be Memory. 2231 /// 2232 /// The \arg Lo class will be NoClass iff the argument is ignored. 2233 /// 2234 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will 2235 /// also be ComplexX87. 2236 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, 2237 bool isNamedArg) const; 2238 2239 llvm::Type *GetByteVectorType(QualType Ty) const; 2240 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, 2241 unsigned IROffset, QualType SourceTy, 2242 unsigned SourceOffset) const; 2243 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, 2244 unsigned IROffset, QualType SourceTy, 2245 unsigned SourceOffset) const; 2246 2247 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 2248 /// such that the argument will be returned in memory. 2249 ABIArgInfo getIndirectReturnResult(QualType Ty) const; 2250 2251 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 2252 /// such that the argument will be passed in memory. 2253 /// 2254 /// \param freeIntRegs - The number of free integer registers remaining 2255 /// available. 2256 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; 2257 2258 ABIArgInfo classifyReturnType(QualType RetTy) const; 2259 2260 ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, 2261 unsigned &neededInt, unsigned &neededSSE, 2262 bool isNamedArg) const; 2263 2264 ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, 2265 unsigned &NeededSSE) const; 2266 2267 ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, 2268 unsigned &NeededSSE) const; 2269 2270 bool IsIllegalVectorType(QualType Ty) const; 2271 2272 /// The 0.98 ABI revision clarified a lot of ambiguities, 2273 /// unfortunately in ways that were not always consistent with 2274 /// certain previous compilers. In particular, platforms which 2275 /// required strict binary compatibility with older versions of GCC 2276 /// may need to exempt themselves. 2277 bool honorsRevision0_98() const { 2278 return !getTarget().getTriple().isOSDarwin(); 2279 } 2280 2281 /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to 2282 /// classify it as INTEGER (for compatibility with older clang compilers). 2283 bool classifyIntegerMMXAsSSE() const { 2284 // Clang <= 3.8 did not do this. 2285 if (getContext().getLangOpts().getClangABICompat() <= 2286 LangOptions::ClangABI::Ver3_8) 2287 return false; 2288 2289 const llvm::Triple &Triple = getTarget().getTriple(); 2290 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) 2291 return false; 2292 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) 2293 return false; 2294 return true; 2295 } 2296 2297 // GCC classifies vectors of __int128 as memory. 2298 bool passInt128VectorsInMem() const { 2299 // Clang <= 9.0 did not do this. 2300 if (getContext().getLangOpts().getClangABICompat() <= 2301 LangOptions::ClangABI::Ver9) 2302 return false; 2303 2304 const llvm::Triple &T = getTarget().getTriple(); 2305 return T.isOSLinux() || T.isOSNetBSD(); 2306 } 2307 2308 X86AVXABILevel AVXLevel; 2309 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on 2310 // 64-bit hardware. 2311 bool Has64BitPointers; 2312 2313 public: 2314 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : 2315 SwiftABIInfo(CGT), AVXLevel(AVXLevel), 2316 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { 2317 } 2318 2319 bool isPassedUsingAVXType(QualType type) const { 2320 unsigned neededInt, neededSSE; 2321 // The freeIntRegs argument doesn't matter here. 2322 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, 2323 /*isNamedArg*/true); 2324 if (info.isDirect()) { 2325 llvm::Type *ty = info.getCoerceToType(); 2326 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) 2327 return vectorTy->getPrimitiveSizeInBits().getFixedSize() > 128; 2328 } 2329 return false; 2330 } 2331 2332 void computeInfo(CGFunctionInfo &FI) const override; 2333 2334 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 2335 QualType Ty) const override; 2336 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 2337 QualType Ty) const override; 2338 2339 bool has64BitPointers() const { 2340 return Has64BitPointers; 2341 } 2342 2343 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 2344 bool asReturnValue) const override { 2345 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 2346 } 2347 bool isSwiftErrorInRegister() const override { 2348 return true; 2349 } 2350 }; 2351 2352 /// WinX86_64ABIInfo - The Windows X86_64 ABI information. 2353 class WinX86_64ABIInfo : public SwiftABIInfo { 2354 public: 2355 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) 2356 : SwiftABIInfo(CGT), AVXLevel(AVXLevel), 2357 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} 2358 2359 void computeInfo(CGFunctionInfo &FI) const override; 2360 2361 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 2362 QualType Ty) const override; 2363 2364 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 2365 // FIXME: Assumes vectorcall is in use. 2366 return isX86VectorTypeForVectorCall(getContext(), Ty); 2367 } 2368 2369 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 2370 uint64_t NumMembers) const override { 2371 // FIXME: Assumes vectorcall is in use. 2372 return isX86VectorCallAggregateSmallEnough(NumMembers); 2373 } 2374 2375 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, 2376 bool asReturnValue) const override { 2377 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 2378 } 2379 2380 bool isSwiftErrorInRegister() const override { 2381 return true; 2382 } 2383 2384 private: 2385 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, 2386 bool IsVectorCall, bool IsRegCall) const; 2387 ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, 2388 const ABIArgInfo ¤t) const; 2389 void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, 2390 bool IsVectorCall, bool IsRegCall) const; 2391 2392 X86AVXABILevel AVXLevel; 2393 2394 bool IsMingw64; 2395 }; 2396 2397 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { 2398 public: 2399 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) 2400 : TargetCodeGenInfo(std::make_unique<X86_64ABIInfo>(CGT, AVXLevel)) {} 2401 2402 const X86_64ABIInfo &getABIInfo() const { 2403 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); 2404 } 2405 2406 /// Disable tail call on x86-64. The epilogue code before the tail jump blocks 2407 /// autoreleaseRV/retainRV and autoreleaseRV/unsafeClaimRV optimizations. 2408 bool markARCOptimizedReturnCallsAsNoTail() const override { return true; } 2409 2410 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 2411 return 7; 2412 } 2413 2414 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2415 llvm::Value *Address) const override { 2416 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 2417 2418 // 0-15 are the 16 integer registers. 2419 // 16 is %rip. 2420 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 2421 return false; 2422 } 2423 2424 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 2425 StringRef Constraint, 2426 llvm::Type* Ty) const override { 2427 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 2428 } 2429 2430 bool isNoProtoCallVariadic(const CallArgList &args, 2431 const FunctionNoProtoType *fnType) const override { 2432 // The default CC on x86-64 sets %al to the number of SSA 2433 // registers used, and GCC sets this when calling an unprototyped 2434 // function, so we override the default behavior. However, don't do 2435 // that when AVX types are involved: the ABI explicitly states it is 2436 // undefined, and it doesn't work in practice because of how the ABI 2437 // defines varargs anyway. 2438 if (fnType->getCallConv() == CC_C) { 2439 bool HasAVXType = false; 2440 for (CallArgList::const_iterator 2441 it = args.begin(), ie = args.end(); it != ie; ++it) { 2442 if (getABIInfo().isPassedUsingAVXType(it->Ty)) { 2443 HasAVXType = true; 2444 break; 2445 } 2446 } 2447 2448 if (!HasAVXType) 2449 return true; 2450 } 2451 2452 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); 2453 } 2454 2455 llvm::Constant * 2456 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 2457 unsigned Sig = (0xeb << 0) | // jmp rel8 2458 (0x06 << 8) | // .+0x08 2459 ('v' << 16) | 2460 ('2' << 24); 2461 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 2462 } 2463 2464 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2465 CodeGen::CodeGenModule &CGM) const override { 2466 if (GV->isDeclaration()) 2467 return; 2468 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2469 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 2470 llvm::Function *Fn = cast<llvm::Function>(GV); 2471 Fn->addFnAttr("stackrealign"); 2472 } 2473 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2474 llvm::Function *Fn = cast<llvm::Function>(GV); 2475 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2476 } 2477 } 2478 } 2479 2480 void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc, 2481 const FunctionDecl *Caller, 2482 const FunctionDecl *Callee, 2483 const CallArgList &Args) const override; 2484 }; 2485 2486 static void initFeatureMaps(const ASTContext &Ctx, 2487 llvm::StringMap<bool> &CallerMap, 2488 const FunctionDecl *Caller, 2489 llvm::StringMap<bool> &CalleeMap, 2490 const FunctionDecl *Callee) { 2491 if (CalleeMap.empty() && CallerMap.empty()) { 2492 // The caller is potentially nullptr in the case where the call isn't in a 2493 // function. In this case, the getFunctionFeatureMap ensures we just get 2494 // the TU level setting (since it cannot be modified by 'target'.. 2495 Ctx.getFunctionFeatureMap(CallerMap, Caller); 2496 Ctx.getFunctionFeatureMap(CalleeMap, Callee); 2497 } 2498 } 2499 2500 static bool checkAVXParamFeature(DiagnosticsEngine &Diag, 2501 SourceLocation CallLoc, 2502 const llvm::StringMap<bool> &CallerMap, 2503 const llvm::StringMap<bool> &CalleeMap, 2504 QualType Ty, StringRef Feature, 2505 bool IsArgument) { 2506 bool CallerHasFeat = CallerMap.lookup(Feature); 2507 bool CalleeHasFeat = CalleeMap.lookup(Feature); 2508 if (!CallerHasFeat && !CalleeHasFeat) 2509 return Diag.Report(CallLoc, diag::warn_avx_calling_convention) 2510 << IsArgument << Ty << Feature; 2511 2512 // Mixing calling conventions here is very clearly an error. 2513 if (!CallerHasFeat || !CalleeHasFeat) 2514 return Diag.Report(CallLoc, diag::err_avx_calling_convention) 2515 << IsArgument << Ty << Feature; 2516 2517 // Else, both caller and callee have the required feature, so there is no need 2518 // to diagnose. 2519 return false; 2520 } 2521 2522 static bool checkAVXParam(DiagnosticsEngine &Diag, ASTContext &Ctx, 2523 SourceLocation CallLoc, 2524 const llvm::StringMap<bool> &CallerMap, 2525 const llvm::StringMap<bool> &CalleeMap, QualType Ty, 2526 bool IsArgument) { 2527 uint64_t Size = Ctx.getTypeSize(Ty); 2528 if (Size > 256) 2529 return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, 2530 "avx512f", IsArgument); 2531 2532 if (Size > 128) 2533 return checkAVXParamFeature(Diag, CallLoc, CallerMap, CalleeMap, Ty, "avx", 2534 IsArgument); 2535 2536 return false; 2537 } 2538 2539 void X86_64TargetCodeGenInfo::checkFunctionCallABI( 2540 CodeGenModule &CGM, SourceLocation CallLoc, const FunctionDecl *Caller, 2541 const FunctionDecl *Callee, const CallArgList &Args) const { 2542 llvm::StringMap<bool> CallerMap; 2543 llvm::StringMap<bool> CalleeMap; 2544 unsigned ArgIndex = 0; 2545 2546 // We need to loop through the actual call arguments rather than the the 2547 // function's parameters, in case this variadic. 2548 for (const CallArg &Arg : Args) { 2549 // The "avx" feature changes how vectors >128 in size are passed. "avx512f" 2550 // additionally changes how vectors >256 in size are passed. Like GCC, we 2551 // warn when a function is called with an argument where this will change. 2552 // Unlike GCC, we also error when it is an obvious ABI mismatch, that is, 2553 // the caller and callee features are mismatched. 2554 // Unfortunately, we cannot do this diagnostic in SEMA, since the callee can 2555 // change its ABI with attribute-target after this call. 2556 if (Arg.getType()->isVectorType() && 2557 CGM.getContext().getTypeSize(Arg.getType()) > 128) { 2558 initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee); 2559 QualType Ty = Arg.getType(); 2560 // The CallArg seems to have desugared the type already, so for clearer 2561 // diagnostics, replace it with the type in the FunctionDecl if possible. 2562 if (ArgIndex < Callee->getNumParams()) 2563 Ty = Callee->getParamDecl(ArgIndex)->getType(); 2564 2565 if (checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap, 2566 CalleeMap, Ty, /*IsArgument*/ true)) 2567 return; 2568 } 2569 ++ArgIndex; 2570 } 2571 2572 // Check return always, as we don't have a good way of knowing in codegen 2573 // whether this value is used, tail-called, etc. 2574 if (Callee->getReturnType()->isVectorType() && 2575 CGM.getContext().getTypeSize(Callee->getReturnType()) > 128) { 2576 initFeatureMaps(CGM.getContext(), CallerMap, Caller, CalleeMap, Callee); 2577 checkAVXParam(CGM.getDiags(), CGM.getContext(), CallLoc, CallerMap, 2578 CalleeMap, Callee->getReturnType(), 2579 /*IsArgument*/ false); 2580 } 2581 } 2582 2583 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { 2584 // If the argument does not end in .lib, automatically add the suffix. 2585 // If the argument contains a space, enclose it in quotes. 2586 // This matches the behavior of MSVC. 2587 bool Quote = (Lib.find(" ") != StringRef::npos); 2588 std::string ArgStr = Quote ? "\"" : ""; 2589 ArgStr += Lib; 2590 if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) 2591 ArgStr += ".lib"; 2592 ArgStr += Quote ? "\"" : ""; 2593 return ArgStr; 2594 } 2595 2596 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { 2597 public: 2598 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 2599 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, 2600 unsigned NumRegisterParameters) 2601 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, 2602 Win32StructABI, NumRegisterParameters, false) {} 2603 2604 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2605 CodeGen::CodeGenModule &CGM) const override; 2606 2607 void getDependentLibraryOption(llvm::StringRef Lib, 2608 llvm::SmallString<24> &Opt) const override { 2609 Opt = "/DEFAULTLIB:"; 2610 Opt += qualifyWindowsLibrary(Lib); 2611 } 2612 2613 void getDetectMismatchOption(llvm::StringRef Name, 2614 llvm::StringRef Value, 2615 llvm::SmallString<32> &Opt) const override { 2616 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 2617 } 2618 }; 2619 2620 static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2621 CodeGen::CodeGenModule &CGM) { 2622 if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { 2623 2624 if (CGM.getCodeGenOpts().StackProbeSize != 4096) 2625 Fn->addFnAttr("stack-probe-size", 2626 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); 2627 if (CGM.getCodeGenOpts().NoStackArgProbe) 2628 Fn->addFnAttr("no-stack-arg-probe"); 2629 } 2630 } 2631 2632 void WinX86_32TargetCodeGenInfo::setTargetAttributes( 2633 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 2634 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 2635 if (GV->isDeclaration()) 2636 return; 2637 addStackProbeTargetAttributes(D, GV, CGM); 2638 } 2639 2640 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 2641 public: 2642 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 2643 X86AVXABILevel AVXLevel) 2644 : TargetCodeGenInfo(std::make_unique<WinX86_64ABIInfo>(CGT, AVXLevel)) {} 2645 2646 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2647 CodeGen::CodeGenModule &CGM) const override; 2648 2649 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 2650 return 7; 2651 } 2652 2653 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2654 llvm::Value *Address) const override { 2655 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 2656 2657 // 0-15 are the 16 integer registers. 2658 // 16 is %rip. 2659 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 2660 return false; 2661 } 2662 2663 void getDependentLibraryOption(llvm::StringRef Lib, 2664 llvm::SmallString<24> &Opt) const override { 2665 Opt = "/DEFAULTLIB:"; 2666 Opt += qualifyWindowsLibrary(Lib); 2667 } 2668 2669 void getDetectMismatchOption(llvm::StringRef Name, 2670 llvm::StringRef Value, 2671 llvm::SmallString<32> &Opt) const override { 2672 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 2673 } 2674 }; 2675 2676 void WinX86_64TargetCodeGenInfo::setTargetAttributes( 2677 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 2678 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 2679 if (GV->isDeclaration()) 2680 return; 2681 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2682 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 2683 llvm::Function *Fn = cast<llvm::Function>(GV); 2684 Fn->addFnAttr("stackrealign"); 2685 } 2686 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2687 llvm::Function *Fn = cast<llvm::Function>(GV); 2688 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2689 } 2690 } 2691 2692 addStackProbeTargetAttributes(D, GV, CGM); 2693 } 2694 } 2695 2696 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, 2697 Class &Hi) const { 2698 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: 2699 // 2700 // (a) If one of the classes is Memory, the whole argument is passed in 2701 // memory. 2702 // 2703 // (b) If X87UP is not preceded by X87, the whole argument is passed in 2704 // memory. 2705 // 2706 // (c) If the size of the aggregate exceeds two eightbytes and the first 2707 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole 2708 // argument is passed in memory. NOTE: This is necessary to keep the 2709 // ABI working for processors that don't support the __m256 type. 2710 // 2711 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. 2712 // 2713 // Some of these are enforced by the merging logic. Others can arise 2714 // only with unions; for example: 2715 // union { _Complex double; unsigned; } 2716 // 2717 // Note that clauses (b) and (c) were added in 0.98. 2718 // 2719 if (Hi == Memory) 2720 Lo = Memory; 2721 if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) 2722 Lo = Memory; 2723 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) 2724 Lo = Memory; 2725 if (Hi == SSEUp && Lo != SSE) 2726 Hi = SSE; 2727 } 2728 2729 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { 2730 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is 2731 // classified recursively so that always two fields are 2732 // considered. The resulting class is calculated according to 2733 // the classes of the fields in the eightbyte: 2734 // 2735 // (a) If both classes are equal, this is the resulting class. 2736 // 2737 // (b) If one of the classes is NO_CLASS, the resulting class is 2738 // the other class. 2739 // 2740 // (c) If one of the classes is MEMORY, the result is the MEMORY 2741 // class. 2742 // 2743 // (d) If one of the classes is INTEGER, the result is the 2744 // INTEGER. 2745 // 2746 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, 2747 // MEMORY is used as class. 2748 // 2749 // (f) Otherwise class SSE is used. 2750 2751 // Accum should never be memory (we should have returned) or 2752 // ComplexX87 (because this cannot be passed in a structure). 2753 assert((Accum != Memory && Accum != ComplexX87) && 2754 "Invalid accumulated classification during merge."); 2755 if (Accum == Field || Field == NoClass) 2756 return Accum; 2757 if (Field == Memory) 2758 return Memory; 2759 if (Accum == NoClass) 2760 return Field; 2761 if (Accum == Integer || Field == Integer) 2762 return Integer; 2763 if (Field == X87 || Field == X87Up || Field == ComplexX87 || 2764 Accum == X87 || Accum == X87Up) 2765 return Memory; 2766 return SSE; 2767 } 2768 2769 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, 2770 Class &Lo, Class &Hi, bool isNamedArg) const { 2771 // FIXME: This code can be simplified by introducing a simple value class for 2772 // Class pairs with appropriate constructor methods for the various 2773 // situations. 2774 2775 // FIXME: Some of the split computations are wrong; unaligned vectors 2776 // shouldn't be passed in registers for example, so there is no chance they 2777 // can straddle an eightbyte. Verify & simplify. 2778 2779 Lo = Hi = NoClass; 2780 2781 Class &Current = OffsetBase < 64 ? Lo : Hi; 2782 Current = Memory; 2783 2784 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 2785 BuiltinType::Kind k = BT->getKind(); 2786 2787 if (k == BuiltinType::Void) { 2788 Current = NoClass; 2789 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { 2790 Lo = Integer; 2791 Hi = Integer; 2792 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { 2793 Current = Integer; 2794 } else if (k == BuiltinType::Float || k == BuiltinType::Double) { 2795 Current = SSE; 2796 } else if (k == BuiltinType::LongDouble) { 2797 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 2798 if (LDF == &llvm::APFloat::IEEEquad()) { 2799 Lo = SSE; 2800 Hi = SSEUp; 2801 } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { 2802 Lo = X87; 2803 Hi = X87Up; 2804 } else if (LDF == &llvm::APFloat::IEEEdouble()) { 2805 Current = SSE; 2806 } else 2807 llvm_unreachable("unexpected long double representation!"); 2808 } 2809 // FIXME: _Decimal32 and _Decimal64 are SSE. 2810 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). 2811 return; 2812 } 2813 2814 if (const EnumType *ET = Ty->getAs<EnumType>()) { 2815 // Classify the underlying integer type. 2816 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); 2817 return; 2818 } 2819 2820 if (Ty->hasPointerRepresentation()) { 2821 Current = Integer; 2822 return; 2823 } 2824 2825 if (Ty->isMemberPointerType()) { 2826 if (Ty->isMemberFunctionPointerType()) { 2827 if (Has64BitPointers) { 2828 // If Has64BitPointers, this is an {i64, i64}, so classify both 2829 // Lo and Hi now. 2830 Lo = Hi = Integer; 2831 } else { 2832 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that 2833 // straddles an eightbyte boundary, Hi should be classified as well. 2834 uint64_t EB_FuncPtr = (OffsetBase) / 64; 2835 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; 2836 if (EB_FuncPtr != EB_ThisAdj) { 2837 Lo = Hi = Integer; 2838 } else { 2839 Current = Integer; 2840 } 2841 } 2842 } else { 2843 Current = Integer; 2844 } 2845 return; 2846 } 2847 2848 if (const VectorType *VT = Ty->getAs<VectorType>()) { 2849 uint64_t Size = getContext().getTypeSize(VT); 2850 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { 2851 // gcc passes the following as integer: 2852 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> 2853 // 2 bytes - <2 x char>, <1 x short> 2854 // 1 byte - <1 x char> 2855 Current = Integer; 2856 2857 // If this type crosses an eightbyte boundary, it should be 2858 // split. 2859 uint64_t EB_Lo = (OffsetBase) / 64; 2860 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; 2861 if (EB_Lo != EB_Hi) 2862 Hi = Lo; 2863 } else if (Size == 64) { 2864 QualType ElementType = VT->getElementType(); 2865 2866 // gcc passes <1 x double> in memory. :( 2867 if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) 2868 return; 2869 2870 // gcc passes <1 x long long> as SSE but clang used to unconditionally 2871 // pass them as integer. For platforms where clang is the de facto 2872 // platform compiler, we must continue to use integer. 2873 if (!classifyIntegerMMXAsSSE() && 2874 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || 2875 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || 2876 ElementType->isSpecificBuiltinType(BuiltinType::Long) || 2877 ElementType->isSpecificBuiltinType(BuiltinType::ULong))) 2878 Current = Integer; 2879 else 2880 Current = SSE; 2881 2882 // If this type crosses an eightbyte boundary, it should be 2883 // split. 2884 if (OffsetBase && OffsetBase != 64) 2885 Hi = Lo; 2886 } else if (Size == 128 || 2887 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { 2888 QualType ElementType = VT->getElementType(); 2889 2890 // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :( 2891 if (passInt128VectorsInMem() && Size != 128 && 2892 (ElementType->isSpecificBuiltinType(BuiltinType::Int128) || 2893 ElementType->isSpecificBuiltinType(BuiltinType::UInt128))) 2894 return; 2895 2896 // Arguments of 256-bits are split into four eightbyte chunks. The 2897 // least significant one belongs to class SSE and all the others to class 2898 // SSEUP. The original Lo and Hi design considers that types can't be 2899 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. 2900 // This design isn't correct for 256-bits, but since there're no cases 2901 // where the upper parts would need to be inspected, avoid adding 2902 // complexity and just consider Hi to match the 64-256 part. 2903 // 2904 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in 2905 // registers if they are "named", i.e. not part of the "..." of a 2906 // variadic function. 2907 // 2908 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are 2909 // split into eight eightbyte chunks, one SSE and seven SSEUP. 2910 Lo = SSE; 2911 Hi = SSEUp; 2912 } 2913 return; 2914 } 2915 2916 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 2917 QualType ET = getContext().getCanonicalType(CT->getElementType()); 2918 2919 uint64_t Size = getContext().getTypeSize(Ty); 2920 if (ET->isIntegralOrEnumerationType()) { 2921 if (Size <= 64) 2922 Current = Integer; 2923 else if (Size <= 128) 2924 Lo = Hi = Integer; 2925 } else if (ET == getContext().FloatTy) { 2926 Current = SSE; 2927 } else if (ET == getContext().DoubleTy) { 2928 Lo = Hi = SSE; 2929 } else if (ET == getContext().LongDoubleTy) { 2930 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 2931 if (LDF == &llvm::APFloat::IEEEquad()) 2932 Current = Memory; 2933 else if (LDF == &llvm::APFloat::x87DoubleExtended()) 2934 Current = ComplexX87; 2935 else if (LDF == &llvm::APFloat::IEEEdouble()) 2936 Lo = Hi = SSE; 2937 else 2938 llvm_unreachable("unexpected long double representation!"); 2939 } 2940 2941 // If this complex type crosses an eightbyte boundary then it 2942 // should be split. 2943 uint64_t EB_Real = (OffsetBase) / 64; 2944 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; 2945 if (Hi == NoClass && EB_Real != EB_Imag) 2946 Hi = Lo; 2947 2948 return; 2949 } 2950 2951 if (const auto *EITy = Ty->getAs<ExtIntType>()) { 2952 if (EITy->getNumBits() <= 64) 2953 Current = Integer; 2954 else if (EITy->getNumBits() <= 128) 2955 Lo = Hi = Integer; 2956 // Larger values need to get passed in memory. 2957 return; 2958 } 2959 2960 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 2961 // Arrays are treated like structures. 2962 2963 uint64_t Size = getContext().getTypeSize(Ty); 2964 2965 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 2966 // than eight eightbytes, ..., it has class MEMORY. 2967 if (Size > 512) 2968 return; 2969 2970 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned 2971 // fields, it has class MEMORY. 2972 // 2973 // Only need to check alignment of array base. 2974 if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) 2975 return; 2976 2977 // Otherwise implement simplified merge. We could be smarter about 2978 // this, but it isn't worth it and would be harder to verify. 2979 Current = NoClass; 2980 uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); 2981 uint64_t ArraySize = AT->getSize().getZExtValue(); 2982 2983 // The only case a 256-bit wide vector could be used is when the array 2984 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 2985 // to work for sizes wider than 128, early check and fallback to memory. 2986 // 2987 if (Size > 128 && 2988 (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) 2989 return; 2990 2991 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { 2992 Class FieldLo, FieldHi; 2993 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); 2994 Lo = merge(Lo, FieldLo); 2995 Hi = merge(Hi, FieldHi); 2996 if (Lo == Memory || Hi == Memory) 2997 break; 2998 } 2999 3000 postMerge(Size, Lo, Hi); 3001 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); 3002 return; 3003 } 3004 3005 if (const RecordType *RT = Ty->getAs<RecordType>()) { 3006 uint64_t Size = getContext().getTypeSize(Ty); 3007 3008 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 3009 // than eight eightbytes, ..., it has class MEMORY. 3010 if (Size > 512) 3011 return; 3012 3013 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial 3014 // copy constructor or a non-trivial destructor, it is passed by invisible 3015 // reference. 3016 if (getRecordArgABI(RT, getCXXABI())) 3017 return; 3018 3019 const RecordDecl *RD = RT->getDecl(); 3020 3021 // Assume variable sized types are passed in memory. 3022 if (RD->hasFlexibleArrayMember()) 3023 return; 3024 3025 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 3026 3027 // Reset Lo class, this will be recomputed. 3028 Current = NoClass; 3029 3030 // If this is a C++ record, classify the bases first. 3031 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 3032 for (const auto &I : CXXRD->bases()) { 3033 assert(!I.isVirtual() && !I.getType()->isDependentType() && 3034 "Unexpected base class!"); 3035 const auto *Base = 3036 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); 3037 3038 // Classify this field. 3039 // 3040 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a 3041 // single eightbyte, each is classified separately. Each eightbyte gets 3042 // initialized to class NO_CLASS. 3043 Class FieldLo, FieldHi; 3044 uint64_t Offset = 3045 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); 3046 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); 3047 Lo = merge(Lo, FieldLo); 3048 Hi = merge(Hi, FieldHi); 3049 if (Lo == Memory || Hi == Memory) { 3050 postMerge(Size, Lo, Hi); 3051 return; 3052 } 3053 } 3054 } 3055 3056 // Classify the fields one at a time, merging the results. 3057 unsigned idx = 0; 3058 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 3059 i != e; ++i, ++idx) { 3060 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 3061 bool BitField = i->isBitField(); 3062 3063 // Ignore padding bit-fields. 3064 if (BitField && i->isUnnamedBitfield()) 3065 continue; 3066 3067 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than 3068 // four eightbytes, or it contains unaligned fields, it has class MEMORY. 3069 // 3070 // The only case a 256-bit wide vector could be used is when the struct 3071 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 3072 // to work for sizes wider than 128, early check and fallback to memory. 3073 // 3074 if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || 3075 Size > getNativeVectorSizeForAVXABI(AVXLevel))) { 3076 Lo = Memory; 3077 postMerge(Size, Lo, Hi); 3078 return; 3079 } 3080 // Note, skip this test for bit-fields, see below. 3081 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { 3082 Lo = Memory; 3083 postMerge(Size, Lo, Hi); 3084 return; 3085 } 3086 3087 // Classify this field. 3088 // 3089 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate 3090 // exceeds a single eightbyte, each is classified 3091 // separately. Each eightbyte gets initialized to class 3092 // NO_CLASS. 3093 Class FieldLo, FieldHi; 3094 3095 // Bit-fields require special handling, they do not force the 3096 // structure to be passed in memory even if unaligned, and 3097 // therefore they can straddle an eightbyte. 3098 if (BitField) { 3099 assert(!i->isUnnamedBitfield()); 3100 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 3101 uint64_t Size = i->getBitWidthValue(getContext()); 3102 3103 uint64_t EB_Lo = Offset / 64; 3104 uint64_t EB_Hi = (Offset + Size - 1) / 64; 3105 3106 if (EB_Lo) { 3107 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); 3108 FieldLo = NoClass; 3109 FieldHi = Integer; 3110 } else { 3111 FieldLo = Integer; 3112 FieldHi = EB_Hi ? Integer : NoClass; 3113 } 3114 } else 3115 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); 3116 Lo = merge(Lo, FieldLo); 3117 Hi = merge(Hi, FieldHi); 3118 if (Lo == Memory || Hi == Memory) 3119 break; 3120 } 3121 3122 postMerge(Size, Lo, Hi); 3123 } 3124 } 3125 3126 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { 3127 // If this is a scalar LLVM value then assume LLVM will pass it in the right 3128 // place naturally. 3129 if (!isAggregateTypeForABI(Ty)) { 3130 // Treat an enum type as its underlying type. 3131 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3132 Ty = EnumTy->getDecl()->getIntegerType(); 3133 3134 if (Ty->isExtIntType()) 3135 return getNaturalAlignIndirect(Ty); 3136 3137 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 3138 : ABIArgInfo::getDirect()); 3139 } 3140 3141 return getNaturalAlignIndirect(Ty); 3142 } 3143 3144 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { 3145 if (const VectorType *VecTy = Ty->getAs<VectorType>()) { 3146 uint64_t Size = getContext().getTypeSize(VecTy); 3147 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); 3148 if (Size <= 64 || Size > LargestVector) 3149 return true; 3150 QualType EltTy = VecTy->getElementType(); 3151 if (passInt128VectorsInMem() && 3152 (EltTy->isSpecificBuiltinType(BuiltinType::Int128) || 3153 EltTy->isSpecificBuiltinType(BuiltinType::UInt128))) 3154 return true; 3155 } 3156 3157 return false; 3158 } 3159 3160 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, 3161 unsigned freeIntRegs) const { 3162 // If this is a scalar LLVM value then assume LLVM will pass it in the right 3163 // place naturally. 3164 // 3165 // This assumption is optimistic, as there could be free registers available 3166 // when we need to pass this argument in memory, and LLVM could try to pass 3167 // the argument in the free register. This does not seem to happen currently, 3168 // but this code would be much safer if we could mark the argument with 3169 // 'onstack'. See PR12193. 3170 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty) && 3171 !Ty->isExtIntType()) { 3172 // Treat an enum type as its underlying type. 3173 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3174 Ty = EnumTy->getDecl()->getIntegerType(); 3175 3176 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 3177 : ABIArgInfo::getDirect()); 3178 } 3179 3180 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 3181 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 3182 3183 // Compute the byval alignment. We specify the alignment of the byval in all 3184 // cases so that the mid-level optimizer knows the alignment of the byval. 3185 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); 3186 3187 // Attempt to avoid passing indirect results using byval when possible. This 3188 // is important for good codegen. 3189 // 3190 // We do this by coercing the value into a scalar type which the backend can 3191 // handle naturally (i.e., without using byval). 3192 // 3193 // For simplicity, we currently only do this when we have exhausted all of the 3194 // free integer registers. Doing this when there are free integer registers 3195 // would require more care, as we would have to ensure that the coerced value 3196 // did not claim the unused register. That would require either reording the 3197 // arguments to the function (so that any subsequent inreg values came first), 3198 // or only doing this optimization when there were no following arguments that 3199 // might be inreg. 3200 // 3201 // We currently expect it to be rare (particularly in well written code) for 3202 // arguments to be passed on the stack when there are still free integer 3203 // registers available (this would typically imply large structs being passed 3204 // by value), so this seems like a fair tradeoff for now. 3205 // 3206 // We can revisit this if the backend grows support for 'onstack' parameter 3207 // attributes. See PR12193. 3208 if (freeIntRegs == 0) { 3209 uint64_t Size = getContext().getTypeSize(Ty); 3210 3211 // If this type fits in an eightbyte, coerce it into the matching integral 3212 // type, which will end up on the stack (with alignment 8). 3213 if (Align == 8 && Size <= 64) 3214 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 3215 Size)); 3216 } 3217 3218 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); 3219 } 3220 3221 /// The ABI specifies that a value should be passed in a full vector XMM/YMM 3222 /// register. Pick an LLVM IR type that will be passed as a vector register. 3223 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { 3224 // Wrapper structs/arrays that only contain vectors are passed just like 3225 // vectors; strip them off if present. 3226 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) 3227 Ty = QualType(InnerTy, 0); 3228 3229 llvm::Type *IRType = CGT.ConvertType(Ty); 3230 if (isa<llvm::VectorType>(IRType)) { 3231 // Don't pass vXi128 vectors in their native type, the backend can't 3232 // legalize them. 3233 if (passInt128VectorsInMem() && 3234 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy(128)) { 3235 // Use a vXi64 vector. 3236 uint64_t Size = getContext().getTypeSize(Ty); 3237 return llvm::FixedVectorType::get(llvm::Type::getInt64Ty(getVMContext()), 3238 Size / 64); 3239 } 3240 3241 return IRType; 3242 } 3243 3244 if (IRType->getTypeID() == llvm::Type::FP128TyID) 3245 return IRType; 3246 3247 // We couldn't find the preferred IR vector type for 'Ty'. 3248 uint64_t Size = getContext().getTypeSize(Ty); 3249 assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); 3250 3251 3252 // Return a LLVM IR vector type based on the size of 'Ty'. 3253 return llvm::FixedVectorType::get(llvm::Type::getDoubleTy(getVMContext()), 3254 Size / 64); 3255 } 3256 3257 /// BitsContainNoUserData - Return true if the specified [start,end) bit range 3258 /// is known to either be off the end of the specified type or being in 3259 /// alignment padding. The user type specified is known to be at most 128 bits 3260 /// in size, and have passed through X86_64ABIInfo::classify with a successful 3261 /// classification that put one of the two halves in the INTEGER class. 3262 /// 3263 /// It is conservatively correct to return false. 3264 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, 3265 unsigned EndBit, ASTContext &Context) { 3266 // If the bytes being queried are off the end of the type, there is no user 3267 // data hiding here. This handles analysis of builtins, vectors and other 3268 // types that don't contain interesting padding. 3269 unsigned TySize = (unsigned)Context.getTypeSize(Ty); 3270 if (TySize <= StartBit) 3271 return true; 3272 3273 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 3274 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); 3275 unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); 3276 3277 // Check each element to see if the element overlaps with the queried range. 3278 for (unsigned i = 0; i != NumElts; ++i) { 3279 // If the element is after the span we care about, then we're done.. 3280 unsigned EltOffset = i*EltSize; 3281 if (EltOffset >= EndBit) break; 3282 3283 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; 3284 if (!BitsContainNoUserData(AT->getElementType(), EltStart, 3285 EndBit-EltOffset, Context)) 3286 return false; 3287 } 3288 // If it overlaps no elements, then it is safe to process as padding. 3289 return true; 3290 } 3291 3292 if (const RecordType *RT = Ty->getAs<RecordType>()) { 3293 const RecordDecl *RD = RT->getDecl(); 3294 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 3295 3296 // If this is a C++ record, check the bases first. 3297 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 3298 for (const auto &I : CXXRD->bases()) { 3299 assert(!I.isVirtual() && !I.getType()->isDependentType() && 3300 "Unexpected base class!"); 3301 const auto *Base = 3302 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); 3303 3304 // If the base is after the span we care about, ignore it. 3305 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); 3306 if (BaseOffset >= EndBit) continue; 3307 3308 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; 3309 if (!BitsContainNoUserData(I.getType(), BaseStart, 3310 EndBit-BaseOffset, Context)) 3311 return false; 3312 } 3313 } 3314 3315 // Verify that no field has data that overlaps the region of interest. Yes 3316 // this could be sped up a lot by being smarter about queried fields, 3317 // however we're only looking at structs up to 16 bytes, so we don't care 3318 // much. 3319 unsigned idx = 0; 3320 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 3321 i != e; ++i, ++idx) { 3322 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); 3323 3324 // If we found a field after the region we care about, then we're done. 3325 if (FieldOffset >= EndBit) break; 3326 3327 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; 3328 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, 3329 Context)) 3330 return false; 3331 } 3332 3333 // If nothing in this record overlapped the area of interest, then we're 3334 // clean. 3335 return true; 3336 } 3337 3338 return false; 3339 } 3340 3341 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a 3342 /// float member at the specified offset. For example, {int,{float}} has a 3343 /// float at offset 4. It is conservatively correct for this routine to return 3344 /// false. 3345 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, 3346 const llvm::DataLayout &TD) { 3347 // Base case if we find a float. 3348 if (IROffset == 0 && IRType->isFloatTy()) 3349 return true; 3350 3351 // If this is a struct, recurse into the field at the specified offset. 3352 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 3353 const llvm::StructLayout *SL = TD.getStructLayout(STy); 3354 unsigned Elt = SL->getElementContainingOffset(IROffset); 3355 IROffset -= SL->getElementOffset(Elt); 3356 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); 3357 } 3358 3359 // If this is an array, recurse into the field at the specified offset. 3360 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 3361 llvm::Type *EltTy = ATy->getElementType(); 3362 unsigned EltSize = TD.getTypeAllocSize(EltTy); 3363 IROffset -= IROffset/EltSize*EltSize; 3364 return ContainsFloatAtOffset(EltTy, IROffset, TD); 3365 } 3366 3367 return false; 3368 } 3369 3370 3371 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the 3372 /// low 8 bytes of an XMM register, corresponding to the SSE class. 3373 llvm::Type *X86_64ABIInfo:: 3374 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, 3375 QualType SourceTy, unsigned SourceOffset) const { 3376 // The only three choices we have are either double, <2 x float>, or float. We 3377 // pass as float if the last 4 bytes is just padding. This happens for 3378 // structs that contain 3 floats. 3379 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, 3380 SourceOffset*8+64, getContext())) 3381 return llvm::Type::getFloatTy(getVMContext()); 3382 3383 // We want to pass as <2 x float> if the LLVM IR type contains a float at 3384 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the 3385 // case. 3386 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && 3387 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) 3388 return llvm::FixedVectorType::get(llvm::Type::getFloatTy(getVMContext()), 3389 2); 3390 3391 return llvm::Type::getDoubleTy(getVMContext()); 3392 } 3393 3394 3395 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in 3396 /// an 8-byte GPR. This means that we either have a scalar or we are talking 3397 /// about the high or low part of an up-to-16-byte struct. This routine picks 3398 /// the best LLVM IR type to represent this, which may be i64 or may be anything 3399 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, 3400 /// etc). 3401 /// 3402 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for 3403 /// the source type. IROffset is an offset in bytes into the LLVM IR type that 3404 /// the 8-byte value references. PrefType may be null. 3405 /// 3406 /// SourceTy is the source-level type for the entire argument. SourceOffset is 3407 /// an offset into this that we're processing (which is always either 0 or 8). 3408 /// 3409 llvm::Type *X86_64ABIInfo:: 3410 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, 3411 QualType SourceTy, unsigned SourceOffset) const { 3412 // If we're dealing with an un-offset LLVM IR type, then it means that we're 3413 // returning an 8-byte unit starting with it. See if we can safely use it. 3414 if (IROffset == 0) { 3415 // Pointers and int64's always fill the 8-byte unit. 3416 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || 3417 IRType->isIntegerTy(64)) 3418 return IRType; 3419 3420 // If we have a 1/2/4-byte integer, we can use it only if the rest of the 3421 // goodness in the source type is just tail padding. This is allowed to 3422 // kick in for struct {double,int} on the int, but not on 3423 // struct{double,int,int} because we wouldn't return the second int. We 3424 // have to do this analysis on the source type because we can't depend on 3425 // unions being lowered a specific way etc. 3426 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || 3427 IRType->isIntegerTy(32) || 3428 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { 3429 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : 3430 cast<llvm::IntegerType>(IRType)->getBitWidth(); 3431 3432 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, 3433 SourceOffset*8+64, getContext())) 3434 return IRType; 3435 } 3436 } 3437 3438 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 3439 // If this is a struct, recurse into the field at the specified offset. 3440 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); 3441 if (IROffset < SL->getSizeInBytes()) { 3442 unsigned FieldIdx = SL->getElementContainingOffset(IROffset); 3443 IROffset -= SL->getElementOffset(FieldIdx); 3444 3445 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, 3446 SourceTy, SourceOffset); 3447 } 3448 } 3449 3450 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 3451 llvm::Type *EltTy = ATy->getElementType(); 3452 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); 3453 unsigned EltOffset = IROffset/EltSize*EltSize; 3454 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, 3455 SourceOffset); 3456 } 3457 3458 // Okay, we don't have any better idea of what to pass, so we pass this in an 3459 // integer register that isn't too big to fit the rest of the struct. 3460 unsigned TySizeInBytes = 3461 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); 3462 3463 assert(TySizeInBytes != SourceOffset && "Empty field?"); 3464 3465 // It is always safe to classify this as an integer type up to i64 that 3466 // isn't larger than the structure. 3467 return llvm::IntegerType::get(getVMContext(), 3468 std::min(TySizeInBytes-SourceOffset, 8U)*8); 3469 } 3470 3471 3472 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally 3473 /// be used as elements of a two register pair to pass or return, return a 3474 /// first class aggregate to represent them. For example, if the low part of 3475 /// a by-value argument should be passed as i32* and the high part as float, 3476 /// return {i32*, float}. 3477 static llvm::Type * 3478 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, 3479 const llvm::DataLayout &TD) { 3480 // In order to correctly satisfy the ABI, we need to the high part to start 3481 // at offset 8. If the high and low parts we inferred are both 4-byte types 3482 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have 3483 // the second element at offset 8. Check for this: 3484 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); 3485 unsigned HiAlign = TD.getABITypeAlignment(Hi); 3486 unsigned HiStart = llvm::alignTo(LoSize, HiAlign); 3487 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); 3488 3489 // To handle this, we have to increase the size of the low part so that the 3490 // second element will start at an 8 byte offset. We can't increase the size 3491 // of the second element because it might make us access off the end of the 3492 // struct. 3493 if (HiStart != 8) { 3494 // There are usually two sorts of types the ABI generation code can produce 3495 // for the low part of a pair that aren't 8 bytes in size: float or 3496 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and 3497 // NaCl). 3498 // Promote these to a larger type. 3499 if (Lo->isFloatTy()) 3500 Lo = llvm::Type::getDoubleTy(Lo->getContext()); 3501 else { 3502 assert((Lo->isIntegerTy() || Lo->isPointerTy()) 3503 && "Invalid/unknown lo type"); 3504 Lo = llvm::Type::getInt64Ty(Lo->getContext()); 3505 } 3506 } 3507 3508 llvm::StructType *Result = llvm::StructType::get(Lo, Hi); 3509 3510 // Verify that the second element is at an 8-byte offset. 3511 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && 3512 "Invalid x86-64 argument pair!"); 3513 return Result; 3514 } 3515 3516 ABIArgInfo X86_64ABIInfo:: 3517 classifyReturnType(QualType RetTy) const { 3518 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the 3519 // classification algorithm. 3520 X86_64ABIInfo::Class Lo, Hi; 3521 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); 3522 3523 // Check some invariants. 3524 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 3525 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 3526 3527 llvm::Type *ResType = nullptr; 3528 switch (Lo) { 3529 case NoClass: 3530 if (Hi == NoClass) 3531 return ABIArgInfo::getIgnore(); 3532 // If the low part is just padding, it takes no register, leave ResType 3533 // null. 3534 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 3535 "Unknown missing lo part"); 3536 break; 3537 3538 case SSEUp: 3539 case X87Up: 3540 llvm_unreachable("Invalid classification for lo word."); 3541 3542 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via 3543 // hidden argument. 3544 case Memory: 3545 return getIndirectReturnResult(RetTy); 3546 3547 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next 3548 // available register of the sequence %rax, %rdx is used. 3549 case Integer: 3550 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 3551 3552 // If we have a sign or zero extended integer, make sure to return Extend 3553 // so that the parameter gets the right LLVM IR attributes. 3554 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 3555 // Treat an enum type as its underlying type. 3556 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 3557 RetTy = EnumTy->getDecl()->getIntegerType(); 3558 3559 if (RetTy->isIntegralOrEnumerationType() && 3560 isPromotableIntegerTypeForABI(RetTy)) 3561 return ABIArgInfo::getExtend(RetTy); 3562 } 3563 break; 3564 3565 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next 3566 // available SSE register of the sequence %xmm0, %xmm1 is used. 3567 case SSE: 3568 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 3569 break; 3570 3571 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is 3572 // returned on the X87 stack in %st0 as 80-bit x87 number. 3573 case X87: 3574 ResType = llvm::Type::getX86_FP80Ty(getVMContext()); 3575 break; 3576 3577 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real 3578 // part of the value is returned in %st0 and the imaginary part in 3579 // %st1. 3580 case ComplexX87: 3581 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); 3582 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), 3583 llvm::Type::getX86_FP80Ty(getVMContext())); 3584 break; 3585 } 3586 3587 llvm::Type *HighPart = nullptr; 3588 switch (Hi) { 3589 // Memory was handled previously and X87 should 3590 // never occur as a hi class. 3591 case Memory: 3592 case X87: 3593 llvm_unreachable("Invalid classification for hi word."); 3594 3595 case ComplexX87: // Previously handled. 3596 case NoClass: 3597 break; 3598 3599 case Integer: 3600 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3601 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3602 return ABIArgInfo::getDirect(HighPart, 8); 3603 break; 3604 case SSE: 3605 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3606 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3607 return ABIArgInfo::getDirect(HighPart, 8); 3608 break; 3609 3610 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte 3611 // is passed in the next available eightbyte chunk if the last used 3612 // vector register. 3613 // 3614 // SSEUP should always be preceded by SSE, just widen. 3615 case SSEUp: 3616 assert(Lo == SSE && "Unexpected SSEUp classification."); 3617 ResType = GetByteVectorType(RetTy); 3618 break; 3619 3620 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is 3621 // returned together with the previous X87 value in %st0. 3622 case X87Up: 3623 // If X87Up is preceded by X87, we don't need to do 3624 // anything. However, in some cases with unions it may not be 3625 // preceded by X87. In such situations we follow gcc and pass the 3626 // extra bits in an SSE reg. 3627 if (Lo != X87) { 3628 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3629 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3630 return ABIArgInfo::getDirect(HighPart, 8); 3631 } 3632 break; 3633 } 3634 3635 // If a high part was specified, merge it together with the low part. It is 3636 // known to pass in the high eightbyte of the result. We do this by forming a 3637 // first class struct aggregate with the high and low part: {low, high} 3638 if (HighPart) 3639 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 3640 3641 return ABIArgInfo::getDirect(ResType); 3642 } 3643 3644 ABIArgInfo X86_64ABIInfo::classifyArgumentType( 3645 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, 3646 bool isNamedArg) 3647 const 3648 { 3649 Ty = useFirstFieldIfTransparentUnion(Ty); 3650 3651 X86_64ABIInfo::Class Lo, Hi; 3652 classify(Ty, 0, Lo, Hi, isNamedArg); 3653 3654 // Check some invariants. 3655 // FIXME: Enforce these by construction. 3656 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 3657 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 3658 3659 neededInt = 0; 3660 neededSSE = 0; 3661 llvm::Type *ResType = nullptr; 3662 switch (Lo) { 3663 case NoClass: 3664 if (Hi == NoClass) 3665 return ABIArgInfo::getIgnore(); 3666 // If the low part is just padding, it takes no register, leave ResType 3667 // null. 3668 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 3669 "Unknown missing lo part"); 3670 break; 3671 3672 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument 3673 // on the stack. 3674 case Memory: 3675 3676 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or 3677 // COMPLEX_X87, it is passed in memory. 3678 case X87: 3679 case ComplexX87: 3680 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) 3681 ++neededInt; 3682 return getIndirectResult(Ty, freeIntRegs); 3683 3684 case SSEUp: 3685 case X87Up: 3686 llvm_unreachable("Invalid classification for lo word."); 3687 3688 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next 3689 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 3690 // and %r9 is used. 3691 case Integer: 3692 ++neededInt; 3693 3694 // Pick an 8-byte type based on the preferred type. 3695 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); 3696 3697 // If we have a sign or zero extended integer, make sure to return Extend 3698 // so that the parameter gets the right LLVM IR attributes. 3699 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 3700 // Treat an enum type as its underlying type. 3701 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3702 Ty = EnumTy->getDecl()->getIntegerType(); 3703 3704 if (Ty->isIntegralOrEnumerationType() && 3705 isPromotableIntegerTypeForABI(Ty)) 3706 return ABIArgInfo::getExtend(Ty); 3707 } 3708 3709 break; 3710 3711 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next 3712 // available SSE register is used, the registers are taken in the 3713 // order from %xmm0 to %xmm7. 3714 case SSE: { 3715 llvm::Type *IRType = CGT.ConvertType(Ty); 3716 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); 3717 ++neededSSE; 3718 break; 3719 } 3720 } 3721 3722 llvm::Type *HighPart = nullptr; 3723 switch (Hi) { 3724 // Memory was handled previously, ComplexX87 and X87 should 3725 // never occur as hi classes, and X87Up must be preceded by X87, 3726 // which is passed in memory. 3727 case Memory: 3728 case X87: 3729 case ComplexX87: 3730 llvm_unreachable("Invalid classification for hi word."); 3731 3732 case NoClass: break; 3733 3734 case Integer: 3735 ++neededInt; 3736 // Pick an 8-byte type based on the preferred type. 3737 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 3738 3739 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 3740 return ABIArgInfo::getDirect(HighPart, 8); 3741 break; 3742 3743 // X87Up generally doesn't occur here (long double is passed in 3744 // memory), except in situations involving unions. 3745 case X87Up: 3746 case SSE: 3747 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 3748 3749 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 3750 return ABIArgInfo::getDirect(HighPart, 8); 3751 3752 ++neededSSE; 3753 break; 3754 3755 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the 3756 // eightbyte is passed in the upper half of the last used SSE 3757 // register. This only happens when 128-bit vectors are passed. 3758 case SSEUp: 3759 assert(Lo == SSE && "Unexpected SSEUp classification"); 3760 ResType = GetByteVectorType(Ty); 3761 break; 3762 } 3763 3764 // If a high part was specified, merge it together with the low part. It is 3765 // known to pass in the high eightbyte of the result. We do this by forming a 3766 // first class struct aggregate with the high and low part: {low, high} 3767 if (HighPart) 3768 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 3769 3770 return ABIArgInfo::getDirect(ResType); 3771 } 3772 3773 ABIArgInfo 3774 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, 3775 unsigned &NeededSSE) const { 3776 auto RT = Ty->getAs<RecordType>(); 3777 assert(RT && "classifyRegCallStructType only valid with struct types"); 3778 3779 if (RT->getDecl()->hasFlexibleArrayMember()) 3780 return getIndirectReturnResult(Ty); 3781 3782 // Sum up bases 3783 if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 3784 if (CXXRD->isDynamicClass()) { 3785 NeededInt = NeededSSE = 0; 3786 return getIndirectReturnResult(Ty); 3787 } 3788 3789 for (const auto &I : CXXRD->bases()) 3790 if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) 3791 .isIndirect()) { 3792 NeededInt = NeededSSE = 0; 3793 return getIndirectReturnResult(Ty); 3794 } 3795 } 3796 3797 // Sum up members 3798 for (const auto *FD : RT->getDecl()->fields()) { 3799 if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { 3800 if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) 3801 .isIndirect()) { 3802 NeededInt = NeededSSE = 0; 3803 return getIndirectReturnResult(Ty); 3804 } 3805 } else { 3806 unsigned LocalNeededInt, LocalNeededSSE; 3807 if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, 3808 LocalNeededSSE, true) 3809 .isIndirect()) { 3810 NeededInt = NeededSSE = 0; 3811 return getIndirectReturnResult(Ty); 3812 } 3813 NeededInt += LocalNeededInt; 3814 NeededSSE += LocalNeededSSE; 3815 } 3816 } 3817 3818 return ABIArgInfo::getDirect(); 3819 } 3820 3821 ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, 3822 unsigned &NeededInt, 3823 unsigned &NeededSSE) const { 3824 3825 NeededInt = 0; 3826 NeededSSE = 0; 3827 3828 return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); 3829 } 3830 3831 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 3832 3833 const unsigned CallingConv = FI.getCallingConvention(); 3834 // It is possible to force Win64 calling convention on any x86_64 target by 3835 // using __attribute__((ms_abi)). In such case to correctly emit Win64 3836 // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. 3837 if (CallingConv == llvm::CallingConv::Win64) { 3838 WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel); 3839 Win64ABIInfo.computeInfo(FI); 3840 return; 3841 } 3842 3843 bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; 3844 3845 // Keep track of the number of assigned registers. 3846 unsigned FreeIntRegs = IsRegCall ? 11 : 6; 3847 unsigned FreeSSERegs = IsRegCall ? 16 : 8; 3848 unsigned NeededInt, NeededSSE; 3849 3850 if (!::classifyReturnType(getCXXABI(), FI, *this)) { 3851 if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && 3852 !FI.getReturnType()->getTypePtr()->isUnionType()) { 3853 FI.getReturnInfo() = 3854 classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); 3855 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { 3856 FreeIntRegs -= NeededInt; 3857 FreeSSERegs -= NeededSSE; 3858 } else { 3859 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); 3860 } 3861 } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>() && 3862 getContext().getCanonicalType(FI.getReturnType() 3863 ->getAs<ComplexType>() 3864 ->getElementType()) == 3865 getContext().LongDoubleTy) 3866 // Complex Long Double Type is passed in Memory when Regcall 3867 // calling convention is used. 3868 FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); 3869 else 3870 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3871 } 3872 3873 // If the return value is indirect, then the hidden argument is consuming one 3874 // integer register. 3875 if (FI.getReturnInfo().isIndirect()) 3876 --FreeIntRegs; 3877 3878 // The chain argument effectively gives us another free register. 3879 if (FI.isChainCall()) 3880 ++FreeIntRegs; 3881 3882 unsigned NumRequiredArgs = FI.getNumRequiredArgs(); 3883 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers 3884 // get assigned (in left-to-right order) for passing as follows... 3885 unsigned ArgNo = 0; 3886 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3887 it != ie; ++it, ++ArgNo) { 3888 bool IsNamedArg = ArgNo < NumRequiredArgs; 3889 3890 if (IsRegCall && it->type->isStructureOrClassType()) 3891 it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); 3892 else 3893 it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, 3894 NeededSSE, IsNamedArg); 3895 3896 // AMD64-ABI 3.2.3p3: If there are no registers available for any 3897 // eightbyte of an argument, the whole argument is passed on the 3898 // stack. If registers have already been assigned for some 3899 // eightbytes of such an argument, the assignments get reverted. 3900 if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { 3901 FreeIntRegs -= NeededInt; 3902 FreeSSERegs -= NeededSSE; 3903 } else { 3904 it->info = getIndirectResult(it->type, FreeIntRegs); 3905 } 3906 } 3907 } 3908 3909 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, 3910 Address VAListAddr, QualType Ty) { 3911 Address overflow_arg_area_p = 3912 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); 3913 llvm::Value *overflow_arg_area = 3914 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); 3915 3916 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 3917 // byte boundary if alignment needed by type exceeds 8 byte boundary. 3918 // It isn't stated explicitly in the standard, but in practice we use 3919 // alignment greater than 16 where necessary. 3920 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); 3921 if (Align > CharUnits::fromQuantity(8)) { 3922 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, 3923 Align); 3924 } 3925 3926 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. 3927 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 3928 llvm::Value *Res = 3929 CGF.Builder.CreateBitCast(overflow_arg_area, 3930 llvm::PointerType::getUnqual(LTy)); 3931 3932 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: 3933 // l->overflow_arg_area + sizeof(type). 3934 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to 3935 // an 8 byte boundary. 3936 3937 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; 3938 llvm::Value *Offset = 3939 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); 3940 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, 3941 "overflow_arg_area.next"); 3942 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); 3943 3944 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. 3945 return Address(Res, Align); 3946 } 3947 3948 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 3949 QualType Ty) const { 3950 // Assume that va_list type is correct; should be pointer to LLVM type: 3951 // struct { 3952 // i32 gp_offset; 3953 // i32 fp_offset; 3954 // i8* overflow_arg_area; 3955 // i8* reg_save_area; 3956 // }; 3957 unsigned neededInt, neededSSE; 3958 3959 Ty = getContext().getCanonicalType(Ty); 3960 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, 3961 /*isNamedArg*/false); 3962 3963 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed 3964 // in the registers. If not go to step 7. 3965 if (!neededInt && !neededSSE) 3966 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); 3967 3968 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of 3969 // general purpose registers needed to pass type and num_fp to hold 3970 // the number of floating point registers needed. 3971 3972 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into 3973 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or 3974 // l->fp_offset > 304 - num_fp * 16 go to step 7. 3975 // 3976 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of 3977 // register save space). 3978 3979 llvm::Value *InRegs = nullptr; 3980 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); 3981 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; 3982 if (neededInt) { 3983 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); 3984 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); 3985 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); 3986 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); 3987 } 3988 3989 if (neededSSE) { 3990 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); 3991 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); 3992 llvm::Value *FitsInFP = 3993 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); 3994 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); 3995 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; 3996 } 3997 3998 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 3999 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 4000 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 4001 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 4002 4003 // Emit code to load the value if it was passed in registers. 4004 4005 CGF.EmitBlock(InRegBlock); 4006 4007 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with 4008 // an offset of l->gp_offset and/or l->fp_offset. This may require 4009 // copying to a temporary location in case the parameter is passed 4010 // in different register classes or requires an alignment greater 4011 // than 8 for general purpose registers and 16 for XMM registers. 4012 // 4013 // FIXME: This really results in shameful code when we end up needing to 4014 // collect arguments from different places; often what should result in a 4015 // simple assembling of a structure from scattered addresses has many more 4016 // loads than necessary. Can we clean this up? 4017 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 4018 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( 4019 CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area"); 4020 4021 Address RegAddr = Address::invalid(); 4022 if (neededInt && neededSSE) { 4023 // FIXME: Cleanup. 4024 assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); 4025 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); 4026 Address Tmp = CGF.CreateMemTemp(Ty); 4027 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); 4028 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); 4029 llvm::Type *TyLo = ST->getElementType(0); 4030 llvm::Type *TyHi = ST->getElementType(1); 4031 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && 4032 "Unexpected ABI info for mixed regs"); 4033 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); 4034 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); 4035 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); 4036 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); 4037 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; 4038 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; 4039 4040 // Copy the first element. 4041 // FIXME: Our choice of alignment here and below is probably pessimistic. 4042 llvm::Value *V = CGF.Builder.CreateAlignedLoad( 4043 TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), 4044 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); 4045 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 4046 4047 // Copy the second element. 4048 V = CGF.Builder.CreateAlignedLoad( 4049 TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), 4050 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); 4051 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 4052 4053 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); 4054 } else if (neededInt) { 4055 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), 4056 CharUnits::fromQuantity(8)); 4057 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); 4058 4059 // Copy to a temporary if necessary to ensure the appropriate alignment. 4060 std::pair<CharUnits, CharUnits> SizeAlign = 4061 getContext().getTypeInfoInChars(Ty); 4062 uint64_t TySize = SizeAlign.first.getQuantity(); 4063 CharUnits TyAlign = SizeAlign.second; 4064 4065 // Copy into a temporary if the type is more aligned than the 4066 // register save area. 4067 if (TyAlign.getQuantity() > 8) { 4068 Address Tmp = CGF.CreateMemTemp(Ty); 4069 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); 4070 RegAddr = Tmp; 4071 } 4072 4073 } else if (neededSSE == 1) { 4074 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), 4075 CharUnits::fromQuantity(16)); 4076 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); 4077 } else { 4078 assert(neededSSE == 2 && "Invalid number of needed registers!"); 4079 // SSE registers are spaced 16 bytes apart in the register save 4080 // area, we need to collect the two eightbytes together. 4081 // The ABI isn't explicit about this, but it seems reasonable 4082 // to assume that the slots are 16-byte aligned, since the stack is 4083 // naturally 16-byte aligned and the prologue is expected to store 4084 // all the SSE registers to the RSA. 4085 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), 4086 CharUnits::fromQuantity(16)); 4087 Address RegAddrHi = 4088 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, 4089 CharUnits::fromQuantity(16)); 4090 llvm::Type *ST = AI.canHaveCoerceToType() 4091 ? AI.getCoerceToType() 4092 : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); 4093 llvm::Value *V; 4094 Address Tmp = CGF.CreateMemTemp(Ty); 4095 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); 4096 V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( 4097 RegAddrLo, ST->getStructElementType(0))); 4098 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 4099 V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( 4100 RegAddrHi, ST->getStructElementType(1))); 4101 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 4102 4103 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); 4104 } 4105 4106 // AMD64-ABI 3.5.7p5: Step 5. Set: 4107 // l->gp_offset = l->gp_offset + num_gp * 8 4108 // l->fp_offset = l->fp_offset + num_fp * 16. 4109 if (neededInt) { 4110 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); 4111 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), 4112 gp_offset_p); 4113 } 4114 if (neededSSE) { 4115 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); 4116 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), 4117 fp_offset_p); 4118 } 4119 CGF.EmitBranch(ContBlock); 4120 4121 // Emit code to load the value if it was passed in memory. 4122 4123 CGF.EmitBlock(InMemBlock); 4124 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); 4125 4126 // Return the appropriate result. 4127 4128 CGF.EmitBlock(ContBlock); 4129 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, 4130 "vaarg.addr"); 4131 return ResAddr; 4132 } 4133 4134 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 4135 QualType Ty) const { 4136 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 4137 CGF.getContext().getTypeInfoInChars(Ty), 4138 CharUnits::fromQuantity(8), 4139 /*allowHigherAlign*/ false); 4140 } 4141 4142 ABIArgInfo 4143 WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, 4144 const ABIArgInfo ¤t) const { 4145 // Assumes vectorCall calling convention. 4146 const Type *Base = nullptr; 4147 uint64_t NumElts = 0; 4148 4149 if (!Ty->isBuiltinType() && !Ty->isVectorType() && 4150 isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { 4151 FreeSSERegs -= NumElts; 4152 return getDirectX86Hva(); 4153 } 4154 return current; 4155 } 4156 4157 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, 4158 bool IsReturnType, bool IsVectorCall, 4159 bool IsRegCall) const { 4160 4161 if (Ty->isVoidType()) 4162 return ABIArgInfo::getIgnore(); 4163 4164 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4165 Ty = EnumTy->getDecl()->getIntegerType(); 4166 4167 TypeInfo Info = getContext().getTypeInfo(Ty); 4168 uint64_t Width = Info.Width; 4169 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); 4170 4171 const RecordType *RT = Ty->getAs<RecordType>(); 4172 if (RT) { 4173 if (!IsReturnType) { 4174 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) 4175 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 4176 } 4177 4178 if (RT->getDecl()->hasFlexibleArrayMember()) 4179 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 4180 4181 } 4182 4183 const Type *Base = nullptr; 4184 uint64_t NumElts = 0; 4185 // vectorcall adds the concept of a homogenous vector aggregate, similar to 4186 // other targets. 4187 if ((IsVectorCall || IsRegCall) && 4188 isHomogeneousAggregate(Ty, Base, NumElts)) { 4189 if (IsRegCall) { 4190 if (FreeSSERegs >= NumElts) { 4191 FreeSSERegs -= NumElts; 4192 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) 4193 return ABIArgInfo::getDirect(); 4194 return ABIArgInfo::getExpand(); 4195 } 4196 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4197 } else if (IsVectorCall) { 4198 if (FreeSSERegs >= NumElts && 4199 (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { 4200 FreeSSERegs -= NumElts; 4201 return ABIArgInfo::getDirect(); 4202 } else if (IsReturnType) { 4203 return ABIArgInfo::getExpand(); 4204 } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { 4205 // HVAs are delayed and reclassified in the 2nd step. 4206 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4207 } 4208 } 4209 } 4210 4211 if (Ty->isMemberPointerType()) { 4212 // If the member pointer is represented by an LLVM int or ptr, pass it 4213 // directly. 4214 llvm::Type *LLTy = CGT.ConvertType(Ty); 4215 if (LLTy->isPointerTy() || LLTy->isIntegerTy()) 4216 return ABIArgInfo::getDirect(); 4217 } 4218 4219 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { 4220 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 4221 // not 1, 2, 4, or 8 bytes, must be passed by reference." 4222 if (Width > 64 || !llvm::isPowerOf2_64(Width)) 4223 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 4224 4225 // Otherwise, coerce it to a small integer. 4226 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); 4227 } 4228 4229 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 4230 switch (BT->getKind()) { 4231 case BuiltinType::Bool: 4232 // Bool type is always extended to the ABI, other builtin types are not 4233 // extended. 4234 return ABIArgInfo::getExtend(Ty); 4235 4236 case BuiltinType::LongDouble: 4237 // Mingw64 GCC uses the old 80 bit extended precision floating point 4238 // unit. It passes them indirectly through memory. 4239 if (IsMingw64) { 4240 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 4241 if (LDF == &llvm::APFloat::x87DoubleExtended()) 4242 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4243 } 4244 break; 4245 4246 case BuiltinType::Int128: 4247 case BuiltinType::UInt128: 4248 // If it's a parameter type, the normal ABI rule is that arguments larger 4249 // than 8 bytes are passed indirectly. GCC follows it. We follow it too, 4250 // even though it isn't particularly efficient. 4251 if (!IsReturnType) 4252 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4253 4254 // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. 4255 // Clang matches them for compatibility. 4256 return ABIArgInfo::getDirect(llvm::FixedVectorType::get( 4257 llvm::Type::getInt64Ty(getVMContext()), 2)); 4258 4259 default: 4260 break; 4261 } 4262 } 4263 4264 if (Ty->isExtIntType()) { 4265 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 4266 // not 1, 2, 4, or 8 bytes, must be passed by reference." 4267 // However, non-power-of-two _ExtInts will be passed as 1,2,4 or 8 bytes 4268 // anyway as long is it fits in them, so we don't have to check the power of 4269 // 2. 4270 if (Width <= 64) 4271 return ABIArgInfo::getDirect(); 4272 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 4273 } 4274 4275 return ABIArgInfo::getDirect(); 4276 } 4277 4278 void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, 4279 unsigned FreeSSERegs, 4280 bool IsVectorCall, 4281 bool IsRegCall) const { 4282 unsigned Count = 0; 4283 for (auto &I : FI.arguments()) { 4284 // Vectorcall in x64 only permits the first 6 arguments to be passed 4285 // as XMM/YMM registers. 4286 if (Count < VectorcallMaxParamNumAsReg) 4287 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); 4288 else { 4289 // Since these cannot be passed in registers, pretend no registers 4290 // are left. 4291 unsigned ZeroSSERegsAvail = 0; 4292 I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, 4293 IsVectorCall, IsRegCall); 4294 } 4295 ++Count; 4296 } 4297 4298 for (auto &I : FI.arguments()) { 4299 I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); 4300 } 4301 } 4302 4303 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 4304 const unsigned CC = FI.getCallingConvention(); 4305 bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall; 4306 bool IsRegCall = CC == llvm::CallingConv::X86_RegCall; 4307 4308 // If __attribute__((sysv_abi)) is in use, use the SysV argument 4309 // classification rules. 4310 if (CC == llvm::CallingConv::X86_64_SysV) { 4311 X86_64ABIInfo SysVABIInfo(CGT, AVXLevel); 4312 SysVABIInfo.computeInfo(FI); 4313 return; 4314 } 4315 4316 unsigned FreeSSERegs = 0; 4317 if (IsVectorCall) { 4318 // We can use up to 4 SSE return registers with vectorcall. 4319 FreeSSERegs = 4; 4320 } else if (IsRegCall) { 4321 // RegCall gives us 16 SSE registers. 4322 FreeSSERegs = 16; 4323 } 4324 4325 if (!getCXXABI().classifyReturnType(FI)) 4326 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, 4327 IsVectorCall, IsRegCall); 4328 4329 if (IsVectorCall) { 4330 // We can use up to 6 SSE register parameters with vectorcall. 4331 FreeSSERegs = 6; 4332 } else if (IsRegCall) { 4333 // RegCall gives us 16 SSE registers, we can reuse the return registers. 4334 FreeSSERegs = 16; 4335 } 4336 4337 if (IsVectorCall) { 4338 computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); 4339 } else { 4340 for (auto &I : FI.arguments()) 4341 I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); 4342 } 4343 4344 } 4345 4346 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4347 QualType Ty) const { 4348 4349 bool IsIndirect = false; 4350 4351 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 4352 // not 1, 2, 4, or 8 bytes, must be passed by reference." 4353 if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { 4354 uint64_t Width = getContext().getTypeSize(Ty); 4355 IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); 4356 } 4357 4358 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, 4359 CGF.getContext().getTypeInfoInChars(Ty), 4360 CharUnits::fromQuantity(8), 4361 /*allowHigherAlign*/ false); 4362 } 4363 4364 static bool PPC_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4365 llvm::Value *Address, bool Is64Bit, 4366 bool IsAIX) { 4367 // This is calculated from the LLVM and GCC tables and verified 4368 // against gcc output. AFAIK all PPC ABIs use the same encoding. 4369 4370 CodeGen::CGBuilderTy &Builder = CGF.Builder; 4371 4372 llvm::IntegerType *i8 = CGF.Int8Ty; 4373 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 4374 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 4375 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 4376 4377 // 0-31: r0-31, the 4-byte or 8-byte general-purpose registers 4378 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 0, 31); 4379 4380 // 32-63: fp0-31, the 8-byte floating-point registers 4381 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 4382 4383 // 64-67 are various 4-byte or 8-byte special-purpose registers: 4384 // 64: mq 4385 // 65: lr 4386 // 66: ctr 4387 // 67: ap 4388 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 64, 67); 4389 4390 // 68-76 are various 4-byte special-purpose registers: 4391 // 68-75 cr0-7 4392 // 76: xer 4393 AssignToArrayRange(Builder, Address, Four8, 68, 76); 4394 4395 // 77-108: v0-31, the 16-byte vector registers 4396 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 4397 4398 // 109: vrsave 4399 // 110: vscr 4400 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 109, 110); 4401 4402 // AIX does not utilize the rest of the registers. 4403 if (IsAIX) 4404 return false; 4405 4406 // 111: spe_acc 4407 // 112: spefscr 4408 // 113: sfp 4409 AssignToArrayRange(Builder, Address, Is64Bit ? Eight8 : Four8, 111, 113); 4410 4411 if (!Is64Bit) 4412 return false; 4413 4414 // TODO: Need to verify if these registers are used on 64 bit AIX with Power8 4415 // or above CPU. 4416 // 64-bit only registers: 4417 // 114: tfhar 4418 // 115: tfiar 4419 // 116: texasr 4420 AssignToArrayRange(Builder, Address, Eight8, 114, 116); 4421 4422 return false; 4423 } 4424 4425 // AIX 4426 namespace { 4427 /// AIXABIInfo - The AIX XCOFF ABI information. 4428 class AIXABIInfo : public ABIInfo { 4429 const bool Is64Bit; 4430 const unsigned PtrByteSize; 4431 CharUnits getParamTypeAlignment(QualType Ty) const; 4432 4433 public: 4434 AIXABIInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit) 4435 : ABIInfo(CGT), Is64Bit(Is64Bit), PtrByteSize(Is64Bit ? 8 : 4) {} 4436 4437 bool isPromotableTypeForABI(QualType Ty) const; 4438 4439 ABIArgInfo classifyReturnType(QualType RetTy) const; 4440 ABIArgInfo classifyArgumentType(QualType Ty) const; 4441 4442 void computeInfo(CGFunctionInfo &FI) const override { 4443 if (!getCXXABI().classifyReturnType(FI)) 4444 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4445 4446 for (auto &I : FI.arguments()) 4447 I.info = classifyArgumentType(I.type); 4448 } 4449 4450 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4451 QualType Ty) const override; 4452 }; 4453 4454 class AIXTargetCodeGenInfo : public TargetCodeGenInfo { 4455 const bool Is64Bit; 4456 4457 public: 4458 AIXTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool Is64Bit) 4459 : TargetCodeGenInfo(std::make_unique<AIXABIInfo>(CGT, Is64Bit)), 4460 Is64Bit(Is64Bit) {} 4461 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4462 return 1; // r1 is the dedicated stack pointer 4463 } 4464 4465 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4466 llvm::Value *Address) const override; 4467 }; 4468 } // namespace 4469 4470 // Return true if the ABI requires Ty to be passed sign- or zero- 4471 // extended to 32/64 bits. 4472 bool AIXABIInfo::isPromotableTypeForABI(QualType Ty) const { 4473 // Treat an enum type as its underlying type. 4474 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4475 Ty = EnumTy->getDecl()->getIntegerType(); 4476 4477 // Promotable integer types are required to be promoted by the ABI. 4478 if (Ty->isPromotableIntegerType()) 4479 return true; 4480 4481 if (!Is64Bit) 4482 return false; 4483 4484 // For 64 bit mode, in addition to the usual promotable integer types, we also 4485 // need to extend all 32-bit types, since the ABI requires promotion to 64 4486 // bits. 4487 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 4488 switch (BT->getKind()) { 4489 case BuiltinType::Int: 4490 case BuiltinType::UInt: 4491 return true; 4492 default: 4493 break; 4494 } 4495 4496 return false; 4497 } 4498 4499 ABIArgInfo AIXABIInfo::classifyReturnType(QualType RetTy) const { 4500 if (RetTy->isAnyComplexType()) 4501 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4502 4503 if (RetTy->isVectorType()) 4504 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4505 4506 if (RetTy->isVoidType()) 4507 return ABIArgInfo::getIgnore(); 4508 4509 // TODO: Evaluate if AIX power alignment rule would have an impact on the 4510 // alignment here. 4511 if (isAggregateTypeForABI(RetTy)) 4512 return getNaturalAlignIndirect(RetTy); 4513 4514 return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 4515 : ABIArgInfo::getDirect()); 4516 } 4517 4518 ABIArgInfo AIXABIInfo::classifyArgumentType(QualType Ty) const { 4519 Ty = useFirstFieldIfTransparentUnion(Ty); 4520 4521 if (Ty->isAnyComplexType()) 4522 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4523 4524 if (Ty->isVectorType()) 4525 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4526 4527 // TODO: Evaluate if AIX power alignment rule would have an impact on the 4528 // alignment here. 4529 if (isAggregateTypeForABI(Ty)) { 4530 // Records with non-trivial destructors/copy-constructors should not be 4531 // passed by value. 4532 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 4533 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 4534 4535 CharUnits CCAlign = getParamTypeAlignment(Ty); 4536 CharUnits TyAlign = getContext().getTypeAlignInChars(Ty); 4537 4538 return ABIArgInfo::getIndirect(CCAlign, /*ByVal*/ true, 4539 /*Realign*/ TyAlign > CCAlign); 4540 } 4541 4542 return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 4543 : ABIArgInfo::getDirect()); 4544 } 4545 4546 CharUnits AIXABIInfo::getParamTypeAlignment(QualType Ty) const { 4547 if (Ty->isAnyComplexType()) 4548 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4549 4550 if (Ty->isVectorType()) 4551 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4552 4553 // If the structure contains a vector type, the alignment is 16. 4554 if (isRecordWithSIMDVectorType(getContext(), Ty)) 4555 return CharUnits::fromQuantity(16); 4556 4557 return CharUnits::fromQuantity(PtrByteSize); 4558 } 4559 4560 Address AIXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4561 QualType Ty) const { 4562 if (Ty->isAnyComplexType()) 4563 llvm::report_fatal_error("complex type is not supported on AIX yet"); 4564 4565 if (Ty->isVectorType()) 4566 llvm::report_fatal_error("vector type is not supported on AIX yet"); 4567 4568 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 4569 TypeInfo.second = getParamTypeAlignment(Ty); 4570 4571 CharUnits SlotSize = CharUnits::fromQuantity(PtrByteSize); 4572 4573 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, TypeInfo, 4574 SlotSize, /*AllowHigher*/ true); 4575 } 4576 4577 bool AIXTargetCodeGenInfo::initDwarfEHRegSizeTable( 4578 CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const { 4579 return PPC_initDwarfEHRegSizeTable(CGF, Address, Is64Bit, /*IsAIX*/ true); 4580 } 4581 4582 // PowerPC-32 4583 namespace { 4584 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. 4585 class PPC32_SVR4_ABIInfo : public DefaultABIInfo { 4586 bool IsSoftFloatABI; 4587 bool IsRetSmallStructInRegABI; 4588 4589 CharUnits getParamTypeAlignment(QualType Ty) const; 4590 4591 public: 4592 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI, 4593 bool RetSmallStructInRegABI) 4594 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI), 4595 IsRetSmallStructInRegABI(RetSmallStructInRegABI) {} 4596 4597 ABIArgInfo classifyReturnType(QualType RetTy) const; 4598 4599 void computeInfo(CGFunctionInfo &FI) const override { 4600 if (!getCXXABI().classifyReturnType(FI)) 4601 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4602 for (auto &I : FI.arguments()) 4603 I.info = classifyArgumentType(I.type); 4604 } 4605 4606 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4607 QualType Ty) const override; 4608 }; 4609 4610 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { 4611 public: 4612 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI, 4613 bool RetSmallStructInRegABI) 4614 : TargetCodeGenInfo(std::make_unique<PPC32_SVR4_ABIInfo>( 4615 CGT, SoftFloatABI, RetSmallStructInRegABI)) {} 4616 4617 static bool isStructReturnInRegABI(const llvm::Triple &Triple, 4618 const CodeGenOptions &Opts); 4619 4620 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4621 // This is recovered from gcc output. 4622 return 1; // r1 is the dedicated stack pointer 4623 } 4624 4625 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4626 llvm::Value *Address) const override; 4627 }; 4628 } 4629 4630 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { 4631 // Complex types are passed just like their elements. 4632 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 4633 Ty = CTy->getElementType(); 4634 4635 if (Ty->isVectorType()) 4636 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 4637 : 4); 4638 4639 // For single-element float/vector structs, we consider the whole type 4640 // to have the same alignment requirements as its single element. 4641 const Type *AlignTy = nullptr; 4642 if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { 4643 const BuiltinType *BT = EltType->getAs<BuiltinType>(); 4644 if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || 4645 (BT && BT->isFloatingPoint())) 4646 AlignTy = EltType; 4647 } 4648 4649 if (AlignTy) 4650 return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); 4651 return CharUnits::fromQuantity(4); 4652 } 4653 4654 ABIArgInfo PPC32_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { 4655 uint64_t Size; 4656 4657 // -msvr4-struct-return puts small aggregates in GPR3 and GPR4. 4658 if (isAggregateTypeForABI(RetTy) && IsRetSmallStructInRegABI && 4659 (Size = getContext().getTypeSize(RetTy)) <= 64) { 4660 // System V ABI (1995), page 3-22, specified: 4661 // > A structure or union whose size is less than or equal to 8 bytes 4662 // > shall be returned in r3 and r4, as if it were first stored in the 4663 // > 8-byte aligned memory area and then the low addressed word were 4664 // > loaded into r3 and the high-addressed word into r4. Bits beyond 4665 // > the last member of the structure or union are not defined. 4666 // 4667 // GCC for big-endian PPC32 inserts the pad before the first member, 4668 // not "beyond the last member" of the struct. To stay compatible 4669 // with GCC, we coerce the struct to an integer of the same size. 4670 // LLVM will extend it and return i32 in r3, or i64 in r3:r4. 4671 if (Size == 0) 4672 return ABIArgInfo::getIgnore(); 4673 else { 4674 llvm::Type *CoerceTy = llvm::Type::getIntNTy(getVMContext(), Size); 4675 return ABIArgInfo::getDirect(CoerceTy); 4676 } 4677 } 4678 4679 return DefaultABIInfo::classifyReturnType(RetTy); 4680 } 4681 4682 // TODO: this implementation is now likely redundant with 4683 // DefaultABIInfo::EmitVAArg. 4684 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, 4685 QualType Ty) const { 4686 if (getTarget().getTriple().isOSDarwin()) { 4687 auto TI = getContext().getTypeInfoInChars(Ty); 4688 TI.second = getParamTypeAlignment(Ty); 4689 4690 CharUnits SlotSize = CharUnits::fromQuantity(4); 4691 return emitVoidPtrVAArg(CGF, VAList, Ty, 4692 classifyArgumentType(Ty).isIndirect(), TI, SlotSize, 4693 /*AllowHigherAlign=*/true); 4694 } 4695 4696 const unsigned OverflowLimit = 8; 4697 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 4698 // TODO: Implement this. For now ignore. 4699 (void)CTy; 4700 return Address::invalid(); // FIXME? 4701 } 4702 4703 // struct __va_list_tag { 4704 // unsigned char gpr; 4705 // unsigned char fpr; 4706 // unsigned short reserved; 4707 // void *overflow_arg_area; 4708 // void *reg_save_area; 4709 // }; 4710 4711 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; 4712 bool isInt = 4713 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); 4714 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; 4715 4716 // All aggregates are passed indirectly? That doesn't seem consistent 4717 // with the argument-lowering code. 4718 bool isIndirect = Ty->isAggregateType(); 4719 4720 CGBuilderTy &Builder = CGF.Builder; 4721 4722 // The calling convention either uses 1-2 GPRs or 1 FPR. 4723 Address NumRegsAddr = Address::invalid(); 4724 if (isInt || IsSoftFloatABI) { 4725 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr"); 4726 } else { 4727 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr"); 4728 } 4729 4730 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); 4731 4732 // "Align" the register count when TY is i64. 4733 if (isI64 || (isF64 && IsSoftFloatABI)) { 4734 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); 4735 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); 4736 } 4737 4738 llvm::Value *CC = 4739 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); 4740 4741 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); 4742 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); 4743 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 4744 4745 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); 4746 4747 llvm::Type *DirectTy = CGF.ConvertType(Ty); 4748 if (isIndirect) DirectTy = DirectTy->getPointerTo(0); 4749 4750 // Case 1: consume registers. 4751 Address RegAddr = Address::invalid(); 4752 { 4753 CGF.EmitBlock(UsingRegs); 4754 4755 Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4); 4756 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), 4757 CharUnits::fromQuantity(8)); 4758 assert(RegAddr.getElementType() == CGF.Int8Ty); 4759 4760 // Floating-point registers start after the general-purpose registers. 4761 if (!(isInt || IsSoftFloatABI)) { 4762 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, 4763 CharUnits::fromQuantity(32)); 4764 } 4765 4766 // Get the address of the saved value by scaling the number of 4767 // registers we've used by the number of 4768 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); 4769 llvm::Value *RegOffset = 4770 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); 4771 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, 4772 RegAddr.getPointer(), RegOffset), 4773 RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); 4774 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); 4775 4776 // Increase the used-register count. 4777 NumRegs = 4778 Builder.CreateAdd(NumRegs, 4779 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); 4780 Builder.CreateStore(NumRegs, NumRegsAddr); 4781 4782 CGF.EmitBranch(Cont); 4783 } 4784 4785 // Case 2: consume space in the overflow area. 4786 Address MemAddr = Address::invalid(); 4787 { 4788 CGF.EmitBlock(UsingOverflow); 4789 4790 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); 4791 4792 // Everything in the overflow area is rounded up to a size of at least 4. 4793 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); 4794 4795 CharUnits Size; 4796 if (!isIndirect) { 4797 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); 4798 Size = TypeInfo.first.alignTo(OverflowAreaAlign); 4799 } else { 4800 Size = CGF.getPointerSize(); 4801 } 4802 4803 Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3); 4804 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), 4805 OverflowAreaAlign); 4806 // Round up address of argument to alignment 4807 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); 4808 if (Align > OverflowAreaAlign) { 4809 llvm::Value *Ptr = OverflowArea.getPointer(); 4810 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), 4811 Align); 4812 } 4813 4814 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); 4815 4816 // Increase the overflow area. 4817 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); 4818 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); 4819 CGF.EmitBranch(Cont); 4820 } 4821 4822 CGF.EmitBlock(Cont); 4823 4824 // Merge the cases with a phi. 4825 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, 4826 "vaarg.addr"); 4827 4828 // Load the pointer if the argument was passed indirectly. 4829 if (isIndirect) { 4830 Result = Address(Builder.CreateLoad(Result, "aggr"), 4831 getContext().getTypeAlignInChars(Ty)); 4832 } 4833 4834 return Result; 4835 } 4836 4837 bool PPC32TargetCodeGenInfo::isStructReturnInRegABI( 4838 const llvm::Triple &Triple, const CodeGenOptions &Opts) { 4839 assert(Triple.getArch() == llvm::Triple::ppc); 4840 4841 switch (Opts.getStructReturnConvention()) { 4842 case CodeGenOptions::SRCK_Default: 4843 break; 4844 case CodeGenOptions::SRCK_OnStack: // -maix-struct-return 4845 return false; 4846 case CodeGenOptions::SRCK_InRegs: // -msvr4-struct-return 4847 return true; 4848 } 4849 4850 if (Triple.isOSBinFormatELF() && !Triple.isOSLinux()) 4851 return true; 4852 4853 return false; 4854 } 4855 4856 bool 4857 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4858 llvm::Value *Address) const { 4859 return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ false, 4860 /*IsAIX*/ false); 4861 } 4862 4863 // PowerPC-64 4864 4865 namespace { 4866 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. 4867 class PPC64_SVR4_ABIInfo : public SwiftABIInfo { 4868 public: 4869 enum ABIKind { 4870 ELFv1 = 0, 4871 ELFv2 4872 }; 4873 4874 private: 4875 static const unsigned GPRBits = 64; 4876 ABIKind Kind; 4877 bool HasQPX; 4878 bool IsSoftFloatABI; 4879 4880 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and 4881 // will be passed in a QPX register. 4882 bool IsQPXVectorTy(const Type *Ty) const { 4883 if (!HasQPX) 4884 return false; 4885 4886 if (const VectorType *VT = Ty->getAs<VectorType>()) { 4887 unsigned NumElements = VT->getNumElements(); 4888 if (NumElements == 1) 4889 return false; 4890 4891 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { 4892 if (getContext().getTypeSize(Ty) <= 256) 4893 return true; 4894 } else if (VT->getElementType()-> 4895 isSpecificBuiltinType(BuiltinType::Float)) { 4896 if (getContext().getTypeSize(Ty) <= 128) 4897 return true; 4898 } 4899 } 4900 4901 return false; 4902 } 4903 4904 bool IsQPXVectorTy(QualType Ty) const { 4905 return IsQPXVectorTy(Ty.getTypePtr()); 4906 } 4907 4908 public: 4909 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, 4910 bool SoftFloatABI) 4911 : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), 4912 IsSoftFloatABI(SoftFloatABI) {} 4913 4914 bool isPromotableTypeForABI(QualType Ty) const; 4915 CharUnits getParamTypeAlignment(QualType Ty) const; 4916 4917 ABIArgInfo classifyReturnType(QualType RetTy) const; 4918 ABIArgInfo classifyArgumentType(QualType Ty) const; 4919 4920 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 4921 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 4922 uint64_t Members) const override; 4923 4924 // TODO: We can add more logic to computeInfo to improve performance. 4925 // Example: For aggregate arguments that fit in a register, we could 4926 // use getDirectInReg (as is done below for structs containing a single 4927 // floating-point value) to avoid pushing them to memory on function 4928 // entry. This would require changing the logic in PPCISelLowering 4929 // when lowering the parameters in the caller and args in the callee. 4930 void computeInfo(CGFunctionInfo &FI) const override { 4931 if (!getCXXABI().classifyReturnType(FI)) 4932 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4933 for (auto &I : FI.arguments()) { 4934 // We rely on the default argument classification for the most part. 4935 // One exception: An aggregate containing a single floating-point 4936 // or vector item must be passed in a register if one is available. 4937 const Type *T = isSingleElementStruct(I.type, getContext()); 4938 if (T) { 4939 const BuiltinType *BT = T->getAs<BuiltinType>(); 4940 if (IsQPXVectorTy(T) || 4941 (T->isVectorType() && getContext().getTypeSize(T) == 128) || 4942 (BT && BT->isFloatingPoint())) { 4943 QualType QT(T, 0); 4944 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); 4945 continue; 4946 } 4947 } 4948 I.info = classifyArgumentType(I.type); 4949 } 4950 } 4951 4952 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4953 QualType Ty) const override; 4954 4955 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 4956 bool asReturnValue) const override { 4957 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 4958 } 4959 4960 bool isSwiftErrorInRegister() const override { 4961 return false; 4962 } 4963 }; 4964 4965 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { 4966 4967 public: 4968 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, 4969 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, 4970 bool SoftFloatABI) 4971 : TargetCodeGenInfo(std::make_unique<PPC64_SVR4_ABIInfo>( 4972 CGT, Kind, HasQPX, SoftFloatABI)) {} 4973 4974 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4975 // This is recovered from gcc output. 4976 return 1; // r1 is the dedicated stack pointer 4977 } 4978 4979 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4980 llvm::Value *Address) const override; 4981 }; 4982 4983 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 4984 public: 4985 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 4986 4987 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4988 // This is recovered from gcc output. 4989 return 1; // r1 is the dedicated stack pointer 4990 } 4991 4992 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4993 llvm::Value *Address) const override; 4994 }; 4995 4996 } 4997 4998 // Return true if the ABI requires Ty to be passed sign- or zero- 4999 // extended to 64 bits. 5000 bool 5001 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { 5002 // Treat an enum type as its underlying type. 5003 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 5004 Ty = EnumTy->getDecl()->getIntegerType(); 5005 5006 // Promotable integer types are required to be promoted by the ABI. 5007 if (isPromotableIntegerTypeForABI(Ty)) 5008 return true; 5009 5010 // In addition to the usual promotable integer types, we also need to 5011 // extend all 32-bit types, since the ABI requires promotion to 64 bits. 5012 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 5013 switch (BT->getKind()) { 5014 case BuiltinType::Int: 5015 case BuiltinType::UInt: 5016 return true; 5017 default: 5018 break; 5019 } 5020 5021 if (const auto *EIT = Ty->getAs<ExtIntType>()) 5022 if (EIT->getNumBits() < 64) 5023 return true; 5024 5025 return false; 5026 } 5027 5028 /// isAlignedParamType - Determine whether a type requires 16-byte or 5029 /// higher alignment in the parameter area. Always returns at least 8. 5030 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { 5031 // Complex types are passed just like their elements. 5032 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 5033 Ty = CTy->getElementType(); 5034 5035 // Only vector types of size 16 bytes need alignment (larger types are 5036 // passed via reference, smaller types are not aligned). 5037 if (IsQPXVectorTy(Ty)) { 5038 if (getContext().getTypeSize(Ty) > 128) 5039 return CharUnits::fromQuantity(32); 5040 5041 return CharUnits::fromQuantity(16); 5042 } else if (Ty->isVectorType()) { 5043 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); 5044 } 5045 5046 // For single-element float/vector structs, we consider the whole type 5047 // to have the same alignment requirements as its single element. 5048 const Type *AlignAsType = nullptr; 5049 const Type *EltType = isSingleElementStruct(Ty, getContext()); 5050 if (EltType) { 5051 const BuiltinType *BT = EltType->getAs<BuiltinType>(); 5052 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && 5053 getContext().getTypeSize(EltType) == 128) || 5054 (BT && BT->isFloatingPoint())) 5055 AlignAsType = EltType; 5056 } 5057 5058 // Likewise for ELFv2 homogeneous aggregates. 5059 const Type *Base = nullptr; 5060 uint64_t Members = 0; 5061 if (!AlignAsType && Kind == ELFv2 && 5062 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) 5063 AlignAsType = Base; 5064 5065 // With special case aggregates, only vector base types need alignment. 5066 if (AlignAsType && IsQPXVectorTy(AlignAsType)) { 5067 if (getContext().getTypeSize(AlignAsType) > 128) 5068 return CharUnits::fromQuantity(32); 5069 5070 return CharUnits::fromQuantity(16); 5071 } else if (AlignAsType) { 5072 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); 5073 } 5074 5075 // Otherwise, we only need alignment for any aggregate type that 5076 // has an alignment requirement of >= 16 bytes. 5077 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { 5078 if (HasQPX && getContext().getTypeAlign(Ty) >= 256) 5079 return CharUnits::fromQuantity(32); 5080 return CharUnits::fromQuantity(16); 5081 } 5082 5083 return CharUnits::fromQuantity(8); 5084 } 5085 5086 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous 5087 /// aggregate. Base is set to the base element type, and Members is set 5088 /// to the number of base elements. 5089 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, 5090 uint64_t &Members) const { 5091 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 5092 uint64_t NElements = AT->getSize().getZExtValue(); 5093 if (NElements == 0) 5094 return false; 5095 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) 5096 return false; 5097 Members *= NElements; 5098 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 5099 const RecordDecl *RD = RT->getDecl(); 5100 if (RD->hasFlexibleArrayMember()) 5101 return false; 5102 5103 Members = 0; 5104 5105 // If this is a C++ record, check the bases first. 5106 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 5107 for (const auto &I : CXXRD->bases()) { 5108 // Ignore empty records. 5109 if (isEmptyRecord(getContext(), I.getType(), true)) 5110 continue; 5111 5112 uint64_t FldMembers; 5113 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) 5114 return false; 5115 5116 Members += FldMembers; 5117 } 5118 } 5119 5120 for (const auto *FD : RD->fields()) { 5121 // Ignore (non-zero arrays of) empty records. 5122 QualType FT = FD->getType(); 5123 while (const ConstantArrayType *AT = 5124 getContext().getAsConstantArrayType(FT)) { 5125 if (AT->getSize().getZExtValue() == 0) 5126 return false; 5127 FT = AT->getElementType(); 5128 } 5129 if (isEmptyRecord(getContext(), FT, true)) 5130 continue; 5131 5132 // For compatibility with GCC, ignore empty bitfields in C++ mode. 5133 if (getContext().getLangOpts().CPlusPlus && 5134 FD->isZeroLengthBitField(getContext())) 5135 continue; 5136 5137 uint64_t FldMembers; 5138 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) 5139 return false; 5140 5141 Members = (RD->isUnion() ? 5142 std::max(Members, FldMembers) : Members + FldMembers); 5143 } 5144 5145 if (!Base) 5146 return false; 5147 5148 // Ensure there is no padding. 5149 if (getContext().getTypeSize(Base) * Members != 5150 getContext().getTypeSize(Ty)) 5151 return false; 5152 } else { 5153 Members = 1; 5154 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 5155 Members = 2; 5156 Ty = CT->getElementType(); 5157 } 5158 5159 // Most ABIs only support float, double, and some vector type widths. 5160 if (!isHomogeneousAggregateBaseType(Ty)) 5161 return false; 5162 5163 // The base type must be the same for all members. Types that 5164 // agree in both total size and mode (float vs. vector) are 5165 // treated as being equivalent here. 5166 const Type *TyPtr = Ty.getTypePtr(); 5167 if (!Base) { 5168 Base = TyPtr; 5169 // If it's a non-power-of-2 vector, its size is already a power-of-2, 5170 // so make sure to widen it explicitly. 5171 if (const VectorType *VT = Base->getAs<VectorType>()) { 5172 QualType EltTy = VT->getElementType(); 5173 unsigned NumElements = 5174 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); 5175 Base = getContext() 5176 .getVectorType(EltTy, NumElements, VT->getVectorKind()) 5177 .getTypePtr(); 5178 } 5179 } 5180 5181 if (Base->isVectorType() != TyPtr->isVectorType() || 5182 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) 5183 return false; 5184 } 5185 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); 5186 } 5187 5188 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 5189 // Homogeneous aggregates for ELFv2 must have base types of float, 5190 // double, long double, or 128-bit vectors. 5191 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 5192 if (BT->getKind() == BuiltinType::Float || 5193 BT->getKind() == BuiltinType::Double || 5194 BT->getKind() == BuiltinType::LongDouble || 5195 (getContext().getTargetInfo().hasFloat128Type() && 5196 (BT->getKind() == BuiltinType::Float128))) { 5197 if (IsSoftFloatABI) 5198 return false; 5199 return true; 5200 } 5201 } 5202 if (const VectorType *VT = Ty->getAs<VectorType>()) { 5203 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) 5204 return true; 5205 } 5206 return false; 5207 } 5208 5209 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( 5210 const Type *Base, uint64_t Members) const { 5211 // Vector and fp128 types require one register, other floating point types 5212 // require one or two registers depending on their size. 5213 uint32_t NumRegs = 5214 ((getContext().getTargetInfo().hasFloat128Type() && 5215 Base->isFloat128Type()) || 5216 Base->isVectorType()) ? 1 5217 : (getContext().getTypeSize(Base) + 63) / 64; 5218 5219 // Homogeneous Aggregates may occupy at most 8 registers. 5220 return Members * NumRegs <= 8; 5221 } 5222 5223 ABIArgInfo 5224 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { 5225 Ty = useFirstFieldIfTransparentUnion(Ty); 5226 5227 if (Ty->isAnyComplexType()) 5228 return ABIArgInfo::getDirect(); 5229 5230 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) 5231 // or via reference (larger than 16 bytes). 5232 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { 5233 uint64_t Size = getContext().getTypeSize(Ty); 5234 if (Size > 128) 5235 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 5236 else if (Size < 128) { 5237 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 5238 return ABIArgInfo::getDirect(CoerceTy); 5239 } 5240 } 5241 5242 if (const auto *EIT = Ty->getAs<ExtIntType>()) 5243 if (EIT->getNumBits() > 128) 5244 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 5245 5246 if (isAggregateTypeForABI(Ty)) { 5247 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 5248 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 5249 5250 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); 5251 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); 5252 5253 // ELFv2 homogeneous aggregates are passed as array types. 5254 const Type *Base = nullptr; 5255 uint64_t Members = 0; 5256 if (Kind == ELFv2 && 5257 isHomogeneousAggregate(Ty, Base, Members)) { 5258 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 5259 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 5260 return ABIArgInfo::getDirect(CoerceTy); 5261 } 5262 5263 // If an aggregate may end up fully in registers, we do not 5264 // use the ByVal method, but pass the aggregate as array. 5265 // This is usually beneficial since we avoid forcing the 5266 // back-end to store the argument to memory. 5267 uint64_t Bits = getContext().getTypeSize(Ty); 5268 if (Bits > 0 && Bits <= 8 * GPRBits) { 5269 llvm::Type *CoerceTy; 5270 5271 // Types up to 8 bytes are passed as integer type (which will be 5272 // properly aligned in the argument save area doubleword). 5273 if (Bits <= GPRBits) 5274 CoerceTy = 5275 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); 5276 // Larger types are passed as arrays, with the base type selected 5277 // according to the required alignment in the save area. 5278 else { 5279 uint64_t RegBits = ABIAlign * 8; 5280 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; 5281 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); 5282 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); 5283 } 5284 5285 return ABIArgInfo::getDirect(CoerceTy); 5286 } 5287 5288 // All other aggregates are passed ByVal. 5289 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), 5290 /*ByVal=*/true, 5291 /*Realign=*/TyAlign > ABIAlign); 5292 } 5293 5294 return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 5295 : ABIArgInfo::getDirect()); 5296 } 5297 5298 ABIArgInfo 5299 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { 5300 if (RetTy->isVoidType()) 5301 return ABIArgInfo::getIgnore(); 5302 5303 if (RetTy->isAnyComplexType()) 5304 return ABIArgInfo::getDirect(); 5305 5306 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) 5307 // or via reference (larger than 16 bytes). 5308 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { 5309 uint64_t Size = getContext().getTypeSize(RetTy); 5310 if (Size > 128) 5311 return getNaturalAlignIndirect(RetTy); 5312 else if (Size < 128) { 5313 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 5314 return ABIArgInfo::getDirect(CoerceTy); 5315 } 5316 } 5317 5318 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 5319 if (EIT->getNumBits() > 128) 5320 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 5321 5322 if (isAggregateTypeForABI(RetTy)) { 5323 // ELFv2 homogeneous aggregates are returned as array types. 5324 const Type *Base = nullptr; 5325 uint64_t Members = 0; 5326 if (Kind == ELFv2 && 5327 isHomogeneousAggregate(RetTy, Base, Members)) { 5328 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 5329 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 5330 return ABIArgInfo::getDirect(CoerceTy); 5331 } 5332 5333 // ELFv2 small aggregates are returned in up to two registers. 5334 uint64_t Bits = getContext().getTypeSize(RetTy); 5335 if (Kind == ELFv2 && Bits <= 2 * GPRBits) { 5336 if (Bits == 0) 5337 return ABIArgInfo::getIgnore(); 5338 5339 llvm::Type *CoerceTy; 5340 if (Bits > GPRBits) { 5341 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); 5342 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); 5343 } else 5344 CoerceTy = 5345 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); 5346 return ABIArgInfo::getDirect(CoerceTy); 5347 } 5348 5349 // All other aggregates are returned indirectly. 5350 return getNaturalAlignIndirect(RetTy); 5351 } 5352 5353 return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 5354 : ABIArgInfo::getDirect()); 5355 } 5356 5357 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. 5358 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5359 QualType Ty) const { 5360 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 5361 TypeInfo.second = getParamTypeAlignment(Ty); 5362 5363 CharUnits SlotSize = CharUnits::fromQuantity(8); 5364 5365 // If we have a complex type and the base type is smaller than 8 bytes, 5366 // the ABI calls for the real and imaginary parts to be right-adjusted 5367 // in separate doublewords. However, Clang expects us to produce a 5368 // pointer to a structure with the two parts packed tightly. So generate 5369 // loads of the real and imaginary parts relative to the va_list pointer, 5370 // and store them to a temporary structure. 5371 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 5372 CharUnits EltSize = TypeInfo.first / 2; 5373 if (EltSize < SlotSize) { 5374 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, 5375 SlotSize * 2, SlotSize, 5376 SlotSize, /*AllowHigher*/ true); 5377 5378 Address RealAddr = Addr; 5379 Address ImagAddr = RealAddr; 5380 if (CGF.CGM.getDataLayout().isBigEndian()) { 5381 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, 5382 SlotSize - EltSize); 5383 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, 5384 2 * SlotSize - EltSize); 5385 } else { 5386 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); 5387 } 5388 5389 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); 5390 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); 5391 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); 5392 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); 5393 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); 5394 5395 Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); 5396 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), 5397 /*init*/ true); 5398 return Temp; 5399 } 5400 } 5401 5402 // Otherwise, just use the general rule. 5403 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, 5404 TypeInfo, SlotSize, /*AllowHigher*/ true); 5405 } 5406 5407 bool 5408 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( 5409 CodeGen::CodeGenFunction &CGF, 5410 llvm::Value *Address) const { 5411 return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true, 5412 /*IsAIX*/ false); 5413 } 5414 5415 bool 5416 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 5417 llvm::Value *Address) const { 5418 return PPC_initDwarfEHRegSizeTable(CGF, Address, /*Is64Bit*/ true, 5419 /*IsAIX*/ false); 5420 } 5421 5422 //===----------------------------------------------------------------------===// 5423 // AArch64 ABI Implementation 5424 //===----------------------------------------------------------------------===// 5425 5426 namespace { 5427 5428 class AArch64ABIInfo : public SwiftABIInfo { 5429 public: 5430 enum ABIKind { 5431 AAPCS = 0, 5432 DarwinPCS, 5433 Win64 5434 }; 5435 5436 private: 5437 ABIKind Kind; 5438 5439 public: 5440 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) 5441 : SwiftABIInfo(CGT), Kind(Kind) {} 5442 5443 private: 5444 ABIKind getABIKind() const { return Kind; } 5445 bool isDarwinPCS() const { return Kind == DarwinPCS; } 5446 5447 ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const; 5448 ABIArgInfo classifyArgumentType(QualType RetTy) const; 5449 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 5450 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 5451 uint64_t Members) const override; 5452 5453 bool isIllegalVectorType(QualType Ty) const; 5454 5455 void computeInfo(CGFunctionInfo &FI) const override { 5456 if (!::classifyReturnType(getCXXABI(), FI, *this)) 5457 FI.getReturnInfo() = 5458 classifyReturnType(FI.getReturnType(), FI.isVariadic()); 5459 5460 for (auto &it : FI.arguments()) 5461 it.info = classifyArgumentType(it.type); 5462 } 5463 5464 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, 5465 CodeGenFunction &CGF) const; 5466 5467 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, 5468 CodeGenFunction &CGF) const; 5469 5470 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5471 QualType Ty) const override { 5472 return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) 5473 : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) 5474 : EmitAAPCSVAArg(VAListAddr, Ty, CGF); 5475 } 5476 5477 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 5478 QualType Ty) const override; 5479 5480 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 5481 bool asReturnValue) const override { 5482 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 5483 } 5484 bool isSwiftErrorInRegister() const override { 5485 return true; 5486 } 5487 5488 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, 5489 unsigned elts) const override; 5490 5491 bool allowBFloatArgsAndRet() const override { 5492 return getTarget().hasBFloat16Type(); 5493 } 5494 }; 5495 5496 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { 5497 public: 5498 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) 5499 : TargetCodeGenInfo(std::make_unique<AArch64ABIInfo>(CGT, Kind)) {} 5500 5501 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 5502 return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; 5503 } 5504 5505 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 5506 return 31; 5507 } 5508 5509 bool doesReturnSlotInterfereWithArgs() const override { return false; } 5510 5511 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5512 CodeGen::CodeGenModule &CGM) const override { 5513 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 5514 if (!FD) 5515 return; 5516 5517 LangOptions::SignReturnAddressScopeKind Scope = 5518 CGM.getLangOpts().getSignReturnAddressScope(); 5519 LangOptions::SignReturnAddressKeyKind Key = 5520 CGM.getLangOpts().getSignReturnAddressKey(); 5521 bool BranchTargetEnforcement = CGM.getLangOpts().BranchTargetEnforcement; 5522 if (const auto *TA = FD->getAttr<TargetAttr>()) { 5523 ParsedTargetAttr Attr = TA->parse(); 5524 if (!Attr.BranchProtection.empty()) { 5525 TargetInfo::BranchProtectionInfo BPI; 5526 StringRef Error; 5527 (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection, 5528 BPI, Error); 5529 assert(Error.empty()); 5530 Scope = BPI.SignReturnAddr; 5531 Key = BPI.SignKey; 5532 BranchTargetEnforcement = BPI.BranchTargetEnforcement; 5533 } 5534 } 5535 5536 auto *Fn = cast<llvm::Function>(GV); 5537 if (Scope != LangOptions::SignReturnAddressScopeKind::None) { 5538 Fn->addFnAttr("sign-return-address", 5539 Scope == LangOptions::SignReturnAddressScopeKind::All 5540 ? "all" 5541 : "non-leaf"); 5542 5543 Fn->addFnAttr("sign-return-address-key", 5544 Key == LangOptions::SignReturnAddressKeyKind::AKey 5545 ? "a_key" 5546 : "b_key"); 5547 } 5548 5549 if (BranchTargetEnforcement) 5550 Fn->addFnAttr("branch-target-enforcement"); 5551 } 5552 }; 5553 5554 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { 5555 public: 5556 WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) 5557 : AArch64TargetCodeGenInfo(CGT, K) {} 5558 5559 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5560 CodeGen::CodeGenModule &CGM) const override; 5561 5562 void getDependentLibraryOption(llvm::StringRef Lib, 5563 llvm::SmallString<24> &Opt) const override { 5564 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); 5565 } 5566 5567 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, 5568 llvm::SmallString<32> &Opt) const override { 5569 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 5570 } 5571 }; 5572 5573 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( 5574 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 5575 AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 5576 if (GV->isDeclaration()) 5577 return; 5578 addStackProbeTargetAttributes(D, GV, CGM); 5579 } 5580 } 5581 5582 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { 5583 Ty = useFirstFieldIfTransparentUnion(Ty); 5584 5585 // Handle illegal vector types here. 5586 if (isIllegalVectorType(Ty)) { 5587 uint64_t Size = getContext().getTypeSize(Ty); 5588 // Android promotes <2 x i8> to i16, not i32 5589 if (isAndroid() && (Size <= 16)) { 5590 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); 5591 return ABIArgInfo::getDirect(ResType); 5592 } 5593 if (Size <= 32) { 5594 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); 5595 return ABIArgInfo::getDirect(ResType); 5596 } 5597 if (Size == 64) { 5598 auto *ResType = 5599 llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); 5600 return ABIArgInfo::getDirect(ResType); 5601 } 5602 if (Size == 128) { 5603 auto *ResType = 5604 llvm::FixedVectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); 5605 return ABIArgInfo::getDirect(ResType); 5606 } 5607 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 5608 } 5609 5610 if (!isAggregateTypeForABI(Ty)) { 5611 // Treat an enum type as its underlying type. 5612 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 5613 Ty = EnumTy->getDecl()->getIntegerType(); 5614 5615 if (const auto *EIT = Ty->getAs<ExtIntType>()) 5616 if (EIT->getNumBits() > 128) 5617 return getNaturalAlignIndirect(Ty); 5618 5619 return (isPromotableIntegerTypeForABI(Ty) && isDarwinPCS() 5620 ? ABIArgInfo::getExtend(Ty) 5621 : ABIArgInfo::getDirect()); 5622 } 5623 5624 // Structures with either a non-trivial destructor or a non-trivial 5625 // copy constructor are always indirect. 5626 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 5627 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == 5628 CGCXXABI::RAA_DirectInMemory); 5629 } 5630 5631 // Empty records are always ignored on Darwin, but actually passed in C++ mode 5632 // elsewhere for GNU compatibility. 5633 uint64_t Size = getContext().getTypeSize(Ty); 5634 bool IsEmpty = isEmptyRecord(getContext(), Ty, true); 5635 if (IsEmpty || Size == 0) { 5636 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) 5637 return ABIArgInfo::getIgnore(); 5638 5639 // GNU C mode. The only argument that gets ignored is an empty one with size 5640 // 0. 5641 if (IsEmpty && Size == 0) 5642 return ABIArgInfo::getIgnore(); 5643 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 5644 } 5645 5646 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. 5647 const Type *Base = nullptr; 5648 uint64_t Members = 0; 5649 if (isHomogeneousAggregate(Ty, Base, Members)) { 5650 return ABIArgInfo::getDirect( 5651 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); 5652 } 5653 5654 // Aggregates <= 16 bytes are passed directly in registers or on the stack. 5655 if (Size <= 128) { 5656 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of 5657 // same size and alignment. 5658 if (getTarget().isRenderScriptTarget()) { 5659 return coerceToIntArray(Ty, getContext(), getVMContext()); 5660 } 5661 unsigned Alignment; 5662 if (Kind == AArch64ABIInfo::AAPCS) { 5663 Alignment = getContext().getTypeUnadjustedAlign(Ty); 5664 Alignment = Alignment < 128 ? 64 : 128; 5665 } else { 5666 Alignment = std::max(getContext().getTypeAlign(Ty), 5667 (unsigned)getTarget().getPointerWidth(0)); 5668 } 5669 Size = llvm::alignTo(Size, Alignment); 5670 5671 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. 5672 // For aggregates with 16-byte alignment, we use i128. 5673 llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment); 5674 return ABIArgInfo::getDirect( 5675 Size == Alignment ? BaseTy 5676 : llvm::ArrayType::get(BaseTy, Size / Alignment)); 5677 } 5678 5679 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 5680 } 5681 5682 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy, 5683 bool IsVariadic) const { 5684 if (RetTy->isVoidType()) 5685 return ABIArgInfo::getIgnore(); 5686 5687 // Large vector types should be returned via memory. 5688 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) 5689 return getNaturalAlignIndirect(RetTy); 5690 5691 if (!isAggregateTypeForABI(RetTy)) { 5692 // Treat an enum type as its underlying type. 5693 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 5694 RetTy = EnumTy->getDecl()->getIntegerType(); 5695 5696 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 5697 if (EIT->getNumBits() > 128) 5698 return getNaturalAlignIndirect(RetTy); 5699 5700 return (isPromotableIntegerTypeForABI(RetTy) && isDarwinPCS() 5701 ? ABIArgInfo::getExtend(RetTy) 5702 : ABIArgInfo::getDirect()); 5703 } 5704 5705 uint64_t Size = getContext().getTypeSize(RetTy); 5706 if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) 5707 return ABIArgInfo::getIgnore(); 5708 5709 const Type *Base = nullptr; 5710 uint64_t Members = 0; 5711 if (isHomogeneousAggregate(RetTy, Base, Members) && 5712 !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 && 5713 IsVariadic)) 5714 // Homogeneous Floating-point Aggregates (HFAs) are returned directly. 5715 return ABIArgInfo::getDirect(); 5716 5717 // Aggregates <= 16 bytes are returned directly in registers or on the stack. 5718 if (Size <= 128) { 5719 // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of 5720 // same size and alignment. 5721 if (getTarget().isRenderScriptTarget()) { 5722 return coerceToIntArray(RetTy, getContext(), getVMContext()); 5723 } 5724 unsigned Alignment = getContext().getTypeAlign(RetTy); 5725 Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes 5726 5727 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. 5728 // For aggregates with 16-byte alignment, we use i128. 5729 if (Alignment < 128 && Size == 128) { 5730 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); 5731 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); 5732 } 5733 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); 5734 } 5735 5736 return getNaturalAlignIndirect(RetTy); 5737 } 5738 5739 /// isIllegalVectorType - check whether the vector type is legal for AArch64. 5740 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { 5741 if (const VectorType *VT = Ty->getAs<VectorType>()) { 5742 // Check whether VT is legal. 5743 unsigned NumElements = VT->getNumElements(); 5744 uint64_t Size = getContext().getTypeSize(VT); 5745 // NumElements should be power of 2. 5746 if (!llvm::isPowerOf2_32(NumElements)) 5747 return true; 5748 5749 // arm64_32 has to be compatible with the ARM logic here, which allows huge 5750 // vectors for some reason. 5751 llvm::Triple Triple = getTarget().getTriple(); 5752 if (Triple.getArch() == llvm::Triple::aarch64_32 && 5753 Triple.isOSBinFormatMachO()) 5754 return Size <= 32; 5755 5756 return Size != 64 && (Size != 128 || NumElements == 1); 5757 } 5758 return false; 5759 } 5760 5761 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, 5762 llvm::Type *eltTy, 5763 unsigned elts) const { 5764 if (!llvm::isPowerOf2_32(elts)) 5765 return false; 5766 if (totalSize.getQuantity() != 8 && 5767 (totalSize.getQuantity() != 16 || elts == 1)) 5768 return false; 5769 return true; 5770 } 5771 5772 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 5773 // Homogeneous aggregates for AAPCS64 must have base types of a floating 5774 // point type or a short-vector type. This is the same as the 32-bit ABI, 5775 // but with the difference that any floating-point type is allowed, 5776 // including __fp16. 5777 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 5778 if (BT->isFloatingPoint()) 5779 return true; 5780 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 5781 unsigned VecSize = getContext().getTypeSize(VT); 5782 if (VecSize == 64 || VecSize == 128) 5783 return true; 5784 } 5785 return false; 5786 } 5787 5788 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 5789 uint64_t Members) const { 5790 return Members <= 4; 5791 } 5792 5793 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, 5794 QualType Ty, 5795 CodeGenFunction &CGF) const { 5796 ABIArgInfo AI = classifyArgumentType(Ty); 5797 bool IsIndirect = AI.isIndirect(); 5798 5799 llvm::Type *BaseTy = CGF.ConvertType(Ty); 5800 if (IsIndirect) 5801 BaseTy = llvm::PointerType::getUnqual(BaseTy); 5802 else if (AI.getCoerceToType()) 5803 BaseTy = AI.getCoerceToType(); 5804 5805 unsigned NumRegs = 1; 5806 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { 5807 BaseTy = ArrTy->getElementType(); 5808 NumRegs = ArrTy->getNumElements(); 5809 } 5810 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); 5811 5812 // The AArch64 va_list type and handling is specified in the Procedure Call 5813 // Standard, section B.4: 5814 // 5815 // struct { 5816 // void *__stack; 5817 // void *__gr_top; 5818 // void *__vr_top; 5819 // int __gr_offs; 5820 // int __vr_offs; 5821 // }; 5822 5823 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); 5824 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 5825 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); 5826 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 5827 5828 CharUnits TySize = getContext().getTypeSizeInChars(Ty); 5829 CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty); 5830 5831 Address reg_offs_p = Address::invalid(); 5832 llvm::Value *reg_offs = nullptr; 5833 int reg_top_index; 5834 int RegSize = IsIndirect ? 8 : TySize.getQuantity(); 5835 if (!IsFPR) { 5836 // 3 is the field number of __gr_offs 5837 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); 5838 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); 5839 reg_top_index = 1; // field number for __gr_top 5840 RegSize = llvm::alignTo(RegSize, 8); 5841 } else { 5842 // 4 is the field number of __vr_offs. 5843 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); 5844 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); 5845 reg_top_index = 2; // field number for __vr_top 5846 RegSize = 16 * NumRegs; 5847 } 5848 5849 //======================================= 5850 // Find out where argument was passed 5851 //======================================= 5852 5853 // If reg_offs >= 0 we're already using the stack for this type of 5854 // argument. We don't want to keep updating reg_offs (in case it overflows, 5855 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves 5856 // whatever they get). 5857 llvm::Value *UsingStack = nullptr; 5858 UsingStack = CGF.Builder.CreateICmpSGE( 5859 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); 5860 5861 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); 5862 5863 // Otherwise, at least some kind of argument could go in these registers, the 5864 // question is whether this particular type is too big. 5865 CGF.EmitBlock(MaybeRegBlock); 5866 5867 // Integer arguments may need to correct register alignment (for example a 5868 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we 5869 // align __gr_offs to calculate the potential address. 5870 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { 5871 int Align = TyAlign.getQuantity(); 5872 5873 reg_offs = CGF.Builder.CreateAdd( 5874 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), 5875 "align_regoffs"); 5876 reg_offs = CGF.Builder.CreateAnd( 5877 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), 5878 "aligned_regoffs"); 5879 } 5880 5881 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. 5882 // The fact that this is done unconditionally reflects the fact that 5883 // allocating an argument to the stack also uses up all the remaining 5884 // registers of the appropriate kind. 5885 llvm::Value *NewOffset = nullptr; 5886 NewOffset = CGF.Builder.CreateAdd( 5887 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); 5888 CGF.Builder.CreateStore(NewOffset, reg_offs_p); 5889 5890 // Now we're in a position to decide whether this argument really was in 5891 // registers or not. 5892 llvm::Value *InRegs = nullptr; 5893 InRegs = CGF.Builder.CreateICmpSLE( 5894 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); 5895 5896 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); 5897 5898 //======================================= 5899 // Argument was in registers 5900 //======================================= 5901 5902 // Now we emit the code for if the argument was originally passed in 5903 // registers. First start the appropriate block: 5904 CGF.EmitBlock(InRegBlock); 5905 5906 llvm::Value *reg_top = nullptr; 5907 Address reg_top_p = 5908 CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); 5909 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); 5910 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), 5911 CharUnits::fromQuantity(IsFPR ? 16 : 8)); 5912 Address RegAddr = Address::invalid(); 5913 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); 5914 5915 if (IsIndirect) { 5916 // If it's been passed indirectly (actually a struct), whatever we find from 5917 // stored registers or on the stack will actually be a struct **. 5918 MemTy = llvm::PointerType::getUnqual(MemTy); 5919 } 5920 5921 const Type *Base = nullptr; 5922 uint64_t NumMembers = 0; 5923 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); 5924 if (IsHFA && NumMembers > 1) { 5925 // Homogeneous aggregates passed in registers will have their elements split 5926 // and stored 16-bytes apart regardless of size (they're notionally in qN, 5927 // qN+1, ...). We reload and store into a temporary local variable 5928 // contiguously. 5929 assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); 5930 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); 5931 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); 5932 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); 5933 Address Tmp = CGF.CreateTempAlloca(HFATy, 5934 std::max(TyAlign, BaseTyInfo.second)); 5935 5936 // On big-endian platforms, the value will be right-aligned in its slot. 5937 int Offset = 0; 5938 if (CGF.CGM.getDataLayout().isBigEndian() && 5939 BaseTyInfo.first.getQuantity() < 16) 5940 Offset = 16 - BaseTyInfo.first.getQuantity(); 5941 5942 for (unsigned i = 0; i < NumMembers; ++i) { 5943 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); 5944 Address LoadAddr = 5945 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); 5946 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); 5947 5948 Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i); 5949 5950 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); 5951 CGF.Builder.CreateStore(Elem, StoreAddr); 5952 } 5953 5954 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); 5955 } else { 5956 // Otherwise the object is contiguous in memory. 5957 5958 // It might be right-aligned in its slot. 5959 CharUnits SlotSize = BaseAddr.getAlignment(); 5960 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && 5961 (IsHFA || !isAggregateTypeForABI(Ty)) && 5962 TySize < SlotSize) { 5963 CharUnits Offset = SlotSize - TySize; 5964 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); 5965 } 5966 5967 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); 5968 } 5969 5970 CGF.EmitBranch(ContBlock); 5971 5972 //======================================= 5973 // Argument was on the stack 5974 //======================================= 5975 CGF.EmitBlock(OnStackBlock); 5976 5977 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); 5978 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); 5979 5980 // Again, stack arguments may need realignment. In this case both integer and 5981 // floating-point ones might be affected. 5982 if (!IsIndirect && TyAlign.getQuantity() > 8) { 5983 int Align = TyAlign.getQuantity(); 5984 5985 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); 5986 5987 OnStackPtr = CGF.Builder.CreateAdd( 5988 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), 5989 "align_stack"); 5990 OnStackPtr = CGF.Builder.CreateAnd( 5991 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), 5992 "align_stack"); 5993 5994 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); 5995 } 5996 Address OnStackAddr(OnStackPtr, 5997 std::max(CharUnits::fromQuantity(8), TyAlign)); 5998 5999 // All stack slots are multiples of 8 bytes. 6000 CharUnits StackSlotSize = CharUnits::fromQuantity(8); 6001 CharUnits StackSize; 6002 if (IsIndirect) 6003 StackSize = StackSlotSize; 6004 else 6005 StackSize = TySize.alignTo(StackSlotSize); 6006 6007 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); 6008 llvm::Value *NewStack = 6009 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); 6010 6011 // Write the new value of __stack for the next call to va_arg 6012 CGF.Builder.CreateStore(NewStack, stack_p); 6013 6014 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && 6015 TySize < StackSlotSize) { 6016 CharUnits Offset = StackSlotSize - TySize; 6017 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); 6018 } 6019 6020 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); 6021 6022 CGF.EmitBranch(ContBlock); 6023 6024 //======================================= 6025 // Tidy up 6026 //======================================= 6027 CGF.EmitBlock(ContBlock); 6028 6029 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, 6030 OnStackAddr, OnStackBlock, "vaargs.addr"); 6031 6032 if (IsIndirect) 6033 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), 6034 TyAlign); 6035 6036 return ResAddr; 6037 } 6038 6039 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, 6040 CodeGenFunction &CGF) const { 6041 // The backend's lowering doesn't support va_arg for aggregates or 6042 // illegal vector types. Lower VAArg here for these cases and use 6043 // the LLVM va_arg instruction for everything else. 6044 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) 6045 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); 6046 6047 uint64_t PointerSize = getTarget().getPointerWidth(0) / 8; 6048 CharUnits SlotSize = CharUnits::fromQuantity(PointerSize); 6049 6050 // Empty records are ignored for parameter passing purposes. 6051 if (isEmptyRecord(getContext(), Ty, true)) { 6052 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); 6053 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 6054 return Addr; 6055 } 6056 6057 // The size of the actual thing passed, which might end up just 6058 // being a pointer for indirect types. 6059 auto TyInfo = getContext().getTypeInfoInChars(Ty); 6060 6061 // Arguments bigger than 16 bytes which aren't homogeneous 6062 // aggregates should be passed indirectly. 6063 bool IsIndirect = false; 6064 if (TyInfo.first.getQuantity() > 16) { 6065 const Type *Base = nullptr; 6066 uint64_t Members = 0; 6067 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); 6068 } 6069 6070 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, 6071 TyInfo, SlotSize, /*AllowHigherAlign*/ true); 6072 } 6073 6074 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 6075 QualType Ty) const { 6076 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 6077 CGF.getContext().getTypeInfoInChars(Ty), 6078 CharUnits::fromQuantity(8), 6079 /*allowHigherAlign*/ false); 6080 } 6081 6082 //===----------------------------------------------------------------------===// 6083 // ARM ABI Implementation 6084 //===----------------------------------------------------------------------===// 6085 6086 namespace { 6087 6088 class ARMABIInfo : public SwiftABIInfo { 6089 public: 6090 enum ABIKind { 6091 APCS = 0, 6092 AAPCS = 1, 6093 AAPCS_VFP = 2, 6094 AAPCS16_VFP = 3, 6095 }; 6096 6097 private: 6098 ABIKind Kind; 6099 bool IsFloatABISoftFP; 6100 6101 public: 6102 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) 6103 : SwiftABIInfo(CGT), Kind(_Kind) { 6104 setCCs(); 6105 IsFloatABISoftFP = CGT.getCodeGenOpts().FloatABI == "softfp" || 6106 CGT.getCodeGenOpts().FloatABI == ""; // default 6107 } 6108 6109 bool isEABI() const { 6110 switch (getTarget().getTriple().getEnvironment()) { 6111 case llvm::Triple::Android: 6112 case llvm::Triple::EABI: 6113 case llvm::Triple::EABIHF: 6114 case llvm::Triple::GNUEABI: 6115 case llvm::Triple::GNUEABIHF: 6116 case llvm::Triple::MuslEABI: 6117 case llvm::Triple::MuslEABIHF: 6118 return true; 6119 default: 6120 return false; 6121 } 6122 } 6123 6124 bool isEABIHF() const { 6125 switch (getTarget().getTriple().getEnvironment()) { 6126 case llvm::Triple::EABIHF: 6127 case llvm::Triple::GNUEABIHF: 6128 case llvm::Triple::MuslEABIHF: 6129 return true; 6130 default: 6131 return false; 6132 } 6133 } 6134 6135 ABIKind getABIKind() const { return Kind; } 6136 6137 bool allowBFloatArgsAndRet() const override { 6138 return !IsFloatABISoftFP && getTarget().hasBFloat16Type(); 6139 } 6140 6141 private: 6142 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic, 6143 unsigned functionCallConv) const; 6144 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, 6145 unsigned functionCallConv) const; 6146 ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, 6147 uint64_t Members) const; 6148 ABIArgInfo coerceIllegalVector(QualType Ty) const; 6149 bool isIllegalVectorType(QualType Ty) const; 6150 bool containsAnyFP16Vectors(QualType Ty) const; 6151 6152 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 6153 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 6154 uint64_t Members) const override; 6155 6156 bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const; 6157 6158 void computeInfo(CGFunctionInfo &FI) const override; 6159 6160 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6161 QualType Ty) const override; 6162 6163 llvm::CallingConv::ID getLLVMDefaultCC() const; 6164 llvm::CallingConv::ID getABIDefaultCC() const; 6165 void setCCs(); 6166 6167 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 6168 bool asReturnValue) const override { 6169 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 6170 } 6171 bool isSwiftErrorInRegister() const override { 6172 return true; 6173 } 6174 bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, 6175 unsigned elts) const override; 6176 }; 6177 6178 class ARMTargetCodeGenInfo : public TargetCodeGenInfo { 6179 public: 6180 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 6181 : TargetCodeGenInfo(std::make_unique<ARMABIInfo>(CGT, K)) {} 6182 6183 const ARMABIInfo &getABIInfo() const { 6184 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); 6185 } 6186 6187 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 6188 return 13; 6189 } 6190 6191 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 6192 return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; 6193 } 6194 6195 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 6196 llvm::Value *Address) const override { 6197 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 6198 6199 // 0-15 are the 16 integer registers. 6200 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); 6201 return false; 6202 } 6203 6204 unsigned getSizeOfUnwindException() const override { 6205 if (getABIInfo().isEABI()) return 88; 6206 return TargetCodeGenInfo::getSizeOfUnwindException(); 6207 } 6208 6209 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6210 CodeGen::CodeGenModule &CGM) const override { 6211 if (GV->isDeclaration()) 6212 return; 6213 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 6214 if (!FD) 6215 return; 6216 6217 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); 6218 if (!Attr) 6219 return; 6220 6221 const char *Kind; 6222 switch (Attr->getInterrupt()) { 6223 case ARMInterruptAttr::Generic: Kind = ""; break; 6224 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; 6225 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; 6226 case ARMInterruptAttr::SWI: Kind = "SWI"; break; 6227 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; 6228 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; 6229 } 6230 6231 llvm::Function *Fn = cast<llvm::Function>(GV); 6232 6233 Fn->addFnAttr("interrupt", Kind); 6234 6235 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); 6236 if (ABI == ARMABIInfo::APCS) 6237 return; 6238 6239 // AAPCS guarantees that sp will be 8-byte aligned on any public interface, 6240 // however this is not necessarily true on taking any interrupt. Instruct 6241 // the backend to perform a realignment as part of the function prologue. 6242 llvm::AttrBuilder B; 6243 B.addStackAlignmentAttr(8); 6244 Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); 6245 } 6246 }; 6247 6248 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { 6249 public: 6250 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 6251 : ARMTargetCodeGenInfo(CGT, K) {} 6252 6253 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6254 CodeGen::CodeGenModule &CGM) const override; 6255 6256 void getDependentLibraryOption(llvm::StringRef Lib, 6257 llvm::SmallString<24> &Opt) const override { 6258 Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); 6259 } 6260 6261 void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, 6262 llvm::SmallString<32> &Opt) const override { 6263 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 6264 } 6265 }; 6266 6267 void WindowsARMTargetCodeGenInfo::setTargetAttributes( 6268 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 6269 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 6270 if (GV->isDeclaration()) 6271 return; 6272 addStackProbeTargetAttributes(D, GV, CGM); 6273 } 6274 } 6275 6276 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 6277 if (!::classifyReturnType(getCXXABI(), FI, *this)) 6278 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(), 6279 FI.getCallingConvention()); 6280 6281 for (auto &I : FI.arguments()) 6282 I.info = classifyArgumentType(I.type, FI.isVariadic(), 6283 FI.getCallingConvention()); 6284 6285 6286 // Always honor user-specified calling convention. 6287 if (FI.getCallingConvention() != llvm::CallingConv::C) 6288 return; 6289 6290 llvm::CallingConv::ID cc = getRuntimeCC(); 6291 if (cc != llvm::CallingConv::C) 6292 FI.setEffectiveCallingConvention(cc); 6293 } 6294 6295 /// Return the default calling convention that LLVM will use. 6296 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { 6297 // The default calling convention that LLVM will infer. 6298 if (isEABIHF() || getTarget().getTriple().isWatchABI()) 6299 return llvm::CallingConv::ARM_AAPCS_VFP; 6300 else if (isEABI()) 6301 return llvm::CallingConv::ARM_AAPCS; 6302 else 6303 return llvm::CallingConv::ARM_APCS; 6304 } 6305 6306 /// Return the calling convention that our ABI would like us to use 6307 /// as the C calling convention. 6308 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { 6309 switch (getABIKind()) { 6310 case APCS: return llvm::CallingConv::ARM_APCS; 6311 case AAPCS: return llvm::CallingConv::ARM_AAPCS; 6312 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 6313 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 6314 } 6315 llvm_unreachable("bad ABI kind"); 6316 } 6317 6318 void ARMABIInfo::setCCs() { 6319 assert(getRuntimeCC() == llvm::CallingConv::C); 6320 6321 // Don't muddy up the IR with a ton of explicit annotations if 6322 // they'd just match what LLVM will infer from the triple. 6323 llvm::CallingConv::ID abiCC = getABIDefaultCC(); 6324 if (abiCC != getLLVMDefaultCC()) 6325 RuntimeCC = abiCC; 6326 } 6327 6328 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { 6329 uint64_t Size = getContext().getTypeSize(Ty); 6330 if (Size <= 32) { 6331 llvm::Type *ResType = 6332 llvm::Type::getInt32Ty(getVMContext()); 6333 return ABIArgInfo::getDirect(ResType); 6334 } 6335 if (Size == 64 || Size == 128) { 6336 auto *ResType = llvm::FixedVectorType::get( 6337 llvm::Type::getInt32Ty(getVMContext()), Size / 32); 6338 return ABIArgInfo::getDirect(ResType); 6339 } 6340 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 6341 } 6342 6343 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, 6344 const Type *Base, 6345 uint64_t Members) const { 6346 assert(Base && "Base class should be set for homogeneous aggregate"); 6347 // Base can be a floating-point or a vector. 6348 if (const VectorType *VT = Base->getAs<VectorType>()) { 6349 // FP16 vectors should be converted to integer vectors 6350 if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) { 6351 uint64_t Size = getContext().getTypeSize(VT); 6352 auto *NewVecTy = llvm::FixedVectorType::get( 6353 llvm::Type::getInt32Ty(getVMContext()), Size / 32); 6354 llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); 6355 return ABIArgInfo::getDirect(Ty, 0, nullptr, false); 6356 } 6357 } 6358 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); 6359 } 6360 6361 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, 6362 unsigned functionCallConv) const { 6363 // 6.1.2.1 The following argument types are VFP CPRCs: 6364 // A single-precision floating-point type (including promoted 6365 // half-precision types); A double-precision floating-point type; 6366 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate 6367 // with a Base Type of a single- or double-precision floating-point type, 6368 // 64-bit containerized vectors or 128-bit containerized vectors with one 6369 // to four Elements. 6370 // Variadic functions should always marshal to the base standard. 6371 bool IsAAPCS_VFP = 6372 !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false); 6373 6374 Ty = useFirstFieldIfTransparentUnion(Ty); 6375 6376 // Handle illegal vector types here. 6377 if (isIllegalVectorType(Ty)) 6378 return coerceIllegalVector(Ty); 6379 6380 if (!isAggregateTypeForABI(Ty)) { 6381 // Treat an enum type as its underlying type. 6382 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 6383 Ty = EnumTy->getDecl()->getIntegerType(); 6384 } 6385 6386 if (const auto *EIT = Ty->getAs<ExtIntType>()) 6387 if (EIT->getNumBits() > 64) 6388 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 6389 6390 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 6391 : ABIArgInfo::getDirect()); 6392 } 6393 6394 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 6395 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 6396 } 6397 6398 // Ignore empty records. 6399 if (isEmptyRecord(getContext(), Ty, true)) 6400 return ABIArgInfo::getIgnore(); 6401 6402 if (IsAAPCS_VFP) { 6403 // Homogeneous Aggregates need to be expanded when we can fit the aggregate 6404 // into VFP registers. 6405 const Type *Base = nullptr; 6406 uint64_t Members = 0; 6407 if (isHomogeneousAggregate(Ty, Base, Members)) 6408 return classifyHomogeneousAggregate(Ty, Base, Members); 6409 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { 6410 // WatchOS does have homogeneous aggregates. Note that we intentionally use 6411 // this convention even for a variadic function: the backend will use GPRs 6412 // if needed. 6413 const Type *Base = nullptr; 6414 uint64_t Members = 0; 6415 if (isHomogeneousAggregate(Ty, Base, Members)) { 6416 assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); 6417 llvm::Type *Ty = 6418 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); 6419 return ABIArgInfo::getDirect(Ty, 0, nullptr, false); 6420 } 6421 } 6422 6423 if (getABIKind() == ARMABIInfo::AAPCS16_VFP && 6424 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { 6425 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're 6426 // bigger than 128-bits, they get placed in space allocated by the caller, 6427 // and a pointer is passed. 6428 return ABIArgInfo::getIndirect( 6429 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); 6430 } 6431 6432 // Support byval for ARM. 6433 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at 6434 // most 8-byte. We realign the indirect argument if type alignment is bigger 6435 // than ABI alignment. 6436 uint64_t ABIAlign = 4; 6437 uint64_t TyAlign; 6438 if (getABIKind() == ARMABIInfo::AAPCS_VFP || 6439 getABIKind() == ARMABIInfo::AAPCS) { 6440 TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); 6441 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); 6442 } else { 6443 TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); 6444 } 6445 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { 6446 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); 6447 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), 6448 /*ByVal=*/true, 6449 /*Realign=*/TyAlign > ABIAlign); 6450 } 6451 6452 // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of 6453 // same size and alignment. 6454 if (getTarget().isRenderScriptTarget()) { 6455 return coerceToIntArray(Ty, getContext(), getVMContext()); 6456 } 6457 6458 // Otherwise, pass by coercing to a structure of the appropriate size. 6459 llvm::Type* ElemTy; 6460 unsigned SizeRegs; 6461 // FIXME: Try to match the types of the arguments more accurately where 6462 // we can. 6463 if (TyAlign <= 4) { 6464 ElemTy = llvm::Type::getInt32Ty(getVMContext()); 6465 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; 6466 } else { 6467 ElemTy = llvm::Type::getInt64Ty(getVMContext()); 6468 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; 6469 } 6470 6471 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); 6472 } 6473 6474 static bool isIntegerLikeType(QualType Ty, ASTContext &Context, 6475 llvm::LLVMContext &VMContext) { 6476 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure 6477 // is called integer-like if its size is less than or equal to one word, and 6478 // the offset of each of its addressable sub-fields is zero. 6479 6480 uint64_t Size = Context.getTypeSize(Ty); 6481 6482 // Check that the type fits in a word. 6483 if (Size > 32) 6484 return false; 6485 6486 // FIXME: Handle vector types! 6487 if (Ty->isVectorType()) 6488 return false; 6489 6490 // Float types are never treated as "integer like". 6491 if (Ty->isRealFloatingType()) 6492 return false; 6493 6494 // If this is a builtin or pointer type then it is ok. 6495 if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) 6496 return true; 6497 6498 // Small complex integer types are "integer like". 6499 if (const ComplexType *CT = Ty->getAs<ComplexType>()) 6500 return isIntegerLikeType(CT->getElementType(), Context, VMContext); 6501 6502 // Single element and zero sized arrays should be allowed, by the definition 6503 // above, but they are not. 6504 6505 // Otherwise, it must be a record type. 6506 const RecordType *RT = Ty->getAs<RecordType>(); 6507 if (!RT) return false; 6508 6509 // Ignore records with flexible arrays. 6510 const RecordDecl *RD = RT->getDecl(); 6511 if (RD->hasFlexibleArrayMember()) 6512 return false; 6513 6514 // Check that all sub-fields are at offset 0, and are themselves "integer 6515 // like". 6516 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 6517 6518 bool HadField = false; 6519 unsigned idx = 0; 6520 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 6521 i != e; ++i, ++idx) { 6522 const FieldDecl *FD = *i; 6523 6524 // Bit-fields are not addressable, we only need to verify they are "integer 6525 // like". We still have to disallow a subsequent non-bitfield, for example: 6526 // struct { int : 0; int x } 6527 // is non-integer like according to gcc. 6528 if (FD->isBitField()) { 6529 if (!RD->isUnion()) 6530 HadField = true; 6531 6532 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 6533 return false; 6534 6535 continue; 6536 } 6537 6538 // Check if this field is at offset 0. 6539 if (Layout.getFieldOffset(idx) != 0) 6540 return false; 6541 6542 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 6543 return false; 6544 6545 // Only allow at most one field in a structure. This doesn't match the 6546 // wording above, but follows gcc in situations with a field following an 6547 // empty structure. 6548 if (!RD->isUnion()) { 6549 if (HadField) 6550 return false; 6551 6552 HadField = true; 6553 } 6554 } 6555 6556 return true; 6557 } 6558 6559 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic, 6560 unsigned functionCallConv) const { 6561 6562 // Variadic functions should always marshal to the base standard. 6563 bool IsAAPCS_VFP = 6564 !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true); 6565 6566 if (RetTy->isVoidType()) 6567 return ABIArgInfo::getIgnore(); 6568 6569 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 6570 // Large vector types should be returned via memory. 6571 if (getContext().getTypeSize(RetTy) > 128) 6572 return getNaturalAlignIndirect(RetTy); 6573 // TODO: FP16/BF16 vectors should be converted to integer vectors 6574 // This check is similar to isIllegalVectorType - refactor? 6575 if ((!getTarget().hasLegalHalfType() && 6576 (VT->getElementType()->isFloat16Type() || 6577 VT->getElementType()->isHalfType())) || 6578 (IsFloatABISoftFP && 6579 VT->getElementType()->isBFloat16Type())) 6580 return coerceIllegalVector(RetTy); 6581 } 6582 6583 if (!isAggregateTypeForABI(RetTy)) { 6584 // Treat an enum type as its underlying type. 6585 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 6586 RetTy = EnumTy->getDecl()->getIntegerType(); 6587 6588 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 6589 if (EIT->getNumBits() > 64) 6590 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 6591 6592 return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 6593 : ABIArgInfo::getDirect(); 6594 } 6595 6596 // Are we following APCS? 6597 if (getABIKind() == APCS) { 6598 if (isEmptyRecord(getContext(), RetTy, false)) 6599 return ABIArgInfo::getIgnore(); 6600 6601 // Complex types are all returned as packed integers. 6602 // 6603 // FIXME: Consider using 2 x vector types if the back end handles them 6604 // correctly. 6605 if (RetTy->isAnyComplexType()) 6606 return ABIArgInfo::getDirect(llvm::IntegerType::get( 6607 getVMContext(), getContext().getTypeSize(RetTy))); 6608 6609 // Integer like structures are returned in r0. 6610 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { 6611 // Return in the smallest viable integer type. 6612 uint64_t Size = getContext().getTypeSize(RetTy); 6613 if (Size <= 8) 6614 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 6615 if (Size <= 16) 6616 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 6617 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6618 } 6619 6620 // Otherwise return in memory. 6621 return getNaturalAlignIndirect(RetTy); 6622 } 6623 6624 // Otherwise this is an AAPCS variant. 6625 6626 if (isEmptyRecord(getContext(), RetTy, true)) 6627 return ABIArgInfo::getIgnore(); 6628 6629 // Check for homogeneous aggregates with AAPCS-VFP. 6630 if (IsAAPCS_VFP) { 6631 const Type *Base = nullptr; 6632 uint64_t Members = 0; 6633 if (isHomogeneousAggregate(RetTy, Base, Members)) 6634 return classifyHomogeneousAggregate(RetTy, Base, Members); 6635 } 6636 6637 // Aggregates <= 4 bytes are returned in r0; other aggregates 6638 // are returned indirectly. 6639 uint64_t Size = getContext().getTypeSize(RetTy); 6640 if (Size <= 32) { 6641 // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of 6642 // same size and alignment. 6643 if (getTarget().isRenderScriptTarget()) { 6644 return coerceToIntArray(RetTy, getContext(), getVMContext()); 6645 } 6646 if (getDataLayout().isBigEndian()) 6647 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) 6648 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6649 6650 // Return in the smallest viable integer type. 6651 if (Size <= 8) 6652 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 6653 if (Size <= 16) 6654 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 6655 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6656 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { 6657 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); 6658 llvm::Type *CoerceTy = 6659 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); 6660 return ABIArgInfo::getDirect(CoerceTy); 6661 } 6662 6663 return getNaturalAlignIndirect(RetTy); 6664 } 6665 6666 /// isIllegalVector - check whether Ty is an illegal vector type. 6667 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { 6668 if (const VectorType *VT = Ty->getAs<VectorType> ()) { 6669 // On targets that don't support half, fp16 or bfloat, they are expanded 6670 // into float, and we don't want the ABI to depend on whether or not they 6671 // are supported in hardware. Thus return false to coerce vectors of these 6672 // types into integer vectors. 6673 // We do not depend on hasLegalHalfType for bfloat as it is a 6674 // separate IR type. 6675 if ((!getTarget().hasLegalHalfType() && 6676 (VT->getElementType()->isFloat16Type() || 6677 VT->getElementType()->isHalfType())) || 6678 (IsFloatABISoftFP && 6679 VT->getElementType()->isBFloat16Type())) 6680 return true; 6681 if (isAndroid()) { 6682 // Android shipped using Clang 3.1, which supported a slightly different 6683 // vector ABI. The primary differences were that 3-element vector types 6684 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path 6685 // accepts that legacy behavior for Android only. 6686 // Check whether VT is legal. 6687 unsigned NumElements = VT->getNumElements(); 6688 // NumElements should be power of 2 or equal to 3. 6689 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) 6690 return true; 6691 } else { 6692 // Check whether VT is legal. 6693 unsigned NumElements = VT->getNumElements(); 6694 uint64_t Size = getContext().getTypeSize(VT); 6695 // NumElements should be power of 2. 6696 if (!llvm::isPowerOf2_32(NumElements)) 6697 return true; 6698 // Size should be greater than 32 bits. 6699 return Size <= 32; 6700 } 6701 } 6702 return false; 6703 } 6704 6705 /// Return true if a type contains any 16-bit floating point vectors 6706 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const { 6707 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 6708 uint64_t NElements = AT->getSize().getZExtValue(); 6709 if (NElements == 0) 6710 return false; 6711 return containsAnyFP16Vectors(AT->getElementType()); 6712 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 6713 const RecordDecl *RD = RT->getDecl(); 6714 6715 // If this is a C++ record, check the bases first. 6716 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 6717 if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) { 6718 return containsAnyFP16Vectors(B.getType()); 6719 })) 6720 return true; 6721 6722 if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) { 6723 return FD && containsAnyFP16Vectors(FD->getType()); 6724 })) 6725 return true; 6726 6727 return false; 6728 } else { 6729 if (const VectorType *VT = Ty->getAs<VectorType>()) 6730 return (VT->getElementType()->isFloat16Type() || 6731 VT->getElementType()->isBFloat16Type() || 6732 VT->getElementType()->isHalfType()); 6733 return false; 6734 } 6735 } 6736 6737 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, 6738 llvm::Type *eltTy, 6739 unsigned numElts) const { 6740 if (!llvm::isPowerOf2_32(numElts)) 6741 return false; 6742 unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); 6743 if (size > 64) 6744 return false; 6745 if (vectorSize.getQuantity() != 8 && 6746 (vectorSize.getQuantity() != 16 || numElts == 1)) 6747 return false; 6748 return true; 6749 } 6750 6751 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 6752 // Homogeneous aggregates for AAPCS-VFP must have base types of float, 6753 // double, or 64-bit or 128-bit vectors. 6754 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 6755 if (BT->getKind() == BuiltinType::Float || 6756 BT->getKind() == BuiltinType::Double || 6757 BT->getKind() == BuiltinType::LongDouble) 6758 return true; 6759 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 6760 unsigned VecSize = getContext().getTypeSize(VT); 6761 if (VecSize == 64 || VecSize == 128) 6762 return true; 6763 } 6764 return false; 6765 } 6766 6767 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 6768 uint64_t Members) const { 6769 return Members <= 4; 6770 } 6771 6772 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention, 6773 bool acceptHalf) const { 6774 // Give precedence to user-specified calling conventions. 6775 if (callConvention != llvm::CallingConv::C) 6776 return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP); 6777 else 6778 return (getABIKind() == AAPCS_VFP) || 6779 (acceptHalf && (getABIKind() == AAPCS16_VFP)); 6780 } 6781 6782 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6783 QualType Ty) const { 6784 CharUnits SlotSize = CharUnits::fromQuantity(4); 6785 6786 // Empty records are ignored for parameter passing purposes. 6787 if (isEmptyRecord(getContext(), Ty, true)) { 6788 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); 6789 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 6790 return Addr; 6791 } 6792 6793 CharUnits TySize = getContext().getTypeSizeInChars(Ty); 6794 CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty); 6795 6796 // Use indirect if size of the illegal vector is bigger than 16 bytes. 6797 bool IsIndirect = false; 6798 const Type *Base = nullptr; 6799 uint64_t Members = 0; 6800 if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { 6801 IsIndirect = true; 6802 6803 // ARMv7k passes structs bigger than 16 bytes indirectly, in space 6804 // allocated by the caller. 6805 } else if (TySize > CharUnits::fromQuantity(16) && 6806 getABIKind() == ARMABIInfo::AAPCS16_VFP && 6807 !isHomogeneousAggregate(Ty, Base, Members)) { 6808 IsIndirect = true; 6809 6810 // Otherwise, bound the type's ABI alignment. 6811 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for 6812 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. 6813 // Our callers should be prepared to handle an under-aligned address. 6814 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || 6815 getABIKind() == ARMABIInfo::AAPCS) { 6816 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); 6817 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); 6818 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { 6819 // ARMv7k allows type alignment up to 16 bytes. 6820 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); 6821 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); 6822 } else { 6823 TyAlignForABI = CharUnits::fromQuantity(4); 6824 } 6825 6826 std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI }; 6827 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, 6828 SlotSize, /*AllowHigherAlign*/ true); 6829 } 6830 6831 //===----------------------------------------------------------------------===// 6832 // NVPTX ABI Implementation 6833 //===----------------------------------------------------------------------===// 6834 6835 namespace { 6836 6837 class NVPTXTargetCodeGenInfo; 6838 6839 class NVPTXABIInfo : public ABIInfo { 6840 NVPTXTargetCodeGenInfo &CGInfo; 6841 6842 public: 6843 NVPTXABIInfo(CodeGenTypes &CGT, NVPTXTargetCodeGenInfo &Info) 6844 : ABIInfo(CGT), CGInfo(Info) {} 6845 6846 ABIArgInfo classifyReturnType(QualType RetTy) const; 6847 ABIArgInfo classifyArgumentType(QualType Ty) const; 6848 6849 void computeInfo(CGFunctionInfo &FI) const override; 6850 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6851 QualType Ty) const override; 6852 bool isUnsupportedType(QualType T) const; 6853 ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, unsigned MaxSize) const; 6854 }; 6855 6856 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { 6857 public: 6858 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) 6859 : TargetCodeGenInfo(std::make_unique<NVPTXABIInfo>(CGT, *this)) {} 6860 6861 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6862 CodeGen::CodeGenModule &M) const override; 6863 bool shouldEmitStaticExternCAliases() const override; 6864 6865 llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const override { 6866 // On the device side, surface reference is represented as an object handle 6867 // in 64-bit integer. 6868 return llvm::Type::getInt64Ty(getABIInfo().getVMContext()); 6869 } 6870 6871 llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const override { 6872 // On the device side, texture reference is represented as an object handle 6873 // in 64-bit integer. 6874 return llvm::Type::getInt64Ty(getABIInfo().getVMContext()); 6875 } 6876 6877 bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF, LValue Dst, 6878 LValue Src) const override { 6879 emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src); 6880 return true; 6881 } 6882 6883 bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF, LValue Dst, 6884 LValue Src) const override { 6885 emitBuiltinSurfTexDeviceCopy(CGF, Dst, Src); 6886 return true; 6887 } 6888 6889 private: 6890 // Adds a NamedMDNode with GV, Name, and Operand as operands, and adds the 6891 // resulting MDNode to the nvvm.annotations MDNode. 6892 static void addNVVMMetadata(llvm::GlobalValue *GV, StringRef Name, 6893 int Operand); 6894 6895 static void emitBuiltinSurfTexDeviceCopy(CodeGenFunction &CGF, LValue Dst, 6896 LValue Src) { 6897 llvm::Value *Handle = nullptr; 6898 llvm::Constant *C = 6899 llvm::dyn_cast<llvm::Constant>(Src.getAddress(CGF).getPointer()); 6900 // Lookup `addrspacecast` through the constant pointer if any. 6901 if (auto *ASC = llvm::dyn_cast_or_null<llvm::AddrSpaceCastOperator>(C)) 6902 C = llvm::cast<llvm::Constant>(ASC->getPointerOperand()); 6903 if (auto *GV = llvm::dyn_cast_or_null<llvm::GlobalVariable>(C)) { 6904 // Load the handle from the specific global variable using 6905 // `nvvm.texsurf.handle.internal` intrinsic. 6906 Handle = CGF.EmitRuntimeCall( 6907 CGF.CGM.getIntrinsic(llvm::Intrinsic::nvvm_texsurf_handle_internal, 6908 {GV->getType()}), 6909 {GV}, "texsurf_handle"); 6910 } else 6911 Handle = CGF.EmitLoadOfScalar(Src, SourceLocation()); 6912 CGF.EmitStoreOfScalar(Handle, Dst); 6913 } 6914 }; 6915 6916 /// Checks if the type is unsupported directly by the current target. 6917 bool NVPTXABIInfo::isUnsupportedType(QualType T) const { 6918 ASTContext &Context = getContext(); 6919 if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type()) 6920 return true; 6921 if (!Context.getTargetInfo().hasFloat128Type() && 6922 (T->isFloat128Type() || 6923 (T->isRealFloatingType() && Context.getTypeSize(T) == 128))) 6924 return true; 6925 if (const auto *EIT = T->getAs<ExtIntType>()) 6926 return EIT->getNumBits() > 6927 (Context.getTargetInfo().hasInt128Type() ? 128U : 64U); 6928 if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() && 6929 Context.getTypeSize(T) > 64U) 6930 return true; 6931 if (const auto *AT = T->getAsArrayTypeUnsafe()) 6932 return isUnsupportedType(AT->getElementType()); 6933 const auto *RT = T->getAs<RecordType>(); 6934 if (!RT) 6935 return false; 6936 const RecordDecl *RD = RT->getDecl(); 6937 6938 // If this is a C++ record, check the bases first. 6939 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 6940 for (const CXXBaseSpecifier &I : CXXRD->bases()) 6941 if (isUnsupportedType(I.getType())) 6942 return true; 6943 6944 for (const FieldDecl *I : RD->fields()) 6945 if (isUnsupportedType(I->getType())) 6946 return true; 6947 return false; 6948 } 6949 6950 /// Coerce the given type into an array with maximum allowed size of elements. 6951 ABIArgInfo NVPTXABIInfo::coerceToIntArrayWithLimit(QualType Ty, 6952 unsigned MaxSize) const { 6953 // Alignment and Size are measured in bits. 6954 const uint64_t Size = getContext().getTypeSize(Ty); 6955 const uint64_t Alignment = getContext().getTypeAlign(Ty); 6956 const unsigned Div = std::min<unsigned>(MaxSize, Alignment); 6957 llvm::Type *IntType = llvm::Type::getIntNTy(getVMContext(), Div); 6958 const uint64_t NumElements = (Size + Div - 1) / Div; 6959 return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); 6960 } 6961 6962 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { 6963 if (RetTy->isVoidType()) 6964 return ABIArgInfo::getIgnore(); 6965 6966 if (getContext().getLangOpts().OpenMP && 6967 getContext().getLangOpts().OpenMPIsDevice && isUnsupportedType(RetTy)) 6968 return coerceToIntArrayWithLimit(RetTy, 64); 6969 6970 // note: this is different from default ABI 6971 if (!RetTy->isScalarType()) 6972 return ABIArgInfo::getDirect(); 6973 6974 // Treat an enum type as its underlying type. 6975 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 6976 RetTy = EnumTy->getDecl()->getIntegerType(); 6977 6978 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 6979 : ABIArgInfo::getDirect()); 6980 } 6981 6982 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { 6983 // Treat an enum type as its underlying type. 6984 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 6985 Ty = EnumTy->getDecl()->getIntegerType(); 6986 6987 // Return aggregates type as indirect by value 6988 if (isAggregateTypeForABI(Ty)) { 6989 // Under CUDA device compilation, tex/surf builtin types are replaced with 6990 // object types and passed directly. 6991 if (getContext().getLangOpts().CUDAIsDevice) { 6992 if (Ty->isCUDADeviceBuiltinSurfaceType()) 6993 return ABIArgInfo::getDirect( 6994 CGInfo.getCUDADeviceBuiltinSurfaceDeviceType()); 6995 if (Ty->isCUDADeviceBuiltinTextureType()) 6996 return ABIArgInfo::getDirect( 6997 CGInfo.getCUDADeviceBuiltinTextureDeviceType()); 6998 } 6999 return getNaturalAlignIndirect(Ty, /* byval */ true); 7000 } 7001 7002 if (const auto *EIT = Ty->getAs<ExtIntType>()) { 7003 if ((EIT->getNumBits() > 128) || 7004 (!getContext().getTargetInfo().hasInt128Type() && 7005 EIT->getNumBits() > 64)) 7006 return getNaturalAlignIndirect(Ty, /* byval */ true); 7007 } 7008 7009 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 7010 : ABIArgInfo::getDirect()); 7011 } 7012 7013 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { 7014 if (!getCXXABI().classifyReturnType(FI)) 7015 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 7016 for (auto &I : FI.arguments()) 7017 I.info = classifyArgumentType(I.type); 7018 7019 // Always honor user-specified calling convention. 7020 if (FI.getCallingConvention() != llvm::CallingConv::C) 7021 return; 7022 7023 FI.setEffectiveCallingConvention(getRuntimeCC()); 7024 } 7025 7026 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7027 QualType Ty) const { 7028 llvm_unreachable("NVPTX does not support varargs"); 7029 } 7030 7031 void NVPTXTargetCodeGenInfo::setTargetAttributes( 7032 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 7033 if (GV->isDeclaration()) 7034 return; 7035 const VarDecl *VD = dyn_cast_or_null<VarDecl>(D); 7036 if (VD) { 7037 if (M.getLangOpts().CUDA) { 7038 if (VD->getType()->isCUDADeviceBuiltinSurfaceType()) 7039 addNVVMMetadata(GV, "surface", 1); 7040 else if (VD->getType()->isCUDADeviceBuiltinTextureType()) 7041 addNVVMMetadata(GV, "texture", 1); 7042 return; 7043 } 7044 } 7045 7046 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 7047 if (!FD) return; 7048 7049 llvm::Function *F = cast<llvm::Function>(GV); 7050 7051 // Perform special handling in OpenCL mode 7052 if (M.getLangOpts().OpenCL) { 7053 // Use OpenCL function attributes to check for kernel functions 7054 // By default, all functions are device functions 7055 if (FD->hasAttr<OpenCLKernelAttr>()) { 7056 // OpenCL __kernel functions get kernel metadata 7057 // Create !{<func-ref>, metadata !"kernel", i32 1} node 7058 addNVVMMetadata(F, "kernel", 1); 7059 // And kernel functions are not subject to inlining 7060 F->addFnAttr(llvm::Attribute::NoInline); 7061 } 7062 } 7063 7064 // Perform special handling in CUDA mode. 7065 if (M.getLangOpts().CUDA) { 7066 // CUDA __global__ functions get a kernel metadata entry. Since 7067 // __global__ functions cannot be called from the device, we do not 7068 // need to set the noinline attribute. 7069 if (FD->hasAttr<CUDAGlobalAttr>()) { 7070 // Create !{<func-ref>, metadata !"kernel", i32 1} node 7071 addNVVMMetadata(F, "kernel", 1); 7072 } 7073 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { 7074 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node 7075 llvm::APSInt MaxThreads(32); 7076 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); 7077 if (MaxThreads > 0) 7078 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); 7079 7080 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was 7081 // not specified in __launch_bounds__ or if the user specified a 0 value, 7082 // we don't have to add a PTX directive. 7083 if (Attr->getMinBlocks()) { 7084 llvm::APSInt MinBlocks(32); 7085 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); 7086 if (MinBlocks > 0) 7087 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node 7088 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); 7089 } 7090 } 7091 } 7092 } 7093 7094 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::GlobalValue *GV, 7095 StringRef Name, int Operand) { 7096 llvm::Module *M = GV->getParent(); 7097 llvm::LLVMContext &Ctx = M->getContext(); 7098 7099 // Get "nvvm.annotations" metadata node 7100 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); 7101 7102 llvm::Metadata *MDVals[] = { 7103 llvm::ConstantAsMetadata::get(GV), llvm::MDString::get(Ctx, Name), 7104 llvm::ConstantAsMetadata::get( 7105 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; 7106 // Append metadata to nvvm.annotations 7107 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 7108 } 7109 7110 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { 7111 return false; 7112 } 7113 } 7114 7115 //===----------------------------------------------------------------------===// 7116 // SystemZ ABI Implementation 7117 //===----------------------------------------------------------------------===// 7118 7119 namespace { 7120 7121 class SystemZABIInfo : public SwiftABIInfo { 7122 bool HasVector; 7123 bool IsSoftFloatABI; 7124 7125 public: 7126 SystemZABIInfo(CodeGenTypes &CGT, bool HV, bool SF) 7127 : SwiftABIInfo(CGT), HasVector(HV), IsSoftFloatABI(SF) {} 7128 7129 bool isPromotableIntegerTypeForABI(QualType Ty) const; 7130 bool isCompoundType(QualType Ty) const; 7131 bool isVectorArgumentType(QualType Ty) const; 7132 bool isFPArgumentType(QualType Ty) const; 7133 QualType GetSingleElementType(QualType Ty) const; 7134 7135 ABIArgInfo classifyReturnType(QualType RetTy) const; 7136 ABIArgInfo classifyArgumentType(QualType ArgTy) const; 7137 7138 void computeInfo(CGFunctionInfo &FI) const override { 7139 if (!getCXXABI().classifyReturnType(FI)) 7140 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 7141 for (auto &I : FI.arguments()) 7142 I.info = classifyArgumentType(I.type); 7143 } 7144 7145 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7146 QualType Ty) const override; 7147 7148 bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, 7149 bool asReturnValue) const override { 7150 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 7151 } 7152 bool isSwiftErrorInRegister() const override { 7153 return false; 7154 } 7155 }; 7156 7157 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { 7158 public: 7159 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector, bool SoftFloatABI) 7160 : TargetCodeGenInfo( 7161 std::make_unique<SystemZABIInfo>(CGT, HasVector, SoftFloatABI)) {} 7162 }; 7163 7164 } 7165 7166 bool SystemZABIInfo::isPromotableIntegerTypeForABI(QualType Ty) const { 7167 // Treat an enum type as its underlying type. 7168 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 7169 Ty = EnumTy->getDecl()->getIntegerType(); 7170 7171 // Promotable integer types are required to be promoted by the ABI. 7172 if (ABIInfo::isPromotableIntegerTypeForABI(Ty)) 7173 return true; 7174 7175 if (const auto *EIT = Ty->getAs<ExtIntType>()) 7176 if (EIT->getNumBits() < 64) 7177 return true; 7178 7179 // 32-bit values must also be promoted. 7180 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 7181 switch (BT->getKind()) { 7182 case BuiltinType::Int: 7183 case BuiltinType::UInt: 7184 return true; 7185 default: 7186 return false; 7187 } 7188 return false; 7189 } 7190 7191 bool SystemZABIInfo::isCompoundType(QualType Ty) const { 7192 return (Ty->isAnyComplexType() || 7193 Ty->isVectorType() || 7194 isAggregateTypeForABI(Ty)); 7195 } 7196 7197 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { 7198 return (HasVector && 7199 Ty->isVectorType() && 7200 getContext().getTypeSize(Ty) <= 128); 7201 } 7202 7203 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { 7204 if (IsSoftFloatABI) 7205 return false; 7206 7207 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 7208 switch (BT->getKind()) { 7209 case BuiltinType::Float: 7210 case BuiltinType::Double: 7211 return true; 7212 default: 7213 return false; 7214 } 7215 7216 return false; 7217 } 7218 7219 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { 7220 const RecordType *RT = Ty->getAs<RecordType>(); 7221 7222 if (RT && RT->isStructureOrClassType()) { 7223 const RecordDecl *RD = RT->getDecl(); 7224 QualType Found; 7225 7226 // If this is a C++ record, check the bases first. 7227 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 7228 for (const auto &I : CXXRD->bases()) { 7229 QualType Base = I.getType(); 7230 7231 // Empty bases don't affect things either way. 7232 if (isEmptyRecord(getContext(), Base, true)) 7233 continue; 7234 7235 if (!Found.isNull()) 7236 return Ty; 7237 Found = GetSingleElementType(Base); 7238 } 7239 7240 // Check the fields. 7241 for (const auto *FD : RD->fields()) { 7242 // For compatibility with GCC, ignore empty bitfields in C++ mode. 7243 // Unlike isSingleElementStruct(), empty structure and array fields 7244 // do count. So do anonymous bitfields that aren't zero-sized. 7245 if (getContext().getLangOpts().CPlusPlus && 7246 FD->isZeroLengthBitField(getContext())) 7247 continue; 7248 // Like isSingleElementStruct(), ignore C++20 empty data members. 7249 if (FD->hasAttr<NoUniqueAddressAttr>() && 7250 isEmptyRecord(getContext(), FD->getType(), true)) 7251 continue; 7252 7253 // Unlike isSingleElementStruct(), arrays do not count. 7254 // Nested structures still do though. 7255 if (!Found.isNull()) 7256 return Ty; 7257 Found = GetSingleElementType(FD->getType()); 7258 } 7259 7260 // Unlike isSingleElementStruct(), trailing padding is allowed. 7261 // An 8-byte aligned struct s { float f; } is passed as a double. 7262 if (!Found.isNull()) 7263 return Found; 7264 } 7265 7266 return Ty; 7267 } 7268 7269 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7270 QualType Ty) const { 7271 // Assume that va_list type is correct; should be pointer to LLVM type: 7272 // struct { 7273 // i64 __gpr; 7274 // i64 __fpr; 7275 // i8 *__overflow_arg_area; 7276 // i8 *__reg_save_area; 7277 // }; 7278 7279 // Every non-vector argument occupies 8 bytes and is passed by preference 7280 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are 7281 // always passed on the stack. 7282 Ty = getContext().getCanonicalType(Ty); 7283 auto TyInfo = getContext().getTypeInfoInChars(Ty); 7284 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); 7285 llvm::Type *DirectTy = ArgTy; 7286 ABIArgInfo AI = classifyArgumentType(Ty); 7287 bool IsIndirect = AI.isIndirect(); 7288 bool InFPRs = false; 7289 bool IsVector = false; 7290 CharUnits UnpaddedSize; 7291 CharUnits DirectAlign; 7292 if (IsIndirect) { 7293 DirectTy = llvm::PointerType::getUnqual(DirectTy); 7294 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); 7295 } else { 7296 if (AI.getCoerceToType()) 7297 ArgTy = AI.getCoerceToType(); 7298 InFPRs = (!IsSoftFloatABI && (ArgTy->isFloatTy() || ArgTy->isDoubleTy())); 7299 IsVector = ArgTy->isVectorTy(); 7300 UnpaddedSize = TyInfo.first; 7301 DirectAlign = TyInfo.second; 7302 } 7303 CharUnits PaddedSize = CharUnits::fromQuantity(8); 7304 if (IsVector && UnpaddedSize > PaddedSize) 7305 PaddedSize = CharUnits::fromQuantity(16); 7306 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); 7307 7308 CharUnits Padding = (PaddedSize - UnpaddedSize); 7309 7310 llvm::Type *IndexTy = CGF.Int64Ty; 7311 llvm::Value *PaddedSizeV = 7312 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); 7313 7314 if (IsVector) { 7315 // Work out the address of a vector argument on the stack. 7316 // Vector arguments are always passed in the high bits of a 7317 // single (8 byte) or double (16 byte) stack slot. 7318 Address OverflowArgAreaPtr = 7319 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); 7320 Address OverflowArgArea = 7321 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), 7322 TyInfo.second); 7323 Address MemAddr = 7324 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); 7325 7326 // Update overflow_arg_area_ptr pointer 7327 llvm::Value *NewOverflowArgArea = 7328 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, 7329 "overflow_arg_area"); 7330 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 7331 7332 return MemAddr; 7333 } 7334 7335 assert(PaddedSize.getQuantity() == 8); 7336 7337 unsigned MaxRegs, RegCountField, RegSaveIndex; 7338 CharUnits RegPadding; 7339 if (InFPRs) { 7340 MaxRegs = 4; // Maximum of 4 FPR arguments 7341 RegCountField = 1; // __fpr 7342 RegSaveIndex = 16; // save offset for f0 7343 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR 7344 } else { 7345 MaxRegs = 5; // Maximum of 5 GPR arguments 7346 RegCountField = 0; // __gpr 7347 RegSaveIndex = 2; // save offset for r2 7348 RegPadding = Padding; // values are passed in the low bits of a GPR 7349 } 7350 7351 Address RegCountPtr = 7352 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); 7353 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); 7354 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); 7355 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, 7356 "fits_in_regs"); 7357 7358 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 7359 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 7360 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 7361 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 7362 7363 // Emit code to load the value if it was passed in registers. 7364 CGF.EmitBlock(InRegBlock); 7365 7366 // Work out the address of an argument register. 7367 llvm::Value *ScaledRegCount = 7368 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); 7369 llvm::Value *RegBase = 7370 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() 7371 + RegPadding.getQuantity()); 7372 llvm::Value *RegOffset = 7373 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); 7374 Address RegSaveAreaPtr = 7375 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); 7376 llvm::Value *RegSaveArea = 7377 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); 7378 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, 7379 "raw_reg_addr"), 7380 PaddedSize); 7381 Address RegAddr = 7382 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); 7383 7384 // Update the register count 7385 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); 7386 llvm::Value *NewRegCount = 7387 CGF.Builder.CreateAdd(RegCount, One, "reg_count"); 7388 CGF.Builder.CreateStore(NewRegCount, RegCountPtr); 7389 CGF.EmitBranch(ContBlock); 7390 7391 // Emit code to load the value if it was passed in memory. 7392 CGF.EmitBlock(InMemBlock); 7393 7394 // Work out the address of a stack argument. 7395 Address OverflowArgAreaPtr = 7396 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); 7397 Address OverflowArgArea = 7398 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), 7399 PaddedSize); 7400 Address RawMemAddr = 7401 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); 7402 Address MemAddr = 7403 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); 7404 7405 // Update overflow_arg_area_ptr pointer 7406 llvm::Value *NewOverflowArgArea = 7407 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, 7408 "overflow_arg_area"); 7409 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 7410 CGF.EmitBranch(ContBlock); 7411 7412 // Return the appropriate result. 7413 CGF.EmitBlock(ContBlock); 7414 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, 7415 MemAddr, InMemBlock, "va_arg.addr"); 7416 7417 if (IsIndirect) 7418 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), 7419 TyInfo.second); 7420 7421 return ResAddr; 7422 } 7423 7424 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { 7425 if (RetTy->isVoidType()) 7426 return ABIArgInfo::getIgnore(); 7427 if (isVectorArgumentType(RetTy)) 7428 return ABIArgInfo::getDirect(); 7429 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) 7430 return getNaturalAlignIndirect(RetTy); 7431 return (isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 7432 : ABIArgInfo::getDirect()); 7433 } 7434 7435 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { 7436 // Handle the generic C++ ABI. 7437 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 7438 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 7439 7440 // Integers and enums are extended to full register width. 7441 if (isPromotableIntegerTypeForABI(Ty)) 7442 return ABIArgInfo::getExtend(Ty); 7443 7444 // Handle vector types and vector-like structure types. Note that 7445 // as opposed to float-like structure types, we do not allow any 7446 // padding for vector-like structures, so verify the sizes match. 7447 uint64_t Size = getContext().getTypeSize(Ty); 7448 QualType SingleElementTy = GetSingleElementType(Ty); 7449 if (isVectorArgumentType(SingleElementTy) && 7450 getContext().getTypeSize(SingleElementTy) == Size) 7451 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); 7452 7453 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. 7454 if (Size != 8 && Size != 16 && Size != 32 && Size != 64) 7455 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 7456 7457 // Handle small structures. 7458 if (const RecordType *RT = Ty->getAs<RecordType>()) { 7459 // Structures with flexible arrays have variable length, so really 7460 // fail the size test above. 7461 const RecordDecl *RD = RT->getDecl(); 7462 if (RD->hasFlexibleArrayMember()) 7463 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 7464 7465 // The structure is passed as an unextended integer, a float, or a double. 7466 llvm::Type *PassTy; 7467 if (isFPArgumentType(SingleElementTy)) { 7468 assert(Size == 32 || Size == 64); 7469 if (Size == 32) 7470 PassTy = llvm::Type::getFloatTy(getVMContext()); 7471 else 7472 PassTy = llvm::Type::getDoubleTy(getVMContext()); 7473 } else 7474 PassTy = llvm::IntegerType::get(getVMContext(), Size); 7475 return ABIArgInfo::getDirect(PassTy); 7476 } 7477 7478 // Non-structure compounds are passed indirectly. 7479 if (isCompoundType(Ty)) 7480 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 7481 7482 return ABIArgInfo::getDirect(nullptr); 7483 } 7484 7485 //===----------------------------------------------------------------------===// 7486 // MSP430 ABI Implementation 7487 //===----------------------------------------------------------------------===// 7488 7489 namespace { 7490 7491 class MSP430ABIInfo : public DefaultABIInfo { 7492 static ABIArgInfo complexArgInfo() { 7493 ABIArgInfo Info = ABIArgInfo::getDirect(); 7494 Info.setCanBeFlattened(false); 7495 return Info; 7496 } 7497 7498 public: 7499 MSP430ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 7500 7501 ABIArgInfo classifyReturnType(QualType RetTy) const { 7502 if (RetTy->isAnyComplexType()) 7503 return complexArgInfo(); 7504 7505 return DefaultABIInfo::classifyReturnType(RetTy); 7506 } 7507 7508 ABIArgInfo classifyArgumentType(QualType RetTy) const { 7509 if (RetTy->isAnyComplexType()) 7510 return complexArgInfo(); 7511 7512 return DefaultABIInfo::classifyArgumentType(RetTy); 7513 } 7514 7515 // Just copy the original implementations because 7516 // DefaultABIInfo::classify{Return,Argument}Type() are not virtual 7517 void computeInfo(CGFunctionInfo &FI) const override { 7518 if (!getCXXABI().classifyReturnType(FI)) 7519 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 7520 for (auto &I : FI.arguments()) 7521 I.info = classifyArgumentType(I.type); 7522 } 7523 7524 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7525 QualType Ty) const override { 7526 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); 7527 } 7528 }; 7529 7530 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { 7531 public: 7532 MSP430TargetCodeGenInfo(CodeGenTypes &CGT) 7533 : TargetCodeGenInfo(std::make_unique<MSP430ABIInfo>(CGT)) {} 7534 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 7535 CodeGen::CodeGenModule &M) const override; 7536 }; 7537 7538 } 7539 7540 void MSP430TargetCodeGenInfo::setTargetAttributes( 7541 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 7542 if (GV->isDeclaration()) 7543 return; 7544 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 7545 const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); 7546 if (!InterruptAttr) 7547 return; 7548 7549 // Handle 'interrupt' attribute: 7550 llvm::Function *F = cast<llvm::Function>(GV); 7551 7552 // Step 1: Set ISR calling convention. 7553 F->setCallingConv(llvm::CallingConv::MSP430_INTR); 7554 7555 // Step 2: Add attributes goodness. 7556 F->addFnAttr(llvm::Attribute::NoInline); 7557 F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); 7558 } 7559 } 7560 7561 //===----------------------------------------------------------------------===// 7562 // MIPS ABI Implementation. This works for both little-endian and 7563 // big-endian variants. 7564 //===----------------------------------------------------------------------===// 7565 7566 namespace { 7567 class MipsABIInfo : public ABIInfo { 7568 bool IsO32; 7569 unsigned MinABIStackAlignInBytes, StackAlignInBytes; 7570 void CoerceToIntArgs(uint64_t TySize, 7571 SmallVectorImpl<llvm::Type *> &ArgList) const; 7572 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; 7573 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; 7574 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; 7575 public: 7576 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : 7577 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), 7578 StackAlignInBytes(IsO32 ? 8 : 16) {} 7579 7580 ABIArgInfo classifyReturnType(QualType RetTy) const; 7581 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; 7582 void computeInfo(CGFunctionInfo &FI) const override; 7583 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7584 QualType Ty) const override; 7585 ABIArgInfo extendType(QualType Ty) const; 7586 }; 7587 7588 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { 7589 unsigned SizeOfUnwindException; 7590 public: 7591 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) 7592 : TargetCodeGenInfo(std::make_unique<MipsABIInfo>(CGT, IsO32)), 7593 SizeOfUnwindException(IsO32 ? 24 : 32) {} 7594 7595 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 7596 return 29; 7597 } 7598 7599 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 7600 CodeGen::CodeGenModule &CGM) const override { 7601 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 7602 if (!FD) return; 7603 llvm::Function *Fn = cast<llvm::Function>(GV); 7604 7605 if (FD->hasAttr<MipsLongCallAttr>()) 7606 Fn->addFnAttr("long-call"); 7607 else if (FD->hasAttr<MipsShortCallAttr>()) 7608 Fn->addFnAttr("short-call"); 7609 7610 // Other attributes do not have a meaning for declarations. 7611 if (GV->isDeclaration()) 7612 return; 7613 7614 if (FD->hasAttr<Mips16Attr>()) { 7615 Fn->addFnAttr("mips16"); 7616 } 7617 else if (FD->hasAttr<NoMips16Attr>()) { 7618 Fn->addFnAttr("nomips16"); 7619 } 7620 7621 if (FD->hasAttr<MicroMipsAttr>()) 7622 Fn->addFnAttr("micromips"); 7623 else if (FD->hasAttr<NoMicroMipsAttr>()) 7624 Fn->addFnAttr("nomicromips"); 7625 7626 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); 7627 if (!Attr) 7628 return; 7629 7630 const char *Kind; 7631 switch (Attr->getInterrupt()) { 7632 case MipsInterruptAttr::eic: Kind = "eic"; break; 7633 case MipsInterruptAttr::sw0: Kind = "sw0"; break; 7634 case MipsInterruptAttr::sw1: Kind = "sw1"; break; 7635 case MipsInterruptAttr::hw0: Kind = "hw0"; break; 7636 case MipsInterruptAttr::hw1: Kind = "hw1"; break; 7637 case MipsInterruptAttr::hw2: Kind = "hw2"; break; 7638 case MipsInterruptAttr::hw3: Kind = "hw3"; break; 7639 case MipsInterruptAttr::hw4: Kind = "hw4"; break; 7640 case MipsInterruptAttr::hw5: Kind = "hw5"; break; 7641 } 7642 7643 Fn->addFnAttr("interrupt", Kind); 7644 7645 } 7646 7647 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 7648 llvm::Value *Address) const override; 7649 7650 unsigned getSizeOfUnwindException() const override { 7651 return SizeOfUnwindException; 7652 } 7653 }; 7654 } 7655 7656 void MipsABIInfo::CoerceToIntArgs( 7657 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { 7658 llvm::IntegerType *IntTy = 7659 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 7660 7661 // Add (TySize / MinABIStackAlignInBytes) args of IntTy. 7662 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) 7663 ArgList.push_back(IntTy); 7664 7665 // If necessary, add one more integer type to ArgList. 7666 unsigned R = TySize % (MinABIStackAlignInBytes * 8); 7667 7668 if (R) 7669 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); 7670 } 7671 7672 // In N32/64, an aligned double precision floating point field is passed in 7673 // a register. 7674 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { 7675 SmallVector<llvm::Type*, 8> ArgList, IntArgList; 7676 7677 if (IsO32) { 7678 CoerceToIntArgs(TySize, ArgList); 7679 return llvm::StructType::get(getVMContext(), ArgList); 7680 } 7681 7682 if (Ty->isComplexType()) 7683 return CGT.ConvertType(Ty); 7684 7685 const RecordType *RT = Ty->getAs<RecordType>(); 7686 7687 // Unions/vectors are passed in integer registers. 7688 if (!RT || !RT->isStructureOrClassType()) { 7689 CoerceToIntArgs(TySize, ArgList); 7690 return llvm::StructType::get(getVMContext(), ArgList); 7691 } 7692 7693 const RecordDecl *RD = RT->getDecl(); 7694 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 7695 assert(!(TySize % 8) && "Size of structure must be multiple of 8."); 7696 7697 uint64_t LastOffset = 0; 7698 unsigned idx = 0; 7699 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); 7700 7701 // Iterate over fields in the struct/class and check if there are any aligned 7702 // double fields. 7703 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 7704 i != e; ++i, ++idx) { 7705 const QualType Ty = i->getType(); 7706 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 7707 7708 if (!BT || BT->getKind() != BuiltinType::Double) 7709 continue; 7710 7711 uint64_t Offset = Layout.getFieldOffset(idx); 7712 if (Offset % 64) // Ignore doubles that are not aligned. 7713 continue; 7714 7715 // Add ((Offset - LastOffset) / 64) args of type i64. 7716 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) 7717 ArgList.push_back(I64); 7718 7719 // Add double type. 7720 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); 7721 LastOffset = Offset + 64; 7722 } 7723 7724 CoerceToIntArgs(TySize - LastOffset, IntArgList); 7725 ArgList.append(IntArgList.begin(), IntArgList.end()); 7726 7727 return llvm::StructType::get(getVMContext(), ArgList); 7728 } 7729 7730 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, 7731 uint64_t Offset) const { 7732 if (OrigOffset + MinABIStackAlignInBytes > Offset) 7733 return nullptr; 7734 7735 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); 7736 } 7737 7738 ABIArgInfo 7739 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { 7740 Ty = useFirstFieldIfTransparentUnion(Ty); 7741 7742 uint64_t OrigOffset = Offset; 7743 uint64_t TySize = getContext().getTypeSize(Ty); 7744 uint64_t Align = getContext().getTypeAlign(Ty) / 8; 7745 7746 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), 7747 (uint64_t)StackAlignInBytes); 7748 unsigned CurrOffset = llvm::alignTo(Offset, Align); 7749 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; 7750 7751 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { 7752 // Ignore empty aggregates. 7753 if (TySize == 0) 7754 return ABIArgInfo::getIgnore(); 7755 7756 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 7757 Offset = OrigOffset + MinABIStackAlignInBytes; 7758 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 7759 } 7760 7761 // If we have reached here, aggregates are passed directly by coercing to 7762 // another structure type. Padding is inserted if the offset of the 7763 // aggregate is unaligned. 7764 ABIArgInfo ArgInfo = 7765 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, 7766 getPaddingType(OrigOffset, CurrOffset)); 7767 ArgInfo.setInReg(true); 7768 return ArgInfo; 7769 } 7770 7771 // Treat an enum type as its underlying type. 7772 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 7773 Ty = EnumTy->getDecl()->getIntegerType(); 7774 7775 // Make sure we pass indirectly things that are too large. 7776 if (const auto *EIT = Ty->getAs<ExtIntType>()) 7777 if (EIT->getNumBits() > 128 || 7778 (EIT->getNumBits() > 64 && 7779 !getContext().getTargetInfo().hasInt128Type())) 7780 return getNaturalAlignIndirect(Ty); 7781 7782 // All integral types are promoted to the GPR width. 7783 if (Ty->isIntegralOrEnumerationType()) 7784 return extendType(Ty); 7785 7786 return ABIArgInfo::getDirect( 7787 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); 7788 } 7789 7790 llvm::Type* 7791 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { 7792 const RecordType *RT = RetTy->getAs<RecordType>(); 7793 SmallVector<llvm::Type*, 8> RTList; 7794 7795 if (RT && RT->isStructureOrClassType()) { 7796 const RecordDecl *RD = RT->getDecl(); 7797 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 7798 unsigned FieldCnt = Layout.getFieldCount(); 7799 7800 // N32/64 returns struct/classes in floating point registers if the 7801 // following conditions are met: 7802 // 1. The size of the struct/class is no larger than 128-bit. 7803 // 2. The struct/class has one or two fields all of which are floating 7804 // point types. 7805 // 3. The offset of the first field is zero (this follows what gcc does). 7806 // 7807 // Any other composite results are returned in integer registers. 7808 // 7809 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { 7810 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); 7811 for (; b != e; ++b) { 7812 const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); 7813 7814 if (!BT || !BT->isFloatingPoint()) 7815 break; 7816 7817 RTList.push_back(CGT.ConvertType(b->getType())); 7818 } 7819 7820 if (b == e) 7821 return llvm::StructType::get(getVMContext(), RTList, 7822 RD->hasAttr<PackedAttr>()); 7823 7824 RTList.clear(); 7825 } 7826 } 7827 7828 CoerceToIntArgs(Size, RTList); 7829 return llvm::StructType::get(getVMContext(), RTList); 7830 } 7831 7832 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { 7833 uint64_t Size = getContext().getTypeSize(RetTy); 7834 7835 if (RetTy->isVoidType()) 7836 return ABIArgInfo::getIgnore(); 7837 7838 // O32 doesn't treat zero-sized structs differently from other structs. 7839 // However, N32/N64 ignores zero sized return values. 7840 if (!IsO32 && Size == 0) 7841 return ABIArgInfo::getIgnore(); 7842 7843 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { 7844 if (Size <= 128) { 7845 if (RetTy->isAnyComplexType()) 7846 return ABIArgInfo::getDirect(); 7847 7848 // O32 returns integer vectors in registers and N32/N64 returns all small 7849 // aggregates in registers. 7850 if (!IsO32 || 7851 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { 7852 ABIArgInfo ArgInfo = 7853 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 7854 ArgInfo.setInReg(true); 7855 return ArgInfo; 7856 } 7857 } 7858 7859 return getNaturalAlignIndirect(RetTy); 7860 } 7861 7862 // Treat an enum type as its underlying type. 7863 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 7864 RetTy = EnumTy->getDecl()->getIntegerType(); 7865 7866 // Make sure we pass indirectly things that are too large. 7867 if (const auto *EIT = RetTy->getAs<ExtIntType>()) 7868 if (EIT->getNumBits() > 128 || 7869 (EIT->getNumBits() > 64 && 7870 !getContext().getTargetInfo().hasInt128Type())) 7871 return getNaturalAlignIndirect(RetTy); 7872 7873 if (isPromotableIntegerTypeForABI(RetTy)) 7874 return ABIArgInfo::getExtend(RetTy); 7875 7876 if ((RetTy->isUnsignedIntegerOrEnumerationType() || 7877 RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) 7878 return ABIArgInfo::getSignExtend(RetTy); 7879 7880 return ABIArgInfo::getDirect(); 7881 } 7882 7883 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { 7884 ABIArgInfo &RetInfo = FI.getReturnInfo(); 7885 if (!getCXXABI().classifyReturnType(FI)) 7886 RetInfo = classifyReturnType(FI.getReturnType()); 7887 7888 // Check if a pointer to an aggregate is passed as a hidden argument. 7889 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; 7890 7891 for (auto &I : FI.arguments()) 7892 I.info = classifyArgumentType(I.type, Offset); 7893 } 7894 7895 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7896 QualType OrigTy) const { 7897 QualType Ty = OrigTy; 7898 7899 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. 7900 // Pointers are also promoted in the same way but this only matters for N32. 7901 unsigned SlotSizeInBits = IsO32 ? 32 : 64; 7902 unsigned PtrWidth = getTarget().getPointerWidth(0); 7903 bool DidPromote = false; 7904 if ((Ty->isIntegerType() && 7905 getContext().getIntWidth(Ty) < SlotSizeInBits) || 7906 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { 7907 DidPromote = true; 7908 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, 7909 Ty->isSignedIntegerType()); 7910 } 7911 7912 auto TyInfo = getContext().getTypeInfoInChars(Ty); 7913 7914 // The alignment of things in the argument area is never larger than 7915 // StackAlignInBytes. 7916 TyInfo.second = 7917 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); 7918 7919 // MinABIStackAlignInBytes is the size of argument slots on the stack. 7920 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); 7921 7922 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 7923 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); 7924 7925 7926 // If there was a promotion, "unpromote" into a temporary. 7927 // TODO: can we just use a pointer into a subset of the original slot? 7928 if (DidPromote) { 7929 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); 7930 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); 7931 7932 // Truncate down to the right width. 7933 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() 7934 : CGF.IntPtrTy); 7935 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); 7936 if (OrigTy->isPointerType()) 7937 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); 7938 7939 CGF.Builder.CreateStore(V, Temp); 7940 Addr = Temp; 7941 } 7942 7943 return Addr; 7944 } 7945 7946 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { 7947 int TySize = getContext().getTypeSize(Ty); 7948 7949 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. 7950 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) 7951 return ABIArgInfo::getSignExtend(Ty); 7952 7953 return ABIArgInfo::getExtend(Ty); 7954 } 7955 7956 bool 7957 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 7958 llvm::Value *Address) const { 7959 // This information comes from gcc's implementation, which seems to 7960 // as canonical as it gets. 7961 7962 // Everything on MIPS is 4 bytes. Double-precision FP registers 7963 // are aliased to pairs of single-precision FP registers. 7964 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 7965 7966 // 0-31 are the general purpose registers, $0 - $31. 7967 // 32-63 are the floating-point registers, $f0 - $f31. 7968 // 64 and 65 are the multiply/divide registers, $hi and $lo. 7969 // 66 is the (notional, I think) register for signal-handler return. 7970 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); 7971 7972 // 67-74 are the floating-point status registers, $fcc0 - $fcc7. 7973 // They are one bit wide and ignored here. 7974 7975 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. 7976 // (coprocessor 1 is the FP unit) 7977 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. 7978 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. 7979 // 176-181 are the DSP accumulator registers. 7980 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); 7981 return false; 7982 } 7983 7984 //===----------------------------------------------------------------------===// 7985 // AVR ABI Implementation. 7986 //===----------------------------------------------------------------------===// 7987 7988 namespace { 7989 class AVRTargetCodeGenInfo : public TargetCodeGenInfo { 7990 public: 7991 AVRTargetCodeGenInfo(CodeGenTypes &CGT) 7992 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} 7993 7994 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 7995 CodeGen::CodeGenModule &CGM) const override { 7996 if (GV->isDeclaration()) 7997 return; 7998 const auto *FD = dyn_cast_or_null<FunctionDecl>(D); 7999 if (!FD) return; 8000 auto *Fn = cast<llvm::Function>(GV); 8001 8002 if (FD->getAttr<AVRInterruptAttr>()) 8003 Fn->addFnAttr("interrupt"); 8004 8005 if (FD->getAttr<AVRSignalAttr>()) 8006 Fn->addFnAttr("signal"); 8007 } 8008 }; 8009 } 8010 8011 //===----------------------------------------------------------------------===// 8012 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. 8013 // Currently subclassed only to implement custom OpenCL C function attribute 8014 // handling. 8015 //===----------------------------------------------------------------------===// 8016 8017 namespace { 8018 8019 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { 8020 public: 8021 TCETargetCodeGenInfo(CodeGenTypes &CGT) 8022 : DefaultTargetCodeGenInfo(CGT) {} 8023 8024 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 8025 CodeGen::CodeGenModule &M) const override; 8026 }; 8027 8028 void TCETargetCodeGenInfo::setTargetAttributes( 8029 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 8030 if (GV->isDeclaration()) 8031 return; 8032 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 8033 if (!FD) return; 8034 8035 llvm::Function *F = cast<llvm::Function>(GV); 8036 8037 if (M.getLangOpts().OpenCL) { 8038 if (FD->hasAttr<OpenCLKernelAttr>()) { 8039 // OpenCL C Kernel functions are not subject to inlining 8040 F->addFnAttr(llvm::Attribute::NoInline); 8041 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); 8042 if (Attr) { 8043 // Convert the reqd_work_group_size() attributes to metadata. 8044 llvm::LLVMContext &Context = F->getContext(); 8045 llvm::NamedMDNode *OpenCLMetadata = 8046 M.getModule().getOrInsertNamedMetadata( 8047 "opencl.kernel_wg_size_info"); 8048 8049 SmallVector<llvm::Metadata *, 5> Operands; 8050 Operands.push_back(llvm::ConstantAsMetadata::get(F)); 8051 8052 Operands.push_back( 8053 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 8054 M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); 8055 Operands.push_back( 8056 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 8057 M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); 8058 Operands.push_back( 8059 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 8060 M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); 8061 8062 // Add a boolean constant operand for "required" (true) or "hint" 8063 // (false) for implementing the work_group_size_hint attr later. 8064 // Currently always true as the hint is not yet implemented. 8065 Operands.push_back( 8066 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); 8067 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); 8068 } 8069 } 8070 } 8071 } 8072 8073 } 8074 8075 //===----------------------------------------------------------------------===// 8076 // Hexagon ABI Implementation 8077 //===----------------------------------------------------------------------===// 8078 8079 namespace { 8080 8081 class HexagonABIInfo : public DefaultABIInfo { 8082 public: 8083 HexagonABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 8084 8085 private: 8086 ABIArgInfo classifyReturnType(QualType RetTy) const; 8087 ABIArgInfo classifyArgumentType(QualType RetTy) const; 8088 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned *RegsLeft) const; 8089 8090 void computeInfo(CGFunctionInfo &FI) const override; 8091 8092 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8093 QualType Ty) const override; 8094 Address EmitVAArgFromMemory(CodeGenFunction &CFG, Address VAListAddr, 8095 QualType Ty) const; 8096 Address EmitVAArgForHexagon(CodeGenFunction &CFG, Address VAListAddr, 8097 QualType Ty) const; 8098 Address EmitVAArgForHexagonLinux(CodeGenFunction &CFG, Address VAListAddr, 8099 QualType Ty) const; 8100 }; 8101 8102 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { 8103 public: 8104 HexagonTargetCodeGenInfo(CodeGenTypes &CGT) 8105 : TargetCodeGenInfo(std::make_unique<HexagonABIInfo>(CGT)) {} 8106 8107 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 8108 return 29; 8109 } 8110 8111 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 8112 CodeGen::CodeGenModule &GCM) const override { 8113 if (GV->isDeclaration()) 8114 return; 8115 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 8116 if (!FD) 8117 return; 8118 } 8119 }; 8120 8121 } // namespace 8122 8123 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { 8124 unsigned RegsLeft = 6; 8125 if (!getCXXABI().classifyReturnType(FI)) 8126 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 8127 for (auto &I : FI.arguments()) 8128 I.info = classifyArgumentType(I.type, &RegsLeft); 8129 } 8130 8131 static bool HexagonAdjustRegsLeft(uint64_t Size, unsigned *RegsLeft) { 8132 assert(Size <= 64 && "Not expecting to pass arguments larger than 64 bits" 8133 " through registers"); 8134 8135 if (*RegsLeft == 0) 8136 return false; 8137 8138 if (Size <= 32) { 8139 (*RegsLeft)--; 8140 return true; 8141 } 8142 8143 if (2 <= (*RegsLeft & (~1U))) { 8144 *RegsLeft = (*RegsLeft & (~1U)) - 2; 8145 return true; 8146 } 8147 8148 // Next available register was r5 but candidate was greater than 32-bits so it 8149 // has to go on the stack. However we still consume r5 8150 if (*RegsLeft == 1) 8151 *RegsLeft = 0; 8152 8153 return false; 8154 } 8155 8156 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty, 8157 unsigned *RegsLeft) const { 8158 if (!isAggregateTypeForABI(Ty)) { 8159 // Treat an enum type as its underlying type. 8160 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 8161 Ty = EnumTy->getDecl()->getIntegerType(); 8162 8163 uint64_t Size = getContext().getTypeSize(Ty); 8164 if (Size <= 64) 8165 HexagonAdjustRegsLeft(Size, RegsLeft); 8166 8167 if (Size > 64 && Ty->isExtIntType()) 8168 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 8169 8170 return isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) 8171 : ABIArgInfo::getDirect(); 8172 } 8173 8174 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 8175 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 8176 8177 // Ignore empty records. 8178 if (isEmptyRecord(getContext(), Ty, true)) 8179 return ABIArgInfo::getIgnore(); 8180 8181 uint64_t Size = getContext().getTypeSize(Ty); 8182 unsigned Align = getContext().getTypeAlign(Ty); 8183 8184 if (Size > 64) 8185 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 8186 8187 if (HexagonAdjustRegsLeft(Size, RegsLeft)) 8188 Align = Size <= 32 ? 32 : 64; 8189 if (Size <= Align) { 8190 // Pass in the smallest viable integer type. 8191 if (!llvm::isPowerOf2_64(Size)) 8192 Size = llvm::NextPowerOf2(Size); 8193 return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size)); 8194 } 8195 return DefaultABIInfo::classifyArgumentType(Ty); 8196 } 8197 8198 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { 8199 if (RetTy->isVoidType()) 8200 return ABIArgInfo::getIgnore(); 8201 8202 const TargetInfo &T = CGT.getTarget(); 8203 uint64_t Size = getContext().getTypeSize(RetTy); 8204 8205 if (RetTy->getAs<VectorType>()) { 8206 // HVX vectors are returned in vector registers or register pairs. 8207 if (T.hasFeature("hvx")) { 8208 assert(T.hasFeature("hvx-length64b") || T.hasFeature("hvx-length128b")); 8209 uint64_t VecSize = T.hasFeature("hvx-length64b") ? 64*8 : 128*8; 8210 if (Size == VecSize || Size == 2*VecSize) 8211 return ABIArgInfo::getDirectInReg(); 8212 } 8213 // Large vector types should be returned via memory. 8214 if (Size > 64) 8215 return getNaturalAlignIndirect(RetTy); 8216 } 8217 8218 if (!isAggregateTypeForABI(RetTy)) { 8219 // Treat an enum type as its underlying type. 8220 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 8221 RetTy = EnumTy->getDecl()->getIntegerType(); 8222 8223 if (Size > 64 && RetTy->isExtIntType()) 8224 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 8225 8226 return isPromotableIntegerTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) 8227 : ABIArgInfo::getDirect(); 8228 } 8229 8230 if (isEmptyRecord(getContext(), RetTy, true)) 8231 return ABIArgInfo::getIgnore(); 8232 8233 // Aggregates <= 8 bytes are returned in registers, other aggregates 8234 // are returned indirectly. 8235 if (Size <= 64) { 8236 // Return in the smallest viable integer type. 8237 if (!llvm::isPowerOf2_64(Size)) 8238 Size = llvm::NextPowerOf2(Size); 8239 return ABIArgInfo::getDirect(llvm::Type::getIntNTy(getVMContext(), Size)); 8240 } 8241 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); 8242 } 8243 8244 Address HexagonABIInfo::EmitVAArgFromMemory(CodeGenFunction &CGF, 8245 Address VAListAddr, 8246 QualType Ty) const { 8247 // Load the overflow area pointer. 8248 Address __overflow_area_pointer_p = 8249 CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p"); 8250 llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad( 8251 __overflow_area_pointer_p, "__overflow_area_pointer"); 8252 8253 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; 8254 if (Align > 4) { 8255 // Alignment should be a power of 2. 8256 assert((Align & (Align - 1)) == 0 && "Alignment is not power of 2!"); 8257 8258 // overflow_arg_area = (overflow_arg_area + align - 1) & -align; 8259 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); 8260 8261 // Add offset to the current pointer to access the argument. 8262 __overflow_area_pointer = 8263 CGF.Builder.CreateGEP(__overflow_area_pointer, Offset); 8264 llvm::Value *AsInt = 8265 CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty); 8266 8267 // Create a mask which should be "AND"ed 8268 // with (overflow_arg_area + align - 1) 8269 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -(int)Align); 8270 __overflow_area_pointer = CGF.Builder.CreateIntToPtr( 8271 CGF.Builder.CreateAnd(AsInt, Mask), __overflow_area_pointer->getType(), 8272 "__overflow_area_pointer.align"); 8273 } 8274 8275 // Get the type of the argument from memory and bitcast 8276 // overflow area pointer to the argument type. 8277 llvm::Type *PTy = CGF.ConvertTypeForMem(Ty); 8278 Address AddrTyped = CGF.Builder.CreateBitCast( 8279 Address(__overflow_area_pointer, CharUnits::fromQuantity(Align)), 8280 llvm::PointerType::getUnqual(PTy)); 8281 8282 // Round up to the minimum stack alignment for varargs which is 4 bytes. 8283 uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4); 8284 8285 __overflow_area_pointer = CGF.Builder.CreateGEP( 8286 __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 8287 "__overflow_area_pointer.next"); 8288 CGF.Builder.CreateStore(__overflow_area_pointer, __overflow_area_pointer_p); 8289 8290 return AddrTyped; 8291 } 8292 8293 Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF, 8294 Address VAListAddr, 8295 QualType Ty) const { 8296 // FIXME: Need to handle alignment 8297 llvm::Type *BP = CGF.Int8PtrTy; 8298 llvm::Type *BPP = CGF.Int8PtrPtrTy; 8299 CGBuilderTy &Builder = CGF.Builder; 8300 Address VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 8301 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 8302 // Handle address alignment for type alignment > 32 bits 8303 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; 8304 if (TyAlign > 4) { 8305 assert((TyAlign & (TyAlign - 1)) == 0 && "Alignment is not power of 2!"); 8306 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); 8307 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); 8308 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); 8309 Addr = Builder.CreateIntToPtr(AddrAsInt, BP); 8310 } 8311 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 8312 Address AddrTyped = Builder.CreateBitCast( 8313 Address(Addr, CharUnits::fromQuantity(TyAlign)), PTy); 8314 8315 uint64_t Offset = llvm::alignTo(CGF.getContext().getTypeSize(Ty) / 8, 4); 8316 llvm::Value *NextAddr = Builder.CreateGEP( 8317 Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); 8318 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 8319 8320 return AddrTyped; 8321 } 8322 8323 Address HexagonABIInfo::EmitVAArgForHexagonLinux(CodeGenFunction &CGF, 8324 Address VAListAddr, 8325 QualType Ty) const { 8326 int ArgSize = CGF.getContext().getTypeSize(Ty) / 8; 8327 8328 if (ArgSize > 8) 8329 return EmitVAArgFromMemory(CGF, VAListAddr, Ty); 8330 8331 // Here we have check if the argument is in register area or 8332 // in overflow area. 8333 // If the saved register area pointer + argsize rounded up to alignment > 8334 // saved register area end pointer, argument is in overflow area. 8335 unsigned RegsLeft = 6; 8336 Ty = CGF.getContext().getCanonicalType(Ty); 8337 (void)classifyArgumentType(Ty, &RegsLeft); 8338 8339 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); 8340 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 8341 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); 8342 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 8343 8344 // Get rounded size of the argument.GCC does not allow vararg of 8345 // size < 4 bytes. We follow the same logic here. 8346 ArgSize = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8; 8347 int ArgAlign = (CGF.getContext().getTypeSize(Ty) <= 32) ? 4 : 8; 8348 8349 // Argument may be in saved register area 8350 CGF.EmitBlock(MaybeRegBlock); 8351 8352 // Load the current saved register area pointer. 8353 Address __current_saved_reg_area_pointer_p = CGF.Builder.CreateStructGEP( 8354 VAListAddr, 0, "__current_saved_reg_area_pointer_p"); 8355 llvm::Value *__current_saved_reg_area_pointer = CGF.Builder.CreateLoad( 8356 __current_saved_reg_area_pointer_p, "__current_saved_reg_area_pointer"); 8357 8358 // Load the saved register area end pointer. 8359 Address __saved_reg_area_end_pointer_p = CGF.Builder.CreateStructGEP( 8360 VAListAddr, 1, "__saved_reg_area_end_pointer_p"); 8361 llvm::Value *__saved_reg_area_end_pointer = CGF.Builder.CreateLoad( 8362 __saved_reg_area_end_pointer_p, "__saved_reg_area_end_pointer"); 8363 8364 // If the size of argument is > 4 bytes, check if the stack 8365 // location is aligned to 8 bytes 8366 if (ArgAlign > 4) { 8367 8368 llvm::Value *__current_saved_reg_area_pointer_int = 8369 CGF.Builder.CreatePtrToInt(__current_saved_reg_area_pointer, 8370 CGF.Int32Ty); 8371 8372 __current_saved_reg_area_pointer_int = CGF.Builder.CreateAdd( 8373 __current_saved_reg_area_pointer_int, 8374 llvm::ConstantInt::get(CGF.Int32Ty, (ArgAlign - 1)), 8375 "align_current_saved_reg_area_pointer"); 8376 8377 __current_saved_reg_area_pointer_int = 8378 CGF.Builder.CreateAnd(__current_saved_reg_area_pointer_int, 8379 llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign), 8380 "align_current_saved_reg_area_pointer"); 8381 8382 __current_saved_reg_area_pointer = 8383 CGF.Builder.CreateIntToPtr(__current_saved_reg_area_pointer_int, 8384 __current_saved_reg_area_pointer->getType(), 8385 "align_current_saved_reg_area_pointer"); 8386 } 8387 8388 llvm::Value *__new_saved_reg_area_pointer = 8389 CGF.Builder.CreateGEP(__current_saved_reg_area_pointer, 8390 llvm::ConstantInt::get(CGF.Int32Ty, ArgSize), 8391 "__new_saved_reg_area_pointer"); 8392 8393 llvm::Value *UsingStack = 0; 8394 UsingStack = CGF.Builder.CreateICmpSGT(__new_saved_reg_area_pointer, 8395 __saved_reg_area_end_pointer); 8396 8397 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, InRegBlock); 8398 8399 // Argument in saved register area 8400 // Implement the block where argument is in register saved area 8401 CGF.EmitBlock(InRegBlock); 8402 8403 llvm::Type *PTy = CGF.ConvertType(Ty); 8404 llvm::Value *__saved_reg_area_p = CGF.Builder.CreateBitCast( 8405 __current_saved_reg_area_pointer, llvm::PointerType::getUnqual(PTy)); 8406 8407 CGF.Builder.CreateStore(__new_saved_reg_area_pointer, 8408 __current_saved_reg_area_pointer_p); 8409 8410 CGF.EmitBranch(ContBlock); 8411 8412 // Argument in overflow area 8413 // Implement the block where the argument is in overflow area. 8414 CGF.EmitBlock(OnStackBlock); 8415 8416 // Load the overflow area pointer 8417 Address __overflow_area_pointer_p = 8418 CGF.Builder.CreateStructGEP(VAListAddr, 2, "__overflow_area_pointer_p"); 8419 llvm::Value *__overflow_area_pointer = CGF.Builder.CreateLoad( 8420 __overflow_area_pointer_p, "__overflow_area_pointer"); 8421 8422 // Align the overflow area pointer according to the alignment of the argument 8423 if (ArgAlign > 4) { 8424 llvm::Value *__overflow_area_pointer_int = 8425 CGF.Builder.CreatePtrToInt(__overflow_area_pointer, CGF.Int32Ty); 8426 8427 __overflow_area_pointer_int = 8428 CGF.Builder.CreateAdd(__overflow_area_pointer_int, 8429 llvm::ConstantInt::get(CGF.Int32Ty, ArgAlign - 1), 8430 "align_overflow_area_pointer"); 8431 8432 __overflow_area_pointer_int = 8433 CGF.Builder.CreateAnd(__overflow_area_pointer_int, 8434 llvm::ConstantInt::get(CGF.Int32Ty, -ArgAlign), 8435 "align_overflow_area_pointer"); 8436 8437 __overflow_area_pointer = CGF.Builder.CreateIntToPtr( 8438 __overflow_area_pointer_int, __overflow_area_pointer->getType(), 8439 "align_overflow_area_pointer"); 8440 } 8441 8442 // Get the pointer for next argument in overflow area and store it 8443 // to overflow area pointer. 8444 llvm::Value *__new_overflow_area_pointer = CGF.Builder.CreateGEP( 8445 __overflow_area_pointer, llvm::ConstantInt::get(CGF.Int32Ty, ArgSize), 8446 "__overflow_area_pointer.next"); 8447 8448 CGF.Builder.CreateStore(__new_overflow_area_pointer, 8449 __overflow_area_pointer_p); 8450 8451 CGF.Builder.CreateStore(__new_overflow_area_pointer, 8452 __current_saved_reg_area_pointer_p); 8453 8454 // Bitcast the overflow area pointer to the type of argument. 8455 llvm::Type *OverflowPTy = CGF.ConvertTypeForMem(Ty); 8456 llvm::Value *__overflow_area_p = CGF.Builder.CreateBitCast( 8457 __overflow_area_pointer, llvm::PointerType::getUnqual(OverflowPTy)); 8458 8459 CGF.EmitBranch(ContBlock); 8460 8461 // Get the correct pointer to load the variable argument 8462 // Implement the ContBlock 8463 CGF.EmitBlock(ContBlock); 8464 8465 llvm::Type *MemPTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 8466 llvm::PHINode *ArgAddr = CGF.Builder.CreatePHI(MemPTy, 2, "vaarg.addr"); 8467 ArgAddr->addIncoming(__saved_reg_area_p, InRegBlock); 8468 ArgAddr->addIncoming(__overflow_area_p, OnStackBlock); 8469 8470 return Address(ArgAddr, CharUnits::fromQuantity(ArgAlign)); 8471 } 8472 8473 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8474 QualType Ty) const { 8475 8476 if (getTarget().getTriple().isMusl()) 8477 return EmitVAArgForHexagonLinux(CGF, VAListAddr, Ty); 8478 8479 return EmitVAArgForHexagon(CGF, VAListAddr, Ty); 8480 } 8481 8482 //===----------------------------------------------------------------------===// 8483 // Lanai ABI Implementation 8484 //===----------------------------------------------------------------------===// 8485 8486 namespace { 8487 class LanaiABIInfo : public DefaultABIInfo { 8488 public: 8489 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 8490 8491 bool shouldUseInReg(QualType Ty, CCState &State) const; 8492 8493 void computeInfo(CGFunctionInfo &FI) const override { 8494 CCState State(FI); 8495 // Lanai uses 4 registers to pass arguments unless the function has the 8496 // regparm attribute set. 8497 if (FI.getHasRegParm()) { 8498 State.FreeRegs = FI.getRegParm(); 8499 } else { 8500 State.FreeRegs = 4; 8501 } 8502 8503 if (!getCXXABI().classifyReturnType(FI)) 8504 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 8505 for (auto &I : FI.arguments()) 8506 I.info = classifyArgumentType(I.type, State); 8507 } 8508 8509 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; 8510 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; 8511 }; 8512 } // end anonymous namespace 8513 8514 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { 8515 unsigned Size = getContext().getTypeSize(Ty); 8516 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; 8517 8518 if (SizeInRegs == 0) 8519 return false; 8520 8521 if (SizeInRegs > State.FreeRegs) { 8522 State.FreeRegs = 0; 8523 return false; 8524 } 8525 8526 State.FreeRegs -= SizeInRegs; 8527 8528 return true; 8529 } 8530 8531 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, 8532 CCState &State) const { 8533 if (!ByVal) { 8534 if (State.FreeRegs) { 8535 --State.FreeRegs; // Non-byval indirects just use one pointer. 8536 return getNaturalAlignIndirectInReg(Ty); 8537 } 8538 return getNaturalAlignIndirect(Ty, false); 8539 } 8540 8541 // Compute the byval alignment. 8542 const unsigned MinABIStackAlignInBytes = 4; 8543 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 8544 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, 8545 /*Realign=*/TypeAlign > 8546 MinABIStackAlignInBytes); 8547 } 8548 8549 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, 8550 CCState &State) const { 8551 // Check with the C++ ABI first. 8552 const RecordType *RT = Ty->getAs<RecordType>(); 8553 if (RT) { 8554 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 8555 if (RAA == CGCXXABI::RAA_Indirect) { 8556 return getIndirectResult(Ty, /*ByVal=*/false, State); 8557 } else if (RAA == CGCXXABI::RAA_DirectInMemory) { 8558 return getNaturalAlignIndirect(Ty, /*ByRef=*/true); 8559 } 8560 } 8561 8562 if (isAggregateTypeForABI(Ty)) { 8563 // Structures with flexible arrays are always indirect. 8564 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 8565 return getIndirectResult(Ty, /*ByVal=*/true, State); 8566 8567 // Ignore empty structs/unions. 8568 if (isEmptyRecord(getContext(), Ty, true)) 8569 return ABIArgInfo::getIgnore(); 8570 8571 llvm::LLVMContext &LLVMContext = getVMContext(); 8572 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; 8573 if (SizeInRegs <= State.FreeRegs) { 8574 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 8575 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); 8576 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 8577 State.FreeRegs -= SizeInRegs; 8578 return ABIArgInfo::getDirectInReg(Result); 8579 } else { 8580 State.FreeRegs = 0; 8581 } 8582 return getIndirectResult(Ty, true, State); 8583 } 8584 8585 // Treat an enum type as its underlying type. 8586 if (const auto *EnumTy = Ty->getAs<EnumType>()) 8587 Ty = EnumTy->getDecl()->getIntegerType(); 8588 8589 bool InReg = shouldUseInReg(Ty, State); 8590 8591 // Don't pass >64 bit integers in registers. 8592 if (const auto *EIT = Ty->getAs<ExtIntType>()) 8593 if (EIT->getNumBits() > 64) 8594 return getIndirectResult(Ty, /*ByVal=*/true, State); 8595 8596 if (isPromotableIntegerTypeForABI(Ty)) { 8597 if (InReg) 8598 return ABIArgInfo::getDirectInReg(); 8599 return ABIArgInfo::getExtend(Ty); 8600 } 8601 if (InReg) 8602 return ABIArgInfo::getDirectInReg(); 8603 return ABIArgInfo::getDirect(); 8604 } 8605 8606 namespace { 8607 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { 8608 public: 8609 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 8610 : TargetCodeGenInfo(std::make_unique<LanaiABIInfo>(CGT)) {} 8611 }; 8612 } 8613 8614 //===----------------------------------------------------------------------===// 8615 // AMDGPU ABI Implementation 8616 //===----------------------------------------------------------------------===// 8617 8618 namespace { 8619 8620 class AMDGPUABIInfo final : public DefaultABIInfo { 8621 private: 8622 static const unsigned MaxNumRegsForArgsRet = 16; 8623 8624 unsigned numRegsForType(QualType Ty) const; 8625 8626 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 8627 bool isHomogeneousAggregateSmallEnough(const Type *Base, 8628 uint64_t Members) const override; 8629 8630 // Coerce HIP pointer arguments from generic pointers to global ones. 8631 llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS, 8632 unsigned ToAS) const { 8633 // Structure types. 8634 if (auto STy = dyn_cast<llvm::StructType>(Ty)) { 8635 SmallVector<llvm::Type *, 8> EltTys; 8636 bool Changed = false; 8637 for (auto T : STy->elements()) { 8638 auto NT = coerceKernelArgumentType(T, FromAS, ToAS); 8639 EltTys.push_back(NT); 8640 Changed |= (NT != T); 8641 } 8642 // Skip if there is no change in element types. 8643 if (!Changed) 8644 return STy; 8645 if (STy->hasName()) 8646 return llvm::StructType::create( 8647 EltTys, (STy->getName() + ".coerce").str(), STy->isPacked()); 8648 return llvm::StructType::get(getVMContext(), EltTys, STy->isPacked()); 8649 } 8650 // Array types. 8651 if (auto ATy = dyn_cast<llvm::ArrayType>(Ty)) { 8652 auto T = ATy->getElementType(); 8653 auto NT = coerceKernelArgumentType(T, FromAS, ToAS); 8654 // Skip if there is no change in that element type. 8655 if (NT == T) 8656 return ATy; 8657 return llvm::ArrayType::get(NT, ATy->getNumElements()); 8658 } 8659 // Single value types. 8660 if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS) 8661 return llvm::PointerType::get( 8662 cast<llvm::PointerType>(Ty)->getElementType(), ToAS); 8663 return Ty; 8664 } 8665 8666 public: 8667 explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : 8668 DefaultABIInfo(CGT) {} 8669 8670 ABIArgInfo classifyReturnType(QualType RetTy) const; 8671 ABIArgInfo classifyKernelArgumentType(QualType Ty) const; 8672 ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; 8673 8674 void computeInfo(CGFunctionInfo &FI) const override; 8675 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8676 QualType Ty) const override; 8677 }; 8678 8679 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 8680 return true; 8681 } 8682 8683 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( 8684 const Type *Base, uint64_t Members) const { 8685 uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; 8686 8687 // Homogeneous Aggregates may occupy at most 16 registers. 8688 return Members * NumRegs <= MaxNumRegsForArgsRet; 8689 } 8690 8691 /// Estimate number of registers the type will use when passed in registers. 8692 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { 8693 unsigned NumRegs = 0; 8694 8695 if (const VectorType *VT = Ty->getAs<VectorType>()) { 8696 // Compute from the number of elements. The reported size is based on the 8697 // in-memory size, which includes the padding 4th element for 3-vectors. 8698 QualType EltTy = VT->getElementType(); 8699 unsigned EltSize = getContext().getTypeSize(EltTy); 8700 8701 // 16-bit element vectors should be passed as packed. 8702 if (EltSize == 16) 8703 return (VT->getNumElements() + 1) / 2; 8704 8705 unsigned EltNumRegs = (EltSize + 31) / 32; 8706 return EltNumRegs * VT->getNumElements(); 8707 } 8708 8709 if (const RecordType *RT = Ty->getAs<RecordType>()) { 8710 const RecordDecl *RD = RT->getDecl(); 8711 assert(!RD->hasFlexibleArrayMember()); 8712 8713 for (const FieldDecl *Field : RD->fields()) { 8714 QualType FieldTy = Field->getType(); 8715 NumRegs += numRegsForType(FieldTy); 8716 } 8717 8718 return NumRegs; 8719 } 8720 8721 return (getContext().getTypeSize(Ty) + 31) / 32; 8722 } 8723 8724 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { 8725 llvm::CallingConv::ID CC = FI.getCallingConvention(); 8726 8727 if (!getCXXABI().classifyReturnType(FI)) 8728 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 8729 8730 unsigned NumRegsLeft = MaxNumRegsForArgsRet; 8731 for (auto &Arg : FI.arguments()) { 8732 if (CC == llvm::CallingConv::AMDGPU_KERNEL) { 8733 Arg.info = classifyKernelArgumentType(Arg.type); 8734 } else { 8735 Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); 8736 } 8737 } 8738 } 8739 8740 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 8741 QualType Ty) const { 8742 llvm_unreachable("AMDGPU does not support varargs"); 8743 } 8744 8745 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { 8746 if (isAggregateTypeForABI(RetTy)) { 8747 // Records with non-trivial destructors/copy-constructors should not be 8748 // returned by value. 8749 if (!getRecordArgABI(RetTy, getCXXABI())) { 8750 // Ignore empty structs/unions. 8751 if (isEmptyRecord(getContext(), RetTy, true)) 8752 return ABIArgInfo::getIgnore(); 8753 8754 // Lower single-element structs to just return a regular value. 8755 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 8756 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 8757 8758 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 8759 const RecordDecl *RD = RT->getDecl(); 8760 if (RD->hasFlexibleArrayMember()) 8761 return DefaultABIInfo::classifyReturnType(RetTy); 8762 } 8763 8764 // Pack aggregates <= 4 bytes into single VGPR or pair. 8765 uint64_t Size = getContext().getTypeSize(RetTy); 8766 if (Size <= 16) 8767 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 8768 8769 if (Size <= 32) 8770 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 8771 8772 if (Size <= 64) { 8773 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); 8774 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); 8775 } 8776 8777 if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) 8778 return ABIArgInfo::getDirect(); 8779 } 8780 } 8781 8782 // Otherwise just do the default thing. 8783 return DefaultABIInfo::classifyReturnType(RetTy); 8784 } 8785 8786 /// For kernels all parameters are really passed in a special buffer. It doesn't 8787 /// make sense to pass anything byval, so everything must be direct. 8788 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { 8789 Ty = useFirstFieldIfTransparentUnion(Ty); 8790 8791 // TODO: Can we omit empty structs? 8792 8793 llvm::Type *LTy = nullptr; 8794 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) 8795 LTy = CGT.ConvertType(QualType(SeltTy, 0)); 8796 8797 if (getContext().getLangOpts().HIP) { 8798 if (!LTy) 8799 LTy = CGT.ConvertType(Ty); 8800 LTy = coerceKernelArgumentType( 8801 LTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default), 8802 /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device)); 8803 } 8804 8805 // If we set CanBeFlattened to true, CodeGen will expand the struct to its 8806 // individual elements, which confuses the Clover OpenCL backend; therefore we 8807 // have to set it to false here. Other args of getDirect() are just defaults. 8808 return ABIArgInfo::getDirect(LTy, 0, nullptr, false); 8809 } 8810 8811 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, 8812 unsigned &NumRegsLeft) const { 8813 assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); 8814 8815 Ty = useFirstFieldIfTransparentUnion(Ty); 8816 8817 if (isAggregateTypeForABI(Ty)) { 8818 // Records with non-trivial destructors/copy-constructors should not be 8819 // passed by value. 8820 if (auto RAA = getRecordArgABI(Ty, getCXXABI())) 8821 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 8822 8823 // Ignore empty structs/unions. 8824 if (isEmptyRecord(getContext(), Ty, true)) 8825 return ABIArgInfo::getIgnore(); 8826 8827 // Lower single-element structs to just pass a regular value. TODO: We 8828 // could do reasonable-size multiple-element structs too, using getExpand(), 8829 // though watch out for things like bitfields. 8830 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) 8831 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 8832 8833 if (const RecordType *RT = Ty->getAs<RecordType>()) { 8834 const RecordDecl *RD = RT->getDecl(); 8835 if (RD->hasFlexibleArrayMember()) 8836 return DefaultABIInfo::classifyArgumentType(Ty); 8837 } 8838 8839 // Pack aggregates <= 8 bytes into single VGPR or pair. 8840 uint64_t Size = getContext().getTypeSize(Ty); 8841 if (Size <= 64) { 8842 unsigned NumRegs = (Size + 31) / 32; 8843 NumRegsLeft -= std::min(NumRegsLeft, NumRegs); 8844 8845 if (Size <= 16) 8846 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 8847 8848 if (Size <= 32) 8849 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 8850 8851 // XXX: Should this be i64 instead, and should the limit increase? 8852 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); 8853 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); 8854 } 8855 8856 if (NumRegsLeft > 0) { 8857 unsigned NumRegs = numRegsForType(Ty); 8858 if (NumRegsLeft >= NumRegs) { 8859 NumRegsLeft -= NumRegs; 8860 return ABIArgInfo::getDirect(); 8861 } 8862 } 8863 } 8864 8865 // Otherwise just do the default thing. 8866 ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); 8867 if (!ArgInfo.isIndirect()) { 8868 unsigned NumRegs = numRegsForType(Ty); 8869 NumRegsLeft -= std::min(NumRegs, NumRegsLeft); 8870 } 8871 8872 return ArgInfo; 8873 } 8874 8875 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { 8876 public: 8877 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) 8878 : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {} 8879 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 8880 CodeGen::CodeGenModule &M) const override; 8881 unsigned getOpenCLKernelCallingConv() const override; 8882 8883 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, 8884 llvm::PointerType *T, QualType QT) const override; 8885 8886 LangAS getASTAllocaAddressSpace() const override { 8887 return getLangASFromTargetAS( 8888 getABIInfo().getDataLayout().getAllocaAddrSpace()); 8889 } 8890 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, 8891 const VarDecl *D) const override; 8892 llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts, 8893 SyncScope Scope, 8894 llvm::AtomicOrdering Ordering, 8895 llvm::LLVMContext &Ctx) const override; 8896 llvm::Function * 8897 createEnqueuedBlockKernel(CodeGenFunction &CGF, 8898 llvm::Function *BlockInvokeFunc, 8899 llvm::Value *BlockLiteral) const override; 8900 bool shouldEmitStaticExternCAliases() const override; 8901 void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; 8902 }; 8903 } 8904 8905 static bool requiresAMDGPUProtectedVisibility(const Decl *D, 8906 llvm::GlobalValue *GV) { 8907 if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) 8908 return false; 8909 8910 return D->hasAttr<OpenCLKernelAttr>() || 8911 (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) || 8912 (isa<VarDecl>(D) && 8913 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || 8914 cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() || 8915 cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType())); 8916 } 8917 8918 void AMDGPUTargetCodeGenInfo::setTargetAttributes( 8919 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 8920 if (requiresAMDGPUProtectedVisibility(D, GV)) { 8921 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 8922 GV->setDSOLocal(true); 8923 } 8924 8925 if (GV->isDeclaration()) 8926 return; 8927 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 8928 if (!FD) 8929 return; 8930 8931 llvm::Function *F = cast<llvm::Function>(GV); 8932 8933 const auto *ReqdWGS = M.getLangOpts().OpenCL ? 8934 FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; 8935 8936 8937 const bool IsOpenCLKernel = M.getLangOpts().OpenCL && 8938 FD->hasAttr<OpenCLKernelAttr>(); 8939 const bool IsHIPKernel = M.getLangOpts().HIP && 8940 FD->hasAttr<CUDAGlobalAttr>(); 8941 if ((IsOpenCLKernel || IsHIPKernel) && 8942 (M.getTriple().getOS() == llvm::Triple::AMDHSA)) 8943 F->addFnAttr("amdgpu-implicitarg-num-bytes", "56"); 8944 8945 if (IsHIPKernel) 8946 F->addFnAttr("uniform-work-group-size", "true"); 8947 8948 8949 const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); 8950 if (ReqdWGS || FlatWGS) { 8951 unsigned Min = 0; 8952 unsigned Max = 0; 8953 if (FlatWGS) { 8954 Min = FlatWGS->getMin() 8955 ->EvaluateKnownConstInt(M.getContext()) 8956 .getExtValue(); 8957 Max = FlatWGS->getMax() 8958 ->EvaluateKnownConstInt(M.getContext()) 8959 .getExtValue(); 8960 } 8961 if (ReqdWGS && Min == 0 && Max == 0) 8962 Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); 8963 8964 if (Min != 0) { 8965 assert(Min <= Max && "Min must be less than or equal Max"); 8966 8967 std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); 8968 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); 8969 } else 8970 assert(Max == 0 && "Max must be zero"); 8971 } else if (IsOpenCLKernel || IsHIPKernel) { 8972 // By default, restrict the maximum size to a value specified by 8973 // --gpu-max-threads-per-block=n or its default value for HIP. 8974 const unsigned OpenCLDefaultMaxWorkGroupSize = 256; 8975 const unsigned DefaultMaxWorkGroupSize = 8976 IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize 8977 : M.getLangOpts().GPUMaxThreadsPerBlock; 8978 std::string AttrVal = 8979 std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize); 8980 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); 8981 } 8982 8983 if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { 8984 unsigned Min = 8985 Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue(); 8986 unsigned Max = Attr->getMax() ? Attr->getMax() 8987 ->EvaluateKnownConstInt(M.getContext()) 8988 .getExtValue() 8989 : 0; 8990 8991 if (Min != 0) { 8992 assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); 8993 8994 std::string AttrVal = llvm::utostr(Min); 8995 if (Max != 0) 8996 AttrVal = AttrVal + "," + llvm::utostr(Max); 8997 F->addFnAttr("amdgpu-waves-per-eu", AttrVal); 8998 } else 8999 assert(Max == 0 && "Max must be zero"); 9000 } 9001 9002 if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { 9003 unsigned NumSGPR = Attr->getNumSGPR(); 9004 9005 if (NumSGPR != 0) 9006 F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); 9007 } 9008 9009 if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { 9010 uint32_t NumVGPR = Attr->getNumVGPR(); 9011 9012 if (NumVGPR != 0) 9013 F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); 9014 } 9015 } 9016 9017 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { 9018 return llvm::CallingConv::AMDGPU_KERNEL; 9019 } 9020 9021 // Currently LLVM assumes null pointers always have value 0, 9022 // which results in incorrectly transformed IR. Therefore, instead of 9023 // emitting null pointers in private and local address spaces, a null 9024 // pointer in generic address space is emitted which is casted to a 9025 // pointer in local or private address space. 9026 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( 9027 const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, 9028 QualType QT) const { 9029 if (CGM.getContext().getTargetNullPointerValue(QT) == 0) 9030 return llvm::ConstantPointerNull::get(PT); 9031 9032 auto &Ctx = CGM.getContext(); 9033 auto NPT = llvm::PointerType::get(PT->getElementType(), 9034 Ctx.getTargetAddressSpace(LangAS::opencl_generic)); 9035 return llvm::ConstantExpr::getAddrSpaceCast( 9036 llvm::ConstantPointerNull::get(NPT), PT); 9037 } 9038 9039 LangAS 9040 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, 9041 const VarDecl *D) const { 9042 assert(!CGM.getLangOpts().OpenCL && 9043 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && 9044 "Address space agnostic languages only"); 9045 LangAS DefaultGlobalAS = getLangASFromTargetAS( 9046 CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); 9047 if (!D) 9048 return DefaultGlobalAS; 9049 9050 LangAS AddrSpace = D->getType().getAddressSpace(); 9051 assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); 9052 if (AddrSpace != LangAS::Default) 9053 return AddrSpace; 9054 9055 if (CGM.isTypeConstant(D->getType(), false)) { 9056 if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) 9057 return ConstAS.getValue(); 9058 } 9059 return DefaultGlobalAS; 9060 } 9061 9062 llvm::SyncScope::ID 9063 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, 9064 SyncScope Scope, 9065 llvm::AtomicOrdering Ordering, 9066 llvm::LLVMContext &Ctx) const { 9067 std::string Name; 9068 switch (Scope) { 9069 case SyncScope::OpenCLWorkGroup: 9070 Name = "workgroup"; 9071 break; 9072 case SyncScope::OpenCLDevice: 9073 Name = "agent"; 9074 break; 9075 case SyncScope::OpenCLAllSVMDevices: 9076 Name = ""; 9077 break; 9078 case SyncScope::OpenCLSubGroup: 9079 Name = "wavefront"; 9080 } 9081 9082 if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) { 9083 if (!Name.empty()) 9084 Name = Twine(Twine(Name) + Twine("-")).str(); 9085 9086 Name = Twine(Twine(Name) + Twine("one-as")).str(); 9087 } 9088 9089 return Ctx.getOrInsertSyncScopeID(Name); 9090 } 9091 9092 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { 9093 return false; 9094 } 9095 9096 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( 9097 const FunctionType *&FT) const { 9098 FT = getABIInfo().getContext().adjustFunctionType( 9099 FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); 9100 } 9101 9102 //===----------------------------------------------------------------------===// 9103 // SPARC v8 ABI Implementation. 9104 // Based on the SPARC Compliance Definition version 2.4.1. 9105 // 9106 // Ensures that complex values are passed in registers. 9107 // 9108 namespace { 9109 class SparcV8ABIInfo : public DefaultABIInfo { 9110 public: 9111 SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 9112 9113 private: 9114 ABIArgInfo classifyReturnType(QualType RetTy) const; 9115 void computeInfo(CGFunctionInfo &FI) const override; 9116 }; 9117 } // end anonymous namespace 9118 9119 9120 ABIArgInfo 9121 SparcV8ABIInfo::classifyReturnType(QualType Ty) const { 9122 if (Ty->isAnyComplexType()) { 9123 return ABIArgInfo::getDirect(); 9124 } 9125 else { 9126 return DefaultABIInfo::classifyReturnType(Ty); 9127 } 9128 } 9129 9130 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { 9131 9132 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 9133 for (auto &Arg : FI.arguments()) 9134 Arg.info = classifyArgumentType(Arg.type); 9135 } 9136 9137 namespace { 9138 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { 9139 public: 9140 SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) 9141 : TargetCodeGenInfo(std::make_unique<SparcV8ABIInfo>(CGT)) {} 9142 }; 9143 } // end anonymous namespace 9144 9145 //===----------------------------------------------------------------------===// 9146 // SPARC v9 ABI Implementation. 9147 // Based on the SPARC Compliance Definition version 2.4.1. 9148 // 9149 // Function arguments a mapped to a nominal "parameter array" and promoted to 9150 // registers depending on their type. Each argument occupies 8 or 16 bytes in 9151 // the array, structs larger than 16 bytes are passed indirectly. 9152 // 9153 // One case requires special care: 9154 // 9155 // struct mixed { 9156 // int i; 9157 // float f; 9158 // }; 9159 // 9160 // When a struct mixed is passed by value, it only occupies 8 bytes in the 9161 // parameter array, but the int is passed in an integer register, and the float 9162 // is passed in a floating point register. This is represented as two arguments 9163 // with the LLVM IR inreg attribute: 9164 // 9165 // declare void f(i32 inreg %i, float inreg %f) 9166 // 9167 // The code generator will only allocate 4 bytes from the parameter array for 9168 // the inreg arguments. All other arguments are allocated a multiple of 8 9169 // bytes. 9170 // 9171 namespace { 9172 class SparcV9ABIInfo : public ABIInfo { 9173 public: 9174 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 9175 9176 private: 9177 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; 9178 void computeInfo(CGFunctionInfo &FI) const override; 9179 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9180 QualType Ty) const override; 9181 9182 // Coercion type builder for structs passed in registers. The coercion type 9183 // serves two purposes: 9184 // 9185 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' 9186 // in registers. 9187 // 2. Expose aligned floating point elements as first-level elements, so the 9188 // code generator knows to pass them in floating point registers. 9189 // 9190 // We also compute the InReg flag which indicates that the struct contains 9191 // aligned 32-bit floats. 9192 // 9193 struct CoerceBuilder { 9194 llvm::LLVMContext &Context; 9195 const llvm::DataLayout &DL; 9196 SmallVector<llvm::Type*, 8> Elems; 9197 uint64_t Size; 9198 bool InReg; 9199 9200 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) 9201 : Context(c), DL(dl), Size(0), InReg(false) {} 9202 9203 // Pad Elems with integers until Size is ToSize. 9204 void pad(uint64_t ToSize) { 9205 assert(ToSize >= Size && "Cannot remove elements"); 9206 if (ToSize == Size) 9207 return; 9208 9209 // Finish the current 64-bit word. 9210 uint64_t Aligned = llvm::alignTo(Size, 64); 9211 if (Aligned > Size && Aligned <= ToSize) { 9212 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); 9213 Size = Aligned; 9214 } 9215 9216 // Add whole 64-bit words. 9217 while (Size + 64 <= ToSize) { 9218 Elems.push_back(llvm::Type::getInt64Ty(Context)); 9219 Size += 64; 9220 } 9221 9222 // Final in-word padding. 9223 if (Size < ToSize) { 9224 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); 9225 Size = ToSize; 9226 } 9227 } 9228 9229 // Add a floating point element at Offset. 9230 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { 9231 // Unaligned floats are treated as integers. 9232 if (Offset % Bits) 9233 return; 9234 // The InReg flag is only required if there are any floats < 64 bits. 9235 if (Bits < 64) 9236 InReg = true; 9237 pad(Offset); 9238 Elems.push_back(Ty); 9239 Size = Offset + Bits; 9240 } 9241 9242 // Add a struct type to the coercion type, starting at Offset (in bits). 9243 void addStruct(uint64_t Offset, llvm::StructType *StrTy) { 9244 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); 9245 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { 9246 llvm::Type *ElemTy = StrTy->getElementType(i); 9247 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); 9248 switch (ElemTy->getTypeID()) { 9249 case llvm::Type::StructTyID: 9250 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); 9251 break; 9252 case llvm::Type::FloatTyID: 9253 addFloat(ElemOffset, ElemTy, 32); 9254 break; 9255 case llvm::Type::DoubleTyID: 9256 addFloat(ElemOffset, ElemTy, 64); 9257 break; 9258 case llvm::Type::FP128TyID: 9259 addFloat(ElemOffset, ElemTy, 128); 9260 break; 9261 case llvm::Type::PointerTyID: 9262 if (ElemOffset % 64 == 0) { 9263 pad(ElemOffset); 9264 Elems.push_back(ElemTy); 9265 Size += 64; 9266 } 9267 break; 9268 default: 9269 break; 9270 } 9271 } 9272 } 9273 9274 // Check if Ty is a usable substitute for the coercion type. 9275 bool isUsableType(llvm::StructType *Ty) const { 9276 return llvm::makeArrayRef(Elems) == Ty->elements(); 9277 } 9278 9279 // Get the coercion type as a literal struct type. 9280 llvm::Type *getType() const { 9281 if (Elems.size() == 1) 9282 return Elems.front(); 9283 else 9284 return llvm::StructType::get(Context, Elems); 9285 } 9286 }; 9287 }; 9288 } // end anonymous namespace 9289 9290 ABIArgInfo 9291 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { 9292 if (Ty->isVoidType()) 9293 return ABIArgInfo::getIgnore(); 9294 9295 uint64_t Size = getContext().getTypeSize(Ty); 9296 9297 // Anything too big to fit in registers is passed with an explicit indirect 9298 // pointer / sret pointer. 9299 if (Size > SizeLimit) 9300 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 9301 9302 // Treat an enum type as its underlying type. 9303 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 9304 Ty = EnumTy->getDecl()->getIntegerType(); 9305 9306 // Integer types smaller than a register are extended. 9307 if (Size < 64 && Ty->isIntegerType()) 9308 return ABIArgInfo::getExtend(Ty); 9309 9310 if (const auto *EIT = Ty->getAs<ExtIntType>()) 9311 if (EIT->getNumBits() < 64) 9312 return ABIArgInfo::getExtend(Ty); 9313 9314 // Other non-aggregates go in registers. 9315 if (!isAggregateTypeForABI(Ty)) 9316 return ABIArgInfo::getDirect(); 9317 9318 // If a C++ object has either a non-trivial copy constructor or a non-trivial 9319 // destructor, it is passed with an explicit indirect pointer / sret pointer. 9320 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 9321 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 9322 9323 // This is a small aggregate type that should be passed in registers. 9324 // Build a coercion type from the LLVM struct type. 9325 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); 9326 if (!StrTy) 9327 return ABIArgInfo::getDirect(); 9328 9329 CoerceBuilder CB(getVMContext(), getDataLayout()); 9330 CB.addStruct(0, StrTy); 9331 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); 9332 9333 // Try to use the original type for coercion. 9334 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); 9335 9336 if (CB.InReg) 9337 return ABIArgInfo::getDirectInReg(CoerceTy); 9338 else 9339 return ABIArgInfo::getDirect(CoerceTy); 9340 } 9341 9342 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9343 QualType Ty) const { 9344 ABIArgInfo AI = classifyType(Ty, 16 * 8); 9345 llvm::Type *ArgTy = CGT.ConvertType(Ty); 9346 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 9347 AI.setCoerceToType(ArgTy); 9348 9349 CharUnits SlotSize = CharUnits::fromQuantity(8); 9350 9351 CGBuilderTy &Builder = CGF.Builder; 9352 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); 9353 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 9354 9355 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 9356 9357 Address ArgAddr = Address::invalid(); 9358 CharUnits Stride; 9359 switch (AI.getKind()) { 9360 case ABIArgInfo::Expand: 9361 case ABIArgInfo::CoerceAndExpand: 9362 case ABIArgInfo::InAlloca: 9363 llvm_unreachable("Unsupported ABI kind for va_arg"); 9364 9365 case ABIArgInfo::Extend: { 9366 Stride = SlotSize; 9367 CharUnits Offset = SlotSize - TypeInfo.first; 9368 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); 9369 break; 9370 } 9371 9372 case ABIArgInfo::Direct: { 9373 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); 9374 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); 9375 ArgAddr = Addr; 9376 break; 9377 } 9378 9379 case ABIArgInfo::Indirect: 9380 Stride = SlotSize; 9381 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); 9382 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), 9383 TypeInfo.second); 9384 break; 9385 9386 case ABIArgInfo::Ignore: 9387 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); 9388 } 9389 9390 // Update VAList. 9391 Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); 9392 Builder.CreateStore(NextPtr.getPointer(), VAListAddr); 9393 9394 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); 9395 } 9396 9397 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { 9398 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); 9399 for (auto &I : FI.arguments()) 9400 I.info = classifyType(I.type, 16 * 8); 9401 } 9402 9403 namespace { 9404 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { 9405 public: 9406 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) 9407 : TargetCodeGenInfo(std::make_unique<SparcV9ABIInfo>(CGT)) {} 9408 9409 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 9410 return 14; 9411 } 9412 9413 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 9414 llvm::Value *Address) const override; 9415 }; 9416 } // end anonymous namespace 9417 9418 bool 9419 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 9420 llvm::Value *Address) const { 9421 // This is calculated from the LLVM and GCC tables and verified 9422 // against gcc output. AFAIK all ABIs use the same encoding. 9423 9424 CodeGen::CGBuilderTy &Builder = CGF.Builder; 9425 9426 llvm::IntegerType *i8 = CGF.Int8Ty; 9427 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 9428 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 9429 9430 // 0-31: the 8-byte general-purpose registers 9431 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 9432 9433 // 32-63: f0-31, the 4-byte floating-point registers 9434 AssignToArrayRange(Builder, Address, Four8, 32, 63); 9435 9436 // Y = 64 9437 // PSR = 65 9438 // WIM = 66 9439 // TBR = 67 9440 // PC = 68 9441 // NPC = 69 9442 // FSR = 70 9443 // CSR = 71 9444 AssignToArrayRange(Builder, Address, Eight8, 64, 71); 9445 9446 // 72-87: d0-15, the 8-byte floating-point registers 9447 AssignToArrayRange(Builder, Address, Eight8, 72, 87); 9448 9449 return false; 9450 } 9451 9452 // ARC ABI implementation. 9453 namespace { 9454 9455 class ARCABIInfo : public DefaultABIInfo { 9456 public: 9457 using DefaultABIInfo::DefaultABIInfo; 9458 9459 private: 9460 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9461 QualType Ty) const override; 9462 9463 void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { 9464 if (!State.FreeRegs) 9465 return; 9466 if (Info.isIndirect() && Info.getInReg()) 9467 State.FreeRegs--; 9468 else if (Info.isDirect() && Info.getInReg()) { 9469 unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; 9470 if (sz < State.FreeRegs) 9471 State.FreeRegs -= sz; 9472 else 9473 State.FreeRegs = 0; 9474 } 9475 } 9476 9477 void computeInfo(CGFunctionInfo &FI) const override { 9478 CCState State(FI); 9479 // ARC uses 8 registers to pass arguments. 9480 State.FreeRegs = 8; 9481 9482 if (!getCXXABI().classifyReturnType(FI)) 9483 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 9484 updateState(FI.getReturnInfo(), FI.getReturnType(), State); 9485 for (auto &I : FI.arguments()) { 9486 I.info = classifyArgumentType(I.type, State.FreeRegs); 9487 updateState(I.info, I.type, State); 9488 } 9489 } 9490 9491 ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; 9492 ABIArgInfo getIndirectByValue(QualType Ty) const; 9493 ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; 9494 ABIArgInfo classifyReturnType(QualType RetTy) const; 9495 }; 9496 9497 class ARCTargetCodeGenInfo : public TargetCodeGenInfo { 9498 public: 9499 ARCTargetCodeGenInfo(CodeGenTypes &CGT) 9500 : TargetCodeGenInfo(std::make_unique<ARCABIInfo>(CGT)) {} 9501 }; 9502 9503 9504 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { 9505 return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : 9506 getNaturalAlignIndirect(Ty, false); 9507 } 9508 9509 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { 9510 // Compute the byval alignment. 9511 const unsigned MinABIStackAlignInBytes = 4; 9512 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 9513 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, 9514 TypeAlign > MinABIStackAlignInBytes); 9515 } 9516 9517 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9518 QualType Ty) const { 9519 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 9520 getContext().getTypeInfoInChars(Ty), 9521 CharUnits::fromQuantity(4), true); 9522 } 9523 9524 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, 9525 uint8_t FreeRegs) const { 9526 // Handle the generic C++ ABI. 9527 const RecordType *RT = Ty->getAs<RecordType>(); 9528 if (RT) { 9529 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 9530 if (RAA == CGCXXABI::RAA_Indirect) 9531 return getIndirectByRef(Ty, FreeRegs > 0); 9532 9533 if (RAA == CGCXXABI::RAA_DirectInMemory) 9534 return getIndirectByValue(Ty); 9535 } 9536 9537 // Treat an enum type as its underlying type. 9538 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 9539 Ty = EnumTy->getDecl()->getIntegerType(); 9540 9541 auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; 9542 9543 if (isAggregateTypeForABI(Ty)) { 9544 // Structures with flexible arrays are always indirect. 9545 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 9546 return getIndirectByValue(Ty); 9547 9548 // Ignore empty structs/unions. 9549 if (isEmptyRecord(getContext(), Ty, true)) 9550 return ABIArgInfo::getIgnore(); 9551 9552 llvm::LLVMContext &LLVMContext = getVMContext(); 9553 9554 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 9555 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); 9556 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 9557 9558 return FreeRegs >= SizeInRegs ? 9559 ABIArgInfo::getDirectInReg(Result) : 9560 ABIArgInfo::getDirect(Result, 0, nullptr, false); 9561 } 9562 9563 if (const auto *EIT = Ty->getAs<ExtIntType>()) 9564 if (EIT->getNumBits() > 64) 9565 return getIndirectByValue(Ty); 9566 9567 return isPromotableIntegerTypeForABI(Ty) 9568 ? (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) 9569 : ABIArgInfo::getExtend(Ty)) 9570 : (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() 9571 : ABIArgInfo::getDirect()); 9572 } 9573 9574 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { 9575 if (RetTy->isAnyComplexType()) 9576 return ABIArgInfo::getDirectInReg(); 9577 9578 // Arguments of size > 4 registers are indirect. 9579 auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; 9580 if (RetSize > 4) 9581 return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); 9582 9583 return DefaultABIInfo::classifyReturnType(RetTy); 9584 } 9585 9586 } // End anonymous namespace. 9587 9588 //===----------------------------------------------------------------------===// 9589 // XCore ABI Implementation 9590 //===----------------------------------------------------------------------===// 9591 9592 namespace { 9593 9594 /// A SmallStringEnc instance is used to build up the TypeString by passing 9595 /// it by reference between functions that append to it. 9596 typedef llvm::SmallString<128> SmallStringEnc; 9597 9598 /// TypeStringCache caches the meta encodings of Types. 9599 /// 9600 /// The reason for caching TypeStrings is two fold: 9601 /// 1. To cache a type's encoding for later uses; 9602 /// 2. As a means to break recursive member type inclusion. 9603 /// 9604 /// A cache Entry can have a Status of: 9605 /// NonRecursive: The type encoding is not recursive; 9606 /// Recursive: The type encoding is recursive; 9607 /// Incomplete: An incomplete TypeString; 9608 /// IncompleteUsed: An incomplete TypeString that has been used in a 9609 /// Recursive type encoding. 9610 /// 9611 /// A NonRecursive entry will have all of its sub-members expanded as fully 9612 /// as possible. Whilst it may contain types which are recursive, the type 9613 /// itself is not recursive and thus its encoding may be safely used whenever 9614 /// the type is encountered. 9615 /// 9616 /// A Recursive entry will have all of its sub-members expanded as fully as 9617 /// possible. The type itself is recursive and it may contain other types which 9618 /// are recursive. The Recursive encoding must not be used during the expansion 9619 /// of a recursive type's recursive branch. For simplicity the code uses 9620 /// IncompleteCount to reject all usage of Recursive encodings for member types. 9621 /// 9622 /// An Incomplete entry is always a RecordType and only encodes its 9623 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and 9624 /// are placed into the cache during type expansion as a means to identify and 9625 /// handle recursive inclusion of types as sub-members. If there is recursion 9626 /// the entry becomes IncompleteUsed. 9627 /// 9628 /// During the expansion of a RecordType's members: 9629 /// 9630 /// If the cache contains a NonRecursive encoding for the member type, the 9631 /// cached encoding is used; 9632 /// 9633 /// If the cache contains a Recursive encoding for the member type, the 9634 /// cached encoding is 'Swapped' out, as it may be incorrect, and... 9635 /// 9636 /// If the member is a RecordType, an Incomplete encoding is placed into the 9637 /// cache to break potential recursive inclusion of itself as a sub-member; 9638 /// 9639 /// Once a member RecordType has been expanded, its temporary incomplete 9640 /// entry is removed from the cache. If a Recursive encoding was swapped out 9641 /// it is swapped back in; 9642 /// 9643 /// If an incomplete entry is used to expand a sub-member, the incomplete 9644 /// entry is marked as IncompleteUsed. The cache keeps count of how many 9645 /// IncompleteUsed entries it currently contains in IncompleteUsedCount; 9646 /// 9647 /// If a member's encoding is found to be a NonRecursive or Recursive viz: 9648 /// IncompleteUsedCount==0, the member's encoding is added to the cache. 9649 /// Else the member is part of a recursive type and thus the recursion has 9650 /// been exited too soon for the encoding to be correct for the member. 9651 /// 9652 class TypeStringCache { 9653 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; 9654 struct Entry { 9655 std::string Str; // The encoded TypeString for the type. 9656 enum Status State; // Information about the encoding in 'Str'. 9657 std::string Swapped; // A temporary place holder for a Recursive encoding 9658 // during the expansion of RecordType's members. 9659 }; 9660 std::map<const IdentifierInfo *, struct Entry> Map; 9661 unsigned IncompleteCount; // Number of Incomplete entries in the Map. 9662 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. 9663 public: 9664 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} 9665 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); 9666 bool removeIncomplete(const IdentifierInfo *ID); 9667 void addIfComplete(const IdentifierInfo *ID, StringRef Str, 9668 bool IsRecursive); 9669 StringRef lookupStr(const IdentifierInfo *ID); 9670 }; 9671 9672 /// TypeString encodings for enum & union fields must be order. 9673 /// FieldEncoding is a helper for this ordering process. 9674 class FieldEncoding { 9675 bool HasName; 9676 std::string Enc; 9677 public: 9678 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} 9679 StringRef str() { return Enc; } 9680 bool operator<(const FieldEncoding &rhs) const { 9681 if (HasName != rhs.HasName) return HasName; 9682 return Enc < rhs.Enc; 9683 } 9684 }; 9685 9686 class XCoreABIInfo : public DefaultABIInfo { 9687 public: 9688 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 9689 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9690 QualType Ty) const override; 9691 }; 9692 9693 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { 9694 mutable TypeStringCache TSC; 9695 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 9696 const CodeGen::CodeGenModule &M) const; 9697 9698 public: 9699 XCoreTargetCodeGenInfo(CodeGenTypes &CGT) 9700 : TargetCodeGenInfo(std::make_unique<XCoreABIInfo>(CGT)) {} 9701 void emitTargetMetadata(CodeGen::CodeGenModule &CGM, 9702 const llvm::MapVector<GlobalDecl, StringRef> 9703 &MangledDeclNames) const override; 9704 }; 9705 9706 } // End anonymous namespace. 9707 9708 // TODO: this implementation is likely now redundant with the default 9709 // EmitVAArg. 9710 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 9711 QualType Ty) const { 9712 CGBuilderTy &Builder = CGF.Builder; 9713 9714 // Get the VAList. 9715 CharUnits SlotSize = CharUnits::fromQuantity(4); 9716 Address AP(Builder.CreateLoad(VAListAddr), SlotSize); 9717 9718 // Handle the argument. 9719 ABIArgInfo AI = classifyArgumentType(Ty); 9720 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); 9721 llvm::Type *ArgTy = CGT.ConvertType(Ty); 9722 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 9723 AI.setCoerceToType(ArgTy); 9724 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 9725 9726 Address Val = Address::invalid(); 9727 CharUnits ArgSize = CharUnits::Zero(); 9728 switch (AI.getKind()) { 9729 case ABIArgInfo::Expand: 9730 case ABIArgInfo::CoerceAndExpand: 9731 case ABIArgInfo::InAlloca: 9732 llvm_unreachable("Unsupported ABI kind for va_arg"); 9733 case ABIArgInfo::Ignore: 9734 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); 9735 ArgSize = CharUnits::Zero(); 9736 break; 9737 case ABIArgInfo::Extend: 9738 case ABIArgInfo::Direct: 9739 Val = Builder.CreateBitCast(AP, ArgPtrTy); 9740 ArgSize = CharUnits::fromQuantity( 9741 getDataLayout().getTypeAllocSize(AI.getCoerceToType())); 9742 ArgSize = ArgSize.alignTo(SlotSize); 9743 break; 9744 case ABIArgInfo::Indirect: 9745 Val = Builder.CreateElementBitCast(AP, ArgPtrTy); 9746 Val = Address(Builder.CreateLoad(Val), TypeAlign); 9747 ArgSize = SlotSize; 9748 break; 9749 } 9750 9751 // Increment the VAList. 9752 if (!ArgSize.isZero()) { 9753 Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize); 9754 Builder.CreateStore(APN.getPointer(), VAListAddr); 9755 } 9756 9757 return Val; 9758 } 9759 9760 /// During the expansion of a RecordType, an incomplete TypeString is placed 9761 /// into the cache as a means to identify and break recursion. 9762 /// If there is a Recursive encoding in the cache, it is swapped out and will 9763 /// be reinserted by removeIncomplete(). 9764 /// All other types of encoding should have been used rather than arriving here. 9765 void TypeStringCache::addIncomplete(const IdentifierInfo *ID, 9766 std::string StubEnc) { 9767 if (!ID) 9768 return; 9769 Entry &E = Map[ID]; 9770 assert( (E.Str.empty() || E.State == Recursive) && 9771 "Incorrectly use of addIncomplete"); 9772 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); 9773 E.Swapped.swap(E.Str); // swap out the Recursive 9774 E.Str.swap(StubEnc); 9775 E.State = Incomplete; 9776 ++IncompleteCount; 9777 } 9778 9779 /// Once the RecordType has been expanded, the temporary incomplete TypeString 9780 /// must be removed from the cache. 9781 /// If a Recursive was swapped out by addIncomplete(), it will be replaced. 9782 /// Returns true if the RecordType was defined recursively. 9783 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { 9784 if (!ID) 9785 return false; 9786 auto I = Map.find(ID); 9787 assert(I != Map.end() && "Entry not present"); 9788 Entry &E = I->second; 9789 assert( (E.State == Incomplete || 9790 E.State == IncompleteUsed) && 9791 "Entry must be an incomplete type"); 9792 bool IsRecursive = false; 9793 if (E.State == IncompleteUsed) { 9794 // We made use of our Incomplete encoding, thus we are recursive. 9795 IsRecursive = true; 9796 --IncompleteUsedCount; 9797 } 9798 if (E.Swapped.empty()) 9799 Map.erase(I); 9800 else { 9801 // Swap the Recursive back. 9802 E.Swapped.swap(E.Str); 9803 E.Swapped.clear(); 9804 E.State = Recursive; 9805 } 9806 --IncompleteCount; 9807 return IsRecursive; 9808 } 9809 9810 /// Add the encoded TypeString to the cache only if it is NonRecursive or 9811 /// Recursive (viz: all sub-members were expanded as fully as possible). 9812 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, 9813 bool IsRecursive) { 9814 if (!ID || IncompleteUsedCount) 9815 return; // No key or it is is an incomplete sub-type so don't add. 9816 Entry &E = Map[ID]; 9817 if (IsRecursive && !E.Str.empty()) { 9818 assert(E.State==Recursive && E.Str.size() == Str.size() && 9819 "This is not the same Recursive entry"); 9820 // The parent container was not recursive after all, so we could have used 9821 // this Recursive sub-member entry after all, but we assumed the worse when 9822 // we started viz: IncompleteCount!=0. 9823 return; 9824 } 9825 assert(E.Str.empty() && "Entry already present"); 9826 E.Str = Str.str(); 9827 E.State = IsRecursive? Recursive : NonRecursive; 9828 } 9829 9830 /// Return a cached TypeString encoding for the ID. If there isn't one, or we 9831 /// are recursively expanding a type (IncompleteCount != 0) and the cached 9832 /// encoding is Recursive, return an empty StringRef. 9833 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { 9834 if (!ID) 9835 return StringRef(); // We have no key. 9836 auto I = Map.find(ID); 9837 if (I == Map.end()) 9838 return StringRef(); // We have no encoding. 9839 Entry &E = I->second; 9840 if (E.State == Recursive && IncompleteCount) 9841 return StringRef(); // We don't use Recursive encodings for member types. 9842 9843 if (E.State == Incomplete) { 9844 // The incomplete type is being used to break out of recursion. 9845 E.State = IncompleteUsed; 9846 ++IncompleteUsedCount; 9847 } 9848 return E.Str; 9849 } 9850 9851 /// The XCore ABI includes a type information section that communicates symbol 9852 /// type information to the linker. The linker uses this information to verify 9853 /// safety/correctness of things such as array bound and pointers et al. 9854 /// The ABI only requires C (and XC) language modules to emit TypeStrings. 9855 /// This type information (TypeString) is emitted into meta data for all global 9856 /// symbols: definitions, declarations, functions & variables. 9857 /// 9858 /// The TypeString carries type, qualifier, name, size & value details. 9859 /// Please see 'Tools Development Guide' section 2.16.2 for format details: 9860 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf 9861 /// The output is tested by test/CodeGen/xcore-stringtype.c. 9862 /// 9863 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 9864 const CodeGen::CodeGenModule &CGM, 9865 TypeStringCache &TSC); 9866 9867 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. 9868 void XCoreTargetCodeGenInfo::emitTargetMD( 9869 const Decl *D, llvm::GlobalValue *GV, 9870 const CodeGen::CodeGenModule &CGM) const { 9871 SmallStringEnc Enc; 9872 if (getTypeString(Enc, D, CGM, TSC)) { 9873 llvm::LLVMContext &Ctx = CGM.getModule().getContext(); 9874 llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), 9875 llvm::MDString::get(Ctx, Enc.str())}; 9876 llvm::NamedMDNode *MD = 9877 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); 9878 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 9879 } 9880 } 9881 9882 void XCoreTargetCodeGenInfo::emitTargetMetadata( 9883 CodeGen::CodeGenModule &CGM, 9884 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const { 9885 // Warning, new MangledDeclNames may be appended within this loop. 9886 // We rely on MapVector insertions adding new elements to the end 9887 // of the container. 9888 for (unsigned I = 0; I != MangledDeclNames.size(); ++I) { 9889 auto Val = *(MangledDeclNames.begin() + I); 9890 llvm::GlobalValue *GV = CGM.GetGlobalValue(Val.second); 9891 if (GV) { 9892 const Decl *D = Val.first.getDecl()->getMostRecentDecl(); 9893 emitTargetMD(D, GV, CGM); 9894 } 9895 } 9896 } 9897 //===----------------------------------------------------------------------===// 9898 // SPIR ABI Implementation 9899 //===----------------------------------------------------------------------===// 9900 9901 namespace { 9902 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { 9903 public: 9904 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 9905 : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {} 9906 unsigned getOpenCLKernelCallingConv() const override; 9907 }; 9908 9909 } // End anonymous namespace. 9910 9911 namespace clang { 9912 namespace CodeGen { 9913 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { 9914 DefaultABIInfo SPIRABI(CGM.getTypes()); 9915 SPIRABI.computeInfo(FI); 9916 } 9917 } 9918 } 9919 9920 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { 9921 return llvm::CallingConv::SPIR_KERNEL; 9922 } 9923 9924 static bool appendType(SmallStringEnc &Enc, QualType QType, 9925 const CodeGen::CodeGenModule &CGM, 9926 TypeStringCache &TSC); 9927 9928 /// Helper function for appendRecordType(). 9929 /// Builds a SmallVector containing the encoded field types in declaration 9930 /// order. 9931 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, 9932 const RecordDecl *RD, 9933 const CodeGen::CodeGenModule &CGM, 9934 TypeStringCache &TSC) { 9935 for (const auto *Field : RD->fields()) { 9936 SmallStringEnc Enc; 9937 Enc += "m("; 9938 Enc += Field->getName(); 9939 Enc += "){"; 9940 if (Field->isBitField()) { 9941 Enc += "b("; 9942 llvm::raw_svector_ostream OS(Enc); 9943 OS << Field->getBitWidthValue(CGM.getContext()); 9944 Enc += ':'; 9945 } 9946 if (!appendType(Enc, Field->getType(), CGM, TSC)) 9947 return false; 9948 if (Field->isBitField()) 9949 Enc += ')'; 9950 Enc += '}'; 9951 FE.emplace_back(!Field->getName().empty(), Enc); 9952 } 9953 return true; 9954 } 9955 9956 /// Appends structure and union types to Enc and adds encoding to cache. 9957 /// Recursively calls appendType (via extractFieldType) for each field. 9958 /// Union types have their fields ordered according to the ABI. 9959 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, 9960 const CodeGen::CodeGenModule &CGM, 9961 TypeStringCache &TSC, const IdentifierInfo *ID) { 9962 // Append the cached TypeString if we have one. 9963 StringRef TypeString = TSC.lookupStr(ID); 9964 if (!TypeString.empty()) { 9965 Enc += TypeString; 9966 return true; 9967 } 9968 9969 // Start to emit an incomplete TypeString. 9970 size_t Start = Enc.size(); 9971 Enc += (RT->isUnionType()? 'u' : 's'); 9972 Enc += '('; 9973 if (ID) 9974 Enc += ID->getName(); 9975 Enc += "){"; 9976 9977 // We collect all encoded fields and order as necessary. 9978 bool IsRecursive = false; 9979 const RecordDecl *RD = RT->getDecl()->getDefinition(); 9980 if (RD && !RD->field_empty()) { 9981 // An incomplete TypeString stub is placed in the cache for this RecordType 9982 // so that recursive calls to this RecordType will use it whilst building a 9983 // complete TypeString for this RecordType. 9984 SmallVector<FieldEncoding, 16> FE; 9985 std::string StubEnc(Enc.substr(Start).str()); 9986 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. 9987 TSC.addIncomplete(ID, std::move(StubEnc)); 9988 if (!extractFieldType(FE, RD, CGM, TSC)) { 9989 (void) TSC.removeIncomplete(ID); 9990 return false; 9991 } 9992 IsRecursive = TSC.removeIncomplete(ID); 9993 // The ABI requires unions to be sorted but not structures. 9994 // See FieldEncoding::operator< for sort algorithm. 9995 if (RT->isUnionType()) 9996 llvm::sort(FE); 9997 // We can now complete the TypeString. 9998 unsigned E = FE.size(); 9999 for (unsigned I = 0; I != E; ++I) { 10000 if (I) 10001 Enc += ','; 10002 Enc += FE[I].str(); 10003 } 10004 } 10005 Enc += '}'; 10006 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); 10007 return true; 10008 } 10009 10010 /// Appends enum types to Enc and adds the encoding to the cache. 10011 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, 10012 TypeStringCache &TSC, 10013 const IdentifierInfo *ID) { 10014 // Append the cached TypeString if we have one. 10015 StringRef TypeString = TSC.lookupStr(ID); 10016 if (!TypeString.empty()) { 10017 Enc += TypeString; 10018 return true; 10019 } 10020 10021 size_t Start = Enc.size(); 10022 Enc += "e("; 10023 if (ID) 10024 Enc += ID->getName(); 10025 Enc += "){"; 10026 10027 // We collect all encoded enumerations and order them alphanumerically. 10028 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { 10029 SmallVector<FieldEncoding, 16> FE; 10030 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; 10031 ++I) { 10032 SmallStringEnc EnumEnc; 10033 EnumEnc += "m("; 10034 EnumEnc += I->getName(); 10035 EnumEnc += "){"; 10036 I->getInitVal().toString(EnumEnc); 10037 EnumEnc += '}'; 10038 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); 10039 } 10040 llvm::sort(FE); 10041 unsigned E = FE.size(); 10042 for (unsigned I = 0; I != E; ++I) { 10043 if (I) 10044 Enc += ','; 10045 Enc += FE[I].str(); 10046 } 10047 } 10048 Enc += '}'; 10049 TSC.addIfComplete(ID, Enc.substr(Start), false); 10050 return true; 10051 } 10052 10053 /// Appends type's qualifier to Enc. 10054 /// This is done prior to appending the type's encoding. 10055 static void appendQualifier(SmallStringEnc &Enc, QualType QT) { 10056 // Qualifiers are emitted in alphabetical order. 10057 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; 10058 int Lookup = 0; 10059 if (QT.isConstQualified()) 10060 Lookup += 1<<0; 10061 if (QT.isRestrictQualified()) 10062 Lookup += 1<<1; 10063 if (QT.isVolatileQualified()) 10064 Lookup += 1<<2; 10065 Enc += Table[Lookup]; 10066 } 10067 10068 /// Appends built-in types to Enc. 10069 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { 10070 const char *EncType; 10071 switch (BT->getKind()) { 10072 case BuiltinType::Void: 10073 EncType = "0"; 10074 break; 10075 case BuiltinType::Bool: 10076 EncType = "b"; 10077 break; 10078 case BuiltinType::Char_U: 10079 EncType = "uc"; 10080 break; 10081 case BuiltinType::UChar: 10082 EncType = "uc"; 10083 break; 10084 case BuiltinType::SChar: 10085 EncType = "sc"; 10086 break; 10087 case BuiltinType::UShort: 10088 EncType = "us"; 10089 break; 10090 case BuiltinType::Short: 10091 EncType = "ss"; 10092 break; 10093 case BuiltinType::UInt: 10094 EncType = "ui"; 10095 break; 10096 case BuiltinType::Int: 10097 EncType = "si"; 10098 break; 10099 case BuiltinType::ULong: 10100 EncType = "ul"; 10101 break; 10102 case BuiltinType::Long: 10103 EncType = "sl"; 10104 break; 10105 case BuiltinType::ULongLong: 10106 EncType = "ull"; 10107 break; 10108 case BuiltinType::LongLong: 10109 EncType = "sll"; 10110 break; 10111 case BuiltinType::Float: 10112 EncType = "ft"; 10113 break; 10114 case BuiltinType::Double: 10115 EncType = "d"; 10116 break; 10117 case BuiltinType::LongDouble: 10118 EncType = "ld"; 10119 break; 10120 default: 10121 return false; 10122 } 10123 Enc += EncType; 10124 return true; 10125 } 10126 10127 /// Appends a pointer encoding to Enc before calling appendType for the pointee. 10128 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, 10129 const CodeGen::CodeGenModule &CGM, 10130 TypeStringCache &TSC) { 10131 Enc += "p("; 10132 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) 10133 return false; 10134 Enc += ')'; 10135 return true; 10136 } 10137 10138 /// Appends array encoding to Enc before calling appendType for the element. 10139 static bool appendArrayType(SmallStringEnc &Enc, QualType QT, 10140 const ArrayType *AT, 10141 const CodeGen::CodeGenModule &CGM, 10142 TypeStringCache &TSC, StringRef NoSizeEnc) { 10143 if (AT->getSizeModifier() != ArrayType::Normal) 10144 return false; 10145 Enc += "a("; 10146 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 10147 CAT->getSize().toStringUnsigned(Enc); 10148 else 10149 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". 10150 Enc += ':'; 10151 // The Qualifiers should be attached to the type rather than the array. 10152 appendQualifier(Enc, QT); 10153 if (!appendType(Enc, AT->getElementType(), CGM, TSC)) 10154 return false; 10155 Enc += ')'; 10156 return true; 10157 } 10158 10159 /// Appends a function encoding to Enc, calling appendType for the return type 10160 /// and the arguments. 10161 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, 10162 const CodeGen::CodeGenModule &CGM, 10163 TypeStringCache &TSC) { 10164 Enc += "f{"; 10165 if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) 10166 return false; 10167 Enc += "}("; 10168 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { 10169 // N.B. we are only interested in the adjusted param types. 10170 auto I = FPT->param_type_begin(); 10171 auto E = FPT->param_type_end(); 10172 if (I != E) { 10173 do { 10174 if (!appendType(Enc, *I, CGM, TSC)) 10175 return false; 10176 ++I; 10177 if (I != E) 10178 Enc += ','; 10179 } while (I != E); 10180 if (FPT->isVariadic()) 10181 Enc += ",va"; 10182 } else { 10183 if (FPT->isVariadic()) 10184 Enc += "va"; 10185 else 10186 Enc += '0'; 10187 } 10188 } 10189 Enc += ')'; 10190 return true; 10191 } 10192 10193 /// Handles the type's qualifier before dispatching a call to handle specific 10194 /// type encodings. 10195 static bool appendType(SmallStringEnc &Enc, QualType QType, 10196 const CodeGen::CodeGenModule &CGM, 10197 TypeStringCache &TSC) { 10198 10199 QualType QT = QType.getCanonicalType(); 10200 10201 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) 10202 // The Qualifiers should be attached to the type rather than the array. 10203 // Thus we don't call appendQualifier() here. 10204 return appendArrayType(Enc, QT, AT, CGM, TSC, ""); 10205 10206 appendQualifier(Enc, QT); 10207 10208 if (const BuiltinType *BT = QT->getAs<BuiltinType>()) 10209 return appendBuiltinType(Enc, BT); 10210 10211 if (const PointerType *PT = QT->getAs<PointerType>()) 10212 return appendPointerType(Enc, PT, CGM, TSC); 10213 10214 if (const EnumType *ET = QT->getAs<EnumType>()) 10215 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); 10216 10217 if (const RecordType *RT = QT->getAsStructureType()) 10218 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 10219 10220 if (const RecordType *RT = QT->getAsUnionType()) 10221 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 10222 10223 if (const FunctionType *FT = QT->getAs<FunctionType>()) 10224 return appendFunctionType(Enc, FT, CGM, TSC); 10225 10226 return false; 10227 } 10228 10229 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 10230 const CodeGen::CodeGenModule &CGM, 10231 TypeStringCache &TSC) { 10232 if (!D) 10233 return false; 10234 10235 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 10236 if (FD->getLanguageLinkage() != CLanguageLinkage) 10237 return false; 10238 return appendType(Enc, FD->getType(), CGM, TSC); 10239 } 10240 10241 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 10242 if (VD->getLanguageLinkage() != CLanguageLinkage) 10243 return false; 10244 QualType QT = VD->getType().getCanonicalType(); 10245 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { 10246 // Global ArrayTypes are given a size of '*' if the size is unknown. 10247 // The Qualifiers should be attached to the type rather than the array. 10248 // Thus we don't call appendQualifier() here. 10249 return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); 10250 } 10251 return appendType(Enc, QT, CGM, TSC); 10252 } 10253 return false; 10254 } 10255 10256 //===----------------------------------------------------------------------===// 10257 // RISCV ABI Implementation 10258 //===----------------------------------------------------------------------===// 10259 10260 namespace { 10261 class RISCVABIInfo : public DefaultABIInfo { 10262 private: 10263 // Size of the integer ('x') registers in bits. 10264 unsigned XLen; 10265 // Size of the floating point ('f') registers in bits. Note that the target 10266 // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target 10267 // with soft float ABI has FLen==0). 10268 unsigned FLen; 10269 static const int NumArgGPRs = 8; 10270 static const int NumArgFPRs = 8; 10271 bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, 10272 llvm::Type *&Field1Ty, 10273 CharUnits &Field1Off, 10274 llvm::Type *&Field2Ty, 10275 CharUnits &Field2Off) const; 10276 10277 public: 10278 RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen) 10279 : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {} 10280 10281 // DefaultABIInfo's classifyReturnType and classifyArgumentType are 10282 // non-virtual, but computeInfo is virtual, so we overload it. 10283 void computeInfo(CGFunctionInfo &FI) const override; 10284 10285 ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft, 10286 int &ArgFPRsLeft) const; 10287 ABIArgInfo classifyReturnType(QualType RetTy) const; 10288 10289 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 10290 QualType Ty) const override; 10291 10292 ABIArgInfo extendType(QualType Ty) const; 10293 10294 bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, 10295 CharUnits &Field1Off, llvm::Type *&Field2Ty, 10296 CharUnits &Field2Off, int &NeededArgGPRs, 10297 int &NeededArgFPRs) const; 10298 ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty, 10299 CharUnits Field1Off, 10300 llvm::Type *Field2Ty, 10301 CharUnits Field2Off) const; 10302 }; 10303 } // end anonymous namespace 10304 10305 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { 10306 QualType RetTy = FI.getReturnType(); 10307 if (!getCXXABI().classifyReturnType(FI)) 10308 FI.getReturnInfo() = classifyReturnType(RetTy); 10309 10310 // IsRetIndirect is true if classifyArgumentType indicated the value should 10311 // be passed indirect, or if the type size is a scalar greater than 2*XLen 10312 // and not a complex type with elements <= FLen. e.g. fp128 is passed direct 10313 // in LLVM IR, relying on the backend lowering code to rewrite the argument 10314 // list and pass indirectly on RV32. 10315 bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect; 10316 if (!IsRetIndirect && RetTy->isScalarType() && 10317 getContext().getTypeSize(RetTy) > (2 * XLen)) { 10318 if (RetTy->isComplexType() && FLen) { 10319 QualType EltTy = RetTy->getAs<ComplexType>()->getElementType(); 10320 IsRetIndirect = getContext().getTypeSize(EltTy) > FLen; 10321 } else { 10322 // This is a normal scalar > 2*XLen, such as fp128 on RV32. 10323 IsRetIndirect = true; 10324 } 10325 } 10326 10327 // We must track the number of GPRs used in order to conform to the RISC-V 10328 // ABI, as integer scalars passed in registers should have signext/zeroext 10329 // when promoted, but are anyext if passed on the stack. As GPR usage is 10330 // different for variadic arguments, we must also track whether we are 10331 // examining a vararg or not. 10332 int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; 10333 int ArgFPRsLeft = FLen ? NumArgFPRs : 0; 10334 int NumFixedArgs = FI.getNumRequiredArgs(); 10335 10336 int ArgNum = 0; 10337 for (auto &ArgInfo : FI.arguments()) { 10338 bool IsFixed = ArgNum < NumFixedArgs; 10339 ArgInfo.info = 10340 classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft); 10341 ArgNum++; 10342 } 10343 } 10344 10345 // Returns true if the struct is a potential candidate for the floating point 10346 // calling convention. If this function returns true, the caller is 10347 // responsible for checking that if there is only a single field then that 10348 // field is a float. 10349 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, 10350 llvm::Type *&Field1Ty, 10351 CharUnits &Field1Off, 10352 llvm::Type *&Field2Ty, 10353 CharUnits &Field2Off) const { 10354 bool IsInt = Ty->isIntegralOrEnumerationType(); 10355 bool IsFloat = Ty->isRealFloatingType(); 10356 10357 if (IsInt || IsFloat) { 10358 uint64_t Size = getContext().getTypeSize(Ty); 10359 if (IsInt && Size > XLen) 10360 return false; 10361 // Can't be eligible if larger than the FP registers. Half precision isn't 10362 // currently supported on RISC-V and the ABI hasn't been confirmed, so 10363 // default to the integer ABI in that case. 10364 if (IsFloat && (Size > FLen || Size < 32)) 10365 return false; 10366 // Can't be eligible if an integer type was already found (int+int pairs 10367 // are not eligible). 10368 if (IsInt && Field1Ty && Field1Ty->isIntegerTy()) 10369 return false; 10370 if (!Field1Ty) { 10371 Field1Ty = CGT.ConvertType(Ty); 10372 Field1Off = CurOff; 10373 return true; 10374 } 10375 if (!Field2Ty) { 10376 Field2Ty = CGT.ConvertType(Ty); 10377 Field2Off = CurOff; 10378 return true; 10379 } 10380 return false; 10381 } 10382 10383 if (auto CTy = Ty->getAs<ComplexType>()) { 10384 if (Field1Ty) 10385 return false; 10386 QualType EltTy = CTy->getElementType(); 10387 if (getContext().getTypeSize(EltTy) > FLen) 10388 return false; 10389 Field1Ty = CGT.ConvertType(EltTy); 10390 Field1Off = CurOff; 10391 assert(CurOff.isZero() && "Unexpected offset for first field"); 10392 Field2Ty = Field1Ty; 10393 Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy); 10394 return true; 10395 } 10396 10397 if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) { 10398 uint64_t ArraySize = ATy->getSize().getZExtValue(); 10399 QualType EltTy = ATy->getElementType(); 10400 CharUnits EltSize = getContext().getTypeSizeInChars(EltTy); 10401 for (uint64_t i = 0; i < ArraySize; ++i) { 10402 bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty, 10403 Field1Off, Field2Ty, Field2Off); 10404 if (!Ret) 10405 return false; 10406 CurOff += EltSize; 10407 } 10408 return true; 10409 } 10410 10411 if (const auto *RTy = Ty->getAs<RecordType>()) { 10412 // Structures with either a non-trivial destructor or a non-trivial 10413 // copy constructor are not eligible for the FP calling convention. 10414 if (getRecordArgABI(Ty, CGT.getCXXABI())) 10415 return false; 10416 if (isEmptyRecord(getContext(), Ty, true)) 10417 return true; 10418 const RecordDecl *RD = RTy->getDecl(); 10419 // Unions aren't eligible unless they're empty (which is caught above). 10420 if (RD->isUnion()) 10421 return false; 10422 int ZeroWidthBitFieldCount = 0; 10423 for (const FieldDecl *FD : RD->fields()) { 10424 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 10425 uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex()); 10426 QualType QTy = FD->getType(); 10427 if (FD->isBitField()) { 10428 unsigned BitWidth = FD->getBitWidthValue(getContext()); 10429 // Allow a bitfield with a type greater than XLen as long as the 10430 // bitwidth is XLen or less. 10431 if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen) 10432 QTy = getContext().getIntTypeForBitwidth(XLen, false); 10433 if (BitWidth == 0) { 10434 ZeroWidthBitFieldCount++; 10435 continue; 10436 } 10437 } 10438 10439 bool Ret = detectFPCCEligibleStructHelper( 10440 QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits), 10441 Field1Ty, Field1Off, Field2Ty, Field2Off); 10442 if (!Ret) 10443 return false; 10444 10445 // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp 10446 // or int+fp structs, but are ignored for a struct with an fp field and 10447 // any number of zero-width bitfields. 10448 if (Field2Ty && ZeroWidthBitFieldCount > 0) 10449 return false; 10450 } 10451 return Field1Ty != nullptr; 10452 } 10453 10454 return false; 10455 } 10456 10457 // Determine if a struct is eligible for passing according to the floating 10458 // point calling convention (i.e., when flattened it contains a single fp 10459 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and 10460 // NeededArgGPRs are incremented appropriately. 10461 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, 10462 CharUnits &Field1Off, 10463 llvm::Type *&Field2Ty, 10464 CharUnits &Field2Off, 10465 int &NeededArgGPRs, 10466 int &NeededArgFPRs) const { 10467 Field1Ty = nullptr; 10468 Field2Ty = nullptr; 10469 NeededArgGPRs = 0; 10470 NeededArgFPRs = 0; 10471 bool IsCandidate = detectFPCCEligibleStructHelper( 10472 Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off); 10473 // Not really a candidate if we have a single int but no float. 10474 if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy()) 10475 return false; 10476 if (!IsCandidate) 10477 return false; 10478 if (Field1Ty && Field1Ty->isFloatingPointTy()) 10479 NeededArgFPRs++; 10480 else if (Field1Ty) 10481 NeededArgGPRs++; 10482 if (Field2Ty && Field2Ty->isFloatingPointTy()) 10483 NeededArgFPRs++; 10484 else if (Field2Ty) 10485 NeededArgGPRs++; 10486 return IsCandidate; 10487 } 10488 10489 // Call getCoerceAndExpand for the two-element flattened struct described by 10490 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an 10491 // appropriate coerceToType and unpaddedCoerceToType. 10492 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct( 10493 llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty, 10494 CharUnits Field2Off) const { 10495 SmallVector<llvm::Type *, 3> CoerceElts; 10496 SmallVector<llvm::Type *, 2> UnpaddedCoerceElts; 10497 if (!Field1Off.isZero()) 10498 CoerceElts.push_back(llvm::ArrayType::get( 10499 llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity())); 10500 10501 CoerceElts.push_back(Field1Ty); 10502 UnpaddedCoerceElts.push_back(Field1Ty); 10503 10504 if (!Field2Ty) { 10505 return ABIArgInfo::getCoerceAndExpand( 10506 llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()), 10507 UnpaddedCoerceElts[0]); 10508 } 10509 10510 CharUnits Field2Align = 10511 CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty)); 10512 CharUnits Field1Size = 10513 CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty)); 10514 CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align); 10515 10516 CharUnits Padding = CharUnits::Zero(); 10517 if (Field2Off > Field2OffNoPadNoPack) 10518 Padding = Field2Off - Field2OffNoPadNoPack; 10519 else if (Field2Off != Field2Align && Field2Off > Field1Size) 10520 Padding = Field2Off - Field1Size; 10521 10522 bool IsPacked = !Field2Off.isMultipleOf(Field2Align); 10523 10524 if (!Padding.isZero()) 10525 CoerceElts.push_back(llvm::ArrayType::get( 10526 llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity())); 10527 10528 CoerceElts.push_back(Field2Ty); 10529 UnpaddedCoerceElts.push_back(Field2Ty); 10530 10531 auto CoerceToType = 10532 llvm::StructType::get(getVMContext(), CoerceElts, IsPacked); 10533 auto UnpaddedCoerceToType = 10534 llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked); 10535 10536 return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType); 10537 } 10538 10539 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, 10540 int &ArgGPRsLeft, 10541 int &ArgFPRsLeft) const { 10542 assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); 10543 Ty = useFirstFieldIfTransparentUnion(Ty); 10544 10545 // Structures with either a non-trivial destructor or a non-trivial 10546 // copy constructor are always passed indirectly. 10547 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 10548 if (ArgGPRsLeft) 10549 ArgGPRsLeft -= 1; 10550 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == 10551 CGCXXABI::RAA_DirectInMemory); 10552 } 10553 10554 // Ignore empty structs/unions. 10555 if (isEmptyRecord(getContext(), Ty, true)) 10556 return ABIArgInfo::getIgnore(); 10557 10558 uint64_t Size = getContext().getTypeSize(Ty); 10559 10560 // Pass floating point values via FPRs if possible. 10561 if (IsFixed && Ty->isFloatingType() && !Ty->isComplexType() && 10562 FLen >= Size && ArgFPRsLeft) { 10563 ArgFPRsLeft--; 10564 return ABIArgInfo::getDirect(); 10565 } 10566 10567 // Complex types for the hard float ABI must be passed direct rather than 10568 // using CoerceAndExpand. 10569 if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) { 10570 QualType EltTy = Ty->castAs<ComplexType>()->getElementType(); 10571 if (getContext().getTypeSize(EltTy) <= FLen) { 10572 ArgFPRsLeft -= 2; 10573 return ABIArgInfo::getDirect(); 10574 } 10575 } 10576 10577 if (IsFixed && FLen && Ty->isStructureOrClassType()) { 10578 llvm::Type *Field1Ty = nullptr; 10579 llvm::Type *Field2Ty = nullptr; 10580 CharUnits Field1Off = CharUnits::Zero(); 10581 CharUnits Field2Off = CharUnits::Zero(); 10582 int NeededArgGPRs; 10583 int NeededArgFPRs; 10584 bool IsCandidate = 10585 detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off, 10586 NeededArgGPRs, NeededArgFPRs); 10587 if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft && 10588 NeededArgFPRs <= ArgFPRsLeft) { 10589 ArgGPRsLeft -= NeededArgGPRs; 10590 ArgFPRsLeft -= NeededArgFPRs; 10591 return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty, 10592 Field2Off); 10593 } 10594 } 10595 10596 uint64_t NeededAlign = getContext().getTypeAlign(Ty); 10597 bool MustUseStack = false; 10598 // Determine the number of GPRs needed to pass the current argument 10599 // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" 10600 // register pairs, so may consume 3 registers. 10601 int NeededArgGPRs = 1; 10602 if (!IsFixed && NeededAlign == 2 * XLen) 10603 NeededArgGPRs = 2 + (ArgGPRsLeft % 2); 10604 else if (Size > XLen && Size <= 2 * XLen) 10605 NeededArgGPRs = 2; 10606 10607 if (NeededArgGPRs > ArgGPRsLeft) { 10608 MustUseStack = true; 10609 NeededArgGPRs = ArgGPRsLeft; 10610 } 10611 10612 ArgGPRsLeft -= NeededArgGPRs; 10613 10614 if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { 10615 // Treat an enum type as its underlying type. 10616 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 10617 Ty = EnumTy->getDecl()->getIntegerType(); 10618 10619 // All integral types are promoted to XLen width, unless passed on the 10620 // stack. 10621 if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { 10622 return extendType(Ty); 10623 } 10624 10625 if (const auto *EIT = Ty->getAs<ExtIntType>()) { 10626 if (EIT->getNumBits() < XLen && !MustUseStack) 10627 return extendType(Ty); 10628 if (EIT->getNumBits() > 128 || 10629 (!getContext().getTargetInfo().hasInt128Type() && 10630 EIT->getNumBits() > 64)) 10631 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 10632 } 10633 10634 return ABIArgInfo::getDirect(); 10635 } 10636 10637 // Aggregates which are <= 2*XLen will be passed in registers if possible, 10638 // so coerce to integers. 10639 if (Size <= 2 * XLen) { 10640 unsigned Alignment = getContext().getTypeAlign(Ty); 10641 10642 // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is 10643 // required, and a 2-element XLen array if only XLen alignment is required. 10644 if (Size <= XLen) { 10645 return ABIArgInfo::getDirect( 10646 llvm::IntegerType::get(getVMContext(), XLen)); 10647 } else if (Alignment == 2 * XLen) { 10648 return ABIArgInfo::getDirect( 10649 llvm::IntegerType::get(getVMContext(), 2 * XLen)); 10650 } else { 10651 return ABIArgInfo::getDirect(llvm::ArrayType::get( 10652 llvm::IntegerType::get(getVMContext(), XLen), 2)); 10653 } 10654 } 10655 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 10656 } 10657 10658 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { 10659 if (RetTy->isVoidType()) 10660 return ABIArgInfo::getIgnore(); 10661 10662 int ArgGPRsLeft = 2; 10663 int ArgFPRsLeft = FLen ? 2 : 0; 10664 10665 // The rules for return and argument types are the same, so defer to 10666 // classifyArgumentType. 10667 return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft, 10668 ArgFPRsLeft); 10669 } 10670 10671 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 10672 QualType Ty) const { 10673 CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); 10674 10675 // Empty records are ignored for parameter passing purposes. 10676 if (isEmptyRecord(getContext(), Ty, true)) { 10677 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); 10678 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 10679 return Addr; 10680 } 10681 10682 std::pair<CharUnits, CharUnits> SizeAndAlign = 10683 getContext().getTypeInfoInChars(Ty); 10684 10685 // Arguments bigger than 2*Xlen bytes are passed indirectly. 10686 bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; 10687 10688 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, 10689 SlotSize, /*AllowHigherAlign=*/true); 10690 } 10691 10692 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { 10693 int TySize = getContext().getTypeSize(Ty); 10694 // RV64 ABI requires unsigned 32 bit integers to be sign extended. 10695 if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) 10696 return ABIArgInfo::getSignExtend(Ty); 10697 return ABIArgInfo::getExtend(Ty); 10698 } 10699 10700 namespace { 10701 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { 10702 public: 10703 RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, 10704 unsigned FLen) 10705 : TargetCodeGenInfo(std::make_unique<RISCVABIInfo>(CGT, XLen, FLen)) {} 10706 10707 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 10708 CodeGen::CodeGenModule &CGM) const override { 10709 const auto *FD = dyn_cast_or_null<FunctionDecl>(D); 10710 if (!FD) return; 10711 10712 const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); 10713 if (!Attr) 10714 return; 10715 10716 const char *Kind; 10717 switch (Attr->getInterrupt()) { 10718 case RISCVInterruptAttr::user: Kind = "user"; break; 10719 case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; 10720 case RISCVInterruptAttr::machine: Kind = "machine"; break; 10721 } 10722 10723 auto *Fn = cast<llvm::Function>(GV); 10724 10725 Fn->addFnAttr("interrupt", Kind); 10726 } 10727 }; 10728 } // namespace 10729 10730 //===----------------------------------------------------------------------===// 10731 // VE ABI Implementation. 10732 // 10733 namespace { 10734 class VEABIInfo : public DefaultABIInfo { 10735 public: 10736 VEABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 10737 10738 private: 10739 ABIArgInfo classifyReturnType(QualType RetTy) const; 10740 ABIArgInfo classifyArgumentType(QualType RetTy) const; 10741 void computeInfo(CGFunctionInfo &FI) const override; 10742 }; 10743 } // end anonymous namespace 10744 10745 ABIArgInfo VEABIInfo::classifyReturnType(QualType Ty) const { 10746 if (Ty->isAnyComplexType()) 10747 return ABIArgInfo::getDirect(); 10748 uint64_t Size = getContext().getTypeSize(Ty); 10749 if (Size < 64 && Ty->isIntegerType()) 10750 return ABIArgInfo::getExtend(Ty); 10751 return DefaultABIInfo::classifyReturnType(Ty); 10752 } 10753 10754 ABIArgInfo VEABIInfo::classifyArgumentType(QualType Ty) const { 10755 if (Ty->isAnyComplexType()) 10756 return ABIArgInfo::getDirect(); 10757 uint64_t Size = getContext().getTypeSize(Ty); 10758 if (Size < 64 && Ty->isIntegerType()) 10759 return ABIArgInfo::getExtend(Ty); 10760 return DefaultABIInfo::classifyArgumentType(Ty); 10761 } 10762 10763 void VEABIInfo::computeInfo(CGFunctionInfo &FI) const { 10764 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 10765 for (auto &Arg : FI.arguments()) 10766 Arg.info = classifyArgumentType(Arg.type); 10767 } 10768 10769 namespace { 10770 class VETargetCodeGenInfo : public TargetCodeGenInfo { 10771 public: 10772 VETargetCodeGenInfo(CodeGenTypes &CGT) 10773 : TargetCodeGenInfo(std::make_unique<VEABIInfo>(CGT)) {} 10774 // VE ABI requires the arguments of variadic and prototype-less functions 10775 // are passed in both registers and memory. 10776 bool isNoProtoCallVariadic(const CallArgList &args, 10777 const FunctionNoProtoType *fnType) const override { 10778 return true; 10779 } 10780 }; 10781 } // end anonymous namespace 10782 10783 //===----------------------------------------------------------------------===// 10784 // Driver code 10785 //===----------------------------------------------------------------------===// 10786 10787 bool CodeGenModule::supportsCOMDAT() const { 10788 return getTriple().supportsCOMDAT(); 10789 } 10790 10791 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { 10792 if (TheTargetCodeGenInfo) 10793 return *TheTargetCodeGenInfo; 10794 10795 // Helper to set the unique_ptr while still keeping the return value. 10796 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { 10797 this->TheTargetCodeGenInfo.reset(P); 10798 return *P; 10799 }; 10800 10801 const llvm::Triple &Triple = getTarget().getTriple(); 10802 switch (Triple.getArch()) { 10803 default: 10804 return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); 10805 10806 case llvm::Triple::le32: 10807 return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); 10808 case llvm::Triple::mips: 10809 case llvm::Triple::mipsel: 10810 if (Triple.getOS() == llvm::Triple::NaCl) 10811 return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); 10812 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); 10813 10814 case llvm::Triple::mips64: 10815 case llvm::Triple::mips64el: 10816 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); 10817 10818 case llvm::Triple::avr: 10819 return SetCGInfo(new AVRTargetCodeGenInfo(Types)); 10820 10821 case llvm::Triple::aarch64: 10822 case llvm::Triple::aarch64_32: 10823 case llvm::Triple::aarch64_be: { 10824 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; 10825 if (getTarget().getABI() == "darwinpcs") 10826 Kind = AArch64ABIInfo::DarwinPCS; 10827 else if (Triple.isOSWindows()) 10828 return SetCGInfo( 10829 new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); 10830 10831 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); 10832 } 10833 10834 case llvm::Triple::wasm32: 10835 case llvm::Triple::wasm64: { 10836 WebAssemblyABIInfo::ABIKind Kind = WebAssemblyABIInfo::MVP; 10837 if (getTarget().getABI() == "experimental-mv") 10838 Kind = WebAssemblyABIInfo::ExperimentalMV; 10839 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types, Kind)); 10840 } 10841 10842 case llvm::Triple::arm: 10843 case llvm::Triple::armeb: 10844 case llvm::Triple::thumb: 10845 case llvm::Triple::thumbeb: { 10846 if (Triple.getOS() == llvm::Triple::Win32) { 10847 return SetCGInfo( 10848 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); 10849 } 10850 10851 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; 10852 StringRef ABIStr = getTarget().getABI(); 10853 if (ABIStr == "apcs-gnu") 10854 Kind = ARMABIInfo::APCS; 10855 else if (ABIStr == "aapcs16") 10856 Kind = ARMABIInfo::AAPCS16_VFP; 10857 else if (CodeGenOpts.FloatABI == "hard" || 10858 (CodeGenOpts.FloatABI != "soft" && 10859 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 10860 Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 10861 Triple.getEnvironment() == llvm::Triple::EABIHF))) 10862 Kind = ARMABIInfo::AAPCS_VFP; 10863 10864 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); 10865 } 10866 10867 case llvm::Triple::ppc: { 10868 if (Triple.isOSAIX()) 10869 return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ false)); 10870 10871 bool IsSoftFloat = 10872 CodeGenOpts.FloatABI == "soft" || getTarget().hasFeature("spe"); 10873 bool RetSmallStructInRegABI = 10874 PPC32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); 10875 return SetCGInfo( 10876 new PPC32TargetCodeGenInfo(Types, IsSoftFloat, RetSmallStructInRegABI)); 10877 } 10878 case llvm::Triple::ppc64: 10879 if (Triple.isOSAIX()) 10880 return SetCGInfo(new AIXTargetCodeGenInfo(Types, /*Is64Bit*/ true)); 10881 10882 if (Triple.isOSBinFormatELF()) { 10883 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; 10884 if (getTarget().getABI() == "elfv2") 10885 Kind = PPC64_SVR4_ABIInfo::ELFv2; 10886 bool HasQPX = getTarget().getABI() == "elfv1-qpx"; 10887 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; 10888 10889 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, 10890 IsSoftFloat)); 10891 } 10892 return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); 10893 case llvm::Triple::ppc64le: { 10894 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); 10895 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; 10896 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") 10897 Kind = PPC64_SVR4_ABIInfo::ELFv1; 10898 bool HasQPX = getTarget().getABI() == "elfv1-qpx"; 10899 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; 10900 10901 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, 10902 IsSoftFloat)); 10903 } 10904 10905 case llvm::Triple::nvptx: 10906 case llvm::Triple::nvptx64: 10907 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); 10908 10909 case llvm::Triple::msp430: 10910 return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); 10911 10912 case llvm::Triple::riscv32: 10913 case llvm::Triple::riscv64: { 10914 StringRef ABIStr = getTarget().getABI(); 10915 unsigned XLen = getTarget().getPointerWidth(0); 10916 unsigned ABIFLen = 0; 10917 if (ABIStr.endswith("f")) 10918 ABIFLen = 32; 10919 else if (ABIStr.endswith("d")) 10920 ABIFLen = 64; 10921 return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen)); 10922 } 10923 10924 case llvm::Triple::systemz: { 10925 bool SoftFloat = CodeGenOpts.FloatABI == "soft"; 10926 bool HasVector = !SoftFloat && getTarget().getABI() == "vector"; 10927 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector, SoftFloat)); 10928 } 10929 10930 case llvm::Triple::tce: 10931 case llvm::Triple::tcele: 10932 return SetCGInfo(new TCETargetCodeGenInfo(Types)); 10933 10934 case llvm::Triple::x86: { 10935 bool IsDarwinVectorABI = Triple.isOSDarwin(); 10936 bool RetSmallStructInRegABI = 10937 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); 10938 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); 10939 10940 if (Triple.getOS() == llvm::Triple::Win32) { 10941 return SetCGInfo(new WinX86_32TargetCodeGenInfo( 10942 Types, IsDarwinVectorABI, RetSmallStructInRegABI, 10943 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); 10944 } else { 10945 return SetCGInfo(new X86_32TargetCodeGenInfo( 10946 Types, IsDarwinVectorABI, RetSmallStructInRegABI, 10947 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, 10948 CodeGenOpts.FloatABI == "soft")); 10949 } 10950 } 10951 10952 case llvm::Triple::x86_64: { 10953 StringRef ABI = getTarget().getABI(); 10954 X86AVXABILevel AVXLevel = 10955 (ABI == "avx512" 10956 ? X86AVXABILevel::AVX512 10957 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); 10958 10959 switch (Triple.getOS()) { 10960 case llvm::Triple::Win32: 10961 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); 10962 default: 10963 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); 10964 } 10965 } 10966 case llvm::Triple::hexagon: 10967 return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); 10968 case llvm::Triple::lanai: 10969 return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); 10970 case llvm::Triple::r600: 10971 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); 10972 case llvm::Triple::amdgcn: 10973 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); 10974 case llvm::Triple::sparc: 10975 return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); 10976 case llvm::Triple::sparcv9: 10977 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); 10978 case llvm::Triple::xcore: 10979 return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); 10980 case llvm::Triple::arc: 10981 return SetCGInfo(new ARCTargetCodeGenInfo(Types)); 10982 case llvm::Triple::spir: 10983 case llvm::Triple::spir64: 10984 return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); 10985 case llvm::Triple::ve: 10986 return SetCGInfo(new VETargetCodeGenInfo(Types)); 10987 } 10988 } 10989 10990 /// Create an OpenCL kernel for an enqueued block. 10991 /// 10992 /// The kernel has the same function type as the block invoke function. Its 10993 /// name is the name of the block invoke function postfixed with "_kernel". 10994 /// It simply calls the block invoke function then returns. 10995 llvm::Function * 10996 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, 10997 llvm::Function *Invoke, 10998 llvm::Value *BlockLiteral) const { 10999 auto *InvokeFT = Invoke->getFunctionType(); 11000 llvm::SmallVector<llvm::Type *, 2> ArgTys; 11001 for (auto &P : InvokeFT->params()) 11002 ArgTys.push_back(P); 11003 auto &C = CGF.getLLVMContext(); 11004 std::string Name = Invoke->getName().str() + "_kernel"; 11005 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); 11006 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, 11007 &CGF.CGM.getModule()); 11008 auto IP = CGF.Builder.saveIP(); 11009 auto *BB = llvm::BasicBlock::Create(C, "entry", F); 11010 auto &Builder = CGF.Builder; 11011 Builder.SetInsertPoint(BB); 11012 llvm::SmallVector<llvm::Value *, 2> Args; 11013 for (auto &A : F->args()) 11014 Args.push_back(&A); 11015 Builder.CreateCall(Invoke, Args); 11016 Builder.CreateRetVoid(); 11017 Builder.restoreIP(IP); 11018 return F; 11019 } 11020 11021 /// Create an OpenCL kernel for an enqueued block. 11022 /// 11023 /// The type of the first argument (the block literal) is the struct type 11024 /// of the block literal instead of a pointer type. The first argument 11025 /// (block literal) is passed directly by value to the kernel. The kernel 11026 /// allocates the same type of struct on stack and stores the block literal 11027 /// to it and passes its pointer to the block invoke function. The kernel 11028 /// has "enqueued-block" function attribute and kernel argument metadata. 11029 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( 11030 CodeGenFunction &CGF, llvm::Function *Invoke, 11031 llvm::Value *BlockLiteral) const { 11032 auto &Builder = CGF.Builder; 11033 auto &C = CGF.getLLVMContext(); 11034 11035 auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); 11036 auto *InvokeFT = Invoke->getFunctionType(); 11037 llvm::SmallVector<llvm::Type *, 2> ArgTys; 11038 llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; 11039 llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; 11040 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; 11041 llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; 11042 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; 11043 llvm::SmallVector<llvm::Metadata *, 8> ArgNames; 11044 11045 ArgTys.push_back(BlockTy); 11046 ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); 11047 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); 11048 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); 11049 ArgTypeQuals.push_back(llvm::MDString::get(C, "")); 11050 AccessQuals.push_back(llvm::MDString::get(C, "none")); 11051 ArgNames.push_back(llvm::MDString::get(C, "block_literal")); 11052 for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { 11053 ArgTys.push_back(InvokeFT->getParamType(I)); 11054 ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); 11055 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); 11056 AccessQuals.push_back(llvm::MDString::get(C, "none")); 11057 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); 11058 ArgTypeQuals.push_back(llvm::MDString::get(C, "")); 11059 ArgNames.push_back( 11060 llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); 11061 } 11062 std::string Name = Invoke->getName().str() + "_kernel"; 11063 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); 11064 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, 11065 &CGF.CGM.getModule()); 11066 F->addFnAttr("enqueued-block"); 11067 auto IP = CGF.Builder.saveIP(); 11068 auto *BB = llvm::BasicBlock::Create(C, "entry", F); 11069 Builder.SetInsertPoint(BB); 11070 const auto BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(BlockTy); 11071 auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); 11072 BlockPtr->setAlignment(BlockAlign); 11073 Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); 11074 auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); 11075 llvm::SmallVector<llvm::Value *, 2> Args; 11076 Args.push_back(Cast); 11077 for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) 11078 Args.push_back(I); 11079 Builder.CreateCall(Invoke, Args); 11080 Builder.CreateRetVoid(); 11081 Builder.restoreIP(IP); 11082 11083 F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); 11084 F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); 11085 F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); 11086 F->setMetadata("kernel_arg_base_type", 11087 llvm::MDNode::get(C, ArgBaseTypeNames)); 11088 F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); 11089 if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) 11090 F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); 11091 11092 return F; 11093 } 11094