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