[llvm] [HaxagonConstantPropagation] Use getSigned() (PR #176715)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 19 01:31:48 PST 2026


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/176715

The value is created via sign extension from a 32-bit value, so we should use getSigned().

>From 4d1c6d020bca5c4d4b62e396ba4f276dbe955947 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 19 Jan 2026 10:30:18 +0100
Subject: [PATCH] [HaxagonConstantPropagation] Use getSigned()

The value is created via sign extension from a 32-bit value, so
we should use getSigned().
---
 .../Hexagon/HexagonConstPropagation.cpp       |  3 +-
 llvm/test/CodeGen/Hexagon/constp-signed.ll    | 47 +++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/Hexagon/constp-signed.ll

diff --git a/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp b/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp
index 35da34ed0a899..1c887667f8858 100644
--- a/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonConstPropagation.cpp
@@ -2247,7 +2247,8 @@ bool HexagonConstEvaluator::evaluate(const RegSubRegPair &R,
     int32_t V32;
     memcpy(&V32, &U32, sizeof V32);
     IntegerType *Ty = Type::getInt32Ty(CX);
-    const ConstantInt *C32 = ConstantInt::get(Ty, static_cast<int64_t>(V32));
+    const ConstantInt *C32 =
+        ConstantInt::getSigned(Ty, static_cast<int64_t>(V32));
     Result.add(C32);
   }
   return true;
diff --git a/llvm/test/CodeGen/Hexagon/constp-signed.ll b/llvm/test/CodeGen/Hexagon/constp-signed.ll
new file mode 100644
index 0000000000000..f39359073e2a9
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/constp-signed.ll
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=hexagon-unknown-linux < %s | FileCheck %s
+
+; Make sure we do not assert on negative constants during constant
+; propagation.
+
+define i64 @test(i1 %tobool) {
+; CHECK-LABEL: test:
+; CHECK:         .cfi_startproc
+; CHECK-NEXT:  // %bb.0: // %entry
+; CHECK-NEXT:    {
+; CHECK-NEXT:     r3:2 = combine(#0,r0)
+; CHECK-NEXT:     r1:0 = combine(#-1,#-1)
+; CHECK-NEXT:    }
+; CHECK-NEXT:    {
+; CHECK-NEXT:     p0 = tstbit(r2,#0)
+; CHECK-NEXT:     r2 = #0
+; CHECK-NEXT:    }
+; CHECK-NEXT:    .p2align 4
+; CHECK-NEXT:  .LBB0_1: // %while.body
+; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
+; CHECK-NEXT:    {
+; CHECK-NEXT:     p1 = cmp.eq(r2,#0)
+; CHECK-NEXT:     r2 = #1
+; CHECK-NEXT:     if (!p1.new) r0 = add(r3,#0)
+; CHECK-NEXT:     if (!p1.new) r1 = add(r3,#0)
+; CHECK-NEXT:    }
+; CHECK-NEXT:    {
+; CHECK-NEXT:     if (p0) jump:nt .LBB0_1
+; CHECK-NEXT:    }
+; CHECK-NEXT:  // %bb.2: // %while.end
+; CHECK-NEXT:    {
+; CHECK-NEXT:     jumpr r31
+; CHECK-NEXT:    }
+entry:
+  br label %while.body
+
+while.body:
+  %sel.iv = phi i64 [ %sel, %while.body ], [ -1, %entry ]
+  %phi = phi i32 [ 1, %while.body ], [ 0, %entry ]
+  %cmp = icmp eq i32 %phi, 0
+  %sel = select i1 %cmp, i64 %sel.iv, i64 0
+  br i1 %tobool, label %while.body, label %while.end
+
+while.end:
+  ret i64 %sel
+}



More information about the llvm-commits mailing list