Fix comment spelling and tidy diagnostic call in profile reader.
[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/StringRef.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/ProfileData/SampleProf.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 namespace llvm {
24
25 namespace sampleprof {
26
27 /// \brief Sample-based profile writer. Base class.
28 class SampleProfileWriter {
29 public:
30   SampleProfileWriter(StringRef Filename, std::error_code &EC,
31                       sys::fs::OpenFlags Flags)
32       : OS(Filename, EC, Flags) {}
33   virtual ~SampleProfileWriter() {}
34
35   /// \brief Write sample profiles in \p S for function \p F.
36   ///
37   /// \returns true if the file was updated successfully. False, otherwise.
38   virtual bool write(const Function &F, const FunctionSamples &S) = 0;
39
40   /// \brief Write all the sample profiles for all the functions in \p M.
41   ///
42   /// \returns true if the file was updated successfully. False, otherwise.
43   bool write(const Module &M, StringMap<FunctionSamples> &P) {
44     for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
45       if (!write((*I), P[I->getName()]))
46         return false;
47     return true;
48   }
49
50 protected:
51   /// \brief Output stream where to emit the profile to.
52   raw_fd_ostream OS;
53 };
54
55 /// \brief Sample-based profile writer (text format).
56 class SampleProfileWriterText : public SampleProfileWriter {
57 public:
58   SampleProfileWriterText(StringRef F, std::error_code &EC)
59       : SampleProfileWriter(F, EC, sys::fs::F_Text) {}
60
61   bool write(const Function &F, const FunctionSamples &S) override;
62   bool write(const Module &M, StringMap<FunctionSamples> &P) {
63     return SampleProfileWriter::write(M, P);
64   }
65 };
66
67 /// \brief Sample-based profile writer (binary format).
68 class SampleProfileWriterBinary : public SampleProfileWriter {
69 public:
70   SampleProfileWriterBinary(StringRef F, std::error_code &EC);
71
72   bool write(const Function &F, const FunctionSamples &S) override;
73   bool write(const Module &M, StringMap<FunctionSamples> &P) {
74     return SampleProfileWriter::write(M, P);
75   }
76 };
77
78 } // End namespace sampleprof
79
80 } // End namespace llvm
81
82 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H