Skip to content

[SIL Utils] Move IndexTrieNode into its own header in Utils. NFC. #10274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions include/swift/SILOptimizer/Utils/IndexTrie.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===--- IndexTrie - Trie for a sequence of integer indices ----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_SILOPTIMIZER_UTILS_INDEXTREE_H
#define SWIFT_SILOPTIMIZER_UTILS_INDEXTREE_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>

namespace swift {

// Trie node representing a sequence of unsigned integer indices.
class IndexTrieNode {
static const unsigned RootIdx = ~0U;
unsigned Index;
llvm::SmallVector<IndexTrieNode*, 8> Children;

public:
IndexTrieNode(): Index(RootIdx) {}

explicit IndexTrieNode(unsigned V): Index(V) {}

IndexTrieNode(IndexTrieNode &) =delete;
IndexTrieNode &operator=(const IndexTrieNode&) =delete;

~IndexTrieNode() {
for (auto *N : Children)
delete N;
}

bool isRoot() const { return Index == RootIdx; }

bool isLeaf() const { return Children.empty(); }

unsigned getIndex() const { return Index; }

IndexTrieNode *getChild(unsigned Idx) {
assert(Idx != RootIdx);

auto I = std::lower_bound(Children.begin(), Children.end(), Idx,
[](IndexTrieNode *a, unsigned i) {
return a->Index < i;
});
if (I != Children.end() && (*I)->Index == Idx)
return *I;
auto *N = new IndexTrieNode(Idx);
Children.insert(I, N);
return N;
}

ArrayRef<IndexTrieNode*> getChildren() const { return Children; }
};

} // end namespace swift

#endif
45 changes: 1 addition & 44 deletions lib/SILOptimizer/Transforms/DeadObjectElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
#include "swift/SILOptimizer/Utils/IndexTrie.h"
#include "swift/SILOptimizer/Utils/Local.h"
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
Expand Down Expand Up @@ -277,50 +278,6 @@ hasUnremovableUsers(SILInstruction *AllocRef, UserList &Users) {
// NonTrivial DeadObject Elimination
//===----------------------------------------------------------------------===//

namespace {
// Trie node representing a sequence of unsigned integer indices.
class IndexTrieNode {
static const unsigned RootIdx = ~0U;
unsigned Index;
llvm::SmallVector<IndexTrieNode*, 8> Children;

public:
IndexTrieNode(): Index(RootIdx) {}

explicit IndexTrieNode(unsigned V): Index(V) {}

IndexTrieNode(IndexTrieNode &) =delete;
IndexTrieNode &operator=(const IndexTrieNode&) =delete;

~IndexTrieNode() {
for (auto *N : Children)
delete N;
}

bool isRoot() const { return Index == RootIdx; }

bool isLeaf() const { return Children.empty(); }

unsigned getIndex() const { return Index; }

IndexTrieNode *getChild(unsigned Idx) {
assert(Idx != RootIdx);

auto I = std::lower_bound(Children.begin(), Children.end(), Idx,
[](IndexTrieNode *a, unsigned i) {
return a->Index < i;
});
if (I != Children.end() && (*I)->Index == Idx)
return *I;
auto *N = new IndexTrieNode(Idx);
Children.insert(I, N);
return N;
}

ArrayRef<IndexTrieNode*> getChildren() const { return Children; }
};
} // end anonymous namespace

namespace {
/// Determine if an object is dead. Compute its original lifetime. Find the
/// lifetime endpoints reached by each store of a refcounted object into the
Expand Down