Description
As discussed in #45043, we sometimes want to have false edges in the MIR that are used by borrowck and other conservative safety analyses, but which are optimized away when we actually generate code. The current hack is to e.g. replace a GOTO X
terminator with something like IF TRUE { X } ELSE { Y }
. This works (and should be optimized away), but it's not that flexible, and not that obvious.
It'd be nice to have something more first class. This could look like a vector of false_edges: Vec<BasicBlock>
as part of BasicBlockData
(that would get cleared after analysis), but that's a bit unfortunate since in that case they can be easily overlooked, since the code now has to remember to look not only at the terminator but also elsewhere.
On Gitter, we were thinking that a nice approach might be to add a new TerminatorKind
variant named something like FalseEdges
. This could even wrap the "true" terminator and then add additional edges:
enum TerminatorKind {
...
FalseEdges(Box<FalseEdgesData>) // introducing box here keeps total size down
}
struct FalseEdgesData {
true_terminator: TerminatorKind,
false_edges: Vec<BasicBlock>
}
This would then be replaced with the true_terminator
by the terminator simplification code.
For now, places that could benefit from this are marked with FIXME.
The steps to make this change are roughly:
- Introduce the variant listed above. Get things to compile. Reach out to myself or others on gitter if weird things arise in the process.
- Modify the terminator simplification code to replace
FalseEdges
with the underlying true terminator. - Replace the existing code using hacky false edges with the new stuff.
- Huzzah!