From 78ecca4c1999375875f8cc53cafbce2029b8f8a0 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Fri, 28 Mar 2025 14:38:56 +0700 Subject: [PATCH] llvm-reduce: Simplify instruction reduction to avoid worklist --- .../llvm-reduce/deltas/ReduceInstructions.cpp | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp b/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp index 9917fed000b7a..e1b7924594b5e 100644 --- a/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp +++ b/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp @@ -31,36 +31,20 @@ static bool shouldAlwaysKeep(const Instruction &I) { /// accordingly. It also removes allocations of out-of-chunk arguments. static void extractInstrFromModule(Oracle &O, ReducerWorkItem &WorkItem) { Module &Program = WorkItem.getModule(); - std::vector InitInstToKeep; - for (auto &F : Program) + for (auto &F : Program) { for (auto &BB : F) { // Removing the terminator would make the block invalid. Only iterate over // instructions before the terminator. - InitInstToKeep.push_back(BB.getTerminator()); - for (auto &Inst : make_range(BB.begin(), std::prev(BB.end()))) { - if (shouldAlwaysKeep(Inst) || O.shouldKeep()) - InitInstToKeep.push_back(&Inst); - } - } - - // We create a vector first, then convert it to a set, so that we don't have - // to pay the cost of rebalancing the set frequently if the order we insert - // the elements doesn't match the order they should appear inside the set. - std::set InstToKeep(InitInstToKeep.begin(), - InitInstToKeep.end()); - - std::vector InstToDelete; - for (auto &F : Program) - for (auto &BB : F) - for (auto &Inst : BB) - if (!InstToKeep.count(&Inst)) { + for (auto &Inst : + make_early_inc_range(make_range(BB.begin(), std::prev(BB.end())))) { + if (!shouldAlwaysKeep(Inst) && !O.shouldKeep()) { Inst.replaceAllUsesWith(getDefaultValue(Inst.getType())); - InstToDelete.push_back(&Inst); + Inst.eraseFromParent(); } - - for (auto &I : InstToDelete) - I->eraseFromParent(); + } + } + } } void llvm::reduceInstructionsDeltaPass(TestRunner &Test) {