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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::collections::HashMap;

use crate::mir::ops;

fn visit_simple_alloc_op_kind(
    op_kind: &mut ops::OpKind,
    boxed_reg: ops::RegId,
    native_reg: ops::RegId,
    native_to_boxed: &mut HashMap<ops::RegId, ops::RegId>,
) {
    use std::collections::hash_map::Entry;

    match native_to_boxed.entry(native_reg) {
        Entry::Occupied(existing_output) => {
            *op_kind = ops::OpKind::Alias(boxed_reg, *existing_output.get())
        }
        Entry::Vacant(vacant_entry) => {
            vacant_entry.insert(boxed_reg);
        }
    }
}

fn remove_branch_redundant_alloc_ops(
    ops: &mut [ops::Op],
    // We can use a single `HashMap` because simple alloc ops all take distinct native reg types
    native_to_boxed: &mut HashMap<ops::RegId, ops::RegId>,
) {
    for op in ops.iter_mut() {
        match op.kind {
            ops::OpKind::AllocBoxedInt(boxed_reg, native_reg)
            | ops::OpKind::AllocBoxedFloat(boxed_reg, native_reg)
            | ops::OpKind::AllocBoxedChar(boxed_reg, native_reg)
            | ops::OpKind::AllocBoxedSym(boxed_reg, native_reg) => {
                visit_simple_alloc_op_kind(&mut op.kind, boxed_reg, native_reg, native_to_boxed);
            }
            ops::OpKind::LoadBoxedIntValue(native_reg, boxed_reg)
            | ops::OpKind::LoadBoxedFloatValue(native_reg, boxed_reg)
            | ops::OpKind::LoadBoxedCharValue(native_reg, boxed_reg)
            | ops::OpKind::LoadBoxedSymInterned(native_reg, boxed_reg) => {
                native_to_boxed.insert(native_reg, boxed_reg);
            }
            ops::OpKind::Cond(ref mut cond_op) => {
                remove_branch_redundant_alloc_ops(
                    &mut cond_op.true_ops,
                    &mut native_to_boxed.clone(),
                );
                remove_branch_redundant_alloc_ops(
                    &mut cond_op.false_ops,
                    &mut native_to_boxed.clone(),
                );
            }
            _ => {}
        }
    }
}

/// Updates `ops` in-place to replace allocs of the same native value with `OpKind::Alias`
pub fn remove_redundant_alloc_ops(ops: &mut [ops::Op]) {
    let mut native_to_boxed = HashMap::new();
    remove_branch_redundant_alloc_ops(ops, &mut native_to_boxed)
}

#[cfg(test)]
mod test {
    use super::*;

    use crate::source::EMPTY_SPAN;

    #[test]
    fn test_box_different_native_regs() {
        let native_reg1 = ops::RegId::alloc();
        let boxed_reg1 = ops::RegId::alloc();

        let native_reg2 = ops::RegId::alloc();
        let boxed_reg2 = ops::RegId::alloc();

        let ops = &mut [
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedInt(boxed_reg1, native_reg1),
            ),
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedInt(boxed_reg2, native_reg2),
            ),
        ];

        // Should be identical because we're boxing different native values
        let expected_ops = ops.clone();

        remove_redundant_alloc_ops(ops);
        assert_eq!(&expected_ops, ops);
    }

    #[test]
    fn test_box_same_native_regs() {
        let native_reg1 = ops::RegId::alloc();
        let boxed_reg1 = ops::RegId::alloc();

        let boxed_reg2 = ops::RegId::alloc();

        let ops = &mut [
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedFloat(boxed_reg1, native_reg1),
            ),
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedFloat(boxed_reg2, native_reg1),
            ),
        ];

        let expected_ops = &[
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedFloat(boxed_reg1, native_reg1),
            ),
            // Should remove the redundant alloc of the same native value
            ops::Op::new(EMPTY_SPAN, ops::OpKind::Alias(boxed_reg2, boxed_reg1)),
        ];

        remove_redundant_alloc_ops(ops);
        assert_eq!(expected_ops, ops);
    }

    #[test]
    fn test_reboxing() {
        let native_reg1 = ops::RegId::alloc();
        let boxed_reg1 = ops::RegId::alloc();
        let boxed_reg2 = ops::RegId::alloc();

        let ops = &mut [
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::LoadBoxedSymInterned(native_reg1, boxed_reg1),
            ),
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedSym(boxed_reg2, native_reg1),
            ),
        ];

        let expected_ops = &[
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::LoadBoxedSymInterned(native_reg1, boxed_reg1),
            ),
            // Should re-use the original box we got the native value from
            ops::Op::new(EMPTY_SPAN, ops::OpKind::Alias(boxed_reg2, boxed_reg1)),
        ];

        remove_redundant_alloc_ops(ops);
        assert_eq!(expected_ops, ops);
    }

    #[test]
    fn test_cond_branch() {
        let outer_native_reg1 = ops::RegId::alloc();
        let outer_boxed_reg1 = ops::RegId::alloc();

        let test_reg = ops::RegId::alloc();

        let branch_boxed_reg1 = ops::RegId::alloc();
        let branch_native_reg2 = ops::RegId::alloc();
        let branch_boxed_reg2 = ops::RegId::alloc();

        let outer_boxed_reg2 = ops::RegId::alloc();

        let ops = &mut [
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedChar(outer_boxed_reg1, outer_native_reg1),
            ),
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::Cond(ops::CondOp {
                    reg_phi: None,
                    test_reg,
                    true_ops: Box::new([
                        ops::Op::new(
                            EMPTY_SPAN,
                            ops::OpKind::AllocBoxedChar(branch_boxed_reg1, outer_native_reg1),
                        ),
                        ops::Op::new(
                            EMPTY_SPAN,
                            ops::OpKind::AllocBoxedChar(branch_boxed_reg2, branch_native_reg2),
                        ),
                    ]),
                    false_ops: Box::new([]),
                }),
            ),
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedChar(outer_boxed_reg2, branch_native_reg2),
            ),
        ];

        let expected_ops = &[
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::AllocBoxedChar(outer_boxed_reg1, outer_native_reg1),
            ),
            ops::Op::new(
                EMPTY_SPAN,
                ops::OpKind::Cond(ops::CondOp {
                    reg_phi: None,
                    test_reg,
                    true_ops: Box::new([
                        ops::Op::new(
                            EMPTY_SPAN,
                            // We can use the alloc from outside this branch
                            ops::OpKind::Alias(branch_boxed_reg1, outer_boxed_reg1),
                        ),
                        ops::Op::new(
                            EMPTY_SPAN,
                            ops::OpKind::AllocBoxedChar(branch_boxed_reg2, branch_native_reg2),
                        ),
                    ]),
                    false_ops: Box::new([]),
                }),
            ),
            ops::Op::new(
                EMPTY_SPAN,
                // We can't use an alloc from within the branch
                ops::OpKind::AllocBoxedChar(outer_boxed_reg2, branch_native_reg2),
            ),
        ];

        remove_redundant_alloc_ops(ops);
        assert_eq!(expected_ops, ops);
    }
}