[llvm] daaea12 - [Mem2Reg] Always allow single-store optimization for dominating stores

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 5 00:59:18 PDT 2024


Author: Nikita Popov
Date: 2024-07-05T09:55:19+02:00
New Revision: daaea128bb84f8ed7b9de36aa3a51f33b775c05a

URL: https://github.com/llvm/llvm-project/commit/daaea128bb84f8ed7b9de36aa3a51f33b775c05a
DIFF: https://github.com/llvm/llvm-project/commit/daaea128bb84f8ed7b9de36aa3a51f33b775c05a.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index cd5ab55c2122f..546a6cd56b250 100644
--- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -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;
 
@@ -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


        


More information about the llvm-commits mailing list