@@ -725,6 +725,69 @@ void GVNPass::ValueTable::verifyRemoved(const Value *V) const {
725
725
" Inst still occurs in value numbering map!" );
726
726
}
727
727
728
+ // ===----------------------------------------------------------------------===//
729
+ // LeaderMap External Functions
730
+ // ===----------------------------------------------------------------------===//
731
+
732
+ // / Push a new Value to the LeaderTable onto the list for its value number.
733
+ void GVNPass::LeaderMap::insert (uint32_t N, Value *V, const BasicBlock *BB) {
734
+ LeaderListNode &Curr = NumToLeaders[N];
735
+ if (!Curr.Entry .Val ) {
736
+ Curr.Entry .Val = V;
737
+ Curr.Entry .BB = BB;
738
+ return ;
739
+ }
740
+
741
+ LeaderListNode *Node = TableAllocator.Allocate <LeaderListNode>();
742
+ Node->Entry .Val = V;
743
+ Node->Entry .BB = BB;
744
+ Node->Next = Curr.Next ;
745
+ Curr.Next = Node;
746
+ }
747
+
748
+ // / Scan the list of values corresponding to a given
749
+ // / value number, and remove the given instruction if encountered.
750
+ void GVNPass::LeaderMap::erase (uint32_t N, Instruction *I,
751
+ const BasicBlock *BB) {
752
+ LeaderListNode *Prev = nullptr ;
753
+ LeaderListNode *Curr = &NumToLeaders[N];
754
+
755
+ while (Curr && (Curr->Entry .Val != I || Curr->Entry .BB != BB)) {
756
+ Prev = Curr;
757
+ Curr = Curr->Next ;
758
+ }
759
+
760
+ if (!Curr)
761
+ return ;
762
+
763
+ if (Prev) {
764
+ Prev->Next = Curr->Next ;
765
+ } else {
766
+ if (!Curr->Next ) {
767
+ Curr->Entry .Val = nullptr ;
768
+ Curr->Entry .BB = nullptr ;
769
+ } else {
770
+ LeaderListNode *Next = Curr->Next ;
771
+ Curr->Entry .Val = Next->Entry .Val ;
772
+ Curr->Entry .BB = Next->Entry .BB ;
773
+ Curr->Next = Next->Next ;
774
+ }
775
+ }
776
+ }
777
+
778
+ void GVNPass::LeaderMap::verifyRemoved (const Value *V) const {
779
+ // Walk through the value number scope to make sure the instruction isn't
780
+ // ferreted away in it.
781
+ for (const auto &I : NumToLeaders) {
782
+ (void )I;
783
+ assert (I.second .Entry .Val != V && " Inst still in value numbering scope!" );
784
+ assert (
785
+ std::none_of (leader_iterator (&I.second ), leader_iterator (nullptr ),
786
+ [=](const LeaderTableEntry &E) { return E.Val == V; }) &&
787
+ " Inst still in value numbering scope!" );
788
+ }
789
+ }
790
+
728
791
// ===----------------------------------------------------------------------===//
729
792
// GVN Pass
730
793
// ===----------------------------------------------------------------------===//
@@ -1467,7 +1530,7 @@ void GVNPass::eliminatePartiallyRedundantLoad(
1467
1530
OldLoad->replaceAllUsesWith (NewLoad);
1468
1531
replaceValuesPerBlockEntry (ValuesPerBlock, OldLoad, NewLoad);
1469
1532
if (uint32_t ValNo = VN.lookup (OldLoad, false ))
1470
- removeFromLeaderTable (ValNo, OldLoad, OldLoad->getParent ());
1533
+ LeaderTable. erase (ValNo, OldLoad, OldLoad->getParent ());
1471
1534
VN.erase (OldLoad);
1472
1535
removeInstruction (OldLoad);
1473
1536
}
@@ -2204,10 +2267,9 @@ GVNPass::ValueTable::assignExpNewValueNum(Expression &Exp) {
2204
2267
// / defined in \p BB.
2205
2268
bool GVNPass::ValueTable::areAllValsInBB (uint32_t Num, const BasicBlock *BB,
2206
2269
GVNPass &Gvn) {
2207
- LeaderTableEntry *Vals = &Gvn.LeaderTable [Num];
2208
- while (Vals && Vals->BB == BB)
2209
- Vals = Vals->Next ;
2210
- return !Vals;
2270
+ return all_of (
2271
+ Gvn.LeaderTable .getLeaders (Num),
2272
+ [=](const LeaderMap::LeaderTableEntry &L) { return L.BB == BB; });
2211
2273
}
2212
2274
2213
2275
// / Wrap phiTranslateImpl to provide caching functionality.
@@ -2229,12 +2291,11 @@ bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2229
2291
const BasicBlock *PhiBlock,
2230
2292
GVNPass &Gvn) {
2231
2293
CallInst *Call = nullptr ;
2232
- LeaderTableEntry *Vals = & Gvn.LeaderTable [ Num] ;
2233
- while (Vals ) {
2234
- Call = dyn_cast<CallInst>(Vals-> Val );
2294
+ auto Leaders = Gvn.LeaderTable . getLeaders ( Num) ;
2295
+ for ( const auto &Entry : Leaders ) {
2296
+ Call = dyn_cast<CallInst>(Entry. Val );
2235
2297
if (Call && Call->getParent () == PhiBlock)
2236
2298
break ;
2237
- Vals = Vals->Next ;
2238
2299
}
2239
2300
2240
2301
if (AA->doesNotAccessMemory (Call))
@@ -2327,23 +2388,17 @@ void GVNPass::ValueTable::eraseTranslateCacheEntry(
2327
2388
// question. This is fast because dominator tree queries consist of only
2328
2389
// a few comparisons of DFS numbers.
2329
2390
Value *GVNPass::findLeader (const BasicBlock *BB, uint32_t num) {
2330
- LeaderTableEntry Vals = LeaderTable[num];
2331
- if (!Vals.Val ) return nullptr ;
2391
+ auto Leaders = LeaderTable.getLeaders (num);
2392
+ if (Leaders.empty ())
2393
+ return nullptr ;
2332
2394
2333
2395
Value *Val = nullptr ;
2334
- if (DT->dominates (Vals.BB , BB)) {
2335
- Val = Vals.Val ;
2336
- if (isa<Constant>(Val)) return Val;
2337
- }
2338
-
2339
- LeaderTableEntry* Next = Vals.Next ;
2340
- while (Next) {
2341
- if (DT->dominates (Next->BB , BB)) {
2342
- if (isa<Constant>(Next->Val )) return Next->Val ;
2343
- if (!Val) Val = Next->Val ;
2396
+ for (const auto &Entry : Leaders) {
2397
+ if (DT->dominates (Entry.BB , BB)) {
2398
+ Val = Entry.Val ;
2399
+ if (isa<Constant>(Val))
2400
+ return Val;
2344
2401
}
2345
-
2346
- Next = Next->Next ;
2347
2402
}
2348
2403
2349
2404
return Val;
@@ -2452,7 +2507,7 @@ bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
2452
2507
// have the simple case where the edge dominates the end.
2453
2508
if (RootDominatesEnd && !isa<Instruction>(RHS) &&
2454
2509
canReplacePointersIfEqual (LHS, RHS, DL))
2455
- addToLeaderTable (LVN, RHS, Root.getEnd ());
2510
+ LeaderTable. insert (LVN, RHS, Root.getEnd ());
2456
2511
2457
2512
// Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
2458
2513
// LHS always has at least one use that is not dominated by Root, this will
@@ -2546,7 +2601,7 @@ bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
2546
2601
// The leader table only tracks basic blocks, not edges. Only add to if we
2547
2602
// have the simple case where the edge dominates the end.
2548
2603
if (RootDominatesEnd)
2549
- addToLeaderTable (Num, NotVal, Root.getEnd ());
2604
+ LeaderTable. insert (Num, NotVal, Root.getEnd ());
2550
2605
2551
2606
continue ;
2552
2607
}
@@ -2596,7 +2651,7 @@ bool GVNPass::processInstruction(Instruction *I) {
2596
2651
return true ;
2597
2652
2598
2653
unsigned Num = VN.lookupOrAdd (Load);
2599
- addToLeaderTable (Num, Load, Load->getParent ());
2654
+ LeaderTable. insert (Num, Load, Load->getParent ());
2600
2655
return false ;
2601
2656
}
2602
2657
@@ -2664,15 +2719,15 @@ bool GVNPass::processInstruction(Instruction *I) {
2664
2719
// Allocations are always uniquely numbered, so we can save time and memory
2665
2720
// by fast failing them.
2666
2721
if (isa<AllocaInst>(I) || I->isTerminator () || isa<PHINode>(I)) {
2667
- addToLeaderTable (Num, I, I->getParent ());
2722
+ LeaderTable. insert (Num, I, I->getParent ());
2668
2723
return false ;
2669
2724
}
2670
2725
2671
2726
// If the number we were assigned was a brand new VN, then we don't
2672
2727
// need to do a lookup to see if the number already exists
2673
2728
// somewhere in the domtree: it can't!
2674
2729
if (Num >= NextNum) {
2675
- addToLeaderTable (Num, I, I->getParent ());
2730
+ LeaderTable. insert (Num, I, I->getParent ());
2676
2731
return false ;
2677
2732
}
2678
2733
@@ -2681,7 +2736,7 @@ bool GVNPass::processInstruction(Instruction *I) {
2681
2736
Value *Repl = findLeader (I->getParent (), Num);
2682
2737
if (!Repl) {
2683
2738
// Failure, just remember this instance for future use.
2684
- addToLeaderTable (Num, I, I->getParent ());
2739
+ LeaderTable. insert (Num, I, I->getParent ());
2685
2740
return false ;
2686
2741
}
2687
2742
@@ -2876,7 +2931,7 @@ bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
2876
2931
VN.add (Instr, Num);
2877
2932
2878
2933
// Update the availability map to include the new instruction.
2879
- addToLeaderTable (Num, Instr, Pred);
2934
+ LeaderTable. insert (Num, Instr, Pred);
2880
2935
return true ;
2881
2936
}
2882
2937
@@ -3027,13 +3082,13 @@ bool GVNPass::performScalarPRE(Instruction *CurInst) {
3027
3082
// After creating a new PHI for ValNo, the phi translate result for ValNo will
3028
3083
// be changed, so erase the related stale entries in phi translate cache.
3029
3084
VN.eraseTranslateCacheEntry (ValNo, *CurrentBlock);
3030
- addToLeaderTable (ValNo, Phi, CurrentBlock);
3085
+ LeaderTable. insert (ValNo, Phi, CurrentBlock);
3031
3086
Phi->setDebugLoc (CurInst->getDebugLoc ());
3032
3087
CurInst->replaceAllUsesWith (Phi);
3033
3088
if (MD && Phi->getType ()->isPtrOrPtrVectorTy ())
3034
3089
MD->invalidateCachedPointerInfo (Phi);
3035
3090
VN.erase (CurInst);
3036
- removeFromLeaderTable (ValNo, CurInst, CurrentBlock);
3091
+ LeaderTable. erase (ValNo, CurInst, CurrentBlock);
3037
3092
3038
3093
LLVM_DEBUG (dbgs () << " GVN PRE removed: " << *CurInst << ' \n ' );
3039
3094
removeInstruction (CurInst);
@@ -3127,7 +3182,6 @@ void GVNPass::cleanupGlobalSets() {
3127
3182
VN.clear ();
3128
3183
LeaderTable.clear ();
3129
3184
BlockRPONumber.clear ();
3130
- TableAllocator.Reset ();
3131
3185
ICF->clear ();
3132
3186
InvalidBlockRPONumbers = true ;
3133
3187
}
@@ -3147,18 +3201,7 @@ void GVNPass::removeInstruction(Instruction *I) {
3147
3201
// / internal data structures.
3148
3202
void GVNPass::verifyRemoved (const Instruction *Inst) const {
3149
3203
VN.verifyRemoved (Inst);
3150
-
3151
- // Walk through the value number scope to make sure the instruction isn't
3152
- // ferreted away in it.
3153
- for (const auto &I : LeaderTable) {
3154
- const LeaderTableEntry *Node = &I.second ;
3155
- assert (Node->Val != Inst && " Inst still in value numbering scope!" );
3156
-
3157
- while (Node->Next ) {
3158
- Node = Node->Next ;
3159
- assert (Node->Val != Inst && " Inst still in value numbering scope!" );
3160
- }
3161
- }
3204
+ LeaderTable.verifyRemoved (Inst);
3162
3205
}
3163
3206
3164
3207
// / BB is declared dead, which implied other blocks become dead as well. This
@@ -3285,7 +3328,7 @@ void GVNPass::assignValNumForDeadCode() {
3285
3328
for (BasicBlock *BB : DeadBlocks) {
3286
3329
for (Instruction &Inst : *BB) {
3287
3330
unsigned ValNum = VN.lookupOrAdd (&Inst);
3288
- addToLeaderTable (ValNum, &Inst, BB);
3331
+ LeaderTable. insert (ValNum, &Inst, BB);
3289
3332
}
3290
3333
}
3291
3334
}
0 commit comments