|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:collection'; |
| 6 | + |
| 7 | +import 'package:cfg/ir/flow_graph.dart'; |
| 8 | +import 'package:cfg/ir/instructions.dart'; |
| 9 | +import 'package:cfg/passes/pass.dart'; |
| 10 | +import 'package:cfg/passes/simplification.dart'; |
| 11 | +import 'package:cfg/utils/misc.dart'; |
| 12 | + |
| 13 | +/// Dominator-based value numbering. |
| 14 | +/// |
| 15 | +/// Can be optionally combined with simplification pass. |
| 16 | +/// |
| 17 | +/// The algorithm is described in |
| 18 | +/// Briggs, P.; Cooper, Keith D.; Simpson, L. Taylor (1997). "Value Numbering" |
| 19 | +/// (https://www.cs.tufts.edu/~nr/cs257/archive/keith-cooper/value-numbering.pdf). |
| 20 | +final class ValueNumbering extends Pass { |
| 21 | + final Simplification? simplification; |
| 22 | + |
| 23 | + ValueNumbering({this.simplification}) : super('ValueNumbering'); |
| 24 | + |
| 25 | + @override |
| 26 | + void initialize(ErrorContext errorContext, FlowGraph graph) { |
| 27 | + super.initialize(errorContext, graph); |
| 28 | + simplification?.initialize(errorContext, graph); |
| 29 | + } |
| 30 | + |
| 31 | + @override |
| 32 | + void run() { |
| 33 | + final workList = <(Block, ValueNumberingMap)>[]; |
| 34 | + workList.add((graph.entryBlock, _createMap())); |
| 35 | + |
| 36 | + while (workList.isNotEmpty) { |
| 37 | + final (block, map) = workList.removeLast(); |
| 38 | + currentBlock = block; |
| 39 | + |
| 40 | + for (var instr in block) { |
| 41 | + currentInstruction = instr; |
| 42 | + // Apply simplification first. |
| 43 | + // It can return the same instruction, previously seen |
| 44 | + // instruction or a new instruction. |
| 45 | + final simplification = this.simplification; |
| 46 | + if (simplification != null) { |
| 47 | + instr = simplification.simplify(instr); |
| 48 | + } |
| 49 | + if (!instr.isIdempotent) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + final replacement = map[instr]; |
| 53 | + if (replacement == null) { |
| 54 | + assert(instr.block == block); |
| 55 | + map[instr] = instr; |
| 56 | + } else if (replacement != instr) { |
| 57 | + assert(replacement.isIdempotent); |
| 58 | + if (instr is Definition) { |
| 59 | + instr.replaceUsesWith(replacement as Definition); |
| 60 | + } |
| 61 | + instr.removeFromGraph(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + for (int i = 0, n = block.dominatedBlocks.length; i < n; ++i) { |
| 66 | + // Reuse map for the last dominated block. |
| 67 | + var newMap = map; |
| 68 | + if (i != n - 1) { |
| 69 | + newMap = _createMap(); |
| 70 | + newMap.addAll(map); |
| 71 | + } |
| 72 | + workList.add((block.dominatedBlocks[i], newMap)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + graph.invalidateInstructionNumbering(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Maps idempotent instructions to their first occurrence. |
| 81 | +/// |
| 82 | +/// Unordered [HashMap] is used as these maps are not iterated |
| 83 | +/// (only looked up and copied). |
| 84 | +typedef ValueNumberingMap = HashMap<Instruction, Instruction>; |
| 85 | + |
| 86 | +ValueNumberingMap _createMap() => ValueNumberingMap( |
| 87 | + equals: _instructionEquals, |
| 88 | + hashCode: _instructionHashCode, |
| 89 | +); |
| 90 | + |
| 91 | +bool _instructionEquals(Instruction a, Instruction b) { |
| 92 | + assert(a.isIdempotent); |
| 93 | + assert(b.isIdempotent); |
| 94 | + if (a.runtimeType != b.runtimeType) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + if (a.inputCount != b.inputCount) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + for (int i = 0, n = a.inputCount; i < n; ++i) { |
| 101 | + if (a.inputDefAt(i) != b.inputDefAt(i)) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + return a.attributesEqual(b); |
| 106 | +} |
| 107 | + |
| 108 | +int _instructionHashCode(Instruction instr) { |
| 109 | + assert(instr.isIdempotent); |
| 110 | + var hash = instr.runtimeType.hashCode; |
| 111 | + for (int i = 0, n = instr.inputCount; i < n; ++i) { |
| 112 | + hash = combineHash(hash, instr.inputDefAt(i).id); |
| 113 | + } |
| 114 | + return finalizeHash(hash); |
| 115 | +} |
0 commit comments