Skip to content

Commit 48de8d7

Browse files
dtcxzywllvmbot
authored andcommitted
[InstSimplify] Make sure the simplified value doesn't generate poison in threadBinOpOverSelect (#87075)
Alive2: https://alive2.llvm.org/ce/z/y_Jmdn Fix #87042. (cherry picked from commit 3197f9d)
1 parent b6ebea7 commit 48de8d7

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ static Value *threadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS,
439439
// Check that the simplified value has the form "X op Y" where "op" is the
440440
// same as the original operation.
441441
Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
442-
if (Simplified && Simplified->getOpcode() == unsigned(Opcode)) {
442+
if (Simplified && Simplified->getOpcode() == unsigned(Opcode) &&
443+
!Simplified->hasPoisonGeneratingFlags()) {
443444
// The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
444445
// We already know that "op" is the same as for the simplified value. See
445446
// if the operands match too. If so, return the simplified value.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3+
4+
; %or2 cannot be folded into %or1 because %or1 has disjoint.
5+
; TODO: Can we move the logic into InstCombine and drop the disjoint flag?
6+
define i64 @test(i1 %cond, i64 %x) {
7+
; CHECK-LABEL: define i64 @test(
8+
; CHECK-SAME: i1 [[COND:%.*]], i64 [[X:%.*]]) {
9+
; CHECK-NEXT: [[OR1:%.*]] = or disjoint i64 [[X]], 7
10+
; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[COND]], i64 [[OR1]], i64 [[X]]
11+
; CHECK-NEXT: [[OR2:%.*]] = or i64 [[SEL1]], 7
12+
; CHECK-NEXT: ret i64 [[OR2]]
13+
;
14+
%or1 = or disjoint i64 %x, 7
15+
%sel1 = select i1 %cond, i64 %or1, i64 %x
16+
%or2 = or i64 %sel1, 7
17+
ret i64 %or2
18+
}
19+
20+
define i64 @pr87042(i64 %x) {
21+
; CHECK-LABEL: define i64 @pr87042(
22+
; CHECK-SAME: i64 [[X:%.*]]) {
23+
; CHECK-NEXT: [[AND1:%.*]] = and i64 [[X]], 65535
24+
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i64 [[AND1]], 0
25+
; CHECK-NEXT: [[OR1:%.*]] = or disjoint i64 [[X]], 7
26+
; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i64 [[OR1]], i64 [[X]]
27+
; CHECK-NEXT: [[AND2:%.*]] = and i64 [[SEL1]], 16776960
28+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i64 [[AND2]], 0
29+
; CHECK-NEXT: [[OR2:%.*]] = or i64 [[SEL1]], 7
30+
; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[CMP2]], i64 [[OR2]], i64 [[SEL1]]
31+
; CHECK-NEXT: ret i64 [[SEL2]]
32+
;
33+
%and1 = and i64 %x, 65535
34+
%cmp1 = icmp eq i64 %and1, 0
35+
%or1 = or disjoint i64 %x, 7
36+
%sel1 = select i1 %cmp1, i64 %or1, i64 %x
37+
%and2 = and i64 %sel1, 16776960
38+
%cmp2 = icmp eq i64 %and2, 0
39+
%or2 = or i64 %sel1, 7
40+
%sel2 = select i1 %cmp2, i64 %or2, i64 %sel1
41+
ret i64 %sel2
42+
}

0 commit comments

Comments
 (0)