rust/compiler/rustc_mir_transform/src
bors 357f660729 Auto merge of #101168 - jachris:dataflow-const-prop, r=oli-obk
Add new MIR constant propagation based on dataflow analysis

The current constant propagation in `rustc_mir_transform/src/const_prop.rs` fails to handle many cases that would be expected from a constant propagation optimization. For example:
```rust
let x = if true { 0 } else { 0 };
```
This pull request adds a new constant propagation MIR optimization pass based on the existing dataflow analysis framework. Since most of the analysis is not unique to constant propagation, a generic framework has been extracted. It works on top of the existing framework and could be reused for other optimzations.

Closes #80038. Closes #81605.

## Todo
### Essential
- [x] [Writes to inactive enum variants](https://github.com/rust-lang/rust/pull/101168#pullrequestreview-1089493974). Resolved by rejecting the registration of places with downcast projections for now. Could be improved by flooding other variants if mutable access to a variant is observed.
- [X] Handle [`StatementKind::CopyNonOverlapping`](https://github.com/rust-lang/rust/pull/101168#discussion_r957774914). Resolved by flooding the destination.
- [x] Handle `UnsafeCell` / `!Freeze` correctly.
- [X] Overflow propagation of `CheckedBinaryOp`: Decided to not propagate if overflow flag is `true` (`false` will still be propagated)
- [x] More documentation in general.
- [x] Arguments for correctness, documentation of necessary assumptions.
- [x] Better performance, or alternatively, require `-Zmir-opt-level=3` for now.

### Extra
- [x]  Add explicit unreachability, i.e. upgrading the lattice from $\mathbb{P} \to \mathbb{V}$ to $\set{\bot} \cup (\mathbb{P} \to \mathbb{V})$.
- [x] Use storage statements to improve precision.
- [ ] Consider opening issue for duplicate diagnostics: https://github.com/rust-lang/rust/pull/101168#issuecomment-1276609950
- [ ] Flood moved-from places with $\bot$ (requires some changes for places with tracked projections).
- [ ] Add downcast projections back in.
- [ ] [Algebraic simplifications](https://github.com/rust-lang/rust/pull/101168#discussion_r957967878) (possibly with a shared API; done by old const prop).
- [ ] Propagation through slices / arrays.
- [ ] Find other optimizations that are done by old `const_prop.rs`, but not by this one.
2022-11-15 09:38:05 +00:00
..
coverage Remove from compiler/ crates 2022-09-29 16:49:04 +09:00
inline Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
abort_unwinding_calls.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
add_call_guards.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
add_moves_for_packed_drops.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
add_retag.rs Use the declaration's SourceInfo for FnEntry retags, not the outermost 2022-08-28 11:40:10 -04:00
check_const_item_mutation.rs Refactor rustc lint API 2022-10-01 10:03:06 +00:00
check_packed_ref.rs Refactor rustc lint API 2022-10-01 10:03:06 +00:00
check_unsafety.rs Add support for custom MIR parsing 2022-11-08 23:13:15 -08:00
cleanup_post_borrowck.rs Add option to mir::MutVisitor to not invalidate CFG. 2022-08-09 01:51:10 -07:00
const_debuginfo.rs Rename debugging_opts to unstable_opts 2022-07-13 17:47:06 -05:00
const_goto.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
const_prop.rs add is_sized method on Abi and Layout, and use it 2022-11-13 12:23:53 +01:00
const_prop_lint.rs add is_sized method on Abi and Layout, and use it 2022-11-13 12:23:53 +01:00
dataflow_const_prop.rs Disable limits if mir-opt-level >= 4 2022-11-12 20:14:34 +01:00
dead_store_elimination.rs Generalize the Assume intrinsic statement to a general Intrinsic statement 2022-09-06 14:18:32 +00:00
deaggregator.rs Refactor MIR phases 2022-08-30 01:40:14 -07:00
deduce_param_attrs.rs Accept TyCtxt instead of TyCtxtAt in Ty::is_* functions 2022-10-27 15:06:08 +04:00
deduplicate_blocks.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
deref_separator.rs Refactor MIR phases 2022-08-30 01:40:14 -07:00
dest_prop.rs Generalize the Assume intrinsic statement to a general Intrinsic statement 2022-09-06 14:18:32 +00:00
dump_mir.rs Inline on_mir_pass and add inline to dump_mir 2021-10-14 15:26:59 -05:00
early_otherwise_branch.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
elaborate_box_derefs.rs remove the Subst trait, always use EarlyBinder 2022-09-19 11:37:27 +02:00
elaborate_drops.rs Refactor MIR phases 2022-08-30 01:40:14 -07:00
ffi_unwind_calls.rs Refactor rustc lint API 2022-10-01 10:03:06 +00:00
function_item_references.rs Refactor rustc lint API 2022-10-01 10:03:06 +00:00
generator.rs remove the Subst trait, always use EarlyBinder 2022-09-19 11:37:27 +02:00
inline.rs Fix MIR inlining of asm_unwind 2022-10-07 12:59:38 +01:00
instcombine.rs Make MIR basic blocks field public 2022-07-07 08:11:49 +02:00
lib.rs Add initial version of value analysis and dataflow constant propagation 2022-11-07 10:35:08 +01:00
lower_intrinsics.rs Generalize the Assume intrinsic statement to a general Intrinsic statement 2022-09-06 14:18:32 +00:00
lower_slice_len.rs lint: add bad opt access internal lint 2022-07-27 11:24:27 +01:00
match_branches.rs Make MIR basic blocks field public 2022-07-07 08:11:49 +02:00
multiple_return_terminators.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
normalize_array_len.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
nrvo.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
pass_manager.rs Add support for custom MIR parsing 2022-11-08 23:13:15 -08:00
remove_false_edges.rs Separate RemoveFalseEdges from SimplifyBranches 2021-11-30 17:14:48 -08:00
remove_noop_landing_pads.rs Generalize the Assume intrinsic statement to a general Intrinsic statement 2022-09-06 14:18:32 +00:00
remove_storage_markers.rs Make MIR basic blocks field public 2022-07-07 08:11:49 +02:00
remove_uninit_drops.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
remove_unneeded_drops.rs Make MIR basic blocks field public 2022-07-07 08:11:49 +02:00
remove_zsts.rs Make MIR basic blocks field public 2022-07-07 08:11:49 +02:00
required_consts.rs Don't ICE when encountering ConstKind::Error in RequiredConstsVisitor 2022-11-10 05:14:04 +00:00
reveal_all.rs Add option to mir::MutVisitor to not invalidate CFG. 2022-08-09 01:51:10 -07:00
separate_const_switch.rs Generalize the Assume intrinsic statement to a general Intrinsic statement 2022-09-06 14:18:32 +00:00
shim.rs Some tracing and comment cleanups 2022-11-04 17:10:07 +00:00
simplify.rs Generalize the Assume intrinsic statement to a general Intrinsic statement 2022-09-06 14:18:32 +00:00
simplify_branches.rs Auto merge of #91279 - scottmcm:small-refactor, r=nagisa 2021-12-06 13:04:18 +00:00
simplify_comparison_integral.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
simplify_try.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
uninhabited_enum_branching.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
unreachable_prop.rs Fix typo in UnreachableProp 2022-08-23 08:18:18 +02:00