[llvm] 3d90048 - [InstCombine] enhance freelyNegateValue() by handling xor
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 1 12:05:25 PDT 2020
Author: Sanjay Patel
Date: 2020-04-01T15:05:13-04:00
New Revision: 3d9004879118c734fd9a5e112fb2bf22cf647668
URL: https://github.com/llvm/llvm-project/commit/3d9004879118c734fd9a5e112fb2bf22cf647668
DIFF: https://github.com/llvm/llvm-project/commit/3d9004879118c734fd9a5e112fb2bf22cf647668.diff
LOG: [InstCombine] enhance freelyNegateValue() by handling xor
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,
but seems like we're going to need to improve here and/or revive D68408?
Alive2 proofs:
http://volta.cs.utah.edu:8080/z/Re5tMU
http://volta.cs.utah.edu:8080/z/An-uns
Differential Revision: https://reviews.llvm.org/D77230
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/sub-of-negatible.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 23370cf21f07..c3890ed69421 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -910,6 +910,18 @@ Value *InstCombiner::freelyNegateValue(Value *V) {
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));
diff --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
index 2b26b304eb6b..897242e4837b 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -378,9 +378,9 @@ define i8 @n21(i8 %x, i16 %y) {
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 i4 @negate_xor(i4 %x) {
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 i8 @negate_xor_use(i8 %x) {
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
More information about the llvm-commits
mailing list