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