xref: /freebsd/contrib/llvm-project/llvm/include/llvm/CodeGenData/OutlinedHashTreeRecord.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- OutlinedHashTreeRecord.h --------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===---------------------------------------------------------------------===//
8 //
9 // This defines the OutlinedHashTreeRecord class. This class holds the outlined
10 // hash tree for both serialization and deserialization processes. It utilizes
11 // two data formats for serialization: raw binary data and YAML.
12 // These two formats can be used interchangeably.
13 //
14 //===---------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H
17 #define LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H
18 
19 #include "llvm/CodeGenData/OutlinedHashTree.h"
20 
21 namespace llvm {
22 
23 /// HashNodeStable is the serialized, stable, and compact representation
24 /// of a HashNode.
25 struct HashNodeStable {
26   llvm::yaml::Hex64 Hash;
27   unsigned Terminals;
28   std::vector<unsigned> SuccessorIds;
29 };
30 
31 using IdHashNodeStableMapTy = std::map<unsigned, HashNodeStable>;
32 using IdHashNodeMapTy = DenseMap<unsigned, HashNode *>;
33 using HashNodeIdMapTy = DenseMap<const HashNode *, unsigned>;
34 
35 struct OutlinedHashTreeRecord {
36   std::unique_ptr<OutlinedHashTree> HashTree;
37 
OutlinedHashTreeRecordOutlinedHashTreeRecord38   OutlinedHashTreeRecord() { HashTree = std::make_unique<OutlinedHashTree>(); }
OutlinedHashTreeRecordOutlinedHashTreeRecord39   OutlinedHashTreeRecord(std::unique_ptr<OutlinedHashTree> HashTree)
40       : HashTree(std::move(HashTree)) {};
41 
42   /// Serialize the outlined hash tree to a raw_ostream.
43   void serialize(raw_ostream &OS) const;
44   /// Deserialize the outlined hash tree from a raw_ostream.
45   void deserialize(const unsigned char *&Ptr);
46   /// Serialize the outlined hash tree to a YAML stream.
47   void serializeYAML(yaml::Output &YOS) const;
48   /// Deserialize the outlined hash tree from a YAML stream.
49   void deserializeYAML(yaml::Input &YIS);
50 
51   /// Merge the other outlined hash tree into this one.
mergeOutlinedHashTreeRecord52   void merge(const OutlinedHashTreeRecord &Other) {
53     HashTree->merge(Other.HashTree.get());
54   }
55 
56   /// \returns true if the outlined hash tree is empty.
emptyOutlinedHashTreeRecord57   bool empty() const { return HashTree->empty(); }
58 
59   /// Print the outlined hash tree in a YAML format.
60   void print(raw_ostream &OS = llvm::errs()) const {
61     yaml::Output YOS(OS);
62     serializeYAML(YOS);
63   }
64 
65 private:
66   /// Convert the outlined hash tree to stable data.
67   void convertToStableData(IdHashNodeStableMapTy &IdNodeStableMap) const;
68 
69   /// Convert the stable data back to the outlined hash tree.
70   void convertFromStableData(const IdHashNodeStableMapTy &IdNodeStableMap);
71 };
72 
73 } // end namespace llvm
74 
75 #endif // LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H
76