[llvm] c009d11 - [InstCombine] Perform C-(X+C2) --> (C-C2)-X transform before using Negator
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 3 05:07:23 PST 2020
Author: Roman Lebedev
Date: 2020-11-03T16:06:52+03:00
New Revision: c009d11bdac4a7f4a3a8ae85e42da053828a6f24
URL: https://github.com/llvm/llvm-project/commit/c009d11bdac4a7f4a3a8ae85e42da053828a6f24
DIFF: https://github.com/llvm/llvm-project/commit/c009d11bdac4a7f4a3a8ae85e42da053828a6f24.diff
LOG: [InstCombine] Perform C-(X+C2) --> (C-C2)-X transform before using Negator
In particular, it makes it fire for C=0, because negator doesn't want
to perform that fold since in general it's not beneficial.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/sub-of-negatible.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 4987ba7091ae..e78df30570e0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1723,6 +1723,15 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
return Res;
}
+ if (Constant *C = dyn_cast<Constant>(Op0)) {
+ Value *X;
+ Constant *C2;
+
+ // C-(X+C2) --> (C-C2)-X
+ if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
+ return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
+ }
+
auto TryToNarrowDeduceFlags = [this, &I, &Op0, &Op1]() -> Instruction * {
if (Instruction *Ext = narrowMathIfNoOverflow(I))
return Ext;
@@ -1834,10 +1843,6 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
// C-(C2-X) --> X+(C-C2)
if (match(Op1, m_Sub(m_Constant(C2), m_Value(X))) && !isa<ConstantExpr>(C2))
return BinaryOperator::CreateAdd(X, ConstantExpr::getSub(C, C2));
-
- // C-(X+C2) --> (C-C2)-X
- if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
- return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
}
const APInt *Op0C;
diff --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
index bd8e9d23d73f..c415b55d2c92 100644
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll
@@ -311,8 +311,8 @@ define i8 @n14(i8 %x, i8 %y, i8 %z) {
define i8 @neg_of_add_with_constant(i8 %x) {
; CHECK-LABEL: @neg_of_add_with_constant(
-; CHECK-NEXT: [[S_NEG:%.*]] = sub i8 -42, [[X:%.*]]
-; CHECK-NEXT: ret i8 [[S_NEG]]
+; CHECK-NEXT: [[R:%.*]] = sub i8 -42, [[X:%.*]]
+; CHECK-NEXT: ret i8 [[R]]
;
%s = add i8 %x, 42
%r = sub i8 0, %s
@@ -323,7 +323,7 @@ define i8 @neg_of_add_with_constant_multi_use(i8 %x) {
; CHECK-LABEL: @neg_of_add_with_constant_multi_use(
; CHECK-NEXT: [[S:%.*]] = add i8 [[X:%.*]], 42
; CHECK-NEXT: call void @use8(i8 [[S]])
-; CHECK-NEXT: [[R:%.*]] = sub i8 0, [[S]]
+; CHECK-NEXT: [[R:%.*]] = sub i8 -42, [[X]]
; CHECK-NEXT: ret i8 [[R]]
;
%s = add i8 %x, 42
@@ -1247,8 +1247,8 @@ define i8 @negate_left_shift_by_constant_extrause(i8 %x, i8 %y, i8 %z, i8 %k) {
; `add` with single negatible operand is still negatible
define i8 @negate_add_with_single_negatible_operand(i8 %x, i8 %y) {
; CHECK-LABEL: @negate_add_with_single_negatible_operand(
-; CHECK-NEXT: [[T0_NEG:%.*]] = sub i8 -42, [[X:%.*]]
-; CHECK-NEXT: ret i8 [[T0_NEG]]
+; CHECK-NEXT: [[T1:%.*]] = sub i8 -42, [[X:%.*]]
+; CHECK-NEXT: ret i8 [[T1]]
;
%t0 = add i8 %x, 42
%t1 = sub i8 0, %t0
@@ -1271,7 +1271,7 @@ define i8 @negate_add_with_single_negatible_operand_extrause(i8 %x, i8 %y) {
; CHECK-LABEL: @negate_add_with_single_negatible_operand_extrause(
; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], 42
; CHECK-NEXT: call void @use8(i8 [[T0]])
-; CHECK-NEXT: [[T1:%.*]] = sub i8 0, [[T0]]
+; CHECK-NEXT: [[T1:%.*]] = sub i8 -42, [[X]]
; CHECK-NEXT: ret i8 [[T1]]
;
%t0 = add i8 %x, 42
More information about the llvm-commits
mailing list