| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:cfg/ir/exceptional_dependencies.dart'; | |
| import 'package:cfg/ir/instructions.dart'; | |
| import 'package:cfg/ir/ir_to_text.dart'; | |
| import 'package:cfg/passes/pass.dart'; | |
| import 'package:cfg/utils/bit_vector.dart'; | |
| /// Classic mark-and-sweep dead code elimination. | |
| /// | |
| /// Instructions which cannot be eliminated (see | |
| /// [Instruction.canBeEliminated]) are marked live first. | |
| /// Liveness is then propagated backwards from live instructions to their | |
| /// inputs. All instructions which are not marked live are removed. | |
| /// | |
| /// Unused [Parameter] instructions are also removed (including function | |
| /// parameters, receiver, closure, type parameters, exception and | |
| /// stack trace parameters). Back-ends should not rely on the presence | |
| /// of [Parameter] instructions. | |
| /// | |
| /// Unlike elimination of instructions without uses, this algorithm | |
| /// also removes dead cycles (e.g. [Phi] instructions which only use | |
| /// each other). | |
| /// | |
| /// [ExceptionalInput] instructions are not live by themselves: | |
| /// [ExceptionalInput] is live only if there is a live catch block | |
| /// [Parameter] for the same variable in one of the enclosing exception | |
| /// handlers. | |
| /// | |
| /// This pass does not change control flow. | |
| final class DeadCodeElimination extends Pass { | |
| DeadCodeElimination() : super('DeadCodeElimination'); | |
| /// Live instructions, indexed by [Instruction.id]. | |
| late final BitVector _live; | |
| /// Live instructions which inputs are not processed yet. | |
| final List<Instruction> _workList = []; | |
| /// Dependencies between [ExceptionalInput] instructions and catch block parameters. | |
| final _exceptionalDependencies = ExceptionalDependencies(); | |
| @override | |
| void run() { | |
| _live = BitVector(graph.instructions.length); | |
| _markLive(); | |
| _removeDeadInstructions(); | |
| } | |
| void _markLive() { | |
| for (final block in graph.preorder) { | |
| super.currentBlock = block; | |
| for (final instr in block) { | |
| switch (instr) { | |
| case ExceptionalInput(): | |
| // Liveness of ExceptionalInput instructions depends on | |
| // liveness of the corresponding catch block parameters. | |
| _exceptionalDependencies.addExceptionalInput(instr); | |
| continue; | |
| case Parameter(): | |
| _exceptionalDependencies.addParameter(instr); | |
| } | |
| if (!instr.canBeEliminated) { | |
| _markInstructionLive(instr); | |
| } else { | |
| // Instructions which do not yield a value never have uses, | |
| // so they would be removed unconditionally. | |
| assert( | |
| instr is Definition, | |
| 'Unexpected removable instruction without result: ' | |
| '${IrToText.instruction(instr)}', | |
| ); | |
| } | |
| } | |
| } | |
| super.currentBlock = null; | |
| _propagateLiveness(); | |
| } | |
| void _markInstructionLive(Instruction instr) { | |
| final id = instr.id; | |
| if (!_live[id]) { | |
| _live.add(id); | |
| _workList.add(instr); | |
| } | |
| } | |
| /// Mark inputs of live instructions live until a fixed point is reached. | |
| /// | |
| /// [ExceptionalInput] instructions are treated as implicit inputs | |
| /// of the corresponding catch block parameters. | |
| void _propagateLiveness() { | |
| while (_workList.isNotEmpty) { | |
| final instr = _workList.removeLast(); | |
| super.currentInstruction = instr; | |
| for (int i = 0, n = instr.inputCount; i < n; ++i) { | |
| _markInstructionLive(instr.inputDefAt(i)); | |
| } | |
| for (int i = 0, n = instr.implicitInputCount; i < n; ++i) { | |
| _markInstructionLive(instr.implicitInputDefAt(i)); | |
| } | |
| if (instr is Parameter && instr.hasExceptionalInputs) { | |
| for (final ei in _exceptionalDependencies.exceptionalInputsOf(instr)) { | |
| _markInstructionLive(ei); | |
| } | |
| } | |
| } | |
| super.currentInstruction = null; | |
| } | |
| /// Remove all instructions which are not marked live. | |
| /// | |
| /// Blocks are processed in postorder and instructions within each block | |
| /// are processed backwards. As [Constant] instructions are located | |
| /// at the beginning of the entry block (which is the last block in | |
| /// postorder) and have no inputs, all dead users of a dead [Constant] | |
| /// are removed before the [Constant] itself. This is required by | |
| /// [FlowGraph.removeConstant]. | |
| void _removeDeadInstructions() { | |
| var changed = false; | |
| for (final block in graph.postorder) { | |
| super.currentBlock = block; | |
| for (final instr in block.reversed) { | |
| if (!_live[instr.id]) { | |
| if (instr is Constant) { | |
| graph.removeConstant(instr); | |
| } | |
| instr.removeFromGraph(); | |
| changed = true; | |
| } | |
| } | |
| } | |
| super.currentBlock = null; | |
| if (changed) { | |
| graph.invalidateInstructionNumbering(); | |
| } | |
| } | |
| } |