[PGO] Allow input value node list to be null
[oota-llvm.git] / include / llvm / ProfileData / SampleProfWriter.h
1 //===- SampleProfWriter.h - Write LLVM sample profile data ----------------===//
2 //
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains definitions needed for writing sample profiles.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
14 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
15
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ProfileData/SampleProf.h"
19 #include "llvm/Support/ErrorOr.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 namespace llvm {
24
25 namespace sampleprof {
26
27 enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC };
28
29 /// \brief Sample-based profile writer. Base class.
30 class SampleProfileWriter {
31 public:
32   SampleProfileWriter(StringRef Filename, std::error_code &EC,
33                       sys::fs::OpenFlags Flags)
34       : OS(Filename, EC, Flags) {}
35   virtual ~SampleProfileWriter() {}
36
37   /// Write sample profiles in \p S for function \p FName.
38   ///
39   /// \returns status code of the file update operation.
40   virtual std::error_code write(StringRef FName, const FunctionSamples &S) = 0;
41
42   /// Write all the sample profiles in the given map of samples.
43   ///
44   /// \returns status code of the file update operation.
45   std::error_code write(const StringMap<FunctionSamples> &ProfileMap) {
46     if (std::error_code EC = writeHeader(ProfileMap))
47       return EC;
48
49     for (const auto &I : ProfileMap) {
50       StringRef FName = I.first();
51       const FunctionSamples &Profile = I.second;
52       if (std::error_code EC = write(FName, Profile))
53         return EC;
54     }
55     return sampleprof_error::success;
56   }
57
58   /// Profile writer factory.
59   ///
60   /// Create a new writer based on the value of \p Format.
61   static ErrorOr<std::unique_ptr<SampleProfileWriter>>
62   create(StringRef Filename, SampleProfileFormat Format);
63
64 protected:
65   /// \brief Write a file header for the profile file.
66   virtual std::error_code
67   writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0;
68
69   /// \brief Output stream where to emit the profile to.
70   raw_fd_ostream OS;
71 };
72
73 /// \brief Sample-based profile writer (text format).
74 class SampleProfileWriterText : public SampleProfileWriter {
75 public:
76   SampleProfileWriterText(StringRef F, std::error_code &EC)
77       : SampleProfileWriter(F, EC, sys::fs::F_Text), Indent(0) {}
78
79   std::error_code write(StringRef FName, const FunctionSamples &S) override;
80
81 protected:
82   std::error_code
83   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override {
84     return sampleprof_error::success;
85   }
86
87 private:
88   /// Indent level to use when writing.
89   ///
90   /// This is used when printing inlined callees.
91   unsigned Indent;
92 };
93
94 /// \brief Sample-based profile writer (binary format).
95 class SampleProfileWriterBinary : public SampleProfileWriter {
96 public:
97   SampleProfileWriterBinary(StringRef F, std::error_code &EC)
98       : SampleProfileWriter(F, EC, sys::fs::F_None), NameTable() {}
99
100   std::error_code write(StringRef F, const FunctionSamples &S) override;
101
102 protected:
103   std::error_code
104   writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
105   std::error_code writeNameIdx(StringRef FName);
106   std::error_code writeBody(StringRef FName, const FunctionSamples &S);
107
108 private:
109   void addName(StringRef FName);
110   void addNames(const FunctionSamples &S);
111
112   MapVector<StringRef, uint32_t> NameTable;
113 };
114
115 } // End namespace sampleprof
116
117 } // End namespace llvm
118
119 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H