Skip to content

Commit

Permalink
[Mem2Reg] Don't use single store optimization for potentially poison …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
nikic authored and kbluck committed Jul 6, 2024
1 parent 6b9afa3 commit d475bab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 8 additions & 2 deletions llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,14 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
SmallSet<DbgAssignIntrinsic *, 8> *DbgAssignsToDelete,
SmallSet<DbgVariableRecord *, 8> *DVRAssignsToDelete) {
StoreInst *OnlyStore = Info.OnlyStore;
bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0));
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);
BasicBlock *StoreBB = OnlyStore->getParent();
int StoreIndex = -1;

Expand Down Expand Up @@ -565,7 +572,6 @@ rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI,
}

// Otherwise, we *can* safely rewrite this load.
Value *ReplVal = OnlyStore->getOperand(0);
// If the replacement value is the load, this must occur in unreachable
// code.
if (ReplVal == LI)
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/Mem2Reg/single-store.ll
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=mem2reg < %s | FileCheck %s

; FIXME: This is a miscompile.
define i8 @single_store_literal_poison(i1 %cond) {
; CHECK-LABEL: define i8 @single_store_literal_poison(
; CHECK-SAME: i1 [[COND:%.*]]) {
; CHECK-NEXT: br i1 [[COND]], label %[[IF:.*]], label %[[EXIT:.*]]
; CHECK: [[IF]]:
; CHECK-NEXT: br label %[[EXIT]]
; CHECK: [[EXIT]]:
; CHECK-NEXT: ret i8 poison
; CHECK-NEXT: ret i8 undef
;
%a = alloca i8, align 1
br i1 %cond, label %if, label %exit
Expand Down

0 comments on commit d475bab

Please sign in to comment.