@@ -386,7 +386,6 @@ namespace {
386
386
// / This is the cache kept by LazyValueInfo which
387
387
// / maintains information about queries across the clients' queries.
388
388
class LazyValueInfoCache {
389
- protected:
390
389
// / This is all of the cached block information for exactly one Value*.
391
390
// / The entries are sorted by the BasicBlock* of the
392
391
// / entries, allowing us to do a lookup with a binary search.
@@ -412,6 +411,7 @@ namespace {
412
411
// / don't spend time removing unused blocks from our caches.
413
412
DenseSet<AssertingVH<BasicBlock> > SeenBlocks;
414
413
414
+ public:
415
415
void insertResult (Value *Val, BasicBlock *BB, const LVILatticeVal &Result) {
416
416
SeenBlocks.insert (BB);
417
417
@@ -439,7 +439,7 @@ namespace {
439
439
return ODI->second .count (V);
440
440
}
441
441
442
- bool hasCachedValueInfo (Value *V, BasicBlock *BB) {
442
+ bool hasCachedValueInfo (Value *V, BasicBlock *BB) const {
443
443
if (isOverdefined (V, BB))
444
444
return true ;
445
445
@@ -450,7 +450,7 @@ namespace {
450
450
return I->second ->BlockVals .count (BB);
451
451
}
452
452
453
- LVILatticeVal getCachedValueInfo (Value *V, BasicBlock *BB) {
453
+ LVILatticeVal getCachedValueInfo (Value *V, BasicBlock *BB) const {
454
454
if (isOverdefined (V, BB))
455
455
return LVILatticeVal::getOverdefined ();
456
456
@@ -463,7 +463,6 @@ namespace {
463
463
return BBI->second ;
464
464
}
465
465
466
- public:
467
466
// / clear - Empty the cache.
468
467
void clear () {
469
468
SeenBlocks.clear ();
@@ -478,6 +477,11 @@ namespace {
478
477
// / that a block has been deleted.
479
478
void eraseBlock (BasicBlock *BB);
480
479
480
+ // / Updates the cache to remove any influence an overdefined value in
481
+ // / OldSucc might have (unless also overdefined in NewSucc). This just
482
+ // / flushes elements from the cache and does not add any.
483
+ void threadEdgeImpl (BasicBlock *OldSucc,BasicBlock *NewSucc);
484
+
481
485
friend struct LVIValueHandle ;
482
486
};
483
487
}
@@ -518,11 +522,70 @@ void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
518
522
I.second ->BlockVals .erase (BB);
519
523
}
520
524
525
+ void LazyValueInfoCache::threadEdgeImpl (BasicBlock *OldSucc,
526
+ BasicBlock *NewSucc) {
527
+ // When an edge in the graph has been threaded, values that we could not
528
+ // determine a value for before (i.e. were marked overdefined) may be
529
+ // possible to solve now. We do NOT try to proactively update these values.
530
+ // Instead, we clear their entries from the cache, and allow lazy updating to
531
+ // recompute them when needed.
532
+
533
+ // The updating process is fairly simple: we need to drop cached info
534
+ // for all values that were marked overdefined in OldSucc, and for those same
535
+ // values in any successor of OldSucc (except NewSucc) in which they were
536
+ // also marked overdefined.
537
+ std::vector<BasicBlock*> worklist;
538
+ worklist.push_back (OldSucc);
539
+
540
+ auto I = OverDefinedCache.find (OldSucc);
541
+ if (I == OverDefinedCache.end ())
542
+ return ; // Nothing to process here.
543
+ SmallVector<Value *, 4 > ValsToClear (I->second .begin (), I->second .end ());
544
+
545
+ // Use a worklist to perform a depth-first search of OldSucc's successors.
546
+ // NOTE: We do not need a visited list since any blocks we have already
547
+ // visited will have had their overdefined markers cleared already, and we
548
+ // thus won't loop to their successors.
549
+ while (!worklist.empty ()) {
550
+ BasicBlock *ToUpdate = worklist.back ();
551
+ worklist.pop_back ();
552
+
553
+ // Skip blocks only accessible through NewSucc.
554
+ if (ToUpdate == NewSucc) continue ;
555
+
556
+ bool changed = false ;
557
+ for (Value *V : ValsToClear) {
558
+ // If a value was marked overdefined in OldSucc, and is here too...
559
+ auto OI = OverDefinedCache.find (ToUpdate);
560
+ if (OI == OverDefinedCache.end ())
561
+ continue ;
562
+ SmallPtrSetImpl<Value *> &ValueSet = OI->second ;
563
+ if (!ValueSet.count (V))
564
+ continue ;
565
+
566
+ ValueSet.erase (V);
567
+ if (ValueSet.empty ())
568
+ OverDefinedCache.erase (OI);
569
+
570
+ // If we removed anything, then we potentially need to update
571
+ // blocks successors too.
572
+ changed = true ;
573
+ }
574
+
575
+ if (!changed) continue ;
576
+
577
+ worklist.insert (worklist.end (), succ_begin (ToUpdate), succ_end (ToUpdate));
578
+ }
579
+ }
580
+
521
581
namespace {
522
582
// The actual implementation of the lazy analysis and update. Note that the
523
583
// inheritance from LazyValueInfoCache is intended to be temporary while
524
584
// splitting the code and then transitioning to a has-a relationship.
525
- class LazyValueInfoImpl : public LazyValueInfoCache {
585
+ class LazyValueInfoImpl {
586
+
587
+ // / Cached results from previous queries
588
+ LazyValueInfoCache TheCache;
526
589
527
590
// / This stack holds the state of the value solver during a query.
528
591
// / It basically emulates the callstack of the naive
@@ -587,6 +650,17 @@ namespace {
587
650
LVILatticeVal getValueOnEdge (Value *V, BasicBlock *FromBB,BasicBlock *ToBB,
588
651
Instruction *CxtI = nullptr );
589
652
653
+ // / Complete flush all previously computed values
654
+ void clear () {
655
+ TheCache.clear ();
656
+ }
657
+
658
+ // / This is part of the update interface to inform the cache
659
+ // / that a block has been deleted.
660
+ void eraseBlock (BasicBlock *BB) {
661
+ TheCache.eraseBlock (BB);
662
+ }
663
+
590
664
// / This is the update interface to inform the cache that an edge from
591
665
// / PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
592
666
void threadEdge (BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
@@ -605,11 +679,11 @@ void LazyValueInfoImpl::solve() {
605
679
if (solveBlockValue (e.second , e.first )) {
606
680
// The work item was completely processed.
607
681
assert (BlockValueStack.top () == e && " Nothing should have been pushed!" );
608
- assert (hasCachedValueInfo (e.second , e.first ) &&
682
+ assert (TheCache. hasCachedValueInfo (e.second , e.first ) &&
609
683
" Result should be in cache!" );
610
684
611
685
DEBUG (dbgs () << " POP " << *e.second << " in " << e.first ->getName ()
612
- << " = " << getCachedValueInfo (e.second , e.first ) << " \n " );
686
+ << " = " << TheCache. getCachedValueInfo (e.second , e.first ) << " \n " );
613
687
614
688
BlockValueStack.pop ();
615
689
BlockValueSet.erase (e);
@@ -625,16 +699,15 @@ bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) {
625
699
if (isa<Constant>(Val))
626
700
return true ;
627
701
628
- return hasCachedValueInfo (Val, BB);
702
+ return TheCache. hasCachedValueInfo (Val, BB);
629
703
}
630
704
631
705
LVILatticeVal LazyValueInfoImpl::getBlockValue (Value *Val, BasicBlock *BB) {
632
706
// If already a constant, there is nothing to compute.
633
707
if (Constant *VC = dyn_cast<Constant>(Val))
634
708
return LVILatticeVal::get (VC);
635
709
636
- SeenBlocks.insert (BB);
637
- return getCachedValueInfo (Val, BB);
710
+ return TheCache.getCachedValueInfo (Val, BB);
638
711
}
639
712
640
713
static LVILatticeVal getFromRangeMetadata (Instruction *BBI) {
@@ -657,10 +730,10 @@ bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
657
730
if (isa<Constant>(Val))
658
731
return true ;
659
732
660
- if (hasCachedValueInfo (Val, BB)) {
733
+ if (TheCache. hasCachedValueInfo (Val, BB)) {
661
734
// If we have a cached value, use that.
662
735
DEBUG (dbgs () << " reuse BB '" << BB->getName ()
663
- << " ' val=" << getCachedValueInfo (Val, BB) << ' \n ' );
736
+ << " ' val=" << TheCache. getCachedValueInfo (Val, BB) << ' \n ' );
664
737
665
738
// Since we're reusing a cached value, we don't need to update the
666
739
// OverDefinedCache. The cache will have been properly updated whenever the
@@ -676,21 +749,21 @@ bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
676
749
if (!BBI || BBI->getParent () != BB) {
677
750
if (!solveBlockValueNonLocal (Res, Val, BB))
678
751
return false ;
679
- insertResult (Val, BB, Res);
752
+ TheCache. insertResult (Val, BB, Res);
680
753
return true ;
681
754
}
682
755
683
756
if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
684
757
if (!solveBlockValuePHINode (Res, PN, BB))
685
758
return false ;
686
- insertResult (Val, BB, Res);
759
+ TheCache. insertResult (Val, BB, Res);
687
760
return true ;
688
761
}
689
762
690
763
if (auto *SI = dyn_cast<SelectInst>(BBI)) {
691
764
if (!solveBlockValueSelect (Res, SI, BB))
692
765
return false ;
693
- insertResult (Val, BB, Res);
766
+ TheCache. insertResult (Val, BB, Res);
694
767
return true ;
695
768
}
696
769
@@ -706,29 +779,29 @@ bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) {
706
779
PointerType *PT = dyn_cast<PointerType>(BBI->getType ());
707
780
if (PT && isKnownNonNull (BBI)) {
708
781
Res = LVILatticeVal::getNot (ConstantPointerNull::get (PT));
709
- insertResult (Val, BB, Res);
782
+ TheCache. insertResult (Val, BB, Res);
710
783
return true ;
711
784
}
712
785
if (BBI->getType ()->isIntegerTy ()) {
713
786
if (isa<CastInst>(BBI)) {
714
787
if (!solveBlockValueCast (Res, BBI, BB))
715
788
return false ;
716
- insertResult (Val, BB, Res);
789
+ TheCache. insertResult (Val, BB, Res);
717
790
return true ;
718
791
}
719
792
BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI);
720
793
if (BO && isa<ConstantInt>(BO->getOperand (1 ))) {
721
794
if (!solveBlockValueBinaryOp (Res, BBI, BB))
722
795
return false ;
723
- insertResult (Val, BB, Res);
796
+ TheCache. insertResult (Val, BB, Res);
724
797
return true ;
725
798
}
726
799
}
727
800
728
801
DEBUG (dbgs () << " compute BB '" << BB->getName ()
729
802
<< " ' - unknown inst def found.\n " );
730
803
Res = getFromRangeMetadata (BBI);
731
- insertResult (Val, BB, Res);
804
+ TheCache. insertResult (Val, BB, Res);
732
805
return true ;
733
806
}
734
807
@@ -1465,59 +1538,8 @@ getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB,
1465
1538
}
1466
1539
1467
1540
void LazyValueInfoImpl::threadEdge (BasicBlock *PredBB, BasicBlock *OldSucc,
1468
- BasicBlock *NewSucc) {
1469
- // When an edge in the graph has been threaded, values that we could not
1470
- // determine a value for before (i.e. were marked overdefined) may be
1471
- // possible to solve now. We do NOT try to proactively update these values.
1472
- // Instead, we clear their entries from the cache, and allow lazy updating to
1473
- // recompute them when needed.
1474
-
1475
- // The updating process is fairly simple: we need to drop cached info
1476
- // for all values that were marked overdefined in OldSucc, and for those same
1477
- // values in any successor of OldSucc (except NewSucc) in which they were
1478
- // also marked overdefined.
1479
- std::vector<BasicBlock*> worklist;
1480
- worklist.push_back (OldSucc);
1481
-
1482
- auto I = OverDefinedCache.find (OldSucc);
1483
- if (I == OverDefinedCache.end ())
1484
- return ; // Nothing to process here.
1485
- SmallVector<Value *, 4 > ValsToClear (I->second .begin (), I->second .end ());
1486
-
1487
- // Use a worklist to perform a depth-first search of OldSucc's successors.
1488
- // NOTE: We do not need a visited list since any blocks we have already
1489
- // visited will have had their overdefined markers cleared already, and we
1490
- // thus won't loop to their successors.
1491
- while (!worklist.empty ()) {
1492
- BasicBlock *ToUpdate = worklist.back ();
1493
- worklist.pop_back ();
1494
-
1495
- // Skip blocks only accessible through NewSucc.
1496
- if (ToUpdate == NewSucc) continue ;
1497
-
1498
- bool changed = false ;
1499
- for (Value *V : ValsToClear) {
1500
- // If a value was marked overdefined in OldSucc, and is here too...
1501
- auto OI = OverDefinedCache.find (ToUpdate);
1502
- if (OI == OverDefinedCache.end ())
1503
- continue ;
1504
- SmallPtrSetImpl<Value *> &ValueSet = OI->second ;
1505
- if (!ValueSet.count (V))
1506
- continue ;
1507
-
1508
- ValueSet.erase (V);
1509
- if (ValueSet.empty ())
1510
- OverDefinedCache.erase (OI);
1511
-
1512
- // If we removed anything, then we potentially need to update
1513
- // blocks successors too.
1514
- changed = true ;
1515
- }
1516
-
1517
- if (!changed) continue ;
1518
-
1519
- worklist.insert (worklist.end (), succ_begin (ToUpdate), succ_end (ToUpdate));
1520
- }
1541
+ BasicBlock *NewSucc) {
1542
+ TheCache.threadEdgeImpl (OldSucc, NewSucc);
1521
1543
}
1522
1544
1523
1545
// ===----------------------------------------------------------------------===//
0 commit comments