-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Optimize if
conditions with integer equality to matches on the integer
#75144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I would like to work on this |
Hi @oli-obk Is it an oversight that SwitchInt does not allow to switch on signed values? |
The u128 is just a zero-extended version of the value to switch on. For example |
FYI: Codegen does the this transformation in other direction: rust/src/librustc_codegen_ssa/mir/block.rs Lines 220 to 224 in 30f0a07
|
Hmm... that's good to know. Maybe we should additionally have the opposite optimization as a cleanup before we go into codegen, then this codegen arm can just become an assert. |
For cg_clif |
so... should we remove that transformation from codegen_ssa and let the backend handle it? |
I think so. It can wait though, as I don't yet use codegen_ssa anyway. |
So we still want to do the MIR optimization? I understood from @bjorn3 that switching directly on the integer would be beneficial for cranelift but not necessarily for llvm. Did I get that right? |
I think that is correct. |
Yes, we still want this optimization. The llvm backend already handles this change correctly, so I think we should just keep the optimization unconditionally since it simplifies the MIR. |
…int-to-switch, r=oli-obk New pass to optimize `if`conditions on integrals to switches on the integer Fixes rust-lang#75144 Pass to convert `if` conditions on integrals into switches on the integral. For an example, it turns something like ``` _3 = Eq(move _4, const 43i32); StorageDead(_4); switchInt(_3) -> [false: bb2, otherwise: bb3]; ``` into: ``` switchInt(_4) -> [43i32: bb3, otherwise: bb2]; ```
If we generate the MIR for
we get something like
which we could also write as
(plus move the
StorageDead
to the beginning ofbb2
andbb3
). The same goes for!=
(Ne
).cc @rust-lang/wg-mir-opt
The text was updated successfully, but these errors were encountered: