From daaea128bb84f8ed7b9de36aa3a51f33b775c05a Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 5 Jul 2024 09:13:59 +0200 Subject: [PATCH] [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. --- llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index cd5ab55c2122fb..546a6cd56b2508 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(ReplVal); + // load with it would be incorrect. If the store dominates the load, we know + // it is always initialized. + bool RequireDominatingStore = + isa(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