|
1 | 1 | # Liveness analysis |
2 | 2 |
|
3 | | -**These rules are not yet described.** |
| 3 | +The role of the liveness computation is to figure out, for each cfg node, |
| 4 | +which variables may be accessed at some point in the future. We also |
| 5 | +distinguish between variables that may be accessed in general and those that |
| 6 | +may only be dropped. This is because a "full access" may potentially |
| 7 | +dereference any reference found in the variable, whereas a drop is more |
| 8 | +limited in its effects. |
| 9 | + |
| 10 | +One interesting wrinkle around drops is that we also need to consider the |
| 11 | +initialization state of each variable. This is because `Drop` statements can |
| 12 | +be added for variables which are never initialized, or whose values have |
| 13 | +been moved. Such statements are considered no-ops in MIR. |
| 14 | + |
| 15 | +## Inputs |
| 16 | + |
| 17 | +#### `var_used_at` |
| 18 | + |
| 19 | +Variable is used at the given CFG node |
| 20 | + |
| 21 | + .decl var_used_at(variable: Var, node: Node) |
| 22 | + .input var_used_at |
| 23 | + |
| 24 | +#### `var_defined_at` |
| 25 | + |
| 26 | +Variable is defined (overwritten) at the given CFG node |
| 27 | + |
| 28 | + .decl var_defined_at(variable: Var, node: Node) |
| 29 | + .input var_defined_at |
| 30 | + |
| 31 | +#### `var_dropped_at` |
| 32 | + |
| 33 | +Variable is dropped at this cfg node |
| 34 | + |
| 35 | + .decl var_dropped_at(variable: Var, node: Node) |
| 36 | + .input var_dropped_at |
| 37 | + |
| 38 | +#### `use_of_var_derefs_origin` |
| 39 | + |
| 40 | +References with the given origin may be |
| 41 | +dereferenced when the variable is used. |
| 42 | + |
| 43 | +In rustc, we generate this whenever the |
| 44 | +type of the variable includes the given |
| 45 | +origin. |
| 46 | + |
| 47 | + .decl use_of_var_derefs_origin(variable: Var, origin: Origin) |
| 48 | + .input use_of_var_derefs_origin |
| 49 | + |
| 50 | +References with the given origin may be |
| 51 | +dereferenced when the variable is dropped |
| 52 | + |
| 53 | +#### `drop_of_var_derefs_origin` |
| 54 | + |
| 55 | +In rustc, we generate this by examining the type |
| 56 | +and taking into account various |
| 57 | +unstable attributes. It is always a subset |
| 58 | +of `use_of_var_derefs_origin`. |
| 59 | + |
| 60 | + .decl drop_of_var_derefs_origin(variable: Var, origin: Origin) |
| 61 | + .input drop_of_var_derefs_origin |
| 62 | + |
| 63 | +## Relations |
| 64 | + |
| 65 | +#### `var_live_on_entry` |
| 66 | + |
| 67 | +Variables that are live on entry. |
| 68 | + |
| 69 | + .decl var_live_on_entry(var: Var, node: Node) |
| 70 | + |
| 71 | + var_live_on_entry(var, node) :- |
| 72 | + var_used_at(var, node). |
| 73 | + |
| 74 | + var_live_on_entry(var, sourceNode) :- |
| 75 | + var_live_on_entry(var, targetNode), |
| 76 | + cfg_edge(sourceNode, targetNode), |
| 77 | + !var_defined_at(var, sourceNode). |
| 78 | + |
| 79 | +#### `var_drop_live_on_entry` |
| 80 | + |
| 81 | +Variables that are "drop live" on entry. |
| 82 | + |
| 83 | +The initial rule is that, when a variable is dropped, that makes it |
| 84 | +drop-live -- unless we know that the variable is fully uninitialized, in |
| 85 | +which case the drop is a no-op. |
| 86 | + |
| 87 | +**Optimization:** In rustc, we compute drop-live only up to the point where |
| 88 | +something becomes "use-live". We could do the same here by adding some `!` |
| 89 | +checks against `var_live_on_entry`, though it would require stratification |
| 90 | +in the datalog (not a problem). |
| 91 | + |
| 92 | + .decl var_drop_live_on_entry(var: Var, node: Node) |
| 93 | + |
| 94 | + var_drop_live_on_entry(var, targetNode) :- |
| 95 | + var_dropped_at(var, targetNode), |
| 96 | + cfg_edge(sourceNode, targetNode), |
| 97 | + var_maybe_partly_initialized_on_exit(var, sourceNode). |
| 98 | + |
| 99 | + var_drop_live_on_entry(var, sourceNode) :- |
| 100 | + var_drop_live_on_entry(var, targetNode), |
| 101 | + cfg_edge(sourceNode, targetNode), |
| 102 | + !var_defined_at(var, sourceNode), |
| 103 | + var_maybe_partly_initialized_on_exit(var, sourceNode). |
| 104 | + |
| 105 | +#### `origin_live_on_entry` |
| 106 | + |
| 107 | +An origin is live at the node N if some reference with that origin may be |
| 108 | +dereferenced in the future. |
| 109 | + |
| 110 | + .decl origin_live_on_entry(origin: Origin, node: Node) |
| 111 | + |
| 112 | + origin_live_on_entry(origin, node) :- |
| 113 | + var_live_on_entry(var, node), |
| 114 | + use_of_var_derefs_origin(var, origin). |
| 115 | + |
| 116 | + origin_live_on_entry(origin, node) :- |
| 117 | + var_drop_live_on_entry(var, node), |
| 118 | + drop_of_var_derefs_origin(var, origin). |
| 119 | + |
0 commit comments