|
| 1 | +import csharp |
| 2 | + |
| 3 | +/** |
| 4 | + * INTERNAL: Do not use. |
| 5 | + * |
| 6 | + * Provides a simple SSA implementation for local scope variables. |
| 7 | + */ |
| 8 | +module BaseSsa { |
| 9 | + private import ControlFlowGraph |
| 10 | + private import AssignableDefinitions |
| 11 | + |
| 12 | + private class SimpleLocalScopeVariable extends LocalScopeVariable { |
| 13 | + SimpleLocalScopeVariable() { |
| 14 | + not exists(AssignableDefinition def1, AssignableDefinition def2 | |
| 15 | + def1.getTarget() = this and |
| 16 | + def2.getTarget() = this and |
| 17 | + def1.getEnclosingCallable() != def2.getEnclosingCallable() |
| 18 | + ) |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Holds if the `i`th node of basic block `bb` is assignable definition `def`, |
| 24 | + * targeting local scope variable `v`. |
| 25 | + */ |
| 26 | + private predicate defAt(BasicBlock bb, int i, AssignableDefinition def, SimpleLocalScopeVariable v) { |
| 27 | + bb.getNode(i) = def.getAControlFlowNode() and |
| 28 | + v = def.getTarget() and |
| 29 | + // In cases like `(x, x) = (0, 1)`, we discard the first (dead) definition of `x` |
| 30 | + not exists(TupleAssignmentDefinition first, TupleAssignmentDefinition second | |
| 31 | + first = def | |
| 32 | + second.getAssignment() = first.getAssignment() and |
| 33 | + second.getEvaluationOrder() > first.getEvaluationOrder() and |
| 34 | + second.getTarget() = v |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Holds if basic block `bb` would need to start with a phi node for local scope |
| 40 | + * variable `v` in an SSA representation. |
| 41 | + */ |
| 42 | + private predicate needsPhiNode(BasicBlock bb, SimpleLocalScopeVariable v) { |
| 43 | + exists(BasicBlock def | |
| 44 | + def.inDominanceFrontier(bb) | |
| 45 | + defAt(def, _, _, v) or |
| 46 | + needsPhiNode(def, v) |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + private newtype SsaRefKind = SsaRead() or SsaDef() |
| 51 | + |
| 52 | + /** |
| 53 | + * Holds if the `i`th node of basic block `bb` is a reference to `v`, |
| 54 | + * either a read (when `k` is `SsaRead()`) or a write including phi nodes |
| 55 | + * (when `k` is `SsaDef()`). |
| 56 | + */ |
| 57 | + private predicate ssaRef(BasicBlock bb, int i, SimpleLocalScopeVariable v, SsaRefKind k) { |
| 58 | + bb.getNode(i).getElement() = v.getAnAccess().(VariableRead) and |
| 59 | + k = SsaRead() |
| 60 | + or |
| 61 | + defAt(bb, i, _, v) and |
| 62 | + k = SsaDef() |
| 63 | + or |
| 64 | + needsPhiNode(bb, v) and |
| 65 | + i = -1 and |
| 66 | + k = SsaDef() |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Gets the (1-based) rank of the reference to `v` at the `i`th node of basic |
| 71 | + * block `bb`, which has the given reference kind `k`. |
| 72 | + */ |
| 73 | + private int ssaRefRank(BasicBlock bb, int i, SimpleLocalScopeVariable v, SsaRefKind k) { |
| 74 | + i = rank[result](int j | ssaRef(bb, j, v, _)) and |
| 75 | + ssaRef(bb, i, v, k) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Holds if definition `def` of local scope variable `v` inside basic block |
| 80 | + * `bb` reaches the reference at rank `rnk`, without passing through another |
| 81 | + * definition of `v`, including phi nodes. |
| 82 | + */ |
| 83 | + private predicate defReachesRank(BasicBlock bb, AssignableDefinition def, SimpleLocalScopeVariable v, int rnk) { |
| 84 | + exists(int i | |
| 85 | + rnk = ssaRefRank(bb, i, v, SsaDef()) | |
| 86 | + defAt(bb, i, def, v) |
| 87 | + ) |
| 88 | + or |
| 89 | + defReachesRank(bb, def, v, rnk - 1) and |
| 90 | + rnk = ssaRefRank(bb, _, v, SsaRead()) |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Holds if definition `def` of local scope variable `v` reaches the end of |
| 95 | + * basic block `bb` without passing through another definition of `v`, including |
| 96 | + * phi nodes. |
| 97 | + */ |
| 98 | + private predicate reachesEndOf(AssignableDefinition def, SimpleLocalScopeVariable v, BasicBlock bb) { |
| 99 | + exists(int rnk | |
| 100 | + defReachesRank(bb, def, v, rnk) and |
| 101 | + rnk = max(ssaRefRank(bb, _, v, _)) |
| 102 | + ) |
| 103 | + or |
| 104 | + exists(BasicBlock mid | |
| 105 | + reachesEndOf(def, v, mid) and |
| 106 | + not exists(ssaRefRank(mid, _, v, SsaDef())) and |
| 107 | + bb = mid.getASuccessor() |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets a read of the SSA definition for variable `v` at definition `def`. That is, |
| 113 | + * a read that is guaranteed to read the value assigned at definition `def`. |
| 114 | + */ |
| 115 | + cached AssignableRead getARead(AssignableDefinition def, SimpleLocalScopeVariable v) { |
| 116 | + exists(BasicBlock bb, int i, int rnk | |
| 117 | + result.getTarget() = v and |
| 118 | + result.getAControlFlowNode() = bb.getNode(i) and |
| 119 | + rnk = ssaRefRank(bb, i, v, SsaRead()) |
| 120 | + | |
| 121 | + defReachesRank(bb, def, v, rnk) |
| 122 | + or |
| 123 | + reachesEndOf(def, v, bb.getAPredecessor()) and |
| 124 | + not ssaRefRank(bb, _, v, SsaDef()) < rnk |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
0 commit comments