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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the INRIA Non-Commercial License Agreement. *)
(* *)
(* *********************************************************************)
(** Elimination of redundant conversions to small numerical types. *)
Require Import Coqlib.
Require Import Maps.
Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import Globalenvs.
Require Import Op.
Require Import Registers.
Require Import RTL.
Require Import Lattice.
Require Import Kildall.
(** * Static analysis *)
(** Compile-time approximations *)
Inductive approx : Type :=
| Unknown (**r any value *)
| Int7 (**r [[0,127]] *)
| Int8s (**r [[-128,127]] *)
| Int8u (**r [[0,255]] *)
| Int15 (**r [[0,32767]] *)
| Int16s (**r [[-32768,32767]] *)
| Int16u (**r [[0,65535]] *)
| Single (**r single-precision float *)
| Novalue. (**r empty *)
(** We equip this type of approximations with a semi-lattice structure.
The ordering is inclusion between the sets of values denoted by
the approximations. *)
Module Approx <: SEMILATTICE_WITH_TOP.
Definition t := approx.
Definition eq (x y: t) := (x = y).
Definition eq_refl: forall x, eq x x := (@refl_equal t).
Definition eq_sym: forall x y, eq x y -> eq y x := (@sym_equal t).
Definition eq_trans: forall x y z, eq x y -> eq y z -> eq x z := (@trans_equal t).
Lemma eq_dec: forall (x y: t), {x=y} + {x<>y}.
Proof.
decide equality.
Qed.
Definition beq (x y: t) := if eq_dec x y then true else false.
Lemma beq_correct: forall x y, beq x y = true -> x = y.
Proof.
unfold beq; intros. destruct (eq_dec x y). auto. congruence.
Qed.
Definition ge (x y: t) : Prop :=
match x, y with
| Unknown, _ => True
| _, Novalue => True
| Int7, Int7 => True
| Int8s, (Int7 | Int8s) => True
| Int8u, (Int7 | Int8u) => True
| Int15, (Int7 | Int8u | Int15) => True
| Int16s, (Int7 | Int8s | Int8u | Int15 | Int16s) => True
| Int16u, (Int7 | Int8u | Int15 | Int16u) => True
| Single, Single => True
| _, _ => False
end.
Lemma ge_refl: forall x y, eq x y -> ge x y.
Proof.
unfold eq, ge; intros. subst y. destruct x; auto.
Qed.
Lemma ge_trans: forall x y z, ge x y -> ge y z -> ge x z.
Proof.
unfold ge; intros.
destruct x; auto; (destruct y; auto; try contradiction; destruct z; auto).
Qed.
Lemma ge_compat: forall x x' y y', eq x x' -> eq y y' -> ge x y -> ge x' y'.
Proof.
unfold eq; intros. congruence.
Qed.
Definition bge (x y: t) : bool :=
match x, y with
| Unknown, _ => true
| _, Novalue => true
| Int7, Int7 => true
| Int8s, (Int7 | Int8s) => true
| Int8u, (Int7 | Int8u) => true
| Int15, (Int7 | Int8u | Int15) => true
| Int16s, (Int7 | Int8s | Int8u | Int15 | Int16s) => true
| Int16u, (Int7 | Int8u | Int15 | Int16u) => true
| Single, Single => true
| _, _ => false
end.
Lemma bge_correct: forall x y, bge x y = true -> ge x y.
Proof.
destruct x; destruct y; simpl; auto || congruence.
Qed.
Definition bot := Novalue.
Definition top := Unknown.
Lemma ge_bot: forall x, ge x bot.
Proof.
unfold ge, bot. destruct x; auto.
Qed.
Lemma ge_top: forall x, ge top x.
Proof.
unfold ge, top. auto.
Qed.
Definition lub (x y: t) : t :=
match x, y with
| Novalue, _ => y
| _, Novalue => x
| Int7, Int7 => Int7
| Int7, Int8u => Int8u
| Int7, Int8s => Int8s
| Int7, Int15 => Int15
| Int7, Int16u => Int16u
| Int7, Int16s => Int16s
| Int8u, (Int7|Int8u) => Int8u
| Int8u, Int15 => Int15
| Int8u, Int16u => Int16u
| Int8u, Int16s => Int16s
| Int8s, (Int7|Int8s) => Int8s
| Int8s, (Int15|Int16s) => Int16s
| Int15, (Int7|Int8u|Int15) => Int15
| Int15, Int16u => Int16u
| Int15, (Int8s|Int16s) => Int16s
| Int16u, (Int7|Int8u|Int15|Int16u) => Int16u
| Int16s, (Int7|Int8u|Int8s|Int15|Int16s) => Int16s
| Single, Single => Single
| _, _ => Unknown
end.
Lemma ge_lub_left: forall x y, ge (lub x y) x.
Proof.
unfold lub, ge; intros.
destruct x; destruct y; auto.
Qed.
Lemma ge_lub_right: forall x y, ge (lub x y) y.
Proof.
unfold lub, ge; intros.
destruct x; destruct y; auto.
Qed.
End Approx.
Module D := LPMap Approx.
(** Abstract interpretation of operators *)
Definition approx_bitwise_op (v1 v2: approx) : approx :=
if Approx.bge Int8u v1 && Approx.bge Int8u v2 then Int8u
else if Approx.bge Int16u v1 && Approx.bge Int16u v2 then Int16u
else Unknown.
Function approx_operation (op: operation) (vl: list approx) : approx :=
match op, vl with
| Omove, v1 :: nil => v1
| Ointconst n, _ =>
if Int.eq_dec n (Int.zero_ext 7 n) then Int7
else if Int.eq_dec n (Int.zero_ext 8 n) then Int8u
else if Int.eq_dec n (Int.sign_ext 8 n) then Int8s
else if Int.eq_dec n (Int.zero_ext 15 n) then Int15
else if Int.eq_dec n (Int.zero_ext 16 n) then Int16u
else if Int.eq_dec n (Int.sign_ext 16 n) then Int16s
else Unknown
| Ofloatconst n, _ =>
if Float.eq_dec n (Float.singleoffloat n) then Single else Unknown
| Ocast8signed, _ => Int8s
| Ocast8unsigned, _ => Int8u
| Ocast16signed, _ => Int16s
| Ocast16unsigned, _ => Int16u
| Osingleoffloat, _ => Single
| Oand, v1 :: v2 :: nil => approx_bitwise_op v1 v2
| Oor, v1 :: v2 :: nil => approx_bitwise_op v1 v2
| Oxor, v1 :: v2 :: nil => approx_bitwise_op v1 v2
(* Problem: what about and/or/xor immediate? and other
machine-specific operators? *)
| Ocmp c, _ => Int7
| _, _ => Unknown
end.
Definition approx_of_chunk (chunk: memory_chunk) :=
match chunk with
| Mint8signed => Int8s
| Mint8unsigned => Int8u
| Mint16signed => Int16s
| Mint16unsigned => Int16u
| Mint32 => Unknown
| Mfloat32 => Single
| Mfloat64 => Unknown
end.
(** Transfer function for the analysis *)
Definition approx_reg (app: D.t) (r: reg) :=
D.get r app.
Definition approx_regs (app: D.t) (rl: list reg):=
List.map (approx_reg app) rl.
Definition transfer (f: function) (pc: node) (before: D.t) :=
match f.(fn_code)!pc with
| None => before
| Some i =>
match i with
| Iop op args res s =>
let a := approx_operation op (approx_regs before args) in
D.set res a before
| Iload chunk addr args dst s =>
D.set dst (approx_of_chunk chunk) before
| Icall sig ros args res s =>
D.set res Unknown before
| Ibuiltin ef args res s =>
D.set res Unknown before
| _ =>
before
end
end.
(** The static analysis is a forward dataflow analysis. *)
Module DS := Dataflow_Solver(D)(NodeSetForward).
Definition analyze (f: RTL.function): PMap.t D.t :=
match DS.fixpoint (successors f) (transfer f)
((f.(fn_entrypoint), D.top) :: nil) with
| None => PMap.init D.top
| Some res => res
end.
(** * Code transformation *)
(** Cast operations that have no effect (because the argument is already
in the right range) are turned into moves. *)
Function transf_operation (op: operation) (vl: list approx) : operation :=
match op, vl with
| Ocast8signed, v :: nil => if Approx.bge Int8s v then Omove else op
| Ocast8unsigned, v :: nil => if Approx.bge Int8u v then Omove else op
| Ocast16signed, v :: nil => if Approx.bge Int16s v then Omove else op
| Ocast16unsigned, v :: nil => if Approx.bge Int16u v then Omove else op
| Osingleoffloat, v :: nil => if Approx.bge Single v then Omove else op
| _, _ => op
end.
Definition transf_instr (app: D.t) (instr: instruction) :=
match instr with
| Iop op args res s =>
let op' := transf_operation op (approx_regs app args) in
Iop op' args res s
| _ =>
instr
end.
Definition transf_code (approxs: PMap.t D.t) (instrs: code) : code :=
PTree.map (fun pc instr => transf_instr approxs!!pc instr) instrs.
Definition transf_function (f: function) : function :=
let approxs := analyze f in
mkfunction
f.(fn_sig)
f.(fn_params)
f.(fn_stacksize)
(transf_code approxs f.(fn_code))
f.(fn_entrypoint).
Definition transf_fundef (fd: fundef) : fundef :=
AST.transf_fundef transf_function fd.
Definition transf_program (p: program) : program :=
transform_program transf_fundef p.
|