@@ -475,6 +475,19 @@ void GVNPass::ValueTable::add(Value *V, uint32_t Num) {
475
475
NumberingPhi[Num] = PN;
476
476
}
477
477
478
+ // Include the incoming memory state into the hash of the expression for the
479
+ // given instruction. If the incoming memory state is:
480
+ // * LiveOnEntry, add the value number of the entry block,
481
+ // * a MemoryPhi, add the value number of the basic block corresponding to that
482
+ // MemoryPhi,
483
+ // * a MemoryDef, add the value number of the memory setting instruction.
484
+ void GVNPass::ValueTable::addMemoryStateToExp (Instruction *I, Expression &Exp) {
485
+ assert (MSSA && " addMemoryStateToExp should not be called without MemorySSA" );
486
+ assert (MSSA->getMemoryAccess (I) && " Instruction does not access memory" );
487
+ MemoryAccess *MA = MSSA->getSkipSelfWalker ()->getClobberingMemoryAccess (I);
488
+ Exp.VarArgs .push_back (lookupOrAdd (MA));
489
+ }
490
+
478
491
uint32_t GVNPass::ValueTable::lookupOrAddCall (CallInst *C) {
479
492
// FIXME: Currently the calls which may access the thread id may
480
493
// be considered as not accessing the memory. But this is
@@ -595,15 +608,48 @@ uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
595
608
return V;
596
609
}
597
610
611
+ if (MSSA && AA->onlyReadsMemory (C)) {
612
+ Expression Exp = createExpr (C);
613
+ addMemoryStateToExp (C, Exp);
614
+ auto [V, _] = assignExpNewValueNum (Exp);
615
+ ValueNumbering[C] = V;
616
+ return V;
617
+ }
618
+
598
619
ValueNumbering[C] = NextValueNumber;
599
620
return NextValueNumber++;
600
621
}
601
622
623
+ // / Returns the value number for the specified load or store instruction.
624
+ uint32_t GVNPass::ValueTable::lookupOrAddLoadStore (Instruction *I) {
625
+ if (!MSSA) {
626
+ ValueNumbering[I] = NextValueNumber;
627
+ return NextValueNumber++;
628
+ }
629
+
630
+ Expression Exp;
631
+ Exp.Ty = I->getType ();
632
+ Exp.Opcode = I->getOpcode ();
633
+ for (Use &Op : I->operands ())
634
+ Exp.VarArgs .push_back (lookupOrAdd (Op));
635
+ addMemoryStateToExp (I, Exp);
636
+
637
+ auto [V, _] = assignExpNewValueNum (Exp);
638
+ ValueNumbering[I] = V;
639
+ return V;
640
+ }
641
+
602
642
// / Returns true if a value number exists for the specified value.
603
643
bool GVNPass::ValueTable::exists (Value *V) const {
604
644
return ValueNumbering.contains (V);
605
645
}
606
646
647
+ uint32_t GVNPass::ValueTable::lookupOrAdd (MemoryAccess *MA) {
648
+ return MSSA->isLiveOnEntryDef (MA) || isa<MemoryPhi>(MA)
649
+ ? lookupOrAdd (MA->getBlock ())
650
+ : lookupOrAdd (cast<MemoryUseOrDef>(MA)->getMemoryInst ());
651
+ }
652
+
607
653
// / lookupOrAdd - Returns the value number for the specified value, assigning
608
654
// / it a new number if it did not have one before.
609
655
uint32_t GVNPass::ValueTable::lookupOrAdd (Value *V) {
@@ -614,6 +660,8 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
614
660
auto *I = dyn_cast<Instruction>(V);
615
661
if (!I) {
616
662
ValueNumbering[V] = NextValueNumber;
663
+ if (isa<BasicBlock>(V))
664
+ NumberingBB[NextValueNumber] = cast<BasicBlock>(V);
617
665
return NextValueNumber++;
618
666
}
619
667
@@ -673,6 +721,9 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
673
721
ValueNumbering[V] = NextValueNumber;
674
722
NumberingPhi[NextValueNumber] = cast<PHINode>(V);
675
723
return NextValueNumber++;
724
+ case Instruction::Load:
725
+ case Instruction::Store:
726
+ return lookupOrAddLoadStore (I);
676
727
default :
677
728
ValueNumbering[V] = NextValueNumber;
678
729
return NextValueNumber++;
@@ -710,6 +761,7 @@ void GVNPass::ValueTable::clear() {
710
761
ValueNumbering.clear ();
711
762
ExpressionNumbering.clear ();
712
763
NumberingPhi.clear ();
764
+ NumberingBB.clear ();
713
765
PhiTranslateTable.clear ();
714
766
NextValueNumber = 1 ;
715
767
Expressions.clear ();
@@ -724,6 +776,8 @@ void GVNPass::ValueTable::erase(Value *V) {
724
776
// If V is PHINode, V <--> value number is an one-to-one mapping.
725
777
if (isa<PHINode>(V))
726
778
NumberingPhi.erase (Num);
779
+ else if (isa<BasicBlock>(V))
780
+ NumberingBB.erase (Num);
727
781
}
728
782
729
783
// / verifyRemoved - Verify that the value is removed from all internal data
@@ -2295,15 +2349,39 @@ bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2295
2349
uint32_t GVNPass::ValueTable::phiTranslateImpl (const BasicBlock *Pred,
2296
2350
const BasicBlock *PhiBlock,
2297
2351
uint32_t Num, GVNPass &GVN) {
2352
+ // See if we can refine the value number by looking at the PN incoming value
2353
+ // for the given predecessor.
2298
2354
if (PHINode *PN = NumberingPhi[Num]) {
2299
- for ( unsigned I = 0 ; I != PN->getNumIncomingValues (); ++I) {
2300
- if (PN-> getParent () == PhiBlock && PN->getIncomingBlock (I) == Pred )
2301
- if (uint32_t TransVal = lookup ( PN->getIncomingValue (I), false ) )
2302
- return TransVal;
2303
- }
2355
+ if ( PN->getParent () == PhiBlock)
2356
+ for ( unsigned I = 0 ; I != PN->getNumIncomingValues (); ++I )
2357
+ if (PN->getIncomingBlock (I) == Pred )
2358
+ if ( uint32_t TransVal = lookup (PN-> getIncomingValue (I), false ))
2359
+ return TransVal;
2304
2360
return Num;
2305
2361
}
2306
2362
2363
+ if (BasicBlock *BB = NumberingBB[Num]) {
2364
+ assert (MSSA && " NumberingBB is non-empty only when using MemorySSA" );
2365
+ // Value numbers of basic blocks are used to represent memory state in
2366
+ // load/store instructions and read-only function calls when said state is
2367
+ // set by a MemoryPhi.
2368
+ if (BB != PhiBlock)
2369
+ return Num;
2370
+ MemoryPhi *MPhi = MSSA->getMemoryAccess (BB);
2371
+ for (unsigned i = 0 , N = MPhi->getNumIncomingValues (); i != N; ++i) {
2372
+ if (MPhi->getIncomingBlock (i) != Pred)
2373
+ continue ;
2374
+ MemoryAccess *MA = MPhi->getIncomingValue (i);
2375
+ if (auto *PredPhi = dyn_cast<MemoryPhi>(MA))
2376
+ return lookupOrAdd (PredPhi->getBlock ());
2377
+ if (MSSA->isLiveOnEntryDef (MA))
2378
+ return lookupOrAdd (&BB->getParent ()->getEntryBlock ());
2379
+ return lookupOrAdd (cast<MemoryUseOrDef>(MA)->getMemoryInst ());
2380
+ }
2381
+ llvm_unreachable (
2382
+ " CFG/MemorySSA mismatch: predecessor not found among incoming blocks" );
2383
+ }
2384
+
2307
2385
// If there is any value related with Num is defined in a BB other than
2308
2386
// PhiBlock, it cannot depend on a phi in PhiBlock without going through
2309
2387
// a backedge. We can do an early exit in that case to save compile time.
@@ -2738,6 +2816,7 @@ bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2738
2816
ICF = &ImplicitCFT;
2739
2817
this ->LI = &LI;
2740
2818
VN.setMemDep (MD);
2819
+ VN.setMemorySSA (MSSA);
2741
2820
ORE = RunORE;
2742
2821
InvalidBlockRPONumbers = true ;
2743
2822
MemorySSAUpdater Updater (MSSA);
0 commit comments