@@ -474,6 +474,19 @@ void GVNPass::ValueTable::add(Value *V, uint32_t Num) {
474474 NumberingPhi[Num] = PN;
475475}
476476
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+
477490uint32_t GVNPass::ValueTable::lookupOrAddCall (CallInst *C) {
478491 // FIXME: Currently the calls which may access the thread id may
479492 // be considered as not accessing the memory. But this is
@@ -594,15 +607,48 @@ uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
594607 return V;
595608 }
596609
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+
597618 ValueNumbering[C] = NextValueNumber;
598619 return NextValueNumber++;
599620}
600621
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+
601641// / Returns true if a value number exists for the specified value.
602642bool GVNPass::ValueTable::exists (Value *V) const {
603643 return ValueNumbering.contains (V);
604644}
605645
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+
606652// / lookupOrAdd - Returns the value number for the specified value, assigning
607653// / it a new number if it did not have one before.
608654uint32_t GVNPass::ValueTable::lookupOrAdd (Value *V) {
@@ -613,6 +659,8 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
613659 auto *I = dyn_cast<Instruction>(V);
614660 if (!I) {
615661 ValueNumbering[V] = NextValueNumber;
662+ if (isa<BasicBlock>(V))
663+ NumberingBB[NextValueNumber] = cast<BasicBlock>(V);
616664 return NextValueNumber++;
617665 }
618666
@@ -672,6 +720,9 @@ uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
672720 ValueNumbering[V] = NextValueNumber;
673721 NumberingPhi[NextValueNumber] = cast<PHINode>(V);
674722 return NextValueNumber++;
723+ case Instruction::Load:
724+ case Instruction::Store:
725+ return computeLoadStoreVN (I);
675726 default :
676727 ValueNumbering[V] = NextValueNumber;
677728 return NextValueNumber++;
@@ -709,6 +760,7 @@ void GVNPass::ValueTable::clear() {
709760 ValueNumbering.clear ();
710761 ExpressionNumbering.clear ();
711762 NumberingPhi.clear ();
763+ NumberingBB.clear ();
712764 PhiTranslateTable.clear ();
713765 NextValueNumber = 1 ;
714766 Expressions.clear ();
@@ -723,6 +775,8 @@ void GVNPass::ValueTable::erase(Value *V) {
723775 // If V is PHINode, V <--> value number is an one-to-one mapping.
724776 if (isa<PHINode>(V))
725777 NumberingPhi.erase (Num);
778+ else if (isa<BasicBlock>(V))
779+ NumberingBB.erase (Num);
726780}
727781
728782// / 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,
23102364uint32_t GVNPass::ValueTable::phiTranslateImpl (const BasicBlock *Pred,
23112365 const BasicBlock *PhiBlock,
23122366 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.
23132369 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;
23192375 return Num;
23202376 }
23212377
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+
23222400 // If there is any value related with Num is defined in a BB other than
23232401 // PhiBlock, it cannot depend on a phi in PhiBlock without going through
23242402 // 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,
27612839 ICF = &ImplicitCFT;
27622840 this ->LI = &LI;
27632841 VN.setMemDep (MD);
2842+ VN.setMemorySSA (MSSA);
27642843 ORE = RunORE;
27652844 InvalidBlockRPONumbers = true ;
27662845 MemorySSAUpdater Updater (MSSA);
0 commit comments