[llvm] [RISCV] Make rv64 i64 -> i32 trunc free (PR #230681)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 9 14:49:11 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: Ramkumar Ramachandra (artagnon)
<details>
<summary>Changes</summary>
This removes a FIXME. isTruncateFree only has three users: LoopVectorize, LoopStrengthReduce, and ScalarEvolution. LoopVectorize has been picked to write a test to exercise the change.
Assisted-by: llm
---
Full diff: https://github.com/llvm/llvm-project/pull/230681.diff
2 Files Affected:
- (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+1-3)
- (added) llvm/test/Transforms/LoopVectorize/RISCV/induction-trunc.ll (+43)
``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index efafed10d6f1e..e6a9be976f3d1 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -2630,10 +2630,8 @@ bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
// On RV32, 64-bit integers are split into their high and low parts and held
// in two different registers, so the trunc is free since the low register can
// just be used.
-// FIXME: Should we consider i64->i32 free on RV64 to match the EVT version of
-// isTruncateFree?
bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
- if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
+ if (!SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
return false;
unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
unsigned DestBits = DstTy->getPrimitiveSizeInBits();
diff --git a/llvm/test/Transforms/LoopVectorize/RISCV/induction-trunc.ll b/llvm/test/Transforms/LoopVectorize/RISCV/induction-trunc.ll
new file mode 100644
index 0000000000000..0034917f4c9bd
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/RISCV/induction-trunc.ll
@@ -0,0 +1,43 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
+; RUN: opt -mtriple=riscv64-linux-gnu -mattr=+v -p loop-vectorize -force-vector-width=1 -force-vector-interleave=2 -S %s | FileCheck %s
+
+define void @induction_trunc_free(ptr %dst) {
+; CHECK-LABEL: define void @induction_trunc_free(
+; CHECK-SAME: ptr [[DST:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = add nuw i64 1, [[INDEX]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[TMP0]], 1
+; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP0]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = trunc i64 [[TMP1]] to i32
+; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[DST]], i64 [[TMP0]]
+; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i32, ptr [[DST]], i64 [[TMP1]]
+; CHECK-NEXT: store i32 [[TMP2]], ptr [[TMP4]], align 4
+; CHECK-NEXT: store i32 [[TMP3]], ptr [[TMP5]], align 4
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
+; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1024
+; CHECK-NEXT: br i1 [[TMP6]], label %[[SCALAR_PH:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK: [[SCALAR_PH]]:
+; CHECK-NEXT: br label %[[LOOP:.*]]
+; CHECK: [[LOOP]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i64 [ 1, %entry ], [ %iv.next, %loop ]
+ %free.trunc = trunc i64 %iv to i32
+ %gep.dst = getelementptr inbounds i32, ptr %dst, i64 %iv
+ store i32 %free.trunc, ptr %gep.dst
+ %iv.next = add nuw nsw i64 %iv, 1
+ %ec = icmp eq i64 %iv.next, 1025
+ br i1 %ec, label %end, label %loop
+
+end:
+ ret void
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/230681
More information about the llvm-commits
mailing list