[llvm] [RISCV] Disable constant hoisting for mul by one more/less than a pow… (PR #67385)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 17:26:45 PDT 2023


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/67385

…er of 2.

We can use a shift+add/sub for these. This often has same or lower latency than a multiply and may have more execution resources available.

>From 0e9f606a62f053969dd3a23c003f767eca88b210 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Mon, 25 Sep 2023 17:24:08 -0700
Subject: [PATCH] [RISCV] Disable constant hoisting for mul by one more/less
 than a power of 2.

We can use a shift+add/sub for these. This often has same or lower latency
than a multiply and may have more execution resources available.
---
 .../Target/RISCV/RISCVTargetTransformInfo.cpp |  3 +++
 .../ConstantHoisting/RISCV/immediates.ll      | 24 ++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 4a34362a792f150..1f069cb9eb514e4 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -127,6 +127,9 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
     // Power of 2 is a shift. Negated power of 2 is a shift and a negate.
     if (Imm.isPowerOf2() || Imm.isNegatedPowerOf2())
       return TTI::TCC_Free;
+    // One more or less than a power of 2 can use SLLI+ADD/SUB.
+    if ((Imm + 1).isPowerOf2() || (Imm - 1).isPowerOf2())
+      return TTI::TCC_Free;
     // FIXME: There is no MULI instruction.
     Takes12BitImm = true;
     break;
diff --git a/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll b/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll
index 4cf028d05780870..131ef673a61f829 100644
--- a/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll
+++ b/llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll
@@ -22,9 +22,9 @@ define i64 @test2(i64 %a) nounwind {
 ; Check that we hoist immediates with large values.
 define i64 @test3(i64 %a) nounwind {
 ; CHECK-LABEL: test3
-; CHECK: %const = bitcast i64 32767 to i64
-  %1 = mul i64 %a, 32767
-  %2 = add i64 %1, 32767
+; CHECK: %const = bitcast i64 32766 to i64
+  %1 = mul i64 %a, 32766
+  %2 = add i64 %1, 32766
   ret i64 %2
 }
 
@@ -132,3 +132,21 @@ define i64 @test14(i64 %a) nounwind {
   ret i64 %2
 }
 
+; Check that we don't hoist mul by one less than a power of 2.
+define i64 @test15(i64 %a) nounwind {
+; CHECK-LABEL: test15
+; CHECK: mul i64 %a, 65535
+  %1 = mul i64 %a, 65535
+  %2 = mul i64 %1, 65535
+  ret i64 %2
+}
+
+; Check that we don't hoist mul by one more than a power of 2.
+define i64 @test16(i64 %a) nounwind {
+; CHECK-LABEL: test16
+; CHECK: mul i64 %a, 65537
+  %1 = mul i64 %a, 65537
+  %2 = mul i64 %1, 65537
+  ret i64 %2
+}
+



More information about the llvm-commits mailing list