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

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 25 08:59:13 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Amr Hesham (AmrDeveloper)

<details>
<summary>Changes</summary>

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

Fixes: #<!-- -->79266

---
Full diff: https://github.com/llvm/llvm-project/pull/105992.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+22) 
- (added) llvm/test/Transforms/InstCombine/disjoint_or.ll (+32) 


``````````diff
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
new file mode 100644
index 00000000000000..81fa4c33199fd4
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/disjoint_or.ll
@@ -0,0 +1,32 @@
+; 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:    [[SHL:%.*]] = shl i32 [[A]], 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
+  %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:    [[SHL:%.*]] = shl <2 x i32> [[A]], <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>
+  %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
+}

``````````

</details>


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


More information about the llvm-commits mailing list