1 //===- Loads.cpp - Local load analysis ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines simple local analyses for load instructions. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Analysis/Loads.h" 14 #include "llvm/Analysis/AliasAnalysis.h" 15 #include "llvm/Analysis/CaptureTracking.h" 16 #include "llvm/Analysis/LoopInfo.h" 17 #include "llvm/Analysis/MemoryBuiltins.h" 18 #include "llvm/Analysis/ScalarEvolution.h" 19 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 20 #include "llvm/Analysis/ValueTracking.h" 21 #include "llvm/IR/DataLayout.h" 22 #include "llvm/IR/GlobalAlias.h" 23 #include "llvm/IR/GlobalVariable.h" 24 #include "llvm/IR/IntrinsicInst.h" 25 #include "llvm/IR/LLVMContext.h" 26 #include "llvm/IR/Module.h" 27 #include "llvm/IR/Operator.h" 28 #include "llvm/IR/Statepoint.h" 29 30 using namespace llvm; 31 32 static bool isAligned(const Value *Base, const APInt &Offset, Align Alignment, 33 const DataLayout &DL) { 34 Align BA = Base->getPointerAlignment(DL); 35 const APInt APAlign(Offset.getBitWidth(), Alignment.value()); 36 assert(APAlign.isPowerOf2() && "must be a power of 2!"); 37 return BA >= Alignment && !(Offset & (APAlign - 1)); 38 } 39 40 /// Test if V is always a pointer to allocated and suitably aligned memory for 41 /// a simple load or store. 42 static bool isDereferenceableAndAlignedPointer( 43 const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL, 44 const Instruction *CtxI, const DominatorTree *DT, 45 SmallPtrSetImpl<const Value *> &Visited, unsigned MaxDepth) { 46 assert(V->getType()->isPointerTy() && "Base must be pointer"); 47 48 // Recursion limit. 49 if (MaxDepth-- == 0) 50 return false; 51 52 // Already visited? Bail out, we've likely hit unreachable code. 53 if (!Visited.insert(V).second) 54 return false; 55 56 // Note that it is not safe to speculate into a malloc'd region because 57 // malloc may return null. 58 59 // bitcast instructions are no-ops as far as dereferenceability is concerned. 60 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) { 61 if (BC->getSrcTy()->isPointerTy()) 62 return isDereferenceableAndAlignedPointer( 63 BC->getOperand(0), Alignment, Size, DL, CtxI, DT, Visited, MaxDepth); 64 } 65 66 bool CheckForNonNull = false; 67 APInt KnownDerefBytes(Size.getBitWidth(), 68 V->getPointerDereferenceableBytes(DL, CheckForNonNull)); 69 if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size)) 70 if (!CheckForNonNull || isKnownNonZero(V, DL, 0, nullptr, CtxI, DT)) { 71 // As we recursed through GEPs to get here, we've incrementally checked 72 // that each step advanced by a multiple of the alignment. If our base is 73 // properly aligned, then the original offset accessed must also be. 74 Type *Ty = V->getType(); 75 assert(Ty->isSized() && "must be sized"); 76 APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0); 77 return isAligned(V, Offset, Alignment, DL); 78 } 79 80 // For GEPs, determine if the indexing lands within the allocated object. 81 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 82 const Value *Base = GEP->getPointerOperand(); 83 84 APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0); 85 if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() || 86 !Offset.urem(APInt(Offset.getBitWidth(), Alignment.value())) 87 .isMinValue()) 88 return false; 89 90 // If the base pointer is dereferenceable for Offset+Size bytes, then the 91 // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base 92 // pointer is aligned to Align bytes, and the Offset is divisible by Align 93 // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also 94 // aligned to Align bytes. 95 96 // Offset and Size may have different bit widths if we have visited an 97 // addrspacecast, so we can't do arithmetic directly on the APInt values. 98 return isDereferenceableAndAlignedPointer( 99 Base, Alignment, Offset + Size.sextOrTrunc(Offset.getBitWidth()), DL, 100 CtxI, DT, Visited, MaxDepth); 101 } 102 103 // For gc.relocate, look through relocations 104 if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V)) 105 return isDereferenceableAndAlignedPointer( 106 RelocateInst->getDerivedPtr(), Alignment, Size, DL, CtxI, DT, Visited, MaxDepth); 107 108 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) 109 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Alignment, 110 Size, DL, CtxI, DT, Visited, MaxDepth); 111 112 if (const auto *Call = dyn_cast<CallBase>(V)) { 113 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, true)) 114 return isDereferenceableAndAlignedPointer(RP, Alignment, Size, DL, CtxI, 115 DT, Visited, MaxDepth); 116 117 // If we have a call we can't recurse through, check to see if this is an 118 // allocation function for which we can establish an minimum object size. 119 // Such a minimum object size is analogous to a deref_or_null attribute in 120 // that we still need to prove the result non-null at point of use. 121 // NOTE: We can only use the object size as a base fact as we a) need to 122 // prove alignment too, and b) don't want the compile time impact of a 123 // separate recursive walk. 124 ObjectSizeOpts Opts; 125 // TODO: It may be okay to round to align, but that would imply that 126 // accessing slightly out of bounds was legal, and we're currently 127 // inconsistent about that. For the moment, be conservative. 128 Opts.RoundToAlign = false; 129 Opts.NullIsUnknownSize = true; 130 uint64_t ObjSize; 131 // TODO: Plumb through TLI so that malloc routines and such working. 132 if (getObjectSize(V, ObjSize, DL, nullptr, Opts)) { 133 APInt KnownDerefBytes(Size.getBitWidth(), ObjSize); 134 if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) && 135 isKnownNonZero(V, DL, 0, nullptr, CtxI, DT) && 136 // TODO: We're currently inconsistent about whether deref(N) is a 137 // global fact or a point in time fact. Once D61652 eventually 138 // lands, this check will be restricted to the point in time 139 // variant. For that variant, we need to prove that object hasn't 140 // been conditionally freed before ontext instruction - if it has, we 141 // might be hoisting over the inverse conditional and creating a 142 // dynamic use after free. 143 !PointerMayBeCapturedBefore(V, true, true, CtxI, DT, true)) { 144 // As we recursed through GEPs to get here, we've incrementally 145 // checked that each step advanced by a multiple of the alignment. If 146 // our base is properly aligned, then the original offset accessed 147 // must also be. 148 Type *Ty = V->getType(); 149 assert(Ty->isSized() && "must be sized"); 150 APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0); 151 return isAligned(V, Offset, Alignment, DL); 152 } 153 } 154 } 155 156 // If we don't know, assume the worst. 157 return false; 158 } 159 160 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Align Alignment, 161 const APInt &Size, 162 const DataLayout &DL, 163 const Instruction *CtxI, 164 const DominatorTree *DT) { 165 // Note: At the moment, Size can be zero. This ends up being interpreted as 166 // a query of whether [Base, V] is dereferenceable and V is aligned (since 167 // that's what the implementation happened to do). It's unclear if this is 168 // the desired semantic, but at least SelectionDAG does exercise this case. 169 170 SmallPtrSet<const Value *, 32> Visited; 171 return ::isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, DT, 172 Visited, 16); 173 } 174 175 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, 176 MaybeAlign MA, 177 const DataLayout &DL, 178 const Instruction *CtxI, 179 const DominatorTree *DT) { 180 // For unsized types or scalable vectors we don't know exactly how many bytes 181 // are dereferenced, so bail out. 182 if (!Ty->isSized() || isa<ScalableVectorType>(Ty)) 183 return false; 184 185 // When dereferenceability information is provided by a dereferenceable 186 // attribute, we know exactly how many bytes are dereferenceable. If we can 187 // determine the exact offset to the attributed variable, we can use that 188 // information here. 189 190 // Require ABI alignment for loads without alignment specification 191 const Align Alignment = DL.getValueOrABITypeAlignment(MA, Ty); 192 APInt AccessSize(DL.getPointerTypeSizeInBits(V->getType()), 193 DL.getTypeStoreSize(Ty)); 194 return isDereferenceableAndAlignedPointer(V, Alignment, AccessSize, DL, CtxI, 195 DT); 196 } 197 198 bool llvm::isDereferenceablePointer(const Value *V, Type *Ty, 199 const DataLayout &DL, 200 const Instruction *CtxI, 201 const DominatorTree *DT) { 202 return isDereferenceableAndAlignedPointer(V, Ty, Align(1), DL, CtxI, DT); 203 } 204 205 /// Test if A and B will obviously have the same value. 206 /// 207 /// This includes recognizing that %t0 and %t1 will have the same 208 /// value in code like this: 209 /// \code 210 /// %t0 = getelementptr \@a, 0, 3 211 /// store i32 0, i32* %t0 212 /// %t1 = getelementptr \@a, 0, 3 213 /// %t2 = load i32* %t1 214 /// \endcode 215 /// 216 static bool AreEquivalentAddressValues(const Value *A, const Value *B) { 217 // Test if the values are trivially equivalent. 218 if (A == B) 219 return true; 220 221 // Test if the values come from identical arithmetic instructions. 222 // Use isIdenticalToWhenDefined instead of isIdenticalTo because 223 // this function is only used when one address use dominates the 224 // other, which means that they'll always either have the same 225 // value or one of them will have an undefined value. 226 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) || 227 isa<GetElementPtrInst>(A)) 228 if (const Instruction *BI = dyn_cast<Instruction>(B)) 229 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) 230 return true; 231 232 // Otherwise they may not be equivalent. 233 return false; 234 } 235 236 bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, 237 ScalarEvolution &SE, 238 DominatorTree &DT) { 239 auto &DL = LI->getModule()->getDataLayout(); 240 Value *Ptr = LI->getPointerOperand(); 241 242 APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()), 243 DL.getTypeStoreSize(LI->getType()).getFixedSize()); 244 const Align Alignment = LI->getAlign(); 245 246 Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI(); 247 248 // If given a uniform (i.e. non-varying) address, see if we can prove the 249 // access is safe within the loop w/o needing predication. 250 if (L->isLoopInvariant(Ptr)) 251 return isDereferenceableAndAlignedPointer(Ptr, Alignment, EltSize, DL, 252 HeaderFirstNonPHI, &DT); 253 254 // Otherwise, check to see if we have a repeating access pattern where we can 255 // prove that all accesses are well aligned and dereferenceable. 256 auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Ptr)); 257 if (!AddRec || AddRec->getLoop() != L || !AddRec->isAffine()) 258 return false; 259 auto* Step = dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(SE)); 260 if (!Step) 261 return false; 262 // TODO: generalize to access patterns which have gaps 263 if (Step->getAPInt() != EltSize) 264 return false; 265 266 auto TC = SE.getSmallConstantMaxTripCount(L); 267 if (!TC) 268 return false; 269 270 const APInt AccessSize = TC * EltSize; 271 272 auto *StartS = dyn_cast<SCEVUnknown>(AddRec->getStart()); 273 if (!StartS) 274 return false; 275 assert(SE.isLoopInvariant(StartS, L) && "implied by addrec definition"); 276 Value *Base = StartS->getValue(); 277 278 // For the moment, restrict ourselves to the case where the access size is a 279 // multiple of the requested alignment and the base is aligned. 280 // TODO: generalize if a case found which warrants 281 if (EltSize.urem(Alignment.value()) != 0) 282 return false; 283 return isDereferenceableAndAlignedPointer(Base, Alignment, AccessSize, DL, 284 HeaderFirstNonPHI, &DT); 285 } 286 287 /// Check if executing a load of this pointer value cannot trap. 288 /// 289 /// If DT and ScanFrom are specified this method performs context-sensitive 290 /// analysis and returns true if it is safe to load immediately before ScanFrom. 291 /// 292 /// If it is not obviously safe to load from the specified pointer, we do 293 /// a quick local scan of the basic block containing \c ScanFrom, to determine 294 /// if the address is already accessed. 295 /// 296 /// This uses the pointee type to determine how many bytes need to be safe to 297 /// load from the pointer. 298 bool llvm::isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size, 299 const DataLayout &DL, 300 Instruction *ScanFrom, 301 const DominatorTree *DT) { 302 // If DT is not specified we can't make context-sensitive query 303 const Instruction* CtxI = DT ? ScanFrom : nullptr; 304 if (isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, DT)) 305 return true; 306 307 if (!ScanFrom) 308 return false; 309 310 if (Size.getBitWidth() > 64) 311 return false; 312 const uint64_t LoadSize = Size.getZExtValue(); 313 314 // Otherwise, be a little bit aggressive by scanning the local block where we 315 // want to check to see if the pointer is already being loaded or stored 316 // from/to. If so, the previous load or store would have already trapped, 317 // so there is no harm doing an extra load (also, CSE will later eliminate 318 // the load entirely). 319 BasicBlock::iterator BBI = ScanFrom->getIterator(), 320 E = ScanFrom->getParent()->begin(); 321 322 // We can at least always strip pointer casts even though we can't use the 323 // base here. 324 V = V->stripPointerCasts(); 325 326 while (BBI != E) { 327 --BBI; 328 329 // If we see a free or a call which may write to memory (i.e. which might do 330 // a free) the pointer could be marked invalid. 331 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() && 332 !isa<DbgInfoIntrinsic>(BBI)) 333 return false; 334 335 Value *AccessedPtr; 336 Type *AccessedTy; 337 Align AccessedAlign; 338 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { 339 // Ignore volatile loads. The execution of a volatile load cannot 340 // be used to prove an address is backed by regular memory; it can, 341 // for example, point to an MMIO register. 342 if (LI->isVolatile()) 343 continue; 344 AccessedPtr = LI->getPointerOperand(); 345 AccessedTy = LI->getType(); 346 AccessedAlign = LI->getAlign(); 347 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { 348 // Ignore volatile stores (see comment for loads). 349 if (SI->isVolatile()) 350 continue; 351 AccessedPtr = SI->getPointerOperand(); 352 AccessedTy = SI->getValueOperand()->getType(); 353 AccessedAlign = SI->getAlign(); 354 } else 355 continue; 356 357 if (AccessedAlign < Alignment) 358 continue; 359 360 // Handle trivial cases. 361 if (AccessedPtr == V && 362 LoadSize <= DL.getTypeStoreSize(AccessedTy)) 363 return true; 364 365 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) && 366 LoadSize <= DL.getTypeStoreSize(AccessedTy)) 367 return true; 368 } 369 return false; 370 } 371 372 bool llvm::isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment, 373 const DataLayout &DL, 374 Instruction *ScanFrom, 375 const DominatorTree *DT) { 376 APInt Size(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty)); 377 return isSafeToLoadUnconditionally(V, Alignment, Size, DL, ScanFrom, DT); 378 } 379 380 /// DefMaxInstsToScan - the default number of maximum instructions 381 /// to scan in the block, used by FindAvailableLoadedValue(). 382 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump 383 /// threading in part by eliminating partially redundant loads. 384 /// At that point, the value of MaxInstsToScan was already set to '6' 385 /// without documented explanation. 386 cl::opt<unsigned> 387 llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden, 388 cl::desc("Use this to specify the default maximum number of instructions " 389 "to scan backward from a given instruction, when searching for " 390 "available loaded value")); 391 392 Value *llvm::FindAvailableLoadedValue(LoadInst *Load, 393 BasicBlock *ScanBB, 394 BasicBlock::iterator &ScanFrom, 395 unsigned MaxInstsToScan, 396 AAResults *AA, bool *IsLoad, 397 unsigned *NumScanedInst) { 398 // Don't CSE load that is volatile or anything stronger than unordered. 399 if (!Load->isUnordered()) 400 return nullptr; 401 402 return FindAvailablePtrLoadStore( 403 Load->getPointerOperand(), Load->getType(), Load->isAtomic(), ScanBB, 404 ScanFrom, MaxInstsToScan, AA, IsLoad, NumScanedInst); 405 } 406 407 // Check if the load and the store have the same base, constant offsets and 408 // non-overlapping access ranges. 409 static bool AreNonOverlapSameBaseLoadAndStore( 410 Value *LoadPtr, Type *LoadTy, Value *StorePtr, Type *StoreTy, 411 const DataLayout &DL) { 412 APInt LoadOffset(DL.getTypeSizeInBits(LoadPtr->getType()), 0); 413 APInt StoreOffset(DL.getTypeSizeInBits(StorePtr->getType()), 0); 414 Value *LoadBase = LoadPtr->stripAndAccumulateConstantOffsets( 415 DL, LoadOffset, /* AllowNonInbounds */ false); 416 Value *StoreBase = StorePtr->stripAndAccumulateConstantOffsets( 417 DL, StoreOffset, /* AllowNonInbounds */ false); 418 if (LoadBase != StoreBase) 419 return false; 420 auto LoadAccessSize = LocationSize::precise(DL.getTypeStoreSize(LoadTy)); 421 auto StoreAccessSize = LocationSize::precise(DL.getTypeStoreSize(StoreTy)); 422 ConstantRange LoadRange(LoadOffset, 423 LoadOffset + LoadAccessSize.toRaw()); 424 ConstantRange StoreRange(StoreOffset, 425 StoreOffset + StoreAccessSize.toRaw()); 426 return LoadRange.intersectWith(StoreRange).isEmptySet(); 427 } 428 429 Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy, 430 bool AtLeastAtomic, BasicBlock *ScanBB, 431 BasicBlock::iterator &ScanFrom, 432 unsigned MaxInstsToScan, 433 AAResults *AA, bool *IsLoadCSE, 434 unsigned *NumScanedInst) { 435 if (MaxInstsToScan == 0) 436 MaxInstsToScan = ~0U; 437 438 const DataLayout &DL = ScanBB->getModule()->getDataLayout(); 439 Value *StrippedPtr = Ptr->stripPointerCasts(); 440 441 while (ScanFrom != ScanBB->begin()) { 442 // We must ignore debug info directives when counting (otherwise they 443 // would affect codegen). 444 Instruction *Inst = &*--ScanFrom; 445 if (isa<DbgInfoIntrinsic>(Inst)) 446 continue; 447 448 // Restore ScanFrom to expected value in case next test succeeds 449 ScanFrom++; 450 451 if (NumScanedInst) 452 ++(*NumScanedInst); 453 454 // Don't scan huge blocks. 455 if (MaxInstsToScan-- == 0) 456 return nullptr; 457 458 --ScanFrom; 459 // If this is a load of Ptr, the loaded value is available. 460 // (This is true even if the load is volatile or atomic, although 461 // those cases are unlikely.) 462 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) 463 if (AreEquivalentAddressValues( 464 LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) && 465 CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) { 466 467 // We can value forward from an atomic to a non-atomic, but not the 468 // other way around. 469 if (LI->isAtomic() < AtLeastAtomic) 470 return nullptr; 471 472 if (IsLoadCSE) 473 *IsLoadCSE = true; 474 return LI; 475 } 476 477 // Try to get the store size for the type. 478 auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy)); 479 480 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 481 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts(); 482 // If this is a store through Ptr, the value is available! 483 // (This is true even if the store is volatile or atomic, although 484 // those cases are unlikely.) 485 if (AreEquivalentAddressValues(StorePtr, StrippedPtr) && 486 CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(), 487 AccessTy, DL)) { 488 489 // We can value forward from an atomic to a non-atomic, but not the 490 // other way around. 491 if (SI->isAtomic() < AtLeastAtomic) 492 return nullptr; 493 494 if (IsLoadCSE) 495 *IsLoadCSE = false; 496 return SI->getOperand(0); 497 } 498 499 // If both StrippedPtr and StorePtr reach all the way to an alloca or 500 // global and they are different, ignore the store. This is a trivial form 501 // of alias analysis that is important for reg2mem'd code. 502 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) && 503 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) && 504 StrippedPtr != StorePtr) 505 continue; 506 507 if (!AA) { 508 // When AA isn't available, but if the load and the store have the same 509 // base, constant offsets and non-overlapping access ranges, ignore the 510 // store. This is a simple form of alias analysis that is used by the 511 // inliner. FIXME: use BasicAA if possible. 512 if (AreNonOverlapSameBaseLoadAndStore( 513 Ptr, AccessTy, SI->getPointerOperand(), 514 SI->getValueOperand()->getType(), DL)) 515 continue; 516 } else { 517 // If we have alias analysis and it says the store won't modify the 518 // loaded value, ignore the store. 519 if (!isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize))) 520 continue; 521 } 522 523 // Otherwise the store that may or may not alias the pointer, bail out. 524 ++ScanFrom; 525 return nullptr; 526 } 527 528 // If this is some other instruction that may clobber Ptr, bail out. 529 if (Inst->mayWriteToMemory()) { 530 // If alias analysis claims that it really won't modify the load, 531 // ignore it. 532 if (AA && !isModSet(AA->getModRefInfo(Inst, StrippedPtr, AccessSize))) 533 continue; 534 535 // May modify the pointer, bail out. 536 ++ScanFrom; 537 return nullptr; 538 } 539 } 540 541 // Got to the start of the block, we didn't find it, but are done for this 542 // block. 543 return nullptr; 544 } 545 546 bool llvm::canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL, 547 Instruction *CtxI) { 548 Type *Ty = A->getType(); 549 assert(Ty == B->getType() && Ty->isPointerTy() && 550 "values must have matching pointer types"); 551 552 // NOTE: The checks in the function are incomplete and currently miss illegal 553 // cases! The current implementation is a starting point and the 554 // implementation should be made stricter over time. 555 if (auto *C = dyn_cast<Constant>(B)) { 556 // Do not allow replacing a pointer with a constant pointer, unless it is 557 // either null or at least one byte is dereferenceable. 558 APInt OneByte(DL.getPointerTypeSizeInBits(Ty), 1); 559 return C->isNullValue() || 560 isDereferenceableAndAlignedPointer(B, Align(1), OneByte, DL, CtxI); 561 } 562 563 return true; 564 } 565