Skip to content

Commit 386918b

Browse files
committed
[BasicAA] Fix handling of indirect assumption based results
If a result is potentially based on a not yet proven assumption, BasicAA will remember it inside AssumptionBasedResults and remove the cache entry it an assumption higher up is later disproven. However, we currently miss the case where another cache entry ends up depending on such an AssumptionBased result. Fix this by introducing an additional AssumptionBased state for cache entries. If such a result is used, we'll still increment AAQI.NumAssumptionUses, which means that the using entry will also become AssumptionBased and be cleared if the assumption is disproven. At the end of the root query, convert remaining AssumptionBased results into definitive results. Fixes #98978.
1 parent a8fbbbd commit 386918b

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

llvm/include/llvm/Analysis/AliasAnalysis.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,23 @@ class AAQueryInfo {
244244
public:
245245
using LocPair = std::pair<AACacheLoc, AACacheLoc>;
246246
struct CacheEntry {
247+
/// Cache entry is neither an assumption nor does it use a (non-definitive)
248+
/// assumption.
249+
static constexpr int Definitive = -2;
250+
/// Cache entry is not an assumption itself, but may be using an assumption
251+
/// from higher up the stack.
252+
static constexpr int AssumptionBased = -1;
253+
247254
AliasResult Result;
248-
/// Number of times a NoAlias assumption has been used.
249-
/// 0 for assumptions that have not been used, -1 for definitive results.
255+
/// Number of times a NoAlias assumption has been used, 0 for assumptions
256+
/// that have not been used. Can also take one of the Definitive or
257+
/// AssumptionBased values documented above.
250258
int NumAssumptionUses;
259+
251260
/// Whether this is a definitive (non-assumption) result.
252-
bool isDefinitive() const { return NumAssumptionUses < 0; }
261+
bool isDefinitive() const { return NumAssumptionUses == Definitive; }
262+
/// Whether this is an assumption that has not been proven yet.
263+
bool isAssumption() const { return NumAssumptionUses >= 0; }
253264
};
254265

255266
// Alias analysis result aggregration using which this query is performed.

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,9 +1692,12 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
16921692
if (!Pair.second) {
16931693
auto &Entry = Pair.first->second;
16941694
if (!Entry.isDefinitive()) {
1695-
// Remember that we used an assumption.
1696-
++Entry.NumAssumptionUses;
1695+
// Remember that we used an assumption. This may either be a direct use
1696+
// of an assumption, or a use of an entry that may itself be based on an
1697+
// assumption.
16971698
++AAQI.NumAssumptionUses;
1699+
if (Entry.isAssumption())
1700+
++Entry.NumAssumptionUses;
16981701
}
16991702
// Cache contains sorted {V1,V2} pairs but we should return original order.
17001703
auto Result = Entry.Result;
@@ -1722,7 +1725,6 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
17221725
Entry.Result = Result;
17231726
// Cache contains sorted {V1,V2} pairs.
17241727
Entry.Result.swap(Swapped);
1725-
Entry.NumAssumptionUses = -1;
17261728

17271729
// If the assumption has been disproven, remove any results that may have
17281730
// been based on this assumption. Do this after the Entry updates above to
@@ -1734,8 +1736,26 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
17341736
// The result may still be based on assumptions higher up in the chain.
17351737
// Remember it, so it can be purged from the cache later.
17361738
if (OrigNumAssumptionUses != AAQI.NumAssumptionUses &&
1737-
Result != AliasResult::MayAlias)
1739+
Result != AliasResult::MayAlias) {
17381740
AAQI.AssumptionBasedResults.push_back(Locs);
1741+
Entry.NumAssumptionUses = AAQueryInfo::CacheEntry::AssumptionBased;
1742+
} else {
1743+
Entry.NumAssumptionUses = AAQueryInfo::CacheEntry::Definitive;
1744+
}
1745+
1746+
// Depth is incremented before this function is called, so Depth==1 indicates
1747+
// a root query.
1748+
if (AAQI.Depth == 1) {
1749+
// Any remaining assumption based results must be based on proven
1750+
// assumptions, so convert them to definitive results.
1751+
for (const auto &Loc : AAQI.AssumptionBasedResults) {
1752+
auto It = AAQI.AliasCache.find(Loc);
1753+
if (It != AAQI.AliasCache.end())
1754+
It->second.NumAssumptionUses = AAQueryInfo::CacheEntry::Definitive;
1755+
}
1756+
AAQI.AssumptionBasedResults.clear();
1757+
AAQI.NumAssumptionUses = 0;
1758+
}
17391759
return Result;
17401760
}
17411761

llvm/test/Transforms/SLPVectorizer/X86/pr98978.ll

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,40 @@
33

44
target triple = "x86_64-redhat-linux-gnu"
55

6+
; Should not get vectorized.
67
define void @test(ptr %p1, i64 %arg1, i64 %arg2) {
78
; CHECK-LABEL: define void @test(
89
; CHECK-SAME: ptr [[P1:%.*]], i64 [[ARG1:%.*]], i64 [[ARG2:%.*]]) {
9-
; CHECK-NEXT: [[ENTRY:.*]]:
10-
; CHECK-NEXT: br label %[[LOOP1:.*]]
11-
; CHECK: [[LOOP1]]:
12-
; CHECK-NEXT: [[I:%.*]] = phi ptr [ [[I21:%.*]], %[[BB20:.*]] ], [ [[P1]], %[[ENTRY]] ]
10+
; CHECK-NEXT: [[_PREHEADER48_PREHEADER_1:.*]]:
11+
; CHECK-NEXT: br label %[[_LOOPEXIT49_1:.*]]
12+
; CHECK: [[_LOOPEXIT49_1]]:
13+
; CHECK-NEXT: [[I:%.*]] = phi ptr [ [[I21:%.*]], %[[BB20:.*]] ], [ [[P1]], %[[_PREHEADER48_PREHEADER_1]] ]
1314
; CHECK-NEXT: br i1 false, label %[[BB22:.*]], label %[[DOTPREHEADER48_PREHEADER_1:.*]]
1415
; CHECK: [[DEAD:.*]]:
1516
; CHECK-NEXT: br label %[[DOTPREHEADER48_PREHEADER_1]]
16-
; CHECK: [[_PREHEADER48_PREHEADER_1:.*:]]
17-
; CHECK-NEXT: [[I5:%.*]] = phi ptr [ [[I]], %[[DEAD]] ], [ [[I]], %[[LOOP1]] ]
17+
; CHECK: [[_PREHEADER48_PREHEADER_2:.*:]]
18+
; CHECK-NEXT: [[I5:%.*]] = phi ptr [ [[I]], %[[DEAD]] ], [ [[I]], %[[_LOOPEXIT49_1]] ]
1819
; CHECK-NEXT: br label %[[DOTLOOPEXIT49_1:.*]]
1920
; CHECK: [[DEAD1:.*]]:
2021
; CHECK-NEXT: br i1 false, label %[[DOTLOOPEXIT49_1]], label %[[BB20]]
21-
; CHECK: [[_LOOPEXIT49_1:.*:]]
22+
; CHECK: [[_LOOPEXIT49_2:.*:]]
2223
; CHECK-NEXT: [[I6:%.*]] = phi ptr [ [[I5]], %[[DEAD1]] ], [ [[I5]], %[[DOTPREHEADER48_PREHEADER_1]] ]
2324
; CHECK-NEXT: [[I7:%.*]] = getelementptr i8, ptr [[I6]], i64 [[ARG1]]
2425
; CHECK-NEXT: br label %[[BB10:.*]]
2526
; CHECK: [[DEAD2:.*]]:
2627
; CHECK-NEXT: br label %[[BB10]]
2728
; CHECK: [[BB10]]:
2829
; CHECK-NEXT: [[I11:%.*]] = phi ptr [ [[I7]], %[[DOTLOOPEXIT49_1]] ], [ null, %[[DEAD2]] ]
29-
; CHECK-NEXT: [[TMP0:%.*]] = load <2 x i64>, ptr [[I11]], align 1
30-
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <2 x i64> [[TMP0]], <2 x i64> poison, <2 x i32> <i32 1, i32 0>
31-
; CHECK-NEXT: store <2 x i64> [[TMP1]], ptr [[I6]], align 1
30+
; CHECK-NEXT: [[I16:%.*]] = getelementptr i8, ptr [[I11]], i64 8
31+
; CHECK-NEXT: [[I17:%.*]] = load i64, ptr [[I16]], align 1
32+
; CHECK-NEXT: store i64 [[I17]], ptr [[I6]], align 1
33+
; CHECK-NEXT: [[I18:%.*]] = getelementptr i8, ptr [[I6]], i64 8
34+
; CHECK-NEXT: [[I19:%.*]] = load i64, ptr [[I11]], align 1
35+
; CHECK-NEXT: store i64 [[I19]], ptr [[I18]], align 1
3236
; CHECK-NEXT: br label %[[BB20]]
3337
; CHECK: [[BB20]]:
3438
; CHECK-NEXT: [[I21]] = phi ptr [ [[I5]], %[[DEAD1]] ], [ [[I6]], %[[BB10]] ]
35-
; CHECK-NEXT: br label %[[LOOP1]]
39+
; CHECK-NEXT: br label %[[_LOOPEXIT49_1]]
3640
; CHECK: [[BB22]]:
3741
; CHECK-NEXT: [[I23:%.*]] = getelementptr i8, ptr [[I]], i64 [[ARG2]]
3842
; CHECK-NEXT: [[I25:%.*]] = getelementptr i8, ptr [[I23]], i64 8

0 commit comments

Comments
 (0)