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