[llvm] [InstCombine] Optimize 'xor-and-select' sequence to 'or' for bool (PR #66394)

Qi Hu via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 08:54:38 PDT 2023


https://github.com/Qi-Hu created https://github.com/llvm/llvm-project/pull/66394:

Optimize `xor`-`and`-`select` sequence to `or` : https://alive2.llvm.org/ce/z/52ZT62

>From ab6002ff59116bbddb1b0b75c392bed7e344e3e6 Mon Sep 17 00:00:00 2001
From: Qi Hu <qi.hu at huawei.com>
Date: Wed, 13 Sep 2023 18:33:51 -0400
Subject: [PATCH] [InstCombine] Optimize 'xor-and-select' sequence to 'or' for
 bool

xor-and-select -> or : https://alive2.llvm.org/ce/z/52ZT62
---
 .../Transforms/InstCombine/InstCombineSelect.cpp   |  8 ++++++++
 .../InstCombine/fold-xor-and-select-i1.ll          | 14 ++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/fold-xor-and-select-i1.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index e2965e7a5703976..8a0ba6f766b4ad1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3202,6 +3202,14 @@ Instruction *InstCombinerImpl::foldSelectOfBools(SelectInst &SI) {
     }
   }
 
+  // select (~b & a), a, b -> or a, b
+  // only for scalar types
+  if (match(CondVal, m_And(m_Not(m_Specific(FalseVal)), m_Specific(TrueVal))) &&
+      TrueVal->getType()->isIntegerTy(1) &&
+      FalseVal->getType()->isIntegerTy(1)) {
+    return BinaryOperator::CreateOr(TrueVal, FalseVal);
+  }
+
   return nullptr;
 }
 
diff --git a/llvm/test/Transforms/InstCombine/fold-xor-and-select-i1.ll b/llvm/test/Transforms/InstCombine/fold-xor-and-select-i1.ll
new file mode 100644
index 000000000000000..a5870a3ce9d973f
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fold-xor-and-select-i1.ll
@@ -0,0 +1,14 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @max_if(i1 %a, i1 %b) {
+; CHECK-LABEL: define i1 @max_if
+; CHECK-SAME: (i1 [[A:%.*]], i1 [[B:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = or i1 [[A]], [[B]]
+; CHECK-NEXT:    ret i1 [[TMP1]]
+;
+  %1 = xor i1 %b, true
+  %cmp = and i1 %1, %a
+  %2 = select i1 %cmp, i1 %a, i1 %b
+  ret i1 %2
+}



More information about the llvm-commits mailing list