[PATCH] D77230: [InstCombine] enhance freelyNegateValue() by handling xor
Sanjay Patel via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 1 10:26:39 PDT 2020
spatel created this revision.
spatel added reviewers: lebedev.ri, nikic, dmgreen.
Herald added subscribers: hiraditya, mcrosier.
Negation is equivalent to bitwise-not + 1, so try to convert more subtracts into adds using this relationship:
0 - (A ^ C) => ((A ^ C) ^ -1) + 1 => A ^ ~C + 1
I doubt this will recover the regression noted in rGf2fbdf76d8d0 <https://reviews.llvm.org/rGf2fbdf76d8d07f6a0fbd97825cbc533660d64a37>, but seems like we're going to need to improve here and/or revive D68408 <https://reviews.llvm.org/D68408>?
Alive2 proofs:
http://volta.cs.utah.edu:8080/z/Re5tMU
http://volta.cs.utah.edu:8080/z/An-uns
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.254235.patch
Type: text/x-patch
Size: 2701 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200401/56085fd9/attachment-0001.bin>
More information about the llvm-commits
mailing list