1 //! Peephole optimizations that can be performed while creating clif ir.
3 use cranelift_codegen::ir::{condcodes::IntCC, InstructionData, Opcode, Value, ValueDef};
4 use cranelift_frontend::FunctionBuilder;
6 /// If the given value was produced by a `bint` instruction, return it's input, otherwise return the
8 pub(crate) fn maybe_unwrap_bint(bcx: &mut FunctionBuilder<'_>, arg: Value) -> Value {
9 if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
10 match bcx.func.dfg[arg_inst] {
11 InstructionData::Unary { opcode: Opcode::Bint, arg } => arg,
19 /// If the given value was produced by the lowering of `Rvalue::Not` return the input and true,
20 /// otherwise return the given value and false.
21 pub(crate) fn maybe_unwrap_bool_not(bcx: &mut FunctionBuilder<'_>, arg: Value) -> (Value, bool) {
22 if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
23 match bcx.func.dfg[arg_inst] {
24 // This is the lowering of `Rvalue::Not`
25 InstructionData::IntCompareImm {
26 opcode: Opcode::IcmpImm,
30 } if imm.bits() == 0 => (arg, true),
38 /// Returns whether the branch is statically known to be taken or `None` if it isn't statically known.
39 pub(crate) fn maybe_known_branch_taken(
40 bcx: &FunctionBuilder<'_>,
44 let arg_inst = if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
50 match bcx.func.dfg[arg_inst] {
51 InstructionData::UnaryBool { opcode: Opcode::Bconst, imm } => {
58 InstructionData::UnaryImm { opcode: Opcode::Iconst, imm } => {