Skip to content

Commit d5c8931

Browse files
committed
!fixup also generate umax if the value may be poison.
1 parent 5469289 commit d5c8931

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,15 @@ Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
684684
const SCEV *RHSExpr = S->getRHS();
685685
Value *RHS = expand(RHSExpr);
686686
if (SafeUDivMode) {
687-
if (!ScalarEvolution::isGuaranteedNotToBePoison(RHSExpr))
687+
bool GuaranteedNotPoison =
688+
ScalarEvolution::isGuaranteedNotToBePoison(RHSExpr);
689+
if (!GuaranteedNotPoison)
688690
RHS = Builder.CreateFreeze(RHS);
689-
if (!SE.isKnownNonZero(RHSExpr))
691+
692+
// We need an umax if either RHSExpr is not known to be zero, or if it is
693+
// not guaranteed to be non-poison. In the later case, the frozen poison may
694+
// be 0.
695+
if (!SE.isKnownNonZero(RHSExpr) || !GuaranteedNotPoison)
690696
RHS = Builder.CreateIntrinsic(RHS->getType(), Intrinsic::umax,
691697
{RHS, ConstantInt::get(RHS->getType(), 1)});
692698
}

llvm/test/Transforms/LoopVectorize/trip-count-expansion-may-introduce-ub.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,8 @@ define i64 @multi_exit_count_with_udiv_by_value_in_latch_different_bounds_diviso
12711271
; CHECK-NEXT: entry:
12721272
; CHECK-NEXT: [[M_1:%.*]] = call i64 @llvm.umax.i64(i64 [[M]], i64 1)
12731273
; CHECK-NEXT: [[TMP9:%.*]] = freeze i64 [[M_1]]
1274-
; CHECK-NEXT: [[TMP0:%.*]] = udiv i64 42, [[TMP9]]
1274+
; CHECK-NEXT: [[TMP10:%.*]] = call i64 @llvm.umax.i64(i64 [[TMP9]], i64 1)
1275+
; CHECK-NEXT: [[TMP0:%.*]] = udiv i64 42, [[TMP10]]
12751276
; CHECK-NEXT: [[TMP1:%.*]] = freeze i64 [[TMP0]]
12761277
; CHECK-NEXT: [[SMAX:%.*]] = call i64 @llvm.smax.i64(i64 [[N]], i64 0)
12771278
; CHECK-NEXT: [[UMIN:%.*]] = call i64 @llvm.umin.i64(i64 [[TMP1]], i64 [[SMAX]])

0 commit comments

Comments
 (0)