1 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // These classes wrap the information about a call or function 11 // definition used to handle ABI compliancy. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "TargetInfo.h" 16 #include "ABIInfo.h" 17 #include "CGCXXABI.h" 18 #include "CGValue.h" 19 #include "CodeGenFunction.h" 20 #include "clang/AST/RecordLayout.h" 21 #include "clang/CodeGen/CGFunctionInfo.h" 22 #include "clang/CodeGen/SwiftCallingConv.h" 23 #include "clang/Frontend/CodeGenOptions.h" 24 #include "llvm/ADT/StringExtras.h" 25 #include "llvm/ADT/Triple.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/Type.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <algorithm> // std::sort 30 31 using namespace clang; 32 using namespace CodeGen; 33 34 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, 35 llvm::Value *Array, 36 llvm::Value *Value, 37 unsigned FirstIndex, 38 unsigned LastIndex) { 39 // Alternatively, we could emit this as a loop in the source. 40 for (unsigned I = FirstIndex; I <= LastIndex; ++I) { 41 llvm::Value *Cell = 42 Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); 43 Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); 44 } 45 } 46 47 static bool isAggregateTypeForABI(QualType T) { 48 return !CodeGenFunction::hasScalarEvaluationKind(T) || 49 T->isMemberFunctionPointerType(); 50 } 51 52 ABIArgInfo 53 ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, 54 llvm::Type *Padding) const { 55 return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), 56 ByRef, Realign, Padding); 57 } 58 59 ABIArgInfo 60 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { 61 return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), 62 /*ByRef*/ false, Realign); 63 } 64 65 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 66 QualType Ty) const { 67 return Address::invalid(); 68 } 69 70 ABIInfo::~ABIInfo() {} 71 72 /// Does the given lowering require more than the given number of 73 /// registers when expanded? 74 /// 75 /// This is intended to be the basis of a reasonable basic implementation 76 /// of should{Pass,Return}IndirectlyForSwift. 77 /// 78 /// For most targets, a limit of four total registers is reasonable; this 79 /// limits the amount of code required in order to move around the value 80 /// in case it wasn't produced immediately prior to the call by the caller 81 /// (or wasn't produced in exactly the right registers) or isn't used 82 /// immediately within the callee. But some targets may need to further 83 /// limit the register count due to an inability to support that many 84 /// return registers. 85 static bool occupiesMoreThan(CodeGenTypes &cgt, 86 ArrayRef<llvm::Type*> scalarTypes, 87 unsigned maxAllRegisters) { 88 unsigned intCount = 0, fpCount = 0; 89 for (llvm::Type *type : scalarTypes) { 90 if (type->isPointerTy()) { 91 intCount++; 92 } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { 93 auto ptrWidth = cgt.getTarget().getPointerWidth(0); 94 intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; 95 } else { 96 assert(type->isVectorTy() || type->isFloatingPointTy()); 97 fpCount++; 98 } 99 } 100 101 return (intCount + fpCount > maxAllRegisters); 102 } 103 104 bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, 105 llvm::Type *eltTy, 106 unsigned numElts) const { 107 // The default implementation of this assumes that the target guarantees 108 // 128-bit SIMD support but nothing more. 109 return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); 110 } 111 112 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, 113 CGCXXABI &CXXABI) { 114 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 115 if (!RD) 116 return CGCXXABI::RAA_Default; 117 return CXXABI.getRecordArgABI(RD); 118 } 119 120 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, 121 CGCXXABI &CXXABI) { 122 const RecordType *RT = T->getAs<RecordType>(); 123 if (!RT) 124 return CGCXXABI::RAA_Default; 125 return getRecordArgABI(RT, CXXABI); 126 } 127 128 /// Pass transparent unions as if they were the type of the first element. Sema 129 /// should ensure that all elements of the union have the same "machine type". 130 static QualType useFirstFieldIfTransparentUnion(QualType Ty) { 131 if (const RecordType *UT = Ty->getAsUnionType()) { 132 const RecordDecl *UD = UT->getDecl(); 133 if (UD->hasAttr<TransparentUnionAttr>()) { 134 assert(!UD->field_empty() && "sema created an empty transparent union"); 135 return UD->field_begin()->getType(); 136 } 137 } 138 return Ty; 139 } 140 141 CGCXXABI &ABIInfo::getCXXABI() const { 142 return CGT.getCXXABI(); 143 } 144 145 ASTContext &ABIInfo::getContext() const { 146 return CGT.getContext(); 147 } 148 149 llvm::LLVMContext &ABIInfo::getVMContext() const { 150 return CGT.getLLVMContext(); 151 } 152 153 const llvm::DataLayout &ABIInfo::getDataLayout() const { 154 return CGT.getDataLayout(); 155 } 156 157 const TargetInfo &ABIInfo::getTarget() const { 158 return CGT.getTarget(); 159 } 160 161 bool ABIInfo:: isAndroid() const { return getTarget().getTriple().isAndroid(); } 162 163 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 164 return false; 165 } 166 167 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 168 uint64_t Members) const { 169 return false; 170 } 171 172 bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { 173 return false; 174 } 175 176 LLVM_DUMP_METHOD void ABIArgInfo::dump() const { 177 raw_ostream &OS = llvm::errs(); 178 OS << "(ABIArgInfo Kind="; 179 switch (TheKind) { 180 case Direct: 181 OS << "Direct Type="; 182 if (llvm::Type *Ty = getCoerceToType()) 183 Ty->print(OS); 184 else 185 OS << "null"; 186 break; 187 case Extend: 188 OS << "Extend"; 189 break; 190 case Ignore: 191 OS << "Ignore"; 192 break; 193 case InAlloca: 194 OS << "InAlloca Offset=" << getInAllocaFieldIndex(); 195 break; 196 case Indirect: 197 OS << "Indirect Align=" << getIndirectAlign().getQuantity() 198 << " ByVal=" << getIndirectByVal() 199 << " Realign=" << getIndirectRealign(); 200 break; 201 case Expand: 202 OS << "Expand"; 203 break; 204 case CoerceAndExpand: 205 OS << "CoerceAndExpand Type="; 206 getCoerceAndExpandType()->print(OS); 207 break; 208 } 209 OS << ")\n"; 210 } 211 212 // Dynamically round a pointer up to a multiple of the given alignment. 213 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, 214 llvm::Value *Ptr, 215 CharUnits Align) { 216 llvm::Value *PtrAsInt = Ptr; 217 // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; 218 PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); 219 PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, 220 llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); 221 PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, 222 llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); 223 PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, 224 Ptr->getType(), 225 Ptr->getName() + ".aligned"); 226 return PtrAsInt; 227 } 228 229 /// Emit va_arg for a platform using the common void* representation, 230 /// where arguments are simply emitted in an array of slots on the stack. 231 /// 232 /// This version implements the core direct-value passing rules. 233 /// 234 /// \param SlotSize - The size and alignment of a stack slot. 235 /// Each argument will be allocated to a multiple of this number of 236 /// slots, and all the slots will be aligned to this value. 237 /// \param AllowHigherAlign - The slot alignment is not a cap; 238 /// an argument type with an alignment greater than the slot size 239 /// will be emitted on a higher-alignment address, potentially 240 /// leaving one or more empty slots behind as padding. If this 241 /// is false, the returned address might be less-aligned than 242 /// DirectAlign. 243 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, 244 Address VAListAddr, 245 llvm::Type *DirectTy, 246 CharUnits DirectSize, 247 CharUnits DirectAlign, 248 CharUnits SlotSize, 249 bool AllowHigherAlign) { 250 // Cast the element type to i8* if necessary. Some platforms define 251 // va_list as a struct containing an i8* instead of just an i8*. 252 if (VAListAddr.getElementType() != CGF.Int8PtrTy) 253 VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); 254 255 llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); 256 257 // If the CC aligns values higher than the slot size, do so if needed. 258 Address Addr = Address::invalid(); 259 if (AllowHigherAlign && DirectAlign > SlotSize) { 260 Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), 261 DirectAlign); 262 } else { 263 Addr = Address(Ptr, SlotSize); 264 } 265 266 // Advance the pointer past the argument, then store that back. 267 CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); 268 llvm::Value *NextPtr = 269 CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize, 270 "argp.next"); 271 CGF.Builder.CreateStore(NextPtr, VAListAddr); 272 273 // If the argument is smaller than a slot, and this is a big-endian 274 // target, the argument will be right-adjusted in its slot. 275 if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian()) { 276 Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); 277 } 278 279 Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); 280 return Addr; 281 } 282 283 /// Emit va_arg for a platform using the common void* representation, 284 /// where arguments are simply emitted in an array of slots on the stack. 285 /// 286 /// \param IsIndirect - Values of this type are passed indirectly. 287 /// \param ValueInfo - The size and alignment of this type, generally 288 /// computed with getContext().getTypeInfoInChars(ValueTy). 289 /// \param SlotSizeAndAlign - The size and alignment of a stack slot. 290 /// Each argument will be allocated to a multiple of this number of 291 /// slots, and all the slots will be aligned to this value. 292 /// \param AllowHigherAlign - The slot alignment is not a cap; 293 /// an argument type with an alignment greater than the slot size 294 /// will be emitted on a higher-alignment address, potentially 295 /// leaving one or more empty slots behind as padding. 296 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, 297 QualType ValueTy, bool IsIndirect, 298 std::pair<CharUnits, CharUnits> ValueInfo, 299 CharUnits SlotSizeAndAlign, 300 bool AllowHigherAlign) { 301 // The size and alignment of the value that was passed directly. 302 CharUnits DirectSize, DirectAlign; 303 if (IsIndirect) { 304 DirectSize = CGF.getPointerSize(); 305 DirectAlign = CGF.getPointerAlign(); 306 } else { 307 DirectSize = ValueInfo.first; 308 DirectAlign = ValueInfo.second; 309 } 310 311 // Cast the address we've calculated to the right type. 312 llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); 313 if (IsIndirect) 314 DirectTy = DirectTy->getPointerTo(0); 315 316 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, 317 DirectSize, DirectAlign, 318 SlotSizeAndAlign, 319 AllowHigherAlign); 320 321 if (IsIndirect) { 322 Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); 323 } 324 325 return Addr; 326 327 } 328 329 static Address emitMergePHI(CodeGenFunction &CGF, 330 Address Addr1, llvm::BasicBlock *Block1, 331 Address Addr2, llvm::BasicBlock *Block2, 332 const llvm::Twine &Name = "") { 333 assert(Addr1.getType() == Addr2.getType()); 334 llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); 335 PHI->addIncoming(Addr1.getPointer(), Block1); 336 PHI->addIncoming(Addr2.getPointer(), Block2); 337 CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); 338 return Address(PHI, Align); 339 } 340 341 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } 342 343 // If someone can figure out a general rule for this, that would be great. 344 // It's probably just doomed to be platform-dependent, though. 345 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { 346 // Verified for: 347 // x86-64 FreeBSD, Linux, Darwin 348 // x86-32 FreeBSD, Linux, Darwin 349 // PowerPC Linux, Darwin 350 // ARM Darwin (*not* EABI) 351 // AArch64 Linux 352 return 32; 353 } 354 355 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, 356 const FunctionNoProtoType *fnType) const { 357 // The following conventions are known to require this to be false: 358 // x86_stdcall 359 // MIPS 360 // For everything else, we just prefer false unless we opt out. 361 return false; 362 } 363 364 void 365 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, 366 llvm::SmallString<24> &Opt) const { 367 // This assumes the user is passing a library name like "rt" instead of a 368 // filename like "librt.a/so", and that they don't care whether it's static or 369 // dynamic. 370 Opt = "-l"; 371 Opt += Lib; 372 } 373 374 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); 375 376 /// isEmptyField - Return true iff a the field is "empty", that is it 377 /// is an unnamed bit-field or an (array of) empty record(s). 378 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, 379 bool AllowArrays) { 380 if (FD->isUnnamedBitfield()) 381 return true; 382 383 QualType FT = FD->getType(); 384 385 // Constant arrays of empty records count as empty, strip them off. 386 // Constant arrays of zero length always count as empty. 387 if (AllowArrays) 388 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 389 if (AT->getSize() == 0) 390 return true; 391 FT = AT->getElementType(); 392 } 393 394 const RecordType *RT = FT->getAs<RecordType>(); 395 if (!RT) 396 return false; 397 398 // C++ record fields are never empty, at least in the Itanium ABI. 399 // 400 // FIXME: We should use a predicate for whether this behavior is true in the 401 // current ABI. 402 if (isa<CXXRecordDecl>(RT->getDecl())) 403 return false; 404 405 return isEmptyRecord(Context, FT, AllowArrays); 406 } 407 408 /// isEmptyRecord - Return true iff a structure contains only empty 409 /// fields. Note that a structure with a flexible array member is not 410 /// considered empty. 411 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { 412 const RecordType *RT = T->getAs<RecordType>(); 413 if (!RT) 414 return false; 415 const RecordDecl *RD = RT->getDecl(); 416 if (RD->hasFlexibleArrayMember()) 417 return false; 418 419 // If this is a C++ record, check the bases first. 420 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 421 for (const auto &I : CXXRD->bases()) 422 if (!isEmptyRecord(Context, I.getType(), true)) 423 return false; 424 425 for (const auto *I : RD->fields()) 426 if (!isEmptyField(Context, I, AllowArrays)) 427 return false; 428 return true; 429 } 430 431 /// isSingleElementStruct - Determine if a structure is a "single 432 /// element struct", i.e. it has exactly one non-empty field or 433 /// exactly one field which is itself a single element 434 /// struct. Structures with flexible array members are never 435 /// considered single element structs. 436 /// 437 /// \return The field declaration for the single non-empty field, if 438 /// it exists. 439 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { 440 const RecordType *RT = T->getAs<RecordType>(); 441 if (!RT) 442 return nullptr; 443 444 const RecordDecl *RD = RT->getDecl(); 445 if (RD->hasFlexibleArrayMember()) 446 return nullptr; 447 448 const Type *Found = nullptr; 449 450 // If this is a C++ record, check the bases first. 451 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 452 for (const auto &I : CXXRD->bases()) { 453 // Ignore empty records. 454 if (isEmptyRecord(Context, I.getType(), true)) 455 continue; 456 457 // If we already found an element then this isn't a single-element struct. 458 if (Found) 459 return nullptr; 460 461 // If this is non-empty and not a single element struct, the composite 462 // cannot be a single element struct. 463 Found = isSingleElementStruct(I.getType(), Context); 464 if (!Found) 465 return nullptr; 466 } 467 } 468 469 // Check for single element. 470 for (const auto *FD : RD->fields()) { 471 QualType FT = FD->getType(); 472 473 // Ignore empty fields. 474 if (isEmptyField(Context, FD, true)) 475 continue; 476 477 // If we already found an element then this isn't a single-element 478 // struct. 479 if (Found) 480 return nullptr; 481 482 // Treat single element arrays as the element. 483 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 484 if (AT->getSize().getZExtValue() != 1) 485 break; 486 FT = AT->getElementType(); 487 } 488 489 if (!isAggregateTypeForABI(FT)) { 490 Found = FT.getTypePtr(); 491 } else { 492 Found = isSingleElementStruct(FT, Context); 493 if (!Found) 494 return nullptr; 495 } 496 } 497 498 // We don't consider a struct a single-element struct if it has 499 // padding beyond the element type. 500 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) 501 return nullptr; 502 503 return Found; 504 } 505 506 namespace { 507 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, 508 const ABIArgInfo &AI) { 509 // This default implementation defers to the llvm backend's va_arg 510 // instruction. It can handle only passing arguments directly 511 // (typically only handled in the backend for primitive types), or 512 // aggregates passed indirectly by pointer (NOTE: if the "byval" 513 // flag has ABI impact in the callee, this implementation cannot 514 // work.) 515 516 // Only a few cases are covered here at the moment -- those needed 517 // by the default abi. 518 llvm::Value *Val; 519 520 if (AI.isIndirect()) { 521 assert(!AI.getPaddingType() && 522 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); 523 assert( 524 !AI.getIndirectRealign() && 525 "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); 526 527 auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); 528 CharUnits TyAlignForABI = TyInfo.second; 529 530 llvm::Type *BaseTy = 531 llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 532 llvm::Value *Addr = 533 CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); 534 return Address(Addr, TyAlignForABI); 535 } else { 536 assert((AI.isDirect() || AI.isExtend()) && 537 "Unexpected ArgInfo Kind in generic VAArg emitter!"); 538 539 assert(!AI.getInReg() && 540 "Unexpected InReg seen in arginfo in generic VAArg emitter!"); 541 assert(!AI.getPaddingType() && 542 "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); 543 assert(!AI.getDirectOffset() && 544 "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); 545 assert(!AI.getCoerceToType() && 546 "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); 547 548 Address Temp = CGF.CreateMemTemp(Ty, "varet"); 549 Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); 550 CGF.Builder.CreateStore(Val, Temp); 551 return Temp; 552 } 553 } 554 555 /// DefaultABIInfo - The default implementation for ABI specific 556 /// details. This implementation provides information which results in 557 /// self-consistent and sensible LLVM IR generation, but does not 558 /// conform to any particular ABI. 559 class DefaultABIInfo : public ABIInfo { 560 public: 561 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 562 563 ABIArgInfo classifyReturnType(QualType RetTy) const; 564 ABIArgInfo classifyArgumentType(QualType RetTy) const; 565 566 void computeInfo(CGFunctionInfo &FI) const override { 567 if (!getCXXABI().classifyReturnType(FI)) 568 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 569 for (auto &I : FI.arguments()) 570 I.info = classifyArgumentType(I.type); 571 } 572 573 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 574 QualType Ty) const override { 575 return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); 576 } 577 }; 578 579 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { 580 public: 581 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 582 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 583 }; 584 585 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { 586 Ty = useFirstFieldIfTransparentUnion(Ty); 587 588 if (isAggregateTypeForABI(Ty)) { 589 // Records with non-trivial destructors/copy-constructors should not be 590 // passed by value. 591 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 592 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 593 594 return getNaturalAlignIndirect(Ty); 595 } 596 597 // Treat an enum type as its underlying type. 598 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 599 Ty = EnumTy->getDecl()->getIntegerType(); 600 601 return (Ty->isPromotableIntegerType() ? 602 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 603 } 604 605 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { 606 if (RetTy->isVoidType()) 607 return ABIArgInfo::getIgnore(); 608 609 if (isAggregateTypeForABI(RetTy)) 610 return getNaturalAlignIndirect(RetTy); 611 612 // Treat an enum type as its underlying type. 613 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 614 RetTy = EnumTy->getDecl()->getIntegerType(); 615 616 return (RetTy->isPromotableIntegerType() ? 617 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 618 } 619 620 //===----------------------------------------------------------------------===// 621 // WebAssembly ABI Implementation 622 // 623 // This is a very simple ABI that relies a lot on DefaultABIInfo. 624 //===----------------------------------------------------------------------===// 625 626 class WebAssemblyABIInfo final : public DefaultABIInfo { 627 public: 628 explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) 629 : DefaultABIInfo(CGT) {} 630 631 private: 632 ABIArgInfo classifyReturnType(QualType RetTy) const; 633 ABIArgInfo classifyArgumentType(QualType Ty) const; 634 635 // DefaultABIInfo's classifyReturnType and classifyArgumentType are 636 // non-virtual, but computeInfo and EmitVAArg are virtual, so we 637 // overload them. 638 void computeInfo(CGFunctionInfo &FI) const override { 639 if (!getCXXABI().classifyReturnType(FI)) 640 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 641 for (auto &Arg : FI.arguments()) 642 Arg.info = classifyArgumentType(Arg.type); 643 } 644 645 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 646 QualType Ty) const override; 647 }; 648 649 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { 650 public: 651 explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 652 : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} 653 }; 654 655 /// \brief Classify argument of given type \p Ty. 656 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { 657 Ty = useFirstFieldIfTransparentUnion(Ty); 658 659 if (isAggregateTypeForABI(Ty)) { 660 // Records with non-trivial destructors/copy-constructors should not be 661 // passed by value. 662 if (auto RAA = getRecordArgABI(Ty, getCXXABI())) 663 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 664 // Ignore empty structs/unions. 665 if (isEmptyRecord(getContext(), Ty, true)) 666 return ABIArgInfo::getIgnore(); 667 // Lower single-element structs to just pass a regular value. TODO: We 668 // could do reasonable-size multiple-element structs too, using getExpand(), 669 // though watch out for things like bitfields. 670 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) 671 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 672 } 673 674 // Otherwise just do the default thing. 675 return DefaultABIInfo::classifyArgumentType(Ty); 676 } 677 678 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { 679 if (isAggregateTypeForABI(RetTy)) { 680 // Records with non-trivial destructors/copy-constructors should not be 681 // returned by value. 682 if (!getRecordArgABI(RetTy, getCXXABI())) { 683 // Ignore empty structs/unions. 684 if (isEmptyRecord(getContext(), RetTy, true)) 685 return ABIArgInfo::getIgnore(); 686 // Lower single-element structs to just return a regular value. TODO: We 687 // could do reasonable-size multiple-element structs too, using 688 // ABIArgInfo::getDirect(). 689 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 690 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 691 } 692 } 693 694 // Otherwise just do the default thing. 695 return DefaultABIInfo::classifyReturnType(RetTy); 696 } 697 698 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 699 QualType Ty) const { 700 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect=*/ false, 701 getContext().getTypeInfoInChars(Ty), 702 CharUnits::fromQuantity(4), 703 /*AllowHigherAlign=*/ true); 704 } 705 706 //===----------------------------------------------------------------------===// 707 // le32/PNaCl bitcode ABI Implementation 708 // 709 // This is a simplified version of the x86_32 ABI. Arguments and return values 710 // are always passed on the stack. 711 //===----------------------------------------------------------------------===// 712 713 class PNaClABIInfo : public ABIInfo { 714 public: 715 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 716 717 ABIArgInfo classifyReturnType(QualType RetTy) const; 718 ABIArgInfo classifyArgumentType(QualType RetTy) const; 719 720 void computeInfo(CGFunctionInfo &FI) const override; 721 Address EmitVAArg(CodeGenFunction &CGF, 722 Address VAListAddr, QualType Ty) const override; 723 }; 724 725 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { 726 public: 727 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 728 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} 729 }; 730 731 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { 732 if (!getCXXABI().classifyReturnType(FI)) 733 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 734 735 for (auto &I : FI.arguments()) 736 I.info = classifyArgumentType(I.type); 737 } 738 739 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 740 QualType Ty) const { 741 // The PNaCL ABI is a bit odd, in that varargs don't use normal 742 // function classification. Structs get passed directly for varargs 743 // functions, through a rewriting transform in 744 // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows 745 // this target to actually support a va_arg instructions with an 746 // aggregate type, unlike other targets. 747 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); 748 } 749 750 /// \brief Classify argument of given type \p Ty. 751 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { 752 if (isAggregateTypeForABI(Ty)) { 753 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 754 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 755 return getNaturalAlignIndirect(Ty); 756 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 757 // Treat an enum type as its underlying type. 758 Ty = EnumTy->getDecl()->getIntegerType(); 759 } else if (Ty->isFloatingType()) { 760 // Floating-point types don't go inreg. 761 return ABIArgInfo::getDirect(); 762 } 763 764 return (Ty->isPromotableIntegerType() ? 765 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 766 } 767 768 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { 769 if (RetTy->isVoidType()) 770 return ABIArgInfo::getIgnore(); 771 772 // In the PNaCl ABI we always return records/structures on the stack. 773 if (isAggregateTypeForABI(RetTy)) 774 return getNaturalAlignIndirect(RetTy); 775 776 // Treat an enum type as its underlying type. 777 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 778 RetTy = EnumTy->getDecl()->getIntegerType(); 779 780 return (RetTy->isPromotableIntegerType() ? 781 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 782 } 783 784 /// IsX86_MMXType - Return true if this is an MMX type. 785 bool IsX86_MMXType(llvm::Type *IRType) { 786 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. 787 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && 788 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && 789 IRType->getScalarSizeInBits() != 64; 790 } 791 792 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 793 StringRef Constraint, 794 llvm::Type* Ty) { 795 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { 796 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { 797 // Invalid MMX constraint 798 return nullptr; 799 } 800 801 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); 802 } 803 804 // No operation needed 805 return Ty; 806 } 807 808 /// Returns true if this type can be passed in SSE registers with the 809 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. 810 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { 811 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 812 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) 813 return true; 814 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 815 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX 816 // registers specially. 817 unsigned VecSize = Context.getTypeSize(VT); 818 if (VecSize == 128 || VecSize == 256 || VecSize == 512) 819 return true; 820 } 821 return false; 822 } 823 824 /// Returns true if this aggregate is small enough to be passed in SSE registers 825 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. 826 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { 827 return NumMembers <= 4; 828 } 829 830 //===----------------------------------------------------------------------===// 831 // X86-32 ABI Implementation 832 //===----------------------------------------------------------------------===// 833 834 /// \brief Similar to llvm::CCState, but for Clang. 835 struct CCState { 836 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} 837 838 unsigned CC; 839 unsigned FreeRegs; 840 unsigned FreeSSERegs; 841 }; 842 843 /// X86_32ABIInfo - The X86-32 ABI information. 844 class X86_32ABIInfo : public SwiftABIInfo { 845 enum Class { 846 Integer, 847 Float 848 }; 849 850 static const unsigned MinABIStackAlignInBytes = 4; 851 852 bool IsDarwinVectorABI; 853 bool IsRetSmallStructInRegABI; 854 bool IsWin32StructABI; 855 bool IsSoftFloatABI; 856 bool IsMCUABI; 857 unsigned DefaultNumRegisterParameters; 858 859 static bool isRegisterSize(unsigned Size) { 860 return (Size == 8 || Size == 16 || Size == 32 || Size == 64); 861 } 862 863 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 864 // FIXME: Assumes vectorcall is in use. 865 return isX86VectorTypeForVectorCall(getContext(), Ty); 866 } 867 868 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 869 uint64_t NumMembers) const override { 870 // FIXME: Assumes vectorcall is in use. 871 return isX86VectorCallAggregateSmallEnough(NumMembers); 872 } 873 874 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; 875 876 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 877 /// such that the argument will be passed in memory. 878 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; 879 880 ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; 881 882 /// \brief Return the alignment to use for the given type on the stack. 883 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; 884 885 Class classify(QualType Ty) const; 886 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; 887 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; 888 /// \brief Updates the number of available free registers, returns 889 /// true if any registers were allocated. 890 bool updateFreeRegs(QualType Ty, CCState &State) const; 891 892 bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, 893 bool &NeedsPadding) const; 894 bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; 895 896 bool canExpandIndirectArgument(QualType Ty) const; 897 898 /// \brief Rewrite the function info so that all memory arguments use 899 /// inalloca. 900 void rewriteWithInAlloca(CGFunctionInfo &FI) const; 901 902 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 903 CharUnits &StackOffset, ABIArgInfo &Info, 904 QualType Type) const; 905 906 public: 907 908 void computeInfo(CGFunctionInfo &FI) const override; 909 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 910 QualType Ty) const override; 911 912 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, 913 bool RetSmallStructInRegABI, bool Win32StructABI, 914 unsigned NumRegisterParameters, bool SoftFloatABI) 915 : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), 916 IsRetSmallStructInRegABI(RetSmallStructInRegABI), 917 IsWin32StructABI(Win32StructABI), 918 IsSoftFloatABI(SoftFloatABI), 919 IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), 920 DefaultNumRegisterParameters(NumRegisterParameters) {} 921 922 bool shouldPassIndirectlyForSwift(CharUnits totalSize, 923 ArrayRef<llvm::Type*> scalars, 924 bool asReturnValue) const override { 925 // LLVM's x86-32 lowering currently only assigns up to three 926 // integer registers and three fp registers. Oddly, it'll use up to 927 // four vector registers for vectors, but those can overlap with the 928 // scalar registers. 929 return occupiesMoreThan(CGT, scalars, /*total*/ 3); 930 } 931 }; 932 933 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { 934 public: 935 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, 936 bool RetSmallStructInRegABI, bool Win32StructABI, 937 unsigned NumRegisterParameters, bool SoftFloatABI) 938 : TargetCodeGenInfo(new X86_32ABIInfo( 939 CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, 940 NumRegisterParameters, SoftFloatABI)) {} 941 942 static bool isStructReturnInRegABI( 943 const llvm::Triple &Triple, const CodeGenOptions &Opts); 944 945 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 946 CodeGen::CodeGenModule &CGM) const override; 947 948 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 949 // Darwin uses different dwarf register numbers for EH. 950 if (CGM.getTarget().getTriple().isOSDarwin()) return 5; 951 return 4; 952 } 953 954 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 955 llvm::Value *Address) const override; 956 957 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 958 StringRef Constraint, 959 llvm::Type* Ty) const override { 960 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 961 } 962 963 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, 964 std::string &Constraints, 965 std::vector<llvm::Type *> &ResultRegTypes, 966 std::vector<llvm::Type *> &ResultTruncRegTypes, 967 std::vector<LValue> &ResultRegDests, 968 std::string &AsmString, 969 unsigned NumOutputs) const override; 970 971 llvm::Constant * 972 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 973 unsigned Sig = (0xeb << 0) | // jmp rel8 974 (0x06 << 8) | // .+0x08 975 ('F' << 16) | 976 ('T' << 24); 977 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 978 } 979 980 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 981 return "movl\t%ebp, %ebp" 982 "\t\t## marker for objc_retainAutoreleaseReturnValue"; 983 } 984 }; 985 986 } 987 988 /// Rewrite input constraint references after adding some output constraints. 989 /// In the case where there is one output and one input and we add one output, 990 /// we need to replace all operand references greater than or equal to 1: 991 /// mov $0, $1 992 /// mov eax, $1 993 /// The result will be: 994 /// mov $0, $2 995 /// mov eax, $2 996 static void rewriteInputConstraintReferences(unsigned FirstIn, 997 unsigned NumNewOuts, 998 std::string &AsmString) { 999 std::string Buf; 1000 llvm::raw_string_ostream OS(Buf); 1001 size_t Pos = 0; 1002 while (Pos < AsmString.size()) { 1003 size_t DollarStart = AsmString.find('$', Pos); 1004 if (DollarStart == std::string::npos) 1005 DollarStart = AsmString.size(); 1006 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); 1007 if (DollarEnd == std::string::npos) 1008 DollarEnd = AsmString.size(); 1009 OS << StringRef(&AsmString[Pos], DollarEnd - Pos); 1010 Pos = DollarEnd; 1011 size_t NumDollars = DollarEnd - DollarStart; 1012 if (NumDollars % 2 != 0 && Pos < AsmString.size()) { 1013 // We have an operand reference. 1014 size_t DigitStart = Pos; 1015 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); 1016 if (DigitEnd == std::string::npos) 1017 DigitEnd = AsmString.size(); 1018 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); 1019 unsigned OperandIndex; 1020 if (!OperandStr.getAsInteger(10, OperandIndex)) { 1021 if (OperandIndex >= FirstIn) 1022 OperandIndex += NumNewOuts; 1023 OS << OperandIndex; 1024 } else { 1025 OS << OperandStr; 1026 } 1027 Pos = DigitEnd; 1028 } 1029 } 1030 AsmString = std::move(OS.str()); 1031 } 1032 1033 /// Add output constraints for EAX:EDX because they are return registers. 1034 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( 1035 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, 1036 std::vector<llvm::Type *> &ResultRegTypes, 1037 std::vector<llvm::Type *> &ResultTruncRegTypes, 1038 std::vector<LValue> &ResultRegDests, std::string &AsmString, 1039 unsigned NumOutputs) const { 1040 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); 1041 1042 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is 1043 // larger. 1044 if (!Constraints.empty()) 1045 Constraints += ','; 1046 if (RetWidth <= 32) { 1047 Constraints += "={eax}"; 1048 ResultRegTypes.push_back(CGF.Int32Ty); 1049 } else { 1050 // Use the 'A' constraint for EAX:EDX. 1051 Constraints += "=A"; 1052 ResultRegTypes.push_back(CGF.Int64Ty); 1053 } 1054 1055 // Truncate EAX or EAX:EDX to an integer of the appropriate size. 1056 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); 1057 ResultTruncRegTypes.push_back(CoerceTy); 1058 1059 // Coerce the integer by bitcasting the return slot pointer. 1060 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), 1061 CoerceTy->getPointerTo())); 1062 ResultRegDests.push_back(ReturnSlot); 1063 1064 rewriteInputConstraintReferences(NumOutputs, 1, AsmString); 1065 } 1066 1067 /// shouldReturnTypeInRegister - Determine if the given type should be 1068 /// returned in a register (for the Darwin and MCU ABI). 1069 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, 1070 ASTContext &Context) const { 1071 uint64_t Size = Context.getTypeSize(Ty); 1072 1073 // For i386, type must be register sized. 1074 // For the MCU ABI, it only needs to be <= 8-byte 1075 if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) 1076 return false; 1077 1078 if (Ty->isVectorType()) { 1079 // 64- and 128- bit vectors inside structures are not returned in 1080 // registers. 1081 if (Size == 64 || Size == 128) 1082 return false; 1083 1084 return true; 1085 } 1086 1087 // If this is a builtin, pointer, enum, complex type, member pointer, or 1088 // member function pointer it is ok. 1089 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || 1090 Ty->isAnyComplexType() || Ty->isEnumeralType() || 1091 Ty->isBlockPointerType() || Ty->isMemberPointerType()) 1092 return true; 1093 1094 // Arrays are treated like records. 1095 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) 1096 return shouldReturnTypeInRegister(AT->getElementType(), Context); 1097 1098 // Otherwise, it must be a record type. 1099 const RecordType *RT = Ty->getAs<RecordType>(); 1100 if (!RT) return false; 1101 1102 // FIXME: Traverse bases here too. 1103 1104 // Structure types are passed in register if all fields would be 1105 // passed in a register. 1106 for (const auto *FD : RT->getDecl()->fields()) { 1107 // Empty fields are ignored. 1108 if (isEmptyField(Context, FD, true)) 1109 continue; 1110 1111 // Check fields recursively. 1112 if (!shouldReturnTypeInRegister(FD->getType(), Context)) 1113 return false; 1114 } 1115 return true; 1116 } 1117 1118 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { 1119 // Treat complex types as the element type. 1120 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 1121 Ty = CTy->getElementType(); 1122 1123 // Check for a type which we know has a simple scalar argument-passing 1124 // convention without any padding. (We're specifically looking for 32 1125 // and 64-bit integer and integer-equivalents, float, and double.) 1126 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && 1127 !Ty->isEnumeralType() && !Ty->isBlockPointerType()) 1128 return false; 1129 1130 uint64_t Size = Context.getTypeSize(Ty); 1131 return Size == 32 || Size == 64; 1132 } 1133 1134 /// Test whether an argument type which is to be passed indirectly (on the 1135 /// stack) would have the equivalent layout if it was expanded into separate 1136 /// arguments. If so, we prefer to do the latter to avoid inhibiting 1137 /// optimizations. 1138 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { 1139 // We can only expand structure types. 1140 const RecordType *RT = Ty->getAs<RecordType>(); 1141 if (!RT) 1142 return false; 1143 const RecordDecl *RD = RT->getDecl(); 1144 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 1145 if (!IsWin32StructABI ) { 1146 // On non-Windows, we have to conservatively match our old bitcode 1147 // prototypes in order to be ABI-compatible at the bitcode level. 1148 if (!CXXRD->isCLike()) 1149 return false; 1150 } else { 1151 // Don't do this for dynamic classes. 1152 if (CXXRD->isDynamicClass()) 1153 return false; 1154 // Don't do this if there are any non-empty bases. 1155 for (const CXXBaseSpecifier &Base : CXXRD->bases()) { 1156 if (!isEmptyRecord(getContext(), Base.getType(), /*AllowArrays=*/true)) 1157 return false; 1158 } 1159 } 1160 } 1161 1162 uint64_t Size = 0; 1163 1164 for (const auto *FD : RD->fields()) { 1165 // Scalar arguments on the stack get 4 byte alignment on x86. If the 1166 // argument is smaller than 32-bits, expanding the struct will create 1167 // alignment padding. 1168 if (!is32Or64BitBasicType(FD->getType(), getContext())) 1169 return false; 1170 1171 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know 1172 // how to expand them yet, and the predicate for telling if a bitfield still 1173 // counts as "basic" is more complicated than what we were doing previously. 1174 if (FD->isBitField()) 1175 return false; 1176 1177 Size += getContext().getTypeSize(FD->getType()); 1178 } 1179 1180 // We can do this if there was no alignment padding. 1181 return Size == getContext().getTypeSize(Ty); 1182 } 1183 1184 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { 1185 // If the return value is indirect, then the hidden argument is consuming one 1186 // integer register. 1187 if (State.FreeRegs) { 1188 --State.FreeRegs; 1189 if (!IsMCUABI) 1190 return getNaturalAlignIndirectInReg(RetTy); 1191 } 1192 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); 1193 } 1194 1195 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, 1196 CCState &State) const { 1197 if (RetTy->isVoidType()) 1198 return ABIArgInfo::getIgnore(); 1199 1200 const Type *Base = nullptr; 1201 uint64_t NumElts = 0; 1202 if (State.CC == llvm::CallingConv::X86_VectorCall && 1203 isHomogeneousAggregate(RetTy, Base, NumElts)) { 1204 // The LLVM struct type for such an aggregate should lower properly. 1205 return ABIArgInfo::getDirect(); 1206 } 1207 1208 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 1209 // On Darwin, some vectors are returned in registers. 1210 if (IsDarwinVectorABI) { 1211 uint64_t Size = getContext().getTypeSize(RetTy); 1212 1213 // 128-bit vectors are a special case; they are returned in 1214 // registers and we need to make sure to pick a type the LLVM 1215 // backend will like. 1216 if (Size == 128) 1217 return ABIArgInfo::getDirect(llvm::VectorType::get( 1218 llvm::Type::getInt64Ty(getVMContext()), 2)); 1219 1220 // Always return in register if it fits in a general purpose 1221 // register, or if it is 64 bits and has a single element. 1222 if ((Size == 8 || Size == 16 || Size == 32) || 1223 (Size == 64 && VT->getNumElements() == 1)) 1224 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 1225 Size)); 1226 1227 return getIndirectReturnResult(RetTy, State); 1228 } 1229 1230 return ABIArgInfo::getDirect(); 1231 } 1232 1233 if (isAggregateTypeForABI(RetTy)) { 1234 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 1235 // Structures with flexible arrays are always indirect. 1236 if (RT->getDecl()->hasFlexibleArrayMember()) 1237 return getIndirectReturnResult(RetTy, State); 1238 } 1239 1240 // If specified, structs and unions are always indirect. 1241 if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) 1242 return getIndirectReturnResult(RetTy, State); 1243 1244 // Ignore empty structs/unions. 1245 if (isEmptyRecord(getContext(), RetTy, true)) 1246 return ABIArgInfo::getIgnore(); 1247 1248 // Small structures which are register sized are generally returned 1249 // in a register. 1250 if (shouldReturnTypeInRegister(RetTy, getContext())) { 1251 uint64_t Size = getContext().getTypeSize(RetTy); 1252 1253 // As a special-case, if the struct is a "single-element" struct, and 1254 // the field is of type "float" or "double", return it in a 1255 // floating-point register. (MSVC does not apply this special case.) 1256 // We apply a similar transformation for pointer types to improve the 1257 // quality of the generated IR. 1258 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 1259 if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) 1260 || SeltTy->hasPointerRepresentation()) 1261 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 1262 1263 // FIXME: We should be able to narrow this integer in cases with dead 1264 // padding. 1265 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); 1266 } 1267 1268 return getIndirectReturnResult(RetTy, State); 1269 } 1270 1271 // Treat an enum type as its underlying type. 1272 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 1273 RetTy = EnumTy->getDecl()->getIntegerType(); 1274 1275 return (RetTy->isPromotableIntegerType() ? 1276 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 1277 } 1278 1279 static bool isSSEVectorType(ASTContext &Context, QualType Ty) { 1280 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; 1281 } 1282 1283 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { 1284 const RecordType *RT = Ty->getAs<RecordType>(); 1285 if (!RT) 1286 return 0; 1287 const RecordDecl *RD = RT->getDecl(); 1288 1289 // If this is a C++ record, check the bases first. 1290 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 1291 for (const auto &I : CXXRD->bases()) 1292 if (!isRecordWithSSEVectorType(Context, I.getType())) 1293 return false; 1294 1295 for (const auto *i : RD->fields()) { 1296 QualType FT = i->getType(); 1297 1298 if (isSSEVectorType(Context, FT)) 1299 return true; 1300 1301 if (isRecordWithSSEVectorType(Context, FT)) 1302 return true; 1303 } 1304 1305 return false; 1306 } 1307 1308 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, 1309 unsigned Align) const { 1310 // Otherwise, if the alignment is less than or equal to the minimum ABI 1311 // alignment, just use the default; the backend will handle this. 1312 if (Align <= MinABIStackAlignInBytes) 1313 return 0; // Use default alignment. 1314 1315 // On non-Darwin, the stack type alignment is always 4. 1316 if (!IsDarwinVectorABI) { 1317 // Set explicit alignment, since we may need to realign the top. 1318 return MinABIStackAlignInBytes; 1319 } 1320 1321 // Otherwise, if the type contains an SSE vector type, the alignment is 16. 1322 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || 1323 isRecordWithSSEVectorType(getContext(), Ty))) 1324 return 16; 1325 1326 return MinABIStackAlignInBytes; 1327 } 1328 1329 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, 1330 CCState &State) const { 1331 if (!ByVal) { 1332 if (State.FreeRegs) { 1333 --State.FreeRegs; // Non-byval indirects just use one pointer. 1334 if (!IsMCUABI) 1335 return getNaturalAlignIndirectInReg(Ty); 1336 } 1337 return getNaturalAlignIndirect(Ty, false); 1338 } 1339 1340 // Compute the byval alignment. 1341 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 1342 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); 1343 if (StackAlign == 0) 1344 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); 1345 1346 // If the stack alignment is less than the type alignment, realign the 1347 // argument. 1348 bool Realign = TypeAlign > StackAlign; 1349 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), 1350 /*ByVal=*/true, Realign); 1351 } 1352 1353 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { 1354 const Type *T = isSingleElementStruct(Ty, getContext()); 1355 if (!T) 1356 T = Ty.getTypePtr(); 1357 1358 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 1359 BuiltinType::Kind K = BT->getKind(); 1360 if (K == BuiltinType::Float || K == BuiltinType::Double) 1361 return Float; 1362 } 1363 return Integer; 1364 } 1365 1366 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { 1367 if (!IsSoftFloatABI) { 1368 Class C = classify(Ty); 1369 if (C == Float) 1370 return false; 1371 } 1372 1373 unsigned Size = getContext().getTypeSize(Ty); 1374 unsigned SizeInRegs = (Size + 31) / 32; 1375 1376 if (SizeInRegs == 0) 1377 return false; 1378 1379 if (!IsMCUABI) { 1380 if (SizeInRegs > State.FreeRegs) { 1381 State.FreeRegs = 0; 1382 return false; 1383 } 1384 } else { 1385 // The MCU psABI allows passing parameters in-reg even if there are 1386 // earlier parameters that are passed on the stack. Also, 1387 // it does not allow passing >8-byte structs in-register, 1388 // even if there are 3 free registers available. 1389 if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) 1390 return false; 1391 } 1392 1393 State.FreeRegs -= SizeInRegs; 1394 return true; 1395 } 1396 1397 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, 1398 bool &InReg, 1399 bool &NeedsPadding) const { 1400 // On Windows, aggregates other than HFAs are never passed in registers, and 1401 // they do not consume register slots. Homogenous floating-point aggregates 1402 // (HFAs) have already been dealt with at this point. 1403 if (IsWin32StructABI && isAggregateTypeForABI(Ty)) 1404 return false; 1405 1406 NeedsPadding = false; 1407 InReg = !IsMCUABI; 1408 1409 if (!updateFreeRegs(Ty, State)) 1410 return false; 1411 1412 if (IsMCUABI) 1413 return true; 1414 1415 if (State.CC == llvm::CallingConv::X86_FastCall || 1416 State.CC == llvm::CallingConv::X86_VectorCall) { 1417 if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) 1418 NeedsPadding = true; 1419 1420 return false; 1421 } 1422 1423 return true; 1424 } 1425 1426 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { 1427 if (!updateFreeRegs(Ty, State)) 1428 return false; 1429 1430 if (IsMCUABI) 1431 return false; 1432 1433 if (State.CC == llvm::CallingConv::X86_FastCall || 1434 State.CC == llvm::CallingConv::X86_VectorCall) { 1435 if (getContext().getTypeSize(Ty) > 32) 1436 return false; 1437 1438 return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || 1439 Ty->isReferenceType()); 1440 } 1441 1442 return true; 1443 } 1444 1445 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, 1446 CCState &State) const { 1447 // FIXME: Set alignment on indirect arguments. 1448 1449 Ty = useFirstFieldIfTransparentUnion(Ty); 1450 1451 // Check with the C++ ABI first. 1452 const RecordType *RT = Ty->getAs<RecordType>(); 1453 if (RT) { 1454 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 1455 if (RAA == CGCXXABI::RAA_Indirect) { 1456 return getIndirectResult(Ty, false, State); 1457 } else if (RAA == CGCXXABI::RAA_DirectInMemory) { 1458 // The field index doesn't matter, we'll fix it up later. 1459 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); 1460 } 1461 } 1462 1463 // vectorcall adds the concept of a homogenous vector aggregate, similar 1464 // to other targets. 1465 const Type *Base = nullptr; 1466 uint64_t NumElts = 0; 1467 if (State.CC == llvm::CallingConv::X86_VectorCall && 1468 isHomogeneousAggregate(Ty, Base, NumElts)) { 1469 if (State.FreeSSERegs >= NumElts) { 1470 State.FreeSSERegs -= NumElts; 1471 if (Ty->isBuiltinType() || Ty->isVectorType()) 1472 return ABIArgInfo::getDirect(); 1473 return ABIArgInfo::getExpand(); 1474 } 1475 return getIndirectResult(Ty, /*ByVal=*/false, State); 1476 } 1477 1478 if (isAggregateTypeForABI(Ty)) { 1479 // Structures with flexible arrays are always indirect. 1480 // FIXME: This should not be byval! 1481 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 1482 return getIndirectResult(Ty, true, State); 1483 1484 // Ignore empty structs/unions on non-Windows. 1485 if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) 1486 return ABIArgInfo::getIgnore(); 1487 1488 llvm::LLVMContext &LLVMContext = getVMContext(); 1489 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 1490 bool NeedsPadding = false; 1491 bool InReg; 1492 if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { 1493 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; 1494 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); 1495 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 1496 if (InReg) 1497 return ABIArgInfo::getDirectInReg(Result); 1498 else 1499 return ABIArgInfo::getDirect(Result); 1500 } 1501 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; 1502 1503 // Expand small (<= 128-bit) record types when we know that the stack layout 1504 // of those arguments will match the struct. This is important because the 1505 // LLVM backend isn't smart enough to remove byval, which inhibits many 1506 // optimizations. 1507 // Don't do this for the MCU if there are still free integer registers 1508 // (see X86_64 ABI for full explanation). 1509 if (getContext().getTypeSize(Ty) <= 4 * 32 && 1510 (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) 1511 return ABIArgInfo::getExpandWithPadding( 1512 State.CC == llvm::CallingConv::X86_FastCall || 1513 State.CC == llvm::CallingConv::X86_VectorCall, 1514 PaddingType); 1515 1516 return getIndirectResult(Ty, true, State); 1517 } 1518 1519 if (const VectorType *VT = Ty->getAs<VectorType>()) { 1520 // On Darwin, some vectors are passed in memory, we handle this by passing 1521 // it as an i8/i16/i32/i64. 1522 if (IsDarwinVectorABI) { 1523 uint64_t Size = getContext().getTypeSize(Ty); 1524 if ((Size == 8 || Size == 16 || Size == 32) || 1525 (Size == 64 && VT->getNumElements() == 1)) 1526 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 1527 Size)); 1528 } 1529 1530 if (IsX86_MMXType(CGT.ConvertType(Ty))) 1531 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); 1532 1533 return ABIArgInfo::getDirect(); 1534 } 1535 1536 1537 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 1538 Ty = EnumTy->getDecl()->getIntegerType(); 1539 1540 bool InReg = shouldPrimitiveUseInReg(Ty, State); 1541 1542 if (Ty->isPromotableIntegerType()) { 1543 if (InReg) 1544 return ABIArgInfo::getExtendInReg(); 1545 return ABIArgInfo::getExtend(); 1546 } 1547 1548 if (InReg) 1549 return ABIArgInfo::getDirectInReg(); 1550 return ABIArgInfo::getDirect(); 1551 } 1552 1553 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { 1554 CCState State(FI.getCallingConvention()); 1555 if (IsMCUABI) 1556 State.FreeRegs = 3; 1557 else if (State.CC == llvm::CallingConv::X86_FastCall) 1558 State.FreeRegs = 2; 1559 else if (State.CC == llvm::CallingConv::X86_VectorCall) { 1560 State.FreeRegs = 2; 1561 State.FreeSSERegs = 6; 1562 } else if (FI.getHasRegParm()) 1563 State.FreeRegs = FI.getRegParm(); 1564 else 1565 State.FreeRegs = DefaultNumRegisterParameters; 1566 1567 if (!getCXXABI().classifyReturnType(FI)) { 1568 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); 1569 } else if (FI.getReturnInfo().isIndirect()) { 1570 // The C++ ABI is not aware of register usage, so we have to check if the 1571 // return value was sret and put it in a register ourselves if appropriate. 1572 if (State.FreeRegs) { 1573 --State.FreeRegs; // The sret parameter consumes a register. 1574 if (!IsMCUABI) 1575 FI.getReturnInfo().setInReg(true); 1576 } 1577 } 1578 1579 // The chain argument effectively gives us another free register. 1580 if (FI.isChainCall()) 1581 ++State.FreeRegs; 1582 1583 bool UsedInAlloca = false; 1584 for (auto &I : FI.arguments()) { 1585 I.info = classifyArgumentType(I.type, State); 1586 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); 1587 } 1588 1589 // If we needed to use inalloca for any argument, do a second pass and rewrite 1590 // all the memory arguments to use inalloca. 1591 if (UsedInAlloca) 1592 rewriteWithInAlloca(FI); 1593 } 1594 1595 void 1596 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 1597 CharUnits &StackOffset, ABIArgInfo &Info, 1598 QualType Type) const { 1599 // Arguments are always 4-byte-aligned. 1600 CharUnits FieldAlign = CharUnits::fromQuantity(4); 1601 1602 assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); 1603 Info = ABIArgInfo::getInAlloca(FrameFields.size()); 1604 FrameFields.push_back(CGT.ConvertTypeForMem(Type)); 1605 StackOffset += getContext().getTypeSizeInChars(Type); 1606 1607 // Insert padding bytes to respect alignment. 1608 CharUnits FieldEnd = StackOffset; 1609 StackOffset = FieldEnd.alignTo(FieldAlign); 1610 if (StackOffset != FieldEnd) { 1611 CharUnits NumBytes = StackOffset - FieldEnd; 1612 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); 1613 Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); 1614 FrameFields.push_back(Ty); 1615 } 1616 } 1617 1618 static bool isArgInAlloca(const ABIArgInfo &Info) { 1619 // Leave ignored and inreg arguments alone. 1620 switch (Info.getKind()) { 1621 case ABIArgInfo::InAlloca: 1622 return true; 1623 case ABIArgInfo::Indirect: 1624 assert(Info.getIndirectByVal()); 1625 return true; 1626 case ABIArgInfo::Ignore: 1627 return false; 1628 case ABIArgInfo::Direct: 1629 case ABIArgInfo::Extend: 1630 if (Info.getInReg()) 1631 return false; 1632 return true; 1633 case ABIArgInfo::Expand: 1634 case ABIArgInfo::CoerceAndExpand: 1635 // These are aggregate types which are never passed in registers when 1636 // inalloca is involved. 1637 return true; 1638 } 1639 llvm_unreachable("invalid enum"); 1640 } 1641 1642 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { 1643 assert(IsWin32StructABI && "inalloca only supported on win32"); 1644 1645 // Build a packed struct type for all of the arguments in memory. 1646 SmallVector<llvm::Type *, 6> FrameFields; 1647 1648 // The stack alignment is always 4. 1649 CharUnits StackAlign = CharUnits::fromQuantity(4); 1650 1651 CharUnits StackOffset; 1652 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); 1653 1654 // Put 'this' into the struct before 'sret', if necessary. 1655 bool IsThisCall = 1656 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; 1657 ABIArgInfo &Ret = FI.getReturnInfo(); 1658 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && 1659 isArgInAlloca(I->info)) { 1660 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 1661 ++I; 1662 } 1663 1664 // Put the sret parameter into the inalloca struct if it's in memory. 1665 if (Ret.isIndirect() && !Ret.getInReg()) { 1666 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); 1667 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); 1668 // On Windows, the hidden sret parameter is always returned in eax. 1669 Ret.setInAllocaSRet(IsWin32StructABI); 1670 } 1671 1672 // Skip the 'this' parameter in ecx. 1673 if (IsThisCall) 1674 ++I; 1675 1676 // Put arguments passed in memory into the struct. 1677 for (; I != E; ++I) { 1678 if (isArgInAlloca(I->info)) 1679 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 1680 } 1681 1682 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, 1683 /*isPacked=*/true), 1684 StackAlign); 1685 } 1686 1687 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, 1688 Address VAListAddr, QualType Ty) const { 1689 1690 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 1691 1692 // x86-32 changes the alignment of certain arguments on the stack. 1693 // 1694 // Just messing with TypeInfo like this works because we never pass 1695 // anything indirectly. 1696 TypeInfo.second = CharUnits::fromQuantity( 1697 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); 1698 1699 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, 1700 TypeInfo, CharUnits::fromQuantity(4), 1701 /*AllowHigherAlign*/ true); 1702 } 1703 1704 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( 1705 const llvm::Triple &Triple, const CodeGenOptions &Opts) { 1706 assert(Triple.getArch() == llvm::Triple::x86); 1707 1708 switch (Opts.getStructReturnConvention()) { 1709 case CodeGenOptions::SRCK_Default: 1710 break; 1711 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return 1712 return false; 1713 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return 1714 return true; 1715 } 1716 1717 if (Triple.isOSDarwin() || Triple.isOSIAMCU()) 1718 return true; 1719 1720 switch (Triple.getOS()) { 1721 case llvm::Triple::DragonFly: 1722 case llvm::Triple::FreeBSD: 1723 case llvm::Triple::OpenBSD: 1724 case llvm::Triple::Bitrig: 1725 case llvm::Triple::Win32: 1726 return true; 1727 default: 1728 return false; 1729 } 1730 } 1731 1732 void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, 1733 llvm::GlobalValue *GV, 1734 CodeGen::CodeGenModule &CGM) const { 1735 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 1736 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 1737 // Get the LLVM function. 1738 llvm::Function *Fn = cast<llvm::Function>(GV); 1739 1740 // Now add the 'alignstack' attribute with a value of 16. 1741 llvm::AttrBuilder B; 1742 B.addStackAlignmentAttr(16); 1743 Fn->addAttributes(llvm::AttributeSet::FunctionIndex, 1744 llvm::AttributeSet::get(CGM.getLLVMContext(), 1745 llvm::AttributeSet::FunctionIndex, 1746 B)); 1747 } 1748 if (FD->hasAttr<AnyX86InterruptAttr>()) { 1749 llvm::Function *Fn = cast<llvm::Function>(GV); 1750 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 1751 } 1752 } 1753 } 1754 1755 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( 1756 CodeGen::CodeGenFunction &CGF, 1757 llvm::Value *Address) const { 1758 CodeGen::CGBuilderTy &Builder = CGF.Builder; 1759 1760 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 1761 1762 // 0-7 are the eight integer registers; the order is different 1763 // on Darwin (for EH), but the range is the same. 1764 // 8 is %eip. 1765 AssignToArrayRange(Builder, Address, Four8, 0, 8); 1766 1767 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { 1768 // 12-16 are st(0..4). Not sure why we stop at 4. 1769 // These have size 16, which is sizeof(long double) on 1770 // platforms with 8-byte alignment for that type. 1771 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); 1772 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); 1773 1774 } else { 1775 // 9 is %eflags, which doesn't get a size on Darwin for some 1776 // reason. 1777 Builder.CreateAlignedStore( 1778 Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), 1779 CharUnits::One()); 1780 1781 // 11-16 are st(0..5). Not sure why we stop at 5. 1782 // These have size 12, which is sizeof(long double) on 1783 // platforms with 4-byte alignment for that type. 1784 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); 1785 AssignToArrayRange(Builder, Address, Twelve8, 11, 16); 1786 } 1787 1788 return false; 1789 } 1790 1791 //===----------------------------------------------------------------------===// 1792 // X86-64 ABI Implementation 1793 //===----------------------------------------------------------------------===// 1794 1795 1796 namespace { 1797 /// The AVX ABI level for X86 targets. 1798 enum class X86AVXABILevel { 1799 None, 1800 AVX, 1801 AVX512 1802 }; 1803 1804 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. 1805 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { 1806 switch (AVXLevel) { 1807 case X86AVXABILevel::AVX512: 1808 return 512; 1809 case X86AVXABILevel::AVX: 1810 return 256; 1811 case X86AVXABILevel::None: 1812 return 128; 1813 } 1814 llvm_unreachable("Unknown AVXLevel"); 1815 } 1816 1817 /// X86_64ABIInfo - The X86_64 ABI information. 1818 class X86_64ABIInfo : public SwiftABIInfo { 1819 enum Class { 1820 Integer = 0, 1821 SSE, 1822 SSEUp, 1823 X87, 1824 X87Up, 1825 ComplexX87, 1826 NoClass, 1827 Memory 1828 }; 1829 1830 /// merge - Implement the X86_64 ABI merging algorithm. 1831 /// 1832 /// Merge an accumulating classification \arg Accum with a field 1833 /// classification \arg Field. 1834 /// 1835 /// \param Accum - The accumulating classification. This should 1836 /// always be either NoClass or the result of a previous merge 1837 /// call. In addition, this should never be Memory (the caller 1838 /// should just return Memory for the aggregate). 1839 static Class merge(Class Accum, Class Field); 1840 1841 /// postMerge - Implement the X86_64 ABI post merging algorithm. 1842 /// 1843 /// Post merger cleanup, reduces a malformed Hi and Lo pair to 1844 /// final MEMORY or SSE classes when necessary. 1845 /// 1846 /// \param AggregateSize - The size of the current aggregate in 1847 /// the classification process. 1848 /// 1849 /// \param Lo - The classification for the parts of the type 1850 /// residing in the low word of the containing object. 1851 /// 1852 /// \param Hi - The classification for the parts of the type 1853 /// residing in the higher words of the containing object. 1854 /// 1855 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; 1856 1857 /// classify - Determine the x86_64 register classes in which the 1858 /// given type T should be passed. 1859 /// 1860 /// \param Lo - The classification for the parts of the type 1861 /// residing in the low word of the containing object. 1862 /// 1863 /// \param Hi - The classification for the parts of the type 1864 /// residing in the high word of the containing object. 1865 /// 1866 /// \param OffsetBase - The bit offset of this type in the 1867 /// containing object. Some parameters are classified different 1868 /// depending on whether they straddle an eightbyte boundary. 1869 /// 1870 /// \param isNamedArg - Whether the argument in question is a "named" 1871 /// argument, as used in AMD64-ABI 3.5.7. 1872 /// 1873 /// If a word is unused its result will be NoClass; if a type should 1874 /// be passed in Memory then at least the classification of \arg Lo 1875 /// will be Memory. 1876 /// 1877 /// The \arg Lo class will be NoClass iff the argument is ignored. 1878 /// 1879 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will 1880 /// also be ComplexX87. 1881 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, 1882 bool isNamedArg) const; 1883 1884 llvm::Type *GetByteVectorType(QualType Ty) const; 1885 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, 1886 unsigned IROffset, QualType SourceTy, 1887 unsigned SourceOffset) const; 1888 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, 1889 unsigned IROffset, QualType SourceTy, 1890 unsigned SourceOffset) const; 1891 1892 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1893 /// such that the argument will be returned in memory. 1894 ABIArgInfo getIndirectReturnResult(QualType Ty) const; 1895 1896 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1897 /// such that the argument will be passed in memory. 1898 /// 1899 /// \param freeIntRegs - The number of free integer registers remaining 1900 /// available. 1901 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; 1902 1903 ABIArgInfo classifyReturnType(QualType RetTy) const; 1904 1905 ABIArgInfo classifyArgumentType(QualType Ty, 1906 unsigned freeIntRegs, 1907 unsigned &neededInt, 1908 unsigned &neededSSE, 1909 bool isNamedArg) const; 1910 1911 bool IsIllegalVectorType(QualType Ty) const; 1912 1913 /// The 0.98 ABI revision clarified a lot of ambiguities, 1914 /// unfortunately in ways that were not always consistent with 1915 /// certain previous compilers. In particular, platforms which 1916 /// required strict binary compatibility with older versions of GCC 1917 /// may need to exempt themselves. 1918 bool honorsRevision0_98() const { 1919 return !getTarget().getTriple().isOSDarwin(); 1920 } 1921 1922 /// GCC classifies <1 x long long> as SSE but compatibility with older clang 1923 // compilers require us to classify it as INTEGER. 1924 bool classifyIntegerMMXAsSSE() const { 1925 const llvm::Triple &Triple = getTarget().getTriple(); 1926 if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) 1927 return false; 1928 if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) 1929 return false; 1930 return true; 1931 } 1932 1933 X86AVXABILevel AVXLevel; 1934 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on 1935 // 64-bit hardware. 1936 bool Has64BitPointers; 1937 1938 public: 1939 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : 1940 SwiftABIInfo(CGT), AVXLevel(AVXLevel), 1941 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { 1942 } 1943 1944 bool isPassedUsingAVXType(QualType type) const { 1945 unsigned neededInt, neededSSE; 1946 // The freeIntRegs argument doesn't matter here. 1947 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, 1948 /*isNamedArg*/true); 1949 if (info.isDirect()) { 1950 llvm::Type *ty = info.getCoerceToType(); 1951 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) 1952 return (vectorTy->getBitWidth() > 128); 1953 } 1954 return false; 1955 } 1956 1957 void computeInfo(CGFunctionInfo &FI) const override; 1958 1959 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 1960 QualType Ty) const override; 1961 Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 1962 QualType Ty) const override; 1963 1964 bool has64BitPointers() const { 1965 return Has64BitPointers; 1966 } 1967 1968 bool shouldPassIndirectlyForSwift(CharUnits totalSize, 1969 ArrayRef<llvm::Type*> scalars, 1970 bool asReturnValue) const override { 1971 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 1972 } 1973 }; 1974 1975 /// WinX86_64ABIInfo - The Windows X86_64 ABI information. 1976 class WinX86_64ABIInfo : public ABIInfo { 1977 public: 1978 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) 1979 : ABIInfo(CGT), 1980 IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} 1981 1982 void computeInfo(CGFunctionInfo &FI) const override; 1983 1984 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 1985 QualType Ty) const override; 1986 1987 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 1988 // FIXME: Assumes vectorcall is in use. 1989 return isX86VectorTypeForVectorCall(getContext(), Ty); 1990 } 1991 1992 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 1993 uint64_t NumMembers) const override { 1994 // FIXME: Assumes vectorcall is in use. 1995 return isX86VectorCallAggregateSmallEnough(NumMembers); 1996 } 1997 1998 private: 1999 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, 2000 bool IsReturnType) const; 2001 2002 bool IsMingw64; 2003 }; 2004 2005 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { 2006 public: 2007 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) 2008 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} 2009 2010 const X86_64ABIInfo &getABIInfo() const { 2011 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); 2012 } 2013 2014 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 2015 return 7; 2016 } 2017 2018 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2019 llvm::Value *Address) const override { 2020 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 2021 2022 // 0-15 are the 16 integer registers. 2023 // 16 is %rip. 2024 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 2025 return false; 2026 } 2027 2028 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 2029 StringRef Constraint, 2030 llvm::Type* Ty) const override { 2031 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 2032 } 2033 2034 bool isNoProtoCallVariadic(const CallArgList &args, 2035 const FunctionNoProtoType *fnType) const override { 2036 // The default CC on x86-64 sets %al to the number of SSA 2037 // registers used, and GCC sets this when calling an unprototyped 2038 // function, so we override the default behavior. However, don't do 2039 // that when AVX types are involved: the ABI explicitly states it is 2040 // undefined, and it doesn't work in practice because of how the ABI 2041 // defines varargs anyway. 2042 if (fnType->getCallConv() == CC_C) { 2043 bool HasAVXType = false; 2044 for (CallArgList::const_iterator 2045 it = args.begin(), ie = args.end(); it != ie; ++it) { 2046 if (getABIInfo().isPassedUsingAVXType(it->Ty)) { 2047 HasAVXType = true; 2048 break; 2049 } 2050 } 2051 2052 if (!HasAVXType) 2053 return true; 2054 } 2055 2056 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); 2057 } 2058 2059 llvm::Constant * 2060 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 2061 unsigned Sig; 2062 if (getABIInfo().has64BitPointers()) 2063 Sig = (0xeb << 0) | // jmp rel8 2064 (0x0a << 8) | // .+0x0c 2065 ('F' << 16) | 2066 ('T' << 24); 2067 else 2068 Sig = (0xeb << 0) | // jmp rel8 2069 (0x06 << 8) | // .+0x08 2070 ('F' << 16) | 2071 ('T' << 24); 2072 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 2073 } 2074 2075 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2076 CodeGen::CodeGenModule &CGM) const override { 2077 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2078 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2079 llvm::Function *Fn = cast<llvm::Function>(GV); 2080 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2081 } 2082 } 2083 } 2084 }; 2085 2086 class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { 2087 public: 2088 PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) 2089 : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} 2090 2091 void getDependentLibraryOption(llvm::StringRef Lib, 2092 llvm::SmallString<24> &Opt) const override { 2093 Opt = "\01"; 2094 // If the argument contains a space, enclose it in quotes. 2095 if (Lib.find(" ") != StringRef::npos) 2096 Opt += "\"" + Lib.str() + "\""; 2097 else 2098 Opt += Lib; 2099 } 2100 }; 2101 2102 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { 2103 // If the argument does not end in .lib, automatically add the suffix. 2104 // If the argument contains a space, enclose it in quotes. 2105 // This matches the behavior of MSVC. 2106 bool Quote = (Lib.find(" ") != StringRef::npos); 2107 std::string ArgStr = Quote ? "\"" : ""; 2108 ArgStr += Lib; 2109 if (!Lib.endswith_lower(".lib")) 2110 ArgStr += ".lib"; 2111 ArgStr += Quote ? "\"" : ""; 2112 return ArgStr; 2113 } 2114 2115 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { 2116 public: 2117 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 2118 bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, 2119 unsigned NumRegisterParameters) 2120 : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, 2121 Win32StructABI, NumRegisterParameters, false) {} 2122 2123 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2124 CodeGen::CodeGenModule &CGM) const override; 2125 2126 void getDependentLibraryOption(llvm::StringRef Lib, 2127 llvm::SmallString<24> &Opt) const override { 2128 Opt = "/DEFAULTLIB:"; 2129 Opt += qualifyWindowsLibrary(Lib); 2130 } 2131 2132 void getDetectMismatchOption(llvm::StringRef Name, 2133 llvm::StringRef Value, 2134 llvm::SmallString<32> &Opt) const override { 2135 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 2136 } 2137 }; 2138 2139 static void addStackProbeSizeTargetAttribute(const Decl *D, 2140 llvm::GlobalValue *GV, 2141 CodeGen::CodeGenModule &CGM) { 2142 if (D && isa<FunctionDecl>(D)) { 2143 if (CGM.getCodeGenOpts().StackProbeSize != 4096) { 2144 llvm::Function *Fn = cast<llvm::Function>(GV); 2145 2146 Fn->addFnAttr("stack-probe-size", 2147 llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); 2148 } 2149 } 2150 } 2151 2152 void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, 2153 llvm::GlobalValue *GV, 2154 CodeGen::CodeGenModule &CGM) const { 2155 X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 2156 2157 addStackProbeSizeTargetAttribute(D, GV, CGM); 2158 } 2159 2160 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 2161 public: 2162 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 2163 X86AVXABILevel AVXLevel) 2164 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} 2165 2166 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 2167 CodeGen::CodeGenModule &CGM) const override; 2168 2169 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 2170 return 7; 2171 } 2172 2173 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2174 llvm::Value *Address) const override { 2175 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 2176 2177 // 0-15 are the 16 integer registers. 2178 // 16 is %rip. 2179 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 2180 return false; 2181 } 2182 2183 void getDependentLibraryOption(llvm::StringRef Lib, 2184 llvm::SmallString<24> &Opt) const override { 2185 Opt = "/DEFAULTLIB:"; 2186 Opt += qualifyWindowsLibrary(Lib); 2187 } 2188 2189 void getDetectMismatchOption(llvm::StringRef Name, 2190 llvm::StringRef Value, 2191 llvm::SmallString<32> &Opt) const override { 2192 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 2193 } 2194 }; 2195 2196 void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, 2197 llvm::GlobalValue *GV, 2198 CodeGen::CodeGenModule &CGM) const { 2199 TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 2200 2201 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 2202 if (FD->hasAttr<AnyX86InterruptAttr>()) { 2203 llvm::Function *Fn = cast<llvm::Function>(GV); 2204 Fn->setCallingConv(llvm::CallingConv::X86_INTR); 2205 } 2206 } 2207 2208 addStackProbeSizeTargetAttribute(D, GV, CGM); 2209 } 2210 } 2211 2212 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, 2213 Class &Hi) const { 2214 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: 2215 // 2216 // (a) If one of the classes is Memory, the whole argument is passed in 2217 // memory. 2218 // 2219 // (b) If X87UP is not preceded by X87, the whole argument is passed in 2220 // memory. 2221 // 2222 // (c) If the size of the aggregate exceeds two eightbytes and the first 2223 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole 2224 // argument is passed in memory. NOTE: This is necessary to keep the 2225 // ABI working for processors that don't support the __m256 type. 2226 // 2227 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. 2228 // 2229 // Some of these are enforced by the merging logic. Others can arise 2230 // only with unions; for example: 2231 // union { _Complex double; unsigned; } 2232 // 2233 // Note that clauses (b) and (c) were added in 0.98. 2234 // 2235 if (Hi == Memory) 2236 Lo = Memory; 2237 if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) 2238 Lo = Memory; 2239 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) 2240 Lo = Memory; 2241 if (Hi == SSEUp && Lo != SSE) 2242 Hi = SSE; 2243 } 2244 2245 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { 2246 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is 2247 // classified recursively so that always two fields are 2248 // considered. The resulting class is calculated according to 2249 // the classes of the fields in the eightbyte: 2250 // 2251 // (a) If both classes are equal, this is the resulting class. 2252 // 2253 // (b) If one of the classes is NO_CLASS, the resulting class is 2254 // the other class. 2255 // 2256 // (c) If one of the classes is MEMORY, the result is the MEMORY 2257 // class. 2258 // 2259 // (d) If one of the classes is INTEGER, the result is the 2260 // INTEGER. 2261 // 2262 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, 2263 // MEMORY is used as class. 2264 // 2265 // (f) Otherwise class SSE is used. 2266 2267 // Accum should never be memory (we should have returned) or 2268 // ComplexX87 (because this cannot be passed in a structure). 2269 assert((Accum != Memory && Accum != ComplexX87) && 2270 "Invalid accumulated classification during merge."); 2271 if (Accum == Field || Field == NoClass) 2272 return Accum; 2273 if (Field == Memory) 2274 return Memory; 2275 if (Accum == NoClass) 2276 return Field; 2277 if (Accum == Integer || Field == Integer) 2278 return Integer; 2279 if (Field == X87 || Field == X87Up || Field == ComplexX87 || 2280 Accum == X87 || Accum == X87Up) 2281 return Memory; 2282 return SSE; 2283 } 2284 2285 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, 2286 Class &Lo, Class &Hi, bool isNamedArg) const { 2287 // FIXME: This code can be simplified by introducing a simple value class for 2288 // Class pairs with appropriate constructor methods for the various 2289 // situations. 2290 2291 // FIXME: Some of the split computations are wrong; unaligned vectors 2292 // shouldn't be passed in registers for example, so there is no chance they 2293 // can straddle an eightbyte. Verify & simplify. 2294 2295 Lo = Hi = NoClass; 2296 2297 Class &Current = OffsetBase < 64 ? Lo : Hi; 2298 Current = Memory; 2299 2300 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 2301 BuiltinType::Kind k = BT->getKind(); 2302 2303 if (k == BuiltinType::Void) { 2304 Current = NoClass; 2305 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { 2306 Lo = Integer; 2307 Hi = Integer; 2308 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { 2309 Current = Integer; 2310 } else if (k == BuiltinType::Float || k == BuiltinType::Double) { 2311 Current = SSE; 2312 } else if (k == BuiltinType::LongDouble) { 2313 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 2314 if (LDF == &llvm::APFloat::IEEEquad) { 2315 Lo = SSE; 2316 Hi = SSEUp; 2317 } else if (LDF == &llvm::APFloat::x87DoubleExtended) { 2318 Lo = X87; 2319 Hi = X87Up; 2320 } else if (LDF == &llvm::APFloat::IEEEdouble) { 2321 Current = SSE; 2322 } else 2323 llvm_unreachable("unexpected long double representation!"); 2324 } 2325 // FIXME: _Decimal32 and _Decimal64 are SSE. 2326 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). 2327 return; 2328 } 2329 2330 if (const EnumType *ET = Ty->getAs<EnumType>()) { 2331 // Classify the underlying integer type. 2332 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); 2333 return; 2334 } 2335 2336 if (Ty->hasPointerRepresentation()) { 2337 Current = Integer; 2338 return; 2339 } 2340 2341 if (Ty->isMemberPointerType()) { 2342 if (Ty->isMemberFunctionPointerType()) { 2343 if (Has64BitPointers) { 2344 // If Has64BitPointers, this is an {i64, i64}, so classify both 2345 // Lo and Hi now. 2346 Lo = Hi = Integer; 2347 } else { 2348 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that 2349 // straddles an eightbyte boundary, Hi should be classified as well. 2350 uint64_t EB_FuncPtr = (OffsetBase) / 64; 2351 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; 2352 if (EB_FuncPtr != EB_ThisAdj) { 2353 Lo = Hi = Integer; 2354 } else { 2355 Current = Integer; 2356 } 2357 } 2358 } else { 2359 Current = Integer; 2360 } 2361 return; 2362 } 2363 2364 if (const VectorType *VT = Ty->getAs<VectorType>()) { 2365 uint64_t Size = getContext().getTypeSize(VT); 2366 if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { 2367 // gcc passes the following as integer: 2368 // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> 2369 // 2 bytes - <2 x char>, <1 x short> 2370 // 1 byte - <1 x char> 2371 Current = Integer; 2372 2373 // If this type crosses an eightbyte boundary, it should be 2374 // split. 2375 uint64_t EB_Lo = (OffsetBase) / 64; 2376 uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; 2377 if (EB_Lo != EB_Hi) 2378 Hi = Lo; 2379 } else if (Size == 64) { 2380 QualType ElementType = VT->getElementType(); 2381 2382 // gcc passes <1 x double> in memory. :( 2383 if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) 2384 return; 2385 2386 // gcc passes <1 x long long> as SSE but clang used to unconditionally 2387 // pass them as integer. For platforms where clang is the de facto 2388 // platform compiler, we must continue to use integer. 2389 if (!classifyIntegerMMXAsSSE() && 2390 (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || 2391 ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || 2392 ElementType->isSpecificBuiltinType(BuiltinType::Long) || 2393 ElementType->isSpecificBuiltinType(BuiltinType::ULong))) 2394 Current = Integer; 2395 else 2396 Current = SSE; 2397 2398 // If this type crosses an eightbyte boundary, it should be 2399 // split. 2400 if (OffsetBase && OffsetBase != 64) 2401 Hi = Lo; 2402 } else if (Size == 128 || 2403 (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { 2404 // Arguments of 256-bits are split into four eightbyte chunks. The 2405 // least significant one belongs to class SSE and all the others to class 2406 // SSEUP. The original Lo and Hi design considers that types can't be 2407 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. 2408 // This design isn't correct for 256-bits, but since there're no cases 2409 // where the upper parts would need to be inspected, avoid adding 2410 // complexity and just consider Hi to match the 64-256 part. 2411 // 2412 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in 2413 // registers if they are "named", i.e. not part of the "..." of a 2414 // variadic function. 2415 // 2416 // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are 2417 // split into eight eightbyte chunks, one SSE and seven SSEUP. 2418 Lo = SSE; 2419 Hi = SSEUp; 2420 } 2421 return; 2422 } 2423 2424 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 2425 QualType ET = getContext().getCanonicalType(CT->getElementType()); 2426 2427 uint64_t Size = getContext().getTypeSize(Ty); 2428 if (ET->isIntegralOrEnumerationType()) { 2429 if (Size <= 64) 2430 Current = Integer; 2431 else if (Size <= 128) 2432 Lo = Hi = Integer; 2433 } else if (ET == getContext().FloatTy) { 2434 Current = SSE; 2435 } else if (ET == getContext().DoubleTy) { 2436 Lo = Hi = SSE; 2437 } else if (ET == getContext().LongDoubleTy) { 2438 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 2439 if (LDF == &llvm::APFloat::IEEEquad) 2440 Current = Memory; 2441 else if (LDF == &llvm::APFloat::x87DoubleExtended) 2442 Current = ComplexX87; 2443 else if (LDF == &llvm::APFloat::IEEEdouble) 2444 Lo = Hi = SSE; 2445 else 2446 llvm_unreachable("unexpected long double representation!"); 2447 } 2448 2449 // If this complex type crosses an eightbyte boundary then it 2450 // should be split. 2451 uint64_t EB_Real = (OffsetBase) / 64; 2452 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; 2453 if (Hi == NoClass && EB_Real != EB_Imag) 2454 Hi = Lo; 2455 2456 return; 2457 } 2458 2459 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 2460 // Arrays are treated like structures. 2461 2462 uint64_t Size = getContext().getTypeSize(Ty); 2463 2464 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 2465 // than four eightbytes, ..., it has class MEMORY. 2466 if (Size > 256) 2467 return; 2468 2469 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned 2470 // fields, it has class MEMORY. 2471 // 2472 // Only need to check alignment of array base. 2473 if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) 2474 return; 2475 2476 // Otherwise implement simplified merge. We could be smarter about 2477 // this, but it isn't worth it and would be harder to verify. 2478 Current = NoClass; 2479 uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); 2480 uint64_t ArraySize = AT->getSize().getZExtValue(); 2481 2482 // The only case a 256-bit wide vector could be used is when the array 2483 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 2484 // to work for sizes wider than 128, early check and fallback to memory. 2485 if (Size > 128 && EltSize != 256) 2486 return; 2487 2488 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { 2489 Class FieldLo, FieldHi; 2490 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); 2491 Lo = merge(Lo, FieldLo); 2492 Hi = merge(Hi, FieldHi); 2493 if (Lo == Memory || Hi == Memory) 2494 break; 2495 } 2496 2497 postMerge(Size, Lo, Hi); 2498 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); 2499 return; 2500 } 2501 2502 if (const RecordType *RT = Ty->getAs<RecordType>()) { 2503 uint64_t Size = getContext().getTypeSize(Ty); 2504 2505 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 2506 // than four eightbytes, ..., it has class MEMORY. 2507 if (Size > 256) 2508 return; 2509 2510 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial 2511 // copy constructor or a non-trivial destructor, it is passed by invisible 2512 // reference. 2513 if (getRecordArgABI(RT, getCXXABI())) 2514 return; 2515 2516 const RecordDecl *RD = RT->getDecl(); 2517 2518 // Assume variable sized types are passed in memory. 2519 if (RD->hasFlexibleArrayMember()) 2520 return; 2521 2522 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 2523 2524 // Reset Lo class, this will be recomputed. 2525 Current = NoClass; 2526 2527 // If this is a C++ record, classify the bases first. 2528 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 2529 for (const auto &I : CXXRD->bases()) { 2530 assert(!I.isVirtual() && !I.getType()->isDependentType() && 2531 "Unexpected base class!"); 2532 const CXXRecordDecl *Base = 2533 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); 2534 2535 // Classify this field. 2536 // 2537 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a 2538 // single eightbyte, each is classified separately. Each eightbyte gets 2539 // initialized to class NO_CLASS. 2540 Class FieldLo, FieldHi; 2541 uint64_t Offset = 2542 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); 2543 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); 2544 Lo = merge(Lo, FieldLo); 2545 Hi = merge(Hi, FieldHi); 2546 if (Lo == Memory || Hi == Memory) { 2547 postMerge(Size, Lo, Hi); 2548 return; 2549 } 2550 } 2551 } 2552 2553 // Classify the fields one at a time, merging the results. 2554 unsigned idx = 0; 2555 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 2556 i != e; ++i, ++idx) { 2557 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 2558 bool BitField = i->isBitField(); 2559 2560 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than 2561 // four eightbytes, or it contains unaligned fields, it has class MEMORY. 2562 // 2563 // The only case a 256-bit wide vector could be used is when the struct 2564 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 2565 // to work for sizes wider than 128, early check and fallback to memory. 2566 // 2567 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { 2568 Lo = Memory; 2569 postMerge(Size, Lo, Hi); 2570 return; 2571 } 2572 // Note, skip this test for bit-fields, see below. 2573 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { 2574 Lo = Memory; 2575 postMerge(Size, Lo, Hi); 2576 return; 2577 } 2578 2579 // Classify this field. 2580 // 2581 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate 2582 // exceeds a single eightbyte, each is classified 2583 // separately. Each eightbyte gets initialized to class 2584 // NO_CLASS. 2585 Class FieldLo, FieldHi; 2586 2587 // Bit-fields require special handling, they do not force the 2588 // structure to be passed in memory even if unaligned, and 2589 // therefore they can straddle an eightbyte. 2590 if (BitField) { 2591 // Ignore padding bit-fields. 2592 if (i->isUnnamedBitfield()) 2593 continue; 2594 2595 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 2596 uint64_t Size = i->getBitWidthValue(getContext()); 2597 2598 uint64_t EB_Lo = Offset / 64; 2599 uint64_t EB_Hi = (Offset + Size - 1) / 64; 2600 2601 if (EB_Lo) { 2602 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); 2603 FieldLo = NoClass; 2604 FieldHi = Integer; 2605 } else { 2606 FieldLo = Integer; 2607 FieldHi = EB_Hi ? Integer : NoClass; 2608 } 2609 } else 2610 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); 2611 Lo = merge(Lo, FieldLo); 2612 Hi = merge(Hi, FieldHi); 2613 if (Lo == Memory || Hi == Memory) 2614 break; 2615 } 2616 2617 postMerge(Size, Lo, Hi); 2618 } 2619 } 2620 2621 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { 2622 // If this is a scalar LLVM value then assume LLVM will pass it in the right 2623 // place naturally. 2624 if (!isAggregateTypeForABI(Ty)) { 2625 // Treat an enum type as its underlying type. 2626 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2627 Ty = EnumTy->getDecl()->getIntegerType(); 2628 2629 return (Ty->isPromotableIntegerType() ? 2630 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 2631 } 2632 2633 return getNaturalAlignIndirect(Ty); 2634 } 2635 2636 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { 2637 if (const VectorType *VecTy = Ty->getAs<VectorType>()) { 2638 uint64_t Size = getContext().getTypeSize(VecTy); 2639 unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); 2640 if (Size <= 64 || Size > LargestVector) 2641 return true; 2642 } 2643 2644 return false; 2645 } 2646 2647 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, 2648 unsigned freeIntRegs) const { 2649 // If this is a scalar LLVM value then assume LLVM will pass it in the right 2650 // place naturally. 2651 // 2652 // This assumption is optimistic, as there could be free registers available 2653 // when we need to pass this argument in memory, and LLVM could try to pass 2654 // the argument in the free register. This does not seem to happen currently, 2655 // but this code would be much safer if we could mark the argument with 2656 // 'onstack'. See PR12193. 2657 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { 2658 // Treat an enum type as its underlying type. 2659 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2660 Ty = EnumTy->getDecl()->getIntegerType(); 2661 2662 return (Ty->isPromotableIntegerType() ? 2663 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 2664 } 2665 2666 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 2667 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 2668 2669 // Compute the byval alignment. We specify the alignment of the byval in all 2670 // cases so that the mid-level optimizer knows the alignment of the byval. 2671 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); 2672 2673 // Attempt to avoid passing indirect results using byval when possible. This 2674 // is important for good codegen. 2675 // 2676 // We do this by coercing the value into a scalar type which the backend can 2677 // handle naturally (i.e., without using byval). 2678 // 2679 // For simplicity, we currently only do this when we have exhausted all of the 2680 // free integer registers. Doing this when there are free integer registers 2681 // would require more care, as we would have to ensure that the coerced value 2682 // did not claim the unused register. That would require either reording the 2683 // arguments to the function (so that any subsequent inreg values came first), 2684 // or only doing this optimization when there were no following arguments that 2685 // might be inreg. 2686 // 2687 // We currently expect it to be rare (particularly in well written code) for 2688 // arguments to be passed on the stack when there are still free integer 2689 // registers available (this would typically imply large structs being passed 2690 // by value), so this seems like a fair tradeoff for now. 2691 // 2692 // We can revisit this if the backend grows support for 'onstack' parameter 2693 // attributes. See PR12193. 2694 if (freeIntRegs == 0) { 2695 uint64_t Size = getContext().getTypeSize(Ty); 2696 2697 // If this type fits in an eightbyte, coerce it into the matching integral 2698 // type, which will end up on the stack (with alignment 8). 2699 if (Align == 8 && Size <= 64) 2700 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 2701 Size)); 2702 } 2703 2704 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); 2705 } 2706 2707 /// The ABI specifies that a value should be passed in a full vector XMM/YMM 2708 /// register. Pick an LLVM IR type that will be passed as a vector register. 2709 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { 2710 // Wrapper structs/arrays that only contain vectors are passed just like 2711 // vectors; strip them off if present. 2712 if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) 2713 Ty = QualType(InnerTy, 0); 2714 2715 llvm::Type *IRType = CGT.ConvertType(Ty); 2716 if (isa<llvm::VectorType>(IRType) || 2717 IRType->getTypeID() == llvm::Type::FP128TyID) 2718 return IRType; 2719 2720 // We couldn't find the preferred IR vector type for 'Ty'. 2721 uint64_t Size = getContext().getTypeSize(Ty); 2722 assert((Size == 128 || Size == 256) && "Invalid type found!"); 2723 2724 // Return a LLVM IR vector type based on the size of 'Ty'. 2725 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2726 Size / 64); 2727 } 2728 2729 /// BitsContainNoUserData - Return true if the specified [start,end) bit range 2730 /// is known to either be off the end of the specified type or being in 2731 /// alignment padding. The user type specified is known to be at most 128 bits 2732 /// in size, and have passed through X86_64ABIInfo::classify with a successful 2733 /// classification that put one of the two halves in the INTEGER class. 2734 /// 2735 /// It is conservatively correct to return false. 2736 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, 2737 unsigned EndBit, ASTContext &Context) { 2738 // If the bytes being queried are off the end of the type, there is no user 2739 // data hiding here. This handles analysis of builtins, vectors and other 2740 // types that don't contain interesting padding. 2741 unsigned TySize = (unsigned)Context.getTypeSize(Ty); 2742 if (TySize <= StartBit) 2743 return true; 2744 2745 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 2746 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); 2747 unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); 2748 2749 // Check each element to see if the element overlaps with the queried range. 2750 for (unsigned i = 0; i != NumElts; ++i) { 2751 // If the element is after the span we care about, then we're done.. 2752 unsigned EltOffset = i*EltSize; 2753 if (EltOffset >= EndBit) break; 2754 2755 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; 2756 if (!BitsContainNoUserData(AT->getElementType(), EltStart, 2757 EndBit-EltOffset, Context)) 2758 return false; 2759 } 2760 // If it overlaps no elements, then it is safe to process as padding. 2761 return true; 2762 } 2763 2764 if (const RecordType *RT = Ty->getAs<RecordType>()) { 2765 const RecordDecl *RD = RT->getDecl(); 2766 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 2767 2768 // If this is a C++ record, check the bases first. 2769 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 2770 for (const auto &I : CXXRD->bases()) { 2771 assert(!I.isVirtual() && !I.getType()->isDependentType() && 2772 "Unexpected base class!"); 2773 const CXXRecordDecl *Base = 2774 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); 2775 2776 // If the base is after the span we care about, ignore it. 2777 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); 2778 if (BaseOffset >= EndBit) continue; 2779 2780 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; 2781 if (!BitsContainNoUserData(I.getType(), BaseStart, 2782 EndBit-BaseOffset, Context)) 2783 return false; 2784 } 2785 } 2786 2787 // Verify that no field has data that overlaps the region of interest. Yes 2788 // this could be sped up a lot by being smarter about queried fields, 2789 // however we're only looking at structs up to 16 bytes, so we don't care 2790 // much. 2791 unsigned idx = 0; 2792 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 2793 i != e; ++i, ++idx) { 2794 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); 2795 2796 // If we found a field after the region we care about, then we're done. 2797 if (FieldOffset >= EndBit) break; 2798 2799 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; 2800 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, 2801 Context)) 2802 return false; 2803 } 2804 2805 // If nothing in this record overlapped the area of interest, then we're 2806 // clean. 2807 return true; 2808 } 2809 2810 return false; 2811 } 2812 2813 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a 2814 /// float member at the specified offset. For example, {int,{float}} has a 2815 /// float at offset 4. It is conservatively correct for this routine to return 2816 /// false. 2817 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, 2818 const llvm::DataLayout &TD) { 2819 // Base case if we find a float. 2820 if (IROffset == 0 && IRType->isFloatTy()) 2821 return true; 2822 2823 // If this is a struct, recurse into the field at the specified offset. 2824 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 2825 const llvm::StructLayout *SL = TD.getStructLayout(STy); 2826 unsigned Elt = SL->getElementContainingOffset(IROffset); 2827 IROffset -= SL->getElementOffset(Elt); 2828 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); 2829 } 2830 2831 // If this is an array, recurse into the field at the specified offset. 2832 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 2833 llvm::Type *EltTy = ATy->getElementType(); 2834 unsigned EltSize = TD.getTypeAllocSize(EltTy); 2835 IROffset -= IROffset/EltSize*EltSize; 2836 return ContainsFloatAtOffset(EltTy, IROffset, TD); 2837 } 2838 2839 return false; 2840 } 2841 2842 2843 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the 2844 /// low 8 bytes of an XMM register, corresponding to the SSE class. 2845 llvm::Type *X86_64ABIInfo:: 2846 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, 2847 QualType SourceTy, unsigned SourceOffset) const { 2848 // The only three choices we have are either double, <2 x float>, or float. We 2849 // pass as float if the last 4 bytes is just padding. This happens for 2850 // structs that contain 3 floats. 2851 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, 2852 SourceOffset*8+64, getContext())) 2853 return llvm::Type::getFloatTy(getVMContext()); 2854 2855 // We want to pass as <2 x float> if the LLVM IR type contains a float at 2856 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the 2857 // case. 2858 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && 2859 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) 2860 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); 2861 2862 return llvm::Type::getDoubleTy(getVMContext()); 2863 } 2864 2865 2866 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in 2867 /// an 8-byte GPR. This means that we either have a scalar or we are talking 2868 /// about the high or low part of an up-to-16-byte struct. This routine picks 2869 /// the best LLVM IR type to represent this, which may be i64 or may be anything 2870 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, 2871 /// etc). 2872 /// 2873 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for 2874 /// the source type. IROffset is an offset in bytes into the LLVM IR type that 2875 /// the 8-byte value references. PrefType may be null. 2876 /// 2877 /// SourceTy is the source-level type for the entire argument. SourceOffset is 2878 /// an offset into this that we're processing (which is always either 0 or 8). 2879 /// 2880 llvm::Type *X86_64ABIInfo:: 2881 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, 2882 QualType SourceTy, unsigned SourceOffset) const { 2883 // If we're dealing with an un-offset LLVM IR type, then it means that we're 2884 // returning an 8-byte unit starting with it. See if we can safely use it. 2885 if (IROffset == 0) { 2886 // Pointers and int64's always fill the 8-byte unit. 2887 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || 2888 IRType->isIntegerTy(64)) 2889 return IRType; 2890 2891 // If we have a 1/2/4-byte integer, we can use it only if the rest of the 2892 // goodness in the source type is just tail padding. This is allowed to 2893 // kick in for struct {double,int} on the int, but not on 2894 // struct{double,int,int} because we wouldn't return the second int. We 2895 // have to do this analysis on the source type because we can't depend on 2896 // unions being lowered a specific way etc. 2897 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || 2898 IRType->isIntegerTy(32) || 2899 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { 2900 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : 2901 cast<llvm::IntegerType>(IRType)->getBitWidth(); 2902 2903 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, 2904 SourceOffset*8+64, getContext())) 2905 return IRType; 2906 } 2907 } 2908 2909 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 2910 // If this is a struct, recurse into the field at the specified offset. 2911 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); 2912 if (IROffset < SL->getSizeInBytes()) { 2913 unsigned FieldIdx = SL->getElementContainingOffset(IROffset); 2914 IROffset -= SL->getElementOffset(FieldIdx); 2915 2916 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, 2917 SourceTy, SourceOffset); 2918 } 2919 } 2920 2921 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 2922 llvm::Type *EltTy = ATy->getElementType(); 2923 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); 2924 unsigned EltOffset = IROffset/EltSize*EltSize; 2925 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, 2926 SourceOffset); 2927 } 2928 2929 // Okay, we don't have any better idea of what to pass, so we pass this in an 2930 // integer register that isn't too big to fit the rest of the struct. 2931 unsigned TySizeInBytes = 2932 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); 2933 2934 assert(TySizeInBytes != SourceOffset && "Empty field?"); 2935 2936 // It is always safe to classify this as an integer type up to i64 that 2937 // isn't larger than the structure. 2938 return llvm::IntegerType::get(getVMContext(), 2939 std::min(TySizeInBytes-SourceOffset, 8U)*8); 2940 } 2941 2942 2943 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally 2944 /// be used as elements of a two register pair to pass or return, return a 2945 /// first class aggregate to represent them. For example, if the low part of 2946 /// a by-value argument should be passed as i32* and the high part as float, 2947 /// return {i32*, float}. 2948 static llvm::Type * 2949 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, 2950 const llvm::DataLayout &TD) { 2951 // In order to correctly satisfy the ABI, we need to the high part to start 2952 // at offset 8. If the high and low parts we inferred are both 4-byte types 2953 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have 2954 // the second element at offset 8. Check for this: 2955 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); 2956 unsigned HiAlign = TD.getABITypeAlignment(Hi); 2957 unsigned HiStart = llvm::alignTo(LoSize, HiAlign); 2958 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); 2959 2960 // To handle this, we have to increase the size of the low part so that the 2961 // second element will start at an 8 byte offset. We can't increase the size 2962 // of the second element because it might make us access off the end of the 2963 // struct. 2964 if (HiStart != 8) { 2965 // There are usually two sorts of types the ABI generation code can produce 2966 // for the low part of a pair that aren't 8 bytes in size: float or 2967 // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and 2968 // NaCl). 2969 // Promote these to a larger type. 2970 if (Lo->isFloatTy()) 2971 Lo = llvm::Type::getDoubleTy(Lo->getContext()); 2972 else { 2973 assert((Lo->isIntegerTy() || Lo->isPointerTy()) 2974 && "Invalid/unknown lo type"); 2975 Lo = llvm::Type::getInt64Ty(Lo->getContext()); 2976 } 2977 } 2978 2979 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); 2980 2981 2982 // Verify that the second element is at an 8-byte offset. 2983 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && 2984 "Invalid x86-64 argument pair!"); 2985 return Result; 2986 } 2987 2988 ABIArgInfo X86_64ABIInfo:: 2989 classifyReturnType(QualType RetTy) const { 2990 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the 2991 // classification algorithm. 2992 X86_64ABIInfo::Class Lo, Hi; 2993 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); 2994 2995 // Check some invariants. 2996 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 2997 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 2998 2999 llvm::Type *ResType = nullptr; 3000 switch (Lo) { 3001 case NoClass: 3002 if (Hi == NoClass) 3003 return ABIArgInfo::getIgnore(); 3004 // If the low part is just padding, it takes no register, leave ResType 3005 // null. 3006 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 3007 "Unknown missing lo part"); 3008 break; 3009 3010 case SSEUp: 3011 case X87Up: 3012 llvm_unreachable("Invalid classification for lo word."); 3013 3014 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via 3015 // hidden argument. 3016 case Memory: 3017 return getIndirectReturnResult(RetTy); 3018 3019 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next 3020 // available register of the sequence %rax, %rdx is used. 3021 case Integer: 3022 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 3023 3024 // If we have a sign or zero extended integer, make sure to return Extend 3025 // so that the parameter gets the right LLVM IR attributes. 3026 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 3027 // Treat an enum type as its underlying type. 3028 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 3029 RetTy = EnumTy->getDecl()->getIntegerType(); 3030 3031 if (RetTy->isIntegralOrEnumerationType() && 3032 RetTy->isPromotableIntegerType()) 3033 return ABIArgInfo::getExtend(); 3034 } 3035 break; 3036 3037 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next 3038 // available SSE register of the sequence %xmm0, %xmm1 is used. 3039 case SSE: 3040 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 3041 break; 3042 3043 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is 3044 // returned on the X87 stack in %st0 as 80-bit x87 number. 3045 case X87: 3046 ResType = llvm::Type::getX86_FP80Ty(getVMContext()); 3047 break; 3048 3049 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real 3050 // part of the value is returned in %st0 and the imaginary part in 3051 // %st1. 3052 case ComplexX87: 3053 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); 3054 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), 3055 llvm::Type::getX86_FP80Ty(getVMContext()), 3056 nullptr); 3057 break; 3058 } 3059 3060 llvm::Type *HighPart = nullptr; 3061 switch (Hi) { 3062 // Memory was handled previously and X87 should 3063 // never occur as a hi class. 3064 case Memory: 3065 case X87: 3066 llvm_unreachable("Invalid classification for hi word."); 3067 3068 case ComplexX87: // Previously handled. 3069 case NoClass: 3070 break; 3071 3072 case Integer: 3073 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3074 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3075 return ABIArgInfo::getDirect(HighPart, 8); 3076 break; 3077 case SSE: 3078 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3079 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3080 return ABIArgInfo::getDirect(HighPart, 8); 3081 break; 3082 3083 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte 3084 // is passed in the next available eightbyte chunk if the last used 3085 // vector register. 3086 // 3087 // SSEUP should always be preceded by SSE, just widen. 3088 case SSEUp: 3089 assert(Lo == SSE && "Unexpected SSEUp classification."); 3090 ResType = GetByteVectorType(RetTy); 3091 break; 3092 3093 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is 3094 // returned together with the previous X87 value in %st0. 3095 case X87Up: 3096 // If X87Up is preceded by X87, we don't need to do 3097 // anything. However, in some cases with unions it may not be 3098 // preceded by X87. In such situations we follow gcc and pass the 3099 // extra bits in an SSE reg. 3100 if (Lo != X87) { 3101 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 3102 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 3103 return ABIArgInfo::getDirect(HighPart, 8); 3104 } 3105 break; 3106 } 3107 3108 // If a high part was specified, merge it together with the low part. It is 3109 // known to pass in the high eightbyte of the result. We do this by forming a 3110 // first class struct aggregate with the high and low part: {low, high} 3111 if (HighPart) 3112 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 3113 3114 return ABIArgInfo::getDirect(ResType); 3115 } 3116 3117 ABIArgInfo X86_64ABIInfo::classifyArgumentType( 3118 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, 3119 bool isNamedArg) 3120 const 3121 { 3122 Ty = useFirstFieldIfTransparentUnion(Ty); 3123 3124 X86_64ABIInfo::Class Lo, Hi; 3125 classify(Ty, 0, Lo, Hi, isNamedArg); 3126 3127 // Check some invariants. 3128 // FIXME: Enforce these by construction. 3129 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 3130 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 3131 3132 neededInt = 0; 3133 neededSSE = 0; 3134 llvm::Type *ResType = nullptr; 3135 switch (Lo) { 3136 case NoClass: 3137 if (Hi == NoClass) 3138 return ABIArgInfo::getIgnore(); 3139 // If the low part is just padding, it takes no register, leave ResType 3140 // null. 3141 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 3142 "Unknown missing lo part"); 3143 break; 3144 3145 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument 3146 // on the stack. 3147 case Memory: 3148 3149 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or 3150 // COMPLEX_X87, it is passed in memory. 3151 case X87: 3152 case ComplexX87: 3153 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) 3154 ++neededInt; 3155 return getIndirectResult(Ty, freeIntRegs); 3156 3157 case SSEUp: 3158 case X87Up: 3159 llvm_unreachable("Invalid classification for lo word."); 3160 3161 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next 3162 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 3163 // and %r9 is used. 3164 case Integer: 3165 ++neededInt; 3166 3167 // Pick an 8-byte type based on the preferred type. 3168 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); 3169 3170 // If we have a sign or zero extended integer, make sure to return Extend 3171 // so that the parameter gets the right LLVM IR attributes. 3172 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 3173 // Treat an enum type as its underlying type. 3174 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3175 Ty = EnumTy->getDecl()->getIntegerType(); 3176 3177 if (Ty->isIntegralOrEnumerationType() && 3178 Ty->isPromotableIntegerType()) 3179 return ABIArgInfo::getExtend(); 3180 } 3181 3182 break; 3183 3184 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next 3185 // available SSE register is used, the registers are taken in the 3186 // order from %xmm0 to %xmm7. 3187 case SSE: { 3188 llvm::Type *IRType = CGT.ConvertType(Ty); 3189 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); 3190 ++neededSSE; 3191 break; 3192 } 3193 } 3194 3195 llvm::Type *HighPart = nullptr; 3196 switch (Hi) { 3197 // Memory was handled previously, ComplexX87 and X87 should 3198 // never occur as hi classes, and X87Up must be preceded by X87, 3199 // which is passed in memory. 3200 case Memory: 3201 case X87: 3202 case ComplexX87: 3203 llvm_unreachable("Invalid classification for hi word."); 3204 3205 case NoClass: break; 3206 3207 case Integer: 3208 ++neededInt; 3209 // Pick an 8-byte type based on the preferred type. 3210 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 3211 3212 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 3213 return ABIArgInfo::getDirect(HighPart, 8); 3214 break; 3215 3216 // X87Up generally doesn't occur here (long double is passed in 3217 // memory), except in situations involving unions. 3218 case X87Up: 3219 case SSE: 3220 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 3221 3222 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 3223 return ABIArgInfo::getDirect(HighPart, 8); 3224 3225 ++neededSSE; 3226 break; 3227 3228 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the 3229 // eightbyte is passed in the upper half of the last used SSE 3230 // register. This only happens when 128-bit vectors are passed. 3231 case SSEUp: 3232 assert(Lo == SSE && "Unexpected SSEUp classification"); 3233 ResType = GetByteVectorType(Ty); 3234 break; 3235 } 3236 3237 // If a high part was specified, merge it together with the low part. It is 3238 // known to pass in the high eightbyte of the result. We do this by forming a 3239 // first class struct aggregate with the high and low part: {low, high} 3240 if (HighPart) 3241 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 3242 3243 return ABIArgInfo::getDirect(ResType); 3244 } 3245 3246 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 3247 3248 if (!getCXXABI().classifyReturnType(FI)) 3249 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3250 3251 // Keep track of the number of assigned registers. 3252 unsigned freeIntRegs = 6, freeSSERegs = 8; 3253 3254 // If the return value is indirect, then the hidden argument is consuming one 3255 // integer register. 3256 if (FI.getReturnInfo().isIndirect()) 3257 --freeIntRegs; 3258 3259 // The chain argument effectively gives us another free register. 3260 if (FI.isChainCall()) 3261 ++freeIntRegs; 3262 3263 unsigned NumRequiredArgs = FI.getNumRequiredArgs(); 3264 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers 3265 // get assigned (in left-to-right order) for passing as follows... 3266 unsigned ArgNo = 0; 3267 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3268 it != ie; ++it, ++ArgNo) { 3269 bool IsNamedArg = ArgNo < NumRequiredArgs; 3270 3271 unsigned neededInt, neededSSE; 3272 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, 3273 neededSSE, IsNamedArg); 3274 3275 // AMD64-ABI 3.2.3p3: If there are no registers available for any 3276 // eightbyte of an argument, the whole argument is passed on the 3277 // stack. If registers have already been assigned for some 3278 // eightbytes of such an argument, the assignments get reverted. 3279 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { 3280 freeIntRegs -= neededInt; 3281 freeSSERegs -= neededSSE; 3282 } else { 3283 it->info = getIndirectResult(it->type, freeIntRegs); 3284 } 3285 } 3286 } 3287 3288 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, 3289 Address VAListAddr, QualType Ty) { 3290 Address overflow_arg_area_p = CGF.Builder.CreateStructGEP( 3291 VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p"); 3292 llvm::Value *overflow_arg_area = 3293 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); 3294 3295 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 3296 // byte boundary if alignment needed by type exceeds 8 byte boundary. 3297 // It isn't stated explicitly in the standard, but in practice we use 3298 // alignment greater than 16 where necessary. 3299 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); 3300 if (Align > CharUnits::fromQuantity(8)) { 3301 overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, 3302 Align); 3303 } 3304 3305 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. 3306 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 3307 llvm::Value *Res = 3308 CGF.Builder.CreateBitCast(overflow_arg_area, 3309 llvm::PointerType::getUnqual(LTy)); 3310 3311 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: 3312 // l->overflow_arg_area + sizeof(type). 3313 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to 3314 // an 8 byte boundary. 3315 3316 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; 3317 llvm::Value *Offset = 3318 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); 3319 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, 3320 "overflow_arg_area.next"); 3321 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); 3322 3323 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. 3324 return Address(Res, Align); 3325 } 3326 3327 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 3328 QualType Ty) const { 3329 // Assume that va_list type is correct; should be pointer to LLVM type: 3330 // struct { 3331 // i32 gp_offset; 3332 // i32 fp_offset; 3333 // i8* overflow_arg_area; 3334 // i8* reg_save_area; 3335 // }; 3336 unsigned neededInt, neededSSE; 3337 3338 Ty = getContext().getCanonicalType(Ty); 3339 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, 3340 /*isNamedArg*/false); 3341 3342 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed 3343 // in the registers. If not go to step 7. 3344 if (!neededInt && !neededSSE) 3345 return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); 3346 3347 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of 3348 // general purpose registers needed to pass type and num_fp to hold 3349 // the number of floating point registers needed. 3350 3351 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into 3352 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or 3353 // l->fp_offset > 304 - num_fp * 16 go to step 7. 3354 // 3355 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of 3356 // register save space). 3357 3358 llvm::Value *InRegs = nullptr; 3359 Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); 3360 llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; 3361 if (neededInt) { 3362 gp_offset_p = 3363 CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(), 3364 "gp_offset_p"); 3365 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); 3366 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); 3367 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); 3368 } 3369 3370 if (neededSSE) { 3371 fp_offset_p = 3372 CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4), 3373 "fp_offset_p"); 3374 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); 3375 llvm::Value *FitsInFP = 3376 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); 3377 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); 3378 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; 3379 } 3380 3381 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 3382 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 3383 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 3384 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 3385 3386 // Emit code to load the value if it was passed in registers. 3387 3388 CGF.EmitBlock(InRegBlock); 3389 3390 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with 3391 // an offset of l->gp_offset and/or l->fp_offset. This may require 3392 // copying to a temporary location in case the parameter is passed 3393 // in different register classes or requires an alignment greater 3394 // than 8 for general purpose registers and 16 for XMM registers. 3395 // 3396 // FIXME: This really results in shameful code when we end up needing to 3397 // collect arguments from different places; often what should result in a 3398 // simple assembling of a structure from scattered addresses has many more 3399 // loads than necessary. Can we clean this up? 3400 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 3401 llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( 3402 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)), 3403 "reg_save_area"); 3404 3405 Address RegAddr = Address::invalid(); 3406 if (neededInt && neededSSE) { 3407 // FIXME: Cleanup. 3408 assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); 3409 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); 3410 Address Tmp = CGF.CreateMemTemp(Ty); 3411 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); 3412 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); 3413 llvm::Type *TyLo = ST->getElementType(0); 3414 llvm::Type *TyHi = ST->getElementType(1); 3415 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && 3416 "Unexpected ABI info for mixed regs"); 3417 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); 3418 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); 3419 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); 3420 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); 3421 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; 3422 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; 3423 3424 // Copy the first element. 3425 llvm::Value *V = 3426 CGF.Builder.CreateDefaultAlignedLoad( 3427 CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); 3428 CGF.Builder.CreateStore(V, 3429 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); 3430 3431 // Copy the second element. 3432 V = CGF.Builder.CreateDefaultAlignedLoad( 3433 CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); 3434 CharUnits Offset = CharUnits::fromQuantity( 3435 getDataLayout().getStructLayout(ST)->getElementOffset(1)); 3436 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset)); 3437 3438 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); 3439 } else if (neededInt) { 3440 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), 3441 CharUnits::fromQuantity(8)); 3442 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); 3443 3444 // Copy to a temporary if necessary to ensure the appropriate alignment. 3445 std::pair<CharUnits, CharUnits> SizeAlign = 3446 getContext().getTypeInfoInChars(Ty); 3447 uint64_t TySize = SizeAlign.first.getQuantity(); 3448 CharUnits TyAlign = SizeAlign.second; 3449 3450 // Copy into a temporary if the type is more aligned than the 3451 // register save area. 3452 if (TyAlign.getQuantity() > 8) { 3453 Address Tmp = CGF.CreateMemTemp(Ty); 3454 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); 3455 RegAddr = Tmp; 3456 } 3457 3458 } else if (neededSSE == 1) { 3459 RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), 3460 CharUnits::fromQuantity(16)); 3461 RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); 3462 } else { 3463 assert(neededSSE == 2 && "Invalid number of needed registers!"); 3464 // SSE registers are spaced 16 bytes apart in the register save 3465 // area, we need to collect the two eightbytes together. 3466 // The ABI isn't explicit about this, but it seems reasonable 3467 // to assume that the slots are 16-byte aligned, since the stack is 3468 // naturally 16-byte aligned and the prologue is expected to store 3469 // all the SSE registers to the RSA. 3470 Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), 3471 CharUnits::fromQuantity(16)); 3472 Address RegAddrHi = 3473 CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, 3474 CharUnits::fromQuantity(16)); 3475 llvm::Type *DoubleTy = CGF.DoubleTy; 3476 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); 3477 llvm::Value *V; 3478 Address Tmp = CGF.CreateMemTemp(Ty); 3479 Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); 3480 V = CGF.Builder.CreateLoad( 3481 CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy)); 3482 CGF.Builder.CreateStore(V, 3483 CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero())); 3484 V = CGF.Builder.CreateLoad( 3485 CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy)); 3486 CGF.Builder.CreateStore(V, 3487 CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8))); 3488 3489 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); 3490 } 3491 3492 // AMD64-ABI 3.5.7p5: Step 5. Set: 3493 // l->gp_offset = l->gp_offset + num_gp * 8 3494 // l->fp_offset = l->fp_offset + num_fp * 16. 3495 if (neededInt) { 3496 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); 3497 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), 3498 gp_offset_p); 3499 } 3500 if (neededSSE) { 3501 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); 3502 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), 3503 fp_offset_p); 3504 } 3505 CGF.EmitBranch(ContBlock); 3506 3507 // Emit code to load the value if it was passed in memory. 3508 3509 CGF.EmitBlock(InMemBlock); 3510 Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); 3511 3512 // Return the appropriate result. 3513 3514 CGF.EmitBlock(ContBlock); 3515 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, 3516 "vaarg.addr"); 3517 return ResAddr; 3518 } 3519 3520 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, 3521 QualType Ty) const { 3522 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 3523 CGF.getContext().getTypeInfoInChars(Ty), 3524 CharUnits::fromQuantity(8), 3525 /*allowHigherAlign*/ false); 3526 } 3527 3528 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, 3529 bool IsReturnType) const { 3530 3531 if (Ty->isVoidType()) 3532 return ABIArgInfo::getIgnore(); 3533 3534 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3535 Ty = EnumTy->getDecl()->getIntegerType(); 3536 3537 TypeInfo Info = getContext().getTypeInfo(Ty); 3538 uint64_t Width = Info.Width; 3539 CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); 3540 3541 const RecordType *RT = Ty->getAs<RecordType>(); 3542 if (RT) { 3543 if (!IsReturnType) { 3544 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) 3545 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 3546 } 3547 3548 if (RT->getDecl()->hasFlexibleArrayMember()) 3549 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 3550 3551 } 3552 3553 // vectorcall adds the concept of a homogenous vector aggregate, similar to 3554 // other targets. 3555 const Type *Base = nullptr; 3556 uint64_t NumElts = 0; 3557 if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { 3558 if (FreeSSERegs >= NumElts) { 3559 FreeSSERegs -= NumElts; 3560 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) 3561 return ABIArgInfo::getDirect(); 3562 return ABIArgInfo::getExpand(); 3563 } 3564 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 3565 } 3566 3567 3568 if (Ty->isMemberPointerType()) { 3569 // If the member pointer is represented by an LLVM int or ptr, pass it 3570 // directly. 3571 llvm::Type *LLTy = CGT.ConvertType(Ty); 3572 if (LLTy->isPointerTy() || LLTy->isIntegerTy()) 3573 return ABIArgInfo::getDirect(); 3574 } 3575 3576 if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { 3577 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 3578 // not 1, 2, 4, or 8 bytes, must be passed by reference." 3579 if (Width > 64 || !llvm::isPowerOf2_64(Width)) 3580 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 3581 3582 // Otherwise, coerce it to a small integer. 3583 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); 3584 } 3585 3586 // Bool type is always extended to the ABI, other builtin types are not 3587 // extended. 3588 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 3589 if (BT && BT->getKind() == BuiltinType::Bool) 3590 return ABIArgInfo::getExtend(); 3591 3592 // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It 3593 // passes them indirectly through memory. 3594 if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) { 3595 const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); 3596 if (LDF == &llvm::APFloat::x87DoubleExtended) 3597 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 3598 } 3599 3600 return ABIArgInfo::getDirect(); 3601 } 3602 3603 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 3604 bool IsVectorCall = 3605 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; 3606 3607 // We can use up to 4 SSE return registers with vectorcall. 3608 unsigned FreeSSERegs = IsVectorCall ? 4 : 0; 3609 if (!getCXXABI().classifyReturnType(FI)) 3610 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); 3611 3612 // We can use up to 6 SSE register parameters with vectorcall. 3613 FreeSSERegs = IsVectorCall ? 6 : 0; 3614 for (auto &I : FI.arguments()) 3615 I.info = classify(I.type, FreeSSERegs, false); 3616 } 3617 3618 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 3619 QualType Ty) const { 3620 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 3621 CGF.getContext().getTypeInfoInChars(Ty), 3622 CharUnits::fromQuantity(8), 3623 /*allowHigherAlign*/ false); 3624 } 3625 3626 // PowerPC-32 3627 namespace { 3628 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. 3629 class PPC32_SVR4_ABIInfo : public DefaultABIInfo { 3630 bool IsSoftFloatABI; 3631 public: 3632 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) 3633 : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} 3634 3635 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 3636 QualType Ty) const override; 3637 }; 3638 3639 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { 3640 public: 3641 PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) 3642 : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} 3643 3644 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 3645 // This is recovered from gcc output. 3646 return 1; // r1 is the dedicated stack pointer 3647 } 3648 3649 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3650 llvm::Value *Address) const override; 3651 }; 3652 3653 } 3654 3655 // TODO: this implementation is now likely redundant with 3656 // DefaultABIInfo::EmitVAArg. 3657 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, 3658 QualType Ty) const { 3659 const unsigned OverflowLimit = 8; 3660 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 3661 // TODO: Implement this. For now ignore. 3662 (void)CTy; 3663 return Address::invalid(); // FIXME? 3664 } 3665 3666 // struct __va_list_tag { 3667 // unsigned char gpr; 3668 // unsigned char fpr; 3669 // unsigned short reserved; 3670 // void *overflow_arg_area; 3671 // void *reg_save_area; 3672 // }; 3673 3674 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; 3675 bool isInt = 3676 Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); 3677 bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; 3678 3679 // All aggregates are passed indirectly? That doesn't seem consistent 3680 // with the argument-lowering code. 3681 bool isIndirect = Ty->isAggregateType(); 3682 3683 CGBuilderTy &Builder = CGF.Builder; 3684 3685 // The calling convention either uses 1-2 GPRs or 1 FPR. 3686 Address NumRegsAddr = Address::invalid(); 3687 if (isInt || IsSoftFloatABI) { 3688 NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr"); 3689 } else { 3690 NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr"); 3691 } 3692 3693 llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); 3694 3695 // "Align" the register count when TY is i64. 3696 if (isI64 || (isF64 && IsSoftFloatABI)) { 3697 NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); 3698 NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); 3699 } 3700 3701 llvm::Value *CC = 3702 Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); 3703 3704 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); 3705 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); 3706 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 3707 3708 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); 3709 3710 llvm::Type *DirectTy = CGF.ConvertType(Ty); 3711 if (isIndirect) DirectTy = DirectTy->getPointerTo(0); 3712 3713 // Case 1: consume registers. 3714 Address RegAddr = Address::invalid(); 3715 { 3716 CGF.EmitBlock(UsingRegs); 3717 3718 Address RegSaveAreaPtr = 3719 Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8)); 3720 RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), 3721 CharUnits::fromQuantity(8)); 3722 assert(RegAddr.getElementType() == CGF.Int8Ty); 3723 3724 // Floating-point registers start after the general-purpose registers. 3725 if (!(isInt || IsSoftFloatABI)) { 3726 RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, 3727 CharUnits::fromQuantity(32)); 3728 } 3729 3730 // Get the address of the saved value by scaling the number of 3731 // registers we've used by the number of 3732 CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); 3733 llvm::Value *RegOffset = 3734 Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); 3735 RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, 3736 RegAddr.getPointer(), RegOffset), 3737 RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); 3738 RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); 3739 3740 // Increase the used-register count. 3741 NumRegs = 3742 Builder.CreateAdd(NumRegs, 3743 Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); 3744 Builder.CreateStore(NumRegs, NumRegsAddr); 3745 3746 CGF.EmitBranch(Cont); 3747 } 3748 3749 // Case 2: consume space in the overflow area. 3750 Address MemAddr = Address::invalid(); 3751 { 3752 CGF.EmitBlock(UsingOverflow); 3753 3754 Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); 3755 3756 // Everything in the overflow area is rounded up to a size of at least 4. 3757 CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); 3758 3759 CharUnits Size; 3760 if (!isIndirect) { 3761 auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); 3762 Size = TypeInfo.first.alignTo(OverflowAreaAlign); 3763 } else { 3764 Size = CGF.getPointerSize(); 3765 } 3766 3767 Address OverflowAreaAddr = 3768 Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4)); 3769 Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), 3770 OverflowAreaAlign); 3771 // Round up address of argument to alignment 3772 CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); 3773 if (Align > OverflowAreaAlign) { 3774 llvm::Value *Ptr = OverflowArea.getPointer(); 3775 OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), 3776 Align); 3777 } 3778 3779 MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); 3780 3781 // Increase the overflow area. 3782 OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); 3783 Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); 3784 CGF.EmitBranch(Cont); 3785 } 3786 3787 CGF.EmitBlock(Cont); 3788 3789 // Merge the cases with a phi. 3790 Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, 3791 "vaarg.addr"); 3792 3793 // Load the pointer if the argument was passed indirectly. 3794 if (isIndirect) { 3795 Result = Address(Builder.CreateLoad(Result, "aggr"), 3796 getContext().getTypeAlignInChars(Ty)); 3797 } 3798 3799 return Result; 3800 } 3801 3802 bool 3803 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3804 llvm::Value *Address) const { 3805 // This is calculated from the LLVM and GCC tables and verified 3806 // against gcc output. AFAIK all ABIs use the same encoding. 3807 3808 CodeGen::CGBuilderTy &Builder = CGF.Builder; 3809 3810 llvm::IntegerType *i8 = CGF.Int8Ty; 3811 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 3812 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 3813 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 3814 3815 // 0-31: r0-31, the 4-byte general-purpose registers 3816 AssignToArrayRange(Builder, Address, Four8, 0, 31); 3817 3818 // 32-63: fp0-31, the 8-byte floating-point registers 3819 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 3820 3821 // 64-76 are various 4-byte special-purpose registers: 3822 // 64: mq 3823 // 65: lr 3824 // 66: ctr 3825 // 67: ap 3826 // 68-75 cr0-7 3827 // 76: xer 3828 AssignToArrayRange(Builder, Address, Four8, 64, 76); 3829 3830 // 77-108: v0-31, the 16-byte vector registers 3831 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 3832 3833 // 109: vrsave 3834 // 110: vscr 3835 // 111: spe_acc 3836 // 112: spefscr 3837 // 113: sfp 3838 AssignToArrayRange(Builder, Address, Four8, 109, 113); 3839 3840 return false; 3841 } 3842 3843 // PowerPC-64 3844 3845 namespace { 3846 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. 3847 class PPC64_SVR4_ABIInfo : public ABIInfo { 3848 public: 3849 enum ABIKind { 3850 ELFv1 = 0, 3851 ELFv2 3852 }; 3853 3854 private: 3855 static const unsigned GPRBits = 64; 3856 ABIKind Kind; 3857 bool HasQPX; 3858 3859 // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and 3860 // will be passed in a QPX register. 3861 bool IsQPXVectorTy(const Type *Ty) const { 3862 if (!HasQPX) 3863 return false; 3864 3865 if (const VectorType *VT = Ty->getAs<VectorType>()) { 3866 unsigned NumElements = VT->getNumElements(); 3867 if (NumElements == 1) 3868 return false; 3869 3870 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { 3871 if (getContext().getTypeSize(Ty) <= 256) 3872 return true; 3873 } else if (VT->getElementType()-> 3874 isSpecificBuiltinType(BuiltinType::Float)) { 3875 if (getContext().getTypeSize(Ty) <= 128) 3876 return true; 3877 } 3878 } 3879 3880 return false; 3881 } 3882 3883 bool IsQPXVectorTy(QualType Ty) const { 3884 return IsQPXVectorTy(Ty.getTypePtr()); 3885 } 3886 3887 public: 3888 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) 3889 : ABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} 3890 3891 bool isPromotableTypeForABI(QualType Ty) const; 3892 CharUnits getParamTypeAlignment(QualType Ty) const; 3893 3894 ABIArgInfo classifyReturnType(QualType RetTy) const; 3895 ABIArgInfo classifyArgumentType(QualType Ty) const; 3896 3897 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 3898 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 3899 uint64_t Members) const override; 3900 3901 // TODO: We can add more logic to computeInfo to improve performance. 3902 // Example: For aggregate arguments that fit in a register, we could 3903 // use getDirectInReg (as is done below for structs containing a single 3904 // floating-point value) to avoid pushing them to memory on function 3905 // entry. This would require changing the logic in PPCISelLowering 3906 // when lowering the parameters in the caller and args in the callee. 3907 void computeInfo(CGFunctionInfo &FI) const override { 3908 if (!getCXXABI().classifyReturnType(FI)) 3909 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3910 for (auto &I : FI.arguments()) { 3911 // We rely on the default argument classification for the most part. 3912 // One exception: An aggregate containing a single floating-point 3913 // or vector item must be passed in a register if one is available. 3914 const Type *T = isSingleElementStruct(I.type, getContext()); 3915 if (T) { 3916 const BuiltinType *BT = T->getAs<BuiltinType>(); 3917 if (IsQPXVectorTy(T) || 3918 (T->isVectorType() && getContext().getTypeSize(T) == 128) || 3919 (BT && BT->isFloatingPoint())) { 3920 QualType QT(T, 0); 3921 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); 3922 continue; 3923 } 3924 } 3925 I.info = classifyArgumentType(I.type); 3926 } 3927 } 3928 3929 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 3930 QualType Ty) const override; 3931 }; 3932 3933 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { 3934 3935 public: 3936 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, 3937 PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) 3938 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} 3939 3940 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 3941 // This is recovered from gcc output. 3942 return 1; // r1 is the dedicated stack pointer 3943 } 3944 3945 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3946 llvm::Value *Address) const override; 3947 }; 3948 3949 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 3950 public: 3951 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 3952 3953 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 3954 // This is recovered from gcc output. 3955 return 1; // r1 is the dedicated stack pointer 3956 } 3957 3958 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3959 llvm::Value *Address) const override; 3960 }; 3961 3962 } 3963 3964 // Return true if the ABI requires Ty to be passed sign- or zero- 3965 // extended to 64 bits. 3966 bool 3967 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { 3968 // Treat an enum type as its underlying type. 3969 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3970 Ty = EnumTy->getDecl()->getIntegerType(); 3971 3972 // Promotable integer types are required to be promoted by the ABI. 3973 if (Ty->isPromotableIntegerType()) 3974 return true; 3975 3976 // In addition to the usual promotable integer types, we also need to 3977 // extend all 32-bit types, since the ABI requires promotion to 64 bits. 3978 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 3979 switch (BT->getKind()) { 3980 case BuiltinType::Int: 3981 case BuiltinType::UInt: 3982 return true; 3983 default: 3984 break; 3985 } 3986 3987 return false; 3988 } 3989 3990 /// isAlignedParamType - Determine whether a type requires 16-byte or 3991 /// higher alignment in the parameter area. Always returns at least 8. 3992 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { 3993 // Complex types are passed just like their elements. 3994 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 3995 Ty = CTy->getElementType(); 3996 3997 // Only vector types of size 16 bytes need alignment (larger types are 3998 // passed via reference, smaller types are not aligned). 3999 if (IsQPXVectorTy(Ty)) { 4000 if (getContext().getTypeSize(Ty) > 128) 4001 return CharUnits::fromQuantity(32); 4002 4003 return CharUnits::fromQuantity(16); 4004 } else if (Ty->isVectorType()) { 4005 return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); 4006 } 4007 4008 // For single-element float/vector structs, we consider the whole type 4009 // to have the same alignment requirements as its single element. 4010 const Type *AlignAsType = nullptr; 4011 const Type *EltType = isSingleElementStruct(Ty, getContext()); 4012 if (EltType) { 4013 const BuiltinType *BT = EltType->getAs<BuiltinType>(); 4014 if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && 4015 getContext().getTypeSize(EltType) == 128) || 4016 (BT && BT->isFloatingPoint())) 4017 AlignAsType = EltType; 4018 } 4019 4020 // Likewise for ELFv2 homogeneous aggregates. 4021 const Type *Base = nullptr; 4022 uint64_t Members = 0; 4023 if (!AlignAsType && Kind == ELFv2 && 4024 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) 4025 AlignAsType = Base; 4026 4027 // With special case aggregates, only vector base types need alignment. 4028 if (AlignAsType && IsQPXVectorTy(AlignAsType)) { 4029 if (getContext().getTypeSize(AlignAsType) > 128) 4030 return CharUnits::fromQuantity(32); 4031 4032 return CharUnits::fromQuantity(16); 4033 } else if (AlignAsType) { 4034 return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); 4035 } 4036 4037 // Otherwise, we only need alignment for any aggregate type that 4038 // has an alignment requirement of >= 16 bytes. 4039 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { 4040 if (HasQPX && getContext().getTypeAlign(Ty) >= 256) 4041 return CharUnits::fromQuantity(32); 4042 return CharUnits::fromQuantity(16); 4043 } 4044 4045 return CharUnits::fromQuantity(8); 4046 } 4047 4048 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous 4049 /// aggregate. Base is set to the base element type, and Members is set 4050 /// to the number of base elements. 4051 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, 4052 uint64_t &Members) const { 4053 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 4054 uint64_t NElements = AT->getSize().getZExtValue(); 4055 if (NElements == 0) 4056 return false; 4057 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) 4058 return false; 4059 Members *= NElements; 4060 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 4061 const RecordDecl *RD = RT->getDecl(); 4062 if (RD->hasFlexibleArrayMember()) 4063 return false; 4064 4065 Members = 0; 4066 4067 // If this is a C++ record, check the bases first. 4068 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 4069 for (const auto &I : CXXRD->bases()) { 4070 // Ignore empty records. 4071 if (isEmptyRecord(getContext(), I.getType(), true)) 4072 continue; 4073 4074 uint64_t FldMembers; 4075 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) 4076 return false; 4077 4078 Members += FldMembers; 4079 } 4080 } 4081 4082 for (const auto *FD : RD->fields()) { 4083 // Ignore (non-zero arrays of) empty records. 4084 QualType FT = FD->getType(); 4085 while (const ConstantArrayType *AT = 4086 getContext().getAsConstantArrayType(FT)) { 4087 if (AT->getSize().getZExtValue() == 0) 4088 return false; 4089 FT = AT->getElementType(); 4090 } 4091 if (isEmptyRecord(getContext(), FT, true)) 4092 continue; 4093 4094 // For compatibility with GCC, ignore empty bitfields in C++ mode. 4095 if (getContext().getLangOpts().CPlusPlus && 4096 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) 4097 continue; 4098 4099 uint64_t FldMembers; 4100 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) 4101 return false; 4102 4103 Members = (RD->isUnion() ? 4104 std::max(Members, FldMembers) : Members + FldMembers); 4105 } 4106 4107 if (!Base) 4108 return false; 4109 4110 // Ensure there is no padding. 4111 if (getContext().getTypeSize(Base) * Members != 4112 getContext().getTypeSize(Ty)) 4113 return false; 4114 } else { 4115 Members = 1; 4116 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 4117 Members = 2; 4118 Ty = CT->getElementType(); 4119 } 4120 4121 // Most ABIs only support float, double, and some vector type widths. 4122 if (!isHomogeneousAggregateBaseType(Ty)) 4123 return false; 4124 4125 // The base type must be the same for all members. Types that 4126 // agree in both total size and mode (float vs. vector) are 4127 // treated as being equivalent here. 4128 const Type *TyPtr = Ty.getTypePtr(); 4129 if (!Base) { 4130 Base = TyPtr; 4131 // If it's a non-power-of-2 vector, its size is already a power-of-2, 4132 // so make sure to widen it explicitly. 4133 if (const VectorType *VT = Base->getAs<VectorType>()) { 4134 QualType EltTy = VT->getElementType(); 4135 unsigned NumElements = 4136 getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); 4137 Base = getContext() 4138 .getVectorType(EltTy, NumElements, VT->getVectorKind()) 4139 .getTypePtr(); 4140 } 4141 } 4142 4143 if (Base->isVectorType() != TyPtr->isVectorType() || 4144 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) 4145 return false; 4146 } 4147 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); 4148 } 4149 4150 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 4151 // Homogeneous aggregates for ELFv2 must have base types of float, 4152 // double, long double, or 128-bit vectors. 4153 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 4154 if (BT->getKind() == BuiltinType::Float || 4155 BT->getKind() == BuiltinType::Double || 4156 BT->getKind() == BuiltinType::LongDouble) 4157 return true; 4158 } 4159 if (const VectorType *VT = Ty->getAs<VectorType>()) { 4160 if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) 4161 return true; 4162 } 4163 return false; 4164 } 4165 4166 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( 4167 const Type *Base, uint64_t Members) const { 4168 // Vector types require one register, floating point types require one 4169 // or two registers depending on their size. 4170 uint32_t NumRegs = 4171 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; 4172 4173 // Homogeneous Aggregates may occupy at most 8 registers. 4174 return Members * NumRegs <= 8; 4175 } 4176 4177 ABIArgInfo 4178 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { 4179 Ty = useFirstFieldIfTransparentUnion(Ty); 4180 4181 if (Ty->isAnyComplexType()) 4182 return ABIArgInfo::getDirect(); 4183 4184 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) 4185 // or via reference (larger than 16 bytes). 4186 if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { 4187 uint64_t Size = getContext().getTypeSize(Ty); 4188 if (Size > 128) 4189 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 4190 else if (Size < 128) { 4191 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 4192 return ABIArgInfo::getDirect(CoerceTy); 4193 } 4194 } 4195 4196 if (isAggregateTypeForABI(Ty)) { 4197 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 4198 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 4199 4200 uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); 4201 uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); 4202 4203 // ELFv2 homogeneous aggregates are passed as array types. 4204 const Type *Base = nullptr; 4205 uint64_t Members = 0; 4206 if (Kind == ELFv2 && 4207 isHomogeneousAggregate(Ty, Base, Members)) { 4208 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 4209 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 4210 return ABIArgInfo::getDirect(CoerceTy); 4211 } 4212 4213 // If an aggregate may end up fully in registers, we do not 4214 // use the ByVal method, but pass the aggregate as array. 4215 // This is usually beneficial since we avoid forcing the 4216 // back-end to store the argument to memory. 4217 uint64_t Bits = getContext().getTypeSize(Ty); 4218 if (Bits > 0 && Bits <= 8 * GPRBits) { 4219 llvm::Type *CoerceTy; 4220 4221 // Types up to 8 bytes are passed as integer type (which will be 4222 // properly aligned in the argument save area doubleword). 4223 if (Bits <= GPRBits) 4224 CoerceTy = 4225 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); 4226 // Larger types are passed as arrays, with the base type selected 4227 // according to the required alignment in the save area. 4228 else { 4229 uint64_t RegBits = ABIAlign * 8; 4230 uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; 4231 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); 4232 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); 4233 } 4234 4235 return ABIArgInfo::getDirect(CoerceTy); 4236 } 4237 4238 // All other aggregates are passed ByVal. 4239 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), 4240 /*ByVal=*/true, 4241 /*Realign=*/TyAlign > ABIAlign); 4242 } 4243 4244 return (isPromotableTypeForABI(Ty) ? 4245 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 4246 } 4247 4248 ABIArgInfo 4249 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { 4250 if (RetTy->isVoidType()) 4251 return ABIArgInfo::getIgnore(); 4252 4253 if (RetTy->isAnyComplexType()) 4254 return ABIArgInfo::getDirect(); 4255 4256 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) 4257 // or via reference (larger than 16 bytes). 4258 if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { 4259 uint64_t Size = getContext().getTypeSize(RetTy); 4260 if (Size > 128) 4261 return getNaturalAlignIndirect(RetTy); 4262 else if (Size < 128) { 4263 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 4264 return ABIArgInfo::getDirect(CoerceTy); 4265 } 4266 } 4267 4268 if (isAggregateTypeForABI(RetTy)) { 4269 // ELFv2 homogeneous aggregates are returned as array types. 4270 const Type *Base = nullptr; 4271 uint64_t Members = 0; 4272 if (Kind == ELFv2 && 4273 isHomogeneousAggregate(RetTy, Base, Members)) { 4274 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 4275 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 4276 return ABIArgInfo::getDirect(CoerceTy); 4277 } 4278 4279 // ELFv2 small aggregates are returned in up to two registers. 4280 uint64_t Bits = getContext().getTypeSize(RetTy); 4281 if (Kind == ELFv2 && Bits <= 2 * GPRBits) { 4282 if (Bits == 0) 4283 return ABIArgInfo::getIgnore(); 4284 4285 llvm::Type *CoerceTy; 4286 if (Bits > GPRBits) { 4287 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); 4288 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); 4289 } else 4290 CoerceTy = 4291 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); 4292 return ABIArgInfo::getDirect(CoerceTy); 4293 } 4294 4295 // All other aggregates are returned indirectly. 4296 return getNaturalAlignIndirect(RetTy); 4297 } 4298 4299 return (isPromotableTypeForABI(RetTy) ? 4300 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 4301 } 4302 4303 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. 4304 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4305 QualType Ty) const { 4306 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 4307 TypeInfo.second = getParamTypeAlignment(Ty); 4308 4309 CharUnits SlotSize = CharUnits::fromQuantity(8); 4310 4311 // If we have a complex type and the base type is smaller than 8 bytes, 4312 // the ABI calls for the real and imaginary parts to be right-adjusted 4313 // in separate doublewords. However, Clang expects us to produce a 4314 // pointer to a structure with the two parts packed tightly. So generate 4315 // loads of the real and imaginary parts relative to the va_list pointer, 4316 // and store them to a temporary structure. 4317 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 4318 CharUnits EltSize = TypeInfo.first / 2; 4319 if (EltSize < SlotSize) { 4320 Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, 4321 SlotSize * 2, SlotSize, 4322 SlotSize, /*AllowHigher*/ true); 4323 4324 Address RealAddr = Addr; 4325 Address ImagAddr = RealAddr; 4326 if (CGF.CGM.getDataLayout().isBigEndian()) { 4327 RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, 4328 SlotSize - EltSize); 4329 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, 4330 2 * SlotSize - EltSize); 4331 } else { 4332 ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); 4333 } 4334 4335 llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); 4336 RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); 4337 ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); 4338 llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); 4339 llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); 4340 4341 Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); 4342 CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), 4343 /*init*/ true); 4344 return Temp; 4345 } 4346 } 4347 4348 // Otherwise, just use the general rule. 4349 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, 4350 TypeInfo, SlotSize, /*AllowHigher*/ true); 4351 } 4352 4353 static bool 4354 PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4355 llvm::Value *Address) { 4356 // This is calculated from the LLVM and GCC tables and verified 4357 // against gcc output. AFAIK all ABIs use the same encoding. 4358 4359 CodeGen::CGBuilderTy &Builder = CGF.Builder; 4360 4361 llvm::IntegerType *i8 = CGF.Int8Ty; 4362 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 4363 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 4364 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 4365 4366 // 0-31: r0-31, the 8-byte general-purpose registers 4367 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 4368 4369 // 32-63: fp0-31, the 8-byte floating-point registers 4370 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 4371 4372 // 64-76 are various 4-byte special-purpose registers: 4373 // 64: mq 4374 // 65: lr 4375 // 66: ctr 4376 // 67: ap 4377 // 68-75 cr0-7 4378 // 76: xer 4379 AssignToArrayRange(Builder, Address, Four8, 64, 76); 4380 4381 // 77-108: v0-31, the 16-byte vector registers 4382 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 4383 4384 // 109: vrsave 4385 // 110: vscr 4386 // 111: spe_acc 4387 // 112: spefscr 4388 // 113: sfp 4389 AssignToArrayRange(Builder, Address, Four8, 109, 113); 4390 4391 return false; 4392 } 4393 4394 bool 4395 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( 4396 CodeGen::CodeGenFunction &CGF, 4397 llvm::Value *Address) const { 4398 4399 return PPC64_initDwarfEHRegSizeTable(CGF, Address); 4400 } 4401 4402 bool 4403 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4404 llvm::Value *Address) const { 4405 4406 return PPC64_initDwarfEHRegSizeTable(CGF, Address); 4407 } 4408 4409 //===----------------------------------------------------------------------===// 4410 // AArch64 ABI Implementation 4411 //===----------------------------------------------------------------------===// 4412 4413 namespace { 4414 4415 class AArch64ABIInfo : public SwiftABIInfo { 4416 public: 4417 enum ABIKind { 4418 AAPCS = 0, 4419 DarwinPCS 4420 }; 4421 4422 private: 4423 ABIKind Kind; 4424 4425 public: 4426 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) 4427 : SwiftABIInfo(CGT), Kind(Kind) {} 4428 4429 private: 4430 ABIKind getABIKind() const { return Kind; } 4431 bool isDarwinPCS() const { return Kind == DarwinPCS; } 4432 4433 ABIArgInfo classifyReturnType(QualType RetTy) const; 4434 ABIArgInfo classifyArgumentType(QualType RetTy) const; 4435 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 4436 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 4437 uint64_t Members) const override; 4438 4439 bool isIllegalVectorType(QualType Ty) const; 4440 4441 void computeInfo(CGFunctionInfo &FI) const override { 4442 if (!getCXXABI().classifyReturnType(FI)) 4443 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4444 4445 for (auto &it : FI.arguments()) 4446 it.info = classifyArgumentType(it.type); 4447 } 4448 4449 Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, 4450 CodeGenFunction &CGF) const; 4451 4452 Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, 4453 CodeGenFunction &CGF) const; 4454 4455 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4456 QualType Ty) const override { 4457 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) 4458 : EmitAAPCSVAArg(VAListAddr, Ty, CGF); 4459 } 4460 4461 bool shouldPassIndirectlyForSwift(CharUnits totalSize, 4462 ArrayRef<llvm::Type*> scalars, 4463 bool asReturnValue) const override { 4464 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 4465 } 4466 }; 4467 4468 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { 4469 public: 4470 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) 4471 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} 4472 4473 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 4474 return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; 4475 } 4476 4477 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 4478 return 31; 4479 } 4480 4481 bool doesReturnSlotInterfereWithArgs() const override { return false; } 4482 }; 4483 } 4484 4485 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { 4486 Ty = useFirstFieldIfTransparentUnion(Ty); 4487 4488 // Handle illegal vector types here. 4489 if (isIllegalVectorType(Ty)) { 4490 uint64_t Size = getContext().getTypeSize(Ty); 4491 // Android promotes <2 x i8> to i16, not i32 4492 if (isAndroid() && (Size <= 16)) { 4493 llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); 4494 return ABIArgInfo::getDirect(ResType); 4495 } 4496 if (Size <= 32) { 4497 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); 4498 return ABIArgInfo::getDirect(ResType); 4499 } 4500 if (Size == 64) { 4501 llvm::Type *ResType = 4502 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); 4503 return ABIArgInfo::getDirect(ResType); 4504 } 4505 if (Size == 128) { 4506 llvm::Type *ResType = 4507 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); 4508 return ABIArgInfo::getDirect(ResType); 4509 } 4510 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 4511 } 4512 4513 if (!isAggregateTypeForABI(Ty)) { 4514 // Treat an enum type as its underlying type. 4515 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4516 Ty = EnumTy->getDecl()->getIntegerType(); 4517 4518 return (Ty->isPromotableIntegerType() && isDarwinPCS() 4519 ? ABIArgInfo::getExtend() 4520 : ABIArgInfo::getDirect()); 4521 } 4522 4523 // Structures with either a non-trivial destructor or a non-trivial 4524 // copy constructor are always indirect. 4525 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 4526 return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == 4527 CGCXXABI::RAA_DirectInMemory); 4528 } 4529 4530 // Empty records are always ignored on Darwin, but actually passed in C++ mode 4531 // elsewhere for GNU compatibility. 4532 if (isEmptyRecord(getContext(), Ty, true)) { 4533 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) 4534 return ABIArgInfo::getIgnore(); 4535 4536 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 4537 } 4538 4539 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. 4540 const Type *Base = nullptr; 4541 uint64_t Members = 0; 4542 if (isHomogeneousAggregate(Ty, Base, Members)) { 4543 return ABIArgInfo::getDirect( 4544 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); 4545 } 4546 4547 // Aggregates <= 16 bytes are passed directly in registers or on the stack. 4548 uint64_t Size = getContext().getTypeSize(Ty); 4549 if (Size <= 128) { 4550 unsigned Alignment = getContext().getTypeAlign(Ty); 4551 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes 4552 4553 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. 4554 // For aggregates with 16-byte alignment, we use i128. 4555 if (Alignment < 128 && Size == 128) { 4556 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); 4557 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); 4558 } 4559 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); 4560 } 4561 4562 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 4563 } 4564 4565 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { 4566 if (RetTy->isVoidType()) 4567 return ABIArgInfo::getIgnore(); 4568 4569 // Large vector types should be returned via memory. 4570 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) 4571 return getNaturalAlignIndirect(RetTy); 4572 4573 if (!isAggregateTypeForABI(RetTy)) { 4574 // Treat an enum type as its underlying type. 4575 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 4576 RetTy = EnumTy->getDecl()->getIntegerType(); 4577 4578 return (RetTy->isPromotableIntegerType() && isDarwinPCS() 4579 ? ABIArgInfo::getExtend() 4580 : ABIArgInfo::getDirect()); 4581 } 4582 4583 if (isEmptyRecord(getContext(), RetTy, true)) 4584 return ABIArgInfo::getIgnore(); 4585 4586 const Type *Base = nullptr; 4587 uint64_t Members = 0; 4588 if (isHomogeneousAggregate(RetTy, Base, Members)) 4589 // Homogeneous Floating-point Aggregates (HFAs) are returned directly. 4590 return ABIArgInfo::getDirect(); 4591 4592 // Aggregates <= 16 bytes are returned directly in registers or on the stack. 4593 uint64_t Size = getContext().getTypeSize(RetTy); 4594 if (Size <= 128) { 4595 unsigned Alignment = getContext().getTypeAlign(RetTy); 4596 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes 4597 4598 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. 4599 // For aggregates with 16-byte alignment, we use i128. 4600 if (Alignment < 128 && Size == 128) { 4601 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); 4602 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); 4603 } 4604 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); 4605 } 4606 4607 return getNaturalAlignIndirect(RetTy); 4608 } 4609 4610 /// isIllegalVectorType - check whether the vector type is legal for AArch64. 4611 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { 4612 if (const VectorType *VT = Ty->getAs<VectorType>()) { 4613 // Check whether VT is legal. 4614 unsigned NumElements = VT->getNumElements(); 4615 uint64_t Size = getContext().getTypeSize(VT); 4616 // NumElements should be power of 2. 4617 if (!llvm::isPowerOf2_32(NumElements)) 4618 return true; 4619 return Size != 64 && (Size != 128 || NumElements == 1); 4620 } 4621 return false; 4622 } 4623 4624 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 4625 // Homogeneous aggregates for AAPCS64 must have base types of a floating 4626 // point type or a short-vector type. This is the same as the 32-bit ABI, 4627 // but with the difference that any floating-point type is allowed, 4628 // including __fp16. 4629 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 4630 if (BT->isFloatingPoint()) 4631 return true; 4632 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 4633 unsigned VecSize = getContext().getTypeSize(VT); 4634 if (VecSize == 64 || VecSize == 128) 4635 return true; 4636 } 4637 return false; 4638 } 4639 4640 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 4641 uint64_t Members) const { 4642 return Members <= 4; 4643 } 4644 4645 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, 4646 QualType Ty, 4647 CodeGenFunction &CGF) const { 4648 ABIArgInfo AI = classifyArgumentType(Ty); 4649 bool IsIndirect = AI.isIndirect(); 4650 4651 llvm::Type *BaseTy = CGF.ConvertType(Ty); 4652 if (IsIndirect) 4653 BaseTy = llvm::PointerType::getUnqual(BaseTy); 4654 else if (AI.getCoerceToType()) 4655 BaseTy = AI.getCoerceToType(); 4656 4657 unsigned NumRegs = 1; 4658 if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { 4659 BaseTy = ArrTy->getElementType(); 4660 NumRegs = ArrTy->getNumElements(); 4661 } 4662 bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); 4663 4664 // The AArch64 va_list type and handling is specified in the Procedure Call 4665 // Standard, section B.4: 4666 // 4667 // struct { 4668 // void *__stack; 4669 // void *__gr_top; 4670 // void *__vr_top; 4671 // int __gr_offs; 4672 // int __vr_offs; 4673 // }; 4674 4675 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); 4676 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 4677 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); 4678 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 4679 4680 auto TyInfo = getContext().getTypeInfoInChars(Ty); 4681 CharUnits TyAlign = TyInfo.second; 4682 4683 Address reg_offs_p = Address::invalid(); 4684 llvm::Value *reg_offs = nullptr; 4685 int reg_top_index; 4686 CharUnits reg_top_offset; 4687 int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity(); 4688 if (!IsFPR) { 4689 // 3 is the field number of __gr_offs 4690 reg_offs_p = 4691 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), 4692 "gr_offs_p"); 4693 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); 4694 reg_top_index = 1; // field number for __gr_top 4695 reg_top_offset = CharUnits::fromQuantity(8); 4696 RegSize = llvm::alignTo(RegSize, 8); 4697 } else { 4698 // 4 is the field number of __vr_offs. 4699 reg_offs_p = 4700 CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28), 4701 "vr_offs_p"); 4702 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); 4703 reg_top_index = 2; // field number for __vr_top 4704 reg_top_offset = CharUnits::fromQuantity(16); 4705 RegSize = 16 * NumRegs; 4706 } 4707 4708 //======================================= 4709 // Find out where argument was passed 4710 //======================================= 4711 4712 // If reg_offs >= 0 we're already using the stack for this type of 4713 // argument. We don't want to keep updating reg_offs (in case it overflows, 4714 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves 4715 // whatever they get). 4716 llvm::Value *UsingStack = nullptr; 4717 UsingStack = CGF.Builder.CreateICmpSGE( 4718 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); 4719 4720 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); 4721 4722 // Otherwise, at least some kind of argument could go in these registers, the 4723 // question is whether this particular type is too big. 4724 CGF.EmitBlock(MaybeRegBlock); 4725 4726 // Integer arguments may need to correct register alignment (for example a 4727 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we 4728 // align __gr_offs to calculate the potential address. 4729 if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { 4730 int Align = TyAlign.getQuantity(); 4731 4732 reg_offs = CGF.Builder.CreateAdd( 4733 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), 4734 "align_regoffs"); 4735 reg_offs = CGF.Builder.CreateAnd( 4736 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), 4737 "aligned_regoffs"); 4738 } 4739 4740 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. 4741 // The fact that this is done unconditionally reflects the fact that 4742 // allocating an argument to the stack also uses up all the remaining 4743 // registers of the appropriate kind. 4744 llvm::Value *NewOffset = nullptr; 4745 NewOffset = CGF.Builder.CreateAdd( 4746 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); 4747 CGF.Builder.CreateStore(NewOffset, reg_offs_p); 4748 4749 // Now we're in a position to decide whether this argument really was in 4750 // registers or not. 4751 llvm::Value *InRegs = nullptr; 4752 InRegs = CGF.Builder.CreateICmpSLE( 4753 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); 4754 4755 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); 4756 4757 //======================================= 4758 // Argument was in registers 4759 //======================================= 4760 4761 // Now we emit the code for if the argument was originally passed in 4762 // registers. First start the appropriate block: 4763 CGF.EmitBlock(InRegBlock); 4764 4765 llvm::Value *reg_top = nullptr; 4766 Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, 4767 reg_top_offset, "reg_top_p"); 4768 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); 4769 Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), 4770 CharUnits::fromQuantity(IsFPR ? 16 : 8)); 4771 Address RegAddr = Address::invalid(); 4772 llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); 4773 4774 if (IsIndirect) { 4775 // If it's been passed indirectly (actually a struct), whatever we find from 4776 // stored registers or on the stack will actually be a struct **. 4777 MemTy = llvm::PointerType::getUnqual(MemTy); 4778 } 4779 4780 const Type *Base = nullptr; 4781 uint64_t NumMembers = 0; 4782 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); 4783 if (IsHFA && NumMembers > 1) { 4784 // Homogeneous aggregates passed in registers will have their elements split 4785 // and stored 16-bytes apart regardless of size (they're notionally in qN, 4786 // qN+1, ...). We reload and store into a temporary local variable 4787 // contiguously. 4788 assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); 4789 auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); 4790 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); 4791 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); 4792 Address Tmp = CGF.CreateTempAlloca(HFATy, 4793 std::max(TyAlign, BaseTyInfo.second)); 4794 4795 // On big-endian platforms, the value will be right-aligned in its slot. 4796 int Offset = 0; 4797 if (CGF.CGM.getDataLayout().isBigEndian() && 4798 BaseTyInfo.first.getQuantity() < 16) 4799 Offset = 16 - BaseTyInfo.first.getQuantity(); 4800 4801 for (unsigned i = 0; i < NumMembers; ++i) { 4802 CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); 4803 Address LoadAddr = 4804 CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); 4805 LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); 4806 4807 Address StoreAddr = 4808 CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first); 4809 4810 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); 4811 CGF.Builder.CreateStore(Elem, StoreAddr); 4812 } 4813 4814 RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); 4815 } else { 4816 // Otherwise the object is contiguous in memory. 4817 4818 // It might be right-aligned in its slot. 4819 CharUnits SlotSize = BaseAddr.getAlignment(); 4820 if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && 4821 (IsHFA || !isAggregateTypeForABI(Ty)) && 4822 TyInfo.first < SlotSize) { 4823 CharUnits Offset = SlotSize - TyInfo.first; 4824 BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); 4825 } 4826 4827 RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); 4828 } 4829 4830 CGF.EmitBranch(ContBlock); 4831 4832 //======================================= 4833 // Argument was on the stack 4834 //======================================= 4835 CGF.EmitBlock(OnStackBlock); 4836 4837 Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, 4838 CharUnits::Zero(), "stack_p"); 4839 llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); 4840 4841 // Again, stack arguments may need realignment. In this case both integer and 4842 // floating-point ones might be affected. 4843 if (!IsIndirect && TyAlign.getQuantity() > 8) { 4844 int Align = TyAlign.getQuantity(); 4845 4846 OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); 4847 4848 OnStackPtr = CGF.Builder.CreateAdd( 4849 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), 4850 "align_stack"); 4851 OnStackPtr = CGF.Builder.CreateAnd( 4852 OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), 4853 "align_stack"); 4854 4855 OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); 4856 } 4857 Address OnStackAddr(OnStackPtr, 4858 std::max(CharUnits::fromQuantity(8), TyAlign)); 4859 4860 // All stack slots are multiples of 8 bytes. 4861 CharUnits StackSlotSize = CharUnits::fromQuantity(8); 4862 CharUnits StackSize; 4863 if (IsIndirect) 4864 StackSize = StackSlotSize; 4865 else 4866 StackSize = TyInfo.first.alignTo(StackSlotSize); 4867 4868 llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); 4869 llvm::Value *NewStack = 4870 CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); 4871 4872 // Write the new value of __stack for the next call to va_arg 4873 CGF.Builder.CreateStore(NewStack, stack_p); 4874 4875 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && 4876 TyInfo.first < StackSlotSize) { 4877 CharUnits Offset = StackSlotSize - TyInfo.first; 4878 OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); 4879 } 4880 4881 OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); 4882 4883 CGF.EmitBranch(ContBlock); 4884 4885 //======================================= 4886 // Tidy up 4887 //======================================= 4888 CGF.EmitBlock(ContBlock); 4889 4890 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, 4891 OnStackAddr, OnStackBlock, "vaargs.addr"); 4892 4893 if (IsIndirect) 4894 return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), 4895 TyInfo.second); 4896 4897 return ResAddr; 4898 } 4899 4900 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, 4901 CodeGenFunction &CGF) const { 4902 // The backend's lowering doesn't support va_arg for aggregates or 4903 // illegal vector types. Lower VAArg here for these cases and use 4904 // the LLVM va_arg instruction for everything else. 4905 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) 4906 return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); 4907 4908 CharUnits SlotSize = CharUnits::fromQuantity(8); 4909 4910 // Empty records are ignored for parameter passing purposes. 4911 if (isEmptyRecord(getContext(), Ty, true)) { 4912 Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); 4913 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 4914 return Addr; 4915 } 4916 4917 // The size of the actual thing passed, which might end up just 4918 // being a pointer for indirect types. 4919 auto TyInfo = getContext().getTypeInfoInChars(Ty); 4920 4921 // Arguments bigger than 16 bytes which aren't homogeneous 4922 // aggregates should be passed indirectly. 4923 bool IsIndirect = false; 4924 if (TyInfo.first.getQuantity() > 16) { 4925 const Type *Base = nullptr; 4926 uint64_t Members = 0; 4927 IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); 4928 } 4929 4930 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, 4931 TyInfo, SlotSize, /*AllowHigherAlign*/ true); 4932 } 4933 4934 //===----------------------------------------------------------------------===// 4935 // ARM ABI Implementation 4936 //===----------------------------------------------------------------------===// 4937 4938 namespace { 4939 4940 class ARMABIInfo : public SwiftABIInfo { 4941 public: 4942 enum ABIKind { 4943 APCS = 0, 4944 AAPCS = 1, 4945 AAPCS_VFP = 2, 4946 AAPCS16_VFP = 3, 4947 }; 4948 4949 private: 4950 ABIKind Kind; 4951 4952 public: 4953 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) 4954 : SwiftABIInfo(CGT), Kind(_Kind) { 4955 setCCs(); 4956 } 4957 4958 bool isEABI() const { 4959 switch (getTarget().getTriple().getEnvironment()) { 4960 case llvm::Triple::Android: 4961 case llvm::Triple::EABI: 4962 case llvm::Triple::EABIHF: 4963 case llvm::Triple::GNUEABI: 4964 case llvm::Triple::GNUEABIHF: 4965 return true; 4966 default: 4967 return false; 4968 } 4969 } 4970 4971 bool isEABIHF() const { 4972 switch (getTarget().getTriple().getEnvironment()) { 4973 case llvm::Triple::EABIHF: 4974 case llvm::Triple::GNUEABIHF: 4975 return true; 4976 default: 4977 return false; 4978 } 4979 } 4980 4981 ABIKind getABIKind() const { return Kind; } 4982 4983 private: 4984 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; 4985 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; 4986 bool isIllegalVectorType(QualType Ty) const; 4987 4988 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 4989 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 4990 uint64_t Members) const override; 4991 4992 void computeInfo(CGFunctionInfo &FI) const override; 4993 4994 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 4995 QualType Ty) const override; 4996 4997 llvm::CallingConv::ID getLLVMDefaultCC() const; 4998 llvm::CallingConv::ID getABIDefaultCC() const; 4999 void setCCs(); 5000 5001 bool shouldPassIndirectlyForSwift(CharUnits totalSize, 5002 ArrayRef<llvm::Type*> scalars, 5003 bool asReturnValue) const override { 5004 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 5005 } 5006 }; 5007 5008 class ARMTargetCodeGenInfo : public TargetCodeGenInfo { 5009 public: 5010 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 5011 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} 5012 5013 const ARMABIInfo &getABIInfo() const { 5014 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); 5015 } 5016 5017 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 5018 return 13; 5019 } 5020 5021 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 5022 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; 5023 } 5024 5025 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 5026 llvm::Value *Address) const override { 5027 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 5028 5029 // 0-15 are the 16 integer registers. 5030 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); 5031 return false; 5032 } 5033 5034 unsigned getSizeOfUnwindException() const override { 5035 if (getABIInfo().isEABI()) return 88; 5036 return TargetCodeGenInfo::getSizeOfUnwindException(); 5037 } 5038 5039 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5040 CodeGen::CodeGenModule &CGM) const override { 5041 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 5042 if (!FD) 5043 return; 5044 5045 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); 5046 if (!Attr) 5047 return; 5048 5049 const char *Kind; 5050 switch (Attr->getInterrupt()) { 5051 case ARMInterruptAttr::Generic: Kind = ""; break; 5052 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; 5053 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; 5054 case ARMInterruptAttr::SWI: Kind = "SWI"; break; 5055 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; 5056 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; 5057 } 5058 5059 llvm::Function *Fn = cast<llvm::Function>(GV); 5060 5061 Fn->addFnAttr("interrupt", Kind); 5062 5063 ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); 5064 if (ABI == ARMABIInfo::APCS) 5065 return; 5066 5067 // AAPCS guarantees that sp will be 8-byte aligned on any public interface, 5068 // however this is not necessarily true on taking any interrupt. Instruct 5069 // the backend to perform a realignment as part of the function prologue. 5070 llvm::AttrBuilder B; 5071 B.addStackAlignmentAttr(8); 5072 Fn->addAttributes(llvm::AttributeSet::FunctionIndex, 5073 llvm::AttributeSet::get(CGM.getLLVMContext(), 5074 llvm::AttributeSet::FunctionIndex, 5075 B)); 5076 } 5077 }; 5078 5079 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { 5080 public: 5081 WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 5082 : ARMTargetCodeGenInfo(CGT, K) {} 5083 5084 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5085 CodeGen::CodeGenModule &CGM) const override; 5086 }; 5087 5088 void WindowsARMTargetCodeGenInfo::setTargetAttributes( 5089 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { 5090 ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); 5091 addStackProbeSizeTargetAttribute(D, GV, CGM); 5092 } 5093 } 5094 5095 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 5096 if (!getCXXABI().classifyReturnType(FI)) 5097 FI.getReturnInfo() = 5098 classifyReturnType(FI.getReturnType(), FI.isVariadic()); 5099 5100 for (auto &I : FI.arguments()) 5101 I.info = classifyArgumentType(I.type, FI.isVariadic()); 5102 5103 // Always honor user-specified calling convention. 5104 if (FI.getCallingConvention() != llvm::CallingConv::C) 5105 return; 5106 5107 llvm::CallingConv::ID cc = getRuntimeCC(); 5108 if (cc != llvm::CallingConv::C) 5109 FI.setEffectiveCallingConvention(cc); 5110 } 5111 5112 /// Return the default calling convention that LLVM will use. 5113 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { 5114 // The default calling convention that LLVM will infer. 5115 if (isEABIHF() || getTarget().getTriple().isWatchABI()) 5116 return llvm::CallingConv::ARM_AAPCS_VFP; 5117 else if (isEABI()) 5118 return llvm::CallingConv::ARM_AAPCS; 5119 else 5120 return llvm::CallingConv::ARM_APCS; 5121 } 5122 5123 /// Return the calling convention that our ABI would like us to use 5124 /// as the C calling convention. 5125 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { 5126 switch (getABIKind()) { 5127 case APCS: return llvm::CallingConv::ARM_APCS; 5128 case AAPCS: return llvm::CallingConv::ARM_AAPCS; 5129 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 5130 case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 5131 } 5132 llvm_unreachable("bad ABI kind"); 5133 } 5134 5135 void ARMABIInfo::setCCs() { 5136 assert(getRuntimeCC() == llvm::CallingConv::C); 5137 5138 // Don't muddy up the IR with a ton of explicit annotations if 5139 // they'd just match what LLVM will infer from the triple. 5140 llvm::CallingConv::ID abiCC = getABIDefaultCC(); 5141 if (abiCC != getLLVMDefaultCC()) 5142 RuntimeCC = abiCC; 5143 5144 // AAPCS apparently requires runtime support functions to be soft-float, but 5145 // that's almost certainly for historic reasons (Thumb1 not supporting VFP 5146 // most likely). It's more convenient for AAPCS16_VFP to be hard-float. 5147 switch (getABIKind()) { 5148 case APCS: 5149 case AAPCS16_VFP: 5150 if (abiCC != getLLVMDefaultCC()) 5151 BuiltinCC = abiCC; 5152 break; 5153 case AAPCS: 5154 case AAPCS_VFP: 5155 BuiltinCC = llvm::CallingConv::ARM_AAPCS; 5156 break; 5157 } 5158 } 5159 5160 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, 5161 bool isVariadic) const { 5162 // 6.1.2.1 The following argument types are VFP CPRCs: 5163 // A single-precision floating-point type (including promoted 5164 // half-precision types); A double-precision floating-point type; 5165 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate 5166 // with a Base Type of a single- or double-precision floating-point type, 5167 // 64-bit containerized vectors or 128-bit containerized vectors with one 5168 // to four Elements. 5169 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; 5170 5171 Ty = useFirstFieldIfTransparentUnion(Ty); 5172 5173 // Handle illegal vector types here. 5174 if (isIllegalVectorType(Ty)) { 5175 uint64_t Size = getContext().getTypeSize(Ty); 5176 if (Size <= 32) { 5177 llvm::Type *ResType = 5178 llvm::Type::getInt32Ty(getVMContext()); 5179 return ABIArgInfo::getDirect(ResType); 5180 } 5181 if (Size == 64) { 5182 llvm::Type *ResType = llvm::VectorType::get( 5183 llvm::Type::getInt32Ty(getVMContext()), 2); 5184 return ABIArgInfo::getDirect(ResType); 5185 } 5186 if (Size == 128) { 5187 llvm::Type *ResType = llvm::VectorType::get( 5188 llvm::Type::getInt32Ty(getVMContext()), 4); 5189 return ABIArgInfo::getDirect(ResType); 5190 } 5191 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 5192 } 5193 5194 // __fp16 gets passed as if it were an int or float, but with the top 16 bits 5195 // unspecified. This is not done for OpenCL as it handles the half type 5196 // natively, and does not need to interwork with AAPCS code. 5197 if (Ty->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { 5198 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? 5199 llvm::Type::getFloatTy(getVMContext()) : 5200 llvm::Type::getInt32Ty(getVMContext()); 5201 return ABIArgInfo::getDirect(ResType); 5202 } 5203 5204 if (!isAggregateTypeForABI(Ty)) { 5205 // Treat an enum type as its underlying type. 5206 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 5207 Ty = EnumTy->getDecl()->getIntegerType(); 5208 } 5209 5210 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() 5211 : ABIArgInfo::getDirect()); 5212 } 5213 5214 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 5215 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 5216 } 5217 5218 // Ignore empty records. 5219 if (isEmptyRecord(getContext(), Ty, true)) 5220 return ABIArgInfo::getIgnore(); 5221 5222 if (IsEffectivelyAAPCS_VFP) { 5223 // Homogeneous Aggregates need to be expanded when we can fit the aggregate 5224 // into VFP registers. 5225 const Type *Base = nullptr; 5226 uint64_t Members = 0; 5227 if (isHomogeneousAggregate(Ty, Base, Members)) { 5228 assert(Base && "Base class should be set for homogeneous aggregate"); 5229 // Base can be a floating-point or a vector. 5230 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); 5231 } 5232 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { 5233 // WatchOS does have homogeneous aggregates. Note that we intentionally use 5234 // this convention even for a variadic function: the backend will use GPRs 5235 // if needed. 5236 const Type *Base = nullptr; 5237 uint64_t Members = 0; 5238 if (isHomogeneousAggregate(Ty, Base, Members)) { 5239 assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); 5240 llvm::Type *Ty = 5241 llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); 5242 return ABIArgInfo::getDirect(Ty, 0, nullptr, false); 5243 } 5244 } 5245 5246 if (getABIKind() == ARMABIInfo::AAPCS16_VFP && 5247 getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { 5248 // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're 5249 // bigger than 128-bits, they get placed in space allocated by the caller, 5250 // and a pointer is passed. 5251 return ABIArgInfo::getIndirect( 5252 CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); 5253 } 5254 5255 // Support byval for ARM. 5256 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at 5257 // most 8-byte. We realign the indirect argument if type alignment is bigger 5258 // than ABI alignment. 5259 uint64_t ABIAlign = 4; 5260 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; 5261 if (getABIKind() == ARMABIInfo::AAPCS_VFP || 5262 getABIKind() == ARMABIInfo::AAPCS) 5263 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); 5264 5265 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { 5266 assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); 5267 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), 5268 /*ByVal=*/true, 5269 /*Realign=*/TyAlign > ABIAlign); 5270 } 5271 5272 // Otherwise, pass by coercing to a structure of the appropriate size. 5273 llvm::Type* ElemTy; 5274 unsigned SizeRegs; 5275 // FIXME: Try to match the types of the arguments more accurately where 5276 // we can. 5277 if (getContext().getTypeAlign(Ty) <= 32) { 5278 ElemTy = llvm::Type::getInt32Ty(getVMContext()); 5279 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; 5280 } else { 5281 ElemTy = llvm::Type::getInt64Ty(getVMContext()); 5282 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; 5283 } 5284 5285 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); 5286 } 5287 5288 static bool isIntegerLikeType(QualType Ty, ASTContext &Context, 5289 llvm::LLVMContext &VMContext) { 5290 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure 5291 // is called integer-like if its size is less than or equal to one word, and 5292 // the offset of each of its addressable sub-fields is zero. 5293 5294 uint64_t Size = Context.getTypeSize(Ty); 5295 5296 // Check that the type fits in a word. 5297 if (Size > 32) 5298 return false; 5299 5300 // FIXME: Handle vector types! 5301 if (Ty->isVectorType()) 5302 return false; 5303 5304 // Float types are never treated as "integer like". 5305 if (Ty->isRealFloatingType()) 5306 return false; 5307 5308 // If this is a builtin or pointer type then it is ok. 5309 if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) 5310 return true; 5311 5312 // Small complex integer types are "integer like". 5313 if (const ComplexType *CT = Ty->getAs<ComplexType>()) 5314 return isIntegerLikeType(CT->getElementType(), Context, VMContext); 5315 5316 // Single element and zero sized arrays should be allowed, by the definition 5317 // above, but they are not. 5318 5319 // Otherwise, it must be a record type. 5320 const RecordType *RT = Ty->getAs<RecordType>(); 5321 if (!RT) return false; 5322 5323 // Ignore records with flexible arrays. 5324 const RecordDecl *RD = RT->getDecl(); 5325 if (RD->hasFlexibleArrayMember()) 5326 return false; 5327 5328 // Check that all sub-fields are at offset 0, and are themselves "integer 5329 // like". 5330 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 5331 5332 bool HadField = false; 5333 unsigned idx = 0; 5334 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 5335 i != e; ++i, ++idx) { 5336 const FieldDecl *FD = *i; 5337 5338 // Bit-fields are not addressable, we only need to verify they are "integer 5339 // like". We still have to disallow a subsequent non-bitfield, for example: 5340 // struct { int : 0; int x } 5341 // is non-integer like according to gcc. 5342 if (FD->isBitField()) { 5343 if (!RD->isUnion()) 5344 HadField = true; 5345 5346 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 5347 return false; 5348 5349 continue; 5350 } 5351 5352 // Check if this field is at offset 0. 5353 if (Layout.getFieldOffset(idx) != 0) 5354 return false; 5355 5356 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 5357 return false; 5358 5359 // Only allow at most one field in a structure. This doesn't match the 5360 // wording above, but follows gcc in situations with a field following an 5361 // empty structure. 5362 if (!RD->isUnion()) { 5363 if (HadField) 5364 return false; 5365 5366 HadField = true; 5367 } 5368 } 5369 5370 return true; 5371 } 5372 5373 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, 5374 bool isVariadic) const { 5375 bool IsEffectivelyAAPCS_VFP = 5376 (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic; 5377 5378 if (RetTy->isVoidType()) 5379 return ABIArgInfo::getIgnore(); 5380 5381 // Large vector types should be returned via memory. 5382 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { 5383 return getNaturalAlignIndirect(RetTy); 5384 } 5385 5386 // __fp16 gets returned as if it were an int or float, but with the top 16 5387 // bits unspecified. This is not done for OpenCL as it handles the half type 5388 // natively, and does not need to interwork with AAPCS code. 5389 if (RetTy->isHalfType() && !getContext().getLangOpts().NativeHalfArgsAndReturns) { 5390 llvm::Type *ResType = IsEffectivelyAAPCS_VFP ? 5391 llvm::Type::getFloatTy(getVMContext()) : 5392 llvm::Type::getInt32Ty(getVMContext()); 5393 return ABIArgInfo::getDirect(ResType); 5394 } 5395 5396 if (!isAggregateTypeForABI(RetTy)) { 5397 // Treat an enum type as its underlying type. 5398 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 5399 RetTy = EnumTy->getDecl()->getIntegerType(); 5400 5401 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() 5402 : ABIArgInfo::getDirect(); 5403 } 5404 5405 // Are we following APCS? 5406 if (getABIKind() == APCS) { 5407 if (isEmptyRecord(getContext(), RetTy, false)) 5408 return ABIArgInfo::getIgnore(); 5409 5410 // Complex types are all returned as packed integers. 5411 // 5412 // FIXME: Consider using 2 x vector types if the back end handles them 5413 // correctly. 5414 if (RetTy->isAnyComplexType()) 5415 return ABIArgInfo::getDirect(llvm::IntegerType::get( 5416 getVMContext(), getContext().getTypeSize(RetTy))); 5417 5418 // Integer like structures are returned in r0. 5419 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { 5420 // Return in the smallest viable integer type. 5421 uint64_t Size = getContext().getTypeSize(RetTy); 5422 if (Size <= 8) 5423 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 5424 if (Size <= 16) 5425 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 5426 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 5427 } 5428 5429 // Otherwise return in memory. 5430 return getNaturalAlignIndirect(RetTy); 5431 } 5432 5433 // Otherwise this is an AAPCS variant. 5434 5435 if (isEmptyRecord(getContext(), RetTy, true)) 5436 return ABIArgInfo::getIgnore(); 5437 5438 // Check for homogeneous aggregates with AAPCS-VFP. 5439 if (IsEffectivelyAAPCS_VFP) { 5440 const Type *Base = nullptr; 5441 uint64_t Members = 0; 5442 if (isHomogeneousAggregate(RetTy, Base, Members)) { 5443 assert(Base && "Base class should be set for homogeneous aggregate"); 5444 // Homogeneous Aggregates are returned directly. 5445 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); 5446 } 5447 } 5448 5449 // Aggregates <= 4 bytes are returned in r0; other aggregates 5450 // are returned indirectly. 5451 uint64_t Size = getContext().getTypeSize(RetTy); 5452 if (Size <= 32) { 5453 if (getDataLayout().isBigEndian()) 5454 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) 5455 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 5456 5457 // Return in the smallest viable integer type. 5458 if (Size <= 8) 5459 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 5460 if (Size <= 16) 5461 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 5462 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 5463 } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { 5464 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); 5465 llvm::Type *CoerceTy = 5466 llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); 5467 return ABIArgInfo::getDirect(CoerceTy); 5468 } 5469 5470 return getNaturalAlignIndirect(RetTy); 5471 } 5472 5473 /// isIllegalVector - check whether Ty is an illegal vector type. 5474 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { 5475 if (const VectorType *VT = Ty->getAs<VectorType> ()) { 5476 if (isAndroid()) { 5477 // Android shipped using Clang 3.1, which supported a slightly different 5478 // vector ABI. The primary differences were that 3-element vector types 5479 // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path 5480 // accepts that legacy behavior for Android only. 5481 // Check whether VT is legal. 5482 unsigned NumElements = VT->getNumElements(); 5483 // NumElements should be power of 2 or equal to 3. 5484 if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) 5485 return true; 5486 } else { 5487 // Check whether VT is legal. 5488 unsigned NumElements = VT->getNumElements(); 5489 uint64_t Size = getContext().getTypeSize(VT); 5490 // NumElements should be power of 2. 5491 if (!llvm::isPowerOf2_32(NumElements)) 5492 return true; 5493 // Size should be greater than 32 bits. 5494 return Size <= 32; 5495 } 5496 } 5497 return false; 5498 } 5499 5500 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 5501 // Homogeneous aggregates for AAPCS-VFP must have base types of float, 5502 // double, or 64-bit or 128-bit vectors. 5503 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 5504 if (BT->getKind() == BuiltinType::Float || 5505 BT->getKind() == BuiltinType::Double || 5506 BT->getKind() == BuiltinType::LongDouble) 5507 return true; 5508 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 5509 unsigned VecSize = getContext().getTypeSize(VT); 5510 if (VecSize == 64 || VecSize == 128) 5511 return true; 5512 } 5513 return false; 5514 } 5515 5516 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 5517 uint64_t Members) const { 5518 return Members <= 4; 5519 } 5520 5521 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5522 QualType Ty) const { 5523 CharUnits SlotSize = CharUnits::fromQuantity(4); 5524 5525 // Empty records are ignored for parameter passing purposes. 5526 if (isEmptyRecord(getContext(), Ty, true)) { 5527 Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); 5528 Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); 5529 return Addr; 5530 } 5531 5532 auto TyInfo = getContext().getTypeInfoInChars(Ty); 5533 CharUnits TyAlignForABI = TyInfo.second; 5534 5535 // Use indirect if size of the illegal vector is bigger than 16 bytes. 5536 bool IsIndirect = false; 5537 const Type *Base = nullptr; 5538 uint64_t Members = 0; 5539 if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { 5540 IsIndirect = true; 5541 5542 // ARMv7k passes structs bigger than 16 bytes indirectly, in space 5543 // allocated by the caller. 5544 } else if (TyInfo.first > CharUnits::fromQuantity(16) && 5545 getABIKind() == ARMABIInfo::AAPCS16_VFP && 5546 !isHomogeneousAggregate(Ty, Base, Members)) { 5547 IsIndirect = true; 5548 5549 // Otherwise, bound the type's ABI alignment. 5550 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for 5551 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. 5552 // Our callers should be prepared to handle an under-aligned address. 5553 } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || 5554 getABIKind() == ARMABIInfo::AAPCS) { 5555 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); 5556 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); 5557 } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { 5558 // ARMv7k allows type alignment up to 16 bytes. 5559 TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); 5560 TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); 5561 } else { 5562 TyAlignForABI = CharUnits::fromQuantity(4); 5563 } 5564 TyInfo.second = TyAlignForABI; 5565 5566 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, 5567 SlotSize, /*AllowHigherAlign*/ true); 5568 } 5569 5570 //===----------------------------------------------------------------------===// 5571 // NVPTX ABI Implementation 5572 //===----------------------------------------------------------------------===// 5573 5574 namespace { 5575 5576 class NVPTXABIInfo : public ABIInfo { 5577 public: 5578 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 5579 5580 ABIArgInfo classifyReturnType(QualType RetTy) const; 5581 ABIArgInfo classifyArgumentType(QualType Ty) const; 5582 5583 void computeInfo(CGFunctionInfo &FI) const override; 5584 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5585 QualType Ty) const override; 5586 }; 5587 5588 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { 5589 public: 5590 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) 5591 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} 5592 5593 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5594 CodeGen::CodeGenModule &M) const override; 5595 private: 5596 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the 5597 // resulting MDNode to the nvvm.annotations MDNode. 5598 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); 5599 }; 5600 5601 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { 5602 if (RetTy->isVoidType()) 5603 return ABIArgInfo::getIgnore(); 5604 5605 // note: this is different from default ABI 5606 if (!RetTy->isScalarType()) 5607 return ABIArgInfo::getDirect(); 5608 5609 // Treat an enum type as its underlying type. 5610 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 5611 RetTy = EnumTy->getDecl()->getIntegerType(); 5612 5613 return (RetTy->isPromotableIntegerType() ? 5614 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 5615 } 5616 5617 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { 5618 // Treat an enum type as its underlying type. 5619 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 5620 Ty = EnumTy->getDecl()->getIntegerType(); 5621 5622 // Return aggregates type as indirect by value 5623 if (isAggregateTypeForABI(Ty)) 5624 return getNaturalAlignIndirect(Ty, /* byval */ true); 5625 5626 return (Ty->isPromotableIntegerType() ? 5627 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 5628 } 5629 5630 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { 5631 if (!getCXXABI().classifyReturnType(FI)) 5632 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 5633 for (auto &I : FI.arguments()) 5634 I.info = classifyArgumentType(I.type); 5635 5636 // Always honor user-specified calling convention. 5637 if (FI.getCallingConvention() != llvm::CallingConv::C) 5638 return; 5639 5640 FI.setEffectiveCallingConvention(getRuntimeCC()); 5641 } 5642 5643 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5644 QualType Ty) const { 5645 llvm_unreachable("NVPTX does not support varargs"); 5646 } 5647 5648 void NVPTXTargetCodeGenInfo:: 5649 setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 5650 CodeGen::CodeGenModule &M) const{ 5651 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 5652 if (!FD) return; 5653 5654 llvm::Function *F = cast<llvm::Function>(GV); 5655 5656 // Perform special handling in OpenCL mode 5657 if (M.getLangOpts().OpenCL) { 5658 // Use OpenCL function attributes to check for kernel functions 5659 // By default, all functions are device functions 5660 if (FD->hasAttr<OpenCLKernelAttr>()) { 5661 // OpenCL __kernel functions get kernel metadata 5662 // Create !{<func-ref>, metadata !"kernel", i32 1} node 5663 addNVVMMetadata(F, "kernel", 1); 5664 // And kernel functions are not subject to inlining 5665 F->addFnAttr(llvm::Attribute::NoInline); 5666 } 5667 } 5668 5669 // Perform special handling in CUDA mode. 5670 if (M.getLangOpts().CUDA) { 5671 // CUDA __global__ functions get a kernel metadata entry. Since 5672 // __global__ functions cannot be called from the device, we do not 5673 // need to set the noinline attribute. 5674 if (FD->hasAttr<CUDAGlobalAttr>()) { 5675 // Create !{<func-ref>, metadata !"kernel", i32 1} node 5676 addNVVMMetadata(F, "kernel", 1); 5677 } 5678 if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { 5679 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node 5680 llvm::APSInt MaxThreads(32); 5681 MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); 5682 if (MaxThreads > 0) 5683 addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); 5684 5685 // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was 5686 // not specified in __launch_bounds__ or if the user specified a 0 value, 5687 // we don't have to add a PTX directive. 5688 if (Attr->getMinBlocks()) { 5689 llvm::APSInt MinBlocks(32); 5690 MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); 5691 if (MinBlocks > 0) 5692 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node 5693 addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); 5694 } 5695 } 5696 } 5697 } 5698 5699 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, 5700 int Operand) { 5701 llvm::Module *M = F->getParent(); 5702 llvm::LLVMContext &Ctx = M->getContext(); 5703 5704 // Get "nvvm.annotations" metadata node 5705 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); 5706 5707 llvm::Metadata *MDVals[] = { 5708 llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), 5709 llvm::ConstantAsMetadata::get( 5710 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; 5711 // Append metadata to nvvm.annotations 5712 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 5713 } 5714 } 5715 5716 //===----------------------------------------------------------------------===// 5717 // SystemZ ABI Implementation 5718 //===----------------------------------------------------------------------===// 5719 5720 namespace { 5721 5722 class SystemZABIInfo : public SwiftABIInfo { 5723 bool HasVector; 5724 5725 public: 5726 SystemZABIInfo(CodeGenTypes &CGT, bool HV) 5727 : SwiftABIInfo(CGT), HasVector(HV) {} 5728 5729 bool isPromotableIntegerType(QualType Ty) const; 5730 bool isCompoundType(QualType Ty) const; 5731 bool isVectorArgumentType(QualType Ty) const; 5732 bool isFPArgumentType(QualType Ty) const; 5733 QualType GetSingleElementType(QualType Ty) const; 5734 5735 ABIArgInfo classifyReturnType(QualType RetTy) const; 5736 ABIArgInfo classifyArgumentType(QualType ArgTy) const; 5737 5738 void computeInfo(CGFunctionInfo &FI) const override { 5739 if (!getCXXABI().classifyReturnType(FI)) 5740 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 5741 for (auto &I : FI.arguments()) 5742 I.info = classifyArgumentType(I.type); 5743 } 5744 5745 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5746 QualType Ty) const override; 5747 5748 bool shouldPassIndirectlyForSwift(CharUnits totalSize, 5749 ArrayRef<llvm::Type*> scalars, 5750 bool asReturnValue) const override { 5751 return occupiesMoreThan(CGT, scalars, /*total*/ 4); 5752 } 5753 }; 5754 5755 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { 5756 public: 5757 SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) 5758 : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} 5759 }; 5760 5761 } 5762 5763 bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { 5764 // Treat an enum type as its underlying type. 5765 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 5766 Ty = EnumTy->getDecl()->getIntegerType(); 5767 5768 // Promotable integer types are required to be promoted by the ABI. 5769 if (Ty->isPromotableIntegerType()) 5770 return true; 5771 5772 // 32-bit values must also be promoted. 5773 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 5774 switch (BT->getKind()) { 5775 case BuiltinType::Int: 5776 case BuiltinType::UInt: 5777 return true; 5778 default: 5779 return false; 5780 } 5781 return false; 5782 } 5783 5784 bool SystemZABIInfo::isCompoundType(QualType Ty) const { 5785 return (Ty->isAnyComplexType() || 5786 Ty->isVectorType() || 5787 isAggregateTypeForABI(Ty)); 5788 } 5789 5790 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { 5791 return (HasVector && 5792 Ty->isVectorType() && 5793 getContext().getTypeSize(Ty) <= 128); 5794 } 5795 5796 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { 5797 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 5798 switch (BT->getKind()) { 5799 case BuiltinType::Float: 5800 case BuiltinType::Double: 5801 return true; 5802 default: 5803 return false; 5804 } 5805 5806 return false; 5807 } 5808 5809 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { 5810 if (const RecordType *RT = Ty->getAsStructureType()) { 5811 const RecordDecl *RD = RT->getDecl(); 5812 QualType Found; 5813 5814 // If this is a C++ record, check the bases first. 5815 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 5816 for (const auto &I : CXXRD->bases()) { 5817 QualType Base = I.getType(); 5818 5819 // Empty bases don't affect things either way. 5820 if (isEmptyRecord(getContext(), Base, true)) 5821 continue; 5822 5823 if (!Found.isNull()) 5824 return Ty; 5825 Found = GetSingleElementType(Base); 5826 } 5827 5828 // Check the fields. 5829 for (const auto *FD : RD->fields()) { 5830 // For compatibility with GCC, ignore empty bitfields in C++ mode. 5831 // Unlike isSingleElementStruct(), empty structure and array fields 5832 // do count. So do anonymous bitfields that aren't zero-sized. 5833 if (getContext().getLangOpts().CPlusPlus && 5834 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) 5835 continue; 5836 5837 // Unlike isSingleElementStruct(), arrays do not count. 5838 // Nested structures still do though. 5839 if (!Found.isNull()) 5840 return Ty; 5841 Found = GetSingleElementType(FD->getType()); 5842 } 5843 5844 // Unlike isSingleElementStruct(), trailing padding is allowed. 5845 // An 8-byte aligned struct s { float f; } is passed as a double. 5846 if (!Found.isNull()) 5847 return Found; 5848 } 5849 5850 return Ty; 5851 } 5852 5853 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 5854 QualType Ty) const { 5855 // Assume that va_list type is correct; should be pointer to LLVM type: 5856 // struct { 5857 // i64 __gpr; 5858 // i64 __fpr; 5859 // i8 *__overflow_arg_area; 5860 // i8 *__reg_save_area; 5861 // }; 5862 5863 // Every non-vector argument occupies 8 bytes and is passed by preference 5864 // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are 5865 // always passed on the stack. 5866 Ty = getContext().getCanonicalType(Ty); 5867 auto TyInfo = getContext().getTypeInfoInChars(Ty); 5868 llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); 5869 llvm::Type *DirectTy = ArgTy; 5870 ABIArgInfo AI = classifyArgumentType(Ty); 5871 bool IsIndirect = AI.isIndirect(); 5872 bool InFPRs = false; 5873 bool IsVector = false; 5874 CharUnits UnpaddedSize; 5875 CharUnits DirectAlign; 5876 if (IsIndirect) { 5877 DirectTy = llvm::PointerType::getUnqual(DirectTy); 5878 UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); 5879 } else { 5880 if (AI.getCoerceToType()) 5881 ArgTy = AI.getCoerceToType(); 5882 InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); 5883 IsVector = ArgTy->isVectorTy(); 5884 UnpaddedSize = TyInfo.first; 5885 DirectAlign = TyInfo.second; 5886 } 5887 CharUnits PaddedSize = CharUnits::fromQuantity(8); 5888 if (IsVector && UnpaddedSize > PaddedSize) 5889 PaddedSize = CharUnits::fromQuantity(16); 5890 assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); 5891 5892 CharUnits Padding = (PaddedSize - UnpaddedSize); 5893 5894 llvm::Type *IndexTy = CGF.Int64Ty; 5895 llvm::Value *PaddedSizeV = 5896 llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); 5897 5898 if (IsVector) { 5899 // Work out the address of a vector argument on the stack. 5900 // Vector arguments are always passed in the high bits of a 5901 // single (8 byte) or double (16 byte) stack slot. 5902 Address OverflowArgAreaPtr = 5903 CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16), 5904 "overflow_arg_area_ptr"); 5905 Address OverflowArgArea = 5906 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), 5907 TyInfo.second); 5908 Address MemAddr = 5909 CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); 5910 5911 // Update overflow_arg_area_ptr pointer 5912 llvm::Value *NewOverflowArgArea = 5913 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, 5914 "overflow_arg_area"); 5915 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 5916 5917 return MemAddr; 5918 } 5919 5920 assert(PaddedSize.getQuantity() == 8); 5921 5922 unsigned MaxRegs, RegCountField, RegSaveIndex; 5923 CharUnits RegPadding; 5924 if (InFPRs) { 5925 MaxRegs = 4; // Maximum of 4 FPR arguments 5926 RegCountField = 1; // __fpr 5927 RegSaveIndex = 16; // save offset for f0 5928 RegPadding = CharUnits(); // floats are passed in the high bits of an FPR 5929 } else { 5930 MaxRegs = 5; // Maximum of 5 GPR arguments 5931 RegCountField = 0; // __gpr 5932 RegSaveIndex = 2; // save offset for r2 5933 RegPadding = Padding; // values are passed in the low bits of a GPR 5934 } 5935 5936 Address RegCountPtr = CGF.Builder.CreateStructGEP( 5937 VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8), 5938 "reg_count_ptr"); 5939 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); 5940 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); 5941 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, 5942 "fits_in_regs"); 5943 5944 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 5945 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 5946 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 5947 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 5948 5949 // Emit code to load the value if it was passed in registers. 5950 CGF.EmitBlock(InRegBlock); 5951 5952 // Work out the address of an argument register. 5953 llvm::Value *ScaledRegCount = 5954 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); 5955 llvm::Value *RegBase = 5956 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() 5957 + RegPadding.getQuantity()); 5958 llvm::Value *RegOffset = 5959 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); 5960 Address RegSaveAreaPtr = 5961 CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24), 5962 "reg_save_area_ptr"); 5963 llvm::Value *RegSaveArea = 5964 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); 5965 Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, 5966 "raw_reg_addr"), 5967 PaddedSize); 5968 Address RegAddr = 5969 CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); 5970 5971 // Update the register count 5972 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); 5973 llvm::Value *NewRegCount = 5974 CGF.Builder.CreateAdd(RegCount, One, "reg_count"); 5975 CGF.Builder.CreateStore(NewRegCount, RegCountPtr); 5976 CGF.EmitBranch(ContBlock); 5977 5978 // Emit code to load the value if it was passed in memory. 5979 CGF.EmitBlock(InMemBlock); 5980 5981 // Work out the address of a stack argument. 5982 Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( 5983 VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr"); 5984 Address OverflowArgArea = 5985 Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), 5986 PaddedSize); 5987 Address RawMemAddr = 5988 CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); 5989 Address MemAddr = 5990 CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); 5991 5992 // Update overflow_arg_area_ptr pointer 5993 llvm::Value *NewOverflowArgArea = 5994 CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, 5995 "overflow_arg_area"); 5996 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 5997 CGF.EmitBranch(ContBlock); 5998 5999 // Return the appropriate result. 6000 CGF.EmitBlock(ContBlock); 6001 Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, 6002 MemAddr, InMemBlock, "va_arg.addr"); 6003 6004 if (IsIndirect) 6005 ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), 6006 TyInfo.second); 6007 6008 return ResAddr; 6009 } 6010 6011 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { 6012 if (RetTy->isVoidType()) 6013 return ABIArgInfo::getIgnore(); 6014 if (isVectorArgumentType(RetTy)) 6015 return ABIArgInfo::getDirect(); 6016 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) 6017 return getNaturalAlignIndirect(RetTy); 6018 return (isPromotableIntegerType(RetTy) ? 6019 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 6020 } 6021 6022 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { 6023 // Handle the generic C++ ABI. 6024 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 6025 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 6026 6027 // Integers and enums are extended to full register width. 6028 if (isPromotableIntegerType(Ty)) 6029 return ABIArgInfo::getExtend(); 6030 6031 // Handle vector types and vector-like structure types. Note that 6032 // as opposed to float-like structure types, we do not allow any 6033 // padding for vector-like structures, so verify the sizes match. 6034 uint64_t Size = getContext().getTypeSize(Ty); 6035 QualType SingleElementTy = GetSingleElementType(Ty); 6036 if (isVectorArgumentType(SingleElementTy) && 6037 getContext().getTypeSize(SingleElementTy) == Size) 6038 return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); 6039 6040 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. 6041 if (Size != 8 && Size != 16 && Size != 32 && Size != 64) 6042 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 6043 6044 // Handle small structures. 6045 if (const RecordType *RT = Ty->getAs<RecordType>()) { 6046 // Structures with flexible arrays have variable length, so really 6047 // fail the size test above. 6048 const RecordDecl *RD = RT->getDecl(); 6049 if (RD->hasFlexibleArrayMember()) 6050 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 6051 6052 // The structure is passed as an unextended integer, a float, or a double. 6053 llvm::Type *PassTy; 6054 if (isFPArgumentType(SingleElementTy)) { 6055 assert(Size == 32 || Size == 64); 6056 if (Size == 32) 6057 PassTy = llvm::Type::getFloatTy(getVMContext()); 6058 else 6059 PassTy = llvm::Type::getDoubleTy(getVMContext()); 6060 } else 6061 PassTy = llvm::IntegerType::get(getVMContext(), Size); 6062 return ABIArgInfo::getDirect(PassTy); 6063 } 6064 6065 // Non-structure compounds are passed indirectly. 6066 if (isCompoundType(Ty)) 6067 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 6068 6069 return ABIArgInfo::getDirect(nullptr); 6070 } 6071 6072 //===----------------------------------------------------------------------===// 6073 // MSP430 ABI Implementation 6074 //===----------------------------------------------------------------------===// 6075 6076 namespace { 6077 6078 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { 6079 public: 6080 MSP430TargetCodeGenInfo(CodeGenTypes &CGT) 6081 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 6082 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6083 CodeGen::CodeGenModule &M) const override; 6084 }; 6085 6086 } 6087 6088 void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, 6089 llvm::GlobalValue *GV, 6090 CodeGen::CodeGenModule &M) const { 6091 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { 6092 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { 6093 // Handle 'interrupt' attribute: 6094 llvm::Function *F = cast<llvm::Function>(GV); 6095 6096 // Step 1: Set ISR calling convention. 6097 F->setCallingConv(llvm::CallingConv::MSP430_INTR); 6098 6099 // Step 2: Add attributes goodness. 6100 F->addFnAttr(llvm::Attribute::NoInline); 6101 6102 // Step 3: Emit ISR vector alias. 6103 unsigned Num = attr->getNumber() / 2; 6104 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, 6105 "__isr_" + Twine(Num), F); 6106 } 6107 } 6108 } 6109 6110 //===----------------------------------------------------------------------===// 6111 // MIPS ABI Implementation. This works for both little-endian and 6112 // big-endian variants. 6113 //===----------------------------------------------------------------------===// 6114 6115 namespace { 6116 class MipsABIInfo : public ABIInfo { 6117 bool IsO32; 6118 unsigned MinABIStackAlignInBytes, StackAlignInBytes; 6119 void CoerceToIntArgs(uint64_t TySize, 6120 SmallVectorImpl<llvm::Type *> &ArgList) const; 6121 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; 6122 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; 6123 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; 6124 public: 6125 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : 6126 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), 6127 StackAlignInBytes(IsO32 ? 8 : 16) {} 6128 6129 ABIArgInfo classifyReturnType(QualType RetTy) const; 6130 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; 6131 void computeInfo(CGFunctionInfo &FI) const override; 6132 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6133 QualType Ty) const override; 6134 bool shouldSignExtUnsignedType(QualType Ty) const override; 6135 }; 6136 6137 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { 6138 unsigned SizeOfUnwindException; 6139 public: 6140 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) 6141 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), 6142 SizeOfUnwindException(IsO32 ? 24 : 32) {} 6143 6144 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 6145 return 29; 6146 } 6147 6148 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6149 CodeGen::CodeGenModule &CGM) const override { 6150 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 6151 if (!FD) return; 6152 llvm::Function *Fn = cast<llvm::Function>(GV); 6153 if (FD->hasAttr<Mips16Attr>()) { 6154 Fn->addFnAttr("mips16"); 6155 } 6156 else if (FD->hasAttr<NoMips16Attr>()) { 6157 Fn->addFnAttr("nomips16"); 6158 } 6159 6160 const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); 6161 if (!Attr) 6162 return; 6163 6164 const char *Kind; 6165 switch (Attr->getInterrupt()) { 6166 case MipsInterruptAttr::eic: Kind = "eic"; break; 6167 case MipsInterruptAttr::sw0: Kind = "sw0"; break; 6168 case MipsInterruptAttr::sw1: Kind = "sw1"; break; 6169 case MipsInterruptAttr::hw0: Kind = "hw0"; break; 6170 case MipsInterruptAttr::hw1: Kind = "hw1"; break; 6171 case MipsInterruptAttr::hw2: Kind = "hw2"; break; 6172 case MipsInterruptAttr::hw3: Kind = "hw3"; break; 6173 case MipsInterruptAttr::hw4: Kind = "hw4"; break; 6174 case MipsInterruptAttr::hw5: Kind = "hw5"; break; 6175 } 6176 6177 Fn->addFnAttr("interrupt", Kind); 6178 6179 } 6180 6181 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 6182 llvm::Value *Address) const override; 6183 6184 unsigned getSizeOfUnwindException() const override { 6185 return SizeOfUnwindException; 6186 } 6187 }; 6188 } 6189 6190 void MipsABIInfo::CoerceToIntArgs( 6191 uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { 6192 llvm::IntegerType *IntTy = 6193 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 6194 6195 // Add (TySize / MinABIStackAlignInBytes) args of IntTy. 6196 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) 6197 ArgList.push_back(IntTy); 6198 6199 // If necessary, add one more integer type to ArgList. 6200 unsigned R = TySize % (MinABIStackAlignInBytes * 8); 6201 6202 if (R) 6203 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); 6204 } 6205 6206 // In N32/64, an aligned double precision floating point field is passed in 6207 // a register. 6208 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { 6209 SmallVector<llvm::Type*, 8> ArgList, IntArgList; 6210 6211 if (IsO32) { 6212 CoerceToIntArgs(TySize, ArgList); 6213 return llvm::StructType::get(getVMContext(), ArgList); 6214 } 6215 6216 if (Ty->isComplexType()) 6217 return CGT.ConvertType(Ty); 6218 6219 const RecordType *RT = Ty->getAs<RecordType>(); 6220 6221 // Unions/vectors are passed in integer registers. 6222 if (!RT || !RT->isStructureOrClassType()) { 6223 CoerceToIntArgs(TySize, ArgList); 6224 return llvm::StructType::get(getVMContext(), ArgList); 6225 } 6226 6227 const RecordDecl *RD = RT->getDecl(); 6228 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 6229 assert(!(TySize % 8) && "Size of structure must be multiple of 8."); 6230 6231 uint64_t LastOffset = 0; 6232 unsigned idx = 0; 6233 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); 6234 6235 // Iterate over fields in the struct/class and check if there are any aligned 6236 // double fields. 6237 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 6238 i != e; ++i, ++idx) { 6239 const QualType Ty = i->getType(); 6240 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 6241 6242 if (!BT || BT->getKind() != BuiltinType::Double) 6243 continue; 6244 6245 uint64_t Offset = Layout.getFieldOffset(idx); 6246 if (Offset % 64) // Ignore doubles that are not aligned. 6247 continue; 6248 6249 // Add ((Offset - LastOffset) / 64) args of type i64. 6250 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) 6251 ArgList.push_back(I64); 6252 6253 // Add double type. 6254 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); 6255 LastOffset = Offset + 64; 6256 } 6257 6258 CoerceToIntArgs(TySize - LastOffset, IntArgList); 6259 ArgList.append(IntArgList.begin(), IntArgList.end()); 6260 6261 return llvm::StructType::get(getVMContext(), ArgList); 6262 } 6263 6264 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, 6265 uint64_t Offset) const { 6266 if (OrigOffset + MinABIStackAlignInBytes > Offset) 6267 return nullptr; 6268 6269 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); 6270 } 6271 6272 ABIArgInfo 6273 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { 6274 Ty = useFirstFieldIfTransparentUnion(Ty); 6275 6276 uint64_t OrigOffset = Offset; 6277 uint64_t TySize = getContext().getTypeSize(Ty); 6278 uint64_t Align = getContext().getTypeAlign(Ty) / 8; 6279 6280 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), 6281 (uint64_t)StackAlignInBytes); 6282 unsigned CurrOffset = llvm::alignTo(Offset, Align); 6283 Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; 6284 6285 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { 6286 // Ignore empty aggregates. 6287 if (TySize == 0) 6288 return ABIArgInfo::getIgnore(); 6289 6290 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 6291 Offset = OrigOffset + MinABIStackAlignInBytes; 6292 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 6293 } 6294 6295 // If we have reached here, aggregates are passed directly by coercing to 6296 // another structure type. Padding is inserted if the offset of the 6297 // aggregate is unaligned. 6298 ABIArgInfo ArgInfo = 6299 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, 6300 getPaddingType(OrigOffset, CurrOffset)); 6301 ArgInfo.setInReg(true); 6302 return ArgInfo; 6303 } 6304 6305 // Treat an enum type as its underlying type. 6306 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 6307 Ty = EnumTy->getDecl()->getIntegerType(); 6308 6309 // All integral types are promoted to the GPR width. 6310 if (Ty->isIntegralOrEnumerationType()) 6311 return ABIArgInfo::getExtend(); 6312 6313 return ABIArgInfo::getDirect( 6314 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); 6315 } 6316 6317 llvm::Type* 6318 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { 6319 const RecordType *RT = RetTy->getAs<RecordType>(); 6320 SmallVector<llvm::Type*, 8> RTList; 6321 6322 if (RT && RT->isStructureOrClassType()) { 6323 const RecordDecl *RD = RT->getDecl(); 6324 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 6325 unsigned FieldCnt = Layout.getFieldCount(); 6326 6327 // N32/64 returns struct/classes in floating point registers if the 6328 // following conditions are met: 6329 // 1. The size of the struct/class is no larger than 128-bit. 6330 // 2. The struct/class has one or two fields all of which are floating 6331 // point types. 6332 // 3. The offset of the first field is zero (this follows what gcc does). 6333 // 6334 // Any other composite results are returned in integer registers. 6335 // 6336 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { 6337 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); 6338 for (; b != e; ++b) { 6339 const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); 6340 6341 if (!BT || !BT->isFloatingPoint()) 6342 break; 6343 6344 RTList.push_back(CGT.ConvertType(b->getType())); 6345 } 6346 6347 if (b == e) 6348 return llvm::StructType::get(getVMContext(), RTList, 6349 RD->hasAttr<PackedAttr>()); 6350 6351 RTList.clear(); 6352 } 6353 } 6354 6355 CoerceToIntArgs(Size, RTList); 6356 return llvm::StructType::get(getVMContext(), RTList); 6357 } 6358 6359 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { 6360 uint64_t Size = getContext().getTypeSize(RetTy); 6361 6362 if (RetTy->isVoidType()) 6363 return ABIArgInfo::getIgnore(); 6364 6365 // O32 doesn't treat zero-sized structs differently from other structs. 6366 // However, N32/N64 ignores zero sized return values. 6367 if (!IsO32 && Size == 0) 6368 return ABIArgInfo::getIgnore(); 6369 6370 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { 6371 if (Size <= 128) { 6372 if (RetTy->isAnyComplexType()) 6373 return ABIArgInfo::getDirect(); 6374 6375 // O32 returns integer vectors in registers and N32/N64 returns all small 6376 // aggregates in registers. 6377 if (!IsO32 || 6378 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { 6379 ABIArgInfo ArgInfo = 6380 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 6381 ArgInfo.setInReg(true); 6382 return ArgInfo; 6383 } 6384 } 6385 6386 return getNaturalAlignIndirect(RetTy); 6387 } 6388 6389 // Treat an enum type as its underlying type. 6390 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 6391 RetTy = EnumTy->getDecl()->getIntegerType(); 6392 6393 return (RetTy->isPromotableIntegerType() ? 6394 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 6395 } 6396 6397 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { 6398 ABIArgInfo &RetInfo = FI.getReturnInfo(); 6399 if (!getCXXABI().classifyReturnType(FI)) 6400 RetInfo = classifyReturnType(FI.getReturnType()); 6401 6402 // Check if a pointer to an aggregate is passed as a hidden argument. 6403 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; 6404 6405 for (auto &I : FI.arguments()) 6406 I.info = classifyArgumentType(I.type, Offset); 6407 } 6408 6409 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6410 QualType OrigTy) const { 6411 QualType Ty = OrigTy; 6412 6413 // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. 6414 // Pointers are also promoted in the same way but this only matters for N32. 6415 unsigned SlotSizeInBits = IsO32 ? 32 : 64; 6416 unsigned PtrWidth = getTarget().getPointerWidth(0); 6417 bool DidPromote = false; 6418 if ((Ty->isIntegerType() && 6419 getContext().getIntWidth(Ty) < SlotSizeInBits) || 6420 (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { 6421 DidPromote = true; 6422 Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, 6423 Ty->isSignedIntegerType()); 6424 } 6425 6426 auto TyInfo = getContext().getTypeInfoInChars(Ty); 6427 6428 // The alignment of things in the argument area is never larger than 6429 // StackAlignInBytes. 6430 TyInfo.second = 6431 std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); 6432 6433 // MinABIStackAlignInBytes is the size of argument slots on the stack. 6434 CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); 6435 6436 Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 6437 TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); 6438 6439 6440 // If there was a promotion, "unpromote" into a temporary. 6441 // TODO: can we just use a pointer into a subset of the original slot? 6442 if (DidPromote) { 6443 Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); 6444 llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); 6445 6446 // Truncate down to the right width. 6447 llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() 6448 : CGF.IntPtrTy); 6449 llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); 6450 if (OrigTy->isPointerType()) 6451 V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); 6452 6453 CGF.Builder.CreateStore(V, Temp); 6454 Addr = Temp; 6455 } 6456 6457 return Addr; 6458 } 6459 6460 bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { 6461 int TySize = getContext().getTypeSize(Ty); 6462 6463 // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. 6464 if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) 6465 return true; 6466 6467 return false; 6468 } 6469 6470 bool 6471 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 6472 llvm::Value *Address) const { 6473 // This information comes from gcc's implementation, which seems to 6474 // as canonical as it gets. 6475 6476 // Everything on MIPS is 4 bytes. Double-precision FP registers 6477 // are aliased to pairs of single-precision FP registers. 6478 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 6479 6480 // 0-31 are the general purpose registers, $0 - $31. 6481 // 32-63 are the floating-point registers, $f0 - $f31. 6482 // 64 and 65 are the multiply/divide registers, $hi and $lo. 6483 // 66 is the (notional, I think) register for signal-handler return. 6484 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); 6485 6486 // 67-74 are the floating-point status registers, $fcc0 - $fcc7. 6487 // They are one bit wide and ignored here. 6488 6489 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. 6490 // (coprocessor 1 is the FP unit) 6491 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. 6492 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. 6493 // 176-181 are the DSP accumulator registers. 6494 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); 6495 return false; 6496 } 6497 6498 //===----------------------------------------------------------------------===// 6499 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. 6500 // Currently subclassed only to implement custom OpenCL C function attribute 6501 // handling. 6502 //===----------------------------------------------------------------------===// 6503 6504 namespace { 6505 6506 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { 6507 public: 6508 TCETargetCodeGenInfo(CodeGenTypes &CGT) 6509 : DefaultTargetCodeGenInfo(CGT) {} 6510 6511 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6512 CodeGen::CodeGenModule &M) const override; 6513 }; 6514 6515 void TCETargetCodeGenInfo::setTargetAttributes( 6516 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { 6517 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 6518 if (!FD) return; 6519 6520 llvm::Function *F = cast<llvm::Function>(GV); 6521 6522 if (M.getLangOpts().OpenCL) { 6523 if (FD->hasAttr<OpenCLKernelAttr>()) { 6524 // OpenCL C Kernel functions are not subject to inlining 6525 F->addFnAttr(llvm::Attribute::NoInline); 6526 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); 6527 if (Attr) { 6528 // Convert the reqd_work_group_size() attributes to metadata. 6529 llvm::LLVMContext &Context = F->getContext(); 6530 llvm::NamedMDNode *OpenCLMetadata = 6531 M.getModule().getOrInsertNamedMetadata( 6532 "opencl.kernel_wg_size_info"); 6533 6534 SmallVector<llvm::Metadata *, 5> Operands; 6535 Operands.push_back(llvm::ConstantAsMetadata::get(F)); 6536 6537 Operands.push_back( 6538 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 6539 M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); 6540 Operands.push_back( 6541 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 6542 M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); 6543 Operands.push_back( 6544 llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( 6545 M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); 6546 6547 // Add a boolean constant operand for "required" (true) or "hint" 6548 // (false) for implementing the work_group_size_hint attr later. 6549 // Currently always true as the hint is not yet implemented. 6550 Operands.push_back( 6551 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); 6552 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); 6553 } 6554 } 6555 } 6556 } 6557 6558 } 6559 6560 //===----------------------------------------------------------------------===// 6561 // Hexagon ABI Implementation 6562 //===----------------------------------------------------------------------===// 6563 6564 namespace { 6565 6566 class HexagonABIInfo : public ABIInfo { 6567 6568 6569 public: 6570 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 6571 6572 private: 6573 6574 ABIArgInfo classifyReturnType(QualType RetTy) const; 6575 ABIArgInfo classifyArgumentType(QualType RetTy) const; 6576 6577 void computeInfo(CGFunctionInfo &FI) const override; 6578 6579 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6580 QualType Ty) const override; 6581 }; 6582 6583 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { 6584 public: 6585 HexagonTargetCodeGenInfo(CodeGenTypes &CGT) 6586 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} 6587 6588 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 6589 return 29; 6590 } 6591 }; 6592 6593 } 6594 6595 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { 6596 if (!getCXXABI().classifyReturnType(FI)) 6597 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 6598 for (auto &I : FI.arguments()) 6599 I.info = classifyArgumentType(I.type); 6600 } 6601 6602 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { 6603 if (!isAggregateTypeForABI(Ty)) { 6604 // Treat an enum type as its underlying type. 6605 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 6606 Ty = EnumTy->getDecl()->getIntegerType(); 6607 6608 return (Ty->isPromotableIntegerType() ? 6609 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 6610 } 6611 6612 // Ignore empty records. 6613 if (isEmptyRecord(getContext(), Ty, true)) 6614 return ABIArgInfo::getIgnore(); 6615 6616 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 6617 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 6618 6619 uint64_t Size = getContext().getTypeSize(Ty); 6620 if (Size > 64) 6621 return getNaturalAlignIndirect(Ty, /*ByVal=*/true); 6622 // Pass in the smallest viable integer type. 6623 else if (Size > 32) 6624 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 6625 else if (Size > 16) 6626 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6627 else if (Size > 8) 6628 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 6629 else 6630 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 6631 } 6632 6633 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { 6634 if (RetTy->isVoidType()) 6635 return ABIArgInfo::getIgnore(); 6636 6637 // Large vector types should be returned via memory. 6638 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) 6639 return getNaturalAlignIndirect(RetTy); 6640 6641 if (!isAggregateTypeForABI(RetTy)) { 6642 // Treat an enum type as its underlying type. 6643 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 6644 RetTy = EnumTy->getDecl()->getIntegerType(); 6645 6646 return (RetTy->isPromotableIntegerType() ? 6647 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 6648 } 6649 6650 if (isEmptyRecord(getContext(), RetTy, true)) 6651 return ABIArgInfo::getIgnore(); 6652 6653 // Aggregates <= 8 bytes are returned in r0; other aggregates 6654 // are returned indirectly. 6655 uint64_t Size = getContext().getTypeSize(RetTy); 6656 if (Size <= 64) { 6657 // Return in the smallest viable integer type. 6658 if (Size <= 8) 6659 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 6660 if (Size <= 16) 6661 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 6662 if (Size <= 32) 6663 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 6664 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 6665 } 6666 6667 return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); 6668 } 6669 6670 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6671 QualType Ty) const { 6672 // FIXME: Someone needs to audit that this handle alignment correctly. 6673 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, 6674 getContext().getTypeInfoInChars(Ty), 6675 CharUnits::fromQuantity(4), 6676 /*AllowHigherAlign*/ true); 6677 } 6678 6679 //===----------------------------------------------------------------------===// 6680 // Lanai ABI Implementation 6681 //===----------------------------------------------------------------------===// 6682 6683 namespace { 6684 class LanaiABIInfo : public DefaultABIInfo { 6685 public: 6686 LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 6687 6688 bool shouldUseInReg(QualType Ty, CCState &State) const; 6689 6690 void computeInfo(CGFunctionInfo &FI) const override { 6691 CCState State(FI.getCallingConvention()); 6692 // Lanai uses 4 registers to pass arguments unless the function has the 6693 // regparm attribute set. 6694 if (FI.getHasRegParm()) { 6695 State.FreeRegs = FI.getRegParm(); 6696 } else { 6697 State.FreeRegs = 4; 6698 } 6699 6700 if (!getCXXABI().classifyReturnType(FI)) 6701 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 6702 for (auto &I : FI.arguments()) 6703 I.info = classifyArgumentType(I.type, State); 6704 } 6705 6706 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; 6707 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; 6708 }; 6709 } // end anonymous namespace 6710 6711 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { 6712 unsigned Size = getContext().getTypeSize(Ty); 6713 unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; 6714 6715 if (SizeInRegs == 0) 6716 return false; 6717 6718 if (SizeInRegs > State.FreeRegs) { 6719 State.FreeRegs = 0; 6720 return false; 6721 } 6722 6723 State.FreeRegs -= SizeInRegs; 6724 6725 return true; 6726 } 6727 6728 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, 6729 CCState &State) const { 6730 if (!ByVal) { 6731 if (State.FreeRegs) { 6732 --State.FreeRegs; // Non-byval indirects just use one pointer. 6733 return getNaturalAlignIndirectInReg(Ty); 6734 } 6735 return getNaturalAlignIndirect(Ty, false); 6736 } 6737 6738 // Compute the byval alignment. 6739 const unsigned MinABIStackAlignInBytes = 4; 6740 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 6741 return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, 6742 /*Realign=*/TypeAlign > 6743 MinABIStackAlignInBytes); 6744 } 6745 6746 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, 6747 CCState &State) const { 6748 // Check with the C++ ABI first. 6749 const RecordType *RT = Ty->getAs<RecordType>(); 6750 if (RT) { 6751 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 6752 if (RAA == CGCXXABI::RAA_Indirect) { 6753 return getIndirectResult(Ty, /*ByVal=*/false, State); 6754 } else if (RAA == CGCXXABI::RAA_DirectInMemory) { 6755 return getNaturalAlignIndirect(Ty, /*ByRef=*/true); 6756 } 6757 } 6758 6759 if (isAggregateTypeForABI(Ty)) { 6760 // Structures with flexible arrays are always indirect. 6761 if (RT && RT->getDecl()->hasFlexibleArrayMember()) 6762 return getIndirectResult(Ty, /*ByVal=*/true, State); 6763 6764 // Ignore empty structs/unions. 6765 if (isEmptyRecord(getContext(), Ty, true)) 6766 return ABIArgInfo::getIgnore(); 6767 6768 llvm::LLVMContext &LLVMContext = getVMContext(); 6769 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; 6770 if (SizeInRegs <= State.FreeRegs) { 6771 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 6772 SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); 6773 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 6774 State.FreeRegs -= SizeInRegs; 6775 return ABIArgInfo::getDirectInReg(Result); 6776 } else { 6777 State.FreeRegs = 0; 6778 } 6779 return getIndirectResult(Ty, true, State); 6780 } 6781 6782 // Treat an enum type as its underlying type. 6783 if (const auto *EnumTy = Ty->getAs<EnumType>()) 6784 Ty = EnumTy->getDecl()->getIntegerType(); 6785 6786 bool InReg = shouldUseInReg(Ty, State); 6787 if (Ty->isPromotableIntegerType()) { 6788 if (InReg) 6789 return ABIArgInfo::getDirectInReg(); 6790 return ABIArgInfo::getExtend(); 6791 } 6792 if (InReg) 6793 return ABIArgInfo::getDirectInReg(); 6794 return ABIArgInfo::getDirect(); 6795 } 6796 6797 namespace { 6798 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { 6799 public: 6800 LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 6801 : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} 6802 }; 6803 } 6804 6805 //===----------------------------------------------------------------------===// 6806 // AMDGPU ABI Implementation 6807 //===----------------------------------------------------------------------===// 6808 6809 namespace { 6810 6811 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { 6812 public: 6813 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) 6814 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 6815 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 6816 CodeGen::CodeGenModule &M) const override; 6817 }; 6818 6819 } 6820 6821 void AMDGPUTargetCodeGenInfo::setTargetAttributes( 6822 const Decl *D, 6823 llvm::GlobalValue *GV, 6824 CodeGen::CodeGenModule &M) const { 6825 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); 6826 if (!FD) 6827 return; 6828 6829 if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { 6830 llvm::Function *F = cast<llvm::Function>(GV); 6831 uint32_t NumVGPR = Attr->getNumVGPR(); 6832 if (NumVGPR != 0) 6833 F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); 6834 } 6835 6836 if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { 6837 llvm::Function *F = cast<llvm::Function>(GV); 6838 unsigned NumSGPR = Attr->getNumSGPR(); 6839 if (NumSGPR != 0) 6840 F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); 6841 } 6842 } 6843 6844 6845 //===----------------------------------------------------------------------===// 6846 // SPARC v9 ABI Implementation. 6847 // Based on the SPARC Compliance Definition version 2.4.1. 6848 // 6849 // Function arguments a mapped to a nominal "parameter array" and promoted to 6850 // registers depending on their type. Each argument occupies 8 or 16 bytes in 6851 // the array, structs larger than 16 bytes are passed indirectly. 6852 // 6853 // One case requires special care: 6854 // 6855 // struct mixed { 6856 // int i; 6857 // float f; 6858 // }; 6859 // 6860 // When a struct mixed is passed by value, it only occupies 8 bytes in the 6861 // parameter array, but the int is passed in an integer register, and the float 6862 // is passed in a floating point register. This is represented as two arguments 6863 // with the LLVM IR inreg attribute: 6864 // 6865 // declare void f(i32 inreg %i, float inreg %f) 6866 // 6867 // The code generator will only allocate 4 bytes from the parameter array for 6868 // the inreg arguments. All other arguments are allocated a multiple of 8 6869 // bytes. 6870 // 6871 namespace { 6872 class SparcV9ABIInfo : public ABIInfo { 6873 public: 6874 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 6875 6876 private: 6877 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; 6878 void computeInfo(CGFunctionInfo &FI) const override; 6879 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 6880 QualType Ty) const override; 6881 6882 // Coercion type builder for structs passed in registers. The coercion type 6883 // serves two purposes: 6884 // 6885 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' 6886 // in registers. 6887 // 2. Expose aligned floating point elements as first-level elements, so the 6888 // code generator knows to pass them in floating point registers. 6889 // 6890 // We also compute the InReg flag which indicates that the struct contains 6891 // aligned 32-bit floats. 6892 // 6893 struct CoerceBuilder { 6894 llvm::LLVMContext &Context; 6895 const llvm::DataLayout &DL; 6896 SmallVector<llvm::Type*, 8> Elems; 6897 uint64_t Size; 6898 bool InReg; 6899 6900 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) 6901 : Context(c), DL(dl), Size(0), InReg(false) {} 6902 6903 // Pad Elems with integers until Size is ToSize. 6904 void pad(uint64_t ToSize) { 6905 assert(ToSize >= Size && "Cannot remove elements"); 6906 if (ToSize == Size) 6907 return; 6908 6909 // Finish the current 64-bit word. 6910 uint64_t Aligned = llvm::alignTo(Size, 64); 6911 if (Aligned > Size && Aligned <= ToSize) { 6912 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); 6913 Size = Aligned; 6914 } 6915 6916 // Add whole 64-bit words. 6917 while (Size + 64 <= ToSize) { 6918 Elems.push_back(llvm::Type::getInt64Ty(Context)); 6919 Size += 64; 6920 } 6921 6922 // Final in-word padding. 6923 if (Size < ToSize) { 6924 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); 6925 Size = ToSize; 6926 } 6927 } 6928 6929 // Add a floating point element at Offset. 6930 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { 6931 // Unaligned floats are treated as integers. 6932 if (Offset % Bits) 6933 return; 6934 // The InReg flag is only required if there are any floats < 64 bits. 6935 if (Bits < 64) 6936 InReg = true; 6937 pad(Offset); 6938 Elems.push_back(Ty); 6939 Size = Offset + Bits; 6940 } 6941 6942 // Add a struct type to the coercion type, starting at Offset (in bits). 6943 void addStruct(uint64_t Offset, llvm::StructType *StrTy) { 6944 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); 6945 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { 6946 llvm::Type *ElemTy = StrTy->getElementType(i); 6947 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); 6948 switch (ElemTy->getTypeID()) { 6949 case llvm::Type::StructTyID: 6950 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); 6951 break; 6952 case llvm::Type::FloatTyID: 6953 addFloat(ElemOffset, ElemTy, 32); 6954 break; 6955 case llvm::Type::DoubleTyID: 6956 addFloat(ElemOffset, ElemTy, 64); 6957 break; 6958 case llvm::Type::FP128TyID: 6959 addFloat(ElemOffset, ElemTy, 128); 6960 break; 6961 case llvm::Type::PointerTyID: 6962 if (ElemOffset % 64 == 0) { 6963 pad(ElemOffset); 6964 Elems.push_back(ElemTy); 6965 Size += 64; 6966 } 6967 break; 6968 default: 6969 break; 6970 } 6971 } 6972 } 6973 6974 // Check if Ty is a usable substitute for the coercion type. 6975 bool isUsableType(llvm::StructType *Ty) const { 6976 return llvm::makeArrayRef(Elems) == Ty->elements(); 6977 } 6978 6979 // Get the coercion type as a literal struct type. 6980 llvm::Type *getType() const { 6981 if (Elems.size() == 1) 6982 return Elems.front(); 6983 else 6984 return llvm::StructType::get(Context, Elems); 6985 } 6986 }; 6987 }; 6988 } // end anonymous namespace 6989 6990 ABIArgInfo 6991 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { 6992 if (Ty->isVoidType()) 6993 return ABIArgInfo::getIgnore(); 6994 6995 uint64_t Size = getContext().getTypeSize(Ty); 6996 6997 // Anything too big to fit in registers is passed with an explicit indirect 6998 // pointer / sret pointer. 6999 if (Size > SizeLimit) 7000 return getNaturalAlignIndirect(Ty, /*ByVal=*/false); 7001 7002 // Treat an enum type as its underlying type. 7003 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 7004 Ty = EnumTy->getDecl()->getIntegerType(); 7005 7006 // Integer types smaller than a register are extended. 7007 if (Size < 64 && Ty->isIntegerType()) 7008 return ABIArgInfo::getExtend(); 7009 7010 // Other non-aggregates go in registers. 7011 if (!isAggregateTypeForABI(Ty)) 7012 return ABIArgInfo::getDirect(); 7013 7014 // If a C++ object has either a non-trivial copy constructor or a non-trivial 7015 // destructor, it is passed with an explicit indirect pointer / sret pointer. 7016 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 7017 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); 7018 7019 // This is a small aggregate type that should be passed in registers. 7020 // Build a coercion type from the LLVM struct type. 7021 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); 7022 if (!StrTy) 7023 return ABIArgInfo::getDirect(); 7024 7025 CoerceBuilder CB(getVMContext(), getDataLayout()); 7026 CB.addStruct(0, StrTy); 7027 CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); 7028 7029 // Try to use the original type for coercion. 7030 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); 7031 7032 if (CB.InReg) 7033 return ABIArgInfo::getDirectInReg(CoerceTy); 7034 else 7035 return ABIArgInfo::getDirect(CoerceTy); 7036 } 7037 7038 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7039 QualType Ty) const { 7040 ABIArgInfo AI = classifyType(Ty, 16 * 8); 7041 llvm::Type *ArgTy = CGT.ConvertType(Ty); 7042 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 7043 AI.setCoerceToType(ArgTy); 7044 7045 CharUnits SlotSize = CharUnits::fromQuantity(8); 7046 7047 CGBuilderTy &Builder = CGF.Builder; 7048 Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); 7049 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 7050 7051 auto TypeInfo = getContext().getTypeInfoInChars(Ty); 7052 7053 Address ArgAddr = Address::invalid(); 7054 CharUnits Stride; 7055 switch (AI.getKind()) { 7056 case ABIArgInfo::Expand: 7057 case ABIArgInfo::CoerceAndExpand: 7058 case ABIArgInfo::InAlloca: 7059 llvm_unreachable("Unsupported ABI kind for va_arg"); 7060 7061 case ABIArgInfo::Extend: { 7062 Stride = SlotSize; 7063 CharUnits Offset = SlotSize - TypeInfo.first; 7064 ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); 7065 break; 7066 } 7067 7068 case ABIArgInfo::Direct: { 7069 auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); 7070 Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); 7071 ArgAddr = Addr; 7072 break; 7073 } 7074 7075 case ABIArgInfo::Indirect: 7076 Stride = SlotSize; 7077 ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); 7078 ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), 7079 TypeInfo.second); 7080 break; 7081 7082 case ABIArgInfo::Ignore: 7083 return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); 7084 } 7085 7086 // Update VAList. 7087 llvm::Value *NextPtr = 7088 Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next"); 7089 Builder.CreateStore(NextPtr, VAListAddr); 7090 7091 return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); 7092 } 7093 7094 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { 7095 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); 7096 for (auto &I : FI.arguments()) 7097 I.info = classifyType(I.type, 16 * 8); 7098 } 7099 7100 namespace { 7101 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { 7102 public: 7103 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) 7104 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} 7105 7106 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 7107 return 14; 7108 } 7109 7110 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 7111 llvm::Value *Address) const override; 7112 }; 7113 } // end anonymous namespace 7114 7115 bool 7116 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 7117 llvm::Value *Address) const { 7118 // This is calculated from the LLVM and GCC tables and verified 7119 // against gcc output. AFAIK all ABIs use the same encoding. 7120 7121 CodeGen::CGBuilderTy &Builder = CGF.Builder; 7122 7123 llvm::IntegerType *i8 = CGF.Int8Ty; 7124 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 7125 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 7126 7127 // 0-31: the 8-byte general-purpose registers 7128 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 7129 7130 // 32-63: f0-31, the 4-byte floating-point registers 7131 AssignToArrayRange(Builder, Address, Four8, 32, 63); 7132 7133 // Y = 64 7134 // PSR = 65 7135 // WIM = 66 7136 // TBR = 67 7137 // PC = 68 7138 // NPC = 69 7139 // FSR = 70 7140 // CSR = 71 7141 AssignToArrayRange(Builder, Address, Eight8, 64, 71); 7142 7143 // 72-87: d0-15, the 8-byte floating-point registers 7144 AssignToArrayRange(Builder, Address, Eight8, 72, 87); 7145 7146 return false; 7147 } 7148 7149 7150 //===----------------------------------------------------------------------===// 7151 // XCore ABI Implementation 7152 //===----------------------------------------------------------------------===// 7153 7154 namespace { 7155 7156 /// A SmallStringEnc instance is used to build up the TypeString by passing 7157 /// it by reference between functions that append to it. 7158 typedef llvm::SmallString<128> SmallStringEnc; 7159 7160 /// TypeStringCache caches the meta encodings of Types. 7161 /// 7162 /// The reason for caching TypeStrings is two fold: 7163 /// 1. To cache a type's encoding for later uses; 7164 /// 2. As a means to break recursive member type inclusion. 7165 /// 7166 /// A cache Entry can have a Status of: 7167 /// NonRecursive: The type encoding is not recursive; 7168 /// Recursive: The type encoding is recursive; 7169 /// Incomplete: An incomplete TypeString; 7170 /// IncompleteUsed: An incomplete TypeString that has been used in a 7171 /// Recursive type encoding. 7172 /// 7173 /// A NonRecursive entry will have all of its sub-members expanded as fully 7174 /// as possible. Whilst it may contain types which are recursive, the type 7175 /// itself is not recursive and thus its encoding may be safely used whenever 7176 /// the type is encountered. 7177 /// 7178 /// A Recursive entry will have all of its sub-members expanded as fully as 7179 /// possible. The type itself is recursive and it may contain other types which 7180 /// are recursive. The Recursive encoding must not be used during the expansion 7181 /// of a recursive type's recursive branch. For simplicity the code uses 7182 /// IncompleteCount to reject all usage of Recursive encodings for member types. 7183 /// 7184 /// An Incomplete entry is always a RecordType and only encodes its 7185 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and 7186 /// are placed into the cache during type expansion as a means to identify and 7187 /// handle recursive inclusion of types as sub-members. If there is recursion 7188 /// the entry becomes IncompleteUsed. 7189 /// 7190 /// During the expansion of a RecordType's members: 7191 /// 7192 /// If the cache contains a NonRecursive encoding for the member type, the 7193 /// cached encoding is used; 7194 /// 7195 /// If the cache contains a Recursive encoding for the member type, the 7196 /// cached encoding is 'Swapped' out, as it may be incorrect, and... 7197 /// 7198 /// If the member is a RecordType, an Incomplete encoding is placed into the 7199 /// cache to break potential recursive inclusion of itself as a sub-member; 7200 /// 7201 /// Once a member RecordType has been expanded, its temporary incomplete 7202 /// entry is removed from the cache. If a Recursive encoding was swapped out 7203 /// it is swapped back in; 7204 /// 7205 /// If an incomplete entry is used to expand a sub-member, the incomplete 7206 /// entry is marked as IncompleteUsed. The cache keeps count of how many 7207 /// IncompleteUsed entries it currently contains in IncompleteUsedCount; 7208 /// 7209 /// If a member's encoding is found to be a NonRecursive or Recursive viz: 7210 /// IncompleteUsedCount==0, the member's encoding is added to the cache. 7211 /// Else the member is part of a recursive type and thus the recursion has 7212 /// been exited too soon for the encoding to be correct for the member. 7213 /// 7214 class TypeStringCache { 7215 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; 7216 struct Entry { 7217 std::string Str; // The encoded TypeString for the type. 7218 enum Status State; // Information about the encoding in 'Str'. 7219 std::string Swapped; // A temporary place holder for a Recursive encoding 7220 // during the expansion of RecordType's members. 7221 }; 7222 std::map<const IdentifierInfo *, struct Entry> Map; 7223 unsigned IncompleteCount; // Number of Incomplete entries in the Map. 7224 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. 7225 public: 7226 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} 7227 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); 7228 bool removeIncomplete(const IdentifierInfo *ID); 7229 void addIfComplete(const IdentifierInfo *ID, StringRef Str, 7230 bool IsRecursive); 7231 StringRef lookupStr(const IdentifierInfo *ID); 7232 }; 7233 7234 /// TypeString encodings for enum & union fields must be order. 7235 /// FieldEncoding is a helper for this ordering process. 7236 class FieldEncoding { 7237 bool HasName; 7238 std::string Enc; 7239 public: 7240 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} 7241 StringRef str() {return Enc.c_str();} 7242 bool operator<(const FieldEncoding &rhs) const { 7243 if (HasName != rhs.HasName) return HasName; 7244 return Enc < rhs.Enc; 7245 } 7246 }; 7247 7248 class XCoreABIInfo : public DefaultABIInfo { 7249 public: 7250 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 7251 Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7252 QualType Ty) const override; 7253 }; 7254 7255 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { 7256 mutable TypeStringCache TSC; 7257 public: 7258 XCoreTargetCodeGenInfo(CodeGenTypes &CGT) 7259 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} 7260 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 7261 CodeGen::CodeGenModule &M) const override; 7262 }; 7263 7264 } // End anonymous namespace. 7265 7266 // TODO: this implementation is likely now redundant with the default 7267 // EmitVAArg. 7268 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, 7269 QualType Ty) const { 7270 CGBuilderTy &Builder = CGF.Builder; 7271 7272 // Get the VAList. 7273 CharUnits SlotSize = CharUnits::fromQuantity(4); 7274 Address AP(Builder.CreateLoad(VAListAddr), SlotSize); 7275 7276 // Handle the argument. 7277 ABIArgInfo AI = classifyArgumentType(Ty); 7278 CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); 7279 llvm::Type *ArgTy = CGT.ConvertType(Ty); 7280 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 7281 AI.setCoerceToType(ArgTy); 7282 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 7283 7284 Address Val = Address::invalid(); 7285 CharUnits ArgSize = CharUnits::Zero(); 7286 switch (AI.getKind()) { 7287 case ABIArgInfo::Expand: 7288 case ABIArgInfo::CoerceAndExpand: 7289 case ABIArgInfo::InAlloca: 7290 llvm_unreachable("Unsupported ABI kind for va_arg"); 7291 case ABIArgInfo::Ignore: 7292 Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); 7293 ArgSize = CharUnits::Zero(); 7294 break; 7295 case ABIArgInfo::Extend: 7296 case ABIArgInfo::Direct: 7297 Val = Builder.CreateBitCast(AP, ArgPtrTy); 7298 ArgSize = CharUnits::fromQuantity( 7299 getDataLayout().getTypeAllocSize(AI.getCoerceToType())); 7300 ArgSize = ArgSize.alignTo(SlotSize); 7301 break; 7302 case ABIArgInfo::Indirect: 7303 Val = Builder.CreateElementBitCast(AP, ArgPtrTy); 7304 Val = Address(Builder.CreateLoad(Val), TypeAlign); 7305 ArgSize = SlotSize; 7306 break; 7307 } 7308 7309 // Increment the VAList. 7310 if (!ArgSize.isZero()) { 7311 llvm::Value *APN = 7312 Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize); 7313 Builder.CreateStore(APN, VAListAddr); 7314 } 7315 7316 return Val; 7317 } 7318 7319 /// During the expansion of a RecordType, an incomplete TypeString is placed 7320 /// into the cache as a means to identify and break recursion. 7321 /// If there is a Recursive encoding in the cache, it is swapped out and will 7322 /// be reinserted by removeIncomplete(). 7323 /// All other types of encoding should have been used rather than arriving here. 7324 void TypeStringCache::addIncomplete(const IdentifierInfo *ID, 7325 std::string StubEnc) { 7326 if (!ID) 7327 return; 7328 Entry &E = Map[ID]; 7329 assert( (E.Str.empty() || E.State == Recursive) && 7330 "Incorrectly use of addIncomplete"); 7331 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); 7332 E.Swapped.swap(E.Str); // swap out the Recursive 7333 E.Str.swap(StubEnc); 7334 E.State = Incomplete; 7335 ++IncompleteCount; 7336 } 7337 7338 /// Once the RecordType has been expanded, the temporary incomplete TypeString 7339 /// must be removed from the cache. 7340 /// If a Recursive was swapped out by addIncomplete(), it will be replaced. 7341 /// Returns true if the RecordType was defined recursively. 7342 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { 7343 if (!ID) 7344 return false; 7345 auto I = Map.find(ID); 7346 assert(I != Map.end() && "Entry not present"); 7347 Entry &E = I->second; 7348 assert( (E.State == Incomplete || 7349 E.State == IncompleteUsed) && 7350 "Entry must be an incomplete type"); 7351 bool IsRecursive = false; 7352 if (E.State == IncompleteUsed) { 7353 // We made use of our Incomplete encoding, thus we are recursive. 7354 IsRecursive = true; 7355 --IncompleteUsedCount; 7356 } 7357 if (E.Swapped.empty()) 7358 Map.erase(I); 7359 else { 7360 // Swap the Recursive back. 7361 E.Swapped.swap(E.Str); 7362 E.Swapped.clear(); 7363 E.State = Recursive; 7364 } 7365 --IncompleteCount; 7366 return IsRecursive; 7367 } 7368 7369 /// Add the encoded TypeString to the cache only if it is NonRecursive or 7370 /// Recursive (viz: all sub-members were expanded as fully as possible). 7371 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, 7372 bool IsRecursive) { 7373 if (!ID || IncompleteUsedCount) 7374 return; // No key or it is is an incomplete sub-type so don't add. 7375 Entry &E = Map[ID]; 7376 if (IsRecursive && !E.Str.empty()) { 7377 assert(E.State==Recursive && E.Str.size() == Str.size() && 7378 "This is not the same Recursive entry"); 7379 // The parent container was not recursive after all, so we could have used 7380 // this Recursive sub-member entry after all, but we assumed the worse when 7381 // we started viz: IncompleteCount!=0. 7382 return; 7383 } 7384 assert(E.Str.empty() && "Entry already present"); 7385 E.Str = Str.str(); 7386 E.State = IsRecursive? Recursive : NonRecursive; 7387 } 7388 7389 /// Return a cached TypeString encoding for the ID. If there isn't one, or we 7390 /// are recursively expanding a type (IncompleteCount != 0) and the cached 7391 /// encoding is Recursive, return an empty StringRef. 7392 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { 7393 if (!ID) 7394 return StringRef(); // We have no key. 7395 auto I = Map.find(ID); 7396 if (I == Map.end()) 7397 return StringRef(); // We have no encoding. 7398 Entry &E = I->second; 7399 if (E.State == Recursive && IncompleteCount) 7400 return StringRef(); // We don't use Recursive encodings for member types. 7401 7402 if (E.State == Incomplete) { 7403 // The incomplete type is being used to break out of recursion. 7404 E.State = IncompleteUsed; 7405 ++IncompleteUsedCount; 7406 } 7407 return E.Str.c_str(); 7408 } 7409 7410 /// The XCore ABI includes a type information section that communicates symbol 7411 /// type information to the linker. The linker uses this information to verify 7412 /// safety/correctness of things such as array bound and pointers et al. 7413 /// The ABI only requires C (and XC) language modules to emit TypeStrings. 7414 /// This type information (TypeString) is emitted into meta data for all global 7415 /// symbols: definitions, declarations, functions & variables. 7416 /// 7417 /// The TypeString carries type, qualifier, name, size & value details. 7418 /// Please see 'Tools Development Guide' section 2.16.2 for format details: 7419 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf 7420 /// The output is tested by test/CodeGen/xcore-stringtype.c. 7421 /// 7422 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 7423 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); 7424 7425 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. 7426 void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 7427 CodeGen::CodeGenModule &CGM) const { 7428 SmallStringEnc Enc; 7429 if (getTypeString(Enc, D, CGM, TSC)) { 7430 llvm::LLVMContext &Ctx = CGM.getModule().getContext(); 7431 llvm::SmallVector<llvm::Metadata *, 2> MDVals; 7432 MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); 7433 MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); 7434 llvm::NamedMDNode *MD = 7435 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); 7436 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 7437 } 7438 } 7439 7440 //===----------------------------------------------------------------------===// 7441 // SPIR ABI Implementation 7442 //===----------------------------------------------------------------------===// 7443 7444 namespace { 7445 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { 7446 public: 7447 SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 7448 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 7449 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 7450 CodeGen::CodeGenModule &M) const override; 7451 }; 7452 } // End anonymous namespace. 7453 7454 /// Emit SPIR specific metadata: OpenCL and SPIR version. 7455 void SPIRTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 7456 CodeGen::CodeGenModule &CGM) const { 7457 llvm::LLVMContext &Ctx = CGM.getModule().getContext(); 7458 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(Ctx); 7459 llvm::Module &M = CGM.getModule(); 7460 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the 7461 // opencl.spir.version named metadata. 7462 llvm::Metadata *SPIRVerElts[] = { 7463 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2)), 7464 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 0))}; 7465 llvm::NamedMDNode *SPIRVerMD = 7466 M.getOrInsertNamedMetadata("opencl.spir.version"); 7467 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); 7468 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the 7469 // opencl.ocl.version named metadata node. 7470 llvm::Metadata *OCLVerElts[] = { 7471 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 7472 Int32Ty, CGM.getLangOpts().OpenCLVersion / 100)), 7473 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 7474 Int32Ty, (CGM.getLangOpts().OpenCLVersion % 100) / 10))}; 7475 llvm::NamedMDNode *OCLVerMD = 7476 M.getOrInsertNamedMetadata("opencl.ocl.version"); 7477 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); 7478 } 7479 7480 static bool appendType(SmallStringEnc &Enc, QualType QType, 7481 const CodeGen::CodeGenModule &CGM, 7482 TypeStringCache &TSC); 7483 7484 /// Helper function for appendRecordType(). 7485 /// Builds a SmallVector containing the encoded field types in declaration 7486 /// order. 7487 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, 7488 const RecordDecl *RD, 7489 const CodeGen::CodeGenModule &CGM, 7490 TypeStringCache &TSC) { 7491 for (const auto *Field : RD->fields()) { 7492 SmallStringEnc Enc; 7493 Enc += "m("; 7494 Enc += Field->getName(); 7495 Enc += "){"; 7496 if (Field->isBitField()) { 7497 Enc += "b("; 7498 llvm::raw_svector_ostream OS(Enc); 7499 OS << Field->getBitWidthValue(CGM.getContext()); 7500 Enc += ':'; 7501 } 7502 if (!appendType(Enc, Field->getType(), CGM, TSC)) 7503 return false; 7504 if (Field->isBitField()) 7505 Enc += ')'; 7506 Enc += '}'; 7507 FE.emplace_back(!Field->getName().empty(), Enc); 7508 } 7509 return true; 7510 } 7511 7512 /// Appends structure and union types to Enc and adds encoding to cache. 7513 /// Recursively calls appendType (via extractFieldType) for each field. 7514 /// Union types have their fields ordered according to the ABI. 7515 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, 7516 const CodeGen::CodeGenModule &CGM, 7517 TypeStringCache &TSC, const IdentifierInfo *ID) { 7518 // Append the cached TypeString if we have one. 7519 StringRef TypeString = TSC.lookupStr(ID); 7520 if (!TypeString.empty()) { 7521 Enc += TypeString; 7522 return true; 7523 } 7524 7525 // Start to emit an incomplete TypeString. 7526 size_t Start = Enc.size(); 7527 Enc += (RT->isUnionType()? 'u' : 's'); 7528 Enc += '('; 7529 if (ID) 7530 Enc += ID->getName(); 7531 Enc += "){"; 7532 7533 // We collect all encoded fields and order as necessary. 7534 bool IsRecursive = false; 7535 const RecordDecl *RD = RT->getDecl()->getDefinition(); 7536 if (RD && !RD->field_empty()) { 7537 // An incomplete TypeString stub is placed in the cache for this RecordType 7538 // so that recursive calls to this RecordType will use it whilst building a 7539 // complete TypeString for this RecordType. 7540 SmallVector<FieldEncoding, 16> FE; 7541 std::string StubEnc(Enc.substr(Start).str()); 7542 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. 7543 TSC.addIncomplete(ID, std::move(StubEnc)); 7544 if (!extractFieldType(FE, RD, CGM, TSC)) { 7545 (void) TSC.removeIncomplete(ID); 7546 return false; 7547 } 7548 IsRecursive = TSC.removeIncomplete(ID); 7549 // The ABI requires unions to be sorted but not structures. 7550 // See FieldEncoding::operator< for sort algorithm. 7551 if (RT->isUnionType()) 7552 std::sort(FE.begin(), FE.end()); 7553 // We can now complete the TypeString. 7554 unsigned E = FE.size(); 7555 for (unsigned I = 0; I != E; ++I) { 7556 if (I) 7557 Enc += ','; 7558 Enc += FE[I].str(); 7559 } 7560 } 7561 Enc += '}'; 7562 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); 7563 return true; 7564 } 7565 7566 /// Appends enum types to Enc and adds the encoding to the cache. 7567 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, 7568 TypeStringCache &TSC, 7569 const IdentifierInfo *ID) { 7570 // Append the cached TypeString if we have one. 7571 StringRef TypeString = TSC.lookupStr(ID); 7572 if (!TypeString.empty()) { 7573 Enc += TypeString; 7574 return true; 7575 } 7576 7577 size_t Start = Enc.size(); 7578 Enc += "e("; 7579 if (ID) 7580 Enc += ID->getName(); 7581 Enc += "){"; 7582 7583 // We collect all encoded enumerations and order them alphanumerically. 7584 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { 7585 SmallVector<FieldEncoding, 16> FE; 7586 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; 7587 ++I) { 7588 SmallStringEnc EnumEnc; 7589 EnumEnc += "m("; 7590 EnumEnc += I->getName(); 7591 EnumEnc += "){"; 7592 I->getInitVal().toString(EnumEnc); 7593 EnumEnc += '}'; 7594 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); 7595 } 7596 std::sort(FE.begin(), FE.end()); 7597 unsigned E = FE.size(); 7598 for (unsigned I = 0; I != E; ++I) { 7599 if (I) 7600 Enc += ','; 7601 Enc += FE[I].str(); 7602 } 7603 } 7604 Enc += '}'; 7605 TSC.addIfComplete(ID, Enc.substr(Start), false); 7606 return true; 7607 } 7608 7609 /// Appends type's qualifier to Enc. 7610 /// This is done prior to appending the type's encoding. 7611 static void appendQualifier(SmallStringEnc &Enc, QualType QT) { 7612 // Qualifiers are emitted in alphabetical order. 7613 static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; 7614 int Lookup = 0; 7615 if (QT.isConstQualified()) 7616 Lookup += 1<<0; 7617 if (QT.isRestrictQualified()) 7618 Lookup += 1<<1; 7619 if (QT.isVolatileQualified()) 7620 Lookup += 1<<2; 7621 Enc += Table[Lookup]; 7622 } 7623 7624 /// Appends built-in types to Enc. 7625 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { 7626 const char *EncType; 7627 switch (BT->getKind()) { 7628 case BuiltinType::Void: 7629 EncType = "0"; 7630 break; 7631 case BuiltinType::Bool: 7632 EncType = "b"; 7633 break; 7634 case BuiltinType::Char_U: 7635 EncType = "uc"; 7636 break; 7637 case BuiltinType::UChar: 7638 EncType = "uc"; 7639 break; 7640 case BuiltinType::SChar: 7641 EncType = "sc"; 7642 break; 7643 case BuiltinType::UShort: 7644 EncType = "us"; 7645 break; 7646 case BuiltinType::Short: 7647 EncType = "ss"; 7648 break; 7649 case BuiltinType::UInt: 7650 EncType = "ui"; 7651 break; 7652 case BuiltinType::Int: 7653 EncType = "si"; 7654 break; 7655 case BuiltinType::ULong: 7656 EncType = "ul"; 7657 break; 7658 case BuiltinType::Long: 7659 EncType = "sl"; 7660 break; 7661 case BuiltinType::ULongLong: 7662 EncType = "ull"; 7663 break; 7664 case BuiltinType::LongLong: 7665 EncType = "sll"; 7666 break; 7667 case BuiltinType::Float: 7668 EncType = "ft"; 7669 break; 7670 case BuiltinType::Double: 7671 EncType = "d"; 7672 break; 7673 case BuiltinType::LongDouble: 7674 EncType = "ld"; 7675 break; 7676 default: 7677 return false; 7678 } 7679 Enc += EncType; 7680 return true; 7681 } 7682 7683 /// Appends a pointer encoding to Enc before calling appendType for the pointee. 7684 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, 7685 const CodeGen::CodeGenModule &CGM, 7686 TypeStringCache &TSC) { 7687 Enc += "p("; 7688 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) 7689 return false; 7690 Enc += ')'; 7691 return true; 7692 } 7693 7694 /// Appends array encoding to Enc before calling appendType for the element. 7695 static bool appendArrayType(SmallStringEnc &Enc, QualType QT, 7696 const ArrayType *AT, 7697 const CodeGen::CodeGenModule &CGM, 7698 TypeStringCache &TSC, StringRef NoSizeEnc) { 7699 if (AT->getSizeModifier() != ArrayType::Normal) 7700 return false; 7701 Enc += "a("; 7702 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 7703 CAT->getSize().toStringUnsigned(Enc); 7704 else 7705 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". 7706 Enc += ':'; 7707 // The Qualifiers should be attached to the type rather than the array. 7708 appendQualifier(Enc, QT); 7709 if (!appendType(Enc, AT->getElementType(), CGM, TSC)) 7710 return false; 7711 Enc += ')'; 7712 return true; 7713 } 7714 7715 /// Appends a function encoding to Enc, calling appendType for the return type 7716 /// and the arguments. 7717 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, 7718 const CodeGen::CodeGenModule &CGM, 7719 TypeStringCache &TSC) { 7720 Enc += "f{"; 7721 if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) 7722 return false; 7723 Enc += "}("; 7724 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { 7725 // N.B. we are only interested in the adjusted param types. 7726 auto I = FPT->param_type_begin(); 7727 auto E = FPT->param_type_end(); 7728 if (I != E) { 7729 do { 7730 if (!appendType(Enc, *I, CGM, TSC)) 7731 return false; 7732 ++I; 7733 if (I != E) 7734 Enc += ','; 7735 } while (I != E); 7736 if (FPT->isVariadic()) 7737 Enc += ",va"; 7738 } else { 7739 if (FPT->isVariadic()) 7740 Enc += "va"; 7741 else 7742 Enc += '0'; 7743 } 7744 } 7745 Enc += ')'; 7746 return true; 7747 } 7748 7749 /// Handles the type's qualifier before dispatching a call to handle specific 7750 /// type encodings. 7751 static bool appendType(SmallStringEnc &Enc, QualType QType, 7752 const CodeGen::CodeGenModule &CGM, 7753 TypeStringCache &TSC) { 7754 7755 QualType QT = QType.getCanonicalType(); 7756 7757 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) 7758 // The Qualifiers should be attached to the type rather than the array. 7759 // Thus we don't call appendQualifier() here. 7760 return appendArrayType(Enc, QT, AT, CGM, TSC, ""); 7761 7762 appendQualifier(Enc, QT); 7763 7764 if (const BuiltinType *BT = QT->getAs<BuiltinType>()) 7765 return appendBuiltinType(Enc, BT); 7766 7767 if (const PointerType *PT = QT->getAs<PointerType>()) 7768 return appendPointerType(Enc, PT, CGM, TSC); 7769 7770 if (const EnumType *ET = QT->getAs<EnumType>()) 7771 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); 7772 7773 if (const RecordType *RT = QT->getAsStructureType()) 7774 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 7775 7776 if (const RecordType *RT = QT->getAsUnionType()) 7777 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 7778 7779 if (const FunctionType *FT = QT->getAs<FunctionType>()) 7780 return appendFunctionType(Enc, FT, CGM, TSC); 7781 7782 return false; 7783 } 7784 7785 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 7786 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { 7787 if (!D) 7788 return false; 7789 7790 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 7791 if (FD->getLanguageLinkage() != CLanguageLinkage) 7792 return false; 7793 return appendType(Enc, FD->getType(), CGM, TSC); 7794 } 7795 7796 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 7797 if (VD->getLanguageLinkage() != CLanguageLinkage) 7798 return false; 7799 QualType QT = VD->getType().getCanonicalType(); 7800 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { 7801 // Global ArrayTypes are given a size of '*' if the size is unknown. 7802 // The Qualifiers should be attached to the type rather than the array. 7803 // Thus we don't call appendQualifier() here. 7804 return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); 7805 } 7806 return appendType(Enc, QT, CGM, TSC); 7807 } 7808 return false; 7809 } 7810 7811 7812 //===----------------------------------------------------------------------===// 7813 // Driver code 7814 //===----------------------------------------------------------------------===// 7815 7816 const llvm::Triple &CodeGenModule::getTriple() const { 7817 return getTarget().getTriple(); 7818 } 7819 7820 bool CodeGenModule::supportsCOMDAT() const { 7821 return getTriple().supportsCOMDAT(); 7822 } 7823 7824 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { 7825 if (TheTargetCodeGenInfo) 7826 return *TheTargetCodeGenInfo; 7827 7828 // Helper to set the unique_ptr while still keeping the return value. 7829 auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { 7830 this->TheTargetCodeGenInfo.reset(P); 7831 return *P; 7832 }; 7833 7834 const llvm::Triple &Triple = getTarget().getTriple(); 7835 switch (Triple.getArch()) { 7836 default: 7837 return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); 7838 7839 case llvm::Triple::le32: 7840 return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); 7841 case llvm::Triple::mips: 7842 case llvm::Triple::mipsel: 7843 if (Triple.getOS() == llvm::Triple::NaCl) 7844 return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); 7845 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); 7846 7847 case llvm::Triple::mips64: 7848 case llvm::Triple::mips64el: 7849 return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); 7850 7851 case llvm::Triple::aarch64: 7852 case llvm::Triple::aarch64_be: { 7853 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; 7854 if (getTarget().getABI() == "darwinpcs") 7855 Kind = AArch64ABIInfo::DarwinPCS; 7856 7857 return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); 7858 } 7859 7860 case llvm::Triple::wasm32: 7861 case llvm::Triple::wasm64: 7862 return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); 7863 7864 case llvm::Triple::arm: 7865 case llvm::Triple::armeb: 7866 case llvm::Triple::thumb: 7867 case llvm::Triple::thumbeb: { 7868 if (Triple.getOS() == llvm::Triple::Win32) { 7869 return SetCGInfo( 7870 new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); 7871 } 7872 7873 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; 7874 StringRef ABIStr = getTarget().getABI(); 7875 if (ABIStr == "apcs-gnu") 7876 Kind = ARMABIInfo::APCS; 7877 else if (ABIStr == "aapcs16") 7878 Kind = ARMABIInfo::AAPCS16_VFP; 7879 else if (CodeGenOpts.FloatABI == "hard" || 7880 (CodeGenOpts.FloatABI != "soft" && 7881 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 7882 Triple.getEnvironment() == llvm::Triple::EABIHF))) 7883 Kind = ARMABIInfo::AAPCS_VFP; 7884 7885 return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); 7886 } 7887 7888 case llvm::Triple::ppc: 7889 return SetCGInfo( 7890 new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft")); 7891 case llvm::Triple::ppc64: 7892 if (Triple.isOSBinFormatELF()) { 7893 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; 7894 if (getTarget().getABI() == "elfv2") 7895 Kind = PPC64_SVR4_ABIInfo::ELFv2; 7896 bool HasQPX = getTarget().getABI() == "elfv1-qpx"; 7897 7898 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); 7899 } else 7900 return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); 7901 case llvm::Triple::ppc64le: { 7902 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); 7903 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; 7904 if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") 7905 Kind = PPC64_SVR4_ABIInfo::ELFv1; 7906 bool HasQPX = getTarget().getABI() == "elfv1-qpx"; 7907 7908 return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); 7909 } 7910 7911 case llvm::Triple::nvptx: 7912 case llvm::Triple::nvptx64: 7913 return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); 7914 7915 case llvm::Triple::msp430: 7916 return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); 7917 7918 case llvm::Triple::systemz: { 7919 bool HasVector = getTarget().getABI() == "vector"; 7920 return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); 7921 } 7922 7923 case llvm::Triple::tce: 7924 return SetCGInfo(new TCETargetCodeGenInfo(Types)); 7925 7926 case llvm::Triple::x86: { 7927 bool IsDarwinVectorABI = Triple.isOSDarwin(); 7928 bool RetSmallStructInRegABI = 7929 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); 7930 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); 7931 7932 if (Triple.getOS() == llvm::Triple::Win32) { 7933 return SetCGInfo(new WinX86_32TargetCodeGenInfo( 7934 Types, IsDarwinVectorABI, RetSmallStructInRegABI, 7935 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); 7936 } else { 7937 return SetCGInfo(new X86_32TargetCodeGenInfo( 7938 Types, IsDarwinVectorABI, RetSmallStructInRegABI, 7939 IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, 7940 CodeGenOpts.FloatABI == "soft")); 7941 } 7942 } 7943 7944 case llvm::Triple::x86_64: { 7945 StringRef ABI = getTarget().getABI(); 7946 X86AVXABILevel AVXLevel = 7947 (ABI == "avx512" 7948 ? X86AVXABILevel::AVX512 7949 : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); 7950 7951 switch (Triple.getOS()) { 7952 case llvm::Triple::Win32: 7953 return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); 7954 case llvm::Triple::PS4: 7955 return SetCGInfo(new PS4TargetCodeGenInfo(Types, AVXLevel)); 7956 default: 7957 return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); 7958 } 7959 } 7960 case llvm::Triple::hexagon: 7961 return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); 7962 case llvm::Triple::lanai: 7963 return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); 7964 case llvm::Triple::r600: 7965 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); 7966 case llvm::Triple::amdgcn: 7967 return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); 7968 case llvm::Triple::sparcv9: 7969 return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); 7970 case llvm::Triple::xcore: 7971 return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); 7972 case llvm::Triple::spir: 7973 case llvm::Triple::spir64: 7974 return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); 7975 } 7976 } 7977