rust/compiler/rustc_middle/src/mir
Nicholas Nethercote 003a3f8cd3 Use br instead of switch in more cases.
`codegen_switchint_terminator` already uses `br` instead of `switch`
when there is one normal target plus the `otherwise` target. But there's
another common case with two normal targets and an `otherwise` target
that points to an empty unreachable BB. This comes up a lot when
switching on the tags of enums that use niches.

The pattern looks like this:
```
bb1:                                              ; preds = %bb6
  %3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
  %4 = sub i8 %3, 2
  %5 = icmp eq i8 %4, 0
  %_6 = select i1 %5, i64 0, i64 1
  switch i64 %_6, label %bb3 [
    i64 0, label %bb4
    i64 1, label %bb2
  ]

bb3:                                              ; preds = %bb1
  unreachable
```
This commit adds code to convert the `switch` to a `br`:
```
bb1:                                              ; preds = %bb6
  %3 = load i8, ptr %_2, align 1, !range !9, !noundef !4
  %4 = sub i8 %3, 2
  %5 = icmp eq i8 %4, 0
  %_6 = select i1 %5, i64 0, i64 1
  %6 = icmp eq i64 %_6, 0
  br i1 %6, label %bb4, label %bb2

bb3:                                              ; No predecessors!
  unreachable
```
This has a surprisingly large effect on compile times, with reductions
of 5% on debug builds of some crates. The reduction is all due to LLVM
taking less time. Maybe LLVM is just much better at handling `br` than
`switch`.

The resulting code is still suboptimal.
- The `icmp`, `select`, `icmp` sequence is silly, converting an `i1` to an `i64`
  and back to an `i1`. But with the current code structure it's hard to avoid,
  and LLVM will easily clean it up, in opt builds at least.
- `bb3` is usually now truly dead code (though not always, so it can't
  be removed universally).
2022-10-31 10:16:39 +11:00
..
interpret Auto merge of #103227 - lcnr:bye-bye-unevaluated-const, r=oli-obk 2022-10-22 07:49:33 +00:00
basic_blocks.rs Fix a bunch of typo 2022-08-31 18:24:55 +08:00
coverage.rs Add #[derive(TypeVisitable)] 2022-07-05 22:25:15 +01:00
generic_graph.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
generic_graphviz.rs Rename debugging_opts to unstable_opts 2022-07-13 17:47:06 -05:00
graph_cyclic_cache.rs impl TypeVisitable in type traversal macros 2022-07-05 22:25:33 +01:00
graphviz.rs Rename debugging_opts to unstable_opts 2022-07-13 17:47:06 -05:00
mod.rs Use br instead of switch in more cases. 2022-10-31 10:16:39 +11:00
mono.rs Rename some OwnerId fields. 2022-10-29 20:28:38 +11:00
patch.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
predecessors.rs impl TypeVisitable in type traversal macros 2022-07-05 22:25:33 +01:00
pretty.rs introduce mir::Unevaluated 2022-09-22 12:35:28 +02:00
query.rs Revert "Make ClosureOutlivesRequirement not rely on an unresolved type" 2022-10-27 16:15:11 +00:00
spanview.rs Generalize the Assume intrinsic statement to a general Intrinsic statement 2022-09-06 14:18:32 +00:00
switch_sources.rs Remove unused StableMap and StableSet types from rustc_data_structures 2022-07-20 13:11:39 +02:00
syntax.rs Use tidy-alphabetical in the compiler 2022-10-12 17:49:10 +05:30
tcx.rs Auto merge of #99806 - oli-obk:unconstrained_opaque_type, r=estebank 2022-09-20 12:09:52 +00:00
terminator.rs derive TypeVisitable and TypeFoldable for mir types 2022-09-15 14:42:43 +00:00
traversal.rs Replace Body::basic_blocks() with field access 2022-08-26 14:27:08 +02:00
type_foldable.rs add inline to TrivialTypeTraversalImpls 2022-10-17 10:54:03 +02:00
type_visitable.rs add inline to TrivialTypeTraversalImpls 2022-10-17 10:54:03 +02:00
visit.rs Auto merge of #99806 - oli-obk:unconstrained_opaque_type, r=estebank 2022-09-20 12:09:52 +00:00