Skip to content

Commit c54cff0

Browse files
committed
[Mem2Reg] Always allow single-store optimization for dominating stores
In llvm#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 daaea12)
1 parent 75ec529 commit c54cff0

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,10 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
514514
Value *ReplVal = OnlyStore->getOperand(0);
515515
// Loads may either load the stored value or uninitialized memory (undef).
516516
// If the stored value may be poison, then replacing an uninitialized memory
517-
// load with it would be incorrect.
518-
if (!isGuaranteedNotToBePoison(ReplVal))
519-
return false;
520-
521-
bool StoringGlobalVal = !isa<Instruction>(ReplVal);
517+
// load with it would be incorrect. If the store dominates the load, we know
518+
// it is always initialized.
519+
bool RequireDominatingStore =
520+
isa<Instruction>(ReplVal) || !isGuaranteedNotToBePoison(ReplVal);
522521
BasicBlock *StoreBB = OnlyStore->getParent();
523522
int StoreIndex = -1;
524523

@@ -535,7 +534,7 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
535534
// only value stored to the alloca. We can do this if the value is
536535
// dominated by the store. If not, we use the rest of the mem2reg machinery
537536
// to insert the phi nodes as needed.
538-
if (!StoringGlobalVal) { // Non-instructions are always dominated.
537+
if (RequireDominatingStore) {
539538
if (LI->getParent() == StoreBB) {
540539
// If we have a use that is in the same block as the store, compare the
541540
// indices of the two instructions to see which one came first. If the

0 commit comments

Comments
 (0)