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_CGDATA_OUTLINEDHASHTREERECORD_H 17 #define LLVM_CGDATA_OUTLINEDHASHTREERECORD_H 18 19 #include "llvm/CGData/OutlinedHashTree.h" 20 #include "llvm/Support/Compiler.h" 21 22 namespace llvm { 23 24 /// HashNodeStable is the serialized, stable, and compact representation 25 /// of a HashNode. 26 struct HashNodeStable { 27 llvm::yaml::Hex64 Hash; 28 unsigned Terminals; 29 std::vector<unsigned> SuccessorIds; 30 }; 31 32 using IdHashNodeStableMapTy = std::map<unsigned, HashNodeStable>; 33 using IdHashNodeMapTy = DenseMap<unsigned, HashNode *>; 34 using HashNodeIdMapTy = DenseMap<const HashNode *, unsigned>; 35 36 struct OutlinedHashTreeRecord { 37 std::unique_ptr<OutlinedHashTree> HashTree; 38 OutlinedHashTreeRecordOutlinedHashTreeRecord39 OutlinedHashTreeRecord() { HashTree = std::make_unique<OutlinedHashTree>(); } OutlinedHashTreeRecordOutlinedHashTreeRecord40 OutlinedHashTreeRecord(std::unique_ptr<OutlinedHashTree> HashTree) 41 : HashTree(std::move(HashTree)) {}; 42 43 /// Serialize the outlined hash tree to a raw_ostream. 44 LLVM_ABI void serialize(raw_ostream &OS) const; 45 /// Deserialize the outlined hash tree from a raw_ostream. 46 LLVM_ABI void deserialize(const unsigned char *&Ptr); 47 /// Serialize the outlined hash tree to a YAML stream. 48 LLVM_ABI void serializeYAML(yaml::Output &YOS) const; 49 /// Deserialize the outlined hash tree from a YAML stream. 50 LLVM_ABI void deserializeYAML(yaml::Input &YIS); 51 52 /// Merge the other outlined hash tree into this one. mergeOutlinedHashTreeRecord53 void merge(const OutlinedHashTreeRecord &Other) { 54 HashTree->merge(Other.HashTree.get()); 55 } 56 57 /// \returns true if the outlined hash tree is empty. emptyOutlinedHashTreeRecord58 bool empty() const { return HashTree->empty(); } 59 60 /// Print the outlined hash tree in a YAML format. 61 void print(raw_ostream &OS = llvm::errs()) const { 62 yaml::Output YOS(OS); 63 serializeYAML(YOS); 64 } 65 66 private: 67 /// Convert the outlined hash tree to stable data. 68 void convertToStableData(IdHashNodeStableMapTy &IdNodeStableMap) const; 69 70 /// Convert the stable data back to the outlined hash tree. 71 void convertFromStableData(const IdHashNodeStableMapTy &IdNodeStableMap); 72 }; 73 74 } // end namespace llvm 75 76 #endif // LLVM_CGDATA_OUTLINEDHASHTREERECORD_H 77