-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SIL] Extracted SILInstructionWorklist from SILCombine. #27020
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
//===--- SILInstructionWorklist.h -------------------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// | ||
/// When visiting the instructions in a function/basic block, one often modifies | ||
/// the list of instructions to be visited. That fact introduces complexity in | ||
/// the form of ensuring that no effort is wasted moving items down in the list | ||
/// when an item is removed from it and also in the form of ensuring that no | ||
/// instructions which have been modified/deleted are visited. | ||
/// | ||
/// The SILInstructionWorklist manages that complexity. It is responsible for | ||
/// ensuring that removing an instruction is not unnecessarily expensive and | ||
/// that only valid instructions are removed from the list. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "swift/SIL/SILInstruction.h" | ||
#include "swift/SIL/SILValue.h" | ||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
|
||
namespace swift { | ||
|
||
/// Manages a list of instructions awaiting visitation. | ||
class SILInstructionWorklist final { | ||
llvm::SmallVector<SILInstruction *, 256> worklist; | ||
llvm::DenseMap<SILInstruction *, unsigned> worklistMap; | ||
StringRef loggingName; | ||
|
||
void operator=(const SILInstructionWorklist &rhs) = delete; | ||
SILInstructionWorklist(const SILInstructionWorklist &worklist) = delete; | ||
|
||
public: | ||
SILInstructionWorklist(const char *loggingName = "InstructionWorklist") | ||
: loggingName(loggingName) {} | ||
|
||
/// Returns true if the worklist is empty. | ||
bool isEmpty() const { return worklist.empty(); } | ||
|
||
/// Add the specified instruction to the worklist if it isn't already in it. | ||
void add(SILInstruction *instruction); | ||
|
||
/// If the given ValueBase is a SILInstruction add it to the worklist. | ||
void addValue(ValueBase *value) { | ||
if (auto *instruction = value->getDefiningInstruction()) | ||
add(instruction); | ||
} | ||
|
||
/// Add the given list of instructions in reverse order to the worklist. This | ||
/// routine assumes that the worklist is empty and the given list has no | ||
/// duplicates. | ||
void addInitialGroup(ArrayRef<SILInstruction *> list); | ||
|
||
// If instruction is in the worklist, remove it. | ||
void remove(SILInstruction *instruction) { | ||
auto iterator = worklistMap.find(instruction); | ||
if (iterator == worklistMap.end()) | ||
return; // Not in worklist. | ||
|
||
// Don't bother moving everything down, just null out the slot. We will | ||
// check before we process any instruction if it is null. | ||
worklist[iterator->second] = nullptr; | ||
worklistMap.erase(iterator); | ||
} | ||
|
||
/// Remove the top element from the worklist. | ||
SILInstruction *removeOne() { | ||
SILInstruction *instruction = worklist.pop_back_val(); | ||
worklistMap.erase(instruction); | ||
return instruction; | ||
} | ||
|
||
/// When an instruction has been simplified, add all of its users to the | ||
/// worklist, since additional simplifications of its users may have been | ||
/// exposed. | ||
void addUsersToWorklist(ValueBase *instruction) { | ||
for (auto *use : instruction->getUses()) | ||
add(use->getUser()); | ||
} | ||
|
||
void addUsersToWorklist(SILValue value) { | ||
for (auto *use : value->getUses()) | ||
add(use->getUser()); | ||
} | ||
|
||
/// When an instruction has been simplified, add all of its users to the | ||
/// worklist, since additional simplifications of its users may have been | ||
/// exposed. | ||
void addUsersOfAllResultsToWorklist(SILInstruction *instruction) { | ||
for (auto result : instruction->getResults()) { | ||
addUsersToWorklist(result); | ||
} | ||
} | ||
|
||
/// Check that the worklist is empty and nuke the backing store for the map if | ||
/// it is large. | ||
void zap() { | ||
assert(worklistMap.empty() && "Worklist empty, but the map is not?"); | ||
|
||
// Do an explicit clear, this shrinks the map if needed. | ||
worklistMap.clear(); | ||
} | ||
}; | ||
|
||
} // end namespace swift |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//===--- SILInstructionWorklist.cpp ---------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#define DEBUG_TYPE "sil-instruction-worklist" | ||
#include "swift/SIL/SILInstructionWorklist.h" | ||
|
||
using namespace swift; | ||
|
||
void SILInstructionWorklist::add(SILInstruction *instruction) { | ||
if (!worklistMap.insert(std::make_pair(instruction, worklist.size())).second) | ||
return; | ||
|
||
LLVM_DEBUG(llvm::dbgs() << loggingName << ": ADD: " << *instruction << '\n'); | ||
worklist.push_back(instruction); | ||
} | ||
|
||
void SILInstructionWorklist::addInitialGroup(ArrayRef<SILInstruction *> list) { | ||
assert(worklist.empty() && "worklist must be empty to add initial group"); | ||
worklist.reserve(list.size() + 16); | ||
worklistMap.reserve(list.size()); | ||
LLVM_DEBUG(llvm::dbgs() << loggingName << ": ADDING: " << list.size() | ||
<< " instrs to worklist\n"); | ||
while (!list.empty()) { | ||
SILInstruction *instruction = list.back(); | ||
list = list.slice(0, list.size() - 1); | ||
nate-chandler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
worklistMap.insert(std::make_pair(instruction, worklist.size())); | ||
worklist.push_back(instruction); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.