Skip to content

Commit 7a3ad48

Browse files
committed
Temporarily Revert "Reapply [LVI] Normalize pointer behavior" as it's broken python 3.6.
Reverting to figure out if it's a problem in python or the compiler for now. This reverts commit 885a05f.
1 parent 056c319 commit 7a3ad48

File tree

2 files changed

+90
-99
lines changed

2 files changed

+90
-99
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 89 additions & 97 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

@@ -290,17 +273,15 @@ void LVIValueHandle::deleted() {
290273
}
291274

292275
void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
293-
// The SeenBlocks shortcut applies only to the value caches,
294-
// always clear the dereferenced pointer cache.
295-
DereferencedPointerCache.erase(BB);
296-
297276
// Shortcut if we have never seen this block.
298277
DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB);
299278
if (I == SeenBlocks.end())
300279
return;
301280
SeenBlocks.erase(I);
302281

303-
OverDefinedCache.erase(BB);
282+
auto ODI = OverDefinedCache.find(BB);
283+
if (ODI != OverDefinedCache.end())
284+
OverDefinedCache.erase(ODI);
304285

305286
for (auto &I : ValueCache)
306287
I.second->BlockVals.erase(BB);
@@ -457,7 +438,6 @@ namespace {
457438
BasicBlock *BB);
458439
bool solveBlockValueExtractValue(ValueLatticeElement &BBLV,
459440
ExtractValueInst *EVI, BasicBlock *BB);
460-
bool isNonNullDueToDereferenceInBlock(Value *Val, BasicBlock *BB);
461441
void intersectAssumeOrGuardBlockValueConstantRange(Value *Val,
462442
ValueLatticeElement &BBLV,
463443
Instruction *BBI);
@@ -639,6 +619,17 @@ bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
639619

640620
bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
641621
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+
642633
// If this value is a nonnull pointer, record it's range and bailout. Note
643634
// that for all other pointer typed values, we terminate the search at the
644635
// definition. We could easily extend this to look through geps, bitcasts,
@@ -648,22 +639,11 @@ bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
648639
// This does mean that we have a sensitivity to where the defining
649640
// instruction is placed, even if it could legally be hoisted much higher.
650641
// That is unfortunate.
651-
PointerType *PT = dyn_cast<PointerType>(Val->getType());
652-
if (PT && isKnownNonZero(Val, DL)) {
642+
PointerType *PT = dyn_cast<PointerType>(BBI->getType());
643+
if (PT && isKnownNonZero(BBI, DL)) {
653644
Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT));
654645
return true;
655646
}
656-
657-
Instruction *BBI = dyn_cast<Instruction>(Val);
658-
if (!BBI || BBI->getParent() != BB)
659-
return solveBlockValueNonLocal(Res, Val, BB);
660-
661-
if (PHINode *PN = dyn_cast<PHINode>(BBI))
662-
return solveBlockValuePHINode(Res, PN, BB);
663-
664-
if (auto *SI = dyn_cast<SelectInst>(BBI))
665-
return solveBlockValueSelect(Res, SI, BB);
666-
667647
if (BBI->getType()->isIntegerTy()) {
668648
if (auto *CI = dyn_cast<CastInst>(BBI))
669649
return solveBlockValueCast(Res, CI, BB);
@@ -684,63 +664,75 @@ bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
684664
return true;
685665
}
686666

687-
static void AddDereferencedPointer(
688-
Value *Ptr, SmallPtrSet<Value *, 4> &PtrSet, const DataLayout &DL) {
689-
// TODO: Use NullPointerIsDefined instead.
690-
if (Ptr->getType()->getPointerAddressSpace() == 0) {
691-
Ptr = GetUnderlyingObject(Ptr, DL);
692-
PtrSet.insert(Ptr);
693-
}
694-
}
695-
696-
static void AddPointersDereferencedByInstruction(
697-
Instruction *I, SmallPtrSet<Value *, 4> &PtrSet, const DataLayout &DL) {
667+
static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) {
698668
if (LoadInst *L = dyn_cast<LoadInst>(I)) {
699-
AddDereferencedPointer(L->getPointerOperand(), PtrSet, DL);
700-
} else if (StoreInst *S = dyn_cast<StoreInst>(I)) {
701-
AddDereferencedPointer(S->getPointerOperand(), PtrSet, DL);
702-
} else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
703-
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;
704680

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

709-
AddDereferencedPointer(MI->getRawDest(), PtrSet, DL);
685+
if (MI->getDestAddressSpace() == 0)
686+
if (GetUnderlyingObject(MI->getRawDest(),
687+
MI->getModule()->getDataLayout()) == Ptr)
688+
return true;
710689
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
711-
AddDereferencedPointer(MTI->getRawSource(), PtrSet, DL);
690+
if (MTI->getSourceAddressSpace() == 0)
691+
if (GetUnderlyingObject(MTI->getRawSource(),
692+
MTI->getModule()->getDataLayout()) == Ptr)
693+
return true;
712694
}
695+
return false;
713696
}
714697

715-
bool LazyValueInfoImpl::isNonNullDueToDereferenceInBlock(
716-
Value *Val, BasicBlock *BB) {
717-
if (NullPointerIsDefined(BB->getParent(),
718-
Val->getType()->getPointerAddressSpace()))
719-
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());
720704

721705
const DataLayout &DL = BB->getModule()->getDataLayout();
722-
Val = GetUnderlyingObject(Val, DL);
723-
724-
LazyValueInfoCache::PerBlockValueCacheTy::iterator It;
725-
bool NeedsInit;
726-
std::tie(It, NeedsInit) = TheCache.getOrInitDereferencedPointers(BB);
727-
728-
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))
729710
for (Instruction &I : *BB)
730-
AddPointersDereferencedByInstruction(&I, It->second, DL);
731-
732-
return It->second.count(Val);
711+
if (InstructionDereferencesPointer(&I, UnderlyingVal))
712+
return true;
713+
return false;
733714
}
734715

735716
bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
736-
Value *Val, BasicBlock *BB) {
717+
Value *Val, BasicBlock *BB) {
737718
ValueLatticeElement Result; // Start Undefined.
738719

739720
// If this is the entry block, we must be asking about an argument. The
740721
// value is overdefined.
741722
if (BB == &BB->getParent()->getEntryBlock()) {
742723
assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
743-
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;
744736
return true;
745737
}
746738

@@ -766,6 +758,14 @@ bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
766758
if (Result.isOverdefined()) {
767759
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
768760
<< "' - 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+
769769
BBLV = Result;
770770
return true;
771771
}
@@ -838,24 +838,16 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
838838
// If guards are not used in the module, don't spend time looking for them
839839
auto *GuardDecl = BBI->getModule()->getFunction(
840840
Intrinsic::getName(Intrinsic::experimental_guard));
841-
if (GuardDecl && !GuardDecl->use_empty()) {
842-
if (BBI->getIterator() == BBI->getParent()->begin())
843-
return;
844-
for (Instruction &I : make_range(std::next(BBI->getIterator().getReverse()),
845-
BBI->getParent()->rend())) {
846-
Value *Cond = nullptr;
847-
if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond))))
848-
BBLV = intersect(BBLV, getValueFromCondition(Val, Cond));
849-
}
850-
}
841+
if (!GuardDecl || GuardDecl->use_empty())
842+
return;
851843

852-
if (BBLV.isOverdefined()) {
853-
// Check whether we're checking at the terminator, and the pointer has
854-
// been dereferenced in this block.
855-
PointerType *PTy = dyn_cast<PointerType>(Val->getType());
856-
if (PTy && BBI->getParent()->getTerminator() == BBI &&
857-
isNonNullDueToDereferenceInBlock(Val, BBI->getParent()))
858-
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));
859851
}
860852
}
861853

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)