Skip to content

Commit edd78ee

Browse files
committed
[InstCombine] Fold Xor with or disjoint
Implement a missing optimization fold from ((select C1, C2) | A) ^ C3) to be ((Select C1 ^ C3, C2 ^ C3) | A)
1 parent aa7d82f commit edd78ee

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4056,6 +4056,25 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
40564056
return nullptr;
40574057
}
40584058

4059+
static Instruction *foldXorToOr(BinaryOperator &I,
4060+
InstCombiner::BuilderTy &Builder) {
4061+
assert(I.getOpcode() == Instruction::Xor);
4062+
Value *A, *B, *C, *D, *E;
4063+
4064+
// ((select C, A, B) | E) ^ D) -> (select C, A ^ D, B ^ D) | E)
4065+
if (match(&I,
4066+
m_c_Xor(m_c_DisjointOr(m_Select(m_Value(C), m_Value(A), m_Value(B)),
4067+
m_Value(E)),
4068+
m_Value(D)))) {
4069+
Value *XorAB = Builder.CreateXor(A, D);
4070+
Value *XorBD = Builder.CreateXor(B, D);
4071+
Value *S = Builder.CreateSelect(C, XorAB, XorBD);
4072+
return BinaryOperator::CreateDisjointOr(S, E);
4073+
}
4074+
4075+
return nullptr;
4076+
}
4077+
40594078
/// A ^ B can be specified using other logic ops in a variety of patterns. We
40604079
/// can fold these early and efficiently by morphing an existing instruction.
40614080
static Instruction *foldXorToXor(BinaryOperator &I,
@@ -4658,6 +4677,9 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
46584677
if (Instruction *NewXor = foldXorToXor(I, Builder))
46594678
return NewXor;
46604679

4680+
if (Instruction *NewOr = foldXorToOr(I, Builder))
4681+
return NewOr;
4682+
46614683
// (A&B)^(A&C) -> A&(B^C) etc
46624684
if (Value *V = foldUsingDistributiveLaws(I))
46634685
return replaceInstUsesWith(I, V);

llvm/test/Transforms/InstCombine/disjoint_or.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
define i32 @fold_xor_with_disjoint_or(i32 %a, i1 %c) {
55
; CHECK-LABEL: define i32 @fold_xor_with_disjoint_or(
66
; CHECK-SAME: i32 [[A:%.*]], i1 [[C:%.*]]) {
7-
; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i32 0, i32 4
87
; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[A]], 4
9-
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[S]], [[SHL]]
10-
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[OR]], 4
8+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C]], i32 4, i32 0
9+
; CHECK-NEXT: [[XOR:%.*]] = or disjoint i32 [[TMP1]], [[SHL]]
1110
; CHECK-NEXT: ret i32 [[XOR]]
1211
;
1312
%s = select i1 %c, i32 0, i32 4
@@ -20,10 +19,9 @@ define i32 @fold_xor_with_disjoint_or(i32 %a, i1 %c) {
2019
define <2 x i32> @fold_xor_with_disjoint_or_vec(<2 x i32> %a, i1 %c) {
2120
; CHECK-LABEL: define <2 x i32> @fold_xor_with_disjoint_or_vec(
2221
; CHECK-SAME: <2 x i32> [[A:%.*]], i1 [[C:%.*]]) {
23-
; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], <2 x i32> zeroinitializer, <2 x i32> <i32 4, i32 4>
2422
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> [[A]], <i32 4, i32 4>
25-
; CHECK-NEXT: [[OR:%.*]] = or disjoint <2 x i32> [[S]], [[SHL]]
26-
; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i32> [[OR]], <i32 4, i32 4>
23+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C]], <2 x i32> <i32 4, i32 4>, <2 x i32> zeroinitializer
24+
; CHECK-NEXT: [[XOR:%.*]] = or disjoint <2 x i32> [[TMP1]], [[SHL]]
2725
; CHECK-NEXT: ret <2 x i32> [[XOR]]
2826
;
2927
%s = select i1 %c, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 4, i32 4>

0 commit comments

Comments
 (0)