22 assert(
N &&
"Got a null node?");
26 return N->getEndIdx() -
N->getStartIdx() + 1;
31 std::optional<unsigned> BoundaryStart)
33 BoundaryStart(BoundaryStart) {
39 unsigned SuffixesToAdd = 0;
44 for (
unsigned PfxEndIdx = 0,
End =
Str.size(); PfxEndIdx <
End; PfxEndIdx++) {
46 LeafEndIdx = PfxEndIdx;
47 SuffixesToAdd = extend(PfxEndIdx, SuffixesToAdd);
57 unsigned StartIdx,
unsigned Edge) {
58 assert(StartIdx <= LeafEndIdx &&
"String can't start after it ends!");
59 auto *
N =
new (LeafNodeAllocator.Allocate())
71 unsigned StartIdx,
unsigned EndIdx,
73 assert(StartIdx <= EndIdx &&
"String can't start after it ends!");
75 "Non-root internal nodes must have parents!");
76 auto *
N =
new (InternalNodeAllocator.Allocate())
80 N->setConcatLen(Parent->
getConcatLen() + (EndIdx - StartIdx + 1));
91void SuffixTree::setLeafNodes() {
98 unsigned LeafCounter = 0;
102 DenseMap<SuffixTreeInternalNode *,
103 std::pair<SuffixTreeNode *, SuffixTreeNode *>>
107 while (!ToVisit.
empty()) {
111 auto I = ChildrenMap.find(CurrInternalNode);
112 if (
I == ChildrenMap.end()) {
117 auto J = CurrInternalNode->Children.begin();
118 if (J != CurrInternalNode->Children.end()) {
120 SuffixTreeNode *FirstChild = J->second;
121 SuffixTreeNode *LastChild =
nullptr;
122 for (; J != CurrInternalNode->Children.end(); ++J) {
123 LastChild = J->second;
126 ChildrenMap[CurrInternalNode] = {FirstChild, LastChild};
134 if (CurrInternalNode->isRoot())
138 auto [FirstChild, LastChild] =
I->second;
147 "LeftLeafIdx should not be larger than RightLeafIdx");
156 LeafNodes.push_back(CurrLeafNode);
161unsigned SuffixTree::extend(
unsigned EndIdx,
unsigned SuffixesToAdd) {
162 SuffixTreeInternalNode *NeedsLink =
nullptr;
164 while (SuffixesToAdd > 0) {
167 if (Active.Len == 0) {
172 assert(Active.Idx <= EndIdx &&
"Start index can't be after end index!");
175 unsigned FirstChar =
Str[Active.Idx];
180 if (
auto It = isBoundary(FirstChar) ? Active.Node->Children.end()
181 : Active.Node->Children.find(FirstChar);
182 It == Active.Node->Children.end()) {
186 if (!isBoundary(FirstChar) || Active.Node->getConcatLen() >= MinLength)
187 insertLeaf(*Active.Node, EndIdx, FirstChar);
192 NeedsLink->
setLink(Active.Node);
198 SuffixTreeNode *NextNode = It->second;
204 if (Active.Len >= SubstringLen) {
208 "Expected an internal node?");
209 Active.Idx += SubstringLen;
210 Active.Len -= SubstringLen;
217 unsigned LastChar =
Str[EndIdx];
224 if (NeedsLink && !Active.Node->isRoot()) {
225 NeedsLink->
setLink(Active.Node);
247 SuffixTreeInternalNode *SplitNode = insertInternalNode(
249 NextNode->
getStartIdx() + Active.Len - 1, FirstChar);
253 if (!isBoundary(LastChar) || SplitNode->
getConcatLen() >= MinLength)
254 insertLeaf(*SplitNode, EndIdx, LastChar);
267 NeedsLink = SplitNode;
274 if (Active.Node->isRoot()) {
275 if (Active.Len > 0) {
277 Active.Idx = EndIdx - SuffixesToAdd + 1;
281 Active.Node = Active.Node->getLink();
285 return SuffixesToAdd;
288void SuffixTree::RepeatedSubstringIterator::advance() {
291 RS = RepeatedSubstring();
295 SmallVector<unsigned> RepeatedSubstringStarts;
298 while (!InternalNodesToVisit.empty()) {
299 RepeatedSubstringStarts.
clear();
300 auto *Curr = InternalNodesToVisit.pop_back_val();
304 unsigned Length = Curr->getConcatLen();
308 for (
auto &ChildPair : Curr->Children)
310 if (
auto *InternalChild =
312 InternalNodesToVisit.push_back(InternalChild);
324 if (OutlinerLeafDescendants) {
325 for (
unsigned I = Curr->getLeftLeafIdx();
I <= Curr->getRightLeafIdx();
327 RepeatedSubstringStarts.
push_back(LeafNodes[
I]->getSuffixIdx());
329 for (
auto &ChildPair : Curr->Children)
331 RepeatedSubstringStarts.
push_back(Leaf->getSuffixIdx());
335 if (RepeatedSubstringStarts.
size() < 2)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
std::pair< BasicBlock *, BasicBlock * > Edge
static size_t numElementsInSubstring(const SuffixTreeNode *N)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
void push_back(const T &Elt)
LLVM_ABI SuffixTree(const ArrayRef< unsigned > &Str, bool OutlinerLeafDescendants=false, std::optional< unsigned > BoundaryStart=std::nullopt)
Construct a suffix tree from a sequence of unsigned integers.
ArrayRef< unsigned > Str
Each element is an integer representing an instruction in the module.
bool OutlinerLeafDescendants
Whether to consider leaf descendants or only leaf children.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
@ End
Run the machine verifier once, at the end of the pipeline.
DenseMap< unsigned, SuffixTreeNode * > Children
The children of this node.
void setLink(SuffixTreeInternalNode *L)
Sets Link to L. Assumes L is not null.
A node in a suffix tree which represents a substring or suffix.
LLVM_ABI void incrementStartIdx(unsigned Inc)
Advance this node's StartIdx by Inc.
LLVM_ABI unsigned getLeftLeafIdx() const
LLVM_ABI unsigned getRightLeafIdx() const
LLVM_ABI void setRightLeafIdx(unsigned Idx)
Set the index of the right most leaf node of this node to Idx.
LLVM_ABI unsigned getConcatLen() const
static const unsigned EmptyIdx
Represents an undefined index in the suffix tree.
LLVM_ABI unsigned getStartIdx() const
LLVM_ABI void setLeftLeafIdx(unsigned Idx)
Set the index of the left most leaf node of this node to Idx.