From 9b27d13204969bfb57ef408ec2c95e5b1f63fc43 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Wed, 27 Jul 2022 09:33:45 -0700 Subject: [PATCH] [RISCV] Disable constant hoisting for multiply by negated power of 2. A mul by a negated power of 2 is a slli followed by neg. This doesn't require any constant materialization and may be lower latency than mul. The neg may also be foldable into other arithmetic. Reviewed By: reames Differential Revision: https://reviews.llvm.org/D130047 --- llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp | 6 ++++++ llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp index f9cd5ff..cea2fb9 100644 --- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp +++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp @@ -71,7 +71,13 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx, case Instruction::Add: case Instruction::Or: case Instruction::Xor: + Takes12BitImm = true; + break; case Instruction::Mul: + // Negated power of 2 is a shift and a negate. + if (Imm.isNegatedPowerOf2()) + return TTI::TCC_Free; + // FIXME: There is no MULI instruction. Takes12BitImm = true; break; case Instruction::Sub: diff --git a/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll b/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll index 92441af..e1badcb 100644 --- a/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll +++ b/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll @@ -72,3 +72,12 @@ define i64 @test8(i64 %a) nounwind "target-features"="+zba" { %2 = and i64 %1, 4294967295 ret i64 %2 } + +; Check that we don't hoist mul with negated power of 2. +define i64 @test9(i64 %a) nounwind { +; CHECK-LABEL: test9 +; CHECK: mul i64 %a, -4294967296 + %1 = mul i64 %a, -4294967296 + %2 = mul i64 %1, -4294967296 + ret i64 %2 +} -- 2.7.4