2010-06-15 10:49:03 +02:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Heptagon *)
|
|
|
|
(* *)
|
|
|
|
(* Author : Marc Pouzet *)
|
|
|
|
(* Organization : Demons, LRI, University of Paris-Sud, Orsay *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
(* control optimisation *)
|
|
|
|
|
2010-07-23 19:45:19 +02:00
|
|
|
open Idents
|
2010-06-15 10:49:03 +02:00
|
|
|
open Misc
|
2010-07-08 17:17:00 +02:00
|
|
|
open Obc
|
2011-02-14 15:21:57 +01:00
|
|
|
open Obc_utils
|
2010-07-23 22:06:06 +02:00
|
|
|
open Clocks
|
2011-04-26 18:02:18 +02:00
|
|
|
open Signature
|
2011-04-14 11:53:39 +02:00
|
|
|
open Obc_mapfold
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-04-26 18:02:18 +02:00
|
|
|
let appears_in_exp, appears_in_lhs =
|
|
|
|
let lhsdesc _ (x, acc) ld = match ld with
|
|
|
|
| Lvar y -> ld, (x, acc or (x=y))
|
|
|
|
| Lmem y -> ld, (x, acc or (x=y))
|
|
|
|
| _ -> raise Errors.Fallback
|
|
|
|
in
|
|
|
|
let funs = { Obc_mapfold.defaults with lhsdesc = lhsdesc } in
|
|
|
|
let appears_in_exp x e =
|
|
|
|
let _, (_, acc) = exp_it funs (x, false) e in
|
|
|
|
acc
|
|
|
|
in
|
|
|
|
let appears_in_lhs x l =
|
|
|
|
let _, (_, acc) = lhs_it funs (x, false) l in
|
|
|
|
acc
|
|
|
|
in
|
|
|
|
appears_in_exp, appears_in_lhs
|
|
|
|
|
|
|
|
let used_vars e =
|
|
|
|
let add x acc = if List.mem x acc then acc else x::acc in
|
|
|
|
let lhsdesc funs acc ld = match ld with
|
|
|
|
| Lvar y -> ld, add y acc
|
|
|
|
| Lmem y -> ld, add y acc
|
|
|
|
| _ -> raise Errors.Fallback
|
|
|
|
in
|
|
|
|
let funs = { Obc_mapfold.defaults with lhsdesc = lhsdesc } in
|
|
|
|
let _, vars = Obc_mapfold.exp_it funs [] e in
|
|
|
|
vars
|
|
|
|
|
|
|
|
let rec is_modified_by_call x args e_list = match args, e_list with
|
|
|
|
| [], [] -> false
|
|
|
|
| a::args, e::e_list ->
|
|
|
|
if Linearity.is_linear a.a_linearity && appears_in_exp x e then
|
|
|
|
true
|
|
|
|
else
|
|
|
|
is_modified_by_call x args e_list
|
|
|
|
| _, _ -> assert false
|
|
|
|
|
|
|
|
let is_modified_handlers j x handlers =
|
|
|
|
let act _ acc a = match a with
|
|
|
|
| Aassgn(l, _) -> a, acc or (appears_in_lhs x l)
|
|
|
|
| Acall (name_list, o, Mstep, e_list) ->
|
|
|
|
(* first, check if e is one of the output of the function*)
|
|
|
|
if List.exists (appears_in_lhs x) name_list then
|
|
|
|
a, true
|
|
|
|
else (
|
|
|
|
let sig_info = find_obj (obj_ref_name o) j in
|
|
|
|
a, acc or (is_modified_by_call x sig_info.node_inputs e_list)
|
|
|
|
)
|
|
|
|
| _ -> raise Errors.Fallback
|
|
|
|
in
|
|
|
|
let funs = { Obc_mapfold.defaults with act = act } in
|
|
|
|
List.exists (fun (_, b) -> snd (block_it funs false b)) handlers
|
|
|
|
|
|
|
|
let is_modified_handlers j e handlers =
|
|
|
|
let vars = used_vars e in
|
|
|
|
List.exists (fun x -> is_modified_handlers j x handlers) vars
|
|
|
|
|
2010-07-22 09:36:22 +02:00
|
|
|
let fuse_blocks b1 b2 =
|
|
|
|
{ b1 with b_locals = b1.b_locals @ b2.b_locals;
|
|
|
|
b_body = b1.b_body @ b2.b_body }
|
|
|
|
|
2010-06-15 10:49:03 +02:00
|
|
|
let rec find c = function
|
|
|
|
| [] -> raise Not_found
|
|
|
|
| (c1, s1) :: h ->
|
|
|
|
if c = c1 then s1, h else let s, h = find c h in s, (c1, s1) :: h
|
|
|
|
|
2010-07-08 17:17:00 +02:00
|
|
|
let is_deadcode = function
|
|
|
|
| Aassgn (lhs, e) ->
|
|
|
|
(match e.e_desc with
|
2011-07-21 11:54:52 +02:00
|
|
|
| Eextvalue w -> Obc_compare.compare_lhs_extvalue lhs w = 0
|
2010-07-08 17:17:00 +02:00
|
|
|
| _ -> false
|
2010-06-26 16:53:25 +02:00
|
|
|
)
|
2010-09-14 09:39:02 +02:00
|
|
|
| Acase (_, []) -> true
|
2010-07-22 09:36:22 +02:00
|
|
|
| Afor(_, _, _, { b_body = [] }) -> true
|
2010-07-08 17:17:00 +02:00
|
|
|
| _ -> false
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-04-26 18:02:18 +02:00
|
|
|
let rec joinlist j l =
|
2010-07-08 17:17:00 +02:00
|
|
|
let l = List.filter (fun a -> not (is_deadcode a)) l in
|
|
|
|
match l with
|
|
|
|
| [] -> []
|
|
|
|
| [s1] -> [s1]
|
|
|
|
| s1::s2::l ->
|
|
|
|
match s1, s2 with
|
|
|
|
| Acase(e1, h1),
|
2011-10-05 10:49:51 +02:00
|
|
|
Acase(e2, h2) when Obc_compare.exp_compare e1 e2 = 0 ->
|
2011-04-26 18:02:18 +02:00
|
|
|
if is_modified_handlers j e1 h1 then
|
|
|
|
s1::(joinlist j (s2::l))
|
|
|
|
else
|
|
|
|
joinlist j ((Acase(e1, joinhandlers j h1 h2))::l)
|
|
|
|
| s1, s2 -> s1::(joinlist j (s2::l))
|
2010-06-15 10:49:03 +02:00
|
|
|
|
2011-04-26 18:02:18 +02:00
|
|
|
and join_block j b =
|
|
|
|
{ b with b_body = joinlist j b.b_body }
|
2010-07-22 09:36:22 +02:00
|
|
|
|
2011-04-26 18:02:18 +02:00
|
|
|
and joinhandlers j h1 h2 =
|
2010-06-15 10:49:03 +02:00
|
|
|
match h1 with
|
|
|
|
| [] -> h2
|
|
|
|
| (c1, s1) :: h1' ->
|
|
|
|
let s1', h2' =
|
2010-07-22 09:36:22 +02:00
|
|
|
try let s2, h2'' = find c1 h2 in fuse_blocks s1 s2, h2''
|
2010-07-08 17:17:00 +02:00
|
|
|
with Not_found -> s1, h2 in
|
2011-04-26 18:02:18 +02:00
|
|
|
(c1, join_block j s1') :: joinhandlers j h1' h2'
|
|
|
|
|
|
|
|
let block _ j b =
|
|
|
|
{ b with b_body = joinlist j b.b_body }, j
|
2011-04-14 11:53:39 +02:00
|
|
|
|
2011-04-26 18:02:18 +02:00
|
|
|
let class_def funs acc cd =
|
|
|
|
let cd, _ = Obc_mapfold.class_def funs cd.cd_objs cd in
|
|
|
|
cd, acc
|
2011-04-14 11:53:39 +02:00
|
|
|
|
|
|
|
let program p =
|
2011-04-26 18:02:18 +02:00
|
|
|
let p, _ = program_it { defaults with class_def = class_def; block = block } [] p in
|
2011-04-14 11:53:39 +02:00
|
|
|
p
|