Skip to content

Commit efbb4d0

Browse files
committed
[BasicAA] Include MayBeCrossIteration in cache key
Rather than switching to a new AAQI instance with empty cache when MayBeCrossIteration is toggled, include the value in the cache key. The implementation redundantly include the information in both sides of the pair, but that seems simpler than trying to store it only on one side. Differential Revision: https://reviews.llvm.org/D136175
1 parent f2bd7ac commit efbb4d0

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

llvm/include/llvm/Analysis/AliasAnalysis.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -189,24 +189,30 @@ class EarliestEscapeInfo final : public CaptureInfo {
189189
void removeInstruction(Instruction *I);
190190
};
191191

192-
/// Reduced version of MemoryLocation that only stores a pointer and size.
193-
/// Used for caching AATags independent BasicAA results.
192+
/// Cache key for BasicAA results. It only includes the pointer and size from
193+
/// MemoryLocation, as BasicAA is AATags independent. Additionally, it includes
194+
/// the value of MayBeCrossIteration, which may affect BasicAA results.
194195
struct AACacheLoc {
195-
const Value *Ptr;
196+
using PtrTy = PointerIntPair<const Value *, 1, bool>;
197+
PtrTy Ptr;
196198
LocationSize Size;
199+
200+
AACacheLoc(PtrTy Ptr, LocationSize Size) : Ptr(Ptr), Size(Size) {}
201+
AACacheLoc(const Value *Ptr, LocationSize Size, bool MayBeCrossIteration)
202+
: Ptr(Ptr, MayBeCrossIteration), Size(Size) {}
197203
};
198204

199205
template <> struct DenseMapInfo<AACacheLoc> {
200206
static inline AACacheLoc getEmptyKey() {
201-
return {DenseMapInfo<const Value *>::getEmptyKey(),
207+
return {DenseMapInfo<AACacheLoc::PtrTy>::getEmptyKey(),
202208
DenseMapInfo<LocationSize>::getEmptyKey()};
203209
}
204210
static inline AACacheLoc getTombstoneKey() {
205-
return {DenseMapInfo<const Value *>::getTombstoneKey(),
211+
return {DenseMapInfo<AACacheLoc::PtrTy>::getTombstoneKey(),
206212
DenseMapInfo<LocationSize>::getTombstoneKey()};
207213
}
208214
static unsigned getHashValue(const AACacheLoc &Val) {
209-
return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
215+
return DenseMapInfo<AACacheLoc::PtrTy>::getHashValue(Val.Ptr) ^
210216
DenseMapInfo<LocationSize>::getHashValue(Val.Size);
211217
}
212218
static bool isEqual(const AACacheLoc &LHS, const AACacheLoc &RHS) {
@@ -257,15 +263,6 @@ class AAQueryInfo {
257263
SmallVector<AAQueryInfo::LocPair, 4> AssumptionBasedResults;
258264

259265
AAQueryInfo(AAResults &AAR, CaptureInfo *CI) : AAR(AAR), CI(CI) {}
260-
261-
/// Create a new AAQueryInfo based on this one, but with the cache cleared.
262-
/// This is used for recursive queries across phis, where cache results may
263-
/// not be valid.
264-
AAQueryInfo withEmptyCache() {
265-
AAQueryInfo NewAAQI(AAR, CI);
266-
NewAAQI.Depth = Depth;
267-
return NewAAQI;
268-
}
269266
};
270267

271268
/// AAQueryInfo that uses SimpleCaptureInfo.

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,14 +1403,8 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
14031403
// different loop iterations.
14041404
SaveAndRestore<bool> SavedMayBeCrossIteration(MayBeCrossIteration, true);
14051405

1406-
// If we enabled the MayBeCrossIteration flag, alias analysis results that
1407-
// have been cached earlier may no longer be valid. Perform recursive queries
1408-
// with a new AAQueryInfo.
1409-
AAQueryInfo NewAAQI = AAQI.withEmptyCache();
1410-
AAQueryInfo *UseAAQI = !SavedMayBeCrossIteration.get() ? &NewAAQI : &AAQI;
1411-
14121406
AliasResult Alias = AAQI.AAR.alias(MemoryLocation(V1Srcs[0], PNSize),
1413-
MemoryLocation(V2, V2Size), *UseAAQI);
1407+
MemoryLocation(V2, V2Size), AAQI);
14141408

14151409
// Early exit if the check of the first PHI source against V2 is MayAlias.
14161410
// Other results are not possible.
@@ -1427,7 +1421,7 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
14271421
Value *V = V1Srcs[i];
14281422

14291423
AliasResult ThisAlias = AAQI.AAR.alias(
1430-
MemoryLocation(V, PNSize), MemoryLocation(V2, V2Size), *UseAAQI);
1424+
MemoryLocation(V, PNSize), MemoryLocation(V2, V2Size), AAQI);
14311425
Alias = MergeAliasResults(ThisAlias, Alias);
14321426
if (Alias == AliasResult::MayAlias)
14331427
break;
@@ -1544,8 +1538,11 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
15441538
return AliasResult::MayAlias;
15451539

15461540
// Check the cache before climbing up use-def chains. This also terminates
1547-
// otherwise infinitely recursive queries.
1548-
AAQueryInfo::LocPair Locs({V1, V1Size}, {V2, V2Size});
1541+
// otherwise infinitely recursive queries. Include MayBeCrossIteration in the
1542+
// cache key, because some cases where MayBeCrossIteration==false returns
1543+
// MustAlias or NoAlias may become MayAlias under MayBeCrossIteration==true.
1544+
AAQueryInfo::LocPair Locs({V1, V1Size, MayBeCrossIteration},
1545+
{V2, V2Size, MayBeCrossIteration});
15491546
const bool Swapped = V1 > V2;
15501547
if (Swapped)
15511548
std::swap(Locs.first, Locs.second);

0 commit comments

Comments
 (0)