Skip to content

Commit cc11e8b

Browse files
authored
Merge pull request #10274 from devincoughlin/index-trie
2 parents d48881b + 2896d5b commit cc11e8b

File tree

2 files changed

+67
-44
lines changed

2 files changed

+67
-44
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===--- IndexTrie - Trie for a sequence of integer indices ----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SILOPTIMIZER_UTILS_INDEXTREE_H
14+
#define SWIFT_SILOPTIMIZER_UTILS_INDEXTREE_H
15+
16+
#include "llvm/ADT/ArrayRef.h"
17+
#include "llvm/ADT/SmallVector.h"
18+
#include <algorithm>
19+
20+
namespace swift {
21+
22+
// Trie node representing a sequence of unsigned integer indices.
23+
class IndexTrieNode {
24+
static const unsigned RootIdx = ~0U;
25+
unsigned Index;
26+
llvm::SmallVector<IndexTrieNode*, 8> Children;
27+
28+
public:
29+
IndexTrieNode(): Index(RootIdx) {}
30+
31+
explicit IndexTrieNode(unsigned V): Index(V) {}
32+
33+
IndexTrieNode(IndexTrieNode &) =delete;
34+
IndexTrieNode &operator=(const IndexTrieNode&) =delete;
35+
36+
~IndexTrieNode() {
37+
for (auto *N : Children)
38+
delete N;
39+
}
40+
41+
bool isRoot() const { return Index == RootIdx; }
42+
43+
bool isLeaf() const { return Children.empty(); }
44+
45+
unsigned getIndex() const { return Index; }
46+
47+
IndexTrieNode *getChild(unsigned Idx) {
48+
assert(Idx != RootIdx);
49+
50+
auto I = std::lower_bound(Children.begin(), Children.end(), Idx,
51+
[](IndexTrieNode *a, unsigned i) {
52+
return a->Index < i;
53+
});
54+
if (I != Children.end() && (*I)->Index == Idx)
55+
return *I;
56+
auto *N = new IndexTrieNode(Idx);
57+
Children.insert(I, N);
58+
return N;
59+
}
60+
61+
ArrayRef<IndexTrieNode*> getChildren() const { return Children; }
62+
};
63+
64+
} // end namespace swift
65+
66+
#endif

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "swift/SIL/DebugUtils.h"
3737
#include "swift/SIL/InstructionUtils.h"
3838
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
39+
#include "swift/SILOptimizer/Utils/IndexTrie.h"
3940
#include "swift/SILOptimizer/Utils/Local.h"
4041
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
4142
#include "swift/SILOptimizer/PassManager/Transforms.h"
@@ -277,50 +278,6 @@ hasUnremovableUsers(SILInstruction *AllocRef, UserList &Users) {
277278
// NonTrivial DeadObject Elimination
278279
//===----------------------------------------------------------------------===//
279280

280-
namespace {
281-
// Trie node representing a sequence of unsigned integer indices.
282-
class IndexTrieNode {
283-
static const unsigned RootIdx = ~0U;
284-
unsigned Index;
285-
llvm::SmallVector<IndexTrieNode*, 8> Children;
286-
287-
public:
288-
IndexTrieNode(): Index(RootIdx) {}
289-
290-
explicit IndexTrieNode(unsigned V): Index(V) {}
291-
292-
IndexTrieNode(IndexTrieNode &) =delete;
293-
IndexTrieNode &operator=(const IndexTrieNode&) =delete;
294-
295-
~IndexTrieNode() {
296-
for (auto *N : Children)
297-
delete N;
298-
}
299-
300-
bool isRoot() const { return Index == RootIdx; }
301-
302-
bool isLeaf() const { return Children.empty(); }
303-
304-
unsigned getIndex() const { return Index; }
305-
306-
IndexTrieNode *getChild(unsigned Idx) {
307-
assert(Idx != RootIdx);
308-
309-
auto I = std::lower_bound(Children.begin(), Children.end(), Idx,
310-
[](IndexTrieNode *a, unsigned i) {
311-
return a->Index < i;
312-
});
313-
if (I != Children.end() && (*I)->Index == Idx)
314-
return *I;
315-
auto *N = new IndexTrieNode(Idx);
316-
Children.insert(I, N);
317-
return N;
318-
}
319-
320-
ArrayRef<IndexTrieNode*> getChildren() const { return Children; }
321-
};
322-
} // end anonymous namespace
323-
324281
namespace {
325282
/// Determine if an object is dead. Compute its original lifetime. Find the
326283
/// lifetime endpoints reached by each store of a refcounted object into the

0 commit comments

Comments
 (0)