[llvm] [AVR] Fix SETUGT during 128b -> 64b lowering (PR #182690)
Patryk Wychowaniec via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 21 10:29:32 PST 2026
https://github.com/Patryk27 created https://github.com/llvm/llvm-project/pull/182690
We've got an optimization that turns `icmp ugt %x, <const>` into `icmp uge %x, <const+1>`, because the latter better matches how comparisons work in AVR (note that the comment there is wrong, though):
https://github.com/llvm/llvm-project/blob/a67bf7d796df75592a26a377420696e3c7b7993f/llvm/lib/Target/AVR/AVRISelLowering.cpp#L716
Our current implementation assumes that we never get told to do a "dumb" isel like this:
```llvm
%0 = icmp ugt i16 65535, %x ; always false
```
... because this would've been already optimized out - turns our that's not always true, we might be told do select such an instruction during i128->i64 lowering:
```llvm
%0 = icmp ugt i128 %x, 18446744073709551615 ; 2^64-1
```
... in which case the current code unnecessarily crashes.
This pull request changes the approach so that we apply the operator-swapping optimization only if it's applicable instead of panicking when it's not.
Closes https://github.com/llvm/llvm-project/issues/181504.
>From f72ab20115ff9a639f16916518bf2376f5a3ec0d Mon Sep 17 00:00:00 2001
From: Patryk Wychowaniec <pwychowaniec at pm.me>
Date: Sat, 21 Feb 2026 18:43:41 +0100
Subject: [PATCH] [AVR] Fix SETUGT during 128b -> 64b lowering
---
llvm/lib/Target/AVR/AVRISelLowering.cpp | 18 ++++++++++--------
llvm/test/CodeGen/AVR/cmp.ll | 18 ++++++++++++++++++
2 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Target/AVR/AVRISelLowering.cpp b/llvm/lib/Target/AVR/AVRISelLowering.cpp
index 5fbb14650d757..6cf6e090fabe9 100644
--- a/llvm/lib/Target/AVR/AVRISelLowering.cpp
+++ b/llvm/lib/Target/AVR/AVRISelLowering.cpp
@@ -714,15 +714,17 @@ SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
break;
}
case ISD::SETUGT: {
- // Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
- // fold the constant into the cmp instruction.
+ // Turn `lhs > rhs` with constant rhs into `lhs >= rhs + 1`, because this
+ // allows us to fold the constant into the cmp instruction.
if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
- // Doing a "icmp ugt i16 65535, %0" comparison should have been converted
- // already to something else. Assert to make sure this assumption holds.
- assert((!C->isAllOnes()) && "integer overflow in comparison transform");
- RHS = DAG.getConstant(C->getZExtValue() + 1, DL, VT);
- CC = ISD::SETUGE;
- break;
+ if (C->getConstantIntValue()->isMaxValue(false)) {
+ // Applying this optimization requires calculating rhs+1, which we can't
+ // do if that overflows; it can happen during i128->i64 lowering.
+ } else {
+ RHS = DAG.getConstant(C->getZExtValue() + 1, DL, VT);
+ CC = ISD::SETUGE;
+ break;
+ }
}
// Swap operands and reverse the branching condition.
std::swap(LHS, RHS);
diff --git a/llvm/test/CodeGen/AVR/cmp.ll b/llvm/test/CodeGen/AVR/cmp.ll
index c932bda1807f1..cdb062cee17c8 100644
--- a/llvm/test/CodeGen/AVR/cmp.ll
+++ b/llvm/test/CodeGen/AVR/cmp.ll
@@ -313,3 +313,21 @@ if.then:
if.else:
ret void
}
+
+define i1 @cmp_issue181504(i128 %0) {
+; See: https://github.com/llvm/llvm-project/issues/181504
+;
+; This generates too much code to reasonably assert, so this test exists mostly
+; to make sure we don't crash.
+;
+; CHECK-LABEL: cmp_issue181504
+; CHECK: ldi r30, -1
+; CHECK-NEXT: ldi r26, 255
+; CHECK-NEXT: ldi r27, 255
+; CHECK-NEXT: ldi r31, 1
+; CHECK-NEXT: cp r30, r10
+bb0:
+ %1 = icmp ugt i128 %0, 18446744073709551615 ; 2^64-1
+
+ ret i1 %1
+}
More information about the llvm-commits
mailing list