[PATCH] D133996: Add a cache for DL.getTypeAllocSize() to BasicAA.

Justin Lebar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 15 19:21:06 PDT 2022


jlebar updated this revision to Diff 460602.
jlebar added a comment.

Use structured binding for iterator.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133996/new/

https://reviews.llvm.org/D133996

Files:
  llvm/include/llvm/Analysis/BasicAliasAnalysis.h
  llvm/lib/Analysis/BasicAliasAnalysis.cpp


Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -508,6 +508,13 @@
   }
 };
 
+TypeSize BasicAAResult::GetTypeAllocSize(Type *Ty) {
+  auto [it, inserted] = TypeAllocSizeCache.insert({Ty, TypeSize::Fixed(0)});
+  if (inserted) {
+    it->second = DL.getTypeAllocSize(Ty);
+  }
+  return it->second;
+}
 
 /// If V is a symbolic pointer expression, decompose it into a base pointer
 /// with a constant offset and a number of scaled symbolic offsets.
@@ -527,6 +534,7 @@
   unsigned MaxIndexSize = DL.getMaxIndexSizeInBits();
   DecomposedGEP Decomposed;
   Decomposed.Offset = APInt(MaxIndexSize, 0);
+
   do {
     // See if this is a bitcast or GEP.
     const Operator *Op = dyn_cast<Operator>(V);
@@ -611,7 +619,7 @@
           continue;
 
         // Don't attempt to analyze GEPs if the scalable index is not zero.
-        TypeSize AllocTypeSize = DL.getTypeAllocSize(GTI.getIndexedType());
+        TypeSize AllocTypeSize = GetTypeAllocSize(GTI.getIndexedType());
         if (AllocTypeSize.isScalable()) {
           Decomposed.Base = V;
           return Decomposed;
@@ -622,7 +630,7 @@
         continue;
       }
 
-      TypeSize AllocTypeSize = DL.getTypeAllocSize(GTI.getIndexedType());
+      TypeSize AllocTypeSize = GetTypeAllocSize(GTI.getIndexedType());
       if (AllocTypeSize.isScalable()) {
         Decomposed.Base = V;
         return Decomposed;
Index: llvm/include/llvm/Analysis/BasicAliasAnalysis.h
===================================================================
--- llvm/include/llvm/Analysis/BasicAliasAnalysis.h
+++ llvm/include/llvm/Analysis/BasicAliasAnalysis.h
@@ -113,9 +113,15 @@
   /// Tracks instructions visited by pointsToConstantMemory.
   SmallPtrSet<const Value *, 16> Visited;
 
-  static DecomposedGEP
-  DecomposeGEPExpression(const Value *V, const DataLayout &DL,
-                         AssumptionCache *AC, DominatorTree *DT);
+  // Cache around DL.getTypeAllocSize(), which is surprisingly expensive.
+  DenseMap<const Type *, TypeSize> TypeAllocSizeCache;
+
+  // Returns DL.getTypeAllocSize() and inserts it into the cache if it's not
+  // present.
+  TypeSize GetTypeAllocSize(Type *Ty);
+
+  DecomposedGEP DecomposeGEPExpression(const Value *V, const DataLayout &DL,
+                                       AssumptionCache *AC, DominatorTree *DT);
 
   /// A Heuristic for aliasGEP that searches for a constant offset
   /// between the variables.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133996.460602.patch
Type: text/x-patch
Size: 2570 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220916/bf761b17/attachment-0001.bin>


More information about the llvm-commits mailing list