Project

General

Profile

Revision 45c13277 src/optimize_machine.ml

View differences:

src/optimize_machine.ml
117 117
let optimize_machines machines =
118 118
  List.map optimize_machine machines
119 119

  
120
(* variable substitution for optimizing purposes *)
121

  
122
(* checks whether an [instr] is skip and can be removed from program *)
123
let rec instr_is_skip instr =
124
  match instr with
125
  | MLocalAssign (i, LocalVar v) when i = v -> true
126
  | MStateAssign (i, StateVar v) when i = v -> true
127
  | MBranch (g, hl) -> List.for_all (fun (_, il) -> instrs_are_skip il) hl
128
  | _               -> false
129
and instrs_are_skip instrs =
130
  List.for_all instr_is_skip instrs
131

  
132
let instr_cons instr cont =
133
 if instr_is_skip instr then cont else instr::cont
134

  
135
let rec instr_remove_skip instr cont =
136
  match instr with
137
  | MLocalAssign (i, LocalVar v) when i = v -> cont
138
  | MStateAssign (i, StateVar v) when i = v -> cont
139
  | MBranch (g, hl) -> MBranch (g, List.map (fun (h, il) -> (h, instrs_remove_skip il [])) hl) :: cont
140
  | _               -> instr::cont
141

  
142
and instrs_remove_skip instrs cont =
143
  List.fold_right instr_remove_skip instrs cont
144

  
145
let rec value_replace_var fvar value =
146
  match value with
147
  | Cst c -> value
148
  | LocalVar v -> LocalVar (fvar v)
149
  | StateVar v -> value
150
  | Fun (id, args) -> Fun (id, List.map (value_replace_var fvar) args) 
151
  | Array vl -> Array (List.map (value_replace_var fvar) vl)
152
  | Access (t, i) -> Access(value_replace_var fvar t, i)
153
  | Power (v, n) -> Power(value_replace_var fvar v, n)
154

  
155
let rec instr_replace_var fvar instr cont =
156
  match instr with
157
  | MLocalAssign (i, v) -> instr_cons (MLocalAssign (fvar i, value_replace_var fvar v)) cont
158
  | MStateAssign (i, v) -> instr_cons (MStateAssign (i, value_replace_var fvar v)) cont
159
  | MReset i            -> instr_cons instr cont
160
  | MStep (il, i, vl)   -> instr_cons (MStep (List.map fvar il, i, List.map (value_replace_var fvar) vl)) cont
161
  | MBranch (g, hl)     -> instr_cons (MBranch (value_replace_var fvar g, List.map (fun (h, il) -> (h, instrs_replace_var fvar il [])) hl)) cont
162

  
163
and instrs_replace_var fvar instrs cont =
164
  List.fold_right (instr_replace_var fvar) instrs cont
165

  
166
let step_replace_var fvar step =
167
  (* Some outputs may have been replaced by locals.
168
     We then need to rename those outputs
169
     without changing their clocks, etc *)
170
  let outputs' =
171
    List.map (fun o -> { o with var_id = (fvar o).var_id }) step.step_outputs in
172
  let locals'  =
173
    List.fold_left (fun res l ->
174
      let l' = fvar l in
175
      if List.exists (fun o -> o.var_id = l'.var_id) outputs'
176
      then res
177
      else Utils.add_cons l' res)
178
      [] step.step_locals in
179
  { step with
180
    step_checks = List.map (fun (l, v) -> (l, value_replace_var fvar v)) step.step_checks;
181
    step_outputs = outputs';
182
    step_locals = locals';
183
    step_instrs = instrs_replace_var fvar step.step_instrs [];
184
}
185

  
186
let rec machine_replace_variables fvar m =
187
  { m with
188
    mstep = step_replace_var fvar m.mstep
189
  }
190

  
191
let machine_reuse_variables m reuse =
192
  let fvar v =
193
    try
194
      Hashtbl.find reuse v.var_id
195
    with Not_found -> v in
196
  machine_replace_variables fvar m
197

  
198
let machines_reuse_variables prog node_schs =
199
  List.map 
200
    (fun m -> 
201
      machine_reuse_variables m (Utils.IMap.find m.mname.node_id node_schs).Scheduling.reuse_table
202
    ) prog
203

  
120 204

  
121 205
(* Local Variables: *)
122 206
(* compile-command:"make -C .." *)

Also available in: Unified diff