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