Project

General

Profile

Revision b1655a21 src/optimize_machine.ml

View differences:

src/optimize_machine.ml
11 11

  
12 12
open LustreSpec 
13 13
open Corelang
14
open Causality
14 15
open Machine_code 
15 16

  
16 17
let rec eliminate elim instr =
......
201 202
      machine_reuse_variables m (Utils.IMap.find m.mname.node_id node_schs).Scheduling.reuse_table
202 203
    ) prog
203 204

  
205
let rec instr_assign res instr =
206
  match instr with
207
  | MLocalAssign (i, _) -> Disjunction.CISet.add i res
208
  | MStateAssign (i, _) -> Disjunction.CISet.add i res
209
  | MBranch (g, hl)     -> List.fold_left (fun res (h, b) -> instrs_assign res b) res hl
210
  | MStep (il, _, _)    -> List.fold_right Disjunction.CISet.add il res
211
  | _                   -> res
212

  
213
and instrs_assign res instrs =
214
  List.fold_left instr_assign res instrs
215

  
216
let rec instr_constant_assign var instr =
217
  match instr with
218
  | MLocalAssign (i, Cst (Const_tag _))
219
  | MStateAssign (i, Cst (Const_tag _)) -> i = var
220
  | MBranch (g, hl)                     -> List.for_all (fun (h, b) -> instrs_constant_assign var b) hl
221
  | _                                   -> false
222

  
223
and instrs_constant_assign var instrs =
224
  List.fold_left (fun res i -> if Disjunction.CISet.mem var (instr_assign Disjunction.CISet.empty i) then instr_constant_assign var i else res) false instrs
225

  
226
let rec instr_reduce branches instr1 cont =
227
  match instr1 with
228
  | MLocalAssign (_, Cst (Const_tag c)) -> instr1 :: (List.assoc c branches @ cont)
229
  | MStateAssign (_, Cst (Const_tag c)) -> instr1 :: (List.assoc c branches @ cont)
230
  | MBranch (g, hl)                     -> MBranch (g, List.map (fun (h, b) -> (h, instrs_reduce branches b [])) hl) :: cont
231
  | _                                   -> instr1 :: cont
232

  
233
and instrs_reduce branches instrs cont =
234
 match instrs with
235
 | []        -> cont
236
 | [i]       -> instr_reduce branches i cont
237
 | i1::i2::q -> i1 :: instrs_reduce branches (i2::q) cont
238

  
239
let rec instrs_fusion instrs =
240
  match instrs with
241
  | []
242
  | [_]                                                               ->
243
    instrs
244
  | i1::(MBranch (LocalVar v, hl))::q when instr_constant_assign v i1 ->
245
    instr_reduce (List.map (fun (h, b) -> h, instrs_fusion b) hl) i1 (instrs_fusion q)
246
  | i1::(MBranch (StateVar v, hl))::q when instr_constant_assign v i1 ->
247
    instr_reduce (List.map (fun (h, b) -> h, instrs_fusion b) hl) i1 (instrs_fusion q) 
248
  | i1::i2::q                                                         ->
249
    i1 :: instrs_fusion (i2::q)
250

  
251
let step_fusion step =
252
  { step with
253
    step_instrs = instrs_fusion step.step_instrs;
254
  }
255

  
256
let rec machine_fusion m =
257
  { m with
258
    mstep = step_fusion m.mstep
259
  }
260

  
261
let machines_fusion prog =
262
  List.map machine_fusion prog
204 263

  
205 264
(* Local Variables: *)
206 265
(* compile-command:"make -C .." *)

Also available in: Unified diff