Skip to content

Commit 9c49dcc

Browse files
committed
[ConstantFold] Don't fold and/or i1 poison to poison (NFC)
.. because it causes miscompilation when combined with select i1 -> and/or. It is the select fold which is incorrect; but it is costly to disable the fold, so hack this one. D92270
1 parent c3d4846 commit 9c49dcc

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,14 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
11051105
}
11061106

11071107
// Binary operations propagate poison.
1108-
if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1108+
// FIXME: Currently, or/and i1 poison aren't folded into poison because
1109+
// it causes miscompilation when combined with another optimization in
1110+
// InstCombine (select i1 -> and/or). The select fold is wrong, but
1111+
// fixing it requires an effort, so temporarily disable this until it is
1112+
// fixed.
1113+
bool PoisonFold = !C1->getType()->isIntegerTy(1) ||
1114+
(Opcode != Instruction::Or && Opcode != Instruction::And);
1115+
if (PoisonFold && (isa<PoisonValue>(C1) || isa<PoisonValue>(C2)))
11091116
return PoisonValue::get(C1->getType());
11101117

11111118
// Handle scalar UndefValue and scalable vector UndefValue. Fixed-length

llvm/test/Transforms/InstSimplify/ConstProp/poison.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,15 @@ define void @other_ops(i8 %x) {
114114
call void (...) @use(i1 %i1, i1 %i2, i8 %i3, i8 %i4, i8* getelementptr (i8, i8* poison, i64 1), i8* getelementptr inbounds (i8, i8* undef, i64 1))
115115
ret void
116116
}
117+
118+
; TODO: these must be folded into poison; D92270
119+
define void @logicalops_i1(i1 %x) {
120+
; CHECK-LABEL: @logicalops_i1(
121+
; CHECK-NEXT: call void (...) @use(i1 true, i1 false)
122+
; CHECK-NEXT: ret void
123+
;
124+
%i1 = or i1 %x, poison
125+
%i2 = and i1 %x, poison
126+
call void (...) @use(i1 %i1, i1 %i2)
127+
ret void
128+
}

0 commit comments

Comments
 (0)