-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathoptimize_constants.re
More file actions
61 lines (54 loc) 路 1.64 KB
/
Copy pathoptimize_constants.re
File metadata and controls
61 lines (54 loc) 路 1.64 KB
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
open Anftree;
open Grain_typed;
open Grain_utils;
open Types;
let known_constants = ref(Ident.empty: Ident.tbl(imm_expression_desc));
let add_constant = (id, value) =>
known_constants := Ident.add(id, value, known_constants^);
module ConstantPropagationArg: Anf_mapper.MapArgument = {
include Anf_mapper.DefaultMapArgument;
let enter_anf_expression = ({anf_desc: desc} as a) => {
switch (desc) {
| AELet(g, r, Immutable, binds, body) =>
List.iter(
((id, v)) =>
switch (v) {
| {comp_desc: CNumber(Const_number_int(n) as c')}
when
n <= Literals.simple_number_max
&& n >= Literals.simple_number_min =>
add_constant(id, ImmConst(Const_number(c')))
| {comp_desc: CImmExpr({imm_desc})} =>
switch (imm_desc) {
// We don't substitute mutable variables, since we do pass-by-value, not pass-by-reference
| ImmId(rhs_id) when Analyze_mutable_vars.is_mutable(rhs_id) =>
()
| _ => add_constant(id, imm_desc)
}
| _ => ()
},
binds,
)
| _ => ()
};
a;
};
let leave_imm_expression = ({imm_desc: desc} as i) =>
switch (desc) {
| ImmId(id) =>
switch (Ident.find_same_opt(id, known_constants^)) {
| Some(value) => {
...i,
imm_desc: value,
}
| None => i
}
| _ => i
};
};
module ConstantPropagationMapper = Anf_mapper.MakeMap(ConstantPropagationArg);
let optimize = anfprog => {
/* Reset state */
known_constants := Ident.empty;
ConstantPropagationMapper.map_anf_program(anfprog);
};