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