[PATCH] D59151: [BasicAA] Reduce no of map seaches [NFCI].

Alina Sbirlea via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 13:24:02 PST 2019


asbirlea created this revision.
asbirlea added reviewers: chandlerc, aschwaighofer.
Herald added subscribers: jlebar, sanjoy.
Herald added a project: LLVM.

This is a refactoring patch.

- Reduce the number of map searches by reusing the iterator.
- Add asserts to check that the entry is in the cache, as this is

something BasicAA relies on to avoid infinite recursion.


Repository:
  rL LLVM

https://reviews.llvm.org/D59151

Files:
  lib/Analysis/BasicAliasAnalysis.cpp


Index: lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- lib/Analysis/BasicAliasAnalysis.cpp
+++ lib/Analysis/BasicAliasAnalysis.cpp
@@ -1585,10 +1585,11 @@
       // that causes a MayAlias.
       // Pretend the phis do not alias.
       AliasResult Alias = NoAlias;
-      assert(AliasCache.count(Locs) &&
+      auto CacheIt = AliasCache.find(Locs);
+      assert((CacheIt != AliasCache.end()) &&
              "There must exist an entry for the phi node");
-      AliasResult OrigAliasResult = AliasCache[Locs];
-      AliasCache[Locs] = NoAlias;
+      AliasResult OrigAliasResult = CacheIt->second;
+      CacheIt->second = NoAlias;
 
       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
         AliasResult ThisAlias =
@@ -1601,9 +1602,11 @@
       }
 
       // Reset if speculation failed.
-      if (Alias != NoAlias)
-        AliasCache[Locs] = OrigAliasResult;
-
+      if (Alias != NoAlias) {
+        auto Pair = AliasCache.insert(std::make_pair(Locs, OrigAliasResult));
+        assert(!Pair.second && "Entry must have existed");
+        Pair.first->second = OrigAliasResult;
+      }
       return Alias;
     }
 
@@ -1824,8 +1827,11 @@
   if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
     AliasResult Result = aliasPHI(PN, V1Size, V1AAInfo,
                                   V2, V2Size, V2AAInfo, O2);
-    if (Result != MayAlias)
-      return AliasCache[Locs] = Result;
+    if (Result != MayAlias) {
+      Pair = AliasCache.insert(std::make_pair(Locs, Result));
+      assert(!Pair.second && "Entry must have existed");
+      return Pair.first->second = Result;
+    }
   }
 
   if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
@@ -1837,8 +1843,11 @@
   if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
     AliasResult Result =
         aliasSelect(S1, V1Size, V1AAInfo, V2, V2Size, V2AAInfo, O2);
-    if (Result != MayAlias)
-      return AliasCache[Locs] = Result;
+    if (Result != MayAlias) {
+      Pair = AliasCache.insert(std::make_pair(Locs, Result));
+      assert(!Pair.second && "Entry must have existed");
+      return Pair.first->second = Result;
+    }
   }
 
   // If both pointers are pointing into the same object and one of them
@@ -1846,14 +1855,19 @@
   if (O1 == O2)
     if (V1Size.isPrecise() && V2Size.isPrecise() &&
         (isObjectSize(O1, V1Size.getValue(), DL, TLI, NullIsValidLocation) ||
-         isObjectSize(O2, V2Size.getValue(), DL, TLI, NullIsValidLocation)))
-      return AliasCache[Locs] = PartialAlias;
+         isObjectSize(O2, V2Size.getValue(), DL, TLI, NullIsValidLocation))) {
+      Pair = AliasCache.insert(std::make_pair(Locs, PartialAlias));
+      assert(!Pair.second && "Entry must have existed");
+      return Pair.first->second = PartialAlias;
+    }
 
   // Recurse back into the best AA results we have, potentially with refined
   // memory locations. We have already ensured that BasicAA has a MayAlias
   // cache result for these, so any recursion back into BasicAA won't loop.
   AliasResult Result = getBestAAResults().alias(Locs.first, Locs.second);
-  return AliasCache[Locs] = Result;
+  Pair = AliasCache.insert(std::make_pair(Locs, Result));
+  assert(!Pair.second && "Entry must have existed");
+  return Pair.first->second = Result;
 }
 
 /// Check whether two Values can be considered equivalent.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59151.189914.patch
Type: text/x-patch
Size: 3391 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190308/5f6c4a7d/attachment.bin>


More information about the llvm-commits mailing list