Skip to content

Commit b52df4e

Browse files
committed
[Loads] Add optimized FindAvailableLoadedValue() overload (NFCI)
FindAvailableLoadedValue() accepts an iterator by reference. If no available value is found, then the iterator will either be left at a clobbering instruction or the beginning of the basic block. This allows using FindAvailableLoadedValue() across multiple blocks. If this functionality is not needed, as is the case in InstCombine, then we can use a much more efficient implementation: First try to find an available value, and only perform clobber checks if we actually found one. As this function only looks at a very small number of instructions (6 by default) and usually doesn't find an available value, this saves many expensive alias analysis queries. (cherry picked from commit e0615bc)
1 parent cd129c7 commit b52df4e

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

llvm/include/llvm/Analysis/Loads.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ Value *FindAvailableLoadedValue(LoadInst *Load,
127127
bool *IsLoadCSE = nullptr,
128128
unsigned *NumScanedInst = nullptr);
129129

130+
/// This overload provides a more efficient implementation of
131+
/// FindAvailableLoadedValue() for the case where we are not interested in
132+
/// finding the closest clobbering instruction if no available load is found.
133+
/// This overload cannot be used to scan across multiple blocks.
134+
Value *FindAvailableLoadedValue(LoadInst *Load, AAResults &AA, bool *IsLoadCSE,
135+
unsigned MaxInstsToScan = DefMaxInstsToScan);
136+
130137
/// Scan backwards to see if we have the value of the given pointer available
131138
/// locally within a small number of instructions.
132139
///

llvm/lib/Analysis/Loads.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,51 @@ Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy,
557557
return nullptr;
558558
}
559559

560+
Value *llvm::FindAvailableLoadedValue(LoadInst *Load, AAResults &AA,
561+
bool *IsLoadCSE,
562+
unsigned MaxInstsToScan) {
563+
const DataLayout &DL = Load->getModule()->getDataLayout();
564+
Value *StrippedPtr = Load->getPointerOperand()->stripPointerCasts();
565+
BasicBlock *ScanBB = Load->getParent();
566+
Type *AccessTy = Load->getType();
567+
bool AtLeastAtomic = Load->isAtomic();
568+
569+
if (!Load->isUnordered())
570+
return nullptr;
571+
572+
// Try to find an available value first, and delay expensive alias analysis
573+
// queries until later.
574+
Value *Available = nullptr;;
575+
SmallVector<Instruction *> MustNotAliasInsts;
576+
for (Instruction &Inst : make_range(++Load->getReverseIterator(),
577+
ScanBB->rend())) {
578+
if (isa<DbgInfoIntrinsic>(&Inst))
579+
continue;
580+
581+
if (MaxInstsToScan-- == 0)
582+
return nullptr;
583+
584+
Available = getAvailableLoadStore(&Inst, StrippedPtr, AccessTy,
585+
AtLeastAtomic, DL, IsLoadCSE);
586+
if (Available)
587+
break;
588+
589+
if (Inst.mayWriteToMemory())
590+
MustNotAliasInsts.push_back(&Inst);
591+
}
592+
593+
// If we found an available value, ensure that the instructions in between
594+
// did not modify the memory location.
595+
if (Available) {
596+
auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy));
597+
for (Instruction *Inst : MustNotAliasInsts)
598+
if (isModSet(AA.getModRefInfo(Inst, StrippedPtr, AccessSize)))
599+
return nullptr;
600+
}
601+
602+
return Available;
603+
}
604+
560605
bool llvm::canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
561606
Instruction *CtxI) {
562607
Type *Ty = A->getType();

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,8 @@ Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) {
956956
// Do really simple store-to-load forwarding and load CSE, to catch cases
957957
// where there are several consecutive memory accesses to the same location,
958958
// separated by a few arithmetic operations.
959-
BasicBlock::iterator BBI(LI);
960959
bool IsLoadCSE = false;
961-
if (Value *AvailableVal = FindAvailableLoadedValue(
962-
&LI, LI.getParent(), BBI, DefMaxInstsToScan, AA, &IsLoadCSE)) {
960+
if (Value *AvailableVal = FindAvailableLoadedValue(&LI, *AA, &IsLoadCSE)) {
963961
if (IsLoadCSE)
964962
combineMetadataForCSE(cast<LoadInst>(AvailableVal), &LI, false);
965963

0 commit comments

Comments
 (0)