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. @@ -537,6 +574,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 @@ -636,6 +697,10 @@ Params.ComputeFullInlineCost || ORE), Params(Params), Threshold(Params.DefaultThreshold), BoostIndirectCalls(BoostIndirect) {} + + /// Annotation Writer for cost annotation + CostAnnotationWriter Writer; + void dump(); virtual ~InlineCostCallAnalyzer() {} @@ -654,6 +719,25 @@ EnabledSROAArgValues.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)) { @@ -1761,12 +1845,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(); @@ -2047,6 +2135,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,141 @@ +; RUN: opt < %s -inline -debug-only=inline-cost -disable-output -print-instruction-detail |& FileCheck %s + +; CHECK: Analyzing call of _Z3sumii... (caller:main) +; CHECK: define i32 @_Z3sumii(i32 %x, i32 %y) !dbg !6 { +; CHECK: entry: +; CHECK: ; cost before = -40, cost after = -35, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %x.addr = alloca i32, align 4 +; CHECK: ; cost before = -35, cost after = -30, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %y.addr = alloca i32, align 4 +; CHECK: ; cost before = -30, cost after = -25, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: store i32 %x, i32* %x.addr, align 4 +; CHECK: ; cost before = -25, cost after = -20, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: store i32 %y, i32* %y.addr, align 4 +; CHECK: ; cost before = -20, cost after = -15, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %0 = load i32, i32* %x.addr, align 4, !dbg !8 +; CHECK: ; cost before = -15, cost after = -10, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %1 = load i32, i32* %y.addr, align 4, !dbg !8 +; CHECK: ; cost before = -10, cost after = -5, threshold before = 674, threshold after = 674, cost delta = 5 +; CHECK: %add = add nsw i32 %0, %1, !dbg !8 +; CHECK: ; cost before = -5, cost after = -5, threshold before = 674, threshold after = 674, cost delta = 0 +; CHECK: ret i32 %add, !dbg !8 +; CHECK: } +; CHECK: NumConstantArgs: 0 +; CHECK: NumConstantOffsetPtrArgs: 0 +; CHECK: NumAllocaArgs: 0 +; CHECK: NumConstantPtrCmps: 0 +; CHECK: NumConstantPtrDiffs: 0 +; CHECK: NumInstructionsSimplified: 1 +; CHECK: NumInstructions: 8 +; CHECK: SROACostSavings: 0 +; CHECK: SROACostSavingsLost: 0 +; CHECK: LoadEliminationCost: 0 +; CHECK: ContainsNoDuplicateCall: 0 +; CHECK: Cost: -5 +; CHECK: Threshold: 337 + +; Original C++ test case +; +; #include +; +; int sum(int x, int y) { +; return x + y; +; } +; +; int main() { +; int s, i = 0; +; while (i++ < 20000 * 20000) +; if (i != 100) s = sum(i, s); else s = 30; +; printf("sum is %d\n", s); +; return 0; +; } +; +@.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1 + +; Function Attrs: nounwind uwtable +define i32 @_Z3sumii(i32 %x, i32 %y) !dbg !4 { +entry: + %x.addr = alloca i32, align 4 + %y.addr = alloca i32, align 4 + store i32 %x, i32* %x.addr, align 4 + store i32 %y, i32* %y.addr, align 4 + %0 = load i32, i32* %x.addr, align 4, !dbg !11 + %1 = load i32, i32* %y.addr, align 4, !dbg !11 + %add = add nsw i32 %0, %1, !dbg !11 + ret i32 %add, !dbg !11 +} + +; Function Attrs: uwtable +define i32 @main() !dbg !7 { +entry: + %retval = alloca i32, align 4 + %s = alloca i32, align 4 + %i = alloca i32, align 4 + store i32 0, i32* %retval + store i32 0, i32* %i, align 4, !dbg !12 + br label %while.cond, !dbg !13 + +while.cond: ; preds = %if.end, %entry + %0 = load i32, i32* %i, align 4, !dbg !14 + %inc = add nsw i32 %0, 1, !dbg !14 + store i32 %inc, i32* %i, align 4, !dbg !14 + %cmp = icmp slt i32 %0, 400000000, !dbg !14 + br i1 %cmp, label %while.body, label %while.end, !dbg !14 + +while.body: ; preds = %while.cond + %1 = load i32, i32* %i, align 4, !dbg !16 + %cmp1 = icmp ne i32 %1, 100, !dbg !16 + br i1 %cmp1, label %if.then, label %if.else, !dbg !16 + + +if.then: ; preds = %while.body + %2 = load i32, i32* %i, align 4, !dbg !18 + %3 = load i32, i32* %s, align 4, !dbg !18 + %call = call i32 @_Z3sumii(i32 %2, i32 %3), !dbg !18 + store i32 %call, i32* %s, align 4, !dbg !18 + br label %if.end, !dbg !18 + +if.else: ; preds = %while.body + store i32 30, i32* %s, align 4, !dbg !20 + br label %if.end + +if.end: ; preds = %if.else, %if.then + br label %while.cond, !dbg !22 + +while.end: ; preds = %while.cond + %4 = load i32, i32* %s, align 4, !dbg !24 + %call2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0), i32 %4), !dbg !24 + ret i32 0, !dbg !25 +} + +declare i32 @printf(i8*, ...) #2 + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!8, !9} +!llvm.ident = !{!10} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, producer: "clang version 3.5 ", isOptimized: false, emissionKind: NoDebug, file: !1, enums: !2, retainedTypes: !2, globals: !2, imports: !2) +!1 = !DIFile(filename: "calls.cc", directory: ".") +!2 = !{} +!4 = distinct !DISubprogram(name: "sum", line: 3, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 3, file: !1, scope: !5, type: !6, retainedNodes: !2) +!5 = !DIFile(filename: "calls.cc", directory: ".") +!6 = !DISubroutineType(types: !2) +!7 = distinct !DISubprogram(name: "main", line: 7, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !0, scopeLine: 7, file: !1, scope: !5, type: !6, retainedNodes: !2) +!8 = !{i32 2, !"Dwarf Version", i32 4} +!9 = !{i32 1, !"Debug Info Version", i32 3} +!10 = !{!"clang version 3.5 "} +!11 = !DILocation(line: 4, scope: !4) +!12 = !DILocation(line: 8, scope: !7) +!13 = !DILocation(line: 9, scope: !7) +!14 = !DILocation(line: 9, scope: !15) +!15 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !7) +!16 = !DILocation(line: 10, scope: !17) +!17 = distinct !DILexicalBlock(line: 10, column: 0, file: !1, scope: !7) +!18 = !DILocation(line: 10, scope: !19) +!19 = !DILexicalBlockFile(discriminator: 2, file: !1, scope: !17) +!20 = !DILocation(line: 10, scope: !21) +!21 = !DILexicalBlockFile(discriminator: 4, file: !1, scope: !17) +!22 = !DILocation(line: 10, scope: !23) +!23 = !DILexicalBlockFile(discriminator: 6, file: !1, scope: !17) +!24 = !DILocation(line: 11, scope: !7) +!25 = !DILocation(line: 12, scope: !7)