Skip to content

Commit 60889b6

Browse files
author
git apple-llvm automerger
committed
Merge commit 'e594b284810c' from llvm.org/release/19.x into stable/20240723
2 parents ce2adcd + e594b28 commit 60889b6

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,18 +1928,24 @@ Instruction *WidenIV::widenIVUse(WidenIV::NarrowIVDefUse DU,
19281928
if (!WideAddRec.first)
19291929
return nullptr;
19301930

1931-
// Reuse the IV increment that SCEVExpander created. Recompute flags, unless
1932-
// the flags for both increments agree and it is safe to use the ones from
1933-
// the original inc. In that case, the new use of the wide increment won't
1934-
// be more poisonous.
1935-
bool NeedToRecomputeFlags =
1936-
!SCEVExpander::canReuseFlagsFromOriginalIVInc(OrigPhi, WidePhi,
1937-
DU.NarrowUse, WideInc) ||
1938-
DU.NarrowUse->hasNoUnsignedWrap() != WideInc->hasNoUnsignedWrap() ||
1939-
DU.NarrowUse->hasNoSignedWrap() != WideInc->hasNoSignedWrap();
1931+
auto CanUseWideInc = [&]() {
1932+
if (!WideInc)
1933+
return false;
1934+
// Reuse the IV increment that SCEVExpander created. Recompute flags,
1935+
// unless the flags for both increments agree and it is safe to use the
1936+
// ones from the original inc. In that case, the new use of the wide
1937+
// increment won't be more poisonous.
1938+
bool NeedToRecomputeFlags =
1939+
!SCEVExpander::canReuseFlagsFromOriginalIVInc(
1940+
OrigPhi, WidePhi, DU.NarrowUse, WideInc) ||
1941+
DU.NarrowUse->hasNoUnsignedWrap() != WideInc->hasNoUnsignedWrap() ||
1942+
DU.NarrowUse->hasNoSignedWrap() != WideInc->hasNoSignedWrap();
1943+
return WideAddRec.first == WideIncExpr &&
1944+
Rewriter.hoistIVInc(WideInc, DU.NarrowUse, NeedToRecomputeFlags);
1945+
};
1946+
19401947
Instruction *WideUse = nullptr;
1941-
if (WideAddRec.first == WideIncExpr &&
1942-
Rewriter.hoistIVInc(WideInc, DU.NarrowUse, NeedToRecomputeFlags))
1948+
if (CanUseWideInc())
19431949
WideUse = WideInc;
19441950
else {
19451951
WideUse = cloneIVUser(DU, WideAddRec.first);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=indvars < %s | FileCheck %s
3+
4+
target datalayout = "n8:16:32:64"
5+
6+
; Make sure it does not crash.
7+
8+
define i32 @m() {
9+
; CHECK-LABEL: define i32 @m() {
10+
; CHECK-NEXT: [[ENTRY:.*:]]
11+
; CHECK-NEXT: br label %[[FOR_BODY_I6:.*]]
12+
; CHECK: [[FOR_BODY_I6]]:
13+
; CHECK-NEXT: br i1 true, label %[[I_EXIT:.*]], label %[[IF_END_I:.*]]
14+
; CHECK: [[IF_END_I]]:
15+
; CHECK-NEXT: store i64 0, ptr null, align 8
16+
; CHECK-NEXT: br label %[[FOR_BODY_I6]]
17+
; CHECK: [[I_EXIT]]:
18+
; CHECK-NEXT: ret i32 0
19+
;
20+
entry:
21+
%div.i4 = sdiv i32 1, 0
22+
br label %for.body.i6
23+
24+
for.body.i6: ; preds = %if.end.i, %entry
25+
%add57.i = phi i32 [ %add.i7, %if.end.i ], [ 0, %entry ]
26+
br i1 true, label %i.exit, label %if.end.i
27+
28+
if.end.i: ; preds = %for.body.i6
29+
%add.i7 = add i32 %add57.i, %div.i4
30+
%conv.i = zext i32 %add57.i to i64
31+
store i64 %conv.i, ptr null, align 8
32+
br label %for.body.i6
33+
34+
i.exit: ; preds = %for.body.i6
35+
ret i32 0
36+
}

0 commit comments

Comments
 (0)