Skip to content

Commit 9742ec2

Browse files
committed
[InstCombine] Fold Xor with or disjoint
Implement a missing optimization to fold (A | B) ^ C to (A ^ C) ^ B
1 parent 25dfbc6 commit 9742ec2

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4706,6 +4706,13 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
47064706
return Xor;
47074707

47084708
Value *X, *Y;
4709+
4710+
// (A | B) ^ C -> (A ^ C) ^ B
4711+
if (match(Op0, m_OneUse(m_c_DisjointOr(m_Value(X), m_Value(Y))))) {
4712+
Value *XorAC = Builder.CreateXor(X, Op1);
4713+
return BinaryOperator::CreateXor(XorAC, Y);
4714+
}
4715+
47094716
Constant *C1;
47104717
if (match(Op1, m_Constant(C1))) {
47114718
Constant *C2;
@@ -4978,4 +4985,4 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
49784985
return Res;
49794986

49804987
return nullptr;
4981-
}
4988+
}

llvm/test/Transforms/InstCombine/xor.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,8 +1489,8 @@ define i4 @PR96857_xor_without_noundef(i4 %val0, i4 %val1, i4 %val2) {
14891489
define i32 @or_disjoint_with_xor(i32 %a, i32 %b, i32 %c) {
14901490
; CHECK-LABEL: @or_disjoint_with_xor(
14911491
; CHECK-NEXT: entry:
1492-
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[A:%.*]], [[B:%.*]]
1493-
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[OR]], [[C:%.*]]
1492+
; CHECK-NEXT: [[TMP0:%.*]] = xor i32 [[A:%.*]], [[C:%.*]]
1493+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[TMP0]], [[B:%.*]]
14941494
; CHECK-NEXT: ret i32 [[XOR]]
14951495
;
14961496
entry:
@@ -1502,8 +1502,8 @@ entry:
15021502
define <2 x i32> @or_disjoint_with_xor_vec(<2 x i32> %a, < 2 x i32> %b, <2 x i32> %c) {
15031503
; CHECK-LABEL: @or_disjoint_with_xor_vec(
15041504
; CHECK-NEXT: entry:
1505-
; CHECK-NEXT: [[OR:%.*]] = or disjoint <2 x i32> [[A:%.*]], [[B:%.*]]
1506-
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i32> [[OR]], [[C:%.*]]
1505+
; CHECK-NEXT: [[TMP0:%.*]] = xor <2 x i32> [[A:%.*]], [[C:%.*]]
1506+
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i32> [[TMP0]], [[B:%.*]]
15071507
; CHECK-NEXT: ret <2 x i32> [[XOR]]
15081508
;
15091509
entry:

0 commit comments

Comments
 (0)