|
18 | 18 | // Operations on Stack IR.
|
19 | 19 | //
|
20 | 20 |
|
| 21 | +#include "ir/branch-utils.h" |
21 | 22 | #include "ir/iteration.h"
|
22 | 23 | #include "ir/local-graph.h"
|
23 | 24 | #include "pass.h"
|
@@ -269,17 +270,26 @@ void StackIROptimizer::local2Stack() {
|
269 | 270 | }
|
270 | 271 | }
|
271 | 272 |
|
272 |
| -// There may be unnecessary blocks we can remove: blocks |
273 |
| -// without branches to them are always ok to remove. |
274 |
| -// TODO: a branch to a block in an if body can become |
275 |
| -// a branch to that if body |
| 273 | +// There may be unnecessary blocks we can remove: blocks without arriving |
| 274 | +// branches are always ok to remove. |
| 275 | +// TODO: A branch to a block in an if body can become a branch to that if body. |
276 | 276 | void StackIROptimizer::removeUnneededBlocks() {
|
| 277 | + // First, find all branch targets. |
| 278 | + std::unordered_set<Name> targets; |
| 279 | + for (auto*& inst : insts) { |
| 280 | + if (inst) { |
| 281 | + BranchUtils::operateOnScopeNameUses( |
| 282 | + inst->origin, [&](Name& name) { targets.insert(name); }); |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + // Remove untargeted blocks. |
277 | 287 | for (auto*& inst : insts) {
|
278 | 288 | if (!inst) {
|
279 | 289 | continue;
|
280 | 290 | }
|
281 | 291 | if (auto* block = inst->origin->dynCast<Block>()) {
|
282 |
| - if (!BranchUtils::BranchSeeker::has(block, block->name)) { |
| 292 | + if (!block->name.is() || !targets.count(block->name)) { |
283 | 293 | // TODO optimize, maybe run remove-unused-names
|
284 | 294 | inst = nullptr;
|
285 | 295 | }
|
|
0 commit comments