Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit f97a90d

Browse files
committed
[DAGCombiner] Teach DAGCombiner that A-(-B) is A+B.
We already knew A+(-B) is A-B in visitAdd. This does the opposite for visitSub. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337502 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 91284ba commit f97a90d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,6 +2582,11 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
25822582
if (isAllOnesConstantOrAllOnesSplatConstant(N0))
25832583
return DAG.getNode(ISD::XOR, DL, VT, N1, N0);
25842584

2585+
// fold (A - (0-B)) -> A+B
2586+
if (N1.getOpcode() == ISD::SUB &&
2587+
isNullConstantOrNullSplatConstant(N1.getOperand(0)))
2588+
return DAG.getNode(ISD::ADD, DL, VT, N0, N1.getOperand(1));
2589+
25852590
// fold A-(A-B) -> B
25862591
if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
25872592
return N1.getOperand(1);

test/CodeGen/X86/combine-srem.ll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,9 @@ define <4 x i32> @combine_vec_srem_by_pow2a_neg(<4 x i32> %x) {
248248
; SSE-NEXT: paddd %xmm0, %xmm1
249249
; SSE-NEXT: psrad $2, %xmm1
250250
; SSE-NEXT: pxor %xmm2, %xmm2
251-
; SSE-NEXT: pxor %xmm3, %xmm3
252-
; SSE-NEXT: psubd %xmm1, %xmm3
253-
; SSE-NEXT: pslld $2, %xmm3
254-
; SSE-NEXT: psubd %xmm3, %xmm2
255-
; SSE-NEXT: psubd %xmm2, %xmm0
251+
; SSE-NEXT: psubd %xmm1, %xmm2
252+
; SSE-NEXT: pslld $2, %xmm2
253+
; SSE-NEXT: paddd %xmm2, %xmm0
256254
; SSE-NEXT: retq
257255
;
258256
; AVX-LABEL: combine_vec_srem_by_pow2a_neg:
@@ -264,8 +262,7 @@ define <4 x i32> @combine_vec_srem_by_pow2a_neg(<4 x i32> %x) {
264262
; AVX-NEXT: vpxor %xmm2, %xmm2, %xmm2
265263
; AVX-NEXT: vpsubd %xmm1, %xmm2, %xmm1
266264
; AVX-NEXT: vpslld $2, %xmm1, %xmm1
267-
; AVX-NEXT: vpsubd %xmm1, %xmm2, %xmm1
268-
; AVX-NEXT: vpsubd %xmm1, %xmm0, %xmm0
265+
; AVX-NEXT: vpaddd %xmm1, %xmm0, %xmm0
269266
; AVX-NEXT: retq
270267
%1 = srem <4 x i32> %x, <i32 -4, i32 -4, i32 -4, i32 -4>
271268
ret <4 x i32> %1

0 commit comments

Comments
 (0)