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 "CodeGenFunction.h" 19 #include "clang/AST/RecordLayout.h" 20 #include "clang/Frontend/CodeGenOptions.h" 21 #include "llvm/ADT/Triple.h" 22 #include "llvm/IR/DataLayout.h" 23 #include "llvm/IR/Type.h" 24 #include "llvm/Support/raw_ostream.h" 25 using namespace clang; 26 using namespace CodeGen; 27 28 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, 29 llvm::Value *Array, 30 llvm::Value *Value, 31 unsigned FirstIndex, 32 unsigned LastIndex) { 33 // Alternatively, we could emit this as a loop in the source. 34 for (unsigned I = FirstIndex; I <= LastIndex; ++I) { 35 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); 36 Builder.CreateStore(Value, Cell); 37 } 38 } 39 40 static bool isAggregateTypeForABI(QualType T) { 41 return !CodeGenFunction::hasScalarEvaluationKind(T) || 42 T->isMemberFunctionPointerType(); 43 } 44 45 ABIInfo::~ABIInfo() {} 46 47 static bool isRecordReturnIndirect(const RecordType *RT, CodeGen::CodeGenTypes &CGT) { 48 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 49 if (!RD) 50 return false; 51 return CGT.CGM.getCXXABI().isReturnTypeIndirect(RD); 52 } 53 54 55 static bool isRecordReturnIndirect(QualType T, CodeGen::CodeGenTypes &CGT) { 56 const RecordType *RT = T->getAs<RecordType>(); 57 if (!RT) 58 return false; 59 return isRecordReturnIndirect(RT, CGT); 60 } 61 62 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, 63 CodeGen::CodeGenTypes &CGT) { 64 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 65 if (!RD) 66 return CGCXXABI::RAA_Default; 67 return CGT.CGM.getCXXABI().getRecordArgABI(RD); 68 } 69 70 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, 71 CodeGen::CodeGenTypes &CGT) { 72 const RecordType *RT = T->getAs<RecordType>(); 73 if (!RT) 74 return CGCXXABI::RAA_Default; 75 return getRecordArgABI(RT, CGT); 76 } 77 78 ASTContext &ABIInfo::getContext() const { 79 return CGT.getContext(); 80 } 81 82 llvm::LLVMContext &ABIInfo::getVMContext() const { 83 return CGT.getLLVMContext(); 84 } 85 86 const llvm::DataLayout &ABIInfo::getDataLayout() const { 87 return CGT.getDataLayout(); 88 } 89 90 const TargetInfo &ABIInfo::getTarget() const { 91 return CGT.getTarget(); 92 } 93 94 void ABIArgInfo::dump() const { 95 raw_ostream &OS = llvm::errs(); 96 OS << "(ABIArgInfo Kind="; 97 switch (TheKind) { 98 case Direct: 99 OS << "Direct Type="; 100 if (llvm::Type *Ty = getCoerceToType()) 101 Ty->print(OS); 102 else 103 OS << "null"; 104 break; 105 case Extend: 106 OS << "Extend"; 107 break; 108 case Ignore: 109 OS << "Ignore"; 110 break; 111 case Indirect: 112 OS << "Indirect Align=" << getIndirectAlign() 113 << " ByVal=" << getIndirectByVal() 114 << " Realign=" << getIndirectRealign(); 115 break; 116 case Expand: 117 OS << "Expand"; 118 break; 119 } 120 OS << ")\n"; 121 } 122 123 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } 124 125 // If someone can figure out a general rule for this, that would be great. 126 // It's probably just doomed to be platform-dependent, though. 127 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { 128 // Verified for: 129 // x86-64 FreeBSD, Linux, Darwin 130 // x86-32 FreeBSD, Linux, Darwin 131 // PowerPC Linux, Darwin 132 // ARM Darwin (*not* EABI) 133 // AArch64 Linux 134 return 32; 135 } 136 137 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, 138 const FunctionNoProtoType *fnType) const { 139 // The following conventions are known to require this to be false: 140 // x86_stdcall 141 // MIPS 142 // For everything else, we just prefer false unless we opt out. 143 return false; 144 } 145 146 void 147 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, 148 llvm::SmallString<24> &Opt) const { 149 // This assumes the user is passing a library name like "rt" instead of a 150 // filename like "librt.a/so", and that they don't care whether it's static or 151 // dynamic. 152 Opt = "-l"; 153 Opt += Lib; 154 } 155 156 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); 157 158 /// isEmptyField - Return true iff a the field is "empty", that is it 159 /// is an unnamed bit-field or an (array of) empty record(s). 160 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, 161 bool AllowArrays) { 162 if (FD->isUnnamedBitfield()) 163 return true; 164 165 QualType FT = FD->getType(); 166 167 // Constant arrays of empty records count as empty, strip them off. 168 // Constant arrays of zero length always count as empty. 169 if (AllowArrays) 170 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 171 if (AT->getSize() == 0) 172 return true; 173 FT = AT->getElementType(); 174 } 175 176 const RecordType *RT = FT->getAs<RecordType>(); 177 if (!RT) 178 return false; 179 180 // C++ record fields are never empty, at least in the Itanium ABI. 181 // 182 // FIXME: We should use a predicate for whether this behavior is true in the 183 // current ABI. 184 if (isa<CXXRecordDecl>(RT->getDecl())) 185 return false; 186 187 return isEmptyRecord(Context, FT, AllowArrays); 188 } 189 190 /// isEmptyRecord - Return true iff a structure contains only empty 191 /// fields. Note that a structure with a flexible array member is not 192 /// considered empty. 193 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { 194 const RecordType *RT = T->getAs<RecordType>(); 195 if (!RT) 196 return 0; 197 const RecordDecl *RD = RT->getDecl(); 198 if (RD->hasFlexibleArrayMember()) 199 return false; 200 201 // If this is a C++ record, check the bases first. 202 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 203 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 204 e = CXXRD->bases_end(); i != e; ++i) 205 if (!isEmptyRecord(Context, i->getType(), true)) 206 return false; 207 208 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 209 i != e; ++i) 210 if (!isEmptyField(Context, *i, AllowArrays)) 211 return false; 212 return true; 213 } 214 215 /// isSingleElementStruct - Determine if a structure is a "single 216 /// element struct", i.e. it has exactly one non-empty field or 217 /// exactly one field which is itself a single element 218 /// struct. Structures with flexible array members are never 219 /// considered single element structs. 220 /// 221 /// \return The field declaration for the single non-empty field, if 222 /// it exists. 223 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { 224 const RecordType *RT = T->getAsStructureType(); 225 if (!RT) 226 return 0; 227 228 const RecordDecl *RD = RT->getDecl(); 229 if (RD->hasFlexibleArrayMember()) 230 return 0; 231 232 const Type *Found = 0; 233 234 // If this is a C++ record, check the bases first. 235 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 236 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 237 e = CXXRD->bases_end(); i != e; ++i) { 238 // Ignore empty records. 239 if (isEmptyRecord(Context, i->getType(), true)) 240 continue; 241 242 // If we already found an element then this isn't a single-element struct. 243 if (Found) 244 return 0; 245 246 // If this is non-empty and not a single element struct, the composite 247 // cannot be a single element struct. 248 Found = isSingleElementStruct(i->getType(), Context); 249 if (!Found) 250 return 0; 251 } 252 } 253 254 // Check for single element. 255 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 256 i != e; ++i) { 257 const FieldDecl *FD = *i; 258 QualType FT = FD->getType(); 259 260 // Ignore empty fields. 261 if (isEmptyField(Context, FD, true)) 262 continue; 263 264 // If we already found an element then this isn't a single-element 265 // struct. 266 if (Found) 267 return 0; 268 269 // Treat single element arrays as the element. 270 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 271 if (AT->getSize().getZExtValue() != 1) 272 break; 273 FT = AT->getElementType(); 274 } 275 276 if (!isAggregateTypeForABI(FT)) { 277 Found = FT.getTypePtr(); 278 } else { 279 Found = isSingleElementStruct(FT, Context); 280 if (!Found) 281 return 0; 282 } 283 } 284 285 // We don't consider a struct a single-element struct if it has 286 // padding beyond the element type. 287 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) 288 return 0; 289 290 return Found; 291 } 292 293 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { 294 // Treat complex types as the element type. 295 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 296 Ty = CTy->getElementType(); 297 298 // Check for a type which we know has a simple scalar argument-passing 299 // convention without any padding. (We're specifically looking for 32 300 // and 64-bit integer and integer-equivalents, float, and double.) 301 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && 302 !Ty->isEnumeralType() && !Ty->isBlockPointerType()) 303 return false; 304 305 uint64_t Size = Context.getTypeSize(Ty); 306 return Size == 32 || Size == 64; 307 } 308 309 /// canExpandIndirectArgument - Test whether an argument type which is to be 310 /// passed indirectly (on the stack) would have the equivalent layout if it was 311 /// expanded into separate arguments. If so, we prefer to do the latter to avoid 312 /// inhibiting optimizations. 313 /// 314 // FIXME: This predicate is missing many cases, currently it just follows 315 // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We 316 // should probably make this smarter, or better yet make the LLVM backend 317 // capable of handling it. 318 static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { 319 // We can only expand structure types. 320 const RecordType *RT = Ty->getAs<RecordType>(); 321 if (!RT) 322 return false; 323 324 // We can only expand (C) structures. 325 // 326 // FIXME: This needs to be generalized to handle classes as well. 327 const RecordDecl *RD = RT->getDecl(); 328 if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) 329 return false; 330 331 uint64_t Size = 0; 332 333 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 334 i != e; ++i) { 335 const FieldDecl *FD = *i; 336 337 if (!is32Or64BitBasicType(FD->getType(), Context)) 338 return false; 339 340 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know 341 // how to expand them yet, and the predicate for telling if a bitfield still 342 // counts as "basic" is more complicated than what we were doing previously. 343 if (FD->isBitField()) 344 return false; 345 346 Size += Context.getTypeSize(FD->getType()); 347 } 348 349 // Make sure there are not any holes in the struct. 350 if (Size != Context.getTypeSize(Ty)) 351 return false; 352 353 return true; 354 } 355 356 namespace { 357 /// DefaultABIInfo - The default implementation for ABI specific 358 /// details. This implementation provides information which results in 359 /// self-consistent and sensible LLVM IR generation, but does not 360 /// conform to any particular ABI. 361 class DefaultABIInfo : public ABIInfo { 362 public: 363 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 364 365 ABIArgInfo classifyReturnType(QualType RetTy) const; 366 ABIArgInfo classifyArgumentType(QualType RetTy) const; 367 368 virtual void computeInfo(CGFunctionInfo &FI) const { 369 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 370 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 371 it != ie; ++it) 372 it->info = classifyArgumentType(it->type); 373 } 374 375 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 376 CodeGenFunction &CGF) const; 377 }; 378 379 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { 380 public: 381 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 382 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 383 }; 384 385 llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 386 CodeGenFunction &CGF) const { 387 return 0; 388 } 389 390 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { 391 if (isAggregateTypeForABI(Ty)) { 392 // Records with non trivial destructors/constructors should not be passed 393 // by value. 394 if (isRecordReturnIndirect(Ty, CGT)) 395 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 396 397 return ABIArgInfo::getIndirect(0); 398 } 399 400 // Treat an enum type as its underlying type. 401 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 402 Ty = EnumTy->getDecl()->getIntegerType(); 403 404 return (Ty->isPromotableIntegerType() ? 405 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 406 } 407 408 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { 409 if (RetTy->isVoidType()) 410 return ABIArgInfo::getIgnore(); 411 412 if (isAggregateTypeForABI(RetTy)) 413 return ABIArgInfo::getIndirect(0); 414 415 // Treat an enum type as its underlying type. 416 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 417 RetTy = EnumTy->getDecl()->getIntegerType(); 418 419 return (RetTy->isPromotableIntegerType() ? 420 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 421 } 422 423 //===----------------------------------------------------------------------===// 424 // le32/PNaCl bitcode ABI Implementation 425 // 426 // This is a simplified version of the x86_32 ABI. Arguments and return values 427 // are always passed on the stack. 428 //===----------------------------------------------------------------------===// 429 430 class PNaClABIInfo : public ABIInfo { 431 public: 432 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 433 434 ABIArgInfo classifyReturnType(QualType RetTy) const; 435 ABIArgInfo classifyArgumentType(QualType RetTy) const; 436 437 virtual void computeInfo(CGFunctionInfo &FI) const; 438 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 439 CodeGenFunction &CGF) const; 440 }; 441 442 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { 443 public: 444 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 445 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} 446 }; 447 448 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { 449 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 450 451 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 452 it != ie; ++it) 453 it->info = classifyArgumentType(it->type); 454 } 455 456 llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 457 CodeGenFunction &CGF) const { 458 return 0; 459 } 460 461 /// \brief Classify argument of given type \p Ty. 462 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { 463 if (isAggregateTypeForABI(Ty)) { 464 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) 465 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 466 return ABIArgInfo::getIndirect(0); 467 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 468 // Treat an enum type as its underlying type. 469 Ty = EnumTy->getDecl()->getIntegerType(); 470 } else if (Ty->isFloatingType()) { 471 // Floating-point types don't go inreg. 472 return ABIArgInfo::getDirect(); 473 } 474 475 return (Ty->isPromotableIntegerType() ? 476 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 477 } 478 479 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { 480 if (RetTy->isVoidType()) 481 return ABIArgInfo::getIgnore(); 482 483 // In the PNaCl ABI we always return records/structures on the stack. 484 if (isAggregateTypeForABI(RetTy)) 485 return ABIArgInfo::getIndirect(0); 486 487 // Treat an enum type as its underlying type. 488 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 489 RetTy = EnumTy->getDecl()->getIntegerType(); 490 491 return (RetTy->isPromotableIntegerType() ? 492 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 493 } 494 495 /// IsX86_MMXType - Return true if this is an MMX type. 496 bool IsX86_MMXType(llvm::Type *IRType) { 497 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. 498 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && 499 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && 500 IRType->getScalarSizeInBits() != 64; 501 } 502 503 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 504 StringRef Constraint, 505 llvm::Type* Ty) { 506 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { 507 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { 508 // Invalid MMX constraint 509 return 0; 510 } 511 512 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); 513 } 514 515 // No operation needed 516 return Ty; 517 } 518 519 //===----------------------------------------------------------------------===// 520 // X86-32 ABI Implementation 521 //===----------------------------------------------------------------------===// 522 523 /// X86_32ABIInfo - The X86-32 ABI information. 524 class X86_32ABIInfo : public ABIInfo { 525 enum Class { 526 Integer, 527 Float 528 }; 529 530 static const unsigned MinABIStackAlignInBytes = 4; 531 532 bool IsDarwinVectorABI; 533 bool IsSmallStructInRegABI; 534 bool IsWin32StructABI; 535 unsigned DefaultNumRegisterParameters; 536 537 static bool isRegisterSize(unsigned Size) { 538 return (Size == 8 || Size == 16 || Size == 32 || Size == 64); 539 } 540 541 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, 542 unsigned callingConvention); 543 544 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 545 /// such that the argument will be passed in memory. 546 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, 547 unsigned &FreeRegs) const; 548 549 /// \brief Return the alignment to use for the given type on the stack. 550 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; 551 552 Class classify(QualType Ty) const; 553 ABIArgInfo classifyReturnType(QualType RetTy, 554 unsigned callingConvention) const; 555 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs, 556 bool IsFastCall) const; 557 bool shouldUseInReg(QualType Ty, unsigned &FreeRegs, 558 bool IsFastCall, bool &NeedsPadding) const; 559 560 public: 561 562 virtual void computeInfo(CGFunctionInfo &FI) const; 563 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 564 CodeGenFunction &CGF) const; 565 566 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, 567 unsigned r) 568 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), 569 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} 570 }; 571 572 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { 573 public: 574 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 575 bool d, bool p, bool w, unsigned r) 576 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} 577 578 static bool isStructReturnInRegABI( 579 const llvm::Triple &Triple, const CodeGenOptions &Opts); 580 581 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 582 CodeGen::CodeGenModule &CGM) const; 583 584 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 585 // Darwin uses different dwarf register numbers for EH. 586 if (CGM.getTarget().getTriple().isOSDarwin()) return 5; 587 return 4; 588 } 589 590 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 591 llvm::Value *Address) const; 592 593 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 594 StringRef Constraint, 595 llvm::Type* Ty) const { 596 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 597 } 598 599 }; 600 601 } 602 603 /// shouldReturnTypeInRegister - Determine if the given type should be 604 /// passed in a register (for the Darwin ABI). 605 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, 606 ASTContext &Context, 607 unsigned callingConvention) { 608 uint64_t Size = Context.getTypeSize(Ty); 609 610 // Type must be register sized. 611 if (!isRegisterSize(Size)) 612 return false; 613 614 if (Ty->isVectorType()) { 615 // 64- and 128- bit vectors inside structures are not returned in 616 // registers. 617 if (Size == 64 || Size == 128) 618 return false; 619 620 return true; 621 } 622 623 // If this is a builtin, pointer, enum, complex type, member pointer, or 624 // member function pointer it is ok. 625 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || 626 Ty->isAnyComplexType() || Ty->isEnumeralType() || 627 Ty->isBlockPointerType() || Ty->isMemberPointerType()) 628 return true; 629 630 // Arrays are treated like records. 631 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) 632 return shouldReturnTypeInRegister(AT->getElementType(), Context, 633 callingConvention); 634 635 // Otherwise, it must be a record type. 636 const RecordType *RT = Ty->getAs<RecordType>(); 637 if (!RT) return false; 638 639 // FIXME: Traverse bases here too. 640 641 // For thiscall conventions, structures will never be returned in 642 // a register. This is for compatibility with the MSVC ABI 643 if (callingConvention == llvm::CallingConv::X86_ThisCall && 644 RT->isStructureType()) { 645 return false; 646 } 647 648 // Structure types are passed in register if all fields would be 649 // passed in a register. 650 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), 651 e = RT->getDecl()->field_end(); i != e; ++i) { 652 const FieldDecl *FD = *i; 653 654 // Empty fields are ignored. 655 if (isEmptyField(Context, FD, true)) 656 continue; 657 658 // Check fields recursively. 659 if (!shouldReturnTypeInRegister(FD->getType(), Context, 660 callingConvention)) 661 return false; 662 } 663 return true; 664 } 665 666 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, 667 unsigned callingConvention) const { 668 if (RetTy->isVoidType()) 669 return ABIArgInfo::getIgnore(); 670 671 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 672 // On Darwin, some vectors are returned in registers. 673 if (IsDarwinVectorABI) { 674 uint64_t Size = getContext().getTypeSize(RetTy); 675 676 // 128-bit vectors are a special case; they are returned in 677 // registers and we need to make sure to pick a type the LLVM 678 // backend will like. 679 if (Size == 128) 680 return ABIArgInfo::getDirect(llvm::VectorType::get( 681 llvm::Type::getInt64Ty(getVMContext()), 2)); 682 683 // Always return in register if it fits in a general purpose 684 // register, or if it is 64 bits and has a single element. 685 if ((Size == 8 || Size == 16 || Size == 32) || 686 (Size == 64 && VT->getNumElements() == 1)) 687 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 688 Size)); 689 690 return ABIArgInfo::getIndirect(0); 691 } 692 693 return ABIArgInfo::getDirect(); 694 } 695 696 if (isAggregateTypeForABI(RetTy)) { 697 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 698 if (isRecordReturnIndirect(RT, CGT)) 699 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 700 701 // Structures with flexible arrays are always indirect. 702 if (RT->getDecl()->hasFlexibleArrayMember()) 703 return ABIArgInfo::getIndirect(0); 704 } 705 706 // If specified, structs and unions are always indirect. 707 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) 708 return ABIArgInfo::getIndirect(0); 709 710 // Small structures which are register sized are generally returned 711 // in a register. 712 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(), 713 callingConvention)) { 714 uint64_t Size = getContext().getTypeSize(RetTy); 715 716 // As a special-case, if the struct is a "single-element" struct, and 717 // the field is of type "float" or "double", return it in a 718 // floating-point register. (MSVC does not apply this special case.) 719 // We apply a similar transformation for pointer types to improve the 720 // quality of the generated IR. 721 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 722 if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) 723 || SeltTy->hasPointerRepresentation()) 724 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 725 726 // FIXME: We should be able to narrow this integer in cases with dead 727 // padding. 728 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); 729 } 730 731 return ABIArgInfo::getIndirect(0); 732 } 733 734 // Treat an enum type as its underlying type. 735 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 736 RetTy = EnumTy->getDecl()->getIntegerType(); 737 738 return (RetTy->isPromotableIntegerType() ? 739 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 740 } 741 742 static bool isSSEVectorType(ASTContext &Context, QualType Ty) { 743 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; 744 } 745 746 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { 747 const RecordType *RT = Ty->getAs<RecordType>(); 748 if (!RT) 749 return 0; 750 const RecordDecl *RD = RT->getDecl(); 751 752 // If this is a C++ record, check the bases first. 753 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 754 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 755 e = CXXRD->bases_end(); i != e; ++i) 756 if (!isRecordWithSSEVectorType(Context, i->getType())) 757 return false; 758 759 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 760 i != e; ++i) { 761 QualType FT = i->getType(); 762 763 if (isSSEVectorType(Context, FT)) 764 return true; 765 766 if (isRecordWithSSEVectorType(Context, FT)) 767 return true; 768 } 769 770 return false; 771 } 772 773 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, 774 unsigned Align) const { 775 // Otherwise, if the alignment is less than or equal to the minimum ABI 776 // alignment, just use the default; the backend will handle this. 777 if (Align <= MinABIStackAlignInBytes) 778 return 0; // Use default alignment. 779 780 // On non-Darwin, the stack type alignment is always 4. 781 if (!IsDarwinVectorABI) { 782 // Set explicit alignment, since we may need to realign the top. 783 return MinABIStackAlignInBytes; 784 } 785 786 // Otherwise, if the type contains an SSE vector type, the alignment is 16. 787 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || 788 isRecordWithSSEVectorType(getContext(), Ty))) 789 return 16; 790 791 return MinABIStackAlignInBytes; 792 } 793 794 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, 795 unsigned &FreeRegs) const { 796 if (!ByVal) { 797 if (FreeRegs) { 798 --FreeRegs; // Non byval indirects just use one pointer. 799 return ABIArgInfo::getIndirectInReg(0, false); 800 } 801 return ABIArgInfo::getIndirect(0, false); 802 } 803 804 // Compute the byval alignment. 805 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 806 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); 807 if (StackAlign == 0) 808 return ABIArgInfo::getIndirect(4); 809 810 // If the stack alignment is less than the type alignment, realign the 811 // argument. 812 if (StackAlign < TypeAlign) 813 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, 814 /*Realign=*/true); 815 816 return ABIArgInfo::getIndirect(StackAlign); 817 } 818 819 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { 820 const Type *T = isSingleElementStruct(Ty, getContext()); 821 if (!T) 822 T = Ty.getTypePtr(); 823 824 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 825 BuiltinType::Kind K = BT->getKind(); 826 if (K == BuiltinType::Float || K == BuiltinType::Double) 827 return Float; 828 } 829 return Integer; 830 } 831 832 bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs, 833 bool IsFastCall, bool &NeedsPadding) const { 834 NeedsPadding = false; 835 Class C = classify(Ty); 836 if (C == Float) 837 return false; 838 839 unsigned Size = getContext().getTypeSize(Ty); 840 unsigned SizeInRegs = (Size + 31) / 32; 841 842 if (SizeInRegs == 0) 843 return false; 844 845 if (SizeInRegs > FreeRegs) { 846 FreeRegs = 0; 847 return false; 848 } 849 850 FreeRegs -= SizeInRegs; 851 852 if (IsFastCall) { 853 if (Size > 32) 854 return false; 855 856 if (Ty->isIntegralOrEnumerationType()) 857 return true; 858 859 if (Ty->isPointerType()) 860 return true; 861 862 if (Ty->isReferenceType()) 863 return true; 864 865 if (FreeRegs) 866 NeedsPadding = true; 867 868 return false; 869 } 870 871 return true; 872 } 873 874 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, 875 unsigned &FreeRegs, 876 bool IsFastCall) const { 877 // FIXME: Set alignment on indirect arguments. 878 if (isAggregateTypeForABI(Ty)) { 879 if (const RecordType *RT = Ty->getAs<RecordType>()) { 880 if (IsWin32StructABI) 881 return getIndirectResult(Ty, true, FreeRegs); 882 883 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT)) 884 return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs); 885 886 // Structures with flexible arrays are always indirect. 887 if (RT->getDecl()->hasFlexibleArrayMember()) 888 return getIndirectResult(Ty, true, FreeRegs); 889 } 890 891 // Ignore empty structs/unions. 892 if (isEmptyRecord(getContext(), Ty, true)) 893 return ABIArgInfo::getIgnore(); 894 895 llvm::LLVMContext &LLVMContext = getVMContext(); 896 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 897 bool NeedsPadding; 898 if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) { 899 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; 900 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); 901 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 902 return ABIArgInfo::getDirectInReg(Result); 903 } 904 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0; 905 906 // Expand small (<= 128-bit) record types when we know that the stack layout 907 // of those arguments will match the struct. This is important because the 908 // LLVM backend isn't smart enough to remove byval, which inhibits many 909 // optimizations. 910 if (getContext().getTypeSize(Ty) <= 4*32 && 911 canExpandIndirectArgument(Ty, getContext())) 912 return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType); 913 914 return getIndirectResult(Ty, true, FreeRegs); 915 } 916 917 if (const VectorType *VT = Ty->getAs<VectorType>()) { 918 // On Darwin, some vectors are passed in memory, we handle this by passing 919 // it as an i8/i16/i32/i64. 920 if (IsDarwinVectorABI) { 921 uint64_t Size = getContext().getTypeSize(Ty); 922 if ((Size == 8 || Size == 16 || Size == 32) || 923 (Size == 64 && VT->getNumElements() == 1)) 924 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 925 Size)); 926 } 927 928 if (IsX86_MMXType(CGT.ConvertType(Ty))) 929 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); 930 931 return ABIArgInfo::getDirect(); 932 } 933 934 935 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 936 Ty = EnumTy->getDecl()->getIntegerType(); 937 938 bool NeedsPadding; 939 bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding); 940 941 if (Ty->isPromotableIntegerType()) { 942 if (InReg) 943 return ABIArgInfo::getExtendInReg(); 944 return ABIArgInfo::getExtend(); 945 } 946 if (InReg) 947 return ABIArgInfo::getDirectInReg(); 948 return ABIArgInfo::getDirect(); 949 } 950 951 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { 952 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), 953 FI.getCallingConvention()); 954 955 unsigned CC = FI.getCallingConvention(); 956 bool IsFastCall = CC == llvm::CallingConv::X86_FastCall; 957 unsigned FreeRegs; 958 if (IsFastCall) 959 FreeRegs = 2; 960 else if (FI.getHasRegParm()) 961 FreeRegs = FI.getRegParm(); 962 else 963 FreeRegs = DefaultNumRegisterParameters; 964 965 // If the return value is indirect, then the hidden argument is consuming one 966 // integer register. 967 if (FI.getReturnInfo().isIndirect() && FreeRegs) { 968 --FreeRegs; 969 ABIArgInfo &Old = FI.getReturnInfo(); 970 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(), 971 Old.getIndirectByVal(), 972 Old.getIndirectRealign()); 973 } 974 975 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 976 it != ie; ++it) 977 it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall); 978 } 979 980 llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 981 CodeGenFunction &CGF) const { 982 llvm::Type *BPP = CGF.Int8PtrPtrTy; 983 984 CGBuilderTy &Builder = CGF.Builder; 985 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 986 "ap"); 987 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 988 989 // Compute if the address needs to be aligned 990 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); 991 Align = getTypeStackAlignInBytes(Ty, Align); 992 Align = std::max(Align, 4U); 993 if (Align > 4) { 994 // addr = (addr + align - 1) & -align; 995 llvm::Value *Offset = 996 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); 997 Addr = CGF.Builder.CreateGEP(Addr, Offset); 998 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, 999 CGF.Int32Ty); 1000 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); 1001 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), 1002 Addr->getType(), 1003 "ap.cur.aligned"); 1004 } 1005 1006 llvm::Type *PTy = 1007 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 1008 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 1009 1010 uint64_t Offset = 1011 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); 1012 llvm::Value *NextAddr = 1013 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 1014 "ap.next"); 1015 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 1016 1017 return AddrTyped; 1018 } 1019 1020 void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, 1021 llvm::GlobalValue *GV, 1022 CodeGen::CodeGenModule &CGM) const { 1023 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1024 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 1025 // Get the LLVM function. 1026 llvm::Function *Fn = cast<llvm::Function>(GV); 1027 1028 // Now add the 'alignstack' attribute with a value of 16. 1029 llvm::AttrBuilder B; 1030 B.addStackAlignmentAttr(16); 1031 Fn->addAttributes(llvm::AttributeSet::FunctionIndex, 1032 llvm::AttributeSet::get(CGM.getLLVMContext(), 1033 llvm::AttributeSet::FunctionIndex, 1034 B)); 1035 } 1036 } 1037 } 1038 1039 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( 1040 CodeGen::CodeGenFunction &CGF, 1041 llvm::Value *Address) const { 1042 CodeGen::CGBuilderTy &Builder = CGF.Builder; 1043 1044 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 1045 1046 // 0-7 are the eight integer registers; the order is different 1047 // on Darwin (for EH), but the range is the same. 1048 // 8 is %eip. 1049 AssignToArrayRange(Builder, Address, Four8, 0, 8); 1050 1051 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { 1052 // 12-16 are st(0..4). Not sure why we stop at 4. 1053 // These have size 16, which is sizeof(long double) on 1054 // platforms with 8-byte alignment for that type. 1055 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); 1056 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); 1057 1058 } else { 1059 // 9 is %eflags, which doesn't get a size on Darwin for some 1060 // reason. 1061 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); 1062 1063 // 11-16 are st(0..5). Not sure why we stop at 5. 1064 // These have size 12, which is sizeof(long double) on 1065 // platforms with 4-byte alignment for that type. 1066 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); 1067 AssignToArrayRange(Builder, Address, Twelve8, 11, 16); 1068 } 1069 1070 return false; 1071 } 1072 1073 //===----------------------------------------------------------------------===// 1074 // X86-64 ABI Implementation 1075 //===----------------------------------------------------------------------===// 1076 1077 1078 namespace { 1079 /// X86_64ABIInfo - The X86_64 ABI information. 1080 class X86_64ABIInfo : public ABIInfo { 1081 enum Class { 1082 Integer = 0, 1083 SSE, 1084 SSEUp, 1085 X87, 1086 X87Up, 1087 ComplexX87, 1088 NoClass, 1089 Memory 1090 }; 1091 1092 /// merge - Implement the X86_64 ABI merging algorithm. 1093 /// 1094 /// Merge an accumulating classification \arg Accum with a field 1095 /// classification \arg Field. 1096 /// 1097 /// \param Accum - The accumulating classification. This should 1098 /// always be either NoClass or the result of a previous merge 1099 /// call. In addition, this should never be Memory (the caller 1100 /// should just return Memory for the aggregate). 1101 static Class merge(Class Accum, Class Field); 1102 1103 /// postMerge - Implement the X86_64 ABI post merging algorithm. 1104 /// 1105 /// Post merger cleanup, reduces a malformed Hi and Lo pair to 1106 /// final MEMORY or SSE classes when necessary. 1107 /// 1108 /// \param AggregateSize - The size of the current aggregate in 1109 /// the classification process. 1110 /// 1111 /// \param Lo - The classification for the parts of the type 1112 /// residing in the low word of the containing object. 1113 /// 1114 /// \param Hi - The classification for the parts of the type 1115 /// residing in the higher words of the containing object. 1116 /// 1117 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; 1118 1119 /// classify - Determine the x86_64 register classes in which the 1120 /// given type T should be passed. 1121 /// 1122 /// \param Lo - The classification for the parts of the type 1123 /// residing in the low word of the containing object. 1124 /// 1125 /// \param Hi - The classification for the parts of the type 1126 /// residing in the high word of the containing object. 1127 /// 1128 /// \param OffsetBase - The bit offset of this type in the 1129 /// containing object. Some parameters are classified different 1130 /// depending on whether they straddle an eightbyte boundary. 1131 /// 1132 /// \param isNamedArg - Whether the argument in question is a "named" 1133 /// argument, as used in AMD64-ABI 3.5.7. 1134 /// 1135 /// If a word is unused its result will be NoClass; if a type should 1136 /// be passed in Memory then at least the classification of \arg Lo 1137 /// will be Memory. 1138 /// 1139 /// The \arg Lo class will be NoClass iff the argument is ignored. 1140 /// 1141 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will 1142 /// also be ComplexX87. 1143 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, 1144 bool isNamedArg) const; 1145 1146 llvm::Type *GetByteVectorType(QualType Ty) const; 1147 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, 1148 unsigned IROffset, QualType SourceTy, 1149 unsigned SourceOffset) const; 1150 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, 1151 unsigned IROffset, QualType SourceTy, 1152 unsigned SourceOffset) const; 1153 1154 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1155 /// such that the argument will be returned in memory. 1156 ABIArgInfo getIndirectReturnResult(QualType Ty) const; 1157 1158 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1159 /// such that the argument will be passed in memory. 1160 /// 1161 /// \param freeIntRegs - The number of free integer registers remaining 1162 /// available. 1163 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; 1164 1165 ABIArgInfo classifyReturnType(QualType RetTy) const; 1166 1167 ABIArgInfo classifyArgumentType(QualType Ty, 1168 unsigned freeIntRegs, 1169 unsigned &neededInt, 1170 unsigned &neededSSE, 1171 bool isNamedArg) const; 1172 1173 bool IsIllegalVectorType(QualType Ty) const; 1174 1175 /// The 0.98 ABI revision clarified a lot of ambiguities, 1176 /// unfortunately in ways that were not always consistent with 1177 /// certain previous compilers. In particular, platforms which 1178 /// required strict binary compatibility with older versions of GCC 1179 /// may need to exempt themselves. 1180 bool honorsRevision0_98() const { 1181 return !getTarget().getTriple().isOSDarwin(); 1182 } 1183 1184 bool HasAVX; 1185 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on 1186 // 64-bit hardware. 1187 bool Has64BitPointers; 1188 1189 public: 1190 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : 1191 ABIInfo(CGT), HasAVX(hasavx), 1192 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { 1193 } 1194 1195 bool isPassedUsingAVXType(QualType type) const { 1196 unsigned neededInt, neededSSE; 1197 // The freeIntRegs argument doesn't matter here. 1198 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, 1199 /*isNamedArg*/true); 1200 if (info.isDirect()) { 1201 llvm::Type *ty = info.getCoerceToType(); 1202 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) 1203 return (vectorTy->getBitWidth() > 128); 1204 } 1205 return false; 1206 } 1207 1208 virtual void computeInfo(CGFunctionInfo &FI) const; 1209 1210 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 1211 CodeGenFunction &CGF) const; 1212 }; 1213 1214 /// WinX86_64ABIInfo - The Windows X86_64 ABI information. 1215 class WinX86_64ABIInfo : public ABIInfo { 1216 1217 ABIArgInfo classify(QualType Ty, bool IsReturnType) const; 1218 1219 public: 1220 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 1221 1222 virtual void computeInfo(CGFunctionInfo &FI) const; 1223 1224 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 1225 CodeGenFunction &CGF) const; 1226 }; 1227 1228 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { 1229 public: 1230 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 1231 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} 1232 1233 const X86_64ABIInfo &getABIInfo() const { 1234 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); 1235 } 1236 1237 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 1238 return 7; 1239 } 1240 1241 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 1242 llvm::Value *Address) const { 1243 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 1244 1245 // 0-15 are the 16 integer registers. 1246 // 16 is %rip. 1247 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 1248 return false; 1249 } 1250 1251 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 1252 StringRef Constraint, 1253 llvm::Type* Ty) const { 1254 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 1255 } 1256 1257 bool isNoProtoCallVariadic(const CallArgList &args, 1258 const FunctionNoProtoType *fnType) const { 1259 // The default CC on x86-64 sets %al to the number of SSA 1260 // registers used, and GCC sets this when calling an unprototyped 1261 // function, so we override the default behavior. However, don't do 1262 // that when AVX types are involved: the ABI explicitly states it is 1263 // undefined, and it doesn't work in practice because of how the ABI 1264 // defines varargs anyway. 1265 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) { 1266 bool HasAVXType = false; 1267 for (CallArgList::const_iterator 1268 it = args.begin(), ie = args.end(); it != ie; ++it) { 1269 if (getABIInfo().isPassedUsingAVXType(it->Ty)) { 1270 HasAVXType = true; 1271 break; 1272 } 1273 } 1274 1275 if (!HasAVXType) 1276 return true; 1277 } 1278 1279 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); 1280 } 1281 1282 }; 1283 1284 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { 1285 // If the argument does not end in .lib, automatically add the suffix. This 1286 // matches the behavior of MSVC. 1287 std::string ArgStr = Lib; 1288 if (Lib.size() <= 4 || 1289 Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) { 1290 ArgStr += ".lib"; 1291 } 1292 return ArgStr; 1293 } 1294 1295 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { 1296 public: 1297 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 1298 bool d, bool p, bool w, unsigned RegParms) 1299 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} 1300 1301 void getDependentLibraryOption(llvm::StringRef Lib, 1302 llvm::SmallString<24> &Opt) const { 1303 Opt = "/DEFAULTLIB:"; 1304 Opt += qualifyWindowsLibrary(Lib); 1305 } 1306 1307 void getDetectMismatchOption(llvm::StringRef Name, 1308 llvm::StringRef Value, 1309 llvm::SmallString<32> &Opt) const { 1310 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 1311 } 1312 }; 1313 1314 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 1315 public: 1316 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 1317 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} 1318 1319 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 1320 return 7; 1321 } 1322 1323 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 1324 llvm::Value *Address) const { 1325 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 1326 1327 // 0-15 are the 16 integer registers. 1328 // 16 is %rip. 1329 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 1330 return false; 1331 } 1332 1333 void getDependentLibraryOption(llvm::StringRef Lib, 1334 llvm::SmallString<24> &Opt) const { 1335 Opt = "/DEFAULTLIB:"; 1336 Opt += qualifyWindowsLibrary(Lib); 1337 } 1338 1339 void getDetectMismatchOption(llvm::StringRef Name, 1340 llvm::StringRef Value, 1341 llvm::SmallString<32> &Opt) const { 1342 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 1343 } 1344 }; 1345 1346 } 1347 1348 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, 1349 Class &Hi) const { 1350 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: 1351 // 1352 // (a) If one of the classes is Memory, the whole argument is passed in 1353 // memory. 1354 // 1355 // (b) If X87UP is not preceded by X87, the whole argument is passed in 1356 // memory. 1357 // 1358 // (c) If the size of the aggregate exceeds two eightbytes and the first 1359 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole 1360 // argument is passed in memory. NOTE: This is necessary to keep the 1361 // ABI working for processors that don't support the __m256 type. 1362 // 1363 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. 1364 // 1365 // Some of these are enforced by the merging logic. Others can arise 1366 // only with unions; for example: 1367 // union { _Complex double; unsigned; } 1368 // 1369 // Note that clauses (b) and (c) were added in 0.98. 1370 // 1371 if (Hi == Memory) 1372 Lo = Memory; 1373 if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) 1374 Lo = Memory; 1375 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) 1376 Lo = Memory; 1377 if (Hi == SSEUp && Lo != SSE) 1378 Hi = SSE; 1379 } 1380 1381 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { 1382 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is 1383 // classified recursively so that always two fields are 1384 // considered. The resulting class is calculated according to 1385 // the classes of the fields in the eightbyte: 1386 // 1387 // (a) If both classes are equal, this is the resulting class. 1388 // 1389 // (b) If one of the classes is NO_CLASS, the resulting class is 1390 // the other class. 1391 // 1392 // (c) If one of the classes is MEMORY, the result is the MEMORY 1393 // class. 1394 // 1395 // (d) If one of the classes is INTEGER, the result is the 1396 // INTEGER. 1397 // 1398 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, 1399 // MEMORY is used as class. 1400 // 1401 // (f) Otherwise class SSE is used. 1402 1403 // Accum should never be memory (we should have returned) or 1404 // ComplexX87 (because this cannot be passed in a structure). 1405 assert((Accum != Memory && Accum != ComplexX87) && 1406 "Invalid accumulated classification during merge."); 1407 if (Accum == Field || Field == NoClass) 1408 return Accum; 1409 if (Field == Memory) 1410 return Memory; 1411 if (Accum == NoClass) 1412 return Field; 1413 if (Accum == Integer || Field == Integer) 1414 return Integer; 1415 if (Field == X87 || Field == X87Up || Field == ComplexX87 || 1416 Accum == X87 || Accum == X87Up) 1417 return Memory; 1418 return SSE; 1419 } 1420 1421 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, 1422 Class &Lo, Class &Hi, bool isNamedArg) const { 1423 // FIXME: This code can be simplified by introducing a simple value class for 1424 // Class pairs with appropriate constructor methods for the various 1425 // situations. 1426 1427 // FIXME: Some of the split computations are wrong; unaligned vectors 1428 // shouldn't be passed in registers for example, so there is no chance they 1429 // can straddle an eightbyte. Verify & simplify. 1430 1431 Lo = Hi = NoClass; 1432 1433 Class &Current = OffsetBase < 64 ? Lo : Hi; 1434 Current = Memory; 1435 1436 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 1437 BuiltinType::Kind k = BT->getKind(); 1438 1439 if (k == BuiltinType::Void) { 1440 Current = NoClass; 1441 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { 1442 Lo = Integer; 1443 Hi = Integer; 1444 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { 1445 Current = Integer; 1446 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || 1447 (k == BuiltinType::LongDouble && 1448 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) { 1449 Current = SSE; 1450 } else if (k == BuiltinType::LongDouble) { 1451 Lo = X87; 1452 Hi = X87Up; 1453 } 1454 // FIXME: _Decimal32 and _Decimal64 are SSE. 1455 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). 1456 return; 1457 } 1458 1459 if (const EnumType *ET = Ty->getAs<EnumType>()) { 1460 // Classify the underlying integer type. 1461 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); 1462 return; 1463 } 1464 1465 if (Ty->hasPointerRepresentation()) { 1466 Current = Integer; 1467 return; 1468 } 1469 1470 if (Ty->isMemberPointerType()) { 1471 if (Ty->isMemberFunctionPointerType() && Has64BitPointers) 1472 Lo = Hi = Integer; 1473 else 1474 Current = Integer; 1475 return; 1476 } 1477 1478 if (const VectorType *VT = Ty->getAs<VectorType>()) { 1479 uint64_t Size = getContext().getTypeSize(VT); 1480 if (Size == 32) { 1481 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x 1482 // float> as integer. 1483 Current = Integer; 1484 1485 // If this type crosses an eightbyte boundary, it should be 1486 // split. 1487 uint64_t EB_Real = (OffsetBase) / 64; 1488 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; 1489 if (EB_Real != EB_Imag) 1490 Hi = Lo; 1491 } else if (Size == 64) { 1492 // gcc passes <1 x double> in memory. :( 1493 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) 1494 return; 1495 1496 // gcc passes <1 x long long> as INTEGER. 1497 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || 1498 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || 1499 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || 1500 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) 1501 Current = Integer; 1502 else 1503 Current = SSE; 1504 1505 // If this type crosses an eightbyte boundary, it should be 1506 // split. 1507 if (OffsetBase && OffsetBase != 64) 1508 Hi = Lo; 1509 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { 1510 // Arguments of 256-bits are split into four eightbyte chunks. The 1511 // least significant one belongs to class SSE and all the others to class 1512 // SSEUP. The original Lo and Hi design considers that types can't be 1513 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. 1514 // This design isn't correct for 256-bits, but since there're no cases 1515 // where the upper parts would need to be inspected, avoid adding 1516 // complexity and just consider Hi to match the 64-256 part. 1517 // 1518 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in 1519 // registers if they are "named", i.e. not part of the "..." of a 1520 // variadic function. 1521 Lo = SSE; 1522 Hi = SSEUp; 1523 } 1524 return; 1525 } 1526 1527 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 1528 QualType ET = getContext().getCanonicalType(CT->getElementType()); 1529 1530 uint64_t Size = getContext().getTypeSize(Ty); 1531 if (ET->isIntegralOrEnumerationType()) { 1532 if (Size <= 64) 1533 Current = Integer; 1534 else if (Size <= 128) 1535 Lo = Hi = Integer; 1536 } else if (ET == getContext().FloatTy) 1537 Current = SSE; 1538 else if (ET == getContext().DoubleTy || 1539 (ET == getContext().LongDoubleTy && 1540 getTarget().getTriple().getOS() == llvm::Triple::NaCl)) 1541 Lo = Hi = SSE; 1542 else if (ET == getContext().LongDoubleTy) 1543 Current = ComplexX87; 1544 1545 // If this complex type crosses an eightbyte boundary then it 1546 // should be split. 1547 uint64_t EB_Real = (OffsetBase) / 64; 1548 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; 1549 if (Hi == NoClass && EB_Real != EB_Imag) 1550 Hi = Lo; 1551 1552 return; 1553 } 1554 1555 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 1556 // Arrays are treated like structures. 1557 1558 uint64_t Size = getContext().getTypeSize(Ty); 1559 1560 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 1561 // than four eightbytes, ..., it has class MEMORY. 1562 if (Size > 256) 1563 return; 1564 1565 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned 1566 // fields, it has class MEMORY. 1567 // 1568 // Only need to check alignment of array base. 1569 if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) 1570 return; 1571 1572 // Otherwise implement simplified merge. We could be smarter about 1573 // this, but it isn't worth it and would be harder to verify. 1574 Current = NoClass; 1575 uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); 1576 uint64_t ArraySize = AT->getSize().getZExtValue(); 1577 1578 // The only case a 256-bit wide vector could be used is when the array 1579 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 1580 // to work for sizes wider than 128, early check and fallback to memory. 1581 if (Size > 128 && EltSize != 256) 1582 return; 1583 1584 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { 1585 Class FieldLo, FieldHi; 1586 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); 1587 Lo = merge(Lo, FieldLo); 1588 Hi = merge(Hi, FieldHi); 1589 if (Lo == Memory || Hi == Memory) 1590 break; 1591 } 1592 1593 postMerge(Size, Lo, Hi); 1594 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); 1595 return; 1596 } 1597 1598 if (const RecordType *RT = Ty->getAs<RecordType>()) { 1599 uint64_t Size = getContext().getTypeSize(Ty); 1600 1601 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 1602 // than four eightbytes, ..., it has class MEMORY. 1603 if (Size > 256) 1604 return; 1605 1606 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial 1607 // copy constructor or a non-trivial destructor, it is passed by invisible 1608 // reference. 1609 if (getRecordArgABI(RT, CGT)) 1610 return; 1611 1612 const RecordDecl *RD = RT->getDecl(); 1613 1614 // Assume variable sized types are passed in memory. 1615 if (RD->hasFlexibleArrayMember()) 1616 return; 1617 1618 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 1619 1620 // Reset Lo class, this will be recomputed. 1621 Current = NoClass; 1622 1623 // If this is a C++ record, classify the bases first. 1624 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 1625 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 1626 e = CXXRD->bases_end(); i != e; ++i) { 1627 assert(!i->isVirtual() && !i->getType()->isDependentType() && 1628 "Unexpected base class!"); 1629 const CXXRecordDecl *Base = 1630 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1631 1632 // Classify this field. 1633 // 1634 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a 1635 // single eightbyte, each is classified separately. Each eightbyte gets 1636 // initialized to class NO_CLASS. 1637 Class FieldLo, FieldHi; 1638 uint64_t Offset = 1639 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); 1640 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); 1641 Lo = merge(Lo, FieldLo); 1642 Hi = merge(Hi, FieldHi); 1643 if (Lo == Memory || Hi == Memory) 1644 break; 1645 } 1646 } 1647 1648 // Classify the fields one at a time, merging the results. 1649 unsigned idx = 0; 1650 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 1651 i != e; ++i, ++idx) { 1652 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 1653 bool BitField = i->isBitField(); 1654 1655 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than 1656 // four eightbytes, or it contains unaligned fields, it has class MEMORY. 1657 // 1658 // The only case a 256-bit wide vector could be used is when the struct 1659 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 1660 // to work for sizes wider than 128, early check and fallback to memory. 1661 // 1662 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { 1663 Lo = Memory; 1664 return; 1665 } 1666 // Note, skip this test for bit-fields, see below. 1667 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { 1668 Lo = Memory; 1669 return; 1670 } 1671 1672 // Classify this field. 1673 // 1674 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate 1675 // exceeds a single eightbyte, each is classified 1676 // separately. Each eightbyte gets initialized to class 1677 // NO_CLASS. 1678 Class FieldLo, FieldHi; 1679 1680 // Bit-fields require special handling, they do not force the 1681 // structure to be passed in memory even if unaligned, and 1682 // therefore they can straddle an eightbyte. 1683 if (BitField) { 1684 // Ignore padding bit-fields. 1685 if (i->isUnnamedBitfield()) 1686 continue; 1687 1688 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 1689 uint64_t Size = i->getBitWidthValue(getContext()); 1690 1691 uint64_t EB_Lo = Offset / 64; 1692 uint64_t EB_Hi = (Offset + Size - 1) / 64; 1693 FieldLo = FieldHi = NoClass; 1694 if (EB_Lo) { 1695 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); 1696 FieldLo = NoClass; 1697 FieldHi = Integer; 1698 } else { 1699 FieldLo = Integer; 1700 FieldHi = EB_Hi ? Integer : NoClass; 1701 } 1702 } else 1703 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); 1704 Lo = merge(Lo, FieldLo); 1705 Hi = merge(Hi, FieldHi); 1706 if (Lo == Memory || Hi == Memory) 1707 break; 1708 } 1709 1710 postMerge(Size, Lo, Hi); 1711 } 1712 } 1713 1714 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { 1715 // If this is a scalar LLVM value then assume LLVM will pass it in the right 1716 // place naturally. 1717 if (!isAggregateTypeForABI(Ty)) { 1718 // Treat an enum type as its underlying type. 1719 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 1720 Ty = EnumTy->getDecl()->getIntegerType(); 1721 1722 return (Ty->isPromotableIntegerType() ? 1723 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 1724 } 1725 1726 return ABIArgInfo::getIndirect(0); 1727 } 1728 1729 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { 1730 if (const VectorType *VecTy = Ty->getAs<VectorType>()) { 1731 uint64_t Size = getContext().getTypeSize(VecTy); 1732 unsigned LargestVector = HasAVX ? 256 : 128; 1733 if (Size <= 64 || Size > LargestVector) 1734 return true; 1735 } 1736 1737 return false; 1738 } 1739 1740 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, 1741 unsigned freeIntRegs) const { 1742 // If this is a scalar LLVM value then assume LLVM will pass it in the right 1743 // place naturally. 1744 // 1745 // This assumption is optimistic, as there could be free registers available 1746 // when we need to pass this argument in memory, and LLVM could try to pass 1747 // the argument in the free register. This does not seem to happen currently, 1748 // but this code would be much safer if we could mark the argument with 1749 // 'onstack'. See PR12193. 1750 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { 1751 // Treat an enum type as its underlying type. 1752 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 1753 Ty = EnumTy->getDecl()->getIntegerType(); 1754 1755 return (Ty->isPromotableIntegerType() ? 1756 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 1757 } 1758 1759 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) 1760 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 1761 1762 // Compute the byval alignment. We specify the alignment of the byval in all 1763 // cases so that the mid-level optimizer knows the alignment of the byval. 1764 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); 1765 1766 // Attempt to avoid passing indirect results using byval when possible. This 1767 // is important for good codegen. 1768 // 1769 // We do this by coercing the value into a scalar type which the backend can 1770 // handle naturally (i.e., without using byval). 1771 // 1772 // For simplicity, we currently only do this when we have exhausted all of the 1773 // free integer registers. Doing this when there are free integer registers 1774 // would require more care, as we would have to ensure that the coerced value 1775 // did not claim the unused register. That would require either reording the 1776 // arguments to the function (so that any subsequent inreg values came first), 1777 // or only doing this optimization when there were no following arguments that 1778 // might be inreg. 1779 // 1780 // We currently expect it to be rare (particularly in well written code) for 1781 // arguments to be passed on the stack when there are still free integer 1782 // registers available (this would typically imply large structs being passed 1783 // by value), so this seems like a fair tradeoff for now. 1784 // 1785 // We can revisit this if the backend grows support for 'onstack' parameter 1786 // attributes. See PR12193. 1787 if (freeIntRegs == 0) { 1788 uint64_t Size = getContext().getTypeSize(Ty); 1789 1790 // If this type fits in an eightbyte, coerce it into the matching integral 1791 // type, which will end up on the stack (with alignment 8). 1792 if (Align == 8 && Size <= 64) 1793 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 1794 Size)); 1795 } 1796 1797 return ABIArgInfo::getIndirect(Align); 1798 } 1799 1800 /// GetByteVectorType - The ABI specifies that a value should be passed in an 1801 /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a 1802 /// vector register. 1803 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { 1804 llvm::Type *IRType = CGT.ConvertType(Ty); 1805 1806 // Wrapper structs that just contain vectors are passed just like vectors, 1807 // strip them off if present. 1808 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); 1809 while (STy && STy->getNumElements() == 1) { 1810 IRType = STy->getElementType(0); 1811 STy = dyn_cast<llvm::StructType>(IRType); 1812 } 1813 1814 // If the preferred type is a 16-byte vector, prefer to pass it. 1815 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ 1816 llvm::Type *EltTy = VT->getElementType(); 1817 unsigned BitWidth = VT->getBitWidth(); 1818 if ((BitWidth >= 128 && BitWidth <= 256) && 1819 (EltTy->isFloatTy() || EltTy->isDoubleTy() || 1820 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || 1821 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || 1822 EltTy->isIntegerTy(128))) 1823 return VT; 1824 } 1825 1826 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); 1827 } 1828 1829 /// BitsContainNoUserData - Return true if the specified [start,end) bit range 1830 /// is known to either be off the end of the specified type or being in 1831 /// alignment padding. The user type specified is known to be at most 128 bits 1832 /// in size, and have passed through X86_64ABIInfo::classify with a successful 1833 /// classification that put one of the two halves in the INTEGER class. 1834 /// 1835 /// It is conservatively correct to return false. 1836 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, 1837 unsigned EndBit, ASTContext &Context) { 1838 // If the bytes being queried are off the end of the type, there is no user 1839 // data hiding here. This handles analysis of builtins, vectors and other 1840 // types that don't contain interesting padding. 1841 unsigned TySize = (unsigned)Context.getTypeSize(Ty); 1842 if (TySize <= StartBit) 1843 return true; 1844 1845 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 1846 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); 1847 unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); 1848 1849 // Check each element to see if the element overlaps with the queried range. 1850 for (unsigned i = 0; i != NumElts; ++i) { 1851 // If the element is after the span we care about, then we're done.. 1852 unsigned EltOffset = i*EltSize; 1853 if (EltOffset >= EndBit) break; 1854 1855 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; 1856 if (!BitsContainNoUserData(AT->getElementType(), EltStart, 1857 EndBit-EltOffset, Context)) 1858 return false; 1859 } 1860 // If it overlaps no elements, then it is safe to process as padding. 1861 return true; 1862 } 1863 1864 if (const RecordType *RT = Ty->getAs<RecordType>()) { 1865 const RecordDecl *RD = RT->getDecl(); 1866 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1867 1868 // If this is a C++ record, check the bases first. 1869 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 1870 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 1871 e = CXXRD->bases_end(); i != e; ++i) { 1872 assert(!i->isVirtual() && !i->getType()->isDependentType() && 1873 "Unexpected base class!"); 1874 const CXXRecordDecl *Base = 1875 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1876 1877 // If the base is after the span we care about, ignore it. 1878 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); 1879 if (BaseOffset >= EndBit) continue; 1880 1881 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; 1882 if (!BitsContainNoUserData(i->getType(), BaseStart, 1883 EndBit-BaseOffset, Context)) 1884 return false; 1885 } 1886 } 1887 1888 // Verify that no field has data that overlaps the region of interest. Yes 1889 // this could be sped up a lot by being smarter about queried fields, 1890 // however we're only looking at structs up to 16 bytes, so we don't care 1891 // much. 1892 unsigned idx = 0; 1893 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 1894 i != e; ++i, ++idx) { 1895 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); 1896 1897 // If we found a field after the region we care about, then we're done. 1898 if (FieldOffset >= EndBit) break; 1899 1900 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; 1901 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, 1902 Context)) 1903 return false; 1904 } 1905 1906 // If nothing in this record overlapped the area of interest, then we're 1907 // clean. 1908 return true; 1909 } 1910 1911 return false; 1912 } 1913 1914 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a 1915 /// float member at the specified offset. For example, {int,{float}} has a 1916 /// float at offset 4. It is conservatively correct for this routine to return 1917 /// false. 1918 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, 1919 const llvm::DataLayout &TD) { 1920 // Base case if we find a float. 1921 if (IROffset == 0 && IRType->isFloatTy()) 1922 return true; 1923 1924 // If this is a struct, recurse into the field at the specified offset. 1925 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 1926 const llvm::StructLayout *SL = TD.getStructLayout(STy); 1927 unsigned Elt = SL->getElementContainingOffset(IROffset); 1928 IROffset -= SL->getElementOffset(Elt); 1929 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); 1930 } 1931 1932 // If this is an array, recurse into the field at the specified offset. 1933 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 1934 llvm::Type *EltTy = ATy->getElementType(); 1935 unsigned EltSize = TD.getTypeAllocSize(EltTy); 1936 IROffset -= IROffset/EltSize*EltSize; 1937 return ContainsFloatAtOffset(EltTy, IROffset, TD); 1938 } 1939 1940 return false; 1941 } 1942 1943 1944 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the 1945 /// low 8 bytes of an XMM register, corresponding to the SSE class. 1946 llvm::Type *X86_64ABIInfo:: 1947 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, 1948 QualType SourceTy, unsigned SourceOffset) const { 1949 // The only three choices we have are either double, <2 x float>, or float. We 1950 // pass as float if the last 4 bytes is just padding. This happens for 1951 // structs that contain 3 floats. 1952 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, 1953 SourceOffset*8+64, getContext())) 1954 return llvm::Type::getFloatTy(getVMContext()); 1955 1956 // We want to pass as <2 x float> if the LLVM IR type contains a float at 1957 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the 1958 // case. 1959 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && 1960 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) 1961 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); 1962 1963 return llvm::Type::getDoubleTy(getVMContext()); 1964 } 1965 1966 1967 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in 1968 /// an 8-byte GPR. This means that we either have a scalar or we are talking 1969 /// about the high or low part of an up-to-16-byte struct. This routine picks 1970 /// the best LLVM IR type to represent this, which may be i64 or may be anything 1971 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, 1972 /// etc). 1973 /// 1974 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for 1975 /// the source type. IROffset is an offset in bytes into the LLVM IR type that 1976 /// the 8-byte value references. PrefType may be null. 1977 /// 1978 /// SourceTy is the source level type for the entire argument. SourceOffset is 1979 /// an offset into this that we're processing (which is always either 0 or 8). 1980 /// 1981 llvm::Type *X86_64ABIInfo:: 1982 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, 1983 QualType SourceTy, unsigned SourceOffset) const { 1984 // If we're dealing with an un-offset LLVM IR type, then it means that we're 1985 // returning an 8-byte unit starting with it. See if we can safely use it. 1986 if (IROffset == 0) { 1987 // Pointers and int64's always fill the 8-byte unit. 1988 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || 1989 IRType->isIntegerTy(64)) 1990 return IRType; 1991 1992 // If we have a 1/2/4-byte integer, we can use it only if the rest of the 1993 // goodness in the source type is just tail padding. This is allowed to 1994 // kick in for struct {double,int} on the int, but not on 1995 // struct{double,int,int} because we wouldn't return the second int. We 1996 // have to do this analysis on the source type because we can't depend on 1997 // unions being lowered a specific way etc. 1998 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || 1999 IRType->isIntegerTy(32) || 2000 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { 2001 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : 2002 cast<llvm::IntegerType>(IRType)->getBitWidth(); 2003 2004 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, 2005 SourceOffset*8+64, getContext())) 2006 return IRType; 2007 } 2008 } 2009 2010 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 2011 // If this is a struct, recurse into the field at the specified offset. 2012 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); 2013 if (IROffset < SL->getSizeInBytes()) { 2014 unsigned FieldIdx = SL->getElementContainingOffset(IROffset); 2015 IROffset -= SL->getElementOffset(FieldIdx); 2016 2017 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, 2018 SourceTy, SourceOffset); 2019 } 2020 } 2021 2022 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 2023 llvm::Type *EltTy = ATy->getElementType(); 2024 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); 2025 unsigned EltOffset = IROffset/EltSize*EltSize; 2026 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, 2027 SourceOffset); 2028 } 2029 2030 // Okay, we don't have any better idea of what to pass, so we pass this in an 2031 // integer register that isn't too big to fit the rest of the struct. 2032 unsigned TySizeInBytes = 2033 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); 2034 2035 assert(TySizeInBytes != SourceOffset && "Empty field?"); 2036 2037 // It is always safe to classify this as an integer type up to i64 that 2038 // isn't larger than the structure. 2039 return llvm::IntegerType::get(getVMContext(), 2040 std::min(TySizeInBytes-SourceOffset, 8U)*8); 2041 } 2042 2043 2044 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally 2045 /// be used as elements of a two register pair to pass or return, return a 2046 /// first class aggregate to represent them. For example, if the low part of 2047 /// a by-value argument should be passed as i32* and the high part as float, 2048 /// return {i32*, float}. 2049 static llvm::Type * 2050 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, 2051 const llvm::DataLayout &TD) { 2052 // In order to correctly satisfy the ABI, we need to the high part to start 2053 // at offset 8. If the high and low parts we inferred are both 4-byte types 2054 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have 2055 // the second element at offset 8. Check for this: 2056 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); 2057 unsigned HiAlign = TD.getABITypeAlignment(Hi); 2058 unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); 2059 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); 2060 2061 // To handle this, we have to increase the size of the low part so that the 2062 // second element will start at an 8 byte offset. We can't increase the size 2063 // of the second element because it might make us access off the end of the 2064 // struct. 2065 if (HiStart != 8) { 2066 // There are only two sorts of types the ABI generation code can produce for 2067 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. 2068 // Promote these to a larger type. 2069 if (Lo->isFloatTy()) 2070 Lo = llvm::Type::getDoubleTy(Lo->getContext()); 2071 else { 2072 assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); 2073 Lo = llvm::Type::getInt64Ty(Lo->getContext()); 2074 } 2075 } 2076 2077 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); 2078 2079 2080 // Verify that the second element is at an 8-byte offset. 2081 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && 2082 "Invalid x86-64 argument pair!"); 2083 return Result; 2084 } 2085 2086 ABIArgInfo X86_64ABIInfo:: 2087 classifyReturnType(QualType RetTy) const { 2088 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the 2089 // classification algorithm. 2090 X86_64ABIInfo::Class Lo, Hi; 2091 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); 2092 2093 // Check some invariants. 2094 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 2095 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 2096 2097 llvm::Type *ResType = 0; 2098 switch (Lo) { 2099 case NoClass: 2100 if (Hi == NoClass) 2101 return ABIArgInfo::getIgnore(); 2102 // If the low part is just padding, it takes no register, leave ResType 2103 // null. 2104 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 2105 "Unknown missing lo part"); 2106 break; 2107 2108 case SSEUp: 2109 case X87Up: 2110 llvm_unreachable("Invalid classification for lo word."); 2111 2112 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via 2113 // hidden argument. 2114 case Memory: 2115 return getIndirectReturnResult(RetTy); 2116 2117 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next 2118 // available register of the sequence %rax, %rdx is used. 2119 case Integer: 2120 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 2121 2122 // If we have a sign or zero extended integer, make sure to return Extend 2123 // so that the parameter gets the right LLVM IR attributes. 2124 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 2125 // Treat an enum type as its underlying type. 2126 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 2127 RetTy = EnumTy->getDecl()->getIntegerType(); 2128 2129 if (RetTy->isIntegralOrEnumerationType() && 2130 RetTy->isPromotableIntegerType()) 2131 return ABIArgInfo::getExtend(); 2132 } 2133 break; 2134 2135 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next 2136 // available SSE register of the sequence %xmm0, %xmm1 is used. 2137 case SSE: 2138 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 2139 break; 2140 2141 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is 2142 // returned on the X87 stack in %st0 as 80-bit x87 number. 2143 case X87: 2144 ResType = llvm::Type::getX86_FP80Ty(getVMContext()); 2145 break; 2146 2147 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real 2148 // part of the value is returned in %st0 and the imaginary part in 2149 // %st1. 2150 case ComplexX87: 2151 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); 2152 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), 2153 llvm::Type::getX86_FP80Ty(getVMContext()), 2154 NULL); 2155 break; 2156 } 2157 2158 llvm::Type *HighPart = 0; 2159 switch (Hi) { 2160 // Memory was handled previously and X87 should 2161 // never occur as a hi class. 2162 case Memory: 2163 case X87: 2164 llvm_unreachable("Invalid classification for hi word."); 2165 2166 case ComplexX87: // Previously handled. 2167 case NoClass: 2168 break; 2169 2170 case Integer: 2171 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 2172 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 2173 return ABIArgInfo::getDirect(HighPart, 8); 2174 break; 2175 case SSE: 2176 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 2177 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 2178 return ABIArgInfo::getDirect(HighPart, 8); 2179 break; 2180 2181 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte 2182 // is passed in the next available eightbyte chunk if the last used 2183 // vector register. 2184 // 2185 // SSEUP should always be preceded by SSE, just widen. 2186 case SSEUp: 2187 assert(Lo == SSE && "Unexpected SSEUp classification."); 2188 ResType = GetByteVectorType(RetTy); 2189 break; 2190 2191 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is 2192 // returned together with the previous X87 value in %st0. 2193 case X87Up: 2194 // If X87Up is preceded by X87, we don't need to do 2195 // anything. However, in some cases with unions it may not be 2196 // preceded by X87. In such situations we follow gcc and pass the 2197 // extra bits in an SSE reg. 2198 if (Lo != X87) { 2199 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 2200 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 2201 return ABIArgInfo::getDirect(HighPart, 8); 2202 } 2203 break; 2204 } 2205 2206 // If a high part was specified, merge it together with the low part. It is 2207 // known to pass in the high eightbyte of the result. We do this by forming a 2208 // first class struct aggregate with the high and low part: {low, high} 2209 if (HighPart) 2210 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 2211 2212 return ABIArgInfo::getDirect(ResType); 2213 } 2214 2215 ABIArgInfo X86_64ABIInfo::classifyArgumentType( 2216 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, 2217 bool isNamedArg) 2218 const 2219 { 2220 X86_64ABIInfo::Class Lo, Hi; 2221 classify(Ty, 0, Lo, Hi, isNamedArg); 2222 2223 // Check some invariants. 2224 // FIXME: Enforce these by construction. 2225 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 2226 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 2227 2228 neededInt = 0; 2229 neededSSE = 0; 2230 llvm::Type *ResType = 0; 2231 switch (Lo) { 2232 case NoClass: 2233 if (Hi == NoClass) 2234 return ABIArgInfo::getIgnore(); 2235 // If the low part is just padding, it takes no register, leave ResType 2236 // null. 2237 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 2238 "Unknown missing lo part"); 2239 break; 2240 2241 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument 2242 // on the stack. 2243 case Memory: 2244 2245 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or 2246 // COMPLEX_X87, it is passed in memory. 2247 case X87: 2248 case ComplexX87: 2249 if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect) 2250 ++neededInt; 2251 return getIndirectResult(Ty, freeIntRegs); 2252 2253 case SSEUp: 2254 case X87Up: 2255 llvm_unreachable("Invalid classification for lo word."); 2256 2257 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next 2258 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 2259 // and %r9 is used. 2260 case Integer: 2261 ++neededInt; 2262 2263 // Pick an 8-byte type based on the preferred type. 2264 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); 2265 2266 // If we have a sign or zero extended integer, make sure to return Extend 2267 // so that the parameter gets the right LLVM IR attributes. 2268 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 2269 // Treat an enum type as its underlying type. 2270 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2271 Ty = EnumTy->getDecl()->getIntegerType(); 2272 2273 if (Ty->isIntegralOrEnumerationType() && 2274 Ty->isPromotableIntegerType()) 2275 return ABIArgInfo::getExtend(); 2276 } 2277 2278 break; 2279 2280 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next 2281 // available SSE register is used, the registers are taken in the 2282 // order from %xmm0 to %xmm7. 2283 case SSE: { 2284 llvm::Type *IRType = CGT.ConvertType(Ty); 2285 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); 2286 ++neededSSE; 2287 break; 2288 } 2289 } 2290 2291 llvm::Type *HighPart = 0; 2292 switch (Hi) { 2293 // Memory was handled previously, ComplexX87 and X87 should 2294 // never occur as hi classes, and X87Up must be preceded by X87, 2295 // which is passed in memory. 2296 case Memory: 2297 case X87: 2298 case ComplexX87: 2299 llvm_unreachable("Invalid classification for hi word."); 2300 2301 case NoClass: break; 2302 2303 case Integer: 2304 ++neededInt; 2305 // Pick an 8-byte type based on the preferred type. 2306 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 2307 2308 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 2309 return ABIArgInfo::getDirect(HighPart, 8); 2310 break; 2311 2312 // X87Up generally doesn't occur here (long double is passed in 2313 // memory), except in situations involving unions. 2314 case X87Up: 2315 case SSE: 2316 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 2317 2318 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 2319 return ABIArgInfo::getDirect(HighPart, 8); 2320 2321 ++neededSSE; 2322 break; 2323 2324 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the 2325 // eightbyte is passed in the upper half of the last used SSE 2326 // register. This only happens when 128-bit vectors are passed. 2327 case SSEUp: 2328 assert(Lo == SSE && "Unexpected SSEUp classification"); 2329 ResType = GetByteVectorType(Ty); 2330 break; 2331 } 2332 2333 // If a high part was specified, merge it together with the low part. It is 2334 // known to pass in the high eightbyte of the result. We do this by forming a 2335 // first class struct aggregate with the high and low part: {low, high} 2336 if (HighPart) 2337 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 2338 2339 return ABIArgInfo::getDirect(ResType); 2340 } 2341 2342 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 2343 2344 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 2345 2346 // Keep track of the number of assigned registers. 2347 unsigned freeIntRegs = 6, freeSSERegs = 8; 2348 2349 // If the return value is indirect, then the hidden argument is consuming one 2350 // integer register. 2351 if (FI.getReturnInfo().isIndirect()) 2352 --freeIntRegs; 2353 2354 bool isVariadic = FI.isVariadic(); 2355 unsigned numRequiredArgs = 0; 2356 if (isVariadic) 2357 numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs(); 2358 2359 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers 2360 // get assigned (in left-to-right order) for passing as follows... 2361 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 2362 it != ie; ++it) { 2363 bool isNamedArg = true; 2364 if (isVariadic) 2365 isNamedArg = (it - FI.arg_begin()) < 2366 static_cast<signed>(numRequiredArgs); 2367 2368 unsigned neededInt, neededSSE; 2369 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, 2370 neededSSE, isNamedArg); 2371 2372 // AMD64-ABI 3.2.3p3: If there are no registers available for any 2373 // eightbyte of an argument, the whole argument is passed on the 2374 // stack. If registers have already been assigned for some 2375 // eightbytes of such an argument, the assignments get reverted. 2376 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { 2377 freeIntRegs -= neededInt; 2378 freeSSERegs -= neededSSE; 2379 } else { 2380 it->info = getIndirectResult(it->type, freeIntRegs); 2381 } 2382 } 2383 } 2384 2385 static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, 2386 QualType Ty, 2387 CodeGenFunction &CGF) { 2388 llvm::Value *overflow_arg_area_p = 2389 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); 2390 llvm::Value *overflow_arg_area = 2391 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); 2392 2393 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 2394 // byte boundary if alignment needed by type exceeds 8 byte boundary. 2395 // It isn't stated explicitly in the standard, but in practice we use 2396 // alignment greater than 16 where necessary. 2397 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; 2398 if (Align > 8) { 2399 // overflow_arg_area = (overflow_arg_area + align - 1) & -align; 2400 llvm::Value *Offset = 2401 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); 2402 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); 2403 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, 2404 CGF.Int64Ty); 2405 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); 2406 overflow_arg_area = 2407 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), 2408 overflow_arg_area->getType(), 2409 "overflow_arg_area.align"); 2410 } 2411 2412 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. 2413 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 2414 llvm::Value *Res = 2415 CGF.Builder.CreateBitCast(overflow_arg_area, 2416 llvm::PointerType::getUnqual(LTy)); 2417 2418 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: 2419 // l->overflow_arg_area + sizeof(type). 2420 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to 2421 // an 8 byte boundary. 2422 2423 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; 2424 llvm::Value *Offset = 2425 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); 2426 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, 2427 "overflow_arg_area.next"); 2428 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); 2429 2430 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. 2431 return Res; 2432 } 2433 2434 llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2435 CodeGenFunction &CGF) const { 2436 // Assume that va_list type is correct; should be pointer to LLVM type: 2437 // struct { 2438 // i32 gp_offset; 2439 // i32 fp_offset; 2440 // i8* overflow_arg_area; 2441 // i8* reg_save_area; 2442 // }; 2443 unsigned neededInt, neededSSE; 2444 2445 Ty = CGF.getContext().getCanonicalType(Ty); 2446 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, 2447 /*isNamedArg*/false); 2448 2449 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed 2450 // in the registers. If not go to step 7. 2451 if (!neededInt && !neededSSE) 2452 return EmitVAArgFromMemory(VAListAddr, Ty, CGF); 2453 2454 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of 2455 // general purpose registers needed to pass type and num_fp to hold 2456 // the number of floating point registers needed. 2457 2458 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into 2459 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or 2460 // l->fp_offset > 304 - num_fp * 16 go to step 7. 2461 // 2462 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of 2463 // register save space). 2464 2465 llvm::Value *InRegs = 0; 2466 llvm::Value *gp_offset_p = 0, *gp_offset = 0; 2467 llvm::Value *fp_offset_p = 0, *fp_offset = 0; 2468 if (neededInt) { 2469 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); 2470 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); 2471 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); 2472 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); 2473 } 2474 2475 if (neededSSE) { 2476 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); 2477 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); 2478 llvm::Value *FitsInFP = 2479 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); 2480 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); 2481 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; 2482 } 2483 2484 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 2485 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 2486 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 2487 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 2488 2489 // Emit code to load the value if it was passed in registers. 2490 2491 CGF.EmitBlock(InRegBlock); 2492 2493 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with 2494 // an offset of l->gp_offset and/or l->fp_offset. This may require 2495 // copying to a temporary location in case the parameter is passed 2496 // in different register classes or requires an alignment greater 2497 // than 8 for general purpose registers and 16 for XMM registers. 2498 // 2499 // FIXME: This really results in shameful code when we end up needing to 2500 // collect arguments from different places; often what should result in a 2501 // simple assembling of a structure from scattered addresses has many more 2502 // loads than necessary. Can we clean this up? 2503 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 2504 llvm::Value *RegAddr = 2505 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), 2506 "reg_save_area"); 2507 if (neededInt && neededSSE) { 2508 // FIXME: Cleanup. 2509 assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); 2510 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); 2511 llvm::Value *Tmp = CGF.CreateMemTemp(Ty); 2512 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); 2513 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); 2514 llvm::Type *TyLo = ST->getElementType(0); 2515 llvm::Type *TyHi = ST->getElementType(1); 2516 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && 2517 "Unexpected ABI info for mixed regs"); 2518 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); 2519 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); 2520 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); 2521 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); 2522 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; 2523 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; 2524 llvm::Value *V = 2525 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); 2526 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 2527 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); 2528 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 2529 2530 RegAddr = CGF.Builder.CreateBitCast(Tmp, 2531 llvm::PointerType::getUnqual(LTy)); 2532 } else if (neededInt) { 2533 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); 2534 RegAddr = CGF.Builder.CreateBitCast(RegAddr, 2535 llvm::PointerType::getUnqual(LTy)); 2536 2537 // Copy to a temporary if necessary to ensure the appropriate alignment. 2538 std::pair<CharUnits, CharUnits> SizeAlign = 2539 CGF.getContext().getTypeInfoInChars(Ty); 2540 uint64_t TySize = SizeAlign.first.getQuantity(); 2541 unsigned TyAlign = SizeAlign.second.getQuantity(); 2542 if (TyAlign > 8) { 2543 llvm::Value *Tmp = CGF.CreateMemTemp(Ty); 2544 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); 2545 RegAddr = Tmp; 2546 } 2547 } else if (neededSSE == 1) { 2548 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); 2549 RegAddr = CGF.Builder.CreateBitCast(RegAddr, 2550 llvm::PointerType::getUnqual(LTy)); 2551 } else { 2552 assert(neededSSE == 2 && "Invalid number of needed registers!"); 2553 // SSE registers are spaced 16 bytes apart in the register save 2554 // area, we need to collect the two eightbytes together. 2555 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); 2556 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); 2557 llvm::Type *DoubleTy = CGF.DoubleTy; 2558 llvm::Type *DblPtrTy = 2559 llvm::PointerType::getUnqual(DoubleTy); 2560 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); 2561 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); 2562 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); 2563 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, 2564 DblPtrTy)); 2565 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 2566 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, 2567 DblPtrTy)); 2568 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 2569 RegAddr = CGF.Builder.CreateBitCast(Tmp, 2570 llvm::PointerType::getUnqual(LTy)); 2571 } 2572 2573 // AMD64-ABI 3.5.7p5: Step 5. Set: 2574 // l->gp_offset = l->gp_offset + num_gp * 8 2575 // l->fp_offset = l->fp_offset + num_fp * 16. 2576 if (neededInt) { 2577 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); 2578 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), 2579 gp_offset_p); 2580 } 2581 if (neededSSE) { 2582 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); 2583 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), 2584 fp_offset_p); 2585 } 2586 CGF.EmitBranch(ContBlock); 2587 2588 // Emit code to load the value if it was passed in memory. 2589 2590 CGF.EmitBlock(InMemBlock); 2591 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); 2592 2593 // Return the appropriate result. 2594 2595 CGF.EmitBlock(ContBlock); 2596 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, 2597 "vaarg.addr"); 2598 ResAddr->addIncoming(RegAddr, InRegBlock); 2599 ResAddr->addIncoming(MemAddr, InMemBlock); 2600 return ResAddr; 2601 } 2602 2603 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { 2604 2605 if (Ty->isVoidType()) 2606 return ABIArgInfo::getIgnore(); 2607 2608 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2609 Ty = EnumTy->getDecl()->getIntegerType(); 2610 2611 uint64_t Size = getContext().getTypeSize(Ty); 2612 2613 if (const RecordType *RT = Ty->getAs<RecordType>()) { 2614 if (IsReturnType) { 2615 if (isRecordReturnIndirect(RT, CGT)) 2616 return ABIArgInfo::getIndirect(0, false); 2617 } else { 2618 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT)) 2619 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 2620 } 2621 2622 if (RT->getDecl()->hasFlexibleArrayMember()) 2623 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 2624 2625 // FIXME: mingw-w64-gcc emits 128-bit struct as i128 2626 if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32) 2627 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 2628 Size)); 2629 2630 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 2631 // not 1, 2, 4, or 8 bytes, must be passed by reference." 2632 if (Size <= 64 && 2633 (Size & (Size - 1)) == 0) 2634 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 2635 Size)); 2636 2637 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 2638 } 2639 2640 if (Ty->isPromotableIntegerType()) 2641 return ABIArgInfo::getExtend(); 2642 2643 return ABIArgInfo::getDirect(); 2644 } 2645 2646 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 2647 2648 QualType RetTy = FI.getReturnType(); 2649 FI.getReturnInfo() = classify(RetTy, true); 2650 2651 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 2652 it != ie; ++it) 2653 it->info = classify(it->type, false); 2654 } 2655 2656 llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2657 CodeGenFunction &CGF) const { 2658 llvm::Type *BPP = CGF.Int8PtrPtrTy; 2659 2660 CGBuilderTy &Builder = CGF.Builder; 2661 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 2662 "ap"); 2663 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 2664 llvm::Type *PTy = 2665 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 2666 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 2667 2668 uint64_t Offset = 2669 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); 2670 llvm::Value *NextAddr = 2671 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 2672 "ap.next"); 2673 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 2674 2675 return AddrTyped; 2676 } 2677 2678 namespace { 2679 2680 class NaClX86_64ABIInfo : public ABIInfo { 2681 public: 2682 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 2683 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} 2684 virtual void computeInfo(CGFunctionInfo &FI) const; 2685 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2686 CodeGenFunction &CGF) const; 2687 private: 2688 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. 2689 X86_64ABIInfo NInfo; // Used for everything else. 2690 }; 2691 2692 class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 2693 public: 2694 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 2695 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} 2696 }; 2697 2698 } 2699 2700 void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 2701 if (FI.getASTCallingConvention() == CC_PnaclCall) 2702 PInfo.computeInfo(FI); 2703 else 2704 NInfo.computeInfo(FI); 2705 } 2706 2707 llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2708 CodeGenFunction &CGF) const { 2709 // Always use the native convention; calling pnacl-style varargs functions 2710 // is unuspported. 2711 return NInfo.EmitVAArg(VAListAddr, Ty, CGF); 2712 } 2713 2714 2715 // PowerPC-32 2716 2717 namespace { 2718 class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 2719 public: 2720 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 2721 2722 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 2723 // This is recovered from gcc output. 2724 return 1; // r1 is the dedicated stack pointer 2725 } 2726 2727 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2728 llvm::Value *Address) const; 2729 }; 2730 2731 } 2732 2733 bool 2734 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2735 llvm::Value *Address) const { 2736 // This is calculated from the LLVM and GCC tables and verified 2737 // against gcc output. AFAIK all ABIs use the same encoding. 2738 2739 CodeGen::CGBuilderTy &Builder = CGF.Builder; 2740 2741 llvm::IntegerType *i8 = CGF.Int8Ty; 2742 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 2743 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 2744 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 2745 2746 // 0-31: r0-31, the 4-byte general-purpose registers 2747 AssignToArrayRange(Builder, Address, Four8, 0, 31); 2748 2749 // 32-63: fp0-31, the 8-byte floating-point registers 2750 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 2751 2752 // 64-76 are various 4-byte special-purpose registers: 2753 // 64: mq 2754 // 65: lr 2755 // 66: ctr 2756 // 67: ap 2757 // 68-75 cr0-7 2758 // 76: xer 2759 AssignToArrayRange(Builder, Address, Four8, 64, 76); 2760 2761 // 77-108: v0-31, the 16-byte vector registers 2762 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 2763 2764 // 109: vrsave 2765 // 110: vscr 2766 // 111: spe_acc 2767 // 112: spefscr 2768 // 113: sfp 2769 AssignToArrayRange(Builder, Address, Four8, 109, 113); 2770 2771 return false; 2772 } 2773 2774 // PowerPC-64 2775 2776 namespace { 2777 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. 2778 class PPC64_SVR4_ABIInfo : public DefaultABIInfo { 2779 2780 public: 2781 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 2782 2783 bool isPromotableTypeForABI(QualType Ty) const; 2784 2785 ABIArgInfo classifyReturnType(QualType RetTy) const; 2786 ABIArgInfo classifyArgumentType(QualType Ty) const; 2787 2788 // TODO: We can add more logic to computeInfo to improve performance. 2789 // Example: For aggregate arguments that fit in a register, we could 2790 // use getDirectInReg (as is done below for structs containing a single 2791 // floating-point value) to avoid pushing them to memory on function 2792 // entry. This would require changing the logic in PPCISelLowering 2793 // when lowering the parameters in the caller and args in the callee. 2794 virtual void computeInfo(CGFunctionInfo &FI) const { 2795 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 2796 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 2797 it != ie; ++it) { 2798 // We rely on the default argument classification for the most part. 2799 // One exception: An aggregate containing a single floating-point 2800 // or vector item must be passed in a register if one is available. 2801 const Type *T = isSingleElementStruct(it->type, getContext()); 2802 if (T) { 2803 const BuiltinType *BT = T->getAs<BuiltinType>(); 2804 if (T->isVectorType() || (BT && BT->isFloatingPoint())) { 2805 QualType QT(T, 0); 2806 it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); 2807 continue; 2808 } 2809 } 2810 it->info = classifyArgumentType(it->type); 2811 } 2812 } 2813 2814 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, 2815 QualType Ty, 2816 CodeGenFunction &CGF) const; 2817 }; 2818 2819 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { 2820 public: 2821 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT) 2822 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {} 2823 2824 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 2825 // This is recovered from gcc output. 2826 return 1; // r1 is the dedicated stack pointer 2827 } 2828 2829 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2830 llvm::Value *Address) const; 2831 }; 2832 2833 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 2834 public: 2835 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 2836 2837 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 2838 // This is recovered from gcc output. 2839 return 1; // r1 is the dedicated stack pointer 2840 } 2841 2842 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2843 llvm::Value *Address) const; 2844 }; 2845 2846 } 2847 2848 // Return true if the ABI requires Ty to be passed sign- or zero- 2849 // extended to 64 bits. 2850 bool 2851 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { 2852 // Treat an enum type as its underlying type. 2853 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2854 Ty = EnumTy->getDecl()->getIntegerType(); 2855 2856 // Promotable integer types are required to be promoted by the ABI. 2857 if (Ty->isPromotableIntegerType()) 2858 return true; 2859 2860 // In addition to the usual promotable integer types, we also need to 2861 // extend all 32-bit types, since the ABI requires promotion to 64 bits. 2862 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 2863 switch (BT->getKind()) { 2864 case BuiltinType::Int: 2865 case BuiltinType::UInt: 2866 return true; 2867 default: 2868 break; 2869 } 2870 2871 return false; 2872 } 2873 2874 ABIArgInfo 2875 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { 2876 if (Ty->isAnyComplexType()) 2877 return ABIArgInfo::getDirect(); 2878 2879 if (isAggregateTypeForABI(Ty)) { 2880 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) 2881 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 2882 2883 return ABIArgInfo::getIndirect(0); 2884 } 2885 2886 return (isPromotableTypeForABI(Ty) ? 2887 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 2888 } 2889 2890 ABIArgInfo 2891 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { 2892 if (RetTy->isVoidType()) 2893 return ABIArgInfo::getIgnore(); 2894 2895 if (RetTy->isAnyComplexType()) 2896 return ABIArgInfo::getDirect(); 2897 2898 if (isAggregateTypeForABI(RetTy)) 2899 return ABIArgInfo::getIndirect(0); 2900 2901 return (isPromotableTypeForABI(RetTy) ? 2902 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 2903 } 2904 2905 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. 2906 llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, 2907 QualType Ty, 2908 CodeGenFunction &CGF) const { 2909 llvm::Type *BP = CGF.Int8PtrTy; 2910 llvm::Type *BPP = CGF.Int8PtrPtrTy; 2911 2912 CGBuilderTy &Builder = CGF.Builder; 2913 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 2914 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 2915 2916 // Update the va_list pointer. The pointer should be bumped by the 2917 // size of the object. We can trust getTypeSize() except for a complex 2918 // type whose base type is smaller than a doubleword. For these, the 2919 // size of the object is 16 bytes; see below for further explanation. 2920 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; 2921 QualType BaseTy; 2922 unsigned CplxBaseSize = 0; 2923 2924 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 2925 BaseTy = CTy->getElementType(); 2926 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; 2927 if (CplxBaseSize < 8) 2928 SizeInBytes = 16; 2929 } 2930 2931 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); 2932 llvm::Value *NextAddr = 2933 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), 2934 "ap.next"); 2935 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 2936 2937 // If we have a complex type and the base type is smaller than 8 bytes, 2938 // the ABI calls for the real and imaginary parts to be right-adjusted 2939 // in separate doublewords. However, Clang expects us to produce a 2940 // pointer to a structure with the two parts packed tightly. So generate 2941 // loads of the real and imaginary parts relative to the va_list pointer, 2942 // and store them to a temporary structure. 2943 if (CplxBaseSize && CplxBaseSize < 8) { 2944 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); 2945 llvm::Value *ImagAddr = RealAddr; 2946 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); 2947 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); 2948 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); 2949 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); 2950 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); 2951 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); 2952 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); 2953 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), 2954 "vacplx"); 2955 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); 2956 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); 2957 Builder.CreateStore(Real, RealPtr, false); 2958 Builder.CreateStore(Imag, ImagPtr, false); 2959 return Ptr; 2960 } 2961 2962 // If the argument is smaller than 8 bytes, it is right-adjusted in 2963 // its doubleword slot. Adjust the pointer to pick it up from the 2964 // correct offset. 2965 if (SizeInBytes < 8) { 2966 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); 2967 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); 2968 Addr = Builder.CreateIntToPtr(AddrAsInt, BP); 2969 } 2970 2971 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 2972 return Builder.CreateBitCast(Addr, PTy); 2973 } 2974 2975 static bool 2976 PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2977 llvm::Value *Address) { 2978 // This is calculated from the LLVM and GCC tables and verified 2979 // against gcc output. AFAIK all ABIs use the same encoding. 2980 2981 CodeGen::CGBuilderTy &Builder = CGF.Builder; 2982 2983 llvm::IntegerType *i8 = CGF.Int8Ty; 2984 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 2985 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 2986 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 2987 2988 // 0-31: r0-31, the 8-byte general-purpose registers 2989 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 2990 2991 // 32-63: fp0-31, the 8-byte floating-point registers 2992 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 2993 2994 // 64-76 are various 4-byte special-purpose registers: 2995 // 64: mq 2996 // 65: lr 2997 // 66: ctr 2998 // 67: ap 2999 // 68-75 cr0-7 3000 // 76: xer 3001 AssignToArrayRange(Builder, Address, Four8, 64, 76); 3002 3003 // 77-108: v0-31, the 16-byte vector registers 3004 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 3005 3006 // 109: vrsave 3007 // 110: vscr 3008 // 111: spe_acc 3009 // 112: spefscr 3010 // 113: sfp 3011 AssignToArrayRange(Builder, Address, Four8, 109, 113); 3012 3013 return false; 3014 } 3015 3016 bool 3017 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( 3018 CodeGen::CodeGenFunction &CGF, 3019 llvm::Value *Address) const { 3020 3021 return PPC64_initDwarfEHRegSizeTable(CGF, Address); 3022 } 3023 3024 bool 3025 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3026 llvm::Value *Address) const { 3027 3028 return PPC64_initDwarfEHRegSizeTable(CGF, Address); 3029 } 3030 3031 //===----------------------------------------------------------------------===// 3032 // ARM ABI Implementation 3033 //===----------------------------------------------------------------------===// 3034 3035 namespace { 3036 3037 class ARMABIInfo : public ABIInfo { 3038 public: 3039 enum ABIKind { 3040 APCS = 0, 3041 AAPCS = 1, 3042 AAPCS_VFP 3043 }; 3044 3045 private: 3046 ABIKind Kind; 3047 3048 public: 3049 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { 3050 setRuntimeCC(); 3051 } 3052 3053 bool isEABI() const { 3054 StringRef Env = getTarget().getTriple().getEnvironmentName(); 3055 return (Env == "gnueabi" || Env == "eabi" || 3056 Env == "android" || Env == "androideabi"); 3057 } 3058 3059 private: 3060 ABIKind getABIKind() const { return Kind; } 3061 3062 ABIArgInfo classifyReturnType(QualType RetTy) const; 3063 ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs, 3064 unsigned &AllocatedVFP, 3065 bool &IsHA) const; 3066 bool isIllegalVectorType(QualType Ty) const; 3067 3068 virtual void computeInfo(CGFunctionInfo &FI) const; 3069 3070 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3071 CodeGenFunction &CGF) const; 3072 3073 llvm::CallingConv::ID getLLVMDefaultCC() const; 3074 llvm::CallingConv::ID getABIDefaultCC() const; 3075 void setRuntimeCC(); 3076 }; 3077 3078 class ARMTargetCodeGenInfo : public TargetCodeGenInfo { 3079 public: 3080 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 3081 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} 3082 3083 const ARMABIInfo &getABIInfo() const { 3084 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); 3085 } 3086 3087 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 3088 return 13; 3089 } 3090 3091 StringRef getARCRetainAutoreleasedReturnValueMarker() const { 3092 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; 3093 } 3094 3095 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3096 llvm::Value *Address) const { 3097 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 3098 3099 // 0-15 are the 16 integer registers. 3100 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); 3101 return false; 3102 } 3103 3104 unsigned getSizeOfUnwindException() const { 3105 if (getABIInfo().isEABI()) return 88; 3106 return TargetCodeGenInfo::getSizeOfUnwindException(); 3107 } 3108 }; 3109 3110 } 3111 3112 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 3113 // To correctly handle Homogeneous Aggregate, we need to keep track of the 3114 // VFP registers allocated so far. 3115 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive 3116 // VFP registers of the appropriate type unallocated then the argument is 3117 // allocated to the lowest-numbered sequence of such registers. 3118 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are 3119 // unallocated are marked as unavailable. 3120 unsigned AllocatedVFP = 0; 3121 int VFPRegs[16] = { 0 }; 3122 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3123 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3124 it != ie; ++it) { 3125 unsigned PreAllocation = AllocatedVFP; 3126 bool IsHA = false; 3127 // 6.1.2.3 There is one VFP co-processor register class using registers 3128 // s0-s15 (d0-d7) for passing arguments. 3129 const unsigned NumVFPs = 16; 3130 it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA); 3131 // If we do not have enough VFP registers for the HA, any VFP registers 3132 // that are unallocated are marked as unavailable. To achieve this, we add 3133 // padding of (NumVFPs - PreAllocation) floats. 3134 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { 3135 llvm::Type *PaddingTy = llvm::ArrayType::get( 3136 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); 3137 it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); 3138 } 3139 } 3140 3141 // Always honor user-specified calling convention. 3142 if (FI.getCallingConvention() != llvm::CallingConv::C) 3143 return; 3144 3145 llvm::CallingConv::ID cc = getRuntimeCC(); 3146 if (cc != llvm::CallingConv::C) 3147 FI.setEffectiveCallingConvention(cc); 3148 } 3149 3150 /// Return the default calling convention that LLVM will use. 3151 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { 3152 // The default calling convention that LLVM will infer. 3153 if (getTarget().getTriple().getEnvironmentName()=="gnueabihf") 3154 return llvm::CallingConv::ARM_AAPCS_VFP; 3155 else if (isEABI()) 3156 return llvm::CallingConv::ARM_AAPCS; 3157 else 3158 return llvm::CallingConv::ARM_APCS; 3159 } 3160 3161 /// Return the calling convention that our ABI would like us to use 3162 /// as the C calling convention. 3163 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { 3164 switch (getABIKind()) { 3165 case APCS: return llvm::CallingConv::ARM_APCS; 3166 case AAPCS: return llvm::CallingConv::ARM_AAPCS; 3167 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 3168 } 3169 llvm_unreachable("bad ABI kind"); 3170 } 3171 3172 void ARMABIInfo::setRuntimeCC() { 3173 assert(getRuntimeCC() == llvm::CallingConv::C); 3174 3175 // Don't muddy up the IR with a ton of explicit annotations if 3176 // they'd just match what LLVM will infer from the triple. 3177 llvm::CallingConv::ID abiCC = getABIDefaultCC(); 3178 if (abiCC != getLLVMDefaultCC()) 3179 RuntimeCC = abiCC; 3180 } 3181 3182 /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous 3183 /// aggregate. If HAMembers is non-null, the number of base elements 3184 /// contained in the type is returned through it; this is used for the 3185 /// recursive calls that check aggregate component types. 3186 static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, 3187 ASTContext &Context, 3188 uint64_t *HAMembers = 0) { 3189 uint64_t Members = 0; 3190 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 3191 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) 3192 return false; 3193 Members *= AT->getSize().getZExtValue(); 3194 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 3195 const RecordDecl *RD = RT->getDecl(); 3196 if (RD->hasFlexibleArrayMember()) 3197 return false; 3198 3199 Members = 0; 3200 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 3201 i != e; ++i) { 3202 const FieldDecl *FD = *i; 3203 uint64_t FldMembers; 3204 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) 3205 return false; 3206 3207 Members = (RD->isUnion() ? 3208 std::max(Members, FldMembers) : Members + FldMembers); 3209 } 3210 } else { 3211 Members = 1; 3212 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 3213 Members = 2; 3214 Ty = CT->getElementType(); 3215 } 3216 3217 // Homogeneous aggregates for AAPCS-VFP must have base types of float, 3218 // double, or 64-bit or 128-bit vectors. 3219 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 3220 if (BT->getKind() != BuiltinType::Float && 3221 BT->getKind() != BuiltinType::Double && 3222 BT->getKind() != BuiltinType::LongDouble) 3223 return false; 3224 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 3225 unsigned VecSize = Context.getTypeSize(VT); 3226 if (VecSize != 64 && VecSize != 128) 3227 return false; 3228 } else { 3229 return false; 3230 } 3231 3232 // The base type must be the same for all members. Vector types of the 3233 // same total size are treated as being equivalent here. 3234 const Type *TyPtr = Ty.getTypePtr(); 3235 if (!Base) 3236 Base = TyPtr; 3237 if (Base != TyPtr && 3238 (!Base->isVectorType() || !TyPtr->isVectorType() || 3239 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr))) 3240 return false; 3241 } 3242 3243 // Homogeneous Aggregates can have at most 4 members of the base type. 3244 if (HAMembers) 3245 *HAMembers = Members; 3246 3247 return (Members > 0 && Members <= 4); 3248 } 3249 3250 /// markAllocatedVFPs - update VFPRegs according to the alignment and 3251 /// number of VFP registers (unit is S register) requested. 3252 static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP, 3253 unsigned Alignment, 3254 unsigned NumRequired) { 3255 // Early Exit. 3256 if (AllocatedVFP >= 16) 3257 return; 3258 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive 3259 // VFP registers of the appropriate type unallocated then the argument is 3260 // allocated to the lowest-numbered sequence of such registers. 3261 for (unsigned I = 0; I < 16; I += Alignment) { 3262 bool FoundSlot = true; 3263 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) 3264 if (J >= 16 || VFPRegs[J]) { 3265 FoundSlot = false; 3266 break; 3267 } 3268 if (FoundSlot) { 3269 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) 3270 VFPRegs[J] = 1; 3271 AllocatedVFP += NumRequired; 3272 return; 3273 } 3274 } 3275 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are 3276 // unallocated are marked as unavailable. 3277 for (unsigned I = 0; I < 16; I++) 3278 VFPRegs[I] = 1; 3279 AllocatedVFP = 17; // We do not have enough VFP registers. 3280 } 3281 3282 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs, 3283 unsigned &AllocatedVFP, 3284 bool &IsHA) const { 3285 // We update number of allocated VFPs according to 3286 // 6.1.2.1 The following argument types are VFP CPRCs: 3287 // A single-precision floating-point type (including promoted 3288 // half-precision types); A double-precision floating-point type; 3289 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate 3290 // with a Base Type of a single- or double-precision floating-point type, 3291 // 64-bit containerized vectors or 128-bit containerized vectors with one 3292 // to four Elements. 3293 3294 // Handle illegal vector types here. 3295 if (isIllegalVectorType(Ty)) { 3296 uint64_t Size = getContext().getTypeSize(Ty); 3297 if (Size <= 32) { 3298 llvm::Type *ResType = 3299 llvm::Type::getInt32Ty(getVMContext()); 3300 return ABIArgInfo::getDirect(ResType); 3301 } 3302 if (Size == 64) { 3303 llvm::Type *ResType = llvm::VectorType::get( 3304 llvm::Type::getInt32Ty(getVMContext()), 2); 3305 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); 3306 return ABIArgInfo::getDirect(ResType); 3307 } 3308 if (Size == 128) { 3309 llvm::Type *ResType = llvm::VectorType::get( 3310 llvm::Type::getInt32Ty(getVMContext()), 4); 3311 markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4); 3312 return ABIArgInfo::getDirect(ResType); 3313 } 3314 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 3315 } 3316 // Update VFPRegs for legal vector types. 3317 if (const VectorType *VT = Ty->getAs<VectorType>()) { 3318 uint64_t Size = getContext().getTypeSize(VT); 3319 // Size of a legal vector should be power of 2 and above 64. 3320 markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32); 3321 } 3322 // Update VFPRegs for floating point types. 3323 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 3324 if (BT->getKind() == BuiltinType::Half || 3325 BT->getKind() == BuiltinType::Float) 3326 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1); 3327 if (BT->getKind() == BuiltinType::Double || 3328 BT->getKind() == BuiltinType::LongDouble) 3329 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); 3330 } 3331 3332 if (!isAggregateTypeForABI(Ty)) { 3333 // Treat an enum type as its underlying type. 3334 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3335 Ty = EnumTy->getDecl()->getIntegerType(); 3336 3337 return (Ty->isPromotableIntegerType() ? 3338 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 3339 } 3340 3341 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) 3342 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 3343 3344 // Ignore empty records. 3345 if (isEmptyRecord(getContext(), Ty, true)) 3346 return ABIArgInfo::getIgnore(); 3347 3348 if (getABIKind() == ARMABIInfo::AAPCS_VFP) { 3349 // Homogeneous Aggregates need to be expanded when we can fit the aggregate 3350 // into VFP registers. 3351 const Type *Base = 0; 3352 uint64_t Members = 0; 3353 if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { 3354 assert(Base && "Base class should be set for homogeneous aggregate"); 3355 // Base can be a floating-point or a vector. 3356 if (Base->isVectorType()) { 3357 // ElementSize is in number of floats. 3358 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; 3359 markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize, 3360 Members * ElementSize); 3361 } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) 3362 markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members); 3363 else { 3364 assert(Base->isSpecificBuiltinType(BuiltinType::Double) || 3365 Base->isSpecificBuiltinType(BuiltinType::LongDouble)); 3366 markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2); 3367 } 3368 IsHA = true; 3369 return ABIArgInfo::getExpand(); 3370 } 3371 } 3372 3373 // Support byval for ARM. 3374 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at 3375 // most 8-byte. We realign the indirect argument if type alignment is bigger 3376 // than ABI alignment. 3377 uint64_t ABIAlign = 4; 3378 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; 3379 if (getABIKind() == ARMABIInfo::AAPCS_VFP || 3380 getABIKind() == ARMABIInfo::AAPCS) 3381 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); 3382 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { 3383 return ABIArgInfo::getIndirect(0, /*ByVal=*/true, 3384 /*Realign=*/TyAlign > ABIAlign); 3385 } 3386 3387 // Otherwise, pass by coercing to a structure of the appropriate size. 3388 llvm::Type* ElemTy; 3389 unsigned SizeRegs; 3390 // FIXME: Try to match the types of the arguments more accurately where 3391 // we can. 3392 if (getContext().getTypeAlign(Ty) <= 32) { 3393 ElemTy = llvm::Type::getInt32Ty(getVMContext()); 3394 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; 3395 } else { 3396 ElemTy = llvm::Type::getInt64Ty(getVMContext()); 3397 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; 3398 } 3399 3400 llvm::Type *STy = 3401 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); 3402 return ABIArgInfo::getDirect(STy); 3403 } 3404 3405 static bool isIntegerLikeType(QualType Ty, ASTContext &Context, 3406 llvm::LLVMContext &VMContext) { 3407 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure 3408 // is called integer-like if its size is less than or equal to one word, and 3409 // the offset of each of its addressable sub-fields is zero. 3410 3411 uint64_t Size = Context.getTypeSize(Ty); 3412 3413 // Check that the type fits in a word. 3414 if (Size > 32) 3415 return false; 3416 3417 // FIXME: Handle vector types! 3418 if (Ty->isVectorType()) 3419 return false; 3420 3421 // Float types are never treated as "integer like". 3422 if (Ty->isRealFloatingType()) 3423 return false; 3424 3425 // If this is a builtin or pointer type then it is ok. 3426 if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) 3427 return true; 3428 3429 // Small complex integer types are "integer like". 3430 if (const ComplexType *CT = Ty->getAs<ComplexType>()) 3431 return isIntegerLikeType(CT->getElementType(), Context, VMContext); 3432 3433 // Single element and zero sized arrays should be allowed, by the definition 3434 // above, but they are not. 3435 3436 // Otherwise, it must be a record type. 3437 const RecordType *RT = Ty->getAs<RecordType>(); 3438 if (!RT) return false; 3439 3440 // Ignore records with flexible arrays. 3441 const RecordDecl *RD = RT->getDecl(); 3442 if (RD->hasFlexibleArrayMember()) 3443 return false; 3444 3445 // Check that all sub-fields are at offset 0, and are themselves "integer 3446 // like". 3447 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 3448 3449 bool HadField = false; 3450 unsigned idx = 0; 3451 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 3452 i != e; ++i, ++idx) { 3453 const FieldDecl *FD = *i; 3454 3455 // Bit-fields are not addressable, we only need to verify they are "integer 3456 // like". We still have to disallow a subsequent non-bitfield, for example: 3457 // struct { int : 0; int x } 3458 // is non-integer like according to gcc. 3459 if (FD->isBitField()) { 3460 if (!RD->isUnion()) 3461 HadField = true; 3462 3463 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 3464 return false; 3465 3466 continue; 3467 } 3468 3469 // Check if this field is at offset 0. 3470 if (Layout.getFieldOffset(idx) != 0) 3471 return false; 3472 3473 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 3474 return false; 3475 3476 // Only allow at most one field in a structure. This doesn't match the 3477 // wording above, but follows gcc in situations with a field following an 3478 // empty structure. 3479 if (!RD->isUnion()) { 3480 if (HadField) 3481 return false; 3482 3483 HadField = true; 3484 } 3485 } 3486 3487 return true; 3488 } 3489 3490 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { 3491 if (RetTy->isVoidType()) 3492 return ABIArgInfo::getIgnore(); 3493 3494 // Large vector types should be returned via memory. 3495 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) 3496 return ABIArgInfo::getIndirect(0); 3497 3498 if (!isAggregateTypeForABI(RetTy)) { 3499 // Treat an enum type as its underlying type. 3500 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 3501 RetTy = EnumTy->getDecl()->getIntegerType(); 3502 3503 return (RetTy->isPromotableIntegerType() ? 3504 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 3505 } 3506 3507 // Structures with either a non-trivial destructor or a non-trivial 3508 // copy constructor are always indirect. 3509 if (isRecordReturnIndirect(RetTy, CGT)) 3510 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 3511 3512 // Are we following APCS? 3513 if (getABIKind() == APCS) { 3514 if (isEmptyRecord(getContext(), RetTy, false)) 3515 return ABIArgInfo::getIgnore(); 3516 3517 // Complex types are all returned as packed integers. 3518 // 3519 // FIXME: Consider using 2 x vector types if the back end handles them 3520 // correctly. 3521 if (RetTy->isAnyComplexType()) 3522 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 3523 getContext().getTypeSize(RetTy))); 3524 3525 // Integer like structures are returned in r0. 3526 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { 3527 // Return in the smallest viable integer type. 3528 uint64_t Size = getContext().getTypeSize(RetTy); 3529 if (Size <= 8) 3530 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 3531 if (Size <= 16) 3532 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 3533 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 3534 } 3535 3536 // Otherwise return in memory. 3537 return ABIArgInfo::getIndirect(0); 3538 } 3539 3540 // Otherwise this is an AAPCS variant. 3541 3542 if (isEmptyRecord(getContext(), RetTy, true)) 3543 return ABIArgInfo::getIgnore(); 3544 3545 // Check for homogeneous aggregates with AAPCS-VFP. 3546 if (getABIKind() == AAPCS_VFP) { 3547 const Type *Base = 0; 3548 if (isHomogeneousAggregate(RetTy, Base, getContext())) { 3549 assert(Base && "Base class should be set for homogeneous aggregate"); 3550 // Homogeneous Aggregates are returned directly. 3551 return ABIArgInfo::getDirect(); 3552 } 3553 } 3554 3555 // Aggregates <= 4 bytes are returned in r0; other aggregates 3556 // are returned indirectly. 3557 uint64_t Size = getContext().getTypeSize(RetTy); 3558 if (Size <= 32) { 3559 // Return in the smallest viable integer type. 3560 if (Size <= 8) 3561 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 3562 if (Size <= 16) 3563 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 3564 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 3565 } 3566 3567 return ABIArgInfo::getIndirect(0); 3568 } 3569 3570 /// isIllegalVector - check whether Ty is an illegal vector type. 3571 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { 3572 if (const VectorType *VT = Ty->getAs<VectorType>()) { 3573 // Check whether VT is legal. 3574 unsigned NumElements = VT->getNumElements(); 3575 // NumElements should be power of 2. 3576 if (((NumElements & (NumElements - 1)) != 0) && NumElements != 3) 3577 return true; 3578 } 3579 return false; 3580 } 3581 3582 llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3583 CodeGenFunction &CGF) const { 3584 llvm::Type *BP = CGF.Int8PtrTy; 3585 llvm::Type *BPP = CGF.Int8PtrPtrTy; 3586 3587 CGBuilderTy &Builder = CGF.Builder; 3588 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 3589 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 3590 3591 if (isEmptyRecord(getContext(), Ty, true)) { 3592 // These are ignored for parameter passing purposes. 3593 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 3594 return Builder.CreateBitCast(Addr, PTy); 3595 } 3596 3597 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; 3598 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; 3599 bool IsIndirect = false; 3600 3601 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for 3602 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. 3603 if (getABIKind() == ARMABIInfo::AAPCS_VFP || 3604 getABIKind() == ARMABIInfo::AAPCS) 3605 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); 3606 else 3607 TyAlign = 4; 3608 // Use indirect if size of the illegal vector is bigger than 32 bytes. 3609 if (isIllegalVectorType(Ty) && Size > 32) { 3610 IsIndirect = true; 3611 Size = 4; 3612 TyAlign = 4; 3613 } 3614 3615 // Handle address alignment for ABI alignment > 4 bytes. 3616 if (TyAlign > 4) { 3617 assert((TyAlign & (TyAlign - 1)) == 0 && 3618 "Alignment is not power of 2!"); 3619 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); 3620 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); 3621 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); 3622 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); 3623 } 3624 3625 uint64_t Offset = 3626 llvm::RoundUpToAlignment(Size, 4); 3627 llvm::Value *NextAddr = 3628 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 3629 "ap.next"); 3630 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 3631 3632 if (IsIndirect) 3633 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); 3634 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { 3635 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur 3636 // may not be correctly aligned for the vector type. We create an aligned 3637 // temporary space and copy the content over from ap.cur to the temporary 3638 // space. This is necessary if the natural alignment of the type is greater 3639 // than the ABI alignment. 3640 llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); 3641 CharUnits CharSize = getContext().getTypeSizeInChars(Ty); 3642 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), 3643 "var.align"); 3644 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); 3645 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); 3646 Builder.CreateMemCpy(Dst, Src, 3647 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), 3648 TyAlign, false); 3649 Addr = AlignedTemp; //The content is in aligned location. 3650 } 3651 llvm::Type *PTy = 3652 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 3653 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 3654 3655 return AddrTyped; 3656 } 3657 3658 namespace { 3659 3660 class NaClARMABIInfo : public ABIInfo { 3661 public: 3662 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) 3663 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} 3664 virtual void computeInfo(CGFunctionInfo &FI) const; 3665 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3666 CodeGenFunction &CGF) const; 3667 private: 3668 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. 3669 ARMABIInfo NInfo; // Used for everything else. 3670 }; 3671 3672 class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { 3673 public: 3674 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) 3675 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} 3676 }; 3677 3678 } 3679 3680 void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 3681 if (FI.getASTCallingConvention() == CC_PnaclCall) 3682 PInfo.computeInfo(FI); 3683 else 3684 static_cast<const ABIInfo&>(NInfo).computeInfo(FI); 3685 } 3686 3687 llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3688 CodeGenFunction &CGF) const { 3689 // Always use the native convention; calling pnacl-style varargs functions 3690 // is unsupported. 3691 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); 3692 } 3693 3694 //===----------------------------------------------------------------------===// 3695 // AArch64 ABI Implementation 3696 //===----------------------------------------------------------------------===// 3697 3698 namespace { 3699 3700 class AArch64ABIInfo : public ABIInfo { 3701 public: 3702 AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 3703 3704 private: 3705 // The AArch64 PCS is explicit about return types and argument types being 3706 // handled identically, so we don't need to draw a distinction between 3707 // Argument and Return classification. 3708 ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs, 3709 int &FreeVFPRegs) const; 3710 3711 ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt, 3712 llvm::Type *DirectTy = 0) const; 3713 3714 virtual void computeInfo(CGFunctionInfo &FI) const; 3715 3716 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3717 CodeGenFunction &CGF) const; 3718 }; 3719 3720 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { 3721 public: 3722 AArch64TargetCodeGenInfo(CodeGenTypes &CGT) 3723 :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {} 3724 3725 const AArch64ABIInfo &getABIInfo() const { 3726 return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); 3727 } 3728 3729 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 3730 return 31; 3731 } 3732 3733 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3734 llvm::Value *Address) const { 3735 // 0-31 are x0-x30 and sp: 8 bytes each 3736 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 3737 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31); 3738 3739 // 64-95 are v0-v31: 16 bytes each 3740 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); 3741 AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95); 3742 3743 return false; 3744 } 3745 3746 }; 3747 3748 } 3749 3750 void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 3751 int FreeIntRegs = 8, FreeVFPRegs = 8; 3752 3753 FI.getReturnInfo() = classifyGenericType(FI.getReturnType(), 3754 FreeIntRegs, FreeVFPRegs); 3755 3756 FreeIntRegs = FreeVFPRegs = 8; 3757 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3758 it != ie; ++it) { 3759 it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs); 3760 3761 } 3762 } 3763 3764 ABIArgInfo 3765 AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, 3766 bool IsInt, llvm::Type *DirectTy) const { 3767 if (FreeRegs >= RegsNeeded) { 3768 FreeRegs -= RegsNeeded; 3769 return ABIArgInfo::getDirect(DirectTy); 3770 } 3771 3772 llvm::Type *Padding = 0; 3773 3774 // We need padding so that later arguments don't get filled in anyway. That 3775 // wouldn't happen if only ByVal arguments followed in the same category, but 3776 // a large structure will simply seem to be a pointer as far as LLVM is 3777 // concerned. 3778 if (FreeRegs > 0) { 3779 if (IsInt) 3780 Padding = llvm::Type::getInt64Ty(getVMContext()); 3781 else 3782 Padding = llvm::Type::getFloatTy(getVMContext()); 3783 3784 // Either [N x i64] or [N x float]. 3785 Padding = llvm::ArrayType::get(Padding, FreeRegs); 3786 FreeRegs = 0; 3787 } 3788 3789 return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8, 3790 /*IsByVal=*/ true, /*Realign=*/ false, 3791 Padding); 3792 } 3793 3794 3795 ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty, 3796 int &FreeIntRegs, 3797 int &FreeVFPRegs) const { 3798 // Can only occurs for return, but harmless otherwise. 3799 if (Ty->isVoidType()) 3800 return ABIArgInfo::getIgnore(); 3801 3802 // Large vector types should be returned via memory. There's no such concept 3803 // in the ABI, but they'd be over 16 bytes anyway so no matter how they're 3804 // classified they'd go into memory (see B.3). 3805 if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) { 3806 if (FreeIntRegs > 0) 3807 --FreeIntRegs; 3808 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 3809 } 3810 3811 // All non-aggregate LLVM types have a concrete ABI representation so they can 3812 // be passed directly. After this block we're guaranteed to be in a 3813 // complicated case. 3814 if (!isAggregateTypeForABI(Ty)) { 3815 // Treat an enum type as its underlying type. 3816 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3817 Ty = EnumTy->getDecl()->getIntegerType(); 3818 3819 if (Ty->isFloatingType() || Ty->isVectorType()) 3820 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false); 3821 3822 assert(getContext().getTypeSize(Ty) <= 128 && 3823 "unexpectedly large scalar type"); 3824 3825 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; 3826 3827 // If the type may need padding registers to ensure "alignment", we must be 3828 // careful when this is accounted for. Increasing the effective size covers 3829 // all cases. 3830 if (getContext().getTypeAlign(Ty) == 128) 3831 RegsNeeded += FreeIntRegs % 2 != 0; 3832 3833 return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true); 3834 } 3835 3836 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) { 3837 if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect) 3838 --FreeIntRegs; 3839 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 3840 } 3841 3842 if (isEmptyRecord(getContext(), Ty, true)) { 3843 if (!getContext().getLangOpts().CPlusPlus) { 3844 // Empty structs outside C++ mode are a GNU extension, so no ABI can 3845 // possibly tell us what to do. It turns out (I believe) that GCC ignores 3846 // the object for parameter-passsing purposes. 3847 return ABIArgInfo::getIgnore(); 3848 } 3849 3850 // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode 3851 // description of va_arg in the PCS require that an empty struct does 3852 // actually occupy space for parameter-passing. I'm hoping for a 3853 // clarification giving an explicit paragraph to point to in future. 3854 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true, 3855 llvm::Type::getInt8Ty(getVMContext())); 3856 } 3857 3858 // Homogeneous vector aggregates get passed in registers or on the stack. 3859 const Type *Base = 0; 3860 uint64_t NumMembers = 0; 3861 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) { 3862 assert(Base && "Base class should be set for homogeneous aggregate"); 3863 // Homogeneous aggregates are passed and returned directly. 3864 return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers, 3865 /*IsInt=*/ false); 3866 } 3867 3868 uint64_t Size = getContext().getTypeSize(Ty); 3869 if (Size <= 128) { 3870 // Small structs can use the same direct type whether they're in registers 3871 // or on the stack. 3872 llvm::Type *BaseTy; 3873 unsigned NumBases; 3874 int SizeInRegs = (Size + 63) / 64; 3875 3876 if (getContext().getTypeAlign(Ty) == 128) { 3877 BaseTy = llvm::Type::getIntNTy(getVMContext(), 128); 3878 NumBases = 1; 3879 3880 // If the type may need padding registers to ensure "alignment", we must 3881 // be careful when this is accounted for. Increasing the effective size 3882 // covers all cases. 3883 SizeInRegs += FreeIntRegs % 2 != 0; 3884 } else { 3885 BaseTy = llvm::Type::getInt64Ty(getVMContext()); 3886 NumBases = SizeInRegs; 3887 } 3888 llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases); 3889 3890 return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs, 3891 /*IsInt=*/ true, DirectTy); 3892 } 3893 3894 // If the aggregate is > 16 bytes, it's passed and returned indirectly. In 3895 // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere. 3896 --FreeIntRegs; 3897 return ABIArgInfo::getIndirect(0, /* byVal = */ false); 3898 } 3899 3900 llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3901 CodeGenFunction &CGF) const { 3902 // The AArch64 va_list type and handling is specified in the Procedure Call 3903 // Standard, section B.4: 3904 // 3905 // struct { 3906 // void *__stack; 3907 // void *__gr_top; 3908 // void *__vr_top; 3909 // int __gr_offs; 3910 // int __vr_offs; 3911 // }; 3912 3913 assert(!CGF.CGM.getDataLayout().isBigEndian() 3914 && "va_arg not implemented for big-endian AArch64"); 3915 3916 int FreeIntRegs = 8, FreeVFPRegs = 8; 3917 Ty = CGF.getContext().getCanonicalType(Ty); 3918 ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs); 3919 3920 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); 3921 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 3922 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); 3923 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 3924 3925 llvm::Value *reg_offs_p = 0, *reg_offs = 0; 3926 int reg_top_index; 3927 int RegSize; 3928 if (FreeIntRegs < 8) { 3929 assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs"); 3930 // 3 is the field number of __gr_offs 3931 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); 3932 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); 3933 reg_top_index = 1; // field number for __gr_top 3934 RegSize = 8 * (8 - FreeIntRegs); 3935 } else { 3936 assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs"); 3937 // 4 is the field number of __vr_offs. 3938 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); 3939 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); 3940 reg_top_index = 2; // field number for __vr_top 3941 RegSize = 16 * (8 - FreeVFPRegs); 3942 } 3943 3944 //======================================= 3945 // Find out where argument was passed 3946 //======================================= 3947 3948 // If reg_offs >= 0 we're already using the stack for this type of 3949 // argument. We don't want to keep updating reg_offs (in case it overflows, 3950 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves 3951 // whatever they get). 3952 llvm::Value *UsingStack = 0; 3953 UsingStack = CGF.Builder.CreateICmpSGE(reg_offs, 3954 llvm::ConstantInt::get(CGF.Int32Ty, 0)); 3955 3956 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); 3957 3958 // Otherwise, at least some kind of argument could go in these registers, the 3959 // quesiton is whether this particular type is too big. 3960 CGF.EmitBlock(MaybeRegBlock); 3961 3962 // Integer arguments may need to correct register alignment (for example a 3963 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we 3964 // align __gr_offs to calculate the potential address. 3965 if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { 3966 int Align = getContext().getTypeAlign(Ty) / 8; 3967 3968 reg_offs = CGF.Builder.CreateAdd(reg_offs, 3969 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), 3970 "align_regoffs"); 3971 reg_offs = CGF.Builder.CreateAnd(reg_offs, 3972 llvm::ConstantInt::get(CGF.Int32Ty, -Align), 3973 "aligned_regoffs"); 3974 } 3975 3976 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. 3977 llvm::Value *NewOffset = 0; 3978 NewOffset = CGF.Builder.CreateAdd(reg_offs, 3979 llvm::ConstantInt::get(CGF.Int32Ty, RegSize), 3980 "new_reg_offs"); 3981 CGF.Builder.CreateStore(NewOffset, reg_offs_p); 3982 3983 // Now we're in a position to decide whether this argument really was in 3984 // registers or not. 3985 llvm::Value *InRegs = 0; 3986 InRegs = CGF.Builder.CreateICmpSLE(NewOffset, 3987 llvm::ConstantInt::get(CGF.Int32Ty, 0), 3988 "inreg"); 3989 3990 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); 3991 3992 //======================================= 3993 // Argument was in registers 3994 //======================================= 3995 3996 // Now we emit the code for if the argument was originally passed in 3997 // registers. First start the appropriate block: 3998 CGF.EmitBlock(InRegBlock); 3999 4000 llvm::Value *reg_top_p = 0, *reg_top = 0; 4001 reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); 4002 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); 4003 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); 4004 llvm::Value *RegAddr = 0; 4005 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 4006 4007 if (!AI.isDirect()) { 4008 // If it's been passed indirectly (actually a struct), whatever we find from 4009 // stored registers or on the stack will actually be a struct **. 4010 MemTy = llvm::PointerType::getUnqual(MemTy); 4011 } 4012 4013 const Type *Base = 0; 4014 uint64_t NumMembers; 4015 if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers) 4016 && NumMembers > 1) { 4017 // Homogeneous aggregates passed in registers will have their elements split 4018 // and stored 16-bytes apart regardless of size (they're notionally in qN, 4019 // qN+1, ...). We reload and store into a temporary local variable 4020 // contiguously. 4021 assert(AI.isDirect() && "Homogeneous aggregates should be passed directly"); 4022 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); 4023 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); 4024 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); 4025 4026 for (unsigned i = 0; i < NumMembers; ++i) { 4027 llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i); 4028 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); 4029 LoadAddr = CGF.Builder.CreateBitCast(LoadAddr, 4030 llvm::PointerType::getUnqual(BaseTy)); 4031 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); 4032 4033 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); 4034 CGF.Builder.CreateStore(Elem, StoreAddr); 4035 } 4036 4037 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); 4038 } else { 4039 // Otherwise the object is contiguous in memory 4040 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); 4041 } 4042 4043 CGF.EmitBranch(ContBlock); 4044 4045 //======================================= 4046 // Argument was on the stack 4047 //======================================= 4048 CGF.EmitBlock(OnStackBlock); 4049 4050 llvm::Value *stack_p = 0, *OnStackAddr = 0; 4051 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); 4052 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); 4053 4054 // Again, stack arguments may need realigmnent. In this case both integer and 4055 // floating-point ones might be affected. 4056 if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { 4057 int Align = getContext().getTypeAlign(Ty) / 8; 4058 4059 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); 4060 4061 OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr, 4062 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), 4063 "align_stack"); 4064 OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr, 4065 llvm::ConstantInt::get(CGF.Int64Ty, -Align), 4066 "align_stack"); 4067 4068 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); 4069 } 4070 4071 uint64_t StackSize; 4072 if (AI.isDirect()) 4073 StackSize = getContext().getTypeSize(Ty) / 8; 4074 else 4075 StackSize = 8; 4076 4077 // All stack slots are 8 bytes 4078 StackSize = llvm::RoundUpToAlignment(StackSize, 8); 4079 4080 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); 4081 llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, 4082 "new_stack"); 4083 4084 // Write the new value of __stack for the next call to va_arg 4085 CGF.Builder.CreateStore(NewStack, stack_p); 4086 4087 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); 4088 4089 CGF.EmitBranch(ContBlock); 4090 4091 //======================================= 4092 // Tidy up 4093 //======================================= 4094 CGF.EmitBlock(ContBlock); 4095 4096 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); 4097 ResAddr->addIncoming(RegAddr, InRegBlock); 4098 ResAddr->addIncoming(OnStackAddr, OnStackBlock); 4099 4100 if (AI.isDirect()) 4101 return ResAddr; 4102 4103 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); 4104 } 4105 4106 //===----------------------------------------------------------------------===// 4107 // NVPTX ABI Implementation 4108 //===----------------------------------------------------------------------===// 4109 4110 namespace { 4111 4112 class NVPTXABIInfo : public ABIInfo { 4113 public: 4114 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 4115 4116 ABIArgInfo classifyReturnType(QualType RetTy) const; 4117 ABIArgInfo classifyArgumentType(QualType Ty) const; 4118 4119 virtual void computeInfo(CGFunctionInfo &FI) const; 4120 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 4121 CodeGenFunction &CFG) const; 4122 }; 4123 4124 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { 4125 public: 4126 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) 4127 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} 4128 4129 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 4130 CodeGen::CodeGenModule &M) const; 4131 private: 4132 static void addKernelMetadata(llvm::Function *F); 4133 }; 4134 4135 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { 4136 if (RetTy->isVoidType()) 4137 return ABIArgInfo::getIgnore(); 4138 if (isAggregateTypeForABI(RetTy)) 4139 return ABIArgInfo::getIndirect(0); 4140 return ABIArgInfo::getDirect(); 4141 } 4142 4143 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { 4144 if (isAggregateTypeForABI(Ty)) 4145 return ABIArgInfo::getIndirect(0); 4146 4147 return ABIArgInfo::getDirect(); 4148 } 4149 4150 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { 4151 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4152 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 4153 it != ie; ++it) 4154 it->info = classifyArgumentType(it->type); 4155 4156 // Always honor user-specified calling convention. 4157 if (FI.getCallingConvention() != llvm::CallingConv::C) 4158 return; 4159 4160 FI.setEffectiveCallingConvention(getRuntimeCC()); 4161 } 4162 4163 llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 4164 CodeGenFunction &CFG) const { 4165 llvm_unreachable("NVPTX does not support varargs"); 4166 } 4167 4168 void NVPTXTargetCodeGenInfo:: 4169 SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 4170 CodeGen::CodeGenModule &M) const{ 4171 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 4172 if (!FD) return; 4173 4174 llvm::Function *F = cast<llvm::Function>(GV); 4175 4176 // Perform special handling in OpenCL mode 4177 if (M.getLangOpts().OpenCL) { 4178 // Use OpenCL function attributes to check for kernel functions 4179 // By default, all functions are device functions 4180 if (FD->hasAttr<OpenCLKernelAttr>()) { 4181 // OpenCL __kernel functions get kernel metadata 4182 addKernelMetadata(F); 4183 // And kernel functions are not subject to inlining 4184 F->addFnAttr(llvm::Attribute::NoInline); 4185 } 4186 } 4187 4188 // Perform special handling in CUDA mode. 4189 if (M.getLangOpts().CUDA) { 4190 // CUDA __global__ functions get a kernel metadata entry. Since 4191 // __global__ functions cannot be called from the device, we do not 4192 // need to set the noinline attribute. 4193 if (FD->getAttr<CUDAGlobalAttr>()) 4194 addKernelMetadata(F); 4195 } 4196 } 4197 4198 void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) { 4199 llvm::Module *M = F->getParent(); 4200 llvm::LLVMContext &Ctx = M->getContext(); 4201 4202 // Get "nvvm.annotations" metadata node 4203 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); 4204 4205 // Create !{<func-ref>, metadata !"kernel", i32 1} node 4206 llvm::SmallVector<llvm::Value *, 3> MDVals; 4207 MDVals.push_back(F); 4208 MDVals.push_back(llvm::MDString::get(Ctx, "kernel")); 4209 MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1)); 4210 4211 // Append metadata to nvvm.annotations 4212 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 4213 } 4214 4215 } 4216 4217 //===----------------------------------------------------------------------===// 4218 // SystemZ ABI Implementation 4219 //===----------------------------------------------------------------------===// 4220 4221 namespace { 4222 4223 class SystemZABIInfo : public ABIInfo { 4224 public: 4225 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 4226 4227 bool isPromotableIntegerType(QualType Ty) const; 4228 bool isCompoundType(QualType Ty) const; 4229 bool isFPArgumentType(QualType Ty) const; 4230 4231 ABIArgInfo classifyReturnType(QualType RetTy) const; 4232 ABIArgInfo classifyArgumentType(QualType ArgTy) const; 4233 4234 virtual void computeInfo(CGFunctionInfo &FI) const { 4235 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 4236 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 4237 it != ie; ++it) 4238 it->info = classifyArgumentType(it->type); 4239 } 4240 4241 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 4242 CodeGenFunction &CGF) const; 4243 }; 4244 4245 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { 4246 public: 4247 SystemZTargetCodeGenInfo(CodeGenTypes &CGT) 4248 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} 4249 }; 4250 4251 } 4252 4253 bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { 4254 // Treat an enum type as its underlying type. 4255 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4256 Ty = EnumTy->getDecl()->getIntegerType(); 4257 4258 // Promotable integer types are required to be promoted by the ABI. 4259 if (Ty->isPromotableIntegerType()) 4260 return true; 4261 4262 // 32-bit values must also be promoted. 4263 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 4264 switch (BT->getKind()) { 4265 case BuiltinType::Int: 4266 case BuiltinType::UInt: 4267 return true; 4268 default: 4269 return false; 4270 } 4271 return false; 4272 } 4273 4274 bool SystemZABIInfo::isCompoundType(QualType Ty) const { 4275 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); 4276 } 4277 4278 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { 4279 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 4280 switch (BT->getKind()) { 4281 case BuiltinType::Float: 4282 case BuiltinType::Double: 4283 return true; 4284 default: 4285 return false; 4286 } 4287 4288 if (const RecordType *RT = Ty->getAsStructureType()) { 4289 const RecordDecl *RD = RT->getDecl(); 4290 bool Found = false; 4291 4292 // If this is a C++ record, check the bases first. 4293 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 4294 for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(), 4295 E = CXXRD->bases_end(); I != E; ++I) { 4296 QualType Base = I->getType(); 4297 4298 // Empty bases don't affect things either way. 4299 if (isEmptyRecord(getContext(), Base, true)) 4300 continue; 4301 4302 if (Found) 4303 return false; 4304 Found = isFPArgumentType(Base); 4305 if (!Found) 4306 return false; 4307 } 4308 4309 // Check the fields. 4310 for (RecordDecl::field_iterator I = RD->field_begin(), 4311 E = RD->field_end(); I != E; ++I) { 4312 const FieldDecl *FD = *I; 4313 4314 // Empty bitfields don't affect things either way. 4315 // Unlike isSingleElementStruct(), empty structure and array fields 4316 // do count. So do anonymous bitfields that aren't zero-sized. 4317 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) 4318 return true; 4319 4320 // Unlike isSingleElementStruct(), arrays do not count. 4321 // Nested isFPArgumentType structures still do though. 4322 if (Found) 4323 return false; 4324 Found = isFPArgumentType(FD->getType()); 4325 if (!Found) 4326 return false; 4327 } 4328 4329 // Unlike isSingleElementStruct(), trailing padding is allowed. 4330 // An 8-byte aligned struct s { float f; } is passed as a double. 4331 return Found; 4332 } 4333 4334 return false; 4335 } 4336 4337 llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 4338 CodeGenFunction &CGF) const { 4339 // Assume that va_list type is correct; should be pointer to LLVM type: 4340 // struct { 4341 // i64 __gpr; 4342 // i64 __fpr; 4343 // i8 *__overflow_arg_area; 4344 // i8 *__reg_save_area; 4345 // }; 4346 4347 // Every argument occupies 8 bytes and is passed by preference in either 4348 // GPRs or FPRs. 4349 Ty = CGF.getContext().getCanonicalType(Ty); 4350 ABIArgInfo AI = classifyArgumentType(Ty); 4351 bool InFPRs = isFPArgumentType(Ty); 4352 4353 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 4354 bool IsIndirect = AI.isIndirect(); 4355 unsigned UnpaddedBitSize; 4356 if (IsIndirect) { 4357 APTy = llvm::PointerType::getUnqual(APTy); 4358 UnpaddedBitSize = 64; 4359 } else 4360 UnpaddedBitSize = getContext().getTypeSize(Ty); 4361 unsigned PaddedBitSize = 64; 4362 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); 4363 4364 unsigned PaddedSize = PaddedBitSize / 8; 4365 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; 4366 4367 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; 4368 if (InFPRs) { 4369 MaxRegs = 4; // Maximum of 4 FPR arguments 4370 RegCountField = 1; // __fpr 4371 RegSaveIndex = 16; // save offset for f0 4372 RegPadding = 0; // floats are passed in the high bits of an FPR 4373 } else { 4374 MaxRegs = 5; // Maximum of 5 GPR arguments 4375 RegCountField = 0; // __gpr 4376 RegSaveIndex = 2; // save offset for r2 4377 RegPadding = Padding; // values are passed in the low bits of a GPR 4378 } 4379 4380 llvm::Value *RegCountPtr = 4381 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); 4382 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); 4383 llvm::Type *IndexTy = RegCount->getType(); 4384 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); 4385 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, 4386 "fits_in_regs"); 4387 4388 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 4389 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 4390 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 4391 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 4392 4393 // Emit code to load the value if it was passed in registers. 4394 CGF.EmitBlock(InRegBlock); 4395 4396 // Work out the address of an argument register. 4397 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); 4398 llvm::Value *ScaledRegCount = 4399 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); 4400 llvm::Value *RegBase = 4401 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); 4402 llvm::Value *RegOffset = 4403 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); 4404 llvm::Value *RegSaveAreaPtr = 4405 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); 4406 llvm::Value *RegSaveArea = 4407 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); 4408 llvm::Value *RawRegAddr = 4409 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); 4410 llvm::Value *RegAddr = 4411 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); 4412 4413 // Update the register count 4414 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); 4415 llvm::Value *NewRegCount = 4416 CGF.Builder.CreateAdd(RegCount, One, "reg_count"); 4417 CGF.Builder.CreateStore(NewRegCount, RegCountPtr); 4418 CGF.EmitBranch(ContBlock); 4419 4420 // Emit code to load the value if it was passed in memory. 4421 CGF.EmitBlock(InMemBlock); 4422 4423 // Work out the address of a stack argument. 4424 llvm::Value *OverflowArgAreaPtr = 4425 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); 4426 llvm::Value *OverflowArgArea = 4427 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); 4428 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); 4429 llvm::Value *RawMemAddr = 4430 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); 4431 llvm::Value *MemAddr = 4432 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); 4433 4434 // Update overflow_arg_area_ptr pointer 4435 llvm::Value *NewOverflowArgArea = 4436 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); 4437 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 4438 CGF.EmitBranch(ContBlock); 4439 4440 // Return the appropriate result. 4441 CGF.EmitBlock(ContBlock); 4442 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); 4443 ResAddr->addIncoming(RegAddr, InRegBlock); 4444 ResAddr->addIncoming(MemAddr, InMemBlock); 4445 4446 if (IsIndirect) 4447 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); 4448 4449 return ResAddr; 4450 } 4451 4452 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( 4453 const llvm::Triple &Triple, const CodeGenOptions &Opts) { 4454 assert(Triple.getArch() == llvm::Triple::x86); 4455 4456 switch (Opts.getStructReturnConvention()) { 4457 case CodeGenOptions::SRCK_Default: 4458 break; 4459 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return 4460 return false; 4461 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return 4462 return true; 4463 } 4464 4465 if (Triple.isOSDarwin()) 4466 return true; 4467 4468 switch (Triple.getOS()) { 4469 case llvm::Triple::Cygwin: 4470 case llvm::Triple::MinGW32: 4471 case llvm::Triple::AuroraUX: 4472 case llvm::Triple::DragonFly: 4473 case llvm::Triple::FreeBSD: 4474 case llvm::Triple::OpenBSD: 4475 case llvm::Triple::Bitrig: 4476 case llvm::Triple::Win32: 4477 return true; 4478 default: 4479 return false; 4480 } 4481 } 4482 4483 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { 4484 if (RetTy->isVoidType()) 4485 return ABIArgInfo::getIgnore(); 4486 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) 4487 return ABIArgInfo::getIndirect(0); 4488 return (isPromotableIntegerType(RetTy) ? 4489 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 4490 } 4491 4492 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { 4493 // Handle the generic C++ ABI. 4494 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) 4495 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 4496 4497 // Integers and enums are extended to full register width. 4498 if (isPromotableIntegerType(Ty)) 4499 return ABIArgInfo::getExtend(); 4500 4501 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. 4502 uint64_t Size = getContext().getTypeSize(Ty); 4503 if (Size != 8 && Size != 16 && Size != 32 && Size != 64) 4504 return ABIArgInfo::getIndirect(0); 4505 4506 // Handle small structures. 4507 if (const RecordType *RT = Ty->getAs<RecordType>()) { 4508 // Structures with flexible arrays have variable length, so really 4509 // fail the size test above. 4510 const RecordDecl *RD = RT->getDecl(); 4511 if (RD->hasFlexibleArrayMember()) 4512 return ABIArgInfo::getIndirect(0); 4513 4514 // The structure is passed as an unextended integer, a float, or a double. 4515 llvm::Type *PassTy; 4516 if (isFPArgumentType(Ty)) { 4517 assert(Size == 32 || Size == 64); 4518 if (Size == 32) 4519 PassTy = llvm::Type::getFloatTy(getVMContext()); 4520 else 4521 PassTy = llvm::Type::getDoubleTy(getVMContext()); 4522 } else 4523 PassTy = llvm::IntegerType::get(getVMContext(), Size); 4524 return ABIArgInfo::getDirect(PassTy); 4525 } 4526 4527 // Non-structure compounds are passed indirectly. 4528 if (isCompoundType(Ty)) 4529 return ABIArgInfo::getIndirect(0); 4530 4531 return ABIArgInfo::getDirect(0); 4532 } 4533 4534 //===----------------------------------------------------------------------===// 4535 // MSP430 ABI Implementation 4536 //===----------------------------------------------------------------------===// 4537 4538 namespace { 4539 4540 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { 4541 public: 4542 MSP430TargetCodeGenInfo(CodeGenTypes &CGT) 4543 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 4544 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 4545 CodeGen::CodeGenModule &M) const; 4546 }; 4547 4548 } 4549 4550 void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, 4551 llvm::GlobalValue *GV, 4552 CodeGen::CodeGenModule &M) const { 4553 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 4554 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { 4555 // Handle 'interrupt' attribute: 4556 llvm::Function *F = cast<llvm::Function>(GV); 4557 4558 // Step 1: Set ISR calling convention. 4559 F->setCallingConv(llvm::CallingConv::MSP430_INTR); 4560 4561 // Step 2: Add attributes goodness. 4562 F->addFnAttr(llvm::Attribute::NoInline); 4563 4564 // Step 3: Emit ISR vector alias. 4565 unsigned Num = attr->getNumber() / 2; 4566 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, 4567 "__isr_" + Twine(Num), 4568 GV, &M.getModule()); 4569 } 4570 } 4571 } 4572 4573 //===----------------------------------------------------------------------===// 4574 // MIPS ABI Implementation. This works for both little-endian and 4575 // big-endian variants. 4576 //===----------------------------------------------------------------------===// 4577 4578 namespace { 4579 class MipsABIInfo : public ABIInfo { 4580 bool IsO32; 4581 unsigned MinABIStackAlignInBytes, StackAlignInBytes; 4582 void CoerceToIntArgs(uint64_t TySize, 4583 SmallVectorImpl<llvm::Type *> &ArgList) const; 4584 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; 4585 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; 4586 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; 4587 public: 4588 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : 4589 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), 4590 StackAlignInBytes(IsO32 ? 8 : 16) {} 4591 4592 ABIArgInfo classifyReturnType(QualType RetTy) const; 4593 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; 4594 virtual void computeInfo(CGFunctionInfo &FI) const; 4595 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 4596 CodeGenFunction &CGF) const; 4597 }; 4598 4599 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { 4600 unsigned SizeOfUnwindException; 4601 public: 4602 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) 4603 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), 4604 SizeOfUnwindException(IsO32 ? 24 : 32) {} 4605 4606 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 4607 return 29; 4608 } 4609 4610 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 4611 CodeGen::CodeGenModule &CGM) const { 4612 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 4613 if (!FD) return; 4614 llvm::Function *Fn = cast<llvm::Function>(GV); 4615 if (FD->hasAttr<Mips16Attr>()) { 4616 Fn->addFnAttr("mips16"); 4617 } 4618 else if (FD->hasAttr<NoMips16Attr>()) { 4619 Fn->addFnAttr("nomips16"); 4620 } 4621 } 4622 4623 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4624 llvm::Value *Address) const; 4625 4626 unsigned getSizeOfUnwindException() const { 4627 return SizeOfUnwindException; 4628 } 4629 }; 4630 } 4631 4632 void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, 4633 SmallVectorImpl<llvm::Type *> &ArgList) const { 4634 llvm::IntegerType *IntTy = 4635 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 4636 4637 // Add (TySize / MinABIStackAlignInBytes) args of IntTy. 4638 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) 4639 ArgList.push_back(IntTy); 4640 4641 // If necessary, add one more integer type to ArgList. 4642 unsigned R = TySize % (MinABIStackAlignInBytes * 8); 4643 4644 if (R) 4645 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); 4646 } 4647 4648 // In N32/64, an aligned double precision floating point field is passed in 4649 // a register. 4650 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { 4651 SmallVector<llvm::Type*, 8> ArgList, IntArgList; 4652 4653 if (IsO32) { 4654 CoerceToIntArgs(TySize, ArgList); 4655 return llvm::StructType::get(getVMContext(), ArgList); 4656 } 4657 4658 if (Ty->isComplexType()) 4659 return CGT.ConvertType(Ty); 4660 4661 const RecordType *RT = Ty->getAs<RecordType>(); 4662 4663 // Unions/vectors are passed in integer registers. 4664 if (!RT || !RT->isStructureOrClassType()) { 4665 CoerceToIntArgs(TySize, ArgList); 4666 return llvm::StructType::get(getVMContext(), ArgList); 4667 } 4668 4669 const RecordDecl *RD = RT->getDecl(); 4670 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 4671 assert(!(TySize % 8) && "Size of structure must be multiple of 8."); 4672 4673 uint64_t LastOffset = 0; 4674 unsigned idx = 0; 4675 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); 4676 4677 // Iterate over fields in the struct/class and check if there are any aligned 4678 // double fields. 4679 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 4680 i != e; ++i, ++idx) { 4681 const QualType Ty = i->getType(); 4682 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 4683 4684 if (!BT || BT->getKind() != BuiltinType::Double) 4685 continue; 4686 4687 uint64_t Offset = Layout.getFieldOffset(idx); 4688 if (Offset % 64) // Ignore doubles that are not aligned. 4689 continue; 4690 4691 // Add ((Offset - LastOffset) / 64) args of type i64. 4692 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) 4693 ArgList.push_back(I64); 4694 4695 // Add double type. 4696 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); 4697 LastOffset = Offset + 64; 4698 } 4699 4700 CoerceToIntArgs(TySize - LastOffset, IntArgList); 4701 ArgList.append(IntArgList.begin(), IntArgList.end()); 4702 4703 return llvm::StructType::get(getVMContext(), ArgList); 4704 } 4705 4706 llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const { 4707 assert((Offset % MinABIStackAlignInBytes) == 0); 4708 4709 if ((Align - 1) & Offset) 4710 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 4711 4712 return 0; 4713 } 4714 4715 ABIArgInfo 4716 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { 4717 uint64_t OrigOffset = Offset; 4718 uint64_t TySize = getContext().getTypeSize(Ty); 4719 uint64_t Align = getContext().getTypeAlign(Ty) / 8; 4720 4721 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), 4722 (uint64_t)StackAlignInBytes); 4723 Offset = llvm::RoundUpToAlignment(Offset, Align); 4724 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8; 4725 4726 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { 4727 // Ignore empty aggregates. 4728 if (TySize == 0) 4729 return ABIArgInfo::getIgnore(); 4730 4731 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) { 4732 Offset = OrigOffset + MinABIStackAlignInBytes; 4733 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 4734 } 4735 4736 // If we have reached here, aggregates are passed directly by coercing to 4737 // another structure type. Padding is inserted if the offset of the 4738 // aggregate is unaligned. 4739 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, 4740 getPaddingType(Align, OrigOffset)); 4741 } 4742 4743 // Treat an enum type as its underlying type. 4744 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 4745 Ty = EnumTy->getDecl()->getIntegerType(); 4746 4747 if (Ty->isPromotableIntegerType()) 4748 return ABIArgInfo::getExtend(); 4749 4750 return ABIArgInfo::getDirect(0, 0, 4751 IsO32 ? 0 : getPaddingType(Align, OrigOffset)); 4752 } 4753 4754 llvm::Type* 4755 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { 4756 const RecordType *RT = RetTy->getAs<RecordType>(); 4757 SmallVector<llvm::Type*, 8> RTList; 4758 4759 if (RT && RT->isStructureOrClassType()) { 4760 const RecordDecl *RD = RT->getDecl(); 4761 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 4762 unsigned FieldCnt = Layout.getFieldCount(); 4763 4764 // N32/64 returns struct/classes in floating point registers if the 4765 // following conditions are met: 4766 // 1. The size of the struct/class is no larger than 128-bit. 4767 // 2. The struct/class has one or two fields all of which are floating 4768 // point types. 4769 // 3. The offset of the first field is zero (this follows what gcc does). 4770 // 4771 // Any other composite results are returned in integer registers. 4772 // 4773 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { 4774 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); 4775 for (; b != e; ++b) { 4776 const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); 4777 4778 if (!BT || !BT->isFloatingPoint()) 4779 break; 4780 4781 RTList.push_back(CGT.ConvertType(b->getType())); 4782 } 4783 4784 if (b == e) 4785 return llvm::StructType::get(getVMContext(), RTList, 4786 RD->hasAttr<PackedAttr>()); 4787 4788 RTList.clear(); 4789 } 4790 } 4791 4792 CoerceToIntArgs(Size, RTList); 4793 return llvm::StructType::get(getVMContext(), RTList); 4794 } 4795 4796 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { 4797 uint64_t Size = getContext().getTypeSize(RetTy); 4798 4799 if (RetTy->isVoidType() || Size == 0) 4800 return ABIArgInfo::getIgnore(); 4801 4802 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { 4803 if (isRecordReturnIndirect(RetTy, CGT)) 4804 return ABIArgInfo::getIndirect(0); 4805 4806 if (Size <= 128) { 4807 if (RetTy->isAnyComplexType()) 4808 return ABIArgInfo::getDirect(); 4809 4810 // O32 returns integer vectors in registers. 4811 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) 4812 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 4813 4814 if (!IsO32) 4815 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 4816 } 4817 4818 return ABIArgInfo::getIndirect(0); 4819 } 4820 4821 // Treat an enum type as its underlying type. 4822 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 4823 RetTy = EnumTy->getDecl()->getIntegerType(); 4824 4825 return (RetTy->isPromotableIntegerType() ? 4826 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 4827 } 4828 4829 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { 4830 ABIArgInfo &RetInfo = FI.getReturnInfo(); 4831 RetInfo = classifyReturnType(FI.getReturnType()); 4832 4833 // Check if a pointer to an aggregate is passed as a hidden argument. 4834 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; 4835 4836 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 4837 it != ie; ++it) 4838 it->info = classifyArgumentType(it->type, Offset); 4839 } 4840 4841 llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 4842 CodeGenFunction &CGF) const { 4843 llvm::Type *BP = CGF.Int8PtrTy; 4844 llvm::Type *BPP = CGF.Int8PtrPtrTy; 4845 4846 CGBuilderTy &Builder = CGF.Builder; 4847 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 4848 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 4849 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; 4850 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 4851 llvm::Value *AddrTyped; 4852 unsigned PtrWidth = getTarget().getPointerWidth(0); 4853 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; 4854 4855 if (TypeAlign > MinABIStackAlignInBytes) { 4856 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); 4857 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); 4858 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); 4859 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); 4860 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); 4861 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); 4862 } 4863 else 4864 AddrTyped = Builder.CreateBitCast(Addr, PTy); 4865 4866 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); 4867 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); 4868 uint64_t Offset = 4869 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); 4870 llvm::Value *NextAddr = 4871 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), 4872 "ap.next"); 4873 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 4874 4875 return AddrTyped; 4876 } 4877 4878 bool 4879 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 4880 llvm::Value *Address) const { 4881 // This information comes from gcc's implementation, which seems to 4882 // as canonical as it gets. 4883 4884 // Everything on MIPS is 4 bytes. Double-precision FP registers 4885 // are aliased to pairs of single-precision FP registers. 4886 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 4887 4888 // 0-31 are the general purpose registers, $0 - $31. 4889 // 32-63 are the floating-point registers, $f0 - $f31. 4890 // 64 and 65 are the multiply/divide registers, $hi and $lo. 4891 // 66 is the (notional, I think) register for signal-handler return. 4892 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); 4893 4894 // 67-74 are the floating-point status registers, $fcc0 - $fcc7. 4895 // They are one bit wide and ignored here. 4896 4897 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. 4898 // (coprocessor 1 is the FP unit) 4899 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. 4900 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. 4901 // 176-181 are the DSP accumulator registers. 4902 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); 4903 return false; 4904 } 4905 4906 //===----------------------------------------------------------------------===// 4907 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. 4908 // Currently subclassed only to implement custom OpenCL C function attribute 4909 // handling. 4910 //===----------------------------------------------------------------------===// 4911 4912 namespace { 4913 4914 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { 4915 public: 4916 TCETargetCodeGenInfo(CodeGenTypes &CGT) 4917 : DefaultTargetCodeGenInfo(CGT) {} 4918 4919 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 4920 CodeGen::CodeGenModule &M) const; 4921 }; 4922 4923 void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, 4924 llvm::GlobalValue *GV, 4925 CodeGen::CodeGenModule &M) const { 4926 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 4927 if (!FD) return; 4928 4929 llvm::Function *F = cast<llvm::Function>(GV); 4930 4931 if (M.getLangOpts().OpenCL) { 4932 if (FD->hasAttr<OpenCLKernelAttr>()) { 4933 // OpenCL C Kernel functions are not subject to inlining 4934 F->addFnAttr(llvm::Attribute::NoInline); 4935 4936 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) { 4937 4938 // Convert the reqd_work_group_size() attributes to metadata. 4939 llvm::LLVMContext &Context = F->getContext(); 4940 llvm::NamedMDNode *OpenCLMetadata = 4941 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); 4942 4943 SmallVector<llvm::Value*, 5> Operands; 4944 Operands.push_back(F); 4945 4946 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 4947 llvm::APInt(32, 4948 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim()))); 4949 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 4950 llvm::APInt(32, 4951 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim()))); 4952 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 4953 llvm::APInt(32, 4954 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim()))); 4955 4956 // Add a boolean constant operand for "required" (true) or "hint" (false) 4957 // for implementing the work_group_size_hint attr later. Currently 4958 // always true as the hint is not yet implemented. 4959 Operands.push_back(llvm::ConstantInt::getTrue(Context)); 4960 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); 4961 } 4962 } 4963 } 4964 } 4965 4966 } 4967 4968 //===----------------------------------------------------------------------===// 4969 // Hexagon ABI Implementation 4970 //===----------------------------------------------------------------------===// 4971 4972 namespace { 4973 4974 class HexagonABIInfo : public ABIInfo { 4975 4976 4977 public: 4978 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 4979 4980 private: 4981 4982 ABIArgInfo classifyReturnType(QualType RetTy) const; 4983 ABIArgInfo classifyArgumentType(QualType RetTy) const; 4984 4985 virtual void computeInfo(CGFunctionInfo &FI) const; 4986 4987 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 4988 CodeGenFunction &CGF) const; 4989 }; 4990 4991 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { 4992 public: 4993 HexagonTargetCodeGenInfo(CodeGenTypes &CGT) 4994 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} 4995 4996 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 4997 return 29; 4998 } 4999 }; 5000 5001 } 5002 5003 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { 5004 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 5005 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 5006 it != ie; ++it) 5007 it->info = classifyArgumentType(it->type); 5008 } 5009 5010 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { 5011 if (!isAggregateTypeForABI(Ty)) { 5012 // Treat an enum type as its underlying type. 5013 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 5014 Ty = EnumTy->getDecl()->getIntegerType(); 5015 5016 return (Ty->isPromotableIntegerType() ? 5017 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 5018 } 5019 5020 // Ignore empty records. 5021 if (isEmptyRecord(getContext(), Ty, true)) 5022 return ABIArgInfo::getIgnore(); 5023 5024 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) 5025 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 5026 5027 uint64_t Size = getContext().getTypeSize(Ty); 5028 if (Size > 64) 5029 return ABIArgInfo::getIndirect(0, /*ByVal=*/true); 5030 // Pass in the smallest viable integer type. 5031 else if (Size > 32) 5032 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 5033 else if (Size > 16) 5034 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 5035 else if (Size > 8) 5036 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 5037 else 5038 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 5039 } 5040 5041 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { 5042 if (RetTy->isVoidType()) 5043 return ABIArgInfo::getIgnore(); 5044 5045 // Large vector types should be returned via memory. 5046 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) 5047 return ABIArgInfo::getIndirect(0); 5048 5049 if (!isAggregateTypeForABI(RetTy)) { 5050 // Treat an enum type as its underlying type. 5051 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 5052 RetTy = EnumTy->getDecl()->getIntegerType(); 5053 5054 return (RetTy->isPromotableIntegerType() ? 5055 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 5056 } 5057 5058 // Structures with either a non-trivial destructor or a non-trivial 5059 // copy constructor are always indirect. 5060 if (isRecordReturnIndirect(RetTy, CGT)) 5061 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 5062 5063 if (isEmptyRecord(getContext(), RetTy, true)) 5064 return ABIArgInfo::getIgnore(); 5065 5066 // Aggregates <= 8 bytes are returned in r0; other aggregates 5067 // are returned indirectly. 5068 uint64_t Size = getContext().getTypeSize(RetTy); 5069 if (Size <= 64) { 5070 // Return in the smallest viable integer type. 5071 if (Size <= 8) 5072 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 5073 if (Size <= 16) 5074 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 5075 if (Size <= 32) 5076 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 5077 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 5078 } 5079 5080 return ABIArgInfo::getIndirect(0, /*ByVal=*/true); 5081 } 5082 5083 llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 5084 CodeGenFunction &CGF) const { 5085 // FIXME: Need to handle alignment 5086 llvm::Type *BPP = CGF.Int8PtrPtrTy; 5087 5088 CGBuilderTy &Builder = CGF.Builder; 5089 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 5090 "ap"); 5091 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 5092 llvm::Type *PTy = 5093 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 5094 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 5095 5096 uint64_t Offset = 5097 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); 5098 llvm::Value *NextAddr = 5099 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 5100 "ap.next"); 5101 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 5102 5103 return AddrTyped; 5104 } 5105 5106 5107 //===----------------------------------------------------------------------===// 5108 // SPARC v9 ABI Implementation. 5109 // Based on the SPARC Compliance Definition version 2.4.1. 5110 // 5111 // Function arguments a mapped to a nominal "parameter array" and promoted to 5112 // registers depending on their type. Each argument occupies 8 or 16 bytes in 5113 // the array, structs larger than 16 bytes are passed indirectly. 5114 // 5115 // One case requires special care: 5116 // 5117 // struct mixed { 5118 // int i; 5119 // float f; 5120 // }; 5121 // 5122 // When a struct mixed is passed by value, it only occupies 8 bytes in the 5123 // parameter array, but the int is passed in an integer register, and the float 5124 // is passed in a floating point register. This is represented as two arguments 5125 // with the LLVM IR inreg attribute: 5126 // 5127 // declare void f(i32 inreg %i, float inreg %f) 5128 // 5129 // The code generator will only allocate 4 bytes from the parameter array for 5130 // the inreg arguments. All other arguments are allocated a multiple of 8 5131 // bytes. 5132 // 5133 namespace { 5134 class SparcV9ABIInfo : public ABIInfo { 5135 public: 5136 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 5137 5138 private: 5139 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; 5140 virtual void computeInfo(CGFunctionInfo &FI) const; 5141 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 5142 CodeGenFunction &CGF) const; 5143 5144 // Coercion type builder for structs passed in registers. The coercion type 5145 // serves two purposes: 5146 // 5147 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' 5148 // in registers. 5149 // 2. Expose aligned floating point elements as first-level elements, so the 5150 // code generator knows to pass them in floating point registers. 5151 // 5152 // We also compute the InReg flag which indicates that the struct contains 5153 // aligned 32-bit floats. 5154 // 5155 struct CoerceBuilder { 5156 llvm::LLVMContext &Context; 5157 const llvm::DataLayout &DL; 5158 SmallVector<llvm::Type*, 8> Elems; 5159 uint64_t Size; 5160 bool InReg; 5161 5162 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) 5163 : Context(c), DL(dl), Size(0), InReg(false) {} 5164 5165 // Pad Elems with integers until Size is ToSize. 5166 void pad(uint64_t ToSize) { 5167 assert(ToSize >= Size && "Cannot remove elements"); 5168 if (ToSize == Size) 5169 return; 5170 5171 // Finish the current 64-bit word. 5172 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); 5173 if (Aligned > Size && Aligned <= ToSize) { 5174 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); 5175 Size = Aligned; 5176 } 5177 5178 // Add whole 64-bit words. 5179 while (Size + 64 <= ToSize) { 5180 Elems.push_back(llvm::Type::getInt64Ty(Context)); 5181 Size += 64; 5182 } 5183 5184 // Final in-word padding. 5185 if (Size < ToSize) { 5186 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); 5187 Size = ToSize; 5188 } 5189 } 5190 5191 // Add a floating point element at Offset. 5192 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { 5193 // Unaligned floats are treated as integers. 5194 if (Offset % Bits) 5195 return; 5196 // The InReg flag is only required if there are any floats < 64 bits. 5197 if (Bits < 64) 5198 InReg = true; 5199 pad(Offset); 5200 Elems.push_back(Ty); 5201 Size = Offset + Bits; 5202 } 5203 5204 // Add a struct type to the coercion type, starting at Offset (in bits). 5205 void addStruct(uint64_t Offset, llvm::StructType *StrTy) { 5206 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); 5207 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { 5208 llvm::Type *ElemTy = StrTy->getElementType(i); 5209 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); 5210 switch (ElemTy->getTypeID()) { 5211 case llvm::Type::StructTyID: 5212 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); 5213 break; 5214 case llvm::Type::FloatTyID: 5215 addFloat(ElemOffset, ElemTy, 32); 5216 break; 5217 case llvm::Type::DoubleTyID: 5218 addFloat(ElemOffset, ElemTy, 64); 5219 break; 5220 case llvm::Type::FP128TyID: 5221 addFloat(ElemOffset, ElemTy, 128); 5222 break; 5223 case llvm::Type::PointerTyID: 5224 if (ElemOffset % 64 == 0) { 5225 pad(ElemOffset); 5226 Elems.push_back(ElemTy); 5227 Size += 64; 5228 } 5229 break; 5230 default: 5231 break; 5232 } 5233 } 5234 } 5235 5236 // Check if Ty is a usable substitute for the coercion type. 5237 bool isUsableType(llvm::StructType *Ty) const { 5238 if (Ty->getNumElements() != Elems.size()) 5239 return false; 5240 for (unsigned i = 0, e = Elems.size(); i != e; ++i) 5241 if (Elems[i] != Ty->getElementType(i)) 5242 return false; 5243 return true; 5244 } 5245 5246 // Get the coercion type as a literal struct type. 5247 llvm::Type *getType() const { 5248 if (Elems.size() == 1) 5249 return Elems.front(); 5250 else 5251 return llvm::StructType::get(Context, Elems); 5252 } 5253 }; 5254 }; 5255 } // end anonymous namespace 5256 5257 ABIArgInfo 5258 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { 5259 if (Ty->isVoidType()) 5260 return ABIArgInfo::getIgnore(); 5261 5262 uint64_t Size = getContext().getTypeSize(Ty); 5263 5264 // Anything too big to fit in registers is passed with an explicit indirect 5265 // pointer / sret pointer. 5266 if (Size > SizeLimit) 5267 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 5268 5269 // Treat an enum type as its underlying type. 5270 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 5271 Ty = EnumTy->getDecl()->getIntegerType(); 5272 5273 // Integer types smaller than a register are extended. 5274 if (Size < 64 && Ty->isIntegerType()) 5275 return ABIArgInfo::getExtend(); 5276 5277 // Other non-aggregates go in registers. 5278 if (!isAggregateTypeForABI(Ty)) 5279 return ABIArgInfo::getDirect(); 5280 5281 // This is a small aggregate type that should be passed in registers. 5282 // Build a coercion type from the LLVM struct type. 5283 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); 5284 if (!StrTy) 5285 return ABIArgInfo::getDirect(); 5286 5287 CoerceBuilder CB(getVMContext(), getDataLayout()); 5288 CB.addStruct(0, StrTy); 5289 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); 5290 5291 // Try to use the original type for coercion. 5292 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); 5293 5294 if (CB.InReg) 5295 return ABIArgInfo::getDirectInReg(CoerceTy); 5296 else 5297 return ABIArgInfo::getDirect(CoerceTy); 5298 } 5299 5300 llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 5301 CodeGenFunction &CGF) const { 5302 ABIArgInfo AI = classifyType(Ty, 16 * 8); 5303 llvm::Type *ArgTy = CGT.ConvertType(Ty); 5304 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 5305 AI.setCoerceToType(ArgTy); 5306 5307 llvm::Type *BPP = CGF.Int8PtrPtrTy; 5308 CGBuilderTy &Builder = CGF.Builder; 5309 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 5310 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 5311 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 5312 llvm::Value *ArgAddr; 5313 unsigned Stride; 5314 5315 switch (AI.getKind()) { 5316 case ABIArgInfo::Expand: 5317 llvm_unreachable("Unsupported ABI kind for va_arg"); 5318 5319 case ABIArgInfo::Extend: 5320 Stride = 8; 5321 ArgAddr = Builder 5322 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), 5323 "extend"); 5324 break; 5325 5326 case ABIArgInfo::Direct: 5327 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); 5328 ArgAddr = Addr; 5329 break; 5330 5331 case ABIArgInfo::Indirect: 5332 Stride = 8; 5333 ArgAddr = Builder.CreateBitCast(Addr, 5334 llvm::PointerType::getUnqual(ArgPtrTy), 5335 "indirect"); 5336 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); 5337 break; 5338 5339 case ABIArgInfo::Ignore: 5340 return llvm::UndefValue::get(ArgPtrTy); 5341 } 5342 5343 // Update VAList. 5344 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); 5345 Builder.CreateStore(Addr, VAListAddrAsBPP); 5346 5347 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); 5348 } 5349 5350 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { 5351 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); 5352 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 5353 it != ie; ++it) 5354 it->info = classifyType(it->type, 16 * 8); 5355 } 5356 5357 namespace { 5358 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { 5359 public: 5360 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) 5361 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} 5362 }; 5363 } // end anonymous namespace 5364 5365 5366 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { 5367 if (TheTargetCodeGenInfo) 5368 return *TheTargetCodeGenInfo; 5369 5370 const llvm::Triple &Triple = getTarget().getTriple(); 5371 switch (Triple.getArch()) { 5372 default: 5373 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); 5374 5375 case llvm::Triple::le32: 5376 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); 5377 case llvm::Triple::mips: 5378 case llvm::Triple::mipsel: 5379 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); 5380 5381 case llvm::Triple::mips64: 5382 case llvm::Triple::mips64el: 5383 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); 5384 5385 case llvm::Triple::aarch64: 5386 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types)); 5387 5388 case llvm::Triple::arm: 5389 case llvm::Triple::thumb: 5390 { 5391 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; 5392 if (strcmp(getTarget().getABI(), "apcs-gnu") == 0) 5393 Kind = ARMABIInfo::APCS; 5394 else if (CodeGenOpts.FloatABI == "hard" || 5395 (CodeGenOpts.FloatABI != "soft" && 5396 Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) 5397 Kind = ARMABIInfo::AAPCS_VFP; 5398 5399 switch (Triple.getOS()) { 5400 case llvm::Triple::NaCl: 5401 return *(TheTargetCodeGenInfo = 5402 new NaClARMTargetCodeGenInfo(Types, Kind)); 5403 default: 5404 return *(TheTargetCodeGenInfo = 5405 new ARMTargetCodeGenInfo(Types, Kind)); 5406 } 5407 } 5408 5409 case llvm::Triple::ppc: 5410 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); 5411 case llvm::Triple::ppc64: 5412 if (Triple.isOSBinFormatELF()) 5413 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); 5414 else 5415 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); 5416 case llvm::Triple::ppc64le: 5417 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); 5418 return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); 5419 5420 case llvm::Triple::nvptx: 5421 case llvm::Triple::nvptx64: 5422 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); 5423 5424 case llvm::Triple::msp430: 5425 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); 5426 5427 case llvm::Triple::systemz: 5428 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); 5429 5430 case llvm::Triple::tce: 5431 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); 5432 5433 case llvm::Triple::x86: { 5434 bool IsDarwinVectorABI = Triple.isOSDarwin(); 5435 bool IsSmallStructInRegABI = 5436 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); 5437 bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32); 5438 5439 if (Triple.getOS() == llvm::Triple::Win32) { 5440 return *(TheTargetCodeGenInfo = 5441 new WinX86_32TargetCodeGenInfo(Types, 5442 IsDarwinVectorABI, IsSmallStructInRegABI, 5443 IsWin32FloatStructABI, 5444 CodeGenOpts.NumRegisterParameters)); 5445 } else { 5446 return *(TheTargetCodeGenInfo = 5447 new X86_32TargetCodeGenInfo(Types, 5448 IsDarwinVectorABI, IsSmallStructInRegABI, 5449 IsWin32FloatStructABI, 5450 CodeGenOpts.NumRegisterParameters)); 5451 } 5452 } 5453 5454 case llvm::Triple::x86_64: { 5455 bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0; 5456 5457 switch (Triple.getOS()) { 5458 case llvm::Triple::Win32: 5459 case llvm::Triple::MinGW32: 5460 case llvm::Triple::Cygwin: 5461 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); 5462 case llvm::Triple::NaCl: 5463 return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, 5464 HasAVX)); 5465 default: 5466 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, 5467 HasAVX)); 5468 } 5469 } 5470 case llvm::Triple::hexagon: 5471 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); 5472 case llvm::Triple::sparcv9: 5473 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); 5474 } 5475 } 5476