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