diff --git a/llvm/include/llvm/Transforms/Utils/Local.h b/llvm/include/llvm/Transforms/Utils/Local.h index b6f9e7f57356..e005b28b453e 100644 --- a/llvm/include/llvm/Transforms/Utils/Local.h +++ b/llvm/include/llvm/Transforms/Utils/Local.h @@ -326,10 +326,12 @@ void combineMetadataForCSE(Instruction *K, const Instruction *J); /// the given edge. Returns the number of replacements made. unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Edge); + /// Replace each use of 'From' with 'To' if that use is dominated by -/// the end of the given BasicBlock. Returns the number of replacements made. +/// the end of 'BB'. Returns the number of replacements made. +/// Replace use of 'From' with 'To' in 'BB' if 'IncludeSelf' is true. unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, - const BasicBlock *BB); + const BasicBlock *BB, bool IncludeSelf); /// Return true if the CallSite CS calls a gc leaf function. diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp index a108ce8d2925..aaec475a6822 100644 --- a/llvm/lib/Transforms/Scalar/GVN.cpp +++ b/llvm/lib/Transforms/Scalar/GVN.cpp @@ -1962,7 +1962,7 @@ bool GVN::propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root, unsigned NumReplacements = DominatesByEdge ? replaceDominatedUsesWith(LHS, RHS, *DT, Root) - : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart()); + : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart(), false); Changed |= NumReplacements > 0; NumGVNEqProp += NumReplacements; @@ -2038,7 +2038,7 @@ bool GVN::propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root, DominatesByEdge ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root) : replaceDominatedUsesWith(NotCmp, NotVal, *DT, - Root.getStart()); + Root.getStart(), false); Changed |= NumReplacements > 0; NumGVNEqProp += NumReplacements; } diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 87524e660f0c..b347bad99580 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1679,7 +1679,8 @@ unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, - const BasicBlock *BB) { + const BasicBlock *BB, + bool IncludeSelf) { assert(From->getType() == To->getType()); unsigned Count = 0; @@ -1687,7 +1688,8 @@ unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, UI != UE;) { Use &U = *UI++; auto *I = cast(U.getUser()); - if (DT.properlyDominates(BB, I->getParent())) { + if ((IncludeSelf && BB == I->getParent()) || + DT.properlyDominates(BB, I->getParent())) { U.set(To); DEBUG(dbgs() << "Replace dominated use of '" << From->getName() << "' as " << *To << " in " << *U << "\n");