Skip to content

Commit 37d7299

Browse files
committed
[SCEV] Track and invalidate ValuesAtScopes users
ValuesAtScopes maps a SCEV and a Loop to another SCEV. While we invalidate entries if the left-hand SCEV is invalidated, we currently don't do this for the right-hand SCEV. Fix this by tracking users in a reverse map and using it for invalidation. This is conceptually the same change as D114738, but using the reverse map to avoid performance issues. Differential Revision: https://reviews.llvm.org/D114788
1 parent c737d4d commit 37d7299

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,11 @@ class ScalarEvolution {
14921492
DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>>
14931493
ValuesAtScopes;
14941494

1495+
/// Reverse map for invalidation purposes: Stores of which SCEV and which
1496+
/// loop this is the value-at-scope of.
1497+
DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>>
1498+
ValuesAtScopesUsers;
1499+
14951500
/// Memoized computeLoopDisposition results.
14961501
DenseMap<const SCEV *,
14971502
SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>>

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7607,6 +7607,7 @@ void ScalarEvolution::forgetAllLoops() {
76077607
ConstantEvolutionLoopExitValue.clear();
76087608
ValueExprMap.clear();
76097609
ValuesAtScopes.clear();
7610+
ValuesAtScopesUsers.clear();
76107611
LoopDispositions.clear();
76117612
BlockDispositions.clear();
76127613
UnsignedRanges.clear();
@@ -8847,6 +8848,9 @@ const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
88478848
LS.second = C;
88488849
break;
88498850
}
8851+
8852+
if (!isa<SCEVConstant>(C))
8853+
ValuesAtScopesUsers[C].push_back({L, V});
88508854
return C;
88518855
}
88528856

@@ -12465,6 +12469,7 @@ ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
1246512469
ConstantEvolutionLoopExitValue(
1246612470
std::move(Arg.ConstantEvolutionLoopExitValue)),
1246712471
ValuesAtScopes(std::move(Arg.ValuesAtScopes)),
12472+
ValuesAtScopesUsers(std::move(Arg.ValuesAtScopesUsers)),
1246812473
LoopDispositions(std::move(Arg.LoopDispositions)),
1246912474
LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)),
1247012475
BlockDispositions(std::move(Arg.BlockDispositions)),
@@ -12919,7 +12924,6 @@ void ScalarEvolution::forgetMemoizedResults(ArrayRef<const SCEV *> SCEVs) {
1291912924
}
1292012925

1292112926
void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) {
12922-
ValuesAtScopes.erase(S);
1292312927
LoopDispositions.erase(S);
1292412928
BlockDispositions.erase(S);
1292512929
UnsignedRanges.erase(S);
@@ -12938,6 +12942,22 @@ void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) {
1293812942
}
1293912943
ExprValueMap.erase(ExprIt);
1294012944
}
12945+
12946+
auto ScopeIt = ValuesAtScopes.find(S);
12947+
if (ScopeIt != ValuesAtScopes.end()) {
12948+
for (const auto &Pair : ScopeIt->second)
12949+
if (!isa_and_nonnull<SCEVConstant>(Pair.second))
12950+
erase_value(ValuesAtScopesUsers[Pair.second],
12951+
std::make_pair(Pair.first, S));
12952+
ValuesAtScopes.erase(ScopeIt);
12953+
}
12954+
12955+
auto ScopeUserIt = ValuesAtScopesUsers.find(S);
12956+
if (ScopeUserIt != ValuesAtScopesUsers.end()) {
12957+
for (const auto &Pair : ScopeUserIt->second)
12958+
erase_value(ValuesAtScopes[Pair.second], std::make_pair(Pair.first, S));
12959+
ValuesAtScopesUsers.erase(ScopeUserIt);
12960+
}
1294112961
}
1294212962

1294312963
void
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes='loop-mssa(indvars,indvars)' -S < %s | FileCheck %s
3+
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
@c = external global i16, align 1
7+
@a = external global i16, align 1
8+
9+
; When we delete %c.promoted, we need to ensure we clear the getSCEVAtScope
10+
; cache or we'll crash trying to access loop disposition of the exit value
11+
; for the remaining IV.
12+
define void @f() {
13+
; CHECK-LABEL: @f(
14+
; CHECK-NEXT: entry:
15+
; CHECK-NEXT: br label [[FOR_COND:%.*]]
16+
; CHECK: for.cond:
17+
; CHECK-NEXT: br i1 false, label [[FOR_BODY:%.*]], label [[FOR_END4:%.*]]
18+
; CHECK: for.body:
19+
; CHECK-NEXT: br label [[FOR_BODY2:%.*]]
20+
; CHECK: for.body2:
21+
; CHECK-NEXT: [[INC2:%.*]] = phi i16 [ undef, [[FOR_BODY]] ], [ [[INC:%.*]], [[FOR_BODY2]] ]
22+
; CHECK-NEXT: [[INC]] = add nsw i16 [[INC2]], 1
23+
; CHECK-NEXT: store i16 [[INC]], i16* undef, align 1
24+
; CHECK-NEXT: br i1 true, label [[FOR_BODY2]], label [[CRIT_EDGE:%.*]]
25+
; CHECK: crit_edge:
26+
; CHECK-NEXT: [[INC_LCSSA:%.*]] = phi i16 [ [[INC]], [[FOR_BODY2]] ]
27+
; CHECK-NEXT: store i16 [[INC_LCSSA]], i16* @a, align 1
28+
; CHECK-NEXT: unreachable
29+
; CHECK: for.end4:
30+
; CHECK-NEXT: ret void
31+
;
32+
entry:
33+
br label %for.cond
34+
35+
for.cond: ; preds = %entry
36+
br i1 false, label %for.body, label %for.end4
37+
38+
for.body: ; preds = %for.cond
39+
%c.promoted = load i16, i16* @c, align 1
40+
br label %for.body2
41+
42+
for.body2: ; preds = %for.body2, %for.body
43+
%inc33 = phi i16 [ %c.promoted, %for.body ], [ %inc3, %for.body2 ]
44+
%inc2 = phi i16 [ undef, %for.body ], [ %inc, %for.body2 ]
45+
%inc = add nsw i16 %inc2, 1
46+
store i16 %inc, i16* undef, align 1
47+
%inc3 = add nsw i16 %inc33, 1
48+
%tobool = icmp ne i16 %inc3, 0
49+
br i1 %tobool, label %for.body2, label %crit_edge
50+
51+
crit_edge: ; preds = %for.body2
52+
%inc.lcssa = phi i16 [ %inc, %for.body2 ]
53+
store i16 %inc.lcssa, i16* @a, align 1
54+
unreachable
55+
56+
for.end4: ; preds = %for.cond
57+
ret void
58+
}

0 commit comments

Comments
 (0)