[PGO] Add --text option for llvm-profdata show|merge commands
[oota-llvm.git] / lib / ProfileData / InstrProfWriter.cpp
index cb123e29982fd56737a36d16041c60efd80e6688..1c97b1ad0d39fc501aca23eea8ce9ea8e076ddae 100644 (file)
@@ -16,6 +16,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/EndianStream.h"
 #include "llvm/Support/OnDiskHashTable.h"
+#include <tuple>
 
 using namespace llvm;
 
@@ -87,32 +88,6 @@ public:
 };
 }
 
-static std::error_code combineInstrProfRecords(InstrProfRecord &Dest,
-                                               InstrProfRecord &Source,
-                                               uint64_t &MaxFunctionCount) {
-  // If the number of counters doesn't match we either have bad data
-  // or a hash collision.
-  if (Dest.Counts.size() != Source.Counts.size())
-    return instrprof_error::count_mismatch;
-
-  for (size_t I = 0, E = Source.Counts.size(); I < E; ++I) {
-    if (Dest.Counts[I] + Source.Counts[I] < Dest.Counts[I])
-      return instrprof_error::counter_overflow;
-    Dest.Counts[I] += Source.Counts[I];
-  }
-
-  for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
-    if (std::error_code EC = Dest.mergeValueProfData(Kind, Source))
-      return EC;
-  }
-
-  // We keep track of the max function count as we go for simplicity.
-  if (Dest.Counts[0] > MaxFunctionCount)
-    MaxFunctionCount = Dest.Counts[0];
-
-  return instrprof_error::success;
-}
-
 // Internal interface for testing purpose only.
 void InstrProfWriter::setValueProfDataEndianness(
     support::endianness Endianness) {
@@ -127,19 +102,27 @@ std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I) {
   updateStringTableReferences(I);
   auto &ProfileDataMap = FunctionData[I.Name];
 
-  auto Where = ProfileDataMap.find(I.Hash);
-  if (Where == ProfileDataMap.end()) {
+  bool NewFunc;
+  ProfilingData::iterator Where;
+  std::tie(Where, NewFunc) =
+      ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
+  InstrProfRecord &Dest = Where->second;
+  if (NewFunc) {
     // We've never seen a function with this name and hash, add it.
-    ProfileDataMap[I.Hash] = I;
-
-    // We keep track of the max function count as we go for simplicity.
-    if (I.Counts[0] > MaxFunctionCount)
-      MaxFunctionCount = I.Counts[0];
-    return instrprof_error::success;
+    Dest = std::move(I);
+  } else {
+    // We're updating a function we've seen before.
+    instrprof_error MergeResult = Dest.merge(I);
+    if (MergeResult != instrprof_error::success) {
+      return MergeResult;
+    }
   }
 
-  // We're updating a function we've seen before.
-  return combineInstrProfRecords(Where->second, I, MaxFunctionCount);
+  // We keep track of the max function count as we go for simplicity.
+  if (Dest.Counts[0] > MaxFunctionCount)
+    MaxFunctionCount = Dest.Counts[0];
+
+  return instrprof_error::success;
 }
 
 std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
@@ -188,6 +171,21 @@ void InstrProfWriter::write(raw_fd_ostream &OS) {
   endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
 }
 
+void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
+                                        raw_fd_ostream &OS) {
+  OS << Func.Name << "\n" << Func.Hash << "\n" << Func.Counts.size() << "\n";
+  for (uint64_t Count : Func.Counts)
+    OS << Count << "\n";
+
+  OS << "\n";
+}
+
+void InstrProfWriter::writeText(raw_fd_ostream &OS) {
+  for (const auto &I : FunctionData)
+    for (const auto &Func : I.getValue())
+      writeRecordInText(Func.second, OS);
+}
+
 std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
   std::string Data;
   llvm::raw_string_ostream OS(Data);