Skip to content

Commit f58930f

Browse files
authored
[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.
1 parent 6222c8f commit f58930f

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
@@ -525,7 +525,14 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
525525
SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
526526
SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
527527
StoreInst *OnlyStore = Info.OnlyStore;
528-
bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0));
528+
Value *ReplVal = OnlyStore->getOperand(0);
529+
// Loads may either load the stored value or uninitialized memory (undef).
530+
// If the stored value may be poison, then replacing an uninitialized memory
531+
// load with it would be incorrect.
532+
if (!isGuaranteedNotToBePoison(ReplVal))
533+
return false;
534+
535+
bool StoringGlobalVal = !isa<Instruction>(ReplVal);
529536
BasicBlock *StoreBB = OnlyStore->getParent();
530537
int StoreIndex = -1;
531538

@@ -565,7 +572,6 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
565572
}
566573

567574
// Otherwise, we *can* safely rewrite this load.
568-
Value *ReplVal = OnlyStore->getOperand(0);
569575
// If the replacement value is the load, this must occur in unreachable
570576
// code.
571577
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)