xref: /llvm-project/bolt/include/bolt/Profile/ProfileReaderBase.h (revision fd38366e4525c5507bbb2a2fc1f7d113a964224e)
1 //===- bolt/Profile/ProfileReaderBase.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 //  Interface to be implemented by all profile readers.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef BOLT_PROFILE_PROFILE_READER_BASE_H
14 #define BOLT_PROFILE_PROFILE_READER_BASE_H
15 
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/Support/Error.h"
18 
19 namespace llvm {
20 namespace bolt {
21 
22 class BinaryContext;
23 class BinaryFunction;
24 class BoltAddressTranslation;
25 
26 class ProfileReaderBase {
27 protected:
28   /// Name of the file with profile.
29   std::string Filename;
30 
31 public:
32   ProfileReaderBase() = delete;
33   ProfileReaderBase(const ProfileReaderBase &) = delete;
34   ProfileReaderBase &operator=(const ProfileReaderBase &) = delete;
35   ProfileReaderBase(ProfileReaderBase &&) = delete;
36   ProfileReaderBase &operator=(ProfileReaderBase &&) = delete;
37 
38   /// Construct a reader for a given file.
ProfileReaderBase(StringRef Filename)39   explicit ProfileReaderBase(StringRef Filename) : Filename(Filename) {}
40 
41   virtual ~ProfileReaderBase() = default;
42 
43   /// Return the name of the file containing the profile.
getFilename()44   StringRef getFilename() const { return Filename; }
45 
46   /// Instruct the profiler to use address-translation tables.
setBAT(BoltAddressTranslation * BAT)47   virtual void setBAT(BoltAddressTranslation *BAT) {}
48 
49   /// Pre-process the profile when functions in \p BC are discovered,
50   /// but not yet disassembled. Once the profile is pre-processed, calls to
51   /// mayHaveProfileData() should be able to identify if the function possibly
52   /// has a profile available.
53   virtual Error preprocessProfile(BinaryContext &BC) = 0;
54 
55   /// Assign profile to all objects in the \p BC while functions are
56   /// in pre-CFG state with instruction addresses available.
57   virtual Error readProfilePreCFG(BinaryContext &BC) = 0;
58 
59   /// Assign profile to all objects in the \p BC.
60   virtual Error readProfile(BinaryContext &BC) = 0;
61 
62   /// Return the string identifying the reader.
63   virtual StringRef getReaderName() const = 0;
64 
65   /// Return true if the function \p BF may have a profile available.
66   /// The result is based on the name(s) of the function alone and the profile
67   /// match is not guaranteed.
mayHaveProfileData(const BinaryFunction & BF)68   virtual bool mayHaveProfileData(const BinaryFunction &BF) { return true; }
69 
70   /// Return true if the profile contains an entry for a local object
71   /// that has an associated file name.
hasLocalsWithFileName()72   virtual bool hasLocalsWithFileName() const { return true; }
73 
74   /// Return all event names used to collect this profile.
getEventNames()75   virtual StringSet<> getEventNames() const { return StringSet<>(); }
76 
77   /// Return true if the source of the profile should be trusted. E.g., even
78   /// good source of profile data may contain discrepancies. Nevertheless, the
79   /// rest of the profile is correct.
80   virtual bool isTrustedSource() const = 0;
81 };
82 
83 } // namespace bolt
84 } // namespace llvm
85 
86 #endif
87