[Mem2Reg] Always allow single-store optimization for dominating stores
In #97711 the single-store optimization was disabled for the case
where the value is potentially poison, as this may produce incorrect
results for loads of uninitialized memory.
However, this resulted in compile-time regressions. Address these
by still allowing the single-store optimization to occur in cases
where the store dominates the load, as we know that such a load
will always read initialized memory.
(cherry picked from commit daaea128bb84f8ed7b9de36aa3a51f33b775c05a)
diff --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index d37342f..bc0d12f 100644
--- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -514,11 +514,10 @@
Value *ReplVal = OnlyStore->getOperand(0);
// Loads may either load the stored value or uninitialized memory (undef).
// If the stored value may be poison, then replacing an uninitialized memory
- // load with it would be incorrect.
- if (!isGuaranteedNotToBePoison(ReplVal))
- return false;
-
- bool StoringGlobalVal = !isa<Instruction>(ReplVal);
+ // load with it would be incorrect. If the store dominates the load, we know
+ // it is always initialized.
+ bool RequireDominatingStore =
+ isa<Instruction>(ReplVal) || !isGuaranteedNotToBePoison(ReplVal);
BasicBlock *StoreBB = OnlyStore->getParent();
int StoreIndex = -1;
@@ -535,7 +534,7 @@
// only value stored to the alloca. We can do this if the value is
// dominated by the store. If not, we use the rest of the mem2reg machinery
// to insert the phi nodes as needed.
- if (!StoringGlobalVal) { // Non-instructions are always dominated.
+ if (RequireDominatingStore) {
if (LI->getParent() == StoreBB) {
// If we have a use that is in the same block as the store, compare the
// indices of the two instructions to see which one came first. If the