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