]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_cranelift/src/optimize/peephole.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / src / optimize / peephole.rs
CommitLineData
29967ef6
XL
1//! Peephole optimizations that can be performed while creating clif ir.
2
94222f64 3use cranelift_codegen::ir::{condcodes::IntCC, InstructionData, Opcode, Value, ValueDef};
29967ef6
XL
4use cranelift_frontend::FunctionBuilder;
5
29967ef6
XL
6/// If the given value was produced by the lowering of `Rvalue::Not` return the input and true,
7/// otherwise return the given value and false.
8pub(crate) fn maybe_unwrap_bool_not(bcx: &mut FunctionBuilder<'_>, arg: Value) -> (Value, bool) {
9 if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
10 match bcx.func.dfg[arg_inst] {
11 // This is the lowering of `Rvalue::Not`
12 InstructionData::IntCompareImm {
13 opcode: Opcode::IcmpImm,
14 cond: IntCC::Equal,
15 arg,
16 imm,
17 } if imm.bits() == 0 => (arg, true),
18 _ => (arg, false),
19 }
20 } else {
21 (arg, false)
22 }
23}
24
5869c6ff
XL
25/// Returns whether the branch is statically known to be taken or `None` if it isn't statically known.
26pub(crate) fn maybe_known_branch_taken(
27 bcx: &FunctionBuilder<'_>,
28 arg: Value,
29 test_zero: bool,
30) -> Option<bool> {
31 let arg_inst = if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
32 arg_inst
33 } else {
34 return None;
35 };
36
37 match bcx.func.dfg[arg_inst] {
6a06907d 38 InstructionData::UnaryImm { opcode: Opcode::Iconst, imm } => {
5869c6ff
XL
39 if test_zero {
40 Some(imm.bits() == 0)
41 } else {
42 Some(imm.bits() != 0)
43 }
44 }
45 _ => None,
46 }
47}