+// Represents the source position in GCC sample profiles.
+struct SourceInfo {
+ SourceInfo()
+ : FuncName(), DirName(), FileName(), StartLine(0), Line(0),
+ Discriminator(0) {}
+
+ SourceInfo(StringRef FuncName, StringRef DirName, StringRef FileName,
+ uint32_t StartLine, uint32_t Line, uint32_t Discriminator)
+ : FuncName(FuncName), DirName(DirName), FileName(FileName),
+ StartLine(StartLine), Line(Line), Discriminator(Discriminator) {}
+
+ bool operator<(const SourceInfo &p) const;
+
+ uint32_t Offset() const { return ((Line - StartLine) << 16) | Discriminator; }
+
+ bool Malformed() const { return Line < StartLine; }
+
+ StringRef FuncName;
+ StringRef DirName;
+ StringRef FileName;
+ uint32_t StartLine;
+ uint32_t Line;
+ uint32_t Discriminator;
+};
+
+typedef SmallVector<FunctionSamples *, 10> InlineCallStack;
+
+// Supported histogram types in GCC. Currently, we only need support for
+// call target histograms.
+enum HistType {
+ HIST_TYPE_INTERVAL,
+ HIST_TYPE_POW2,
+ HIST_TYPE_SINGLE_VALUE,
+ HIST_TYPE_CONST_DELTA,
+ HIST_TYPE_INDIR_CALL,
+ HIST_TYPE_AVERAGE,
+ HIST_TYPE_IOR,
+ HIST_TYPE_INDIR_CALL_TOPN
+};
+
+class SampleProfileReaderGCC : public SampleProfileReader {
+public:
+ SampleProfileReaderGCC(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
+ : SampleProfileReader(std::move(B), C), GcovBuffer(Buffer.get()) {}
+
+ /// \brief Read and validate the file header.
+ std::error_code readHeader() override;
+
+ /// \brief Read sample profiles from the associated file.
+ std::error_code read() override;
+
+ /// \brief Return true if \p Buffer is in the format supported by this class.
+ static bool hasFormat(const MemoryBuffer &Buffer);
+
+protected:
+ std::error_code readNameTable();
+ std::error_code readOneFunctionProfile(const InlineCallStack &InlineStack,
+ bool Update, uint32_t Offset);
+ std::error_code readFunctionProfiles();
+ std::error_code skipNextWord();
+ template <typename T> ErrorOr<T> readNumber();
+ ErrorOr<StringRef> readString();
+
+ /// \brief Read the section tag and check that it's the same as \p Expected.
+ std::error_code readSectionTag(uint32_t Expected);
+
+ /// GCOV buffer containing the profile.
+ GCOVBuffer GcovBuffer;
+
+ /// Function names in this profile.
+ std::vector<std::string> Names;
+
+ /// GCOV tags used to separate sections in the profile file.
+ static const uint32_t GCOVTagAFDOFileNames = 0xaa000000;
+ static const uint32_t GCOVTagAFDOFunction = 0xac000000;
+};
+