about summary refs log tree commit diff
path: root/compiler/rustc_codegen_cranelift/src/optimize/peephole.rs
blob: a575ed8dc35f80b0a3662df42cade581ab5cf672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Peephole optimizations that can be performed while creating clif ir.

use cranelift_codegen::ir::{
    condcodes::IntCC, types, InstBuilder, InstructionData, Opcode, Value, ValueDef,
};
use cranelift_frontend::FunctionBuilder;

/// If the given value was produced by a `bint` instruction, return it's input, otherwise return the
/// given value.
pub(crate) fn maybe_unwrap_bint(bcx: &mut FunctionBuilder<'_>, arg: Value) -> Value {
    if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
        match bcx.func.dfg[arg_inst] {
            InstructionData::Unary {
                opcode: Opcode::Bint,
                arg,
            } => arg,
            _ => arg,
        }
    } else {
        arg
    }
}

/// If the given value was produced by the lowering of `Rvalue::Not` return the input and true,
/// otherwise return the given value and false.
pub(crate) fn maybe_unwrap_bool_not(bcx: &mut FunctionBuilder<'_>, arg: Value) -> (Value, bool) {
    if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
        match bcx.func.dfg[arg_inst] {
            // This is the lowering of `Rvalue::Not`
            InstructionData::IntCompareImm {
                opcode: Opcode::IcmpImm,
                cond: IntCC::Equal,
                arg,
                imm,
            } if imm.bits() == 0 => (arg, true),
            _ => (arg, false),
        }
    } else {
        (arg, false)
    }
}

pub(crate) fn make_branchable_value(bcx: &mut FunctionBuilder<'_>, arg: Value) -> Value {
    if bcx.func.dfg.value_type(arg).is_bool() {
        return arg;
    }

    (|| {
        let arg_inst = if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
            arg_inst
        } else {
            return None;
        };

        match bcx.func.dfg[arg_inst] {
            // This is the lowering of Rvalue::Not
            InstructionData::Load {
                opcode: Opcode::Load,
                arg: ptr,
                flags,
                offset,
            } => {
                // Using `load.i8 + uextend.i32` would legalize to `uload8 + ireduce.i8 +
                // uextend.i32`. Just `uload8` is much faster.
                match bcx.func.dfg.ctrl_typevar(arg_inst) {
                    types::I8 => Some(bcx.ins().uload8(types::I32, flags, ptr, offset)),
                    types::I16 => Some(bcx.ins().uload16(types::I32, flags, ptr, offset)),
                    _ => None,
                }
            }
            _ => None,
        }
    })()
    .unwrap_or_else(|| {
        match bcx.func.dfg.value_type(arg) {
            types::I8 | types::I16 => {
                // WORKAROUND for brz.i8 and brnz.i8 not yet being implemented
                bcx.ins().uextend(types::I32, arg)
            }
            _ => arg,
        }
    })
}

/// Returns whether the branch is statically known to be taken or `None` if it isn't statically known.
pub(crate) fn maybe_known_branch_taken(
    bcx: &FunctionBuilder<'_>,
    arg: Value,
    test_zero: bool,
) -> Option<bool> {
    let arg_inst = if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
        arg_inst
    } else {
        return None;
    };

    match bcx.func.dfg[arg_inst] {
        InstructionData::UnaryBool {
            opcode: Opcode::Bconst,
            imm,
        } => {
            if test_zero {
                Some(!imm)
            } else {
                Some(imm)
            }
        }
        InstructionData::UnaryImm {
            opcode: Opcode::Iconst,
            imm,
        } => {
            if test_zero {
                Some(imm.bits() == 0)
            } else {
                Some(imm.bits() != 0)
            }
        }
        _ => None,
    }
}