[llvm] [InstCombine] Fold Xor with or disjoint (PR #105992)

Amr Hesham via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 25 08:58:43 PDT 2024


https://github.com/AmrDeveloper created https://github.com/llvm/llvm-project/pull/105992

Implement a missing optimization fold from ((select C1, C2) | A) ^ C3) to be ((Select C1 ^ C3, C2 ^ C3) | A)

Fixes: #79266

>From b8a3d12d87f67960edab043a0394b63b5eb4b312 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sun, 25 Aug 2024 16:56:47 +0200
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests

---
 .../Transforms/InstCombine/disjoint_or.ll     | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/disjoint_or.ll

diff --git a/llvm/test/Transforms/InstCombine/disjoint_or.ll b/llvm/test/Transforms/InstCombine/disjoint_or.ll
new file mode 100644
index 00000000000000..b873cc575eef62
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/disjoint_or.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i32 @fold_xor_with_disjoint_or(i32 %a, i1 %c) {
+; CHECK-LABEL: define i32 @fold_xor_with_disjoint_or(
+; CHECK-SAME: i32 [[A:%.*]], i1 [[C:%.*]]) {
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i32 0, i32 4
+; CHECK-NEXT:    [[SHL:%.*]] = shl i32 [[A]], 4
+; CHECK-NEXT:    [[OR:%.*]] = or disjoint i32 [[S]], [[SHL]]
+; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[OR]], 4
+; CHECK-NEXT:    ret i32 [[XOR]]
+;
+  %s = select i1 %c, i32 0, i32 4
+  %shl = shl i32 %a, 4
+  %or = or disjoint i32 %s, %shl
+  %xor = xor i32 %or, 4
+  ret i32 %xor
+}
+
+define <2 x i32> @fold_xor_with_disjoint_or_vec(<2 x i32> %a, i1 %c) {
+; CHECK-LABEL: define <2 x i32> @fold_xor_with_disjoint_or_vec(
+; CHECK-SAME: <2 x i32> [[A:%.*]], i1 [[C:%.*]]) {
+; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], <2 x i32> zeroinitializer, <2 x i32> <i32 4, i32 4>
+; CHECK-NEXT:    [[SHL:%.*]] = shl <2 x i32> [[A]], <i32 4, i32 4>
+; CHECK-NEXT:    [[OR:%.*]] = or disjoint <2 x i32> [[S]], [[SHL]]
+; CHECK-NEXT:    [[XOR:%.*]] = xor <2 x i32> [[OR]], <i32 4, i32 4>
+; CHECK-NEXT:    ret <2 x i32> [[XOR]]
+;
+  %s = select i1 %c, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 4, i32 4>
+  %shl = shl <2 x i32> %a, <i32 4, i32 4>
+  %or = or disjoint <2 x i32> %s, %shl
+  %xor = xor <2 x i32> %or, <i32 4, i32 4>
+  ret <2 x i32> %xor
+}

>From 7c579c805f21eb0a08768f0dd742968d9513c6db Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sun, 25 Aug 2024 17:01:19 +0200
Subject: [PATCH 2/2] [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)
---
 .../InstCombine/InstCombineAndOrXor.cpp       | 22 +++++++++++++++++++
 .../Transforms/InstCombine/disjoint_or.ll     | 10 ++++-----
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index b703bc7d04db58..59bd133e56b118 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4056,6 +4056,25 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
   return nullptr;
 }
 
+static Instruction *foldXorToOr(BinaryOperator &I,
+                                InstCombiner::BuilderTy &Builder) {
+  assert(I.getOpcode() == Instruction::Xor);
+  Value *A, *B, *C, *D, *E;
+
+  // ((select C, A, B) | E) ^ D) -> (select C, A ^ D, B ^ D) | E)
+  if (match(&I,
+            m_c_Xor(m_c_DisjointOr(m_Select(m_Value(C), m_Value(A), m_Value(B)),
+                                   m_Value(E)),
+                    m_Value(D)))) {
+    Value *XorAB = Builder.CreateXor(A, D);
+    Value *XorBD = Builder.CreateXor(B, D);
+    Value *S = Builder.CreateSelect(C, XorAB, XorBD);
+    return BinaryOperator::CreateDisjointOr(S, E);
+  }
+
+  return nullptr;
+}
+
 /// A ^ B can be specified using other logic ops in a variety of patterns. We
 /// can fold these early and efficiently by morphing an existing instruction.
 static Instruction *foldXorToXor(BinaryOperator &I,
@@ -4658,6 +4677,9 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
   if (Instruction *NewXor = foldXorToXor(I, Builder))
     return NewXor;
 
+  if (Instruction *NewOr = foldXorToOr(I, Builder))
+    return NewOr;
+
   // (A&B)^(A&C) -> A&(B^C) etc
   if (Value *V = foldUsingDistributiveLaws(I))
     return replaceInstUsesWith(I, V);
diff --git a/llvm/test/Transforms/InstCombine/disjoint_or.ll b/llvm/test/Transforms/InstCombine/disjoint_or.ll
index b873cc575eef62..81fa4c33199fd4 100644
--- a/llvm/test/Transforms/InstCombine/disjoint_or.ll
+++ b/llvm/test/Transforms/InstCombine/disjoint_or.ll
@@ -4,10 +4,9 @@
 define i32 @fold_xor_with_disjoint_or(i32 %a, i1 %c) {
 ; CHECK-LABEL: define i32 @fold_xor_with_disjoint_or(
 ; CHECK-SAME: i32 [[A:%.*]], i1 [[C:%.*]]) {
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i32 0, i32 4
 ; CHECK-NEXT:    [[SHL:%.*]] = shl i32 [[A]], 4
-; CHECK-NEXT:    [[OR:%.*]] = or disjoint i32 [[S]], [[SHL]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[OR]], 4
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[C]], i32 4, i32 0
+; CHECK-NEXT:    [[XOR:%.*]] = or disjoint i32 [[TMP1]], [[SHL]]
 ; CHECK-NEXT:    ret i32 [[XOR]]
 ;
   %s = select i1 %c, i32 0, i32 4
@@ -20,10 +19,9 @@ define i32 @fold_xor_with_disjoint_or(i32 %a, i1 %c) {
 define <2 x i32> @fold_xor_with_disjoint_or_vec(<2 x i32> %a, i1 %c) {
 ; CHECK-LABEL: define <2 x i32> @fold_xor_with_disjoint_or_vec(
 ; CHECK-SAME: <2 x i32> [[A:%.*]], i1 [[C:%.*]]) {
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], <2 x i32> zeroinitializer, <2 x i32> <i32 4, i32 4>
 ; CHECK-NEXT:    [[SHL:%.*]] = shl <2 x i32> [[A]], <i32 4, i32 4>
-; CHECK-NEXT:    [[OR:%.*]] = or disjoint <2 x i32> [[S]], [[SHL]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor <2 x i32> [[OR]], <i32 4, i32 4>
+; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[C]], <2 x i32> <i32 4, i32 4>, <2 x i32> zeroinitializer
+; CHECK-NEXT:    [[XOR:%.*]] = or disjoint <2 x i32> [[TMP1]], [[SHL]]
 ; CHECK-NEXT:    ret <2 x i32> [[XOR]]
 ;
   %s = select i1 %c, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 4, i32 4>



More information about the llvm-commits mailing list