Index: llvm/lib/Analysis/InlineCost.cpp =================================================================== --- llvm/lib/Analysis/InlineCost.cpp +++ llvm/lib/Analysis/InlineCost.cpp @@ -27,6 +27,7 @@ #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/Config/llvm-config.h" +#include "llvm/IR/AssemblyAnnotationWriter.h" #include "llvm/IR/CallingConv.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Dominators.h" @@ -38,6 +39,7 @@ #include "llvm/IR/PatternMatch.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/FormattedStream.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -46,6 +48,14 @@ STATISTIC(NumCallsAnalyzed, "Number of call sites analyzed"); +static cl::opt EnableInstructionDetail("print-instruction-detail", + cl::Hidden, cl::init(false), + cl::desc("Prints information on cost and threshold per instruction")); + +static cl::opt EnableInstructionDelta("print-instruction-delta", + cl::Hidden, cl::init(false), + cl::desc("Prints deltas of cost and threshold per instruction")); + static cl::opt InlineThreshold( "inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore, cl::desc("Control the amount of inlining to perform (default = 225)")); @@ -94,6 +104,27 @@ namespace { class InlineCostCallAnalyzer; + +// This struct is used to store information about inlining an instruction +struct InliningCostDetail { + int CostBefore; + int CostAfter; + int CostDelta; + int ThresholdBefore; + int ThresholdAfter; + int ThresholdDelta; +}; + +class CostAnnotationWriter : public AssemblyAnnotationWriter { +public: + // This DenseMap stores the delta change in cost and threshold after + // accounting for the given instruction. + llvm::DenseMap CostThresholdMap; + + virtual void emitInstructionAnnot(const llvm::Instruction *I, + llvm::formatted_raw_ostream &OS); +}; + class CallAnalyzer : public InstVisitor { typedef InstVisitor Base; friend class InstVisitor; @@ -130,6 +161,12 @@ /// Called after a basic block was analyzed. virtual void onBlockAnalyzed(const BasicBlock *BB) {} + /// Called before an instruction was analyzed + virtual void onInstructionAnalysisStart(const Instruction *I) {} + + /// Called after an instruction was analyzed + virtual void onInstructionAnalysisFinish(const Instruction *I) {} + /// Called at the end of the analysis of the callsite. Return the outcome of /// the analysis, i.e. 'InlineResult(true)' if the inlining may happen, or /// the reason it can't. @@ -533,6 +570,30 @@ } } + void onInstructionAnalysisStart(const Instruction *I) override { + // This function is called to store the initial cost of inlining before + // the given instruction was assessed. + if (!EnableInstructionDetail && !EnableInstructionDelta) + return ; + Writer.CostThresholdMap[I].CostBefore = Cost; + Writer.CostThresholdMap[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 (!EnableInstructionDetail && !EnableInstructionDelta) + return ; + Writer.CostThresholdMap[I].CostAfter = Cost; + Writer.CostThresholdMap[I].ThresholdAfter = Threshold; + Writer.CostThresholdMap[I].CostDelta = + Writer.CostThresholdMap[I].CostAfter - + Writer.CostThresholdMap[I].CostBefore; + Writer.CostThresholdMap[I].ThresholdDelta = + Writer.CostThresholdMap[I].ThresholdAfter - + Writer.CostThresholdMap[I].ThresholdBefore; + } + InlineResult finalizeAnalysis() override { // Loops generally act a lot like calls in that they act like barriers to // movement, require a certain amount of setup, etc. So when optimising for @@ -632,6 +693,10 @@ Params.ComputeFullInlineCost || ORE), Params(Params), Threshold(Params.DefaultThreshold), BoostIndirectCalls(BoostIndirect) {} + + /// Annotation Writer for cost annotation + CostAnnotationWriter Writer; + void dump(); virtual ~InlineCostCallAnalyzer() {} @@ -650,6 +715,25 @@ EnabledSROAAllocas.erase(SROAArg); disableLoadElimination(); } + +void CostAnnotationWriter::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. + OS << "; "; + if (EnableInstructionDetail) + OS << "cost before = " << CostThresholdMap[I].CostBefore << + ", cost after = " << CostThresholdMap[I].CostAfter << + ", threshold before = " << CostThresholdMap[I].ThresholdBefore << + ", threshold after = " << CostThresholdMap[I].ThresholdAfter << + ", "; + OS << "cost delta = " << CostThresholdMap[I].CostDelta; + if (CostThresholdMap[I].ThresholdDelta) + OS << ", threshold delta = " << CostThresholdMap[I].ThresholdDelta; + OS << "\n"; +} + /// If 'V' maps to a SROA candidate, disable SROA for it. void CallAnalyzer::disableSROA(Value *V) { if (auto *SROAArg = getSROAArgForValueOrNull(V)) { @@ -1757,12 +1841,16 @@ // instruction. Visit the instructions using our InstVisitor to account for // all of the per-instruction logic. The visit tree returns true if we // consumed the instruction in any way, and false if the instruction's base - // cost should count against inlining. + // cost should count against inlining. The deltas are saved for the + // CostAnnotationWriter. + onInstructionAnalysisStart(&*I); + if (Base::visit(&*I)) ++NumInstructionsSimplified; else onCommonInstructionSimplification(); + onInstructionAnalysisFinish(&*I); using namespace ore; // If the visit this instruction detected an uninlinable pattern, abort. InlineResult IR = InlineResult::success(); @@ -2044,6 +2132,8 @@ /// Dump stats about this call's analysis. LLVM_DUMP_METHOD void InlineCostCallAnalyzer::dump() { #define DEBUG_PRINT_STAT(x) dbgs() << " " #x ": " << x << "\n" + if (EnableInstructionDetail || EnableInstructionDelta) + F.print(dbgs(), &Writer); DEBUG_PRINT_STAT(NumConstantArgs); DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs); DEBUG_PRINT_STAT(NumAllocaArgs); Index: llvm/test/DebugInfo/debuginline-cost-delta.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/debuginline-cost-delta.ll @@ -0,0 +1,43 @@ +; RUN: opt < %s -inline -debug-only=inline-cost -disable-output -print-instruction-detail |& FileCheck %s -check-prefixes=DETAIL,CHECK +; RUN: opt < %s -inline -debug-only=inline-cost -disable-output -print-instruction-delta |& FileCheck %s -check-prefixes=DELTA,CHECK + +; CHECK: Analyzing call of callee1... (caller:foo) +; CHECK: define i32 @callee1(i32 %x) { +; DELTA: ; cost delta = 5 +; DETAIL: ; cost before = -35, cost after = -30, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %x1 = add i32 %x, 1 +; DELTA: ; cost delta = 5 +; DETAIL: ; cost before = -30, cost after = -25, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %x2 = add i32 %x1, 1 +; DELTA: ; cost delta = 5 +; DETAIL: ; cost before = -25, cost after = -20, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %x3 = add i32 %x2, 1 +; DELTA: ; cost delta = 0 +; DETAIL: ; cost before = -20, cost after = -20, threshold before = 674, threshold after = 674, cost delta = 0 +; CHECK: ret i32 %x3 +; CHECK: } +; CHECK: NumConstantArgs: 0 +; CHECK: NumConstantOffsetPtrArgs: 0 +; CHECK: NumAllocaArgs: 0 +; CHECK: NumConstantPtrCmps: 0 +; CHECK: NumConstantPtrDiffs: 0 +; CHECK: NumInstructionsSimplified: 1 +; CHECK: NumInstructions: 4 +; CHECK: SROACostSavings: 0 +; CHECK: SROACostSavingsLost: 0 +; CHECK: LoadEliminationCost: 0 +; CHECK: ContainsNoDuplicateCall: 0 +; CHECK: Cost: -20 +; CHECK: Threshold: 337 + +define i32 @foo(i32 %y) { + %x = call i32 @callee1(i32 %y) + ret i32 %x +} + +define i32 @callee1(i32 %x) { + %x1 = add i32 %x, 1 + %x2 = add i32 %x1, 1 + %x3 = add i32 %x2, 1 + ret i32 %x3 +}