Skip to content

Commit 2fa2da4

Browse files
committed
[InstCombine] Fix miscompilation in sinkNotIntoLogicalOp
1 parent 388daea commit 2fa2da4

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4513,6 +4513,12 @@ bool InstCombinerImpl::sinkNotIntoLogicalOp(Instruction &I) {
45134513
if (Op0 == Op1)
45144514
return false;
45154515

4516+
// If one of the operands is a user of the other,
4517+
// freelyInvert->freelyInvertAllUsersOf will change the operands of I, which
4518+
// may cause miscompilation.
4519+
if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0))))
4520+
return false;
4521+
45164522
Instruction::BinaryOps NewOpc =
45174523
match(&I, m_LogicalAnd()) ? Instruction::Or : Instruction::And;
45184524
bool IsBinaryOp = isa<BinaryOperator>(I);

llvm/test/Transforms/InstCombine/pr142518.ll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
33

4-
define i8 @pr142518(ptr %p, i8 %x, i1 %c) {
4+
define i8 @pr142518(ptr %p, i8 %x, i1 %c) "instcombine-no-verify-fixpoint" {
55
; CHECK-LABEL: define i8 @pr142518(
6-
; CHECK-SAME: ptr [[P:%.*]], i8 [[X:%.*]], i1 [[C:%.*]]) {
6+
; CHECK-SAME: ptr [[P:%.*]], i8 [[X:%.*]], i1 [[C:%.*]]) #[[ATTR0:[0-9]+]] {
77
; CHECK-NEXT: [[ENTRY:.*:]]
8-
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X]], -2
8+
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[X]], -1
99
; CHECK-NEXT: br label %[[LOOP:.*]]
1010
; CHECK: [[LOOP]]:
11+
; CHECK-NEXT: [[NOT1:%.*]] = xor i1 [[CMP1]], true
12+
; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP1]], [[NOT1]]
13+
; CHECK-NEXT: [[CMP:%.*]] = xor i1 [[OR]], true
1114
; CHECK-NEXT: [[EXT2:%.*]] = zext i1 [[CMP]] to i8
1215
; CHECK-NEXT: store i8 [[EXT2]], ptr [[P]], align 1
1316
; CHECK-NEXT: br i1 false, label %[[LOOP]], label %[[EXIT:.*]]
1417
; CHECK: [[EXIT]]:
15-
; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[CMP]] to i8
18+
; CHECK-NEXT: [[NOT3:%.*]] = xor i1 [[OR]], true
19+
; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[NOT3]] to i8
1620
; CHECK-NEXT: ret i8 [[EXT3]]
1721
;
1822
entry:

0 commit comments

Comments
 (0)