rust/compiler/rustc_codegen_ssa/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
..
back Support raw-dylib functions being used inside inlined functions 2022-10-24 16:17:38 -07:00
coverageinfo Remove in_band_lifetimes from rustc_codegen_ssa 2021-12-15 00:41:41 -05:00
debuginfo Remove byte swap of valtree hash on big endian 2022-10-19 14:55:33 +08:00
mir Use br instead of switch in more cases. 2022-10-31 10:16:39 +11:00
traits Accept TyCtxt instead of TyCtxtAt in Ty::is_* functions 2022-10-27 15:06:08 +04:00
base.rs Simplify cast_shift_expr_rhs. 2022-10-25 14:39:20 +11:00
common.rs Simplify cast_shift_expr_rhs. 2022-10-25 14:39:20 +11:00
errors.rs Introduce dedicated -Zdylib-lto flag for enabling LTO on dylibs 2022-10-23 13:48:03 +02:00
glue.rs use unchecked mul to compute slice sizes 2022-06-14 17:09:07 -04:00
lib.rs ADD - initial port of link.rs 2022-10-07 10:03:45 -04:00
meth.rs Call destructors when dyn* object goes out of scope 2022-09-12 16:55:57 -07:00
mono_item.rs Add codegen for global_asm! sym operands 2022-04-15 14:36:30 +01:00
target_features.rs Sort target features alphabetically 2022-10-14 22:01:18 +02:00