@@ -3854,6 +3854,16 @@ Value *NewGVN::findPHIOfOpsLeader(const Expression *E,
38543854 return nullptr ;
38553855}
38563856
3857+ // Return true iff V1 can be replaced with V2.
3858+ static bool canBeReplacedBy (Value *V1, Value *V2) {
3859+ if (auto *CB1 = dyn_cast<CallBase>(V1))
3860+ if (auto *CB2 = dyn_cast<CallBase>(V2))
3861+ return CB1->getAttributes ()
3862+ .intersectWith (CB2->getContext (), CB2->getAttributes ())
3863+ .has_value ();
3864+ return true ;
3865+ }
3866+
38573867bool NewGVN::eliminateInstructions (Function &F) {
38583868 // This is a non-standard eliminator. The normal way to eliminate is
38593869 // to walk the dominator tree in order, keeping track of available
@@ -3963,6 +3973,9 @@ bool NewGVN::eliminateInstructions(Function &F) {
39633973 MembersLeft.insert (Member);
39643974 continue ;
39653975 }
3976+ if (!canBeReplacedBy (Member, Leader))
3977+ continue ;
3978+
39663979 LLVM_DEBUG (dbgs () << " Found replacement " << *(Leader) << " for "
39673980 << *Member << " \n " );
39683981 auto *I = cast<Instruction>(Member);
@@ -4069,8 +4082,11 @@ bool NewGVN::eliminateInstructions(Function &F) {
40694082 if (DominatingLeader != Def) {
40704083 // Even if the instruction is removed, we still need to update
40714084 // flags/metadata due to downstreams users of the leader.
4072- if (!match (DefI, m_Intrinsic<Intrinsic::ssa_copy>()))
4085+ if (!match (DefI, m_Intrinsic<Intrinsic::ssa_copy>())) {
4086+ if (!canBeReplacedBy (DefI, DominatingLeader))
4087+ continue ;
40734088 patchReplacementInstruction (DefI, DominatingLeader);
4089+ }
40744090
40754091 markInstructionForDeletion (DefI);
40764092 }
@@ -4112,17 +4128,21 @@ bool NewGVN::eliminateInstructions(Function &F) {
41124128 // Don't replace our existing users with ourselves.
41134129 if (U->get () == DominatingLeader)
41144130 continue ;
4115- LLVM_DEBUG (dbgs ()
4116- << " Found replacement " << *DominatingLeader << " for "
4117- << *U->get () << " in " << *(U->getUser ()) << " \n " );
41184131
41194132 // If we replaced something in an instruction, handle the patching of
41204133 // metadata. Skip this if we are replacing predicateinfo with its
41214134 // original operand, as we already know we can just drop it.
41224135 auto *ReplacedInst = cast<Instruction>(U->get ());
41234136 auto *PI = PredInfo->getPredicateInfoFor (ReplacedInst);
4124- if (!PI || DominatingLeader != PI->OriginalOp )
4137+ if (!PI || DominatingLeader != PI->OriginalOp ) {
4138+ if (!canBeReplacedBy (ReplacedInst, DominatingLeader))
4139+ continue ;
41254140 patchReplacementInstruction (ReplacedInst, DominatingLeader);
4141+ }
4142+
4143+ LLVM_DEBUG (dbgs ()
4144+ << " Found replacement " << *DominatingLeader << " for "
4145+ << *U->get () << " in " << *(U->getUser ()) << " \n " );
41264146 U->set (DominatingLeader);
41274147 // This is now a use of the dominating leader, which means if the
41284148 // dominating leader was dead, it's now live!
0 commit comments