[PATCH] Instcombine ( ( ~A ^ B ) ^ ( A & ~B ) ) to A | ~B

David Majnemer david.majnemer at gmail.com
Wed Aug 13 16:02:20 PDT 2014


================
Comment at: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:2532-2555
@@ -2531,1 +2531,26 @@
+
+    // (~A ^ B) ^ (A & ~B) -> (A | ~B)
+    // (~A ^ B) ^ (~B & A) -> (A | ~B)
+    if (match(Op0I, m_Xor(m_Not(m_Value(A)), m_Value(B))) &&
+        (match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B)))) ||
+         match(Op1I, m_And(m_Not(m_Specific(B)), m_Specific(A)))))
+      return BinaryOperator::CreateOr(A, Builder->CreateNot(B));
+    // (B ^ ~A) ^ (A & ~B) -> (A | ~B)
+    // (B ^ ~A) ^ (~B & A) -> (A | ~B)
+    if (match(Op0I, m_Xor(m_Value(B), m_Not(m_Value(A)))) &&
+        (match(Op1I, m_And(m_Specific(A), m_Not(m_Specific(B)))) ||
+         match(Op1I, m_And(m_Not(m_Specific(B)), m_Specific(A)))))
+      return BinaryOperator::CreateOr(A, Builder->CreateNot(B));
+    // (A & ~B) ^ (B ^ ~A) -> (A | ~B)
+    // (A & ~B) ^ (~A ^ B) -> (A | ~B)
+    if (match(Op0I, m_And(m_Value(A), m_Not(m_Value(B)))) &&
+        (match(Op1I, m_Xor(m_Specific(B), m_Not(m_Specific(A)))) ||
+         match(Op1I, m_Xor(m_Not(m_Specific(A)), m_Specific(B)))))
+      return BinaryOperator::CreateOr(A, Builder->CreateNot(B));
+    // (~B & A) ^ (B ^ ~A) -> (A | ~B)
+    // (~B & A) ^ (~A ^ B) -> (A | ~B)
+    if (match(Op0I, m_And(m_Not(m_Value(B)), m_Value(A))) &&
+        (match(Op1I, m_Xor(m_Specific(B), m_Not(m_Specific(A)))) ||
+         match(Op1I, m_Xor(m_Not(m_Specific(A)), m_Specific(B)))))
+      return BinaryOperator::CreateOr(A, Builder->CreateNot(B));
   }
----------------
You have what appear to be eight different patterns you are trying to match but only one test case. Are you sure we need all of them? What happens if you ran a reassociate pass first?

================
Comment at: test/Transforms/InstCombine/or-xor.ll:153-161
@@ +152,11 @@
+define i32 @test16(i32 %x, i32 %y) {
+  %1 = xor i32 %x, -1
+  %2 = xor i32 %1, %y
+  %3 = xor i32 %y, -1
+  %4 = and i32 %3, %x
+  %5 = xor i32 %2, %4
+  ret i32 %5
+; CHECK-LABEL: @test16(
+; CHECK-NEXT: %1 = xor i32 %y, -1
+; CHECK-NEXT: %2 = or i32 %x, %1
+; CHECK-NEXT: ret i32 %2
----------------
Please give these variables better names.

http://reviews.llvm.org/D4878






More information about the llvm-commits mailing list