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.

(cherry picked from commit daaea12)
  • Loading branch information
nikic committed Jul 5, 2024
1 parent 75ec529 commit c54cff0
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 @@ -514,11 +514,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 @@ -535,7 +534,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 c54cff0

Please sign in to comment.