Skip to content

Commit daaea12

Browse files
committed
[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.
1 parent a48305e commit daaea12

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
@@ -528,11 +528,10 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
528528
Value *ReplVal = OnlyStore->getOperand(0);
529529
// Loads may either load the stored value or uninitialized memory (undef).
530530
// If the stored value may be poison, then replacing an uninitialized memory
531-
// load with it would be incorrect.
532-
if (!isGuaranteedNotToBePoison(ReplVal))
533-
return false;
534-
535-
bool StoringGlobalVal = !isa<Instruction>(ReplVal);
531+
// load with it would be incorrect. If the store dominates the load, we know
532+
// it is always initialized.
533+
bool RequireDominatingStore =
534+
isa<Instruction>(ReplVal) || !isGuaranteedNotToBePoison(ReplVal);
536535
BasicBlock *StoreBB = OnlyStore->getParent();
537536
int StoreIndex = -1;
538537

@@ -549,7 +548,7 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
549548
// only value stored to the alloca. We can do this if the value is
550549
// dominated by the store. If not, we use the rest of the mem2reg machinery
551550
// to insert the phi nodes as needed.
552-
if (!StoringGlobalVal) { // Non-instructions are always dominated.
551+
if (RequireDominatingStore) {
553552
if (LI->getParent() == StoreBB) {
554553
// If we have a use that is in the same block as the store, compare the
555554
// indices of the two instructions to see which one came first. If the

0 commit comments

Comments
 (0)