@@ -151,11 +151,6 @@ namespace {
151
151
// / This is the cache kept by LazyValueInfo which
152
152
// / maintains information about queries across the clients' queries.
153
153
class LazyValueInfoCache {
154
- public:
155
- typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4 >>
156
- PerBlockValueCacheTy;
157
-
158
- private:
159
154
// / This is all of the cached block information for exactly one Value*.
160
155
// / The entries are sorted by the BasicBlock* of the
161
156
// / entries, allowing us to do a lookup with a binary search.
@@ -167,19 +162,18 @@ namespace {
167
162
SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4 > BlockVals;
168
163
};
169
164
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;
170
169
// / Keep track of all blocks that we have ever seen, so we
171
170
// / don't spend time removing unused blocks from our caches.
172
171
DenseSet<PoisoningVH<BasicBlock> > SeenBlocks;
173
172
174
173
// / This is all of the cached information for all values,
175
174
// / mapped from Value* to key information.
176
175
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;
183
177
184
178
185
179
public:
@@ -235,17 +229,11 @@ namespace {
235
229
return BBI->second ;
236
230
}
237
231
238
- std::pair<PerBlockValueCacheTy::iterator, bool >
239
- getOrInitDereferencedPointers (BasicBlock *BB) {
240
- return DereferencedPointerCache.try_emplace (BB);
241
- }
242
-
243
232
// / clear - Empty the cache.
244
233
void clear () {
245
234
SeenBlocks.clear ();
246
235
ValueCache.clear ();
247
236
OverDefinedCache.clear ();
248
- DereferencedPointerCache.clear ();
249
237
}
250
238
251
239
// / Inform the cache that a given value has been deleted.
@@ -264,22 +252,17 @@ namespace {
264
252
};
265
253
}
266
254
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;) {
270
257
// Copy and increment the iterator immediately so we can erase behind
271
258
// ourselves.
272
259
auto Iter = I++;
273
260
SmallPtrSetImpl<Value *> &ValueSet = Iter->second ;
274
261
ValueSet.erase (V);
275
262
if (ValueSet.empty ())
276
- Cache .erase (Iter);
263
+ OverDefinedCache .erase (Iter);
277
264
}
278
- }
279
265
280
- void LazyValueInfoCache::eraseValue (Value *V) {
281
- eraseValueFromPerBlockValueCache (V, OverDefinedCache);
282
- eraseValueFromPerBlockValueCache (V, DereferencedPointerCache);
283
266
ValueCache.erase (V);
284
267
}
285
268
@@ -290,17 +273,15 @@ void LVIValueHandle::deleted() {
290
273
}
291
274
292
275
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
-
297
276
// Shortcut if we have never seen this block.
298
277
DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find (BB);
299
278
if (I == SeenBlocks.end ())
300
279
return ;
301
280
SeenBlocks.erase (I);
302
281
303
- OverDefinedCache.erase (BB);
282
+ auto ODI = OverDefinedCache.find (BB);
283
+ if (ODI != OverDefinedCache.end ())
284
+ OverDefinedCache.erase (ODI);
304
285
305
286
for (auto &I : ValueCache)
306
287
I.second ->BlockVals .erase (BB);
@@ -457,7 +438,6 @@ namespace {
457
438
BasicBlock *BB);
458
439
bool solveBlockValueExtractValue (ValueLatticeElement &BBLV,
459
440
ExtractValueInst *EVI, BasicBlock *BB);
460
- bool isNonNullDueToDereferenceInBlock (Value *Val, BasicBlock *BB);
461
441
void intersectAssumeOrGuardBlockValueConstantRange (Value *Val,
462
442
ValueLatticeElement &BBLV,
463
443
Instruction *BBI);
@@ -639,6 +619,17 @@ bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
639
619
640
620
bool LazyValueInfoImpl::solveBlockValueImpl (ValueLatticeElement &Res,
641
621
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
+
642
633
// If this value is a nonnull pointer, record it's range and bailout. Note
643
634
// that for all other pointer typed values, we terminate the search at the
644
635
// definition. We could easily extend this to look through geps, bitcasts,
@@ -648,22 +639,11 @@ bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
648
639
// This does mean that we have a sensitivity to where the defining
649
640
// instruction is placed, even if it could legally be hoisted much higher.
650
641
// 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)) {
653
644
Res = ValueLatticeElement::getNot (ConstantPointerNull::get (PT));
654
645
return true ;
655
646
}
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
-
667
647
if (BBI->getType ()->isIntegerTy ()) {
668
648
if (auto *CI = dyn_cast<CastInst>(BBI))
669
649
return solveBlockValueCast (Res, CI, BB);
@@ -684,63 +664,75 @@ bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res,
684
664
return true ;
685
665
}
686
666
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) {
698
668
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 ;
704
680
705
681
// FIXME: check whether it has a valuerange that excludes zero?
706
682
ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength ());
707
- if (!Len || Len->isZero ()) return ;
683
+ if (!Len || Len->isZero ()) return false ;
708
684
709
- AddDereferencedPointer (MI->getRawDest (), PtrSet, DL);
685
+ if (MI->getDestAddressSpace () == 0 )
686
+ if (GetUnderlyingObject (MI->getRawDest (),
687
+ MI->getModule ()->getDataLayout ()) == Ptr)
688
+ return true ;
710
689
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 ;
712
694
}
695
+ return false ;
713
696
}
714
697
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 ());
720
704
721
705
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 ))
729
710
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 ;
733
714
}
734
715
735
716
bool LazyValueInfoImpl::solveBlockValueNonLocal (ValueLatticeElement &BBLV,
736
- Value *Val, BasicBlock *BB) {
717
+ Value *Val, BasicBlock *BB) {
737
718
ValueLatticeElement Result; // Start Undefined.
738
719
739
720
// If this is the entry block, we must be asking about an argument. The
740
721
// value is overdefined.
741
722
if (BB == &BB->getParent ()->getEntryBlock ()) {
742
723
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;
744
736
return true ;
745
737
}
746
738
@@ -766,6 +758,14 @@ bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV,
766
758
if (Result.isOverdefined ()) {
767
759
LLVM_DEBUG (dbgs () << " compute BB '" << BB->getName ()
768
760
<< " ' - 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
+
769
769
BBLV = Result;
770
770
return true ;
771
771
}
@@ -838,24 +838,16 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
838
838
// If guards are not used in the module, don't spend time looking for them
839
839
auto *GuardDecl = BBI->getModule ()->getFunction (
840
840
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 ;
851
843
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 ));
859
851
}
860
852
}
861
853
0 commit comments