1 //===- Transforms/Instrumentation/PGOInstrumentation.h ----------*- 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 /// \file
10 /// This file provides the interface for IR based instrumentation passes (
11 /// (profile-gen, and profile-use).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H
16 #define LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 #include "llvm/IR/PassManager.h"
21 #include <cstdint>
22 #include <string>
23 
24 namespace llvm {
25 
26 class Function;
27 class Instruction;
28 class Module;
29 
30 namespace vfs {
31 class FileSystem;
32 } // namespace vfs
33 
34 /// The instrumentation (profile-instr-gen) pass for IR based PGO.
35 // We use this pass to create COMDAT profile variables for context
36 // sensitive PGO (CSPGO). The reason to have a pass for this is CSPGO
37 // can be run after LTO/ThinLTO linking. Lld linker needs to see
38 // all the COMDAT variables before linking. So we have this pass
39 // always run before linking for CSPGO.
40 class PGOInstrumentationGenCreateVar
41     : public PassInfoMixin<PGOInstrumentationGenCreateVar> {
42 public:
43   PGOInstrumentationGenCreateVar(std::string CSInstrName = "")
44       : CSInstrName(CSInstrName) {}
45   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
46 
47 private:
48   std::string CSInstrName;
49 };
50 
51 /// The instrumentation (profile-instr-gen) pass for IR based PGO.
52 class PGOInstrumentationGen : public PassInfoMixin<PGOInstrumentationGen> {
53 public:
54   PGOInstrumentationGen(bool IsCS = false) : IsCS(IsCS) {}
55   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
56 
57 private:
58   // If this is a context sensitive instrumentation.
59   bool IsCS;
60 };
61 
62 /// The profile annotation (profile-instr-use) pass for IR based PGO.
63 class PGOInstrumentationUse : public PassInfoMixin<PGOInstrumentationUse> {
64 public:
65   PGOInstrumentationUse(std::string Filename = "",
66                         std::string RemappingFilename = "", bool IsCS = false,
67                         IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
68 
69   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
70 
71 private:
72   std::string ProfileFileName;
73   std::string ProfileRemappingFileName;
74   // If this is a context sensitive instrumentation.
75   bool IsCS;
76   IntrusiveRefCntPtr<vfs::FileSystem> FS;
77 };
78 
79 /// The indirect function call promotion pass.
80 class PGOIndirectCallPromotion : public PassInfoMixin<PGOIndirectCallPromotion> {
81 public:
82   PGOIndirectCallPromotion(bool IsInLTO = false, bool SamplePGO = false)
83       : InLTO(IsInLTO), SamplePGO(SamplePGO) {}
84 
85   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
86 
87 private:
88   bool InLTO;
89   bool SamplePGO;
90 };
91 
92 /// The profile size based optimization pass for memory intrinsics.
93 class PGOMemOPSizeOpt : public PassInfoMixin<PGOMemOPSizeOpt> {
94 public:
95   PGOMemOPSizeOpt() = default;
96 
97   PreservedAnalyses run(Function &F, FunctionAnalysisManager &MAM);
98 };
99 
100 void setProfMetadata(Module *M, Instruction *TI, ArrayRef<uint64_t> EdgeCounts,
101                      uint64_t MaxCount);
102 
103 void setIrrLoopHeaderMetadata(Module *M, Instruction *TI, uint64_t Count);
104 
105 } // end namespace llvm
106 
107 #endif // LLVM_TRANSFORMS_INSTRUMENTATION_PGOINSTRUMENTATION_H
108