[PATCH] D12156: Optimize bitwise even/odd test (-x&1 -> x&1) to not use negation.

Balaram Makam via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 10:59:52 PDT 2015


bmakam updated this revision to Diff 32580.
bmakam added a comment.

adding more context to diff.


http://reviews.llvm.org/D12156

Files:
  lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
  test/Transforms/InstCombine/and-with-one.ll

Index: test/Transforms/InstCombine/and-with-one.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/and-with-one.ll
@@ -0,0 +1,25 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+define i64 @test1(i64 %x) {
+; CHECK-LABEL: @test1(
+; CHECK-NOT: %sub = sub nsw i64 0, %x
+; CHECK-NOT: %and = and i64 %sub, 1
+; CHECK-NEXT: %and = and i64 %x, 1
+; CHECK-NEXT: ret i64 %and
+  %sub = sub nsw i64 0, %x
+  %and = and i64 %sub, 1
+  ret i64 %and
+}
+
+define i64 @test2(i64 %x) {
+; CHECK-LABEL: @test2(
+; CHECK-NOT: %sub = sub nsw i64 0, %x
+; CHECK-NEXT: %and = and i64 %x, 1
+; CHECK-NOT: %add = add i64 %sub, %and
+; CHECK-NEXT: %add = sub i64 %and, %x
+; CHECK-NEXT: ret i64 %add
+  %sub = sub nsw i64 0, %x
+  %and = and i64 %sub, 1
+  %add = add i64 %sub, %and
+  ret i64 %add
+}
Index: lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1272,6 +1272,14 @@
         if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
           return BinaryOperator::CreateAnd(V, AndRHS);
 
+        // -x & 1 -> x & 1
+        if (AndRHSMask == 1 && isa<ConstantInt>(Op0LHS)) {
+            const APInt &Op0LHSMask = dyn_cast<ConstantInt>(Op0LHS)->getValue();
+            if (Op0LHSMask == 0) {
+              return BinaryOperator::CreateAnd(Op0RHS, AndRHS);
+            }
+        }
+
         // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS
         // has 1's for all bits that the subtraction with A might affect.
         if (Op0I->hasOneUse() && !match(Op0LHS, m_Zero())) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12156.32580.patch
Type: text/x-patch
Size: 1754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150819/7ccb7731/attachment.bin>


More information about the llvm-commits mailing list