Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -2161,6 +2161,15 @@ return N0; if (isConstantOrConstantVector(N1, /* NoOpaque */ true)) { + // fold ((A-c1)+c2) -> (A+(c2-c1)) + if (N0.getOpcode() == ISD::SUB && + isConstantOrConstantVector(N0.getOperand(1), /* NoOpaque */ true)) { + SDValue Sub = DAG.FoldConstantArithmetic(ISD::SUB, DL, VT, N1.getNode(), + N0.getOperand(1).getNode()); + assert(Sub && "Constant folding failed"); + return DAG.getNode(ISD::ADD, DL, VT, N0.getOperand(0), Sub); + } + // fold ((c1-A)+c2) -> (c1+c2)-A if (N0.getOpcode() == ISD::SUB && isConstantOrConstantVector(N0.getOperand(0), /* NoOpaque */ true)) { Index: test/CodeGen/AArch64/vec_add.ll =================================================================== --- test/CodeGen/AArch64/vec_add.ll +++ test/CodeGen/AArch64/vec_add.ll @@ -26,9 +26,7 @@ define <4 x i32> @add_const_sub_const(<4 x i32> %arg) { ; CHECK-LABEL: add_const_sub_const: ; CHECK: // %bb.0: -; CHECK-NEXT: mvni v2.4s, #1 -; CHECK-NEXT: movi v1.4s, #8 -; CHECK-NEXT: sub v0.4s, v0.4s, v2.4s +; CHECK-NEXT: movi v1.4s, #10 ; CHECK-NEXT: add v0.4s, v0.4s, v1.4s ; CHECK-NEXT: ret %t0 = add <4 x i32> %arg, @@ -62,12 +60,9 @@ define <4 x i32> @add_const_sub_const_nonsplat(<4 x i32> %arg) { ; CHECK-LABEL: add_const_sub_const_nonsplat: ; CHECK: // %bb.0: -; CHECK-NEXT: adrp x9, .LCPI4_1 ; CHECK-NEXT: adrp x8, .LCPI4_0 -; CHECK-NEXT: ldr q1, [x9, :lo12:.LCPI4_1] -; CHECK-NEXT: ldr q2, [x8, :lo12:.LCPI4_0] -; CHECK-NEXT: sub v0.4s, v0.4s, v1.4s -; CHECK-NEXT: add v0.4s, v0.4s, v2.4s +; CHECK-NEXT: ldr q1, [x8, :lo12:.LCPI4_0] +; CHECK-NEXT: add v0.4s, v0.4s, v1.4s ; CHECK-NEXT: ret %t0 = add <4 x i32> %arg, %t1 = sub <4 x i32> %t0, @@ -77,9 +72,7 @@ define <4 x i32> @sub_const_add_const(<4 x i32> %arg) { ; CHECK-LABEL: sub_const_add_const: ; CHECK: // %bb.0: -; CHECK-NEXT: mvni v1.4s, #1 -; CHECK-NEXT: sub v0.4s, v0.4s, v1.4s -; CHECK-NEXT: movi v1.4s, #8 +; CHECK-NEXT: movi v1.4s, #10 ; CHECK-NEXT: add v0.4s, v0.4s, v1.4s ; CHECK-NEXT: ret %t0 = sub <4 x i32> %arg, @@ -95,12 +88,12 @@ ; CHECK-NEXT: .cfi_def_cfa_offset 32 ; CHECK-NEXT: .cfi_offset w30, -16 ; CHECK-NEXT: mvni v1.4s, #1 -; CHECK-NEXT: sub v0.4s, v0.4s, v1.4s ; CHECK-NEXT: str q0, [sp] // 16-byte Folded Spill +; CHECK-NEXT: sub v0.4s, v0.4s, v1.4s ; CHECK-NEXT: bl use ; CHECK-NEXT: ldr q1, [sp] // 16-byte Folded Reload ; CHECK-NEXT: ldr x30, [sp, #16] // 8-byte Folded Reload -; CHECK-NEXT: movi v0.4s, #8 +; CHECK-NEXT: movi v0.4s, #10 ; CHECK-NEXT: add v0.4s, v1.4s, v0.4s ; CHECK-NEXT: add sp, sp, #32 // =32 ; CHECK-NEXT: ret @@ -115,10 +108,7 @@ ; CHECK: // %bb.0: ; CHECK-NEXT: adrp x8, .LCPI7_0 ; CHECK-NEXT: ldr q1, [x8, :lo12:.LCPI7_0] -; CHECK-NEXT: adrp x8, .LCPI7_1 -; CHECK-NEXT: ldr q2, [x8, :lo12:.LCPI7_1] -; CHECK-NEXT: sub v0.4s, v0.4s, v1.4s -; CHECK-NEXT: add v0.4s, v0.4s, v2.4s +; CHECK-NEXT: add v0.4s, v0.4s, v1.4s ; CHECK-NEXT: ret %t0 = sub <4 x i32> %arg, %t1 = add <4 x i32> %t0, Index: test/CodeGen/X86/vec_add.ll =================================================================== --- test/CodeGen/X86/vec_add.ll +++ test/CodeGen/X86/vec_add.ll @@ -36,13 +36,11 @@ define <4 x i32> @add_const_sub_const(<4 x i32> %arg) { ; X86-LABEL: add_const_sub_const: ; X86: # %bb.0: -; X86-NEXT: psubd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: paddd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: retl ; ; X64-LABEL: add_const_sub_const: ; X64: # %bb.0: -; X64-NEXT: psubd {{.*}}(%rip), %xmm0 ; X64-NEXT: paddd {{.*}}(%rip), %xmm0 ; X64-NEXT: retq %t0 = add <4 x i32> %arg, @@ -85,13 +83,11 @@ define <4 x i32> @add_const_sub_const_nonsplat(<4 x i32> %arg) { ; X86-LABEL: add_const_sub_const_nonsplat: ; X86: # %bb.0: -; X86-NEXT: psubd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: paddd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: retl ; ; X64-LABEL: add_const_sub_const_nonsplat: ; X64: # %bb.0: -; X64-NEXT: psubd {{.*}}(%rip), %xmm0 ; X64-NEXT: paddd {{.*}}(%rip), %xmm0 ; X64-NEXT: retq %t0 = add <4 x i32> %arg, @@ -102,13 +98,11 @@ define <4 x i32> @sub_const_add_const(<4 x i32> %arg) { ; X86-LABEL: sub_const_add_const: ; X86: # %bb.0: -; X86-NEXT: psubd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: paddd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: retl ; ; X64-LABEL: sub_const_add_const: ; X64: # %bb.0: -; X64-NEXT: psubd {{.*}}(%rip), %xmm0 ; X64-NEXT: paddd {{.*}}(%rip), %xmm0 ; X64-NEXT: retq %t0 = sub <4 x i32> %arg, @@ -121,8 +115,8 @@ ; X86: # %bb.0: ; X86-NEXT: subl $28, %esp ; X86-NEXT: .cfi_def_cfa_offset 32 -; X86-NEXT: psubd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: movdqu %xmm0, (%esp) # 16-byte Spill +; X86-NEXT: psubd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: calll use ; X86-NEXT: movdqu (%esp), %xmm0 # 16-byte Reload ; X86-NEXT: paddd {{\.LCPI.*}}, %xmm0 @@ -134,8 +128,8 @@ ; X64: # %bb.0: ; X64-NEXT: subq $24, %rsp ; X64-NEXT: .cfi_def_cfa_offset 32 -; X64-NEXT: psubd {{.*}}(%rip), %xmm0 ; X64-NEXT: movdqa %xmm0, (%rsp) # 16-byte Spill +; X64-NEXT: psubd {{.*}}(%rip), %xmm0 ; X64-NEXT: callq use ; X64-NEXT: movdqa (%rsp), %xmm0 # 16-byte Reload ; X64-NEXT: paddd {{.*}}(%rip), %xmm0 @@ -151,13 +145,11 @@ define <4 x i32> @sub_const_add_const_nonsplat(<4 x i32> %arg) { ; X86-LABEL: sub_const_add_const_nonsplat: ; X86: # %bb.0: -; X86-NEXT: psubd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: paddd {{\.LCPI.*}}, %xmm0 ; X86-NEXT: retl ; ; X64-LABEL: sub_const_add_const_nonsplat: ; X64: # %bb.0: -; X64-NEXT: psubd {{.*}}(%rip), %xmm0 ; X64-NEXT: paddd {{.*}}(%rip), %xmm0 ; X64-NEXT: retq %t0 = sub <4 x i32> %arg,