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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use super::cfg::BlockCFG;
use crate::{
    expansion::ast::Value_,
    hlir::ast::{Command, Command_, Exp, UnannotatedExp_},
};

/// returns true if anything changed
pub fn optimize(cfg: &mut BlockCFG) -> bool {
    let mut changed = false;
    for block in cfg.blocks_mut().values_mut() {
        for cmd in block {
            changed = optimize_cmd(cmd) || changed;
        }
    }
    if changed {
        let _dead_blocks = cfg.recompute();
    }
    changed
}

fn optimize_cmd(sp!(_, cmd_): &mut Command) -> bool {
    use Command_ as C;
    use UnannotatedExp_ as E;
    use Value_ as V;
    match cmd_ {
        C::JumpIf {
            cond:
                Exp {
                    exp: sp!(_, E::Value(sp!(_, V::Bool(cond)))),
                    ..
                },
            if_true,
            if_false,
        } => {
            let lbl = if *cond { *if_true } else { *if_false };
            *cmd_ = C::Jump {
                target: lbl,
                from_user: false,
            };
            true
        }
        _ => false,
    }
}