Project

General

Profile

« Previous | Next » 

Revision 17d63fff

Added by LĂ©lio Brun 10 months ago

remove unused variables after tag elimination in machine code (-O >= 3)

View differences:

src/causality.mli
40 40
     program/schedule, but used to simplify causality analysis *)
41 41
  val mk_instance_var : ident -> ident
42 42

  
43
  val mk_read_var : ident -> ident
44

  
43 45
  val is_instance_var : ident -> bool
44 46

  
45 47
  val is_ghost_var : ident -> bool
src/checks/liveness.mli
1 1
open Utils
2 2
open Lustre_types
3 3

  
4
val cone_of_influence: IdentDepGraph.t -> string -> ISet.t
5

  
4 6
val compute_unused_variables : node_desc -> IdentDepGraph.t -> ISet.t
5 7

  
6 8
type fanin = (ident, tag) Hashtbl.t
src/dune
107 107
  misc_lustre_function
108 108
  misc_printer
109 109
  machine_code
110
  machine_code_dep
110 111
  causality
111 112
  scheduling
112 113
  liveness
src/machine_code_dep.ml
1
open Lustre_types
2
open Machine_code_types
3
open Corelang
4
open Utils
5

  
6
let rec add_expr_dependencies inputs g x e = match e.value_desc with
7
  | Var y ->
8
    let y' = y.var_id in
9
    IdentDepGraph.add_edge g x.var_id
10
      (if List.exists (fun z -> z.var_id = y') inputs
11
       then Causality.ExprDep.mk_read_var y' else y')
12
  | Fun (_, es)
13
  | Array es ->
14
    List.iter (add_expr_dependencies inputs g x) es
15
  | Access (e1, e2)
16
  | Power (e1, e2) ->
17
    add_expr_dependencies inputs g x e1;
18
    add_expr_dependencies inputs g x e2
19
  | _ -> ()
20

  
21
let rec add_instr_dependencies inputs g deps instr = match get_instr_desc instr with
22
  | MLocalAssign (x, e)
23
  | MStateAssign (x, e) ->
24
    add_expr_dependencies inputs g x e;
25
    List.iter (add_expr_dependencies inputs g x) deps
26
  | MStep (xs, _, es) ->
27
    List.iter (fun x ->
28
        List.iter (add_expr_dependencies inputs g x) es;
29
        List.iter (add_expr_dependencies inputs g x) deps) xs
30
  | MBranch (e, hl) ->
31
    List.iter (fun (_, l) -> List.iter (add_instr_dependencies inputs g (e :: deps)) l) hl
32
  | _ -> ()
33

  
34
let dep_graph m =
35
  let g = IdentDepGraph.create () in
36
  List.iter (add_instr_dependencies m.mstep.step_inputs g []) m.mstep.step_instrs;
37
  List.iter (fun x -> IdentDepGraph.add_edge g Causality.world x.var_id) m.mstep.step_outputs;
38
  g
39

  
40
let compute_unused_variables m =
41
  let g = dep_graph m in
42
  List.fold_left
43
    (fun unused x ->
44
       (* let coi = Liveness.cone_of_influence g x.var_id in *)
45
       (* Format.printf "unused: %a@;coi of %s: %a@." ISet.pp unused x.var_id ISet.pp coi; *)
46
       ISet.diff unused (Liveness.cone_of_influence g x.var_id))
47
    (List.fold_left (fun s v -> ISet.add v.var_id s) ISet.empty (m.mstep.step_locals @ m.mstep.step_inputs))
48
    m.mstep.step_outputs
src/machine_code_dep.mli
1
val compute_unused_variables : Machine_code_types.machine_t -> Utils.ISet.t
src/optimize_machine.ml
900 900

  
901 901
let rec instr_reduce branches instr1 cont =
902 902
  match get_instr_desc instr1 with
903
  | MLocalAssign (_, { value_desc = Cst (Const_tag c); _ }) ->
904
    instr1 :: (List.assoc c branches @ cont)
903
  | MLocalAssign (_, { value_desc = Cst (Const_tag c); _ })
905 904
  | MStateAssign (_, { value_desc = Cst (Const_tag c); _ }) ->
906 905
    instr1 :: (List.assoc c branches @ cont)
907 906
  | MBranch (g, hl) ->
......
938 937
let step_fusion step =
939 938
  { step with step_instrs = instrs_fusion step.step_instrs }
940 939

  
941
let machine_fusion m = { m with mstep = step_fusion m.mstep }
940
let machine_fusion m =
941
  let m = { m with mstep = step_fusion m.mstep } in
942
  let unused = Machine_code_dep.compute_unused_variables m in
943
  let is_used v = not (ISet.mem v.var_id unused) in
944
  let step_locals = List.filter is_used m.mstep.step_locals in
945
  let rec filter_instrs instrs =
946
    List.filter_map (fun instr -> match get_instr_desc instr with
947
        | MLocalAssign (v, _) ->
948
          if is_used v then Some instr else None
949
        | MBranch (e, hl) ->
950
          Some (update_instr_desc instr
951
                  (MBranch (e, List.map (fun (h, l) -> h, filter_instrs l) hl)))
952
        | _ -> Some instr) instrs
953
  in
954
  let step_instrs = filter_instrs m.mstep.step_instrs in
955
  { m with mstep = { m.mstep with step_locals; step_instrs }}
956
  (* List.iter (fun (g, u) -> Format.printf "%a@;%a@." pp_dep_graph g ISet.pp u) gs; *)
942 957

  
943 958
let machines_fusion prog = List.map machine_fusion prog
944 959

  
......
1100 1115
        Scheduling.remove_prog_inlined_locals removed_table node_schs
1101 1116
      in
1102 1117
      let reuse_tables = Scheduling.compute_prog_reuse_table node_schs in
1103
      machines_fusion (machines_reuse_variables reuse_tables machine_code))
1118
      let machine_code = machines_fusion (machines_reuse_variables reuse_tables machine_code) in
1119
      machine_code)
1104 1120
    else machine_code
1105 1121
  in
1106 1122

  

Also available in: Unified diff