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

Commit a783e1f

Browse files
committed
[X86] When expanding a multiply by a negative of one less than a power of 2, like 31, don't generate a negate of a subtract that we'll never optimize.
We generated a subtract for the power of 2 minus one then negated the result. The negate can be optimized away by swapping the subtract operands, but DAG combine doesn't know how to do that and we don't add any of the new nodes to the worklist anyway. This patch makes use explicitly emit the swapped subtract. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337858 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 89b692c commit a783e1f

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

lib/Target/X86/X86ISelLowering.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33922,14 +33922,20 @@ static SDValue combineMul(SDNode *N, SelectionDAG &DAG,
3392233922
DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
3392333923
DAG.getConstant(Log2_64(NumSign * SignMulAmt - 1), DL,
3392433924
MVT::i8)));
33925+
// To negate, subtract the number from zero
33926+
if (SignMulAmt < 0)
33927+
NewMul = DAG.getNode(ISD::SUB, DL, VT,
33928+
DAG.getConstant(0, DL, VT), NewMul);
3392533929
} else if (IsPowerOf2_64MinusOne) {
3392633930
// (mul x, 2^N - 1) => (sub (shl x, N), x)
33927-
NewMul = DAG.getNode(
33928-
ISD::SUB, DL, VT,
33929-
DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
33930-
DAG.getConstant(Log2_64(NumSign * SignMulAmt + 1), DL,
33931-
MVT::i8)),
33932-
N->getOperand(0));
33931+
NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
33932+
DAG.getConstant(Log2_64(NumSign * SignMulAmt + 1),
33933+
DL, MVT::i8));
33934+
// To negate, reverse the operands of the subtract.
33935+
if (SignMulAmt < 0)
33936+
NewMul = DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), NewMul);
33937+
else
33938+
NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
3393333939
} else if (IsPowerOf2_64MinusTwo && NumSign == 1) {
3393433940
// (mul x, 2^N - 1) => (sub (shl x, N), x)
3393533941
NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
@@ -33938,10 +33944,6 @@ static SDValue combineMul(SDNode *N, SelectionDAG &DAG,
3393833944
NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
3393933945
NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
3394033946
}
33941-
// To negate, subtract the number from zero
33942-
if (NewMul && NumSign == -1)
33943-
NewMul =
33944-
DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), NewMul);
3394533947
}
3394633948
}
3394733949

test/CodeGen/X86/imul.ll

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,16 @@ define i32 @test1(i32 %a) {
324324
; X64: # %bb.0: # %entry
325325
; X64-NEXT: movl %edi, %eax
326326
; X64-NEXT: shll $5, %eax
327-
; X64-NEXT: subl %edi, %eax
328-
; X64-NEXT: negl %eax
327+
; X64-NEXT: subl %eax, %edi
328+
; X64-NEXT: movl %edi, %eax
329329
; X64-NEXT: retq
330330
;
331331
; X86-LABEL: test1:
332332
; X86: # %bb.0: # %entry
333-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
334-
; X86-NEXT: movl %ecx, %eax
335-
; X86-NEXT: shll $5, %eax
333+
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
334+
; X86-NEXT: movl %eax, %ecx
335+
; X86-NEXT: shll $5, %ecx
336336
; X86-NEXT: subl %ecx, %eax
337-
; X86-NEXT: negl %eax
338337
; X86-NEXT: retl
339338
entry:
340339
%tmp3 = mul i32 %a, -31
@@ -414,8 +413,8 @@ define i64 @test5(i64 %a) {
414413
; X64: # %bb.0: # %entry
415414
; X64-NEXT: movq %rdi, %rax
416415
; X64-NEXT: shlq $5, %rax
417-
; X64-NEXT: subq %rdi, %rax
418-
; X64-NEXT: negq %rax
416+
; X64-NEXT: subq %rax, %rdi
417+
; X64-NEXT: movq %rdi, %rax
419418
; X64-NEXT: retq
420419
;
421420
; X86-LABEL: test5:
@@ -424,15 +423,15 @@ define i64 @test5(i64 %a) {
424423
; X86-NEXT: .cfi_def_cfa_offset 8
425424
; X86-NEXT: .cfi_offset %esi, -8
426425
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
427-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
428-
; X86-NEXT: movl %eax, %esi
429-
; X86-NEXT: shll $5, %esi
426+
; X86-NEXT: movl {{[0-9]+}}(%esp), %esi
427+
; X86-NEXT: movl %esi, %eax
428+
; X86-NEXT: shll $5, %eax
430429
; X86-NEXT: subl %eax, %esi
431430
; X86-NEXT: movl $-31, %edx
432431
; X86-NEXT: movl %ecx, %eax
433432
; X86-NEXT: mull %edx
434433
; X86-NEXT: subl %ecx, %edx
435-
; X86-NEXT: subl %esi, %edx
434+
; X86-NEXT: addl %esi, %edx
436435
; X86-NEXT: popl %esi
437436
; X86-NEXT: .cfi_def_cfa_offset 4
438437
; X86-NEXT: retl

0 commit comments

Comments
 (0)