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 "CodeGenFunction.h" 18 #include "clang/AST/RecordLayout.h" 19 #include "clang/Frontend/CodeGenOptions.h" 20 #include "llvm/Type.h" 21 #include "llvm/Target/TargetData.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/Support/raw_ostream.h" 24 using namespace clang; 25 using namespace CodeGen; 26 27 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, 28 llvm::Value *Array, 29 llvm::Value *Value, 30 unsigned FirstIndex, 31 unsigned LastIndex) { 32 // Alternatively, we could emit this as a loop in the source. 33 for (unsigned I = FirstIndex; I <= LastIndex; ++I) { 34 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); 35 Builder.CreateStore(Value, Cell); 36 } 37 } 38 39 static bool isAggregateTypeForABI(QualType T) { 40 return CodeGenFunction::hasAggregateLLVMType(T) || 41 T->isMemberFunctionPointerType(); 42 } 43 44 ABIInfo::~ABIInfo() {} 45 46 ASTContext &ABIInfo::getContext() const { 47 return CGT.getContext(); 48 } 49 50 llvm::LLVMContext &ABIInfo::getVMContext() const { 51 return CGT.getLLVMContext(); 52 } 53 54 const llvm::TargetData &ABIInfo::getTargetData() const { 55 return CGT.getTargetData(); 56 } 57 58 59 void ABIArgInfo::dump() const { 60 raw_ostream &OS = llvm::errs(); 61 OS << "(ABIArgInfo Kind="; 62 switch (TheKind) { 63 case Direct: 64 OS << "Direct Type="; 65 if (llvm::Type *Ty = getCoerceToType()) 66 Ty->print(OS); 67 else 68 OS << "null"; 69 break; 70 case Extend: 71 OS << "Extend"; 72 break; 73 case Ignore: 74 OS << "Ignore"; 75 break; 76 case Indirect: 77 OS << "Indirect Align=" << getIndirectAlign() 78 << " ByVal=" << getIndirectByVal() 79 << " Realign=" << getIndirectRealign(); 80 break; 81 case Expand: 82 OS << "Expand"; 83 break; 84 } 85 OS << ")\n"; 86 } 87 88 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } 89 90 // If someone can figure out a general rule for this, that would be great. 91 // It's probably just doomed to be platform-dependent, though. 92 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { 93 // Verified for: 94 // x86-64 FreeBSD, Linux, Darwin 95 // x86-32 FreeBSD, Linux, Darwin 96 // PowerPC Linux, Darwin 97 // ARM Darwin (*not* EABI) 98 return 32; 99 } 100 101 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, 102 const FunctionNoProtoType *fnType) const { 103 // The following conventions are known to require this to be false: 104 // x86_stdcall 105 // MIPS 106 // For everything else, we just prefer false unless we opt out. 107 return false; 108 } 109 110 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); 111 112 /// isEmptyField - Return true iff a the field is "empty", that is it 113 /// is an unnamed bit-field or an (array of) empty record(s). 114 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, 115 bool AllowArrays) { 116 if (FD->isUnnamedBitfield()) 117 return true; 118 119 QualType FT = FD->getType(); 120 121 // Constant arrays of empty records count as empty, strip them off. 122 // Constant arrays of zero length always count as empty. 123 if (AllowArrays) 124 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 125 if (AT->getSize() == 0) 126 return true; 127 FT = AT->getElementType(); 128 } 129 130 const RecordType *RT = FT->getAs<RecordType>(); 131 if (!RT) 132 return false; 133 134 // C++ record fields are never empty, at least in the Itanium ABI. 135 // 136 // FIXME: We should use a predicate for whether this behavior is true in the 137 // current ABI. 138 if (isa<CXXRecordDecl>(RT->getDecl())) 139 return false; 140 141 return isEmptyRecord(Context, FT, AllowArrays); 142 } 143 144 /// isEmptyRecord - Return true iff a structure contains only empty 145 /// fields. Note that a structure with a flexible array member is not 146 /// considered empty. 147 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { 148 const RecordType *RT = T->getAs<RecordType>(); 149 if (!RT) 150 return 0; 151 const RecordDecl *RD = RT->getDecl(); 152 if (RD->hasFlexibleArrayMember()) 153 return false; 154 155 // If this is a C++ record, check the bases first. 156 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 157 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 158 e = CXXRD->bases_end(); i != e; ++i) 159 if (!isEmptyRecord(Context, i->getType(), true)) 160 return false; 161 162 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 163 i != e; ++i) 164 if (!isEmptyField(Context, *i, AllowArrays)) 165 return false; 166 return true; 167 } 168 169 /// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either 170 /// a non-trivial destructor or a non-trivial copy constructor. 171 static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) { 172 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 173 if (!RD) 174 return false; 175 176 return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor(); 177 } 178 179 /// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is 180 /// a record type with either a non-trivial destructor or a non-trivial copy 181 /// constructor. 182 static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) { 183 const RecordType *RT = T->getAs<RecordType>(); 184 if (!RT) 185 return false; 186 187 return hasNonTrivialDestructorOrCopyConstructor(RT); 188 } 189 190 /// isSingleElementStruct - Determine if a structure is a "single 191 /// element struct", i.e. it has exactly one non-empty field or 192 /// exactly one field which is itself a single element 193 /// struct. Structures with flexible array members are never 194 /// considered single element structs. 195 /// 196 /// \return The field declaration for the single non-empty field, if 197 /// it exists. 198 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { 199 const RecordType *RT = T->getAsStructureType(); 200 if (!RT) 201 return 0; 202 203 const RecordDecl *RD = RT->getDecl(); 204 if (RD->hasFlexibleArrayMember()) 205 return 0; 206 207 const Type *Found = 0; 208 209 // If this is a C++ record, check the bases first. 210 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 211 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 212 e = CXXRD->bases_end(); i != e; ++i) { 213 // Ignore empty records. 214 if (isEmptyRecord(Context, i->getType(), true)) 215 continue; 216 217 // If we already found an element then this isn't a single-element struct. 218 if (Found) 219 return 0; 220 221 // If this is non-empty and not a single element struct, the composite 222 // cannot be a single element struct. 223 Found = isSingleElementStruct(i->getType(), Context); 224 if (!Found) 225 return 0; 226 } 227 } 228 229 // Check for single element. 230 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 231 i != e; ++i) { 232 const FieldDecl *FD = *i; 233 QualType FT = FD->getType(); 234 235 // Ignore empty fields. 236 if (isEmptyField(Context, FD, true)) 237 continue; 238 239 // If we already found an element then this isn't a single-element 240 // struct. 241 if (Found) 242 return 0; 243 244 // Treat single element arrays as the element. 245 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 246 if (AT->getSize().getZExtValue() != 1) 247 break; 248 FT = AT->getElementType(); 249 } 250 251 if (!isAggregateTypeForABI(FT)) { 252 Found = FT.getTypePtr(); 253 } else { 254 Found = isSingleElementStruct(FT, Context); 255 if (!Found) 256 return 0; 257 } 258 } 259 260 // We don't consider a struct a single-element struct if it has 261 // padding beyond the element type. 262 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) 263 return 0; 264 265 return Found; 266 } 267 268 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { 269 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && 270 !Ty->isAnyComplexType() && !Ty->isEnumeralType() && 271 !Ty->isBlockPointerType()) 272 return false; 273 274 uint64_t Size = Context.getTypeSize(Ty); 275 return Size == 32 || Size == 64; 276 } 277 278 /// canExpandIndirectArgument - Test whether an argument type which is to be 279 /// passed indirectly (on the stack) would have the equivalent layout if it was 280 /// expanded into separate arguments. If so, we prefer to do the latter to avoid 281 /// inhibiting optimizations. 282 /// 283 // FIXME: This predicate is missing many cases, currently it just follows 284 // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We 285 // should probably make this smarter, or better yet make the LLVM backend 286 // capable of handling it. 287 static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { 288 // We can only expand structure types. 289 const RecordType *RT = Ty->getAs<RecordType>(); 290 if (!RT) 291 return false; 292 293 // We can only expand (C) structures. 294 // 295 // FIXME: This needs to be generalized to handle classes as well. 296 const RecordDecl *RD = RT->getDecl(); 297 if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) 298 return false; 299 300 uint64_t Size = 0; 301 302 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 303 i != e; ++i) { 304 const FieldDecl *FD = *i; 305 306 if (!is32Or64BitBasicType(FD->getType(), Context)) 307 return false; 308 309 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know 310 // how to expand them yet, and the predicate for telling if a bitfield still 311 // counts as "basic" is more complicated than what we were doing previously. 312 if (FD->isBitField()) 313 return false; 314 315 Size += Context.getTypeSize(FD->getType()); 316 } 317 318 // Make sure there are not any holes in the struct. 319 if (Size != Context.getTypeSize(Ty)) 320 return false; 321 322 return true; 323 } 324 325 namespace { 326 /// DefaultABIInfo - The default implementation for ABI specific 327 /// details. This implementation provides information which results in 328 /// self-consistent and sensible LLVM IR generation, but does not 329 /// conform to any particular ABI. 330 class DefaultABIInfo : public ABIInfo { 331 public: 332 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 333 334 ABIArgInfo classifyReturnType(QualType RetTy) const; 335 ABIArgInfo classifyArgumentType(QualType RetTy) const; 336 337 virtual void computeInfo(CGFunctionInfo &FI) const { 338 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 339 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 340 it != ie; ++it) 341 it->info = classifyArgumentType(it->type); 342 } 343 344 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 345 CodeGenFunction &CGF) const; 346 }; 347 348 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { 349 public: 350 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 351 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 352 }; 353 354 llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 355 CodeGenFunction &CGF) const { 356 return 0; 357 } 358 359 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { 360 if (isAggregateTypeForABI(Ty)) { 361 // Records with non trivial destructors/constructors should not be passed 362 // by value. 363 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) 364 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 365 366 return ABIArgInfo::getIndirect(0); 367 } 368 369 // Treat an enum type as its underlying type. 370 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 371 Ty = EnumTy->getDecl()->getIntegerType(); 372 373 return (Ty->isPromotableIntegerType() ? 374 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 375 } 376 377 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { 378 if (RetTy->isVoidType()) 379 return ABIArgInfo::getIgnore(); 380 381 if (isAggregateTypeForABI(RetTy)) 382 return ABIArgInfo::getIndirect(0); 383 384 // Treat an enum type as its underlying type. 385 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 386 RetTy = EnumTy->getDecl()->getIntegerType(); 387 388 return (RetTy->isPromotableIntegerType() ? 389 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 390 } 391 392 /// UseX86_MMXType - Return true if this is an MMX type that should use the 393 /// special x86_mmx type. 394 bool UseX86_MMXType(llvm::Type *IRType) { 395 // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the 396 // special x86_mmx type. 397 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && 398 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && 399 IRType->getScalarSizeInBits() != 64; 400 } 401 402 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 403 StringRef Constraint, 404 llvm::Type* Ty) { 405 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) 406 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); 407 return Ty; 408 } 409 410 //===----------------------------------------------------------------------===// 411 // X86-32 ABI Implementation 412 //===----------------------------------------------------------------------===// 413 414 /// X86_32ABIInfo - The X86-32 ABI information. 415 class X86_32ABIInfo : public ABIInfo { 416 enum Class { 417 Integer, 418 Float 419 }; 420 421 static const unsigned MinABIStackAlignInBytes = 4; 422 423 bool IsDarwinVectorABI; 424 bool IsSmallStructInRegABI; 425 bool IsMMXDisabled; 426 bool IsWin32FloatStructABI; 427 unsigned DefaultNumRegisterParameters; 428 429 static bool isRegisterSize(unsigned Size) { 430 return (Size == 8 || Size == 16 || Size == 32 || Size == 64); 431 } 432 433 static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, 434 unsigned callingConvention); 435 436 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 437 /// such that the argument will be passed in memory. 438 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const; 439 440 /// \brief Return the alignment to use for the given type on the stack. 441 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; 442 443 Class classify(QualType Ty) const; 444 ABIArgInfo classifyReturnType(QualType RetTy, 445 unsigned callingConvention) const; 446 ABIArgInfo classifyArgumentTypeWithReg(QualType RetTy, 447 unsigned &FreeRegs) const; 448 ABIArgInfo classifyArgumentType(QualType RetTy) const; 449 450 public: 451 452 virtual void computeInfo(CGFunctionInfo &FI) const; 453 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 454 CodeGenFunction &CGF) const; 455 456 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w, 457 unsigned r) 458 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), 459 IsMMXDisabled(m), IsWin32FloatStructABI(w), 460 DefaultNumRegisterParameters(r) {} 461 }; 462 463 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { 464 public: 465 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 466 bool d, bool p, bool m, bool w, unsigned r) 467 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {} 468 469 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 470 CodeGen::CodeGenModule &CGM) const; 471 472 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 473 // Darwin uses different dwarf register numbers for EH. 474 if (CGM.isTargetDarwin()) return 5; 475 476 return 4; 477 } 478 479 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 480 llvm::Value *Address) const; 481 482 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 483 StringRef Constraint, 484 llvm::Type* Ty) const { 485 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 486 } 487 488 }; 489 490 } 491 492 /// shouldReturnTypeInRegister - Determine if the given type should be 493 /// passed in a register (for the Darwin ABI). 494 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, 495 ASTContext &Context, 496 unsigned callingConvention) { 497 uint64_t Size = Context.getTypeSize(Ty); 498 499 // Type must be register sized. 500 if (!isRegisterSize(Size)) 501 return false; 502 503 if (Ty->isVectorType()) { 504 // 64- and 128- bit vectors inside structures are not returned in 505 // registers. 506 if (Size == 64 || Size == 128) 507 return false; 508 509 return true; 510 } 511 512 // If this is a builtin, pointer, enum, complex type, member pointer, or 513 // member function pointer it is ok. 514 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || 515 Ty->isAnyComplexType() || Ty->isEnumeralType() || 516 Ty->isBlockPointerType() || Ty->isMemberPointerType()) 517 return true; 518 519 // Arrays are treated like records. 520 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) 521 return shouldReturnTypeInRegister(AT->getElementType(), Context, 522 callingConvention); 523 524 // Otherwise, it must be a record type. 525 const RecordType *RT = Ty->getAs<RecordType>(); 526 if (!RT) return false; 527 528 // FIXME: Traverse bases here too. 529 530 // For thiscall conventions, structures will never be returned in 531 // a register. This is for compatibility with the MSVC ABI 532 if (callingConvention == llvm::CallingConv::X86_ThisCall && 533 RT->isStructureType()) { 534 return false; 535 } 536 537 // Structure types are passed in register if all fields would be 538 // passed in a register. 539 for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), 540 e = RT->getDecl()->field_end(); i != e; ++i) { 541 const FieldDecl *FD = *i; 542 543 // Empty fields are ignored. 544 if (isEmptyField(Context, FD, true)) 545 continue; 546 547 // Check fields recursively. 548 if (!shouldReturnTypeInRegister(FD->getType(), Context, 549 callingConvention)) 550 return false; 551 } 552 return true; 553 } 554 555 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, 556 unsigned callingConvention) const { 557 if (RetTy->isVoidType()) 558 return ABIArgInfo::getIgnore(); 559 560 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 561 // On Darwin, some vectors are returned in registers. 562 if (IsDarwinVectorABI) { 563 uint64_t Size = getContext().getTypeSize(RetTy); 564 565 // 128-bit vectors are a special case; they are returned in 566 // registers and we need to make sure to pick a type the LLVM 567 // backend will like. 568 if (Size == 128) 569 return ABIArgInfo::getDirect(llvm::VectorType::get( 570 llvm::Type::getInt64Ty(getVMContext()), 2)); 571 572 // Always return in register if it fits in a general purpose 573 // register, or if it is 64 bits and has a single element. 574 if ((Size == 8 || Size == 16 || Size == 32) || 575 (Size == 64 && VT->getNumElements() == 1)) 576 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 577 Size)); 578 579 return ABIArgInfo::getIndirect(0); 580 } 581 582 return ABIArgInfo::getDirect(); 583 } 584 585 if (isAggregateTypeForABI(RetTy)) { 586 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 587 // Structures with either a non-trivial destructor or a non-trivial 588 // copy constructor are always indirect. 589 if (hasNonTrivialDestructorOrCopyConstructor(RT)) 590 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 591 592 // Structures with flexible arrays are always indirect. 593 if (RT->getDecl()->hasFlexibleArrayMember()) 594 return ABIArgInfo::getIndirect(0); 595 } 596 597 // If specified, structs and unions are always indirect. 598 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) 599 return ABIArgInfo::getIndirect(0); 600 601 // Small structures which are register sized are generally returned 602 // in a register. 603 if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(), 604 callingConvention)) { 605 uint64_t Size = getContext().getTypeSize(RetTy); 606 607 // As a special-case, if the struct is a "single-element" struct, and 608 // the field is of type "float" or "double", return it in a 609 // floating-point register. (MSVC does not apply this special case.) 610 // We apply a similar transformation for pointer types to improve the 611 // quality of the generated IR. 612 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 613 if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType()) 614 || SeltTy->hasPointerRepresentation()) 615 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 616 617 // FIXME: We should be able to narrow this integer in cases with dead 618 // padding. 619 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); 620 } 621 622 return ABIArgInfo::getIndirect(0); 623 } 624 625 // Treat an enum type as its underlying type. 626 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 627 RetTy = EnumTy->getDecl()->getIntegerType(); 628 629 return (RetTy->isPromotableIntegerType() ? 630 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 631 } 632 633 static bool isSSEVectorType(ASTContext &Context, QualType Ty) { 634 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; 635 } 636 637 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { 638 const RecordType *RT = Ty->getAs<RecordType>(); 639 if (!RT) 640 return 0; 641 const RecordDecl *RD = RT->getDecl(); 642 643 // If this is a C++ record, check the bases first. 644 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 645 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 646 e = CXXRD->bases_end(); i != e; ++i) 647 if (!isRecordWithSSEVectorType(Context, i->getType())) 648 return false; 649 650 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 651 i != e; ++i) { 652 QualType FT = i->getType(); 653 654 if (isSSEVectorType(Context, FT)) 655 return true; 656 657 if (isRecordWithSSEVectorType(Context, FT)) 658 return true; 659 } 660 661 return false; 662 } 663 664 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, 665 unsigned Align) const { 666 // Otherwise, if the alignment is less than or equal to the minimum ABI 667 // alignment, just use the default; the backend will handle this. 668 if (Align <= MinABIStackAlignInBytes) 669 return 0; // Use default alignment. 670 671 // On non-Darwin, the stack type alignment is always 4. 672 if (!IsDarwinVectorABI) { 673 // Set explicit alignment, since we may need to realign the top. 674 return MinABIStackAlignInBytes; 675 } 676 677 // Otherwise, if the type contains an SSE vector type, the alignment is 16. 678 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || 679 isRecordWithSSEVectorType(getContext(), Ty))) 680 return 16; 681 682 return MinABIStackAlignInBytes; 683 } 684 685 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const { 686 if (!ByVal) 687 return ABIArgInfo::getIndirect(0, false); 688 689 // Compute the byval alignment. 690 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 691 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); 692 if (StackAlign == 0) 693 return ABIArgInfo::getIndirect(4); 694 695 // If the stack alignment is less than the type alignment, realign the 696 // argument. 697 if (StackAlign < TypeAlign) 698 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, 699 /*Realign=*/true); 700 701 return ABIArgInfo::getIndirect(StackAlign); 702 } 703 704 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { 705 const Type *T = isSingleElementStruct(Ty, getContext()); 706 if (!T) 707 T = Ty.getTypePtr(); 708 709 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 710 BuiltinType::Kind K = BT->getKind(); 711 if (K == BuiltinType::Float || K == BuiltinType::Double) 712 return Float; 713 } 714 return Integer; 715 } 716 717 ABIArgInfo 718 X86_32ABIInfo::classifyArgumentTypeWithReg(QualType Ty, 719 unsigned &FreeRegs) const { 720 // Common case first. 721 if (FreeRegs == 0) 722 return classifyArgumentType(Ty); 723 724 Class C = classify(Ty); 725 if (C == Float) 726 return classifyArgumentType(Ty); 727 728 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; 729 if (SizeInRegs == 0) 730 return classifyArgumentType(Ty); 731 732 if (SizeInRegs > FreeRegs) { 733 FreeRegs = 0; 734 return classifyArgumentType(Ty); 735 } 736 assert(SizeInRegs >= 1 && SizeInRegs <= 3); 737 FreeRegs -= SizeInRegs; 738 739 // If it is a simple scalar, keep the type so that we produce a cleaner IR. 740 ABIArgInfo Foo = classifyArgumentType(Ty); 741 if (Foo.isDirect() && !Foo.getDirectOffset() && !Foo.getPaddingType()) 742 return ABIArgInfo::getDirectInReg(Foo.getCoerceToType()); 743 if (Foo.isExtend()) 744 return ABIArgInfo::getExtendInReg(Foo.getCoerceToType()); 745 746 llvm::LLVMContext &LLVMContext = getVMContext(); 747 llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext); 748 SmallVector<llvm::Type*, 3> Elements; 749 for (unsigned I = 0; I < SizeInRegs; ++I) 750 Elements.push_back(Int32); 751 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 752 return ABIArgInfo::getDirectInReg(Result); 753 } 754 755 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const { 756 // FIXME: Set alignment on indirect arguments. 757 if (isAggregateTypeForABI(Ty)) { 758 // Structures with flexible arrays are always indirect. 759 if (const RecordType *RT = Ty->getAs<RecordType>()) { 760 // Structures with either a non-trivial destructor or a non-trivial 761 // copy constructor are always indirect. 762 if (hasNonTrivialDestructorOrCopyConstructor(RT)) 763 return getIndirectResult(Ty, /*ByVal=*/false); 764 765 if (RT->getDecl()->hasFlexibleArrayMember()) 766 return getIndirectResult(Ty); 767 } 768 769 // Ignore empty structs/unions. 770 if (isEmptyRecord(getContext(), Ty, true)) 771 return ABIArgInfo::getIgnore(); 772 773 // Expand small (<= 128-bit) record types when we know that the stack layout 774 // of those arguments will match the struct. This is important because the 775 // LLVM backend isn't smart enough to remove byval, which inhibits many 776 // optimizations. 777 if (getContext().getTypeSize(Ty) <= 4*32 && 778 canExpandIndirectArgument(Ty, getContext())) 779 return ABIArgInfo::getExpand(); 780 781 return getIndirectResult(Ty); 782 } 783 784 if (const VectorType *VT = Ty->getAs<VectorType>()) { 785 // On Darwin, some vectors are passed in memory, we handle this by passing 786 // it as an i8/i16/i32/i64. 787 if (IsDarwinVectorABI) { 788 uint64_t Size = getContext().getTypeSize(Ty); 789 if ((Size == 8 || Size == 16 || Size == 32) || 790 (Size == 64 && VT->getNumElements() == 1)) 791 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 792 Size)); 793 } 794 795 llvm::Type *IRType = CGT.ConvertType(Ty); 796 if (UseX86_MMXType(IRType)) { 797 if (IsMMXDisabled) 798 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 799 64)); 800 ABIArgInfo AAI = ABIArgInfo::getDirect(IRType); 801 AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext())); 802 return AAI; 803 } 804 805 return ABIArgInfo::getDirect(); 806 } 807 808 809 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 810 Ty = EnumTy->getDecl()->getIntegerType(); 811 812 return (Ty->isPromotableIntegerType() ? 813 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 814 } 815 816 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { 817 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), 818 FI.getCallingConvention()); 819 820 unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 821 DefaultNumRegisterParameters; 822 823 // If the return value is indirect, then the hidden argument is consuming one 824 // integer register. 825 if (FI.getReturnInfo().isIndirect() && FreeRegs) { 826 --FreeRegs; 827 ABIArgInfo &Old = FI.getReturnInfo(); 828 Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(), 829 Old.getIndirectByVal(), 830 Old.getIndirectRealign()); 831 } 832 833 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 834 it != ie; ++it) 835 it->info = classifyArgumentTypeWithReg(it->type, FreeRegs); 836 } 837 838 llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 839 CodeGenFunction &CGF) const { 840 llvm::Type *BPP = CGF.Int8PtrPtrTy; 841 842 CGBuilderTy &Builder = CGF.Builder; 843 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 844 "ap"); 845 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 846 847 // Compute if the address needs to be aligned 848 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); 849 Align = getTypeStackAlignInBytes(Ty, Align); 850 Align = std::max(Align, 4U); 851 if (Align > 4) { 852 // addr = (addr + align - 1) & -align; 853 llvm::Value *Offset = 854 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); 855 Addr = CGF.Builder.CreateGEP(Addr, Offset); 856 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, 857 CGF.Int32Ty); 858 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); 859 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), 860 Addr->getType(), 861 "ap.cur.aligned"); 862 } 863 864 llvm::Type *PTy = 865 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 866 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 867 868 uint64_t Offset = 869 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); 870 llvm::Value *NextAddr = 871 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 872 "ap.next"); 873 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 874 875 return AddrTyped; 876 } 877 878 void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, 879 llvm::GlobalValue *GV, 880 CodeGen::CodeGenModule &CGM) const { 881 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 882 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 883 // Get the LLVM function. 884 llvm::Function *Fn = cast<llvm::Function>(GV); 885 886 // Now add the 'alignstack' attribute with a value of 16. 887 Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16)); 888 } 889 } 890 } 891 892 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( 893 CodeGen::CodeGenFunction &CGF, 894 llvm::Value *Address) const { 895 CodeGen::CGBuilderTy &Builder = CGF.Builder; 896 897 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 898 899 // 0-7 are the eight integer registers; the order is different 900 // on Darwin (for EH), but the range is the same. 901 // 8 is %eip. 902 AssignToArrayRange(Builder, Address, Four8, 0, 8); 903 904 if (CGF.CGM.isTargetDarwin()) { 905 // 12-16 are st(0..4). Not sure why we stop at 4. 906 // These have size 16, which is sizeof(long double) on 907 // platforms with 8-byte alignment for that type. 908 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); 909 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); 910 911 } else { 912 // 9 is %eflags, which doesn't get a size on Darwin for some 913 // reason. 914 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); 915 916 // 11-16 are st(0..5). Not sure why we stop at 5. 917 // These have size 12, which is sizeof(long double) on 918 // platforms with 4-byte alignment for that type. 919 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); 920 AssignToArrayRange(Builder, Address, Twelve8, 11, 16); 921 } 922 923 return false; 924 } 925 926 //===----------------------------------------------------------------------===// 927 // X86-64 ABI Implementation 928 //===----------------------------------------------------------------------===// 929 930 931 namespace { 932 /// X86_64ABIInfo - The X86_64 ABI information. 933 class X86_64ABIInfo : public ABIInfo { 934 enum Class { 935 Integer = 0, 936 SSE, 937 SSEUp, 938 X87, 939 X87Up, 940 ComplexX87, 941 NoClass, 942 Memory 943 }; 944 945 /// merge - Implement the X86_64 ABI merging algorithm. 946 /// 947 /// Merge an accumulating classification \arg Accum with a field 948 /// classification \arg Field. 949 /// 950 /// \param Accum - The accumulating classification. This should 951 /// always be either NoClass or the result of a previous merge 952 /// call. In addition, this should never be Memory (the caller 953 /// should just return Memory for the aggregate). 954 static Class merge(Class Accum, Class Field); 955 956 /// postMerge - Implement the X86_64 ABI post merging algorithm. 957 /// 958 /// Post merger cleanup, reduces a malformed Hi and Lo pair to 959 /// final MEMORY or SSE classes when necessary. 960 /// 961 /// \param AggregateSize - The size of the current aggregate in 962 /// the classification process. 963 /// 964 /// \param Lo - The classification for the parts of the type 965 /// residing in the low word of the containing object. 966 /// 967 /// \param Hi - The classification for the parts of the type 968 /// residing in the higher words of the containing object. 969 /// 970 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; 971 972 /// classify - Determine the x86_64 register classes in which the 973 /// given type T should be passed. 974 /// 975 /// \param Lo - The classification for the parts of the type 976 /// residing in the low word of the containing object. 977 /// 978 /// \param Hi - The classification for the parts of the type 979 /// residing in the high word of the containing object. 980 /// 981 /// \param OffsetBase - The bit offset of this type in the 982 /// containing object. Some parameters are classified different 983 /// depending on whether they straddle an eightbyte boundary. 984 /// 985 /// If a word is unused its result will be NoClass; if a type should 986 /// be passed in Memory then at least the classification of \arg Lo 987 /// will be Memory. 988 /// 989 /// The \arg Lo class will be NoClass iff the argument is ignored. 990 /// 991 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will 992 /// also be ComplexX87. 993 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const; 994 995 llvm::Type *GetByteVectorType(QualType Ty) const; 996 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, 997 unsigned IROffset, QualType SourceTy, 998 unsigned SourceOffset) const; 999 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, 1000 unsigned IROffset, QualType SourceTy, 1001 unsigned SourceOffset) const; 1002 1003 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1004 /// such that the argument will be returned in memory. 1005 ABIArgInfo getIndirectReturnResult(QualType Ty) const; 1006 1007 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 1008 /// such that the argument will be passed in memory. 1009 /// 1010 /// \param freeIntRegs - The number of free integer registers remaining 1011 /// available. 1012 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; 1013 1014 ABIArgInfo classifyReturnType(QualType RetTy) const; 1015 1016 ABIArgInfo classifyArgumentType(QualType Ty, 1017 unsigned freeIntRegs, 1018 unsigned &neededInt, 1019 unsigned &neededSSE) const; 1020 1021 bool IsIllegalVectorType(QualType Ty) const; 1022 1023 /// The 0.98 ABI revision clarified a lot of ambiguities, 1024 /// unfortunately in ways that were not always consistent with 1025 /// certain previous compilers. In particular, platforms which 1026 /// required strict binary compatibility with older versions of GCC 1027 /// may need to exempt themselves. 1028 bool honorsRevision0_98() const { 1029 return !getContext().getTargetInfo().getTriple().isOSDarwin(); 1030 } 1031 1032 bool HasAVX; 1033 1034 public: 1035 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : 1036 ABIInfo(CGT), HasAVX(hasavx) {} 1037 1038 bool isPassedUsingAVXType(QualType type) const { 1039 unsigned neededInt, neededSSE; 1040 // The freeIntRegs argument doesn't matter here. 1041 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE); 1042 if (info.isDirect()) { 1043 llvm::Type *ty = info.getCoerceToType(); 1044 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) 1045 return (vectorTy->getBitWidth() > 128); 1046 } 1047 return false; 1048 } 1049 1050 virtual void computeInfo(CGFunctionInfo &FI) const; 1051 1052 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 1053 CodeGenFunction &CGF) const; 1054 }; 1055 1056 /// WinX86_64ABIInfo - The Windows X86_64 ABI information. 1057 class WinX86_64ABIInfo : public ABIInfo { 1058 1059 ABIArgInfo classify(QualType Ty) const; 1060 1061 public: 1062 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 1063 1064 virtual void computeInfo(CGFunctionInfo &FI) const; 1065 1066 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 1067 CodeGenFunction &CGF) const; 1068 }; 1069 1070 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { 1071 public: 1072 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 1073 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} 1074 1075 const X86_64ABIInfo &getABIInfo() const { 1076 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); 1077 } 1078 1079 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 1080 return 7; 1081 } 1082 1083 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 1084 llvm::Value *Address) const { 1085 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 1086 1087 // 0-15 are the 16 integer registers. 1088 // 16 is %rip. 1089 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 1090 return false; 1091 } 1092 1093 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 1094 StringRef Constraint, 1095 llvm::Type* Ty) const { 1096 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 1097 } 1098 1099 bool isNoProtoCallVariadic(const CallArgList &args, 1100 const FunctionNoProtoType *fnType) const { 1101 // The default CC on x86-64 sets %al to the number of SSA 1102 // registers used, and GCC sets this when calling an unprototyped 1103 // function, so we override the default behavior. However, don't do 1104 // that when AVX types are involved: the ABI explicitly states it is 1105 // undefined, and it doesn't work in practice because of how the ABI 1106 // defines varargs anyway. 1107 if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) { 1108 bool HasAVXType = false; 1109 for (CallArgList::const_iterator 1110 it = args.begin(), ie = args.end(); it != ie; ++it) { 1111 if (getABIInfo().isPassedUsingAVXType(it->Ty)) { 1112 HasAVXType = true; 1113 break; 1114 } 1115 } 1116 1117 if (!HasAVXType) 1118 return true; 1119 } 1120 1121 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); 1122 } 1123 1124 }; 1125 1126 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 1127 public: 1128 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 1129 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} 1130 1131 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 1132 return 7; 1133 } 1134 1135 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 1136 llvm::Value *Address) const { 1137 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 1138 1139 // 0-15 are the 16 integer registers. 1140 // 16 is %rip. 1141 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 1142 return false; 1143 } 1144 }; 1145 1146 } 1147 1148 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, 1149 Class &Hi) const { 1150 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: 1151 // 1152 // (a) If one of the classes is Memory, the whole argument is passed in 1153 // memory. 1154 // 1155 // (b) If X87UP is not preceded by X87, the whole argument is passed in 1156 // memory. 1157 // 1158 // (c) If the size of the aggregate exceeds two eightbytes and the first 1159 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole 1160 // argument is passed in memory. NOTE: This is necessary to keep the 1161 // ABI working for processors that don't support the __m256 type. 1162 // 1163 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. 1164 // 1165 // Some of these are enforced by the merging logic. Others can arise 1166 // only with unions; for example: 1167 // union { _Complex double; unsigned; } 1168 // 1169 // Note that clauses (b) and (c) were added in 0.98. 1170 // 1171 if (Hi == Memory) 1172 Lo = Memory; 1173 if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) 1174 Lo = Memory; 1175 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) 1176 Lo = Memory; 1177 if (Hi == SSEUp && Lo != SSE) 1178 Hi = SSE; 1179 } 1180 1181 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { 1182 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is 1183 // classified recursively so that always two fields are 1184 // considered. The resulting class is calculated according to 1185 // the classes of the fields in the eightbyte: 1186 // 1187 // (a) If both classes are equal, this is the resulting class. 1188 // 1189 // (b) If one of the classes is NO_CLASS, the resulting class is 1190 // the other class. 1191 // 1192 // (c) If one of the classes is MEMORY, the result is the MEMORY 1193 // class. 1194 // 1195 // (d) If one of the classes is INTEGER, the result is the 1196 // INTEGER. 1197 // 1198 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, 1199 // MEMORY is used as class. 1200 // 1201 // (f) Otherwise class SSE is used. 1202 1203 // Accum should never be memory (we should have returned) or 1204 // ComplexX87 (because this cannot be passed in a structure). 1205 assert((Accum != Memory && Accum != ComplexX87) && 1206 "Invalid accumulated classification during merge."); 1207 if (Accum == Field || Field == NoClass) 1208 return Accum; 1209 if (Field == Memory) 1210 return Memory; 1211 if (Accum == NoClass) 1212 return Field; 1213 if (Accum == Integer || Field == Integer) 1214 return Integer; 1215 if (Field == X87 || Field == X87Up || Field == ComplexX87 || 1216 Accum == X87 || Accum == X87Up) 1217 return Memory; 1218 return SSE; 1219 } 1220 1221 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, 1222 Class &Lo, Class &Hi) const { 1223 // FIXME: This code can be simplified by introducing a simple value class for 1224 // Class pairs with appropriate constructor methods for the various 1225 // situations. 1226 1227 // FIXME: Some of the split computations are wrong; unaligned vectors 1228 // shouldn't be passed in registers for example, so there is no chance they 1229 // can straddle an eightbyte. Verify & simplify. 1230 1231 Lo = Hi = NoClass; 1232 1233 Class &Current = OffsetBase < 64 ? Lo : Hi; 1234 Current = Memory; 1235 1236 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 1237 BuiltinType::Kind k = BT->getKind(); 1238 1239 if (k == BuiltinType::Void) { 1240 Current = NoClass; 1241 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { 1242 Lo = Integer; 1243 Hi = Integer; 1244 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { 1245 Current = Integer; 1246 } else if (k == BuiltinType::Float || k == BuiltinType::Double) { 1247 Current = SSE; 1248 } else if (k == BuiltinType::LongDouble) { 1249 Lo = X87; 1250 Hi = X87Up; 1251 } 1252 // FIXME: _Decimal32 and _Decimal64 are SSE. 1253 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). 1254 return; 1255 } 1256 1257 if (const EnumType *ET = Ty->getAs<EnumType>()) { 1258 // Classify the underlying integer type. 1259 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi); 1260 return; 1261 } 1262 1263 if (Ty->hasPointerRepresentation()) { 1264 Current = Integer; 1265 return; 1266 } 1267 1268 if (Ty->isMemberPointerType()) { 1269 if (Ty->isMemberFunctionPointerType()) 1270 Lo = Hi = Integer; 1271 else 1272 Current = Integer; 1273 return; 1274 } 1275 1276 if (const VectorType *VT = Ty->getAs<VectorType>()) { 1277 uint64_t Size = getContext().getTypeSize(VT); 1278 if (Size == 32) { 1279 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x 1280 // float> as integer. 1281 Current = Integer; 1282 1283 // If this type crosses an eightbyte boundary, it should be 1284 // split. 1285 uint64_t EB_Real = (OffsetBase) / 64; 1286 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; 1287 if (EB_Real != EB_Imag) 1288 Hi = Lo; 1289 } else if (Size == 64) { 1290 // gcc passes <1 x double> in memory. :( 1291 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) 1292 return; 1293 1294 // gcc passes <1 x long long> as INTEGER. 1295 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || 1296 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || 1297 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || 1298 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) 1299 Current = Integer; 1300 else 1301 Current = SSE; 1302 1303 // If this type crosses an eightbyte boundary, it should be 1304 // split. 1305 if (OffsetBase && OffsetBase != 64) 1306 Hi = Lo; 1307 } else if (Size == 128 || (HasAVX && Size == 256)) { 1308 // Arguments of 256-bits are split into four eightbyte chunks. The 1309 // least significant one belongs to class SSE and all the others to class 1310 // SSEUP. The original Lo and Hi design considers that types can't be 1311 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. 1312 // This design isn't correct for 256-bits, but since there're no cases 1313 // where the upper parts would need to be inspected, avoid adding 1314 // complexity and just consider Hi to match the 64-256 part. 1315 Lo = SSE; 1316 Hi = SSEUp; 1317 } 1318 return; 1319 } 1320 1321 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 1322 QualType ET = getContext().getCanonicalType(CT->getElementType()); 1323 1324 uint64_t Size = getContext().getTypeSize(Ty); 1325 if (ET->isIntegralOrEnumerationType()) { 1326 if (Size <= 64) 1327 Current = Integer; 1328 else if (Size <= 128) 1329 Lo = Hi = Integer; 1330 } else if (ET == getContext().FloatTy) 1331 Current = SSE; 1332 else if (ET == getContext().DoubleTy) 1333 Lo = Hi = SSE; 1334 else if (ET == getContext().LongDoubleTy) 1335 Current = ComplexX87; 1336 1337 // If this complex type crosses an eightbyte boundary then it 1338 // should be split. 1339 uint64_t EB_Real = (OffsetBase) / 64; 1340 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; 1341 if (Hi == NoClass && EB_Real != EB_Imag) 1342 Hi = Lo; 1343 1344 return; 1345 } 1346 1347 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 1348 // Arrays are treated like structures. 1349 1350 uint64_t Size = getContext().getTypeSize(Ty); 1351 1352 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 1353 // than four eightbytes, ..., it has class MEMORY. 1354 if (Size > 256) 1355 return; 1356 1357 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned 1358 // fields, it has class MEMORY. 1359 // 1360 // Only need to check alignment of array base. 1361 if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) 1362 return; 1363 1364 // Otherwise implement simplified merge. We could be smarter about 1365 // this, but it isn't worth it and would be harder to verify. 1366 Current = NoClass; 1367 uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); 1368 uint64_t ArraySize = AT->getSize().getZExtValue(); 1369 1370 // The only case a 256-bit wide vector could be used is when the array 1371 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 1372 // to work for sizes wider than 128, early check and fallback to memory. 1373 if (Size > 128 && EltSize != 256) 1374 return; 1375 1376 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { 1377 Class FieldLo, FieldHi; 1378 classify(AT->getElementType(), Offset, FieldLo, FieldHi); 1379 Lo = merge(Lo, FieldLo); 1380 Hi = merge(Hi, FieldHi); 1381 if (Lo == Memory || Hi == Memory) 1382 break; 1383 } 1384 1385 postMerge(Size, Lo, Hi); 1386 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); 1387 return; 1388 } 1389 1390 if (const RecordType *RT = Ty->getAs<RecordType>()) { 1391 uint64_t Size = getContext().getTypeSize(Ty); 1392 1393 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 1394 // than four eightbytes, ..., it has class MEMORY. 1395 if (Size > 256) 1396 return; 1397 1398 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial 1399 // copy constructor or a non-trivial destructor, it is passed by invisible 1400 // reference. 1401 if (hasNonTrivialDestructorOrCopyConstructor(RT)) 1402 return; 1403 1404 const RecordDecl *RD = RT->getDecl(); 1405 1406 // Assume variable sized types are passed in memory. 1407 if (RD->hasFlexibleArrayMember()) 1408 return; 1409 1410 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 1411 1412 // Reset Lo class, this will be recomputed. 1413 Current = NoClass; 1414 1415 // If this is a C++ record, classify the bases first. 1416 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 1417 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 1418 e = CXXRD->bases_end(); i != e; ++i) { 1419 assert(!i->isVirtual() && !i->getType()->isDependentType() && 1420 "Unexpected base class!"); 1421 const CXXRecordDecl *Base = 1422 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1423 1424 // Classify this field. 1425 // 1426 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a 1427 // single eightbyte, each is classified separately. Each eightbyte gets 1428 // initialized to class NO_CLASS. 1429 Class FieldLo, FieldHi; 1430 uint64_t Offset = 1431 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); 1432 classify(i->getType(), Offset, FieldLo, FieldHi); 1433 Lo = merge(Lo, FieldLo); 1434 Hi = merge(Hi, FieldHi); 1435 if (Lo == Memory || Hi == Memory) 1436 break; 1437 } 1438 } 1439 1440 // Classify the fields one at a time, merging the results. 1441 unsigned idx = 0; 1442 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 1443 i != e; ++i, ++idx) { 1444 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 1445 bool BitField = i->isBitField(); 1446 1447 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than 1448 // four eightbytes, or it contains unaligned fields, it has class MEMORY. 1449 // 1450 // The only case a 256-bit wide vector could be used is when the struct 1451 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 1452 // to work for sizes wider than 128, early check and fallback to memory. 1453 // 1454 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { 1455 Lo = Memory; 1456 return; 1457 } 1458 // Note, skip this test for bit-fields, see below. 1459 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { 1460 Lo = Memory; 1461 return; 1462 } 1463 1464 // Classify this field. 1465 // 1466 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate 1467 // exceeds a single eightbyte, each is classified 1468 // separately. Each eightbyte gets initialized to class 1469 // NO_CLASS. 1470 Class FieldLo, FieldHi; 1471 1472 // Bit-fields require special handling, they do not force the 1473 // structure to be passed in memory even if unaligned, and 1474 // therefore they can straddle an eightbyte. 1475 if (BitField) { 1476 // Ignore padding bit-fields. 1477 if (i->isUnnamedBitfield()) 1478 continue; 1479 1480 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 1481 uint64_t Size = i->getBitWidthValue(getContext()); 1482 1483 uint64_t EB_Lo = Offset / 64; 1484 uint64_t EB_Hi = (Offset + Size - 1) / 64; 1485 FieldLo = FieldHi = NoClass; 1486 if (EB_Lo) { 1487 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); 1488 FieldLo = NoClass; 1489 FieldHi = Integer; 1490 } else { 1491 FieldLo = Integer; 1492 FieldHi = EB_Hi ? Integer : NoClass; 1493 } 1494 } else 1495 classify(i->getType(), Offset, FieldLo, FieldHi); 1496 Lo = merge(Lo, FieldLo); 1497 Hi = merge(Hi, FieldHi); 1498 if (Lo == Memory || Hi == Memory) 1499 break; 1500 } 1501 1502 postMerge(Size, Lo, Hi); 1503 } 1504 } 1505 1506 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { 1507 // If this is a scalar LLVM value then assume LLVM will pass it in the right 1508 // place naturally. 1509 if (!isAggregateTypeForABI(Ty)) { 1510 // Treat an enum type as its underlying type. 1511 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 1512 Ty = EnumTy->getDecl()->getIntegerType(); 1513 1514 return (Ty->isPromotableIntegerType() ? 1515 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 1516 } 1517 1518 return ABIArgInfo::getIndirect(0); 1519 } 1520 1521 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { 1522 if (const VectorType *VecTy = Ty->getAs<VectorType>()) { 1523 uint64_t Size = getContext().getTypeSize(VecTy); 1524 unsigned LargestVector = HasAVX ? 256 : 128; 1525 if (Size <= 64 || Size > LargestVector) 1526 return true; 1527 } 1528 1529 return false; 1530 } 1531 1532 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, 1533 unsigned freeIntRegs) const { 1534 // If this is a scalar LLVM value then assume LLVM will pass it in the right 1535 // place naturally. 1536 // 1537 // This assumption is optimistic, as there could be free registers available 1538 // when we need to pass this argument in memory, and LLVM could try to pass 1539 // the argument in the free register. This does not seem to happen currently, 1540 // but this code would be much safer if we could mark the argument with 1541 // 'onstack'. See PR12193. 1542 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { 1543 // Treat an enum type as its underlying type. 1544 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 1545 Ty = EnumTy->getDecl()->getIntegerType(); 1546 1547 return (Ty->isPromotableIntegerType() ? 1548 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 1549 } 1550 1551 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) 1552 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 1553 1554 // Compute the byval alignment. We specify the alignment of the byval in all 1555 // cases so that the mid-level optimizer knows the alignment of the byval. 1556 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); 1557 1558 // Attempt to avoid passing indirect results using byval when possible. This 1559 // is important for good codegen. 1560 // 1561 // We do this by coercing the value into a scalar type which the backend can 1562 // handle naturally (i.e., without using byval). 1563 // 1564 // For simplicity, we currently only do this when we have exhausted all of the 1565 // free integer registers. Doing this when there are free integer registers 1566 // would require more care, as we would have to ensure that the coerced value 1567 // did not claim the unused register. That would require either reording the 1568 // arguments to the function (so that any subsequent inreg values came first), 1569 // or only doing this optimization when there were no following arguments that 1570 // might be inreg. 1571 // 1572 // We currently expect it to be rare (particularly in well written code) for 1573 // arguments to be passed on the stack when there are still free integer 1574 // registers available (this would typically imply large structs being passed 1575 // by value), so this seems like a fair tradeoff for now. 1576 // 1577 // We can revisit this if the backend grows support for 'onstack' parameter 1578 // attributes. See PR12193. 1579 if (freeIntRegs == 0) { 1580 uint64_t Size = getContext().getTypeSize(Ty); 1581 1582 // If this type fits in an eightbyte, coerce it into the matching integral 1583 // type, which will end up on the stack (with alignment 8). 1584 if (Align == 8 && Size <= 64) 1585 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 1586 Size)); 1587 } 1588 1589 return ABIArgInfo::getIndirect(Align); 1590 } 1591 1592 /// GetByteVectorType - The ABI specifies that a value should be passed in an 1593 /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a 1594 /// vector register. 1595 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { 1596 llvm::Type *IRType = CGT.ConvertType(Ty); 1597 1598 // Wrapper structs that just contain vectors are passed just like vectors, 1599 // strip them off if present. 1600 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); 1601 while (STy && STy->getNumElements() == 1) { 1602 IRType = STy->getElementType(0); 1603 STy = dyn_cast<llvm::StructType>(IRType); 1604 } 1605 1606 // If the preferred type is a 16-byte vector, prefer to pass it. 1607 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ 1608 llvm::Type *EltTy = VT->getElementType(); 1609 unsigned BitWidth = VT->getBitWidth(); 1610 if ((BitWidth >= 128 && BitWidth <= 256) && 1611 (EltTy->isFloatTy() || EltTy->isDoubleTy() || 1612 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || 1613 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || 1614 EltTy->isIntegerTy(128))) 1615 return VT; 1616 } 1617 1618 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); 1619 } 1620 1621 /// BitsContainNoUserData - Return true if the specified [start,end) bit range 1622 /// is known to either be off the end of the specified type or being in 1623 /// alignment padding. The user type specified is known to be at most 128 bits 1624 /// in size, and have passed through X86_64ABIInfo::classify with a successful 1625 /// classification that put one of the two halves in the INTEGER class. 1626 /// 1627 /// It is conservatively correct to return false. 1628 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, 1629 unsigned EndBit, ASTContext &Context) { 1630 // If the bytes being queried are off the end of the type, there is no user 1631 // data hiding here. This handles analysis of builtins, vectors and other 1632 // types that don't contain interesting padding. 1633 unsigned TySize = (unsigned)Context.getTypeSize(Ty); 1634 if (TySize <= StartBit) 1635 return true; 1636 1637 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 1638 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); 1639 unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); 1640 1641 // Check each element to see if the element overlaps with the queried range. 1642 for (unsigned i = 0; i != NumElts; ++i) { 1643 // If the element is after the span we care about, then we're done.. 1644 unsigned EltOffset = i*EltSize; 1645 if (EltOffset >= EndBit) break; 1646 1647 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; 1648 if (!BitsContainNoUserData(AT->getElementType(), EltStart, 1649 EndBit-EltOffset, Context)) 1650 return false; 1651 } 1652 // If it overlaps no elements, then it is safe to process as padding. 1653 return true; 1654 } 1655 1656 if (const RecordType *RT = Ty->getAs<RecordType>()) { 1657 const RecordDecl *RD = RT->getDecl(); 1658 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 1659 1660 // If this is a C++ record, check the bases first. 1661 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 1662 for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), 1663 e = CXXRD->bases_end(); i != e; ++i) { 1664 assert(!i->isVirtual() && !i->getType()->isDependentType() && 1665 "Unexpected base class!"); 1666 const CXXRecordDecl *Base = 1667 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1668 1669 // If the base is after the span we care about, ignore it. 1670 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); 1671 if (BaseOffset >= EndBit) continue; 1672 1673 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; 1674 if (!BitsContainNoUserData(i->getType(), BaseStart, 1675 EndBit-BaseOffset, Context)) 1676 return false; 1677 } 1678 } 1679 1680 // Verify that no field has data that overlaps the region of interest. Yes 1681 // this could be sped up a lot by being smarter about queried fields, 1682 // however we're only looking at structs up to 16 bytes, so we don't care 1683 // much. 1684 unsigned idx = 0; 1685 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 1686 i != e; ++i, ++idx) { 1687 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); 1688 1689 // If we found a field after the region we care about, then we're done. 1690 if (FieldOffset >= EndBit) break; 1691 1692 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; 1693 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, 1694 Context)) 1695 return false; 1696 } 1697 1698 // If nothing in this record overlapped the area of interest, then we're 1699 // clean. 1700 return true; 1701 } 1702 1703 return false; 1704 } 1705 1706 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a 1707 /// float member at the specified offset. For example, {int,{float}} has a 1708 /// float at offset 4. It is conservatively correct for this routine to return 1709 /// false. 1710 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, 1711 const llvm::TargetData &TD) { 1712 // Base case if we find a float. 1713 if (IROffset == 0 && IRType->isFloatTy()) 1714 return true; 1715 1716 // If this is a struct, recurse into the field at the specified offset. 1717 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 1718 const llvm::StructLayout *SL = TD.getStructLayout(STy); 1719 unsigned Elt = SL->getElementContainingOffset(IROffset); 1720 IROffset -= SL->getElementOffset(Elt); 1721 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); 1722 } 1723 1724 // If this is an array, recurse into the field at the specified offset. 1725 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 1726 llvm::Type *EltTy = ATy->getElementType(); 1727 unsigned EltSize = TD.getTypeAllocSize(EltTy); 1728 IROffset -= IROffset/EltSize*EltSize; 1729 return ContainsFloatAtOffset(EltTy, IROffset, TD); 1730 } 1731 1732 return false; 1733 } 1734 1735 1736 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the 1737 /// low 8 bytes of an XMM register, corresponding to the SSE class. 1738 llvm::Type *X86_64ABIInfo:: 1739 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, 1740 QualType SourceTy, unsigned SourceOffset) const { 1741 // The only three choices we have are either double, <2 x float>, or float. We 1742 // pass as float if the last 4 bytes is just padding. This happens for 1743 // structs that contain 3 floats. 1744 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, 1745 SourceOffset*8+64, getContext())) 1746 return llvm::Type::getFloatTy(getVMContext()); 1747 1748 // We want to pass as <2 x float> if the LLVM IR type contains a float at 1749 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the 1750 // case. 1751 if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) && 1752 ContainsFloatAtOffset(IRType, IROffset+4, getTargetData())) 1753 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); 1754 1755 return llvm::Type::getDoubleTy(getVMContext()); 1756 } 1757 1758 1759 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in 1760 /// an 8-byte GPR. This means that we either have a scalar or we are talking 1761 /// about the high or low part of an up-to-16-byte struct. This routine picks 1762 /// the best LLVM IR type to represent this, which may be i64 or may be anything 1763 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, 1764 /// etc). 1765 /// 1766 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for 1767 /// the source type. IROffset is an offset in bytes into the LLVM IR type that 1768 /// the 8-byte value references. PrefType may be null. 1769 /// 1770 /// SourceTy is the source level type for the entire argument. SourceOffset is 1771 /// an offset into this that we're processing (which is always either 0 or 8). 1772 /// 1773 llvm::Type *X86_64ABIInfo:: 1774 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, 1775 QualType SourceTy, unsigned SourceOffset) const { 1776 // If we're dealing with an un-offset LLVM IR type, then it means that we're 1777 // returning an 8-byte unit starting with it. See if we can safely use it. 1778 if (IROffset == 0) { 1779 // Pointers and int64's always fill the 8-byte unit. 1780 if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64)) 1781 return IRType; 1782 1783 // If we have a 1/2/4-byte integer, we can use it only if the rest of the 1784 // goodness in the source type is just tail padding. This is allowed to 1785 // kick in for struct {double,int} on the int, but not on 1786 // struct{double,int,int} because we wouldn't return the second int. We 1787 // have to do this analysis on the source type because we can't depend on 1788 // unions being lowered a specific way etc. 1789 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || 1790 IRType->isIntegerTy(32)) { 1791 unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth(); 1792 1793 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, 1794 SourceOffset*8+64, getContext())) 1795 return IRType; 1796 } 1797 } 1798 1799 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 1800 // If this is a struct, recurse into the field at the specified offset. 1801 const llvm::StructLayout *SL = getTargetData().getStructLayout(STy); 1802 if (IROffset < SL->getSizeInBytes()) { 1803 unsigned FieldIdx = SL->getElementContainingOffset(IROffset); 1804 IROffset -= SL->getElementOffset(FieldIdx); 1805 1806 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, 1807 SourceTy, SourceOffset); 1808 } 1809 } 1810 1811 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 1812 llvm::Type *EltTy = ATy->getElementType(); 1813 unsigned EltSize = getTargetData().getTypeAllocSize(EltTy); 1814 unsigned EltOffset = IROffset/EltSize*EltSize; 1815 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, 1816 SourceOffset); 1817 } 1818 1819 // Okay, we don't have any better idea of what to pass, so we pass this in an 1820 // integer register that isn't too big to fit the rest of the struct. 1821 unsigned TySizeInBytes = 1822 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); 1823 1824 assert(TySizeInBytes != SourceOffset && "Empty field?"); 1825 1826 // It is always safe to classify this as an integer type up to i64 that 1827 // isn't larger than the structure. 1828 return llvm::IntegerType::get(getVMContext(), 1829 std::min(TySizeInBytes-SourceOffset, 8U)*8); 1830 } 1831 1832 1833 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally 1834 /// be used as elements of a two register pair to pass or return, return a 1835 /// first class aggregate to represent them. For example, if the low part of 1836 /// a by-value argument should be passed as i32* and the high part as float, 1837 /// return {i32*, float}. 1838 static llvm::Type * 1839 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, 1840 const llvm::TargetData &TD) { 1841 // In order to correctly satisfy the ABI, we need to the high part to start 1842 // at offset 8. If the high and low parts we inferred are both 4-byte types 1843 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have 1844 // the second element at offset 8. Check for this: 1845 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); 1846 unsigned HiAlign = TD.getABITypeAlignment(Hi); 1847 unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign); 1848 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); 1849 1850 // To handle this, we have to increase the size of the low part so that the 1851 // second element will start at an 8 byte offset. We can't increase the size 1852 // of the second element because it might make us access off the end of the 1853 // struct. 1854 if (HiStart != 8) { 1855 // There are only two sorts of types the ABI generation code can produce for 1856 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. 1857 // Promote these to a larger type. 1858 if (Lo->isFloatTy()) 1859 Lo = llvm::Type::getDoubleTy(Lo->getContext()); 1860 else { 1861 assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); 1862 Lo = llvm::Type::getInt64Ty(Lo->getContext()); 1863 } 1864 } 1865 1866 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); 1867 1868 1869 // Verify that the second element is at an 8-byte offset. 1870 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && 1871 "Invalid x86-64 argument pair!"); 1872 return Result; 1873 } 1874 1875 ABIArgInfo X86_64ABIInfo:: 1876 classifyReturnType(QualType RetTy) const { 1877 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the 1878 // classification algorithm. 1879 X86_64ABIInfo::Class Lo, Hi; 1880 classify(RetTy, 0, Lo, Hi); 1881 1882 // Check some invariants. 1883 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 1884 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 1885 1886 llvm::Type *ResType = 0; 1887 switch (Lo) { 1888 case NoClass: 1889 if (Hi == NoClass) 1890 return ABIArgInfo::getIgnore(); 1891 // If the low part is just padding, it takes no register, leave ResType 1892 // null. 1893 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 1894 "Unknown missing lo part"); 1895 break; 1896 1897 case SSEUp: 1898 case X87Up: 1899 llvm_unreachable("Invalid classification for lo word."); 1900 1901 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via 1902 // hidden argument. 1903 case Memory: 1904 return getIndirectReturnResult(RetTy); 1905 1906 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next 1907 // available register of the sequence %rax, %rdx is used. 1908 case Integer: 1909 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 1910 1911 // If we have a sign or zero extended integer, make sure to return Extend 1912 // so that the parameter gets the right LLVM IR attributes. 1913 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 1914 // Treat an enum type as its underlying type. 1915 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 1916 RetTy = EnumTy->getDecl()->getIntegerType(); 1917 1918 if (RetTy->isIntegralOrEnumerationType() && 1919 RetTy->isPromotableIntegerType()) 1920 return ABIArgInfo::getExtend(); 1921 } 1922 break; 1923 1924 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next 1925 // available SSE register of the sequence %xmm0, %xmm1 is used. 1926 case SSE: 1927 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 1928 break; 1929 1930 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is 1931 // returned on the X87 stack in %st0 as 80-bit x87 number. 1932 case X87: 1933 ResType = llvm::Type::getX86_FP80Ty(getVMContext()); 1934 break; 1935 1936 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real 1937 // part of the value is returned in %st0 and the imaginary part in 1938 // %st1. 1939 case ComplexX87: 1940 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); 1941 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), 1942 llvm::Type::getX86_FP80Ty(getVMContext()), 1943 NULL); 1944 break; 1945 } 1946 1947 llvm::Type *HighPart = 0; 1948 switch (Hi) { 1949 // Memory was handled previously and X87 should 1950 // never occur as a hi class. 1951 case Memory: 1952 case X87: 1953 llvm_unreachable("Invalid classification for hi word."); 1954 1955 case ComplexX87: // Previously handled. 1956 case NoClass: 1957 break; 1958 1959 case Integer: 1960 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 1961 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 1962 return ABIArgInfo::getDirect(HighPart, 8); 1963 break; 1964 case SSE: 1965 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 1966 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 1967 return ABIArgInfo::getDirect(HighPart, 8); 1968 break; 1969 1970 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte 1971 // is passed in the next available eightbyte chunk if the last used 1972 // vector register. 1973 // 1974 // SSEUP should always be preceded by SSE, just widen. 1975 case SSEUp: 1976 assert(Lo == SSE && "Unexpected SSEUp classification."); 1977 ResType = GetByteVectorType(RetTy); 1978 break; 1979 1980 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is 1981 // returned together with the previous X87 value in %st0. 1982 case X87Up: 1983 // If X87Up is preceded by X87, we don't need to do 1984 // anything. However, in some cases with unions it may not be 1985 // preceded by X87. In such situations we follow gcc and pass the 1986 // extra bits in an SSE reg. 1987 if (Lo != X87) { 1988 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 1989 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 1990 return ABIArgInfo::getDirect(HighPart, 8); 1991 } 1992 break; 1993 } 1994 1995 // If a high part was specified, merge it together with the low part. It is 1996 // known to pass in the high eightbyte of the result. We do this by forming a 1997 // first class struct aggregate with the high and low part: {low, high} 1998 if (HighPart) 1999 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); 2000 2001 return ABIArgInfo::getDirect(ResType); 2002 } 2003 2004 ABIArgInfo X86_64ABIInfo::classifyArgumentType( 2005 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE) 2006 const 2007 { 2008 X86_64ABIInfo::Class Lo, Hi; 2009 classify(Ty, 0, Lo, Hi); 2010 2011 // Check some invariants. 2012 // FIXME: Enforce these by construction. 2013 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 2014 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 2015 2016 neededInt = 0; 2017 neededSSE = 0; 2018 llvm::Type *ResType = 0; 2019 switch (Lo) { 2020 case NoClass: 2021 if (Hi == NoClass) 2022 return ABIArgInfo::getIgnore(); 2023 // If the low part is just padding, it takes no register, leave ResType 2024 // null. 2025 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 2026 "Unknown missing lo part"); 2027 break; 2028 2029 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument 2030 // on the stack. 2031 case Memory: 2032 2033 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or 2034 // COMPLEX_X87, it is passed in memory. 2035 case X87: 2036 case ComplexX87: 2037 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) 2038 ++neededInt; 2039 return getIndirectResult(Ty, freeIntRegs); 2040 2041 case SSEUp: 2042 case X87Up: 2043 llvm_unreachable("Invalid classification for lo word."); 2044 2045 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next 2046 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 2047 // and %r9 is used. 2048 case Integer: 2049 ++neededInt; 2050 2051 // Pick an 8-byte type based on the preferred type. 2052 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); 2053 2054 // If we have a sign or zero extended integer, make sure to return Extend 2055 // so that the parameter gets the right LLVM IR attributes. 2056 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 2057 // Treat an enum type as its underlying type. 2058 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2059 Ty = EnumTy->getDecl()->getIntegerType(); 2060 2061 if (Ty->isIntegralOrEnumerationType() && 2062 Ty->isPromotableIntegerType()) 2063 return ABIArgInfo::getExtend(); 2064 } 2065 2066 break; 2067 2068 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next 2069 // available SSE register is used, the registers are taken in the 2070 // order from %xmm0 to %xmm7. 2071 case SSE: { 2072 llvm::Type *IRType = CGT.ConvertType(Ty); 2073 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); 2074 ++neededSSE; 2075 break; 2076 } 2077 } 2078 2079 llvm::Type *HighPart = 0; 2080 switch (Hi) { 2081 // Memory was handled previously, ComplexX87 and X87 should 2082 // never occur as hi classes, and X87Up must be preceded by X87, 2083 // which is passed in memory. 2084 case Memory: 2085 case X87: 2086 case ComplexX87: 2087 llvm_unreachable("Invalid classification for hi word."); 2088 2089 case NoClass: break; 2090 2091 case Integer: 2092 ++neededInt; 2093 // Pick an 8-byte type based on the preferred type. 2094 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 2095 2096 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 2097 return ABIArgInfo::getDirect(HighPart, 8); 2098 break; 2099 2100 // X87Up generally doesn't occur here (long double is passed in 2101 // memory), except in situations involving unions. 2102 case X87Up: 2103 case SSE: 2104 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 2105 2106 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 2107 return ABIArgInfo::getDirect(HighPart, 8); 2108 2109 ++neededSSE; 2110 break; 2111 2112 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the 2113 // eightbyte is passed in the upper half of the last used SSE 2114 // register. This only happens when 128-bit vectors are passed. 2115 case SSEUp: 2116 assert(Lo == SSE && "Unexpected SSEUp classification"); 2117 ResType = GetByteVectorType(Ty); 2118 break; 2119 } 2120 2121 // If a high part was specified, merge it together with the low part. It is 2122 // known to pass in the high eightbyte of the result. We do this by forming a 2123 // first class struct aggregate with the high and low part: {low, high} 2124 if (HighPart) 2125 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); 2126 2127 return ABIArgInfo::getDirect(ResType); 2128 } 2129 2130 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 2131 2132 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 2133 2134 // Keep track of the number of assigned registers. 2135 unsigned freeIntRegs = 6, freeSSERegs = 8; 2136 2137 // If the return value is indirect, then the hidden argument is consuming one 2138 // integer register. 2139 if (FI.getReturnInfo().isIndirect()) 2140 --freeIntRegs; 2141 2142 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers 2143 // get assigned (in left-to-right order) for passing as follows... 2144 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 2145 it != ie; ++it) { 2146 unsigned neededInt, neededSSE; 2147 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, 2148 neededSSE); 2149 2150 // AMD64-ABI 3.2.3p3: If there are no registers available for any 2151 // eightbyte of an argument, the whole argument is passed on the 2152 // stack. If registers have already been assigned for some 2153 // eightbytes of such an argument, the assignments get reverted. 2154 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { 2155 freeIntRegs -= neededInt; 2156 freeSSERegs -= neededSSE; 2157 } else { 2158 it->info = getIndirectResult(it->type, freeIntRegs); 2159 } 2160 } 2161 } 2162 2163 static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, 2164 QualType Ty, 2165 CodeGenFunction &CGF) { 2166 llvm::Value *overflow_arg_area_p = 2167 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); 2168 llvm::Value *overflow_arg_area = 2169 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); 2170 2171 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 2172 // byte boundary if alignment needed by type exceeds 8 byte boundary. 2173 // It isn't stated explicitly in the standard, but in practice we use 2174 // alignment greater than 16 where necessary. 2175 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; 2176 if (Align > 8) { 2177 // overflow_arg_area = (overflow_arg_area + align - 1) & -align; 2178 llvm::Value *Offset = 2179 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); 2180 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); 2181 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, 2182 CGF.Int64Ty); 2183 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); 2184 overflow_arg_area = 2185 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), 2186 overflow_arg_area->getType(), 2187 "overflow_arg_area.align"); 2188 } 2189 2190 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. 2191 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 2192 llvm::Value *Res = 2193 CGF.Builder.CreateBitCast(overflow_arg_area, 2194 llvm::PointerType::getUnqual(LTy)); 2195 2196 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: 2197 // l->overflow_arg_area + sizeof(type). 2198 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to 2199 // an 8 byte boundary. 2200 2201 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; 2202 llvm::Value *Offset = 2203 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); 2204 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, 2205 "overflow_arg_area.next"); 2206 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); 2207 2208 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. 2209 return Res; 2210 } 2211 2212 llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2213 CodeGenFunction &CGF) const { 2214 // Assume that va_list type is correct; should be pointer to LLVM type: 2215 // struct { 2216 // i32 gp_offset; 2217 // i32 fp_offset; 2218 // i8* overflow_arg_area; 2219 // i8* reg_save_area; 2220 // }; 2221 unsigned neededInt, neededSSE; 2222 2223 Ty = CGF.getContext().getCanonicalType(Ty); 2224 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE); 2225 2226 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed 2227 // in the registers. If not go to step 7. 2228 if (!neededInt && !neededSSE) 2229 return EmitVAArgFromMemory(VAListAddr, Ty, CGF); 2230 2231 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of 2232 // general purpose registers needed to pass type and num_fp to hold 2233 // the number of floating point registers needed. 2234 2235 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into 2236 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or 2237 // l->fp_offset > 304 - num_fp * 16 go to step 7. 2238 // 2239 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of 2240 // register save space). 2241 2242 llvm::Value *InRegs = 0; 2243 llvm::Value *gp_offset_p = 0, *gp_offset = 0; 2244 llvm::Value *fp_offset_p = 0, *fp_offset = 0; 2245 if (neededInt) { 2246 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); 2247 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); 2248 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); 2249 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); 2250 } 2251 2252 if (neededSSE) { 2253 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); 2254 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); 2255 llvm::Value *FitsInFP = 2256 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); 2257 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); 2258 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; 2259 } 2260 2261 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 2262 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 2263 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 2264 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 2265 2266 // Emit code to load the value if it was passed in registers. 2267 2268 CGF.EmitBlock(InRegBlock); 2269 2270 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with 2271 // an offset of l->gp_offset and/or l->fp_offset. This may require 2272 // copying to a temporary location in case the parameter is passed 2273 // in different register classes or requires an alignment greater 2274 // than 8 for general purpose registers and 16 for XMM registers. 2275 // 2276 // FIXME: This really results in shameful code when we end up needing to 2277 // collect arguments from different places; often what should result in a 2278 // simple assembling of a structure from scattered addresses has many more 2279 // loads than necessary. Can we clean this up? 2280 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 2281 llvm::Value *RegAddr = 2282 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), 2283 "reg_save_area"); 2284 if (neededInt && neededSSE) { 2285 // FIXME: Cleanup. 2286 assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); 2287 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); 2288 llvm::Value *Tmp = CGF.CreateTempAlloca(ST); 2289 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); 2290 llvm::Type *TyLo = ST->getElementType(0); 2291 llvm::Type *TyHi = ST->getElementType(1); 2292 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && 2293 "Unexpected ABI info for mixed regs"); 2294 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); 2295 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); 2296 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); 2297 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); 2298 llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; 2299 llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; 2300 llvm::Value *V = 2301 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); 2302 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 2303 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); 2304 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 2305 2306 RegAddr = CGF.Builder.CreateBitCast(Tmp, 2307 llvm::PointerType::getUnqual(LTy)); 2308 } else if (neededInt) { 2309 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); 2310 RegAddr = CGF.Builder.CreateBitCast(RegAddr, 2311 llvm::PointerType::getUnqual(LTy)); 2312 } else if (neededSSE == 1) { 2313 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); 2314 RegAddr = CGF.Builder.CreateBitCast(RegAddr, 2315 llvm::PointerType::getUnqual(LTy)); 2316 } else { 2317 assert(neededSSE == 2 && "Invalid number of needed registers!"); 2318 // SSE registers are spaced 16 bytes apart in the register save 2319 // area, we need to collect the two eightbytes together. 2320 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); 2321 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); 2322 llvm::Type *DoubleTy = CGF.DoubleTy; 2323 llvm::Type *DblPtrTy = 2324 llvm::PointerType::getUnqual(DoubleTy); 2325 llvm::StructType *ST = llvm::StructType::get(DoubleTy, 2326 DoubleTy, NULL); 2327 llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); 2328 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, 2329 DblPtrTy)); 2330 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 2331 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, 2332 DblPtrTy)); 2333 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 2334 RegAddr = CGF.Builder.CreateBitCast(Tmp, 2335 llvm::PointerType::getUnqual(LTy)); 2336 } 2337 2338 // AMD64-ABI 3.5.7p5: Step 5. Set: 2339 // l->gp_offset = l->gp_offset + num_gp * 8 2340 // l->fp_offset = l->fp_offset + num_fp * 16. 2341 if (neededInt) { 2342 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); 2343 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), 2344 gp_offset_p); 2345 } 2346 if (neededSSE) { 2347 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); 2348 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), 2349 fp_offset_p); 2350 } 2351 CGF.EmitBranch(ContBlock); 2352 2353 // Emit code to load the value if it was passed in memory. 2354 2355 CGF.EmitBlock(InMemBlock); 2356 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); 2357 2358 // Return the appropriate result. 2359 2360 CGF.EmitBlock(ContBlock); 2361 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, 2362 "vaarg.addr"); 2363 ResAddr->addIncoming(RegAddr, InRegBlock); 2364 ResAddr->addIncoming(MemAddr, InMemBlock); 2365 return ResAddr; 2366 } 2367 2368 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const { 2369 2370 if (Ty->isVoidType()) 2371 return ABIArgInfo::getIgnore(); 2372 2373 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2374 Ty = EnumTy->getDecl()->getIntegerType(); 2375 2376 uint64_t Size = getContext().getTypeSize(Ty); 2377 2378 if (const RecordType *RT = Ty->getAs<RecordType>()) { 2379 if (hasNonTrivialDestructorOrCopyConstructor(RT) || 2380 RT->getDecl()->hasFlexibleArrayMember()) 2381 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 2382 2383 // FIXME: mingw-w64-gcc emits 128-bit struct as i128 2384 if (Size == 128 && 2385 getContext().getTargetInfo().getTriple().getOS() 2386 == llvm::Triple::MinGW32) 2387 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 2388 Size)); 2389 2390 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 2391 // not 1, 2, 4, or 8 bytes, must be passed by reference." 2392 if (Size <= 64 && 2393 (Size & (Size - 1)) == 0) 2394 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 2395 Size)); 2396 2397 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 2398 } 2399 2400 if (Ty->isPromotableIntegerType()) 2401 return ABIArgInfo::getExtend(); 2402 2403 return ABIArgInfo::getDirect(); 2404 } 2405 2406 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 2407 2408 QualType RetTy = FI.getReturnType(); 2409 FI.getReturnInfo() = classify(RetTy); 2410 2411 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 2412 it != ie; ++it) 2413 it->info = classify(it->type); 2414 } 2415 2416 llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2417 CodeGenFunction &CGF) const { 2418 llvm::Type *BPP = CGF.Int8PtrPtrTy; 2419 2420 CGBuilderTy &Builder = CGF.Builder; 2421 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 2422 "ap"); 2423 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 2424 llvm::Type *PTy = 2425 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 2426 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 2427 2428 uint64_t Offset = 2429 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); 2430 llvm::Value *NextAddr = 2431 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 2432 "ap.next"); 2433 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 2434 2435 return AddrTyped; 2436 } 2437 2438 // PowerPC-32 2439 2440 namespace { 2441 class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 2442 public: 2443 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 2444 2445 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 2446 // This is recovered from gcc output. 2447 return 1; // r1 is the dedicated stack pointer 2448 } 2449 2450 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2451 llvm::Value *Address) const; 2452 }; 2453 2454 } 2455 2456 bool 2457 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2458 llvm::Value *Address) const { 2459 // This is calculated from the LLVM and GCC tables and verified 2460 // against gcc output. AFAIK all ABIs use the same encoding. 2461 2462 CodeGen::CGBuilderTy &Builder = CGF.Builder; 2463 2464 llvm::IntegerType *i8 = CGF.Int8Ty; 2465 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 2466 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 2467 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 2468 2469 // 0-31: r0-31, the 4-byte general-purpose registers 2470 AssignToArrayRange(Builder, Address, Four8, 0, 31); 2471 2472 // 32-63: fp0-31, the 8-byte floating-point registers 2473 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 2474 2475 // 64-76 are various 4-byte special-purpose registers: 2476 // 64: mq 2477 // 65: lr 2478 // 66: ctr 2479 // 67: ap 2480 // 68-75 cr0-7 2481 // 76: xer 2482 AssignToArrayRange(Builder, Address, Four8, 64, 76); 2483 2484 // 77-108: v0-31, the 16-byte vector registers 2485 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 2486 2487 // 109: vrsave 2488 // 110: vscr 2489 // 111: spe_acc 2490 // 112: spefscr 2491 // 113: sfp 2492 AssignToArrayRange(Builder, Address, Four8, 109, 113); 2493 2494 return false; 2495 } 2496 2497 // PowerPC-64 2498 2499 namespace { 2500 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 2501 public: 2502 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 2503 2504 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 2505 // This is recovered from gcc output. 2506 return 1; // r1 is the dedicated stack pointer 2507 } 2508 2509 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2510 llvm::Value *Address) const; 2511 }; 2512 2513 } 2514 2515 bool 2516 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2517 llvm::Value *Address) const { 2518 // This is calculated from the LLVM and GCC tables and verified 2519 // against gcc output. AFAIK all ABIs use the same encoding. 2520 2521 CodeGen::CGBuilderTy &Builder = CGF.Builder; 2522 2523 llvm::IntegerType *i8 = CGF.Int8Ty; 2524 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 2525 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 2526 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 2527 2528 // 0-31: r0-31, the 8-byte general-purpose registers 2529 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 2530 2531 // 32-63: fp0-31, the 8-byte floating-point registers 2532 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 2533 2534 // 64-76 are various 4-byte special-purpose registers: 2535 // 64: mq 2536 // 65: lr 2537 // 66: ctr 2538 // 67: ap 2539 // 68-75 cr0-7 2540 // 76: xer 2541 AssignToArrayRange(Builder, Address, Four8, 64, 76); 2542 2543 // 77-108: v0-31, the 16-byte vector registers 2544 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 2545 2546 // 109: vrsave 2547 // 110: vscr 2548 // 111: spe_acc 2549 // 112: spefscr 2550 // 113: sfp 2551 AssignToArrayRange(Builder, Address, Four8, 109, 113); 2552 2553 return false; 2554 } 2555 2556 //===----------------------------------------------------------------------===// 2557 // ARM ABI Implementation 2558 //===----------------------------------------------------------------------===// 2559 2560 namespace { 2561 2562 class ARMABIInfo : public ABIInfo { 2563 public: 2564 enum ABIKind { 2565 APCS = 0, 2566 AAPCS = 1, 2567 AAPCS_VFP 2568 }; 2569 2570 private: 2571 ABIKind Kind; 2572 2573 public: 2574 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {} 2575 2576 bool isEABI() const { 2577 StringRef Env = 2578 getContext().getTargetInfo().getTriple().getEnvironmentName(); 2579 return (Env == "gnueabi" || Env == "eabi" || 2580 Env == "android" || Env == "androideabi"); 2581 } 2582 2583 private: 2584 ABIKind getABIKind() const { return Kind; } 2585 2586 ABIArgInfo classifyReturnType(QualType RetTy) const; 2587 ABIArgInfo classifyArgumentType(QualType RetTy) const; 2588 2589 virtual void computeInfo(CGFunctionInfo &FI) const; 2590 2591 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2592 CodeGenFunction &CGF) const; 2593 }; 2594 2595 class ARMTargetCodeGenInfo : public TargetCodeGenInfo { 2596 public: 2597 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 2598 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} 2599 2600 const ARMABIInfo &getABIInfo() const { 2601 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); 2602 } 2603 2604 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 2605 return 13; 2606 } 2607 2608 StringRef getARCRetainAutoreleasedReturnValueMarker() const { 2609 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; 2610 } 2611 2612 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 2613 llvm::Value *Address) const { 2614 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 2615 2616 // 0-15 are the 16 integer registers. 2617 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); 2618 return false; 2619 } 2620 2621 unsigned getSizeOfUnwindException() const { 2622 if (getABIInfo().isEABI()) return 88; 2623 return TargetCodeGenInfo::getSizeOfUnwindException(); 2624 } 2625 }; 2626 2627 } 2628 2629 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 2630 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 2631 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 2632 it != ie; ++it) 2633 it->info = classifyArgumentType(it->type); 2634 2635 // Always honor user-specified calling convention. 2636 if (FI.getCallingConvention() != llvm::CallingConv::C) 2637 return; 2638 2639 // Calling convention as default by an ABI. 2640 llvm::CallingConv::ID DefaultCC; 2641 if (isEABI()) 2642 DefaultCC = llvm::CallingConv::ARM_AAPCS; 2643 else 2644 DefaultCC = llvm::CallingConv::ARM_APCS; 2645 2646 // If user did not ask for specific calling convention explicitly (e.g. via 2647 // pcs attribute), set effective calling convention if it's different than ABI 2648 // default. 2649 switch (getABIKind()) { 2650 case APCS: 2651 if (DefaultCC != llvm::CallingConv::ARM_APCS) 2652 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS); 2653 break; 2654 case AAPCS: 2655 if (DefaultCC != llvm::CallingConv::ARM_AAPCS) 2656 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS); 2657 break; 2658 case AAPCS_VFP: 2659 if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP) 2660 FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP); 2661 break; 2662 } 2663 } 2664 2665 /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous 2666 /// aggregate. If HAMembers is non-null, the number of base elements 2667 /// contained in the type is returned through it; this is used for the 2668 /// recursive calls that check aggregate component types. 2669 static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, 2670 ASTContext &Context, 2671 uint64_t *HAMembers = 0) { 2672 uint64_t Members = 0; 2673 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 2674 if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) 2675 return false; 2676 Members *= AT->getSize().getZExtValue(); 2677 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 2678 const RecordDecl *RD = RT->getDecl(); 2679 if (RD->hasFlexibleArrayMember()) 2680 return false; 2681 2682 Members = 0; 2683 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 2684 i != e; ++i) { 2685 const FieldDecl *FD = *i; 2686 uint64_t FldMembers; 2687 if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) 2688 return false; 2689 2690 Members = (RD->isUnion() ? 2691 std::max(Members, FldMembers) : Members + FldMembers); 2692 } 2693 } else { 2694 Members = 1; 2695 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 2696 Members = 2; 2697 Ty = CT->getElementType(); 2698 } 2699 2700 // Homogeneous aggregates for AAPCS-VFP must have base types of float, 2701 // double, or 64-bit or 128-bit vectors. 2702 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 2703 if (BT->getKind() != BuiltinType::Float && 2704 BT->getKind() != BuiltinType::Double && 2705 BT->getKind() != BuiltinType::LongDouble) 2706 return false; 2707 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 2708 unsigned VecSize = Context.getTypeSize(VT); 2709 if (VecSize != 64 && VecSize != 128) 2710 return false; 2711 } else { 2712 return false; 2713 } 2714 2715 // The base type must be the same for all members. Vector types of the 2716 // same total size are treated as being equivalent here. 2717 const Type *TyPtr = Ty.getTypePtr(); 2718 if (!Base) 2719 Base = TyPtr; 2720 if (Base != TyPtr && 2721 (!Base->isVectorType() || !TyPtr->isVectorType() || 2722 Context.getTypeSize(Base) != Context.getTypeSize(TyPtr))) 2723 return false; 2724 } 2725 2726 // Homogeneous Aggregates can have at most 4 members of the base type. 2727 if (HAMembers) 2728 *HAMembers = Members; 2729 2730 return (Members > 0 && Members <= 4); 2731 } 2732 2733 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const { 2734 if (!isAggregateTypeForABI(Ty)) { 2735 // Treat an enum type as its underlying type. 2736 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 2737 Ty = EnumTy->getDecl()->getIntegerType(); 2738 2739 return (Ty->isPromotableIntegerType() ? 2740 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 2741 } 2742 2743 // Ignore empty records. 2744 if (isEmptyRecord(getContext(), Ty, true)) 2745 return ABIArgInfo::getIgnore(); 2746 2747 // Structures with either a non-trivial destructor or a non-trivial 2748 // copy constructor are always indirect. 2749 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) 2750 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 2751 2752 if (getABIKind() == ARMABIInfo::AAPCS_VFP) { 2753 // Homogeneous Aggregates need to be expanded. 2754 const Type *Base = 0; 2755 if (isHomogeneousAggregate(Ty, Base, getContext())) { 2756 assert(Base && "Base class should be set for homogeneous aggregate"); 2757 return ABIArgInfo::getExpand(); 2758 } 2759 } 2760 2761 // Support byval for ARM. 2762 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) || 2763 getContext().getTypeAlign(Ty) > 64) { 2764 return ABIArgInfo::getIndirect(0, /*ByVal=*/true); 2765 } 2766 2767 // Otherwise, pass by coercing to a structure of the appropriate size. 2768 llvm::Type* ElemTy; 2769 unsigned SizeRegs; 2770 // FIXME: Try to match the types of the arguments more accurately where 2771 // we can. 2772 if (getContext().getTypeAlign(Ty) <= 32) { 2773 ElemTy = llvm::Type::getInt32Ty(getVMContext()); 2774 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; 2775 } else { 2776 ElemTy = llvm::Type::getInt64Ty(getVMContext()); 2777 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; 2778 } 2779 2780 llvm::Type *STy = 2781 llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); 2782 return ABIArgInfo::getDirect(STy); 2783 } 2784 2785 static bool isIntegerLikeType(QualType Ty, ASTContext &Context, 2786 llvm::LLVMContext &VMContext) { 2787 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure 2788 // is called integer-like if its size is less than or equal to one word, and 2789 // the offset of each of its addressable sub-fields is zero. 2790 2791 uint64_t Size = Context.getTypeSize(Ty); 2792 2793 // Check that the type fits in a word. 2794 if (Size > 32) 2795 return false; 2796 2797 // FIXME: Handle vector types! 2798 if (Ty->isVectorType()) 2799 return false; 2800 2801 // Float types are never treated as "integer like". 2802 if (Ty->isRealFloatingType()) 2803 return false; 2804 2805 // If this is a builtin or pointer type then it is ok. 2806 if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) 2807 return true; 2808 2809 // Small complex integer types are "integer like". 2810 if (const ComplexType *CT = Ty->getAs<ComplexType>()) 2811 return isIntegerLikeType(CT->getElementType(), Context, VMContext); 2812 2813 // Single element and zero sized arrays should be allowed, by the definition 2814 // above, but they are not. 2815 2816 // Otherwise, it must be a record type. 2817 const RecordType *RT = Ty->getAs<RecordType>(); 2818 if (!RT) return false; 2819 2820 // Ignore records with flexible arrays. 2821 const RecordDecl *RD = RT->getDecl(); 2822 if (RD->hasFlexibleArrayMember()) 2823 return false; 2824 2825 // Check that all sub-fields are at offset 0, and are themselves "integer 2826 // like". 2827 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 2828 2829 bool HadField = false; 2830 unsigned idx = 0; 2831 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 2832 i != e; ++i, ++idx) { 2833 const FieldDecl *FD = *i; 2834 2835 // Bit-fields are not addressable, we only need to verify they are "integer 2836 // like". We still have to disallow a subsequent non-bitfield, for example: 2837 // struct { int : 0; int x } 2838 // is non-integer like according to gcc. 2839 if (FD->isBitField()) { 2840 if (!RD->isUnion()) 2841 HadField = true; 2842 2843 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 2844 return false; 2845 2846 continue; 2847 } 2848 2849 // Check if this field is at offset 0. 2850 if (Layout.getFieldOffset(idx) != 0) 2851 return false; 2852 2853 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 2854 return false; 2855 2856 // Only allow at most one field in a structure. This doesn't match the 2857 // wording above, but follows gcc in situations with a field following an 2858 // empty structure. 2859 if (!RD->isUnion()) { 2860 if (HadField) 2861 return false; 2862 2863 HadField = true; 2864 } 2865 } 2866 2867 return true; 2868 } 2869 2870 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { 2871 if (RetTy->isVoidType()) 2872 return ABIArgInfo::getIgnore(); 2873 2874 // Large vector types should be returned via memory. 2875 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) 2876 return ABIArgInfo::getIndirect(0); 2877 2878 if (!isAggregateTypeForABI(RetTy)) { 2879 // Treat an enum type as its underlying type. 2880 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 2881 RetTy = EnumTy->getDecl()->getIntegerType(); 2882 2883 return (RetTy->isPromotableIntegerType() ? 2884 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 2885 } 2886 2887 // Structures with either a non-trivial destructor or a non-trivial 2888 // copy constructor are always indirect. 2889 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) 2890 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 2891 2892 // Are we following APCS? 2893 if (getABIKind() == APCS) { 2894 if (isEmptyRecord(getContext(), RetTy, false)) 2895 return ABIArgInfo::getIgnore(); 2896 2897 // Complex types are all returned as packed integers. 2898 // 2899 // FIXME: Consider using 2 x vector types if the back end handles them 2900 // correctly. 2901 if (RetTy->isAnyComplexType()) 2902 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 2903 getContext().getTypeSize(RetTy))); 2904 2905 // Integer like structures are returned in r0. 2906 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { 2907 // Return in the smallest viable integer type. 2908 uint64_t Size = getContext().getTypeSize(RetTy); 2909 if (Size <= 8) 2910 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 2911 if (Size <= 16) 2912 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 2913 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 2914 } 2915 2916 // Otherwise return in memory. 2917 return ABIArgInfo::getIndirect(0); 2918 } 2919 2920 // Otherwise this is an AAPCS variant. 2921 2922 if (isEmptyRecord(getContext(), RetTy, true)) 2923 return ABIArgInfo::getIgnore(); 2924 2925 // Check for homogeneous aggregates with AAPCS-VFP. 2926 if (getABIKind() == AAPCS_VFP) { 2927 const Type *Base = 0; 2928 if (isHomogeneousAggregate(RetTy, Base, getContext())) { 2929 assert(Base && "Base class should be set for homogeneous aggregate"); 2930 // Homogeneous Aggregates are returned directly. 2931 return ABIArgInfo::getDirect(); 2932 } 2933 } 2934 2935 // Aggregates <= 4 bytes are returned in r0; other aggregates 2936 // are returned indirectly. 2937 uint64_t Size = getContext().getTypeSize(RetTy); 2938 if (Size <= 32) { 2939 // Return in the smallest viable integer type. 2940 if (Size <= 8) 2941 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 2942 if (Size <= 16) 2943 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 2944 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 2945 } 2946 2947 return ABIArgInfo::getIndirect(0); 2948 } 2949 2950 llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2951 CodeGenFunction &CGF) const { 2952 llvm::Type *BP = CGF.Int8PtrTy; 2953 llvm::Type *BPP = CGF.Int8PtrPtrTy; 2954 2955 CGBuilderTy &Builder = CGF.Builder; 2956 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 2957 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 2958 // Handle address alignment for type alignment > 32 bits 2959 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; 2960 if (TyAlign > 4) { 2961 assert((TyAlign & (TyAlign - 1)) == 0 && 2962 "Alignment is not power of 2!"); 2963 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); 2964 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); 2965 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); 2966 Addr = Builder.CreateIntToPtr(AddrAsInt, BP); 2967 } 2968 llvm::Type *PTy = 2969 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 2970 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 2971 2972 uint64_t Offset = 2973 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); 2974 llvm::Value *NextAddr = 2975 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 2976 "ap.next"); 2977 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 2978 2979 return AddrTyped; 2980 } 2981 2982 //===----------------------------------------------------------------------===// 2983 // NVPTX ABI Implementation 2984 //===----------------------------------------------------------------------===// 2985 2986 namespace { 2987 2988 class NVPTXABIInfo : public ABIInfo { 2989 public: 2990 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 2991 2992 ABIArgInfo classifyReturnType(QualType RetTy) const; 2993 ABIArgInfo classifyArgumentType(QualType Ty) const; 2994 2995 virtual void computeInfo(CGFunctionInfo &FI) const; 2996 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 2997 CodeGenFunction &CFG) const; 2998 }; 2999 3000 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { 3001 public: 3002 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) 3003 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} 3004 3005 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 3006 CodeGen::CodeGenModule &M) const; 3007 }; 3008 3009 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { 3010 if (RetTy->isVoidType()) 3011 return ABIArgInfo::getIgnore(); 3012 if (isAggregateTypeForABI(RetTy)) 3013 return ABIArgInfo::getIndirect(0); 3014 return ABIArgInfo::getDirect(); 3015 } 3016 3017 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { 3018 if (isAggregateTypeForABI(Ty)) 3019 return ABIArgInfo::getIndirect(0); 3020 3021 return ABIArgInfo::getDirect(); 3022 } 3023 3024 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { 3025 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3026 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3027 it != ie; ++it) 3028 it->info = classifyArgumentType(it->type); 3029 3030 // Always honor user-specified calling convention. 3031 if (FI.getCallingConvention() != llvm::CallingConv::C) 3032 return; 3033 3034 // Calling convention as default by an ABI. 3035 // We're still using the PTX_Kernel/PTX_Device calling conventions here, 3036 // but we should switch to NVVM metadata later on. 3037 llvm::CallingConv::ID DefaultCC; 3038 const LangOptions &LangOpts = getContext().getLangOpts(); 3039 if (LangOpts.OpenCL || LangOpts.CUDA) { 3040 // If we are in OpenCL or CUDA mode, then default to device functions 3041 DefaultCC = llvm::CallingConv::PTX_Device; 3042 } else { 3043 // If we are in standard C/C++ mode, use the triple to decide on the default 3044 StringRef Env = 3045 getContext().getTargetInfo().getTriple().getEnvironmentName(); 3046 if (Env == "device") 3047 DefaultCC = llvm::CallingConv::PTX_Device; 3048 else 3049 DefaultCC = llvm::CallingConv::PTX_Kernel; 3050 } 3051 FI.setEffectiveCallingConvention(DefaultCC); 3052 3053 } 3054 3055 llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3056 CodeGenFunction &CFG) const { 3057 llvm_unreachable("NVPTX does not support varargs"); 3058 } 3059 3060 void NVPTXTargetCodeGenInfo:: 3061 SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 3062 CodeGen::CodeGenModule &M) const{ 3063 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 3064 if (!FD) return; 3065 3066 llvm::Function *F = cast<llvm::Function>(GV); 3067 3068 // Perform special handling in OpenCL mode 3069 if (M.getLangOpts().OpenCL) { 3070 // Use OpenCL function attributes to set proper calling conventions 3071 // By default, all functions are device functions 3072 if (FD->hasAttr<OpenCLKernelAttr>()) { 3073 // OpenCL __kernel functions get a kernel calling convention 3074 F->setCallingConv(llvm::CallingConv::PTX_Kernel); 3075 // And kernel functions are not subject to inlining 3076 F->addFnAttr(llvm::Attribute::NoInline); 3077 } 3078 } 3079 3080 // Perform special handling in CUDA mode. 3081 if (M.getLangOpts().CUDA) { 3082 // CUDA __global__ functions get a kernel calling convention. Since 3083 // __global__ functions cannot be called from the device, we do not 3084 // need to set the noinline attribute. 3085 if (FD->getAttr<CUDAGlobalAttr>()) 3086 F->setCallingConv(llvm::CallingConv::PTX_Kernel); 3087 } 3088 } 3089 3090 } 3091 3092 //===----------------------------------------------------------------------===// 3093 // MBlaze ABI Implementation 3094 //===----------------------------------------------------------------------===// 3095 3096 namespace { 3097 3098 class MBlazeABIInfo : public ABIInfo { 3099 public: 3100 MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 3101 3102 bool isPromotableIntegerType(QualType Ty) const; 3103 3104 ABIArgInfo classifyReturnType(QualType RetTy) const; 3105 ABIArgInfo classifyArgumentType(QualType RetTy) const; 3106 3107 virtual void computeInfo(CGFunctionInfo &FI) const { 3108 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3109 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3110 it != ie; ++it) 3111 it->info = classifyArgumentType(it->type); 3112 } 3113 3114 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3115 CodeGenFunction &CGF) const; 3116 }; 3117 3118 class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo { 3119 public: 3120 MBlazeTargetCodeGenInfo(CodeGenTypes &CGT) 3121 : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {} 3122 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 3123 CodeGen::CodeGenModule &M) const; 3124 }; 3125 3126 } 3127 3128 bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const { 3129 // MBlaze ABI requires all 8 and 16 bit quantities to be extended. 3130 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 3131 switch (BT->getKind()) { 3132 case BuiltinType::Bool: 3133 case BuiltinType::Char_S: 3134 case BuiltinType::Char_U: 3135 case BuiltinType::SChar: 3136 case BuiltinType::UChar: 3137 case BuiltinType::Short: 3138 case BuiltinType::UShort: 3139 return true; 3140 default: 3141 return false; 3142 } 3143 return false; 3144 } 3145 3146 llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3147 CodeGenFunction &CGF) const { 3148 // FIXME: Implement 3149 return 0; 3150 } 3151 3152 3153 ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const { 3154 if (RetTy->isVoidType()) 3155 return ABIArgInfo::getIgnore(); 3156 if (isAggregateTypeForABI(RetTy)) 3157 return ABIArgInfo::getIndirect(0); 3158 3159 return (isPromotableIntegerType(RetTy) ? 3160 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 3161 } 3162 3163 ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const { 3164 if (isAggregateTypeForABI(Ty)) 3165 return ABIArgInfo::getIndirect(0); 3166 3167 return (isPromotableIntegerType(Ty) ? 3168 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 3169 } 3170 3171 void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D, 3172 llvm::GlobalValue *GV, 3173 CodeGen::CodeGenModule &M) 3174 const { 3175 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 3176 if (!FD) return; 3177 3178 llvm::CallingConv::ID CC = llvm::CallingConv::C; 3179 if (FD->hasAttr<MBlazeInterruptHandlerAttr>()) 3180 CC = llvm::CallingConv::MBLAZE_INTR; 3181 else if (FD->hasAttr<MBlazeSaveVolatilesAttr>()) 3182 CC = llvm::CallingConv::MBLAZE_SVOL; 3183 3184 if (CC != llvm::CallingConv::C) { 3185 // Handle 'interrupt_handler' attribute: 3186 llvm::Function *F = cast<llvm::Function>(GV); 3187 3188 // Step 1: Set ISR calling convention. 3189 F->setCallingConv(CC); 3190 3191 // Step 2: Add attributes goodness. 3192 F->addFnAttr(llvm::Attribute::NoInline); 3193 } 3194 3195 // Step 3: Emit _interrupt_handler alias. 3196 if (CC == llvm::CallingConv::MBLAZE_INTR) 3197 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, 3198 "_interrupt_handler", GV, &M.getModule()); 3199 } 3200 3201 3202 //===----------------------------------------------------------------------===// 3203 // MSP430 ABI Implementation 3204 //===----------------------------------------------------------------------===// 3205 3206 namespace { 3207 3208 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { 3209 public: 3210 MSP430TargetCodeGenInfo(CodeGenTypes &CGT) 3211 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 3212 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 3213 CodeGen::CodeGenModule &M) const; 3214 }; 3215 3216 } 3217 3218 void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, 3219 llvm::GlobalValue *GV, 3220 CodeGen::CodeGenModule &M) const { 3221 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 3222 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { 3223 // Handle 'interrupt' attribute: 3224 llvm::Function *F = cast<llvm::Function>(GV); 3225 3226 // Step 1: Set ISR calling convention. 3227 F->setCallingConv(llvm::CallingConv::MSP430_INTR); 3228 3229 // Step 2: Add attributes goodness. 3230 F->addFnAttr(llvm::Attribute::NoInline); 3231 3232 // Step 3: Emit ISR vector alias. 3233 unsigned Num = attr->getNumber() + 0xffe0; 3234 new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, 3235 "vector_" + Twine::utohexstr(Num), 3236 GV, &M.getModule()); 3237 } 3238 } 3239 } 3240 3241 //===----------------------------------------------------------------------===// 3242 // MIPS ABI Implementation. This works for both little-endian and 3243 // big-endian variants. 3244 //===----------------------------------------------------------------------===// 3245 3246 namespace { 3247 class MipsABIInfo : public ABIInfo { 3248 bool IsO32; 3249 unsigned MinABIStackAlignInBytes, StackAlignInBytes; 3250 void CoerceToIntArgs(uint64_t TySize, 3251 SmallVector<llvm::Type*, 8> &ArgList) const; 3252 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; 3253 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; 3254 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; 3255 public: 3256 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : 3257 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), 3258 StackAlignInBytes(IsO32 ? 8 : 16) {} 3259 3260 ABIArgInfo classifyReturnType(QualType RetTy) const; 3261 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; 3262 virtual void computeInfo(CGFunctionInfo &FI) const; 3263 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3264 CodeGenFunction &CGF) const; 3265 }; 3266 3267 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { 3268 unsigned SizeOfUnwindException; 3269 public: 3270 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) 3271 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), 3272 SizeOfUnwindException(IsO32 ? 24 : 32) {} 3273 3274 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { 3275 return 29; 3276 } 3277 3278 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3279 llvm::Value *Address) const; 3280 3281 unsigned getSizeOfUnwindException() const { 3282 return SizeOfUnwindException; 3283 } 3284 }; 3285 } 3286 3287 void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, 3288 SmallVector<llvm::Type*, 8> &ArgList) const { 3289 llvm::IntegerType *IntTy = 3290 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 3291 3292 // Add (TySize / MinABIStackAlignInBytes) args of IntTy. 3293 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) 3294 ArgList.push_back(IntTy); 3295 3296 // If necessary, add one more integer type to ArgList. 3297 unsigned R = TySize % (MinABIStackAlignInBytes * 8); 3298 3299 if (R) 3300 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); 3301 } 3302 3303 // In N32/64, an aligned double precision floating point field is passed in 3304 // a register. 3305 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { 3306 SmallVector<llvm::Type*, 8> ArgList, IntArgList; 3307 3308 if (IsO32) { 3309 CoerceToIntArgs(TySize, ArgList); 3310 return llvm::StructType::get(getVMContext(), ArgList); 3311 } 3312 3313 if (Ty->isComplexType()) 3314 return CGT.ConvertType(Ty); 3315 3316 const RecordType *RT = Ty->getAs<RecordType>(); 3317 3318 // Unions/vectors are passed in integer registers. 3319 if (!RT || !RT->isStructureOrClassType()) { 3320 CoerceToIntArgs(TySize, ArgList); 3321 return llvm::StructType::get(getVMContext(), ArgList); 3322 } 3323 3324 const RecordDecl *RD = RT->getDecl(); 3325 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 3326 assert(!(TySize % 8) && "Size of structure must be multiple of 8."); 3327 3328 uint64_t LastOffset = 0; 3329 unsigned idx = 0; 3330 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); 3331 3332 // Iterate over fields in the struct/class and check if there are any aligned 3333 // double fields. 3334 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 3335 i != e; ++i, ++idx) { 3336 const QualType Ty = i->getType(); 3337 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 3338 3339 if (!BT || BT->getKind() != BuiltinType::Double) 3340 continue; 3341 3342 uint64_t Offset = Layout.getFieldOffset(idx); 3343 if (Offset % 64) // Ignore doubles that are not aligned. 3344 continue; 3345 3346 // Add ((Offset - LastOffset) / 64) args of type i64. 3347 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) 3348 ArgList.push_back(I64); 3349 3350 // Add double type. 3351 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); 3352 LastOffset = Offset + 64; 3353 } 3354 3355 CoerceToIntArgs(TySize - LastOffset, IntArgList); 3356 ArgList.append(IntArgList.begin(), IntArgList.end()); 3357 3358 return llvm::StructType::get(getVMContext(), ArgList); 3359 } 3360 3361 llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const { 3362 assert((Offset % MinABIStackAlignInBytes) == 0); 3363 3364 if ((Align - 1) & Offset) 3365 return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 3366 3367 return 0; 3368 } 3369 3370 ABIArgInfo 3371 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { 3372 uint64_t OrigOffset = Offset; 3373 uint64_t TySize = getContext().getTypeSize(Ty); 3374 uint64_t Align = getContext().getTypeAlign(Ty) / 8; 3375 3376 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), 3377 (uint64_t)StackAlignInBytes); 3378 Offset = llvm::RoundUpToAlignment(Offset, Align); 3379 Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8; 3380 3381 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { 3382 // Ignore empty aggregates. 3383 if (TySize == 0) 3384 return ABIArgInfo::getIgnore(); 3385 3386 // Records with non trivial destructors/constructors should not be passed 3387 // by value. 3388 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) { 3389 Offset = OrigOffset + MinABIStackAlignInBytes; 3390 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 3391 } 3392 3393 // If we have reached here, aggregates are passed directly by coercing to 3394 // another structure type. Padding is inserted if the offset of the 3395 // aggregate is unaligned. 3396 return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, 3397 getPaddingType(Align, OrigOffset)); 3398 } 3399 3400 // Treat an enum type as its underlying type. 3401 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3402 Ty = EnumTy->getDecl()->getIntegerType(); 3403 3404 if (Ty->isPromotableIntegerType()) 3405 return ABIArgInfo::getExtend(); 3406 3407 return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset)); 3408 } 3409 3410 llvm::Type* 3411 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { 3412 const RecordType *RT = RetTy->getAs<RecordType>(); 3413 SmallVector<llvm::Type*, 8> RTList; 3414 3415 if (RT && RT->isStructureOrClassType()) { 3416 const RecordDecl *RD = RT->getDecl(); 3417 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 3418 unsigned FieldCnt = Layout.getFieldCount(); 3419 3420 // N32/64 returns struct/classes in floating point registers if the 3421 // following conditions are met: 3422 // 1. The size of the struct/class is no larger than 128-bit. 3423 // 2. The struct/class has one or two fields all of which are floating 3424 // point types. 3425 // 3. The offset of the first field is zero (this follows what gcc does). 3426 // 3427 // Any other composite results are returned in integer registers. 3428 // 3429 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { 3430 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); 3431 for (; b != e; ++b) { 3432 const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); 3433 3434 if (!BT || !BT->isFloatingPoint()) 3435 break; 3436 3437 RTList.push_back(CGT.ConvertType(b->getType())); 3438 } 3439 3440 if (b == e) 3441 return llvm::StructType::get(getVMContext(), RTList, 3442 RD->hasAttr<PackedAttr>()); 3443 3444 RTList.clear(); 3445 } 3446 } 3447 3448 CoerceToIntArgs(Size, RTList); 3449 return llvm::StructType::get(getVMContext(), RTList); 3450 } 3451 3452 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { 3453 uint64_t Size = getContext().getTypeSize(RetTy); 3454 3455 if (RetTy->isVoidType() || Size == 0) 3456 return ABIArgInfo::getIgnore(); 3457 3458 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { 3459 if (Size <= 128) { 3460 if (RetTy->isAnyComplexType()) 3461 return ABIArgInfo::getDirect(); 3462 3463 // O32 returns integer vectors in registers. 3464 if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) 3465 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 3466 3467 if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) 3468 return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 3469 } 3470 3471 return ABIArgInfo::getIndirect(0); 3472 } 3473 3474 // Treat an enum type as its underlying type. 3475 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 3476 RetTy = EnumTy->getDecl()->getIntegerType(); 3477 3478 return (RetTy->isPromotableIntegerType() ? 3479 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 3480 } 3481 3482 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { 3483 ABIArgInfo &RetInfo = FI.getReturnInfo(); 3484 RetInfo = classifyReturnType(FI.getReturnType()); 3485 3486 // Check if a pointer to an aggregate is passed as a hidden argument. 3487 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; 3488 3489 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3490 it != ie; ++it) 3491 it->info = classifyArgumentType(it->type, Offset); 3492 } 3493 3494 llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3495 CodeGenFunction &CGF) const { 3496 llvm::Type *BP = CGF.Int8PtrTy; 3497 llvm::Type *BPP = CGF.Int8PtrPtrTy; 3498 3499 CGBuilderTy &Builder = CGF.Builder; 3500 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 3501 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 3502 int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; 3503 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 3504 llvm::Value *AddrTyped; 3505 unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0); 3506 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; 3507 3508 if (TypeAlign > MinABIStackAlignInBytes) { 3509 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); 3510 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); 3511 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); 3512 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); 3513 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); 3514 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); 3515 } 3516 else 3517 AddrTyped = Builder.CreateBitCast(Addr, PTy); 3518 3519 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); 3520 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); 3521 uint64_t Offset = 3522 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); 3523 llvm::Value *NextAddr = 3524 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), 3525 "ap.next"); 3526 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 3527 3528 return AddrTyped; 3529 } 3530 3531 bool 3532 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 3533 llvm::Value *Address) const { 3534 // This information comes from gcc's implementation, which seems to 3535 // as canonical as it gets. 3536 3537 // Everything on MIPS is 4 bytes. Double-precision FP registers 3538 // are aliased to pairs of single-precision FP registers. 3539 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 3540 3541 // 0-31 are the general purpose registers, $0 - $31. 3542 // 32-63 are the floating-point registers, $f0 - $f31. 3543 // 64 and 65 are the multiply/divide registers, $hi and $lo. 3544 // 66 is the (notional, I think) register for signal-handler return. 3545 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); 3546 3547 // 67-74 are the floating-point status registers, $fcc0 - $fcc7. 3548 // They are one bit wide and ignored here. 3549 3550 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. 3551 // (coprocessor 1 is the FP unit) 3552 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. 3553 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. 3554 // 176-181 are the DSP accumulator registers. 3555 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); 3556 return false; 3557 } 3558 3559 //===----------------------------------------------------------------------===// 3560 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. 3561 // Currently subclassed only to implement custom OpenCL C function attribute 3562 // handling. 3563 //===----------------------------------------------------------------------===// 3564 3565 namespace { 3566 3567 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { 3568 public: 3569 TCETargetCodeGenInfo(CodeGenTypes &CGT) 3570 : DefaultTargetCodeGenInfo(CGT) {} 3571 3572 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 3573 CodeGen::CodeGenModule &M) const; 3574 }; 3575 3576 void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, 3577 llvm::GlobalValue *GV, 3578 CodeGen::CodeGenModule &M) const { 3579 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 3580 if (!FD) return; 3581 3582 llvm::Function *F = cast<llvm::Function>(GV); 3583 3584 if (M.getLangOpts().OpenCL) { 3585 if (FD->hasAttr<OpenCLKernelAttr>()) { 3586 // OpenCL C Kernel functions are not subject to inlining 3587 F->addFnAttr(llvm::Attribute::NoInline); 3588 3589 if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) { 3590 3591 // Convert the reqd_work_group_size() attributes to metadata. 3592 llvm::LLVMContext &Context = F->getContext(); 3593 llvm::NamedMDNode *OpenCLMetadata = 3594 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); 3595 3596 SmallVector<llvm::Value*, 5> Operands; 3597 Operands.push_back(F); 3598 3599 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 3600 llvm::APInt(32, 3601 FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim()))); 3602 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 3603 llvm::APInt(32, 3604 FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim()))); 3605 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 3606 llvm::APInt(32, 3607 FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim()))); 3608 3609 // Add a boolean constant operand for "required" (true) or "hint" (false) 3610 // for implementing the work_group_size_hint attr later. Currently 3611 // always true as the hint is not yet implemented. 3612 Operands.push_back(llvm::ConstantInt::getTrue(Context)); 3613 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); 3614 } 3615 } 3616 } 3617 } 3618 3619 } 3620 3621 //===----------------------------------------------------------------------===// 3622 // Hexagon ABI Implementation 3623 //===----------------------------------------------------------------------===// 3624 3625 namespace { 3626 3627 class HexagonABIInfo : public ABIInfo { 3628 3629 3630 public: 3631 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 3632 3633 private: 3634 3635 ABIArgInfo classifyReturnType(QualType RetTy) const; 3636 ABIArgInfo classifyArgumentType(QualType RetTy) const; 3637 3638 virtual void computeInfo(CGFunctionInfo &FI) const; 3639 3640 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3641 CodeGenFunction &CGF) const; 3642 }; 3643 3644 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { 3645 public: 3646 HexagonTargetCodeGenInfo(CodeGenTypes &CGT) 3647 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} 3648 3649 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 3650 return 29; 3651 } 3652 }; 3653 3654 } 3655 3656 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { 3657 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 3658 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 3659 it != ie; ++it) 3660 it->info = classifyArgumentType(it->type); 3661 } 3662 3663 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { 3664 if (!isAggregateTypeForABI(Ty)) { 3665 // Treat an enum type as its underlying type. 3666 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 3667 Ty = EnumTy->getDecl()->getIntegerType(); 3668 3669 return (Ty->isPromotableIntegerType() ? 3670 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 3671 } 3672 3673 // Ignore empty records. 3674 if (isEmptyRecord(getContext(), Ty, true)) 3675 return ABIArgInfo::getIgnore(); 3676 3677 // Structures with either a non-trivial destructor or a non-trivial 3678 // copy constructor are always indirect. 3679 if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) 3680 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 3681 3682 uint64_t Size = getContext().getTypeSize(Ty); 3683 if (Size > 64) 3684 return ABIArgInfo::getIndirect(0, /*ByVal=*/true); 3685 // Pass in the smallest viable integer type. 3686 else if (Size > 32) 3687 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 3688 else if (Size > 16) 3689 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 3690 else if (Size > 8) 3691 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 3692 else 3693 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 3694 } 3695 3696 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { 3697 if (RetTy->isVoidType()) 3698 return ABIArgInfo::getIgnore(); 3699 3700 // Large vector types should be returned via memory. 3701 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) 3702 return ABIArgInfo::getIndirect(0); 3703 3704 if (!isAggregateTypeForABI(RetTy)) { 3705 // Treat an enum type as its underlying type. 3706 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 3707 RetTy = EnumTy->getDecl()->getIntegerType(); 3708 3709 return (RetTy->isPromotableIntegerType() ? 3710 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 3711 } 3712 3713 // Structures with either a non-trivial destructor or a non-trivial 3714 // copy constructor are always indirect. 3715 if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) 3716 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 3717 3718 if (isEmptyRecord(getContext(), RetTy, true)) 3719 return ABIArgInfo::getIgnore(); 3720 3721 // Aggregates <= 8 bytes are returned in r0; other aggregates 3722 // are returned indirectly. 3723 uint64_t Size = getContext().getTypeSize(RetTy); 3724 if (Size <= 64) { 3725 // Return in the smallest viable integer type. 3726 if (Size <= 8) 3727 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 3728 if (Size <= 16) 3729 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 3730 if (Size <= 32) 3731 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 3732 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 3733 } 3734 3735 return ABIArgInfo::getIndirect(0, /*ByVal=*/true); 3736 } 3737 3738 llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 3739 CodeGenFunction &CGF) const { 3740 // FIXME: Need to handle alignment 3741 llvm::Type *BPP = CGF.Int8PtrPtrTy; 3742 3743 CGBuilderTy &Builder = CGF.Builder; 3744 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 3745 "ap"); 3746 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 3747 llvm::Type *PTy = 3748 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 3749 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 3750 3751 uint64_t Offset = 3752 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); 3753 llvm::Value *NextAddr = 3754 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 3755 "ap.next"); 3756 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 3757 3758 return AddrTyped; 3759 } 3760 3761 3762 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { 3763 if (TheTargetCodeGenInfo) 3764 return *TheTargetCodeGenInfo; 3765 3766 const llvm::Triple &Triple = getContext().getTargetInfo().getTriple(); 3767 switch (Triple.getArch()) { 3768 default: 3769 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); 3770 3771 case llvm::Triple::mips: 3772 case llvm::Triple::mipsel: 3773 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); 3774 3775 case llvm::Triple::mips64: 3776 case llvm::Triple::mips64el: 3777 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); 3778 3779 case llvm::Triple::arm: 3780 case llvm::Triple::thumb: 3781 { 3782 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; 3783 3784 if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0) 3785 Kind = ARMABIInfo::APCS; 3786 else if (CodeGenOpts.FloatABI == "hard") 3787 Kind = ARMABIInfo::AAPCS_VFP; 3788 3789 return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); 3790 } 3791 3792 case llvm::Triple::ppc: 3793 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); 3794 case llvm::Triple::ppc64: 3795 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); 3796 3797 case llvm::Triple::nvptx: 3798 case llvm::Triple::nvptx64: 3799 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); 3800 3801 case llvm::Triple::mblaze: 3802 return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types)); 3803 3804 case llvm::Triple::msp430: 3805 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); 3806 3807 case llvm::Triple::tce: 3808 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); 3809 3810 case llvm::Triple::x86: { 3811 bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0; 3812 3813 if (Triple.isOSDarwin()) 3814 return *(TheTargetCodeGenInfo = 3815 new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false, 3816 CodeGenOpts.NumRegisterParameters)); 3817 3818 switch (Triple.getOS()) { 3819 case llvm::Triple::Cygwin: 3820 case llvm::Triple::MinGW32: 3821 case llvm::Triple::AuroraUX: 3822 case llvm::Triple::DragonFly: 3823 case llvm::Triple::FreeBSD: 3824 case llvm::Triple::OpenBSD: 3825 case llvm::Triple::Bitrig: 3826 return *(TheTargetCodeGenInfo = 3827 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, 3828 false, 3829 CodeGenOpts.NumRegisterParameters)); 3830 3831 case llvm::Triple::Win32: 3832 return *(TheTargetCodeGenInfo = 3833 new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true, 3834 CodeGenOpts.NumRegisterParameters)); 3835 3836 default: 3837 return *(TheTargetCodeGenInfo = 3838 new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX, 3839 false, 3840 CodeGenOpts.NumRegisterParameters)); 3841 } 3842 } 3843 3844 case llvm::Triple::x86_64: { 3845 bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0; 3846 3847 switch (Triple.getOS()) { 3848 case llvm::Triple::Win32: 3849 case llvm::Triple::MinGW32: 3850 case llvm::Triple::Cygwin: 3851 return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); 3852 default: 3853 return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, 3854 HasAVX)); 3855 } 3856 } 3857 case llvm::Triple::hexagon: 3858 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); 3859 } 3860 } 3861