1 //===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===// 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 // This contains code to emit Expr nodes as LLVM code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CGCXXABI.h" 15 #include "CGCall.h" 16 #include "CGCleanup.h" 17 #include "CGDebugInfo.h" 18 #include "CGObjCRuntime.h" 19 #include "CGOpenMPRuntime.h" 20 #include "CGRecordLayout.h" 21 #include "CodeGenFunction.h" 22 #include "CodeGenModule.h" 23 #include "ConstantEmitter.h" 24 #include "TargetInfo.h" 25 #include "clang/AST/ASTContext.h" 26 #include "clang/AST/Attr.h" 27 #include "clang/AST/DeclObjC.h" 28 #include "clang/AST/NSAPI.h" 29 #include "clang/Frontend/CodeGenOptions.h" 30 #include "llvm/ADT/Hashing.h" 31 #include "llvm/ADT/StringExtras.h" 32 #include "llvm/IR/DataLayout.h" 33 #include "llvm/IR/Intrinsics.h" 34 #include "llvm/IR/LLVMContext.h" 35 #include "llvm/IR/MDBuilder.h" 36 #include "llvm/Support/ConvertUTF.h" 37 #include "llvm/Support/MathExtras.h" 38 #include "llvm/Support/Path.h" 39 #include "llvm/Transforms/Utils/SanitizerStats.h" 40 41 #include <string> 42 43 using namespace clang; 44 using namespace CodeGen; 45 46 //===--------------------------------------------------------------------===// 47 // Miscellaneous Helper Methods 48 //===--------------------------------------------------------------------===// 49 50 llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) { 51 unsigned addressSpace = 52 cast<llvm::PointerType>(value->getType())->getAddressSpace(); 53 54 llvm::PointerType *destType = Int8PtrTy; 55 if (addressSpace) 56 destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace); 57 58 if (value->getType() == destType) return value; 59 return Builder.CreateBitCast(value, destType); 60 } 61 62 /// CreateTempAlloca - This creates a alloca and inserts it into the entry 63 /// block. 64 Address CodeGenFunction::CreateTempAllocaWithoutCast(llvm::Type *Ty, 65 CharUnits Align, 66 const Twine &Name, 67 llvm::Value *ArraySize) { 68 auto Alloca = CreateTempAlloca(Ty, Name, ArraySize); 69 Alloca->setAlignment(Align.getQuantity()); 70 return Address(Alloca, Align); 71 } 72 73 /// CreateTempAlloca - This creates a alloca and inserts it into the entry 74 /// block. The alloca is casted to default address space if necessary. 75 Address CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, CharUnits Align, 76 const Twine &Name, 77 llvm::Value *ArraySize, 78 Address *AllocaAddr) { 79 auto Alloca = CreateTempAllocaWithoutCast(Ty, Align, Name, ArraySize); 80 if (AllocaAddr) 81 *AllocaAddr = Alloca; 82 llvm::Value *V = Alloca.getPointer(); 83 // Alloca always returns a pointer in alloca address space, which may 84 // be different from the type defined by the language. For example, 85 // in C++ the auto variables are in the default address space. Therefore 86 // cast alloca to the default address space when necessary. 87 if (getASTAllocaAddressSpace() != LangAS::Default) { 88 auto DestAddrSpace = getContext().getTargetAddressSpace(LangAS::Default); 89 llvm::IRBuilderBase::InsertPointGuard IPG(Builder); 90 // When ArraySize is nullptr, alloca is inserted at AllocaInsertPt, 91 // otherwise alloca is inserted at the current insertion point of the 92 // builder. 93 if (!ArraySize) 94 Builder.SetInsertPoint(AllocaInsertPt); 95 V = getTargetHooks().performAddrSpaceCast( 96 *this, V, getASTAllocaAddressSpace(), LangAS::Default, 97 Ty->getPointerTo(DestAddrSpace), /*non-null*/ true); 98 } 99 100 return Address(V, Align); 101 } 102 103 /// CreateTempAlloca - This creates an alloca and inserts it into the entry 104 /// block if \p ArraySize is nullptr, otherwise inserts it at the current 105 /// insertion point of the builder. 106 llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty, 107 const Twine &Name, 108 llvm::Value *ArraySize) { 109 if (ArraySize) 110 return Builder.CreateAlloca(Ty, ArraySize, Name); 111 return new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(), 112 ArraySize, Name, AllocaInsertPt); 113 } 114 115 /// CreateDefaultAlignTempAlloca - This creates an alloca with the 116 /// default alignment of the corresponding LLVM type, which is *not* 117 /// guaranteed to be related in any way to the expected alignment of 118 /// an AST type that might have been lowered to Ty. 119 Address CodeGenFunction::CreateDefaultAlignTempAlloca(llvm::Type *Ty, 120 const Twine &Name) { 121 CharUnits Align = 122 CharUnits::fromQuantity(CGM.getDataLayout().getABITypeAlignment(Ty)); 123 return CreateTempAlloca(Ty, Align, Name); 124 } 125 126 void CodeGenFunction::InitTempAlloca(Address Var, llvm::Value *Init) { 127 assert(isa<llvm::AllocaInst>(Var.getPointer())); 128 auto *Store = new llvm::StoreInst(Init, Var.getPointer()); 129 Store->setAlignment(Var.getAlignment().getQuantity()); 130 llvm::BasicBlock *Block = AllocaInsertPt->getParent(); 131 Block->getInstList().insertAfter(AllocaInsertPt->getIterator(), Store); 132 } 133 134 Address CodeGenFunction::CreateIRTemp(QualType Ty, const Twine &Name) { 135 CharUnits Align = getContext().getTypeAlignInChars(Ty); 136 return CreateTempAlloca(ConvertType(Ty), Align, Name); 137 } 138 139 Address CodeGenFunction::CreateMemTemp(QualType Ty, const Twine &Name, 140 Address *Alloca) { 141 // FIXME: Should we prefer the preferred type alignment here? 142 return CreateMemTemp(Ty, getContext().getTypeAlignInChars(Ty), Name, Alloca); 143 } 144 145 Address CodeGenFunction::CreateMemTemp(QualType Ty, CharUnits Align, 146 const Twine &Name, Address *Alloca) { 147 return CreateTempAlloca(ConvertTypeForMem(Ty), Align, Name, 148 /*ArraySize=*/nullptr, Alloca); 149 } 150 151 Address CodeGenFunction::CreateMemTempWithoutCast(QualType Ty, CharUnits Align, 152 const Twine &Name) { 153 return CreateTempAllocaWithoutCast(ConvertTypeForMem(Ty), Align, Name); 154 } 155 156 Address CodeGenFunction::CreateMemTempWithoutCast(QualType Ty, 157 const Twine &Name) { 158 return CreateMemTempWithoutCast(Ty, getContext().getTypeAlignInChars(Ty), 159 Name); 160 } 161 162 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified 163 /// expression and compare the result against zero, returning an Int1Ty value. 164 llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) { 165 PGO.setCurrentStmt(E); 166 if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) { 167 llvm::Value *MemPtr = EmitScalarExpr(E); 168 return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT); 169 } 170 171 QualType BoolTy = getContext().BoolTy; 172 SourceLocation Loc = E->getExprLoc(); 173 if (!E->getType()->isAnyComplexType()) 174 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy, Loc); 175 176 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(), BoolTy, 177 Loc); 178 } 179 180 /// EmitIgnoredExpr - Emit code to compute the specified expression, 181 /// ignoring the result. 182 void CodeGenFunction::EmitIgnoredExpr(const Expr *E) { 183 if (E->isRValue()) 184 return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true); 185 186 // Just emit it as an l-value and drop the result. 187 EmitLValue(E); 188 } 189 190 /// EmitAnyExpr - Emit code to compute the specified expression which 191 /// can have any type. The result is returned as an RValue struct. 192 /// If this is an aggregate expression, AggSlot indicates where the 193 /// result should be returned. 194 RValue CodeGenFunction::EmitAnyExpr(const Expr *E, 195 AggValueSlot aggSlot, 196 bool ignoreResult) { 197 switch (getEvaluationKind(E->getType())) { 198 case TEK_Scalar: 199 return RValue::get(EmitScalarExpr(E, ignoreResult)); 200 case TEK_Complex: 201 return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult)); 202 case TEK_Aggregate: 203 if (!ignoreResult && aggSlot.isIgnored()) 204 aggSlot = CreateAggTemp(E->getType(), "agg-temp"); 205 EmitAggExpr(E, aggSlot); 206 return aggSlot.asRValue(); 207 } 208 llvm_unreachable("bad evaluation kind"); 209 } 210 211 /// EmitAnyExprToTemp - Similar to EmitAnyExpr(), however, the result will 212 /// always be accessible even if no aggregate location is provided. 213 RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) { 214 AggValueSlot AggSlot = AggValueSlot::ignored(); 215 216 if (hasAggregateEvaluationKind(E->getType())) 217 AggSlot = CreateAggTemp(E->getType(), "agg.tmp"); 218 return EmitAnyExpr(E, AggSlot); 219 } 220 221 /// EmitAnyExprToMem - Evaluate an expression into a given memory 222 /// location. 223 void CodeGenFunction::EmitAnyExprToMem(const Expr *E, 224 Address Location, 225 Qualifiers Quals, 226 bool IsInit) { 227 // FIXME: This function should take an LValue as an argument. 228 switch (getEvaluationKind(E->getType())) { 229 case TEK_Complex: 230 EmitComplexExprIntoLValue(E, MakeAddrLValue(Location, E->getType()), 231 /*isInit*/ false); 232 return; 233 234 case TEK_Aggregate: { 235 EmitAggExpr(E, AggValueSlot::forAddr(Location, Quals, 236 AggValueSlot::IsDestructed_t(IsInit), 237 AggValueSlot::DoesNotNeedGCBarriers, 238 AggValueSlot::IsAliased_t(!IsInit), 239 AggValueSlot::MayOverlap)); 240 return; 241 } 242 243 case TEK_Scalar: { 244 RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false)); 245 LValue LV = MakeAddrLValue(Location, E->getType()); 246 EmitStoreThroughLValue(RV, LV); 247 return; 248 } 249 } 250 llvm_unreachable("bad evaluation kind"); 251 } 252 253 static void 254 pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, 255 const Expr *E, Address ReferenceTemporary) { 256 // Objective-C++ ARC: 257 // If we are binding a reference to a temporary that has ownership, we 258 // need to perform retain/release operations on the temporary. 259 // 260 // FIXME: This should be looking at E, not M. 261 if (auto Lifetime = M->getType().getObjCLifetime()) { 262 switch (Lifetime) { 263 case Qualifiers::OCL_None: 264 case Qualifiers::OCL_ExplicitNone: 265 // Carry on to normal cleanup handling. 266 break; 267 268 case Qualifiers::OCL_Autoreleasing: 269 // Nothing to do; cleaned up by an autorelease pool. 270 return; 271 272 case Qualifiers::OCL_Strong: 273 case Qualifiers::OCL_Weak: 274 switch (StorageDuration Duration = M->getStorageDuration()) { 275 case SD_Static: 276 // Note: we intentionally do not register a cleanup to release 277 // the object on program termination. 278 return; 279 280 case SD_Thread: 281 // FIXME: We should probably register a cleanup in this case. 282 return; 283 284 case SD_Automatic: 285 case SD_FullExpression: 286 CodeGenFunction::Destroyer *Destroy; 287 CleanupKind CleanupKind; 288 if (Lifetime == Qualifiers::OCL_Strong) { 289 const ValueDecl *VD = M->getExtendingDecl(); 290 bool Precise = 291 VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>(); 292 CleanupKind = CGF.getARCCleanupKind(); 293 Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise 294 : &CodeGenFunction::destroyARCStrongImprecise; 295 } else { 296 // __weak objects always get EH cleanups; otherwise, exceptions 297 // could cause really nasty crashes instead of mere leaks. 298 CleanupKind = NormalAndEHCleanup; 299 Destroy = &CodeGenFunction::destroyARCWeak; 300 } 301 if (Duration == SD_FullExpression) 302 CGF.pushDestroy(CleanupKind, ReferenceTemporary, 303 M->getType(), *Destroy, 304 CleanupKind & EHCleanup); 305 else 306 CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary, 307 M->getType(), 308 *Destroy, CleanupKind & EHCleanup); 309 return; 310 311 case SD_Dynamic: 312 llvm_unreachable("temporary cannot have dynamic storage duration"); 313 } 314 llvm_unreachable("unknown storage duration"); 315 } 316 } 317 318 CXXDestructorDecl *ReferenceTemporaryDtor = nullptr; 319 if (const RecordType *RT = 320 E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) { 321 // Get the destructor for the reference temporary. 322 auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 323 if (!ClassDecl->hasTrivialDestructor()) 324 ReferenceTemporaryDtor = ClassDecl->getDestructor(); 325 } 326 327 if (!ReferenceTemporaryDtor) 328 return; 329 330 // Call the destructor for the temporary. 331 switch (M->getStorageDuration()) { 332 case SD_Static: 333 case SD_Thread: { 334 llvm::Constant *CleanupFn; 335 llvm::Constant *CleanupArg; 336 if (E->getType()->isArrayType()) { 337 CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper( 338 ReferenceTemporary, E->getType(), 339 CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions, 340 dyn_cast_or_null<VarDecl>(M->getExtendingDecl())); 341 CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy); 342 } else { 343 CleanupFn = CGF.CGM.getAddrOfCXXStructor(ReferenceTemporaryDtor, 344 StructorType::Complete); 345 CleanupArg = cast<llvm::Constant>(ReferenceTemporary.getPointer()); 346 } 347 CGF.CGM.getCXXABI().registerGlobalDtor( 348 CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg); 349 break; 350 } 351 352 case SD_FullExpression: 353 CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(), 354 CodeGenFunction::destroyCXXObject, 355 CGF.getLangOpts().Exceptions); 356 break; 357 358 case SD_Automatic: 359 CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup, 360 ReferenceTemporary, E->getType(), 361 CodeGenFunction::destroyCXXObject, 362 CGF.getLangOpts().Exceptions); 363 break; 364 365 case SD_Dynamic: 366 llvm_unreachable("temporary cannot have dynamic storage duration"); 367 } 368 } 369 370 static Address createReferenceTemporary(CodeGenFunction &CGF, 371 const MaterializeTemporaryExpr *M, 372 const Expr *Inner, 373 Address *Alloca = nullptr) { 374 auto &TCG = CGF.getTargetHooks(); 375 switch (M->getStorageDuration()) { 376 case SD_FullExpression: 377 case SD_Automatic: { 378 // If we have a constant temporary array or record try to promote it into a 379 // constant global under the same rules a normal constant would've been 380 // promoted. This is easier on the optimizer and generally emits fewer 381 // instructions. 382 QualType Ty = Inner->getType(); 383 if (CGF.CGM.getCodeGenOpts().MergeAllConstants && 384 (Ty->isArrayType() || Ty->isRecordType()) && 385 CGF.CGM.isTypeConstant(Ty, true)) 386 if (auto Init = ConstantEmitter(CGF).tryEmitAbstract(Inner, Ty)) { 387 if (auto AddrSpace = CGF.getTarget().getConstantAddressSpace()) { 388 auto AS = AddrSpace.getValue(); 389 auto *GV = new llvm::GlobalVariable( 390 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, 391 llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp", nullptr, 392 llvm::GlobalValue::NotThreadLocal, 393 CGF.getContext().getTargetAddressSpace(AS)); 394 CharUnits alignment = CGF.getContext().getTypeAlignInChars(Ty); 395 GV->setAlignment(alignment.getQuantity()); 396 llvm::Constant *C = GV; 397 if (AS != LangAS::Default) 398 C = TCG.performAddrSpaceCast( 399 CGF.CGM, GV, AS, LangAS::Default, 400 GV->getValueType()->getPointerTo( 401 CGF.getContext().getTargetAddressSpace(LangAS::Default))); 402 // FIXME: Should we put the new global into a COMDAT? 403 return Address(C, alignment); 404 } 405 } 406 return CGF.CreateMemTemp(Ty, "ref.tmp", Alloca); 407 } 408 case SD_Thread: 409 case SD_Static: 410 return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner); 411 412 case SD_Dynamic: 413 llvm_unreachable("temporary can't have dynamic storage duration"); 414 } 415 llvm_unreachable("unknown storage duration"); 416 } 417 418 LValue CodeGenFunction:: 419 EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) { 420 const Expr *E = M->GetTemporaryExpr(); 421 422 // FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so 423 // as that will cause the lifetime adjustment to be lost for ARC 424 auto ownership = M->getType().getObjCLifetime(); 425 if (ownership != Qualifiers::OCL_None && 426 ownership != Qualifiers::OCL_ExplicitNone) { 427 Address Object = createReferenceTemporary(*this, M, E); 428 if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object.getPointer())) { 429 Object = Address(llvm::ConstantExpr::getBitCast(Var, 430 ConvertTypeForMem(E->getType()) 431 ->getPointerTo(Object.getAddressSpace())), 432 Object.getAlignment()); 433 434 // createReferenceTemporary will promote the temporary to a global with a 435 // constant initializer if it can. It can only do this to a value of 436 // ARC-manageable type if the value is global and therefore "immune" to 437 // ref-counting operations. Therefore we have no need to emit either a 438 // dynamic initialization or a cleanup and we can just return the address 439 // of the temporary. 440 if (Var->hasInitializer()) 441 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl); 442 443 Var->setInitializer(CGM.EmitNullConstant(E->getType())); 444 } 445 LValue RefTempDst = MakeAddrLValue(Object, M->getType(), 446 AlignmentSource::Decl); 447 448 switch (getEvaluationKind(E->getType())) { 449 default: llvm_unreachable("expected scalar or aggregate expression"); 450 case TEK_Scalar: 451 EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false); 452 break; 453 case TEK_Aggregate: { 454 EmitAggExpr(E, AggValueSlot::forAddr(Object, 455 E->getType().getQualifiers(), 456 AggValueSlot::IsDestructed, 457 AggValueSlot::DoesNotNeedGCBarriers, 458 AggValueSlot::IsNotAliased, 459 AggValueSlot::DoesNotOverlap)); 460 break; 461 } 462 } 463 464 pushTemporaryCleanup(*this, M, E, Object); 465 return RefTempDst; 466 } 467 468 SmallVector<const Expr *, 2> CommaLHSs; 469 SmallVector<SubobjectAdjustment, 2> Adjustments; 470 E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); 471 472 for (const auto &Ignored : CommaLHSs) 473 EmitIgnoredExpr(Ignored); 474 475 if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) { 476 if (opaque->getType()->isRecordType()) { 477 assert(Adjustments.empty()); 478 return EmitOpaqueValueLValue(opaque); 479 } 480 } 481 482 // Create and initialize the reference temporary. 483 Address Alloca = Address::invalid(); 484 Address Object = createReferenceTemporary(*this, M, E, &Alloca); 485 if (auto *Var = dyn_cast<llvm::GlobalVariable>( 486 Object.getPointer()->stripPointerCasts())) { 487 Object = Address(llvm::ConstantExpr::getBitCast( 488 cast<llvm::Constant>(Object.getPointer()), 489 ConvertTypeForMem(E->getType())->getPointerTo()), 490 Object.getAlignment()); 491 // If the temporary is a global and has a constant initializer or is a 492 // constant temporary that we promoted to a global, we may have already 493 // initialized it. 494 if (!Var->hasInitializer()) { 495 Var->setInitializer(CGM.EmitNullConstant(E->getType())); 496 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); 497 } 498 } else { 499 switch (M->getStorageDuration()) { 500 case SD_Automatic: 501 case SD_FullExpression: 502 if (auto *Size = EmitLifetimeStart( 503 CGM.getDataLayout().getTypeAllocSize(Alloca.getElementType()), 504 Alloca.getPointer())) { 505 if (M->getStorageDuration() == SD_Automatic) 506 pushCleanupAfterFullExpr<CallLifetimeEnd>(NormalEHLifetimeMarker, 507 Alloca, Size); 508 else 509 pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, Alloca, 510 Size); 511 } 512 break; 513 default: 514 break; 515 } 516 EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true); 517 } 518 pushTemporaryCleanup(*this, M, E, Object); 519 520 // Perform derived-to-base casts and/or field accesses, to get from the 521 // temporary object we created (and, potentially, for which we extended 522 // the lifetime) to the subobject we're binding the reference to. 523 for (unsigned I = Adjustments.size(); I != 0; --I) { 524 SubobjectAdjustment &Adjustment = Adjustments[I-1]; 525 switch (Adjustment.Kind) { 526 case SubobjectAdjustment::DerivedToBaseAdjustment: 527 Object = 528 GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass, 529 Adjustment.DerivedToBase.BasePath->path_begin(), 530 Adjustment.DerivedToBase.BasePath->path_end(), 531 /*NullCheckValue=*/ false, E->getExprLoc()); 532 break; 533 534 case SubobjectAdjustment::FieldAdjustment: { 535 LValue LV = MakeAddrLValue(Object, E->getType(), AlignmentSource::Decl); 536 LV = EmitLValueForField(LV, Adjustment.Field); 537 assert(LV.isSimple() && 538 "materialized temporary field is not a simple lvalue"); 539 Object = LV.getAddress(); 540 break; 541 } 542 543 case SubobjectAdjustment::MemberPointerAdjustment: { 544 llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS); 545 Object = EmitCXXMemberDataPointerAddress(E, Object, Ptr, 546 Adjustment.Ptr.MPT); 547 break; 548 } 549 } 550 } 551 552 return MakeAddrLValue(Object, M->getType(), AlignmentSource::Decl); 553 } 554 555 RValue 556 CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E) { 557 // Emit the expression as an lvalue. 558 LValue LV = EmitLValue(E); 559 assert(LV.isSimple()); 560 llvm::Value *Value = LV.getPointer(); 561 562 if (sanitizePerformTypeCheck() && !E->getType()->isFunctionType()) { 563 // C++11 [dcl.ref]p5 (as amended by core issue 453): 564 // If a glvalue to which a reference is directly bound designates neither 565 // an existing object or function of an appropriate type nor a region of 566 // storage of suitable size and alignment to contain an object of the 567 // reference's type, the behavior is undefined. 568 QualType Ty = E->getType(); 569 EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty); 570 } 571 572 return RValue::get(Value); 573 } 574 575 576 /// getAccessedFieldNo - Given an encoded value and a result number, return the 577 /// input field number being accessed. 578 unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx, 579 const llvm::Constant *Elts) { 580 return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx)) 581 ->getZExtValue(); 582 } 583 584 /// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h. 585 static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low, 586 llvm::Value *High) { 587 llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL); 588 llvm::Value *K47 = Builder.getInt64(47); 589 llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul); 590 llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0); 591 llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul); 592 llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0); 593 return Builder.CreateMul(B1, KMul); 594 } 595 596 bool CodeGenFunction::isNullPointerAllowed(TypeCheckKind TCK) { 597 return TCK == TCK_DowncastPointer || TCK == TCK_Upcast || 598 TCK == TCK_UpcastToVirtualBase || TCK == TCK_DynamicOperation; 599 } 600 601 bool CodeGenFunction::isVptrCheckRequired(TypeCheckKind TCK, QualType Ty) { 602 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); 603 return (RD && RD->hasDefinition() && RD->isDynamicClass()) && 604 (TCK == TCK_MemberAccess || TCK == TCK_MemberCall || 605 TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference || 606 TCK == TCK_UpcastToVirtualBase || TCK == TCK_DynamicOperation); 607 } 608 609 bool CodeGenFunction::sanitizePerformTypeCheck() const { 610 return SanOpts.has(SanitizerKind::Null) | 611 SanOpts.has(SanitizerKind::Alignment) | 612 SanOpts.has(SanitizerKind::ObjectSize) | 613 SanOpts.has(SanitizerKind::Vptr); 614 } 615 616 void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, 617 llvm::Value *Ptr, QualType Ty, 618 CharUnits Alignment, 619 SanitizerSet SkippedChecks) { 620 if (!sanitizePerformTypeCheck()) 621 return; 622 623 // Don't check pointers outside the default address space. The null check 624 // isn't correct, the object-size check isn't supported by LLVM, and we can't 625 // communicate the addresses to the runtime handler for the vptr check. 626 if (Ptr->getType()->getPointerAddressSpace()) 627 return; 628 629 // Don't check pointers to volatile data. The behavior here is implementation- 630 // defined. 631 if (Ty.isVolatileQualified()) 632 return; 633 634 SanitizerScope SanScope(this); 635 636 SmallVector<std::pair<llvm::Value *, SanitizerMask>, 3> Checks; 637 llvm::BasicBlock *Done = nullptr; 638 639 // Quickly determine whether we have a pointer to an alloca. It's possible 640 // to skip null checks, and some alignment checks, for these pointers. This 641 // can reduce compile-time significantly. 642 auto PtrToAlloca = 643 dyn_cast<llvm::AllocaInst>(Ptr->stripPointerCastsNoFollowAliases()); 644 645 llvm::Value *True = llvm::ConstantInt::getTrue(getLLVMContext()); 646 llvm::Value *IsNonNull = nullptr; 647 bool IsGuaranteedNonNull = 648 SkippedChecks.has(SanitizerKind::Null) || PtrToAlloca; 649 bool AllowNullPointers = isNullPointerAllowed(TCK); 650 if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) && 651 !IsGuaranteedNonNull) { 652 // The glvalue must not be an empty glvalue. 653 IsNonNull = Builder.CreateIsNotNull(Ptr); 654 655 // The IR builder can constant-fold the null check if the pointer points to 656 // a constant. 657 IsGuaranteedNonNull = IsNonNull == True; 658 659 // Skip the null check if the pointer is known to be non-null. 660 if (!IsGuaranteedNonNull) { 661 if (AllowNullPointers) { 662 // When performing pointer casts, it's OK if the value is null. 663 // Skip the remaining checks in that case. 664 Done = createBasicBlock("null"); 665 llvm::BasicBlock *Rest = createBasicBlock("not.null"); 666 Builder.CreateCondBr(IsNonNull, Rest, Done); 667 EmitBlock(Rest); 668 } else { 669 Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null)); 670 } 671 } 672 } 673 674 if (SanOpts.has(SanitizerKind::ObjectSize) && 675 !SkippedChecks.has(SanitizerKind::ObjectSize) && 676 !Ty->isIncompleteType()) { 677 uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity(); 678 679 // The glvalue must refer to a large enough storage region. 680 // FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation 681 // to check this. 682 // FIXME: Get object address space 683 llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy }; 684 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys); 685 llvm::Value *Min = Builder.getFalse(); 686 llvm::Value *NullIsUnknown = Builder.getFalse(); 687 llvm::Value *CastAddr = Builder.CreateBitCast(Ptr, Int8PtrTy); 688 llvm::Value *LargeEnough = Builder.CreateICmpUGE( 689 Builder.CreateCall(F, {CastAddr, Min, NullIsUnknown}), 690 llvm::ConstantInt::get(IntPtrTy, Size)); 691 Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize)); 692 } 693 694 uint64_t AlignVal = 0; 695 llvm::Value *PtrAsInt = nullptr; 696 697 if (SanOpts.has(SanitizerKind::Alignment) && 698 !SkippedChecks.has(SanitizerKind::Alignment)) { 699 AlignVal = Alignment.getQuantity(); 700 if (!Ty->isIncompleteType() && !AlignVal) 701 AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity(); 702 703 // The glvalue must be suitably aligned. 704 if (AlignVal > 1 && 705 (!PtrToAlloca || PtrToAlloca->getAlignment() < AlignVal)) { 706 PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy); 707 llvm::Value *Align = Builder.CreateAnd( 708 PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal - 1)); 709 llvm::Value *Aligned = 710 Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0)); 711 if (Aligned != True) 712 Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment)); 713 } 714 } 715 716 if (Checks.size() > 0) { 717 // Make sure we're not losing information. Alignment needs to be a power of 718 // 2 719 assert(!AlignVal || (uint64_t)1 << llvm::Log2_64(AlignVal) == AlignVal); 720 llvm::Constant *StaticData[] = { 721 EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty), 722 llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2_64(AlignVal) : 1), 723 llvm::ConstantInt::get(Int8Ty, TCK)}; 724 EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData, 725 PtrAsInt ? PtrAsInt : Ptr); 726 } 727 728 // If possible, check that the vptr indicates that there is a subobject of 729 // type Ty at offset zero within this object. 730 // 731 // C++11 [basic.life]p5,6: 732 // [For storage which does not refer to an object within its lifetime] 733 // The program has undefined behavior if: 734 // -- the [pointer or glvalue] is used to access a non-static data member 735 // or call a non-static member function 736 if (SanOpts.has(SanitizerKind::Vptr) && 737 !SkippedChecks.has(SanitizerKind::Vptr) && isVptrCheckRequired(TCK, Ty)) { 738 // Ensure that the pointer is non-null before loading it. If there is no 739 // compile-time guarantee, reuse the run-time null check or emit a new one. 740 if (!IsGuaranteedNonNull) { 741 if (!IsNonNull) 742 IsNonNull = Builder.CreateIsNotNull(Ptr); 743 if (!Done) 744 Done = createBasicBlock("vptr.null"); 745 llvm::BasicBlock *VptrNotNull = createBasicBlock("vptr.not.null"); 746 Builder.CreateCondBr(IsNonNull, VptrNotNull, Done); 747 EmitBlock(VptrNotNull); 748 } 749 750 // Compute a hash of the mangled name of the type. 751 // 752 // FIXME: This is not guaranteed to be deterministic! Move to a 753 // fingerprinting mechanism once LLVM provides one. For the time 754 // being the implementation happens to be deterministic. 755 SmallString<64> MangledName; 756 llvm::raw_svector_ostream Out(MangledName); 757 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(), 758 Out); 759 760 // Blacklist based on the mangled type. 761 if (!CGM.getContext().getSanitizerBlacklist().isBlacklistedType( 762 SanitizerKind::Vptr, Out.str())) { 763 llvm::hash_code TypeHash = hash_value(Out.str()); 764 765 // Load the vptr, and compute hash_16_bytes(TypeHash, vptr). 766 llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash); 767 llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0); 768 Address VPtrAddr(Builder.CreateBitCast(Ptr, VPtrTy), getPointerAlign()); 769 llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr); 770 llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty); 771 772 llvm::Value *Hash = emitHash16Bytes(Builder, Low, High); 773 Hash = Builder.CreateTrunc(Hash, IntPtrTy); 774 775 // Look the hash up in our cache. 776 const int CacheSize = 128; 777 llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize); 778 llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable, 779 "__ubsan_vptr_type_cache"); 780 llvm::Value *Slot = Builder.CreateAnd(Hash, 781 llvm::ConstantInt::get(IntPtrTy, 782 CacheSize-1)); 783 llvm::Value *Indices[] = { Builder.getInt32(0), Slot }; 784 llvm::Value *CacheVal = 785 Builder.CreateAlignedLoad(Builder.CreateInBoundsGEP(Cache, Indices), 786 getPointerAlign()); 787 788 // If the hash isn't in the cache, call a runtime handler to perform the 789 // hard work of checking whether the vptr is for an object of the right 790 // type. This will either fill in the cache and return, or produce a 791 // diagnostic. 792 llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash); 793 llvm::Constant *StaticData[] = { 794 EmitCheckSourceLocation(Loc), 795 EmitCheckTypeDescriptor(Ty), 796 CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()), 797 llvm::ConstantInt::get(Int8Ty, TCK) 798 }; 799 llvm::Value *DynamicData[] = { Ptr, Hash }; 800 EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr), 801 SanitizerHandler::DynamicTypeCacheMiss, StaticData, 802 DynamicData); 803 } 804 } 805 806 if (Done) { 807 Builder.CreateBr(Done); 808 EmitBlock(Done); 809 } 810 } 811 812 /// Determine whether this expression refers to a flexible array member in a 813 /// struct. We disable array bounds checks for such members. 814 static bool isFlexibleArrayMemberExpr(const Expr *E) { 815 // For compatibility with existing code, we treat arrays of length 0 or 816 // 1 as flexible array members. 817 const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe(); 818 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { 819 if (CAT->getSize().ugt(1)) 820 return false; 821 } else if (!isa<IncompleteArrayType>(AT)) 822 return false; 823 824 E = E->IgnoreParens(); 825 826 // A flexible array member must be the last member in the class. 827 if (const auto *ME = dyn_cast<MemberExpr>(E)) { 828 // FIXME: If the base type of the member expr is not FD->getParent(), 829 // this should not be treated as a flexible array member access. 830 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { 831 RecordDecl::field_iterator FI( 832 DeclContext::decl_iterator(const_cast<FieldDecl *>(FD))); 833 return ++FI == FD->getParent()->field_end(); 834 } 835 } else if (const auto *IRE = dyn_cast<ObjCIvarRefExpr>(E)) { 836 return IRE->getDecl()->getNextIvar() == nullptr; 837 } 838 839 return false; 840 } 841 842 llvm::Value *CodeGenFunction::LoadPassedObjectSize(const Expr *E, 843 QualType EltTy) { 844 ASTContext &C = getContext(); 845 uint64_t EltSize = C.getTypeSizeInChars(EltTy).getQuantity(); 846 if (!EltSize) 847 return nullptr; 848 849 auto *ArrayDeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()); 850 if (!ArrayDeclRef) 851 return nullptr; 852 853 auto *ParamDecl = dyn_cast<ParmVarDecl>(ArrayDeclRef->getDecl()); 854 if (!ParamDecl) 855 return nullptr; 856 857 auto *POSAttr = ParamDecl->getAttr<PassObjectSizeAttr>(); 858 if (!POSAttr) 859 return nullptr; 860 861 // Don't load the size if it's a lower bound. 862 int POSType = POSAttr->getType(); 863 if (POSType != 0 && POSType != 1) 864 return nullptr; 865 866 // Find the implicit size parameter. 867 auto PassedSizeIt = SizeArguments.find(ParamDecl); 868 if (PassedSizeIt == SizeArguments.end()) 869 return nullptr; 870 871 const ImplicitParamDecl *PassedSizeDecl = PassedSizeIt->second; 872 assert(LocalDeclMap.count(PassedSizeDecl) && "Passed size not loadable"); 873 Address AddrOfSize = LocalDeclMap.find(PassedSizeDecl)->second; 874 llvm::Value *SizeInBytes = EmitLoadOfScalar(AddrOfSize, /*Volatile=*/false, 875 C.getSizeType(), E->getExprLoc()); 876 llvm::Value *SizeOfElement = 877 llvm::ConstantInt::get(SizeInBytes->getType(), EltSize); 878 return Builder.CreateUDiv(SizeInBytes, SizeOfElement); 879 } 880 881 /// If Base is known to point to the start of an array, return the length of 882 /// that array. Return 0 if the length cannot be determined. 883 static llvm::Value *getArrayIndexingBound( 884 CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType) { 885 // For the vector indexing extension, the bound is the number of elements. 886 if (const VectorType *VT = Base->getType()->getAs<VectorType>()) { 887 IndexedType = Base->getType(); 888 return CGF.Builder.getInt32(VT->getNumElements()); 889 } 890 891 Base = Base->IgnoreParens(); 892 893 if (const auto *CE = dyn_cast<CastExpr>(Base)) { 894 if (CE->getCastKind() == CK_ArrayToPointerDecay && 895 !isFlexibleArrayMemberExpr(CE->getSubExpr())) { 896 IndexedType = CE->getSubExpr()->getType(); 897 const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe(); 898 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) 899 return CGF.Builder.getInt(CAT->getSize()); 900 else if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) 901 return CGF.getVLASize(VAT).NumElts; 902 // Ignore pass_object_size here. It's not applicable on decayed pointers. 903 } 904 } 905 906 QualType EltTy{Base->getType()->getPointeeOrArrayElementType(), 0}; 907 if (llvm::Value *POS = CGF.LoadPassedObjectSize(Base, EltTy)) { 908 IndexedType = Base->getType(); 909 return POS; 910 } 911 912 return nullptr; 913 } 914 915 void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base, 916 llvm::Value *Index, QualType IndexType, 917 bool Accessed) { 918 assert(SanOpts.has(SanitizerKind::ArrayBounds) && 919 "should not be called unless adding bounds checks"); 920 SanitizerScope SanScope(this); 921 922 QualType IndexedType; 923 llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType); 924 if (!Bound) 925 return; 926 927 bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType(); 928 llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned); 929 llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false); 930 931 llvm::Constant *StaticData[] = { 932 EmitCheckSourceLocation(E->getExprLoc()), 933 EmitCheckTypeDescriptor(IndexedType), 934 EmitCheckTypeDescriptor(IndexType) 935 }; 936 llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal) 937 : Builder.CreateICmpULE(IndexVal, BoundVal); 938 EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds), 939 SanitizerHandler::OutOfBounds, StaticData, Index); 940 } 941 942 943 CodeGenFunction::ComplexPairTy CodeGenFunction:: 944 EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, 945 bool isInc, bool isPre) { 946 ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc()); 947 948 llvm::Value *NextVal; 949 if (isa<llvm::IntegerType>(InVal.first->getType())) { 950 uint64_t AmountVal = isInc ? 1 : -1; 951 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true); 952 953 // Add the inc/dec to the real part. 954 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); 955 } else { 956 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); 957 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1); 958 if (!isInc) 959 FVal.changeSign(); 960 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal); 961 962 // Add the inc/dec to the real part. 963 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec"); 964 } 965 966 ComplexPairTy IncVal(NextVal, InVal.second); 967 968 // Store the updated result through the lvalue. 969 EmitStoreOfComplex(IncVal, LV, /*init*/ false); 970 971 // If this is a postinc, return the value read from memory, otherwise use the 972 // updated value. 973 return isPre ? IncVal : InVal; 974 } 975 976 void CodeGenModule::EmitExplicitCastExprType(const ExplicitCastExpr *E, 977 CodeGenFunction *CGF) { 978 // Bind VLAs in the cast type. 979 if (CGF && E->getType()->isVariablyModifiedType()) 980 CGF->EmitVariablyModifiedType(E->getType()); 981 982 if (CGDebugInfo *DI = getModuleDebugInfo()) 983 DI->EmitExplicitCastType(E->getType()); 984 } 985 986 //===----------------------------------------------------------------------===// 987 // LValue Expression Emission 988 //===----------------------------------------------------------------------===// 989 990 /// EmitPointerWithAlignment - Given an expression of pointer type, try to 991 /// derive a more accurate bound on the alignment of the pointer. 992 Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E, 993 LValueBaseInfo *BaseInfo, 994 TBAAAccessInfo *TBAAInfo) { 995 // We allow this with ObjC object pointers because of fragile ABIs. 996 assert(E->getType()->isPointerType() || 997 E->getType()->isObjCObjectPointerType()); 998 E = E->IgnoreParens(); 999 1000 // Casts: 1001 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { 1002 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(CE)) 1003 CGM.EmitExplicitCastExprType(ECE, this); 1004 1005 switch (CE->getCastKind()) { 1006 // Non-converting casts (but not C's implicit conversion from void*). 1007 case CK_BitCast: 1008 case CK_NoOp: 1009 case CK_AddressSpaceConversion: 1010 if (auto PtrTy = CE->getSubExpr()->getType()->getAs<PointerType>()) { 1011 if (PtrTy->getPointeeType()->isVoidType()) 1012 break; 1013 1014 LValueBaseInfo InnerBaseInfo; 1015 TBAAAccessInfo InnerTBAAInfo; 1016 Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), 1017 &InnerBaseInfo, 1018 &InnerTBAAInfo); 1019 if (BaseInfo) *BaseInfo = InnerBaseInfo; 1020 if (TBAAInfo) *TBAAInfo = InnerTBAAInfo; 1021 1022 if (isa<ExplicitCastExpr>(CE)) { 1023 LValueBaseInfo TargetTypeBaseInfo; 1024 TBAAAccessInfo TargetTypeTBAAInfo; 1025 CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), 1026 &TargetTypeBaseInfo, 1027 &TargetTypeTBAAInfo); 1028 if (TBAAInfo) 1029 *TBAAInfo = CGM.mergeTBAAInfoForCast(*TBAAInfo, 1030 TargetTypeTBAAInfo); 1031 // If the source l-value is opaque, honor the alignment of the 1032 // casted-to type. 1033 if (InnerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) { 1034 if (BaseInfo) 1035 BaseInfo->mergeForCast(TargetTypeBaseInfo); 1036 Addr = Address(Addr.getPointer(), Align); 1037 } 1038 } 1039 1040 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast) && 1041 CE->getCastKind() == CK_BitCast) { 1042 if (auto PT = E->getType()->getAs<PointerType>()) 1043 EmitVTablePtrCheckForCast(PT->getPointeeType(), Addr.getPointer(), 1044 /*MayBeNull=*/true, 1045 CodeGenFunction::CFITCK_UnrelatedCast, 1046 CE->getLocStart()); 1047 } 1048 return CE->getCastKind() != CK_AddressSpaceConversion 1049 ? Builder.CreateBitCast(Addr, ConvertType(E->getType())) 1050 : Builder.CreateAddrSpaceCast(Addr, 1051 ConvertType(E->getType())); 1052 } 1053 break; 1054 1055 // Array-to-pointer decay. 1056 case CK_ArrayToPointerDecay: 1057 return EmitArrayToPointerDecay(CE->getSubExpr(), BaseInfo, TBAAInfo); 1058 1059 // Derived-to-base conversions. 1060 case CK_UncheckedDerivedToBase: 1061 case CK_DerivedToBase: { 1062 // TODO: Support accesses to members of base classes in TBAA. For now, we 1063 // conservatively pretend that the complete object is of the base class 1064 // type. 1065 if (TBAAInfo) 1066 *TBAAInfo = CGM.getTBAAAccessInfo(E->getType()); 1067 Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), BaseInfo); 1068 auto Derived = CE->getSubExpr()->getType()->getPointeeCXXRecordDecl(); 1069 return GetAddressOfBaseClass(Addr, Derived, 1070 CE->path_begin(), CE->path_end(), 1071 ShouldNullCheckClassCastValue(CE), 1072 CE->getExprLoc()); 1073 } 1074 1075 // TODO: Is there any reason to treat base-to-derived conversions 1076 // specially? 1077 default: 1078 break; 1079 } 1080 } 1081 1082 // Unary &. 1083 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { 1084 if (UO->getOpcode() == UO_AddrOf) { 1085 LValue LV = EmitLValue(UO->getSubExpr()); 1086 if (BaseInfo) *BaseInfo = LV.getBaseInfo(); 1087 if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo(); 1088 return LV.getAddress(); 1089 } 1090 } 1091 1092 // TODO: conditional operators, comma. 1093 1094 // Otherwise, use the alignment of the type. 1095 CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), BaseInfo, 1096 TBAAInfo); 1097 return Address(EmitScalarExpr(E), Align); 1098 } 1099 1100 RValue CodeGenFunction::GetUndefRValue(QualType Ty) { 1101 if (Ty->isVoidType()) 1102 return RValue::get(nullptr); 1103 1104 switch (getEvaluationKind(Ty)) { 1105 case TEK_Complex: { 1106 llvm::Type *EltTy = 1107 ConvertType(Ty->castAs<ComplexType>()->getElementType()); 1108 llvm::Value *U = llvm::UndefValue::get(EltTy); 1109 return RValue::getComplex(std::make_pair(U, U)); 1110 } 1111 1112 // If this is a use of an undefined aggregate type, the aggregate must have an 1113 // identifiable address. Just because the contents of the value are undefined 1114 // doesn't mean that the address can't be taken and compared. 1115 case TEK_Aggregate: { 1116 Address DestPtr = CreateMemTemp(Ty, "undef.agg.tmp"); 1117 return RValue::getAggregate(DestPtr); 1118 } 1119 1120 case TEK_Scalar: 1121 return RValue::get(llvm::UndefValue::get(ConvertType(Ty))); 1122 } 1123 llvm_unreachable("bad evaluation kind"); 1124 } 1125 1126 RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E, 1127 const char *Name) { 1128 ErrorUnsupported(E, Name); 1129 return GetUndefRValue(E->getType()); 1130 } 1131 1132 LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E, 1133 const char *Name) { 1134 ErrorUnsupported(E, Name); 1135 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType())); 1136 return MakeAddrLValue(Address(llvm::UndefValue::get(Ty), CharUnits::One()), 1137 E->getType()); 1138 } 1139 1140 bool CodeGenFunction::IsWrappedCXXThis(const Expr *Obj) { 1141 const Expr *Base = Obj; 1142 while (!isa<CXXThisExpr>(Base)) { 1143 // The result of a dynamic_cast can be null. 1144 if (isa<CXXDynamicCastExpr>(Base)) 1145 return false; 1146 1147 if (const auto *CE = dyn_cast<CastExpr>(Base)) { 1148 Base = CE->getSubExpr(); 1149 } else if (const auto *PE = dyn_cast<ParenExpr>(Base)) { 1150 Base = PE->getSubExpr(); 1151 } else if (const auto *UO = dyn_cast<UnaryOperator>(Base)) { 1152 if (UO->getOpcode() == UO_Extension) 1153 Base = UO->getSubExpr(); 1154 else 1155 return false; 1156 } else { 1157 return false; 1158 } 1159 } 1160 return true; 1161 } 1162 1163 LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) { 1164 LValue LV; 1165 if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E)) 1166 LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true); 1167 else 1168 LV = EmitLValue(E); 1169 if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple()) { 1170 SanitizerSet SkippedChecks; 1171 if (const auto *ME = dyn_cast<MemberExpr>(E)) { 1172 bool IsBaseCXXThis = IsWrappedCXXThis(ME->getBase()); 1173 if (IsBaseCXXThis) 1174 SkippedChecks.set(SanitizerKind::Alignment, true); 1175 if (IsBaseCXXThis || isa<DeclRefExpr>(ME->getBase())) 1176 SkippedChecks.set(SanitizerKind::Null, true); 1177 } 1178 EmitTypeCheck(TCK, E->getExprLoc(), LV.getPointer(), 1179 E->getType(), LV.getAlignment(), SkippedChecks); 1180 } 1181 return LV; 1182 } 1183 1184 /// EmitLValue - Emit code to compute a designator that specifies the location 1185 /// of the expression. 1186 /// 1187 /// This can return one of two things: a simple address or a bitfield reference. 1188 /// In either case, the LLVM Value* in the LValue structure is guaranteed to be 1189 /// an LLVM pointer type. 1190 /// 1191 /// If this returns a bitfield reference, nothing about the pointee type of the 1192 /// LLVM value is known: For example, it may not be a pointer to an integer. 1193 /// 1194 /// If this returns a normal address, and if the lvalue's C type is fixed size, 1195 /// this method guarantees that the returned pointer type will point to an LLVM 1196 /// type of the same size of the lvalue's type. If the lvalue has a variable 1197 /// length type, this is not possible. 1198 /// 1199 LValue CodeGenFunction::EmitLValue(const Expr *E) { 1200 ApplyDebugLocation DL(*this, E); 1201 switch (E->getStmtClass()) { 1202 default: return EmitUnsupportedLValue(E, "l-value expression"); 1203 1204 case Expr::ObjCPropertyRefExprClass: 1205 llvm_unreachable("cannot emit a property reference directly"); 1206 1207 case Expr::ObjCSelectorExprClass: 1208 return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E)); 1209 case Expr::ObjCIsaExprClass: 1210 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E)); 1211 case Expr::BinaryOperatorClass: 1212 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E)); 1213 case Expr::CompoundAssignOperatorClass: { 1214 QualType Ty = E->getType(); 1215 if (const AtomicType *AT = Ty->getAs<AtomicType>()) 1216 Ty = AT->getValueType(); 1217 if (!Ty->isAnyComplexType()) 1218 return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); 1219 return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E)); 1220 } 1221 case Expr::CallExprClass: 1222 case Expr::CXXMemberCallExprClass: 1223 case Expr::CXXOperatorCallExprClass: 1224 case Expr::UserDefinedLiteralClass: 1225 return EmitCallExprLValue(cast<CallExpr>(E)); 1226 case Expr::VAArgExprClass: 1227 return EmitVAArgExprLValue(cast<VAArgExpr>(E)); 1228 case Expr::DeclRefExprClass: 1229 return EmitDeclRefLValue(cast<DeclRefExpr>(E)); 1230 case Expr::ParenExprClass: 1231 return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); 1232 case Expr::GenericSelectionExprClass: 1233 return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr()); 1234 case Expr::PredefinedExprClass: 1235 return EmitPredefinedLValue(cast<PredefinedExpr>(E)); 1236 case Expr::StringLiteralClass: 1237 return EmitStringLiteralLValue(cast<StringLiteral>(E)); 1238 case Expr::ObjCEncodeExprClass: 1239 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E)); 1240 case Expr::PseudoObjectExprClass: 1241 return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E)); 1242 case Expr::InitListExprClass: 1243 return EmitInitListLValue(cast<InitListExpr>(E)); 1244 case Expr::CXXTemporaryObjectExprClass: 1245 case Expr::CXXConstructExprClass: 1246 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E)); 1247 case Expr::CXXBindTemporaryExprClass: 1248 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E)); 1249 case Expr::CXXUuidofExprClass: 1250 return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E)); 1251 case Expr::LambdaExprClass: 1252 return EmitLambdaLValue(cast<LambdaExpr>(E)); 1253 1254 case Expr::ExprWithCleanupsClass: { 1255 const auto *cleanups = cast<ExprWithCleanups>(E); 1256 enterFullExpression(cleanups); 1257 RunCleanupsScope Scope(*this); 1258 LValue LV = EmitLValue(cleanups->getSubExpr()); 1259 if (LV.isSimple()) { 1260 // Defend against branches out of gnu statement expressions surrounded by 1261 // cleanups. 1262 llvm::Value *V = LV.getPointer(); 1263 Scope.ForceCleanup({&V}); 1264 return LValue::MakeAddr(Address(V, LV.getAlignment()), LV.getType(), 1265 getContext(), LV.getBaseInfo(), LV.getTBAAInfo()); 1266 } 1267 // FIXME: Is it possible to create an ExprWithCleanups that produces a 1268 // bitfield lvalue or some other non-simple lvalue? 1269 return LV; 1270 } 1271 1272 case Expr::CXXDefaultArgExprClass: 1273 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr()); 1274 case Expr::CXXDefaultInitExprClass: { 1275 CXXDefaultInitExprScope Scope(*this); 1276 return EmitLValue(cast<CXXDefaultInitExpr>(E)->getExpr()); 1277 } 1278 case Expr::CXXTypeidExprClass: 1279 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E)); 1280 1281 case Expr::ObjCMessageExprClass: 1282 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E)); 1283 case Expr::ObjCIvarRefExprClass: 1284 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E)); 1285 case Expr::StmtExprClass: 1286 return EmitStmtExprLValue(cast<StmtExpr>(E)); 1287 case Expr::UnaryOperatorClass: 1288 return EmitUnaryOpLValue(cast<UnaryOperator>(E)); 1289 case Expr::ArraySubscriptExprClass: 1290 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); 1291 case Expr::OMPArraySectionExprClass: 1292 return EmitOMPArraySectionExpr(cast<OMPArraySectionExpr>(E)); 1293 case Expr::ExtVectorElementExprClass: 1294 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E)); 1295 case Expr::MemberExprClass: 1296 return EmitMemberExpr(cast<MemberExpr>(E)); 1297 case Expr::CompoundLiteralExprClass: 1298 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E)); 1299 case Expr::ConditionalOperatorClass: 1300 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E)); 1301 case Expr::BinaryConditionalOperatorClass: 1302 return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E)); 1303 case Expr::ChooseExprClass: 1304 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr()); 1305 case Expr::OpaqueValueExprClass: 1306 return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E)); 1307 case Expr::SubstNonTypeTemplateParmExprClass: 1308 return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); 1309 case Expr::ImplicitCastExprClass: 1310 case Expr::CStyleCastExprClass: 1311 case Expr::CXXFunctionalCastExprClass: 1312 case Expr::CXXStaticCastExprClass: 1313 case Expr::CXXDynamicCastExprClass: 1314 case Expr::CXXReinterpretCastExprClass: 1315 case Expr::CXXConstCastExprClass: 1316 case Expr::ObjCBridgedCastExprClass: 1317 return EmitCastLValue(cast<CastExpr>(E)); 1318 1319 case Expr::MaterializeTemporaryExprClass: 1320 return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E)); 1321 1322 case Expr::CoawaitExprClass: 1323 return EmitCoawaitLValue(cast<CoawaitExpr>(E)); 1324 case Expr::CoyieldExprClass: 1325 return EmitCoyieldLValue(cast<CoyieldExpr>(E)); 1326 } 1327 } 1328 1329 /// Given an object of the given canonical type, can we safely copy a 1330 /// value out of it based on its initializer? 1331 static bool isConstantEmittableObjectType(QualType type) { 1332 assert(type.isCanonical()); 1333 assert(!type->isReferenceType()); 1334 1335 // Must be const-qualified but non-volatile. 1336 Qualifiers qs = type.getLocalQualifiers(); 1337 if (!qs.hasConst() || qs.hasVolatile()) return false; 1338 1339 // Otherwise, all object types satisfy this except C++ classes with 1340 // mutable subobjects or non-trivial copy/destroy behavior. 1341 if (const auto *RT = dyn_cast<RecordType>(type)) 1342 if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) 1343 if (RD->hasMutableFields() || !RD->isTrivial()) 1344 return false; 1345 1346 return true; 1347 } 1348 1349 /// Can we constant-emit a load of a reference to a variable of the 1350 /// given type? This is different from predicates like 1351 /// Decl::isUsableInConstantExpressions because we do want it to apply 1352 /// in situations that don't necessarily satisfy the language's rules 1353 /// for this (e.g. C++'s ODR-use rules). For example, we want to able 1354 /// to do this with const float variables even if those variables 1355 /// aren't marked 'constexpr'. 1356 enum ConstantEmissionKind { 1357 CEK_None, 1358 CEK_AsReferenceOnly, 1359 CEK_AsValueOrReference, 1360 CEK_AsValueOnly 1361 }; 1362 static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) { 1363 type = type.getCanonicalType(); 1364 if (const auto *ref = dyn_cast<ReferenceType>(type)) { 1365 if (isConstantEmittableObjectType(ref->getPointeeType())) 1366 return CEK_AsValueOrReference; 1367 return CEK_AsReferenceOnly; 1368 } 1369 if (isConstantEmittableObjectType(type)) 1370 return CEK_AsValueOnly; 1371 return CEK_None; 1372 } 1373 1374 /// Try to emit a reference to the given value without producing it as 1375 /// an l-value. This is actually more than an optimization: we can't 1376 /// produce an l-value for variables that we never actually captured 1377 /// in a block or lambda, which means const int variables or constexpr 1378 /// literals or similar. 1379 CodeGenFunction::ConstantEmission 1380 CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) { 1381 ValueDecl *value = refExpr->getDecl(); 1382 1383 // The value needs to be an enum constant or a constant variable. 1384 ConstantEmissionKind CEK; 1385 if (isa<ParmVarDecl>(value)) { 1386 CEK = CEK_None; 1387 } else if (auto *var = dyn_cast<VarDecl>(value)) { 1388 CEK = checkVarTypeForConstantEmission(var->getType()); 1389 } else if (isa<EnumConstantDecl>(value)) { 1390 CEK = CEK_AsValueOnly; 1391 } else { 1392 CEK = CEK_None; 1393 } 1394 if (CEK == CEK_None) return ConstantEmission(); 1395 1396 Expr::EvalResult result; 1397 bool resultIsReference; 1398 QualType resultType; 1399 1400 // It's best to evaluate all the way as an r-value if that's permitted. 1401 if (CEK != CEK_AsReferenceOnly && 1402 refExpr->EvaluateAsRValue(result, getContext())) { 1403 resultIsReference = false; 1404 resultType = refExpr->getType(); 1405 1406 // Otherwise, try to evaluate as an l-value. 1407 } else if (CEK != CEK_AsValueOnly && 1408 refExpr->EvaluateAsLValue(result, getContext())) { 1409 resultIsReference = true; 1410 resultType = value->getType(); 1411 1412 // Failure. 1413 } else { 1414 return ConstantEmission(); 1415 } 1416 1417 // In any case, if the initializer has side-effects, abandon ship. 1418 if (result.HasSideEffects) 1419 return ConstantEmission(); 1420 1421 // Emit as a constant. 1422 auto C = ConstantEmitter(*this).emitAbstract(refExpr->getLocation(), 1423 result.Val, resultType); 1424 1425 // Make sure we emit a debug reference to the global variable. 1426 // This should probably fire even for 1427 if (isa<VarDecl>(value)) { 1428 if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value))) 1429 EmitDeclRefExprDbgValue(refExpr, result.Val); 1430 } else { 1431 assert(isa<EnumConstantDecl>(value)); 1432 EmitDeclRefExprDbgValue(refExpr, result.Val); 1433 } 1434 1435 // If we emitted a reference constant, we need to dereference that. 1436 if (resultIsReference) 1437 return ConstantEmission::forReference(C); 1438 1439 return ConstantEmission::forValue(C); 1440 } 1441 1442 static DeclRefExpr *tryToConvertMemberExprToDeclRefExpr(CodeGenFunction &CGF, 1443 const MemberExpr *ME) { 1444 if (auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) { 1445 // Try to emit static variable member expressions as DREs. 1446 return DeclRefExpr::Create( 1447 CGF.getContext(), NestedNameSpecifierLoc(), SourceLocation(), VD, 1448 /*RefersToEnclosingVariableOrCapture=*/false, ME->getExprLoc(), 1449 ME->getType(), ME->getValueKind()); 1450 } 1451 return nullptr; 1452 } 1453 1454 CodeGenFunction::ConstantEmission 1455 CodeGenFunction::tryEmitAsConstant(const MemberExpr *ME) { 1456 if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, ME)) 1457 return tryEmitAsConstant(DRE); 1458 return ConstantEmission(); 1459 } 1460 1461 llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue, 1462 SourceLocation Loc) { 1463 return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), 1464 lvalue.getType(), Loc, lvalue.getBaseInfo(), 1465 lvalue.getTBAAInfo(), lvalue.isNontemporal()); 1466 } 1467 1468 static bool hasBooleanRepresentation(QualType Ty) { 1469 if (Ty->isBooleanType()) 1470 return true; 1471 1472 if (const EnumType *ET = Ty->getAs<EnumType>()) 1473 return ET->getDecl()->getIntegerType()->isBooleanType(); 1474 1475 if (const AtomicType *AT = Ty->getAs<AtomicType>()) 1476 return hasBooleanRepresentation(AT->getValueType()); 1477 1478 return false; 1479 } 1480 1481 static bool getRangeForType(CodeGenFunction &CGF, QualType Ty, 1482 llvm::APInt &Min, llvm::APInt &End, 1483 bool StrictEnums, bool IsBool) { 1484 const EnumType *ET = Ty->getAs<EnumType>(); 1485 bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums && 1486 ET && !ET->getDecl()->isFixed(); 1487 if (!IsBool && !IsRegularCPlusPlusEnum) 1488 return false; 1489 1490 if (IsBool) { 1491 Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0); 1492 End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2); 1493 } else { 1494 const EnumDecl *ED = ET->getDecl(); 1495 llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType()); 1496 unsigned Bitwidth = LTy->getScalarSizeInBits(); 1497 unsigned NumNegativeBits = ED->getNumNegativeBits(); 1498 unsigned NumPositiveBits = ED->getNumPositiveBits(); 1499 1500 if (NumNegativeBits) { 1501 unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1); 1502 assert(NumBits <= Bitwidth); 1503 End = llvm::APInt(Bitwidth, 1) << (NumBits - 1); 1504 Min = -End; 1505 } else { 1506 assert(NumPositiveBits <= Bitwidth); 1507 End = llvm::APInt(Bitwidth, 1) << NumPositiveBits; 1508 Min = llvm::APInt(Bitwidth, 0); 1509 } 1510 } 1511 return true; 1512 } 1513 1514 llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) { 1515 llvm::APInt Min, End; 1516 if (!getRangeForType(*this, Ty, Min, End, CGM.getCodeGenOpts().StrictEnums, 1517 hasBooleanRepresentation(Ty))) 1518 return nullptr; 1519 1520 llvm::MDBuilder MDHelper(getLLVMContext()); 1521 return MDHelper.createRange(Min, End); 1522 } 1523 1524 bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty, 1525 SourceLocation Loc) { 1526 bool HasBoolCheck = SanOpts.has(SanitizerKind::Bool); 1527 bool HasEnumCheck = SanOpts.has(SanitizerKind::Enum); 1528 if (!HasBoolCheck && !HasEnumCheck) 1529 return false; 1530 1531 bool IsBool = hasBooleanRepresentation(Ty) || 1532 NSAPI(CGM.getContext()).isObjCBOOLType(Ty); 1533 bool NeedsBoolCheck = HasBoolCheck && IsBool; 1534 bool NeedsEnumCheck = HasEnumCheck && Ty->getAs<EnumType>(); 1535 if (!NeedsBoolCheck && !NeedsEnumCheck) 1536 return false; 1537 1538 // Single-bit booleans don't need to be checked. Special-case this to avoid 1539 // a bit width mismatch when handling bitfield values. This is handled by 1540 // EmitFromMemory for the non-bitfield case. 1541 if (IsBool && 1542 cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1) 1543 return false; 1544 1545 llvm::APInt Min, End; 1546 if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool)) 1547 return true; 1548 1549 auto &Ctx = getLLVMContext(); 1550 SanitizerScope SanScope(this); 1551 llvm::Value *Check; 1552 --End; 1553 if (!Min) { 1554 Check = Builder.CreateICmpULE(Value, llvm::ConstantInt::get(Ctx, End)); 1555 } else { 1556 llvm::Value *Upper = 1557 Builder.CreateICmpSLE(Value, llvm::ConstantInt::get(Ctx, End)); 1558 llvm::Value *Lower = 1559 Builder.CreateICmpSGE(Value, llvm::ConstantInt::get(Ctx, Min)); 1560 Check = Builder.CreateAnd(Upper, Lower); 1561 } 1562 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc), 1563 EmitCheckTypeDescriptor(Ty)}; 1564 SanitizerMask Kind = 1565 NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool; 1566 EmitCheck(std::make_pair(Check, Kind), SanitizerHandler::LoadInvalidValue, 1567 StaticArgs, EmitCheckValue(Value)); 1568 return true; 1569 } 1570 1571 llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile, 1572 QualType Ty, 1573 SourceLocation Loc, 1574 LValueBaseInfo BaseInfo, 1575 TBAAAccessInfo TBAAInfo, 1576 bool isNontemporal) { 1577 if (!CGM.getCodeGenOpts().PreserveVec3Type) { 1578 // For better performance, handle vector loads differently. 1579 if (Ty->isVectorType()) { 1580 const llvm::Type *EltTy = Addr.getElementType(); 1581 1582 const auto *VTy = cast<llvm::VectorType>(EltTy); 1583 1584 // Handle vectors of size 3 like size 4 for better performance. 1585 if (VTy->getNumElements() == 3) { 1586 1587 // Bitcast to vec4 type. 1588 llvm::VectorType *vec4Ty = 1589 llvm::VectorType::get(VTy->getElementType(), 4); 1590 Address Cast = Builder.CreateElementBitCast(Addr, vec4Ty, "castToVec4"); 1591 // Now load value. 1592 llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVec4"); 1593 1594 // Shuffle vector to get vec3. 1595 V = Builder.CreateShuffleVector(V, llvm::UndefValue::get(vec4Ty), 1596 {0, 1, 2}, "extractVec"); 1597 return EmitFromMemory(V, Ty); 1598 } 1599 } 1600 } 1601 1602 // Atomic operations have to be done on integral types. 1603 LValue AtomicLValue = 1604 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo); 1605 if (Ty->isAtomicType() || LValueIsSuitableForInlineAtomic(AtomicLValue)) { 1606 return EmitAtomicLoad(AtomicLValue, Loc).getScalarVal(); 1607 } 1608 1609 llvm::LoadInst *Load = Builder.CreateLoad(Addr, Volatile); 1610 if (isNontemporal) { 1611 llvm::MDNode *Node = llvm::MDNode::get( 1612 Load->getContext(), llvm::ConstantAsMetadata::get(Builder.getInt32(1))); 1613 Load->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); 1614 } 1615 1616 CGM.DecorateInstructionWithTBAA(Load, TBAAInfo); 1617 1618 if (EmitScalarRangeCheck(Load, Ty, Loc)) { 1619 // In order to prevent the optimizer from throwing away the check, don't 1620 // attach range metadata to the load. 1621 } else if (CGM.getCodeGenOpts().OptimizationLevel > 0) 1622 if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty)) 1623 Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo); 1624 1625 return EmitFromMemory(Load, Ty); 1626 } 1627 1628 llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { 1629 // Bool has a different representation in memory than in registers. 1630 if (hasBooleanRepresentation(Ty)) { 1631 // This should really always be an i1, but sometimes it's already 1632 // an i8, and it's awkward to track those cases down. 1633 if (Value->getType()->isIntegerTy(1)) 1634 return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool"); 1635 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && 1636 "wrong value rep of bool"); 1637 } 1638 1639 return Value; 1640 } 1641 1642 llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { 1643 // Bool has a different representation in memory than in registers. 1644 if (hasBooleanRepresentation(Ty)) { 1645 assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) && 1646 "wrong value rep of bool"); 1647 return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool"); 1648 } 1649 1650 return Value; 1651 } 1652 1653 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, 1654 bool Volatile, QualType Ty, 1655 LValueBaseInfo BaseInfo, 1656 TBAAAccessInfo TBAAInfo, 1657 bool isInit, bool isNontemporal) { 1658 if (!CGM.getCodeGenOpts().PreserveVec3Type) { 1659 // Handle vectors differently to get better performance. 1660 if (Ty->isVectorType()) { 1661 llvm::Type *SrcTy = Value->getType(); 1662 auto *VecTy = dyn_cast<llvm::VectorType>(SrcTy); 1663 // Handle vec3 special. 1664 if (VecTy && VecTy->getNumElements() == 3) { 1665 // Our source is a vec3, do a shuffle vector to make it a vec4. 1666 llvm::Constant *Mask[] = {Builder.getInt32(0), Builder.getInt32(1), 1667 Builder.getInt32(2), 1668 llvm::UndefValue::get(Builder.getInt32Ty())}; 1669 llvm::Value *MaskV = llvm::ConstantVector::get(Mask); 1670 Value = Builder.CreateShuffleVector(Value, llvm::UndefValue::get(VecTy), 1671 MaskV, "extractVec"); 1672 SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4); 1673 } 1674 if (Addr.getElementType() != SrcTy) { 1675 Addr = Builder.CreateElementBitCast(Addr, SrcTy, "storetmp"); 1676 } 1677 } 1678 } 1679 1680 Value = EmitToMemory(Value, Ty); 1681 1682 LValue AtomicLValue = 1683 LValue::MakeAddr(Addr, Ty, getContext(), BaseInfo, TBAAInfo); 1684 if (Ty->isAtomicType() || 1685 (!isInit && LValueIsSuitableForInlineAtomic(AtomicLValue))) { 1686 EmitAtomicStore(RValue::get(Value), AtomicLValue, isInit); 1687 return; 1688 } 1689 1690 llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile); 1691 if (isNontemporal) { 1692 llvm::MDNode *Node = 1693 llvm::MDNode::get(Store->getContext(), 1694 llvm::ConstantAsMetadata::get(Builder.getInt32(1))); 1695 Store->setMetadata(CGM.getModule().getMDKindID("nontemporal"), Node); 1696 } 1697 1698 CGM.DecorateInstructionWithTBAA(Store, TBAAInfo); 1699 } 1700 1701 void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue, 1702 bool isInit) { 1703 EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(), 1704 lvalue.getType(), lvalue.getBaseInfo(), 1705 lvalue.getTBAAInfo(), isInit, lvalue.isNontemporal()); 1706 } 1707 1708 /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this 1709 /// method emits the address of the lvalue, then loads the result as an rvalue, 1710 /// returning the rvalue. 1711 RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) { 1712 if (LV.isObjCWeak()) { 1713 // load of a __weak object. 1714 Address AddrWeakObj = LV.getAddress(); 1715 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this, 1716 AddrWeakObj)); 1717 } 1718 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 1719 // In MRC mode, we do a load+autorelease. 1720 if (!getLangOpts().ObjCAutoRefCount) { 1721 return RValue::get(EmitARCLoadWeak(LV.getAddress())); 1722 } 1723 1724 // In ARC mode, we load retained and then consume the value. 1725 llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress()); 1726 Object = EmitObjCConsumeObject(LV.getType(), Object); 1727 return RValue::get(Object); 1728 } 1729 1730 if (LV.isSimple()) { 1731 assert(!LV.getType()->isFunctionType()); 1732 1733 // Everything needs a load. 1734 return RValue::get(EmitLoadOfScalar(LV, Loc)); 1735 } 1736 1737 if (LV.isVectorElt()) { 1738 llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddress(), 1739 LV.isVolatileQualified()); 1740 return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(), 1741 "vecext")); 1742 } 1743 1744 // If this is a reference to a subset of the elements of a vector, either 1745 // shuffle the input or extract/insert them as appropriate. 1746 if (LV.isExtVectorElt()) 1747 return EmitLoadOfExtVectorElementLValue(LV); 1748 1749 // Global Register variables always invoke intrinsics 1750 if (LV.isGlobalReg()) 1751 return EmitLoadOfGlobalRegLValue(LV); 1752 1753 assert(LV.isBitField() && "Unknown LValue type!"); 1754 return EmitLoadOfBitfieldLValue(LV, Loc); 1755 } 1756 1757 RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV, 1758 SourceLocation Loc) { 1759 const CGBitFieldInfo &Info = LV.getBitFieldInfo(); 1760 1761 // Get the output type. 1762 llvm::Type *ResLTy = ConvertType(LV.getType()); 1763 1764 Address Ptr = LV.getBitFieldAddress(); 1765 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "bf.load"); 1766 1767 if (Info.IsSigned) { 1768 assert(static_cast<unsigned>(Info.Offset + Info.Size) <= Info.StorageSize); 1769 unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size; 1770 if (HighBits) 1771 Val = Builder.CreateShl(Val, HighBits, "bf.shl"); 1772 if (Info.Offset + HighBits) 1773 Val = Builder.CreateAShr(Val, Info.Offset + HighBits, "bf.ashr"); 1774 } else { 1775 if (Info.Offset) 1776 Val = Builder.CreateLShr(Val, Info.Offset, "bf.lshr"); 1777 if (static_cast<unsigned>(Info.Offset) + Info.Size < Info.StorageSize) 1778 Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(Info.StorageSize, 1779 Info.Size), 1780 "bf.clear"); 1781 } 1782 Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast"); 1783 EmitScalarRangeCheck(Val, LV.getType(), Loc); 1784 return RValue::get(Val); 1785 } 1786 1787 // If this is a reference to a subset of the elements of a vector, create an 1788 // appropriate shufflevector. 1789 RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) { 1790 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddress(), 1791 LV.isVolatileQualified()); 1792 1793 const llvm::Constant *Elts = LV.getExtVectorElts(); 1794 1795 // If the result of the expression is a non-vector type, we must be extracting 1796 // a single element. Just codegen as an extractelement. 1797 const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); 1798 if (!ExprVT) { 1799 unsigned InIdx = getAccessedFieldNo(0, Elts); 1800 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx); 1801 return RValue::get(Builder.CreateExtractElement(Vec, Elt)); 1802 } 1803 1804 // Always use shuffle vector to try to retain the original program structure 1805 unsigned NumResultElts = ExprVT->getNumElements(); 1806 1807 SmallVector<llvm::Constant*, 4> Mask; 1808 for (unsigned i = 0; i != NumResultElts; ++i) 1809 Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts))); 1810 1811 llvm::Value *MaskV = llvm::ConstantVector::get(Mask); 1812 Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()), 1813 MaskV); 1814 return RValue::get(Vec); 1815 } 1816 1817 /// Generates lvalue for partial ext_vector access. 1818 Address CodeGenFunction::EmitExtVectorElementLValue(LValue LV) { 1819 Address VectorAddress = LV.getExtVectorAddress(); 1820 const VectorType *ExprVT = LV.getType()->getAs<VectorType>(); 1821 QualType EQT = ExprVT->getElementType(); 1822 llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT); 1823 1824 Address CastToPointerElement = 1825 Builder.CreateElementBitCast(VectorAddress, VectorElementTy, 1826 "conv.ptr.element"); 1827 1828 const llvm::Constant *Elts = LV.getExtVectorElts(); 1829 unsigned ix = getAccessedFieldNo(0, Elts); 1830 1831 Address VectorBasePtrPlusIx = 1832 Builder.CreateConstInBoundsGEP(CastToPointerElement, ix, 1833 getContext().getTypeSizeInChars(EQT), 1834 "vector.elt"); 1835 1836 return VectorBasePtrPlusIx; 1837 } 1838 1839 /// Load of global gamed gegisters are always calls to intrinsics. 1840 RValue CodeGenFunction::EmitLoadOfGlobalRegLValue(LValue LV) { 1841 assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) && 1842 "Bad type for register variable"); 1843 llvm::MDNode *RegName = cast<llvm::MDNode>( 1844 cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata()); 1845 1846 // We accept integer and pointer types only 1847 llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType()); 1848 llvm::Type *Ty = OrigTy; 1849 if (OrigTy->isPointerTy()) 1850 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy); 1851 llvm::Type *Types[] = { Ty }; 1852 1853 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types); 1854 llvm::Value *Call = Builder.CreateCall( 1855 F, llvm::MetadataAsValue::get(Ty->getContext(), RegName)); 1856 if (OrigTy->isPointerTy()) 1857 Call = Builder.CreateIntToPtr(Call, OrigTy); 1858 return RValue::get(Call); 1859 } 1860 1861 1862 /// EmitStoreThroughLValue - Store the specified rvalue into the specified 1863 /// lvalue, where both are guaranteed to the have the same type, and that type 1864 /// is 'Ty'. 1865 void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, 1866 bool isInit) { 1867 if (!Dst.isSimple()) { 1868 if (Dst.isVectorElt()) { 1869 // Read/modify/write the vector, inserting the new element. 1870 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddress(), 1871 Dst.isVolatileQualified()); 1872 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), 1873 Dst.getVectorIdx(), "vecins"); 1874 Builder.CreateStore(Vec, Dst.getVectorAddress(), 1875 Dst.isVolatileQualified()); 1876 return; 1877 } 1878 1879 // If this is an update of extended vector elements, insert them as 1880 // appropriate. 1881 if (Dst.isExtVectorElt()) 1882 return EmitStoreThroughExtVectorComponentLValue(Src, Dst); 1883 1884 if (Dst.isGlobalReg()) 1885 return EmitStoreThroughGlobalRegLValue(Src, Dst); 1886 1887 assert(Dst.isBitField() && "Unknown LValue type"); 1888 return EmitStoreThroughBitfieldLValue(Src, Dst); 1889 } 1890 1891 // There's special magic for assigning into an ARC-qualified l-value. 1892 if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) { 1893 switch (Lifetime) { 1894 case Qualifiers::OCL_None: 1895 llvm_unreachable("present but none"); 1896 1897 case Qualifiers::OCL_ExplicitNone: 1898 // nothing special 1899 break; 1900 1901 case Qualifiers::OCL_Strong: 1902 if (isInit) { 1903 Src = RValue::get(EmitARCRetain(Dst.getType(), Src.getScalarVal())); 1904 break; 1905 } 1906 EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true); 1907 return; 1908 1909 case Qualifiers::OCL_Weak: 1910 if (isInit) 1911 // Initialize and then skip the primitive store. 1912 EmitARCInitWeak(Dst.getAddress(), Src.getScalarVal()); 1913 else 1914 EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true); 1915 return; 1916 1917 case Qualifiers::OCL_Autoreleasing: 1918 Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(), 1919 Src.getScalarVal())); 1920 // fall into the normal path 1921 break; 1922 } 1923 } 1924 1925 if (Dst.isObjCWeak() && !Dst.isNonGC()) { 1926 // load of a __weak object. 1927 Address LvalueDst = Dst.getAddress(); 1928 llvm::Value *src = Src.getScalarVal(); 1929 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst); 1930 return; 1931 } 1932 1933 if (Dst.isObjCStrong() && !Dst.isNonGC()) { 1934 // load of a __strong object. 1935 Address LvalueDst = Dst.getAddress(); 1936 llvm::Value *src = Src.getScalarVal(); 1937 if (Dst.isObjCIvar()) { 1938 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL"); 1939 llvm::Type *ResultType = IntPtrTy; 1940 Address dst = EmitPointerWithAlignment(Dst.getBaseIvarExp()); 1941 llvm::Value *RHS = dst.getPointer(); 1942 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); 1943 llvm::Value *LHS = 1944 Builder.CreatePtrToInt(LvalueDst.getPointer(), ResultType, 1945 "sub.ptr.lhs.cast"); 1946 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset"); 1947 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst, 1948 BytesBetween); 1949 } else if (Dst.isGlobalObjCRef()) { 1950 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst, 1951 Dst.isThreadLocalRef()); 1952 } 1953 else 1954 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst); 1955 return; 1956 } 1957 1958 assert(Src.isScalar() && "Can't emit an agg store with this method"); 1959 EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit); 1960 } 1961 1962 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, 1963 llvm::Value **Result) { 1964 const CGBitFieldInfo &Info = Dst.getBitFieldInfo(); 1965 llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType()); 1966 Address Ptr = Dst.getBitFieldAddress(); 1967 1968 // Get the source value, truncated to the width of the bit-field. 1969 llvm::Value *SrcVal = Src.getScalarVal(); 1970 1971 // Cast the source to the storage type and shift it into place. 1972 SrcVal = Builder.CreateIntCast(SrcVal, Ptr.getElementType(), 1973 /*IsSigned=*/false); 1974 llvm::Value *MaskedVal = SrcVal; 1975 1976 // See if there are other bits in the bitfield's storage we'll need to load 1977 // and mask together with source before storing. 1978 if (Info.StorageSize != Info.Size) { 1979 assert(Info.StorageSize > Info.Size && "Invalid bitfield size."); 1980 llvm::Value *Val = 1981 Builder.CreateLoad(Ptr, Dst.isVolatileQualified(), "bf.load"); 1982 1983 // Mask the source value as needed. 1984 if (!hasBooleanRepresentation(Dst.getType())) 1985 SrcVal = Builder.CreateAnd(SrcVal, 1986 llvm::APInt::getLowBitsSet(Info.StorageSize, 1987 Info.Size), 1988 "bf.value"); 1989 MaskedVal = SrcVal; 1990 if (Info.Offset) 1991 SrcVal = Builder.CreateShl(SrcVal, Info.Offset, "bf.shl"); 1992 1993 // Mask out the original value. 1994 Val = Builder.CreateAnd(Val, 1995 ~llvm::APInt::getBitsSet(Info.StorageSize, 1996 Info.Offset, 1997 Info.Offset + Info.Size), 1998 "bf.clear"); 1999 2000 // Or together the unchanged values and the source value. 2001 SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set"); 2002 } else { 2003 assert(Info.Offset == 0); 2004 } 2005 2006 // Write the new value back out. 2007 Builder.CreateStore(SrcVal, Ptr, Dst.isVolatileQualified()); 2008 2009 // Return the new value of the bit-field, if requested. 2010 if (Result) { 2011 llvm::Value *ResultVal = MaskedVal; 2012 2013 // Sign extend the value if needed. 2014 if (Info.IsSigned) { 2015 assert(Info.Size <= Info.StorageSize); 2016 unsigned HighBits = Info.StorageSize - Info.Size; 2017 if (HighBits) { 2018 ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl"); 2019 ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr"); 2020 } 2021 } 2022 2023 ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned, 2024 "bf.result.cast"); 2025 *Result = EmitFromMemory(ResultVal, Dst.getType()); 2026 } 2027 } 2028 2029 void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, 2030 LValue Dst) { 2031 // This access turns into a read/modify/write of the vector. Load the input 2032 // value now. 2033 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddress(), 2034 Dst.isVolatileQualified()); 2035 const llvm::Constant *Elts = Dst.getExtVectorElts(); 2036 2037 llvm::Value *SrcVal = Src.getScalarVal(); 2038 2039 if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) { 2040 unsigned NumSrcElts = VTy->getNumElements(); 2041 unsigned NumDstElts = Vec->getType()->getVectorNumElements(); 2042 if (NumDstElts == NumSrcElts) { 2043 // Use shuffle vector is the src and destination are the same number of 2044 // elements and restore the vector mask since it is on the side it will be 2045 // stored. 2046 SmallVector<llvm::Constant*, 4> Mask(NumDstElts); 2047 for (unsigned i = 0; i != NumSrcElts; ++i) 2048 Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i); 2049 2050 llvm::Value *MaskV = llvm::ConstantVector::get(Mask); 2051 Vec = Builder.CreateShuffleVector(SrcVal, 2052 llvm::UndefValue::get(Vec->getType()), 2053 MaskV); 2054 } else if (NumDstElts > NumSrcElts) { 2055 // Extended the source vector to the same length and then shuffle it 2056 // into the destination. 2057 // FIXME: since we're shuffling with undef, can we just use the indices 2058 // into that? This could be simpler. 2059 SmallVector<llvm::Constant*, 4> ExtMask; 2060 for (unsigned i = 0; i != NumSrcElts; ++i) 2061 ExtMask.push_back(Builder.getInt32(i)); 2062 ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty)); 2063 llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask); 2064 llvm::Value *ExtSrcVal = 2065 Builder.CreateShuffleVector(SrcVal, 2066 llvm::UndefValue::get(SrcVal->getType()), 2067 ExtMaskV); 2068 // build identity 2069 SmallVector<llvm::Constant*, 4> Mask; 2070 for (unsigned i = 0; i != NumDstElts; ++i) 2071 Mask.push_back(Builder.getInt32(i)); 2072 2073 // When the vector size is odd and .odd or .hi is used, the last element 2074 // of the Elts constant array will be one past the size of the vector. 2075 // Ignore the last element here, if it is greater than the mask size. 2076 if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size()) 2077 NumSrcElts--; 2078 2079 // modify when what gets shuffled in 2080 for (unsigned i = 0; i != NumSrcElts; ++i) 2081 Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts); 2082 llvm::Value *MaskV = llvm::ConstantVector::get(Mask); 2083 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV); 2084 } else { 2085 // We should never shorten the vector 2086 llvm_unreachable("unexpected shorten vector length"); 2087 } 2088 } else { 2089 // If the Src is a scalar (not a vector) it must be updating one element. 2090 unsigned InIdx = getAccessedFieldNo(0, Elts); 2091 llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx); 2092 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt); 2093 } 2094 2095 Builder.CreateStore(Vec, Dst.getExtVectorAddress(), 2096 Dst.isVolatileQualified()); 2097 } 2098 2099 /// Store of global named registers are always calls to intrinsics. 2100 void CodeGenFunction::EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst) { 2101 assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) && 2102 "Bad type for register variable"); 2103 llvm::MDNode *RegName = cast<llvm::MDNode>( 2104 cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata()); 2105 assert(RegName && "Register LValue is not metadata"); 2106 2107 // We accept integer and pointer types only 2108 llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType()); 2109 llvm::Type *Ty = OrigTy; 2110 if (OrigTy->isPointerTy()) 2111 Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy); 2112 llvm::Type *Types[] = { Ty }; 2113 2114 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types); 2115 llvm::Value *Value = Src.getScalarVal(); 2116 if (OrigTy->isPointerTy()) 2117 Value = Builder.CreatePtrToInt(Value, Ty); 2118 Builder.CreateCall( 2119 F, {llvm::MetadataAsValue::get(Ty->getContext(), RegName), Value}); 2120 } 2121 2122 // setObjCGCLValueClass - sets class of the lvalue for the purpose of 2123 // generating write-barries API. It is currently a global, ivar, 2124 // or neither. 2125 static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, 2126 LValue &LV, 2127 bool IsMemberAccess=false) { 2128 if (Ctx.getLangOpts().getGC() == LangOptions::NonGC) 2129 return; 2130 2131 if (isa<ObjCIvarRefExpr>(E)) { 2132 QualType ExpTy = E->getType(); 2133 if (IsMemberAccess && ExpTy->isPointerType()) { 2134 // If ivar is a structure pointer, assigning to field of 2135 // this struct follows gcc's behavior and makes it a non-ivar 2136 // writer-barrier conservatively. 2137 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); 2138 if (ExpTy->isRecordType()) { 2139 LV.setObjCIvar(false); 2140 return; 2141 } 2142 } 2143 LV.setObjCIvar(true); 2144 auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E)); 2145 LV.setBaseIvarExp(Exp->getBase()); 2146 LV.setObjCArray(E->getType()->isArrayType()); 2147 return; 2148 } 2149 2150 if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) { 2151 if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) { 2152 if (VD->hasGlobalStorage()) { 2153 LV.setGlobalObjCRef(true); 2154 LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None); 2155 } 2156 } 2157 LV.setObjCArray(E->getType()->isArrayType()); 2158 return; 2159 } 2160 2161 if (const auto *Exp = dyn_cast<UnaryOperator>(E)) { 2162 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 2163 return; 2164 } 2165 2166 if (const auto *Exp = dyn_cast<ParenExpr>(E)) { 2167 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 2168 if (LV.isObjCIvar()) { 2169 // If cast is to a structure pointer, follow gcc's behavior and make it 2170 // a non-ivar write-barrier. 2171 QualType ExpTy = E->getType(); 2172 if (ExpTy->isPointerType()) 2173 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType(); 2174 if (ExpTy->isRecordType()) 2175 LV.setObjCIvar(false); 2176 } 2177 return; 2178 } 2179 2180 if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) { 2181 setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV); 2182 return; 2183 } 2184 2185 if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) { 2186 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 2187 return; 2188 } 2189 2190 if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) { 2191 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 2192 return; 2193 } 2194 2195 if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) { 2196 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess); 2197 return; 2198 } 2199 2200 if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) { 2201 setObjCGCLValueClass(Ctx, Exp->getBase(), LV); 2202 if (LV.isObjCIvar() && !LV.isObjCArray()) 2203 // Using array syntax to assigning to what an ivar points to is not 2204 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0; 2205 LV.setObjCIvar(false); 2206 else if (LV.isGlobalObjCRef() && !LV.isObjCArray()) 2207 // Using array syntax to assigning to what global points to is not 2208 // same as assigning to the global itself. {id *G;} G[i] = 0; 2209 LV.setGlobalObjCRef(false); 2210 return; 2211 } 2212 2213 if (const auto *Exp = dyn_cast<MemberExpr>(E)) { 2214 setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true); 2215 // We don't know if member is an 'ivar', but this flag is looked at 2216 // only in the context of LV.isObjCIvar(). 2217 LV.setObjCArray(E->getType()->isArrayType()); 2218 return; 2219 } 2220 } 2221 2222 static llvm::Value * 2223 EmitBitCastOfLValueToProperType(CodeGenFunction &CGF, 2224 llvm::Value *V, llvm::Type *IRType, 2225 StringRef Name = StringRef()) { 2226 unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace(); 2227 return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name); 2228 } 2229 2230 static LValue EmitThreadPrivateVarDeclLValue( 2231 CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr, 2232 llvm::Type *RealVarTy, SourceLocation Loc) { 2233 Addr = CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc); 2234 Addr = CGF.Builder.CreateElementBitCast(Addr, RealVarTy); 2235 return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); 2236 } 2237 2238 static Address emitDeclTargetLinkVarDeclLValue(CodeGenFunction &CGF, 2239 const VarDecl *VD, QualType T) { 2240 for (const auto *D : VD->redecls()) { 2241 if (!VD->hasAttrs()) 2242 continue; 2243 if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>()) 2244 if (Attr->getMapType() == OMPDeclareTargetDeclAttr::MT_Link) { 2245 QualType PtrTy = CGF.getContext().getPointerType(VD->getType()); 2246 Address Addr = 2247 CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD); 2248 return CGF.EmitLoadOfPointer(Addr, PtrTy->castAs<PointerType>()); 2249 } 2250 } 2251 return Address::invalid(); 2252 } 2253 2254 Address 2255 CodeGenFunction::EmitLoadOfReference(LValue RefLVal, 2256 LValueBaseInfo *PointeeBaseInfo, 2257 TBAAAccessInfo *PointeeTBAAInfo) { 2258 llvm::LoadInst *Load = Builder.CreateLoad(RefLVal.getAddress(), 2259 RefLVal.isVolatile()); 2260 CGM.DecorateInstructionWithTBAA(Load, RefLVal.getTBAAInfo()); 2261 2262 CharUnits Align = getNaturalTypeAlignment(RefLVal.getType()->getPointeeType(), 2263 PointeeBaseInfo, PointeeTBAAInfo, 2264 /* forPointeeType= */ true); 2265 return Address(Load, Align); 2266 } 2267 2268 LValue CodeGenFunction::EmitLoadOfReferenceLValue(LValue RefLVal) { 2269 LValueBaseInfo PointeeBaseInfo; 2270 TBAAAccessInfo PointeeTBAAInfo; 2271 Address PointeeAddr = EmitLoadOfReference(RefLVal, &PointeeBaseInfo, 2272 &PointeeTBAAInfo); 2273 return MakeAddrLValue(PointeeAddr, RefLVal.getType()->getPointeeType(), 2274 PointeeBaseInfo, PointeeTBAAInfo); 2275 } 2276 2277 Address CodeGenFunction::EmitLoadOfPointer(Address Ptr, 2278 const PointerType *PtrTy, 2279 LValueBaseInfo *BaseInfo, 2280 TBAAAccessInfo *TBAAInfo) { 2281 llvm::Value *Addr = Builder.CreateLoad(Ptr); 2282 return Address(Addr, getNaturalTypeAlignment(PtrTy->getPointeeType(), 2283 BaseInfo, TBAAInfo, 2284 /*forPointeeType=*/true)); 2285 } 2286 2287 LValue CodeGenFunction::EmitLoadOfPointerLValue(Address PtrAddr, 2288 const PointerType *PtrTy) { 2289 LValueBaseInfo BaseInfo; 2290 TBAAAccessInfo TBAAInfo; 2291 Address Addr = EmitLoadOfPointer(PtrAddr, PtrTy, &BaseInfo, &TBAAInfo); 2292 return MakeAddrLValue(Addr, PtrTy->getPointeeType(), BaseInfo, TBAAInfo); 2293 } 2294 2295 static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, 2296 const Expr *E, const VarDecl *VD) { 2297 QualType T = E->getType(); 2298 2299 // If it's thread_local, emit a call to its wrapper function instead. 2300 if (VD->getTLSKind() == VarDecl::TLS_Dynamic && 2301 CGF.CGM.getCXXABI().usesThreadWrapperFunction()) 2302 return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T); 2303 // Check if the variable is marked as declare target with link clause in 2304 // device codegen. 2305 if (CGF.getLangOpts().OpenMPIsDevice) { 2306 Address Addr = emitDeclTargetLinkVarDeclLValue(CGF, VD, T); 2307 if (Addr.isValid()) 2308 return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); 2309 } 2310 2311 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); 2312 llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType()); 2313 V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy); 2314 CharUnits Alignment = CGF.getContext().getDeclAlign(VD); 2315 Address Addr(V, Alignment); 2316 // Emit reference to the private copy of the variable if it is an OpenMP 2317 // threadprivate variable. 2318 if (CGF.getLangOpts().OpenMP && !CGF.getLangOpts().OpenMPSimd && 2319 VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 2320 return EmitThreadPrivateVarDeclLValue(CGF, VD, T, Addr, RealVarTy, 2321 E->getExprLoc()); 2322 } 2323 LValue LV = VD->getType()->isReferenceType() ? 2324 CGF.EmitLoadOfReferenceLValue(Addr, VD->getType(), 2325 AlignmentSource::Decl) : 2326 CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); 2327 setObjCGCLValueClass(CGF.getContext(), E, LV); 2328 return LV; 2329 } 2330 2331 static llvm::Constant *EmitFunctionDeclPointer(CodeGenModule &CGM, 2332 const FunctionDecl *FD) { 2333 if (FD->hasAttr<WeakRefAttr>()) { 2334 ConstantAddress aliasee = CGM.GetWeakRefReference(FD); 2335 return aliasee.getPointer(); 2336 } 2337 2338 llvm::Constant *V = CGM.GetAddrOfFunction(FD); 2339 if (!FD->hasPrototype()) { 2340 if (const FunctionProtoType *Proto = 2341 FD->getType()->getAs<FunctionProtoType>()) { 2342 // Ugly case: for a K&R-style definition, the type of the definition 2343 // isn't the same as the type of a use. Correct for this with a 2344 // bitcast. 2345 QualType NoProtoType = 2346 CGM.getContext().getFunctionNoProtoType(Proto->getReturnType()); 2347 NoProtoType = CGM.getContext().getPointerType(NoProtoType); 2348 V = llvm::ConstantExpr::getBitCast(V, 2349 CGM.getTypes().ConvertType(NoProtoType)); 2350 } 2351 } 2352 return V; 2353 } 2354 2355 static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, 2356 const Expr *E, const FunctionDecl *FD) { 2357 llvm::Value *V = EmitFunctionDeclPointer(CGF.CGM, FD); 2358 CharUnits Alignment = CGF.getContext().getDeclAlign(FD); 2359 return CGF.MakeAddrLValue(V, E->getType(), Alignment, 2360 AlignmentSource::Decl); 2361 } 2362 2363 static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD, 2364 llvm::Value *ThisValue) { 2365 QualType TagType = CGF.getContext().getTagDeclType(FD->getParent()); 2366 LValue LV = CGF.MakeNaturalAlignAddrLValue(ThisValue, TagType); 2367 return CGF.EmitLValueForField(LV, FD); 2368 } 2369 2370 /// Named Registers are named metadata pointing to the register name 2371 /// which will be read from/written to as an argument to the intrinsic 2372 /// @llvm.read/write_register. 2373 /// So far, only the name is being passed down, but other options such as 2374 /// register type, allocation type or even optimization options could be 2375 /// passed down via the metadata node. 2376 static LValue EmitGlobalNamedRegister(const VarDecl *VD, CodeGenModule &CGM) { 2377 SmallString<64> Name("llvm.named.register."); 2378 AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>(); 2379 assert(Asm->getLabel().size() < 64-Name.size() && 2380 "Register name too big"); 2381 Name.append(Asm->getLabel()); 2382 llvm::NamedMDNode *M = 2383 CGM.getModule().getOrInsertNamedMetadata(Name); 2384 if (M->getNumOperands() == 0) { 2385 llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(), 2386 Asm->getLabel()); 2387 llvm::Metadata *Ops[] = {Str}; 2388 M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); 2389 } 2390 2391 CharUnits Alignment = CGM.getContext().getDeclAlign(VD); 2392 2393 llvm::Value *Ptr = 2394 llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0)); 2395 return LValue::MakeGlobalReg(Address(Ptr, Alignment), VD->getType()); 2396 } 2397 2398 LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { 2399 const NamedDecl *ND = E->getDecl(); 2400 QualType T = E->getType(); 2401 2402 if (const auto *VD = dyn_cast<VarDecl>(ND)) { 2403 // Global Named registers access via intrinsics only 2404 if (VD->getStorageClass() == SC_Register && 2405 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl()) 2406 return EmitGlobalNamedRegister(VD, CGM); 2407 2408 // A DeclRefExpr for a reference initialized by a constant expression can 2409 // appear without being odr-used. Directly emit the constant initializer. 2410 const Expr *Init = VD->getAnyInitializer(VD); 2411 if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() && 2412 VD->isUsableInConstantExpressions(getContext()) && 2413 VD->checkInitIsICE() && 2414 // Do not emit if it is private OpenMP variable. 2415 !(E->refersToEnclosingVariableOrCapture() && 2416 ((CapturedStmtInfo && 2417 (LocalDeclMap.count(VD->getCanonicalDecl()) || 2418 CapturedStmtInfo->lookup(VD->getCanonicalDecl()))) || 2419 LambdaCaptureFields.lookup(VD->getCanonicalDecl()) || 2420 isa<BlockDecl>(CurCodeDecl)))) { 2421 llvm::Constant *Val = 2422 ConstantEmitter(*this).emitAbstract(E->getLocation(), 2423 *VD->evaluateValue(), 2424 VD->getType()); 2425 assert(Val && "failed to emit reference constant expression"); 2426 // FIXME: Eventually we will want to emit vector element references. 2427 2428 // Should we be using the alignment of the constant pointer we emitted? 2429 CharUnits Alignment = getNaturalTypeAlignment(E->getType(), 2430 /* BaseInfo= */ nullptr, 2431 /* TBAAInfo= */ nullptr, 2432 /* forPointeeType= */ true); 2433 return MakeAddrLValue(Address(Val, Alignment), T, AlignmentSource::Decl); 2434 } 2435 2436 // Check for captured variables. 2437 if (E->refersToEnclosingVariableOrCapture()) { 2438 VD = VD->getCanonicalDecl(); 2439 if (auto *FD = LambdaCaptureFields.lookup(VD)) 2440 return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue); 2441 else if (CapturedStmtInfo) { 2442 auto I = LocalDeclMap.find(VD); 2443 if (I != LocalDeclMap.end()) { 2444 if (VD->getType()->isReferenceType()) 2445 return EmitLoadOfReferenceLValue(I->second, VD->getType(), 2446 AlignmentSource::Decl); 2447 return MakeAddrLValue(I->second, T); 2448 } 2449 LValue CapLVal = 2450 EmitCapturedFieldLValue(*this, CapturedStmtInfo->lookup(VD), 2451 CapturedStmtInfo->getContextValue()); 2452 return MakeAddrLValue( 2453 Address(CapLVal.getPointer(), getContext().getDeclAlign(VD)), 2454 CapLVal.getType(), LValueBaseInfo(AlignmentSource::Decl), 2455 CapLVal.getTBAAInfo()); 2456 } 2457 2458 assert(isa<BlockDecl>(CurCodeDecl)); 2459 Address addr = GetAddrOfBlockDecl(VD, VD->hasAttr<BlocksAttr>()); 2460 return MakeAddrLValue(addr, T, AlignmentSource::Decl); 2461 } 2462 } 2463 2464 // FIXME: We should be able to assert this for FunctionDecls as well! 2465 // FIXME: We should be able to assert this for all DeclRefExprs, not just 2466 // those with a valid source location. 2467 assert((ND->isUsed(false) || !isa<VarDecl>(ND) || 2468 !E->getLocation().isValid()) && 2469 "Should not use decl without marking it used!"); 2470 2471 if (ND->hasAttr<WeakRefAttr>()) { 2472 const auto *VD = cast<ValueDecl>(ND); 2473 ConstantAddress Aliasee = CGM.GetWeakRefReference(VD); 2474 return MakeAddrLValue(Aliasee, T, AlignmentSource::Decl); 2475 } 2476 2477 if (const auto *VD = dyn_cast<VarDecl>(ND)) { 2478 // Check if this is a global variable. 2479 if (VD->hasLinkage() || VD->isStaticDataMember()) 2480 return EmitGlobalVarDeclLValue(*this, E, VD); 2481 2482 Address addr = Address::invalid(); 2483 2484 // The variable should generally be present in the local decl map. 2485 auto iter = LocalDeclMap.find(VD); 2486 if (iter != LocalDeclMap.end()) { 2487 addr = iter->second; 2488 2489 // Otherwise, it might be static local we haven't emitted yet for 2490 // some reason; most likely, because it's in an outer function. 2491 } else if (VD->isStaticLocal()) { 2492 addr = Address(CGM.getOrCreateStaticVarDecl( 2493 *VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false)), 2494 getContext().getDeclAlign(VD)); 2495 2496 // No other cases for now. 2497 } else { 2498 llvm_unreachable("DeclRefExpr for Decl not entered in LocalDeclMap?"); 2499 } 2500 2501 2502 // Check for OpenMP threadprivate variables. 2503 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd && 2504 VD->hasAttr<OMPThreadPrivateDeclAttr>()) { 2505 return EmitThreadPrivateVarDeclLValue( 2506 *this, VD, T, addr, getTypes().ConvertTypeForMem(VD->getType()), 2507 E->getExprLoc()); 2508 } 2509 2510 // Drill into block byref variables. 2511 bool isBlockByref = VD->hasAttr<BlocksAttr>(); 2512 if (isBlockByref) { 2513 addr = emitBlockByrefAddress(addr, VD); 2514 } 2515 2516 // Drill into reference types. 2517 LValue LV = VD->getType()->isReferenceType() ? 2518 EmitLoadOfReferenceLValue(addr, VD->getType(), AlignmentSource::Decl) : 2519 MakeAddrLValue(addr, T, AlignmentSource::Decl); 2520 2521 bool isLocalStorage = VD->hasLocalStorage(); 2522 2523 bool NonGCable = isLocalStorage && 2524 !VD->getType()->isReferenceType() && 2525 !isBlockByref; 2526 if (NonGCable) { 2527 LV.getQuals().removeObjCGCAttr(); 2528 LV.setNonGC(true); 2529 } 2530 2531 bool isImpreciseLifetime = 2532 (isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>()); 2533 if (isImpreciseLifetime) 2534 LV.setARCPreciseLifetime(ARCImpreciseLifetime); 2535 setObjCGCLValueClass(getContext(), E, LV); 2536 return LV; 2537 } 2538 2539 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) 2540 return EmitFunctionDeclLValue(*this, E, FD); 2541 2542 // FIXME: While we're emitting a binding from an enclosing scope, all other 2543 // DeclRefExprs we see should be implicitly treated as if they also refer to 2544 // an enclosing scope. 2545 if (const auto *BD = dyn_cast<BindingDecl>(ND)) 2546 return EmitLValue(BD->getBinding()); 2547 2548 llvm_unreachable("Unhandled DeclRefExpr"); 2549 } 2550 2551 LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) { 2552 // __extension__ doesn't affect lvalue-ness. 2553 if (E->getOpcode() == UO_Extension) 2554 return EmitLValue(E->getSubExpr()); 2555 2556 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType()); 2557 switch (E->getOpcode()) { 2558 default: llvm_unreachable("Unknown unary operator lvalue!"); 2559 case UO_Deref: { 2560 QualType T = E->getSubExpr()->getType()->getPointeeType(); 2561 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type"); 2562 2563 LValueBaseInfo BaseInfo; 2564 TBAAAccessInfo TBAAInfo; 2565 Address Addr = EmitPointerWithAlignment(E->getSubExpr(), &BaseInfo, 2566 &TBAAInfo); 2567 LValue LV = MakeAddrLValue(Addr, T, BaseInfo, TBAAInfo); 2568 LV.getQuals().setAddressSpace(ExprTy.getAddressSpace()); 2569 2570 // We should not generate __weak write barrier on indirect reference 2571 // of a pointer to object; as in void foo (__weak id *param); *param = 0; 2572 // But, we continue to generate __strong write barrier on indirect write 2573 // into a pointer to object. 2574 if (getLangOpts().ObjC1 && 2575 getLangOpts().getGC() != LangOptions::NonGC && 2576 LV.isObjCWeak()) 2577 LV.setNonGC(!E->isOBJCGCCandidate(getContext())); 2578 return LV; 2579 } 2580 case UO_Real: 2581 case UO_Imag: { 2582 LValue LV = EmitLValue(E->getSubExpr()); 2583 assert(LV.isSimple() && "real/imag on non-ordinary l-value"); 2584 2585 // __real is valid on scalars. This is a faster way of testing that. 2586 // __imag can only produce an rvalue on scalars. 2587 if (E->getOpcode() == UO_Real && 2588 !LV.getAddress().getElementType()->isStructTy()) { 2589 assert(E->getSubExpr()->getType()->isArithmeticType()); 2590 return LV; 2591 } 2592 2593 QualType T = ExprTy->castAs<ComplexType>()->getElementType(); 2594 2595 Address Component = 2596 (E->getOpcode() == UO_Real 2597 ? emitAddrOfRealComponent(LV.getAddress(), LV.getType()) 2598 : emitAddrOfImagComponent(LV.getAddress(), LV.getType())); 2599 LValue ElemLV = MakeAddrLValue(Component, T, LV.getBaseInfo(), 2600 CGM.getTBAAInfoForSubobject(LV, T)); 2601 ElemLV.getQuals().addQualifiers(LV.getQuals()); 2602 return ElemLV; 2603 } 2604 case UO_PreInc: 2605 case UO_PreDec: { 2606 LValue LV = EmitLValue(E->getSubExpr()); 2607 bool isInc = E->getOpcode() == UO_PreInc; 2608 2609 if (E->getType()->isAnyComplexType()) 2610 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/); 2611 else 2612 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/); 2613 return LV; 2614 } 2615 } 2616 } 2617 2618 LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) { 2619 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E), 2620 E->getType(), AlignmentSource::Decl); 2621 } 2622 2623 LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) { 2624 return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E), 2625 E->getType(), AlignmentSource::Decl); 2626 } 2627 2628 LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { 2629 auto SL = E->getFunctionName(); 2630 assert(SL != nullptr && "No StringLiteral name in PredefinedExpr"); 2631 StringRef FnName = CurFn->getName(); 2632 if (FnName.startswith("\01")) 2633 FnName = FnName.substr(1); 2634 StringRef NameItems[] = { 2635 PredefinedExpr::getIdentTypeName(E->getIdentType()), FnName}; 2636 std::string GVName = llvm::join(NameItems, NameItems + 2, "."); 2637 if (auto *BD = dyn_cast_or_null<BlockDecl>(CurCodeDecl)) { 2638 std::string Name = SL->getString(); 2639 if (!Name.empty()) { 2640 unsigned Discriminator = 2641 CGM.getCXXABI().getMangleContext().getBlockId(BD, true); 2642 if (Discriminator) 2643 Name += "_" + Twine(Discriminator + 1).str(); 2644 auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str()); 2645 return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); 2646 } else { 2647 auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str()); 2648 return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); 2649 } 2650 } 2651 auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName); 2652 return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); 2653 } 2654 2655 /// Emit a type description suitable for use by a runtime sanitizer library. The 2656 /// format of a type descriptor is 2657 /// 2658 /// \code 2659 /// { i16 TypeKind, i16 TypeInfo } 2660 /// \endcode 2661 /// 2662 /// followed by an array of i8 containing the type name. TypeKind is 0 for an 2663 /// integer, 1 for a floating point value, and -1 for anything else. 2664 llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) { 2665 // Only emit each type's descriptor once. 2666 if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T)) 2667 return C; 2668 2669 uint16_t TypeKind = -1; 2670 uint16_t TypeInfo = 0; 2671 2672 if (T->isIntegerType()) { 2673 TypeKind = 0; 2674 TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) | 2675 (T->isSignedIntegerType() ? 1 : 0); 2676 } else if (T->isFloatingType()) { 2677 TypeKind = 1; 2678 TypeInfo = getContext().getTypeSize(T); 2679 } 2680 2681 // Format the type name as if for a diagnostic, including quotes and 2682 // optionally an 'aka'. 2683 SmallString<32> Buffer; 2684 CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype, 2685 (intptr_t)T.getAsOpaquePtr(), 2686 StringRef(), StringRef(), None, Buffer, 2687 None); 2688 2689 llvm::Constant *Components[] = { 2690 Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo), 2691 llvm::ConstantDataArray::getString(getLLVMContext(), Buffer) 2692 }; 2693 llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components); 2694 2695 auto *GV = new llvm::GlobalVariable( 2696 CGM.getModule(), Descriptor->getType(), 2697 /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor); 2698 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2699 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV); 2700 2701 // Remember the descriptor for this type. 2702 CGM.setTypeDescriptorInMap(T, GV); 2703 2704 return GV; 2705 } 2706 2707 llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) { 2708 llvm::Type *TargetTy = IntPtrTy; 2709 2710 if (V->getType() == TargetTy) 2711 return V; 2712 2713 // Floating-point types which fit into intptr_t are bitcast to integers 2714 // and then passed directly (after zero-extension, if necessary). 2715 if (V->getType()->isFloatingPointTy()) { 2716 unsigned Bits = V->getType()->getPrimitiveSizeInBits(); 2717 if (Bits <= TargetTy->getIntegerBitWidth()) 2718 V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(), 2719 Bits)); 2720 } 2721 2722 // Integers which fit in intptr_t are zero-extended and passed directly. 2723 if (V->getType()->isIntegerTy() && 2724 V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth()) 2725 return Builder.CreateZExt(V, TargetTy); 2726 2727 // Pointers are passed directly, everything else is passed by address. 2728 if (!V->getType()->isPointerTy()) { 2729 Address Ptr = CreateDefaultAlignTempAlloca(V->getType()); 2730 Builder.CreateStore(V, Ptr); 2731 V = Ptr.getPointer(); 2732 } 2733 return Builder.CreatePtrToInt(V, TargetTy); 2734 } 2735 2736 /// Emit a representation of a SourceLocation for passing to a handler 2737 /// in a sanitizer runtime library. The format for this data is: 2738 /// \code 2739 /// struct SourceLocation { 2740 /// const char *Filename; 2741 /// int32_t Line, Column; 2742 /// }; 2743 /// \endcode 2744 /// For an invalid SourceLocation, the Filename pointer is null. 2745 llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) { 2746 llvm::Constant *Filename; 2747 int Line, Column; 2748 2749 PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc); 2750 if (PLoc.isValid()) { 2751 StringRef FilenameString = PLoc.getFilename(); 2752 2753 int PathComponentsToStrip = 2754 CGM.getCodeGenOpts().EmitCheckPathComponentsToStrip; 2755 if (PathComponentsToStrip < 0) { 2756 assert(PathComponentsToStrip != INT_MIN); 2757 int PathComponentsToKeep = -PathComponentsToStrip; 2758 auto I = llvm::sys::path::rbegin(FilenameString); 2759 auto E = llvm::sys::path::rend(FilenameString); 2760 while (I != E && --PathComponentsToKeep) 2761 ++I; 2762 2763 FilenameString = FilenameString.substr(I - E); 2764 } else if (PathComponentsToStrip > 0) { 2765 auto I = llvm::sys::path::begin(FilenameString); 2766 auto E = llvm::sys::path::end(FilenameString); 2767 while (I != E && PathComponentsToStrip--) 2768 ++I; 2769 2770 if (I != E) 2771 FilenameString = 2772 FilenameString.substr(I - llvm::sys::path::begin(FilenameString)); 2773 else 2774 FilenameString = llvm::sys::path::filename(FilenameString); 2775 } 2776 2777 auto FilenameGV = CGM.GetAddrOfConstantCString(FilenameString, ".src"); 2778 CGM.getSanitizerMetadata()->disableSanitizerForGlobal( 2779 cast<llvm::GlobalVariable>(FilenameGV.getPointer())); 2780 Filename = FilenameGV.getPointer(); 2781 Line = PLoc.getLine(); 2782 Column = PLoc.getColumn(); 2783 } else { 2784 Filename = llvm::Constant::getNullValue(Int8PtrTy); 2785 Line = Column = 0; 2786 } 2787 2788 llvm::Constant *Data[] = {Filename, Builder.getInt32(Line), 2789 Builder.getInt32(Column)}; 2790 2791 return llvm::ConstantStruct::getAnon(Data); 2792 } 2793 2794 namespace { 2795 /// Specify under what conditions this check can be recovered 2796 enum class CheckRecoverableKind { 2797 /// Always terminate program execution if this check fails. 2798 Unrecoverable, 2799 /// Check supports recovering, runtime has both fatal (noreturn) and 2800 /// non-fatal handlers for this check. 2801 Recoverable, 2802 /// Runtime conditionally aborts, always need to support recovery. 2803 AlwaysRecoverable 2804 }; 2805 } 2806 2807 static CheckRecoverableKind getRecoverableKind(SanitizerMask Kind) { 2808 assert(llvm::countPopulation(Kind) == 1); 2809 switch (Kind) { 2810 case SanitizerKind::Vptr: 2811 return CheckRecoverableKind::AlwaysRecoverable; 2812 case SanitizerKind::Return: 2813 case SanitizerKind::Unreachable: 2814 return CheckRecoverableKind::Unrecoverable; 2815 default: 2816 return CheckRecoverableKind::Recoverable; 2817 } 2818 } 2819 2820 namespace { 2821 struct SanitizerHandlerInfo { 2822 char const *const Name; 2823 unsigned Version; 2824 }; 2825 } 2826 2827 const SanitizerHandlerInfo SanitizerHandlers[] = { 2828 #define SANITIZER_CHECK(Enum, Name, Version) {#Name, Version}, 2829 LIST_SANITIZER_CHECKS 2830 #undef SANITIZER_CHECK 2831 }; 2832 2833 static void emitCheckHandlerCall(CodeGenFunction &CGF, 2834 llvm::FunctionType *FnType, 2835 ArrayRef<llvm::Value *> FnArgs, 2836 SanitizerHandler CheckHandler, 2837 CheckRecoverableKind RecoverKind, bool IsFatal, 2838 llvm::BasicBlock *ContBB) { 2839 assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable); 2840 bool NeedsAbortSuffix = 2841 IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable; 2842 bool MinimalRuntime = CGF.CGM.getCodeGenOpts().SanitizeMinimalRuntime; 2843 const SanitizerHandlerInfo &CheckInfo = SanitizerHandlers[CheckHandler]; 2844 const StringRef CheckName = CheckInfo.Name; 2845 std::string FnName = "__ubsan_handle_" + CheckName.str(); 2846 if (CheckInfo.Version && !MinimalRuntime) 2847 FnName += "_v" + llvm::utostr(CheckInfo.Version); 2848 if (MinimalRuntime) 2849 FnName += "_minimal"; 2850 if (NeedsAbortSuffix) 2851 FnName += "_abort"; 2852 bool MayReturn = 2853 !IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable; 2854 2855 llvm::AttrBuilder B; 2856 if (!MayReturn) { 2857 B.addAttribute(llvm::Attribute::NoReturn) 2858 .addAttribute(llvm::Attribute::NoUnwind); 2859 } 2860 B.addAttribute(llvm::Attribute::UWTable); 2861 2862 llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction( 2863 FnType, FnName, 2864 llvm::AttributeList::get(CGF.getLLVMContext(), 2865 llvm::AttributeList::FunctionIndex, B), 2866 /*Local=*/true); 2867 llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs); 2868 if (!MayReturn) { 2869 HandlerCall->setDoesNotReturn(); 2870 CGF.Builder.CreateUnreachable(); 2871 } else { 2872 CGF.Builder.CreateBr(ContBB); 2873 } 2874 } 2875 2876 void CodeGenFunction::EmitCheck( 2877 ArrayRef<std::pair<llvm::Value *, SanitizerMask>> Checked, 2878 SanitizerHandler CheckHandler, ArrayRef<llvm::Constant *> StaticArgs, 2879 ArrayRef<llvm::Value *> DynamicArgs) { 2880 assert(IsSanitizerScope); 2881 assert(Checked.size() > 0); 2882 assert(CheckHandler >= 0 && 2883 size_t(CheckHandler) < llvm::array_lengthof(SanitizerHandlers)); 2884 const StringRef CheckName = SanitizerHandlers[CheckHandler].Name; 2885 2886 llvm::Value *FatalCond = nullptr; 2887 llvm::Value *RecoverableCond = nullptr; 2888 llvm::Value *TrapCond = nullptr; 2889 for (int i = 0, n = Checked.size(); i < n; ++i) { 2890 llvm::Value *Check = Checked[i].first; 2891 // -fsanitize-trap= overrides -fsanitize-recover=. 2892 llvm::Value *&Cond = 2893 CGM.getCodeGenOpts().SanitizeTrap.has(Checked[i].second) 2894 ? TrapCond 2895 : CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second) 2896 ? RecoverableCond 2897 : FatalCond; 2898 Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check; 2899 } 2900 2901 if (TrapCond) 2902 EmitTrapCheck(TrapCond); 2903 if (!FatalCond && !RecoverableCond) 2904 return; 2905 2906 llvm::Value *JointCond; 2907 if (FatalCond && RecoverableCond) 2908 JointCond = Builder.CreateAnd(FatalCond, RecoverableCond); 2909 else 2910 JointCond = FatalCond ? FatalCond : RecoverableCond; 2911 assert(JointCond); 2912 2913 CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second); 2914 assert(SanOpts.has(Checked[0].second)); 2915 #ifndef NDEBUG 2916 for (int i = 1, n = Checked.size(); i < n; ++i) { 2917 assert(RecoverKind == getRecoverableKind(Checked[i].second) && 2918 "All recoverable kinds in a single check must be same!"); 2919 assert(SanOpts.has(Checked[i].second)); 2920 } 2921 #endif 2922 2923 llvm::BasicBlock *Cont = createBasicBlock("cont"); 2924 llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName); 2925 llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers); 2926 // Give hint that we very much don't expect to execute the handler 2927 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp 2928 llvm::MDBuilder MDHelper(getLLVMContext()); 2929 llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1); 2930 Branch->setMetadata(llvm::LLVMContext::MD_prof, Node); 2931 EmitBlock(Handlers); 2932 2933 // Handler functions take an i8* pointing to the (handler-specific) static 2934 // information block, followed by a sequence of intptr_t arguments 2935 // representing operand values. 2936 SmallVector<llvm::Value *, 4> Args; 2937 SmallVector<llvm::Type *, 4> ArgTypes; 2938 if (!CGM.getCodeGenOpts().SanitizeMinimalRuntime) { 2939 Args.reserve(DynamicArgs.size() + 1); 2940 ArgTypes.reserve(DynamicArgs.size() + 1); 2941 2942 // Emit handler arguments and create handler function type. 2943 if (!StaticArgs.empty()) { 2944 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs); 2945 auto *InfoPtr = 2946 new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false, 2947 llvm::GlobalVariable::PrivateLinkage, Info); 2948 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2949 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr); 2950 Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy)); 2951 ArgTypes.push_back(Int8PtrTy); 2952 } 2953 2954 for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) { 2955 Args.push_back(EmitCheckValue(DynamicArgs[i])); 2956 ArgTypes.push_back(IntPtrTy); 2957 } 2958 } 2959 2960 llvm::FunctionType *FnType = 2961 llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false); 2962 2963 if (!FatalCond || !RecoverableCond) { 2964 // Simple case: we need to generate a single handler call, either 2965 // fatal, or non-fatal. 2966 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, 2967 (FatalCond != nullptr), Cont); 2968 } else { 2969 // Emit two handler calls: first one for set of unrecoverable checks, 2970 // another one for recoverable. 2971 llvm::BasicBlock *NonFatalHandlerBB = 2972 createBasicBlock("non_fatal." + CheckName); 2973 llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName); 2974 Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB); 2975 EmitBlock(FatalHandlerBB); 2976 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, true, 2977 NonFatalHandlerBB); 2978 EmitBlock(NonFatalHandlerBB); 2979 emitCheckHandlerCall(*this, FnType, Args, CheckHandler, RecoverKind, false, 2980 Cont); 2981 } 2982 2983 EmitBlock(Cont); 2984 } 2985 2986 void CodeGenFunction::EmitCfiSlowPathCheck( 2987 SanitizerMask Kind, llvm::Value *Cond, llvm::ConstantInt *TypeId, 2988 llvm::Value *Ptr, ArrayRef<llvm::Constant *> StaticArgs) { 2989 llvm::BasicBlock *Cont = createBasicBlock("cfi.cont"); 2990 2991 llvm::BasicBlock *CheckBB = createBasicBlock("cfi.slowpath"); 2992 llvm::BranchInst *BI = Builder.CreateCondBr(Cond, Cont, CheckBB); 2993 2994 llvm::MDBuilder MDHelper(getLLVMContext()); 2995 llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1); 2996 BI->setMetadata(llvm::LLVMContext::MD_prof, Node); 2997 2998 EmitBlock(CheckBB); 2999 3000 bool WithDiag = !CGM.getCodeGenOpts().SanitizeTrap.has(Kind); 3001 3002 llvm::CallInst *CheckCall; 3003 llvm::Constant *SlowPathFn; 3004 if (WithDiag) { 3005 llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs); 3006 auto *InfoPtr = 3007 new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false, 3008 llvm::GlobalVariable::PrivateLinkage, Info); 3009 InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 3010 CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr); 3011 3012 SlowPathFn = CGM.getModule().getOrInsertFunction( 3013 "__cfi_slowpath_diag", 3014 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, 3015 false)); 3016 CheckCall = Builder.CreateCall( 3017 SlowPathFn, {TypeId, Ptr, Builder.CreateBitCast(InfoPtr, Int8PtrTy)}); 3018 } else { 3019 SlowPathFn = CGM.getModule().getOrInsertFunction( 3020 "__cfi_slowpath", 3021 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy}, false)); 3022 CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr}); 3023 } 3024 3025 CGM.setDSOLocal(cast<llvm::GlobalValue>(SlowPathFn->stripPointerCasts())); 3026 CheckCall->setDoesNotThrow(); 3027 3028 EmitBlock(Cont); 3029 } 3030 3031 // Emit a stub for __cfi_check function so that the linker knows about this 3032 // symbol in LTO mode. 3033 void CodeGenFunction::EmitCfiCheckStub() { 3034 llvm::Module *M = &CGM.getModule(); 3035 auto &Ctx = M->getContext(); 3036 llvm::Function *F = llvm::Function::Create( 3037 llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false), 3038 llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M); 3039 CGM.setDSOLocal(F); 3040 llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F); 3041 // FIXME: consider emitting an intrinsic call like 3042 // call void @llvm.cfi_check(i64 %0, i8* %1, i8* %2) 3043 // which can be lowered in CrossDSOCFI pass to the actual contents of 3044 // __cfi_check. This would allow inlining of __cfi_check calls. 3045 llvm::CallInst::Create( 3046 llvm::Intrinsic::getDeclaration(M, llvm::Intrinsic::trap), "", BB); 3047 llvm::ReturnInst::Create(Ctx, nullptr, BB); 3048 } 3049 3050 // This function is basically a switch over the CFI failure kind, which is 3051 // extracted from CFICheckFailData (1st function argument). Each case is either 3052 // llvm.trap or a call to one of the two runtime handlers, based on 3053 // -fsanitize-trap and -fsanitize-recover settings. Default case (invalid 3054 // failure kind) traps, but this should really never happen. CFICheckFailData 3055 // can be nullptr if the calling module has -fsanitize-trap behavior for this 3056 // check kind; in this case __cfi_check_fail traps as well. 3057 void CodeGenFunction::EmitCfiCheckFail() { 3058 SanitizerScope SanScope(this); 3059 FunctionArgList Args; 3060 ImplicitParamDecl ArgData(getContext(), getContext().VoidPtrTy, 3061 ImplicitParamDecl::Other); 3062 ImplicitParamDecl ArgAddr(getContext(), getContext().VoidPtrTy, 3063 ImplicitParamDecl::Other); 3064 Args.push_back(&ArgData); 3065 Args.push_back(&ArgAddr); 3066 3067 const CGFunctionInfo &FI = 3068 CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, Args); 3069 3070 llvm::Function *F = llvm::Function::Create( 3071 llvm::FunctionType::get(VoidTy, {VoidPtrTy, VoidPtrTy}, false), 3072 llvm::GlobalValue::WeakODRLinkage, "__cfi_check_fail", &CGM.getModule()); 3073 F->setVisibility(llvm::GlobalValue::HiddenVisibility); 3074 3075 StartFunction(GlobalDecl(), CGM.getContext().VoidTy, F, FI, Args, 3076 SourceLocation()); 3077 3078 // This function should not be affected by blacklist. This function does 3079 // not have a source location, but "src:*" would still apply. Revert any 3080 // changes to SanOpts made in StartFunction. 3081 SanOpts = CGM.getLangOpts().Sanitize; 3082 3083 llvm::Value *Data = 3084 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgData), /*Volatile=*/false, 3085 CGM.getContext().VoidPtrTy, ArgData.getLocation()); 3086 llvm::Value *Addr = 3087 EmitLoadOfScalar(GetAddrOfLocalVar(&ArgAddr), /*Volatile=*/false, 3088 CGM.getContext().VoidPtrTy, ArgAddr.getLocation()); 3089 3090 // Data == nullptr means the calling module has trap behaviour for this check. 3091 llvm::Value *DataIsNotNullPtr = 3092 Builder.CreateICmpNE(Data, llvm::ConstantPointerNull::get(Int8PtrTy)); 3093 EmitTrapCheck(DataIsNotNullPtr); 3094 3095 llvm::StructType *SourceLocationTy = 3096 llvm::StructType::get(VoidPtrTy, Int32Ty, Int32Ty); 3097 llvm::StructType *CfiCheckFailDataTy = 3098 llvm::StructType::get(Int8Ty, SourceLocationTy, VoidPtrTy); 3099 3100 llvm::Value *V = Builder.CreateConstGEP2_32( 3101 CfiCheckFailDataTy, 3102 Builder.CreatePointerCast(Data, CfiCheckFailDataTy->getPointerTo(0)), 0, 3103 0); 3104 Address CheckKindAddr(V, getIntAlign()); 3105 llvm::Value *CheckKind = Builder.CreateLoad(CheckKindAddr); 3106 3107 llvm::Value *AllVtables = llvm::MetadataAsValue::get( 3108 CGM.getLLVMContext(), 3109 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables")); 3110 llvm::Value *ValidVtable = Builder.CreateZExt( 3111 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test), 3112 {Addr, AllVtables}), 3113 IntPtrTy); 3114 3115 const std::pair<int, SanitizerMask> CheckKinds[] = { 3116 {CFITCK_VCall, SanitizerKind::CFIVCall}, 3117 {CFITCK_NVCall, SanitizerKind::CFINVCall}, 3118 {CFITCK_DerivedCast, SanitizerKind::CFIDerivedCast}, 3119 {CFITCK_UnrelatedCast, SanitizerKind::CFIUnrelatedCast}, 3120 {CFITCK_ICall, SanitizerKind::CFIICall}}; 3121 3122 SmallVector<std::pair<llvm::Value *, SanitizerMask>, 5> Checks; 3123 for (auto CheckKindMaskPair : CheckKinds) { 3124 int Kind = CheckKindMaskPair.first; 3125 SanitizerMask Mask = CheckKindMaskPair.second; 3126 llvm::Value *Cond = 3127 Builder.CreateICmpNE(CheckKind, llvm::ConstantInt::get(Int8Ty, Kind)); 3128 if (CGM.getLangOpts().Sanitize.has(Mask)) 3129 EmitCheck(std::make_pair(Cond, Mask), SanitizerHandler::CFICheckFail, {}, 3130 {Data, Addr, ValidVtable}); 3131 else 3132 EmitTrapCheck(Cond); 3133 } 3134 3135 FinishFunction(); 3136 // The only reference to this function will be created during LTO link. 3137 // Make sure it survives until then. 3138 CGM.addUsedGlobal(F); 3139 } 3140 3141 void CodeGenFunction::EmitUnreachable(SourceLocation Loc) { 3142 if (SanOpts.has(SanitizerKind::Unreachable)) { 3143 SanitizerScope SanScope(this); 3144 EmitCheck(std::make_pair(static_cast<llvm::Value *>(Builder.getFalse()), 3145 SanitizerKind::Unreachable), 3146 SanitizerHandler::BuiltinUnreachable, 3147 EmitCheckSourceLocation(Loc), None); 3148 } 3149 Builder.CreateUnreachable(); 3150 } 3151 3152 void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked) { 3153 llvm::BasicBlock *Cont = createBasicBlock("cont"); 3154 3155 // If we're optimizing, collapse all calls to trap down to just one per 3156 // function to save on code size. 3157 if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) { 3158 TrapBB = createBasicBlock("trap"); 3159 Builder.CreateCondBr(Checked, Cont, TrapBB); 3160 EmitBlock(TrapBB); 3161 llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); 3162 TrapCall->setDoesNotReturn(); 3163 TrapCall->setDoesNotThrow(); 3164 Builder.CreateUnreachable(); 3165 } else { 3166 Builder.CreateCondBr(Checked, Cont, TrapBB); 3167 } 3168 3169 EmitBlock(Cont); 3170 } 3171 3172 llvm::CallInst *CodeGenFunction::EmitTrapCall(llvm::Intrinsic::ID IntrID) { 3173 llvm::CallInst *TrapCall = Builder.CreateCall(CGM.getIntrinsic(IntrID)); 3174 3175 if (!CGM.getCodeGenOpts().TrapFuncName.empty()) { 3176 auto A = llvm::Attribute::get(getLLVMContext(), "trap-func-name", 3177 CGM.getCodeGenOpts().TrapFuncName); 3178 TrapCall->addAttribute(llvm::AttributeList::FunctionIndex, A); 3179 } 3180 3181 return TrapCall; 3182 } 3183 3184 Address CodeGenFunction::EmitArrayToPointerDecay(const Expr *E, 3185 LValueBaseInfo *BaseInfo, 3186 TBAAAccessInfo *TBAAInfo) { 3187 assert(E->getType()->isArrayType() && 3188 "Array to pointer decay must have array source type!"); 3189 3190 // Expressions of array type can't be bitfields or vector elements. 3191 LValue LV = EmitLValue(E); 3192 Address Addr = LV.getAddress(); 3193 3194 // If the array type was an incomplete type, we need to make sure 3195 // the decay ends up being the right type. 3196 llvm::Type *NewTy = ConvertType(E->getType()); 3197 Addr = Builder.CreateElementBitCast(Addr, NewTy); 3198 3199 // Note that VLA pointers are always decayed, so we don't need to do 3200 // anything here. 3201 if (!E->getType()->isVariableArrayType()) { 3202 assert(isa<llvm::ArrayType>(Addr.getElementType()) && 3203 "Expected pointer to array"); 3204 Addr = Builder.CreateStructGEP(Addr, 0, CharUnits::Zero(), "arraydecay"); 3205 } 3206 3207 // The result of this decay conversion points to an array element within the 3208 // base lvalue. However, since TBAA currently does not support representing 3209 // accesses to elements of member arrays, we conservatively represent accesses 3210 // to the pointee object as if it had no any base lvalue specified. 3211 // TODO: Support TBAA for member arrays. 3212 QualType EltType = E->getType()->castAsArrayTypeUnsafe()->getElementType(); 3213 if (BaseInfo) *BaseInfo = LV.getBaseInfo(); 3214 if (TBAAInfo) *TBAAInfo = CGM.getTBAAAccessInfo(EltType); 3215 3216 return Builder.CreateElementBitCast(Addr, ConvertTypeForMem(EltType)); 3217 } 3218 3219 /// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an 3220 /// array to pointer, return the array subexpression. 3221 static const Expr *isSimpleArrayDecayOperand(const Expr *E) { 3222 // If this isn't just an array->pointer decay, bail out. 3223 const auto *CE = dyn_cast<CastExpr>(E); 3224 if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay) 3225 return nullptr; 3226 3227 // If this is a decay from variable width array, bail out. 3228 const Expr *SubExpr = CE->getSubExpr(); 3229 if (SubExpr->getType()->isVariableArrayType()) 3230 return nullptr; 3231 3232 return SubExpr; 3233 } 3234 3235 static llvm::Value *emitArraySubscriptGEP(CodeGenFunction &CGF, 3236 llvm::Value *ptr, 3237 ArrayRef<llvm::Value*> indices, 3238 bool inbounds, 3239 bool signedIndices, 3240 SourceLocation loc, 3241 const llvm::Twine &name = "arrayidx") { 3242 if (inbounds) { 3243 return CGF.EmitCheckedInBoundsGEP(ptr, indices, signedIndices, 3244 CodeGenFunction::NotSubtraction, loc, 3245 name); 3246 } else { 3247 return CGF.Builder.CreateGEP(ptr, indices, name); 3248 } 3249 } 3250 3251 static CharUnits getArrayElementAlign(CharUnits arrayAlign, 3252 llvm::Value *idx, 3253 CharUnits eltSize) { 3254 // If we have a constant index, we can use the exact offset of the 3255 // element we're accessing. 3256 if (auto constantIdx = dyn_cast<llvm::ConstantInt>(idx)) { 3257 CharUnits offset = constantIdx->getZExtValue() * eltSize; 3258 return arrayAlign.alignmentAtOffset(offset); 3259 3260 // Otherwise, use the worst-case alignment for any element. 3261 } else { 3262 return arrayAlign.alignmentOfArrayElement(eltSize); 3263 } 3264 } 3265 3266 static QualType getFixedSizeElementType(const ASTContext &ctx, 3267 const VariableArrayType *vla) { 3268 QualType eltType; 3269 do { 3270 eltType = vla->getElementType(); 3271 } while ((vla = ctx.getAsVariableArrayType(eltType))); 3272 return eltType; 3273 } 3274 3275 static Address emitArraySubscriptGEP(CodeGenFunction &CGF, Address addr, 3276 ArrayRef<llvm::Value *> indices, 3277 QualType eltType, bool inbounds, 3278 bool signedIndices, SourceLocation loc, 3279 const llvm::Twine &name = "arrayidx") { 3280 // All the indices except that last must be zero. 3281 #ifndef NDEBUG 3282 for (auto idx : indices.drop_back()) 3283 assert(isa<llvm::ConstantInt>(idx) && 3284 cast<llvm::ConstantInt>(idx)->isZero()); 3285 #endif 3286 3287 // Determine the element size of the statically-sized base. This is 3288 // the thing that the indices are expressed in terms of. 3289 if (auto vla = CGF.getContext().getAsVariableArrayType(eltType)) { 3290 eltType = getFixedSizeElementType(CGF.getContext(), vla); 3291 } 3292 3293 // We can use that to compute the best alignment of the element. 3294 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType); 3295 CharUnits eltAlign = 3296 getArrayElementAlign(addr.getAlignment(), indices.back(), eltSize); 3297 3298 llvm::Value *eltPtr = emitArraySubscriptGEP( 3299 CGF, addr.getPointer(), indices, inbounds, signedIndices, loc, name); 3300 return Address(eltPtr, eltAlign); 3301 } 3302 3303 LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E, 3304 bool Accessed) { 3305 // The index must always be an integer, which is not an aggregate. Emit it 3306 // in lexical order (this complexity is, sadly, required by C++17). 3307 llvm::Value *IdxPre = 3308 (E->getLHS() == E->getIdx()) ? EmitScalarExpr(E->getIdx()) : nullptr; 3309 bool SignedIndices = false; 3310 auto EmitIdxAfterBase = [&, IdxPre](bool Promote) -> llvm::Value * { 3311 auto *Idx = IdxPre; 3312 if (E->getLHS() != E->getIdx()) { 3313 assert(E->getRHS() == E->getIdx() && "index was neither LHS nor RHS"); 3314 Idx = EmitScalarExpr(E->getIdx()); 3315 } 3316 3317 QualType IdxTy = E->getIdx()->getType(); 3318 bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType(); 3319 SignedIndices |= IdxSigned; 3320 3321 if (SanOpts.has(SanitizerKind::ArrayBounds)) 3322 EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed); 3323 3324 // Extend or truncate the index type to 32 or 64-bits. 3325 if (Promote && Idx->getType() != IntPtrTy) 3326 Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom"); 3327 3328 return Idx; 3329 }; 3330 IdxPre = nullptr; 3331 3332 // If the base is a vector type, then we are forming a vector element lvalue 3333 // with this subscript. 3334 if (E->getBase()->getType()->isVectorType() && 3335 !isa<ExtVectorElementExpr>(E->getBase())) { 3336 // Emit the vector as an lvalue to get its address. 3337 LValue LHS = EmitLValue(E->getBase()); 3338 auto *Idx = EmitIdxAfterBase(/*Promote*/false); 3339 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!"); 3340 return LValue::MakeVectorElt(LHS.getAddress(), Idx, E->getBase()->getType(), 3341 LHS.getBaseInfo(), TBAAAccessInfo()); 3342 } 3343 3344 // All the other cases basically behave like simple offsetting. 3345 3346 // Handle the extvector case we ignored above. 3347 if (isa<ExtVectorElementExpr>(E->getBase())) { 3348 LValue LV = EmitLValue(E->getBase()); 3349 auto *Idx = EmitIdxAfterBase(/*Promote*/true); 3350 Address Addr = EmitExtVectorElementLValue(LV); 3351 3352 QualType EltType = LV.getType()->castAs<VectorType>()->getElementType(); 3353 Addr = emitArraySubscriptGEP(*this, Addr, Idx, EltType, /*inbounds*/ true, 3354 SignedIndices, E->getExprLoc()); 3355 return MakeAddrLValue(Addr, EltType, LV.getBaseInfo(), 3356 CGM.getTBAAInfoForSubobject(LV, EltType)); 3357 } 3358 3359 LValueBaseInfo EltBaseInfo; 3360 TBAAAccessInfo EltTBAAInfo; 3361 Address Addr = Address::invalid(); 3362 if (const VariableArrayType *vla = 3363 getContext().getAsVariableArrayType(E->getType())) { 3364 // The base must be a pointer, which is not an aggregate. Emit 3365 // it. It needs to be emitted first in case it's what captures 3366 // the VLA bounds. 3367 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo); 3368 auto *Idx = EmitIdxAfterBase(/*Promote*/true); 3369 3370 // The element count here is the total number of non-VLA elements. 3371 llvm::Value *numElements = getVLASize(vla).NumElts; 3372 3373 // Effectively, the multiply by the VLA size is part of the GEP. 3374 // GEP indexes are signed, and scaling an index isn't permitted to 3375 // signed-overflow, so we use the same semantics for our explicit 3376 // multiply. We suppress this if overflow is not undefined behavior. 3377 if (getLangOpts().isSignedOverflowDefined()) { 3378 Idx = Builder.CreateMul(Idx, numElements); 3379 } else { 3380 Idx = Builder.CreateNSWMul(Idx, numElements); 3381 } 3382 3383 Addr = emitArraySubscriptGEP(*this, Addr, Idx, vla->getElementType(), 3384 !getLangOpts().isSignedOverflowDefined(), 3385 SignedIndices, E->getExprLoc()); 3386 3387 } else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){ 3388 // Indexing over an interface, as in "NSString *P; P[4];" 3389 3390 // Emit the base pointer. 3391 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo); 3392 auto *Idx = EmitIdxAfterBase(/*Promote*/true); 3393 3394 CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT); 3395 llvm::Value *InterfaceSizeVal = 3396 llvm::ConstantInt::get(Idx->getType(), InterfaceSize.getQuantity()); 3397 3398 llvm::Value *ScaledIdx = Builder.CreateMul(Idx, InterfaceSizeVal); 3399 3400 // We don't necessarily build correct LLVM struct types for ObjC 3401 // interfaces, so we can't rely on GEP to do this scaling 3402 // correctly, so we need to cast to i8*. FIXME: is this actually 3403 // true? A lot of other things in the fragile ABI would break... 3404 llvm::Type *OrigBaseTy = Addr.getType(); 3405 Addr = Builder.CreateElementBitCast(Addr, Int8Ty); 3406 3407 // Do the GEP. 3408 CharUnits EltAlign = 3409 getArrayElementAlign(Addr.getAlignment(), Idx, InterfaceSize); 3410 llvm::Value *EltPtr = 3411 emitArraySubscriptGEP(*this, Addr.getPointer(), ScaledIdx, false, 3412 SignedIndices, E->getExprLoc()); 3413 Addr = Address(EltPtr, EltAlign); 3414 3415 // Cast back. 3416 Addr = Builder.CreateBitCast(Addr, OrigBaseTy); 3417 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { 3418 // If this is A[i] where A is an array, the frontend will have decayed the 3419 // base to be a ArrayToPointerDecay implicit cast. While correct, it is 3420 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a 3421 // "gep x, i" here. Emit one "gep A, 0, i". 3422 assert(Array->getType()->isArrayType() && 3423 "Array to pointer decay must have array source type!"); 3424 LValue ArrayLV; 3425 // For simple multidimensional array indexing, set the 'accessed' flag for 3426 // better bounds-checking of the base expression. 3427 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array)) 3428 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true); 3429 else 3430 ArrayLV = EmitLValue(Array); 3431 auto *Idx = EmitIdxAfterBase(/*Promote*/true); 3432 3433 // Propagate the alignment from the array itself to the result. 3434 Addr = emitArraySubscriptGEP( 3435 *this, ArrayLV.getAddress(), {CGM.getSize(CharUnits::Zero()), Idx}, 3436 E->getType(), !getLangOpts().isSignedOverflowDefined(), SignedIndices, 3437 E->getExprLoc()); 3438 EltBaseInfo = ArrayLV.getBaseInfo(); 3439 EltTBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, E->getType()); 3440 } else { 3441 // The base must be a pointer; emit it with an estimate of its alignment. 3442 Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo); 3443 auto *Idx = EmitIdxAfterBase(/*Promote*/true); 3444 Addr = emitArraySubscriptGEP(*this, Addr, Idx, E->getType(), 3445 !getLangOpts().isSignedOverflowDefined(), 3446 SignedIndices, E->getExprLoc()); 3447 } 3448 3449 LValue LV = MakeAddrLValue(Addr, E->getType(), EltBaseInfo, EltTBAAInfo); 3450 3451 if (getLangOpts().ObjC1 && 3452 getLangOpts().getGC() != LangOptions::NonGC) { 3453 LV.setNonGC(!E->isOBJCGCCandidate(getContext())); 3454 setObjCGCLValueClass(getContext(), E, LV); 3455 } 3456 return LV; 3457 } 3458 3459 static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, 3460 LValueBaseInfo &BaseInfo, 3461 TBAAAccessInfo &TBAAInfo, 3462 QualType BaseTy, QualType ElTy, 3463 bool IsLowerBound) { 3464 LValue BaseLVal; 3465 if (auto *ASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParenImpCasts())) { 3466 BaseLVal = CGF.EmitOMPArraySectionExpr(ASE, IsLowerBound); 3467 if (BaseTy->isArrayType()) { 3468 Address Addr = BaseLVal.getAddress(); 3469 BaseInfo = BaseLVal.getBaseInfo(); 3470 3471 // If the array type was an incomplete type, we need to make sure 3472 // the decay ends up being the right type. 3473 llvm::Type *NewTy = CGF.ConvertType(BaseTy); 3474 Addr = CGF.Builder.CreateElementBitCast(Addr, NewTy); 3475 3476 // Note that VLA pointers are always decayed, so we don't need to do 3477 // anything here. 3478 if (!BaseTy->isVariableArrayType()) { 3479 assert(isa<llvm::ArrayType>(Addr.getElementType()) && 3480 "Expected pointer to array"); 3481 Addr = CGF.Builder.CreateStructGEP(Addr, 0, CharUnits::Zero(), 3482 "arraydecay"); 3483 } 3484 3485 return CGF.Builder.CreateElementBitCast(Addr, 3486 CGF.ConvertTypeForMem(ElTy)); 3487 } 3488 LValueBaseInfo TypeBaseInfo; 3489 TBAAAccessInfo TypeTBAAInfo; 3490 CharUnits Align = CGF.getNaturalTypeAlignment(ElTy, &TypeBaseInfo, 3491 &TypeTBAAInfo); 3492 BaseInfo.mergeForCast(TypeBaseInfo); 3493 TBAAInfo = CGF.CGM.mergeTBAAInfoForCast(TBAAInfo, TypeTBAAInfo); 3494 return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress()), Align); 3495 } 3496 return CGF.EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo); 3497 } 3498 3499 LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, 3500 bool IsLowerBound) { 3501 QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType(E->getBase()); 3502 QualType ResultExprTy; 3503 if (auto *AT = getContext().getAsArrayType(BaseTy)) 3504 ResultExprTy = AT->getElementType(); 3505 else 3506 ResultExprTy = BaseTy->getPointeeType(); 3507 llvm::Value *Idx = nullptr; 3508 if (IsLowerBound || E->getColonLoc().isInvalid()) { 3509 // Requesting lower bound or upper bound, but without provided length and 3510 // without ':' symbol for the default length -> length = 1. 3511 // Idx = LowerBound ?: 0; 3512 if (auto *LowerBound = E->getLowerBound()) { 3513 Idx = Builder.CreateIntCast( 3514 EmitScalarExpr(LowerBound), IntPtrTy, 3515 LowerBound->getType()->hasSignedIntegerRepresentation()); 3516 } else 3517 Idx = llvm::ConstantInt::getNullValue(IntPtrTy); 3518 } else { 3519 // Try to emit length or lower bound as constant. If this is possible, 1 3520 // is subtracted from constant length or lower bound. Otherwise, emit LLVM 3521 // IR (LB + Len) - 1. 3522 auto &C = CGM.getContext(); 3523 auto *Length = E->getLength(); 3524 llvm::APSInt ConstLength; 3525 if (Length) { 3526 // Idx = LowerBound + Length - 1; 3527 if (Length->isIntegerConstantExpr(ConstLength, C)) { 3528 ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); 3529 Length = nullptr; 3530 } 3531 auto *LowerBound = E->getLowerBound(); 3532 llvm::APSInt ConstLowerBound(PointerWidthInBits, /*isUnsigned=*/false); 3533 if (LowerBound && LowerBound->isIntegerConstantExpr(ConstLowerBound, C)) { 3534 ConstLowerBound = ConstLowerBound.zextOrTrunc(PointerWidthInBits); 3535 LowerBound = nullptr; 3536 } 3537 if (!Length) 3538 --ConstLength; 3539 else if (!LowerBound) 3540 --ConstLowerBound; 3541 3542 if (Length || LowerBound) { 3543 auto *LowerBoundVal = 3544 LowerBound 3545 ? Builder.CreateIntCast( 3546 EmitScalarExpr(LowerBound), IntPtrTy, 3547 LowerBound->getType()->hasSignedIntegerRepresentation()) 3548 : llvm::ConstantInt::get(IntPtrTy, ConstLowerBound); 3549 auto *LengthVal = 3550 Length 3551 ? Builder.CreateIntCast( 3552 EmitScalarExpr(Length), IntPtrTy, 3553 Length->getType()->hasSignedIntegerRepresentation()) 3554 : llvm::ConstantInt::get(IntPtrTy, ConstLength); 3555 Idx = Builder.CreateAdd(LowerBoundVal, LengthVal, "lb_add_len", 3556 /*HasNUW=*/false, 3557 !getLangOpts().isSignedOverflowDefined()); 3558 if (Length && LowerBound) { 3559 Idx = Builder.CreateSub( 3560 Idx, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "idx_sub_1", 3561 /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined()); 3562 } 3563 } else 3564 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength + ConstLowerBound); 3565 } else { 3566 // Idx = ArraySize - 1; 3567 QualType ArrayTy = BaseTy->isPointerType() 3568 ? E->getBase()->IgnoreParenImpCasts()->getType() 3569 : BaseTy; 3570 if (auto *VAT = C.getAsVariableArrayType(ArrayTy)) { 3571 Length = VAT->getSizeExpr(); 3572 if (Length->isIntegerConstantExpr(ConstLength, C)) 3573 Length = nullptr; 3574 } else { 3575 auto *CAT = C.getAsConstantArrayType(ArrayTy); 3576 ConstLength = CAT->getSize(); 3577 } 3578 if (Length) { 3579 auto *LengthVal = Builder.CreateIntCast( 3580 EmitScalarExpr(Length), IntPtrTy, 3581 Length->getType()->hasSignedIntegerRepresentation()); 3582 Idx = Builder.CreateSub( 3583 LengthVal, llvm::ConstantInt::get(IntPtrTy, /*V=*/1), "len_sub_1", 3584 /*HasNUW=*/false, !getLangOpts().isSignedOverflowDefined()); 3585 } else { 3586 ConstLength = ConstLength.zextOrTrunc(PointerWidthInBits); 3587 --ConstLength; 3588 Idx = llvm::ConstantInt::get(IntPtrTy, ConstLength); 3589 } 3590 } 3591 } 3592 assert(Idx); 3593 3594 Address EltPtr = Address::invalid(); 3595 LValueBaseInfo BaseInfo; 3596 TBAAAccessInfo TBAAInfo; 3597 if (auto *VLA = getContext().getAsVariableArrayType(ResultExprTy)) { 3598 // The base must be a pointer, which is not an aggregate. Emit 3599 // it. It needs to be emitted first in case it's what captures 3600 // the VLA bounds. 3601 Address Base = 3602 emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, TBAAInfo, 3603 BaseTy, VLA->getElementType(), IsLowerBound); 3604 // The element count here is the total number of non-VLA elements. 3605 llvm::Value *NumElements = getVLASize(VLA).NumElts; 3606 3607 // Effectively, the multiply by the VLA size is part of the GEP. 3608 // GEP indexes are signed, and scaling an index isn't permitted to 3609 // signed-overflow, so we use the same semantics for our explicit 3610 // multiply. We suppress this if overflow is not undefined behavior. 3611 if (getLangOpts().isSignedOverflowDefined()) 3612 Idx = Builder.CreateMul(Idx, NumElements); 3613 else 3614 Idx = Builder.CreateNSWMul(Idx, NumElements); 3615 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, VLA->getElementType(), 3616 !getLangOpts().isSignedOverflowDefined(), 3617 /*SignedIndices=*/false, E->getExprLoc()); 3618 } else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) { 3619 // If this is A[i] where A is an array, the frontend will have decayed the 3620 // base to be a ArrayToPointerDecay implicit cast. While correct, it is 3621 // inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a 3622 // "gep x, i" here. Emit one "gep A, 0, i". 3623 assert(Array->getType()->isArrayType() && 3624 "Array to pointer decay must have array source type!"); 3625 LValue ArrayLV; 3626 // For simple multidimensional array indexing, set the 'accessed' flag for 3627 // better bounds-checking of the base expression. 3628 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array)) 3629 ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true); 3630 else 3631 ArrayLV = EmitLValue(Array); 3632 3633 // Propagate the alignment from the array itself to the result. 3634 EltPtr = emitArraySubscriptGEP( 3635 *this, ArrayLV.getAddress(), {CGM.getSize(CharUnits::Zero()), Idx}, 3636 ResultExprTy, !getLangOpts().isSignedOverflowDefined(), 3637 /*SignedIndices=*/false, E->getExprLoc()); 3638 BaseInfo = ArrayLV.getBaseInfo(); 3639 TBAAInfo = CGM.getTBAAInfoForSubobject(ArrayLV, ResultExprTy); 3640 } else { 3641 Address Base = emitOMPArraySectionBase(*this, E->getBase(), BaseInfo, 3642 TBAAInfo, BaseTy, ResultExprTy, 3643 IsLowerBound); 3644 EltPtr = emitArraySubscriptGEP(*this, Base, Idx, ResultExprTy, 3645 !getLangOpts().isSignedOverflowDefined(), 3646 /*SignedIndices=*/false, E->getExprLoc()); 3647 } 3648 3649 return MakeAddrLValue(EltPtr, ResultExprTy, BaseInfo, TBAAInfo); 3650 } 3651 3652 LValue CodeGenFunction:: 3653 EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { 3654 // Emit the base vector as an l-value. 3655 LValue Base; 3656 3657 // ExtVectorElementExpr's base can either be a vector or pointer to vector. 3658 if (E->isArrow()) { 3659 // If it is a pointer to a vector, emit the address and form an lvalue with 3660 // it. 3661 LValueBaseInfo BaseInfo; 3662 TBAAAccessInfo TBAAInfo; 3663 Address Ptr = EmitPointerWithAlignment(E->getBase(), &BaseInfo, &TBAAInfo); 3664 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>(); 3665 Base = MakeAddrLValue(Ptr, PT->getPointeeType(), BaseInfo, TBAAInfo); 3666 Base.getQuals().removeObjCGCAttr(); 3667 } else if (E->getBase()->isGLValue()) { 3668 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x), 3669 // emit the base as an lvalue. 3670 assert(E->getBase()->getType()->isVectorType()); 3671 Base = EmitLValue(E->getBase()); 3672 } else { 3673 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such. 3674 assert(E->getBase()->getType()->isVectorType() && 3675 "Result must be a vector"); 3676 llvm::Value *Vec = EmitScalarExpr(E->getBase()); 3677 3678 // Store the vector to memory (because LValue wants an address). 3679 Address VecMem = CreateMemTemp(E->getBase()->getType()); 3680 Builder.CreateStore(Vec, VecMem); 3681 Base = MakeAddrLValue(VecMem, E->getBase()->getType(), 3682 AlignmentSource::Decl); 3683 } 3684 3685 QualType type = 3686 E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers()); 3687 3688 // Encode the element access list into a vector of unsigned indices. 3689 SmallVector<uint32_t, 4> Indices; 3690 E->getEncodedElementAccess(Indices); 3691 3692 if (Base.isSimple()) { 3693 llvm::Constant *CV = 3694 llvm::ConstantDataVector::get(getLLVMContext(), Indices); 3695 return LValue::MakeExtVectorElt(Base.getAddress(), CV, type, 3696 Base.getBaseInfo(), TBAAAccessInfo()); 3697 } 3698 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!"); 3699 3700 llvm::Constant *BaseElts = Base.getExtVectorElts(); 3701 SmallVector<llvm::Constant *, 4> CElts; 3702 3703 for (unsigned i = 0, e = Indices.size(); i != e; ++i) 3704 CElts.push_back(BaseElts->getAggregateElement(Indices[i])); 3705 llvm::Constant *CV = llvm::ConstantVector::get(CElts); 3706 return LValue::MakeExtVectorElt(Base.getExtVectorAddress(), CV, type, 3707 Base.getBaseInfo(), TBAAAccessInfo()); 3708 } 3709 3710 LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { 3711 if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, E)) { 3712 EmitIgnoredExpr(E->getBase()); 3713 return EmitDeclRefLValue(DRE); 3714 } 3715 3716 Expr *BaseExpr = E->getBase(); 3717 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. 3718 LValue BaseLV; 3719 if (E->isArrow()) { 3720 LValueBaseInfo BaseInfo; 3721 TBAAAccessInfo TBAAInfo; 3722 Address Addr = EmitPointerWithAlignment(BaseExpr, &BaseInfo, &TBAAInfo); 3723 QualType PtrTy = BaseExpr->getType()->getPointeeType(); 3724 SanitizerSet SkippedChecks; 3725 bool IsBaseCXXThis = IsWrappedCXXThis(BaseExpr); 3726 if (IsBaseCXXThis) 3727 SkippedChecks.set(SanitizerKind::Alignment, true); 3728 if (IsBaseCXXThis || isa<DeclRefExpr>(BaseExpr)) 3729 SkippedChecks.set(SanitizerKind::Null, true); 3730 EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Addr.getPointer(), PtrTy, 3731 /*Alignment=*/CharUnits::Zero(), SkippedChecks); 3732 BaseLV = MakeAddrLValue(Addr, PtrTy, BaseInfo, TBAAInfo); 3733 } else 3734 BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess); 3735 3736 NamedDecl *ND = E->getMemberDecl(); 3737 if (auto *Field = dyn_cast<FieldDecl>(ND)) { 3738 LValue LV = EmitLValueForField(BaseLV, Field); 3739 setObjCGCLValueClass(getContext(), E, LV); 3740 return LV; 3741 } 3742 3743 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) 3744 return EmitFunctionDeclLValue(*this, E, FD); 3745 3746 llvm_unreachable("Unhandled member declaration!"); 3747 } 3748 3749 /// Given that we are currently emitting a lambda, emit an l-value for 3750 /// one of its members. 3751 LValue CodeGenFunction::EmitLValueForLambdaField(const FieldDecl *Field) { 3752 assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda()); 3753 assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent()); 3754 QualType LambdaTagType = 3755 getContext().getTagDeclType(Field->getParent()); 3756 LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType); 3757 return EmitLValueForField(LambdaLV, Field); 3758 } 3759 3760 /// Drill down to the storage of a field without walking into 3761 /// reference types. 3762 /// 3763 /// The resulting address doesn't necessarily have the right type. 3764 static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, 3765 const FieldDecl *field) { 3766 const RecordDecl *rec = field->getParent(); 3767 3768 unsigned idx = 3769 CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field); 3770 3771 CharUnits offset; 3772 // Adjust the alignment down to the given offset. 3773 // As a special case, if the LLVM field index is 0, we know that this 3774 // is zero. 3775 assert((idx != 0 || CGF.getContext().getASTRecordLayout(rec) 3776 .getFieldOffset(field->getFieldIndex()) == 0) && 3777 "LLVM field at index zero had non-zero offset?"); 3778 if (idx != 0) { 3779 auto &recLayout = CGF.getContext().getASTRecordLayout(rec); 3780 auto offsetInBits = recLayout.getFieldOffset(field->getFieldIndex()); 3781 offset = CGF.getContext().toCharUnitsFromBits(offsetInBits); 3782 } 3783 3784 return CGF.Builder.CreateStructGEP(base, idx, offset, field->getName()); 3785 } 3786 3787 static bool hasAnyVptr(const QualType Type, const ASTContext &Context) { 3788 const auto *RD = Type.getTypePtr()->getAsCXXRecordDecl(); 3789 if (!RD) 3790 return false; 3791 3792 if (RD->isDynamicClass()) 3793 return true; 3794 3795 for (const auto &Base : RD->bases()) 3796 if (hasAnyVptr(Base.getType(), Context)) 3797 return true; 3798 3799 for (const FieldDecl *Field : RD->fields()) 3800 if (hasAnyVptr(Field->getType(), Context)) 3801 return true; 3802 3803 return false; 3804 } 3805 3806 LValue CodeGenFunction::EmitLValueForField(LValue base, 3807 const FieldDecl *field) { 3808 LValueBaseInfo BaseInfo = base.getBaseInfo(); 3809 3810 if (field->isBitField()) { 3811 const CGRecordLayout &RL = 3812 CGM.getTypes().getCGRecordLayout(field->getParent()); 3813 const CGBitFieldInfo &Info = RL.getBitFieldInfo(field); 3814 Address Addr = base.getAddress(); 3815 unsigned Idx = RL.getLLVMFieldNo(field); 3816 if (Idx != 0) 3817 // For structs, we GEP to the field that the record layout suggests. 3818 Addr = Builder.CreateStructGEP(Addr, Idx, Info.StorageOffset, 3819 field->getName()); 3820 // Get the access type. 3821 llvm::Type *FieldIntTy = 3822 llvm::Type::getIntNTy(getLLVMContext(), Info.StorageSize); 3823 if (Addr.getElementType() != FieldIntTy) 3824 Addr = Builder.CreateElementBitCast(Addr, FieldIntTy); 3825 3826 QualType fieldType = 3827 field->getType().withCVRQualifiers(base.getVRQualifiers()); 3828 // TODO: Support TBAA for bit fields. 3829 LValueBaseInfo FieldBaseInfo(BaseInfo.getAlignmentSource()); 3830 return LValue::MakeBitfield(Addr, Info, fieldType, FieldBaseInfo, 3831 TBAAAccessInfo()); 3832 } 3833 3834 // Fields of may-alias structures are may-alias themselves. 3835 // FIXME: this should get propagated down through anonymous structs 3836 // and unions. 3837 QualType FieldType = field->getType(); 3838 const RecordDecl *rec = field->getParent(); 3839 AlignmentSource BaseAlignSource = BaseInfo.getAlignmentSource(); 3840 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(BaseAlignSource)); 3841 TBAAAccessInfo FieldTBAAInfo; 3842 if (base.getTBAAInfo().isMayAlias() || 3843 rec->hasAttr<MayAliasAttr>() || FieldType->isVectorType()) { 3844 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo(); 3845 } else if (rec->isUnion()) { 3846 // TODO: Support TBAA for unions. 3847 FieldTBAAInfo = TBAAAccessInfo::getMayAliasInfo(); 3848 } else { 3849 // If no base type been assigned for the base access, then try to generate 3850 // one for this base lvalue. 3851 FieldTBAAInfo = base.getTBAAInfo(); 3852 if (!FieldTBAAInfo.BaseType) { 3853 FieldTBAAInfo.BaseType = CGM.getTBAABaseTypeInfo(base.getType()); 3854 assert(!FieldTBAAInfo.Offset && 3855 "Nonzero offset for an access with no base type!"); 3856 } 3857 3858 // Adjust offset to be relative to the base type. 3859 const ASTRecordLayout &Layout = 3860 getContext().getASTRecordLayout(field->getParent()); 3861 unsigned CharWidth = getContext().getCharWidth(); 3862 if (FieldTBAAInfo.BaseType) 3863 FieldTBAAInfo.Offset += 3864 Layout.getFieldOffset(field->getFieldIndex()) / CharWidth; 3865 3866 // Update the final access type and size. 3867 FieldTBAAInfo.AccessType = CGM.getTBAATypeInfo(FieldType); 3868 FieldTBAAInfo.Size = 3869 getContext().getTypeSizeInChars(FieldType).getQuantity(); 3870 } 3871 3872 Address addr = base.getAddress(); 3873 unsigned RecordCVR = base.getVRQualifiers(); 3874 if (rec->isUnion()) { 3875 // For unions, there is no pointer adjustment. 3876 assert(!FieldType->isReferenceType() && "union has reference member"); 3877 if (CGM.getCodeGenOpts().StrictVTablePointers && 3878 hasAnyVptr(FieldType, getContext())) 3879 // Because unions can easily skip invariant.barriers, we need to add 3880 // a barrier every time CXXRecord field with vptr is referenced. 3881 addr = Address(Builder.CreateLaunderInvariantGroup(addr.getPointer()), 3882 addr.getAlignment()); 3883 } else { 3884 // For structs, we GEP to the field that the record layout suggests. 3885 addr = emitAddrOfFieldStorage(*this, addr, field); 3886 3887 // If this is a reference field, load the reference right now. 3888 if (FieldType->isReferenceType()) { 3889 LValue RefLVal = MakeAddrLValue(addr, FieldType, FieldBaseInfo, 3890 FieldTBAAInfo); 3891 if (RecordCVR & Qualifiers::Volatile) 3892 RefLVal.getQuals().setVolatile(true); 3893 addr = EmitLoadOfReference(RefLVal, &FieldBaseInfo, &FieldTBAAInfo); 3894 3895 // Qualifiers on the struct don't apply to the referencee. 3896 RecordCVR = 0; 3897 FieldType = FieldType->getPointeeType(); 3898 } 3899 } 3900 3901 // Make sure that the address is pointing to the right type. This is critical 3902 // for both unions and structs. A union needs a bitcast, a struct element 3903 // will need a bitcast if the LLVM type laid out doesn't match the desired 3904 // type. 3905 addr = Builder.CreateElementBitCast( 3906 addr, CGM.getTypes().ConvertTypeForMem(FieldType), field->getName()); 3907 3908 if (field->hasAttr<AnnotateAttr>()) 3909 addr = EmitFieldAnnotations(field, addr); 3910 3911 LValue LV = MakeAddrLValue(addr, FieldType, FieldBaseInfo, FieldTBAAInfo); 3912 LV.getQuals().addCVRQualifiers(RecordCVR); 3913 3914 // __weak attribute on a field is ignored. 3915 if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak) 3916 LV.getQuals().removeObjCGCAttr(); 3917 3918 return LV; 3919 } 3920 3921 LValue 3922 CodeGenFunction::EmitLValueForFieldInitialization(LValue Base, 3923 const FieldDecl *Field) { 3924 QualType FieldType = Field->getType(); 3925 3926 if (!FieldType->isReferenceType()) 3927 return EmitLValueForField(Base, Field); 3928 3929 Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field); 3930 3931 // Make sure that the address is pointing to the right type. 3932 llvm::Type *llvmType = ConvertTypeForMem(FieldType); 3933 V = Builder.CreateElementBitCast(V, llvmType, Field->getName()); 3934 3935 // TODO: Generate TBAA information that describes this access as a structure 3936 // member access and not just an access to an object of the field's type. This 3937 // should be similar to what we do in EmitLValueForField(). 3938 LValueBaseInfo BaseInfo = Base.getBaseInfo(); 3939 AlignmentSource FieldAlignSource = BaseInfo.getAlignmentSource(); 3940 LValueBaseInfo FieldBaseInfo(getFieldAlignmentSource(FieldAlignSource)); 3941 return MakeAddrLValue(V, FieldType, FieldBaseInfo, 3942 CGM.getTBAAInfoForSubobject(Base, FieldType)); 3943 } 3944 3945 LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){ 3946 if (E->isFileScope()) { 3947 ConstantAddress GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E); 3948 return MakeAddrLValue(GlobalPtr, E->getType(), AlignmentSource::Decl); 3949 } 3950 if (E->getType()->isVariablyModifiedType()) 3951 // make sure to emit the VLA size. 3952 EmitVariablyModifiedType(E->getType()); 3953 3954 Address DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral"); 3955 const Expr *InitExpr = E->getInitializer(); 3956 LValue Result = MakeAddrLValue(DeclPtr, E->getType(), AlignmentSource::Decl); 3957 3958 EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(), 3959 /*Init*/ true); 3960 3961 return Result; 3962 } 3963 3964 LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) { 3965 if (!E->isGLValue()) 3966 // Initializing an aggregate temporary in C++11: T{...}. 3967 return EmitAggExprToLValue(E); 3968 3969 // An lvalue initializer list must be initializing a reference. 3970 assert(E->isTransparent() && "non-transparent glvalue init list"); 3971 return EmitLValue(E->getInit(0)); 3972 } 3973 3974 /// Emit the operand of a glvalue conditional operator. This is either a glvalue 3975 /// or a (possibly-parenthesized) throw-expression. If this is a throw, no 3976 /// LValue is returned and the current block has been terminated. 3977 static Optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF, 3978 const Expr *Operand) { 3979 if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) { 3980 CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false); 3981 return None; 3982 } 3983 3984 return CGF.EmitLValue(Operand); 3985 } 3986 3987 LValue CodeGenFunction:: 3988 EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) { 3989 if (!expr->isGLValue()) { 3990 // ?: here should be an aggregate. 3991 assert(hasAggregateEvaluationKind(expr->getType()) && 3992 "Unexpected conditional operator!"); 3993 return EmitAggExprToLValue(expr); 3994 } 3995 3996 OpaqueValueMapping binding(*this, expr); 3997 3998 const Expr *condExpr = expr->getCond(); 3999 bool CondExprBool; 4000 if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { 4001 const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr(); 4002 if (!CondExprBool) std::swap(live, dead); 4003 4004 if (!ContainsLabel(dead)) { 4005 // If the true case is live, we need to track its region. 4006 if (CondExprBool) 4007 incrementProfileCounter(expr); 4008 return EmitLValue(live); 4009 } 4010 } 4011 4012 llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true"); 4013 llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false"); 4014 llvm::BasicBlock *contBlock = createBasicBlock("cond.end"); 4015 4016 ConditionalEvaluation eval(*this); 4017 EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock, getProfileCount(expr)); 4018 4019 // Any temporaries created here are conditional. 4020 EmitBlock(lhsBlock); 4021 incrementProfileCounter(expr); 4022 eval.begin(*this); 4023 Optional<LValue> lhs = 4024 EmitLValueOrThrowExpression(*this, expr->getTrueExpr()); 4025 eval.end(*this); 4026 4027 if (lhs && !lhs->isSimple()) 4028 return EmitUnsupportedLValue(expr, "conditional operator"); 4029 4030 lhsBlock = Builder.GetInsertBlock(); 4031 if (lhs) 4032 Builder.CreateBr(contBlock); 4033 4034 // Any temporaries created here are conditional. 4035 EmitBlock(rhsBlock); 4036 eval.begin(*this); 4037 Optional<LValue> rhs = 4038 EmitLValueOrThrowExpression(*this, expr->getFalseExpr()); 4039 eval.end(*this); 4040 if (rhs && !rhs->isSimple()) 4041 return EmitUnsupportedLValue(expr, "conditional operator"); 4042 rhsBlock = Builder.GetInsertBlock(); 4043 4044 EmitBlock(contBlock); 4045 4046 if (lhs && rhs) { 4047 llvm::PHINode *phi = Builder.CreatePHI(lhs->getPointer()->getType(), 4048 2, "cond-lvalue"); 4049 phi->addIncoming(lhs->getPointer(), lhsBlock); 4050 phi->addIncoming(rhs->getPointer(), rhsBlock); 4051 Address result(phi, std::min(lhs->getAlignment(), rhs->getAlignment())); 4052 AlignmentSource alignSource = 4053 std::max(lhs->getBaseInfo().getAlignmentSource(), 4054 rhs->getBaseInfo().getAlignmentSource()); 4055 TBAAAccessInfo TBAAInfo = CGM.mergeTBAAInfoForConditionalOperator( 4056 lhs->getTBAAInfo(), rhs->getTBAAInfo()); 4057 return MakeAddrLValue(result, expr->getType(), LValueBaseInfo(alignSource), 4058 TBAAInfo); 4059 } else { 4060 assert((lhs || rhs) && 4061 "both operands of glvalue conditional are throw-expressions?"); 4062 return lhs ? *lhs : *rhs; 4063 } 4064 } 4065 4066 /// EmitCastLValue - Casts are never lvalues unless that cast is to a reference 4067 /// type. If the cast is to a reference, we can have the usual lvalue result, 4068 /// otherwise if a cast is needed by the code generator in an lvalue context, 4069 /// then it must mean that we need the address of an aggregate in order to 4070 /// access one of its members. This can happen for all the reasons that casts 4071 /// are permitted with aggregate result, including noop aggregate casts, and 4072 /// cast from scalar to union. 4073 LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { 4074 switch (E->getCastKind()) { 4075 case CK_ToVoid: 4076 case CK_BitCast: 4077 case CK_ArrayToPointerDecay: 4078 case CK_FunctionToPointerDecay: 4079 case CK_NullToMemberPointer: 4080 case CK_NullToPointer: 4081 case CK_IntegralToPointer: 4082 case CK_PointerToIntegral: 4083 case CK_PointerToBoolean: 4084 case CK_VectorSplat: 4085 case CK_IntegralCast: 4086 case CK_BooleanToSignedIntegral: 4087 case CK_IntegralToBoolean: 4088 case CK_IntegralToFloating: 4089 case CK_FloatingToIntegral: 4090 case CK_FloatingToBoolean: 4091 case CK_FloatingCast: 4092 case CK_FloatingRealToComplex: 4093 case CK_FloatingComplexToReal: 4094 case CK_FloatingComplexToBoolean: 4095 case CK_FloatingComplexCast: 4096 case CK_FloatingComplexToIntegralComplex: 4097 case CK_IntegralRealToComplex: 4098 case CK_IntegralComplexToReal: 4099 case CK_IntegralComplexToBoolean: 4100 case CK_IntegralComplexCast: 4101 case CK_IntegralComplexToFloatingComplex: 4102 case CK_DerivedToBaseMemberPointer: 4103 case CK_BaseToDerivedMemberPointer: 4104 case CK_MemberPointerToBoolean: 4105 case CK_ReinterpretMemberPointer: 4106 case CK_AnyPointerToBlockPointerCast: 4107 case CK_ARCProduceObject: 4108 case CK_ARCConsumeObject: 4109 case CK_ARCReclaimReturnedObject: 4110 case CK_ARCExtendBlockObject: 4111 case CK_CopyAndAutoreleaseBlockObject: 4112 case CK_AddressSpaceConversion: 4113 case CK_IntToOCLSampler: 4114 return EmitUnsupportedLValue(E, "unexpected cast lvalue"); 4115 4116 case CK_Dependent: 4117 llvm_unreachable("dependent cast kind in IR gen!"); 4118 4119 case CK_BuiltinFnToFnPtr: 4120 llvm_unreachable("builtin functions are handled elsewhere"); 4121 4122 // These are never l-values; just use the aggregate emission code. 4123 case CK_NonAtomicToAtomic: 4124 case CK_AtomicToNonAtomic: 4125 return EmitAggExprToLValue(E); 4126 4127 case CK_Dynamic: { 4128 LValue LV = EmitLValue(E->getSubExpr()); 4129 Address V = LV.getAddress(); 4130 const auto *DCE = cast<CXXDynamicCastExpr>(E); 4131 return MakeNaturalAlignAddrLValue(EmitDynamicCast(V, DCE), E->getType()); 4132 } 4133 4134 case CK_ConstructorConversion: 4135 case CK_UserDefinedConversion: 4136 case CK_CPointerToObjCPointerCast: 4137 case CK_BlockPointerToObjCPointerCast: 4138 case CK_NoOp: 4139 case CK_LValueToRValue: 4140 return EmitLValue(E->getSubExpr()); 4141 4142 case CK_UncheckedDerivedToBase: 4143 case CK_DerivedToBase: { 4144 const RecordType *DerivedClassTy = 4145 E->getSubExpr()->getType()->getAs<RecordType>(); 4146 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 4147 4148 LValue LV = EmitLValue(E->getSubExpr()); 4149 Address This = LV.getAddress(); 4150 4151 // Perform the derived-to-base conversion 4152 Address Base = GetAddressOfBaseClass( 4153 This, DerivedClassDecl, E->path_begin(), E->path_end(), 4154 /*NullCheckValue=*/false, E->getExprLoc()); 4155 4156 // TODO: Support accesses to members of base classes in TBAA. For now, we 4157 // conservatively pretend that the complete object is of the base class 4158 // type. 4159 return MakeAddrLValue(Base, E->getType(), LV.getBaseInfo(), 4160 CGM.getTBAAInfoForSubobject(LV, E->getType())); 4161 } 4162 case CK_ToUnion: 4163 return EmitAggExprToLValue(E); 4164 case CK_BaseToDerived: { 4165 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>(); 4166 auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 4167 4168 LValue LV = EmitLValue(E->getSubExpr()); 4169 4170 // Perform the base-to-derived conversion 4171 Address Derived = 4172 GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl, 4173 E->path_begin(), E->path_end(), 4174 /*NullCheckValue=*/false); 4175 4176 // C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is 4177 // performed and the object is not of the derived type. 4178 if (sanitizePerformTypeCheck()) 4179 EmitTypeCheck(TCK_DowncastReference, E->getExprLoc(), 4180 Derived.getPointer(), E->getType()); 4181 4182 if (SanOpts.has(SanitizerKind::CFIDerivedCast)) 4183 EmitVTablePtrCheckForCast(E->getType(), Derived.getPointer(), 4184 /*MayBeNull=*/false, 4185 CFITCK_DerivedCast, E->getLocStart()); 4186 4187 return MakeAddrLValue(Derived, E->getType(), LV.getBaseInfo(), 4188 CGM.getTBAAInfoForSubobject(LV, E->getType())); 4189 } 4190 case CK_LValueBitCast: { 4191 // This must be a reinterpret_cast (or c-style equivalent). 4192 const auto *CE = cast<ExplicitCastExpr>(E); 4193 4194 CGM.EmitExplicitCastExprType(CE, this); 4195 LValue LV = EmitLValue(E->getSubExpr()); 4196 Address V = Builder.CreateBitCast(LV.getAddress(), 4197 ConvertType(CE->getTypeAsWritten())); 4198 4199 if (SanOpts.has(SanitizerKind::CFIUnrelatedCast)) 4200 EmitVTablePtrCheckForCast(E->getType(), V.getPointer(), 4201 /*MayBeNull=*/false, 4202 CFITCK_UnrelatedCast, E->getLocStart()); 4203 4204 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(), 4205 CGM.getTBAAInfoForSubobject(LV, E->getType())); 4206 } 4207 case CK_ObjCObjectLValueCast: { 4208 LValue LV = EmitLValue(E->getSubExpr()); 4209 Address V = Builder.CreateElementBitCast(LV.getAddress(), 4210 ConvertType(E->getType())); 4211 return MakeAddrLValue(V, E->getType(), LV.getBaseInfo(), 4212 CGM.getTBAAInfoForSubobject(LV, E->getType())); 4213 } 4214 case CK_ZeroToOCLQueue: 4215 llvm_unreachable("NULL to OpenCL queue lvalue cast is not valid"); 4216 case CK_ZeroToOCLEvent: 4217 llvm_unreachable("NULL to OpenCL event lvalue cast is not valid"); 4218 } 4219 4220 llvm_unreachable("Unhandled lvalue cast kind?"); 4221 } 4222 4223 LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) { 4224 assert(OpaqueValueMappingData::shouldBindAsLValue(e)); 4225 return getOrCreateOpaqueLValueMapping(e); 4226 } 4227 4228 LValue 4229 CodeGenFunction::getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e) { 4230 assert(OpaqueValueMapping::shouldBindAsLValue(e)); 4231 4232 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator 4233 it = OpaqueLValues.find(e); 4234 4235 if (it != OpaqueLValues.end()) 4236 return it->second; 4237 4238 assert(e->isUnique() && "LValue for a nonunique OVE hasn't been emitted"); 4239 return EmitLValue(e->getSourceExpr()); 4240 } 4241 4242 RValue 4243 CodeGenFunction::getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e) { 4244 assert(!OpaqueValueMapping::shouldBindAsLValue(e)); 4245 4246 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator 4247 it = OpaqueRValues.find(e); 4248 4249 if (it != OpaqueRValues.end()) 4250 return it->second; 4251 4252 assert(e->isUnique() && "RValue for a nonunique OVE hasn't been emitted"); 4253 return EmitAnyExpr(e->getSourceExpr()); 4254 } 4255 4256 RValue CodeGenFunction::EmitRValueForField(LValue LV, 4257 const FieldDecl *FD, 4258 SourceLocation Loc) { 4259 QualType FT = FD->getType(); 4260 LValue FieldLV = EmitLValueForField(LV, FD); 4261 switch (getEvaluationKind(FT)) { 4262 case TEK_Complex: 4263 return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc)); 4264 case TEK_Aggregate: 4265 return FieldLV.asAggregateRValue(); 4266 case TEK_Scalar: 4267 // This routine is used to load fields one-by-one to perform a copy, so 4268 // don't load reference fields. 4269 if (FD->getType()->isReferenceType()) 4270 return RValue::get(FieldLV.getPointer()); 4271 return EmitLoadOfLValue(FieldLV, Loc); 4272 } 4273 llvm_unreachable("bad evaluation kind"); 4274 } 4275 4276 //===--------------------------------------------------------------------===// 4277 // Expression Emission 4278 //===--------------------------------------------------------------------===// 4279 4280 RValue CodeGenFunction::EmitCallExpr(const CallExpr *E, 4281 ReturnValueSlot ReturnValue) { 4282 // Builtins never have block type. 4283 if (E->getCallee()->getType()->isBlockPointerType()) 4284 return EmitBlockCallExpr(E, ReturnValue); 4285 4286 if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E)) 4287 return EmitCXXMemberCallExpr(CE, ReturnValue); 4288 4289 if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E)) 4290 return EmitCUDAKernelCallExpr(CE, ReturnValue); 4291 4292 if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E)) 4293 if (const CXXMethodDecl *MD = 4294 dyn_cast_or_null<CXXMethodDecl>(CE->getCalleeDecl())) 4295 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue); 4296 4297 CGCallee callee = EmitCallee(E->getCallee()); 4298 4299 if (callee.isBuiltin()) { 4300 return EmitBuiltinExpr(callee.getBuiltinDecl(), callee.getBuiltinID(), 4301 E, ReturnValue); 4302 } 4303 4304 if (callee.isPseudoDestructor()) { 4305 return EmitCXXPseudoDestructorExpr(callee.getPseudoDestructorExpr()); 4306 } 4307 4308 return EmitCall(E->getCallee()->getType(), callee, E, ReturnValue); 4309 } 4310 4311 /// Emit a CallExpr without considering whether it might be a subclass. 4312 RValue CodeGenFunction::EmitSimpleCallExpr(const CallExpr *E, 4313 ReturnValueSlot ReturnValue) { 4314 CGCallee Callee = EmitCallee(E->getCallee()); 4315 return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue); 4316 } 4317 4318 static CGCallee EmitDirectCallee(CodeGenFunction &CGF, const FunctionDecl *FD) { 4319 if (auto builtinID = FD->getBuiltinID()) { 4320 return CGCallee::forBuiltin(builtinID, FD); 4321 } 4322 4323 llvm::Constant *calleePtr = EmitFunctionDeclPointer(CGF.CGM, FD); 4324 return CGCallee::forDirect(calleePtr, FD); 4325 } 4326 4327 CGCallee CodeGenFunction::EmitCallee(const Expr *E) { 4328 E = E->IgnoreParens(); 4329 4330 // Look through function-to-pointer decay. 4331 if (auto ICE = dyn_cast<ImplicitCastExpr>(E)) { 4332 if (ICE->getCastKind() == CK_FunctionToPointerDecay || 4333 ICE->getCastKind() == CK_BuiltinFnToFnPtr) { 4334 return EmitCallee(ICE->getSubExpr()); 4335 } 4336 4337 // Resolve direct calls. 4338 } else if (auto DRE = dyn_cast<DeclRefExpr>(E)) { 4339 if (auto FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { 4340 return EmitDirectCallee(*this, FD); 4341 } 4342 } else if (auto ME = dyn_cast<MemberExpr>(E)) { 4343 if (auto FD = dyn_cast<FunctionDecl>(ME->getMemberDecl())) { 4344 EmitIgnoredExpr(ME->getBase()); 4345 return EmitDirectCallee(*this, FD); 4346 } 4347 4348 // Look through template substitutions. 4349 } else if (auto NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { 4350 return EmitCallee(NTTP->getReplacement()); 4351 4352 // Treat pseudo-destructor calls differently. 4353 } else if (auto PDE = dyn_cast<CXXPseudoDestructorExpr>(E)) { 4354 return CGCallee::forPseudoDestructor(PDE); 4355 } 4356 4357 // Otherwise, we have an indirect reference. 4358 llvm::Value *calleePtr; 4359 QualType functionType; 4360 if (auto ptrType = E->getType()->getAs<PointerType>()) { 4361 calleePtr = EmitScalarExpr(E); 4362 functionType = ptrType->getPointeeType(); 4363 } else { 4364 functionType = E->getType(); 4365 calleePtr = EmitLValue(E).getPointer(); 4366 } 4367 assert(functionType->isFunctionType()); 4368 CGCalleeInfo calleeInfo(functionType->getAs<FunctionProtoType>(), 4369 E->getReferencedDeclOfCallee()); 4370 CGCallee callee(calleeInfo, calleePtr); 4371 return callee; 4372 } 4373 4374 LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) { 4375 // Comma expressions just emit their LHS then their RHS as an l-value. 4376 if (E->getOpcode() == BO_Comma) { 4377 EmitIgnoredExpr(E->getLHS()); 4378 EnsureInsertPoint(); 4379 return EmitLValue(E->getRHS()); 4380 } 4381 4382 if (E->getOpcode() == BO_PtrMemD || 4383 E->getOpcode() == BO_PtrMemI) 4384 return EmitPointerToDataMemberBinaryExpr(E); 4385 4386 assert(E->getOpcode() == BO_Assign && "unexpected binary l-value"); 4387 4388 // Note that in all of these cases, __block variables need the RHS 4389 // evaluated first just in case the variable gets moved by the RHS. 4390 4391 switch (getEvaluationKind(E->getType())) { 4392 case TEK_Scalar: { 4393 switch (E->getLHS()->getType().getObjCLifetime()) { 4394 case Qualifiers::OCL_Strong: 4395 return EmitARCStoreStrong(E, /*ignored*/ false).first; 4396 4397 case Qualifiers::OCL_Autoreleasing: 4398 return EmitARCStoreAutoreleasing(E).first; 4399 4400 // No reason to do any of these differently. 4401 case Qualifiers::OCL_None: 4402 case Qualifiers::OCL_ExplicitNone: 4403 case Qualifiers::OCL_Weak: 4404 break; 4405 } 4406 4407 RValue RV = EmitAnyExpr(E->getRHS()); 4408 LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store); 4409 if (RV.isScalar()) 4410 EmitNullabilityCheck(LV, RV.getScalarVal(), E->getExprLoc()); 4411 EmitStoreThroughLValue(RV, LV); 4412 return LV; 4413 } 4414 4415 case TEK_Complex: 4416 return EmitComplexAssignmentLValue(E); 4417 4418 case TEK_Aggregate: 4419 return EmitAggExprToLValue(E); 4420 } 4421 llvm_unreachable("bad evaluation kind"); 4422 } 4423 4424 LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { 4425 RValue RV = EmitCallExpr(E); 4426 4427 if (!RV.isScalar()) 4428 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), 4429 AlignmentSource::Decl); 4430 4431 assert(E->getCallReturnType(getContext())->isReferenceType() && 4432 "Can't have a scalar return unless the return type is a " 4433 "reference type!"); 4434 4435 return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType()); 4436 } 4437 4438 LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) { 4439 // FIXME: This shouldn't require another copy. 4440 return EmitAggExprToLValue(E); 4441 } 4442 4443 LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) { 4444 assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor() 4445 && "binding l-value to type which needs a temporary"); 4446 AggValueSlot Slot = CreateAggTemp(E->getType()); 4447 EmitCXXConstructExpr(E, Slot); 4448 return MakeAddrLValue(Slot.getAddress(), E->getType(), AlignmentSource::Decl); 4449 } 4450 4451 LValue 4452 CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { 4453 return MakeNaturalAlignAddrLValue(EmitCXXTypeidExpr(E), E->getType()); 4454 } 4455 4456 Address CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) { 4457 return Builder.CreateElementBitCast(CGM.GetAddrOfUuidDescriptor(E), 4458 ConvertType(E->getType())); 4459 } 4460 4461 LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) { 4462 return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType(), 4463 AlignmentSource::Decl); 4464 } 4465 4466 LValue 4467 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) { 4468 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); 4469 Slot.setExternallyDestructed(); 4470 EmitAggExpr(E->getSubExpr(), Slot); 4471 EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddress()); 4472 return MakeAddrLValue(Slot.getAddress(), E->getType(), AlignmentSource::Decl); 4473 } 4474 4475 LValue 4476 CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) { 4477 AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue"); 4478 EmitLambdaExpr(E, Slot); 4479 return MakeAddrLValue(Slot.getAddress(), E->getType(), AlignmentSource::Decl); 4480 } 4481 4482 LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) { 4483 RValue RV = EmitObjCMessageExpr(E); 4484 4485 if (!RV.isScalar()) 4486 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), 4487 AlignmentSource::Decl); 4488 4489 assert(E->getMethodDecl()->getReturnType()->isReferenceType() && 4490 "Can't have a scalar return unless the return type is a " 4491 "reference type!"); 4492 4493 return MakeNaturalAlignPointeeAddrLValue(RV.getScalarVal(), E->getType()); 4494 } 4495 4496 LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) { 4497 Address V = 4498 CGM.getObjCRuntime().GetAddrOfSelector(*this, E->getSelector()); 4499 return MakeAddrLValue(V, E->getType(), AlignmentSource::Decl); 4500 } 4501 4502 llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface, 4503 const ObjCIvarDecl *Ivar) { 4504 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar); 4505 } 4506 4507 LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy, 4508 llvm::Value *BaseValue, 4509 const ObjCIvarDecl *Ivar, 4510 unsigned CVRQualifiers) { 4511 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue, 4512 Ivar, CVRQualifiers); 4513 } 4514 4515 LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { 4516 // FIXME: A lot of the code below could be shared with EmitMemberExpr. 4517 llvm::Value *BaseValue = nullptr; 4518 const Expr *BaseExpr = E->getBase(); 4519 Qualifiers BaseQuals; 4520 QualType ObjectTy; 4521 if (E->isArrow()) { 4522 BaseValue = EmitScalarExpr(BaseExpr); 4523 ObjectTy = BaseExpr->getType()->getPointeeType(); 4524 BaseQuals = ObjectTy.getQualifiers(); 4525 } else { 4526 LValue BaseLV = EmitLValue(BaseExpr); 4527 BaseValue = BaseLV.getPointer(); 4528 ObjectTy = BaseExpr->getType(); 4529 BaseQuals = ObjectTy.getQualifiers(); 4530 } 4531 4532 LValue LV = 4533 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), 4534 BaseQuals.getCVRQualifiers()); 4535 setObjCGCLValueClass(getContext(), E, LV); 4536 return LV; 4537 } 4538 4539 LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) { 4540 // Can only get l-value for message expression returning aggregate type 4541 RValue RV = EmitAnyExprToTemp(E); 4542 return MakeAddrLValue(RV.getAggregateAddress(), E->getType(), 4543 AlignmentSource::Decl); 4544 } 4545 4546 RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee, 4547 const CallExpr *E, ReturnValueSlot ReturnValue, 4548 llvm::Value *Chain) { 4549 // Get the actual function type. The callee type will always be a pointer to 4550 // function type or a block pointer type. 4551 assert(CalleeType->isFunctionPointerType() && 4552 "Call must have function pointer type!"); 4553 4554 const Decl *TargetDecl = OrigCallee.getAbstractInfo().getCalleeDecl(); 4555 4556 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) 4557 // We can only guarantee that a function is called from the correct 4558 // context/function based on the appropriate target attributes, 4559 // so only check in the case where we have both always_inline and target 4560 // since otherwise we could be making a conditional call after a check for 4561 // the proper cpu features (and it won't cause code generation issues due to 4562 // function based code generation). 4563 if (TargetDecl->hasAttr<AlwaysInlineAttr>() && 4564 TargetDecl->hasAttr<TargetAttr>()) 4565 checkTargetFeatures(E, FD); 4566 4567 CalleeType = getContext().getCanonicalType(CalleeType); 4568 4569 auto PointeeType = cast<PointerType>(CalleeType)->getPointeeType(); 4570 4571 CGCallee Callee = OrigCallee; 4572 4573 if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function) && 4574 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) { 4575 if (llvm::Constant *PrefixSig = 4576 CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) { 4577 SanitizerScope SanScope(this); 4578 // Remove any (C++17) exception specifications, to allow calling e.g. a 4579 // noexcept function through a non-noexcept pointer. 4580 auto ProtoTy = 4581 getContext().getFunctionTypeWithExceptionSpec(PointeeType, EST_None); 4582 llvm::Constant *FTRTTIConst = 4583 CGM.GetAddrOfRTTIDescriptor(ProtoTy, /*ForEH=*/true); 4584 llvm::Type *PrefixStructTyElems[] = {PrefixSig->getType(), Int32Ty}; 4585 llvm::StructType *PrefixStructTy = llvm::StructType::get( 4586 CGM.getLLVMContext(), PrefixStructTyElems, /*isPacked=*/true); 4587 4588 llvm::Value *CalleePtr = Callee.getFunctionPointer(); 4589 4590 llvm::Value *CalleePrefixStruct = Builder.CreateBitCast( 4591 CalleePtr, llvm::PointerType::getUnqual(PrefixStructTy)); 4592 llvm::Value *CalleeSigPtr = 4593 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 0); 4594 llvm::Value *CalleeSig = 4595 Builder.CreateAlignedLoad(CalleeSigPtr, getIntAlign()); 4596 llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig); 4597 4598 llvm::BasicBlock *Cont = createBasicBlock("cont"); 4599 llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck"); 4600 Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont); 4601 4602 EmitBlock(TypeCheck); 4603 llvm::Value *CalleeRTTIPtr = 4604 Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, 0, 1); 4605 llvm::Value *CalleeRTTIEncoded = 4606 Builder.CreateAlignedLoad(CalleeRTTIPtr, getPointerAlign()); 4607 llvm::Value *CalleeRTTI = 4608 DecodeAddrUsedInPrologue(CalleePtr, CalleeRTTIEncoded); 4609 llvm::Value *CalleeRTTIMatch = 4610 Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst); 4611 llvm::Constant *StaticData[] = { 4612 EmitCheckSourceLocation(E->getLocStart()), 4613 EmitCheckTypeDescriptor(CalleeType) 4614 }; 4615 EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function), 4616 SanitizerHandler::FunctionTypeMismatch, StaticData, CalleePtr); 4617 4618 Builder.CreateBr(Cont); 4619 EmitBlock(Cont); 4620 } 4621 } 4622 4623 const auto *FnType = cast<FunctionType>(PointeeType); 4624 4625 // If we are checking indirect calls and this call is indirect, check that the 4626 // function pointer is a member of the bit set for the function type. 4627 if (SanOpts.has(SanitizerKind::CFIICall) && 4628 (!TargetDecl || !isa<FunctionDecl>(TargetDecl))) { 4629 SanitizerScope SanScope(this); 4630 EmitSanitizerStatReport(llvm::SanStat_CFI_ICall); 4631 4632 llvm::Metadata *MD; 4633 if (CGM.getCodeGenOpts().SanitizeCfiICallGeneralizePointers) 4634 MD = CGM.CreateMetadataIdentifierGeneralized(QualType(FnType, 0)); 4635 else 4636 MD = CGM.CreateMetadataIdentifierForType(QualType(FnType, 0)); 4637 4638 llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD); 4639 4640 llvm::Value *CalleePtr = Callee.getFunctionPointer(); 4641 llvm::Value *CastedCallee = Builder.CreateBitCast(CalleePtr, Int8PtrTy); 4642 llvm::Value *TypeTest = Builder.CreateCall( 4643 CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedCallee, TypeId}); 4644 4645 auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD); 4646 llvm::Constant *StaticData[] = { 4647 llvm::ConstantInt::get(Int8Ty, CFITCK_ICall), 4648 EmitCheckSourceLocation(E->getLocStart()), 4649 EmitCheckTypeDescriptor(QualType(FnType, 0)), 4650 }; 4651 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) { 4652 EmitCfiSlowPathCheck(SanitizerKind::CFIICall, TypeTest, CrossDsoTypeId, 4653 CastedCallee, StaticData); 4654 } else { 4655 EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIICall), 4656 SanitizerHandler::CFICheckFail, StaticData, 4657 {CastedCallee, llvm::UndefValue::get(IntPtrTy)}); 4658 } 4659 } 4660 4661 CallArgList Args; 4662 if (Chain) 4663 Args.add(RValue::get(Builder.CreateBitCast(Chain, CGM.VoidPtrTy)), 4664 CGM.getContext().VoidPtrTy); 4665 4666 // C++17 requires that we evaluate arguments to a call using assignment syntax 4667 // right-to-left, and that we evaluate arguments to certain other operators 4668 // left-to-right. Note that we allow this to override the order dictated by 4669 // the calling convention on the MS ABI, which means that parameter 4670 // destruction order is not necessarily reverse construction order. 4671 // FIXME: Revisit this based on C++ committee response to unimplementability. 4672 EvaluationOrder Order = EvaluationOrder::Default; 4673 if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) { 4674 if (OCE->isAssignmentOp()) 4675 Order = EvaluationOrder::ForceRightToLeft; 4676 else { 4677 switch (OCE->getOperator()) { 4678 case OO_LessLess: 4679 case OO_GreaterGreater: 4680 case OO_AmpAmp: 4681 case OO_PipePipe: 4682 case OO_Comma: 4683 case OO_ArrowStar: 4684 Order = EvaluationOrder::ForceLeftToRight; 4685 break; 4686 default: 4687 break; 4688 } 4689 } 4690 } 4691 4692 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), E->arguments(), 4693 E->getDirectCallee(), /*ParamsToSkip*/ 0, Order); 4694 4695 const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall( 4696 Args, FnType, /*isChainCall=*/Chain); 4697 4698 // C99 6.5.2.2p6: 4699 // If the expression that denotes the called function has a type 4700 // that does not include a prototype, [the default argument 4701 // promotions are performed]. If the number of arguments does not 4702 // equal the number of parameters, the behavior is undefined. If 4703 // the function is defined with a type that includes a prototype, 4704 // and either the prototype ends with an ellipsis (, ...) or the 4705 // types of the arguments after promotion are not compatible with 4706 // the types of the parameters, the behavior is undefined. If the 4707 // function is defined with a type that does not include a 4708 // prototype, and the types of the arguments after promotion are 4709 // not compatible with those of the parameters after promotion, 4710 // the behavior is undefined [except in some trivial cases]. 4711 // That is, in the general case, we should assume that a call 4712 // through an unprototyped function type works like a *non-variadic* 4713 // call. The way we make this work is to cast to the exact type 4714 // of the promoted arguments. 4715 // 4716 // Chain calls use this same code path to add the invisible chain parameter 4717 // to the function type. 4718 if (isa<FunctionNoProtoType>(FnType) || Chain) { 4719 llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo); 4720 CalleeTy = CalleeTy->getPointerTo(); 4721 4722 llvm::Value *CalleePtr = Callee.getFunctionPointer(); 4723 CalleePtr = Builder.CreateBitCast(CalleePtr, CalleeTy, "callee.knr.cast"); 4724 Callee.setFunctionPointer(CalleePtr); 4725 } 4726 4727 return EmitCall(FnInfo, Callee, ReturnValue, Args, nullptr, E->getExprLoc()); 4728 } 4729 4730 LValue CodeGenFunction:: 4731 EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) { 4732 Address BaseAddr = Address::invalid(); 4733 if (E->getOpcode() == BO_PtrMemI) { 4734 BaseAddr = EmitPointerWithAlignment(E->getLHS()); 4735 } else { 4736 BaseAddr = EmitLValue(E->getLHS()).getAddress(); 4737 } 4738 4739 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS()); 4740 4741 const MemberPointerType *MPT 4742 = E->getRHS()->getType()->getAs<MemberPointerType>(); 4743 4744 LValueBaseInfo BaseInfo; 4745 TBAAAccessInfo TBAAInfo; 4746 Address MemberAddr = 4747 EmitCXXMemberDataPointerAddress(E, BaseAddr, OffsetV, MPT, &BaseInfo, 4748 &TBAAInfo); 4749 4750 return MakeAddrLValue(MemberAddr, MPT->getPointeeType(), BaseInfo, TBAAInfo); 4751 } 4752 4753 /// Given the address of a temporary variable, produce an r-value of 4754 /// its type. 4755 RValue CodeGenFunction::convertTempToRValue(Address addr, 4756 QualType type, 4757 SourceLocation loc) { 4758 LValue lvalue = MakeAddrLValue(addr, type, AlignmentSource::Decl); 4759 switch (getEvaluationKind(type)) { 4760 case TEK_Complex: 4761 return RValue::getComplex(EmitLoadOfComplex(lvalue, loc)); 4762 case TEK_Aggregate: 4763 return lvalue.asAggregateRValue(); 4764 case TEK_Scalar: 4765 return RValue::get(EmitLoadOfScalar(lvalue, loc)); 4766 } 4767 llvm_unreachable("bad evaluation kind"); 4768 } 4769 4770 void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) { 4771 assert(Val->getType()->isFPOrFPVectorTy()); 4772 if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val)) 4773 return; 4774 4775 llvm::MDBuilder MDHelper(getLLVMContext()); 4776 llvm::MDNode *Node = MDHelper.createFPMath(Accuracy); 4777 4778 cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node); 4779 } 4780 4781 namespace { 4782 struct LValueOrRValue { 4783 LValue LV; 4784 RValue RV; 4785 }; 4786 } 4787 4788 static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF, 4789 const PseudoObjectExpr *E, 4790 bool forLValue, 4791 AggValueSlot slot) { 4792 SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 4793 4794 // Find the result expression, if any. 4795 const Expr *resultExpr = E->getResultExpr(); 4796 LValueOrRValue result; 4797 4798 for (PseudoObjectExpr::const_semantics_iterator 4799 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 4800 const Expr *semantic = *i; 4801 4802 // If this semantic expression is an opaque value, bind it 4803 // to the result of its source expression. 4804 if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 4805 // Skip unique OVEs. 4806 if (ov->isUnique()) { 4807 assert(ov != resultExpr && 4808 "A unique OVE cannot be used as the result expression"); 4809 continue; 4810 } 4811 4812 // If this is the result expression, we may need to evaluate 4813 // directly into the slot. 4814 typedef CodeGenFunction::OpaqueValueMappingData OVMA; 4815 OVMA opaqueData; 4816 if (ov == resultExpr && ov->isRValue() && !forLValue && 4817 CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) { 4818 CGF.EmitAggExpr(ov->getSourceExpr(), slot); 4819 LValue LV = CGF.MakeAddrLValue(slot.getAddress(), ov->getType(), 4820 AlignmentSource::Decl); 4821 opaqueData = OVMA::bind(CGF, ov, LV); 4822 result.RV = slot.asRValue(); 4823 4824 // Otherwise, emit as normal. 4825 } else { 4826 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 4827 4828 // If this is the result, also evaluate the result now. 4829 if (ov == resultExpr) { 4830 if (forLValue) 4831 result.LV = CGF.EmitLValue(ov); 4832 else 4833 result.RV = CGF.EmitAnyExpr(ov, slot); 4834 } 4835 } 4836 4837 opaques.push_back(opaqueData); 4838 4839 // Otherwise, if the expression is the result, evaluate it 4840 // and remember the result. 4841 } else if (semantic == resultExpr) { 4842 if (forLValue) 4843 result.LV = CGF.EmitLValue(semantic); 4844 else 4845 result.RV = CGF.EmitAnyExpr(semantic, slot); 4846 4847 // Otherwise, evaluate the expression in an ignored context. 4848 } else { 4849 CGF.EmitIgnoredExpr(semantic); 4850 } 4851 } 4852 4853 // Unbind all the opaques now. 4854 for (unsigned i = 0, e = opaques.size(); i != e; ++i) 4855 opaques[i].unbind(CGF); 4856 4857 return result; 4858 } 4859 4860 RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E, 4861 AggValueSlot slot) { 4862 return emitPseudoObjectExpr(*this, E, false, slot).RV; 4863 } 4864 4865 LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) { 4866 return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV; 4867 } 4868