@@ -474,6 +474,19 @@ void GVNPass::ValueTable::add(Value *V, uint32_t Num) {
474
474
NumberingPhi[Num] = PN;
475
475
}
476
476
477
+ // / Include the incoming memory state into the hash of the expression for the
478
+ // / given instruction. If the incoming memory state is:
479
+ // / * LiveOnEntry, add the value number of the entry block,
480
+ // / * a MemoryPhi, add the value number of the basic block corresponding to that
481
+ // / MemoryPhi,
482
+ // / * a MemoryDef, add the value number of the memory setting instruction.
483
+ void GVNPass::ValueTable::addMemoryStateToExp (Instruction *I, Expression &Exp) {
484
+ assert (MSSA && " addMemoryStateToExp should not be called without MemorySSA" );
485
+ assert (MSSA->getMemoryAccess (I) && " Instruction does not access memory" );
486
+ MemoryAccess *MA = MSSA->getSkipSelfWalker ()->getClobberingMemoryAccess (I);
487
+ Exp.VarArgs .push_back (lookupOrAdd (MA));
488
+ }
489
+
477
490
uint32_t GVNPass::ValueTable::lookupOrAddCall (CallInst *C) {
478
491
// FIXME: Currently the calls which may access the thread id may
479
492
// be considered as not accessing the memory. But this is
@@ -594,15 +607,48 @@ uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
594
607
return V;
595
608
}
596
609
610
+ if (MSSA && IsMSSAEnabled && AA->onlyReadsMemory (C)) {
611
+ Expression Exp = createExpr (C);
612
+ addMemoryStateToExp (C, Exp);
613
+ auto [V, _] = assignExpNewValueNum (Exp);
614
+ ValueNumbering[C] = V;
615
+ return V;
616
+ }
617
+
597
618
ValueNumbering[C] = NextValueNumber;
598
619
return NextValueNumber++;
599
620
}
600
621
622
+ // / Returns the value number for the specified load or store instruction.
623
+ uint32_t GVNPass::ValueTable::computeLoadStoreVN (Instruction *I) {
624
+ if (!MSSA || !IsMSSAEnabled) {
625
+ ValueNumbering[I] = NextValueNumber;
626
+ return NextValueNumber++;
627
+ }
628
+
629
+ Expression Exp;
630
+ Exp.Ty = I->getType ();
631
+ Exp.Opcode = I->getOpcode ();
632
+ for (Use &Op : I->operands ())
633
+ Exp.VarArgs .push_back (lookupOrAdd (Op));
634
+ addMemoryStateToExp (I, Exp);
635
+
636
+ auto [V, _] = assignExpNewValueNum (Exp);
637
+ ValueNumbering[I] = V;
638
+ return V;
639
+ }
640
+
601
641
// / Returns true if a value number exists for the specified value.
602
642
bool GVNPass::ValueTable::exists (Value *V) const {
603
643
return ValueNumbering.contains (V);
604
644
}
605
645
646
+ uint32_t GVNPass::ValueTable::lookupOrAdd (MemoryAccess *MA) {
647
+ return MSSA->isLiveOnEntryDef (MA) || isa<MemoryPhi>(MA)
648
+ ? lookupOrAdd (MA->getBlock ())
649
+ : lookupOrAdd (cast<MemoryUseOrDef>(MA)->getMemoryInst ());
650
+ }
651
+
606
652
// / lookupOrAdd - Returns the value number for the specified value, assigning
607
653
// / it a new number if it did not have one before.
608
654
uint32_t GVNPass::ValueTable::lookupOrAdd (Value *V) {
@@ -613,6 +659,8 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
613
659
auto *I = dyn_cast<Instruction>(V);
614
660
if (!I) {
615
661
ValueNumbering[V] = NextValueNumber;
662
+ if (isa<BasicBlock>(V))
663
+ NumberingBB[NextValueNumber] = cast<BasicBlock>(V);
616
664
return NextValueNumber++;
617
665
}
618
666
@@ -672,6 +720,9 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
672
720
ValueNumbering[V] = NextValueNumber;
673
721
NumberingPhi[NextValueNumber] = cast<PHINode>(V);
674
722
return NextValueNumber++;
723
+ case Instruction::Load:
724
+ case Instruction::Store:
725
+ return computeLoadStoreVN (I);
675
726
default :
676
727
ValueNumbering[V] = NextValueNumber;
677
728
return NextValueNumber++;
@@ -709,6 +760,7 @@ void GVNPass::ValueTable::clear() {
709
760
ValueNumbering.clear ();
710
761
ExpressionNumbering.clear ();
711
762
NumberingPhi.clear ();
763
+ NumberingBB.clear ();
712
764
PhiTranslateTable.clear ();
713
765
NextValueNumber = 1 ;
714
766
Expressions.clear ();
@@ -723,6 +775,8 @@ void GVNPass::ValueTable::erase(Value *V) {
723
775
// If V is PHINode, V <--> value number is an one-to-one mapping.
724
776
if (isa<PHINode>(V))
725
777
NumberingPhi.erase (Num);
778
+ else if (isa<BasicBlock>(V))
779
+ NumberingBB.erase (Num);
726
780
}
727
781
728
782
// / verifyRemoved - Verify that the value is removed from all internal data
@@ -2310,15 +2364,39 @@ bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2310
2364
uint32_t GVNPass::ValueTable::phiTranslateImpl (const BasicBlock *Pred,
2311
2365
const BasicBlock *PhiBlock,
2312
2366
uint32_t Num, GVNPass &GVN) {
2367
+ // See if we can refine the value number by looking at the PN incoming value
2368
+ // for the given predecessor.
2313
2369
if (PHINode *PN = NumberingPhi[Num]) {
2314
- for ( unsigned I = 0 ; I != PN->getNumIncomingValues (); ++I) {
2315
- if (PN-> getParent () == PhiBlock && PN->getIncomingBlock (I) == Pred )
2316
- if (uint32_t TransVal = lookup ( PN->getIncomingValue (I), false ) )
2317
- return TransVal;
2318
- }
2370
+ if ( PN->getParent () == PhiBlock)
2371
+ for ( unsigned I = 0 ; I != PN->getNumIncomingValues (); ++I )
2372
+ if (PN->getIncomingBlock (I) == Pred )
2373
+ if ( uint32_t TransVal = lookup (PN-> getIncomingValue (I), false ))
2374
+ return TransVal;
2319
2375
return Num;
2320
2376
}
2321
2377
2378
+ if (BasicBlock *BB = NumberingBB[Num]) {
2379
+ assert (MSSA && " NumberingBB is non-empty only when using MemorySSA" );
2380
+ // Value numbers of basic blocks are used to represent memory state in
2381
+ // load/store instructions and read-only function calls when said state is
2382
+ // set by a MemoryPhi.
2383
+ if (BB != PhiBlock)
2384
+ return Num;
2385
+ MemoryPhi *MPhi = MSSA->getMemoryAccess (BB);
2386
+ for (unsigned i = 0 , N = MPhi->getNumIncomingValues (); i != N; ++i) {
2387
+ if (MPhi->getIncomingBlock (i) != Pred)
2388
+ continue ;
2389
+ MemoryAccess *MA = MPhi->getIncomingValue (i);
2390
+ if (auto *PredPhi = dyn_cast<MemoryPhi>(MA))
2391
+ return lookupOrAdd (PredPhi->getBlock ());
2392
+ if (MSSA->isLiveOnEntryDef (MA))
2393
+ return lookupOrAdd (&BB->getParent ()->getEntryBlock ());
2394
+ return lookupOrAdd (cast<MemoryUseOrDef>(MA)->getMemoryInst ());
2395
+ }
2396
+ llvm_unreachable (
2397
+ " CFG/MemorySSA mismatch: predecessor not found among incoming blocks" );
2398
+ }
2399
+
2322
2400
// If there is any value related with Num is defined in a BB other than
2323
2401
// PhiBlock, it cannot depend on a phi in PhiBlock without going through
2324
2402
// a backedge. We can do an early exit in that case to save compile time.
@@ -2761,6 +2839,7 @@ bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2761
2839
ICF = &ImplicitCFT;
2762
2840
this ->LI = &LI;
2763
2841
VN.setMemDep (MD);
2842
+ VN.setMemorySSA (MSSA);
2764
2843
ORE = RunORE;
2765
2844
InvalidBlockRPONumbers = true ;
2766
2845
MemorySSAUpdater Updater (MSSA);
0 commit comments