[AA] Move Depth member from AAResults to AAQI (NFC)
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 12 Feb 2021 14:41:22 +0000 (15:41 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 12 Feb 2021 20:42:36 +0000 (21:42 +0100)
Rather than storing the query depth in AAResults, store it in AAQI.
This makes more sense, as it is a property of the query. This
sidesteps the issue of D94363, fixing slightly inaccurate AA
statistics. Additionally, I plan to use the Depth from BasicAA in
the future, where fetching it from AAResults would be unreliable.

This change is not quite as straightforward as it seems, because
we need to preserve the depth when creating a new AAQI for recursive
queries across phis. I'm adding a new method for this, as we may
need to preserve additional information here in the future.

llvm/include/llvm/Analysis/AliasAnalysis.h
llvm/lib/Analysis/AliasAnalysis.cpp
llvm/lib/Analysis/BasicAliasAnalysis.cpp

index 9f74612..59963bc 100644 (file)
@@ -360,6 +360,9 @@ public:
   using IsCapturedCacheT = SmallDenseMap<const Value *, bool, 8>;
   IsCapturedCacheT IsCapturedCache;
 
+  /// Query depth used to distinguish recursive queries.
+  unsigned Depth = 0;
+
   /// How many active NoAlias assumption uses there are.
   int NumAssumptionUses = 0;
 
@@ -369,6 +372,15 @@ public:
   SmallVector<AAQueryInfo::LocPair, 4> AssumptionBasedResults;
 
   AAQueryInfo() : AliasCache(), IsCapturedCache() {}
+
+  /// Create a new AAQueryInfo based on this one, but with the cache cleared.
+  /// This is used for recursive queries across phis, where cache results may
+  /// not be valid.
+  AAQueryInfo withEmptyCache() {
+    AAQueryInfo NewAAQI;
+    NewAAQI.Depth = Depth;
+    return NewAAQI;
+  }
 };
 
 class BatchAAResults;
@@ -797,9 +809,6 @@ private:
 
   std::vector<AnalysisKey *> AADeps;
 
-  /// Query depth used to distinguish recursive queries.
-  unsigned Depth = 0;
-
   friend class BatchAAResults;
 };
 
index fae7a84..ebf7070 100644 (file)
@@ -118,15 +118,15 @@ AliasResult AAResults::alias(const MemoryLocation &LocA,
                              const MemoryLocation &LocB, AAQueryInfo &AAQI) {
   AliasResult Result = MayAlias;
 
-  Depth++;
+  AAQI.Depth++;
   for (const auto &AA : AAs) {
     Result = AA->alias(LocA, LocB, AAQI);
     if (Result != MayAlias)
       break;
   }
-  Depth--;
+  AAQI.Depth--;
 
-  if (Depth == 0) {
+  if (AAQI.Depth == 0) {
     if (Result == NoAlias)
       ++NumNoAlias;
     else if (Result == MustAlias)
index 97d0cb6..a3add55 100644 (file)
@@ -1456,7 +1456,7 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
   // If we inserted a block into VisitedPhiBBs, alias analysis results that
   // have been cached earlier may no longer be valid. Perform recursive queries
   // with a new AAQueryInfo.
-  AAQueryInfo NewAAQI;
+  AAQueryInfo NewAAQI = AAQI.withEmptyCache();
   AAQueryInfo *UseAAQI = BlockInserted ? &NewAAQI : &AAQI;
 
   AliasResult Alias = getBestAAResults().alias(