|
| 1 | +################################################################################ |
| 2 | +# Copyright IBM Corporation 2025 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +################################################################################ |
| 16 | + |
| 17 | +"""Stage 2 of the level-3 dataflow ladder: dominance and control dependence. |
| 18 | +
|
| 19 | +Post-dominators are computed with the Cooper–Harper–Kennedy iterative |
| 20 | +algorithm over the reverse CFG. Infinite loops are already normalized by the |
| 21 | +CFG builder (synthetic escape edge to EXIT), so the post-dominator tree always |
| 22 | +has the unique root EXIT. |
| 23 | +
|
| 24 | +Control dependence follows Ferrante–Ottenstein–Warren: for each CFG edge |
| 25 | +``(a, b)`` where ``b`` does not post-dominate ``a``, every node on the |
| 26 | +post-dominator-tree path from ``b`` up to (but not including) ``a``'s |
| 27 | +immediate post-dominator is control-dependent on ``a``. |
| 28 | +
|
| 29 | +Nodes with no branch-node control dependence are control-dependent on ENTRY — |
| 30 | +the conventional region root, which keeps every statement anchored in the PDG |
| 31 | +and gives interprocedural traversals a path from a callee's ENTRY to its |
| 32 | +unconditional statements. |
| 33 | +""" |
| 34 | + |
| 35 | +from __future__ import annotations |
| 36 | + |
| 37 | +from typing import Dict, List, Set, Tuple |
| 38 | + |
| 39 | +from codeanalyzer.dataflow.cfg import ControlFlowGraph |
| 40 | + |
| 41 | + |
| 42 | +def _postorder(adj: Dict[int, List[int]], root: int) -> List[int]: |
| 43 | + """Iterative DFS postorder over ``adj`` from ``root``.""" |
| 44 | + order: List[int] = [] |
| 45 | + visited: Set[int] = set() |
| 46 | + stack: List[Tuple[int, int]] = [(root, 0)] |
| 47 | + visited.add(root) |
| 48 | + while stack: |
| 49 | + node, i = stack.pop() |
| 50 | + children = adj.get(node, []) |
| 51 | + if i < len(children): |
| 52 | + stack.append((node, i + 1)) |
| 53 | + child = children[i] |
| 54 | + if child not in visited: |
| 55 | + visited.add(child) |
| 56 | + stack.append((child, 0)) |
| 57 | + else: |
| 58 | + order.append(node) |
| 59 | + return order |
| 60 | + |
| 61 | + |
| 62 | +def post_dominators(cfg: ControlFlowGraph) -> Dict[int, int]: |
| 63 | + """Immediate post-dominator of every node, as ``{node: ipdom}``. |
| 64 | +
|
| 65 | + EXIT is its own post-dominator (the tree root). Cooper–Harper–Kennedy |
| 66 | + ("A Simple, Fast Dominance Algorithm") run on the reverse CFG. |
| 67 | + """ |
| 68 | + # Reverse CFG: successors of n are the CFG predecessors of n. |
| 69 | + radj: Dict[int, List[int]] = {n.id: [] for n in cfg.nodes} |
| 70 | + rpred: Dict[int, List[int]] = {n.id: [] for n in cfg.nodes} |
| 71 | + for e in cfg.edges: |
| 72 | + if e.source == e.target: |
| 73 | + continue # self-loops carry no dominance information |
| 74 | + radj[e.target].append(e.source) |
| 75 | + rpred[e.source].append(e.target) |
| 76 | + |
| 77 | + root = cfg.exit_id |
| 78 | + post = _postorder(radj, root) |
| 79 | + number = {n: i for i, n in enumerate(post)} # postorder number |
| 80 | + rpo = list(reversed(post)) # reverse postorder: root first |
| 81 | + |
| 82 | + ipdom: Dict[int, int] = {root: root} |
| 83 | + |
| 84 | + def intersect(a: int, b: int) -> int: |
| 85 | + while a != b: |
| 86 | + while number[a] < number[b]: |
| 87 | + a = ipdom[a] |
| 88 | + while number[b] < number[a]: |
| 89 | + b = ipdom[b] |
| 90 | + return a |
| 91 | + |
| 92 | + changed = True |
| 93 | + while changed: |
| 94 | + changed = False |
| 95 | + for node in rpo: |
| 96 | + if node == root: |
| 97 | + continue |
| 98 | + preds = [p for p in rpred[node] if p in ipdom] |
| 99 | + if not preds: |
| 100 | + continue |
| 101 | + new = preds[0] |
| 102 | + for p in preds[1:]: |
| 103 | + new = intersect(new, p) |
| 104 | + if ipdom.get(node) != new: |
| 105 | + ipdom[node] = new |
| 106 | + changed = True |
| 107 | + |
| 108 | + return ipdom |
| 109 | + |
| 110 | + |
| 111 | +def control_dependence(cfg: ControlFlowGraph) -> List[Tuple[int, int]]: |
| 112 | + """CDG edges ``(branch_node, dependent_node)`` per Ferrante–Ottenstein– |
| 113 | + Warren, plus ENTRY-region edges for nodes with no other controller.""" |
| 114 | + ipdom = post_dominators(cfg) |
| 115 | + |
| 116 | + deps: Set[Tuple[int, int]] = set() |
| 117 | + for e in cfg.edges: |
| 118 | + a, b = e.source, e.target |
| 119 | + if a == b: |
| 120 | + continue |
| 121 | + # b post-dominates a iff b is an ancestor of a in the pdom tree. |
| 122 | + runner = b |
| 123 | + stop = ipdom.get(a) |
| 124 | + # Walk from b up the post-dominator tree to (not including) ipdom(a). |
| 125 | + while runner != stop and runner != a: |
| 126 | + deps.add((a, runner)) |
| 127 | + nxt = ipdom.get(runner) |
| 128 | + if nxt is None or nxt == runner: |
| 129 | + break |
| 130 | + runner = nxt |
| 131 | + |
| 132 | + # ENTRY as the region root for otherwise-uncontrolled nodes. |
| 133 | + controlled = {t for (_, t) in deps} |
| 134 | + for n in cfg.nodes: |
| 135 | + if n.id in (cfg.entry_id, cfg.exit_id): |
| 136 | + continue |
| 137 | + if n.id not in controlled: |
| 138 | + deps.add((cfg.entry_id, n.id)) |
| 139 | + |
| 140 | + return sorted(deps) |
0 commit comments