Meet operator for constant propagation: Merges information from multiple predecessors. A variable is constant only if it has the same constant value coming from all paths.
(map1: &ConstantMap, map2: &ConstantMap)
| 6 | fn meet_constants(map1: &ConstantMap, map2: &ConstantMap) -> ConstantMap { |
| 7 | let mut result = ConstantMap::default(); |
| 8 | // Consider variables present in map1 |
| 9 | for (var, const1) in map1 { |
| 10 | match map2.get(var) { |
| 11 | Some(const2) if const1 == const2 => { |
| 12 | // Same constant value on both paths |
| 13 | result.insert(var.clone(), const1.clone()); |
| 14 | } |
| 15 | _ => { |
| 16 | // Different constants or only constant on one path -> not constant after merge |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | // Variables only in map2 were already handled implicitly (won't be in result) |
| 21 | result |
| 22 | } |
| 23 | |
| 24 | pub fn analyze_constant_propagation( |
| 25 | entry_label: &String, |
| 26 | cfg: &HashMap<String, BasicBlockInfo>, |
| 27 | // Potentially add function signature here if needed for argument constants |
no outgoing calls
no test coverage detected