1 //=-- InstrProfWriter.h - Instrumented profiling writer -----------*- C++ -*-=// 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 contains support for writing profiling data for instrumentation 11 // based PGO and coverage. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H 16 #define LLVM_PROFILEDATA_INSTRPROFWRITER_H 17 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ProfileData/InstrProf.h" 20 #include "llvm/Support/DataTypes.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include "llvm/Support/raw_ostream.h" 23 24 namespace llvm { 25 26 /// Writer for instrumentation based profile data. 27 class InstrProfWriter { 28 public: 29 typedef SmallDenseMap<uint64_t, InstrProfRecord, 1> ProfilingData; 30 31 private: 32 InstrProfStringTable StringTable; 33 StringMap<ProfilingData> FunctionData; 34 uint64_t MaxFunctionCount; 35 public: 36 InstrProfWriter() : MaxFunctionCount(0) {} 37 38 /// Update string entries in profile data with references to StringTable. 39 void updateStringTableReferences(InstrProfRecord &I); 40 /// Add function counts for the given function. If there are already counts 41 /// for this function and the hash and number of counts match, each counter is 42 /// summed. 43 std::error_code addRecord(InstrProfRecord &&I); 44 /// Write the profile to \c OS 45 void write(raw_fd_ostream &OS); 46 /// Write the profile, returning the raw data. For testing. 47 std::unique_ptr<MemoryBuffer> writeBuffer(); 48 49 // Internal interface for testing purpose only. 50 void setValueProfDataEndianness(support::endianness Endianness); 51 52 private: 53 std::pair<uint64_t, uint64_t> writeImpl(raw_ostream &OS); 54 }; 55 56 } // end namespace llvm 57 58 #endif 59