summaryrefslogtreecommitdiff
path: root/bytecomp/heuristic.ml
blob: 7736a04628c49ce6d002afd25a9636bdd0fc8ac4 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
(***********************************************************************)
(*                                                                     *)
(*                           Objective Caml                            *)
(*                                                                     *)
(*            Luc Maranget, projet Moscova, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 2008 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(* $Id$ *)

(** Heuristics for matching *)

open Typedtree
open Discr
open Printf

type occ = int list

let print_occ =
  let rec p_rec chan = function
    | [] -> ()
    | [x]  -> fprintf chan "%i" x
    | x::xs  -> fprintf chan "%i.%a" x p_rec xs in
  fun chan xs ->
    fprintf chan "<%a>" p_rec (List.rev xs)


(*************************************)
(* put column no k in first position *)
(*************************************)

let rec swap k x = function
  | [] -> assert false
  | y::ys ->
      if k = 1 then y,x::ys
      else
	let z,ys = swap (k-1) x ys in
	z,y::ys

let swap k xs = match k with
| 1 -> xs
| _ ->
    match xs with
    | x::xs ->
	let z,xs = swap (k-1) x xs in
	z::xs
    | [] -> assert false

let swap_all k xs pss =
  let xs = swap k xs
  and pss = List.map (fun (ps,e) -> swap k ps,e) pss in
  xs,pss

(**************************************)
(* Bricks to build/combine heuristics *)
(**************************************)

let mapi f xs =
  let rec map_rec i = function
    | [] -> []
    | x::xs -> f i x::map_rec (i+1) xs in
  map_rec 0 xs



let get_mins name ws =
  if Matchcommon.verbose > 0 then begin
    eprintf "%s: " name ;
    List.iter (fun (k,w) -> eprintf "<%i,%i>" k w) ws
  end ;
  let ws =
    List.sort
      (fun (i1,w1) (i2,w2) ->
	let c = Pervasives.compare w1 w2 in
	if c=0 then Pervasives.compare i1 i2 else c)
      ws in
  let ks =
    match ws with
    | [] -> assert false
    | (i,w)::rem ->
	let rec do_rec = function
	  | [] -> []
	  | (ii,ww)::rem ->
	      if w=ww then ii::do_rec rem
	      else [] in
	i::do_rec rem in
  if Matchcommon.verbose > 0 then begin
    eprintf " -> " ;
    List.iter (fun k -> eprintf "<%i>" k) ks ;
    prerr_newline ()
  end ;
  ks

let compose h1 h2 ks = match h1 ks with
| [] -> assert false
| [k] -> k
| ks -> h2 ks


(* Generic heuristic that look at one column only *)

let extract_first_col pss =
  List.fold_right
    (fun ps (fst,rest)-> match ps with
    | p::ps -> p::fst,ps::rest
    | [] -> assert false)
    pss ([],[])

let conses ps qss = List.map2 (fun x xs -> x::xs) ps qss
and appends pss qss = List.map2 (@) pss qss

let generic name score = 
  let rec do_rec  i right ks xs = match ks,xs with
    | [],_ -> []
    | k::rk,_::xs ->
	let ps,qss = extract_first_col right in
	if i < k then
	  do_rec (i+1) qss ks xs
	else
	  (k,score ps)::do_rec (i+1) qss rk xs
    | _ -> assert false in
  fun xs pss ks ->
    let ws =  do_rec 1 pss ks xs in
    get_mins name ws



(********************************)
(* Compute 'partial' directions *)
(********************************)


let generic2 name score =
  let rec do_rec i left right ks xs = match ks,xs with
    | [],_ -> []
    | k::rk,_::xs ->
	let col,right = extract_first_col right in
	if i < k then
	  do_rec (i+1) (conses col left) right ks xs
	else
	  let d = score col (appends left right) in
	  (k,d)::do_rec (i+1) (conses col left) right rk xs
    | _ -> assert false in
  fun xs pss ks ->
    let ws =  do_rec 1 (List.map (fun _ -> []) pss) pss ks xs in
    get_mins name ws


(* Does omega matches a pattern ? *)
let rec match_omega p = match p.pat_desc with
  | Tpat_any|Tpat_var _ -> true
  | Tpat_alias (p,_)
  | Tpat_or (p,_,_) -> match_omega p
  | (Tpat_array _|Tpat_record _|Tpat_variant (_, _, _)
  | Tpat_construct (_, _)| Tpat_tuple _|Tpat_constant _)
      -> false

let direction col pss =
  let rec do_rec n col qss pss = match col,pss with
  | [],[] -> 0
  | c::col, ps::pss ->
      if match_omega c then begin
	if Parmatch.satisfiable qss ps then 0
	else begin
	  do_rec (n+1) col (ps::qss) pss - 1
	end
      end else 
	do_rec (n+1) col (ps::qss) pss - 1
  | _ -> assert false in
  do_rec 0 col [] pss


let directions = generic2 "DIRECTIONS" direction
      
(************************)
(* Counted needed lines *)
(************************)

let needed_one col pss =
  let rec do_rec n col qss pss = match col,pss with
  | [],[] -> 0
  | c::col, ps::pss ->
      if match_omega c then begin
	if Parmatch.satisfiable qss ps then
	  do_rec (n+1) col (ps::qss) pss
	else begin
	  do_rec (n+1) col (ps::qss) pss - 1
	end
      end else 
	do_rec (n+1) col (ps::qss) pss - 1
  | _ -> assert false in
  do_rec 0 col [] pss

let needed = generic2 "NEEDED" needed_one

(*************)
(* Leaf edge *)
(*************)

let leaf_here qss = match qss with
| (qs,_)::_ -> List.for_all match_omega qs
| []    -> true

let leafedge col pss =
  let cs =  Discr.collect_ps col in
  let pss = conses col pss in
  let mat = List.map (fun ps -> ps,()) pss in
  DSet.fold
    (fun c r ->
      if leaf_here (Discr.specialize c mat) then
	r-1
      else
	r)
    cs
    (if Discr.has_default cs && leaf_here (Discr.default mat) then
	-1
      else
	0)


let leafedges = generic2 "LEAFEDGE" leafedge


(********************************************)
(* Minimize specialized/default matrix size *)
(********************************************)

(* size is approximated by the number of rows *)  

let rec nwilds_pat p = match p.pat_desc with
| Tpat_any | Tpat_var _ -> 1
| Tpat_alias (p,_) -> nwilds_pat p
| Tpat_or (p1,p2,_) -> nwilds_pat p1 + nwilds_pat p2
| Tpat_construct _ | Tpat_variant _ 
| Tpat_array _|Tpat_record _|Tpat_tuple _|Tpat_constant _ -> 0

let rec nwilds ps =
  List.fold_left
    (fun r p -> r+nwilds_pat p)
    0 ps

let nmats ps =
  let ds = Discr.collect_ps ps in
  match DSet.cardinal ds with
  | 0 -> 1
  | n -> n + (if Discr.has_default ds then 1 else 0)

let ndups col = nwilds col * nmats col

let rec dupconstr_pat p = match p.pat_desc with
  | Tpat_any|Tpat_var _ -> 0
  | Tpat_alias (p,_) -> dupconstr_pat p
  | Tpat_or (p1,p2,_) -> dupconstr_pat p1 + dupconstr_pat p2
  | (Tpat_array _|Tpat_record _|Tpat_variant (_, _, _)
  | Tpat_construct (_, _)| Tpat_tuple _|Tpat_constant _)
      -> 1

let dupconstr col =
  List.fold_left
    (fun r p -> r + dupconstr_pat p)
    0 col

      
let nrows = generic "NROWS" (fun ps -> ndups ps+dupconstr ps)


let ndefaults = generic "DEFAULTS" nwilds


let nbranchs = generic "NBRANCH" nmats


let is_constr p = not (match_omega p)

let first =
  generic "FIRST"
    (fun ps -> match ps with
    | p::_ -> if is_constr p then -1 else 0
    | [] -> assert false)


let arity ps =
  let ds = Discr.collect_ps ps in
  DSet.fold
    (fun c r -> r + Discr.arity c)
    ds 0

let arities = generic "ARITIES" arity

let rec prefix_one ps = match ps with
| p::ps when is_constr p -> 1 + prefix_one ps
| _ -> 0

let prefix =
  generic "PREFIX"
    (fun ps -> -prefix_one ps)

(******************************************)
(* Select minimal variable, this enforces *)
(* irrelevance of previous column swaps   *)
(******************************************)

let as_some = function
  | Some (k,_) -> k
  | None -> assert false

let cmp_opt cmp oo k2 o2 = match oo with
| Some (k1,o1) when cmp o1 o2 -> oo
| _ -> Some (k2,o2)

let min_occs cmp  =
  let rec do_rec min i os ks = match ks,os with
  | [],_ -> as_some min
  | k::rk,o::os ->
      if i < k then
	do_rec min (i+1) os ks
      else
	do_rec (cmp_opt cmp min k o) (i+1) os rk
  | _,_ -> assert false in
  fun os ks -> do_rec None 1 os ks


let cmp_rev cmp o1 o2 = cmp (List.rev o1) (List.rev o2)

let rec cmp_left o1 o2 = match o1,o2 with
| i1::o1,i2::o2 ->
    if i1 < i2 then true
    else if i2 < i1 then false
    else cmp_left o1 o2
| _,_ -> assert false

let cmp_lr o1 o2 = 
  let l1 = List.length o1 and l2 = List.length o2 in
  if l1 < l2 then true
  else if l1 > l2 then false
  else cmp_rev cmp_left o1 o2

let cmp_rl o1 o2 = 
  let l1 = List.length o1 and l2 = List.length o2 in
  if l1 < l2 then true
  else if l1 > l2 then false
  else cmp_rev cmp_left o2 o1

let left = min_occs (cmp_rev cmp_left)
let leftright =  min_occs cmp_lr
let rightleft = min_occs cmp_rl

(*************************)
(* Heuristic definitions *)
(*************************)

let exists_constr ps = List.exists is_constr ps

let withconstr xs pss =
  let rec do_rec  k right xs = match xs with
    | [] -> []
    | _::xs ->
	let ps,qss = extract_first_col right in
	if exists_constr ps then
	  k::do_rec (k+1) qss xs
	else
	  do_rec (k+1) qss xs in
  let ws =  do_rec 1 pss  xs in
  ws

module C = Matchcommon

let build h xs mat =
  let rec do_rec i =
    if i >= String.length h then
      left xs
    else
      let h2 = do_rec (i+1)
      and h1 = match h.[i] with
      | 'a' -> arities xs mat
      | 'b' -> nbranchs xs mat
      | 'd' -> ndefaults xs mat
      | 'f' -> first xs mat
      | 'l' -> leafedges xs mat
      | 'n' -> needed xs mat
      | 'p' -> directions xs mat
      | 'q' -> prefix xs mat
      | 'r' -> nrows xs mat
      | 'N' -> (fun ks -> [left xs ks])
      | 'L' -> (fun ks -> [leftright xs ks])
      | 'R' -> (fun ks -> [rightleft xs ks])
      | 'i' -> (fun x -> x)
      | c -> Misc.fatal_error (sprintf "Bad heuristic: %c" c) in
      compose h1 h2 in
  do_rec 0


let best_column xs pss = match xs with
|[]|[_]  -> assert false
| _ ->
    let pss = List.map fst pss
    and xs = List.map (fun (_,_,_,occ) -> occ) xs in
    let ks = withconstr xs pss in
    match ks with
    | [] -> 1
    | [k] -> k
    | _ ->
	if C.verbose > 0 then begin
	  prerr_endline "** HEURISTICS **" ;
	  List.iter
	    (fun occ ->  eprintf " %a" print_occ  occ)
	    xs ;
	  prerr_newline () ;
	  Parmatch.pretty_matrix pss
	end ;
	let k =
	  match C.heuristic with
	  | C.No -> left xs ks
	  | C.Opt -> build "qba" xs pss ks
	  | C.Custom h -> build h xs pss ks in
	if C.verbose > 0 then begin
	  eprintf "Selected: %i\n" k ;
	  flush stderr
	end ;
	k

let opt xs pss =  match xs with
|[]|[_] -> xs,pss
| _ ->
    let k = best_column xs pss in
    swap_all k xs pss