[llvm-branch-commits] [llvm] release/23.x: [Hexagon] Use unsigned compare for unsigned hwloop distance guard (#236824) (PR #245135)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 22 21:26:46 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-hexagon

Author: llvmbot

<details>
<summary>Changes</summary>

Backport e9a4a50160d489074ed44e8aec86009ba95b6109

Requested by: @<!-- -->quic-k

---
Full diff: https://github.com/llvm/llvm-project/pull/245135.diff


3 Files Affected:

- (modified) llvm/lib/Target/Hexagon/HexagonHardwareLoops.cpp (+4-1) 
- (modified) llvm/test/CodeGen/Hexagon/hwloop-dist-check.mir (+1-1) 
- (added) llvm/test/CodeGen/Hexagon/hwloop-unsigned-dist-guard.ll (+27) 


``````````diff
diff --git a/llvm/lib/Target/Hexagon/HexagonHardwareLoops.cpp b/llvm/lib/Target/Hexagon/HexagonHardwareLoops.cpp
index 2d92b2ce41352f..419723c41827ec 100644
--- a/llvm/lib/Target/Hexagon/HexagonHardwareLoops.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonHardwareLoops.cpp
@@ -1007,7 +1007,10 @@ CountValue *HexagonHardwareLoops::computeCount(MachineLoop *Loop,
     //   DistCheck = CMP_GT DistR,  0   --> CmpLess
     //   DistCheck = CMP_GT DistR, -1   --> CmpGreater
     Register DistCheckR = MRI->createVirtualRegister(PredRC);
-    const MCInstrDesc &DistCheckD = TII->get(Hexagon::C2_cmpgti);
+    unsigned DistCheckOpc = (CmpLess && Comparison::isUnsigned(Cmp))
+                                ? Hexagon::C2_cmpgtui
+                                : Hexagon::C2_cmpgti;
+    const MCInstrDesc &DistCheckD = TII->get(DistCheckOpc);
     BuildMI(*PH, InsertPos, DL, DistCheckD, DistCheckR)
         .addReg(DistR, {}, DistSR)
         .addImm((CmpLess) ? 0 : -1);
diff --git a/llvm/test/CodeGen/Hexagon/hwloop-dist-check.mir b/llvm/test/CodeGen/Hexagon/hwloop-dist-check.mir
index 29ea52e491621a..b5b395294f42d0 100644
--- a/llvm/test/CodeGen/Hexagon/hwloop-dist-check.mir
+++ b/llvm/test/CodeGen/Hexagon/hwloop-dist-check.mir
@@ -1,7 +1,7 @@
 # RUN: llc --mtriple=hexagon -run-pass=hwloops %s -o - | FileCheck %s
 
 # CHECK-LABEL: name: f
-# CHECK: [[R1:%[0-9]+]]:predregs = C2_cmpgti [[R0:%[0-9]+]], 0
+# CHECK: [[R1:%[0-9]+]]:predregs = C2_cmpgtui [[R0:%[0-9]+]], 0
 # CHECK: [[R3:%[0-9]+]]:intregs = C2_muxir [[R1:%[0-9]+]], [[R2:%[0-9]+]], 1
 # CHECK-LABEL: name: g
 # CHECK: [[R1:%[0-9]+]]:predregs = C2_cmpgti [[R0:%[0-9]+]], 0
diff --git a/llvm/test/CodeGen/Hexagon/hwloop-unsigned-dist-guard.ll b/llvm/test/CodeGen/Hexagon/hwloop-unsigned-dist-guard.ll
new file mode 100644
index 00000000000000..12dc96dfef9b2a
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/hwloop-unsigned-dist-guard.ll
@@ -0,0 +1,27 @@
+; RUN: llc -O1 -mtriple=hexagon < %s | FileCheck %s
+
+; A counted unsigned less-than loop whose start is a register value gets a
+; distance guard. The guard must compare the distance as unsigned, otherwise a
+; distance with the sign bit set is treated as negative and the trip count is
+; clamped to 1.
+
+; CHECK-LABEL: f:
+; CHECK:       p0 = cmp.gtu(r{{[0-9]+}},#0)
+; CHECK:       loop0(
+; CHECK-NOT:   cmp.gt(r
+
+define void @f(ptr %p, i32 %start, i32 %n) {
+entry:
+  br label %loop
+
+loop:
+  %i = phi i32 [ %start, %entry ], [ %next, %loop ]
+  %addr = getelementptr i8, ptr %p, i32 %i
+  store i8 0, ptr %addr
+  %next = add i32 %i, 1
+  %cond = icmp ult i32 %next, %n
+  br i1 %cond, label %loop, label %exit
+
+exit:
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/245135


More information about the llvm-branch-commits mailing list