LLVM 23.0.0git
OutlinedHashTree.h
Go to the documentation of this file.
1//===- OutlinedHashTree.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 OutlinedHashTree class. It contains sequences of stable
10// hash values of instructions that have been outlined. This OutlinedHashTree
11// can be used to track the outlined instruction sequences across modules.
12//
13//===---------------------------------------------------------------------===//
14
15#ifndef LLVM_CGDATA_OUTLINEDHASHTREE_H
16#define LLVM_CGDATA_OUTLINEDHASHTREE_H
17
18#include "llvm/ADT/DenseMap.h"
23
24namespace llvm {
25
26/// A HashNode is an entry in an OutlinedHashTree, holding a hash value
27/// and a collection of Successors (other HashNodes). If a HashNode has
28/// a positive terminal value (Terminals > 0), it signifies the end of
29/// a hash sequence with that occurrence count.
30struct HashNode {
31 /// The hash value of the node.
33 /// The number of terminals in the sequence ending at this node.
34 std::optional<unsigned> Terminals;
35 /// The successors of this node.
37};
38
40
41 using EdgeCallbackFn =
42 std::function<void(const HashNode *, const HashNode *)>;
43 using NodeCallbackFn = std::function<void(const HashNode *)>;
44
45 using HashSequence = SmallVector<stable_hash>;
46 using HashSequencePair = std::pair<HashSequence, unsigned>;
47
48public:
49 /// Walks every edge and node in the OutlinedHashTree and calls CallbackEdge
50 /// for the edges and CallbackNode for the nodes with the stable_hash for
51 /// the source and the stable_hash of the sink for an edge. These generic
52 /// callbacks can be used to traverse a OutlinedHashTree for the purpose of
53 /// print debugging or serializing it.
54 LLVM_ABI void walkGraph(NodeCallbackFn CallbackNode,
55 EdgeCallbackFn CallbackEdge = nullptr,
56 bool SortedWalk = false) const;
57
58 /// Release all hash nodes except the root hash node.
59 void clear() {
60 assert(getRoot()->Hash == 0 && !getRoot()->Terminals);
61 getRoot()->Successors.clear();
62 }
63
64 /// \returns true if the hash tree has only the root node.
65 bool empty() { return size() == 1; }
66
67 /// \returns the size of a OutlinedHashTree by traversing it. If
68 /// \p GetTerminalCountOnly is true, it only counts the terminal nodes
69 /// (meaning it returns the the number of hash sequences in the
70 /// OutlinedHashTree).
71 LLVM_ABI size_t size(bool GetTerminalCountOnly = false) const;
72
73 /// \returns the depth of a OutlinedHashTree by traversing it.
74 LLVM_ABI size_t depth() const;
75
76 /// \returns the root hash node of a OutlinedHashTree.
77 const HashNode *getRoot() const { return &Root; }
78 HashNode *getRoot() { return &Root; }
79
80 /// Inserts a \p Sequence into the this tree. The last node in the sequence
81 /// will increase Terminals.
82 LLVM_ABI void insert(const HashSequencePair &SequencePair);
83
84 /// Merge a \p OtherTree into this Tree.
85 LLVM_ABI void merge(const OutlinedHashTree *OtherTree);
86
87 /// \returns the matching count if \p Sequence exists in the OutlinedHashTree.
88 LLVM_ABI std::optional<unsigned> find(const HashSequence &Sequence) const;
89
90private:
91 HashNode Root;
92};
93
94} // namespace llvm
95
96#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
void clear()
Release all hash nodes except the root hash node.
LLVM_ABI size_t depth() const
LLVM_ABI void walkGraph(NodeCallbackFn CallbackNode, EdgeCallbackFn CallbackEdge=nullptr, bool SortedWalk=false) const
Walks every edge and node in the OutlinedHashTree and calls CallbackEdge for the edges and CallbackNo...
LLVM_ABI std::optional< unsigned > find(const HashSequence &Sequence) const
const HashNode * getRoot() const
LLVM_ABI void merge(const OutlinedHashTree *OtherTree)
Merge a OtherTree into this Tree.
LLVM_ABI void insert(const HashSequencePair &SequencePair)
Inserts a Sequence into the this tree.
LLVM_ABI size_t size(bool GetTerminalCountOnly=false) const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.
uint64_t stable_hash
An opaque object representing a stable hash code.
A HashNode is an entry in an OutlinedHashTree, holding a hash value and a collection of Successors (o...
std::optional< unsigned > Terminals
The number of terminals in the sequence ending at this node.
DenseMap< stable_hash, std::unique_ptr< HashNode > > Successors
The successors of this node.
stable_hash Hash
The hash value of the node.