[llvm] r308031 - [InstCombine] convert bitwise (in)equality checks to logical ops (PR32401)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 14 08:09:49 PDT 2017


Author: spatel
Date: Fri Jul 14 08:09:49 2017
New Revision: 308031

URL: http://llvm.org/viewvc/llvm-project?rev=308031&view=rev
Log:
[InstCombine] convert bitwise (in)equality checks to logical ops (PR32401)

As discussed in:
https://bugs.llvm.org/show_bug.cgi?id=32401

we have a backend transform to undo this:
https://reviews.llvm.org/rL299542

when it's likely that the xor version leads to better codegen, but we want 
this form in IR for better analysis and simplification potential.


Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/icmp-logical.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=308031&r1=308030&r2=308031&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Fri Jul 14 08:09:49 2017
@@ -1814,9 +1814,21 @@ Instruction *InstCombiner::foldICmpOrCon
         Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType()));
     Value *CmpQ =
         Builder.CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType()));
-    auto LogicOpc = Pred == ICmpInst::Predicate::ICMP_EQ ? Instruction::And
-                                                         : Instruction::Or;
-    return BinaryOperator::Create(LogicOpc, CmpP, CmpQ);
+    auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
+    return BinaryOperator::Create(BOpc, CmpP, CmpQ);
+  }
+
+  // Are we using xors to bitwise check for a pair of (in)equalities? Convert to
+  // a shorter form that has more potential to be folded even further.
+  Value *X1, *X2, *X3, *X4;
+  if (match(Or->getOperand(0), m_OneUse(m_Xor(m_Value(X1), m_Value(X2)))) &&
+      match(Or->getOperand(1), m_OneUse(m_Xor(m_Value(X3), m_Value(X4))))) {
+    // ((X1 ^ X2) || (X3 ^ X4)) == 0 --> (X1 == X2) && (X3 == X4)
+    // ((X1 ^ X2) || (X3 ^ X4)) != 0 --> (X1 != X2) || (X3 != X4)
+    Value *Cmp12 = Builder.CreateICmp(Pred, X1, X2);
+    Value *Cmp34 = Builder.CreateICmp(Pred, X3, X4);
+    auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
+    return BinaryOperator::Create(BOpc, Cmp12, Cmp34);
   }
 
   return nullptr;

Modified: llvm/trunk/test/Transforms/InstCombine/icmp-logical.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp-logical.ll?rev=308031&r1=308030&r2=308031&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp-logical.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp-logical.ll Fri Jul 14 08:09:49 2017
@@ -157,10 +157,9 @@ define i1 @fold_mask_cmps_to_true(i32 %x
 
 define i1 @cmpeq_bitwise(i8 %a, i8 %b, i8 %c, i8 %d) {
 ; CHECK-LABEL: @cmpeq_bitwise(
-; CHECK-NEXT:    [[XOR1:%.*]] = xor i8 %a, %b
-; CHECK-NEXT:    [[XOR2:%.*]] = xor i8 %c, %d
-; CHECK-NEXT:    [[OR:%.*]] = or i8 [[XOR1]], [[XOR2]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[OR]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i8 %a, %b
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 %c, %d
+; CHECK-NEXT:    [[CMP:%.*]] = and i1 [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %xor1 = xor i8 %a, %b
@@ -172,10 +171,9 @@ define i1 @cmpeq_bitwise(i8 %a, i8 %b, i
 
 define <2 x i1> @cmpne_bitwise(<2 x i64> %a, <2 x i64> %b, <2 x i64> %c, <2 x i64> %d) {
 ; CHECK-LABEL: @cmpne_bitwise(
-; CHECK-NEXT:    [[XOR1:%.*]] = xor <2 x i64> %a, %b
-; CHECK-NEXT:    [[XOR2:%.*]] = xor <2 x i64> %c, %d
-; CHECK-NEXT:    [[OR:%.*]] = or <2 x i64> [[XOR1]], [[XOR2]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i64> [[OR]], zeroinitializer
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i64> %a, %b
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne <2 x i64> %c, %d
+; CHECK-NEXT:    [[CMP:%.*]] = or <2 x i1> [[TMP1]], [[TMP2]]
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %xor1 = xor <2 x i64> %a, %b




More information about the llvm-commits mailing list