Skip to content

Commit 43ae5f4

Browse files
committed
Revert "[LVI] Normalize pointer behavior"
This reverts commit 15bc4dc. clang-cmake-x86_64-sde-avx512-linux buildbot reported quite a few compile-time regressions in test-suite, will investigate.
1 parent 4d0e07f commit 43ae5f4

File tree

2 files changed

+90
-96
lines changed

2 files changed

+90
-96
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 89 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,6 @@ namespace {
151151
/// This is the cache kept by LazyValueInfo which
152152
/// maintains information about queries across the clients' queries.
153153
class LazyValueInfoCache {
154-
public:
155-
typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
156-
PerBlockValueCacheTy;
157-
158-
private:
159154
/// This is all of the cached block information for exactly one Value*.
160155
/// The entries are sorted by the BasicBlock* of the
161156
/// entries, allowing us to do a lookup with a binary search.
@@ -167,19 +162,18 @@ namespace {
167162
SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4> BlockVals;
168163
};
169164

165+
/// This tracks, on a per-block basis, the set of values that are
166+
/// over-defined at the end of that block.
167+
typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>>
168+
OverDefinedCacheTy;
170169
/// Keep track of all blocks that we have ever seen, so we
171170
/// don't spend time removing unused blocks from our caches.
172171
DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
173172

174173
/// This is all of the cached information for all values,
175174
/// mapped from Value* to key information.
176175
DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache;
177-
/// This tracks, on a per-block basis, the set of values that are
178-
/// over-defined at the end of that block.
179-
PerBlockValueCacheTy OverDefinedCache;
180-
/// This tracks, on a per-block basis, the set of pointers that are
181-
/// dereferenced in the block (and thus non-null at the end of the block).
182-
PerBlockValueCacheTy DereferencedPointerCache;
176+
OverDefinedCacheTy OverDefinedCache;
183177

184178

185179
public:
@@ -235,17 +229,11 @@ namespace {
235229
return BBI->second;
236230
}
237231

238-
std::pair<PerBlockValueCacheTy::iterator, bool>
239-
getOrInitDereferencedPointers(BasicBlock *BB) {
240-
return DereferencedPointerCache.try_emplace(BB);
241-
}
242-
243232
/// clear - Empty the cache.
244233
void clear() {
245234
SeenBlocks.clear();
246235
ValueCache.clear();
247236
OverDefinedCache.clear();
248-
DereferencedPointerCache.clear();
249237
}
250238

251239
/// Inform the cache that a given value has been deleted.
@@ -264,22 +252,17 @@ namespace {
264252
};
265253
}
266254

267-
static void eraseValueFromPerBlockValueCache(
268-
Value *V, LazyValueInfoCache::PerBlockValueCacheTy &Cache) {
269-
for (auto I = Cache.begin(), E = Cache.end(); I != E;) {
255+
void LazyValueInfoCache::eraseValue(Value *V) {
256+
for (auto I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E;) {
270257
// Copy and increment the iterator immediately so we can erase behind
271258
// ourselves.
272259
auto Iter = I++;
273260
SmallPtrSetImpl<Value *> &ValueSet = Iter->second;
274261
ValueSet.erase(V);
275262
if (ValueSet.empty())
276-
Cache.erase(Iter);
263+
OverDefinedCache.erase(Iter);
277264
}
278-
}
279265

280-
void LazyValueInfoCache::eraseValue(Value *V) {
281-
eraseValueFromPerBlockValueCache(V, OverDefinedCache);
282-
eraseValueFromPerBlockValueCache(V, DereferencedPointerCache);
283266
ValueCache.erase(V);
284267
}
285268

@@ -296,8 +279,9 @@ void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
296279
return;
297280
SeenBlocks.erase(I);
298281

299-
OverDefinedCache.erase(BB);
300-
DereferencedPointerCache.erase(BB);
282+
auto ODI = OverDefinedCache.find(BB);
283+
if (ODI != OverDefinedCache.end())
284+
OverDefinedCache.erase(ODI);
301285

302286
for (auto &I : ValueCache)
303287
I.second->BlockVals.erase(BB);
@@ -454,7 +438,6 @@ namespace {
454438
BasicBlock *BB);
455439
bool solveBlockValueExtractValue(ValueLatticeElement &BBLV,
456440
ExtractValueInst *EVI, BasicBlock *BB);
457-
bool isNonNullDueToDereferenceInBlock(Value *Val, BasicBlock *BB);
458441
void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
459442
ValueLatticeElement &BBLV,
460443
Instruction *BBI);
@@ -636,6 +619,17 @@ bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
636619

637620
bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
638621
Value *Val, BasicBlock *BB) {
622+
623+
Instruction *BBI = dyn_cast<Instruction>(Val);
624+
if (!BBI || BBI->getParent() != BB)
625+
return solveBlockValueNonLocal(Res, Val, BB);
626+
627+
if (PHINode *PN = dyn_cast<PHINode>(BBI))
628+
return solveBlockValuePHINode(Res, PN, BB);
629+
630+
if (auto *SI = dyn_cast<SelectInst>(BBI))
631+
return solveBlockValueSelect(Res, SI, BB);
632+
639633
// If this value is a nonnull pointer, record it's range and bailout. Note
640634
// that for all other pointer typed values, we terminate the search at the
641635
// definition. We could easily extend this to look through geps, bitcasts,
@@ -645,22 +639,11 @@ bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
645639
// This does mean that we have a sensitivity to where the defining
646640
// instruction is placed, even if it could legally be hoisted much higher.
647641
// That is unfortunate.
648-
PointerType *PT = dyn_cast<PointerType>(Val->getType());
649-
if (PT && isKnownNonZero(Val, DL)) {
642+
PointerType *PT = dyn_cast<PointerType>(BBI->getType());
643+
if (PT && isKnownNonZero(BBI, DL)) {
650644
Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
651645
return true;
652646
}
653-
654-
Instruction *BBI = dyn_cast<Instruction>(Val);
655-
if (!BBI || BBI->getParent() != BB)
656-
return solveBlockValueNonLocal(Res, Val, BB);
657-
658-
if (PHINode *PN = dyn_cast<PHINode>(BBI))
659-
return solveBlockValuePHINode(Res, PN, BB);
660-
661-
if (auto *SI = dyn_cast<SelectInst>(BBI))
662-
return solveBlockValueSelect(Res, SI, BB);
663-
664647
if (BBI->getType()->isIntegerTy()) {
665648
if (auto *CI = dyn_cast<CastInst>(BBI))
666649
return solveBlockValueCast(Res, CI, BB);
@@ -681,63 +664,75 @@ bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
681664
return true;
682665
}
683666

684-
static void AddDereferencedPointer(
685-
Value *Ptr, SmallPtrSet<Value *, 4> &PtrSet, const DataLayout &DL) {
686-
// TODO: Use NullPointerIsDefined instead.
687-
if (Ptr->getType()->getPointerAddressSpace() == 0) {
688-
Ptr = GetUnderlyingObject(Ptr, DL);
689-
PtrSet.insert(Ptr);
690-
}
691-
}
692-
693-
static void AddPointersDereferencedByInstruction(
694-
Instruction *I, SmallPtrSet<Value *, 4> &PtrSet, const DataLayout &DL) {
667+
static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
695668
if (LoadInst *L = dyn_cast<LoadInst>(I)) {
696-
AddDereferencedPointer(L->getPointerOperand(), PtrSet, DL);
697-
} else if (StoreInst *S = dyn_cast<StoreInst>(I)) {
698-
AddDereferencedPointer(S->getPointerOperand(), PtrSet, DL);
699-
} else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
700-
if (MI->isVolatile()) return;
669+
return L->getPointerAddressSpace() == 0 &&
670+
GetUnderlyingObject(L->getPointerOperand(),
671+
L->getModule()->getDataLayout()) == Ptr;
672+
}
673+
if (StoreInst *S = dyn_cast<StoreInst>(I)) {
674+
return S->getPointerAddressSpace() == 0 &&
675+
GetUnderlyingObject(S->getPointerOperand(),
676+
S->getModule()->getDataLayout()) == Ptr;
677+
}
678+
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
679+
if (MI->isVolatile()) return false;
701680

702681
// FIXME: check whether it has a valuerange that excludes zero?
703682
ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength());
704-
if (!Len || Len->isZero()) return;
683+
if (!Len || Len->isZero()) return false;
705684

706-
AddDereferencedPointer(MI->getRawDest(), PtrSet, DL);
685+
if (MI->getDestAddressSpace() == 0)
686+
if (GetUnderlyingObject(MI->getRawDest(),
687+
MI->getModule()->getDataLayout()) == Ptr)
688+
return true;
707689
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
708-
AddDereferencedPointer(MTI->getRawSource(), PtrSet, DL);
690+
if (MTI->getSourceAddressSpace() == 0)
691+
if (GetUnderlyingObject(MTI->getRawSource(),
692+
MTI->getModule()->getDataLayout()) == Ptr)
693+
return true;
709694
}
695+
return false;
710696
}
711697

712-
bool LazyValueInfoImpl::isNonNullDueToDereferenceInBlock(
713-
Value *Val, BasicBlock *BB) {
714-
if (NullPointerIsDefined(BB->getParent(),
715-
Val->getType()->getPointerAddressSpace()))
716-
return false;
698+
/// Return true if the allocation associated with Val is ever dereferenced
699+
/// within the given basic block. This establishes the fact Val is not null,
700+
/// but does not imply that the memory at Val is dereferenceable. (Val may
701+
/// point off the end of the dereferenceable part of the object.)
702+
static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) {
703+
assert(Val->getType()->isPointerTy());
717704

718705
const DataLayout &DL = BB->getModule()->getDataLayout();
719-
Val = GetUnderlyingObject(Val, DL);
720-
721-
LazyValueInfoCache::PerBlockValueCacheTy::iterator It;
722-
bool NeedsInit;
723-
std::tie(It, NeedsInit) = TheCache.getOrInitDereferencedPointers(BB);
724-
725-
if (NeedsInit)
706+
Value *UnderlyingVal = GetUnderlyingObject(Val, DL);
707+
// If 'GetUnderlyingObject' didn't converge, skip it. It won't converge
708+
// inside InstructionDereferencesPointer either.
709+
if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1))
726710
for (Instruction &I : *BB)
727-
AddPointersDereferencedByInstruction(&I, It->second, DL);
728-
729-
return It->second.count(Val);
711+
if (InstructionDereferencesPointer(&I, UnderlyingVal))
712+
return true;
713+
return false;
730714
}
731715

732716
bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
733-
Value *Val, BasicBlock *BB) {
717+
Value *Val, BasicBlock *BB) {
734718
ValueLatticeElement Result; // Start Undefined.
735719

736720
// If this is the entry block, we must be asking about an argument. The
737721
// value is overdefined.
738722
if (BB == &BB->getParent()->getEntryBlock()) {
739723
assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
740-
BBLV = ValueLatticeElement::getOverdefined();
724+
// Before giving up, see if we can prove the pointer non-null local to
725+
// this particular block.
726+
PointerType *PTy = dyn_cast<PointerType>(Val->getType());
727+
if (PTy &&
728+
(isKnownNonZero(Val, DL) ||
729+
(isObjectDereferencedInBlock(Val, BB) &&
730+
!NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())))) {
731+
Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
732+
} else {
733+
Result = ValueLatticeElement::getOverdefined();
734+
}
735+
BBLV = Result;
741736
return true;
742737
}
743738

@@ -763,6 +758,14 @@ bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
763758
if (Result.isOverdefined()) {
764759
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
765760
<< "' - overdefined because of pred (non local).\n");
761+
// Before giving up, see if we can prove the pointer non-null local to
762+
// this particular block.
763+
PointerType *PTy = dyn_cast<PointerType>(Val->getType());
764+
if (PTy && isObjectDereferencedInBlock(Val, BB) &&
765+
!NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())) {
766+
Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
767+
}
768+
766769
BBLV = Result;
767770
return true;
768771
}
@@ -835,24 +838,16 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
835838
// If guards are not used in the module, don't spend time looking for them
836839
auto *GuardDecl = BBI->getModule()->getFunction(
837840
Intrinsic::getName(Intrinsic::experimental_guard));
838-
if (GuardDecl && !GuardDecl->use_empty()) {
839-
if (BBI->getIterator() == BBI->getParent()->begin())
840-
return;
841-
for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()),
842-
BBI->getParent()->rend())) {
843-
Value *Cond = nullptr;
844-
if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
845-
BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
846-
}
847-
}
841+
if (!GuardDecl || GuardDecl->use_empty())
842+
return;
848843

849-
if (BBLV.isOverdefined()) {
850-
// Check whether we're checking at the terminator, and the pointer has
851-
// been dereferenced in this block.
852-
PointerType *PTy = dyn_cast<PointerType>(Val->getType());
853-
if (PTy && BBI->getParent()->getTerminator() == BBI &&
854-
isNonNullDueToDereferenceInBlock(Val, BBI->getParent()))
855-
BBLV = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
844+
if (BBI->getIterator() == BBI->getParent()->begin())
845+
return;
846+
for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()),
847+
BBI->getParent()->rend())) {
848+
Value *Cond = nullptr;
849+
if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
850+
BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
856851
}
857852
}
858853

llvm/test/Transforms/JumpThreading/combine-metadata.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ d2:
108108
d3:
109109
%y = load i32*, i32** %ptr
110110
store i32 1, i32* %y
111-
%c2 = icmp eq i32* %y, @p
111+
%c2 = icmp eq i32* %y, null
112112
br i1 %c2, label %ret1, label %ret2
113113

114114
ret1:
@@ -118,6 +118,5 @@ ret2:
118118
ret void
119119
}
120120

121-
@p = external global i32
122121

123122
!0 = !{}

0 commit comments

Comments
 (0)