1 //===--- CGCall.cpp - Encapsulate calling convention details --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // These classes wrap the information about a call or function 11 // definition used to handle ABI compliancy. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "CGCall.h" 16 #include "ABIInfo.h" 17 #include "CGCXXABI.h" 18 #include "CodeGenFunction.h" 19 #include "CodeGenModule.h" 20 #include "TargetInfo.h" 21 #include "clang/AST/Decl.h" 22 #include "clang/AST/DeclCXX.h" 23 #include "clang/AST/DeclObjC.h" 24 #include "clang/Basic/TargetInfo.h" 25 #include "clang/CodeGen/CGFunctionInfo.h" 26 #include "clang/Frontend/CodeGenOptions.h" 27 #include "llvm/ADT/StringExtras.h" 28 #include "llvm/IR/Attributes.h" 29 #include "llvm/IR/CallSite.h" 30 #include "llvm/IR/DataLayout.h" 31 #include "llvm/IR/InlineAsm.h" 32 #include "llvm/IR/Intrinsics.h" 33 #include "llvm/Transforms/Utils/Local.h" 34 using namespace clang; 35 using namespace CodeGen; 36 37 /***/ 38 39 static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { 40 switch (CC) { 41 default: return llvm::CallingConv::C; 42 case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; 43 case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; 44 case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; 45 case CC_X86_64Win64: return llvm::CallingConv::X86_64_Win64; 46 case CC_X86_64SysV: return llvm::CallingConv::X86_64_SysV; 47 case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; 48 case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 49 case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI; 50 // TODO: add support for CC_X86Pascal to llvm 51 } 52 } 53 54 /// Derives the 'this' type for codegen purposes, i.e. ignoring method 55 /// qualification. 56 /// FIXME: address space qualification? 57 static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { 58 QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); 59 return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); 60 } 61 62 /// Returns the canonical formal type of the given C++ method. 63 static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { 64 return MD->getType()->getCanonicalTypeUnqualified() 65 .getAs<FunctionProtoType>(); 66 } 67 68 /// Returns the "extra-canonicalized" return type, which discards 69 /// qualifiers on the return type. Codegen doesn't care about them, 70 /// and it makes ABI code a little easier to be able to assume that 71 /// all parameter and return types are top-level unqualified. 72 static CanQualType GetReturnType(QualType RetTy) { 73 return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); 74 } 75 76 /// Arrange the argument and result information for a value of the given 77 /// unprototyped freestanding function type. 78 const CGFunctionInfo & 79 CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) { 80 // When translating an unprototyped function type, always use a 81 // variadic type. 82 return arrangeLLVMFunctionInfo(FTNP->getReturnType().getUnqualifiedType(), 83 false, None, FTNP->getExtInfo(), 84 RequiredArgs(0)); 85 } 86 87 /// Arrange the LLVM function layout for a value of the given function 88 /// type, on top of any implicit parameters already stored. 89 static const CGFunctionInfo & 90 arrangeLLVMFunctionInfo(CodeGenTypes &CGT, bool IsInstanceMethod, 91 SmallVectorImpl<CanQualType> &prefix, 92 CanQual<FunctionProtoType> FTP) { 93 RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size()); 94 // FIXME: Kill copy. 95 for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i) 96 prefix.push_back(FTP->getParamType(i)); 97 CanQualType resultType = FTP->getReturnType().getUnqualifiedType(); 98 return CGT.arrangeLLVMFunctionInfo(resultType, IsInstanceMethod, prefix, 99 FTP->getExtInfo(), required); 100 } 101 102 /// Arrange the argument and result information for a value of the 103 /// given freestanding function type. 104 const CGFunctionInfo & 105 CodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) { 106 SmallVector<CanQualType, 16> argTypes; 107 return ::arrangeLLVMFunctionInfo(*this, false, argTypes, FTP); 108 } 109 110 static CallingConv getCallingConventionForDecl(const Decl *D, bool IsWindows) { 111 // Set the appropriate calling convention for the Function. 112 if (D->hasAttr<StdCallAttr>()) 113 return CC_X86StdCall; 114 115 if (D->hasAttr<FastCallAttr>()) 116 return CC_X86FastCall; 117 118 if (D->hasAttr<ThisCallAttr>()) 119 return CC_X86ThisCall; 120 121 if (D->hasAttr<PascalAttr>()) 122 return CC_X86Pascal; 123 124 if (PcsAttr *PCS = D->getAttr<PcsAttr>()) 125 return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); 126 127 if (D->hasAttr<PnaclCallAttr>()) 128 return CC_PnaclCall; 129 130 if (D->hasAttr<IntelOclBiccAttr>()) 131 return CC_IntelOclBicc; 132 133 if (D->hasAttr<MSABIAttr>()) 134 return IsWindows ? CC_C : CC_X86_64Win64; 135 136 if (D->hasAttr<SysVABIAttr>()) 137 return IsWindows ? CC_X86_64SysV : CC_C; 138 139 return CC_C; 140 } 141 142 /// Arrange the argument and result information for a call to an 143 /// unknown C++ non-static member function of the given abstract type. 144 /// (Zero value of RD means we don't have any meaningful "this" argument type, 145 /// so fall back to a generic pointer type). 146 /// The member function must be an ordinary function, i.e. not a 147 /// constructor or destructor. 148 const CGFunctionInfo & 149 CodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD, 150 const FunctionProtoType *FTP) { 151 SmallVector<CanQualType, 16> argTypes; 152 153 // Add the 'this' pointer. 154 if (RD) 155 argTypes.push_back(GetThisType(Context, RD)); 156 else 157 argTypes.push_back(Context.VoidPtrTy); 158 159 return ::arrangeLLVMFunctionInfo( 160 *this, true, argTypes, 161 FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); 162 } 163 164 /// Arrange the argument and result information for a declaration or 165 /// definition of the given C++ non-static member function. The 166 /// member function must be an ordinary function, i.e. not a 167 /// constructor or destructor. 168 const CGFunctionInfo & 169 CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) { 170 assert(!isa<CXXConstructorDecl>(MD) && "wrong method for constructors!"); 171 assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); 172 173 CanQual<FunctionProtoType> prototype = GetFormalType(MD); 174 175 if (MD->isInstance()) { 176 // The abstract case is perfectly fine. 177 const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD); 178 return arrangeCXXMethodType(ThisType, prototype.getTypePtr()); 179 } 180 181 return arrangeFreeFunctionType(prototype); 182 } 183 184 const CGFunctionInfo & 185 CodeGenTypes::arrangeCXXStructorDeclaration(const CXXMethodDecl *MD, 186 StructorType Type) { 187 188 SmallVector<CanQualType, 16> argTypes; 189 argTypes.push_back(GetThisType(Context, MD->getParent())); 190 191 GlobalDecl GD; 192 if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { 193 GD = GlobalDecl(CD, toCXXCtorType(Type)); 194 } else { 195 auto *DD = dyn_cast<CXXDestructorDecl>(MD); 196 GD = GlobalDecl(DD, toCXXDtorType(Type)); 197 } 198 199 CanQual<FunctionProtoType> FTP = GetFormalType(MD); 200 201 // Add the formal parameters. 202 for (unsigned i = 0, e = FTP->getNumParams(); i != e; ++i) 203 argTypes.push_back(FTP->getParamType(i)); 204 205 TheCXXABI.buildStructorSignature(MD, Type, argTypes); 206 207 RequiredArgs required = 208 (MD->isVariadic() ? RequiredArgs(argTypes.size()) : RequiredArgs::All); 209 210 FunctionType::ExtInfo extInfo = FTP->getExtInfo(); 211 CanQualType resultType = 212 TheCXXABI.HasThisReturn(GD) ? argTypes.front() : Context.VoidTy; 213 return arrangeLLVMFunctionInfo(resultType, true, argTypes, extInfo, required); 214 } 215 216 /// Arrange a call to a C++ method, passing the given arguments. 217 const CGFunctionInfo & 218 CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args, 219 const CXXConstructorDecl *D, 220 CXXCtorType CtorKind, 221 unsigned ExtraArgs) { 222 // FIXME: Kill copy. 223 SmallVector<CanQualType, 16> ArgTypes; 224 for (const auto &Arg : args) 225 ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); 226 227 CanQual<FunctionProtoType> FPT = GetFormalType(D); 228 RequiredArgs Required = RequiredArgs::forPrototypePlus(FPT, 1 + ExtraArgs); 229 GlobalDecl GD(D, CtorKind); 230 CanQualType ResultType = 231 TheCXXABI.HasThisReturn(GD) ? ArgTypes.front() : Context.VoidTy; 232 233 FunctionType::ExtInfo Info = FPT->getExtInfo(); 234 return arrangeLLVMFunctionInfo(ResultType, true, ArgTypes, Info, Required); 235 } 236 237 /// Arrange the argument and result information for the declaration or 238 /// definition of the given function. 239 const CGFunctionInfo & 240 CodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) { 241 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 242 if (MD->isInstance()) 243 return arrangeCXXMethodDeclaration(MD); 244 245 CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); 246 247 assert(isa<FunctionType>(FTy)); 248 249 // When declaring a function without a prototype, always use a 250 // non-variadic type. 251 if (isa<FunctionNoProtoType>(FTy)) { 252 CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>(); 253 return arrangeLLVMFunctionInfo(noProto->getReturnType(), false, None, 254 noProto->getExtInfo(), RequiredArgs::All); 255 } 256 257 assert(isa<FunctionProtoType>(FTy)); 258 return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>()); 259 } 260 261 /// Arrange the argument and result information for the declaration or 262 /// definition of an Objective-C method. 263 const CGFunctionInfo & 264 CodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) { 265 // It happens that this is the same as a call with no optional 266 // arguments, except also using the formal 'self' type. 267 return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType()); 268 } 269 270 /// Arrange the argument and result information for the function type 271 /// through which to perform a send to the given Objective-C method, 272 /// using the given receiver type. The receiver type is not always 273 /// the 'self' type of the method or even an Objective-C pointer type. 274 /// This is *not* the right method for actually performing such a 275 /// message send, due to the possibility of optional arguments. 276 const CGFunctionInfo & 277 CodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 278 QualType receiverType) { 279 SmallVector<CanQualType, 16> argTys; 280 argTys.push_back(Context.getCanonicalParamType(receiverType)); 281 argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); 282 // FIXME: Kill copy? 283 for (const auto *I : MD->params()) { 284 argTys.push_back(Context.getCanonicalParamType(I->getType())); 285 } 286 287 FunctionType::ExtInfo einfo; 288 bool IsWindows = getContext().getTargetInfo().getTriple().isOSWindows(); 289 einfo = einfo.withCallingConv(getCallingConventionForDecl(MD, IsWindows)); 290 291 if (getContext().getLangOpts().ObjCAutoRefCount && 292 MD->hasAttr<NSReturnsRetainedAttr>()) 293 einfo = einfo.withProducesResult(true); 294 295 RequiredArgs required = 296 (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All); 297 298 return arrangeLLVMFunctionInfo(GetReturnType(MD->getReturnType()), false, 299 argTys, einfo, required); 300 } 301 302 const CGFunctionInfo & 303 CodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) { 304 // FIXME: Do we need to handle ObjCMethodDecl? 305 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 306 307 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) 308 return arrangeCXXStructorDeclaration(CD, getFromCtorType(GD.getCtorType())); 309 310 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) 311 return arrangeCXXStructorDeclaration(DD, getFromDtorType(GD.getDtorType())); 312 313 return arrangeFunctionDeclaration(FD); 314 } 315 316 /// Arrange a thunk that takes 'this' as the first parameter followed by 317 /// varargs. Return a void pointer, regardless of the actual return type. 318 /// The body of the thunk will end in a musttail call to a function of the 319 /// correct type, and the caller will bitcast the function to the correct 320 /// prototype. 321 const CGFunctionInfo & 322 CodeGenTypes::arrangeMSMemberPointerThunk(const CXXMethodDecl *MD) { 323 assert(MD->isVirtual() && "only virtual memptrs have thunks"); 324 CanQual<FunctionProtoType> FTP = GetFormalType(MD); 325 CanQualType ArgTys[] = { GetThisType(Context, MD->getParent()) }; 326 return arrangeLLVMFunctionInfo(Context.VoidTy, false, ArgTys, 327 FTP->getExtInfo(), RequiredArgs(1)); 328 } 329 330 /// Arrange a call as unto a free function, except possibly with an 331 /// additional number of formal parameters considered required. 332 static const CGFunctionInfo & 333 arrangeFreeFunctionLikeCall(CodeGenTypes &CGT, 334 CodeGenModule &CGM, 335 const CallArgList &args, 336 const FunctionType *fnType, 337 unsigned numExtraRequiredArgs) { 338 assert(args.size() >= numExtraRequiredArgs); 339 340 // In most cases, there are no optional arguments. 341 RequiredArgs required = RequiredArgs::All; 342 343 // If we have a variadic prototype, the required arguments are the 344 // extra prefix plus the arguments in the prototype. 345 if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) { 346 if (proto->isVariadic()) 347 required = RequiredArgs(proto->getNumParams() + numExtraRequiredArgs); 348 349 // If we don't have a prototype at all, but we're supposed to 350 // explicitly use the variadic convention for unprototyped calls, 351 // treat all of the arguments as required but preserve the nominal 352 // possibility of variadics. 353 } else if (CGM.getTargetCodeGenInfo() 354 .isNoProtoCallVariadic(args, 355 cast<FunctionNoProtoType>(fnType))) { 356 required = RequiredArgs(args.size()); 357 } 358 359 return CGT.arrangeFreeFunctionCall(fnType->getReturnType(), args, 360 fnType->getExtInfo(), required); 361 } 362 363 /// Figure out the rules for calling a function with the given formal 364 /// type using the given arguments. The arguments are necessary 365 /// because the function might be unprototyped, in which case it's 366 /// target-dependent in crazy ways. 367 const CGFunctionInfo & 368 CodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args, 369 const FunctionType *fnType) { 370 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 0); 371 } 372 373 /// A block function call is essentially a free-function call with an 374 /// extra implicit argument. 375 const CGFunctionInfo & 376 CodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args, 377 const FunctionType *fnType) { 378 return arrangeFreeFunctionLikeCall(*this, CGM, args, fnType, 1); 379 } 380 381 const CGFunctionInfo & 382 CodeGenTypes::arrangeFreeFunctionCall(QualType resultType, 383 const CallArgList &args, 384 FunctionType::ExtInfo info, 385 RequiredArgs required) { 386 // FIXME: Kill copy. 387 SmallVector<CanQualType, 16> argTypes; 388 for (const auto &Arg : args) 389 argTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); 390 return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, 391 info, required); 392 } 393 394 /// Arrange a call to a C++ method, passing the given arguments. 395 const CGFunctionInfo & 396 CodeGenTypes::arrangeCXXMethodCall(const CallArgList &args, 397 const FunctionProtoType *FPT, 398 RequiredArgs required) { 399 // FIXME: Kill copy. 400 SmallVector<CanQualType, 16> argTypes; 401 for (const auto &Arg : args) 402 argTypes.push_back(Context.getCanonicalParamType(Arg.Ty)); 403 404 FunctionType::ExtInfo info = FPT->getExtInfo(); 405 return arrangeLLVMFunctionInfo(GetReturnType(FPT->getReturnType()), true, 406 argTypes, info, required); 407 } 408 409 const CGFunctionInfo &CodeGenTypes::arrangeFreeFunctionDeclaration( 410 QualType resultType, const FunctionArgList &args, 411 const FunctionType::ExtInfo &info, bool isVariadic) { 412 // FIXME: Kill copy. 413 SmallVector<CanQualType, 16> argTypes; 414 for (auto Arg : args) 415 argTypes.push_back(Context.getCanonicalParamType(Arg->getType())); 416 417 RequiredArgs required = 418 (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All); 419 return arrangeLLVMFunctionInfo(GetReturnType(resultType), false, argTypes, info, 420 required); 421 } 422 423 const CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() { 424 return arrangeLLVMFunctionInfo(getContext().VoidTy, false, None, 425 FunctionType::ExtInfo(), RequiredArgs::All); 426 } 427 428 /// Arrange the argument and result information for an abstract value 429 /// of a given function type. This is the method which all of the 430 /// above functions ultimately defer to. 431 const CGFunctionInfo & 432 CodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType, 433 bool IsInstanceMethod, 434 ArrayRef<CanQualType> argTypes, 435 FunctionType::ExtInfo info, 436 RequiredArgs required) { 437 #ifndef NDEBUG 438 for (ArrayRef<CanQualType>::const_iterator 439 I = argTypes.begin(), E = argTypes.end(); I != E; ++I) 440 assert(I->isCanonicalAsParam()); 441 #endif 442 443 unsigned CC = ClangCallConvToLLVMCallConv(info.getCC()); 444 445 // Lookup or create unique function info. 446 llvm::FoldingSetNodeID ID; 447 CGFunctionInfo::Profile(ID, IsInstanceMethod, info, required, resultType, 448 argTypes); 449 450 void *insertPos = nullptr; 451 CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos); 452 if (FI) 453 return *FI; 454 455 // Construct the function info. We co-allocate the ArgInfos. 456 FI = CGFunctionInfo::create(CC, IsInstanceMethod, info, resultType, argTypes, 457 required); 458 FunctionInfos.InsertNode(FI, insertPos); 459 460 bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted; 461 assert(inserted && "Recursively being processed?"); 462 463 // Compute ABI information. 464 getABIInfo().computeInfo(*FI); 465 466 // Loop over all of the computed argument and return value info. If any of 467 // them are direct or extend without a specified coerce type, specify the 468 // default now. 469 ABIArgInfo &retInfo = FI->getReturnInfo(); 470 if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == nullptr) 471 retInfo.setCoerceToType(ConvertType(FI->getReturnType())); 472 473 for (auto &I : FI->arguments()) 474 if (I.info.canHaveCoerceToType() && I.info.getCoerceToType() == nullptr) 475 I.info.setCoerceToType(ConvertType(I.type)); 476 477 bool erased = FunctionsBeingProcessed.erase(FI); (void)erased; 478 assert(erased && "Not in set?"); 479 480 return *FI; 481 } 482 483 CGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, 484 bool IsInstanceMethod, 485 const FunctionType::ExtInfo &info, 486 CanQualType resultType, 487 ArrayRef<CanQualType> argTypes, 488 RequiredArgs required) { 489 void *buffer = operator new(sizeof(CGFunctionInfo) + 490 sizeof(ArgInfo) * (argTypes.size() + 1)); 491 CGFunctionInfo *FI = new(buffer) CGFunctionInfo(); 492 FI->CallingConvention = llvmCC; 493 FI->EffectiveCallingConvention = llvmCC; 494 FI->ASTCallingConvention = info.getCC(); 495 FI->InstanceMethod = IsInstanceMethod; 496 FI->NoReturn = info.getNoReturn(); 497 FI->ReturnsRetained = info.getProducesResult(); 498 FI->Required = required; 499 FI->HasRegParm = info.getHasRegParm(); 500 FI->RegParm = info.getRegParm(); 501 FI->ArgStruct = nullptr; 502 FI->NumArgs = argTypes.size(); 503 FI->getArgsBuffer()[0].type = resultType; 504 for (unsigned i = 0, e = argTypes.size(); i != e; ++i) 505 FI->getArgsBuffer()[i + 1].type = argTypes[i]; 506 return FI; 507 } 508 509 /***/ 510 511 namespace { 512 // ABIArgInfo::Expand implementation. 513 514 // Specifies the way QualType passed as ABIArgInfo::Expand is expanded. 515 struct TypeExpansion { 516 enum TypeExpansionKind { 517 // Elements of constant arrays are expanded recursively. 518 TEK_ConstantArray, 519 // Record fields are expanded recursively (but if record is a union, only 520 // the field with the largest size is expanded). 521 TEK_Record, 522 // For complex types, real and imaginary parts are expanded recursively. 523 TEK_Complex, 524 // All other types are not expandable. 525 TEK_None 526 }; 527 528 const TypeExpansionKind Kind; 529 530 TypeExpansion(TypeExpansionKind K) : Kind(K) {} 531 virtual ~TypeExpansion() {} 532 }; 533 534 struct ConstantArrayExpansion : TypeExpansion { 535 QualType EltTy; 536 uint64_t NumElts; 537 538 ConstantArrayExpansion(QualType EltTy, uint64_t NumElts) 539 : TypeExpansion(TEK_ConstantArray), EltTy(EltTy), NumElts(NumElts) {} 540 static bool classof(const TypeExpansion *TE) { 541 return TE->Kind == TEK_ConstantArray; 542 } 543 }; 544 545 struct RecordExpansion : TypeExpansion { 546 SmallVector<const FieldDecl *, 1> Fields; 547 548 RecordExpansion(SmallVector<const FieldDecl *, 1> &&Fields) 549 : TypeExpansion(TEK_Record), Fields(Fields) {} 550 static bool classof(const TypeExpansion *TE) { 551 return TE->Kind == TEK_Record; 552 } 553 }; 554 555 struct ComplexExpansion : TypeExpansion { 556 QualType EltTy; 557 558 ComplexExpansion(QualType EltTy) : TypeExpansion(TEK_Complex), EltTy(EltTy) {} 559 static bool classof(const TypeExpansion *TE) { 560 return TE->Kind == TEK_Complex; 561 } 562 }; 563 564 struct NoExpansion : TypeExpansion { 565 NoExpansion() : TypeExpansion(TEK_None) {} 566 static bool classof(const TypeExpansion *TE) { 567 return TE->Kind == TEK_None; 568 } 569 }; 570 } // namespace 571 572 static std::unique_ptr<TypeExpansion> 573 getTypeExpansion(QualType Ty, const ASTContext &Context) { 574 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { 575 return llvm::make_unique<ConstantArrayExpansion>( 576 AT->getElementType(), AT->getSize().getZExtValue()); 577 } 578 if (const RecordType *RT = Ty->getAs<RecordType>()) { 579 SmallVector<const FieldDecl *, 1> Fields; 580 const RecordDecl *RD = RT->getDecl(); 581 assert(!RD->hasFlexibleArrayMember() && 582 "Cannot expand structure with flexible array."); 583 if (RD->isUnion()) { 584 // Unions can be here only in degenerative cases - all the fields are same 585 // after flattening. Thus we have to use the "largest" field. 586 const FieldDecl *LargestFD = nullptr; 587 CharUnits UnionSize = CharUnits::Zero(); 588 589 for (const auto *FD : RD->fields()) { 590 assert(!FD->isBitField() && 591 "Cannot expand structure with bit-field members."); 592 CharUnits FieldSize = Context.getTypeSizeInChars(FD->getType()); 593 if (UnionSize < FieldSize) { 594 UnionSize = FieldSize; 595 LargestFD = FD; 596 } 597 } 598 if (LargestFD) 599 Fields.push_back(LargestFD); 600 } else { 601 for (const auto *FD : RD->fields()) { 602 assert(!FD->isBitField() && 603 "Cannot expand structure with bit-field members."); 604 Fields.push_back(FD); 605 } 606 } 607 return llvm::make_unique<RecordExpansion>(std::move(Fields)); 608 } 609 if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 610 return llvm::make_unique<ComplexExpansion>(CT->getElementType()); 611 } 612 return llvm::make_unique<NoExpansion>(); 613 } 614 615 static int getExpansionSize(QualType Ty, const ASTContext &Context) { 616 auto Exp = getTypeExpansion(Ty, Context); 617 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { 618 return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context); 619 } 620 if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { 621 int Res = 0; 622 for (auto FD : RExp->Fields) 623 Res += getExpansionSize(FD->getType(), Context); 624 return Res; 625 } 626 if (isa<ComplexExpansion>(Exp.get())) 627 return 2; 628 assert(isa<NoExpansion>(Exp.get())); 629 return 1; 630 } 631 632 void 633 CodeGenTypes::getExpandedTypes(QualType Ty, 634 SmallVectorImpl<llvm::Type *>::iterator &TI) { 635 auto Exp = getTypeExpansion(Ty, Context); 636 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { 637 for (int i = 0, n = CAExp->NumElts; i < n; i++) { 638 getExpandedTypes(CAExp->EltTy, TI); 639 } 640 } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { 641 for (auto FD : RExp->Fields) { 642 getExpandedTypes(FD->getType(), TI); 643 } 644 } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) { 645 llvm::Type *EltTy = ConvertType(CExp->EltTy); 646 *TI++ = EltTy; 647 *TI++ = EltTy; 648 } else { 649 assert(isa<NoExpansion>(Exp.get())); 650 *TI++ = ConvertType(Ty); 651 } 652 } 653 654 void CodeGenFunction::ExpandTypeFromArgs( 655 QualType Ty, LValue LV, SmallVectorImpl<llvm::Argument *>::iterator &AI) { 656 assert(LV.isSimple() && 657 "Unexpected non-simple lvalue during struct expansion."); 658 659 auto Exp = getTypeExpansion(Ty, getContext()); 660 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { 661 for (int i = 0, n = CAExp->NumElts; i < n; i++) { 662 llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, i); 663 LValue LV = MakeAddrLValue(EltAddr, CAExp->EltTy); 664 ExpandTypeFromArgs(CAExp->EltTy, LV, AI); 665 } 666 } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { 667 for (auto FD : RExp->Fields) { 668 // FIXME: What are the right qualifiers here? 669 LValue SubLV = EmitLValueForField(LV, FD); 670 ExpandTypeFromArgs(FD->getType(), SubLV, AI); 671 } 672 } else if (auto CExp = dyn_cast<ComplexExpansion>(Exp.get())) { 673 llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real"); 674 EmitStoreThroughLValue(RValue::get(*AI++), 675 MakeAddrLValue(RealAddr, CExp->EltTy)); 676 llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag"); 677 EmitStoreThroughLValue(RValue::get(*AI++), 678 MakeAddrLValue(ImagAddr, CExp->EltTy)); 679 } else { 680 assert(isa<NoExpansion>(Exp.get())); 681 EmitStoreThroughLValue(RValue::get(*AI++), LV); 682 } 683 } 684 685 void CodeGenFunction::ExpandTypeToArgs( 686 QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy, 687 SmallVectorImpl<llvm::Value *> &IRCallArgs, unsigned &IRCallArgPos) { 688 auto Exp = getTypeExpansion(Ty, getContext()); 689 if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) { 690 llvm::Value *Addr = RV.getAggregateAddr(); 691 for (int i = 0, n = CAExp->NumElts; i < n; i++) { 692 llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, i); 693 RValue EltRV = 694 convertTempToRValue(EltAddr, CAExp->EltTy, SourceLocation()); 695 ExpandTypeToArgs(CAExp->EltTy, EltRV, IRFuncTy, IRCallArgs, IRCallArgPos); 696 } 697 } else if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) { 698 LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty); 699 for (auto FD : RExp->Fields) { 700 RValue FldRV = EmitRValueForField(LV, FD, SourceLocation()); 701 ExpandTypeToArgs(FD->getType(), FldRV, IRFuncTy, IRCallArgs, 702 IRCallArgPos); 703 } 704 } else if (isa<ComplexExpansion>(Exp.get())) { 705 ComplexPairTy CV = RV.getComplexVal(); 706 IRCallArgs[IRCallArgPos++] = CV.first; 707 IRCallArgs[IRCallArgPos++] = CV.second; 708 } else { 709 assert(isa<NoExpansion>(Exp.get())); 710 assert(RV.isScalar() && 711 "Unexpected non-scalar rvalue during struct expansion."); 712 713 // Insert a bitcast as needed. 714 llvm::Value *V = RV.getScalarVal(); 715 if (IRCallArgPos < IRFuncTy->getNumParams() && 716 V->getType() != IRFuncTy->getParamType(IRCallArgPos)) 717 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRCallArgPos)); 718 719 IRCallArgs[IRCallArgPos++] = V; 720 } 721 } 722 723 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are 724 /// accessing some number of bytes out of it, try to gep into the struct to get 725 /// at its inner goodness. Dive as deep as possible without entering an element 726 /// with an in-memory size smaller than DstSize. 727 static llvm::Value * 728 EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, 729 llvm::StructType *SrcSTy, 730 uint64_t DstSize, CodeGenFunction &CGF) { 731 // We can't dive into a zero-element struct. 732 if (SrcSTy->getNumElements() == 0) return SrcPtr; 733 734 llvm::Type *FirstElt = SrcSTy->getElementType(0); 735 736 // If the first elt is at least as large as what we're looking for, or if the 737 // first element is the same size as the whole struct, we can enter it. The 738 // comparison must be made on the store size and not the alloca size. Using 739 // the alloca size may overstate the size of the load. 740 uint64_t FirstEltSize = 741 CGF.CGM.getDataLayout().getTypeStoreSize(FirstElt); 742 if (FirstEltSize < DstSize && 743 FirstEltSize < CGF.CGM.getDataLayout().getTypeStoreSize(SrcSTy)) 744 return SrcPtr; 745 746 // GEP into the first element. 747 SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); 748 749 // If the first element is a struct, recurse. 750 llvm::Type *SrcTy = 751 cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 752 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) 753 return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 754 755 return SrcPtr; 756 } 757 758 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both 759 /// are either integers or pointers. This does a truncation of the value if it 760 /// is too large or a zero extension if it is too small. 761 /// 762 /// This behaves as if the value were coerced through memory, so on big-endian 763 /// targets the high bits are preserved in a truncation, while little-endian 764 /// targets preserve the low bits. 765 static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, 766 llvm::Type *Ty, 767 CodeGenFunction &CGF) { 768 if (Val->getType() == Ty) 769 return Val; 770 771 if (isa<llvm::PointerType>(Val->getType())) { 772 // If this is Pointer->Pointer avoid conversion to and from int. 773 if (isa<llvm::PointerType>(Ty)) 774 return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); 775 776 // Convert the pointer to an integer so we can play with its width. 777 Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); 778 } 779 780 llvm::Type *DestIntTy = Ty; 781 if (isa<llvm::PointerType>(DestIntTy)) 782 DestIntTy = CGF.IntPtrTy; 783 784 if (Val->getType() != DestIntTy) { 785 const llvm::DataLayout &DL = CGF.CGM.getDataLayout(); 786 if (DL.isBigEndian()) { 787 // Preserve the high bits on big-endian targets. 788 // That is what memory coercion does. 789 uint64_t SrcSize = DL.getTypeSizeInBits(Val->getType()); 790 uint64_t DstSize = DL.getTypeSizeInBits(DestIntTy); 791 792 if (SrcSize > DstSize) { 793 Val = CGF.Builder.CreateLShr(Val, SrcSize - DstSize, "coerce.highbits"); 794 Val = CGF.Builder.CreateTrunc(Val, DestIntTy, "coerce.val.ii"); 795 } else { 796 Val = CGF.Builder.CreateZExt(Val, DestIntTy, "coerce.val.ii"); 797 Val = CGF.Builder.CreateShl(Val, DstSize - SrcSize, "coerce.highbits"); 798 } 799 } else { 800 // Little-endian targets preserve the low bits. No shifts required. 801 Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); 802 } 803 } 804 805 if (isa<llvm::PointerType>(Ty)) 806 Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); 807 return Val; 808 } 809 810 811 812 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as 813 /// a pointer to an object of type \arg Ty. 814 /// 815 /// This safely handles the case when the src type is smaller than the 816 /// destination type; in this situation the values of bits which not 817 /// present in the src are undefined. 818 static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, 819 llvm::Type *Ty, 820 CodeGenFunction &CGF) { 821 llvm::Type *SrcTy = 822 cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 823 824 // If SrcTy and Ty are the same, just do a load. 825 if (SrcTy == Ty) 826 return CGF.Builder.CreateLoad(SrcPtr); 827 828 uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty); 829 830 if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { 831 SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 832 SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 833 } 834 835 uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); 836 837 // If the source and destination are integer or pointer types, just do an 838 // extension or truncation to the desired type. 839 if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && 840 (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { 841 llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); 842 return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); 843 } 844 845 // If load is legal, just bitcast the src pointer. 846 if (SrcSize >= DstSize) { 847 // Generally SrcSize is never greater than DstSize, since this means we are 848 // losing bits. However, this can happen in cases where the structure has 849 // additional padding, for example due to a user specified alignment. 850 // 851 // FIXME: Assert that we aren't truncating non-padding bits when have access 852 // to that information. 853 llvm::Value *Casted = 854 CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); 855 llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 856 // FIXME: Use better alignment / avoid requiring aligned load. 857 Load->setAlignment(1); 858 return Load; 859 } 860 861 // Otherwise do coercion through memory. This is stupid, but 862 // simple. 863 llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); 864 llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); 865 llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); 866 llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy); 867 // FIXME: Use better alignment. 868 CGF.Builder.CreateMemCpy(Casted, SrcCasted, 869 llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize), 870 1, false); 871 return CGF.Builder.CreateLoad(Tmp); 872 } 873 874 // Function to store a first-class aggregate into memory. We prefer to 875 // store the elements rather than the aggregate to be more friendly to 876 // fast-isel. 877 // FIXME: Do we need to recurse here? 878 static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, 879 llvm::Value *DestPtr, bool DestIsVolatile, 880 bool LowAlignment) { 881 // Prefer scalar stores to first-class aggregate stores. 882 if (llvm::StructType *STy = 883 dyn_cast<llvm::StructType>(Val->getType())) { 884 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 885 llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); 886 llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); 887 llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, 888 DestIsVolatile); 889 if (LowAlignment) 890 SI->setAlignment(1); 891 } 892 } else { 893 llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); 894 if (LowAlignment) 895 SI->setAlignment(1); 896 } 897 } 898 899 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, 900 /// where the source and destination may have different types. 901 /// 902 /// This safely handles the case when the src type is larger than the 903 /// destination type; the upper bits of the src will be lost. 904 static void CreateCoercedStore(llvm::Value *Src, 905 llvm::Value *DstPtr, 906 bool DstIsVolatile, 907 CodeGenFunction &CGF) { 908 llvm::Type *SrcTy = Src->getType(); 909 llvm::Type *DstTy = 910 cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 911 if (SrcTy == DstTy) { 912 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 913 return; 914 } 915 916 uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy); 917 918 if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { 919 DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); 920 DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 921 } 922 923 // If the source and destination are integer or pointer types, just do an 924 // extension or truncation to the desired type. 925 if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && 926 (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { 927 Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); 928 CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 929 return; 930 } 931 932 uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy); 933 934 // If store is legal, just bitcast the src pointer. 935 if (SrcSize <= DstSize) { 936 llvm::Value *Casted = 937 CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); 938 // FIXME: Use better alignment / avoid requiring aligned store. 939 BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); 940 } else { 941 // Otherwise do coercion through memory. This is stupid, but 942 // simple. 943 944 // Generally SrcSize is never greater than DstSize, since this means we are 945 // losing bits. However, this can happen in cases where the structure has 946 // additional padding, for example due to a user specified alignment. 947 // 948 // FIXME: Assert that we aren't truncating non-padding bits when have access 949 // to that information. 950 llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); 951 CGF.Builder.CreateStore(Src, Tmp); 952 llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy(); 953 llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy); 954 llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy); 955 // FIXME: Use better alignment. 956 CGF.Builder.CreateMemCpy(DstCasted, Casted, 957 llvm::ConstantInt::get(CGF.IntPtrTy, DstSize), 958 1, false); 959 } 960 } 961 962 namespace { 963 964 /// Encapsulates information about the way function arguments from 965 /// CGFunctionInfo should be passed to actual LLVM IR function. 966 class ClangToLLVMArgMapping { 967 static const unsigned InvalidIndex = ~0U; 968 unsigned InallocaArgNo; 969 unsigned SRetArgNo; 970 unsigned TotalIRArgs; 971 972 /// Arguments of LLVM IR function corresponding to single Clang argument. 973 struct IRArgs { 974 unsigned PaddingArgIndex; 975 // Argument is expanded to IR arguments at positions 976 // [FirstArgIndex, FirstArgIndex + NumberOfArgs). 977 unsigned FirstArgIndex; 978 unsigned NumberOfArgs; 979 980 IRArgs() 981 : PaddingArgIndex(InvalidIndex), FirstArgIndex(InvalidIndex), 982 NumberOfArgs(0) {} 983 }; 984 985 SmallVector<IRArgs, 8> ArgInfo; 986 987 public: 988 ClangToLLVMArgMapping(const ASTContext &Context, const CGFunctionInfo &FI, 989 bool OnlyRequiredArgs = false) 990 : InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalIRArgs(0), 991 ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) { 992 construct(Context, FI, OnlyRequiredArgs); 993 } 994 995 bool hasInallocaArg() const { return InallocaArgNo != InvalidIndex; } 996 unsigned getInallocaArgNo() const { 997 assert(hasInallocaArg()); 998 return InallocaArgNo; 999 } 1000 1001 bool hasSRetArg() const { return SRetArgNo != InvalidIndex; } 1002 unsigned getSRetArgNo() const { 1003 assert(hasSRetArg()); 1004 return SRetArgNo; 1005 } 1006 1007 unsigned totalIRArgs() const { return TotalIRArgs; } 1008 1009 bool hasPaddingArg(unsigned ArgNo) const { 1010 assert(ArgNo < ArgInfo.size()); 1011 return ArgInfo[ArgNo].PaddingArgIndex != InvalidIndex; 1012 } 1013 unsigned getPaddingArgNo(unsigned ArgNo) const { 1014 assert(hasPaddingArg(ArgNo)); 1015 return ArgInfo[ArgNo].PaddingArgIndex; 1016 } 1017 1018 /// Returns index of first IR argument corresponding to ArgNo, and their 1019 /// quantity. 1020 std::pair<unsigned, unsigned> getIRArgs(unsigned ArgNo) const { 1021 assert(ArgNo < ArgInfo.size()); 1022 return std::make_pair(ArgInfo[ArgNo].FirstArgIndex, 1023 ArgInfo[ArgNo].NumberOfArgs); 1024 } 1025 1026 private: 1027 void construct(const ASTContext &Context, const CGFunctionInfo &FI, 1028 bool OnlyRequiredArgs); 1029 }; 1030 1031 void ClangToLLVMArgMapping::construct(const ASTContext &Context, 1032 const CGFunctionInfo &FI, 1033 bool OnlyRequiredArgs) { 1034 unsigned IRArgNo = 0; 1035 bool SwapThisWithSRet = false; 1036 const ABIArgInfo &RetAI = FI.getReturnInfo(); 1037 1038 if (RetAI.getKind() == ABIArgInfo::Indirect) { 1039 SwapThisWithSRet = RetAI.isSRetAfterThis(); 1040 SRetArgNo = SwapThisWithSRet ? 1 : IRArgNo++; 1041 } 1042 1043 unsigned ArgNo = 0; 1044 unsigned NumArgs = OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size(); 1045 for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(); ArgNo < NumArgs; 1046 ++I, ++ArgNo) { 1047 assert(I != FI.arg_end()); 1048 QualType ArgType = I->type; 1049 const ABIArgInfo &AI = I->info; 1050 // Collect data about IR arguments corresponding to Clang argument ArgNo. 1051 auto &IRArgs = ArgInfo[ArgNo]; 1052 1053 if (AI.getPaddingType()) 1054 IRArgs.PaddingArgIndex = IRArgNo++; 1055 1056 switch (AI.getKind()) { 1057 case ABIArgInfo::Extend: 1058 case ABIArgInfo::Direct: { 1059 // FIXME: handle sseregparm someday... 1060 llvm::StructType *STy = dyn_cast<llvm::StructType>(AI.getCoerceToType()); 1061 if (AI.isDirect() && AI.getCanBeFlattened() && STy) { 1062 IRArgs.NumberOfArgs = STy->getNumElements(); 1063 } else { 1064 IRArgs.NumberOfArgs = 1; 1065 } 1066 break; 1067 } 1068 case ABIArgInfo::Indirect: 1069 IRArgs.NumberOfArgs = 1; 1070 break; 1071 case ABIArgInfo::Ignore: 1072 case ABIArgInfo::InAlloca: 1073 // ignore and inalloca doesn't have matching LLVM parameters. 1074 IRArgs.NumberOfArgs = 0; 1075 break; 1076 case ABIArgInfo::Expand: { 1077 IRArgs.NumberOfArgs = getExpansionSize(ArgType, Context); 1078 break; 1079 } 1080 } 1081 1082 if (IRArgs.NumberOfArgs > 0) { 1083 IRArgs.FirstArgIndex = IRArgNo; 1084 IRArgNo += IRArgs.NumberOfArgs; 1085 } 1086 1087 // Skip over the sret parameter when it comes second. We already handled it 1088 // above. 1089 if (IRArgNo == 1 && SwapThisWithSRet) 1090 IRArgNo++; 1091 } 1092 assert(ArgNo == ArgInfo.size()); 1093 1094 if (FI.usesInAlloca()) 1095 InallocaArgNo = IRArgNo++; 1096 1097 TotalIRArgs = IRArgNo; 1098 } 1099 } // namespace 1100 1101 /***/ 1102 1103 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { 1104 return FI.getReturnInfo().isIndirect(); 1105 } 1106 1107 bool CodeGenModule::ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI) { 1108 return ReturnTypeUsesSRet(FI) && 1109 getTargetCodeGenInfo().doesReturnSlotInterfereWithArgs(); 1110 } 1111 1112 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { 1113 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { 1114 switch (BT->getKind()) { 1115 default: 1116 return false; 1117 case BuiltinType::Float: 1118 return getTarget().useObjCFPRetForRealType(TargetInfo::Float); 1119 case BuiltinType::Double: 1120 return getTarget().useObjCFPRetForRealType(TargetInfo::Double); 1121 case BuiltinType::LongDouble: 1122 return getTarget().useObjCFPRetForRealType(TargetInfo::LongDouble); 1123 } 1124 } 1125 1126 return false; 1127 } 1128 1129 bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { 1130 if (const ComplexType *CT = ResultType->getAs<ComplexType>()) { 1131 if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) { 1132 if (BT->getKind() == BuiltinType::LongDouble) 1133 return getTarget().useObjCFP2RetForComplexLongDouble(); 1134 } 1135 } 1136 1137 return false; 1138 } 1139 1140 llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { 1141 const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD); 1142 return GetFunctionType(FI); 1143 } 1144 1145 llvm::FunctionType * 1146 CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { 1147 1148 bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted; 1149 assert(Inserted && "Recursively being processed?"); 1150 1151 llvm::Type *resultType = nullptr; 1152 const ABIArgInfo &retAI = FI.getReturnInfo(); 1153 switch (retAI.getKind()) { 1154 case ABIArgInfo::Expand: 1155 llvm_unreachable("Invalid ABI kind for return argument"); 1156 1157 case ABIArgInfo::Extend: 1158 case ABIArgInfo::Direct: 1159 resultType = retAI.getCoerceToType(); 1160 break; 1161 1162 case ABIArgInfo::InAlloca: 1163 if (retAI.getInAllocaSRet()) { 1164 // sret things on win32 aren't void, they return the sret pointer. 1165 QualType ret = FI.getReturnType(); 1166 llvm::Type *ty = ConvertType(ret); 1167 unsigned addressSpace = Context.getTargetAddressSpace(ret); 1168 resultType = llvm::PointerType::get(ty, addressSpace); 1169 } else { 1170 resultType = llvm::Type::getVoidTy(getLLVMContext()); 1171 } 1172 break; 1173 1174 case ABIArgInfo::Indirect: { 1175 assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); 1176 resultType = llvm::Type::getVoidTy(getLLVMContext()); 1177 break; 1178 } 1179 1180 case ABIArgInfo::Ignore: 1181 resultType = llvm::Type::getVoidTy(getLLVMContext()); 1182 break; 1183 } 1184 1185 ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI, true); 1186 SmallVector<llvm::Type*, 8> ArgTypes(IRFunctionArgs.totalIRArgs()); 1187 1188 // Add type for sret argument. 1189 if (IRFunctionArgs.hasSRetArg()) { 1190 QualType Ret = FI.getReturnType(); 1191 llvm::Type *Ty = ConvertType(Ret); 1192 unsigned AddressSpace = Context.getTargetAddressSpace(Ret); 1193 ArgTypes[IRFunctionArgs.getSRetArgNo()] = 1194 llvm::PointerType::get(Ty, AddressSpace); 1195 } 1196 1197 // Add type for inalloca argument. 1198 if (IRFunctionArgs.hasInallocaArg()) { 1199 auto ArgStruct = FI.getArgStruct(); 1200 assert(ArgStruct); 1201 ArgTypes[IRFunctionArgs.getInallocaArgNo()] = ArgStruct->getPointerTo(); 1202 } 1203 1204 // Add in all of the required arguments. 1205 unsigned ArgNo = 0; 1206 CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 1207 ie = it + FI.getNumRequiredArgs(); 1208 for (; it != ie; ++it, ++ArgNo) { 1209 const ABIArgInfo &ArgInfo = it->info; 1210 1211 // Insert a padding type to ensure proper alignment. 1212 if (IRFunctionArgs.hasPaddingArg(ArgNo)) 1213 ArgTypes[IRFunctionArgs.getPaddingArgNo(ArgNo)] = 1214 ArgInfo.getPaddingType(); 1215 1216 unsigned FirstIRArg, NumIRArgs; 1217 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); 1218 1219 switch (ArgInfo.getKind()) { 1220 case ABIArgInfo::Ignore: 1221 case ABIArgInfo::InAlloca: 1222 assert(NumIRArgs == 0); 1223 break; 1224 1225 case ABIArgInfo::Indirect: { 1226 assert(NumIRArgs == 1); 1227 // indirect arguments are always on the stack, which is addr space #0. 1228 llvm::Type *LTy = ConvertTypeForMem(it->type); 1229 ArgTypes[FirstIRArg] = LTy->getPointerTo(); 1230 break; 1231 } 1232 1233 case ABIArgInfo::Extend: 1234 case ABIArgInfo::Direct: { 1235 // Fast-isel and the optimizer generally like scalar values better than 1236 // FCAs, so we flatten them if this is safe to do for this argument. 1237 llvm::Type *argType = ArgInfo.getCoerceToType(); 1238 llvm::StructType *st = dyn_cast<llvm::StructType>(argType); 1239 if (st && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) { 1240 assert(NumIRArgs == st->getNumElements()); 1241 for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) 1242 ArgTypes[FirstIRArg + i] = st->getElementType(i); 1243 } else { 1244 assert(NumIRArgs == 1); 1245 ArgTypes[FirstIRArg] = argType; 1246 } 1247 break; 1248 } 1249 1250 case ABIArgInfo::Expand: 1251 auto ArgTypesIter = ArgTypes.begin() + FirstIRArg; 1252 getExpandedTypes(it->type, ArgTypesIter); 1253 assert(ArgTypesIter == ArgTypes.begin() + FirstIRArg + NumIRArgs); 1254 break; 1255 } 1256 } 1257 1258 bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased; 1259 assert(Erased && "Not in set?"); 1260 1261 return llvm::FunctionType::get(resultType, ArgTypes, FI.isVariadic()); 1262 } 1263 1264 llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { 1265 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 1266 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 1267 1268 if (!isFuncTypeConvertible(FPT)) 1269 return llvm::StructType::get(getLLVMContext()); 1270 1271 const CGFunctionInfo *Info; 1272 if (isa<CXXDestructorDecl>(MD)) 1273 Info = 1274 &arrangeCXXStructorDeclaration(MD, getFromDtorType(GD.getDtorType())); 1275 else 1276 Info = &arrangeCXXMethodDeclaration(MD); 1277 return GetFunctionType(*Info); 1278 } 1279 1280 void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, 1281 const Decl *TargetDecl, 1282 AttributeListType &PAL, 1283 unsigned &CallingConv, 1284 bool AttrOnCallSite) { 1285 llvm::AttrBuilder FuncAttrs; 1286 llvm::AttrBuilder RetAttrs; 1287 1288 CallingConv = FI.getEffectiveCallingConvention(); 1289 1290 if (FI.isNoReturn()) 1291 FuncAttrs.addAttribute(llvm::Attribute::NoReturn); 1292 1293 // FIXME: handle sseregparm someday... 1294 if (TargetDecl) { 1295 if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) 1296 FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice); 1297 if (TargetDecl->hasAttr<NoThrowAttr>()) 1298 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1299 if (TargetDecl->hasAttr<NoReturnAttr>()) 1300 FuncAttrs.addAttribute(llvm::Attribute::NoReturn); 1301 if (TargetDecl->hasAttr<NoDuplicateAttr>()) 1302 FuncAttrs.addAttribute(llvm::Attribute::NoDuplicate); 1303 1304 if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { 1305 const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); 1306 if (FPT && FPT->isNothrow(getContext())) 1307 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1308 // Don't use [[noreturn]] or _Noreturn for a call to a virtual function. 1309 // These attributes are not inherited by overloads. 1310 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn); 1311 if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual())) 1312 FuncAttrs.addAttribute(llvm::Attribute::NoReturn); 1313 } 1314 1315 // 'const' and 'pure' attribute functions are also nounwind. 1316 if (TargetDecl->hasAttr<ConstAttr>()) { 1317 FuncAttrs.addAttribute(llvm::Attribute::ReadNone); 1318 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1319 } else if (TargetDecl->hasAttr<PureAttr>()) { 1320 FuncAttrs.addAttribute(llvm::Attribute::ReadOnly); 1321 FuncAttrs.addAttribute(llvm::Attribute::NoUnwind); 1322 } 1323 if (TargetDecl->hasAttr<MallocAttr>()) 1324 RetAttrs.addAttribute(llvm::Attribute::NoAlias); 1325 if (TargetDecl->hasAttr<ReturnsNonNullAttr>()) 1326 RetAttrs.addAttribute(llvm::Attribute::NonNull); 1327 } 1328 1329 if (CodeGenOpts.OptimizeSize) 1330 FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize); 1331 if (CodeGenOpts.OptimizeSize == 2) 1332 FuncAttrs.addAttribute(llvm::Attribute::MinSize); 1333 if (CodeGenOpts.DisableRedZone) 1334 FuncAttrs.addAttribute(llvm::Attribute::NoRedZone); 1335 if (CodeGenOpts.NoImplicitFloat) 1336 FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat); 1337 if (CodeGenOpts.EnableSegmentedStacks && 1338 !(TargetDecl && TargetDecl->hasAttr<NoSplitStackAttr>())) 1339 FuncAttrs.addAttribute("split-stack"); 1340 1341 if (AttrOnCallSite) { 1342 // Attributes that should go on the call site only. 1343 if (!CodeGenOpts.SimplifyLibCalls) 1344 FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin); 1345 } else { 1346 // Attributes that should go on the function, but not the call site. 1347 if (!CodeGenOpts.DisableFPElim) { 1348 FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); 1349 } else if (CodeGenOpts.OmitLeafFramePointer) { 1350 FuncAttrs.addAttribute("no-frame-pointer-elim", "false"); 1351 FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); 1352 } else { 1353 FuncAttrs.addAttribute("no-frame-pointer-elim", "true"); 1354 FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf"); 1355 } 1356 1357 FuncAttrs.addAttribute("less-precise-fpmad", 1358 llvm::toStringRef(CodeGenOpts.LessPreciseFPMAD)); 1359 FuncAttrs.addAttribute("no-infs-fp-math", 1360 llvm::toStringRef(CodeGenOpts.NoInfsFPMath)); 1361 FuncAttrs.addAttribute("no-nans-fp-math", 1362 llvm::toStringRef(CodeGenOpts.NoNaNsFPMath)); 1363 FuncAttrs.addAttribute("unsafe-fp-math", 1364 llvm::toStringRef(CodeGenOpts.UnsafeFPMath)); 1365 FuncAttrs.addAttribute("use-soft-float", 1366 llvm::toStringRef(CodeGenOpts.SoftFloat)); 1367 FuncAttrs.addAttribute("stack-protector-buffer-size", 1368 llvm::utostr(CodeGenOpts.SSPBufferSize)); 1369 1370 if (!CodeGenOpts.StackRealignment) 1371 FuncAttrs.addAttribute("no-realign-stack"); 1372 } 1373 1374 ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI); 1375 1376 QualType RetTy = FI.getReturnType(); 1377 const ABIArgInfo &RetAI = FI.getReturnInfo(); 1378 switch (RetAI.getKind()) { 1379 case ABIArgInfo::Extend: 1380 if (RetTy->hasSignedIntegerRepresentation()) 1381 RetAttrs.addAttribute(llvm::Attribute::SExt); 1382 else if (RetTy->hasUnsignedIntegerRepresentation()) 1383 RetAttrs.addAttribute(llvm::Attribute::ZExt); 1384 // FALL THROUGH 1385 case ABIArgInfo::Direct: 1386 if (RetAI.getInReg()) 1387 RetAttrs.addAttribute(llvm::Attribute::InReg); 1388 break; 1389 case ABIArgInfo::Ignore: 1390 break; 1391 1392 case ABIArgInfo::InAlloca: 1393 case ABIArgInfo::Indirect: { 1394 // inalloca and sret disable readnone and readonly 1395 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) 1396 .removeAttribute(llvm::Attribute::ReadNone); 1397 break; 1398 } 1399 1400 case ABIArgInfo::Expand: 1401 llvm_unreachable("Invalid ABI kind for return argument"); 1402 } 1403 1404 if (const auto *RefTy = RetTy->getAs<ReferenceType>()) { 1405 QualType PTy = RefTy->getPointeeType(); 1406 if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) 1407 RetAttrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy) 1408 .getQuantity()); 1409 else if (getContext().getTargetAddressSpace(PTy) == 0) 1410 RetAttrs.addAttribute(llvm::Attribute::NonNull); 1411 } 1412 1413 // Attach return attributes. 1414 if (RetAttrs.hasAttributes()) { 1415 PAL.push_back(llvm::AttributeSet::get( 1416 getLLVMContext(), llvm::AttributeSet::ReturnIndex, RetAttrs)); 1417 } 1418 1419 // Attach attributes to sret. 1420 if (IRFunctionArgs.hasSRetArg()) { 1421 llvm::AttrBuilder SRETAttrs; 1422 SRETAttrs.addAttribute(llvm::Attribute::StructRet); 1423 if (RetAI.getInReg()) 1424 SRETAttrs.addAttribute(llvm::Attribute::InReg); 1425 PAL.push_back(llvm::AttributeSet::get( 1426 getLLVMContext(), IRFunctionArgs.getSRetArgNo() + 1, SRETAttrs)); 1427 } 1428 1429 // Attach attributes to inalloca argument. 1430 if (IRFunctionArgs.hasInallocaArg()) { 1431 llvm::AttrBuilder Attrs; 1432 Attrs.addAttribute(llvm::Attribute::InAlloca); 1433 PAL.push_back(llvm::AttributeSet::get( 1434 getLLVMContext(), IRFunctionArgs.getInallocaArgNo() + 1, Attrs)); 1435 } 1436 1437 1438 unsigned ArgNo = 0; 1439 for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin(), 1440 E = FI.arg_end(); 1441 I != E; ++I, ++ArgNo) { 1442 QualType ParamType = I->type; 1443 const ABIArgInfo &AI = I->info; 1444 llvm::AttrBuilder Attrs; 1445 1446 // Add attribute for padding argument, if necessary. 1447 if (IRFunctionArgs.hasPaddingArg(ArgNo)) { 1448 if (AI.getPaddingInReg()) 1449 PAL.push_back(llvm::AttributeSet::get( 1450 getLLVMContext(), IRFunctionArgs.getPaddingArgNo(ArgNo) + 1, 1451 llvm::Attribute::InReg)); 1452 } 1453 1454 // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we 1455 // have the corresponding parameter variable. It doesn't make 1456 // sense to do it here because parameters are so messed up. 1457 switch (AI.getKind()) { 1458 case ABIArgInfo::Extend: 1459 if (ParamType->isSignedIntegerOrEnumerationType()) 1460 Attrs.addAttribute(llvm::Attribute::SExt); 1461 else if (ParamType->isUnsignedIntegerOrEnumerationType()) 1462 Attrs.addAttribute(llvm::Attribute::ZExt); 1463 // FALL THROUGH 1464 case ABIArgInfo::Direct: 1465 if (AI.getInReg()) 1466 Attrs.addAttribute(llvm::Attribute::InReg); 1467 break; 1468 1469 case ABIArgInfo::Indirect: 1470 if (AI.getInReg()) 1471 Attrs.addAttribute(llvm::Attribute::InReg); 1472 1473 if (AI.getIndirectByVal()) 1474 Attrs.addAttribute(llvm::Attribute::ByVal); 1475 1476 Attrs.addAlignmentAttr(AI.getIndirectAlign()); 1477 1478 // byval disables readnone and readonly. 1479 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) 1480 .removeAttribute(llvm::Attribute::ReadNone); 1481 break; 1482 1483 case ABIArgInfo::Ignore: 1484 case ABIArgInfo::Expand: 1485 continue; 1486 1487 case ABIArgInfo::InAlloca: 1488 // inalloca disables readnone and readonly. 1489 FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly) 1490 .removeAttribute(llvm::Attribute::ReadNone); 1491 continue; 1492 } 1493 1494 if (const auto *RefTy = ParamType->getAs<ReferenceType>()) { 1495 QualType PTy = RefTy->getPointeeType(); 1496 if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) 1497 Attrs.addDereferenceableAttr(getContext().getTypeSizeInChars(PTy) 1498 .getQuantity()); 1499 else if (getContext().getTargetAddressSpace(PTy) == 0) 1500 Attrs.addAttribute(llvm::Attribute::NonNull); 1501 } 1502 1503 if (Attrs.hasAttributes()) { 1504 unsigned FirstIRArg, NumIRArgs; 1505 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); 1506 for (unsigned i = 0; i < NumIRArgs; i++) 1507 PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), 1508 FirstIRArg + i + 1, Attrs)); 1509 } 1510 } 1511 assert(ArgNo == FI.arg_size()); 1512 1513 if (FuncAttrs.hasAttributes()) 1514 PAL.push_back(llvm:: 1515 AttributeSet::get(getLLVMContext(), 1516 llvm::AttributeSet::FunctionIndex, 1517 FuncAttrs)); 1518 } 1519 1520 /// An argument came in as a promoted argument; demote it back to its 1521 /// declared type. 1522 static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, 1523 const VarDecl *var, 1524 llvm::Value *value) { 1525 llvm::Type *varType = CGF.ConvertType(var->getType()); 1526 1527 // This can happen with promotions that actually don't change the 1528 // underlying type, like the enum promotions. 1529 if (value->getType() == varType) return value; 1530 1531 assert((varType->isIntegerTy() || varType->isFloatingPointTy()) 1532 && "unexpected promotion type"); 1533 1534 if (isa<llvm::IntegerType>(varType)) 1535 return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); 1536 1537 return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); 1538 } 1539 1540 /// Returns the attribute (either parameter attribute, or function 1541 /// attribute), which declares argument ArgNo to be non-null. 1542 static const NonNullAttr *getNonNullAttr(const Decl *FD, const ParmVarDecl *PVD, 1543 QualType ArgType, unsigned ArgNo) { 1544 // FIXME: __attribute__((nonnull)) can also be applied to: 1545 // - references to pointers, where the pointee is known to be 1546 // nonnull (apparently a Clang extension) 1547 // - transparent unions containing pointers 1548 // In the former case, LLVM IR cannot represent the constraint. In 1549 // the latter case, we have no guarantee that the transparent union 1550 // is in fact passed as a pointer. 1551 if (!ArgType->isAnyPointerType() && !ArgType->isBlockPointerType()) 1552 return nullptr; 1553 // First, check attribute on parameter itself. 1554 if (PVD) { 1555 if (auto ParmNNAttr = PVD->getAttr<NonNullAttr>()) 1556 return ParmNNAttr; 1557 } 1558 // Check function attributes. 1559 if (!FD) 1560 return nullptr; 1561 for (const auto *NNAttr : FD->specific_attrs<NonNullAttr>()) { 1562 if (NNAttr->isNonNull(ArgNo)) 1563 return NNAttr; 1564 } 1565 return nullptr; 1566 } 1567 1568 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, 1569 llvm::Function *Fn, 1570 const FunctionArgList &Args) { 1571 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) 1572 // Naked functions don't have prologues. 1573 return; 1574 1575 // If this is an implicit-return-zero function, go ahead and 1576 // initialize the return value. TODO: it might be nice to have 1577 // a more general mechanism for this that didn't require synthesized 1578 // return statements. 1579 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) { 1580 if (FD->hasImplicitReturnZero()) { 1581 QualType RetTy = FD->getReturnType().getUnqualifiedType(); 1582 llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); 1583 llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); 1584 Builder.CreateStore(Zero, ReturnValue); 1585 } 1586 } 1587 1588 // FIXME: We no longer need the types from FunctionArgList; lift up and 1589 // simplify. 1590 1591 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), FI); 1592 // Flattened function arguments. 1593 SmallVector<llvm::Argument *, 16> FnArgs; 1594 FnArgs.reserve(IRFunctionArgs.totalIRArgs()); 1595 for (auto &Arg : Fn->args()) { 1596 FnArgs.push_back(&Arg); 1597 } 1598 assert(FnArgs.size() == IRFunctionArgs.totalIRArgs()); 1599 1600 // If we're using inalloca, all the memory arguments are GEPs off of the last 1601 // parameter, which is a pointer to the complete memory area. 1602 llvm::Value *ArgStruct = nullptr; 1603 if (IRFunctionArgs.hasInallocaArg()) { 1604 ArgStruct = FnArgs[IRFunctionArgs.getInallocaArgNo()]; 1605 assert(ArgStruct->getType() == FI.getArgStruct()->getPointerTo()); 1606 } 1607 1608 // Name the struct return parameter. 1609 if (IRFunctionArgs.hasSRetArg()) { 1610 auto AI = FnArgs[IRFunctionArgs.getSRetArgNo()]; 1611 AI->setName("agg.result"); 1612 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), AI->getArgNo() + 1, 1613 llvm::Attribute::NoAlias)); 1614 } 1615 1616 // Track if we received the parameter as a pointer (indirect, byval, or 1617 // inalloca). If already have a pointer, EmitParmDecl doesn't need to copy it 1618 // into a local alloca for us. 1619 enum ValOrPointer { HaveValue = 0, HavePointer = 1 }; 1620 typedef llvm::PointerIntPair<llvm::Value *, 1> ValueAndIsPtr; 1621 SmallVector<ValueAndIsPtr, 16> ArgVals; 1622 ArgVals.reserve(Args.size()); 1623 1624 // Create a pointer value for every parameter declaration. This usually 1625 // entails copying one or more LLVM IR arguments into an alloca. Don't push 1626 // any cleanups or do anything that might unwind. We do that separately, so 1627 // we can push the cleanups in the correct order for the ABI. 1628 assert(FI.arg_size() == Args.size() && 1629 "Mismatch between function signature & arguments."); 1630 unsigned ArgNo = 0; 1631 CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); 1632 for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 1633 i != e; ++i, ++info_it, ++ArgNo) { 1634 const VarDecl *Arg = *i; 1635 QualType Ty = info_it->type; 1636 const ABIArgInfo &ArgI = info_it->info; 1637 1638 bool isPromoted = 1639 isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); 1640 1641 unsigned FirstIRArg, NumIRArgs; 1642 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); 1643 1644 switch (ArgI.getKind()) { 1645 case ABIArgInfo::InAlloca: { 1646 assert(NumIRArgs == 0); 1647 llvm::Value *V = Builder.CreateStructGEP( 1648 ArgStruct, ArgI.getInAllocaFieldIndex(), Arg->getName()); 1649 ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); 1650 break; 1651 } 1652 1653 case ABIArgInfo::Indirect: { 1654 assert(NumIRArgs == 1); 1655 llvm::Value *V = FnArgs[FirstIRArg]; 1656 1657 if (!hasScalarEvaluationKind(Ty)) { 1658 // Aggregates and complex variables are accessed by reference. All we 1659 // need to do is realign the value, if requested 1660 if (ArgI.getIndirectRealign()) { 1661 llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); 1662 1663 // Copy from the incoming argument pointer to the temporary with the 1664 // appropriate alignment. 1665 // 1666 // FIXME: We should have a common utility for generating an aggregate 1667 // copy. 1668 llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); 1669 CharUnits Size = getContext().getTypeSizeInChars(Ty); 1670 llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); 1671 llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); 1672 Builder.CreateMemCpy(Dst, 1673 Src, 1674 llvm::ConstantInt::get(IntPtrTy, 1675 Size.getQuantity()), 1676 ArgI.getIndirectAlign(), 1677 false); 1678 V = AlignedTemp; 1679 } 1680 ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); 1681 } else { 1682 // Load scalar value from indirect argument. 1683 CharUnits Alignment = getContext().getTypeAlignInChars(Ty); 1684 V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty, 1685 Arg->getLocStart()); 1686 1687 if (isPromoted) 1688 V = emitArgumentDemotion(*this, Arg, V); 1689 ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); 1690 } 1691 break; 1692 } 1693 1694 case ABIArgInfo::Extend: 1695 case ABIArgInfo::Direct: { 1696 1697 // If we have the trivial case, handle it with no muss and fuss. 1698 if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && 1699 ArgI.getCoerceToType() == ConvertType(Ty) && 1700 ArgI.getDirectOffset() == 0) { 1701 assert(NumIRArgs == 1); 1702 auto AI = FnArgs[FirstIRArg]; 1703 llvm::Value *V = AI; 1704 1705 if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) { 1706 if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(), 1707 PVD->getFunctionScopeIndex())) 1708 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), 1709 AI->getArgNo() + 1, 1710 llvm::Attribute::NonNull)); 1711 1712 QualType OTy = PVD->getOriginalType(); 1713 if (const auto *ArrTy = 1714 getContext().getAsConstantArrayType(OTy)) { 1715 // A C99 array parameter declaration with the static keyword also 1716 // indicates dereferenceability, and if the size is constant we can 1717 // use the dereferenceable attribute (which requires the size in 1718 // bytes). 1719 if (ArrTy->getSizeModifier() == ArrayType::Static) { 1720 QualType ETy = ArrTy->getElementType(); 1721 uint64_t ArrSize = ArrTy->getSize().getZExtValue(); 1722 if (!ETy->isIncompleteType() && ETy->isConstantSizeType() && 1723 ArrSize) { 1724 llvm::AttrBuilder Attrs; 1725 Attrs.addDereferenceableAttr( 1726 getContext().getTypeSizeInChars(ETy).getQuantity()*ArrSize); 1727 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), 1728 AI->getArgNo() + 1, Attrs)); 1729 } else if (getContext().getTargetAddressSpace(ETy) == 0) { 1730 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), 1731 AI->getArgNo() + 1, 1732 llvm::Attribute::NonNull)); 1733 } 1734 } 1735 } else if (const auto *ArrTy = 1736 getContext().getAsVariableArrayType(OTy)) { 1737 // For C99 VLAs with the static keyword, we don't know the size so 1738 // we can't use the dereferenceable attribute, but in addrspace(0) 1739 // we know that it must be nonnull. 1740 if (ArrTy->getSizeModifier() == VariableArrayType::Static && 1741 !getContext().getTargetAddressSpace(ArrTy->getElementType())) 1742 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), 1743 AI->getArgNo() + 1, 1744 llvm::Attribute::NonNull)); 1745 } 1746 } 1747 1748 if (Arg->getType().isRestrictQualified()) 1749 AI->addAttr(llvm::AttributeSet::get(getLLVMContext(), 1750 AI->getArgNo() + 1, 1751 llvm::Attribute::NoAlias)); 1752 1753 // Ensure the argument is the correct type. 1754 if (V->getType() != ArgI.getCoerceToType()) 1755 V = Builder.CreateBitCast(V, ArgI.getCoerceToType()); 1756 1757 if (isPromoted) 1758 V = emitArgumentDemotion(*this, Arg, V); 1759 1760 if (const CXXMethodDecl *MD = 1761 dyn_cast_or_null<CXXMethodDecl>(CurCodeDecl)) { 1762 if (MD->isVirtual() && Arg == CXXABIThisDecl) 1763 V = CGM.getCXXABI(). 1764 adjustThisParameterInVirtualFunctionPrologue(*this, CurGD, V); 1765 } 1766 1767 // Because of merging of function types from multiple decls it is 1768 // possible for the type of an argument to not match the corresponding 1769 // type in the function type. Since we are codegening the callee 1770 // in here, add a cast to the argument type. 1771 llvm::Type *LTy = ConvertType(Arg->getType()); 1772 if (V->getType() != LTy) 1773 V = Builder.CreateBitCast(V, LTy); 1774 1775 ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); 1776 break; 1777 } 1778 1779 llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName()); 1780 1781 // The alignment we need to use is the max of the requested alignment for 1782 // the argument plus the alignment required by our access code below. 1783 unsigned AlignmentToUse = 1784 CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType()); 1785 AlignmentToUse = std::max(AlignmentToUse, 1786 (unsigned)getContext().getDeclAlign(Arg).getQuantity()); 1787 1788 Alloca->setAlignment(AlignmentToUse); 1789 llvm::Value *V = Alloca; 1790 llvm::Value *Ptr = V; // Pointer to store into. 1791 1792 // If the value is offset in memory, apply the offset now. 1793 if (unsigned Offs = ArgI.getDirectOffset()) { 1794 Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); 1795 Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); 1796 Ptr = Builder.CreateBitCast(Ptr, 1797 llvm::PointerType::getUnqual(ArgI.getCoerceToType())); 1798 } 1799 1800 // Fast-isel and the optimizer generally like scalar values better than 1801 // FCAs, so we flatten them if this is safe to do for this argument. 1802 llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType()); 1803 if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy && 1804 STy->getNumElements() > 1) { 1805 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy); 1806 llvm::Type *DstTy = 1807 cast<llvm::PointerType>(Ptr->getType())->getElementType(); 1808 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy); 1809 1810 if (SrcSize <= DstSize) { 1811 Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); 1812 1813 assert(STy->getNumElements() == NumIRArgs); 1814 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1815 auto AI = FnArgs[FirstIRArg + i]; 1816 AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1817 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); 1818 Builder.CreateStore(AI, EltPtr); 1819 } 1820 } else { 1821 llvm::AllocaInst *TempAlloca = 1822 CreateTempAlloca(ArgI.getCoerceToType(), "coerce"); 1823 TempAlloca->setAlignment(AlignmentToUse); 1824 llvm::Value *TempV = TempAlloca; 1825 1826 assert(STy->getNumElements() == NumIRArgs); 1827 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1828 auto AI = FnArgs[FirstIRArg + i]; 1829 AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1830 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i); 1831 Builder.CreateStore(AI, EltPtr); 1832 } 1833 1834 Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse); 1835 } 1836 } else { 1837 // Simple case, just do a coerced store of the argument into the alloca. 1838 assert(NumIRArgs == 1); 1839 auto AI = FnArgs[FirstIRArg]; 1840 AI->setName(Arg->getName() + ".coerce"); 1841 CreateCoercedStore(AI, Ptr, /*DestIsVolatile=*/false, *this); 1842 } 1843 1844 1845 // Match to what EmitParmDecl is expecting for this type. 1846 if (CodeGenFunction::hasScalarEvaluationKind(Ty)) { 1847 V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty, Arg->getLocStart()); 1848 if (isPromoted) 1849 V = emitArgumentDemotion(*this, Arg, V); 1850 ArgVals.push_back(ValueAndIsPtr(V, HaveValue)); 1851 } else { 1852 ArgVals.push_back(ValueAndIsPtr(V, HavePointer)); 1853 } 1854 break; 1855 } 1856 1857 case ABIArgInfo::Expand: { 1858 // If this structure was expanded into multiple arguments then 1859 // we need to create a temporary and reconstruct it from the 1860 // arguments. 1861 llvm::AllocaInst *Alloca = CreateMemTemp(Ty); 1862 CharUnits Align = getContext().getDeclAlign(Arg); 1863 Alloca->setAlignment(Align.getQuantity()); 1864 LValue LV = MakeAddrLValue(Alloca, Ty, Align); 1865 ArgVals.push_back(ValueAndIsPtr(Alloca, HavePointer)); 1866 1867 auto FnArgIter = FnArgs.begin() + FirstIRArg; 1868 ExpandTypeFromArgs(Ty, LV, FnArgIter); 1869 assert(FnArgIter == FnArgs.begin() + FirstIRArg + NumIRArgs); 1870 for (unsigned i = 0, e = NumIRArgs; i != e; ++i) { 1871 auto AI = FnArgs[FirstIRArg + i]; 1872 AI->setName(Arg->getName() + "." + Twine(i)); 1873 } 1874 break; 1875 } 1876 1877 case ABIArgInfo::Ignore: 1878 assert(NumIRArgs == 0); 1879 // Initialize the local variable appropriately. 1880 if (!hasScalarEvaluationKind(Ty)) { 1881 ArgVals.push_back(ValueAndIsPtr(CreateMemTemp(Ty), HavePointer)); 1882 } else { 1883 llvm::Value *U = llvm::UndefValue::get(ConvertType(Arg->getType())); 1884 ArgVals.push_back(ValueAndIsPtr(U, HaveValue)); 1885 } 1886 break; 1887 } 1888 } 1889 1890 if (getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { 1891 for (int I = Args.size() - 1; I >= 0; --I) 1892 EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(), 1893 I + 1); 1894 } else { 1895 for (unsigned I = 0, E = Args.size(); I != E; ++I) 1896 EmitParmDecl(*Args[I], ArgVals[I].getPointer(), ArgVals[I].getInt(), 1897 I + 1); 1898 } 1899 } 1900 1901 static void eraseUnusedBitCasts(llvm::Instruction *insn) { 1902 while (insn->use_empty()) { 1903 llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn); 1904 if (!bitcast) return; 1905 1906 // This is "safe" because we would have used a ConstantExpr otherwise. 1907 insn = cast<llvm::Instruction>(bitcast->getOperand(0)); 1908 bitcast->eraseFromParent(); 1909 } 1910 } 1911 1912 /// Try to emit a fused autorelease of a return result. 1913 static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, 1914 llvm::Value *result) { 1915 // We must be immediately followed the cast. 1916 llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); 1917 if (BB->empty()) return nullptr; 1918 if (&BB->back() != result) return nullptr; 1919 1920 llvm::Type *resultType = result->getType(); 1921 1922 // result is in a BasicBlock and is therefore an Instruction. 1923 llvm::Instruction *generator = cast<llvm::Instruction>(result); 1924 1925 SmallVector<llvm::Instruction*,4> insnsToKill; 1926 1927 // Look for: 1928 // %generator = bitcast %type1* %generator2 to %type2* 1929 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { 1930 // We would have emitted this as a constant if the operand weren't 1931 // an Instruction. 1932 generator = cast<llvm::Instruction>(bitcast->getOperand(0)); 1933 1934 // Require the generator to be immediately followed by the cast. 1935 if (generator->getNextNode() != bitcast) 1936 return nullptr; 1937 1938 insnsToKill.push_back(bitcast); 1939 } 1940 1941 // Look for: 1942 // %generator = call i8* @objc_retain(i8* %originalResult) 1943 // or 1944 // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) 1945 llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); 1946 if (!call) return nullptr; 1947 1948 bool doRetainAutorelease; 1949 1950 if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) { 1951 doRetainAutorelease = true; 1952 } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints() 1953 .objc_retainAutoreleasedReturnValue) { 1954 doRetainAutorelease = false; 1955 1956 // If we emitted an assembly marker for this call (and the 1957 // ARCEntrypoints field should have been set if so), go looking 1958 // for that call. If we can't find it, we can't do this 1959 // optimization. But it should always be the immediately previous 1960 // instruction, unless we needed bitcasts around the call. 1961 if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) { 1962 llvm::Instruction *prev = call->getPrevNode(); 1963 assert(prev); 1964 if (isa<llvm::BitCastInst>(prev)) { 1965 prev = prev->getPrevNode(); 1966 assert(prev); 1967 } 1968 assert(isa<llvm::CallInst>(prev)); 1969 assert(cast<llvm::CallInst>(prev)->getCalledValue() == 1970 CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker); 1971 insnsToKill.push_back(prev); 1972 } 1973 } else { 1974 return nullptr; 1975 } 1976 1977 result = call->getArgOperand(0); 1978 insnsToKill.push_back(call); 1979 1980 // Keep killing bitcasts, for sanity. Note that we no longer care 1981 // about precise ordering as long as there's exactly one use. 1982 while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { 1983 if (!bitcast->hasOneUse()) break; 1984 insnsToKill.push_back(bitcast); 1985 result = bitcast->getOperand(0); 1986 } 1987 1988 // Delete all the unnecessary instructions, from latest to earliest. 1989 for (SmallVectorImpl<llvm::Instruction*>::iterator 1990 i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i) 1991 (*i)->eraseFromParent(); 1992 1993 // Do the fused retain/autorelease if we were asked to. 1994 if (doRetainAutorelease) 1995 result = CGF.EmitARCRetainAutoreleaseReturnValue(result); 1996 1997 // Cast back to the result type. 1998 return CGF.Builder.CreateBitCast(result, resultType); 1999 } 2000 2001 /// If this is a +1 of the value of an immutable 'self', remove it. 2002 static llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF, 2003 llvm::Value *result) { 2004 // This is only applicable to a method with an immutable 'self'. 2005 const ObjCMethodDecl *method = 2006 dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl); 2007 if (!method) return nullptr; 2008 const VarDecl *self = method->getSelfDecl(); 2009 if (!self->getType().isConstQualified()) return nullptr; 2010 2011 // Look for a retain call. 2012 llvm::CallInst *retainCall = 2013 dyn_cast<llvm::CallInst>(result->stripPointerCasts()); 2014 if (!retainCall || 2015 retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain) 2016 return nullptr; 2017 2018 // Look for an ordinary load of 'self'. 2019 llvm::Value *retainedValue = retainCall->getArgOperand(0); 2020 llvm::LoadInst *load = 2021 dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts()); 2022 if (!load || load->isAtomic() || load->isVolatile() || 2023 load->getPointerOperand() != CGF.GetAddrOfLocalVar(self)) 2024 return nullptr; 2025 2026 // Okay! Burn it all down. This relies for correctness on the 2027 // assumption that the retain is emitted as part of the return and 2028 // that thereafter everything is used "linearly". 2029 llvm::Type *resultType = result->getType(); 2030 eraseUnusedBitCasts(cast<llvm::Instruction>(result)); 2031 assert(retainCall->use_empty()); 2032 retainCall->eraseFromParent(); 2033 eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue)); 2034 2035 return CGF.Builder.CreateBitCast(load, resultType); 2036 } 2037 2038 /// Emit an ARC autorelease of the result of a function. 2039 /// 2040 /// \return the value to actually return from the function 2041 static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, 2042 llvm::Value *result) { 2043 // If we're returning 'self', kill the initial retain. This is a 2044 // heuristic attempt to "encourage correctness" in the really unfortunate 2045 // case where we have a return of self during a dealloc and we desperately 2046 // need to avoid the possible autorelease. 2047 if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result)) 2048 return self; 2049 2050 // At -O0, try to emit a fused retain/autorelease. 2051 if (CGF.shouldUseFusedARCCalls()) 2052 if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) 2053 return fused; 2054 2055 return CGF.EmitARCAutoreleaseReturnValue(result); 2056 } 2057 2058 /// Heuristically search for a dominating store to the return-value slot. 2059 static llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) { 2060 // If there are multiple uses of the return-value slot, just check 2061 // for something immediately preceding the IP. Sometimes this can 2062 // happen with how we generate implicit-returns; it can also happen 2063 // with noreturn cleanups. 2064 if (!CGF.ReturnValue->hasOneUse()) { 2065 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 2066 if (IP->empty()) return nullptr; 2067 llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back()); 2068 if (!store) return nullptr; 2069 if (store->getPointerOperand() != CGF.ReturnValue) return nullptr; 2070 assert(!store->isAtomic() && !store->isVolatile()); // see below 2071 return store; 2072 } 2073 2074 llvm::StoreInst *store = 2075 dyn_cast<llvm::StoreInst>(CGF.ReturnValue->user_back()); 2076 if (!store) return nullptr; 2077 2078 // These aren't actually possible for non-coerced returns, and we 2079 // only care about non-coerced returns on this code path. 2080 assert(!store->isAtomic() && !store->isVolatile()); 2081 2082 // Now do a first-and-dirty dominance check: just walk up the 2083 // single-predecessors chain from the current insertion point. 2084 llvm::BasicBlock *StoreBB = store->getParent(); 2085 llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 2086 while (IP != StoreBB) { 2087 if (!(IP = IP->getSinglePredecessor())) 2088 return nullptr; 2089 } 2090 2091 // Okay, the store's basic block dominates the insertion point; we 2092 // can do our thing. 2093 return store; 2094 } 2095 2096 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, 2097 bool EmitRetDbgLoc, 2098 SourceLocation EndLoc) { 2099 if (CurCodeDecl && CurCodeDecl->hasAttr<NakedAttr>()) { 2100 // Naked functions don't have epilogues. 2101 Builder.CreateUnreachable(); 2102 return; 2103 } 2104 2105 // Functions with no result always return void. 2106 if (!ReturnValue) { 2107 Builder.CreateRetVoid(); 2108 return; 2109 } 2110 2111 llvm::DebugLoc RetDbgLoc; 2112 llvm::Value *RV = nullptr; 2113 QualType RetTy = FI.getReturnType(); 2114 const ABIArgInfo &RetAI = FI.getReturnInfo(); 2115 2116 switch (RetAI.getKind()) { 2117 case ABIArgInfo::InAlloca: 2118 // Aggregrates get evaluated directly into the destination. Sometimes we 2119 // need to return the sret value in a register, though. 2120 assert(hasAggregateEvaluationKind(RetTy)); 2121 if (RetAI.getInAllocaSRet()) { 2122 llvm::Function::arg_iterator EI = CurFn->arg_end(); 2123 --EI; 2124 llvm::Value *ArgStruct = EI; 2125 llvm::Value *SRet = 2126 Builder.CreateStructGEP(ArgStruct, RetAI.getInAllocaFieldIndex()); 2127 RV = Builder.CreateLoad(SRet, "sret"); 2128 } 2129 break; 2130 2131 case ABIArgInfo::Indirect: { 2132 auto AI = CurFn->arg_begin(); 2133 if (RetAI.isSRetAfterThis()) 2134 ++AI; 2135 switch (getEvaluationKind(RetTy)) { 2136 case TEK_Complex: { 2137 ComplexPairTy RT = 2138 EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy), 2139 EndLoc); 2140 EmitStoreOfComplex(RT, MakeNaturalAlignAddrLValue(AI, RetTy), 2141 /*isInit*/ true); 2142 break; 2143 } 2144 case TEK_Aggregate: 2145 // Do nothing; aggregrates get evaluated directly into the destination. 2146 break; 2147 case TEK_Scalar: 2148 EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), 2149 MakeNaturalAlignAddrLValue(AI, RetTy), 2150 /*isInit*/ true); 2151 break; 2152 } 2153 break; 2154 } 2155 2156 case ABIArgInfo::Extend: 2157 case ABIArgInfo::Direct: 2158 if (RetAI.getCoerceToType() == ConvertType(RetTy) && 2159 RetAI.getDirectOffset() == 0) { 2160 // The internal return value temp always will have pointer-to-return-type 2161 // type, just do a load. 2162 2163 // If there is a dominating store to ReturnValue, we can elide 2164 // the load, zap the store, and usually zap the alloca. 2165 if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) { 2166 // Reuse the debug location from the store unless there is 2167 // cleanup code to be emitted between the store and return 2168 // instruction. 2169 if (EmitRetDbgLoc && !AutoreleaseResult) 2170 RetDbgLoc = SI->getDebugLoc(); 2171 // Get the stored value and nuke the now-dead store. 2172 RV = SI->getValueOperand(); 2173 SI->eraseFromParent(); 2174 2175 // If that was the only use of the return value, nuke it as well now. 2176 if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { 2177 cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); 2178 ReturnValue = nullptr; 2179 } 2180 2181 // Otherwise, we have to do a simple load. 2182 } else { 2183 RV = Builder.CreateLoad(ReturnValue); 2184 } 2185 } else { 2186 llvm::Value *V = ReturnValue; 2187 // If the value is offset in memory, apply the offset now. 2188 if (unsigned Offs = RetAI.getDirectOffset()) { 2189 V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); 2190 V = Builder.CreateConstGEP1_32(V, Offs); 2191 V = Builder.CreateBitCast(V, 2192 llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 2193 } 2194 2195 RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); 2196 } 2197 2198 // In ARC, end functions that return a retainable type with a call 2199 // to objc_autoreleaseReturnValue. 2200 if (AutoreleaseResult) { 2201 assert(getLangOpts().ObjCAutoRefCount && 2202 !FI.isReturnsRetained() && 2203 RetTy->isObjCRetainableType()); 2204 RV = emitAutoreleaseOfResult(*this, RV); 2205 } 2206 2207 break; 2208 2209 case ABIArgInfo::Ignore: 2210 break; 2211 2212 case ABIArgInfo::Expand: 2213 llvm_unreachable("Invalid ABI kind for return argument"); 2214 } 2215 2216 llvm::Instruction *Ret; 2217 if (RV) { 2218 if (SanOpts->ReturnsNonnullAttribute) { 2219 if (auto RetNNAttr = CurGD.getDecl()->getAttr<ReturnsNonNullAttr>()) { 2220 SanitizerScope SanScope(this); 2221 llvm::Value *Cond = Builder.CreateICmpNE( 2222 RV, llvm::Constant::getNullValue(RV->getType())); 2223 llvm::Constant *StaticData[] = { 2224 EmitCheckSourceLocation(EndLoc), 2225 EmitCheckSourceLocation(RetNNAttr->getLocation()), 2226 }; 2227 EmitCheck(Cond, "nonnull_return", StaticData, None, CRK_Recoverable); 2228 } 2229 } 2230 Ret = Builder.CreateRet(RV); 2231 } else { 2232 Ret = Builder.CreateRetVoid(); 2233 } 2234 2235 if (!RetDbgLoc.isUnknown()) 2236 Ret->setDebugLoc(RetDbgLoc); 2237 } 2238 2239 static bool isInAllocaArgument(CGCXXABI &ABI, QualType type) { 2240 const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); 2241 return RD && ABI.getRecordArgABI(RD) == CGCXXABI::RAA_DirectInMemory; 2242 } 2243 2244 static AggValueSlot createPlaceholderSlot(CodeGenFunction &CGF, QualType Ty) { 2245 // FIXME: Generate IR in one pass, rather than going back and fixing up these 2246 // placeholders. 2247 llvm::Type *IRTy = CGF.ConvertTypeForMem(Ty); 2248 llvm::Value *Placeholder = 2249 llvm::UndefValue::get(IRTy->getPointerTo()->getPointerTo()); 2250 Placeholder = CGF.Builder.CreateLoad(Placeholder); 2251 return AggValueSlot::forAddr(Placeholder, CharUnits::Zero(), 2252 Ty.getQualifiers(), 2253 AggValueSlot::IsNotDestructed, 2254 AggValueSlot::DoesNotNeedGCBarriers, 2255 AggValueSlot::IsNotAliased); 2256 } 2257 2258 void CodeGenFunction::EmitDelegateCallArg(CallArgList &args, 2259 const VarDecl *param, 2260 SourceLocation loc) { 2261 // StartFunction converted the ABI-lowered parameter(s) into a 2262 // local alloca. We need to turn that into an r-value suitable 2263 // for EmitCall. 2264 llvm::Value *local = GetAddrOfLocalVar(param); 2265 2266 QualType type = param->getType(); 2267 2268 // For the most part, we just need to load the alloca, except: 2269 // 1) aggregate r-values are actually pointers to temporaries, and 2270 // 2) references to non-scalars are pointers directly to the aggregate. 2271 // I don't know why references to scalars are different here. 2272 if (const ReferenceType *ref = type->getAs<ReferenceType>()) { 2273 if (!hasScalarEvaluationKind(ref->getPointeeType())) 2274 return args.add(RValue::getAggregate(local), type); 2275 2276 // Locals which are references to scalars are represented 2277 // with allocas holding the pointer. 2278 return args.add(RValue::get(Builder.CreateLoad(local)), type); 2279 } 2280 2281 assert(!isInAllocaArgument(CGM.getCXXABI(), type) && 2282 "cannot emit delegate call arguments for inalloca arguments!"); 2283 2284 args.add(convertTempToRValue(local, type, loc), type); 2285 } 2286 2287 static bool isProvablyNull(llvm::Value *addr) { 2288 return isa<llvm::ConstantPointerNull>(addr); 2289 } 2290 2291 static bool isProvablyNonNull(llvm::Value *addr) { 2292 return isa<llvm::AllocaInst>(addr); 2293 } 2294 2295 /// Emit the actual writing-back of a writeback. 2296 static void emitWriteback(CodeGenFunction &CGF, 2297 const CallArgList::Writeback &writeback) { 2298 const LValue &srcLV = writeback.Source; 2299 llvm::Value *srcAddr = srcLV.getAddress(); 2300 assert(!isProvablyNull(srcAddr) && 2301 "shouldn't have writeback for provably null argument"); 2302 2303 llvm::BasicBlock *contBB = nullptr; 2304 2305 // If the argument wasn't provably non-null, we need to null check 2306 // before doing the store. 2307 bool provablyNonNull = isProvablyNonNull(srcAddr); 2308 if (!provablyNonNull) { 2309 llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); 2310 contBB = CGF.createBasicBlock("icr.done"); 2311 2312 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 2313 CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); 2314 CGF.EmitBlock(writebackBB); 2315 } 2316 2317 // Load the value to writeback. 2318 llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); 2319 2320 // Cast it back, in case we're writing an id to a Foo* or something. 2321 value = CGF.Builder.CreateBitCast(value, 2322 cast<llvm::PointerType>(srcAddr->getType())->getElementType(), 2323 "icr.writeback-cast"); 2324 2325 // Perform the writeback. 2326 2327 // If we have a "to use" value, it's something we need to emit a use 2328 // of. This has to be carefully threaded in: if it's done after the 2329 // release it's potentially undefined behavior (and the optimizer 2330 // will ignore it), and if it happens before the retain then the 2331 // optimizer could move the release there. 2332 if (writeback.ToUse) { 2333 assert(srcLV.getObjCLifetime() == Qualifiers::OCL_Strong); 2334 2335 // Retain the new value. No need to block-copy here: the block's 2336 // being passed up the stack. 2337 value = CGF.EmitARCRetainNonBlock(value); 2338 2339 // Emit the intrinsic use here. 2340 CGF.EmitARCIntrinsicUse(writeback.ToUse); 2341 2342 // Load the old value (primitively). 2343 llvm::Value *oldValue = CGF.EmitLoadOfScalar(srcLV, SourceLocation()); 2344 2345 // Put the new value in place (primitively). 2346 CGF.EmitStoreOfScalar(value, srcLV, /*init*/ false); 2347 2348 // Release the old value. 2349 CGF.EmitARCRelease(oldValue, srcLV.isARCPreciseLifetime()); 2350 2351 // Otherwise, we can just do a normal lvalue store. 2352 } else { 2353 CGF.EmitStoreThroughLValue(RValue::get(value), srcLV); 2354 } 2355 2356 // Jump to the continuation block. 2357 if (!provablyNonNull) 2358 CGF.EmitBlock(contBB); 2359 } 2360 2361 static void emitWritebacks(CodeGenFunction &CGF, 2362 const CallArgList &args) { 2363 for (const auto &I : args.writebacks()) 2364 emitWriteback(CGF, I); 2365 } 2366 2367 static void deactivateArgCleanupsBeforeCall(CodeGenFunction &CGF, 2368 const CallArgList &CallArgs) { 2369 assert(CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()); 2370 ArrayRef<CallArgList::CallArgCleanup> Cleanups = 2371 CallArgs.getCleanupsToDeactivate(); 2372 // Iterate in reverse to increase the likelihood of popping the cleanup. 2373 for (ArrayRef<CallArgList::CallArgCleanup>::reverse_iterator 2374 I = Cleanups.rbegin(), E = Cleanups.rend(); I != E; ++I) { 2375 CGF.DeactivateCleanupBlock(I->Cleanup, I->IsActiveIP); 2376 I->IsActiveIP->eraseFromParent(); 2377 } 2378 } 2379 2380 static const Expr *maybeGetUnaryAddrOfOperand(const Expr *E) { 2381 if (const UnaryOperator *uop = dyn_cast<UnaryOperator>(E->IgnoreParens())) 2382 if (uop->getOpcode() == UO_AddrOf) 2383 return uop->getSubExpr(); 2384 return nullptr; 2385 } 2386 2387 /// Emit an argument that's being passed call-by-writeback. That is, 2388 /// we are passing the address of 2389 static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, 2390 const ObjCIndirectCopyRestoreExpr *CRE) { 2391 LValue srcLV; 2392 2393 // Make an optimistic effort to emit the address as an l-value. 2394 // This can fail if the the argument expression is more complicated. 2395 if (const Expr *lvExpr = maybeGetUnaryAddrOfOperand(CRE->getSubExpr())) { 2396 srcLV = CGF.EmitLValue(lvExpr); 2397 2398 // Otherwise, just emit it as a scalar. 2399 } else { 2400 llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr()); 2401 2402 QualType srcAddrType = 2403 CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); 2404 srcLV = CGF.MakeNaturalAlignAddrLValue(srcAddr, srcAddrType); 2405 } 2406 llvm::Value *srcAddr = srcLV.getAddress(); 2407 2408 // The dest and src types don't necessarily match in LLVM terms 2409 // because of the crazy ObjC compatibility rules. 2410 2411 llvm::PointerType *destType = 2412 cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); 2413 2414 // If the address is a constant null, just pass the appropriate null. 2415 if (isProvablyNull(srcAddr)) { 2416 args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), 2417 CRE->getType()); 2418 return; 2419 } 2420 2421 // Create the temporary. 2422 llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(), 2423 "icr.temp"); 2424 // Loading an l-value can introduce a cleanup if the l-value is __weak, 2425 // and that cleanup will be conditional if we can't prove that the l-value 2426 // isn't null, so we need to register a dominating point so that the cleanups 2427 // system will make valid IR. 2428 CodeGenFunction::ConditionalEvaluation condEval(CGF); 2429 2430 // Zero-initialize it if we're not doing a copy-initialization. 2431 bool shouldCopy = CRE->shouldCopy(); 2432 if (!shouldCopy) { 2433 llvm::Value *null = 2434 llvm::ConstantPointerNull::get( 2435 cast<llvm::PointerType>(destType->getElementType())); 2436 CGF.Builder.CreateStore(null, temp); 2437 } 2438 2439 llvm::BasicBlock *contBB = nullptr; 2440 llvm::BasicBlock *originBB = nullptr; 2441 2442 // If the address is *not* known to be non-null, we need to switch. 2443 llvm::Value *finalArgument; 2444 2445 bool provablyNonNull = isProvablyNonNull(srcAddr); 2446 if (provablyNonNull) { 2447 finalArgument = temp; 2448 } else { 2449 llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 2450 2451 finalArgument = CGF.Builder.CreateSelect(isNull, 2452 llvm::ConstantPointerNull::get(destType), 2453 temp, "icr.argument"); 2454 2455 // If we need to copy, then the load has to be conditional, which 2456 // means we need control flow. 2457 if (shouldCopy) { 2458 originBB = CGF.Builder.GetInsertBlock(); 2459 contBB = CGF.createBasicBlock("icr.cont"); 2460 llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); 2461 CGF.Builder.CreateCondBr(isNull, contBB, copyBB); 2462 CGF.EmitBlock(copyBB); 2463 condEval.begin(CGF); 2464 } 2465 } 2466 2467 llvm::Value *valueToUse = nullptr; 2468 2469 // Perform a copy if necessary. 2470 if (shouldCopy) { 2471 RValue srcRV = CGF.EmitLoadOfLValue(srcLV, SourceLocation()); 2472 assert(srcRV.isScalar()); 2473 2474 llvm::Value *src = srcRV.getScalarVal(); 2475 src = CGF.Builder.CreateBitCast(src, destType->getElementType(), 2476 "icr.cast"); 2477 2478 // Use an ordinary store, not a store-to-lvalue. 2479 CGF.Builder.CreateStore(src, temp); 2480 2481 // If optimization is enabled, and the value was held in a 2482 // __strong variable, we need to tell the optimizer that this 2483 // value has to stay alive until we're doing the store back. 2484 // This is because the temporary is effectively unretained, 2485 // and so otherwise we can violate the high-level semantics. 2486 if (CGF.CGM.getCodeGenOpts().OptimizationLevel != 0 && 2487 srcLV.getObjCLifetime() == Qualifiers::OCL_Strong) { 2488 valueToUse = src; 2489 } 2490 } 2491 2492 // Finish the control flow if we needed it. 2493 if (shouldCopy && !provablyNonNull) { 2494 llvm::BasicBlock *copyBB = CGF.Builder.GetInsertBlock(); 2495 CGF.EmitBlock(contBB); 2496 2497 // Make a phi for the value to intrinsically use. 2498 if (valueToUse) { 2499 llvm::PHINode *phiToUse = CGF.Builder.CreatePHI(valueToUse->getType(), 2, 2500 "icr.to-use"); 2501 phiToUse->addIncoming(valueToUse, copyBB); 2502 phiToUse->addIncoming(llvm::UndefValue::get(valueToUse->getType()), 2503 originBB); 2504 valueToUse = phiToUse; 2505 } 2506 2507 condEval.end(CGF); 2508 } 2509 2510 args.addWriteback(srcLV, temp, valueToUse); 2511 args.add(RValue::get(finalArgument), CRE->getType()); 2512 } 2513 2514 void CallArgList::allocateArgumentMemory(CodeGenFunction &CGF) { 2515 assert(!StackBase && !StackCleanup.isValid()); 2516 2517 // Save the stack. 2518 llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stacksave); 2519 StackBase = CGF.Builder.CreateCall(F, "inalloca.save"); 2520 2521 // Control gets really tied up in landing pads, so we have to spill the 2522 // stacksave to an alloca to avoid violating SSA form. 2523 // TODO: This is dead if we never emit the cleanup. We should create the 2524 // alloca and store lazily on the first cleanup emission. 2525 StackBaseMem = CGF.CreateTempAlloca(CGF.Int8PtrTy, "inalloca.spmem"); 2526 CGF.Builder.CreateStore(StackBase, StackBaseMem); 2527 CGF.pushStackRestore(EHCleanup, StackBaseMem); 2528 StackCleanup = CGF.EHStack.getInnermostEHScope(); 2529 assert(StackCleanup.isValid()); 2530 } 2531 2532 void CallArgList::freeArgumentMemory(CodeGenFunction &CGF) const { 2533 if (StackBase) { 2534 CGF.DeactivateCleanupBlock(StackCleanup, StackBase); 2535 llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore); 2536 // We could load StackBase from StackBaseMem, but in the non-exceptional 2537 // case we can skip it. 2538 CGF.Builder.CreateCall(F, StackBase); 2539 } 2540 } 2541 2542 static void emitNonNullArgCheck(CodeGenFunction &CGF, RValue RV, 2543 QualType ArgType, SourceLocation ArgLoc, 2544 const FunctionDecl *FD, unsigned ParmNum) { 2545 if (!CGF.SanOpts->NonnullAttribute || !FD) 2546 return; 2547 auto PVD = ParmNum < FD->getNumParams() ? FD->getParamDecl(ParmNum) : nullptr; 2548 unsigned ArgNo = PVD ? PVD->getFunctionScopeIndex() : ParmNum; 2549 auto NNAttr = getNonNullAttr(FD, PVD, ArgType, ArgNo); 2550 if (!NNAttr) 2551 return; 2552 CodeGenFunction::SanitizerScope SanScope(&CGF); 2553 assert(RV.isScalar()); 2554 llvm::Value *V = RV.getScalarVal(); 2555 llvm::Value *Cond = 2556 CGF.Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType())); 2557 llvm::Constant *StaticData[] = { 2558 CGF.EmitCheckSourceLocation(ArgLoc), 2559 CGF.EmitCheckSourceLocation(NNAttr->getLocation()), 2560 llvm::ConstantInt::get(CGF.Int32Ty, ArgNo + 1), 2561 }; 2562 CGF.EmitCheck(Cond, "nonnull_arg", StaticData, None, 2563 CodeGenFunction::CRK_Recoverable); 2564 } 2565 2566 void CodeGenFunction::EmitCallArgs(CallArgList &Args, 2567 ArrayRef<QualType> ArgTypes, 2568 CallExpr::const_arg_iterator ArgBeg, 2569 CallExpr::const_arg_iterator ArgEnd, 2570 const FunctionDecl *CalleeDecl, 2571 unsigned ParamsToSkip, 2572 bool ForceColumnInfo) { 2573 CGDebugInfo *DI = getDebugInfo(); 2574 SourceLocation CallLoc; 2575 if (DI) CallLoc = DI->getLocation(); 2576 2577 // We *have* to evaluate arguments from right to left in the MS C++ ABI, 2578 // because arguments are destroyed left to right in the callee. 2579 if (CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { 2580 // Insert a stack save if we're going to need any inalloca args. 2581 bool HasInAllocaArgs = false; 2582 for (ArrayRef<QualType>::iterator I = ArgTypes.begin(), E = ArgTypes.end(); 2583 I != E && !HasInAllocaArgs; ++I) 2584 HasInAllocaArgs = isInAllocaArgument(CGM.getCXXABI(), *I); 2585 if (HasInAllocaArgs) { 2586 assert(getTarget().getTriple().getArch() == llvm::Triple::x86); 2587 Args.allocateArgumentMemory(*this); 2588 } 2589 2590 // Evaluate each argument. 2591 size_t CallArgsStart = Args.size(); 2592 for (int I = ArgTypes.size() - 1; I >= 0; --I) { 2593 CallExpr::const_arg_iterator Arg = ArgBeg + I; 2594 EmitCallArg(Args, *Arg, ArgTypes[I]); 2595 emitNonNullArgCheck(*this, Args.back().RV, ArgTypes[I], Arg->getExprLoc(), 2596 CalleeDecl, ParamsToSkip + I); 2597 // Restore the debug location. 2598 if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo); 2599 } 2600 2601 // Un-reverse the arguments we just evaluated so they match up with the LLVM 2602 // IR function. 2603 std::reverse(Args.begin() + CallArgsStart, Args.end()); 2604 return; 2605 } 2606 2607 for (unsigned I = 0, E = ArgTypes.size(); I != E; ++I) { 2608 CallExpr::const_arg_iterator Arg = ArgBeg + I; 2609 assert(Arg != ArgEnd); 2610 EmitCallArg(Args, *Arg, ArgTypes[I]); 2611 emitNonNullArgCheck(*this, Args.back().RV, ArgTypes[I], Arg->getExprLoc(), 2612 CalleeDecl, ParamsToSkip + I); 2613 // Restore the debug location. 2614 if (DI) DI->EmitLocation(Builder, CallLoc, ForceColumnInfo); 2615 } 2616 } 2617 2618 namespace { 2619 2620 struct DestroyUnpassedArg : EHScopeStack::Cleanup { 2621 DestroyUnpassedArg(llvm::Value *Addr, QualType Ty) 2622 : Addr(Addr), Ty(Ty) {} 2623 2624 llvm::Value *Addr; 2625 QualType Ty; 2626 2627 void Emit(CodeGenFunction &CGF, Flags flags) override { 2628 const CXXDestructorDecl *Dtor = Ty->getAsCXXRecordDecl()->getDestructor(); 2629 assert(!Dtor->isTrivial()); 2630 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*for vbase*/ false, 2631 /*Delegating=*/false, Addr); 2632 } 2633 }; 2634 2635 } 2636 2637 void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, 2638 QualType type) { 2639 if (const ObjCIndirectCopyRestoreExpr *CRE 2640 = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { 2641 assert(getLangOpts().ObjCAutoRefCount); 2642 assert(getContext().hasSameType(E->getType(), type)); 2643 return emitWritebackArg(*this, args, CRE); 2644 } 2645 2646 assert(type->isReferenceType() == E->isGLValue() && 2647 "reference binding to unmaterialized r-value!"); 2648 2649 if (E->isGLValue()) { 2650 assert(E->getObjectKind() == OK_Ordinary); 2651 return args.add(EmitReferenceBindingToExpr(E), type); 2652 } 2653 2654 bool HasAggregateEvalKind = hasAggregateEvaluationKind(type); 2655 2656 // In the Microsoft C++ ABI, aggregate arguments are destructed by the callee. 2657 // However, we still have to push an EH-only cleanup in case we unwind before 2658 // we make it to the call. 2659 if (HasAggregateEvalKind && 2660 CGM.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { 2661 // If we're using inalloca, use the argument memory. Otherwise, use a 2662 // temporary. 2663 AggValueSlot Slot; 2664 if (args.isUsingInAlloca()) 2665 Slot = createPlaceholderSlot(*this, type); 2666 else 2667 Slot = CreateAggTemp(type, "agg.tmp"); 2668 2669 const CXXRecordDecl *RD = type->getAsCXXRecordDecl(); 2670 bool DestroyedInCallee = 2671 RD && RD->hasNonTrivialDestructor() && 2672 CGM.getCXXABI().getRecordArgABI(RD) != CGCXXABI::RAA_Default; 2673 if (DestroyedInCallee) 2674 Slot.setExternallyDestructed(); 2675 2676 EmitAggExpr(E, Slot); 2677 RValue RV = Slot.asRValue(); 2678 args.add(RV, type); 2679 2680 if (DestroyedInCallee) { 2681 // Create a no-op GEP between the placeholder and the cleanup so we can 2682 // RAUW it successfully. It also serves as a marker of the first 2683 // instruction where the cleanup is active. 2684 pushFullExprCleanup<DestroyUnpassedArg>(EHCleanup, Slot.getAddr(), type); 2685 // This unreachable is a temporary marker which will be removed later. 2686 llvm::Instruction *IsActive = Builder.CreateUnreachable(); 2687 args.addArgCleanupDeactivation(EHStack.getInnermostEHScope(), IsActive); 2688 } 2689 return; 2690 } 2691 2692 if (HasAggregateEvalKind && isa<ImplicitCastExpr>(E) && 2693 cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { 2694 LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); 2695 assert(L.isSimple()); 2696 if (L.getAlignment() >= getContext().getTypeAlignInChars(type)) { 2697 args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true); 2698 } else { 2699 // We can't represent a misaligned lvalue in the CallArgList, so copy 2700 // to an aligned temporary now. 2701 llvm::Value *tmp = CreateMemTemp(type); 2702 EmitAggregateCopy(tmp, L.getAddress(), type, L.isVolatile(), 2703 L.getAlignment()); 2704 args.add(RValue::getAggregate(tmp), type); 2705 } 2706 return; 2707 } 2708 2709 args.add(EmitAnyExprToTemp(E), type); 2710 } 2711 2712 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 2713 // optimizer it can aggressively ignore unwind edges. 2714 void 2715 CodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) { 2716 if (CGM.getCodeGenOpts().OptimizationLevel != 0 && 2717 !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) 2718 Inst->setMetadata("clang.arc.no_objc_arc_exceptions", 2719 CGM.getNoObjCARCExceptionsMetadata()); 2720 } 2721 2722 /// Emits a call to the given no-arguments nounwind runtime function. 2723 llvm::CallInst * 2724 CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, 2725 const llvm::Twine &name) { 2726 return EmitNounwindRuntimeCall(callee, None, name); 2727 } 2728 2729 /// Emits a call to the given nounwind runtime function. 2730 llvm::CallInst * 2731 CodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee, 2732 ArrayRef<llvm::Value*> args, 2733 const llvm::Twine &name) { 2734 llvm::CallInst *call = EmitRuntimeCall(callee, args, name); 2735 call->setDoesNotThrow(); 2736 return call; 2737 } 2738 2739 /// Emits a simple call (never an invoke) to the given no-arguments 2740 /// runtime function. 2741 llvm::CallInst * 2742 CodeGenFunction::EmitRuntimeCall(llvm::Value *callee, 2743 const llvm::Twine &name) { 2744 return EmitRuntimeCall(callee, None, name); 2745 } 2746 2747 /// Emits a simple call (never an invoke) to the given runtime 2748 /// function. 2749 llvm::CallInst * 2750 CodeGenFunction::EmitRuntimeCall(llvm::Value *callee, 2751 ArrayRef<llvm::Value*> args, 2752 const llvm::Twine &name) { 2753 llvm::CallInst *call = Builder.CreateCall(callee, args, name); 2754 call->setCallingConv(getRuntimeCC()); 2755 return call; 2756 } 2757 2758 /// Emits a call or invoke to the given noreturn runtime function. 2759 void CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee, 2760 ArrayRef<llvm::Value*> args) { 2761 if (getInvokeDest()) { 2762 llvm::InvokeInst *invoke = 2763 Builder.CreateInvoke(callee, 2764 getUnreachableBlock(), 2765 getInvokeDest(), 2766 args); 2767 invoke->setDoesNotReturn(); 2768 invoke->setCallingConv(getRuntimeCC()); 2769 } else { 2770 llvm::CallInst *call = Builder.CreateCall(callee, args); 2771 call->setDoesNotReturn(); 2772 call->setCallingConv(getRuntimeCC()); 2773 Builder.CreateUnreachable(); 2774 } 2775 PGO.setCurrentRegionUnreachable(); 2776 } 2777 2778 /// Emits a call or invoke instruction to the given nullary runtime 2779 /// function. 2780 llvm::CallSite 2781 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, 2782 const Twine &name) { 2783 return EmitRuntimeCallOrInvoke(callee, None, name); 2784 } 2785 2786 /// Emits a call or invoke instruction to the given runtime function. 2787 llvm::CallSite 2788 CodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee, 2789 ArrayRef<llvm::Value*> args, 2790 const Twine &name) { 2791 llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name); 2792 callSite.setCallingConv(getRuntimeCC()); 2793 return callSite; 2794 } 2795 2796 llvm::CallSite 2797 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 2798 const Twine &Name) { 2799 return EmitCallOrInvoke(Callee, None, Name); 2800 } 2801 2802 /// Emits a call or invoke instruction to the given function, depending 2803 /// on the current state of the EH stack. 2804 llvm::CallSite 2805 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 2806 ArrayRef<llvm::Value *> Args, 2807 const Twine &Name) { 2808 llvm::BasicBlock *InvokeDest = getInvokeDest(); 2809 2810 llvm::Instruction *Inst; 2811 if (!InvokeDest) 2812 Inst = Builder.CreateCall(Callee, Args, Name); 2813 else { 2814 llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); 2815 Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name); 2816 EmitBlock(ContBB); 2817 } 2818 2819 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 2820 // optimizer it can aggressively ignore unwind edges. 2821 if (CGM.getLangOpts().ObjCAutoRefCount) 2822 AddObjCARCExceptionMetadata(Inst); 2823 2824 return Inst; 2825 } 2826 2827 /// \brief Store a non-aggregate value to an address to initialize it. For 2828 /// initialization, a non-atomic store will be used. 2829 static void EmitInitStoreOfNonAggregate(CodeGenFunction &CGF, RValue Src, 2830 LValue Dst) { 2831 if (Src.isScalar()) 2832 CGF.EmitStoreOfScalar(Src.getScalarVal(), Dst, /*init=*/true); 2833 else 2834 CGF.EmitStoreOfComplex(Src.getComplexVal(), Dst, /*init=*/true); 2835 } 2836 2837 void CodeGenFunction::deferPlaceholderReplacement(llvm::Instruction *Old, 2838 llvm::Value *New) { 2839 DeferredReplacements.push_back(std::make_pair(Old, New)); 2840 } 2841 2842 RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, 2843 llvm::Value *Callee, 2844 ReturnValueSlot ReturnValue, 2845 const CallArgList &CallArgs, 2846 const Decl *TargetDecl, 2847 llvm::Instruction **callOrInvoke) { 2848 // FIXME: We no longer need the types from CallArgs; lift up and simplify. 2849 2850 // Handle struct-return functions by passing a pointer to the 2851 // location that we would like to return into. 2852 QualType RetTy = CallInfo.getReturnType(); 2853 const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); 2854 2855 llvm::FunctionType *IRFuncTy = 2856 cast<llvm::FunctionType>( 2857 cast<llvm::PointerType>(Callee->getType())->getElementType()); 2858 2859 // If we're using inalloca, insert the allocation after the stack save. 2860 // FIXME: Do this earlier rather than hacking it in here! 2861 llvm::Value *ArgMemory = nullptr; 2862 if (llvm::StructType *ArgStruct = CallInfo.getArgStruct()) { 2863 llvm::Instruction *IP = CallArgs.getStackBase(); 2864 llvm::AllocaInst *AI; 2865 if (IP) { 2866 IP = IP->getNextNode(); 2867 AI = new llvm::AllocaInst(ArgStruct, "argmem", IP); 2868 } else { 2869 AI = CreateTempAlloca(ArgStruct, "argmem"); 2870 } 2871 AI->setUsedWithInAlloca(true); 2872 assert(AI->isUsedWithInAlloca() && !AI->isStaticAlloca()); 2873 ArgMemory = AI; 2874 } 2875 2876 ClangToLLVMArgMapping IRFunctionArgs(CGM.getContext(), CallInfo); 2877 SmallVector<llvm::Value *, 16> IRCallArgs(IRFunctionArgs.totalIRArgs()); 2878 2879 // If the call returns a temporary with struct return, create a temporary 2880 // alloca to hold the result, unless one is given to us. 2881 llvm::Value *SRetPtr = nullptr; 2882 if (RetAI.isIndirect() || RetAI.isInAlloca()) { 2883 SRetPtr = ReturnValue.getValue(); 2884 if (!SRetPtr) 2885 SRetPtr = CreateMemTemp(RetTy); 2886 if (IRFunctionArgs.hasSRetArg()) { 2887 IRCallArgs[IRFunctionArgs.getSRetArgNo()] = SRetPtr; 2888 } else { 2889 llvm::Value *Addr = 2890 Builder.CreateStructGEP(ArgMemory, RetAI.getInAllocaFieldIndex()); 2891 Builder.CreateStore(SRetPtr, Addr); 2892 } 2893 } 2894 2895 assert(CallInfo.arg_size() == CallArgs.size() && 2896 "Mismatch between function signature & arguments."); 2897 unsigned ArgNo = 0; 2898 CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); 2899 for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 2900 I != E; ++I, ++info_it, ++ArgNo) { 2901 const ABIArgInfo &ArgInfo = info_it->info; 2902 RValue RV = I->RV; 2903 2904 CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty); 2905 2906 // Insert a padding argument to ensure proper alignment. 2907 if (IRFunctionArgs.hasPaddingArg(ArgNo)) 2908 IRCallArgs[IRFunctionArgs.getPaddingArgNo(ArgNo)] = 2909 llvm::UndefValue::get(ArgInfo.getPaddingType()); 2910 2911 unsigned FirstIRArg, NumIRArgs; 2912 std::tie(FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs(ArgNo); 2913 2914 switch (ArgInfo.getKind()) { 2915 case ABIArgInfo::InAlloca: { 2916 assert(NumIRArgs == 0); 2917 assert(getTarget().getTriple().getArch() == llvm::Triple::x86); 2918 if (RV.isAggregate()) { 2919 // Replace the placeholder with the appropriate argument slot GEP. 2920 llvm::Instruction *Placeholder = 2921 cast<llvm::Instruction>(RV.getAggregateAddr()); 2922 CGBuilderTy::InsertPoint IP = Builder.saveIP(); 2923 Builder.SetInsertPoint(Placeholder); 2924 llvm::Value *Addr = Builder.CreateStructGEP( 2925 ArgMemory, ArgInfo.getInAllocaFieldIndex()); 2926 Builder.restoreIP(IP); 2927 deferPlaceholderReplacement(Placeholder, Addr); 2928 } else { 2929 // Store the RValue into the argument struct. 2930 llvm::Value *Addr = 2931 Builder.CreateStructGEP(ArgMemory, ArgInfo.getInAllocaFieldIndex()); 2932 unsigned AS = Addr->getType()->getPointerAddressSpace(); 2933 llvm::Type *MemType = ConvertTypeForMem(I->Ty)->getPointerTo(AS); 2934 // There are some cases where a trivial bitcast is not avoidable. The 2935 // definition of a type later in a translation unit may change it's type 2936 // from {}* to (%struct.foo*)*. 2937 if (Addr->getType() != MemType) 2938 Addr = Builder.CreateBitCast(Addr, MemType); 2939 LValue argLV = MakeAddrLValue(Addr, I->Ty, TypeAlign); 2940 EmitInitStoreOfNonAggregate(*this, RV, argLV); 2941 } 2942 break; 2943 } 2944 2945 case ABIArgInfo::Indirect: { 2946 assert(NumIRArgs == 1); 2947 if (RV.isScalar() || RV.isComplex()) { 2948 // Make a temporary alloca to pass the argument. 2949 llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 2950 if (ArgInfo.getIndirectAlign() > AI->getAlignment()) 2951 AI->setAlignment(ArgInfo.getIndirectAlign()); 2952 IRCallArgs[FirstIRArg] = AI; 2953 2954 LValue argLV = MakeAddrLValue(AI, I->Ty, TypeAlign); 2955 EmitInitStoreOfNonAggregate(*this, RV, argLV); 2956 } else { 2957 // We want to avoid creating an unnecessary temporary+copy here; 2958 // however, we need one in three cases: 2959 // 1. If the argument is not byval, and we are required to copy the 2960 // source. (This case doesn't occur on any common architecture.) 2961 // 2. If the argument is byval, RV is not sufficiently aligned, and 2962 // we cannot force it to be sufficiently aligned. 2963 // 3. If the argument is byval, but RV is located in an address space 2964 // different than that of the argument (0). 2965 llvm::Value *Addr = RV.getAggregateAddr(); 2966 unsigned Align = ArgInfo.getIndirectAlign(); 2967 const llvm::DataLayout *TD = &CGM.getDataLayout(); 2968 const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace(); 2969 const unsigned ArgAddrSpace = 2970 (FirstIRArg < IRFuncTy->getNumParams() 2971 ? IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace() 2972 : 0); 2973 if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || 2974 (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align && 2975 llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) || 2976 (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) { 2977 // Create an aligned temporary, and copy to it. 2978 llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 2979 if (Align > AI->getAlignment()) 2980 AI->setAlignment(Align); 2981 IRCallArgs[FirstIRArg] = AI; 2982 EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); 2983 } else { 2984 // Skip the extra memcpy call. 2985 IRCallArgs[FirstIRArg] = Addr; 2986 } 2987 } 2988 break; 2989 } 2990 2991 case ABIArgInfo::Ignore: 2992 assert(NumIRArgs == 0); 2993 break; 2994 2995 case ABIArgInfo::Extend: 2996 case ABIArgInfo::Direct: { 2997 if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && 2998 ArgInfo.getCoerceToType() == ConvertType(info_it->type) && 2999 ArgInfo.getDirectOffset() == 0) { 3000 assert(NumIRArgs == 1); 3001 llvm::Value *V; 3002 if (RV.isScalar()) 3003 V = RV.getScalarVal(); 3004 else 3005 V = Builder.CreateLoad(RV.getAggregateAddr()); 3006 3007 // If the argument doesn't match, perform a bitcast to coerce it. This 3008 // can happen due to trivial type mismatches. 3009 if (FirstIRArg < IRFuncTy->getNumParams() && 3010 V->getType() != IRFuncTy->getParamType(FirstIRArg)) 3011 V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg)); 3012 IRCallArgs[FirstIRArg] = V; 3013 break; 3014 } 3015 3016 // FIXME: Avoid the conversion through memory if possible. 3017 llvm::Value *SrcPtr; 3018 if (RV.isScalar() || RV.isComplex()) { 3019 SrcPtr = CreateMemTemp(I->Ty, "coerce"); 3020 LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign); 3021 EmitInitStoreOfNonAggregate(*this, RV, SrcLV); 3022 } else 3023 SrcPtr = RV.getAggregateAddr(); 3024 3025 // If the value is offset in memory, apply the offset now. 3026 if (unsigned Offs = ArgInfo.getDirectOffset()) { 3027 SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); 3028 SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); 3029 SrcPtr = Builder.CreateBitCast(SrcPtr, 3030 llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); 3031 3032 } 3033 3034 // Fast-isel and the optimizer generally like scalar values better than 3035 // FCAs, so we flatten them if this is safe to do for this argument. 3036 llvm::StructType *STy = 3037 dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType()); 3038 if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) { 3039 llvm::Type *SrcTy = 3040 cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 3041 uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy); 3042 uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy); 3043 3044 // If the source type is smaller than the destination type of the 3045 // coerce-to logic, copy the source value into a temp alloca the size 3046 // of the destination type to allow loading all of it. The bits past 3047 // the source value are left undef. 3048 if (SrcSize < DstSize) { 3049 llvm::AllocaInst *TempAlloca 3050 = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce"); 3051 Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0); 3052 SrcPtr = TempAlloca; 3053 } else { 3054 SrcPtr = Builder.CreateBitCast(SrcPtr, 3055 llvm::PointerType::getUnqual(STy)); 3056 } 3057 3058 assert(NumIRArgs == STy->getNumElements()); 3059 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 3060 llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); 3061 llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); 3062 // We don't know what we're loading from. 3063 LI->setAlignment(1); 3064 IRCallArgs[FirstIRArg + i] = LI; 3065 } 3066 } else { 3067 // In the simple case, just pass the coerced loaded value. 3068 assert(NumIRArgs == 1); 3069 IRCallArgs[FirstIRArg] = 3070 CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), *this); 3071 } 3072 3073 break; 3074 } 3075 3076 case ABIArgInfo::Expand: 3077 unsigned IRArgPos = FirstIRArg; 3078 ExpandTypeToArgs(I->Ty, RV, IRFuncTy, IRCallArgs, IRArgPos); 3079 assert(IRArgPos == FirstIRArg + NumIRArgs); 3080 break; 3081 } 3082 } 3083 3084 if (ArgMemory) { 3085 llvm::Value *Arg = ArgMemory; 3086 if (CallInfo.isVariadic()) { 3087 // When passing non-POD arguments by value to variadic functions, we will 3088 // end up with a variadic prototype and an inalloca call site. In such 3089 // cases, we can't do any parameter mismatch checks. Give up and bitcast 3090 // the callee. 3091 unsigned CalleeAS = 3092 cast<llvm::PointerType>(Callee->getType())->getAddressSpace(); 3093 Callee = Builder.CreateBitCast( 3094 Callee, getTypes().GetFunctionType(CallInfo)->getPointerTo(CalleeAS)); 3095 } else { 3096 llvm::Type *LastParamTy = 3097 IRFuncTy->getParamType(IRFuncTy->getNumParams() - 1); 3098 if (Arg->getType() != LastParamTy) { 3099 #ifndef NDEBUG 3100 // Assert that these structs have equivalent element types. 3101 llvm::StructType *FullTy = CallInfo.getArgStruct(); 3102 llvm::StructType *DeclaredTy = cast<llvm::StructType>( 3103 cast<llvm::PointerType>(LastParamTy)->getElementType()); 3104 assert(DeclaredTy->getNumElements() == FullTy->getNumElements()); 3105 for (llvm::StructType::element_iterator DI = DeclaredTy->element_begin(), 3106 DE = DeclaredTy->element_end(), 3107 FI = FullTy->element_begin(); 3108 DI != DE; ++DI, ++FI) 3109 assert(*DI == *FI); 3110 #endif 3111 Arg = Builder.CreateBitCast(Arg, LastParamTy); 3112 } 3113 } 3114 assert(IRFunctionArgs.hasInallocaArg()); 3115 IRCallArgs[IRFunctionArgs.getInallocaArgNo()] = Arg; 3116 } 3117 3118 if (!CallArgs.getCleanupsToDeactivate().empty()) 3119 deactivateArgCleanupsBeforeCall(*this, CallArgs); 3120 3121 // If the callee is a bitcast of a function to a varargs pointer to function 3122 // type, check to see if we can remove the bitcast. This handles some cases 3123 // with unprototyped functions. 3124 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) 3125 if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { 3126 llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); 3127 llvm::FunctionType *CurFT = 3128 cast<llvm::FunctionType>(CurPT->getElementType()); 3129 llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); 3130 3131 if (CE->getOpcode() == llvm::Instruction::BitCast && 3132 ActualFT->getReturnType() == CurFT->getReturnType() && 3133 ActualFT->getNumParams() == CurFT->getNumParams() && 3134 ActualFT->getNumParams() == IRCallArgs.size() && 3135 (CurFT->isVarArg() || !ActualFT->isVarArg())) { 3136 bool ArgsMatch = true; 3137 for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) 3138 if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { 3139 ArgsMatch = false; 3140 break; 3141 } 3142 3143 // Strip the cast if we can get away with it. This is a nice cleanup, 3144 // but also allows us to inline the function at -O0 if it is marked 3145 // always_inline. 3146 if (ArgsMatch) 3147 Callee = CalleeF; 3148 } 3149 } 3150 3151 assert(IRCallArgs.size() == IRFuncTy->getNumParams() || IRFuncTy->isVarArg()); 3152 for (unsigned i = 0; i < IRCallArgs.size(); ++i) { 3153 // Inalloca argument can have different type. 3154 if (IRFunctionArgs.hasInallocaArg() && 3155 i == IRFunctionArgs.getInallocaArgNo()) 3156 continue; 3157 if (i < IRFuncTy->getNumParams()) 3158 assert(IRCallArgs[i]->getType() == IRFuncTy->getParamType(i)); 3159 } 3160 3161 unsigned CallingConv; 3162 CodeGen::AttributeListType AttributeList; 3163 CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, 3164 CallingConv, true); 3165 llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(), 3166 AttributeList); 3167 3168 llvm::BasicBlock *InvokeDest = nullptr; 3169 if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex, 3170 llvm::Attribute::NoUnwind)) 3171 InvokeDest = getInvokeDest(); 3172 3173 llvm::CallSite CS; 3174 if (!InvokeDest) { 3175 CS = Builder.CreateCall(Callee, IRCallArgs); 3176 } else { 3177 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); 3178 CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, IRCallArgs); 3179 EmitBlock(Cont); 3180 } 3181 if (callOrInvoke) 3182 *callOrInvoke = CS.getInstruction(); 3183 3184 if (CurCodeDecl && CurCodeDecl->hasAttr<FlattenAttr>() && 3185 !CS.hasFnAttr(llvm::Attribute::NoInline)) 3186 Attrs = 3187 Attrs.addAttribute(getLLVMContext(), llvm::AttributeSet::FunctionIndex, 3188 llvm::Attribute::AlwaysInline); 3189 3190 CS.setAttributes(Attrs); 3191 CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 3192 3193 // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 3194 // optimizer it can aggressively ignore unwind edges. 3195 if (CGM.getLangOpts().ObjCAutoRefCount) 3196 AddObjCARCExceptionMetadata(CS.getInstruction()); 3197 3198 // If the call doesn't return, finish the basic block and clear the 3199 // insertion point; this allows the rest of IRgen to discard 3200 // unreachable code. 3201 if (CS.doesNotReturn()) { 3202 Builder.CreateUnreachable(); 3203 Builder.ClearInsertionPoint(); 3204 3205 // FIXME: For now, emit a dummy basic block because expr emitters in 3206 // generally are not ready to handle emitting expressions at unreachable 3207 // points. 3208 EnsureInsertPoint(); 3209 3210 // Return a reasonable RValue. 3211 return GetUndefRValue(RetTy); 3212 } 3213 3214 llvm::Instruction *CI = CS.getInstruction(); 3215 if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) 3216 CI->setName("call"); 3217 3218 // Emit any writebacks immediately. Arguably this should happen 3219 // after any return-value munging. 3220 if (CallArgs.hasWritebacks()) 3221 emitWritebacks(*this, CallArgs); 3222 3223 // The stack cleanup for inalloca arguments has to run out of the normal 3224 // lexical order, so deactivate it and run it manually here. 3225 CallArgs.freeArgumentMemory(*this); 3226 3227 RValue Ret = [&] { 3228 switch (RetAI.getKind()) { 3229 case ABIArgInfo::InAlloca: 3230 case ABIArgInfo::Indirect: 3231 return convertTempToRValue(SRetPtr, RetTy, SourceLocation()); 3232 3233 case ABIArgInfo::Ignore: 3234 // If we are ignoring an argument that had a result, make sure to 3235 // construct the appropriate return value for our caller. 3236 return GetUndefRValue(RetTy); 3237 3238 case ABIArgInfo::Extend: 3239 case ABIArgInfo::Direct: { 3240 llvm::Type *RetIRTy = ConvertType(RetTy); 3241 if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) { 3242 switch (getEvaluationKind(RetTy)) { 3243 case TEK_Complex: { 3244 llvm::Value *Real = Builder.CreateExtractValue(CI, 0); 3245 llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); 3246 return RValue::getComplex(std::make_pair(Real, Imag)); 3247 } 3248 case TEK_Aggregate: { 3249 llvm::Value *DestPtr = ReturnValue.getValue(); 3250 bool DestIsVolatile = ReturnValue.isVolatile(); 3251 3252 if (!DestPtr) { 3253 DestPtr = CreateMemTemp(RetTy, "agg.tmp"); 3254 DestIsVolatile = false; 3255 } 3256 BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); 3257 return RValue::getAggregate(DestPtr); 3258 } 3259 case TEK_Scalar: { 3260 // If the argument doesn't match, perform a bitcast to coerce it. This 3261 // can happen due to trivial type mismatches. 3262 llvm::Value *V = CI; 3263 if (V->getType() != RetIRTy) 3264 V = Builder.CreateBitCast(V, RetIRTy); 3265 return RValue::get(V); 3266 } 3267 } 3268 llvm_unreachable("bad evaluation kind"); 3269 } 3270 3271 llvm::Value *DestPtr = ReturnValue.getValue(); 3272 bool DestIsVolatile = ReturnValue.isVolatile(); 3273 3274 if (!DestPtr) { 3275 DestPtr = CreateMemTemp(RetTy, "coerce"); 3276 DestIsVolatile = false; 3277 } 3278 3279 // If the value is offset in memory, apply the offset now. 3280 llvm::Value *StorePtr = DestPtr; 3281 if (unsigned Offs = RetAI.getDirectOffset()) { 3282 StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); 3283 StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); 3284 StorePtr = Builder.CreateBitCast(StorePtr, 3285 llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 3286 } 3287 CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); 3288 3289 return convertTempToRValue(DestPtr, RetTy, SourceLocation()); 3290 } 3291 3292 case ABIArgInfo::Expand: 3293 llvm_unreachable("Invalid ABI kind for return argument"); 3294 } 3295 3296 llvm_unreachable("Unhandled ABIArgInfo::Kind"); 3297 } (); 3298 3299 if (Ret.isScalar() && TargetDecl) { 3300 if (const auto *AA = TargetDecl->getAttr<AssumeAlignedAttr>()) { 3301 llvm::Value *OffsetValue = nullptr; 3302 if (const auto *Offset = AA->getOffset()) 3303 OffsetValue = EmitScalarExpr(Offset); 3304 3305 llvm::Value *Alignment = EmitScalarExpr(AA->getAlignment()); 3306 llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(Alignment); 3307 EmitAlignmentAssumption(Ret.getScalarVal(), AlignmentCI->getZExtValue(), 3308 OffsetValue); 3309 } 3310 } 3311 3312 return Ret; 3313 } 3314 3315 /* VarArg handling */ 3316 3317 llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { 3318 return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); 3319 } 3320