diff options
author | Kirill Naumov <knaumov@azul.com> | 2020-06-11 20:24:14 +0000 |
---|---|---|
committer | Kirill Naumov <knaumov@azul.com> | 2020-06-11 22:29:03 +0000 |
commit | 1022b5eb5b37f7dc93ae36002de694541db0e0c1 (patch) | |
tree | 6db9189ba5f8a9a0dbd201a160ba8314891a2634 /llvm/lib/Analysis/InlineCost.cpp | |
parent | 030897523d43e3296f69d25a71a140d9e5793c6a (diff) | |
download | llvm-1022b5eb5b37f7dc93ae36002de694541db0e0c1.zip llvm-1022b5eb5b37f7dc93ae36002de694541db0e0c1.tar.gz llvm-1022b5eb5b37f7dc93ae36002de694541db0e0c1.tar.bz2 |
[InlineCost] Preparational patch for creation of Printer pass.
- Renaming the printer class, flag
- Refactoring
- Changing some tests
This patch is a preparational stage for introducing a new printing pass and new
functionality to the existing Annotation Writer. I plan to extend
this functionality for this tool to be more useful when looking at the inline
process.
Diffstat (limited to 'llvm/lib/Analysis/InlineCost.cpp')
-rw-r--r-- | llvm/lib/Analysis/InlineCost.cpp | 74 |
1 files changed, 43 insertions, 31 deletions
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp index 9eea2e9..ba327ca 100644 --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -54,9 +54,9 @@ static cl::opt<int> cl::ZeroOrMore, cl::desc("Default amount of inlining to perform")); -static cl::opt<bool> PrintDebugInstructionDeltas( - "print-instruction-deltas", cl::Hidden, cl::init(false), - cl::desc("Prints deltas of cost and threshold per instruction")); +static cl::opt<bool> PrintInstructionComments( + "print-instruction-comments", cl::Hidden, cl::init(false), + cl::desc("Prints comments for instruction based on inline cost analysis")); static cl::opt<int> InlineThreshold( "inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore, @@ -128,14 +128,14 @@ struct InstructionCostDetail { bool hasThresholdChanged() const { return ThresholdAfter != ThresholdBefore; } }; -class CostAnnotationWriter : public AssemblyAnnotationWriter { -public: - // This DenseMap stores the delta change in cost and threshold after - // accounting for the given instruction. - DenseMap<const Instruction *, InstructionCostDetail> CostThresholdMap; +class InlineCostAnnotationWriter : public AssemblyAnnotationWriter { +private: + InlineCostCallAnalyzer *const ICCA; +public: + InlineCostAnnotationWriter(InlineCostCallAnalyzer *ICCA) : ICCA(ICCA) {} virtual void emitInstructionAnnot(const Instruction *I, - formatted_raw_ostream &OS); + formatted_raw_ostream &OS) override; }; /// Carry out call site analysis, in order to evaluate inlinability. @@ -429,6 +429,11 @@ class InlineCostCallAnalyzer final : public CallAnalyzer { /// Tunable parameters that control the analysis. const InlineParams &Params; + // This DenseMap stores the delta change in cost and threshold after + // accounting for the given instruction. The map is filled only with the + // flag PrintInstructionComments on. + DenseMap<const Instruction *, InstructionCostDetail> InstructionCostDetailMap; + /// Upper bound for the inlining cost. Bonuses are being applied to account /// for speculative "expected profit" of the inlining decision. int Threshold = 0; @@ -598,19 +603,19 @@ class InlineCostCallAnalyzer final : public CallAnalyzer { void onInstructionAnalysisStart(const Instruction *I) override { // This function is called to store the initial cost of inlining before // the given instruction was assessed. - if (!PrintDebugInstructionDeltas) + if (!PrintInstructionComments) return; - Writer.CostThresholdMap[I].CostBefore = Cost; - Writer.CostThresholdMap[I].ThresholdBefore = Threshold; + InstructionCostDetailMap[I].CostBefore = Cost; + InstructionCostDetailMap[I].ThresholdBefore = Threshold; } void onInstructionAnalysisFinish(const Instruction *I) override { // This function is called to find new values of cost and threshold after // the instruction has been assessed. - if (!PrintDebugInstructionDeltas) + if (!PrintInstructionComments) return; - Writer.CostThresholdMap[I].CostAfter = Cost; - Writer.CostThresholdMap[I].ThresholdAfter = Threshold; + InstructionCostDetailMap[I].CostAfter = Cost; + InstructionCostDetailMap[I].ThresholdAfter = Threshold; } InlineResult finalizeAnalysis() override { @@ -713,13 +718,20 @@ public: ComputeFullInlineCost(OptComputeFullInlineCost || Params.ComputeFullInlineCost || ORE), Params(Params), Threshold(Params.DefaultThreshold), - BoostIndirectCalls(BoostIndirect), IgnoreThreshold(IgnoreThreshold) {} + BoostIndirectCalls(BoostIndirect), IgnoreThreshold(IgnoreThreshold), + Writer(this) {} - /// Annotation Writer for cost annotation - CostAnnotationWriter Writer; + /// Annotation Writer for instruction details + InlineCostAnnotationWriter Writer; void dump(); + Optional<InstructionCostDetail> getCostDetails(const Instruction *I) { + if (InstructionCostDetailMap.find(I) != InstructionCostDetailMap.end()) + return InstructionCostDetailMap[I]; + return None; + } + virtual ~InlineCostCallAnalyzer() {} int getThreshold() { return Threshold; } int getCost() { return Cost; } @@ -737,23 +749,23 @@ void CallAnalyzer::disableSROAForArg(AllocaInst *SROAArg) { disableLoadElimination(); } -void CostAnnotationWriter::emitInstructionAnnot(const Instruction *I, +void InlineCostAnnotationWriter::emitInstructionAnnot(const Instruction *I, formatted_raw_ostream &OS) { // The cost of inlining of the given instruction is printed always. // The threshold delta is printed only when it is non-zero. It happens // when we decided to give a bonus at a particular instruction. - if (CostThresholdMap.count(I) == 0) { - OS << "; No analysis for the instruction\n"; - return; + Optional<InstructionCostDetail> Record = ICCA->getCostDetails(I); + if (!Record) + OS << "; No analysis for the instruction"; + else { + OS << "; cost before = " << Record->CostBefore + << ", cost after = " << Record->CostAfter + << ", threshold before = " << Record->ThresholdBefore + << ", threshold after = " << Record->ThresholdAfter << ", "; + OS << "cost delta = " << Record->getCostDelta(); + if (Record->hasThresholdChanged()) + OS << ", threshold delta = " << Record->getThresholdDelta(); } - const auto &Record = CostThresholdMap[I]; - OS << "; cost before = " << Record.CostBefore - << ", cost after = " << Record.CostAfter - << ", threshold before = " << Record.ThresholdBefore - << ", threshold after = " << Record.ThresholdAfter << ", "; - OS << "cost delta = " << Record.getCostDelta(); - if (Record.hasThresholdChanged()) - OS << ", threshold delta = " << Record.getThresholdDelta(); OS << "\n"; } @@ -2159,7 +2171,7 @@ InlineResult CallAnalyzer::analyze() { /// Dump stats about this call's analysis. LLVM_DUMP_METHOD void InlineCostCallAnalyzer::dump() { #define DEBUG_PRINT_STAT(x) dbgs() << " " #x ": " << x << "\n" - if (PrintDebugInstructionDeltas) + if (PrintInstructionComments) F.print(dbgs(), &Writer); DEBUG_PRINT_STAT(NumConstantArgs); DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs); |