diff --git a/llvm/include/llvm/Analysis/BasicAliasAnalysis.h b/llvm/include/llvm/Analysis/BasicAliasAnalysis.h --- a/llvm/include/llvm/Analysis/BasicAliasAnalysis.h +++ b/llvm/include/llvm/Analysis/BasicAliasAnalysis.h @@ -113,9 +113,15 @@ /// Tracks instructions visited by pointsToConstantMemory. SmallPtrSet Visited; - static DecomposedGEP - DecomposeGEPExpression(const Value *V, const DataLayout &DL, - AssumptionCache *AC, DominatorTree *DT); + // Cache around DL.getTypeAllocSize(), which is surprisingly expensive. + DenseMap 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. diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/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(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;