clang API Documentation
00001 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // These classes wrap the information about a call or function 00011 // definition used to handle ABI compliancy. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "TargetInfo.h" 00016 #include "ABIInfo.h" 00017 #include "CGCXXABI.h" 00018 #include "CGValue.h" 00019 #include "CodeGenFunction.h" 00020 #include "clang/AST/RecordLayout.h" 00021 #include "clang/CodeGen/CGFunctionInfo.h" 00022 #include "clang/Frontend/CodeGenOptions.h" 00023 #include "llvm/ADT/Triple.h" 00024 #include "llvm/IR/DataLayout.h" 00025 #include "llvm/IR/Type.h" 00026 #include "llvm/Support/raw_ostream.h" 00027 00028 #include <algorithm> // std::sort 00029 00030 using namespace clang; 00031 using namespace CodeGen; 00032 00033 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, 00034 llvm::Value *Array, 00035 llvm::Value *Value, 00036 unsigned FirstIndex, 00037 unsigned LastIndex) { 00038 // Alternatively, we could emit this as a loop in the source. 00039 for (unsigned I = FirstIndex; I <= LastIndex; ++I) { 00040 llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); 00041 Builder.CreateStore(Value, Cell); 00042 } 00043 } 00044 00045 static bool isAggregateTypeForABI(QualType T) { 00046 return !CodeGenFunction::hasScalarEvaluationKind(T) || 00047 T->isMemberFunctionPointerType(); 00048 } 00049 00050 ABIInfo::~ABIInfo() {} 00051 00052 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, 00053 CGCXXABI &CXXABI) { 00054 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); 00055 if (!RD) 00056 return CGCXXABI::RAA_Default; 00057 return CXXABI.getRecordArgABI(RD); 00058 } 00059 00060 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, 00061 CGCXXABI &CXXABI) { 00062 const RecordType *RT = T->getAs<RecordType>(); 00063 if (!RT) 00064 return CGCXXABI::RAA_Default; 00065 return getRecordArgABI(RT, CXXABI); 00066 } 00067 00068 /// Pass transparent unions as if they were the type of the first element. Sema 00069 /// should ensure that all elements of the union have the same "machine type". 00070 static QualType useFirstFieldIfTransparentUnion(QualType Ty) { 00071 if (const RecordType *UT = Ty->getAsUnionType()) { 00072 const RecordDecl *UD = UT->getDecl(); 00073 if (UD->hasAttr<TransparentUnionAttr>()) { 00074 assert(!UD->field_empty() && "sema created an empty transparent union"); 00075 return UD->field_begin()->getType(); 00076 } 00077 } 00078 return Ty; 00079 } 00080 00081 CGCXXABI &ABIInfo::getCXXABI() const { 00082 return CGT.getCXXABI(); 00083 } 00084 00085 ASTContext &ABIInfo::getContext() const { 00086 return CGT.getContext(); 00087 } 00088 00089 llvm::LLVMContext &ABIInfo::getVMContext() const { 00090 return CGT.getLLVMContext(); 00091 } 00092 00093 const llvm::DataLayout &ABIInfo::getDataLayout() const { 00094 return CGT.getDataLayout(); 00095 } 00096 00097 const TargetInfo &ABIInfo::getTarget() const { 00098 return CGT.getTarget(); 00099 } 00100 00101 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 00102 return false; 00103 } 00104 00105 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 00106 uint64_t Members) const { 00107 return false; 00108 } 00109 00110 void ABIArgInfo::dump() const { 00111 raw_ostream &OS = llvm::errs(); 00112 OS << "(ABIArgInfo Kind="; 00113 switch (TheKind) { 00114 case Direct: 00115 OS << "Direct Type="; 00116 if (llvm::Type *Ty = getCoerceToType()) 00117 Ty->print(OS); 00118 else 00119 OS << "null"; 00120 break; 00121 case Extend: 00122 OS << "Extend"; 00123 break; 00124 case Ignore: 00125 OS << "Ignore"; 00126 break; 00127 case InAlloca: 00128 OS << "InAlloca Offset=" << getInAllocaFieldIndex(); 00129 break; 00130 case Indirect: 00131 OS << "Indirect Align=" << getIndirectAlign() 00132 << " ByVal=" << getIndirectByVal() 00133 << " Realign=" << getIndirectRealign(); 00134 break; 00135 case Expand: 00136 OS << "Expand"; 00137 break; 00138 } 00139 OS << ")\n"; 00140 } 00141 00142 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } 00143 00144 // If someone can figure out a general rule for this, that would be great. 00145 // It's probably just doomed to be platform-dependent, though. 00146 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { 00147 // Verified for: 00148 // x86-64 FreeBSD, Linux, Darwin 00149 // x86-32 FreeBSD, Linux, Darwin 00150 // PowerPC Linux, Darwin 00151 // ARM Darwin (*not* EABI) 00152 // AArch64 Linux 00153 return 32; 00154 } 00155 00156 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, 00157 const FunctionNoProtoType *fnType) const { 00158 // The following conventions are known to require this to be false: 00159 // x86_stdcall 00160 // MIPS 00161 // For everything else, we just prefer false unless we opt out. 00162 return false; 00163 } 00164 00165 void 00166 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, 00167 llvm::SmallString<24> &Opt) const { 00168 // This assumes the user is passing a library name like "rt" instead of a 00169 // filename like "librt.a/so", and that they don't care whether it's static or 00170 // dynamic. 00171 Opt = "-l"; 00172 Opt += Lib; 00173 } 00174 00175 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); 00176 00177 /// isEmptyField - Return true iff a the field is "empty", that is it 00178 /// is an unnamed bit-field or an (array of) empty record(s). 00179 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, 00180 bool AllowArrays) { 00181 if (FD->isUnnamedBitfield()) 00182 return true; 00183 00184 QualType FT = FD->getType(); 00185 00186 // Constant arrays of empty records count as empty, strip them off. 00187 // Constant arrays of zero length always count as empty. 00188 if (AllowArrays) 00189 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 00190 if (AT->getSize() == 0) 00191 return true; 00192 FT = AT->getElementType(); 00193 } 00194 00195 const RecordType *RT = FT->getAs<RecordType>(); 00196 if (!RT) 00197 return false; 00198 00199 // C++ record fields are never empty, at least in the Itanium ABI. 00200 // 00201 // FIXME: We should use a predicate for whether this behavior is true in the 00202 // current ABI. 00203 if (isa<CXXRecordDecl>(RT->getDecl())) 00204 return false; 00205 00206 return isEmptyRecord(Context, FT, AllowArrays); 00207 } 00208 00209 /// isEmptyRecord - Return true iff a structure contains only empty 00210 /// fields. Note that a structure with a flexible array member is not 00211 /// considered empty. 00212 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { 00213 const RecordType *RT = T->getAs<RecordType>(); 00214 if (!RT) 00215 return 0; 00216 const RecordDecl *RD = RT->getDecl(); 00217 if (RD->hasFlexibleArrayMember()) 00218 return false; 00219 00220 // If this is a C++ record, check the bases first. 00221 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 00222 for (const auto &I : CXXRD->bases()) 00223 if (!isEmptyRecord(Context, I.getType(), true)) 00224 return false; 00225 00226 for (const auto *I : RD->fields()) 00227 if (!isEmptyField(Context, I, AllowArrays)) 00228 return false; 00229 return true; 00230 } 00231 00232 /// isSingleElementStruct - Determine if a structure is a "single 00233 /// element struct", i.e. it has exactly one non-empty field or 00234 /// exactly one field which is itself a single element 00235 /// struct. Structures with flexible array members are never 00236 /// considered single element structs. 00237 /// 00238 /// \return The field declaration for the single non-empty field, if 00239 /// it exists. 00240 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { 00241 const RecordType *RT = T->getAsStructureType(); 00242 if (!RT) 00243 return nullptr; 00244 00245 const RecordDecl *RD = RT->getDecl(); 00246 if (RD->hasFlexibleArrayMember()) 00247 return nullptr; 00248 00249 const Type *Found = nullptr; 00250 00251 // If this is a C++ record, check the bases first. 00252 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 00253 for (const auto &I : CXXRD->bases()) { 00254 // Ignore empty records. 00255 if (isEmptyRecord(Context, I.getType(), true)) 00256 continue; 00257 00258 // If we already found an element then this isn't a single-element struct. 00259 if (Found) 00260 return nullptr; 00261 00262 // If this is non-empty and not a single element struct, the composite 00263 // cannot be a single element struct. 00264 Found = isSingleElementStruct(I.getType(), Context); 00265 if (!Found) 00266 return nullptr; 00267 } 00268 } 00269 00270 // Check for single element. 00271 for (const auto *FD : RD->fields()) { 00272 QualType FT = FD->getType(); 00273 00274 // Ignore empty fields. 00275 if (isEmptyField(Context, FD, true)) 00276 continue; 00277 00278 // If we already found an element then this isn't a single-element 00279 // struct. 00280 if (Found) 00281 return nullptr; 00282 00283 // Treat single element arrays as the element. 00284 while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { 00285 if (AT->getSize().getZExtValue() != 1) 00286 break; 00287 FT = AT->getElementType(); 00288 } 00289 00290 if (!isAggregateTypeForABI(FT)) { 00291 Found = FT.getTypePtr(); 00292 } else { 00293 Found = isSingleElementStruct(FT, Context); 00294 if (!Found) 00295 return nullptr; 00296 } 00297 } 00298 00299 // We don't consider a struct a single-element struct if it has 00300 // padding beyond the element type. 00301 if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) 00302 return nullptr; 00303 00304 return Found; 00305 } 00306 00307 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { 00308 // Treat complex types as the element type. 00309 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 00310 Ty = CTy->getElementType(); 00311 00312 // Check for a type which we know has a simple scalar argument-passing 00313 // convention without any padding. (We're specifically looking for 32 00314 // and 64-bit integer and integer-equivalents, float, and double.) 00315 if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && 00316 !Ty->isEnumeralType() && !Ty->isBlockPointerType()) 00317 return false; 00318 00319 uint64_t Size = Context.getTypeSize(Ty); 00320 return Size == 32 || Size == 64; 00321 } 00322 00323 /// canExpandIndirectArgument - Test whether an argument type which is to be 00324 /// passed indirectly (on the stack) would have the equivalent layout if it was 00325 /// expanded into separate arguments. If so, we prefer to do the latter to avoid 00326 /// inhibiting optimizations. 00327 /// 00328 // FIXME: This predicate is missing many cases, currently it just follows 00329 // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We 00330 // should probably make this smarter, or better yet make the LLVM backend 00331 // capable of handling it. 00332 static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { 00333 // We can only expand structure types. 00334 const RecordType *RT = Ty->getAs<RecordType>(); 00335 if (!RT) 00336 return false; 00337 00338 // We can only expand (C) structures. 00339 // 00340 // FIXME: This needs to be generalized to handle classes as well. 00341 const RecordDecl *RD = RT->getDecl(); 00342 if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) 00343 return false; 00344 00345 uint64_t Size = 0; 00346 00347 for (const auto *FD : RD->fields()) { 00348 if (!is32Or64BitBasicType(FD->getType(), Context)) 00349 return false; 00350 00351 // FIXME: Reject bit-fields wholesale; there are two problems, we don't know 00352 // how to expand them yet, and the predicate for telling if a bitfield still 00353 // counts as "basic" is more complicated than what we were doing previously. 00354 if (FD->isBitField()) 00355 return false; 00356 00357 Size += Context.getTypeSize(FD->getType()); 00358 } 00359 00360 // Make sure there are not any holes in the struct. 00361 if (Size != Context.getTypeSize(Ty)) 00362 return false; 00363 00364 return true; 00365 } 00366 00367 namespace { 00368 /// DefaultABIInfo - The default implementation for ABI specific 00369 /// details. This implementation provides information which results in 00370 /// self-consistent and sensible LLVM IR generation, but does not 00371 /// conform to any particular ABI. 00372 class DefaultABIInfo : public ABIInfo { 00373 public: 00374 DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 00375 00376 ABIArgInfo classifyReturnType(QualType RetTy) const; 00377 ABIArgInfo classifyArgumentType(QualType RetTy) const; 00378 00379 void computeInfo(CGFunctionInfo &FI) const override { 00380 if (!getCXXABI().classifyReturnType(FI)) 00381 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 00382 for (auto &I : FI.arguments()) 00383 I.info = classifyArgumentType(I.type); 00384 } 00385 00386 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 00387 CodeGenFunction &CGF) const override; 00388 }; 00389 00390 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { 00391 public: 00392 DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 00393 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 00394 }; 00395 00396 llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 00397 CodeGenFunction &CGF) const { 00398 return nullptr; 00399 } 00400 00401 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { 00402 if (isAggregateTypeForABI(Ty)) 00403 return ABIArgInfo::getIndirect(0); 00404 00405 // Treat an enum type as its underlying type. 00406 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 00407 Ty = EnumTy->getDecl()->getIntegerType(); 00408 00409 return (Ty->isPromotableIntegerType() ? 00410 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 00411 } 00412 00413 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { 00414 if (RetTy->isVoidType()) 00415 return ABIArgInfo::getIgnore(); 00416 00417 if (isAggregateTypeForABI(RetTy)) 00418 return ABIArgInfo::getIndirect(0); 00419 00420 // Treat an enum type as its underlying type. 00421 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 00422 RetTy = EnumTy->getDecl()->getIntegerType(); 00423 00424 return (RetTy->isPromotableIntegerType() ? 00425 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 00426 } 00427 00428 //===----------------------------------------------------------------------===// 00429 // le32/PNaCl bitcode ABI Implementation 00430 // 00431 // This is a simplified version of the x86_32 ABI. Arguments and return values 00432 // are always passed on the stack. 00433 //===----------------------------------------------------------------------===// 00434 00435 class PNaClABIInfo : public ABIInfo { 00436 public: 00437 PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 00438 00439 ABIArgInfo classifyReturnType(QualType RetTy) const; 00440 ABIArgInfo classifyArgumentType(QualType RetTy) const; 00441 00442 void computeInfo(CGFunctionInfo &FI) const override; 00443 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 00444 CodeGenFunction &CGF) const override; 00445 }; 00446 00447 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { 00448 public: 00449 PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) 00450 : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} 00451 }; 00452 00453 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { 00454 if (!getCXXABI().classifyReturnType(FI)) 00455 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 00456 00457 for (auto &I : FI.arguments()) 00458 I.info = classifyArgumentType(I.type); 00459 } 00460 00461 llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 00462 CodeGenFunction &CGF) const { 00463 return nullptr; 00464 } 00465 00466 /// \brief Classify argument of given type \p Ty. 00467 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { 00468 if (isAggregateTypeForABI(Ty)) { 00469 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 00470 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 00471 return ABIArgInfo::getIndirect(0); 00472 } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 00473 // Treat an enum type as its underlying type. 00474 Ty = EnumTy->getDecl()->getIntegerType(); 00475 } else if (Ty->isFloatingType()) { 00476 // Floating-point types don't go inreg. 00477 return ABIArgInfo::getDirect(); 00478 } 00479 00480 return (Ty->isPromotableIntegerType() ? 00481 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 00482 } 00483 00484 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { 00485 if (RetTy->isVoidType()) 00486 return ABIArgInfo::getIgnore(); 00487 00488 // In the PNaCl ABI we always return records/structures on the stack. 00489 if (isAggregateTypeForABI(RetTy)) 00490 return ABIArgInfo::getIndirect(0); 00491 00492 // Treat an enum type as its underlying type. 00493 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 00494 RetTy = EnumTy->getDecl()->getIntegerType(); 00495 00496 return (RetTy->isPromotableIntegerType() ? 00497 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 00498 } 00499 00500 /// IsX86_MMXType - Return true if this is an MMX type. 00501 bool IsX86_MMXType(llvm::Type *IRType) { 00502 // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. 00503 return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && 00504 cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && 00505 IRType->getScalarSizeInBits() != 64; 00506 } 00507 00508 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 00509 StringRef Constraint, 00510 llvm::Type* Ty) { 00511 if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { 00512 if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { 00513 // Invalid MMX constraint 00514 return nullptr; 00515 } 00516 00517 return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); 00518 } 00519 00520 // No operation needed 00521 return Ty; 00522 } 00523 00524 /// Returns true if this type can be passed in SSE registers with the 00525 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. 00526 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { 00527 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 00528 if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) 00529 return true; 00530 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 00531 // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX 00532 // registers specially. 00533 unsigned VecSize = Context.getTypeSize(VT); 00534 if (VecSize == 128 || VecSize == 256 || VecSize == 512) 00535 return true; 00536 } 00537 return false; 00538 } 00539 00540 /// Returns true if this aggregate is small enough to be passed in SSE registers 00541 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. 00542 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { 00543 return NumMembers <= 4; 00544 } 00545 00546 //===----------------------------------------------------------------------===// 00547 // X86-32 ABI Implementation 00548 //===----------------------------------------------------------------------===// 00549 00550 /// \brief Similar to llvm::CCState, but for Clang. 00551 struct CCState { 00552 CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} 00553 00554 unsigned CC; 00555 unsigned FreeRegs; 00556 unsigned FreeSSERegs; 00557 }; 00558 00559 /// X86_32ABIInfo - The X86-32 ABI information. 00560 class X86_32ABIInfo : public ABIInfo { 00561 enum Class { 00562 Integer, 00563 Float 00564 }; 00565 00566 static const unsigned MinABIStackAlignInBytes = 4; 00567 00568 bool IsDarwinVectorABI; 00569 bool IsSmallStructInRegABI; 00570 bool IsWin32StructABI; 00571 unsigned DefaultNumRegisterParameters; 00572 00573 static bool isRegisterSize(unsigned Size) { 00574 return (Size == 8 || Size == 16 || Size == 32 || Size == 64); 00575 } 00576 00577 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 00578 // FIXME: Assumes vectorcall is in use. 00579 return isX86VectorTypeForVectorCall(getContext(), Ty); 00580 } 00581 00582 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 00583 uint64_t NumMembers) const override { 00584 // FIXME: Assumes vectorcall is in use. 00585 return isX86VectorCallAggregateSmallEnough(NumMembers); 00586 } 00587 00588 bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; 00589 00590 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 00591 /// such that the argument will be passed in memory. 00592 ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; 00593 00594 ABIArgInfo getIndirectReturnResult(CCState &State) const; 00595 00596 /// \brief Return the alignment to use for the given type on the stack. 00597 unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; 00598 00599 Class classify(QualType Ty) const; 00600 ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; 00601 ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; 00602 bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; 00603 00604 /// \brief Rewrite the function info so that all memory arguments use 00605 /// inalloca. 00606 void rewriteWithInAlloca(CGFunctionInfo &FI) const; 00607 00608 void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 00609 unsigned &StackOffset, ABIArgInfo &Info, 00610 QualType Type) const; 00611 00612 public: 00613 00614 void computeInfo(CGFunctionInfo &FI) const override; 00615 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 00616 CodeGenFunction &CGF) const override; 00617 00618 X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, 00619 unsigned r) 00620 : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), 00621 IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} 00622 }; 00623 00624 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { 00625 public: 00626 X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 00627 bool d, bool p, bool w, unsigned r) 00628 :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} 00629 00630 static bool isStructReturnInRegABI( 00631 const llvm::Triple &Triple, const CodeGenOptions &Opts); 00632 00633 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 00634 CodeGen::CodeGenModule &CGM) const override; 00635 00636 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 00637 // Darwin uses different dwarf register numbers for EH. 00638 if (CGM.getTarget().getTriple().isOSDarwin()) return 5; 00639 return 4; 00640 } 00641 00642 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 00643 llvm::Value *Address) const override; 00644 00645 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 00646 StringRef Constraint, 00647 llvm::Type* Ty) const override { 00648 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 00649 } 00650 00651 void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, 00652 std::string &Constraints, 00653 std::vector<llvm::Type *> &ResultRegTypes, 00654 std::vector<llvm::Type *> &ResultTruncRegTypes, 00655 std::vector<LValue> &ResultRegDests, 00656 std::string &AsmString, 00657 unsigned NumOutputs) const override; 00658 00659 llvm::Constant * 00660 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 00661 unsigned Sig = (0xeb << 0) | // jmp rel8 00662 (0x06 << 8) | // .+0x08 00663 ('F' << 16) | 00664 ('T' << 24); 00665 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 00666 } 00667 00668 }; 00669 00670 } 00671 00672 /// Rewrite input constraint references after adding some output constraints. 00673 /// In the case where there is one output and one input and we add one output, 00674 /// we need to replace all operand references greater than or equal to 1: 00675 /// mov $0, $1 00676 /// mov eax, $1 00677 /// The result will be: 00678 /// mov $0, $2 00679 /// mov eax, $2 00680 static void rewriteInputConstraintReferences(unsigned FirstIn, 00681 unsigned NumNewOuts, 00682 std::string &AsmString) { 00683 std::string Buf; 00684 llvm::raw_string_ostream OS(Buf); 00685 size_t Pos = 0; 00686 while (Pos < AsmString.size()) { 00687 size_t DollarStart = AsmString.find('$', Pos); 00688 if (DollarStart == std::string::npos) 00689 DollarStart = AsmString.size(); 00690 size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); 00691 if (DollarEnd == std::string::npos) 00692 DollarEnd = AsmString.size(); 00693 OS << StringRef(&AsmString[Pos], DollarEnd - Pos); 00694 Pos = DollarEnd; 00695 size_t NumDollars = DollarEnd - DollarStart; 00696 if (NumDollars % 2 != 0 && Pos < AsmString.size()) { 00697 // We have an operand reference. 00698 size_t DigitStart = Pos; 00699 size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); 00700 if (DigitEnd == std::string::npos) 00701 DigitEnd = AsmString.size(); 00702 StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); 00703 unsigned OperandIndex; 00704 if (!OperandStr.getAsInteger(10, OperandIndex)) { 00705 if (OperandIndex >= FirstIn) 00706 OperandIndex += NumNewOuts; 00707 OS << OperandIndex; 00708 } else { 00709 OS << OperandStr; 00710 } 00711 Pos = DigitEnd; 00712 } 00713 } 00714 AsmString = std::move(OS.str()); 00715 } 00716 00717 /// Add output constraints for EAX:EDX because they are return registers. 00718 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( 00719 CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, 00720 std::vector<llvm::Type *> &ResultRegTypes, 00721 std::vector<llvm::Type *> &ResultTruncRegTypes, 00722 std::vector<LValue> &ResultRegDests, std::string &AsmString, 00723 unsigned NumOutputs) const { 00724 uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); 00725 00726 // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is 00727 // larger. 00728 if (!Constraints.empty()) 00729 Constraints += ','; 00730 if (RetWidth <= 32) { 00731 Constraints += "={eax}"; 00732 ResultRegTypes.push_back(CGF.Int32Ty); 00733 } else { 00734 // Use the 'A' constraint for EAX:EDX. 00735 Constraints += "=A"; 00736 ResultRegTypes.push_back(CGF.Int64Ty); 00737 } 00738 00739 // Truncate EAX or EAX:EDX to an integer of the appropriate size. 00740 llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); 00741 ResultTruncRegTypes.push_back(CoerceTy); 00742 00743 // Coerce the integer by bitcasting the return slot pointer. 00744 ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), 00745 CoerceTy->getPointerTo())); 00746 ResultRegDests.push_back(ReturnSlot); 00747 00748 rewriteInputConstraintReferences(NumOutputs, 1, AsmString); 00749 } 00750 00751 /// shouldReturnTypeInRegister - Determine if the given type should be 00752 /// passed in a register (for the Darwin ABI). 00753 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, 00754 ASTContext &Context) const { 00755 uint64_t Size = Context.getTypeSize(Ty); 00756 00757 // Type must be register sized. 00758 if (!isRegisterSize(Size)) 00759 return false; 00760 00761 if (Ty->isVectorType()) { 00762 // 64- and 128- bit vectors inside structures are not returned in 00763 // registers. 00764 if (Size == 64 || Size == 128) 00765 return false; 00766 00767 return true; 00768 } 00769 00770 // If this is a builtin, pointer, enum, complex type, member pointer, or 00771 // member function pointer it is ok. 00772 if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || 00773 Ty->isAnyComplexType() || Ty->isEnumeralType() || 00774 Ty->isBlockPointerType() || Ty->isMemberPointerType()) 00775 return true; 00776 00777 // Arrays are treated like records. 00778 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) 00779 return shouldReturnTypeInRegister(AT->getElementType(), Context); 00780 00781 // Otherwise, it must be a record type. 00782 const RecordType *RT = Ty->getAs<RecordType>(); 00783 if (!RT) return false; 00784 00785 // FIXME: Traverse bases here too. 00786 00787 // Structure types are passed in register if all fields would be 00788 // passed in a register. 00789 for (const auto *FD : RT->getDecl()->fields()) { 00790 // Empty fields are ignored. 00791 if (isEmptyField(Context, FD, true)) 00792 continue; 00793 00794 // Check fields recursively. 00795 if (!shouldReturnTypeInRegister(FD->getType(), Context)) 00796 return false; 00797 } 00798 return true; 00799 } 00800 00801 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { 00802 // If the return value is indirect, then the hidden argument is consuming one 00803 // integer register. 00804 if (State.FreeRegs) { 00805 --State.FreeRegs; 00806 return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); 00807 } 00808 return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); 00809 } 00810 00811 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State) const { 00812 if (RetTy->isVoidType()) 00813 return ABIArgInfo::getIgnore(); 00814 00815 const Type *Base = nullptr; 00816 uint64_t NumElts = 0; 00817 if (State.CC == llvm::CallingConv::X86_VectorCall && 00818 isHomogeneousAggregate(RetTy, Base, NumElts)) { 00819 // The LLVM struct type for such an aggregate should lower properly. 00820 return ABIArgInfo::getDirect(); 00821 } 00822 00823 if (const VectorType *VT = RetTy->getAs<VectorType>()) { 00824 // On Darwin, some vectors are returned in registers. 00825 if (IsDarwinVectorABI) { 00826 uint64_t Size = getContext().getTypeSize(RetTy); 00827 00828 // 128-bit vectors are a special case; they are returned in 00829 // registers and we need to make sure to pick a type the LLVM 00830 // backend will like. 00831 if (Size == 128) 00832 return ABIArgInfo::getDirect(llvm::VectorType::get( 00833 llvm::Type::getInt64Ty(getVMContext()), 2)); 00834 00835 // Always return in register if it fits in a general purpose 00836 // register, or if it is 64 bits and has a single element. 00837 if ((Size == 8 || Size == 16 || Size == 32) || 00838 (Size == 64 && VT->getNumElements() == 1)) 00839 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 00840 Size)); 00841 00842 return getIndirectReturnResult(State); 00843 } 00844 00845 return ABIArgInfo::getDirect(); 00846 } 00847 00848 if (isAggregateTypeForABI(RetTy)) { 00849 if (const RecordType *RT = RetTy->getAs<RecordType>()) { 00850 // Structures with flexible arrays are always indirect. 00851 if (RT->getDecl()->hasFlexibleArrayMember()) 00852 return getIndirectReturnResult(State); 00853 } 00854 00855 // If specified, structs and unions are always indirect. 00856 if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) 00857 return getIndirectReturnResult(State); 00858 00859 // Small structures which are register sized are generally returned 00860 // in a register. 00861 if (shouldReturnTypeInRegister(RetTy, getContext())) { 00862 uint64_t Size = getContext().getTypeSize(RetTy); 00863 00864 // As a special-case, if the struct is a "single-element" struct, and 00865 // the field is of type "float" or "double", return it in a 00866 // floating-point register. (MSVC does not apply this special case.) 00867 // We apply a similar transformation for pointer types to improve the 00868 // quality of the generated IR. 00869 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) 00870 if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) 00871 || SeltTy->hasPointerRepresentation()) 00872 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); 00873 00874 // FIXME: We should be able to narrow this integer in cases with dead 00875 // padding. 00876 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); 00877 } 00878 00879 return getIndirectReturnResult(State); 00880 } 00881 00882 // Treat an enum type as its underlying type. 00883 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 00884 RetTy = EnumTy->getDecl()->getIntegerType(); 00885 00886 return (RetTy->isPromotableIntegerType() ? 00887 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 00888 } 00889 00890 static bool isSSEVectorType(ASTContext &Context, QualType Ty) { 00891 return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; 00892 } 00893 00894 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { 00895 const RecordType *RT = Ty->getAs<RecordType>(); 00896 if (!RT) 00897 return 0; 00898 const RecordDecl *RD = RT->getDecl(); 00899 00900 // If this is a C++ record, check the bases first. 00901 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 00902 for (const auto &I : CXXRD->bases()) 00903 if (!isRecordWithSSEVectorType(Context, I.getType())) 00904 return false; 00905 00906 for (const auto *i : RD->fields()) { 00907 QualType FT = i->getType(); 00908 00909 if (isSSEVectorType(Context, FT)) 00910 return true; 00911 00912 if (isRecordWithSSEVectorType(Context, FT)) 00913 return true; 00914 } 00915 00916 return false; 00917 } 00918 00919 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, 00920 unsigned Align) const { 00921 // Otherwise, if the alignment is less than or equal to the minimum ABI 00922 // alignment, just use the default; the backend will handle this. 00923 if (Align <= MinABIStackAlignInBytes) 00924 return 0; // Use default alignment. 00925 00926 // On non-Darwin, the stack type alignment is always 4. 00927 if (!IsDarwinVectorABI) { 00928 // Set explicit alignment, since we may need to realign the top. 00929 return MinABIStackAlignInBytes; 00930 } 00931 00932 // Otherwise, if the type contains an SSE vector type, the alignment is 16. 00933 if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || 00934 isRecordWithSSEVectorType(getContext(), Ty))) 00935 return 16; 00936 00937 return MinABIStackAlignInBytes; 00938 } 00939 00940 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, 00941 CCState &State) const { 00942 if (!ByVal) { 00943 if (State.FreeRegs) { 00944 --State.FreeRegs; // Non-byval indirects just use one pointer. 00945 return ABIArgInfo::getIndirectInReg(0, false); 00946 } 00947 return ABIArgInfo::getIndirect(0, false); 00948 } 00949 00950 // Compute the byval alignment. 00951 unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; 00952 unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); 00953 if (StackAlign == 0) 00954 return ABIArgInfo::getIndirect(4, /*ByVal=*/true); 00955 00956 // If the stack alignment is less than the type alignment, realign the 00957 // argument. 00958 bool Realign = TypeAlign > StackAlign; 00959 return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); 00960 } 00961 00962 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { 00963 const Type *T = isSingleElementStruct(Ty, getContext()); 00964 if (!T) 00965 T = Ty.getTypePtr(); 00966 00967 if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 00968 BuiltinType::Kind K = BT->getKind(); 00969 if (K == BuiltinType::Float || K == BuiltinType::Double) 00970 return Float; 00971 } 00972 return Integer; 00973 } 00974 00975 bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, 00976 bool &NeedsPadding) const { 00977 NeedsPadding = false; 00978 Class C = classify(Ty); 00979 if (C == Float) 00980 return false; 00981 00982 unsigned Size = getContext().getTypeSize(Ty); 00983 unsigned SizeInRegs = (Size + 31) / 32; 00984 00985 if (SizeInRegs == 0) 00986 return false; 00987 00988 if (SizeInRegs > State.FreeRegs) { 00989 State.FreeRegs = 0; 00990 return false; 00991 } 00992 00993 State.FreeRegs -= SizeInRegs; 00994 00995 if (State.CC == llvm::CallingConv::X86_FastCall || 00996 State.CC == llvm::CallingConv::X86_VectorCall) { 00997 if (Size > 32) 00998 return false; 00999 01000 if (Ty->isIntegralOrEnumerationType()) 01001 return true; 01002 01003 if (Ty->isPointerType()) 01004 return true; 01005 01006 if (Ty->isReferenceType()) 01007 return true; 01008 01009 if (State.FreeRegs) 01010 NeedsPadding = true; 01011 01012 return false; 01013 } 01014 01015 return true; 01016 } 01017 01018 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, 01019 CCState &State) const { 01020 // FIXME: Set alignment on indirect arguments. 01021 01022 Ty = useFirstFieldIfTransparentUnion(Ty); 01023 01024 // Check with the C++ ABI first. 01025 const RecordType *RT = Ty->getAs<RecordType>(); 01026 if (RT) { 01027 CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); 01028 if (RAA == CGCXXABI::RAA_Indirect) { 01029 return getIndirectResult(Ty, false, State); 01030 } else if (RAA == CGCXXABI::RAA_DirectInMemory) { 01031 // The field index doesn't matter, we'll fix it up later. 01032 return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); 01033 } 01034 } 01035 01036 // vectorcall adds the concept of a homogenous vector aggregate, similar 01037 // to other targets. 01038 const Type *Base = nullptr; 01039 uint64_t NumElts = 0; 01040 if (State.CC == llvm::CallingConv::X86_VectorCall && 01041 isHomogeneousAggregate(Ty, Base, NumElts)) { 01042 if (State.FreeSSERegs >= NumElts) { 01043 State.FreeSSERegs -= NumElts; 01044 if (Ty->isBuiltinType() || Ty->isVectorType()) 01045 return ABIArgInfo::getDirect(); 01046 return ABIArgInfo::getExpand(); 01047 } 01048 return getIndirectResult(Ty, /*ByVal=*/false, State); 01049 } 01050 01051 if (isAggregateTypeForABI(Ty)) { 01052 if (RT) { 01053 // Structs are always byval on win32, regardless of what they contain. 01054 if (IsWin32StructABI) 01055 return getIndirectResult(Ty, true, State); 01056 01057 // Structures with flexible arrays are always indirect. 01058 if (RT->getDecl()->hasFlexibleArrayMember()) 01059 return getIndirectResult(Ty, true, State); 01060 } 01061 01062 // Ignore empty structs/unions. 01063 if (isEmptyRecord(getContext(), Ty, true)) 01064 return ABIArgInfo::getIgnore(); 01065 01066 llvm::LLVMContext &LLVMContext = getVMContext(); 01067 llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); 01068 bool NeedsPadding; 01069 if (shouldUseInReg(Ty, State, NeedsPadding)) { 01070 unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; 01071 SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); 01072 llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); 01073 return ABIArgInfo::getDirectInReg(Result); 01074 } 01075 llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; 01076 01077 // Expand small (<= 128-bit) record types when we know that the stack layout 01078 // of those arguments will match the struct. This is important because the 01079 // LLVM backend isn't smart enough to remove byval, which inhibits many 01080 // optimizations. 01081 if (getContext().getTypeSize(Ty) <= 4*32 && 01082 canExpandIndirectArgument(Ty, getContext())) 01083 return ABIArgInfo::getExpandWithPadding( 01084 State.CC == llvm::CallingConv::X86_FastCall || 01085 State.CC == llvm::CallingConv::X86_VectorCall, 01086 PaddingType); 01087 01088 return getIndirectResult(Ty, true, State); 01089 } 01090 01091 if (const VectorType *VT = Ty->getAs<VectorType>()) { 01092 // On Darwin, some vectors are passed in memory, we handle this by passing 01093 // it as an i8/i16/i32/i64. 01094 if (IsDarwinVectorABI) { 01095 uint64_t Size = getContext().getTypeSize(Ty); 01096 if ((Size == 8 || Size == 16 || Size == 32) || 01097 (Size == 64 && VT->getNumElements() == 1)) 01098 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 01099 Size)); 01100 } 01101 01102 if (IsX86_MMXType(CGT.ConvertType(Ty))) 01103 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); 01104 01105 return ABIArgInfo::getDirect(); 01106 } 01107 01108 01109 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 01110 Ty = EnumTy->getDecl()->getIntegerType(); 01111 01112 bool NeedsPadding; 01113 bool InReg = shouldUseInReg(Ty, State, NeedsPadding); 01114 01115 if (Ty->isPromotableIntegerType()) { 01116 if (InReg) 01117 return ABIArgInfo::getExtendInReg(); 01118 return ABIArgInfo::getExtend(); 01119 } 01120 if (InReg) 01121 return ABIArgInfo::getDirectInReg(); 01122 return ABIArgInfo::getDirect(); 01123 } 01124 01125 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { 01126 CCState State(FI.getCallingConvention()); 01127 if (State.CC == llvm::CallingConv::X86_FastCall) 01128 State.FreeRegs = 2; 01129 else if (State.CC == llvm::CallingConv::X86_VectorCall) { 01130 State.FreeRegs = 2; 01131 State.FreeSSERegs = 6; 01132 } else if (FI.getHasRegParm()) 01133 State.FreeRegs = FI.getRegParm(); 01134 else 01135 State.FreeRegs = DefaultNumRegisterParameters; 01136 01137 if (!getCXXABI().classifyReturnType(FI)) { 01138 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); 01139 } else if (FI.getReturnInfo().isIndirect()) { 01140 // The C++ ABI is not aware of register usage, so we have to check if the 01141 // return value was sret and put it in a register ourselves if appropriate. 01142 if (State.FreeRegs) { 01143 --State.FreeRegs; // The sret parameter consumes a register. 01144 FI.getReturnInfo().setInReg(true); 01145 } 01146 } 01147 01148 bool UsedInAlloca = false; 01149 for (auto &I : FI.arguments()) { 01150 I.info = classifyArgumentType(I.type, State); 01151 UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); 01152 } 01153 01154 // If we needed to use inalloca for any argument, do a second pass and rewrite 01155 // all the memory arguments to use inalloca. 01156 if (UsedInAlloca) 01157 rewriteWithInAlloca(FI); 01158 } 01159 01160 void 01161 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, 01162 unsigned &StackOffset, 01163 ABIArgInfo &Info, QualType Type) const { 01164 assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); 01165 Info = ABIArgInfo::getInAlloca(FrameFields.size()); 01166 FrameFields.push_back(CGT.ConvertTypeForMem(Type)); 01167 StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); 01168 01169 // Insert padding bytes to respect alignment. For x86_32, each argument is 4 01170 // byte aligned. 01171 if (StackOffset % 4U) { 01172 unsigned OldOffset = StackOffset; 01173 StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); 01174 unsigned NumBytes = StackOffset - OldOffset; 01175 assert(NumBytes); 01176 llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); 01177 Ty = llvm::ArrayType::get(Ty, NumBytes); 01178 FrameFields.push_back(Ty); 01179 } 01180 } 01181 01182 static bool isArgInAlloca(const ABIArgInfo &Info) { 01183 // Leave ignored and inreg arguments alone. 01184 switch (Info.getKind()) { 01185 case ABIArgInfo::InAlloca: 01186 return true; 01187 case ABIArgInfo::Indirect: 01188 assert(Info.getIndirectByVal()); 01189 return true; 01190 case ABIArgInfo::Ignore: 01191 return false; 01192 case ABIArgInfo::Direct: 01193 case ABIArgInfo::Extend: 01194 case ABIArgInfo::Expand: 01195 if (Info.getInReg()) 01196 return false; 01197 return true; 01198 } 01199 llvm_unreachable("invalid enum"); 01200 } 01201 01202 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { 01203 assert(IsWin32StructABI && "inalloca only supported on win32"); 01204 01205 // Build a packed struct type for all of the arguments in memory. 01206 SmallVector<llvm::Type *, 6> FrameFields; 01207 01208 unsigned StackOffset = 0; 01209 CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); 01210 01211 // Put 'this' into the struct before 'sret', if necessary. 01212 bool IsThisCall = 01213 FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; 01214 ABIArgInfo &Ret = FI.getReturnInfo(); 01215 if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && 01216 isArgInAlloca(I->info)) { 01217 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 01218 ++I; 01219 } 01220 01221 // Put the sret parameter into the inalloca struct if it's in memory. 01222 if (Ret.isIndirect() && !Ret.getInReg()) { 01223 CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); 01224 addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); 01225 // On Windows, the hidden sret parameter is always returned in eax. 01226 Ret.setInAllocaSRet(IsWin32StructABI); 01227 } 01228 01229 // Skip the 'this' parameter in ecx. 01230 if (IsThisCall) 01231 ++I; 01232 01233 // Put arguments passed in memory into the struct. 01234 for (; I != E; ++I) { 01235 if (isArgInAlloca(I->info)) 01236 addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); 01237 } 01238 01239 FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, 01240 /*isPacked=*/true)); 01241 } 01242 01243 llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 01244 CodeGenFunction &CGF) const { 01245 llvm::Type *BPP = CGF.Int8PtrPtrTy; 01246 01247 CGBuilderTy &Builder = CGF.Builder; 01248 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 01249 "ap"); 01250 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 01251 01252 // Compute if the address needs to be aligned 01253 unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); 01254 Align = getTypeStackAlignInBytes(Ty, Align); 01255 Align = std::max(Align, 4U); 01256 if (Align > 4) { 01257 // addr = (addr + align - 1) & -align; 01258 llvm::Value *Offset = 01259 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); 01260 Addr = CGF.Builder.CreateGEP(Addr, Offset); 01261 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, 01262 CGF.Int32Ty); 01263 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); 01264 Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), 01265 Addr->getType(), 01266 "ap.cur.aligned"); 01267 } 01268 01269 llvm::Type *PTy = 01270 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 01271 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 01272 01273 uint64_t Offset = 01274 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); 01275 llvm::Value *NextAddr = 01276 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 01277 "ap.next"); 01278 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 01279 01280 return AddrTyped; 01281 } 01282 01283 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( 01284 const llvm::Triple &Triple, const CodeGenOptions &Opts) { 01285 assert(Triple.getArch() == llvm::Triple::x86); 01286 01287 switch (Opts.getStructReturnConvention()) { 01288 case CodeGenOptions::SRCK_Default: 01289 break; 01290 case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return 01291 return false; 01292 case CodeGenOptions::SRCK_InRegs: // -freg-struct-return 01293 return true; 01294 } 01295 01296 if (Triple.isOSDarwin()) 01297 return true; 01298 01299 switch (Triple.getOS()) { 01300 case llvm::Triple::DragonFly: 01301 case llvm::Triple::FreeBSD: 01302 case llvm::Triple::OpenBSD: 01303 case llvm::Triple::Bitrig: 01304 return true; 01305 case llvm::Triple::Win32: 01306 switch (Triple.getEnvironment()) { 01307 case llvm::Triple::UnknownEnvironment: 01308 case llvm::Triple::Cygnus: 01309 case llvm::Triple::GNU: 01310 case llvm::Triple::MSVC: 01311 return true; 01312 default: 01313 return false; 01314 } 01315 default: 01316 return false; 01317 } 01318 } 01319 01320 void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, 01321 llvm::GlobalValue *GV, 01322 CodeGen::CodeGenModule &CGM) const { 01323 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 01324 if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { 01325 // Get the LLVM function. 01326 llvm::Function *Fn = cast<llvm::Function>(GV); 01327 01328 // Now add the 'alignstack' attribute with a value of 16. 01329 llvm::AttrBuilder B; 01330 B.addStackAlignmentAttr(16); 01331 Fn->addAttributes(llvm::AttributeSet::FunctionIndex, 01332 llvm::AttributeSet::get(CGM.getLLVMContext(), 01333 llvm::AttributeSet::FunctionIndex, 01334 B)); 01335 } 01336 } 01337 } 01338 01339 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( 01340 CodeGen::CodeGenFunction &CGF, 01341 llvm::Value *Address) const { 01342 CodeGen::CGBuilderTy &Builder = CGF.Builder; 01343 01344 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 01345 01346 // 0-7 are the eight integer registers; the order is different 01347 // on Darwin (for EH), but the range is the same. 01348 // 8 is %eip. 01349 AssignToArrayRange(Builder, Address, Four8, 0, 8); 01350 01351 if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { 01352 // 12-16 are st(0..4). Not sure why we stop at 4. 01353 // These have size 16, which is sizeof(long double) on 01354 // platforms with 8-byte alignment for that type. 01355 llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); 01356 AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); 01357 01358 } else { 01359 // 9 is %eflags, which doesn't get a size on Darwin for some 01360 // reason. 01361 Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); 01362 01363 // 11-16 are st(0..5). Not sure why we stop at 5. 01364 // These have size 12, which is sizeof(long double) on 01365 // platforms with 4-byte alignment for that type. 01366 llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); 01367 AssignToArrayRange(Builder, Address, Twelve8, 11, 16); 01368 } 01369 01370 return false; 01371 } 01372 01373 //===----------------------------------------------------------------------===// 01374 // X86-64 ABI Implementation 01375 //===----------------------------------------------------------------------===// 01376 01377 01378 namespace { 01379 /// X86_64ABIInfo - The X86_64 ABI information. 01380 class X86_64ABIInfo : public ABIInfo { 01381 enum Class { 01382 Integer = 0, 01383 SSE, 01384 SSEUp, 01385 X87, 01386 X87Up, 01387 ComplexX87, 01388 NoClass, 01389 Memory 01390 }; 01391 01392 /// merge - Implement the X86_64 ABI merging algorithm. 01393 /// 01394 /// Merge an accumulating classification \arg Accum with a field 01395 /// classification \arg Field. 01396 /// 01397 /// \param Accum - The accumulating classification. This should 01398 /// always be either NoClass or the result of a previous merge 01399 /// call. In addition, this should never be Memory (the caller 01400 /// should just return Memory for the aggregate). 01401 static Class merge(Class Accum, Class Field); 01402 01403 /// postMerge - Implement the X86_64 ABI post merging algorithm. 01404 /// 01405 /// Post merger cleanup, reduces a malformed Hi and Lo pair to 01406 /// final MEMORY or SSE classes when necessary. 01407 /// 01408 /// \param AggregateSize - The size of the current aggregate in 01409 /// the classification process. 01410 /// 01411 /// \param Lo - The classification for the parts of the type 01412 /// residing in the low word of the containing object. 01413 /// 01414 /// \param Hi - The classification for the parts of the type 01415 /// residing in the higher words of the containing object. 01416 /// 01417 void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; 01418 01419 /// classify - Determine the x86_64 register classes in which the 01420 /// given type T should be passed. 01421 /// 01422 /// \param Lo - The classification for the parts of the type 01423 /// residing in the low word of the containing object. 01424 /// 01425 /// \param Hi - The classification for the parts of the type 01426 /// residing in the high word of the containing object. 01427 /// 01428 /// \param OffsetBase - The bit offset of this type in the 01429 /// containing object. Some parameters are classified different 01430 /// depending on whether they straddle an eightbyte boundary. 01431 /// 01432 /// \param isNamedArg - Whether the argument in question is a "named" 01433 /// argument, as used in AMD64-ABI 3.5.7. 01434 /// 01435 /// If a word is unused its result will be NoClass; if a type should 01436 /// be passed in Memory then at least the classification of \arg Lo 01437 /// will be Memory. 01438 /// 01439 /// The \arg Lo class will be NoClass iff the argument is ignored. 01440 /// 01441 /// If the \arg Lo class is ComplexX87, then the \arg Hi class will 01442 /// also be ComplexX87. 01443 void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, 01444 bool isNamedArg) const; 01445 01446 llvm::Type *GetByteVectorType(QualType Ty) const; 01447 llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, 01448 unsigned IROffset, QualType SourceTy, 01449 unsigned SourceOffset) const; 01450 llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, 01451 unsigned IROffset, QualType SourceTy, 01452 unsigned SourceOffset) const; 01453 01454 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 01455 /// such that the argument will be returned in memory. 01456 ABIArgInfo getIndirectReturnResult(QualType Ty) const; 01457 01458 /// getIndirectResult - Give a source type \arg Ty, return a suitable result 01459 /// such that the argument will be passed in memory. 01460 /// 01461 /// \param freeIntRegs - The number of free integer registers remaining 01462 /// available. 01463 ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; 01464 01465 ABIArgInfo classifyReturnType(QualType RetTy) const; 01466 01467 ABIArgInfo classifyArgumentType(QualType Ty, 01468 unsigned freeIntRegs, 01469 unsigned &neededInt, 01470 unsigned &neededSSE, 01471 bool isNamedArg) const; 01472 01473 bool IsIllegalVectorType(QualType Ty) const; 01474 01475 /// The 0.98 ABI revision clarified a lot of ambiguities, 01476 /// unfortunately in ways that were not always consistent with 01477 /// certain previous compilers. In particular, platforms which 01478 /// required strict binary compatibility with older versions of GCC 01479 /// may need to exempt themselves. 01480 bool honorsRevision0_98() const { 01481 return !getTarget().getTriple().isOSDarwin(); 01482 } 01483 01484 bool HasAVX; 01485 // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on 01486 // 64-bit hardware. 01487 bool Has64BitPointers; 01488 01489 public: 01490 X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : 01491 ABIInfo(CGT), HasAVX(hasavx), 01492 Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { 01493 } 01494 01495 bool isPassedUsingAVXType(QualType type) const { 01496 unsigned neededInt, neededSSE; 01497 // The freeIntRegs argument doesn't matter here. 01498 ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, 01499 /*isNamedArg*/true); 01500 if (info.isDirect()) { 01501 llvm::Type *ty = info.getCoerceToType(); 01502 if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) 01503 return (vectorTy->getBitWidth() > 128); 01504 } 01505 return false; 01506 } 01507 01508 void computeInfo(CGFunctionInfo &FI) const override; 01509 01510 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 01511 CodeGenFunction &CGF) const override; 01512 }; 01513 01514 /// WinX86_64ABIInfo - The Windows X86_64 ABI information. 01515 class WinX86_64ABIInfo : public ABIInfo { 01516 01517 ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, 01518 bool IsReturnType) const; 01519 01520 public: 01521 WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} 01522 01523 void computeInfo(CGFunctionInfo &FI) const override; 01524 01525 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 01526 CodeGenFunction &CGF) const override; 01527 01528 bool isHomogeneousAggregateBaseType(QualType Ty) const override { 01529 // FIXME: Assumes vectorcall is in use. 01530 return isX86VectorTypeForVectorCall(getContext(), Ty); 01531 } 01532 01533 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 01534 uint64_t NumMembers) const override { 01535 // FIXME: Assumes vectorcall is in use. 01536 return isX86VectorCallAggregateSmallEnough(NumMembers); 01537 } 01538 }; 01539 01540 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { 01541 bool HasAVX; 01542 public: 01543 X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 01544 : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) {} 01545 01546 const X86_64ABIInfo &getABIInfo() const { 01547 return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); 01548 } 01549 01550 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 01551 return 7; 01552 } 01553 01554 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 01555 llvm::Value *Address) const override { 01556 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 01557 01558 // 0-15 are the 16 integer registers. 01559 // 16 is %rip. 01560 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 01561 return false; 01562 } 01563 01564 llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 01565 StringRef Constraint, 01566 llvm::Type* Ty) const override { 01567 return X86AdjustInlineAsmType(CGF, Constraint, Ty); 01568 } 01569 01570 bool isNoProtoCallVariadic(const CallArgList &args, 01571 const FunctionNoProtoType *fnType) const override { 01572 // The default CC on x86-64 sets %al to the number of SSA 01573 // registers used, and GCC sets this when calling an unprototyped 01574 // function, so we override the default behavior. However, don't do 01575 // that when AVX types are involved: the ABI explicitly states it is 01576 // undefined, and it doesn't work in practice because of how the ABI 01577 // defines varargs anyway. 01578 if (fnType->getCallConv() == CC_C) { 01579 bool HasAVXType = false; 01580 for (CallArgList::const_iterator 01581 it = args.begin(), ie = args.end(); it != ie; ++it) { 01582 if (getABIInfo().isPassedUsingAVXType(it->Ty)) { 01583 HasAVXType = true; 01584 break; 01585 } 01586 } 01587 01588 if (!HasAVXType) 01589 return true; 01590 } 01591 01592 return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); 01593 } 01594 01595 llvm::Constant * 01596 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { 01597 unsigned Sig = (0xeb << 0) | // jmp rel8 01598 (0x0a << 8) | // .+0x0c 01599 ('F' << 16) | 01600 ('T' << 24); 01601 return llvm::ConstantInt::get(CGM.Int32Ty, Sig); 01602 } 01603 01604 unsigned getOpenMPSimdDefaultAlignment(QualType) const override { 01605 return HasAVX ? 32 : 16; 01606 } 01607 }; 01608 01609 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { 01610 // If the argument does not end in .lib, automatically add the suffix. This 01611 // matches the behavior of MSVC. 01612 std::string ArgStr = Lib; 01613 if (!Lib.endswith_lower(".lib")) 01614 ArgStr += ".lib"; 01615 return ArgStr; 01616 } 01617 01618 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { 01619 public: 01620 WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, 01621 bool d, bool p, bool w, unsigned RegParms) 01622 : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} 01623 01624 void getDependentLibraryOption(llvm::StringRef Lib, 01625 llvm::SmallString<24> &Opt) const override { 01626 Opt = "/DEFAULTLIB:"; 01627 Opt += qualifyWindowsLibrary(Lib); 01628 } 01629 01630 void getDetectMismatchOption(llvm::StringRef Name, 01631 llvm::StringRef Value, 01632 llvm::SmallString<32> &Opt) const override { 01633 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 01634 } 01635 }; 01636 01637 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 01638 bool HasAVX; 01639 public: 01640 WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 01641 : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)), HasAVX(HasAVX) {} 01642 01643 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 01644 return 7; 01645 } 01646 01647 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 01648 llvm::Value *Address) const override { 01649 llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); 01650 01651 // 0-15 are the 16 integer registers. 01652 // 16 is %rip. 01653 AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); 01654 return false; 01655 } 01656 01657 void getDependentLibraryOption(llvm::StringRef Lib, 01658 llvm::SmallString<24> &Opt) const override { 01659 Opt = "/DEFAULTLIB:"; 01660 Opt += qualifyWindowsLibrary(Lib); 01661 } 01662 01663 void getDetectMismatchOption(llvm::StringRef Name, 01664 llvm::StringRef Value, 01665 llvm::SmallString<32> &Opt) const override { 01666 Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; 01667 } 01668 01669 unsigned getOpenMPSimdDefaultAlignment(QualType) const override { 01670 return HasAVX ? 32 : 16; 01671 } 01672 }; 01673 01674 } 01675 01676 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, 01677 Class &Hi) const { 01678 // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: 01679 // 01680 // (a) If one of the classes is Memory, the whole argument is passed in 01681 // memory. 01682 // 01683 // (b) If X87UP is not preceded by X87, the whole argument is passed in 01684 // memory. 01685 // 01686 // (c) If the size of the aggregate exceeds two eightbytes and the first 01687 // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole 01688 // argument is passed in memory. NOTE: This is necessary to keep the 01689 // ABI working for processors that don't support the __m256 type. 01690 // 01691 // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. 01692 // 01693 // Some of these are enforced by the merging logic. Others can arise 01694 // only with unions; for example: 01695 // union { _Complex double; unsigned; } 01696 // 01697 // Note that clauses (b) and (c) were added in 0.98. 01698 // 01699 if (Hi == Memory) 01700 Lo = Memory; 01701 if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) 01702 Lo = Memory; 01703 if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) 01704 Lo = Memory; 01705 if (Hi == SSEUp && Lo != SSE) 01706 Hi = SSE; 01707 } 01708 01709 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { 01710 // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is 01711 // classified recursively so that always two fields are 01712 // considered. The resulting class is calculated according to 01713 // the classes of the fields in the eightbyte: 01714 // 01715 // (a) If both classes are equal, this is the resulting class. 01716 // 01717 // (b) If one of the classes is NO_CLASS, the resulting class is 01718 // the other class. 01719 // 01720 // (c) If one of the classes is MEMORY, the result is the MEMORY 01721 // class. 01722 // 01723 // (d) If one of the classes is INTEGER, the result is the 01724 // INTEGER. 01725 // 01726 // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, 01727 // MEMORY is used as class. 01728 // 01729 // (f) Otherwise class SSE is used. 01730 01731 // Accum should never be memory (we should have returned) or 01732 // ComplexX87 (because this cannot be passed in a structure). 01733 assert((Accum != Memory && Accum != ComplexX87) && 01734 "Invalid accumulated classification during merge."); 01735 if (Accum == Field || Field == NoClass) 01736 return Accum; 01737 if (Field == Memory) 01738 return Memory; 01739 if (Accum == NoClass) 01740 return Field; 01741 if (Accum == Integer || Field == Integer) 01742 return Integer; 01743 if (Field == X87 || Field == X87Up || Field == ComplexX87 || 01744 Accum == X87 || Accum == X87Up) 01745 return Memory; 01746 return SSE; 01747 } 01748 01749 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, 01750 Class &Lo, Class &Hi, bool isNamedArg) const { 01751 // FIXME: This code can be simplified by introducing a simple value class for 01752 // Class pairs with appropriate constructor methods for the various 01753 // situations. 01754 01755 // FIXME: Some of the split computations are wrong; unaligned vectors 01756 // shouldn't be passed in registers for example, so there is no chance they 01757 // can straddle an eightbyte. Verify & simplify. 01758 01759 Lo = Hi = NoClass; 01760 01761 Class &Current = OffsetBase < 64 ? Lo : Hi; 01762 Current = Memory; 01763 01764 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 01765 BuiltinType::Kind k = BT->getKind(); 01766 01767 if (k == BuiltinType::Void) { 01768 Current = NoClass; 01769 } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { 01770 Lo = Integer; 01771 Hi = Integer; 01772 } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { 01773 Current = Integer; 01774 } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || 01775 (k == BuiltinType::LongDouble && 01776 getTarget().getTriple().isOSNaCl())) { 01777 Current = SSE; 01778 } else if (k == BuiltinType::LongDouble) { 01779 Lo = X87; 01780 Hi = X87Up; 01781 } 01782 // FIXME: _Decimal32 and _Decimal64 are SSE. 01783 // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). 01784 return; 01785 } 01786 01787 if (const EnumType *ET = Ty->getAs<EnumType>()) { 01788 // Classify the underlying integer type. 01789 classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); 01790 return; 01791 } 01792 01793 if (Ty->hasPointerRepresentation()) { 01794 Current = Integer; 01795 return; 01796 } 01797 01798 if (Ty->isMemberPointerType()) { 01799 if (Ty->isMemberFunctionPointerType()) { 01800 if (Has64BitPointers) { 01801 // If Has64BitPointers, this is an {i64, i64}, so classify both 01802 // Lo and Hi now. 01803 Lo = Hi = Integer; 01804 } else { 01805 // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that 01806 // straddles an eightbyte boundary, Hi should be classified as well. 01807 uint64_t EB_FuncPtr = (OffsetBase) / 64; 01808 uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; 01809 if (EB_FuncPtr != EB_ThisAdj) { 01810 Lo = Hi = Integer; 01811 } else { 01812 Current = Integer; 01813 } 01814 } 01815 } else { 01816 Current = Integer; 01817 } 01818 return; 01819 } 01820 01821 if (const VectorType *VT = Ty->getAs<VectorType>()) { 01822 uint64_t Size = getContext().getTypeSize(VT); 01823 if (Size == 32) { 01824 // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x 01825 // float> as integer. 01826 Current = Integer; 01827 01828 // If this type crosses an eightbyte boundary, it should be 01829 // split. 01830 uint64_t EB_Real = (OffsetBase) / 64; 01831 uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; 01832 if (EB_Real != EB_Imag) 01833 Hi = Lo; 01834 } else if (Size == 64) { 01835 // gcc passes <1 x double> in memory. :( 01836 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) 01837 return; 01838 01839 // gcc passes <1 x long long> as INTEGER. 01840 if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || 01841 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || 01842 VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || 01843 VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) 01844 Current = Integer; 01845 else 01846 Current = SSE; 01847 01848 // If this type crosses an eightbyte boundary, it should be 01849 // split. 01850 if (OffsetBase && OffsetBase != 64) 01851 Hi = Lo; 01852 } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { 01853 // Arguments of 256-bits are split into four eightbyte chunks. The 01854 // least significant one belongs to class SSE and all the others to class 01855 // SSEUP. The original Lo and Hi design considers that types can't be 01856 // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. 01857 // This design isn't correct for 256-bits, but since there're no cases 01858 // where the upper parts would need to be inspected, avoid adding 01859 // complexity and just consider Hi to match the 64-256 part. 01860 // 01861 // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in 01862 // registers if they are "named", i.e. not part of the "..." of a 01863 // variadic function. 01864 Lo = SSE; 01865 Hi = SSEUp; 01866 } 01867 return; 01868 } 01869 01870 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 01871 QualType ET = getContext().getCanonicalType(CT->getElementType()); 01872 01873 uint64_t Size = getContext().getTypeSize(Ty); 01874 if (ET->isIntegralOrEnumerationType()) { 01875 if (Size <= 64) 01876 Current = Integer; 01877 else if (Size <= 128) 01878 Lo = Hi = Integer; 01879 } else if (ET == getContext().FloatTy) 01880 Current = SSE; 01881 else if (ET == getContext().DoubleTy || 01882 (ET == getContext().LongDoubleTy && 01883 getTarget().getTriple().isOSNaCl())) 01884 Lo = Hi = SSE; 01885 else if (ET == getContext().LongDoubleTy) 01886 Current = ComplexX87; 01887 01888 // If this complex type crosses an eightbyte boundary then it 01889 // should be split. 01890 uint64_t EB_Real = (OffsetBase) / 64; 01891 uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; 01892 if (Hi == NoClass && EB_Real != EB_Imag) 01893 Hi = Lo; 01894 01895 return; 01896 } 01897 01898 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 01899 // Arrays are treated like structures. 01900 01901 uint64_t Size = getContext().getTypeSize(Ty); 01902 01903 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 01904 // than four eightbytes, ..., it has class MEMORY. 01905 if (Size > 256) 01906 return; 01907 01908 // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned 01909 // fields, it has class MEMORY. 01910 // 01911 // Only need to check alignment of array base. 01912 if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) 01913 return; 01914 01915 // Otherwise implement simplified merge. We could be smarter about 01916 // this, but it isn't worth it and would be harder to verify. 01917 Current = NoClass; 01918 uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); 01919 uint64_t ArraySize = AT->getSize().getZExtValue(); 01920 01921 // The only case a 256-bit wide vector could be used is when the array 01922 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 01923 // to work for sizes wider than 128, early check and fallback to memory. 01924 if (Size > 128 && EltSize != 256) 01925 return; 01926 01927 for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { 01928 Class FieldLo, FieldHi; 01929 classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); 01930 Lo = merge(Lo, FieldLo); 01931 Hi = merge(Hi, FieldHi); 01932 if (Lo == Memory || Hi == Memory) 01933 break; 01934 } 01935 01936 postMerge(Size, Lo, Hi); 01937 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); 01938 return; 01939 } 01940 01941 if (const RecordType *RT = Ty->getAs<RecordType>()) { 01942 uint64_t Size = getContext().getTypeSize(Ty); 01943 01944 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger 01945 // than four eightbytes, ..., it has class MEMORY. 01946 if (Size > 256) 01947 return; 01948 01949 // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial 01950 // copy constructor or a non-trivial destructor, it is passed by invisible 01951 // reference. 01952 if (getRecordArgABI(RT, getCXXABI())) 01953 return; 01954 01955 const RecordDecl *RD = RT->getDecl(); 01956 01957 // Assume variable sized types are passed in memory. 01958 if (RD->hasFlexibleArrayMember()) 01959 return; 01960 01961 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 01962 01963 // Reset Lo class, this will be recomputed. 01964 Current = NoClass; 01965 01966 // If this is a C++ record, classify the bases first. 01967 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 01968 for (const auto &I : CXXRD->bases()) { 01969 assert(!I.isVirtual() && !I.getType()->isDependentType() && 01970 "Unexpected base class!"); 01971 const CXXRecordDecl *Base = 01972 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); 01973 01974 // Classify this field. 01975 // 01976 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a 01977 // single eightbyte, each is classified separately. Each eightbyte gets 01978 // initialized to class NO_CLASS. 01979 Class FieldLo, FieldHi; 01980 uint64_t Offset = 01981 OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); 01982 classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); 01983 Lo = merge(Lo, FieldLo); 01984 Hi = merge(Hi, FieldHi); 01985 if (Lo == Memory || Hi == Memory) 01986 break; 01987 } 01988 } 01989 01990 // Classify the fields one at a time, merging the results. 01991 unsigned idx = 0; 01992 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 01993 i != e; ++i, ++idx) { 01994 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 01995 bool BitField = i->isBitField(); 01996 01997 // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than 01998 // four eightbytes, or it contains unaligned fields, it has class MEMORY. 01999 // 02000 // The only case a 256-bit wide vector could be used is when the struct 02001 // contains a single 256-bit element. Since Lo and Hi logic isn't extended 02002 // to work for sizes wider than 128, early check and fallback to memory. 02003 // 02004 if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { 02005 Lo = Memory; 02006 return; 02007 } 02008 // Note, skip this test for bit-fields, see below. 02009 if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { 02010 Lo = Memory; 02011 return; 02012 } 02013 02014 // Classify this field. 02015 // 02016 // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate 02017 // exceeds a single eightbyte, each is classified 02018 // separately. Each eightbyte gets initialized to class 02019 // NO_CLASS. 02020 Class FieldLo, FieldHi; 02021 02022 // Bit-fields require special handling, they do not force the 02023 // structure to be passed in memory even if unaligned, and 02024 // therefore they can straddle an eightbyte. 02025 if (BitField) { 02026 // Ignore padding bit-fields. 02027 if (i->isUnnamedBitfield()) 02028 continue; 02029 02030 uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); 02031 uint64_t Size = i->getBitWidthValue(getContext()); 02032 02033 uint64_t EB_Lo = Offset / 64; 02034 uint64_t EB_Hi = (Offset + Size - 1) / 64; 02035 02036 if (EB_Lo) { 02037 assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); 02038 FieldLo = NoClass; 02039 FieldHi = Integer; 02040 } else { 02041 FieldLo = Integer; 02042 FieldHi = EB_Hi ? Integer : NoClass; 02043 } 02044 } else 02045 classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); 02046 Lo = merge(Lo, FieldLo); 02047 Hi = merge(Hi, FieldHi); 02048 if (Lo == Memory || Hi == Memory) 02049 break; 02050 } 02051 02052 postMerge(Size, Lo, Hi); 02053 } 02054 } 02055 02056 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { 02057 // If this is a scalar LLVM value then assume LLVM will pass it in the right 02058 // place naturally. 02059 if (!isAggregateTypeForABI(Ty)) { 02060 // Treat an enum type as its underlying type. 02061 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 02062 Ty = EnumTy->getDecl()->getIntegerType(); 02063 02064 return (Ty->isPromotableIntegerType() ? 02065 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 02066 } 02067 02068 return ABIArgInfo::getIndirect(0); 02069 } 02070 02071 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { 02072 if (const VectorType *VecTy = Ty->getAs<VectorType>()) { 02073 uint64_t Size = getContext().getTypeSize(VecTy); 02074 unsigned LargestVector = HasAVX ? 256 : 128; 02075 if (Size <= 64 || Size > LargestVector) 02076 return true; 02077 } 02078 02079 return false; 02080 } 02081 02082 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, 02083 unsigned freeIntRegs) const { 02084 // If this is a scalar LLVM value then assume LLVM will pass it in the right 02085 // place naturally. 02086 // 02087 // This assumption is optimistic, as there could be free registers available 02088 // when we need to pass this argument in memory, and LLVM could try to pass 02089 // the argument in the free register. This does not seem to happen currently, 02090 // but this code would be much safer if we could mark the argument with 02091 // 'onstack'. See PR12193. 02092 if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { 02093 // Treat an enum type as its underlying type. 02094 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 02095 Ty = EnumTy->getDecl()->getIntegerType(); 02096 02097 return (Ty->isPromotableIntegerType() ? 02098 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 02099 } 02100 02101 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 02102 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 02103 02104 // Compute the byval alignment. We specify the alignment of the byval in all 02105 // cases so that the mid-level optimizer knows the alignment of the byval. 02106 unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); 02107 02108 // Attempt to avoid passing indirect results using byval when possible. This 02109 // is important for good codegen. 02110 // 02111 // We do this by coercing the value into a scalar type which the backend can 02112 // handle naturally (i.e., without using byval). 02113 // 02114 // For simplicity, we currently only do this when we have exhausted all of the 02115 // free integer registers. Doing this when there are free integer registers 02116 // would require more care, as we would have to ensure that the coerced value 02117 // did not claim the unused register. That would require either reording the 02118 // arguments to the function (so that any subsequent inreg values came first), 02119 // or only doing this optimization when there were no following arguments that 02120 // might be inreg. 02121 // 02122 // We currently expect it to be rare (particularly in well written code) for 02123 // arguments to be passed on the stack when there are still free integer 02124 // registers available (this would typically imply large structs being passed 02125 // by value), so this seems like a fair tradeoff for now. 02126 // 02127 // We can revisit this if the backend grows support for 'onstack' parameter 02128 // attributes. See PR12193. 02129 if (freeIntRegs == 0) { 02130 uint64_t Size = getContext().getTypeSize(Ty); 02131 02132 // If this type fits in an eightbyte, coerce it into the matching integral 02133 // type, which will end up on the stack (with alignment 8). 02134 if (Align == 8 && Size <= 64) 02135 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 02136 Size)); 02137 } 02138 02139 return ABIArgInfo::getIndirect(Align); 02140 } 02141 02142 /// GetByteVectorType - The ABI specifies that a value should be passed in an 02143 /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a 02144 /// vector register. 02145 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { 02146 llvm::Type *IRType = CGT.ConvertType(Ty); 02147 02148 // Wrapper structs that just contain vectors are passed just like vectors, 02149 // strip them off if present. 02150 llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); 02151 while (STy && STy->getNumElements() == 1) { 02152 IRType = STy->getElementType(0); 02153 STy = dyn_cast<llvm::StructType>(IRType); 02154 } 02155 02156 // If the preferred type is a 16-byte vector, prefer to pass it. 02157 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ 02158 llvm::Type *EltTy = VT->getElementType(); 02159 unsigned BitWidth = VT->getBitWidth(); 02160 if ((BitWidth >= 128 && BitWidth <= 256) && 02161 (EltTy->isFloatTy() || EltTy->isDoubleTy() || 02162 EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || 02163 EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || 02164 EltTy->isIntegerTy(128))) 02165 return VT; 02166 } 02167 02168 return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); 02169 } 02170 02171 /// BitsContainNoUserData - Return true if the specified [start,end) bit range 02172 /// is known to either be off the end of the specified type or being in 02173 /// alignment padding. The user type specified is known to be at most 128 bits 02174 /// in size, and have passed through X86_64ABIInfo::classify with a successful 02175 /// classification that put one of the two halves in the INTEGER class. 02176 /// 02177 /// It is conservatively correct to return false. 02178 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, 02179 unsigned EndBit, ASTContext &Context) { 02180 // If the bytes being queried are off the end of the type, there is no user 02181 // data hiding here. This handles analysis of builtins, vectors and other 02182 // types that don't contain interesting padding. 02183 unsigned TySize = (unsigned)Context.getTypeSize(Ty); 02184 if (TySize <= StartBit) 02185 return true; 02186 02187 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 02188 unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); 02189 unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); 02190 02191 // Check each element to see if the element overlaps with the queried range. 02192 for (unsigned i = 0; i != NumElts; ++i) { 02193 // If the element is after the span we care about, then we're done.. 02194 unsigned EltOffset = i*EltSize; 02195 if (EltOffset >= EndBit) break; 02196 02197 unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; 02198 if (!BitsContainNoUserData(AT->getElementType(), EltStart, 02199 EndBit-EltOffset, Context)) 02200 return false; 02201 } 02202 // If it overlaps no elements, then it is safe to process as padding. 02203 return true; 02204 } 02205 02206 if (const RecordType *RT = Ty->getAs<RecordType>()) { 02207 const RecordDecl *RD = RT->getDecl(); 02208 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 02209 02210 // If this is a C++ record, check the bases first. 02211 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 02212 for (const auto &I : CXXRD->bases()) { 02213 assert(!I.isVirtual() && !I.getType()->isDependentType() && 02214 "Unexpected base class!"); 02215 const CXXRecordDecl *Base = 02216 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); 02217 02218 // If the base is after the span we care about, ignore it. 02219 unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); 02220 if (BaseOffset >= EndBit) continue; 02221 02222 unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; 02223 if (!BitsContainNoUserData(I.getType(), BaseStart, 02224 EndBit-BaseOffset, Context)) 02225 return false; 02226 } 02227 } 02228 02229 // Verify that no field has data that overlaps the region of interest. Yes 02230 // this could be sped up a lot by being smarter about queried fields, 02231 // however we're only looking at structs up to 16 bytes, so we don't care 02232 // much. 02233 unsigned idx = 0; 02234 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 02235 i != e; ++i, ++idx) { 02236 unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); 02237 02238 // If we found a field after the region we care about, then we're done. 02239 if (FieldOffset >= EndBit) break; 02240 02241 unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; 02242 if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, 02243 Context)) 02244 return false; 02245 } 02246 02247 // If nothing in this record overlapped the area of interest, then we're 02248 // clean. 02249 return true; 02250 } 02251 02252 return false; 02253 } 02254 02255 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a 02256 /// float member at the specified offset. For example, {int,{float}} has a 02257 /// float at offset 4. It is conservatively correct for this routine to return 02258 /// false. 02259 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, 02260 const llvm::DataLayout &TD) { 02261 // Base case if we find a float. 02262 if (IROffset == 0 && IRType->isFloatTy()) 02263 return true; 02264 02265 // If this is a struct, recurse into the field at the specified offset. 02266 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 02267 const llvm::StructLayout *SL = TD.getStructLayout(STy); 02268 unsigned Elt = SL->getElementContainingOffset(IROffset); 02269 IROffset -= SL->getElementOffset(Elt); 02270 return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); 02271 } 02272 02273 // If this is an array, recurse into the field at the specified offset. 02274 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 02275 llvm::Type *EltTy = ATy->getElementType(); 02276 unsigned EltSize = TD.getTypeAllocSize(EltTy); 02277 IROffset -= IROffset/EltSize*EltSize; 02278 return ContainsFloatAtOffset(EltTy, IROffset, TD); 02279 } 02280 02281 return false; 02282 } 02283 02284 02285 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the 02286 /// low 8 bytes of an XMM register, corresponding to the SSE class. 02287 llvm::Type *X86_64ABIInfo:: 02288 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, 02289 QualType SourceTy, unsigned SourceOffset) const { 02290 // The only three choices we have are either double, <2 x float>, or float. We 02291 // pass as float if the last 4 bytes is just padding. This happens for 02292 // structs that contain 3 floats. 02293 if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, 02294 SourceOffset*8+64, getContext())) 02295 return llvm::Type::getFloatTy(getVMContext()); 02296 02297 // We want to pass as <2 x float> if the LLVM IR type contains a float at 02298 // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the 02299 // case. 02300 if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && 02301 ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) 02302 return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); 02303 02304 return llvm::Type::getDoubleTy(getVMContext()); 02305 } 02306 02307 02308 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in 02309 /// an 8-byte GPR. This means that we either have a scalar or we are talking 02310 /// about the high or low part of an up-to-16-byte struct. This routine picks 02311 /// the best LLVM IR type to represent this, which may be i64 or may be anything 02312 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, 02313 /// etc). 02314 /// 02315 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for 02316 /// the source type. IROffset is an offset in bytes into the LLVM IR type that 02317 /// the 8-byte value references. PrefType may be null. 02318 /// 02319 /// SourceTy is the source-level type for the entire argument. SourceOffset is 02320 /// an offset into this that we're processing (which is always either 0 or 8). 02321 /// 02322 llvm::Type *X86_64ABIInfo:: 02323 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, 02324 QualType SourceTy, unsigned SourceOffset) const { 02325 // If we're dealing with an un-offset LLVM IR type, then it means that we're 02326 // returning an 8-byte unit starting with it. See if we can safely use it. 02327 if (IROffset == 0) { 02328 // Pointers and int64's always fill the 8-byte unit. 02329 if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || 02330 IRType->isIntegerTy(64)) 02331 return IRType; 02332 02333 // If we have a 1/2/4-byte integer, we can use it only if the rest of the 02334 // goodness in the source type is just tail padding. This is allowed to 02335 // kick in for struct {double,int} on the int, but not on 02336 // struct{double,int,int} because we wouldn't return the second int. We 02337 // have to do this analysis on the source type because we can't depend on 02338 // unions being lowered a specific way etc. 02339 if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || 02340 IRType->isIntegerTy(32) || 02341 (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { 02342 unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : 02343 cast<llvm::IntegerType>(IRType)->getBitWidth(); 02344 02345 if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, 02346 SourceOffset*8+64, getContext())) 02347 return IRType; 02348 } 02349 } 02350 02351 if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { 02352 // If this is a struct, recurse into the field at the specified offset. 02353 const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); 02354 if (IROffset < SL->getSizeInBytes()) { 02355 unsigned FieldIdx = SL->getElementContainingOffset(IROffset); 02356 IROffset -= SL->getElementOffset(FieldIdx); 02357 02358 return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, 02359 SourceTy, SourceOffset); 02360 } 02361 } 02362 02363 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { 02364 llvm::Type *EltTy = ATy->getElementType(); 02365 unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); 02366 unsigned EltOffset = IROffset/EltSize*EltSize; 02367 return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, 02368 SourceOffset); 02369 } 02370 02371 // Okay, we don't have any better idea of what to pass, so we pass this in an 02372 // integer register that isn't too big to fit the rest of the struct. 02373 unsigned TySizeInBytes = 02374 (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); 02375 02376 assert(TySizeInBytes != SourceOffset && "Empty field?"); 02377 02378 // It is always safe to classify this as an integer type up to i64 that 02379 // isn't larger than the structure. 02380 return llvm::IntegerType::get(getVMContext(), 02381 std::min(TySizeInBytes-SourceOffset, 8U)*8); 02382 } 02383 02384 02385 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally 02386 /// be used as elements of a two register pair to pass or return, return a 02387 /// first class aggregate to represent them. For example, if the low part of 02388 /// a by-value argument should be passed as i32* and the high part as float, 02389 /// return {i32*, float}. 02390 static llvm::Type * 02391 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, 02392 const llvm::DataLayout &TD) { 02393 // In order to correctly satisfy the ABI, we need to the high part to start 02394 // at offset 8. If the high and low parts we inferred are both 4-byte types 02395 // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have 02396 // the second element at offset 8. Check for this: 02397 unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); 02398 unsigned HiAlign = TD.getABITypeAlignment(Hi); 02399 unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); 02400 assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); 02401 02402 // To handle this, we have to increase the size of the low part so that the 02403 // second element will start at an 8 byte offset. We can't increase the size 02404 // of the second element because it might make us access off the end of the 02405 // struct. 02406 if (HiStart != 8) { 02407 // There are only two sorts of types the ABI generation code can produce for 02408 // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. 02409 // Promote these to a larger type. 02410 if (Lo->isFloatTy()) 02411 Lo = llvm::Type::getDoubleTy(Lo->getContext()); 02412 else { 02413 assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); 02414 Lo = llvm::Type::getInt64Ty(Lo->getContext()); 02415 } 02416 } 02417 02418 llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); 02419 02420 02421 // Verify that the second element is at an 8-byte offset. 02422 assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && 02423 "Invalid x86-64 argument pair!"); 02424 return Result; 02425 } 02426 02427 ABIArgInfo X86_64ABIInfo:: 02428 classifyReturnType(QualType RetTy) const { 02429 // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the 02430 // classification algorithm. 02431 X86_64ABIInfo::Class Lo, Hi; 02432 classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); 02433 02434 // Check some invariants. 02435 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 02436 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 02437 02438 llvm::Type *ResType = nullptr; 02439 switch (Lo) { 02440 case NoClass: 02441 if (Hi == NoClass) 02442 return ABIArgInfo::getIgnore(); 02443 // If the low part is just padding, it takes no register, leave ResType 02444 // null. 02445 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 02446 "Unknown missing lo part"); 02447 break; 02448 02449 case SSEUp: 02450 case X87Up: 02451 llvm_unreachable("Invalid classification for lo word."); 02452 02453 // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via 02454 // hidden argument. 02455 case Memory: 02456 return getIndirectReturnResult(RetTy); 02457 02458 // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next 02459 // available register of the sequence %rax, %rdx is used. 02460 case Integer: 02461 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 02462 02463 // If we have a sign or zero extended integer, make sure to return Extend 02464 // so that the parameter gets the right LLVM IR attributes. 02465 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 02466 // Treat an enum type as its underlying type. 02467 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 02468 RetTy = EnumTy->getDecl()->getIntegerType(); 02469 02470 if (RetTy->isIntegralOrEnumerationType() && 02471 RetTy->isPromotableIntegerType()) 02472 return ABIArgInfo::getExtend(); 02473 } 02474 break; 02475 02476 // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next 02477 // available SSE register of the sequence %xmm0, %xmm1 is used. 02478 case SSE: 02479 ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); 02480 break; 02481 02482 // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is 02483 // returned on the X87 stack in %st0 as 80-bit x87 number. 02484 case X87: 02485 ResType = llvm::Type::getX86_FP80Ty(getVMContext()); 02486 break; 02487 02488 // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real 02489 // part of the value is returned in %st0 and the imaginary part in 02490 // %st1. 02491 case ComplexX87: 02492 assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); 02493 ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), 02494 llvm::Type::getX86_FP80Ty(getVMContext()), 02495 NULL); 02496 break; 02497 } 02498 02499 llvm::Type *HighPart = nullptr; 02500 switch (Hi) { 02501 // Memory was handled previously and X87 should 02502 // never occur as a hi class. 02503 case Memory: 02504 case X87: 02505 llvm_unreachable("Invalid classification for hi word."); 02506 02507 case ComplexX87: // Previously handled. 02508 case NoClass: 02509 break; 02510 02511 case Integer: 02512 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 02513 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 02514 return ABIArgInfo::getDirect(HighPart, 8); 02515 break; 02516 case SSE: 02517 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 02518 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 02519 return ABIArgInfo::getDirect(HighPart, 8); 02520 break; 02521 02522 // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte 02523 // is passed in the next available eightbyte chunk if the last used 02524 // vector register. 02525 // 02526 // SSEUP should always be preceded by SSE, just widen. 02527 case SSEUp: 02528 assert(Lo == SSE && "Unexpected SSEUp classification."); 02529 ResType = GetByteVectorType(RetTy); 02530 break; 02531 02532 // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is 02533 // returned together with the previous X87 value in %st0. 02534 case X87Up: 02535 // If X87Up is preceded by X87, we don't need to do 02536 // anything. However, in some cases with unions it may not be 02537 // preceded by X87. In such situations we follow gcc and pass the 02538 // extra bits in an SSE reg. 02539 if (Lo != X87) { 02540 HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); 02541 if (Lo == NoClass) // Return HighPart at offset 8 in memory. 02542 return ABIArgInfo::getDirect(HighPart, 8); 02543 } 02544 break; 02545 } 02546 02547 // If a high part was specified, merge it together with the low part. It is 02548 // known to pass in the high eightbyte of the result. We do this by forming a 02549 // first class struct aggregate with the high and low part: {low, high} 02550 if (HighPart) 02551 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 02552 02553 return ABIArgInfo::getDirect(ResType); 02554 } 02555 02556 ABIArgInfo X86_64ABIInfo::classifyArgumentType( 02557 QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, 02558 bool isNamedArg) 02559 const 02560 { 02561 Ty = useFirstFieldIfTransparentUnion(Ty); 02562 02563 X86_64ABIInfo::Class Lo, Hi; 02564 classify(Ty, 0, Lo, Hi, isNamedArg); 02565 02566 // Check some invariants. 02567 // FIXME: Enforce these by construction. 02568 assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); 02569 assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); 02570 02571 neededInt = 0; 02572 neededSSE = 0; 02573 llvm::Type *ResType = nullptr; 02574 switch (Lo) { 02575 case NoClass: 02576 if (Hi == NoClass) 02577 return ABIArgInfo::getIgnore(); 02578 // If the low part is just padding, it takes no register, leave ResType 02579 // null. 02580 assert((Hi == SSE || Hi == Integer || Hi == X87Up) && 02581 "Unknown missing lo part"); 02582 break; 02583 02584 // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument 02585 // on the stack. 02586 case Memory: 02587 02588 // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or 02589 // COMPLEX_X87, it is passed in memory. 02590 case X87: 02591 case ComplexX87: 02592 if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) 02593 ++neededInt; 02594 return getIndirectResult(Ty, freeIntRegs); 02595 02596 case SSEUp: 02597 case X87Up: 02598 llvm_unreachable("Invalid classification for lo word."); 02599 02600 // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next 02601 // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 02602 // and %r9 is used. 02603 case Integer: 02604 ++neededInt; 02605 02606 // Pick an 8-byte type based on the preferred type. 02607 ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); 02608 02609 // If we have a sign or zero extended integer, make sure to return Extend 02610 // so that the parameter gets the right LLVM IR attributes. 02611 if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { 02612 // Treat an enum type as its underlying type. 02613 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 02614 Ty = EnumTy->getDecl()->getIntegerType(); 02615 02616 if (Ty->isIntegralOrEnumerationType() && 02617 Ty->isPromotableIntegerType()) 02618 return ABIArgInfo::getExtend(); 02619 } 02620 02621 break; 02622 02623 // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next 02624 // available SSE register is used, the registers are taken in the 02625 // order from %xmm0 to %xmm7. 02626 case SSE: { 02627 llvm::Type *IRType = CGT.ConvertType(Ty); 02628 ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); 02629 ++neededSSE; 02630 break; 02631 } 02632 } 02633 02634 llvm::Type *HighPart = nullptr; 02635 switch (Hi) { 02636 // Memory was handled previously, ComplexX87 and X87 should 02637 // never occur as hi classes, and X87Up must be preceded by X87, 02638 // which is passed in memory. 02639 case Memory: 02640 case X87: 02641 case ComplexX87: 02642 llvm_unreachable("Invalid classification for hi word."); 02643 02644 case NoClass: break; 02645 02646 case Integer: 02647 ++neededInt; 02648 // Pick an 8-byte type based on the preferred type. 02649 HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 02650 02651 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 02652 return ABIArgInfo::getDirect(HighPart, 8); 02653 break; 02654 02655 // X87Up generally doesn't occur here (long double is passed in 02656 // memory), except in situations involving unions. 02657 case X87Up: 02658 case SSE: 02659 HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); 02660 02661 if (Lo == NoClass) // Pass HighPart at offset 8 in memory. 02662 return ABIArgInfo::getDirect(HighPart, 8); 02663 02664 ++neededSSE; 02665 break; 02666 02667 // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the 02668 // eightbyte is passed in the upper half of the last used SSE 02669 // register. This only happens when 128-bit vectors are passed. 02670 case SSEUp: 02671 assert(Lo == SSE && "Unexpected SSEUp classification"); 02672 ResType = GetByteVectorType(Ty); 02673 break; 02674 } 02675 02676 // If a high part was specified, merge it together with the low part. It is 02677 // known to pass in the high eightbyte of the result. We do this by forming a 02678 // first class struct aggregate with the high and low part: {low, high} 02679 if (HighPart) 02680 ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); 02681 02682 return ABIArgInfo::getDirect(ResType); 02683 } 02684 02685 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 02686 02687 if (!getCXXABI().classifyReturnType(FI)) 02688 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 02689 02690 // Keep track of the number of assigned registers. 02691 unsigned freeIntRegs = 6, freeSSERegs = 8; 02692 02693 // If the return value is indirect, then the hidden argument is consuming one 02694 // integer register. 02695 if (FI.getReturnInfo().isIndirect()) 02696 --freeIntRegs; 02697 02698 unsigned NumRequiredArgs = FI.getNumRequiredArgs(); 02699 // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers 02700 // get assigned (in left-to-right order) for passing as follows... 02701 unsigned ArgNo = 0; 02702 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 02703 it != ie; ++it, ++ArgNo) { 02704 bool IsNamedArg = ArgNo < NumRequiredArgs; 02705 02706 unsigned neededInt, neededSSE; 02707 it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, 02708 neededSSE, IsNamedArg); 02709 02710 // AMD64-ABI 3.2.3p3: If there are no registers available for any 02711 // eightbyte of an argument, the whole argument is passed on the 02712 // stack. If registers have already been assigned for some 02713 // eightbytes of such an argument, the assignments get reverted. 02714 if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { 02715 freeIntRegs -= neededInt; 02716 freeSSERegs -= neededSSE; 02717 } else { 02718 it->info = getIndirectResult(it->type, freeIntRegs); 02719 } 02720 } 02721 } 02722 02723 static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, 02724 QualType Ty, 02725 CodeGenFunction &CGF) { 02726 llvm::Value *overflow_arg_area_p = 02727 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); 02728 llvm::Value *overflow_arg_area = 02729 CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); 02730 02731 // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 02732 // byte boundary if alignment needed by type exceeds 8 byte boundary. 02733 // It isn't stated explicitly in the standard, but in practice we use 02734 // alignment greater than 16 where necessary. 02735 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; 02736 if (Align > 8) { 02737 // overflow_arg_area = (overflow_arg_area + align - 1) & -align; 02738 llvm::Value *Offset = 02739 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); 02740 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); 02741 llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, 02742 CGF.Int64Ty); 02743 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); 02744 overflow_arg_area = 02745 CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), 02746 overflow_arg_area->getType(), 02747 "overflow_arg_area.align"); 02748 } 02749 02750 // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. 02751 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 02752 llvm::Value *Res = 02753 CGF.Builder.CreateBitCast(overflow_arg_area, 02754 llvm::PointerType::getUnqual(LTy)); 02755 02756 // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: 02757 // l->overflow_arg_area + sizeof(type). 02758 // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to 02759 // an 8 byte boundary. 02760 02761 uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; 02762 llvm::Value *Offset = 02763 llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); 02764 overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, 02765 "overflow_arg_area.next"); 02766 CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); 02767 02768 // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. 02769 return Res; 02770 } 02771 02772 llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 02773 CodeGenFunction &CGF) const { 02774 // Assume that va_list type is correct; should be pointer to LLVM type: 02775 // struct { 02776 // i32 gp_offset; 02777 // i32 fp_offset; 02778 // i8* overflow_arg_area; 02779 // i8* reg_save_area; 02780 // }; 02781 unsigned neededInt, neededSSE; 02782 02783 Ty = CGF.getContext().getCanonicalType(Ty); 02784 ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, 02785 /*isNamedArg*/false); 02786 02787 // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed 02788 // in the registers. If not go to step 7. 02789 if (!neededInt && !neededSSE) 02790 return EmitVAArgFromMemory(VAListAddr, Ty, CGF); 02791 02792 // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of 02793 // general purpose registers needed to pass type and num_fp to hold 02794 // the number of floating point registers needed. 02795 02796 // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into 02797 // registers. In the case: l->gp_offset > 48 - num_gp * 8 or 02798 // l->fp_offset > 304 - num_fp * 16 go to step 7. 02799 // 02800 // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of 02801 // register save space). 02802 02803 llvm::Value *InRegs = nullptr; 02804 llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; 02805 llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; 02806 if (neededInt) { 02807 gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); 02808 gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); 02809 InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); 02810 InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); 02811 } 02812 02813 if (neededSSE) { 02814 fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); 02815 fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); 02816 llvm::Value *FitsInFP = 02817 llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); 02818 FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); 02819 InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; 02820 } 02821 02822 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 02823 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 02824 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 02825 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 02826 02827 // Emit code to load the value if it was passed in registers. 02828 02829 CGF.EmitBlock(InRegBlock); 02830 02831 // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with 02832 // an offset of l->gp_offset and/or l->fp_offset. This may require 02833 // copying to a temporary location in case the parameter is passed 02834 // in different register classes or requires an alignment greater 02835 // than 8 for general purpose registers and 16 for XMM registers. 02836 // 02837 // FIXME: This really results in shameful code when we end up needing to 02838 // collect arguments from different places; often what should result in a 02839 // simple assembling of a structure from scattered addresses has many more 02840 // loads than necessary. Can we clean this up? 02841 llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); 02842 llvm::Value *RegAddr = 02843 CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), 02844 "reg_save_area"); 02845 if (neededInt && neededSSE) { 02846 // FIXME: Cleanup. 02847 assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); 02848 llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); 02849 llvm::Value *Tmp = CGF.CreateMemTemp(Ty); 02850 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); 02851 assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); 02852 llvm::Type *TyLo = ST->getElementType(0); 02853 llvm::Type *TyHi = ST->getElementType(1); 02854 assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && 02855 "Unexpected ABI info for mixed regs"); 02856 llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); 02857 llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); 02858 llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); 02859 llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); 02860 llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; 02861 llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; 02862 llvm::Value *V = 02863 CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); 02864 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 02865 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); 02866 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 02867 02868 RegAddr = CGF.Builder.CreateBitCast(Tmp, 02869 llvm::PointerType::getUnqual(LTy)); 02870 } else if (neededInt) { 02871 RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); 02872 RegAddr = CGF.Builder.CreateBitCast(RegAddr, 02873 llvm::PointerType::getUnqual(LTy)); 02874 02875 // Copy to a temporary if necessary to ensure the appropriate alignment. 02876 std::pair<CharUnits, CharUnits> SizeAlign = 02877 CGF.getContext().getTypeInfoInChars(Ty); 02878 uint64_t TySize = SizeAlign.first.getQuantity(); 02879 unsigned TyAlign = SizeAlign.second.getQuantity(); 02880 if (TyAlign > 8) { 02881 llvm::Value *Tmp = CGF.CreateMemTemp(Ty); 02882 CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); 02883 RegAddr = Tmp; 02884 } 02885 } else if (neededSSE == 1) { 02886 RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); 02887 RegAddr = CGF.Builder.CreateBitCast(RegAddr, 02888 llvm::PointerType::getUnqual(LTy)); 02889 } else { 02890 assert(neededSSE == 2 && "Invalid number of needed registers!"); 02891 // SSE registers are spaced 16 bytes apart in the register save 02892 // area, we need to collect the two eightbytes together. 02893 llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); 02894 llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); 02895 llvm::Type *DoubleTy = CGF.DoubleTy; 02896 llvm::Type *DblPtrTy = 02897 llvm::PointerType::getUnqual(DoubleTy); 02898 llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); 02899 llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); 02900 Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); 02901 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, 02902 DblPtrTy)); 02903 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); 02904 V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, 02905 DblPtrTy)); 02906 CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); 02907 RegAddr = CGF.Builder.CreateBitCast(Tmp, 02908 llvm::PointerType::getUnqual(LTy)); 02909 } 02910 02911 // AMD64-ABI 3.5.7p5: Step 5. Set: 02912 // l->gp_offset = l->gp_offset + num_gp * 8 02913 // l->fp_offset = l->fp_offset + num_fp * 16. 02914 if (neededInt) { 02915 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); 02916 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), 02917 gp_offset_p); 02918 } 02919 if (neededSSE) { 02920 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); 02921 CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), 02922 fp_offset_p); 02923 } 02924 CGF.EmitBranch(ContBlock); 02925 02926 // Emit code to load the value if it was passed in memory. 02927 02928 CGF.EmitBlock(InMemBlock); 02929 llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); 02930 02931 // Return the appropriate result. 02932 02933 CGF.EmitBlock(ContBlock); 02934 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, 02935 "vaarg.addr"); 02936 ResAddr->addIncoming(RegAddr, InRegBlock); 02937 ResAddr->addIncoming(MemAddr, InMemBlock); 02938 return ResAddr; 02939 } 02940 02941 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, 02942 bool IsReturnType) const { 02943 02944 if (Ty->isVoidType()) 02945 return ABIArgInfo::getIgnore(); 02946 02947 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 02948 Ty = EnumTy->getDecl()->getIntegerType(); 02949 02950 TypeInfo Info = getContext().getTypeInfo(Ty); 02951 uint64_t Width = Info.Width; 02952 unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); 02953 02954 const RecordType *RT = Ty->getAs<RecordType>(); 02955 if (RT) { 02956 if (!IsReturnType) { 02957 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) 02958 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 02959 } 02960 02961 if (RT->getDecl()->hasFlexibleArrayMember()) 02962 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 02963 02964 // FIXME: mingw-w64-gcc emits 128-bit struct as i128 02965 if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) 02966 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 02967 Width)); 02968 } 02969 02970 // vectorcall adds the concept of a homogenous vector aggregate, similar to 02971 // other targets. 02972 const Type *Base = nullptr; 02973 uint64_t NumElts = 0; 02974 if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { 02975 if (FreeSSERegs >= NumElts) { 02976 FreeSSERegs -= NumElts; 02977 if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) 02978 return ABIArgInfo::getDirect(); 02979 return ABIArgInfo::getExpand(); 02980 } 02981 return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 02982 } 02983 02984 02985 if (Ty->isMemberPointerType()) { 02986 // If the member pointer is represented by an LLVM int or ptr, pass it 02987 // directly. 02988 llvm::Type *LLTy = CGT.ConvertType(Ty); 02989 if (LLTy->isPointerTy() || LLTy->isIntegerTy()) 02990 return ABIArgInfo::getDirect(); 02991 } 02992 02993 if (RT || Ty->isMemberPointerType()) { 02994 // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is 02995 // not 1, 2, 4, or 8 bytes, must be passed by reference." 02996 if (Width > 64 || !llvm::isPowerOf2_64(Width)) 02997 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 02998 02999 // Otherwise, coerce it to a small integer. 03000 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); 03001 } 03002 03003 // Bool type is always extended to the ABI, other builtin types are not 03004 // extended. 03005 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 03006 if (BT && BT->getKind() == BuiltinType::Bool) 03007 return ABIArgInfo::getExtend(); 03008 03009 return ABIArgInfo::getDirect(); 03010 } 03011 03012 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 03013 bool IsVectorCall = 03014 FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; 03015 03016 // We can use up to 4 SSE return registers with vectorcall. 03017 unsigned FreeSSERegs = IsVectorCall ? 4 : 0; 03018 if (!getCXXABI().classifyReturnType(FI)) 03019 FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); 03020 03021 // We can use up to 6 SSE register parameters with vectorcall. 03022 FreeSSERegs = IsVectorCall ? 6 : 0; 03023 for (auto &I : FI.arguments()) 03024 I.info = classify(I.type, FreeSSERegs, false); 03025 } 03026 03027 llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 03028 CodeGenFunction &CGF) const { 03029 llvm::Type *BPP = CGF.Int8PtrPtrTy; 03030 03031 CGBuilderTy &Builder = CGF.Builder; 03032 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 03033 "ap"); 03034 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 03035 llvm::Type *PTy = 03036 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 03037 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 03038 03039 uint64_t Offset = 03040 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); 03041 llvm::Value *NextAddr = 03042 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 03043 "ap.next"); 03044 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 03045 03046 return AddrTyped; 03047 } 03048 03049 namespace { 03050 03051 class NaClX86_64ABIInfo : public ABIInfo { 03052 public: 03053 NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 03054 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} 03055 void computeInfo(CGFunctionInfo &FI) const override; 03056 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 03057 CodeGenFunction &CGF) const override; 03058 private: 03059 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. 03060 X86_64ABIInfo NInfo; // Used for everything else. 03061 }; 03062 03063 class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { 03064 bool HasAVX; 03065 public: 03066 NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) 03067 : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) { 03068 } 03069 unsigned getOpenMPSimdDefaultAlignment(QualType) const override { 03070 return HasAVX ? 32 : 16; 03071 } 03072 }; 03073 03074 } 03075 03076 void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { 03077 if (FI.getASTCallingConvention() == CC_PnaclCall) 03078 PInfo.computeInfo(FI); 03079 else 03080 NInfo.computeInfo(FI); 03081 } 03082 03083 llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 03084 CodeGenFunction &CGF) const { 03085 // Always use the native convention; calling pnacl-style varargs functions 03086 // is unuspported. 03087 return NInfo.EmitVAArg(VAListAddr, Ty, CGF); 03088 } 03089 03090 03091 // PowerPC-32 03092 namespace { 03093 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. 03094 class PPC32_SVR4_ABIInfo : public DefaultABIInfo { 03095 public: 03096 PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 03097 03098 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 03099 CodeGenFunction &CGF) const override; 03100 }; 03101 03102 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { 03103 public: 03104 PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} 03105 03106 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 03107 // This is recovered from gcc output. 03108 return 1; // r1 is the dedicated stack pointer 03109 } 03110 03111 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 03112 llvm::Value *Address) const override; 03113 03114 unsigned getOpenMPSimdDefaultAlignment(QualType) const override { 03115 return 16; // Natural alignment for Altivec vectors. 03116 } 03117 }; 03118 03119 } 03120 03121 llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, 03122 QualType Ty, 03123 CodeGenFunction &CGF) const { 03124 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 03125 // TODO: Implement this. For now ignore. 03126 (void)CTy; 03127 return nullptr; 03128 } 03129 03130 bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; 03131 bool isInt = Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); 03132 llvm::Type *CharPtr = CGF.Int8PtrTy; 03133 llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy; 03134 03135 CGBuilderTy &Builder = CGF.Builder; 03136 llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr"); 03137 llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty); 03138 llvm::Value *FPRPtrAsInt = Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1)); 03139 llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr); 03140 llvm::Value *OverflowAreaPtrAsInt = Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3)); 03141 llvm::Value *OverflowAreaPtr = Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr); 03142 llvm::Value *RegsaveAreaPtrAsInt = Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4)); 03143 llvm::Value *RegsaveAreaPtr = Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr); 03144 llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr"); 03145 // Align GPR when TY is i64. 03146 if (isI64) { 03147 llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1)); 03148 llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1)); 03149 llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1)); 03150 GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR); 03151 } 03152 llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr"); 03153 llvm::Value *OverflowArea = Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area"); 03154 llvm::Value *OverflowAreaAsInt = Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty); 03155 llvm::Value *RegsaveArea = Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area"); 03156 llvm::Value *RegsaveAreaAsInt = Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty); 03157 03158 llvm::Value *CC = Builder.CreateICmpULT(isInt ? GPR : FPR, 03159 Builder.getInt8(8), "cond"); 03160 03161 llvm::Value *RegConstant = Builder.CreateMul(isInt ? GPR : FPR, 03162 Builder.getInt8(isInt ? 4 : 8)); 03163 03164 llvm::Value *OurReg = Builder.CreateAdd(RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty)); 03165 03166 if (Ty->isFloatingType()) 03167 OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32)); 03168 03169 llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); 03170 llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); 03171 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 03172 03173 Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); 03174 03175 CGF.EmitBlock(UsingRegs); 03176 03177 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 03178 llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy); 03179 // Increase the GPR/FPR indexes. 03180 if (isInt) { 03181 GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1)); 03182 Builder.CreateStore(GPR, GPRPtr); 03183 } else { 03184 FPR = Builder.CreateAdd(FPR, Builder.getInt8(1)); 03185 Builder.CreateStore(FPR, FPRPtr); 03186 } 03187 CGF.EmitBranch(Cont); 03188 03189 CGF.EmitBlock(UsingOverflow); 03190 03191 // Increase the overflow area. 03192 llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy); 03193 OverflowAreaAsInt = Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8)); 03194 Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), OverflowAreaPtr); 03195 CGF.EmitBranch(Cont); 03196 03197 CGF.EmitBlock(Cont); 03198 03199 llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr"); 03200 Result->addIncoming(Result1, UsingRegs); 03201 Result->addIncoming(Result2, UsingOverflow); 03202 03203 if (Ty->isAggregateType()) { 03204 llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr") ; 03205 return Builder.CreateLoad(AGGPtr, false, "aggr"); 03206 } 03207 03208 return Result; 03209 } 03210 03211 bool 03212 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 03213 llvm::Value *Address) const { 03214 // This is calculated from the LLVM and GCC tables and verified 03215 // against gcc output. AFAIK all ABIs use the same encoding. 03216 03217 CodeGen::CGBuilderTy &Builder = CGF.Builder; 03218 03219 llvm::IntegerType *i8 = CGF.Int8Ty; 03220 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 03221 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 03222 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 03223 03224 // 0-31: r0-31, the 4-byte general-purpose registers 03225 AssignToArrayRange(Builder, Address, Four8, 0, 31); 03226 03227 // 32-63: fp0-31, the 8-byte floating-point registers 03228 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 03229 03230 // 64-76 are various 4-byte special-purpose registers: 03231 // 64: mq 03232 // 65: lr 03233 // 66: ctr 03234 // 67: ap 03235 // 68-75 cr0-7 03236 // 76: xer 03237 AssignToArrayRange(Builder, Address, Four8, 64, 76); 03238 03239 // 77-108: v0-31, the 16-byte vector registers 03240 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 03241 03242 // 109: vrsave 03243 // 110: vscr 03244 // 111: spe_acc 03245 // 112: spefscr 03246 // 113: sfp 03247 AssignToArrayRange(Builder, Address, Four8, 109, 113); 03248 03249 return false; 03250 } 03251 03252 // PowerPC-64 03253 03254 namespace { 03255 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. 03256 class PPC64_SVR4_ABIInfo : public DefaultABIInfo { 03257 public: 03258 enum ABIKind { 03259 ELFv1 = 0, 03260 ELFv2 03261 }; 03262 03263 private: 03264 static const unsigned GPRBits = 64; 03265 ABIKind Kind; 03266 03267 public: 03268 PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind) 03269 : DefaultABIInfo(CGT), Kind(Kind) {} 03270 03271 bool isPromotableTypeForABI(QualType Ty) const; 03272 bool isAlignedParamType(QualType Ty) const; 03273 03274 ABIArgInfo classifyReturnType(QualType RetTy) const; 03275 ABIArgInfo classifyArgumentType(QualType Ty) const; 03276 03277 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 03278 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 03279 uint64_t Members) const override; 03280 03281 // TODO: We can add more logic to computeInfo to improve performance. 03282 // Example: For aggregate arguments that fit in a register, we could 03283 // use getDirectInReg (as is done below for structs containing a single 03284 // floating-point value) to avoid pushing them to memory on function 03285 // entry. This would require changing the logic in PPCISelLowering 03286 // when lowering the parameters in the caller and args in the callee. 03287 void computeInfo(CGFunctionInfo &FI) const override { 03288 if (!getCXXABI().classifyReturnType(FI)) 03289 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 03290 for (auto &I : FI.arguments()) { 03291 // We rely on the default argument classification for the most part. 03292 // One exception: An aggregate containing a single floating-point 03293 // or vector item must be passed in a register if one is available. 03294 const Type *T = isSingleElementStruct(I.type, getContext()); 03295 if (T) { 03296 const BuiltinType *BT = T->getAs<BuiltinType>(); 03297 if ((T->isVectorType() && getContext().getTypeSize(T) == 128) || 03298 (BT && BT->isFloatingPoint())) { 03299 QualType QT(T, 0); 03300 I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); 03301 continue; 03302 } 03303 } 03304 I.info = classifyArgumentType(I.type); 03305 } 03306 } 03307 03308 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 03309 CodeGenFunction &CGF) const override; 03310 }; 03311 03312 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { 03313 public: 03314 PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, 03315 PPC64_SVR4_ABIInfo::ABIKind Kind) 03316 : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind)) {} 03317 03318 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 03319 // This is recovered from gcc output. 03320 return 1; // r1 is the dedicated stack pointer 03321 } 03322 03323 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 03324 llvm::Value *Address) const override; 03325 03326 unsigned getOpenMPSimdDefaultAlignment(QualType) const override { 03327 return 16; // Natural alignment for Altivec and VSX vectors. 03328 } 03329 }; 03330 03331 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { 03332 public: 03333 PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} 03334 03335 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 03336 // This is recovered from gcc output. 03337 return 1; // r1 is the dedicated stack pointer 03338 } 03339 03340 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 03341 llvm::Value *Address) const override; 03342 03343 unsigned getOpenMPSimdDefaultAlignment(QualType) const override { 03344 return 16; // Natural alignment for Altivec vectors. 03345 } 03346 }; 03347 03348 } 03349 03350 // Return true if the ABI requires Ty to be passed sign- or zero- 03351 // extended to 64 bits. 03352 bool 03353 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { 03354 // Treat an enum type as its underlying type. 03355 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 03356 Ty = EnumTy->getDecl()->getIntegerType(); 03357 03358 // Promotable integer types are required to be promoted by the ABI. 03359 if (Ty->isPromotableIntegerType()) 03360 return true; 03361 03362 // In addition to the usual promotable integer types, we also need to 03363 // extend all 32-bit types, since the ABI requires promotion to 64 bits. 03364 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 03365 switch (BT->getKind()) { 03366 case BuiltinType::Int: 03367 case BuiltinType::UInt: 03368 return true; 03369 default: 03370 break; 03371 } 03372 03373 return false; 03374 } 03375 03376 /// isAlignedParamType - Determine whether a type requires 16-byte 03377 /// alignment in the parameter area. 03378 bool 03379 PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty) const { 03380 // Complex types are passed just like their elements. 03381 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) 03382 Ty = CTy->getElementType(); 03383 03384 // Only vector types of size 16 bytes need alignment (larger types are 03385 // passed via reference, smaller types are not aligned). 03386 if (Ty->isVectorType()) 03387 return getContext().getTypeSize(Ty) == 128; 03388 03389 // For single-element float/vector structs, we consider the whole type 03390 // to have the same alignment requirements as its single element. 03391 const Type *AlignAsType = nullptr; 03392 const Type *EltType = isSingleElementStruct(Ty, getContext()); 03393 if (EltType) { 03394 const BuiltinType *BT = EltType->getAs<BuiltinType>(); 03395 if ((EltType->isVectorType() && 03396 getContext().getTypeSize(EltType) == 128) || 03397 (BT && BT->isFloatingPoint())) 03398 AlignAsType = EltType; 03399 } 03400 03401 // Likewise for ELFv2 homogeneous aggregates. 03402 const Type *Base = nullptr; 03403 uint64_t Members = 0; 03404 if (!AlignAsType && Kind == ELFv2 && 03405 isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) 03406 AlignAsType = Base; 03407 03408 // With special case aggregates, only vector base types need alignment. 03409 if (AlignAsType) 03410 return AlignAsType->isVectorType(); 03411 03412 // Otherwise, we only need alignment for any aggregate type that 03413 // has an alignment requirement of >= 16 bytes. 03414 if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) 03415 return true; 03416 03417 return false; 03418 } 03419 03420 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous 03421 /// aggregate. Base is set to the base element type, and Members is set 03422 /// to the number of base elements. 03423 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, 03424 uint64_t &Members) const { 03425 if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 03426 uint64_t NElements = AT->getSize().getZExtValue(); 03427 if (NElements == 0) 03428 return false; 03429 if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) 03430 return false; 03431 Members *= NElements; 03432 } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 03433 const RecordDecl *RD = RT->getDecl(); 03434 if (RD->hasFlexibleArrayMember()) 03435 return false; 03436 03437 Members = 0; 03438 03439 // If this is a C++ record, check the bases first. 03440 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { 03441 for (const auto &I : CXXRD->bases()) { 03442 // Ignore empty records. 03443 if (isEmptyRecord(getContext(), I.getType(), true)) 03444 continue; 03445 03446 uint64_t FldMembers; 03447 if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) 03448 return false; 03449 03450 Members += FldMembers; 03451 } 03452 } 03453 03454 for (const auto *FD : RD->fields()) { 03455 // Ignore (non-zero arrays of) empty records. 03456 QualType FT = FD->getType(); 03457 while (const ConstantArrayType *AT = 03458 getContext().getAsConstantArrayType(FT)) { 03459 if (AT->getSize().getZExtValue() == 0) 03460 return false; 03461 FT = AT->getElementType(); 03462 } 03463 if (isEmptyRecord(getContext(), FT, true)) 03464 continue; 03465 03466 // For compatibility with GCC, ignore empty bitfields in C++ mode. 03467 if (getContext().getLangOpts().CPlusPlus && 03468 FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) 03469 continue; 03470 03471 uint64_t FldMembers; 03472 if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) 03473 return false; 03474 03475 Members = (RD->isUnion() ? 03476 std::max(Members, FldMembers) : Members + FldMembers); 03477 } 03478 03479 if (!Base) 03480 return false; 03481 03482 // Ensure there is no padding. 03483 if (getContext().getTypeSize(Base) * Members != 03484 getContext().getTypeSize(Ty)) 03485 return false; 03486 } else { 03487 Members = 1; 03488 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 03489 Members = 2; 03490 Ty = CT->getElementType(); 03491 } 03492 03493 // Most ABIs only support float, double, and some vector type widths. 03494 if (!isHomogeneousAggregateBaseType(Ty)) 03495 return false; 03496 03497 // The base type must be the same for all members. Types that 03498 // agree in both total size and mode (float vs. vector) are 03499 // treated as being equivalent here. 03500 const Type *TyPtr = Ty.getTypePtr(); 03501 if (!Base) 03502 Base = TyPtr; 03503 03504 if (Base->isVectorType() != TyPtr->isVectorType() || 03505 getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) 03506 return false; 03507 } 03508 return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); 03509 } 03510 03511 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 03512 // Homogeneous aggregates for ELFv2 must have base types of float, 03513 // double, long double, or 128-bit vectors. 03514 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 03515 if (BT->getKind() == BuiltinType::Float || 03516 BT->getKind() == BuiltinType::Double || 03517 BT->getKind() == BuiltinType::LongDouble) 03518 return true; 03519 } 03520 if (const VectorType *VT = Ty->getAs<VectorType>()) { 03521 if (getContext().getTypeSize(VT) == 128) 03522 return true; 03523 } 03524 return false; 03525 } 03526 03527 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( 03528 const Type *Base, uint64_t Members) const { 03529 // Vector types require one register, floating point types require one 03530 // or two registers depending on their size. 03531 uint32_t NumRegs = 03532 Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; 03533 03534 // Homogeneous Aggregates may occupy at most 8 registers. 03535 return Members * NumRegs <= 8; 03536 } 03537 03538 ABIArgInfo 03539 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { 03540 Ty = useFirstFieldIfTransparentUnion(Ty); 03541 03542 if (Ty->isAnyComplexType()) 03543 return ABIArgInfo::getDirect(); 03544 03545 // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) 03546 // or via reference (larger than 16 bytes). 03547 if (Ty->isVectorType()) { 03548 uint64_t Size = getContext().getTypeSize(Ty); 03549 if (Size > 128) 03550 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 03551 else if (Size < 128) { 03552 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 03553 return ABIArgInfo::getDirect(CoerceTy); 03554 } 03555 } 03556 03557 if (isAggregateTypeForABI(Ty)) { 03558 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 03559 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 03560 03561 uint64_t ABIAlign = isAlignedParamType(Ty)? 16 : 8; 03562 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; 03563 03564 // ELFv2 homogeneous aggregates are passed as array types. 03565 const Type *Base = nullptr; 03566 uint64_t Members = 0; 03567 if (Kind == ELFv2 && 03568 isHomogeneousAggregate(Ty, Base, Members)) { 03569 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 03570 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 03571 return ABIArgInfo::getDirect(CoerceTy); 03572 } 03573 03574 // If an aggregate may end up fully in registers, we do not 03575 // use the ByVal method, but pass the aggregate as array. 03576 // This is usually beneficial since we avoid forcing the 03577 // back-end to store the argument to memory. 03578 uint64_t Bits = getContext().getTypeSize(Ty); 03579 if (Bits > 0 && Bits <= 8 * GPRBits) { 03580 llvm::Type *CoerceTy; 03581 03582 // Types up to 8 bytes are passed as integer type (which will be 03583 // properly aligned in the argument save area doubleword). 03584 if (Bits <= GPRBits) 03585 CoerceTy = llvm::IntegerType::get(getVMContext(), 03586 llvm::RoundUpToAlignment(Bits, 8)); 03587 // Larger types are passed as arrays, with the base type selected 03588 // according to the required alignment in the save area. 03589 else { 03590 uint64_t RegBits = ABIAlign * 8; 03591 uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; 03592 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); 03593 CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); 03594 } 03595 03596 return ABIArgInfo::getDirect(CoerceTy); 03597 } 03598 03599 // All other aggregates are passed ByVal. 03600 return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, 03601 /*Realign=*/TyAlign > ABIAlign); 03602 } 03603 03604 return (isPromotableTypeForABI(Ty) ? 03605 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 03606 } 03607 03608 ABIArgInfo 03609 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { 03610 if (RetTy->isVoidType()) 03611 return ABIArgInfo::getIgnore(); 03612 03613 if (RetTy->isAnyComplexType()) 03614 return ABIArgInfo::getDirect(); 03615 03616 // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) 03617 // or via reference (larger than 16 bytes). 03618 if (RetTy->isVectorType()) { 03619 uint64_t Size = getContext().getTypeSize(RetTy); 03620 if (Size > 128) 03621 return ABIArgInfo::getIndirect(0); 03622 else if (Size < 128) { 03623 llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); 03624 return ABIArgInfo::getDirect(CoerceTy); 03625 } 03626 } 03627 03628 if (isAggregateTypeForABI(RetTy)) { 03629 // ELFv2 homogeneous aggregates are returned as array types. 03630 const Type *Base = nullptr; 03631 uint64_t Members = 0; 03632 if (Kind == ELFv2 && 03633 isHomogeneousAggregate(RetTy, Base, Members)) { 03634 llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); 03635 llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); 03636 return ABIArgInfo::getDirect(CoerceTy); 03637 } 03638 03639 // ELFv2 small aggregates are returned in up to two registers. 03640 uint64_t Bits = getContext().getTypeSize(RetTy); 03641 if (Kind == ELFv2 && Bits <= 2 * GPRBits) { 03642 if (Bits == 0) 03643 return ABIArgInfo::getIgnore(); 03644 03645 llvm::Type *CoerceTy; 03646 if (Bits > GPRBits) { 03647 CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); 03648 CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, NULL); 03649 } else 03650 CoerceTy = llvm::IntegerType::get(getVMContext(), 03651 llvm::RoundUpToAlignment(Bits, 8)); 03652 return ABIArgInfo::getDirect(CoerceTy); 03653 } 03654 03655 // All other aggregates are returned indirectly. 03656 return ABIArgInfo::getIndirect(0); 03657 } 03658 03659 return (isPromotableTypeForABI(RetTy) ? 03660 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 03661 } 03662 03663 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. 03664 llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, 03665 QualType Ty, 03666 CodeGenFunction &CGF) const { 03667 llvm::Type *BP = CGF.Int8PtrTy; 03668 llvm::Type *BPP = CGF.Int8PtrPtrTy; 03669 03670 CGBuilderTy &Builder = CGF.Builder; 03671 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 03672 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 03673 03674 // Handle types that require 16-byte alignment in the parameter save area. 03675 if (isAlignedParamType(Ty)) { 03676 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); 03677 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(15)); 03678 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(-16)); 03679 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); 03680 } 03681 03682 // Update the va_list pointer. The pointer should be bumped by the 03683 // size of the object. We can trust getTypeSize() except for a complex 03684 // type whose base type is smaller than a doubleword. For these, the 03685 // size of the object is 16 bytes; see below for further explanation. 03686 unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; 03687 QualType BaseTy; 03688 unsigned CplxBaseSize = 0; 03689 03690 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { 03691 BaseTy = CTy->getElementType(); 03692 CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; 03693 if (CplxBaseSize < 8) 03694 SizeInBytes = 16; 03695 } 03696 03697 unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); 03698 llvm::Value *NextAddr = 03699 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), 03700 "ap.next"); 03701 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 03702 03703 // If we have a complex type and the base type is smaller than 8 bytes, 03704 // the ABI calls for the real and imaginary parts to be right-adjusted 03705 // in separate doublewords. However, Clang expects us to produce a 03706 // pointer to a structure with the two parts packed tightly. So generate 03707 // loads of the real and imaginary parts relative to the va_list pointer, 03708 // and store them to a temporary structure. 03709 if (CplxBaseSize && CplxBaseSize < 8) { 03710 llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); 03711 llvm::Value *ImagAddr = RealAddr; 03712 if (CGF.CGM.getDataLayout().isBigEndian()) { 03713 RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); 03714 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); 03715 } else { 03716 ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); 03717 } 03718 llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); 03719 RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); 03720 ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); 03721 llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); 03722 llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); 03723 llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), 03724 "vacplx"); 03725 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); 03726 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); 03727 Builder.CreateStore(Real, RealPtr, false); 03728 Builder.CreateStore(Imag, ImagPtr, false); 03729 return Ptr; 03730 } 03731 03732 // If the argument is smaller than 8 bytes, it is right-adjusted in 03733 // its doubleword slot. Adjust the pointer to pick it up from the 03734 // correct offset. 03735 if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { 03736 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); 03737 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); 03738 Addr = Builder.CreateIntToPtr(AddrAsInt, BP); 03739 } 03740 03741 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 03742 return Builder.CreateBitCast(Addr, PTy); 03743 } 03744 03745 static bool 03746 PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 03747 llvm::Value *Address) { 03748 // This is calculated from the LLVM and GCC tables and verified 03749 // against gcc output. AFAIK all ABIs use the same encoding. 03750 03751 CodeGen::CGBuilderTy &Builder = CGF.Builder; 03752 03753 llvm::IntegerType *i8 = CGF.Int8Ty; 03754 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 03755 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 03756 llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); 03757 03758 // 0-31: r0-31, the 8-byte general-purpose registers 03759 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 03760 03761 // 32-63: fp0-31, the 8-byte floating-point registers 03762 AssignToArrayRange(Builder, Address, Eight8, 32, 63); 03763 03764 // 64-76 are various 4-byte special-purpose registers: 03765 // 64: mq 03766 // 65: lr 03767 // 66: ctr 03768 // 67: ap 03769 // 68-75 cr0-7 03770 // 76: xer 03771 AssignToArrayRange(Builder, Address, Four8, 64, 76); 03772 03773 // 77-108: v0-31, the 16-byte vector registers 03774 AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); 03775 03776 // 109: vrsave 03777 // 110: vscr 03778 // 111: spe_acc 03779 // 112: spefscr 03780 // 113: sfp 03781 AssignToArrayRange(Builder, Address, Four8, 109, 113); 03782 03783 return false; 03784 } 03785 03786 bool 03787 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( 03788 CodeGen::CodeGenFunction &CGF, 03789 llvm::Value *Address) const { 03790 03791 return PPC64_initDwarfEHRegSizeTable(CGF, Address); 03792 } 03793 03794 bool 03795 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 03796 llvm::Value *Address) const { 03797 03798 return PPC64_initDwarfEHRegSizeTable(CGF, Address); 03799 } 03800 03801 //===----------------------------------------------------------------------===// 03802 // AArch64 ABI Implementation 03803 //===----------------------------------------------------------------------===// 03804 03805 namespace { 03806 03807 class AArch64ABIInfo : public ABIInfo { 03808 public: 03809 enum ABIKind { 03810 AAPCS = 0, 03811 DarwinPCS 03812 }; 03813 03814 private: 03815 ABIKind Kind; 03816 03817 public: 03818 AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} 03819 03820 private: 03821 ABIKind getABIKind() const { return Kind; } 03822 bool isDarwinPCS() const { return Kind == DarwinPCS; } 03823 03824 ABIArgInfo classifyReturnType(QualType RetTy) const; 03825 ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &AllocatedVFP, 03826 bool &IsHA, unsigned &AllocatedGPR, 03827 bool &IsSmallAggr, bool IsNamedArg) const; 03828 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 03829 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 03830 uint64_t Members) const override; 03831 03832 bool isIllegalVectorType(QualType Ty) const; 03833 03834 void computeInfo(CGFunctionInfo &FI) const override { 03835 // To correctly handle Homogeneous Aggregate, we need to keep track of the 03836 // number of SIMD and Floating-point registers allocated so far. 03837 // If the argument is an HFA or an HVA and there are sufficient unallocated 03838 // SIMD and Floating-point registers, then the argument is allocated to SIMD 03839 // and Floating-point Registers (with one register per member of the HFA or 03840 // HVA). Otherwise, the NSRN is set to 8. 03841 unsigned AllocatedVFP = 0; 03842 03843 // To correctly handle small aggregates, we need to keep track of the number 03844 // of GPRs allocated so far. If the small aggregate can't all fit into 03845 // registers, it will be on stack. We don't allow the aggregate to be 03846 // partially in registers. 03847 unsigned AllocatedGPR = 0; 03848 03849 // Find the number of named arguments. Variadic arguments get special 03850 // treatment with the Darwin ABI. 03851 unsigned NumRequiredArgs = FI.getNumRequiredArgs(); 03852 03853 if (!getCXXABI().classifyReturnType(FI)) 03854 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 03855 unsigned ArgNo = 0; 03856 for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); 03857 it != ie; ++it, ++ArgNo) { 03858 unsigned PreAllocation = AllocatedVFP, PreGPR = AllocatedGPR; 03859 bool IsHA = false, IsSmallAggr = false; 03860 const unsigned NumVFPs = 8; 03861 const unsigned NumGPRs = 8; 03862 bool IsNamedArg = ArgNo < NumRequiredArgs; 03863 it->info = classifyArgumentType(it->type, AllocatedVFP, IsHA, 03864 AllocatedGPR, IsSmallAggr, IsNamedArg); 03865 03866 // Under AAPCS the 64-bit stack slot alignment means we can't pass HAs 03867 // as sequences of floats since they'll get "holes" inserted as 03868 // padding by the back end. 03869 if (IsHA && AllocatedVFP > NumVFPs && !isDarwinPCS() && 03870 getContext().getTypeAlign(it->type) < 64) { 03871 uint32_t NumStackSlots = getContext().getTypeSize(it->type); 03872 NumStackSlots = llvm::RoundUpToAlignment(NumStackSlots, 64) / 64; 03873 03874 llvm::Type *CoerceTy = llvm::ArrayType::get( 03875 llvm::Type::getDoubleTy(getVMContext()), NumStackSlots); 03876 it->info = ABIArgInfo::getDirect(CoerceTy); 03877 } 03878 03879 // If we do not have enough VFP registers for the HA, any VFP registers 03880 // that are unallocated are marked as unavailable. To achieve this, we add 03881 // padding of (NumVFPs - PreAllocation) floats. 03882 if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { 03883 llvm::Type *PaddingTy = llvm::ArrayType::get( 03884 llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); 03885 it->info.setPaddingType(PaddingTy); 03886 } 03887 03888 // If we do not have enough GPRs for the small aggregate, any GPR regs 03889 // that are unallocated are marked as unavailable. 03890 if (IsSmallAggr && AllocatedGPR > NumGPRs && PreGPR < NumGPRs) { 03891 llvm::Type *PaddingTy = llvm::ArrayType::get( 03892 llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreGPR); 03893 it->info = 03894 ABIArgInfo::getDirect(it->info.getCoerceToType(), 0, PaddingTy); 03895 } 03896 } 03897 } 03898 03899 llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, 03900 CodeGenFunction &CGF) const; 03901 03902 llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, 03903 CodeGenFunction &CGF) const; 03904 03905 virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 03906 CodeGenFunction &CGF) const override { 03907 return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) 03908 : EmitAAPCSVAArg(VAListAddr, Ty, CGF); 03909 } 03910 }; 03911 03912 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { 03913 public: 03914 AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) 03915 : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} 03916 03917 StringRef getARCRetainAutoreleasedReturnValueMarker() const { 03918 return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; 03919 } 03920 03921 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; } 03922 03923 virtual bool doesReturnSlotInterfereWithArgs() const { return false; } 03924 }; 03925 } 03926 03927 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty, 03928 unsigned &AllocatedVFP, 03929 bool &IsHA, 03930 unsigned &AllocatedGPR, 03931 bool &IsSmallAggr, 03932 bool IsNamedArg) const { 03933 Ty = useFirstFieldIfTransparentUnion(Ty); 03934 03935 // Handle illegal vector types here. 03936 if (isIllegalVectorType(Ty)) { 03937 uint64_t Size = getContext().getTypeSize(Ty); 03938 if (Size <= 32) { 03939 llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); 03940 AllocatedGPR++; 03941 return ABIArgInfo::getDirect(ResType); 03942 } 03943 if (Size == 64) { 03944 llvm::Type *ResType = 03945 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); 03946 AllocatedVFP++; 03947 return ABIArgInfo::getDirect(ResType); 03948 } 03949 if (Size == 128) { 03950 llvm::Type *ResType = 03951 llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); 03952 AllocatedVFP++; 03953 return ABIArgInfo::getDirect(ResType); 03954 } 03955 AllocatedGPR++; 03956 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 03957 } 03958 if (Ty->isVectorType()) 03959 // Size of a legal vector should be either 64 or 128. 03960 AllocatedVFP++; 03961 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 03962 if (BT->getKind() == BuiltinType::Half || 03963 BT->getKind() == BuiltinType::Float || 03964 BT->getKind() == BuiltinType::Double || 03965 BT->getKind() == BuiltinType::LongDouble) 03966 AllocatedVFP++; 03967 } 03968 03969 if (!isAggregateTypeForABI(Ty)) { 03970 // Treat an enum type as its underlying type. 03971 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 03972 Ty = EnumTy->getDecl()->getIntegerType(); 03973 03974 if (!Ty->isFloatingType() && !Ty->isVectorType()) { 03975 unsigned Alignment = getContext().getTypeAlign(Ty); 03976 if (!isDarwinPCS() && Alignment > 64) 03977 AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); 03978 03979 int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; 03980 AllocatedGPR += RegsNeeded; 03981 } 03982 return (Ty->isPromotableIntegerType() && isDarwinPCS() 03983 ? ABIArgInfo::getExtend() 03984 : ABIArgInfo::getDirect()); 03985 } 03986 03987 // Structures with either a non-trivial destructor or a non-trivial 03988 // copy constructor are always indirect. 03989 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 03990 AllocatedGPR++; 03991 return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == 03992 CGCXXABI::RAA_DirectInMemory); 03993 } 03994 03995 // Empty records are always ignored on Darwin, but actually passed in C++ mode 03996 // elsewhere for GNU compatibility. 03997 if (isEmptyRecord(getContext(), Ty, true)) { 03998 if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) 03999 return ABIArgInfo::getIgnore(); 04000 04001 ++AllocatedGPR; 04002 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 04003 } 04004 04005 // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. 04006 const Type *Base = nullptr; 04007 uint64_t Members = 0; 04008 if (isHomogeneousAggregate(Ty, Base, Members)) { 04009 IsHA = true; 04010 if (!IsNamedArg && isDarwinPCS()) { 04011 // With the Darwin ABI, variadic arguments are always passed on the stack 04012 // and should not be expanded. Treat variadic HFAs as arrays of doubles. 04013 uint64_t Size = getContext().getTypeSize(Ty); 04014 llvm::Type *BaseTy = llvm::Type::getDoubleTy(getVMContext()); 04015 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); 04016 } 04017 AllocatedVFP += Members; 04018 return ABIArgInfo::getExpand(); 04019 } 04020 04021 // Aggregates <= 16 bytes are passed directly in registers or on the stack. 04022 uint64_t Size = getContext().getTypeSize(Ty); 04023 if (Size <= 128) { 04024 unsigned Alignment = getContext().getTypeAlign(Ty); 04025 if (!isDarwinPCS() && Alignment > 64) 04026 AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); 04027 04028 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes 04029 AllocatedGPR += Size / 64; 04030 IsSmallAggr = true; 04031 // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. 04032 // For aggregates with 16-byte alignment, we use i128. 04033 if (Alignment < 128 && Size == 128) { 04034 llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); 04035 return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); 04036 } 04037 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); 04038 } 04039 04040 AllocatedGPR++; 04041 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 04042 } 04043 04044 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { 04045 if (RetTy->isVoidType()) 04046 return ABIArgInfo::getIgnore(); 04047 04048 // Large vector types should be returned via memory. 04049 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) 04050 return ABIArgInfo::getIndirect(0); 04051 04052 if (!isAggregateTypeForABI(RetTy)) { 04053 // Treat an enum type as its underlying type. 04054 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 04055 RetTy = EnumTy->getDecl()->getIntegerType(); 04056 04057 return (RetTy->isPromotableIntegerType() && isDarwinPCS() 04058 ? ABIArgInfo::getExtend() 04059 : ABIArgInfo::getDirect()); 04060 } 04061 04062 if (isEmptyRecord(getContext(), RetTy, true)) 04063 return ABIArgInfo::getIgnore(); 04064 04065 const Type *Base = nullptr; 04066 uint64_t Members = 0; 04067 if (isHomogeneousAggregate(RetTy, Base, Members)) 04068 // Homogeneous Floating-point Aggregates (HFAs) are returned directly. 04069 return ABIArgInfo::getDirect(); 04070 04071 // Aggregates <= 16 bytes are returned directly in registers or on the stack. 04072 uint64_t Size = getContext().getTypeSize(RetTy); 04073 if (Size <= 128) { 04074 Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes 04075 return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); 04076 } 04077 04078 return ABIArgInfo::getIndirect(0); 04079 } 04080 04081 /// isIllegalVectorType - check whether the vector type is legal for AArch64. 04082 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { 04083 if (const VectorType *VT = Ty->getAs<VectorType>()) { 04084 // Check whether VT is legal. 04085 unsigned NumElements = VT->getNumElements(); 04086 uint64_t Size = getContext().getTypeSize(VT); 04087 // NumElements should be power of 2 between 1 and 16. 04088 if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) 04089 return true; 04090 return Size != 64 && (Size != 128 || NumElements == 1); 04091 } 04092 return false; 04093 } 04094 04095 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 04096 // Homogeneous aggregates for AAPCS64 must have base types of a floating 04097 // point type or a short-vector type. This is the same as the 32-bit ABI, 04098 // but with the difference that any floating-point type is allowed, 04099 // including __fp16. 04100 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 04101 if (BT->isFloatingPoint()) 04102 return true; 04103 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 04104 unsigned VecSize = getContext().getTypeSize(VT); 04105 if (VecSize == 64 || VecSize == 128) 04106 return true; 04107 } 04108 return false; 04109 } 04110 04111 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 04112 uint64_t Members) const { 04113 return Members <= 4; 04114 } 04115 04116 llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, 04117 CodeGenFunction &CGF) const { 04118 unsigned AllocatedGPR = 0, AllocatedVFP = 0; 04119 bool IsHA = false, IsSmallAggr = false; 04120 ABIArgInfo AI = classifyArgumentType(Ty, AllocatedVFP, IsHA, AllocatedGPR, 04121 IsSmallAggr, false /*IsNamedArg*/); 04122 bool IsIndirect = AI.isIndirect(); 04123 04124 // The AArch64 va_list type and handling is specified in the Procedure Call 04125 // Standard, section B.4: 04126 // 04127 // struct { 04128 // void *__stack; 04129 // void *__gr_top; 04130 // void *__vr_top; 04131 // int __gr_offs; 04132 // int __vr_offs; 04133 // }; 04134 04135 llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); 04136 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 04137 llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); 04138 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 04139 auto &Ctx = CGF.getContext(); 04140 04141 llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; 04142 int reg_top_index; 04143 int RegSize; 04144 if (AllocatedGPR) { 04145 assert(!AllocatedVFP && "Arguments never split between int & VFP regs"); 04146 // 3 is the field number of __gr_offs 04147 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); 04148 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); 04149 reg_top_index = 1; // field number for __gr_top 04150 RegSize = 8 * AllocatedGPR; 04151 } else { 04152 assert(!AllocatedGPR && "Argument must go in VFP or int regs"); 04153 // 4 is the field number of __vr_offs. 04154 reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); 04155 reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); 04156 reg_top_index = 2; // field number for __vr_top 04157 RegSize = 16 * AllocatedVFP; 04158 } 04159 04160 //======================================= 04161 // Find out where argument was passed 04162 //======================================= 04163 04164 // If reg_offs >= 0 we're already using the stack for this type of 04165 // argument. We don't want to keep updating reg_offs (in case it overflows, 04166 // though anyone passing 2GB of arguments, each at most 16 bytes, deserves 04167 // whatever they get). 04168 llvm::Value *UsingStack = nullptr; 04169 UsingStack = CGF.Builder.CreateICmpSGE( 04170 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); 04171 04172 CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); 04173 04174 // Otherwise, at least some kind of argument could go in these registers, the 04175 // question is whether this particular type is too big. 04176 CGF.EmitBlock(MaybeRegBlock); 04177 04178 // Integer arguments may need to correct register alignment (for example a 04179 // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we 04180 // align __gr_offs to calculate the potential address. 04181 if (AllocatedGPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { 04182 int Align = Ctx.getTypeAlign(Ty) / 8; 04183 04184 reg_offs = CGF.Builder.CreateAdd( 04185 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), 04186 "align_regoffs"); 04187 reg_offs = CGF.Builder.CreateAnd( 04188 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), 04189 "aligned_regoffs"); 04190 } 04191 04192 // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. 04193 llvm::Value *NewOffset = nullptr; 04194 NewOffset = CGF.Builder.CreateAdd( 04195 reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); 04196 CGF.Builder.CreateStore(NewOffset, reg_offs_p); 04197 04198 // Now we're in a position to decide whether this argument really was in 04199 // registers or not. 04200 llvm::Value *InRegs = nullptr; 04201 InRegs = CGF.Builder.CreateICmpSLE( 04202 NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); 04203 04204 CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); 04205 04206 //======================================= 04207 // Argument was in registers 04208 //======================================= 04209 04210 // Now we emit the code for if the argument was originally passed in 04211 // registers. First start the appropriate block: 04212 CGF.EmitBlock(InRegBlock); 04213 04214 llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; 04215 reg_top_p = 04216 CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); 04217 reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); 04218 llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); 04219 llvm::Value *RegAddr = nullptr; 04220 llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 04221 04222 if (IsIndirect) { 04223 // If it's been passed indirectly (actually a struct), whatever we find from 04224 // stored registers or on the stack will actually be a struct **. 04225 MemTy = llvm::PointerType::getUnqual(MemTy); 04226 } 04227 04228 const Type *Base = nullptr; 04229 uint64_t NumMembers = 0; 04230 bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); 04231 if (IsHFA && NumMembers > 1) { 04232 // Homogeneous aggregates passed in registers will have their elements split 04233 // and stored 16-bytes apart regardless of size (they're notionally in qN, 04234 // qN+1, ...). We reload and store into a temporary local variable 04235 // contiguously. 04236 assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); 04237 llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); 04238 llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); 04239 llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); 04240 int Offset = 0; 04241 04242 if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) 04243 Offset = 16 - Ctx.getTypeSize(Base) / 8; 04244 for (unsigned i = 0; i < NumMembers; ++i) { 04245 llvm::Value *BaseOffset = 04246 llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); 04247 llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); 04248 LoadAddr = CGF.Builder.CreateBitCast( 04249 LoadAddr, llvm::PointerType::getUnqual(BaseTy)); 04250 llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); 04251 04252 llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); 04253 CGF.Builder.CreateStore(Elem, StoreAddr); 04254 } 04255 04256 RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); 04257 } else { 04258 // Otherwise the object is contiguous in memory 04259 unsigned BeAlign = reg_top_index == 2 ? 16 : 8; 04260 if (CGF.CGM.getDataLayout().isBigEndian() && 04261 (IsHFA || !isAggregateTypeForABI(Ty)) && 04262 Ctx.getTypeSize(Ty) < (BeAlign * 8)) { 04263 int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; 04264 BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); 04265 04266 BaseAddr = CGF.Builder.CreateAdd( 04267 BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); 04268 04269 BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); 04270 } 04271 04272 RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); 04273 } 04274 04275 CGF.EmitBranch(ContBlock); 04276 04277 //======================================= 04278 // Argument was on the stack 04279 //======================================= 04280 CGF.EmitBlock(OnStackBlock); 04281 04282 llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; 04283 stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); 04284 OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); 04285 04286 // Again, stack arguments may need realigmnent. In this case both integer and 04287 // floating-point ones might be affected. 04288 if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { 04289 int Align = Ctx.getTypeAlign(Ty) / 8; 04290 04291 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); 04292 04293 OnStackAddr = CGF.Builder.CreateAdd( 04294 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), 04295 "align_stack"); 04296 OnStackAddr = CGF.Builder.CreateAnd( 04297 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), 04298 "align_stack"); 04299 04300 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); 04301 } 04302 04303 uint64_t StackSize; 04304 if (IsIndirect) 04305 StackSize = 8; 04306 else 04307 StackSize = Ctx.getTypeSize(Ty) / 8; 04308 04309 // All stack slots are 8 bytes 04310 StackSize = llvm::RoundUpToAlignment(StackSize, 8); 04311 04312 llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); 04313 llvm::Value *NewStack = 04314 CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); 04315 04316 // Write the new value of __stack for the next call to va_arg 04317 CGF.Builder.CreateStore(NewStack, stack_p); 04318 04319 if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && 04320 Ctx.getTypeSize(Ty) < 64) { 04321 int Offset = 8 - Ctx.getTypeSize(Ty) / 8; 04322 OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); 04323 04324 OnStackAddr = CGF.Builder.CreateAdd( 04325 OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); 04326 04327 OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); 04328 } 04329 04330 OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); 04331 04332 CGF.EmitBranch(ContBlock); 04333 04334 //======================================= 04335 // Tidy up 04336 //======================================= 04337 CGF.EmitBlock(ContBlock); 04338 04339 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); 04340 ResAddr->addIncoming(RegAddr, InRegBlock); 04341 ResAddr->addIncoming(OnStackAddr, OnStackBlock); 04342 04343 if (IsIndirect) 04344 return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); 04345 04346 return ResAddr; 04347 } 04348 04349 llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, 04350 CodeGenFunction &CGF) const { 04351 // We do not support va_arg for aggregates or illegal vector types. 04352 // Lower VAArg here for these cases and use the LLVM va_arg instruction for 04353 // other cases. 04354 if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) 04355 return nullptr; 04356 04357 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; 04358 uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; 04359 04360 const Type *Base = nullptr; 04361 uint64_t Members = 0; 04362 bool isHA = isHomogeneousAggregate(Ty, Base, Members); 04363 04364 bool isIndirect = false; 04365 // Arguments bigger than 16 bytes which aren't homogeneous aggregates should 04366 // be passed indirectly. 04367 if (Size > 16 && !isHA) { 04368 isIndirect = true; 04369 Size = 8; 04370 Align = 8; 04371 } 04372 04373 llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); 04374 llvm::Type *BPP = llvm::PointerType::getUnqual(BP); 04375 04376 CGBuilderTy &Builder = CGF.Builder; 04377 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 04378 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 04379 04380 if (isEmptyRecord(getContext(), Ty, true)) { 04381 // These are ignored for parameter passing purposes. 04382 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 04383 return Builder.CreateBitCast(Addr, PTy); 04384 } 04385 04386 const uint64_t MinABIAlign = 8; 04387 if (Align > MinABIAlign) { 04388 llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); 04389 Addr = Builder.CreateGEP(Addr, Offset); 04390 llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); 04391 llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); 04392 llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); 04393 Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); 04394 } 04395 04396 uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); 04397 llvm::Value *NextAddr = Builder.CreateGEP( 04398 Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); 04399 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 04400 04401 if (isIndirect) 04402 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); 04403 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 04404 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 04405 04406 return AddrTyped; 04407 } 04408 04409 //===----------------------------------------------------------------------===// 04410 // ARM ABI Implementation 04411 //===----------------------------------------------------------------------===// 04412 04413 namespace { 04414 04415 class ARMABIInfo : public ABIInfo { 04416 public: 04417 enum ABIKind { 04418 APCS = 0, 04419 AAPCS = 1, 04420 AAPCS_VFP 04421 }; 04422 04423 private: 04424 ABIKind Kind; 04425 mutable int VFPRegs[16]; 04426 const unsigned NumVFPs; 04427 const unsigned NumGPRs; 04428 mutable unsigned AllocatedGPRs; 04429 mutable unsigned AllocatedVFPs; 04430 04431 public: 04432 ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind), 04433 NumVFPs(16), NumGPRs(4) { 04434 setRuntimeCC(); 04435 resetAllocatedRegs(); 04436 } 04437 04438 bool isEABI() const { 04439 switch (getTarget().getTriple().getEnvironment()) { 04440 case llvm::Triple::Android: 04441 case llvm::Triple::EABI: 04442 case llvm::Triple::EABIHF: 04443 case llvm::Triple::GNUEABI: 04444 case llvm::Triple::GNUEABIHF: 04445 return true; 04446 default: 04447 return false; 04448 } 04449 } 04450 04451 bool isEABIHF() const { 04452 switch (getTarget().getTriple().getEnvironment()) { 04453 case llvm::Triple::EABIHF: 04454 case llvm::Triple::GNUEABIHF: 04455 return true; 04456 default: 04457 return false; 04458 } 04459 } 04460 04461 ABIKind getABIKind() const { return Kind; } 04462 04463 private: 04464 ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; 04465 ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, 04466 bool &IsCPRC) const; 04467 bool isIllegalVectorType(QualType Ty) const; 04468 04469 bool isHomogeneousAggregateBaseType(QualType Ty) const override; 04470 bool isHomogeneousAggregateSmallEnough(const Type *Ty, 04471 uint64_t Members) const override; 04472 04473 void computeInfo(CGFunctionInfo &FI) const override; 04474 04475 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 04476 CodeGenFunction &CGF) const override; 04477 04478 llvm::CallingConv::ID getLLVMDefaultCC() const; 04479 llvm::CallingConv::ID getABIDefaultCC() const; 04480 void setRuntimeCC(); 04481 04482 void markAllocatedGPRs(unsigned Alignment, unsigned NumRequired) const; 04483 void markAllocatedVFPs(unsigned Alignment, unsigned NumRequired) const; 04484 void resetAllocatedRegs(void) const; 04485 }; 04486 04487 class ARMTargetCodeGenInfo : public TargetCodeGenInfo { 04488 public: 04489 ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) 04490 :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} 04491 04492 const ARMABIInfo &getABIInfo() const { 04493 return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); 04494 } 04495 04496 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 04497 return 13; 04498 } 04499 04500 StringRef getARCRetainAutoreleasedReturnValueMarker() const override { 04501 return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; 04502 } 04503 04504 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 04505 llvm::Value *Address) const override { 04506 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 04507 04508 // 0-15 are the 16 integer registers. 04509 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); 04510 return false; 04511 } 04512 04513 unsigned getSizeOfUnwindException() const override { 04514 if (getABIInfo().isEABI()) return 88; 04515 return TargetCodeGenInfo::getSizeOfUnwindException(); 04516 } 04517 04518 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 04519 CodeGen::CodeGenModule &CGM) const override { 04520 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 04521 if (!FD) 04522 return; 04523 04524 const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); 04525 if (!Attr) 04526 return; 04527 04528 const char *Kind; 04529 switch (Attr->getInterrupt()) { 04530 case ARMInterruptAttr::Generic: Kind = ""; break; 04531 case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; 04532 case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; 04533 case ARMInterruptAttr::SWI: Kind = "SWI"; break; 04534 case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; 04535 case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; 04536 } 04537 04538 llvm::Function *Fn = cast<llvm::Function>(GV); 04539 04540 Fn->addFnAttr("interrupt", Kind); 04541 04542 if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) 04543 return; 04544 04545 // AAPCS guarantees that sp will be 8-byte aligned on any public interface, 04546 // however this is not necessarily true on taking any interrupt. Instruct 04547 // the backend to perform a realignment as part of the function prologue. 04548 llvm::AttrBuilder B; 04549 B.addStackAlignmentAttr(8); 04550 Fn->addAttributes(llvm::AttributeSet::FunctionIndex, 04551 llvm::AttributeSet::get(CGM.getLLVMContext(), 04552 llvm::AttributeSet::FunctionIndex, 04553 B)); 04554 } 04555 04556 }; 04557 04558 } 04559 04560 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 04561 // To correctly handle Homogeneous Aggregate, we need to keep track of the 04562 // VFP registers allocated so far. 04563 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive 04564 // VFP registers of the appropriate type unallocated then the argument is 04565 // allocated to the lowest-numbered sequence of such registers. 04566 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are 04567 // unallocated are marked as unavailable. 04568 resetAllocatedRegs(); 04569 04570 if (getCXXABI().classifyReturnType(FI)) { 04571 if (FI.getReturnInfo().isIndirect()) 04572 markAllocatedGPRs(1, 1); 04573 } else { 04574 FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); 04575 } 04576 for (auto &I : FI.arguments()) { 04577 unsigned PreAllocationVFPs = AllocatedVFPs; 04578 unsigned PreAllocationGPRs = AllocatedGPRs; 04579 bool IsCPRC = false; 04580 // 6.1.2.3 There is one VFP co-processor register class using registers 04581 // s0-s15 (d0-d7) for passing arguments. 04582 I.info = classifyArgumentType(I.type, FI.isVariadic(), IsCPRC); 04583 04584 // If we have allocated some arguments onto the stack (due to running 04585 // out of VFP registers), we cannot split an argument between GPRs and 04586 // the stack. If this situation occurs, we add padding to prevent the 04587 // GPRs from being used. In this situation, the current argument could 04588 // only be allocated by rule C.8, so rule C.6 would mark these GPRs as 04589 // unusable anyway. 04590 // We do not have to do this if the argument is being passed ByVal, as the 04591 // backend can handle that situation correctly. 04592 const bool StackUsed = PreAllocationGPRs > NumGPRs || PreAllocationVFPs > NumVFPs; 04593 const bool IsByVal = I.info.isIndirect() && I.info.getIndirectByVal(); 04594 if (!IsCPRC && PreAllocationGPRs < NumGPRs && AllocatedGPRs > NumGPRs && 04595 StackUsed && !IsByVal) { 04596 llvm::Type *PaddingTy = llvm::ArrayType::get( 04597 llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreAllocationGPRs); 04598 if (I.info.canHaveCoerceToType()) { 04599 I.info = ABIArgInfo::getDirect(I.info.getCoerceToType() /* type */, 04600 0 /* offset */, PaddingTy, true); 04601 } else { 04602 I.info = ABIArgInfo::getDirect(nullptr /* type */, 0 /* offset */, 04603 PaddingTy, true); 04604 } 04605 } 04606 } 04607 04608 // Always honor user-specified calling convention. 04609 if (FI.getCallingConvention() != llvm::CallingConv::C) 04610 return; 04611 04612 llvm::CallingConv::ID cc = getRuntimeCC(); 04613 if (cc != llvm::CallingConv::C) 04614 FI.setEffectiveCallingConvention(cc); 04615 } 04616 04617 /// Return the default calling convention that LLVM will use. 04618 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { 04619 // The default calling convention that LLVM will infer. 04620 if (isEABIHF()) 04621 return llvm::CallingConv::ARM_AAPCS_VFP; 04622 else if (isEABI()) 04623 return llvm::CallingConv::ARM_AAPCS; 04624 else 04625 return llvm::CallingConv::ARM_APCS; 04626 } 04627 04628 /// Return the calling convention that our ABI would like us to use 04629 /// as the C calling convention. 04630 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { 04631 switch (getABIKind()) { 04632 case APCS: return llvm::CallingConv::ARM_APCS; 04633 case AAPCS: return llvm::CallingConv::ARM_AAPCS; 04634 case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 04635 } 04636 llvm_unreachable("bad ABI kind"); 04637 } 04638 04639 void ARMABIInfo::setRuntimeCC() { 04640 assert(getRuntimeCC() == llvm::CallingConv::C); 04641 04642 // Don't muddy up the IR with a ton of explicit annotations if 04643 // they'd just match what LLVM will infer from the triple. 04644 llvm::CallingConv::ID abiCC = getABIDefaultCC(); 04645 if (abiCC != getLLVMDefaultCC()) 04646 RuntimeCC = abiCC; 04647 } 04648 04649 /// markAllocatedVFPs - update VFPRegs according to the alignment and 04650 /// number of VFP registers (unit is S register) requested. 04651 void ARMABIInfo::markAllocatedVFPs(unsigned Alignment, 04652 unsigned NumRequired) const { 04653 // Early Exit. 04654 if (AllocatedVFPs >= 16) { 04655 // We use AllocatedVFP > 16 to signal that some CPRCs were allocated on 04656 // the stack. 04657 AllocatedVFPs = 17; 04658 return; 04659 } 04660 // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive 04661 // VFP registers of the appropriate type unallocated then the argument is 04662 // allocated to the lowest-numbered sequence of such registers. 04663 for (unsigned I = 0; I < 16; I += Alignment) { 04664 bool FoundSlot = true; 04665 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) 04666 if (J >= 16 || VFPRegs[J]) { 04667 FoundSlot = false; 04668 break; 04669 } 04670 if (FoundSlot) { 04671 for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) 04672 VFPRegs[J] = 1; 04673 AllocatedVFPs += NumRequired; 04674 return; 04675 } 04676 } 04677 // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are 04678 // unallocated are marked as unavailable. 04679 for (unsigned I = 0; I < 16; I++) 04680 VFPRegs[I] = 1; 04681 AllocatedVFPs = 17; // We do not have enough VFP registers. 04682 } 04683 04684 /// Update AllocatedGPRs to record the number of general purpose registers 04685 /// which have been allocated. It is valid for AllocatedGPRs to go above 4, 04686 /// this represents arguments being stored on the stack. 04687 void ARMABIInfo::markAllocatedGPRs(unsigned Alignment, 04688 unsigned NumRequired) const { 04689 assert((Alignment == 1 || Alignment == 2) && "Alignment must be 4 or 8 bytes"); 04690 04691 if (Alignment == 2 && AllocatedGPRs & 0x1) 04692 AllocatedGPRs += 1; 04693 04694 AllocatedGPRs += NumRequired; 04695 } 04696 04697 void ARMABIInfo::resetAllocatedRegs(void) const { 04698 AllocatedGPRs = 0; 04699 AllocatedVFPs = 0; 04700 for (unsigned i = 0; i < NumVFPs; ++i) 04701 VFPRegs[i] = 0; 04702 } 04703 04704 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, 04705 bool &IsCPRC) const { 04706 // We update number of allocated VFPs according to 04707 // 6.1.2.1 The following argument types are VFP CPRCs: 04708 // A single-precision floating-point type (including promoted 04709 // half-precision types); A double-precision floating-point type; 04710 // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate 04711 // with a Base Type of a single- or double-precision floating-point type, 04712 // 64-bit containerized vectors or 128-bit containerized vectors with one 04713 // to four Elements. 04714 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; 04715 04716 Ty = useFirstFieldIfTransparentUnion(Ty); 04717 04718 // Handle illegal vector types here. 04719 if (isIllegalVectorType(Ty)) { 04720 uint64_t Size = getContext().getTypeSize(Ty); 04721 if (Size <= 32) { 04722 llvm::Type *ResType = 04723 llvm::Type::getInt32Ty(getVMContext()); 04724 markAllocatedGPRs(1, 1); 04725 return ABIArgInfo::getDirect(ResType); 04726 } 04727 if (Size == 64) { 04728 llvm::Type *ResType = llvm::VectorType::get( 04729 llvm::Type::getInt32Ty(getVMContext()), 2); 04730 if (getABIKind() == ARMABIInfo::AAPCS || isVariadic){ 04731 markAllocatedGPRs(2, 2); 04732 } else { 04733 markAllocatedVFPs(2, 2); 04734 IsCPRC = true; 04735 } 04736 return ABIArgInfo::getDirect(ResType); 04737 } 04738 if (Size == 128) { 04739 llvm::Type *ResType = llvm::VectorType::get( 04740 llvm::Type::getInt32Ty(getVMContext()), 4); 04741 if (getABIKind() == ARMABIInfo::AAPCS || isVariadic) { 04742 markAllocatedGPRs(2, 4); 04743 } else { 04744 markAllocatedVFPs(4, 4); 04745 IsCPRC = true; 04746 } 04747 return ABIArgInfo::getDirect(ResType); 04748 } 04749 markAllocatedGPRs(1, 1); 04750 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 04751 } 04752 // Update VFPRegs for legal vector types. 04753 if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { 04754 if (const VectorType *VT = Ty->getAs<VectorType>()) { 04755 uint64_t Size = getContext().getTypeSize(VT); 04756 // Size of a legal vector should be power of 2 and above 64. 04757 markAllocatedVFPs(Size >= 128 ? 4 : 2, Size / 32); 04758 IsCPRC = true; 04759 } 04760 } 04761 // Update VFPRegs for floating point types. 04762 if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { 04763 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 04764 if (BT->getKind() == BuiltinType::Half || 04765 BT->getKind() == BuiltinType::Float) { 04766 markAllocatedVFPs(1, 1); 04767 IsCPRC = true; 04768 } 04769 if (BT->getKind() == BuiltinType::Double || 04770 BT->getKind() == BuiltinType::LongDouble) { 04771 markAllocatedVFPs(2, 2); 04772 IsCPRC = true; 04773 } 04774 } 04775 } 04776 04777 if (!isAggregateTypeForABI(Ty)) { 04778 // Treat an enum type as its underlying type. 04779 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { 04780 Ty = EnumTy->getDecl()->getIntegerType(); 04781 } 04782 04783 unsigned Size = getContext().getTypeSize(Ty); 04784 if (!IsCPRC) 04785 markAllocatedGPRs(Size > 32 ? 2 : 1, (Size + 31) / 32); 04786 return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() 04787 : ABIArgInfo::getDirect()); 04788 } 04789 04790 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 04791 markAllocatedGPRs(1, 1); 04792 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 04793 } 04794 04795 // Ignore empty records. 04796 if (isEmptyRecord(getContext(), Ty, true)) 04797 return ABIArgInfo::getIgnore(); 04798 04799 if (IsEffectivelyAAPCS_VFP) { 04800 // Homogeneous Aggregates need to be expanded when we can fit the aggregate 04801 // into VFP registers. 04802 const Type *Base = nullptr; 04803 uint64_t Members = 0; 04804 if (isHomogeneousAggregate(Ty, Base, Members)) { 04805 assert(Base && "Base class should be set for homogeneous aggregate"); 04806 // Base can be a floating-point or a vector. 04807 if (Base->isVectorType()) { 04808 // ElementSize is in number of floats. 04809 unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; 04810 markAllocatedVFPs(ElementSize, 04811 Members * ElementSize); 04812 } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) 04813 markAllocatedVFPs(1, Members); 04814 else { 04815 assert(Base->isSpecificBuiltinType(BuiltinType::Double) || 04816 Base->isSpecificBuiltinType(BuiltinType::LongDouble)); 04817 markAllocatedVFPs(2, Members * 2); 04818 } 04819 IsCPRC = true; 04820 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); 04821 } 04822 } 04823 04824 // Support byval for ARM. 04825 // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at 04826 // most 8-byte. We realign the indirect argument if type alignment is bigger 04827 // than ABI alignment. 04828 uint64_t ABIAlign = 4; 04829 uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; 04830 if (getABIKind() == ARMABIInfo::AAPCS_VFP || 04831 getABIKind() == ARMABIInfo::AAPCS) 04832 ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); 04833 if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { 04834 // Update Allocated GPRs. Since this is only used when the size of the 04835 // argument is greater than 64 bytes, this will always use up any available 04836 // registers (of which there are 4). We also don't care about getting the 04837 // alignment right, because general-purpose registers cannot be back-filled. 04838 markAllocatedGPRs(1, 4); 04839 return ABIArgInfo::getIndirect(TyAlign, /*ByVal=*/true, 04840 /*Realign=*/TyAlign > ABIAlign); 04841 } 04842 04843 // Otherwise, pass by coercing to a structure of the appropriate size. 04844 llvm::Type* ElemTy; 04845 unsigned SizeRegs; 04846 // FIXME: Try to match the types of the arguments more accurately where 04847 // we can. 04848 if (getContext().getTypeAlign(Ty) <= 32) { 04849 ElemTy = llvm::Type::getInt32Ty(getVMContext()); 04850 SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; 04851 markAllocatedGPRs(1, SizeRegs); 04852 } else { 04853 ElemTy = llvm::Type::getInt64Ty(getVMContext()); 04854 SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; 04855 markAllocatedGPRs(2, SizeRegs * 2); 04856 } 04857 04858 return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); 04859 } 04860 04861 static bool isIntegerLikeType(QualType Ty, ASTContext &Context, 04862 llvm::LLVMContext &VMContext) { 04863 // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure 04864 // is called integer-like if its size is less than or equal to one word, and 04865 // the offset of each of its addressable sub-fields is zero. 04866 04867 uint64_t Size = Context.getTypeSize(Ty); 04868 04869 // Check that the type fits in a word. 04870 if (Size > 32) 04871 return false; 04872 04873 // FIXME: Handle vector types! 04874 if (Ty->isVectorType()) 04875 return false; 04876 04877 // Float types are never treated as "integer like". 04878 if (Ty->isRealFloatingType()) 04879 return false; 04880 04881 // If this is a builtin or pointer type then it is ok. 04882 if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) 04883 return true; 04884 04885 // Small complex integer types are "integer like". 04886 if (const ComplexType *CT = Ty->getAs<ComplexType>()) 04887 return isIntegerLikeType(CT->getElementType(), Context, VMContext); 04888 04889 // Single element and zero sized arrays should be allowed, by the definition 04890 // above, but they are not. 04891 04892 // Otherwise, it must be a record type. 04893 const RecordType *RT = Ty->getAs<RecordType>(); 04894 if (!RT) return false; 04895 04896 // Ignore records with flexible arrays. 04897 const RecordDecl *RD = RT->getDecl(); 04898 if (RD->hasFlexibleArrayMember()) 04899 return false; 04900 04901 // Check that all sub-fields are at offset 0, and are themselves "integer 04902 // like". 04903 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 04904 04905 bool HadField = false; 04906 unsigned idx = 0; 04907 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 04908 i != e; ++i, ++idx) { 04909 const FieldDecl *FD = *i; 04910 04911 // Bit-fields are not addressable, we only need to verify they are "integer 04912 // like". We still have to disallow a subsequent non-bitfield, for example: 04913 // struct { int : 0; int x } 04914 // is non-integer like according to gcc. 04915 if (FD->isBitField()) { 04916 if (!RD->isUnion()) 04917 HadField = true; 04918 04919 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 04920 return false; 04921 04922 continue; 04923 } 04924 04925 // Check if this field is at offset 0. 04926 if (Layout.getFieldOffset(idx) != 0) 04927 return false; 04928 04929 if (!isIntegerLikeType(FD->getType(), Context, VMContext)) 04930 return false; 04931 04932 // Only allow at most one field in a structure. This doesn't match the 04933 // wording above, but follows gcc in situations with a field following an 04934 // empty structure. 04935 if (!RD->isUnion()) { 04936 if (HadField) 04937 return false; 04938 04939 HadField = true; 04940 } 04941 } 04942 04943 return true; 04944 } 04945 04946 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, 04947 bool isVariadic) const { 04948 bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; 04949 04950 if (RetTy->isVoidType()) 04951 return ABIArgInfo::getIgnore(); 04952 04953 // Large vector types should be returned via memory. 04954 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { 04955 markAllocatedGPRs(1, 1); 04956 return ABIArgInfo::getIndirect(0); 04957 } 04958 04959 if (!isAggregateTypeForABI(RetTy)) { 04960 // Treat an enum type as its underlying type. 04961 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 04962 RetTy = EnumTy->getDecl()->getIntegerType(); 04963 04964 return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() 04965 : ABIArgInfo::getDirect(); 04966 } 04967 04968 // Are we following APCS? 04969 if (getABIKind() == APCS) { 04970 if (isEmptyRecord(getContext(), RetTy, false)) 04971 return ABIArgInfo::getIgnore(); 04972 04973 // Complex types are all returned as packed integers. 04974 // 04975 // FIXME: Consider using 2 x vector types if the back end handles them 04976 // correctly. 04977 if (RetTy->isAnyComplexType()) 04978 return ABIArgInfo::getDirect(llvm::IntegerType::get( 04979 getVMContext(), getContext().getTypeSize(RetTy))); 04980 04981 // Integer like structures are returned in r0. 04982 if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { 04983 // Return in the smallest viable integer type. 04984 uint64_t Size = getContext().getTypeSize(RetTy); 04985 if (Size <= 8) 04986 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 04987 if (Size <= 16) 04988 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 04989 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 04990 } 04991 04992 // Otherwise return in memory. 04993 markAllocatedGPRs(1, 1); 04994 return ABIArgInfo::getIndirect(0); 04995 } 04996 04997 // Otherwise this is an AAPCS variant. 04998 04999 if (isEmptyRecord(getContext(), RetTy, true)) 05000 return ABIArgInfo::getIgnore(); 05001 05002 // Check for homogeneous aggregates with AAPCS-VFP. 05003 if (IsEffectivelyAAPCS_VFP) { 05004 const Type *Base = nullptr; 05005 uint64_t Members; 05006 if (isHomogeneousAggregate(RetTy, Base, Members)) { 05007 assert(Base && "Base class should be set for homogeneous aggregate"); 05008 // Homogeneous Aggregates are returned directly. 05009 return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); 05010 } 05011 } 05012 05013 // Aggregates <= 4 bytes are returned in r0; other aggregates 05014 // are returned indirectly. 05015 uint64_t Size = getContext().getTypeSize(RetTy); 05016 if (Size <= 32) { 05017 if (getDataLayout().isBigEndian()) 05018 // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) 05019 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 05020 05021 // Return in the smallest viable integer type. 05022 if (Size <= 8) 05023 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 05024 if (Size <= 16) 05025 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 05026 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 05027 } 05028 05029 markAllocatedGPRs(1, 1); 05030 return ABIArgInfo::getIndirect(0); 05031 } 05032 05033 /// isIllegalVector - check whether Ty is an illegal vector type. 05034 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { 05035 if (const VectorType *VT = Ty->getAs<VectorType>()) { 05036 // Check whether VT is legal. 05037 unsigned NumElements = VT->getNumElements(); 05038 uint64_t Size = getContext().getTypeSize(VT); 05039 // NumElements should be power of 2. 05040 if ((NumElements & (NumElements - 1)) != 0) 05041 return true; 05042 // Size should be greater than 32 bits. 05043 return Size <= 32; 05044 } 05045 return false; 05046 } 05047 05048 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { 05049 // Homogeneous aggregates for AAPCS-VFP must have base types of float, 05050 // double, or 64-bit or 128-bit vectors. 05051 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { 05052 if (BT->getKind() == BuiltinType::Float || 05053 BT->getKind() == BuiltinType::Double || 05054 BT->getKind() == BuiltinType::LongDouble) 05055 return true; 05056 } else if (const VectorType *VT = Ty->getAs<VectorType>()) { 05057 unsigned VecSize = getContext().getTypeSize(VT); 05058 if (VecSize == 64 || VecSize == 128) 05059 return true; 05060 } 05061 return false; 05062 } 05063 05064 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, 05065 uint64_t Members) const { 05066 return Members <= 4; 05067 } 05068 05069 llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05070 CodeGenFunction &CGF) const { 05071 llvm::Type *BP = CGF.Int8PtrTy; 05072 llvm::Type *BPP = CGF.Int8PtrPtrTy; 05073 05074 CGBuilderTy &Builder = CGF.Builder; 05075 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 05076 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 05077 05078 if (isEmptyRecord(getContext(), Ty, true)) { 05079 // These are ignored for parameter passing purposes. 05080 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 05081 return Builder.CreateBitCast(Addr, PTy); 05082 } 05083 05084 uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; 05085 uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; 05086 bool IsIndirect = false; 05087 05088 // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for 05089 // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. 05090 if (getABIKind() == ARMABIInfo::AAPCS_VFP || 05091 getABIKind() == ARMABIInfo::AAPCS) 05092 TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); 05093 else 05094 TyAlign = 4; 05095 // Use indirect if size of the illegal vector is bigger than 16 bytes. 05096 if (isIllegalVectorType(Ty) && Size > 16) { 05097 IsIndirect = true; 05098 Size = 4; 05099 TyAlign = 4; 05100 } 05101 05102 // Handle address alignment for ABI alignment > 4 bytes. 05103 if (TyAlign > 4) { 05104 assert((TyAlign & (TyAlign - 1)) == 0 && 05105 "Alignment is not power of 2!"); 05106 llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); 05107 AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); 05108 AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); 05109 Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); 05110 } 05111 05112 uint64_t Offset = 05113 llvm::RoundUpToAlignment(Size, 4); 05114 llvm::Value *NextAddr = 05115 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 05116 "ap.next"); 05117 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 05118 05119 if (IsIndirect) 05120 Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); 05121 else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { 05122 // We can't directly cast ap.cur to pointer to a vector type, since ap.cur 05123 // may not be correctly aligned for the vector type. We create an aligned 05124 // temporary space and copy the content over from ap.cur to the temporary 05125 // space. This is necessary if the natural alignment of the type is greater 05126 // than the ABI alignment. 05127 llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); 05128 CharUnits CharSize = getContext().getTypeSizeInChars(Ty); 05129 llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), 05130 "var.align"); 05131 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); 05132 llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); 05133 Builder.CreateMemCpy(Dst, Src, 05134 llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), 05135 TyAlign, false); 05136 Addr = AlignedTemp; //The content is in aligned location. 05137 } 05138 llvm::Type *PTy = 05139 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 05140 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 05141 05142 return AddrTyped; 05143 } 05144 05145 namespace { 05146 05147 class NaClARMABIInfo : public ABIInfo { 05148 public: 05149 NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) 05150 : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} 05151 void computeInfo(CGFunctionInfo &FI) const override; 05152 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05153 CodeGenFunction &CGF) const override; 05154 private: 05155 PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. 05156 ARMABIInfo NInfo; // Used for everything else. 05157 }; 05158 05159 class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { 05160 public: 05161 NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) 05162 : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} 05163 }; 05164 05165 } 05166 05167 void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { 05168 if (FI.getASTCallingConvention() == CC_PnaclCall) 05169 PInfo.computeInfo(FI); 05170 else 05171 static_cast<const ABIInfo&>(NInfo).computeInfo(FI); 05172 } 05173 05174 llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05175 CodeGenFunction &CGF) const { 05176 // Always use the native convention; calling pnacl-style varargs functions 05177 // is unsupported. 05178 return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); 05179 } 05180 05181 //===----------------------------------------------------------------------===// 05182 // NVPTX ABI Implementation 05183 //===----------------------------------------------------------------------===// 05184 05185 namespace { 05186 05187 class NVPTXABIInfo : public ABIInfo { 05188 public: 05189 NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 05190 05191 ABIArgInfo classifyReturnType(QualType RetTy) const; 05192 ABIArgInfo classifyArgumentType(QualType Ty) const; 05193 05194 void computeInfo(CGFunctionInfo &FI) const override; 05195 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05196 CodeGenFunction &CFG) const override; 05197 }; 05198 05199 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { 05200 public: 05201 NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) 05202 : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} 05203 05204 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 05205 CodeGen::CodeGenModule &M) const override; 05206 private: 05207 // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the 05208 // resulting MDNode to the nvvm.annotations MDNode. 05209 static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); 05210 }; 05211 05212 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { 05213 if (RetTy->isVoidType()) 05214 return ABIArgInfo::getIgnore(); 05215 05216 // note: this is different from default ABI 05217 if (!RetTy->isScalarType()) 05218 return ABIArgInfo::getDirect(); 05219 05220 // Treat an enum type as its underlying type. 05221 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 05222 RetTy = EnumTy->getDecl()->getIntegerType(); 05223 05224 return (RetTy->isPromotableIntegerType() ? 05225 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 05226 } 05227 05228 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { 05229 // Treat an enum type as its underlying type. 05230 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 05231 Ty = EnumTy->getDecl()->getIntegerType(); 05232 05233 // Return aggregates type as indirect by value 05234 if (isAggregateTypeForABI(Ty)) 05235 return ABIArgInfo::getIndirect(0, /* byval */ true); 05236 05237 return (Ty->isPromotableIntegerType() ? 05238 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 05239 } 05240 05241 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { 05242 if (!getCXXABI().classifyReturnType(FI)) 05243 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 05244 for (auto &I : FI.arguments()) 05245 I.info = classifyArgumentType(I.type); 05246 05247 // Always honor user-specified calling convention. 05248 if (FI.getCallingConvention() != llvm::CallingConv::C) 05249 return; 05250 05251 FI.setEffectiveCallingConvention(getRuntimeCC()); 05252 } 05253 05254 llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05255 CodeGenFunction &CFG) const { 05256 llvm_unreachable("NVPTX does not support varargs"); 05257 } 05258 05259 void NVPTXTargetCodeGenInfo:: 05260 SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 05261 CodeGen::CodeGenModule &M) const{ 05262 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 05263 if (!FD) return; 05264 05265 llvm::Function *F = cast<llvm::Function>(GV); 05266 05267 // Perform special handling in OpenCL mode 05268 if (M.getLangOpts().OpenCL) { 05269 // Use OpenCL function attributes to check for kernel functions 05270 // By default, all functions are device functions 05271 if (FD->hasAttr<OpenCLKernelAttr>()) { 05272 // OpenCL __kernel functions get kernel metadata 05273 // Create !{<func-ref>, metadata !"kernel", i32 1} node 05274 addNVVMMetadata(F, "kernel", 1); 05275 // And kernel functions are not subject to inlining 05276 F->addFnAttr(llvm::Attribute::NoInline); 05277 } 05278 } 05279 05280 // Perform special handling in CUDA mode. 05281 if (M.getLangOpts().CUDA) { 05282 // CUDA __global__ functions get a kernel metadata entry. Since 05283 // __global__ functions cannot be called from the device, we do not 05284 // need to set the noinline attribute. 05285 if (FD->hasAttr<CUDAGlobalAttr>()) { 05286 // Create !{<func-ref>, metadata !"kernel", i32 1} node 05287 addNVVMMetadata(F, "kernel", 1); 05288 } 05289 if (FD->hasAttr<CUDALaunchBoundsAttr>()) { 05290 // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node 05291 addNVVMMetadata(F, "maxntidx", 05292 FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads()); 05293 // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a 05294 // zero value from getMinBlocks either means it was not specified in 05295 // __launch_bounds__ or the user specified a 0 value. In both cases, we 05296 // don't have to add a PTX directive. 05297 int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks(); 05298 if (MinCTASM > 0) { 05299 // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node 05300 addNVVMMetadata(F, "minctasm", MinCTASM); 05301 } 05302 } 05303 } 05304 } 05305 05306 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, 05307 int Operand) { 05308 llvm::Module *M = F->getParent(); 05309 llvm::LLVMContext &Ctx = M->getContext(); 05310 05311 // Get "nvvm.annotations" metadata node 05312 llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); 05313 05314 llvm::Value *MDVals[] = { 05315 F, llvm::MDString::get(Ctx, Name), 05316 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand)}; 05317 // Append metadata to nvvm.annotations 05318 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 05319 } 05320 } 05321 05322 //===----------------------------------------------------------------------===// 05323 // SystemZ ABI Implementation 05324 //===----------------------------------------------------------------------===// 05325 05326 namespace { 05327 05328 class SystemZABIInfo : public ABIInfo { 05329 public: 05330 SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 05331 05332 bool isPromotableIntegerType(QualType Ty) const; 05333 bool isCompoundType(QualType Ty) const; 05334 bool isFPArgumentType(QualType Ty) const; 05335 05336 ABIArgInfo classifyReturnType(QualType RetTy) const; 05337 ABIArgInfo classifyArgumentType(QualType ArgTy) const; 05338 05339 void computeInfo(CGFunctionInfo &FI) const override { 05340 if (!getCXXABI().classifyReturnType(FI)) 05341 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 05342 for (auto &I : FI.arguments()) 05343 I.info = classifyArgumentType(I.type); 05344 } 05345 05346 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05347 CodeGenFunction &CGF) const override; 05348 }; 05349 05350 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { 05351 public: 05352 SystemZTargetCodeGenInfo(CodeGenTypes &CGT) 05353 : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} 05354 }; 05355 05356 } 05357 05358 bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { 05359 // Treat an enum type as its underlying type. 05360 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 05361 Ty = EnumTy->getDecl()->getIntegerType(); 05362 05363 // Promotable integer types are required to be promoted by the ABI. 05364 if (Ty->isPromotableIntegerType()) 05365 return true; 05366 05367 // 32-bit values must also be promoted. 05368 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 05369 switch (BT->getKind()) { 05370 case BuiltinType::Int: 05371 case BuiltinType::UInt: 05372 return true; 05373 default: 05374 return false; 05375 } 05376 return false; 05377 } 05378 05379 bool SystemZABIInfo::isCompoundType(QualType Ty) const { 05380 return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); 05381 } 05382 05383 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { 05384 if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) 05385 switch (BT->getKind()) { 05386 case BuiltinType::Float: 05387 case BuiltinType::Double: 05388 return true; 05389 default: 05390 return false; 05391 } 05392 05393 if (const RecordType *RT = Ty->getAsStructureType()) { 05394 const RecordDecl *RD = RT->getDecl(); 05395 bool Found = false; 05396 05397 // If this is a C++ record, check the bases first. 05398 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) 05399 for (const auto &I : CXXRD->bases()) { 05400 QualType Base = I.getType(); 05401 05402 // Empty bases don't affect things either way. 05403 if (isEmptyRecord(getContext(), Base, true)) 05404 continue; 05405 05406 if (Found) 05407 return false; 05408 Found = isFPArgumentType(Base); 05409 if (!Found) 05410 return false; 05411 } 05412 05413 // Check the fields. 05414 for (const auto *FD : RD->fields()) { 05415 // Empty bitfields don't affect things either way. 05416 // Unlike isSingleElementStruct(), empty structure and array fields 05417 // do count. So do anonymous bitfields that aren't zero-sized. 05418 if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) 05419 return true; 05420 05421 // Unlike isSingleElementStruct(), arrays do not count. 05422 // Nested isFPArgumentType structures still do though. 05423 if (Found) 05424 return false; 05425 Found = isFPArgumentType(FD->getType()); 05426 if (!Found) 05427 return false; 05428 } 05429 05430 // Unlike isSingleElementStruct(), trailing padding is allowed. 05431 // An 8-byte aligned struct s { float f; } is passed as a double. 05432 return Found; 05433 } 05434 05435 return false; 05436 } 05437 05438 llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05439 CodeGenFunction &CGF) const { 05440 // Assume that va_list type is correct; should be pointer to LLVM type: 05441 // struct { 05442 // i64 __gpr; 05443 // i64 __fpr; 05444 // i8 *__overflow_arg_area; 05445 // i8 *__reg_save_area; 05446 // }; 05447 05448 // Every argument occupies 8 bytes and is passed by preference in either 05449 // GPRs or FPRs. 05450 Ty = CGF.getContext().getCanonicalType(Ty); 05451 ABIArgInfo AI = classifyArgumentType(Ty); 05452 bool InFPRs = isFPArgumentType(Ty); 05453 05454 llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); 05455 bool IsIndirect = AI.isIndirect(); 05456 unsigned UnpaddedBitSize; 05457 if (IsIndirect) { 05458 APTy = llvm::PointerType::getUnqual(APTy); 05459 UnpaddedBitSize = 64; 05460 } else 05461 UnpaddedBitSize = getContext().getTypeSize(Ty); 05462 unsigned PaddedBitSize = 64; 05463 assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); 05464 05465 unsigned PaddedSize = PaddedBitSize / 8; 05466 unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; 05467 05468 unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; 05469 if (InFPRs) { 05470 MaxRegs = 4; // Maximum of 4 FPR arguments 05471 RegCountField = 1; // __fpr 05472 RegSaveIndex = 16; // save offset for f0 05473 RegPadding = 0; // floats are passed in the high bits of an FPR 05474 } else { 05475 MaxRegs = 5; // Maximum of 5 GPR arguments 05476 RegCountField = 0; // __gpr 05477 RegSaveIndex = 2; // save offset for r2 05478 RegPadding = Padding; // values are passed in the low bits of a GPR 05479 } 05480 05481 llvm::Value *RegCountPtr = 05482 CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); 05483 llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); 05484 llvm::Type *IndexTy = RegCount->getType(); 05485 llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); 05486 llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, 05487 "fits_in_regs"); 05488 05489 llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); 05490 llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); 05491 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); 05492 CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); 05493 05494 // Emit code to load the value if it was passed in registers. 05495 CGF.EmitBlock(InRegBlock); 05496 05497 // Work out the address of an argument register. 05498 llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); 05499 llvm::Value *ScaledRegCount = 05500 CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); 05501 llvm::Value *RegBase = 05502 llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); 05503 llvm::Value *RegOffset = 05504 CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); 05505 llvm::Value *RegSaveAreaPtr = 05506 CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); 05507 llvm::Value *RegSaveArea = 05508 CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); 05509 llvm::Value *RawRegAddr = 05510 CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); 05511 llvm::Value *RegAddr = 05512 CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); 05513 05514 // Update the register count 05515 llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); 05516 llvm::Value *NewRegCount = 05517 CGF.Builder.CreateAdd(RegCount, One, "reg_count"); 05518 CGF.Builder.CreateStore(NewRegCount, RegCountPtr); 05519 CGF.EmitBranch(ContBlock); 05520 05521 // Emit code to load the value if it was passed in memory. 05522 CGF.EmitBlock(InMemBlock); 05523 05524 // Work out the address of a stack argument. 05525 llvm::Value *OverflowArgAreaPtr = 05526 CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); 05527 llvm::Value *OverflowArgArea = 05528 CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); 05529 llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); 05530 llvm::Value *RawMemAddr = 05531 CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); 05532 llvm::Value *MemAddr = 05533 CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); 05534 05535 // Update overflow_arg_area_ptr pointer 05536 llvm::Value *NewOverflowArgArea = 05537 CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); 05538 CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); 05539 CGF.EmitBranch(ContBlock); 05540 05541 // Return the appropriate result. 05542 CGF.EmitBlock(ContBlock); 05543 llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); 05544 ResAddr->addIncoming(RegAddr, InRegBlock); 05545 ResAddr->addIncoming(MemAddr, InMemBlock); 05546 05547 if (IsIndirect) 05548 return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); 05549 05550 return ResAddr; 05551 } 05552 05553 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { 05554 if (RetTy->isVoidType()) 05555 return ABIArgInfo::getIgnore(); 05556 if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) 05557 return ABIArgInfo::getIndirect(0); 05558 return (isPromotableIntegerType(RetTy) ? 05559 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 05560 } 05561 05562 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { 05563 // Handle the generic C++ ABI. 05564 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 05565 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 05566 05567 // Integers and enums are extended to full register width. 05568 if (isPromotableIntegerType(Ty)) 05569 return ABIArgInfo::getExtend(); 05570 05571 // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. 05572 uint64_t Size = getContext().getTypeSize(Ty); 05573 if (Size != 8 && Size != 16 && Size != 32 && Size != 64) 05574 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 05575 05576 // Handle small structures. 05577 if (const RecordType *RT = Ty->getAs<RecordType>()) { 05578 // Structures with flexible arrays have variable length, so really 05579 // fail the size test above. 05580 const RecordDecl *RD = RT->getDecl(); 05581 if (RD->hasFlexibleArrayMember()) 05582 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 05583 05584 // The structure is passed as an unextended integer, a float, or a double. 05585 llvm::Type *PassTy; 05586 if (isFPArgumentType(Ty)) { 05587 assert(Size == 32 || Size == 64); 05588 if (Size == 32) 05589 PassTy = llvm::Type::getFloatTy(getVMContext()); 05590 else 05591 PassTy = llvm::Type::getDoubleTy(getVMContext()); 05592 } else 05593 PassTy = llvm::IntegerType::get(getVMContext(), Size); 05594 return ABIArgInfo::getDirect(PassTy); 05595 } 05596 05597 // Non-structure compounds are passed indirectly. 05598 if (isCompoundType(Ty)) 05599 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 05600 05601 return ABIArgInfo::getDirect(nullptr); 05602 } 05603 05604 //===----------------------------------------------------------------------===// 05605 // MSP430 ABI Implementation 05606 //===----------------------------------------------------------------------===// 05607 05608 namespace { 05609 05610 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { 05611 public: 05612 MSP430TargetCodeGenInfo(CodeGenTypes &CGT) 05613 : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} 05614 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 05615 CodeGen::CodeGenModule &M) const override; 05616 }; 05617 05618 } 05619 05620 void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, 05621 llvm::GlobalValue *GV, 05622 CodeGen::CodeGenModule &M) const { 05623 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 05624 if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { 05625 // Handle 'interrupt' attribute: 05626 llvm::Function *F = cast<llvm::Function>(GV); 05627 05628 // Step 1: Set ISR calling convention. 05629 F->setCallingConv(llvm::CallingConv::MSP430_INTR); 05630 05631 // Step 2: Add attributes goodness. 05632 F->addFnAttr(llvm::Attribute::NoInline); 05633 05634 // Step 3: Emit ISR vector alias. 05635 unsigned Num = attr->getNumber() / 2; 05636 llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, 05637 "__isr_" + Twine(Num), F); 05638 } 05639 } 05640 } 05641 05642 //===----------------------------------------------------------------------===// 05643 // MIPS ABI Implementation. This works for both little-endian and 05644 // big-endian variants. 05645 //===----------------------------------------------------------------------===// 05646 05647 namespace { 05648 class MipsABIInfo : public ABIInfo { 05649 bool IsO32; 05650 unsigned MinABIStackAlignInBytes, StackAlignInBytes; 05651 void CoerceToIntArgs(uint64_t TySize, 05652 SmallVectorImpl<llvm::Type *> &ArgList) const; 05653 llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; 05654 llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; 05655 llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; 05656 public: 05657 MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : 05658 ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), 05659 StackAlignInBytes(IsO32 ? 8 : 16) {} 05660 05661 ABIArgInfo classifyReturnType(QualType RetTy) const; 05662 ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; 05663 void computeInfo(CGFunctionInfo &FI) const override; 05664 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05665 CodeGenFunction &CGF) const override; 05666 }; 05667 05668 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { 05669 unsigned SizeOfUnwindException; 05670 public: 05671 MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) 05672 : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), 05673 SizeOfUnwindException(IsO32 ? 24 : 32) {} 05674 05675 int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { 05676 return 29; 05677 } 05678 05679 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 05680 CodeGen::CodeGenModule &CGM) const override { 05681 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 05682 if (!FD) return; 05683 llvm::Function *Fn = cast<llvm::Function>(GV); 05684 if (FD->hasAttr<Mips16Attr>()) { 05685 Fn->addFnAttr("mips16"); 05686 } 05687 else if (FD->hasAttr<NoMips16Attr>()) { 05688 Fn->addFnAttr("nomips16"); 05689 } 05690 } 05691 05692 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 05693 llvm::Value *Address) const override; 05694 05695 unsigned getSizeOfUnwindException() const override { 05696 return SizeOfUnwindException; 05697 } 05698 }; 05699 } 05700 05701 void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, 05702 SmallVectorImpl<llvm::Type *> &ArgList) const { 05703 llvm::IntegerType *IntTy = 05704 llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); 05705 05706 // Add (TySize / MinABIStackAlignInBytes) args of IntTy. 05707 for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) 05708 ArgList.push_back(IntTy); 05709 05710 // If necessary, add one more integer type to ArgList. 05711 unsigned R = TySize % (MinABIStackAlignInBytes * 8); 05712 05713 if (R) 05714 ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); 05715 } 05716 05717 // In N32/64, an aligned double precision floating point field is passed in 05718 // a register. 05719 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { 05720 SmallVector<llvm::Type*, 8> ArgList, IntArgList; 05721 05722 if (IsO32) { 05723 CoerceToIntArgs(TySize, ArgList); 05724 return llvm::StructType::get(getVMContext(), ArgList); 05725 } 05726 05727 if (Ty->isComplexType()) 05728 return CGT.ConvertType(Ty); 05729 05730 const RecordType *RT = Ty->getAs<RecordType>(); 05731 05732 // Unions/vectors are passed in integer registers. 05733 if (!RT || !RT->isStructureOrClassType()) { 05734 CoerceToIntArgs(TySize, ArgList); 05735 return llvm::StructType::get(getVMContext(), ArgList); 05736 } 05737 05738 const RecordDecl *RD = RT->getDecl(); 05739 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 05740 assert(!(TySize % 8) && "Size of structure must be multiple of 8."); 05741 05742 uint64_t LastOffset = 0; 05743 unsigned idx = 0; 05744 llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); 05745 05746 // Iterate over fields in the struct/class and check if there are any aligned 05747 // double fields. 05748 for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 05749 i != e; ++i, ++idx) { 05750 const QualType Ty = i->getType(); 05751 const BuiltinType *BT = Ty->getAs<BuiltinType>(); 05752 05753 if (!BT || BT->getKind() != BuiltinType::Double) 05754 continue; 05755 05756 uint64_t Offset = Layout.getFieldOffset(idx); 05757 if (Offset % 64) // Ignore doubles that are not aligned. 05758 continue; 05759 05760 // Add ((Offset - LastOffset) / 64) args of type i64. 05761 for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) 05762 ArgList.push_back(I64); 05763 05764 // Add double type. 05765 ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); 05766 LastOffset = Offset + 64; 05767 } 05768 05769 CoerceToIntArgs(TySize - LastOffset, IntArgList); 05770 ArgList.append(IntArgList.begin(), IntArgList.end()); 05771 05772 return llvm::StructType::get(getVMContext(), ArgList); 05773 } 05774 05775 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, 05776 uint64_t Offset) const { 05777 if (OrigOffset + MinABIStackAlignInBytes > Offset) 05778 return nullptr; 05779 05780 return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); 05781 } 05782 05783 ABIArgInfo 05784 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { 05785 uint64_t OrigOffset = Offset; 05786 uint64_t TySize = getContext().getTypeSize(Ty); 05787 uint64_t Align = getContext().getTypeAlign(Ty) / 8; 05788 05789 Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), 05790 (uint64_t)StackAlignInBytes); 05791 unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); 05792 Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; 05793 05794 if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { 05795 // Ignore empty aggregates. 05796 if (TySize == 0) 05797 return ABIArgInfo::getIgnore(); 05798 05799 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { 05800 Offset = OrigOffset + MinABIStackAlignInBytes; 05801 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 05802 } 05803 05804 // If we have reached here, aggregates are passed directly by coercing to 05805 // another structure type. Padding is inserted if the offset of the 05806 // aggregate is unaligned. 05807 ABIArgInfo ArgInfo = 05808 ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, 05809 getPaddingType(OrigOffset, CurrOffset)); 05810 ArgInfo.setInReg(true); 05811 return ArgInfo; 05812 } 05813 05814 // Treat an enum type as its underlying type. 05815 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 05816 Ty = EnumTy->getDecl()->getIntegerType(); 05817 05818 // All integral types are promoted to the GPR width. 05819 if (Ty->isIntegralOrEnumerationType()) 05820 return ABIArgInfo::getExtend(); 05821 05822 return ABIArgInfo::getDirect( 05823 nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); 05824 } 05825 05826 llvm::Type* 05827 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { 05828 const RecordType *RT = RetTy->getAs<RecordType>(); 05829 SmallVector<llvm::Type*, 8> RTList; 05830 05831 if (RT && RT->isStructureOrClassType()) { 05832 const RecordDecl *RD = RT->getDecl(); 05833 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); 05834 unsigned FieldCnt = Layout.getFieldCount(); 05835 05836 // N32/64 returns struct/classes in floating point registers if the 05837 // following conditions are met: 05838 // 1. The size of the struct/class is no larger than 128-bit. 05839 // 2. The struct/class has one or two fields all of which are floating 05840 // point types. 05841 // 3. The offset of the first field is zero (this follows what gcc does). 05842 // 05843 // Any other composite results are returned in integer registers. 05844 // 05845 if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { 05846 RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); 05847 for (; b != e; ++b) { 05848 const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); 05849 05850 if (!BT || !BT->isFloatingPoint()) 05851 break; 05852 05853 RTList.push_back(CGT.ConvertType(b->getType())); 05854 } 05855 05856 if (b == e) 05857 return llvm::StructType::get(getVMContext(), RTList, 05858 RD->hasAttr<PackedAttr>()); 05859 05860 RTList.clear(); 05861 } 05862 } 05863 05864 CoerceToIntArgs(Size, RTList); 05865 return llvm::StructType::get(getVMContext(), RTList); 05866 } 05867 05868 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { 05869 uint64_t Size = getContext().getTypeSize(RetTy); 05870 05871 if (RetTy->isVoidType()) 05872 return ABIArgInfo::getIgnore(); 05873 05874 // O32 doesn't treat zero-sized structs differently from other structs. 05875 // However, N32/N64 ignores zero sized return values. 05876 if (!IsO32 && Size == 0) 05877 return ABIArgInfo::getIgnore(); 05878 05879 if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { 05880 if (Size <= 128) { 05881 if (RetTy->isAnyComplexType()) 05882 return ABIArgInfo::getDirect(); 05883 05884 // O32 returns integer vectors in registers and N32/N64 returns all small 05885 // aggregates in registers. 05886 if (!IsO32 || 05887 (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { 05888 ABIArgInfo ArgInfo = 05889 ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); 05890 ArgInfo.setInReg(true); 05891 return ArgInfo; 05892 } 05893 } 05894 05895 return ABIArgInfo::getIndirect(0); 05896 } 05897 05898 // Treat an enum type as its underlying type. 05899 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 05900 RetTy = EnumTy->getDecl()->getIntegerType(); 05901 05902 return (RetTy->isPromotableIntegerType() ? 05903 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 05904 } 05905 05906 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { 05907 ABIArgInfo &RetInfo = FI.getReturnInfo(); 05908 if (!getCXXABI().classifyReturnType(FI)) 05909 RetInfo = classifyReturnType(FI.getReturnType()); 05910 05911 // Check if a pointer to an aggregate is passed as a hidden argument. 05912 uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; 05913 05914 for (auto &I : FI.arguments()) 05915 I.info = classifyArgumentType(I.type, Offset); 05916 } 05917 05918 llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 05919 CodeGenFunction &CGF) const { 05920 llvm::Type *BP = CGF.Int8PtrTy; 05921 llvm::Type *BPP = CGF.Int8PtrPtrTy; 05922 05923 CGBuilderTy &Builder = CGF.Builder; 05924 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 05925 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 05926 int64_t TypeAlign = 05927 std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); 05928 llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 05929 llvm::Value *AddrTyped; 05930 unsigned PtrWidth = getTarget().getPointerWidth(0); 05931 llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; 05932 05933 if (TypeAlign > MinABIStackAlignInBytes) { 05934 llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); 05935 llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); 05936 llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); 05937 llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); 05938 llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); 05939 AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); 05940 } 05941 else 05942 AddrTyped = Builder.CreateBitCast(Addr, PTy); 05943 05944 llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); 05945 TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); 05946 uint64_t Offset = 05947 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); 05948 llvm::Value *NextAddr = 05949 Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), 05950 "ap.next"); 05951 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 05952 05953 return AddrTyped; 05954 } 05955 05956 bool 05957 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 05958 llvm::Value *Address) const { 05959 // This information comes from gcc's implementation, which seems to 05960 // as canonical as it gets. 05961 05962 // Everything on MIPS is 4 bytes. Double-precision FP registers 05963 // are aliased to pairs of single-precision FP registers. 05964 llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); 05965 05966 // 0-31 are the general purpose registers, $0 - $31. 05967 // 32-63 are the floating-point registers, $f0 - $f31. 05968 // 64 and 65 are the multiply/divide registers, $hi and $lo. 05969 // 66 is the (notional, I think) register for signal-handler return. 05970 AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); 05971 05972 // 67-74 are the floating-point status registers, $fcc0 - $fcc7. 05973 // They are one bit wide and ignored here. 05974 05975 // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. 05976 // (coprocessor 1 is the FP unit) 05977 // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. 05978 // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. 05979 // 176-181 are the DSP accumulator registers. 05980 AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); 05981 return false; 05982 } 05983 05984 //===----------------------------------------------------------------------===// 05985 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. 05986 // Currently subclassed only to implement custom OpenCL C function attribute 05987 // handling. 05988 //===----------------------------------------------------------------------===// 05989 05990 namespace { 05991 05992 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { 05993 public: 05994 TCETargetCodeGenInfo(CodeGenTypes &CGT) 05995 : DefaultTargetCodeGenInfo(CGT) {} 05996 05997 void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 05998 CodeGen::CodeGenModule &M) const override; 05999 }; 06000 06001 void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, 06002 llvm::GlobalValue *GV, 06003 CodeGen::CodeGenModule &M) const { 06004 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); 06005 if (!FD) return; 06006 06007 llvm::Function *F = cast<llvm::Function>(GV); 06008 06009 if (M.getLangOpts().OpenCL) { 06010 if (FD->hasAttr<OpenCLKernelAttr>()) { 06011 // OpenCL C Kernel functions are not subject to inlining 06012 F->addFnAttr(llvm::Attribute::NoInline); 06013 const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); 06014 if (Attr) { 06015 // Convert the reqd_work_group_size() attributes to metadata. 06016 llvm::LLVMContext &Context = F->getContext(); 06017 llvm::NamedMDNode *OpenCLMetadata = 06018 M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); 06019 06020 SmallVector<llvm::Value*, 5> Operands; 06021 Operands.push_back(F); 06022 06023 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 06024 llvm::APInt(32, Attr->getXDim()))); 06025 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 06026 llvm::APInt(32, Attr->getYDim()))); 06027 Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 06028 llvm::APInt(32, Attr->getZDim()))); 06029 06030 // Add a boolean constant operand for "required" (true) or "hint" (false) 06031 // for implementing the work_group_size_hint attr later. Currently 06032 // always true as the hint is not yet implemented. 06033 Operands.push_back(llvm::ConstantInt::getTrue(Context)); 06034 OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); 06035 } 06036 } 06037 } 06038 } 06039 06040 } 06041 06042 //===----------------------------------------------------------------------===// 06043 // Hexagon ABI Implementation 06044 //===----------------------------------------------------------------------===// 06045 06046 namespace { 06047 06048 class HexagonABIInfo : public ABIInfo { 06049 06050 06051 public: 06052 HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 06053 06054 private: 06055 06056 ABIArgInfo classifyReturnType(QualType RetTy) const; 06057 ABIArgInfo classifyArgumentType(QualType RetTy) const; 06058 06059 void computeInfo(CGFunctionInfo &FI) const override; 06060 06061 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 06062 CodeGenFunction &CGF) const override; 06063 }; 06064 06065 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { 06066 public: 06067 HexagonTargetCodeGenInfo(CodeGenTypes &CGT) 06068 :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} 06069 06070 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 06071 return 29; 06072 } 06073 }; 06074 06075 } 06076 06077 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { 06078 if (!getCXXABI().classifyReturnType(FI)) 06079 FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); 06080 for (auto &I : FI.arguments()) 06081 I.info = classifyArgumentType(I.type); 06082 } 06083 06084 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { 06085 if (!isAggregateTypeForABI(Ty)) { 06086 // Treat an enum type as its underlying type. 06087 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 06088 Ty = EnumTy->getDecl()->getIntegerType(); 06089 06090 return (Ty->isPromotableIntegerType() ? 06091 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 06092 } 06093 06094 // Ignore empty records. 06095 if (isEmptyRecord(getContext(), Ty, true)) 06096 return ABIArgInfo::getIgnore(); 06097 06098 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 06099 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 06100 06101 uint64_t Size = getContext().getTypeSize(Ty); 06102 if (Size > 64) 06103 return ABIArgInfo::getIndirect(0, /*ByVal=*/true); 06104 // Pass in the smallest viable integer type. 06105 else if (Size > 32) 06106 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 06107 else if (Size > 16) 06108 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 06109 else if (Size > 8) 06110 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 06111 else 06112 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 06113 } 06114 06115 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { 06116 if (RetTy->isVoidType()) 06117 return ABIArgInfo::getIgnore(); 06118 06119 // Large vector types should be returned via memory. 06120 if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) 06121 return ABIArgInfo::getIndirect(0); 06122 06123 if (!isAggregateTypeForABI(RetTy)) { 06124 // Treat an enum type as its underlying type. 06125 if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) 06126 RetTy = EnumTy->getDecl()->getIntegerType(); 06127 06128 return (RetTy->isPromotableIntegerType() ? 06129 ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); 06130 } 06131 06132 if (isEmptyRecord(getContext(), RetTy, true)) 06133 return ABIArgInfo::getIgnore(); 06134 06135 // Aggregates <= 8 bytes are returned in r0; other aggregates 06136 // are returned indirectly. 06137 uint64_t Size = getContext().getTypeSize(RetTy); 06138 if (Size <= 64) { 06139 // Return in the smallest viable integer type. 06140 if (Size <= 8) 06141 return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); 06142 if (Size <= 16) 06143 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); 06144 if (Size <= 32) 06145 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); 06146 return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); 06147 } 06148 06149 return ABIArgInfo::getIndirect(0, /*ByVal=*/true); 06150 } 06151 06152 llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 06153 CodeGenFunction &CGF) const { 06154 // FIXME: Need to handle alignment 06155 llvm::Type *BPP = CGF.Int8PtrPtrTy; 06156 06157 CGBuilderTy &Builder = CGF.Builder; 06158 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, 06159 "ap"); 06160 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 06161 llvm::Type *PTy = 06162 llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); 06163 llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); 06164 06165 uint64_t Offset = 06166 llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); 06167 llvm::Value *NextAddr = 06168 Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), 06169 "ap.next"); 06170 Builder.CreateStore(NextAddr, VAListAddrAsBPP); 06171 06172 return AddrTyped; 06173 } 06174 06175 06176 //===----------------------------------------------------------------------===// 06177 // SPARC v9 ABI Implementation. 06178 // Based on the SPARC Compliance Definition version 2.4.1. 06179 // 06180 // Function arguments a mapped to a nominal "parameter array" and promoted to 06181 // registers depending on their type. Each argument occupies 8 or 16 bytes in 06182 // the array, structs larger than 16 bytes are passed indirectly. 06183 // 06184 // One case requires special care: 06185 // 06186 // struct mixed { 06187 // int i; 06188 // float f; 06189 // }; 06190 // 06191 // When a struct mixed is passed by value, it only occupies 8 bytes in the 06192 // parameter array, but the int is passed in an integer register, and the float 06193 // is passed in a floating point register. This is represented as two arguments 06194 // with the LLVM IR inreg attribute: 06195 // 06196 // declare void f(i32 inreg %i, float inreg %f) 06197 // 06198 // The code generator will only allocate 4 bytes from the parameter array for 06199 // the inreg arguments. All other arguments are allocated a multiple of 8 06200 // bytes. 06201 // 06202 namespace { 06203 class SparcV9ABIInfo : public ABIInfo { 06204 public: 06205 SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} 06206 06207 private: 06208 ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; 06209 void computeInfo(CGFunctionInfo &FI) const override; 06210 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 06211 CodeGenFunction &CGF) const override; 06212 06213 // Coercion type builder for structs passed in registers. The coercion type 06214 // serves two purposes: 06215 // 06216 // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' 06217 // in registers. 06218 // 2. Expose aligned floating point elements as first-level elements, so the 06219 // code generator knows to pass them in floating point registers. 06220 // 06221 // We also compute the InReg flag which indicates that the struct contains 06222 // aligned 32-bit floats. 06223 // 06224 struct CoerceBuilder { 06225 llvm::LLVMContext &Context; 06226 const llvm::DataLayout &DL; 06227 SmallVector<llvm::Type*, 8> Elems; 06228 uint64_t Size; 06229 bool InReg; 06230 06231 CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) 06232 : Context(c), DL(dl), Size(0), InReg(false) {} 06233 06234 // Pad Elems with integers until Size is ToSize. 06235 void pad(uint64_t ToSize) { 06236 assert(ToSize >= Size && "Cannot remove elements"); 06237 if (ToSize == Size) 06238 return; 06239 06240 // Finish the current 64-bit word. 06241 uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); 06242 if (Aligned > Size && Aligned <= ToSize) { 06243 Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); 06244 Size = Aligned; 06245 } 06246 06247 // Add whole 64-bit words. 06248 while (Size + 64 <= ToSize) { 06249 Elems.push_back(llvm::Type::getInt64Ty(Context)); 06250 Size += 64; 06251 } 06252 06253 // Final in-word padding. 06254 if (Size < ToSize) { 06255 Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); 06256 Size = ToSize; 06257 } 06258 } 06259 06260 // Add a floating point element at Offset. 06261 void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { 06262 // Unaligned floats are treated as integers. 06263 if (Offset % Bits) 06264 return; 06265 // The InReg flag is only required if there are any floats < 64 bits. 06266 if (Bits < 64) 06267 InReg = true; 06268 pad(Offset); 06269 Elems.push_back(Ty); 06270 Size = Offset + Bits; 06271 } 06272 06273 // Add a struct type to the coercion type, starting at Offset (in bits). 06274 void addStruct(uint64_t Offset, llvm::StructType *StrTy) { 06275 const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); 06276 for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { 06277 llvm::Type *ElemTy = StrTy->getElementType(i); 06278 uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); 06279 switch (ElemTy->getTypeID()) { 06280 case llvm::Type::StructTyID: 06281 addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); 06282 break; 06283 case llvm::Type::FloatTyID: 06284 addFloat(ElemOffset, ElemTy, 32); 06285 break; 06286 case llvm::Type::DoubleTyID: 06287 addFloat(ElemOffset, ElemTy, 64); 06288 break; 06289 case llvm::Type::FP128TyID: 06290 addFloat(ElemOffset, ElemTy, 128); 06291 break; 06292 case llvm::Type::PointerTyID: 06293 if (ElemOffset % 64 == 0) { 06294 pad(ElemOffset); 06295 Elems.push_back(ElemTy); 06296 Size += 64; 06297 } 06298 break; 06299 default: 06300 break; 06301 } 06302 } 06303 } 06304 06305 // Check if Ty is a usable substitute for the coercion type. 06306 bool isUsableType(llvm::StructType *Ty) const { 06307 if (Ty->getNumElements() != Elems.size()) 06308 return false; 06309 for (unsigned i = 0, e = Elems.size(); i != e; ++i) 06310 if (Elems[i] != Ty->getElementType(i)) 06311 return false; 06312 return true; 06313 } 06314 06315 // Get the coercion type as a literal struct type. 06316 llvm::Type *getType() const { 06317 if (Elems.size() == 1) 06318 return Elems.front(); 06319 else 06320 return llvm::StructType::get(Context, Elems); 06321 } 06322 }; 06323 }; 06324 } // end anonymous namespace 06325 06326 ABIArgInfo 06327 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { 06328 if (Ty->isVoidType()) 06329 return ABIArgInfo::getIgnore(); 06330 06331 uint64_t Size = getContext().getTypeSize(Ty); 06332 06333 // Anything too big to fit in registers is passed with an explicit indirect 06334 // pointer / sret pointer. 06335 if (Size > SizeLimit) 06336 return ABIArgInfo::getIndirect(0, /*ByVal=*/false); 06337 06338 // Treat an enum type as its underlying type. 06339 if (const EnumType *EnumTy = Ty->getAs<EnumType>()) 06340 Ty = EnumTy->getDecl()->getIntegerType(); 06341 06342 // Integer types smaller than a register are extended. 06343 if (Size < 64 && Ty->isIntegerType()) 06344 return ABIArgInfo::getExtend(); 06345 06346 // Other non-aggregates go in registers. 06347 if (!isAggregateTypeForABI(Ty)) 06348 return ABIArgInfo::getDirect(); 06349 06350 // If a C++ object has either a non-trivial copy constructor or a non-trivial 06351 // destructor, it is passed with an explicit indirect pointer / sret pointer. 06352 if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) 06353 return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); 06354 06355 // This is a small aggregate type that should be passed in registers. 06356 // Build a coercion type from the LLVM struct type. 06357 llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); 06358 if (!StrTy) 06359 return ABIArgInfo::getDirect(); 06360 06361 CoerceBuilder CB(getVMContext(), getDataLayout()); 06362 CB.addStruct(0, StrTy); 06363 CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); 06364 06365 // Try to use the original type for coercion. 06366 llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); 06367 06368 if (CB.InReg) 06369 return ABIArgInfo::getDirectInReg(CoerceTy); 06370 else 06371 return ABIArgInfo::getDirect(CoerceTy); 06372 } 06373 06374 llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 06375 CodeGenFunction &CGF) const { 06376 ABIArgInfo AI = classifyType(Ty, 16 * 8); 06377 llvm::Type *ArgTy = CGT.ConvertType(Ty); 06378 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 06379 AI.setCoerceToType(ArgTy); 06380 06381 llvm::Type *BPP = CGF.Int8PtrPtrTy; 06382 CGBuilderTy &Builder = CGF.Builder; 06383 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); 06384 llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); 06385 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 06386 llvm::Value *ArgAddr; 06387 unsigned Stride; 06388 06389 switch (AI.getKind()) { 06390 case ABIArgInfo::Expand: 06391 case ABIArgInfo::InAlloca: 06392 llvm_unreachable("Unsupported ABI kind for va_arg"); 06393 06394 case ABIArgInfo::Extend: 06395 Stride = 8; 06396 ArgAddr = Builder 06397 .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), 06398 "extend"); 06399 break; 06400 06401 case ABIArgInfo::Direct: 06402 Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); 06403 ArgAddr = Addr; 06404 break; 06405 06406 case ABIArgInfo::Indirect: 06407 Stride = 8; 06408 ArgAddr = Builder.CreateBitCast(Addr, 06409 llvm::PointerType::getUnqual(ArgPtrTy), 06410 "indirect"); 06411 ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); 06412 break; 06413 06414 case ABIArgInfo::Ignore: 06415 return llvm::UndefValue::get(ArgPtrTy); 06416 } 06417 06418 // Update VAList. 06419 Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); 06420 Builder.CreateStore(Addr, VAListAddrAsBPP); 06421 06422 return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); 06423 } 06424 06425 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { 06426 FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); 06427 for (auto &I : FI.arguments()) 06428 I.info = classifyType(I.type, 16 * 8); 06429 } 06430 06431 namespace { 06432 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { 06433 public: 06434 SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) 06435 : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} 06436 06437 int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { 06438 return 14; 06439 } 06440 06441 bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 06442 llvm::Value *Address) const override; 06443 }; 06444 } // end anonymous namespace 06445 06446 bool 06447 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 06448 llvm::Value *Address) const { 06449 // This is calculated from the LLVM and GCC tables and verified 06450 // against gcc output. AFAIK all ABIs use the same encoding. 06451 06452 CodeGen::CGBuilderTy &Builder = CGF.Builder; 06453 06454 llvm::IntegerType *i8 = CGF.Int8Ty; 06455 llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); 06456 llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); 06457 06458 // 0-31: the 8-byte general-purpose registers 06459 AssignToArrayRange(Builder, Address, Eight8, 0, 31); 06460 06461 // 32-63: f0-31, the 4-byte floating-point registers 06462 AssignToArrayRange(Builder, Address, Four8, 32, 63); 06463 06464 // Y = 64 06465 // PSR = 65 06466 // WIM = 66 06467 // TBR = 67 06468 // PC = 68 06469 // NPC = 69 06470 // FSR = 70 06471 // CSR = 71 06472 AssignToArrayRange(Builder, Address, Eight8, 64, 71); 06473 06474 // 72-87: d0-15, the 8-byte floating-point registers 06475 AssignToArrayRange(Builder, Address, Eight8, 72, 87); 06476 06477 return false; 06478 } 06479 06480 06481 //===----------------------------------------------------------------------===// 06482 // XCore ABI Implementation 06483 //===----------------------------------------------------------------------===// 06484 06485 namespace { 06486 06487 /// A SmallStringEnc instance is used to build up the TypeString by passing 06488 /// it by reference between functions that append to it. 06489 typedef llvm::SmallString<128> SmallStringEnc; 06490 06491 /// TypeStringCache caches the meta encodings of Types. 06492 /// 06493 /// The reason for caching TypeStrings is two fold: 06494 /// 1. To cache a type's encoding for later uses; 06495 /// 2. As a means to break recursive member type inclusion. 06496 /// 06497 /// A cache Entry can have a Status of: 06498 /// NonRecursive: The type encoding is not recursive; 06499 /// Recursive: The type encoding is recursive; 06500 /// Incomplete: An incomplete TypeString; 06501 /// IncompleteUsed: An incomplete TypeString that has been used in a 06502 /// Recursive type encoding. 06503 /// 06504 /// A NonRecursive entry will have all of its sub-members expanded as fully 06505 /// as possible. Whilst it may contain types which are recursive, the type 06506 /// itself is not recursive and thus its encoding may be safely used whenever 06507 /// the type is encountered. 06508 /// 06509 /// A Recursive entry will have all of its sub-members expanded as fully as 06510 /// possible. The type itself is recursive and it may contain other types which 06511 /// are recursive. The Recursive encoding must not be used during the expansion 06512 /// of a recursive type's recursive branch. For simplicity the code uses 06513 /// IncompleteCount to reject all usage of Recursive encodings for member types. 06514 /// 06515 /// An Incomplete entry is always a RecordType and only encodes its 06516 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and 06517 /// are placed into the cache during type expansion as a means to identify and 06518 /// handle recursive inclusion of types as sub-members. If there is recursion 06519 /// the entry becomes IncompleteUsed. 06520 /// 06521 /// During the expansion of a RecordType's members: 06522 /// 06523 /// If the cache contains a NonRecursive encoding for the member type, the 06524 /// cached encoding is used; 06525 /// 06526 /// If the cache contains a Recursive encoding for the member type, the 06527 /// cached encoding is 'Swapped' out, as it may be incorrect, and... 06528 /// 06529 /// If the member is a RecordType, an Incomplete encoding is placed into the 06530 /// cache to break potential recursive inclusion of itself as a sub-member; 06531 /// 06532 /// Once a member RecordType has been expanded, its temporary incomplete 06533 /// entry is removed from the cache. If a Recursive encoding was swapped out 06534 /// it is swapped back in; 06535 /// 06536 /// If an incomplete entry is used to expand a sub-member, the incomplete 06537 /// entry is marked as IncompleteUsed. The cache keeps count of how many 06538 /// IncompleteUsed entries it currently contains in IncompleteUsedCount; 06539 /// 06540 /// If a member's encoding is found to be a NonRecursive or Recursive viz: 06541 /// IncompleteUsedCount==0, the member's encoding is added to the cache. 06542 /// Else the member is part of a recursive type and thus the recursion has 06543 /// been exited too soon for the encoding to be correct for the member. 06544 /// 06545 class TypeStringCache { 06546 enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; 06547 struct Entry { 06548 std::string Str; // The encoded TypeString for the type. 06549 enum Status State; // Information about the encoding in 'Str'. 06550 std::string Swapped; // A temporary place holder for a Recursive encoding 06551 // during the expansion of RecordType's members. 06552 }; 06553 std::map<const IdentifierInfo *, struct Entry> Map; 06554 unsigned IncompleteCount; // Number of Incomplete entries in the Map. 06555 unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. 06556 public: 06557 TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; 06558 void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); 06559 bool removeIncomplete(const IdentifierInfo *ID); 06560 void addIfComplete(const IdentifierInfo *ID, StringRef Str, 06561 bool IsRecursive); 06562 StringRef lookupStr(const IdentifierInfo *ID); 06563 }; 06564 06565 /// TypeString encodings for enum & union fields must be order. 06566 /// FieldEncoding is a helper for this ordering process. 06567 class FieldEncoding { 06568 bool HasName; 06569 std::string Enc; 06570 public: 06571 FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; 06572 StringRef str() {return Enc.c_str();}; 06573 bool operator<(const FieldEncoding &rhs) const { 06574 if (HasName != rhs.HasName) return HasName; 06575 return Enc < rhs.Enc; 06576 } 06577 }; 06578 06579 class XCoreABIInfo : public DefaultABIInfo { 06580 public: 06581 XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} 06582 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 06583 CodeGenFunction &CGF) const override; 06584 }; 06585 06586 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { 06587 mutable TypeStringCache TSC; 06588 public: 06589 XCoreTargetCodeGenInfo(CodeGenTypes &CGT) 06590 :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} 06591 void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 06592 CodeGen::CodeGenModule &M) const override; 06593 }; 06594 06595 } // End anonymous namespace. 06596 06597 llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, 06598 CodeGenFunction &CGF) const { 06599 CGBuilderTy &Builder = CGF.Builder; 06600 06601 // Get the VAList. 06602 llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, 06603 CGF.Int8PtrPtrTy); 06604 llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); 06605 06606 // Handle the argument. 06607 ABIArgInfo AI = classifyArgumentType(Ty); 06608 llvm::Type *ArgTy = CGT.ConvertType(Ty); 06609 if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) 06610 AI.setCoerceToType(ArgTy); 06611 llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); 06612 llvm::Value *Val; 06613 uint64_t ArgSize = 0; 06614 switch (AI.getKind()) { 06615 case ABIArgInfo::Expand: 06616 case ABIArgInfo::InAlloca: 06617 llvm_unreachable("Unsupported ABI kind for va_arg"); 06618 case ABIArgInfo::Ignore: 06619 Val = llvm::UndefValue::get(ArgPtrTy); 06620 ArgSize = 0; 06621 break; 06622 case ABIArgInfo::Extend: 06623 case ABIArgInfo::Direct: 06624 Val = Builder.CreatePointerCast(AP, ArgPtrTy); 06625 ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); 06626 if (ArgSize < 4) 06627 ArgSize = 4; 06628 break; 06629 case ABIArgInfo::Indirect: 06630 llvm::Value *ArgAddr; 06631 ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); 06632 ArgAddr = Builder.CreateLoad(ArgAddr); 06633 Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); 06634 ArgSize = 4; 06635 break; 06636 } 06637 06638 // Increment the VAList. 06639 if (ArgSize) { 06640 llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); 06641 Builder.CreateStore(APN, VAListAddrAsBPP); 06642 } 06643 return Val; 06644 } 06645 06646 /// During the expansion of a RecordType, an incomplete TypeString is placed 06647 /// into the cache as a means to identify and break recursion. 06648 /// If there is a Recursive encoding in the cache, it is swapped out and will 06649 /// be reinserted by removeIncomplete(). 06650 /// All other types of encoding should have been used rather than arriving here. 06651 void TypeStringCache::addIncomplete(const IdentifierInfo *ID, 06652 std::string StubEnc) { 06653 if (!ID) 06654 return; 06655 Entry &E = Map[ID]; 06656 assert( (E.Str.empty() || E.State == Recursive) && 06657 "Incorrectly use of addIncomplete"); 06658 assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); 06659 E.Swapped.swap(E.Str); // swap out the Recursive 06660 E.Str.swap(StubEnc); 06661 E.State = Incomplete; 06662 ++IncompleteCount; 06663 } 06664 06665 /// Once the RecordType has been expanded, the temporary incomplete TypeString 06666 /// must be removed from the cache. 06667 /// If a Recursive was swapped out by addIncomplete(), it will be replaced. 06668 /// Returns true if the RecordType was defined recursively. 06669 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { 06670 if (!ID) 06671 return false; 06672 auto I = Map.find(ID); 06673 assert(I != Map.end() && "Entry not present"); 06674 Entry &E = I->second; 06675 assert( (E.State == Incomplete || 06676 E.State == IncompleteUsed) && 06677 "Entry must be an incomplete type"); 06678 bool IsRecursive = false; 06679 if (E.State == IncompleteUsed) { 06680 // We made use of our Incomplete encoding, thus we are recursive. 06681 IsRecursive = true; 06682 --IncompleteUsedCount; 06683 } 06684 if (E.Swapped.empty()) 06685 Map.erase(I); 06686 else { 06687 // Swap the Recursive back. 06688 E.Swapped.swap(E.Str); 06689 E.Swapped.clear(); 06690 E.State = Recursive; 06691 } 06692 --IncompleteCount; 06693 return IsRecursive; 06694 } 06695 06696 /// Add the encoded TypeString to the cache only if it is NonRecursive or 06697 /// Recursive (viz: all sub-members were expanded as fully as possible). 06698 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, 06699 bool IsRecursive) { 06700 if (!ID || IncompleteUsedCount) 06701 return; // No key or it is is an incomplete sub-type so don't add. 06702 Entry &E = Map[ID]; 06703 if (IsRecursive && !E.Str.empty()) { 06704 assert(E.State==Recursive && E.Str.size() == Str.size() && 06705 "This is not the same Recursive entry"); 06706 // The parent container was not recursive after all, so we could have used 06707 // this Recursive sub-member entry after all, but we assumed the worse when 06708 // we started viz: IncompleteCount!=0. 06709 return; 06710 } 06711 assert(E.Str.empty() && "Entry already present"); 06712 E.Str = Str.str(); 06713 E.State = IsRecursive? Recursive : NonRecursive; 06714 } 06715 06716 /// Return a cached TypeString encoding for the ID. If there isn't one, or we 06717 /// are recursively expanding a type (IncompleteCount != 0) and the cached 06718 /// encoding is Recursive, return an empty StringRef. 06719 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { 06720 if (!ID) 06721 return StringRef(); // We have no key. 06722 auto I = Map.find(ID); 06723 if (I == Map.end()) 06724 return StringRef(); // We have no encoding. 06725 Entry &E = I->second; 06726 if (E.State == Recursive && IncompleteCount) 06727 return StringRef(); // We don't use Recursive encodings for member types. 06728 06729 if (E.State == Incomplete) { 06730 // The incomplete type is being used to break out of recursion. 06731 E.State = IncompleteUsed; 06732 ++IncompleteUsedCount; 06733 } 06734 return E.Str.c_str(); 06735 } 06736 06737 /// The XCore ABI includes a type information section that communicates symbol 06738 /// type information to the linker. The linker uses this information to verify 06739 /// safety/correctness of things such as array bound and pointers et al. 06740 /// The ABI only requires C (and XC) language modules to emit TypeStrings. 06741 /// This type information (TypeString) is emitted into meta data for all global 06742 /// symbols: definitions, declarations, functions & variables. 06743 /// 06744 /// The TypeString carries type, qualifier, name, size & value details. 06745 /// Please see 'Tools Development Guide' section 2.16.2 for format details: 06746 /// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf> 06747 /// The output is tested by test/CodeGen/xcore-stringtype.c. 06748 /// 06749 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 06750 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); 06751 06752 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. 06753 void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 06754 CodeGen::CodeGenModule &CGM) const { 06755 SmallStringEnc Enc; 06756 if (getTypeString(Enc, D, CGM, TSC)) { 06757 llvm::LLVMContext &Ctx = CGM.getModule().getContext(); 06758 llvm::SmallVector<llvm::Value *, 2> MDVals; 06759 MDVals.push_back(GV); 06760 MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); 06761 llvm::NamedMDNode *MD = 06762 CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); 06763 MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); 06764 } 06765 } 06766 06767 static bool appendType(SmallStringEnc &Enc, QualType QType, 06768 const CodeGen::CodeGenModule &CGM, 06769 TypeStringCache &TSC); 06770 06771 /// Helper function for appendRecordType(). 06772 /// Builds a SmallVector containing the encoded field types in declaration order. 06773 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, 06774 const RecordDecl *RD, 06775 const CodeGen::CodeGenModule &CGM, 06776 TypeStringCache &TSC) { 06777 for (const auto *Field : RD->fields()) { 06778 SmallStringEnc Enc; 06779 Enc += "m("; 06780 Enc += Field->getName(); 06781 Enc += "){"; 06782 if (Field->isBitField()) { 06783 Enc += "b("; 06784 llvm::raw_svector_ostream OS(Enc); 06785 OS.resync(); 06786 OS << Field->getBitWidthValue(CGM.getContext()); 06787 OS.flush(); 06788 Enc += ':'; 06789 } 06790 if (!appendType(Enc, Field->getType(), CGM, TSC)) 06791 return false; 06792 if (Field->isBitField()) 06793 Enc += ')'; 06794 Enc += '}'; 06795 FE.push_back(FieldEncoding(!Field->getName().empty(), Enc)); 06796 } 06797 return true; 06798 } 06799 06800 /// Appends structure and union types to Enc and adds encoding to cache. 06801 /// Recursively calls appendType (via extractFieldType) for each field. 06802 /// Union types have their fields ordered according to the ABI. 06803 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, 06804 const CodeGen::CodeGenModule &CGM, 06805 TypeStringCache &TSC, const IdentifierInfo *ID) { 06806 // Append the cached TypeString if we have one. 06807 StringRef TypeString = TSC.lookupStr(ID); 06808 if (!TypeString.empty()) { 06809 Enc += TypeString; 06810 return true; 06811 } 06812 06813 // Start to emit an incomplete TypeString. 06814 size_t Start = Enc.size(); 06815 Enc += (RT->isUnionType()? 'u' : 's'); 06816 Enc += '('; 06817 if (ID) 06818 Enc += ID->getName(); 06819 Enc += "){"; 06820 06821 // We collect all encoded fields and order as necessary. 06822 bool IsRecursive = false; 06823 const RecordDecl *RD = RT->getDecl()->getDefinition(); 06824 if (RD && !RD->field_empty()) { 06825 // An incomplete TypeString stub is placed in the cache for this RecordType 06826 // so that recursive calls to this RecordType will use it whilst building a 06827 // complete TypeString for this RecordType. 06828 SmallVector<FieldEncoding, 16> FE; 06829 std::string StubEnc(Enc.substr(Start).str()); 06830 StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. 06831 TSC.addIncomplete(ID, std::move(StubEnc)); 06832 if (!extractFieldType(FE, RD, CGM, TSC)) { 06833 (void) TSC.removeIncomplete(ID); 06834 return false; 06835 } 06836 IsRecursive = TSC.removeIncomplete(ID); 06837 // The ABI requires unions to be sorted but not structures. 06838 // See FieldEncoding::operator< for sort algorithm. 06839 if (RT->isUnionType()) 06840 std::sort(FE.begin(), FE.end()); 06841 // We can now complete the TypeString. 06842 unsigned E = FE.size(); 06843 for (unsigned I = 0; I != E; ++I) { 06844 if (I) 06845 Enc += ','; 06846 Enc += FE[I].str(); 06847 } 06848 } 06849 Enc += '}'; 06850 TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); 06851 return true; 06852 } 06853 06854 /// Appends enum types to Enc and adds the encoding to the cache. 06855 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, 06856 TypeStringCache &TSC, 06857 const IdentifierInfo *ID) { 06858 // Append the cached TypeString if we have one. 06859 StringRef TypeString = TSC.lookupStr(ID); 06860 if (!TypeString.empty()) { 06861 Enc += TypeString; 06862 return true; 06863 } 06864 06865 size_t Start = Enc.size(); 06866 Enc += "e("; 06867 if (ID) 06868 Enc += ID->getName(); 06869 Enc += "){"; 06870 06871 // We collect all encoded enumerations and order them alphanumerically. 06872 if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { 06873 SmallVector<FieldEncoding, 16> FE; 06874 for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; 06875 ++I) { 06876 SmallStringEnc EnumEnc; 06877 EnumEnc += "m("; 06878 EnumEnc += I->getName(); 06879 EnumEnc += "){"; 06880 I->getInitVal().toString(EnumEnc); 06881 EnumEnc += '}'; 06882 FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); 06883 } 06884 std::sort(FE.begin(), FE.end()); 06885 unsigned E = FE.size(); 06886 for (unsigned I = 0; I != E; ++I) { 06887 if (I) 06888 Enc += ','; 06889 Enc += FE[I].str(); 06890 } 06891 } 06892 Enc += '}'; 06893 TSC.addIfComplete(ID, Enc.substr(Start), false); 06894 return true; 06895 } 06896 06897 /// Appends type's qualifier to Enc. 06898 /// This is done prior to appending the type's encoding. 06899 static void appendQualifier(SmallStringEnc &Enc, QualType QT) { 06900 // Qualifiers are emitted in alphabetical order. 06901 static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; 06902 int Lookup = 0; 06903 if (QT.isConstQualified()) 06904 Lookup += 1<<0; 06905 if (QT.isRestrictQualified()) 06906 Lookup += 1<<1; 06907 if (QT.isVolatileQualified()) 06908 Lookup += 1<<2; 06909 Enc += Table[Lookup]; 06910 } 06911 06912 /// Appends built-in types to Enc. 06913 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { 06914 const char *EncType; 06915 switch (BT->getKind()) { 06916 case BuiltinType::Void: 06917 EncType = "0"; 06918 break; 06919 case BuiltinType::Bool: 06920 EncType = "b"; 06921 break; 06922 case BuiltinType::Char_U: 06923 EncType = "uc"; 06924 break; 06925 case BuiltinType::UChar: 06926 EncType = "uc"; 06927 break; 06928 case BuiltinType::SChar: 06929 EncType = "sc"; 06930 break; 06931 case BuiltinType::UShort: 06932 EncType = "us"; 06933 break; 06934 case BuiltinType::Short: 06935 EncType = "ss"; 06936 break; 06937 case BuiltinType::UInt: 06938 EncType = "ui"; 06939 break; 06940 case BuiltinType::Int: 06941 EncType = "si"; 06942 break; 06943 case BuiltinType::ULong: 06944 EncType = "ul"; 06945 break; 06946 case BuiltinType::Long: 06947 EncType = "sl"; 06948 break; 06949 case BuiltinType::ULongLong: 06950 EncType = "ull"; 06951 break; 06952 case BuiltinType::LongLong: 06953 EncType = "sll"; 06954 break; 06955 case BuiltinType::Float: 06956 EncType = "ft"; 06957 break; 06958 case BuiltinType::Double: 06959 EncType = "d"; 06960 break; 06961 case BuiltinType::LongDouble: 06962 EncType = "ld"; 06963 break; 06964 default: 06965 return false; 06966 } 06967 Enc += EncType; 06968 return true; 06969 } 06970 06971 /// Appends a pointer encoding to Enc before calling appendType for the pointee. 06972 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, 06973 const CodeGen::CodeGenModule &CGM, 06974 TypeStringCache &TSC) { 06975 Enc += "p("; 06976 if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) 06977 return false; 06978 Enc += ')'; 06979 return true; 06980 } 06981 06982 /// Appends array encoding to Enc before calling appendType for the element. 06983 static bool appendArrayType(SmallStringEnc &Enc, QualType QT, 06984 const ArrayType *AT, 06985 const CodeGen::CodeGenModule &CGM, 06986 TypeStringCache &TSC, StringRef NoSizeEnc) { 06987 if (AT->getSizeModifier() != ArrayType::Normal) 06988 return false; 06989 Enc += "a("; 06990 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 06991 CAT->getSize().toStringUnsigned(Enc); 06992 else 06993 Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". 06994 Enc += ':'; 06995 // The Qualifiers should be attached to the type rather than the array. 06996 appendQualifier(Enc, QT); 06997 if (!appendType(Enc, AT->getElementType(), CGM, TSC)) 06998 return false; 06999 Enc += ')'; 07000 return true; 07001 } 07002 07003 /// Appends a function encoding to Enc, calling appendType for the return type 07004 /// and the arguments. 07005 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, 07006 const CodeGen::CodeGenModule &CGM, 07007 TypeStringCache &TSC) { 07008 Enc += "f{"; 07009 if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) 07010 return false; 07011 Enc += "}("; 07012 if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { 07013 // N.B. we are only interested in the adjusted param types. 07014 auto I = FPT->param_type_begin(); 07015 auto E = FPT->param_type_end(); 07016 if (I != E) { 07017 do { 07018 if (!appendType(Enc, *I, CGM, TSC)) 07019 return false; 07020 ++I; 07021 if (I != E) 07022 Enc += ','; 07023 } while (I != E); 07024 if (FPT->isVariadic()) 07025 Enc += ",va"; 07026 } else { 07027 if (FPT->isVariadic()) 07028 Enc += "va"; 07029 else 07030 Enc += '0'; 07031 } 07032 } 07033 Enc += ')'; 07034 return true; 07035 } 07036 07037 /// Handles the type's qualifier before dispatching a call to handle specific 07038 /// type encodings. 07039 static bool appendType(SmallStringEnc &Enc, QualType QType, 07040 const CodeGen::CodeGenModule &CGM, 07041 TypeStringCache &TSC) { 07042 07043 QualType QT = QType.getCanonicalType(); 07044 07045 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) 07046 // The Qualifiers should be attached to the type rather than the array. 07047 // Thus we don't call appendQualifier() here. 07048 return appendArrayType(Enc, QT, AT, CGM, TSC, ""); 07049 07050 appendQualifier(Enc, QT); 07051 07052 if (const BuiltinType *BT = QT->getAs<BuiltinType>()) 07053 return appendBuiltinType(Enc, BT); 07054 07055 if (const PointerType *PT = QT->getAs<PointerType>()) 07056 return appendPointerType(Enc, PT, CGM, TSC); 07057 07058 if (const EnumType *ET = QT->getAs<EnumType>()) 07059 return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); 07060 07061 if (const RecordType *RT = QT->getAsStructureType()) 07062 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 07063 07064 if (const RecordType *RT = QT->getAsUnionType()) 07065 return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); 07066 07067 if (const FunctionType *FT = QT->getAs<FunctionType>()) 07068 return appendFunctionType(Enc, FT, CGM, TSC); 07069 07070 return false; 07071 } 07072 07073 static bool getTypeString(SmallStringEnc &Enc, const Decl *D, 07074 CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { 07075 if (!D) 07076 return false; 07077 07078 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 07079 if (FD->getLanguageLinkage() != CLanguageLinkage) 07080 return false; 07081 return appendType(Enc, FD->getType(), CGM, TSC); 07082 } 07083 07084 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 07085 if (VD->getLanguageLinkage() != CLanguageLinkage) 07086 return false; 07087 QualType QT = VD->getType().getCanonicalType(); 07088 if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { 07089 // Global ArrayTypes are given a size of '*' if the size is unknown. 07090 // The Qualifiers should be attached to the type rather than the array. 07091 // Thus we don't call appendQualifier() here. 07092 return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); 07093 } 07094 return appendType(Enc, QT, CGM, TSC); 07095 } 07096 return false; 07097 } 07098 07099 07100 //===----------------------------------------------------------------------===// 07101 // Driver code 07102 //===----------------------------------------------------------------------===// 07103 07104 const llvm::Triple &CodeGenModule::getTriple() const { 07105 return getTarget().getTriple(); 07106 } 07107 07108 bool CodeGenModule::supportsCOMDAT() const { 07109 return !getTriple().isOSBinFormatMachO(); 07110 } 07111 07112 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { 07113 if (TheTargetCodeGenInfo) 07114 return *TheTargetCodeGenInfo; 07115 07116 const llvm::Triple &Triple = getTarget().getTriple(); 07117 switch (Triple.getArch()) { 07118 default: 07119 return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); 07120 07121 case llvm::Triple::le32: 07122 return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); 07123 case llvm::Triple::mips: 07124 case llvm::Triple::mipsel: 07125 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); 07126 07127 case llvm::Triple::mips64: 07128 case llvm::Triple::mips64el: 07129 return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); 07130 07131 case llvm::Triple::aarch64: 07132 case llvm::Triple::aarch64_be: { 07133 AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; 07134 if (getTarget().getABI() == "darwinpcs") 07135 Kind = AArch64ABIInfo::DarwinPCS; 07136 07137 return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); 07138 } 07139 07140 case llvm::Triple::arm: 07141 case llvm::Triple::armeb: 07142 case llvm::Triple::thumb: 07143 case llvm::Triple::thumbeb: 07144 { 07145 ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; 07146 if (getTarget().getABI() == "apcs-gnu") 07147 Kind = ARMABIInfo::APCS; 07148 else if (CodeGenOpts.FloatABI == "hard" || 07149 (CodeGenOpts.FloatABI != "soft" && 07150 Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) 07151 Kind = ARMABIInfo::AAPCS_VFP; 07152 07153 switch (Triple.getOS()) { 07154 case llvm::Triple::NaCl: 07155 return *(TheTargetCodeGenInfo = 07156 new NaClARMTargetCodeGenInfo(Types, Kind)); 07157 default: 07158 return *(TheTargetCodeGenInfo = 07159 new ARMTargetCodeGenInfo(Types, Kind)); 07160 } 07161 } 07162 07163 case llvm::Triple::ppc: 07164 return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); 07165 case llvm::Triple::ppc64: 07166 if (Triple.isOSBinFormatELF()) { 07167 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; 07168 if (getTarget().getABI() == "elfv2") 07169 Kind = PPC64_SVR4_ABIInfo::ELFv2; 07170 07171 return *(TheTargetCodeGenInfo = 07172 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind)); 07173 } else 07174 return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); 07175 case llvm::Triple::ppc64le: { 07176 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); 07177 PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; 07178 if (getTarget().getABI() == "elfv1") 07179 Kind = PPC64_SVR4_ABIInfo::ELFv1; 07180 07181 return *(TheTargetCodeGenInfo = 07182 new PPC64_SVR4_TargetCodeGenInfo(Types, Kind)); 07183 } 07184 07185 case llvm::Triple::nvptx: 07186 case llvm::Triple::nvptx64: 07187 return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); 07188 07189 case llvm::Triple::msp430: 07190 return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); 07191 07192 case llvm::Triple::systemz: 07193 return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); 07194 07195 case llvm::Triple::tce: 07196 return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); 07197 07198 case llvm::Triple::x86: { 07199 bool IsDarwinVectorABI = Triple.isOSDarwin(); 07200 bool IsSmallStructInRegABI = 07201 X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); 07202 bool IsWin32FloatStructABI = Triple.isWindowsMSVCEnvironment(); 07203 07204 if (Triple.getOS() == llvm::Triple::Win32) { 07205 return *(TheTargetCodeGenInfo = 07206 new WinX86_32TargetCodeGenInfo(Types, 07207 IsDarwinVectorABI, IsSmallStructInRegABI, 07208 IsWin32FloatStructABI, 07209 CodeGenOpts.NumRegisterParameters)); 07210 } else { 07211 return *(TheTargetCodeGenInfo = 07212 new X86_32TargetCodeGenInfo(Types, 07213 IsDarwinVectorABI, IsSmallStructInRegABI, 07214 IsWin32FloatStructABI, 07215 CodeGenOpts.NumRegisterParameters)); 07216 } 07217 } 07218 07219 case llvm::Triple::x86_64: { 07220 bool HasAVX = getTarget().getABI() == "avx"; 07221 07222 switch (Triple.getOS()) { 07223 case llvm::Triple::Win32: 07224 return *(TheTargetCodeGenInfo = 07225 new WinX86_64TargetCodeGenInfo(Types, HasAVX)); 07226 case llvm::Triple::NaCl: 07227 return *(TheTargetCodeGenInfo = 07228 new NaClX86_64TargetCodeGenInfo(Types, HasAVX)); 07229 default: 07230 return *(TheTargetCodeGenInfo = 07231 new X86_64TargetCodeGenInfo(Types, HasAVX)); 07232 } 07233 } 07234 case llvm::Triple::hexagon: 07235 return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); 07236 case llvm::Triple::sparcv9: 07237 return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); 07238 case llvm::Triple::xcore: 07239 return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); 07240 } 07241 }