Skip to content

Fix a logic error in eraseUseOfValue. #2077

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
Apr 14, 2016
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
7 changes: 5 additions & 2 deletions include/swift/SILOptimizer/Utils/Local.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ bool isInstructionTriviallyDead(SILInstruction *I);
bool isIntermediateRelease(SILInstruction *I,
ConsumedArgToEpilogueReleaseMatcher &ERM);

/// \brief Recursively collect all the uses and transistive uses of the
/// instruction.
void collectUsesOfValue(SILValue V, llvm::DenseSet<SILInstruction *> &Insts);

/// \brief Recursively erase all of the uses of the instruction (but not the
/// instruction itself) and delete instructions that will become trivially
/// dead when this instruction is removed.
/// instruction itself)
void eraseUsesOfInstruction(
SILInstruction *Inst,
std::function<void(SILInstruction *)> C = [](SILInstruction *){});
Expand Down
42 changes: 22 additions & 20 deletions lib/SILOptimizer/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,29 +207,31 @@ void swift::eraseUsesOfInstruction(SILInstruction *Inst,
}
}

void swift::eraseUsesOfValue(SILValue V) {
for (auto UI = V->use_begin(), E = V->use_end(); UI != E;) {
void
swift::collectUsesOfValue(SILValue V, llvm::DenseSet<SILInstruction *> &Insts) {
for (auto UI = V->use_begin(), E = V->use_end(); UI != E; UI++) {
auto *User = UI->getUser();
UI++;

// If the instruction itself has any uses, recursively zap them so that
// nothing uses this instruction.
eraseUsesOfValue(User);
// Instruction has been processed.
if (Insts.find(User) != Insts.end())
continue;

// Walk through the operand list and delete any random instructions that
// will become trivially dead when this instruction is removed.
for (auto &Op : User->getAllOperands()) {
if (auto *OpI = dyn_cast<SILInstruction>(Op.get())) {
// Don't recursively delete the pointer we're getting in.
if (Op.get() != V) {
Op.drop();
recursivelyDeleteTriviallyDeadInstructions(OpI, false,
[](SILInstruction *){});
}
}
}
// Collect the users of this instruction.
Insts.insert(User);
collectUsesOfValue(User, Insts);
}
}

User->eraseFromParent();
void swift::eraseUsesOfValue(SILValue V) {
llvm::DenseSet<SILInstruction *> Insts;
// Collect the uses.
collectUsesOfValue(V, Insts);
// Erase the uses, we can have instructions that become dead because
// of the removal of these instructions, leave to DCE to cleanup.
// Its not safe to do recursively delete here as some of the SILInstruction
// maybe tracked by this set.
for (auto I : Insts) {
I->replaceAllUsesWithUndef();
I->eraseFromParent();
}
}

Expand Down