rust/compiler/rustc_middle/src
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
..
dep_graph Allow query system to recover a HirId. 2022-10-01 15:58:42 +02:00
hir Rename some OwnerId fields. 2022-10-29 20:28:38 +11:00
infer Revert "Make ClosureOutlivesRequirement not rely on an unresolved type" 2022-10-27 16:15:11 +00:00
middle rustc_middle: Remove unnecessary type parameter from AccessLevels 2022-10-29 23:36:56 +04:00
mir Use br instead of switch in more cases. 2022-10-31 10:16:39 +11:00
query Auto merge of #102698 - michaelwoerister:unord-collections, r=lncr 2022-10-29 06:20:48 +00:00
thir Use boxed slices in PatKind. 2022-09-02 07:26:22 +10:00
traits Revert "Make ClosureOutlivesRequirement not rely on an unresolved type" 2022-10-27 16:15:11 +00:00
ty Rollup merge of #103625 - WaffleLapkin:no_tyctxt_dogs_allowed, r=compiler-errors 2022-10-29 14:18:03 +02:00
util span: move MultiSpan 2022-04-05 07:01:00 +01:00
arena.rs Auto merge of #102698 - michaelwoerister:unord-collections, r=lncr 2022-10-29 06:20:48 +00:00
error.rs Migrate all diagnostics 2022-10-23 10:09:44 +02:00
lib.rs const_evaluatable_unchecked to const eval 2022-10-18 16:31:56 +02:00
lint.rs fix typo 2022-10-16 16:47:55 +02:00
macros.rs add inline to TrivialTypeTraversalImpls 2022-10-17 10:54:03 +02:00
metadata.rs rustc: Parameterize ty::Visibility over used ID 2022-09-07 13:35:41 +04:00
tests.rs mv compiler to compiler/ 2020-08-30 18:45:07 +03:00
thir.rs Use tidy-alphabetical in the compiler 2022-10-12 17:49:10 +05:30
values.rs Check representability in adt_sized_constraint 2022-10-10 14:36:12 -05:00