38
38
#include " llvm/ADT/Statistic.h"
39
39
#include " llvm/ADT/StringRef.h"
40
40
#include " llvm/Analysis/AliasAnalysis.h"
41
+ #include " llvm/Analysis/AssumptionCache.h"
41
42
#include " llvm/Analysis/CaptureTracking.h"
43
+ #include " llvm/Analysis/CodeMetrics.h"
42
44
#include " llvm/Analysis/GlobalsModRef.h"
43
45
#include " llvm/Analysis/LoopInfo.h"
44
46
#include " llvm/Analysis/MemoryBuiltins.h"
@@ -762,6 +764,9 @@ struct DSEState {
762
764
// Post-order numbers for each basic block. Used to figure out if memory
763
765
// accesses are executed before another access.
764
766
DenseMap<BasicBlock *, unsigned > PostOrderNumbers;
767
+ // Values that are only used with assumes. Used to refine pointer escape
768
+ // analysis.
769
+ SmallPtrSet<const Value *, 32 > EphValues;
765
770
766
771
// / Keep track of instructions (partly) overlapping with killing MemoryDefs per
767
772
// / basic block.
@@ -776,8 +781,8 @@ struct DSEState {
776
781
DSEState &operator =(const DSEState &) = delete ;
777
782
778
783
DSEState (Function &F, AliasAnalysis &AA, MemorySSA &MSSA, DominatorTree &DT,
779
- PostDominatorTree &PDT, const TargetLibraryInfo &TLI ,
780
- const LoopInfo &LI)
784
+ PostDominatorTree &PDT, AssumptionCache &AC ,
785
+ const TargetLibraryInfo &TLI, const LoopInfo &LI)
781
786
: F(F), AA(AA), EI(DT, LI), BatchAA(AA, &EI), MSSA(MSSA), DT(DT),
782
787
PDT (PDT), TLI(TLI), DL(F.getParent()->getDataLayout()), LI(LI) {
783
788
// Collect blocks with throwing instructions not modeled in MemorySSA and
@@ -809,6 +814,8 @@ struct DSEState {
809
814
AnyUnreachableExit = any_of (PDT.roots (), [](const BasicBlock *E) {
810
815
return isa<UnreachableInst>(E->getTerminator ());
811
816
});
817
+
818
+ CodeMetrics::collectEphemeralValues (&F, &AC, EphValues);
812
819
}
813
820
814
821
// / Return 'OW_Complete' if a store to the 'KillingLoc' location (by \p
@@ -955,7 +962,7 @@ struct DSEState {
955
962
if (!isInvisibleToCallerOnUnwind (V)) {
956
963
I.first ->second = false ;
957
964
} else if (isNoAliasCall (V)) {
958
- I.first ->second = !PointerMayBeCaptured (V, true , false );
965
+ I.first ->second = !PointerMayBeCaptured (V, true , false , EphValues );
959
966
}
960
967
}
961
968
return I.first ->second ;
@@ -974,7 +981,7 @@ struct DSEState {
974
981
// with the killing MemoryDef. But we refrain from doing so for now to
975
982
// limit compile-time and this does not cause any changes to the number
976
983
// of stores removed on a large test set in practice.
977
- I.first ->second = PointerMayBeCaptured (V, false , true );
984
+ I.first ->second = PointerMayBeCaptured (V, false , true , EphValues );
978
985
return !I.first ->second ;
979
986
}
980
987
@@ -1929,12 +1936,13 @@ struct DSEState {
1929
1936
1930
1937
static bool eliminateDeadStores (Function &F, AliasAnalysis &AA, MemorySSA &MSSA,
1931
1938
DominatorTree &DT, PostDominatorTree &PDT,
1939
+ AssumptionCache &AC,
1932
1940
const TargetLibraryInfo &TLI,
1933
1941
const LoopInfo &LI) {
1934
1942
bool MadeChange = false ;
1935
1943
1936
1944
MSSA.ensureOptimizedUses ();
1937
- DSEState State (F, AA, MSSA, DT, PDT, TLI, LI);
1945
+ DSEState State (F, AA, MSSA, DT, PDT, AC, TLI, LI);
1938
1946
// For each store:
1939
1947
for (unsigned I = 0 ; I < State.MemDefs .size (); I++) {
1940
1948
MemoryDef *KillingDef = State.MemDefs [I];
@@ -2114,9 +2122,10 @@ PreservedAnalyses DSEPass::run(Function &F, FunctionAnalysisManager &AM) {
2114
2122
DominatorTree &DT = AM.getResult <DominatorTreeAnalysis>(F);
2115
2123
MemorySSA &MSSA = AM.getResult <MemorySSAAnalysis>(F).getMSSA ();
2116
2124
PostDominatorTree &PDT = AM.getResult <PostDominatorTreeAnalysis>(F);
2125
+ AssumptionCache &AC = AM.getResult <AssumptionAnalysis>(F);
2117
2126
LoopInfo &LI = AM.getResult <LoopAnalysis>(F);
2118
2127
2119
- bool Changed = eliminateDeadStores (F, AA, MSSA, DT, PDT, TLI, LI);
2128
+ bool Changed = eliminateDeadStores (F, AA, MSSA, DT, PDT, AC, TLI, LI);
2120
2129
2121
2130
#ifdef LLVM_ENABLE_STATS
2122
2131
if (AreStatisticsEnabled ())
@@ -2156,9 +2165,11 @@ class DSELegacyPass : public FunctionPass {
2156
2165
MemorySSA &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA ();
2157
2166
PostDominatorTree &PDT =
2158
2167
getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree ();
2168
+ AssumptionCache &AC =
2169
+ getAnalysis<AssumptionCacheTracker>().getAssumptionCache (F);
2159
2170
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo ();
2160
2171
2161
- bool Changed = eliminateDeadStores (F, AA, MSSA, DT, PDT, TLI, LI);
2172
+ bool Changed = eliminateDeadStores (F, AA, MSSA, DT, PDT, AC, TLI, LI);
2162
2173
2163
2174
#ifdef LLVM_ENABLE_STATS
2164
2175
if (AreStatisticsEnabled ())
@@ -2182,6 +2193,7 @@ class DSELegacyPass : public FunctionPass {
2182
2193
AU.addPreserved <MemorySSAWrapperPass>();
2183
2194
AU.addRequired <LoopInfoWrapperPass>();
2184
2195
AU.addPreserved <LoopInfoWrapperPass>();
2196
+ AU.addRequired <AssumptionCacheTracker>();
2185
2197
}
2186
2198
};
2187
2199
@@ -2199,6 +2211,7 @@ INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
2199
2211
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
2200
2212
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2201
2213
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
2214
+ INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
2202
2215
INITIALIZE_PASS_END(DSELegacyPass, " dse" , " Dead Store Elimination" , false ,
2203
2216
false )
2204
2217
0 commit comments