@@ -476,6 +476,27 @@ void GVNPass::ValueTable::add(Value *V, uint32_t num) {
476
476
NumberingPhi[num] = PN;
477
477
}
478
478
479
+ // Include the incoming memory state into the hash of the expression for the
480
+ // given instruction. If the incoming memory state is:
481
+ // * LiveOnEntry, add the value number of the entry block,
482
+ // * a MemoryPhi, add the value number of the basic block corresponding to that
483
+ // MemoryPhi,
484
+ // * a MemoryDef, add the value number of the memory setting instruction.
485
+ void GVNPass::ValueTable::addMemoryStateToExp (Instruction *I, Expression &E) {
486
+ assert (MSSA && " addMemoryStateToExp should not be called without MemorySSA" );
487
+ assert (MSSA->getMemoryAccess (I) && " Instruction does not access memory" );
488
+ MemoryAccess *MA = MSSA->getSkipSelfWalker ()->getClobberingMemoryAccess (I);
489
+
490
+ uint32_t N = 0 ;
491
+ if (isa<MemoryPhi>(MA))
492
+ N = lookupOrAdd (MA->getBlock ());
493
+ else if (MSSA->isLiveOnEntryDef (MA))
494
+ N = lookupOrAdd (&I->getFunction ()->getEntryBlock ());
495
+ else
496
+ N = lookupOrAdd (cast<MemoryDef>(MA)->getMemoryInst ());
497
+ E.varargs .push_back (N);
498
+ }
499
+
479
500
uint32_t GVNPass::ValueTable::lookupOrAddCall (CallInst *C) {
480
501
// FIXME: Currently the calls which may access the thread id may
481
502
// be considered as not accessing the memory. But this is
@@ -596,10 +617,37 @@ uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
596
617
return v;
597
618
}
598
619
620
+ if (MSSA && AA->onlyReadsMemory (C)) {
621
+ Expression exp = createExpr (C);
622
+ addMemoryStateToExp (C, exp);
623
+ uint32_t e = assignExpNewValueNum (exp).first ;
624
+ valueNumbering[C] = e;
625
+ return e;
626
+ }
627
+
599
628
valueNumbering[C] = nextValueNumber;
600
629
return nextValueNumber++;
601
630
}
602
631
632
+ // / Returns the value number for the specified load or store instruction.
633
+ uint32_t GVNPass::ValueTable::lookupOrAddLoadStore (Instruction *I) {
634
+ if (!MSSA) {
635
+ valueNumbering[I] = nextValueNumber;
636
+ return nextValueNumber++;
637
+ }
638
+
639
+ Expression E;
640
+ E.type = I->getType ();
641
+ E.opcode = I->getOpcode ();
642
+ for (Use &Op : I->operands ())
643
+ E.varargs .push_back (lookupOrAdd (Op));
644
+ addMemoryStateToExp (I, E);
645
+
646
+ uint32_t N = assignExpNewValueNum (E).first ;
647
+ valueNumbering[I] = N;
648
+ return N;
649
+ }
650
+
603
651
// / Returns true if a value number exists for the specified value.
604
652
bool GVNPass::ValueTable::exists (Value *V) const {
605
653
return valueNumbering.contains (V);
@@ -615,6 +663,8 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
615
663
auto *I = dyn_cast<Instruction>(V);
616
664
if (!I) {
617
665
valueNumbering[V] = nextValueNumber;
666
+ if (MSSA && isa<BasicBlock>(V))
667
+ NumberingBB[nextValueNumber] = cast<BasicBlock>(V);
618
668
return nextValueNumber++;
619
669
}
620
670
@@ -674,6 +724,9 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
674
724
valueNumbering[V] = nextValueNumber;
675
725
NumberingPhi[nextValueNumber] = cast<PHINode>(V);
676
726
return nextValueNumber++;
727
+ case Instruction::Load:
728
+ case Instruction::Store:
729
+ return lookupOrAddLoadStore (I);
677
730
default :
678
731
valueNumbering[V] = nextValueNumber;
679
732
return nextValueNumber++;
@@ -711,6 +764,7 @@ void GVNPass::ValueTable::clear() {
711
764
valueNumbering.clear ();
712
765
expressionNumbering.clear ();
713
766
NumberingPhi.clear ();
767
+ NumberingBB.clear ();
714
768
PhiTranslateTable.clear ();
715
769
nextValueNumber = 1 ;
716
770
Expressions.clear ();
@@ -725,6 +779,8 @@ void GVNPass::ValueTable::erase(Value *V) {
725
779
// If V is PHINode, V <--> value number is an one-to-one mapping.
726
780
if (isa<PHINode>(V))
727
781
NumberingPhi.erase (Num);
782
+ else if (isa<BasicBlock>(V))
783
+ NumberingBB.erase (Num);
728
784
}
729
785
730
786
// / verifyRemoved - Verify that the value is removed from all internal data
@@ -2294,15 +2350,39 @@ bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
2294
2350
uint32_t GVNPass::ValueTable::phiTranslateImpl (const BasicBlock *Pred,
2295
2351
const BasicBlock *PhiBlock,
2296
2352
uint32_t Num, GVNPass &Gvn) {
2353
+ // See if we can refine the value number by looking at the PN incoming value
2354
+ // for the given predecessor.
2297
2355
if (PHINode *PN = NumberingPhi[Num]) {
2298
- for ( unsigned i = 0 ; i != PN->getNumIncomingValues (); ++i) {
2299
- if (PN-> getParent () == PhiBlock && PN->getIncomingBlock (i) == Pred )
2300
- if (uint32_t TransVal = lookup ( PN->getIncomingValue (i), false ) )
2301
- return TransVal;
2302
- }
2356
+ if ( PN->getParent () == PhiBlock)
2357
+ for ( unsigned i = 0 ; i != PN->getNumIncomingValues (); ++i )
2358
+ if (PN->getIncomingBlock (i) == Pred )
2359
+ if ( uint32_t TransVal = lookup (PN-> getIncomingValue (i), false ))
2360
+ return TransVal;
2303
2361
return Num;
2304
2362
}
2305
2363
2364
+ if (BasicBlock *BB = NumberingBB[Num]) {
2365
+ assert (MSSA && " NumberingBB is non-empty only when using MemorySSA" );
2366
+ // Value numbers of basic blocks are used to represent memory state in
2367
+ // load/store instructions and read-only function calls when said state is
2368
+ // set by a MemoryPhi.
2369
+ if (BB != PhiBlock)
2370
+ return Num;
2371
+ MemoryPhi *MPhi = MSSA->getMemoryAccess (BB);
2372
+ for (unsigned i = 0 , N = MPhi->getNumIncomingValues (); i != N; ++i) {
2373
+ if (MPhi->getIncomingBlock (i) != Pred)
2374
+ continue ;
2375
+ MemoryAccess *MA = MPhi->getIncomingValue (i);
2376
+ if (auto *PredPhi = dyn_cast<MemoryPhi>(MA))
2377
+ return lookupOrAdd (PredPhi->getBlock ());
2378
+ if (MSSA->isLiveOnEntryDef (MA))
2379
+ return lookupOrAdd (&BB->getParent ()->getEntryBlock ());
2380
+ return lookupOrAdd (cast<MemoryUseOrDef>(MA)->getMemoryInst ());
2381
+ }
2382
+ llvm_unreachable (
2383
+ " CFG/MemorySSA mismatch: predecessor not found among incoming blocks" );
2384
+ }
2385
+
2306
2386
// If there is any value related with Num is defined in a BB other than
2307
2387
// PhiBlock, it cannot depend on a phi in PhiBlock without going through
2308
2388
// a backedge. We can do an early exit in that case to save compile time.
@@ -2337,7 +2417,7 @@ uint32_t GVNPass::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
2337
2417
}
2338
2418
2339
2419
if (uint32_t NewNum = expressionNumbering[Exp]) {
2340
- if (Exp.opcode == Instruction::Call && NewNum != Num)
2420
+ if (!MSSA && Exp.opcode == Instruction::Call && NewNum != Num)
2341
2421
return areCallValsEqual (Num, NewNum, Pred, PhiBlock, Gvn) ? NewNum : Num;
2342
2422
return NewNum;
2343
2423
}
@@ -2738,6 +2818,7 @@ bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
2738
2818
ICF = &ImplicitCFT;
2739
2819
this ->LI = &LI;
2740
2820
VN.setMemDep (MD);
2821
+ VN.setMemorySSA (MSSA);
2741
2822
ORE = RunORE;
2742
2823
InvalidBlockRPONumbers = true ;
2743
2824
MemorySSAUpdater Updater (MSSA);
0 commit comments