Skip to content

Commit 40a647f

Browse files
authored
[IndVarSimplify] Drop samesign flags after narrowing compares (#116263)
Samesign flag cannot be preserved after narrowing the compare since the position of the sign bit is changed. Closes #116249.
1 parent c1f6cb7 commit 40a647f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,8 @@ bool IndVarSimplify::canonicalizeExitCondition(Loop *L) {
15301530
L->getLoopPreheader()->getTerminator()->getIterator());
15311531
ICmp->setOperand(Swapped ? 1 : 0, LHSOp);
15321532
ICmp->setOperand(Swapped ? 0 : 1, NewRHS);
1533+
// Samesign flag cannot be preserved after narrowing the compare.
1534+
ICmp->setSameSign(false);
15331535
if (LHS->use_empty())
15341536
DeadInsts.push_back(LHS);
15351537
};

llvm/test/Transforms/IndVarSimplify/finite-exit-comparisons.ll

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,3 +1052,43 @@ for.end: ; preds = %for.body, %entry
10521052
ret void
10531053
}
10541054

1055+
define i8 @test_drop_icmp_samesign(i1 %cond, i32 range(i32 0, 32) %x) {
1056+
; CHECK-LABEL: @test_drop_icmp_samesign(
1057+
; CHECK-NEXT: entry:
1058+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[FOR_BODY_PREHEADER:%.*]], label [[ELSE:%.*]]
1059+
; CHECK: for.body.preheader:
1060+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[X:%.*]] to i8
1061+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
1062+
; CHECK: else:
1063+
; CHECK-NEXT: [[CALL1:%.*]] = call i8 @callee()
1064+
; CHECK-NEXT: br label [[EXIT:%.*]]
1065+
; CHECK: for.body:
1066+
; CHECK-NEXT: [[CALL2:%.*]] = call i8 @callee()
1067+
; CHECK-NEXT: [[COND2:%.*]] = icmp ugt i8 [[TMP0]], [[CALL2]]
1068+
; CHECK-NEXT: br i1 [[COND2]], label [[FOR_BODY]], label [[EXIT_LOOPEXIT:%.*]]
1069+
; CHECK: exit.loopexit:
1070+
; CHECK-NEXT: [[CALL2_LCSSA:%.*]] = phi i8 [ [[CALL2]], [[FOR_BODY]] ]
1071+
; CHECK-NEXT: br label [[EXIT]]
1072+
; CHECK: exit:
1073+
; CHECK-NEXT: [[RES:%.*]] = phi i8 [ [[CALL1]], [[ELSE]] ], [ [[CALL2_LCSSA]], [[EXIT_LOOPEXIT]] ]
1074+
; CHECK-NEXT: ret i8 [[RES]]
1075+
;
1076+
entry:
1077+
br i1 %cond, label %for.body, label %else
1078+
1079+
else:
1080+
%call1 = call i8 @callee()
1081+
br label %exit
1082+
1083+
for.body:
1084+
%call2 = call i8 @callee()
1085+
%ext = zext i8 %call2 to i32
1086+
%cond2 = icmp samesign ugt i32 %x, %ext
1087+
br i1 %cond2, label %for.body, label %exit
1088+
1089+
exit:
1090+
%res = phi i8 [ %call1, %else ], [ %call2, %for.body ]
1091+
ret i8 %res
1092+
}
1093+
1094+
declare i8 @callee()

0 commit comments

Comments
 (0)