[PATCH] D77230: [InstCombine] enhance freelyNegateValue() by handling xor

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 1 12:23:14 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG3d9004879118: [InstCombine] enhance freelyNegateValue() by handling xor (authored by spatel).
Herald added a project: LLVM.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77230/new/

https://reviews.llvm.org/D77230

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/sub-of-negatible.ll


Index: llvm/test/Transforms/InstCombine/sub-of-negatible.ll
===================================================================
--- llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -378,9 +378,9 @@
 
 define i4 @negate_xor(i4 %x) {
 ; CHECK-LABEL: @negate_xor(
-; CHECK-NEXT:    [[O:%.*]] = xor i4 [[X:%.*]], 5
-; CHECK-NEXT:    [[R:%.*]] = sub i4 0, [[O]]
-; CHECK-NEXT:    ret i4 [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = xor i4 [[X:%.*]], -6
+; CHECK-NEXT:    [[O_NEG:%.*]] = add i4 [[TMP1]], 1
+; CHECK-NEXT:    ret i4 [[O_NEG]]
 ;
   %o = xor i4 %x, 5
   %r = sub i4 0, %o
@@ -389,9 +389,9 @@
 
 define <2 x i4> @negate_xor_vec(<2 x i4> %x) {
 ; CHECK-LABEL: @negate_xor_vec(
-; CHECK-NEXT:    [[O:%.*]] = xor <2 x i4> [[X:%.*]], <i4 5, i4 -6>
-; CHECK-NEXT:    [[R:%.*]] = sub <2 x i4> zeroinitializer, [[O]]
-; CHECK-NEXT:    ret <2 x i4> [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = xor <2 x i4> [[X:%.*]], <i4 -6, i4 5>
+; CHECK-NEXT:    [[O_NEG:%.*]] = add <2 x i4> [[TMP1]], <i4 1, i4 1>
+; CHECK-NEXT:    ret <2 x i4> [[O_NEG]]
 ;
   %o = xor <2 x i4> %x, <i4 5, i4 10>
   %r = sub <2 x i4> zeroinitializer, %o
@@ -413,10 +413,10 @@
 
 define i4 @negate_shl_xor(i4 %x, i4 %y) {
 ; CHECK-LABEL: @negate_shl_xor(
-; CHECK-NEXT:    [[O:%.*]] = xor i4 [[X:%.*]], 5
-; CHECK-NEXT:    [[S:%.*]] = shl i4 [[O]], [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = sub i4 0, [[S]]
-; CHECK-NEXT:    ret i4 [[R]]
+; CHECK-NEXT:    [[TMP1:%.*]] = xor i4 [[X:%.*]], -6
+; CHECK-NEXT:    [[O_NEG:%.*]] = add i4 [[TMP1]], 1
+; CHECK-NEXT:    [[S_NEG:%.*]] = shl i4 [[O_NEG]], [[Y:%.*]]
+; CHECK-NEXT:    ret i4 [[S_NEG]]
 ;
   %o = xor i4 %x, 5
   %s = shl i4 %o, %y
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -910,6 +910,18 @@
     return Builder.CreateSub(
         I->getOperand(1), I->getOperand(0), I->getName() + ".neg");
 
+  // Negation is equivalent to bitwise-not + 1:
+  // 0 - (A ^ C)  =>  ((A ^ C) ^ -1) + 1  =>  A ^ ~C + 1
+  case Instruction::Xor: {
+    Constant *C;
+    if (match(I->getOperand(1), m_Constant(C))) {
+      Value *Xor = Builder.CreateXor(I->getOperand(0), ConstantExpr::getNot(C));
+      return Builder.CreateAdd(Xor, ConstantInt::get(Xor->getType(), 1),
+                               I->getName() + ".neg");
+    }
+    return nullptr;
+  }
+
   // 0-(A sdiv C)  =>  A sdiv (0-C)  provided the negation doesn't overflow.
   case Instruction::SDiv: {
     Constant *C = dyn_cast<Constant>(I->getOperand(1));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77230.254271.patch
Type: text/x-patch
Size: 2701 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200401/5781a74d/attachment.bin>


More information about the llvm-commits mailing list