Skip to content

[AVR] Fix codegen after getConstant assertions got enabled #152269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions llvm/lib/Target/AVR/AVRISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
default: {
// Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows
// us to fold the constant into the cmp instruction.
RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
RHS = DAG.getSignedConstant(C->getSExtValue() + 1, DL, VT);
CC = ISD::SETGE;
break;
}
Expand Down Expand Up @@ -713,7 +713,10 @@ SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
// Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
// fold the constant into the cmp instruction.
if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
// 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;
}
Expand Down
15 changes: 15 additions & 0 deletions llvm/test/CodeGen/AVR/cmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,18 @@ define i16 @cmp_i16_gt_1023(i16 %0) {
%3 = zext i1 %2 to i16
ret i16 %3
}

define void @cmp_issue152097(i16 %a) addrspace(1) {
; See: https://github.com/llvm/llvm-project/issues/152097
; CHECK-LABEL: cmp_issue152097
; CHECK: ldi r18, -1
; CHECK-NEXT: cpi r24, -2
; CHECK-NEXT: cpc r25, r18
; CHECK-NEXT: ret
%cmp = icmp ugt i16 -2, %a
br i1 %cmp, label %if.then, label %if.else
if.then:
ret void
if.else:
ret void
}