1 //===- SampleProfWriter.h - Write LLVM sample profile data ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains definitions needed for writing sample profiles.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
14 #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
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"
25 namespace sampleprof {
27 /// \brief Sample-based profile writer. Base class.
28 class SampleProfileWriter {
30 SampleProfileWriter(StringRef Filename, std::error_code &EC,
31 sys::fs::OpenFlags Flags)
32 : OS(Filename, EC, Flags) {}
33 virtual ~SampleProfileWriter() {}
35 /// \brief Write sample profiles in \p S for function \p F.
37 /// \returns true if the file was updated successfully. False, otherwise.
38 virtual bool write(const Function &F, const FunctionSamples &S) = 0;
40 /// \brief Write all the sample profiles for all the functions in \p M.
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()]))
51 /// \brief Output stream where to emit the profile to.
55 /// \brief Sample-based profile writer (text format).
56 class SampleProfileWriterText : public SampleProfileWriter {
58 SampleProfileWriterText(StringRef F, std::error_code &EC)
59 : SampleProfileWriter(F, EC, sys::fs::F_Text) {}
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);
67 /// \brief Sample-based profile writer (binary format).
68 class SampleProfileWriterBinary : public SampleProfileWriter {
70 SampleProfileWriterBinary(StringRef F, std::error_code &EC);
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);
78 } // End namespace sampleprof
80 } // End namespace llvm
82 #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H