1//===- ReplayInlineAdvisor.h - Replay Inline Advisor interface -*- C++ --*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9#ifndef LLVM_ANALYSIS_REPLAYINLINEADVISOR_H
10#define LLVM_ANALYSIS_REPLAYINLINEADVISOR_H
11
12#include "llvm/ADT/StringSet.h"
13#include "llvm/Analysis/InlineAdvisor.h"
14
15namespace llvm {
16class CallBase;
17class Function;
18class LLVMContext;
19class Module;
20
21struct CallSiteFormat {
22  enum class Format : int {
23    Line,
24    LineColumn,
25    LineDiscriminator,
26    LineColumnDiscriminator
27  };
28
29  bool outputColumn() const {
30    return OutputFormat == Format::LineColumn ||
31           OutputFormat == Format::LineColumnDiscriminator;
32  }
33
34  bool outputDiscriminator() const {
35    return OutputFormat == Format::LineDiscriminator ||
36           OutputFormat == Format::LineColumnDiscriminator;
37  }
38
39  Format OutputFormat;
40};
41
42/// Replay Inliner Setup
43struct ReplayInlinerSettings {
44  enum class Scope : int { Function, Module };
45  enum class Fallback : int { Original, AlwaysInline, NeverInline };
46
47  StringRef ReplayFile;
48  Scope ReplayScope;
49  Fallback ReplayFallback;
50  CallSiteFormat ReplayFormat;
51};
52
53/// Get call site location as a string with the given format
54std::string formatCallSiteLocation(DebugLoc DLoc, const CallSiteFormat &Format);
55
56std::unique_ptr<InlineAdvisor>
57getReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
58                       LLVMContext &Context,
59                       std::unique_ptr<InlineAdvisor> OriginalAdvisor,
60                       const ReplayInlinerSettings &ReplaySettings,
61                       bool EmitRemarks, InlineContext IC);
62
63/// Replay inline advisor that uses optimization remarks from inlining of
64/// previous build to guide current inlining. This is useful for inliner tuning.
65class ReplayInlineAdvisor : public InlineAdvisor {
66public:
67  ReplayInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
68                      LLVMContext &Context,
69                      std::unique_ptr<InlineAdvisor> OriginalAdvisor,
70                      const ReplayInlinerSettings &ReplaySettings,
71                      bool EmitRemarks, InlineContext IC);
72  std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
73  bool areReplayRemarksLoaded() const { return HasReplayRemarks; }
74
75private:
76  bool hasInlineAdvice(Function &F) const {
77    return (ReplaySettings.ReplayScope ==
78            ReplayInlinerSettings::Scope::Module) ||
79           CallersToReplay.contains(F.getName());
80  }
81  std::unique_ptr<InlineAdvisor> OriginalAdvisor;
82  bool HasReplayRemarks = false;
83  const ReplayInlinerSettings ReplaySettings;
84  bool EmitRemarks = false;
85
86  StringMap<bool> InlineSitesFromRemarks;
87  StringSet<> CallersToReplay;
88};
89} // namespace llvm
90#endif // LLVM_ANALYSIS_REPLAYINLINEADVISOR_H
91