Skip to content

Commit c009d11

Browse files
committed
[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.
1 parent e465f9c commit c009d11

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,15 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
17231723
return Res;
17241724
}
17251725

1726+
if (Constant *C = dyn_cast<Constant>(Op0)) {
1727+
Value *X;
1728+
Constant *C2;
1729+
1730+
// C-(X+C2) --> (C-C2)-X
1731+
if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
1732+
return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
1733+
}
1734+
17261735
auto TryToNarrowDeduceFlags = [this, &I, &Op0, &Op1]() -> Instruction * {
17271736
if (Instruction *Ext = narrowMathIfNoOverflow(I))
17281737
return Ext;
@@ -1834,10 +1843,6 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
18341843
// C-(C2-X) --> X+(C-C2)
18351844
if (match(Op1, m_Sub(m_Constant(C2), m_Value(X))) && !isa<ConstantExpr>(C2))
18361845
return BinaryOperator::CreateAdd(X, ConstantExpr::getSub(C, C2));
1837-
1838-
// C-(X+C2) --> (C-C2)-X
1839-
if (match(Op1, m_Add(m_Value(X), m_Constant(C2))))
1840-
return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
18411846
}
18421847

18431848
const APInt *Op0C;

llvm/test/Transforms/InstCombine/sub-of-negatible.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ define i8 @n14(i8 %x, i8 %y, i8 %z) {
311311

312312
define i8 @neg_of_add_with_constant(i8 %x) {
313313
; CHECK-LABEL: @neg_of_add_with_constant(
314-
; CHECK-NEXT: [[S_NEG:%.*]] = sub i8 -42, [[X:%.*]]
315-
; CHECK-NEXT: ret i8 [[S_NEG]]
314+
; CHECK-NEXT: [[R:%.*]] = sub i8 -42, [[X:%.*]]
315+
; CHECK-NEXT: ret i8 [[R]]
316316
;
317317
%s = add i8 %x, 42
318318
%r = sub i8 0, %s
@@ -323,7 +323,7 @@ define i8 @neg_of_add_with_constant_multi_use(i8 %x) {
323323
; CHECK-LABEL: @neg_of_add_with_constant_multi_use(
324324
; CHECK-NEXT: [[S:%.*]] = add i8 [[X:%.*]], 42
325325
; CHECK-NEXT: call void @use8(i8 [[S]])
326-
; CHECK-NEXT: [[R:%.*]] = sub i8 0, [[S]]
326+
; CHECK-NEXT: [[R:%.*]] = sub i8 -42, [[X]]
327327
; CHECK-NEXT: ret i8 [[R]]
328328
;
329329
%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) {
12471247
; `add` with single negatible operand is still negatible
12481248
define i8 @negate_add_with_single_negatible_operand(i8 %x, i8 %y) {
12491249
; CHECK-LABEL: @negate_add_with_single_negatible_operand(
1250-
; CHECK-NEXT: [[T0_NEG:%.*]] = sub i8 -42, [[X:%.*]]
1251-
; CHECK-NEXT: ret i8 [[T0_NEG]]
1250+
; CHECK-NEXT: [[T1:%.*]] = sub i8 -42, [[X:%.*]]
1251+
; CHECK-NEXT: ret i8 [[T1]]
12521252
;
12531253
%t0 = add i8 %x, 42
12541254
%t1 = sub i8 0, %t0
@@ -1271,7 +1271,7 @@ define i8 @negate_add_with_single_negatible_operand_extrause(i8 %x, i8 %y) {
12711271
; CHECK-LABEL: @negate_add_with_single_negatible_operand_extrause(
12721272
; CHECK-NEXT: [[T0:%.*]] = add i8 [[X:%.*]], 42
12731273
; CHECK-NEXT: call void @use8(i8 [[T0]])
1274-
; CHECK-NEXT: [[T1:%.*]] = sub i8 0, [[T0]]
1274+
; CHECK-NEXT: [[T1:%.*]] = sub i8 -42, [[X]]
12751275
; CHECK-NEXT: ret i8 [[T1]]
12761276
;
12771277
%t0 = add i8 %x, 42

0 commit comments

Comments
 (0)