Skip to content

Commit 84c1afe

Browse files
authored
llvm-reduce: Simplify instruction reduction to avoid worklist (#133391)
1 parent 304b3c5 commit 84c1afe

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,20 @@ static bool shouldAlwaysKeep(const Instruction &I) {
3131
/// accordingly. It also removes allocations of out-of-chunk arguments.
3232
static void extractInstrFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
3333
Module &Program = WorkItem.getModule();
34-
std::vector<Instruction *> InitInstToKeep;
3534

36-
for (auto &F : Program)
35+
for (auto &F : Program) {
3736
for (auto &BB : F) {
3837
// Removing the terminator would make the block invalid. Only iterate over
3938
// instructions before the terminator.
40-
InitInstToKeep.push_back(BB.getTerminator());
41-
for (auto &Inst : make_range(BB.begin(), std::prev(BB.end()))) {
42-
if (shouldAlwaysKeep(Inst) || O.shouldKeep())
43-
InitInstToKeep.push_back(&Inst);
44-
}
45-
}
46-
47-
// We create a vector first, then convert it to a set, so that we don't have
48-
// to pay the cost of rebalancing the set frequently if the order we insert
49-
// the elements doesn't match the order they should appear inside the set.
50-
std::set<Instruction *> InstToKeep(InitInstToKeep.begin(),
51-
InitInstToKeep.end());
52-
53-
std::vector<Instruction *> InstToDelete;
54-
for (auto &F : Program)
55-
for (auto &BB : F)
56-
for (auto &Inst : BB)
57-
if (!InstToKeep.count(&Inst)) {
39+
for (auto &Inst :
40+
make_early_inc_range(make_range(BB.begin(), std::prev(BB.end())))) {
41+
if (!shouldAlwaysKeep(Inst) && !O.shouldKeep()) {
5842
Inst.replaceAllUsesWith(getDefaultValue(Inst.getType()));
59-
InstToDelete.push_back(&Inst);
43+
Inst.eraseFromParent();
6044
}
61-
62-
for (auto &I : InstToDelete)
63-
I->eraseFromParent();
45+
}
46+
}
47+
}
6448
}
6549

6650
void llvm::reduceInstructionsDeltaPass(TestRunner &Test) {

0 commit comments

Comments
 (0)