-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstantPropagation.ml
174 lines (151 loc) · 5.21 KB
/
ConstantPropagation.ml
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
open QuadTypes
open Error
open Symbol
open Types
open Quads
open UDChains
open OptimizationSupport
let simplified_jump = ref false
let single_constant_propagation flowgraph (uses_hash,defs_hash) temp_hash =
(* Function to handle a use and return the value it (may) have *)
let handle_use i j q =
match q with
| Quad_entry _ -> (
if is_not_temporary q then (
let binding =
try Hashtbl.find uses_hash (q,i,j)
with Not_found ->
internal "No binding for use %s" (string_of_quad_elem_t q);
raise Terminate in
match binding.links with
| [] -> None
| [h] -> h.value
| h::t ->
if (List.for_all (fun x -> x.value = h.value) t)
then h.value
else None
)
else (
let binding =
try Hashtbl.find temp_hash q
with Not_found ->
internal "No binding for use %s" (string_of_quad_elem_t q);
raise Terminate in
binding.temp_value
)
)
| Quad_int x -> Some (int_of_string x)
| _ -> None
in (* End handle use *)
let set_def i j q value =
if is_entry q then (
if is_not_temporary q then (
let binding =
try Hashtbl.find defs_hash (q,i,j)
with Not_found ->
internal "No binding for use %s" (string_of_quad_elem_t q);
raise Terminate in
binding.value <- value
)
else (
let binding =
try Hashtbl.find temp_hash q
with Not_found ->
internal "No binding for use %s" (string_of_quad_elem_t q);
raise Terminate in
binding.temp_value <- value
)
) in (* End set def *)
(*
let has_single_use q =
if is_not_temporary q then (* If it is not a temporary find the links *)
let binding =
try Hashtbl.find defs_hash q
with Not_found ->
internal "No binding for use %s" (string_of_quad_elem_t q);
raise Terminate in
List.length (binding.links) = 1
else true (* If it is temporary it has one use *) in
*)
let handle_single_node i node =
let handle_single_quad j = function
| Quad_calc (op, q1, q2, q) -> (
let calc =
match op with
|"+" -> ( + )
|"-" -> ( - )
|"*" -> ( * )
|"/" -> ( / )
|"%" -> (mod)
| _ -> internal "Not an operator"; raise Terminate in
match (handle_use i j q1, handle_use i j q2) with
| (None, None) -> ()
| (Some v, None) ->
node.code_block.(j) <- Quad_calc (op, Quad_int (string_of_int v), q2, q)
| (None, Some v) ->
node.code_block.(j) <- Quad_calc (op, q1, Quad_int (string_of_int v), q)
| (Some v1, Some v2) ->
let q_value = calc v1 v2 in
set_def i j q (Some (q_value));
if (is_temporary q) then
node.code_block.(j) <- Quad_dummy
else
node.code_block.(j) <- Quad_set (Quad_int (string_of_int q_value), q)
)
| Quad_cond (op, q1, q2, e) -> (
match (handle_use i j q1, handle_use i j q2) with
| (Some v1, Some v2) -> (
let cond =
match op with
| "==" -> ( = )
| "<" -> ( < )
| ">" -> ( > )
| "!=" -> ( != )
| "<=" -> ( <= )
| ">=" -> ( >= )
| _ -> internal "Not a cond operand"; raise Terminate in
if (cond v1 v2) then
node.code_block.(j) <- Quad_jump e
else
node.code_block.(j) <- Quad_dummy;
simplified_jump := true;
)
| (Some v, None) ->
node.code_block.(j) <- Quad_cond(op, Quad_int (string_of_int v), q2, e)
| (None, Some v) ->
node.code_block.(j) <- Quad_cond(op, q1, Quad_int (string_of_int v), e)
| _ -> ()
)
| Quad_set (q1, q2) -> (
match handle_use i j q1 with
| Some v as q_value ->
set_def i j q2 q_value;
if is_temporary q2 then
node.code_block.(j) <- Quad_dummy
else
node.code_block.(j) <- Quad_set (Quad_int (string_of_int v), q2)
| None -> ()
)
| Quad_array (a, q, e) -> (
match handle_use i j q with
| Some v ->
node.code_block.(j) <- Quad_array (a, Quad_int (string_of_int v), e)
| None -> ()
)
| Quad_par (q, pm) when pm = PASS_BY_VALUE -> (
match handle_use i j q with
| Some v ->
node.code_block.(j) <- Quad_par (Quad_int (string_of_int v), pm)
| None -> ()
)
| _ -> () in (* End handle single quad *)
Array.iteri handle_single_quad node.code_block in (* End handle_single_block *)
Array.iteri handle_single_node flowgraph
(* End single_constant_propagation *)
let constant_propagation flowgraphs chains temps =
simplified_jump := false;
let n = Array.length flowgraphs in
for i = 0 to n-1 do
single_constant_propagation flowgraphs.(i) chains.(i) temps.(i)
done;
!simplified_jump