[PATCH] D60395: [InstCombine] canonicalize sdiv with NEG operand
ChenZheng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 7 21:45:46 PDT 2019
shchenz created this revision.
shchenz added reviewers: spatel, lebedev.ri, RKSimon.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
int foo(int i, int j)
{
int res = -( i / j);
int res2 = -i / j;
return res + res2;
}
`clang -O3 -Ofast a.c -c`
0: d0 00 a3 7c neg r5,r3
4: d6 23 63 7c divw r3,r3,r4
8: d6 23 85 7d divw r12,r5,r4
c: 50 60 63 7c subf r3,r3,r12
10: b4 07 63 7c extsw r3,r3
14: 20 00 80 4e blr
We should commonning the two `divw`.
https://rise4fun.com/Alive/9G4
https://reviews.llvm.org/D60395
Files:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/sdiv-canonicalize.ll
Index: llvm/test/Transforms/InstCombine/sdiv-canonicalize.ll
===================================================================
--- llvm/test/Transforms/InstCombine/sdiv-canonicalize.ll
+++ llvm/test/Transforms/InstCombine/sdiv-canonicalize.ll
@@ -3,8 +3,8 @@
define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) {
; CHECK-LABEL: @test_sdiv_canonicalize_op0(
-; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
-; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
+; CHECK-NEXT: [[NEG:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[SDIV:%.*]] = sub i32 0, [[NEG]]
; CHECK-NEXT: ret i32 [[SDIV]]
;
%neg = sub nsw i32 0, %x
@@ -15,8 +15,8 @@
define i32 @test_sdiv_canonicalize_op1(i32 %x, i32 %z) {
; CHECK-LABEL: @test_sdiv_canonicalize_op1(
; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3
-; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
-; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[Y]], [[NEG]]
+; CHECK-NEXT: [[NEG:%.*]] = sdiv i32 [[Y]], [[X:%.*]]
+; CHECK-NEXT: [[SDIV:%.*]] = sub i32 0, [[NEG]]
; CHECK-NEXT: ret i32 [[SDIV]]
;
%y = mul i32 %z, 3
@@ -38,8 +38,8 @@
define <2 x i32> @test_sdiv_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @test_sdiv_canonicalize_vec(
-; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X:%.*]]
-; CHECK-NEXT: [[SDIV:%.*]] = sdiv <2 x i32> [[NEG]], [[Y:%.*]]
+; CHECK-NEXT: [[NEG:%.*]] = sdiv <2 x i32> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[SDIV:%.*]] = sub <2 x i32> zeroinitializer, [[NEG]]
; CHECK-NEXT: ret <2 x i32> [[SDIV]]
;
%neg = sub nsw <2 x i32> <i32 0, i32 0>, %x
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1021,6 +1021,14 @@
return new SExtInst(NarrowOp, Op0->getType());
}
}
+
+ // -X / Y --> -(X / Y)
+ // X / -Y --> -(X / Y)
+ Value *Y;
+ if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))) ||
+ match(&I, m_SDiv(m_Value(X), m_OneUse(m_NSWSub(m_Zero(), m_Value(Y))))))
+ return BinaryOperator::CreateNeg(
+ Builder.CreateSDiv(X, Y, I.getName(), I.isExact()));
if (Constant *RHS = dyn_cast<Constant>(Op1)) {
// X/INT_MIN -> X == INT_MIN
Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1693,8 +1693,11 @@
// 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow.
if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) && match(Op0, m_Zero()) &&
- C->isNotMinSignedValue() && !C->isOneValue())
- return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
+ C->isNotMinSignedValue() && !C->isOneValue()) {
+ auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
+ BO->setIsExact(cast<BinaryOperator>(Op1)->isExact());
+ return BO;
+ }
// 0 - (X << Y) -> (-X << Y) when X is freely negatable.
if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero()))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60395.194092.patch
Type: text/x-patch
Size: 3312 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190408/f54a16da/attachment.bin>
More information about the llvm-commits
mailing list