Skip to content

Commit aeeb9b5

Browse files
authored
[AVR] Fix codegen after getConstant assertions got enabled (#152269)
This fixes #152097 This commit fixes two instances of a (somewhat) recently enabled assertion. One with a test, the other I can't reproduce (might be dead code) but certainly looks like an instance of the same problem. The PR that introduced the regression: #117558 With this patch, the AVR backend is usable again for TinyGo.
1 parent 6cd6de5 commit aeeb9b5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

llvm/lib/Target/AVR/AVRISelLowering.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
669669
default: {
670670
// Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows
671671
// us to fold the constant into the cmp instruction.
672-
RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
672+
RHS = DAG.getSignedConstant(C->getSExtValue() + 1, DL, VT);
673673
CC = ISD::SETGE;
674674
break;
675675
}
@@ -713,7 +713,10 @@ SDValue AVRTargetLowering::getAVRCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
713713
// Turn lhs < rhs with lhs constant into rhs >= lhs+1, this allows us to
714714
// fold the constant into the cmp instruction.
715715
if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) {
716-
RHS = DAG.getConstant(C->getSExtValue() + 1, DL, VT);
716+
// Doing a "icmp ugt i16 65535, %0" comparison should have been converted
717+
// already to something else. Assert to make sure this assumption holds.
718+
assert((!C->isAllOnes()) && "integer overflow in comparison transform");
719+
RHS = DAG.getConstant(C->getZExtValue() + 1, DL, VT);
717720
CC = ISD::SETUGE;
718721
break;
719722
}

llvm/test/CodeGen/AVR/cmp.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,18 @@ define i16 @cmp_i16_gt_1023(i16 %0) {
298298
%3 = zext i1 %2 to i16
299299
ret i16 %3
300300
}
301+
302+
define void @cmp_issue152097(i16 %a) addrspace(1) {
303+
; See: https://github.com/llvm/llvm-project/issues/152097
304+
; CHECK-LABEL: cmp_issue152097
305+
; CHECK: ldi r18, -1
306+
; CHECK-NEXT: cpi r24, -2
307+
; CHECK-NEXT: cpc r25, r18
308+
; CHECK-NEXT: ret
309+
%cmp = icmp ugt i16 -2, %a
310+
br i1 %cmp, label %if.then, label %if.else
311+
if.then:
312+
ret void
313+
if.else:
314+
ret void
315+
}

0 commit comments

Comments
 (0)