annotate llvm/lib/Analysis/InlineAdvisor.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900 (2021-06-07)
parents 0572611fdcc8
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- InlineAdvisor.cpp - analysis pass implementation -------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 // This file implements InlineAdvisorAnalysis and DefaultInlineAdvisor, and
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // related types.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "llvm/Analysis/InlineAdvisor.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "llvm/ADT/Statistic.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "llvm/Analysis/InlineCost.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "llvm/Analysis/ProfileSummaryInfo.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
19 #include "llvm/Analysis/ReplayInlineAdvisor.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "llvm/Analysis/TargetLibraryInfo.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/Analysis/TargetTransformInfo.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
22 #include "llvm/IR/DebugInfoMetadata.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "llvm/IR/Instructions.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
24 #include "llvm/Support/CommandLine.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "llvm/Support/raw_ostream.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 using namespace llvm;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #define DEBUG_TYPE "inline"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 // This weirdly named statistic tracks the number of times that, when attempting
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 // to inline a function A into B, we analyze the callers of B in order to see
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 // if those would be more profitable and blocked inline steps.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 /// Flag to add inline messages as callsite attributes 'inline-remark'.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 static cl::opt<bool>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 InlineRemarkAttribute("inline-remark-attribute", cl::init(false),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 cl::Hidden,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 cl::desc("Enable adding inline-remark attribute to"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 " callsites processed by inliner but decided"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 " to be not inlined"));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 // An integer used to limit the cost of inline deferral. The default negative
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 // number tells shouldBeDeferred to only take the secondary cost into account.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 static cl::opt<int>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 InlineDeferralScale("inline-deferral-scale",
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 cl::desc("Scale to limit the cost of inline deferral"),
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
48 cl::init(2), cl::Hidden);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
50 extern cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
52 void DefaultInlineAdvice::recordUnsuccessfulInliningImpl(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
53 const InlineResult &Result) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 using namespace ore;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
55 llvm::setInlineRemark(*OriginalCB, std::string(Result.getFailureReason()) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
56 "; " + inlineCostStr(*OIC));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
57 ORE.emit([&]() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
58 return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
59 << NV("Callee", Callee) << " will not be inlined into "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
60 << NV("Caller", Caller) << ": "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
61 << NV("Reason", Result.getFailureReason());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
62 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
63 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
65 void DefaultInlineAdvice::recordInliningWithCalleeDeletedImpl() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
66 if (EmitRemarks)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
67 emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
68 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
70 void DefaultInlineAdvice::recordInliningImpl() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
71 if (EmitRemarks)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
72 emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
73 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
75 llvm::Optional<llvm::InlineCost> static getDefaultInlineAdvice(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
76 CallBase &CB, FunctionAnalysisManager &FAM, const InlineParams &Params) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 Function &Caller = *CB.getCaller();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 ProfileSummaryInfo *PSI =
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 FAM.getResult<ModuleAnalysisManagerFunctionProxy>(Caller)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 .getCachedResult<ProfileSummaryAnalysis>(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 *CB.getParent()->getParent()->getParent());
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 return FAM.getResult<AssumptionAnalysis>(F);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 return FAM.getResult<BlockFrequencyAnalysis>(F);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 return FAM.getResult<TargetLibraryAnalysis>(F);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 auto GetInlineCost = [&](CallBase &CB) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 Function &Callee = *CB.getCalledFunction();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 bool RemarksEnabled =
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 DEBUG_TYPE);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 GetBFI, PSI, RemarksEnabled ? &ORE : nullptr);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 };
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
103 return llvm::shouldInline(CB, GetInlineCost, ORE,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
104 Params.EnableDeferral.getValueOr(false));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
105 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
106
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
107 std::unique_ptr<InlineAdvice>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
108 DefaultInlineAdvisor::getAdviceImpl(CallBase &CB) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
109 auto OIC = getDefaultInlineAdvice(CB, FAM, Params);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
110 return std::make_unique<DefaultInlineAdvice>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
111 this, CB, OIC,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
112 FAM.getResult<OptimizationRemarkEmitterAnalysis>(*CB.getCaller()));
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 InlineAdvice::InlineAdvice(InlineAdvisor *Advisor, CallBase &CB,
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
116 OptimizationRemarkEmitter &ORE,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 bool IsInliningRecommended)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 : Advisor(Advisor), Caller(CB.getCaller()), Callee(CB.getCalledFunction()),
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
119 DLoc(CB.getDebugLoc()), Block(CB.getParent()), ORE(ORE),
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 IsInliningRecommended(IsInliningRecommended) {}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 void InlineAdvisor::markFunctionAsDeleted(Function *F) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 assert((!DeletedFunctions.count(F)) &&
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 "Cannot put cause a function to become dead twice!");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 DeletedFunctions.insert(F);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 void InlineAdvisor::freeDeletedFunctions() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 for (auto *F : DeletedFunctions)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 delete F;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 DeletedFunctions.clear();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
134 void InlineAdvice::recordInlineStatsIfNeeded() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
135 if (Advisor->ImportedFunctionsStats)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
136 Advisor->ImportedFunctionsStats->recordInline(*Caller, *Callee);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
137 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
139 void InlineAdvice::recordInlining() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
140 markRecorded();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
141 recordInlineStatsIfNeeded();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
142 recordInliningImpl();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
143 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
144
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 void InlineAdvice::recordInliningWithCalleeDeleted() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 markRecorded();
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
147 recordInlineStatsIfNeeded();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 Advisor->markFunctionAsDeleted(Callee);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 recordInliningWithCalleeDeletedImpl();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 AnalysisKey InlineAdvisorAnalysis::Key;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 bool InlineAdvisorAnalysis::Result::tryCreate(InlineParams Params,
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
155 InliningAdvisorMode Mode,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
156 StringRef ReplayFile) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
157 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 switch (Mode) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 case InliningAdvisorMode::Default:
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160 Advisor.reset(new DefaultInlineAdvisor(M, FAM, Params));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 // Restrict replay to default advisor, ML advisors are stateful so
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
162 // replay will need augmentations to interleave with them correctly.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
163 if (!ReplayFile.empty()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
164 Advisor = std::make_unique<ReplayInlineAdvisor>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165 M, FAM, M.getContext(), std::move(Advisor), ReplayFile,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
166 /* EmitRemarks =*/true);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
167 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 break;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 case InliningAdvisorMode::Development:
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
170 #ifdef LLVM_HAVE_TF_API
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
171 Advisor =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
172 llvm::getDevelopmentModeAdvisor(M, MAM, [&FAM, Params](CallBase &CB) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
173 auto OIC = getDefaultInlineAdvice(CB, FAM, Params);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
174 return OIC.hasValue();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
175 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
176 #endif
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 break;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 case InliningAdvisorMode::Release:
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
179 #ifdef LLVM_HAVE_TF_AOT
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
180 Advisor = llvm::getReleaseModeAdvisor(M, MAM);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
181 #endif
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 break;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
184
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 return !!Advisor;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 /// Return true if inlining of CB can block the caller from being
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 /// inlined which is proved to be more beneficial. \p IC is the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 /// estimated inline cost associated with callsite \p CB.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 /// \p TotalSecondaryCost will be set to the estimated cost of inlining the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 /// caller if \p CB is suppressed for inlining.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 static bool
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 shouldBeDeferred(Function *Caller, InlineCost IC, int &TotalSecondaryCost,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 function_ref<InlineCost(CallBase &CB)> GetInlineCost) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 // For now we only handle local or inline functions.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage())
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 // If the cost of inlining CB is non-positive, it is not going to prevent the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 // caller from being inlined into its callers and hence we don't need to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 // defer.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 if (IC.getCost() <= 0)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 // Try to detect the case where the current inlining candidate caller (call
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 // it B) is a static or linkonce-ODR function and is an inlining candidate
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 // elsewhere, and the current candidate callee (call it C) is large enough
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 // that inlining it into B would make B too big to inline later. In these
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 // circumstances it may be best not to inline C into B, but to inline B into
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 // its callers.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 // This only applies to static and linkonce-ODR functions because those are
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 // expected to be available for inlining in the translation units where they
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 // are used. Thus we will always have the opportunity to make local inlining
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 // decisions. Importantly the linkonce-ODR linkage covers inline functions
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 // and templates in C++.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 // FIXME: All of this logic should be sunk into getInlineCost. It relies on
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 // the internal implementation of the inline cost metrics rather than
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 // treating them as truly abstract units etc.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 TotalSecondaryCost = 0;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 // The candidate cost to be imposed upon the current function.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 int CandidateCost = IC.getCost() - 1;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 // If the caller has local linkage and can be inlined to all its callers, we
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 // can apply a huge negative bonus to TotalSecondaryCost.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 bool ApplyLastCallBonus = Caller->hasLocalLinkage() && !Caller->hasOneUse();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 // This bool tracks what happens if we DO inline C into B.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 bool InliningPreventsSomeOuterInline = false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 unsigned NumCallerUsers = 0;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 for (User *U : Caller->users()) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 CallBase *CS2 = dyn_cast<CallBase>(U);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 // If this isn't a call to Caller (it could be some other sort
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 // of reference) skip it. Such references will prevent the caller
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 // from being removed.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 if (!CS2 || CS2->getCalledFunction() != Caller) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 ApplyLastCallBonus = false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 continue;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 InlineCost IC2 = GetInlineCost(*CS2);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 ++NumCallerCallersAnalyzed;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 if (!IC2) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 ApplyLastCallBonus = false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 continue;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 if (IC2.isAlways())
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 continue;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 // See if inlining of the original callsite would erase the cost delta of
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 // this callsite. We subtract off the penalty for the call instruction,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 // which we would be deleting.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 if (IC2.getCostDelta() <= CandidateCost) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 InliningPreventsSomeOuterInline = true;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 TotalSecondaryCost += IC2.getCost();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 NumCallerUsers++;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 if (!InliningPreventsSomeOuterInline)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 return false;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 // If all outer calls to Caller would get inlined, the cost for the last
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 // one is set very low by getInlineCost, in anticipation that Caller will
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 // be removed entirely. We did not account for this above unless there
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 // is only one caller of Caller.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 if (ApplyLastCallBonus)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 // If InlineDeferralScale is negative, then ignore the cost of primary
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 // inlining -- IC.getCost() multiplied by the number of callers to Caller.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 if (InlineDeferralScale < 0)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 return TotalSecondaryCost < IC.getCost();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
273
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 int TotalCost = TotalSecondaryCost + IC.getCost() * NumCallerUsers;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 int Allowance = IC.getCost() * InlineDeferralScale;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 return TotalCost < Allowance;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 namespace llvm {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
280 static raw_ostream &operator<<(raw_ostream &R, const ore::NV &Arg) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 return R << Arg.Val;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 template <class RemarkT>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 RemarkT &operator<<(RemarkT &&R, const InlineCost &IC) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 using namespace ore;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 if (IC.isAlways()) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 R << "(cost=always)";
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 } else if (IC.isNever()) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 R << "(cost=never)";
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 } else {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 R << "(cost=" << ore::NV("Cost", IC.getCost())
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 << ", threshold=" << ore::NV("Threshold", IC.getThreshold()) << ")";
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 if (const char *Reason = IC.getReason())
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 R << ": " << ore::NV("Reason", Reason);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 return R;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 } // namespace llvm
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 std::string llvm::inlineCostStr(const InlineCost &IC) {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
302 std::string Buffer;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
303 raw_string_ostream Remark(Buffer);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 Remark << IC;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 return Remark.str();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 void llvm::setInlineRemark(CallBase &CB, StringRef Message) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 if (!InlineRemarkAttribute)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 return;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 Attribute Attr = Attribute::get(CB.getContext(), "inline-remark", Message);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 CB.addAttribute(AttributeList::FunctionIndex, Attr);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 /// Return the cost only if the inliner should attempt to inline at the given
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 /// CallSite. If we return the cost, we will emit an optimisation remark later
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 /// using that cost, so we won't do so from this function. Return None if
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 /// inlining should not be attempted.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 Optional<InlineCost>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 llvm::shouldInline(CallBase &CB,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 function_ref<InlineCost(CallBase &CB)> GetInlineCost,
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
323 OptimizationRemarkEmitter &ORE, bool EnableDeferral) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 using namespace ore;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 InlineCost IC = GetInlineCost(CB);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 Instruction *Call = &CB;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 Function *Callee = CB.getCalledFunction();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 Function *Caller = CB.getCaller();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 if (IC.isAlways()) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 LLVM_DEBUG(dbgs() << " Inlining " << inlineCostStr(IC)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 << ", Call: " << CB << "\n");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 return IC;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 if (!IC) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 LLVM_DEBUG(dbgs() << " NOT Inlining " << inlineCostStr(IC)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 << ", Call: " << CB << "\n");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 if (IC.isNever()) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 ORE.emit([&]() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 << NV("Callee", Callee) << " not inlined into "
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 << NV("Caller", Caller) << " because it should never be inlined "
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 << IC;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 } else {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 ORE.emit([&]() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 return OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 << NV("Callee", Callee) << " not inlined into "
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 << NV("Caller", Caller) << " because too costly to inline "
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 << IC;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 setInlineRemark(CB, inlineCostStr(IC));
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 return None;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
358
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 int TotalSecondaryCost = 0;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
360 if (EnableDeferral &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
361 shouldBeDeferred(Caller, IC, TotalSecondaryCost, GetInlineCost)) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 LLVM_DEBUG(dbgs() << " NOT Inlining: " << CB
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 << " Cost = " << IC.getCost()
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 << ", outer Cost = " << TotalSecondaryCost << '\n');
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 ORE.emit([&]() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 return OptimizationRemarkMissed(DEBUG_TYPE, "IncreaseCostInOtherContexts",
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 Call)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 << "Not inlining. Cost of inlining " << NV("Callee", Callee)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 << " increases the cost of inlining " << NV("Caller", Caller)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 << " in other contexts";
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 setInlineRemark(CB, "deferred");
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 // IC does not bool() to false, so get an InlineCost that will.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 // This will not be inspected to make an error message.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 return None;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
377
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 LLVM_DEBUG(dbgs() << " Inlining " << inlineCostStr(IC) << ", Call: " << CB
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 << '\n');
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 return IC;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
382
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
383 std::string llvm::getCallSiteLocation(DebugLoc DLoc) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
384 std::string Buffer;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
385 raw_string_ostream CallSiteLoc(Buffer);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
386 bool First = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
387 for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
388 if (!First)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
389 CallSiteLoc << " @ ";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
390 // Note that negative line offset is actually possible, but we use
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
391 // unsigned int to match line offset representation in remarks so
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
392 // it's directly consumable by relay advisor.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
393 uint32_t Offset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
394 DIL->getLine() - DIL->getScope()->getSubprogram()->getLine();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
395 uint32_t Discriminator = DIL->getBaseDiscriminator();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
396 StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
397 if (Name.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
398 Name = DIL->getScope()->getSubprogram()->getName();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
399 CallSiteLoc << Name.str() << ":" << llvm::utostr(Offset) << ":"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
400 << llvm::utostr(DIL->getColumn());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
401 if (Discriminator)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
402 CallSiteLoc << "." << llvm::utostr(Discriminator);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
403 First = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
404 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
405
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
406 return CallSiteLoc.str();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
407 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
408
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
409 void llvm::addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
410 if (!DLoc.get()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
411 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
412 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
413
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
414 bool First = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
415 Remark << " at callsite ";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
416 for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
417 if (!First)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
418 Remark << " @ ";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
419 unsigned int Offset = DIL->getLine();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
420 Offset -= DIL->getScope()->getSubprogram()->getLine();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
421 unsigned int Discriminator = DIL->getBaseDiscriminator();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
422 StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
423 if (Name.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
424 Name = DIL->getScope()->getSubprogram()->getName();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
425 Remark << Name << ":" << ore::NV("Line", Offset) << ":"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
426 << ore::NV("Column", DIL->getColumn());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
427 if (Discriminator)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
428 Remark << "." << ore::NV("Disc", Discriminator);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
429 First = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
430 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
431
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
432 Remark << ";";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
433 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
434
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
435 void llvm::emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
436 const BasicBlock *Block, const Function &Callee,
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
437 const Function &Caller, const InlineCost &IC,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
438 bool ForProfileContext, const char *PassName) {
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
439 ORE.emit([&]() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
440 bool AlwaysInline = IC.isAlways();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
441 StringRef RemarkName = AlwaysInline ? "AlwaysInline" : "Inlined";
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
442 OptimizationRemark Remark(PassName ? PassName : DEBUG_TYPE, RemarkName,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
443 DLoc, Block);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
444 Remark << ore::NV("Callee", &Callee) << " inlined into ";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
445 Remark << ore::NV("Caller", &Caller);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
446 if (ForProfileContext)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
447 Remark << " to match profiling context";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
448 Remark << " with " << IC;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
449 addLocationToRemarks(Remark, DLoc);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
450 return Remark;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
451 });
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
453
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
454 InlineAdvisor::InlineAdvisor(Module &M, FunctionAnalysisManager &FAM)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
455 : M(M), FAM(FAM) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
456 if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
457 ImportedFunctionsStats =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
458 std::make_unique<ImportedFunctionsInliningStatistics>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
459 ImportedFunctionsStats->setModuleInfo(M);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
460 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
461 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
462
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
463 InlineAdvisor::~InlineAdvisor() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
464 if (ImportedFunctionsStats) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
465 assert(InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
466 ImportedFunctionsStats->dump(InlinerFunctionImportStats ==
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
467 InlinerFunctionImportStatsOpts::Verbose);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
468 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
469
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
470 freeDeletedFunctions();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
471 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
472
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
473 std::unique_ptr<InlineAdvice> InlineAdvisor::getMandatoryAdvice(CallBase &CB,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
474 bool Advice) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
475 return std::make_unique<InlineAdvice>(this, CB, getCallerORE(CB), Advice);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
476 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
477
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
478 InlineAdvisor::MandatoryInliningKind
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
479 InlineAdvisor::getMandatoryKind(CallBase &CB, FunctionAnalysisManager &FAM,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
480 OptimizationRemarkEmitter &ORE) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
481 auto &Callee = *CB.getCalledFunction();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
482
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
483 auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
484 return FAM.getResult<TargetLibraryAnalysis>(F);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
485 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
486
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
487 auto &TIR = FAM.getResult<TargetIRAnalysis>(Callee);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
488
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
489 auto TrivialDecision =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
490 llvm::getAttributeBasedInliningDecision(CB, &Callee, TIR, GetTLI);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
491
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
492 if (TrivialDecision.hasValue()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
493 if (TrivialDecision->isSuccess())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
494 return MandatoryInliningKind::Always;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
495 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
496 return MandatoryInliningKind::Never;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
497 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
498 return MandatoryInliningKind::NotMandatory;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
499 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
500
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
501 std::unique_ptr<InlineAdvice> InlineAdvisor::getAdvice(CallBase &CB,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
502 bool MandatoryOnly) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
503 if (!MandatoryOnly)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
504 return getAdviceImpl(CB);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
505 bool Advice = CB.getCaller() != CB.getCalledFunction() &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
506 MandatoryInliningKind::Always ==
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
507 getMandatoryKind(CB, FAM, getCallerORE(CB));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
508 return getMandatoryAdvice(CB, Advice);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
509 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
510
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
511 OptimizationRemarkEmitter &InlineAdvisor::getCallerORE(CallBase &CB) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
512 return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*CB.getCaller());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
513 }