1 //===- SampleProfWriter.cpp - 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 implements the class that writes LLVM sample profiles. It
11 // supports two file formats: text and binary. The textual representation
12 // is useful for debugging and testing purposes. The binary representation
13 // is more compact, resulting in smaller file sizes. However, they can
14 // both be used interchangeably.
16 // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
19 //===----------------------------------------------------------------------===//
21 #include "llvm/ProfileData/SampleProfWriter.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/LEB128.h"
26 #include "llvm/Support/LineIterator.h"
27 #include "llvm/Support/Regex.h"
29 using namespace llvm::sampleprof;
32 /// \brief Write samples to a text file.
33 bool SampleProfileWriterText::write(const Function &F,
34 const FunctionSamples &S) {
38 OS << F.getName() << ":" << S.getTotalSamples() << ":" << S.getHeadSamples()
41 for (BodySampleMap::const_iterator I = S.getBodySamples().begin(),
42 E = S.getBodySamples().end();
44 LineLocation Loc = I->first;
45 SampleRecord Sample = I->second;
46 if (Loc.Discriminator == 0)
47 OS << Loc.LineOffset << ": ";
49 OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
51 OS << Sample.getSamples();
53 for (SampleRecord::CallTargetList::const_iterator
54 I = Sample.getCallTargets().begin(),
55 E = Sample.getCallTargets().end();
57 OS << " " << (*I).first << ":" << (*I).second;
64 SampleProfileWriterBinary::SampleProfileWriterBinary(StringRef F,
66 : SampleProfileWriter(F, EC, sys::fs::F_None) {
70 // Write the file header.
71 encodeULEB128(SPMagic(), OS);
72 encodeULEB128(SPVersion(), OS);
75 /// \brief Write samples to a binary file.
77 /// \returns true if the samples were written successfully, false otherwise.
78 bool SampleProfileWriterBinary::write(const Function &F,
79 const FunctionSamples &S) {
85 encodeULEB128(S.getTotalSamples(), OS);
86 encodeULEB128(S.getHeadSamples(), OS);
87 encodeULEB128(S.getBodySamples().size(), OS);
88 for (BodySampleMap::const_iterator I = S.getBodySamples().begin(),
89 E = S.getBodySamples().end();
91 LineLocation Loc = I->first;
92 SampleRecord Sample = I->second;
93 encodeULEB128(Loc.LineOffset, OS);
94 encodeULEB128(Loc.Discriminator, OS);
95 encodeULEB128(Sample.getSamples(), OS);
96 encodeULEB128(Sample.getCallTargets().size(), OS);
97 for (SampleRecord::CallTargetList::const_iterator
98 I = Sample.getCallTargets().begin(),
99 E = Sample.getCallTargets().end();
101 std::string Callee = (*I).first;
102 unsigned CalleeSamples = (*I).second;
104 encodeULEB128(0, OS);
105 encodeULEB128(CalleeSamples, OS);