[llvm] r338101 - [X86] Remove an unnecessary 'if' that prevented treating INT64_MAX and -INT64_MAX as power of 2 minus 1 in the multiply expansion code.
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 26 22:56:27 PDT 2018
Author: ctopper
Date: Thu Jul 26 22:56:27 2018
New Revision: 338101
URL: http://llvm.org/viewvc/llvm-project?rev=338101&view=rev
Log:
[X86] Remove an unnecessary 'if' that prevented treating INT64_MAX and -INT64_MAX as power of 2 minus 1 in the multiply expansion code.
Not sure why they were being explicitly excluded, but I believe all the math inside the if works. I changed the absolute value to be uint64_t instead of int64_t so INT64_MIN+1 wouldn't be signed wrap.
Modified:
llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
llvm/trunk/test/CodeGen/X86/imul.ll
Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.cpp?rev=338101&r1=338100&r2=338101&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp Thu Jul 26 22:56:27 2018
@@ -33928,45 +33928,43 @@ static SDValue combineMul(SDNode *N, Sel
"Both cases that could cause potential overflows should have "
"already been handled.");
int64_t SignMulAmt = C->getSExtValue();
- if ((SignMulAmt != INT64_MIN) && (SignMulAmt != INT64_MAX) &&
- (SignMulAmt != -INT64_MAX)) {
- int64_t AbsMulAmt = SignMulAmt < 0 ? -SignMulAmt : SignMulAmt;
- if (isPowerOf2_64(AbsMulAmt - 1)) {
- // (mul x, 2^N + 1) => (add (shl x, N), x)
- NewMul = DAG.getNode(
- ISD::ADD, DL, VT, N->getOperand(0),
- DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
- DAG.getConstant(Log2_64(AbsMulAmt - 1), DL,
- MVT::i8)));
- // To negate, subtract the number from zero
- if (SignMulAmt < 0)
- NewMul = DAG.getNode(ISD::SUB, DL, VT,
- DAG.getConstant(0, DL, VT), NewMul);
- } else if (isPowerOf2_64(AbsMulAmt + 1)) {
- // (mul x, 2^N - 1) => (sub (shl x, N), x)
- NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
- DAG.getConstant(Log2_64(AbsMulAmt + 1),
- DL, MVT::i8));
- // To negate, reverse the operands of the subtract.
- if (SignMulAmt < 0)
- NewMul = DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), NewMul);
- else
- NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
- } else if (SignMulAmt >= 0 && isPowerOf2_64(AbsMulAmt - 2)) {
- // (mul x, 2^N + 2) => (add (add (shl x, N), x), x)
- NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
- DAG.getConstant(Log2_64(AbsMulAmt - 2),
- DL, MVT::i8));
- NewMul = DAG.getNode(ISD::ADD, DL, VT, NewMul, N->getOperand(0));
- NewMul = DAG.getNode(ISD::ADD, DL, VT, NewMul, N->getOperand(0));
- } else if (SignMulAmt >= 0 && isPowerOf2_64(AbsMulAmt + 2)) {
- // (mul x, 2^N - 2) => (sub (sub (shl x, N), x), x)
- NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
- DAG.getConstant(Log2_64(AbsMulAmt + 2),
- DL, MVT::i8));
+ assert(SignMulAmt != INT64_MIN && "Int min should have been handled!");
+ uint64_t AbsMulAmt = SignMulAmt < 0 ? -SignMulAmt : SignMulAmt;
+ if (isPowerOf2_64(AbsMulAmt - 1)) {
+ // (mul x, 2^N + 1) => (add (shl x, N), x)
+ NewMul = DAG.getNode(
+ ISD::ADD, DL, VT, N->getOperand(0),
+ DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
+ DAG.getConstant(Log2_64(AbsMulAmt - 1), DL,
+ MVT::i8)));
+ // To negate, subtract the number from zero
+ if (SignMulAmt < 0)
+ NewMul = DAG.getNode(ISD::SUB, DL, VT,
+ DAG.getConstant(0, DL, VT), NewMul);
+ } else if (isPowerOf2_64(AbsMulAmt + 1)) {
+ // (mul x, 2^N - 1) => (sub (shl x, N), x)
+ NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
+ DAG.getConstant(Log2_64(AbsMulAmt + 1),
+ DL, MVT::i8));
+ // To negate, reverse the operands of the subtract.
+ if (SignMulAmt < 0)
+ NewMul = DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), NewMul);
+ else
NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
- NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
- }
+ } else if (SignMulAmt >= 0 && isPowerOf2_64(AbsMulAmt - 2)) {
+ // (mul x, 2^N + 2) => (add (add (shl x, N), x), x)
+ NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
+ DAG.getConstant(Log2_64(AbsMulAmt - 2),
+ DL, MVT::i8));
+ NewMul = DAG.getNode(ISD::ADD, DL, VT, NewMul, N->getOperand(0));
+ NewMul = DAG.getNode(ISD::ADD, DL, VT, NewMul, N->getOperand(0));
+ } else if (SignMulAmt >= 0 && isPowerOf2_64(AbsMulAmt + 2)) {
+ // (mul x, 2^N - 2) => (sub (sub (shl x, N), x), x)
+ NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
+ DAG.getConstant(Log2_64(AbsMulAmt + 2),
+ DL, MVT::i8));
+ NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
+ NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0));
}
}
Modified: llvm/trunk/test/CodeGen/X86/imul.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/imul.ll?rev=338101&r1=338100&r2=338101&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/imul.ll (original)
+++ llvm/trunk/test/CodeGen/X86/imul.ll Thu Jul 26 22:56:27 2018
@@ -499,8 +499,9 @@ entry:
define i64 @testOverflow(i64 %a) {
; X64-LABEL: testOverflow:
; X64: # %bb.0: # %entry
-; X64-NEXT: movabsq $9223372036854775807, %rax # imm = 0x7FFFFFFFFFFFFFFF
-; X64-NEXT: imulq %rdi, %rax
+; X64-NEXT: movq %rdi, %rax
+; X64-NEXT: shlq $63, %rax
+; X64-NEXT: subq %rdi, %rax
; X64-NEXT: retq
;
; X86-LABEL: testOverflow:
@@ -524,3 +525,27 @@ entry:
%tmp3 = mul i64 %a, 9223372036854775807
ret i64 %tmp3
}
+
+define i64 @testNegOverflow(i64 %a) {
+; X64-LABEL: testNegOverflow:
+; X64: # %bb.0: # %entry
+; X64-NEXT: movq %rdi, %rax
+; X64-NEXT: shlq $63, %rax
+; X64-NEXT: subq %rax, %rdi
+; X64-NEXT: movq %rdi, %rax
+; X64-NEXT: retq
+;
+; X86-LABEL: testNegOverflow:
+; X86: # %bb.0: # %entry
+; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
+; X86-NEXT: movl $1, %edx
+; X86-NEXT: movl %ecx, %eax
+; X86-NEXT: mull %edx
+; X86-NEXT: shll $31, %ecx
+; X86-NEXT: addl %ecx, %edx
+; X86-NEXT: addl {{[0-9]+}}(%esp), %edx
+; X86-NEXT: retl
+entry:
+ %tmp3 = mul i64 %a, -9223372036854775807
+ ret i64 %tmp3
+}
More information about the llvm-commits
mailing list