1 //===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
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 // llvm-profdata merges .profdata files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/PrettyStackTrace.h"
20 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/raw_ostream.h"
25 static cl::opt<std::string> Filename1(cl::Positional, cl::Required,
27 static cl::opt<std::string> Filename2(cl::Positional, cl::Required,
30 static cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
32 cl::desc("Output file"));
33 static cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
34 cl::aliasopt(OutputFilename));
36 static bool readLine(const char *&Start, const char *End, StringRef &S) {
40 for (const char *I = Start; I != End; ++I) {
41 assert(*I && "unexpected binary data");
43 S = StringRef(Start, I - Start);
49 S = StringRef(Start, End - Start);
54 static StringRef getWord(const char *&Start, const char *End) {
55 for (const char *I = Start; I != End; ++I)
57 StringRef S(Start, I - Start);
61 StringRef S(Start, End - Start);
66 static size_t splitWords(const StringRef &Line, std::vector<StringRef> &Words) {
67 const char *Start = Line.data();
68 const char *End = Line.data() + Line.size();
71 Words.push_back(getWord(Start, End));
75 static bool getNumber(const StringRef &S, uint64_t &N) {
77 for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I)
78 if (*I >= '0' && *I <= '9')
79 N = N * 10 + (*I - '0');
86 static void exitWithError(const std::string &Message,
87 const std::string &Filename, int64_t Line = -1) {
88 errs() << "error: " << Filename;
90 errs() << ":" << Line;
91 errs() << ": " << Message << "\n";
95 //===----------------------------------------------------------------------===//
96 int main(int argc, char **argv) {
97 // Print a stack trace if we signal out.
98 sys::PrintStackTraceOnErrorSignal();
99 PrettyStackTraceProgram X(argc, argv);
100 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
102 cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
104 OwningPtr<MemoryBuffer> File1;
105 OwningPtr<MemoryBuffer> File2;
106 if (error_code ec = MemoryBuffer::getFile(Filename1, File1))
107 exitWithError(ec.message(), Filename1);
108 if (error_code ec = MemoryBuffer::getFile(Filename2, File2))
109 exitWithError(ec.message(), Filename2);
111 if (OutputFilename.empty())
112 OutputFilename = "-";
114 std::string ErrorInfo;
115 raw_fd_ostream Output(OutputFilename.data(), ErrorInfo);
116 if (!ErrorInfo.empty())
117 exitWithError(ErrorInfo, OutputFilename);
119 const char *Start1 = File1->getBufferStart();
120 const char *Start2 = File2->getBufferStart();
121 const char *End1 = File1->getBufferEnd();
122 const char *End2 = File2->getBufferEnd();
123 const char *P1 = Start1;
124 const char *P2 = Start2;
126 StringRef Line1, Line2;
128 while (readLine(P1, End1, Line1)) {
130 if (!readLine(P2, End2, Line2))
131 exitWithError("truncated file", Filename2, Num);
133 std::vector<StringRef> Words1, Words2;
134 if (splitWords(Line1, Words1) != splitWords(Line2, Words2))
135 exitWithError("data mismatch", Filename2, Num);
137 if (Words1.size() > 2)
138 exitWithError("invalid data", Filename1, Num);
140 if (Words1.empty()) {
145 if (Words1.size() == 2) {
146 if (Words1[0] != Words2[0])
147 exitWithError("function name mismatch", Filename2, Num);
150 if (!getNumber(Words1[1], N1))
151 exitWithError("bad function count", Filename1, Num);
152 if (!getNumber(Words2[1], N2))
153 exitWithError("bad function count", Filename2, Num);
156 exitWithError("function count mismatch", Filename2, Num);
158 Output << Line1 << "\n";
163 if (!getNumber(Words1[0], N1))
164 exitWithError("invalid counter", Filename1, Num);
165 if (!getNumber(Words2[0], N2))
166 exitWithError("invalid counter", Filename2, Num);
168 uint64_t Sum = N1 + N2;
170 exitWithError("counter overflow", Filename2, Num);
172 Output << N1 + N2 << "\n";
174 if (readLine(P2, End2, Line2))
175 exitWithError("truncated file", Filename1, Num + 1);