Skip to content

Commit 75ec529

Browse files
committed
[Mem2Reg] Don't use single store optimization for potentially poison value (llvm#97711)
If there is a single store, then loads must 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. Fall back to the generic code in that case. This PR only fixes the case where there is a literal poison store -- the case where the value is non-trivially poison will still get miscompiled by phi simplification later, see llvm#96631. Fixes llvm#97702. (cherry picked from commit f58930f)
1 parent 855a732 commit 75ec529

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,14 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
511511
SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
512512
SmallSet<DPValue *, 8> *DPVAssignsToDelete) {
513513
StoreInst *OnlyStore = Info.OnlyStore;
514-
bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0));
514+
Value *ReplVal = OnlyStore->getOperand(0);
515+
// Loads may either load the stored value or uninitialized memory (undef).
516+
// If the stored value may be poison, then replacing an uninitialized memory
517+
// load with it would be incorrect.
518+
if (!isGuaranteedNotToBePoison(ReplVal))
519+
return false;
520+
521+
bool StoringGlobalVal = !isa<Instruction>(ReplVal);
515522
BasicBlock *StoreBB = OnlyStore->getParent();
516523
int StoreIndex = -1;
517524

@@ -551,7 +558,6 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
551558
}
552559

553560
// Otherwise, we *can* safely rewrite this load.
554-
Value *ReplVal = OnlyStore->getOperand(0);
555561
// If the replacement value is the load, this must occur in unreachable
556562
// code.
557563
if (ReplVal == LI)

llvm/test/Transforms/Mem2Reg/single-store.ll

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -S -passes=mem2reg < %s | FileCheck %s
33

4-
; FIXME: This is a miscompile.
54
define i8 @single_store_literal_poison(i1 %cond) {
65
; CHECK-LABEL: define i8 @single_store_literal_poison(
76
; CHECK-SAME: i1 [[COND:%.*]]) {
87
; CHECK-NEXT: br i1 [[COND]], label %[[IF:.*]], label %[[EXIT:.*]]
98
; CHECK: [[IF]]:
109
; CHECK-NEXT: br label %[[EXIT]]
1110
; CHECK: [[EXIT]]:
12-
; CHECK-NEXT: ret i8 poison
11+
; CHECK-NEXT: ret i8 undef
1312
;
1413
%a = alloca i8, align 1
1514
br i1 %cond, label %if, label %exit

0 commit comments

Comments
 (0)