Skip to content

Commit

Permalink
[Mem2Reg] Always allow single-store optimization for dominating stores
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Jul 5, 2024
1 parent a48305e commit daaea12
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,10 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
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;

Expand All @@ -549,7 +548,7 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
// 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
Expand Down

0 comments on commit daaea12

Please sign in to comment.