20
20
#include " llvm/Analysis/BlockFrequencyInfo.h"
21
21
#include " llvm/Analysis/CodeMetrics.h"
22
22
#include " llvm/Analysis/ConstantFolding.h"
23
+ #include " llvm/Analysis/EphemeralValuesCache.h"
23
24
#include " llvm/Analysis/InstructionSimplify.h"
24
25
#include " llvm/Analysis/LoopInfo.h"
25
26
#include " llvm/Analysis/MemoryBuiltins.h"
@@ -269,6 +270,9 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
269
270
// / easily cacheable. Instead, use the cover function paramHasAttr.
270
271
CallBase &CandidateCall;
271
272
273
+ // / Getter for the cache of ephemeral values.
274
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache = nullptr ;
275
+
272
276
// / Extension points for handling callsite features.
273
277
// Called before a basic block was analyzed.
274
278
virtual void onBlockStart (const BasicBlock *BB) {}
@@ -462,7 +466,7 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
462
466
463
467
// Custom analysis routines.
464
468
InlineResult analyzeBlock (BasicBlock *BB,
465
- SmallPtrSetImpl<const Value *> &EphValues);
469
+ const SmallPtrSetImpl<const Value *> &EphValues);
466
470
467
471
// Disable several entry points to the visitor so we don't accidentally use
468
472
// them by declaring but not defining them here.
@@ -510,10 +514,12 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
510
514
function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr ,
511
515
function_ref<const TargetLibraryInfo &(Function &)> GetTLI = nullptr ,
512
516
ProfileSummaryInfo *PSI = nullptr ,
513
- OptimizationRemarkEmitter *ORE = nullptr )
517
+ OptimizationRemarkEmitter *ORE = nullptr ,
518
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache =
519
+ nullptr )
514
520
: TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI),
515
521
GetTLI (GetTLI), PSI(PSI), F(Callee), DL(F.getDataLayout()), ORE(ORE),
516
- CandidateCall(Call) {}
522
+ CandidateCall(Call), GetEphValuesCache(GetEphValuesCache) {}
517
523
518
524
InlineResult analyze ();
519
525
@@ -1126,9 +1132,11 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
1126
1132
function_ref<const TargetLibraryInfo &(Function &)> GetTLI = nullptr ,
1127
1133
ProfileSummaryInfo *PSI = nullptr ,
1128
1134
OptimizationRemarkEmitter *ORE = nullptr , bool BoostIndirect = true ,
1129
- bool IgnoreThreshold = false )
1135
+ bool IgnoreThreshold = false ,
1136
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache =
1137
+ nullptr )
1130
1138
: CallAnalyzer(Callee, Call, TTI, GetAssumptionCache, GetBFI, GetTLI, PSI,
1131
- ORE),
1139
+ ORE, GetEphValuesCache ),
1132
1140
ComputeFullInlineCost (OptComputeFullInlineCost ||
1133
1141
Params.ComputeFullInlineCost || ORE ||
1134
1142
isCostBenefitAnalysisEnabled ()),
@@ -2566,7 +2574,7 @@ bool CallAnalyzer::visitInstruction(Instruction &I) {
2566
2574
// / viable, and true if inlining remains viable.
2567
2575
InlineResult
2568
2576
CallAnalyzer::analyzeBlock (BasicBlock *BB,
2569
- SmallPtrSetImpl<const Value *> &EphValues) {
2577
+ const SmallPtrSetImpl<const Value *> &EphValues) {
2570
2578
for (Instruction &I : *BB) {
2571
2579
// FIXME: Currently, the number of instructions in a function regardless of
2572
2580
// our ability to simplify them during inline to constants or dead code,
@@ -2781,11 +2789,15 @@ InlineResult CallAnalyzer::analyze() {
2781
2789
NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size ();
2782
2790
NumAllocaArgs = SROAArgValues.size ();
2783
2791
2784
- // FIXME: If a caller has multiple calls to a callee, we end up recomputing
2785
- // the ephemeral values multiple times (and they're completely determined by
2786
- // the callee, so this is purely duplicate work).
2787
- SmallPtrSet<const Value *, 32 > EphValues;
2788
- CodeMetrics::collectEphemeralValues (&F, &GetAssumptionCache (F), EphValues);
2792
+ // Collecting the ephemeral values of `F` can be expensive, so use the
2793
+ // ephemeral values cache if available.
2794
+ SmallPtrSet<const Value *, 32 > EphValuesStorage;
2795
+ const SmallPtrSetImpl<const Value *> *EphValues = &EphValuesStorage;
2796
+ if (GetEphValuesCache)
2797
+ EphValues = &GetEphValuesCache (F).ephValues ();
2798
+ else
2799
+ CodeMetrics::collectEphemeralValues (&F, &GetAssumptionCache (F),
2800
+ EphValuesStorage);
2789
2801
2790
2802
// The worklist of live basic blocks in the callee *after* inlining. We avoid
2791
2803
// adding basic blocks of the callee which can be proven to be dead for this
@@ -2824,7 +2836,7 @@ InlineResult CallAnalyzer::analyze() {
2824
2836
2825
2837
// Analyze the cost of this block. If we blow through the threshold, this
2826
2838
// returns false, and we can bail on out.
2827
- InlineResult IR = analyzeBlock (BB, EphValues);
2839
+ InlineResult IR = analyzeBlock (BB, * EphValues);
2828
2840
if (!IR.isSuccess ())
2829
2841
return IR;
2830
2842
@@ -2967,9 +2979,11 @@ InlineCost llvm::getInlineCost(
2967
2979
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2968
2980
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
2969
2981
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
2970
- ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
2982
+ ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE,
2983
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache) {
2971
2984
return getInlineCost (Call, Call.getCalledFunction (), Params, CalleeTTI,
2972
- GetAssumptionCache, GetTLI, GetBFI, PSI, ORE);
2985
+ GetAssumptionCache, GetTLI, GetBFI, PSI, ORE,
2986
+ GetEphValuesCache);
2973
2987
}
2974
2988
2975
2989
std::optional<int > llvm::getInliningCostEstimate (
@@ -3089,7 +3103,8 @@ InlineCost llvm::getInlineCost(
3089
3103
function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
3090
3104
function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
3091
3105
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
3092
- ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
3106
+ ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE,
3107
+ function_ref<EphemeralValuesCache &(Function &)> GetEphValuesCache) {
3093
3108
3094
3109
auto UserDecision =
3095
3110
llvm::getAttributeBasedInliningDecision (Call, Callee, CalleeTTI, GetTLI);
@@ -3105,7 +3120,9 @@ InlineCost llvm::getInlineCost(
3105
3120
<< " )\n " );
3106
3121
3107
3122
InlineCostCallAnalyzer CA (*Callee, Call, Params, CalleeTTI,
3108
- GetAssumptionCache, GetBFI, GetTLI, PSI, ORE);
3123
+ GetAssumptionCache, GetBFI, GetTLI, PSI, ORE,
3124
+ /* BoostIndirect=*/ true , /* IgnoreThreshold=*/ false ,
3125
+ GetEphValuesCache);
3109
3126
InlineResult ShouldInline = CA.analyze ();
3110
3127
3111
3128
LLVM_DEBUG (CA.dump ());
0 commit comments