From f1de1adc82c69894fcc8178c689269215c6f2710 Mon Sep 17 00:00:00 2001 From: Chad Rosier Date: Tue, 3 Mar 2015 17:31:01 +0000 Subject: [PATCH] [AArch64] When combining constant mul of -3, prefer (sub x, (shl x, N)). This change only effects codegen when the constant is -3. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231085 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/AArch64/AArch64ISelLowering.cpp | 18 +++++++++--------- test/CodeGen/AArch64/mul_pow2.ll | 3 +-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/Target/AArch64/AArch64ISelLowering.cpp b/lib/Target/AArch64/AArch64ISelLowering.cpp index d96518135e8..acc89fb8f71 100644 --- a/lib/Target/AArch64/AArch64ISelLowering.cpp +++ b/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -6895,6 +6895,15 @@ static SDValue performMulCombine(SDNode *N, SelectionDAG &DAG, N->getOperand(0)); } } else { + // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) + APInt VNP1 = -Value + 1; + if (VNP1.isPowerOf2()) { + SDValue ShiftedVal = + DAG.getNode(ISD::SHL, SDLoc(N), VT, N->getOperand(0), + DAG.getConstant(VNP1.logBase2(), MVT::i64)); + return DAG.getNode(ISD::SUB, SDLoc(N), VT, N->getOperand(0), + ShiftedVal); + } // (mul x, -(2^N + 1)) => - (add (shl x, N), x) APInt VNM1 = -Value - 1; if (VNM1.isPowerOf2()) { @@ -6905,15 +6914,6 @@ static SDValue performMulCombine(SDNode *N, SelectionDAG &DAG, DAG.getNode(ISD::ADD, SDLoc(N), VT, ShiftedVal, N->getOperand(0)); return DAG.getNode(ISD::SUB, SDLoc(N), VT, DAG.getConstant(0, VT), Add); } - // (mul x, -(2^N - 1)) => (sub x, (shl x, N)) - APInt VNP1 = -Value + 1; - if (VNP1.isPowerOf2()) { - SDValue ShiftedVal = - DAG.getNode(ISD::SHL, SDLoc(N), VT, N->getOperand(0), - DAG.getConstant(VNP1.logBase2(), MVT::i64)); - return DAG.getNode(ISD::SUB, SDLoc(N), VT, N->getOperand(0), - ShiftedVal); - } } } return SDValue(); diff --git a/test/CodeGen/AArch64/mul_pow2.ll b/test/CodeGen/AArch64/mul_pow2.ll index efc0ec8c40e..b828223ef1c 100644 --- a/test/CodeGen/AArch64/mul_pow2.ll +++ b/test/CodeGen/AArch64/mul_pow2.ll @@ -74,8 +74,7 @@ define i32 @ntest2(i32 %x) { define i32 @ntest3(i32 %x) { ; CHECK-LABEL: ntest3 -; CHECK: add {{w[0-9]+}}, w0, w0, lsl #1 -; CHECK: neg w0, {{w[0-9]+}} +; CHECK: sub w0, w0, w0, lsl #2 %mul = mul nsw i32 %x, -3 ret i32 %mul -- 2.34.1