rust/compiler
bors 70f74719a9 Auto merge of #85646 - Moxinilian:separate-const-switch, r=cjgillot
MIR opt: separate constant predecessors of a switch

For each block S ending with a switch, this pass copies S for each of S's predecessors that seem to assign the value being switched over as a const. This is done using a somewhat simple heuristic to determine what seems to be a const transitively.

More precisely, this is what the pass does:
- find a block that ends in a switch
- track if there is an unique place set before the current basic block that determines the result of the switch (this is the part that resolves switching over discriminants)
- if there is, iterate over the parents that have a reasonable terminator and find if the found determining place is likely to be (transitively) set from a const within that parent block
- if so, add the corresponding edge to a vector of edges to duplicate
- once this is done, iterate over the found edges: copy the target block and replace the reference to the target block in the origin block with the new block

This pass is not optimal and could probably duplicate in more cases, but the intention was mostly to address cases like in #85133 or #85365, to avoid creating new enums that get destroyed immediately afterwards (notably making the new try v2 `?` desugar zero-cost).

A benefit of this pass working the way it does is that it is easy to ensure its correctness: the worst that can happen is for it to needlessly copy a basic block, which is likely to be destroyed by cleanup passes afterwards. The complex parts where aliasing matters are only heuristics and the hard work is left to further passes like ConstProp.

# LLVM blocker

Unfortunately, I believe it would be unwise to enable this optimization by default for now. Indeed, currently switch lowering passes like SimplifyCFG in LLVM lose the information on the set of possible variant values, which means it tends to actually generate worse code with this optimization enabled. A fix would have to be done in LLVM itself. This is something I also want to look into. I have opened [a bug report at the LLVM bug tracker](https://bugs.llvm.org/show_bug.cgi?id=50455).

When this is done, I hope we can enable this pass by default. It should be fairly fast and I think it is beneficial in many cases. Notably, it should be a sound alternative to simplify-arm-identity. By the way, ConstProp only seems to pick up the optimization in functions that are not generic. This is however most likely an issue in ConstProp that I will look into afterwards.

This is my first contribution to rustc, and I would like to thank everyone on the Zulip mir-opt chat for the help and support, and especially `@scottmcm` for the guidance.
2021-07-25 13:51:48 +00:00
..
rustc
rustc_apfloat
rustc_arena
rustc_ast Remove special case for ExprKind::Paren in MutVisitor 2021-07-19 17:23:10 -05:00
rustc_ast_lowering Auto merge of #83723 - cjgillot:ownernode, r=petrochenkov 2021-07-25 11:11:02 +00:00
rustc_ast_passes Auto merge of #83918 - workingjubilee:stable-rangefrom-pat, r=joshtriplett 2021-07-11 06:31:42 +00:00
rustc_ast_pretty Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
rustc_attr Enhance well-formedness checks for #[repr(...)] attributes 2021-07-09 22:03:48 +02:00
rustc_builtin_macros Rollup merge of #87206 - matthiaskrgr:clippy_collect, r=davidtwco 2021-07-21 15:52:45 +02:00
rustc_codegen_cranelift Rollup merge of #87092 - ricobbe:fix-raw-dylib-multiple-definitions, r=petrochenkov 2021-07-18 14:21:56 +09:00
rustc_codegen_llvm Auto merge of #86950 - tmiasko:personality, r=nagisa 2021-07-18 20:33:23 +00:00
rustc_codegen_ssa Auto merge of #87153 - michaelwoerister:debuginfo-names-dyn-trait-projection-bounds, r=wesleywiser 2021-07-19 21:25:43 +00:00
rustc_data_structures Auto merge of #86429 - JohnTitor:get-by-key-enum-part-2, r=oli-obk 2021-07-23 23:17:38 +00:00
rustc_driver
rustc_error_codes Rollup merge of #87342 - midgleyc:add-E0757-long, r=GuillaumeGomez 2021-07-21 15:52:53 +02:00
rustc_errors Various diagnostics clean ups/tweaks 2021-07-19 08:43:35 -07:00
rustc_expand Auto merge of #87381 - Aaron1011:note-semi-trailing-macro, r=petrochenkov 2021-07-25 04:34:58 +00:00
rustc_feature Add internal attribute and tests. 2021-07-20 22:14:43 +08:00
rustc_fs_util
rustc_graphviz
rustc_hir Auto merge of #83723 - cjgillot:ownernode, r=petrochenkov 2021-07-25 11:11:02 +00:00
rustc_hir_pretty Introduce OwnerNode::Crate. 2021-07-25 12:22:47 +02:00
rustc_incremental Move OnDiskCache to rustc_query_impl. 2021-07-18 11:14:07 +02:00
rustc_index
rustc_infer Auto merge of #86461 - crlf0710:rich_vtable, r=nikomatsakis 2021-07-24 10:21:23 +00:00
rustc_interface Remove deadlock virtual call. 2021-07-18 11:14:08 +02:00
rustc_lexer
rustc_lint Auto merge of #83723 - cjgillot:ownernode, r=petrochenkov 2021-07-25 11:11:02 +00:00
rustc_lint_defs Display an extra note for trailing semicolon lint with trailing macro 2021-07-24 11:46:44 -05:00
rustc_llvm Upgrade cc crate to 1.0.69 2021-07-13 17:58:50 +09:00
rustc_macros Move OnDiskCache to rustc_query_impl. 2021-07-18 11:14:07 +02:00
rustc_metadata Auto merge of #83723 - cjgillot:ownernode, r=petrochenkov 2021-07-25 11:11:02 +00:00
rustc_middle Auto merge of #83723 - cjgillot:ownernode, r=petrochenkov 2021-07-25 11:11:02 +00:00
rustc_mir Auto merge of #85646 - Moxinilian:separate-const-switch, r=cjgillot 2021-07-25 13:51:48 +00:00
rustc_mir_build Implement AssignToDroppingUnionField in THIR unsafeck 2021-07-23 15:38:19 +02:00
rustc_parse Auto merge of #87242 - JohnTitor:rollup-t9rmwpo, r=JohnTitor 2021-07-18 08:15:17 +00:00
rustc_parse_format Rework SESSION_GLOBALS API to prevent overwriting it 2021-07-08 16:16:28 +02:00
rustc_passes Only check macro attributes when checking the crate root. 2021-07-25 12:23:37 +02:00
rustc_plugin_impl
rustc_privacy
rustc_query_impl Support HIR wf checking for function signatures 2021-07-20 10:58:14 -05:00
rustc_query_system
rustc_resolve Merge the BTreeMap in hir::Crate. 2021-07-25 12:18:56 +02:00
rustc_save_analysis Introduce OwnerNode::Crate. 2021-07-25 12:22:47 +02:00
rustc_serialize
rustc_session Rename known_attrs to expanded_inert_attrs and move to rustc_expand 2021-07-23 17:03:07 -05:00
rustc_span Add internal attribute and tests. 2021-07-20 22:14:43 +08:00
rustc_symbol_mangling
rustc_target Add support for powerpc-unknown-freebsd 2021-07-22 17:29:33 +02:00
rustc_trait_selection Auto merge of #86461 - crlf0710:rich_vtable, r=nikomatsakis 2021-07-24 10:21:23 +00:00
rustc_traits Add initial implementation of HIR-based WF checking for diagnostics 2021-07-16 16:29:02 -05:00
rustc_ty_utils Add impl_constness query 2021-07-10 20:54:49 +08:00
rustc_type_ir
rustc_typeck Auto merge of #86580 - BoxyUwU:cgd-subst-ice, r=nikomatsakis 2021-07-24 20:01:51 +00:00