Skip to content

Commit 4c9cd62

Browse files
committed
[sil-optimizer] Allow for clients of SILInstructionWorklist to add operands to the worklist without deleting instructions.
This is needed to allow for clients to compose InstModCallbacks with SILInstructionWorklist callbacks.
1 parent 841f761 commit 4c9cd62

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

include/swift/SIL/SILInstructionWorklist.h

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
128128
}
129129
}
130130

131+
/// All operands of \p instruction to the worklist when performing 2 stage
132+
/// instruction deletion. Meant to be used right before deleting an
133+
/// instruction in callbacks like InstModCallback::onNotifyWillBeDeleted().
134+
void addOperandsToWorklist(SILInstruction &instruction) {
135+
assert(!instruction.hasUsesOfAnyResult() &&
136+
"Cannot erase instruction that is used!");
137+
138+
// Make sure that we reprocess all operands now that we reduced their
139+
// use counts.
140+
if (instruction.getNumOperands() < 8) {
141+
for (auto &operand : instruction.getAllOperands()) {
142+
if (auto *operandInstruction =
143+
operand.get()->getDefiningInstruction()) {
144+
withDebugStream([&](llvm::raw_ostream &stream,
145+
StringRef loggingName) {
146+
stream << loggingName << ": add op " << *operandInstruction << '\n'
147+
<< " from erased inst to worklist\n";
148+
});
149+
add(operandInstruction);
150+
}
151+
}
152+
}
153+
}
154+
131155
/// When an instruction has been simplified, add all of its users to the
132156
/// worklist, since additional simplifications of its users may have been
133157
/// exposed.
@@ -296,29 +320,15 @@ class SILInstructionWorklist : SILInstructionWorklistBase {
296320
}
297321

298322
void eraseSingleInstFromFunction(SILInstruction &instruction,
299-
bool addOperandsToWorklist) {
323+
bool shouldAddOperandsToWorklist) {
300324
withDebugStream([&](llvm::raw_ostream &stream, StringRef loggingName) {
301325
stream << loggingName << ": ERASE " << instruction << '\n';
302326
});
303327

304-
assert(!instruction.hasUsesOfAnyResult() &&
305-
"Cannot erase instruction that is used!");
328+
// If we are asked to add operands to the worklist, do so now.
329+
if (shouldAddOperandsToWorklist)
330+
addOperandsToWorklist(instruction);
306331

307-
// Make sure that we reprocess all operands now that we reduced their
308-
// use counts.
309-
if (instruction.getNumOperands() < 8 && addOperandsToWorklist) {
310-
for (auto &operand : instruction.getAllOperands()) {
311-
if (auto *operandInstruction =
312-
operand.get()->getDefiningInstruction()) {
313-
withDebugStream([&](llvm::raw_ostream &stream,
314-
StringRef loggingName) {
315-
stream << loggingName << ": add op " << *operandInstruction << '\n'
316-
<< " from erased inst to worklist\n";
317-
});
318-
add(operandInstruction);
319-
}
320-
}
321-
}
322332
erase(&instruction);
323333
instruction.eraseFromParent();
324334
}

0 commit comments

Comments
 (0)