[DIBuilder] Make createReferenceType take size and align
[oota-llvm.git] / lib / ProfileData / SampleProfWriter.cpp
1 //===- SampleProfWriter.cpp - 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 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.
15 //
16 // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
17 // supported formats.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "llvm/ProfileData/SampleProfWriter.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/LineIterator.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Regex.h"
28
29 using namespace llvm::sampleprof;
30 using namespace llvm;
31
32 /// \brief Write samples to a text file.
33 ///
34 /// Note: it may be tempting to implement this in terms of
35 /// FunctionSamples::dump().  Please don't.  The dump functionality is intended
36 /// for debugging and has no specified form.
37 ///
38 /// The format used here is more structured and deliberate because
39 /// it needs to be parsed by the SampleProfileReaderText class.
40 std::error_code SampleProfileWriterText::write(StringRef FName,
41                                                const FunctionSamples &S) {
42   OS << FName << ":" << S.getTotalSamples();
43   if (Indent == 0)
44     OS << ":" << S.getHeadSamples();
45   OS << "\n";
46
47   for (const auto &I : S.getBodySamples()) {
48     LineLocation Loc = I.first;
49     const SampleRecord &Sample = I.second;
50     OS.indent(Indent + 1);
51     if (Loc.Discriminator == 0)
52       OS << Loc.LineOffset << ": ";
53     else
54       OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
55
56     OS << Sample.getSamples();
57
58     for (const auto &J : Sample.getCallTargets())
59       OS << " " << J.first() << ":" << J.second;
60     OS << "\n";
61   }
62
63   Indent += 1;
64   for (const auto &I : S.getCallsiteSamples()) {
65     CallsiteLocation Loc = I.first;
66     const FunctionSamples &CalleeSamples = I.second;
67     OS.indent(Indent);
68     if (Loc.Discriminator == 0)
69       OS << Loc.LineOffset << ": ";
70     else
71       OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
72     if (std::error_code EC = write(Loc.CalleeName, CalleeSamples))
73       return EC;
74   }
75   Indent -= 1;
76
77   return sampleprof_error::success;
78 }
79
80 std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
81   const auto &ret = NameTable.find(FName);
82   if (ret == NameTable.end())
83     return sampleprof_error::truncated_name_table;
84   encodeULEB128(ret->second, OS);
85   return sampleprof_error::success;
86 }
87
88 void SampleProfileWriterBinary::addName(StringRef FName) {
89   auto NextIdx = NameTable.size();
90   NameTable.insert(std::make_pair(FName, NextIdx));
91 }
92
93 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
94   // Add all the names in indirect call targets.
95   for (const auto &I : S.getBodySamples()) {
96     const SampleRecord &Sample = I.second;
97     for (const auto &J : Sample.getCallTargets())
98       addName(J.first());
99   }
100
101   // Recursively add all the names for inlined callsites.
102   for (const auto &J : S.getCallsiteSamples()) {
103     CallsiteLocation Loc = J.first;
104     const FunctionSamples &CalleeSamples = J.second;
105     addName(Loc.CalleeName);
106     addNames(CalleeSamples);
107   }
108 }
109
110 std::error_code SampleProfileWriterBinary::writeHeader(
111     const StringMap<FunctionSamples> &ProfileMap) {
112   // Write file magic identifier.
113   encodeULEB128(SPMagic(), OS);
114   encodeULEB128(SPVersion(), OS);
115
116   // Generate the name table for all the functions referenced in the profile.
117   for (const auto &I : ProfileMap) {
118     addName(I.first());
119     addNames(I.second);
120   }
121
122   // Write out the name table.
123   encodeULEB128(NameTable.size(), OS);
124   for (auto N : NameTable) {
125     OS << N.first;
126     encodeULEB128(0, OS);
127   }
128
129   return sampleprof_error::success;
130 }
131
132 std::error_code SampleProfileWriterBinary::writeBody(StringRef FName,
133                                                      const FunctionSamples &S) {
134   if (std::error_code EC = writeNameIdx(FName))
135     return EC;
136
137   encodeULEB128(S.getTotalSamples(), OS);
138
139   // Emit all the body samples.
140   encodeULEB128(S.getBodySamples().size(), OS);
141   for (const auto &I : S.getBodySamples()) {
142     LineLocation Loc = I.first;
143     const SampleRecord &Sample = I.second;
144     encodeULEB128(Loc.LineOffset, OS);
145     encodeULEB128(Loc.Discriminator, OS);
146     encodeULEB128(Sample.getSamples(), OS);
147     encodeULEB128(Sample.getCallTargets().size(), OS);
148     for (const auto &J : Sample.getCallTargets()) {
149       StringRef Callee = J.first();
150       uint64_t CalleeSamples = J.second;
151       if (std::error_code EC = writeNameIdx(Callee))
152         return EC;
153       encodeULEB128(CalleeSamples, OS);
154     }
155   }
156
157   // Recursively emit all the callsite samples.
158   encodeULEB128(S.getCallsiteSamples().size(), OS);
159   for (const auto &J : S.getCallsiteSamples()) {
160     CallsiteLocation Loc = J.first;
161     const FunctionSamples &CalleeSamples = J.second;
162     encodeULEB128(Loc.LineOffset, OS);
163     encodeULEB128(Loc.Discriminator, OS);
164     if (std::error_code EC = writeBody(Loc.CalleeName, CalleeSamples))
165       return EC;
166   }
167
168   return sampleprof_error::success;
169 }
170
171 /// \brief Write samples of a top-level function to a binary file.
172 ///
173 /// \returns true if the samples were written successfully, false otherwise.
174 std::error_code SampleProfileWriterBinary::write(StringRef FName,
175                                                  const FunctionSamples &S) {
176   encodeULEB128(S.getHeadSamples(), OS);
177   return writeBody(FName, S);
178 }
179
180 /// \brief Create a sample profile writer based on the specified format.
181 ///
182 /// \param Filename The file to create.
183 ///
184 /// \param Writer The writer to instantiate according to the specified format.
185 ///
186 /// \param Format Encoding format for the profile file.
187 ///
188 /// \returns an error code indicating the status of the created writer.
189 ErrorOr<std::unique_ptr<SampleProfileWriter>>
190 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
191   std::error_code EC;
192   std::unique_ptr<SampleProfileWriter> Writer;
193
194   if (Format == SPF_Binary)
195     Writer.reset(new SampleProfileWriterBinary(Filename, EC));
196   else if (Format == SPF_Text)
197     Writer.reset(new SampleProfileWriterText(Filename, EC));
198   else if (Format == SPF_GCC)
199     EC = sampleprof_error::unsupported_writing_format;
200   else
201     EC = sampleprof_error::unrecognized_format;
202
203   if (EC)
204     return EC;
205
206   return std::move(Writer);
207 }