unroll.ml 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. open Types
  2. open Util
  3. (* Only unroll if the resulting number of statements is at most 25 *)
  4. let may_be_unrolled i_values body =
  5. List.length i_values * List.length body <= 25
  6. let rec range i j step =
  7. if i >= j then [] else i :: (range (Int32.add i step) j step)
  8. let rec assigns name = function
  9. | VarLet (dec, _, _, _) -> nameof dec = name
  10. | _ -> false
  11. let rec replace_var name replacement = function
  12. | VarUse (VarDec (_, var, _, _), None, _) when var = name -> replacement
  13. | node -> traverse_unit (replace_var name replacement) node
  14. let rec get_body_step i rest = function
  15. | [] -> None
  16. | [VarLet (
  17. VarDec (Int, assigned, None, _), None,
  18. Binop (Add,
  19. VarUse (VarDec (Int, added, None, _), None, _),
  20. Const (IntVal step, _),
  21. _),
  22. _)] when assigned = added -> Some (step, List.rev rest)
  23. | hd :: tl -> get_body_step i (hd :: rest) tl
  24. let rec unroll_body counters = function
  25. | [] -> []
  26. (*
  27. * Look for the following pattern:
  28. * i = 0;
  29. * while (a < stop) {
  30. * <body>;
  31. * b = c + step;
  32. * }
  33. * where a = b = c = i and start, stop, step are integer constants and i is a
  34. * generated variable
  35. *)
  36. | (VarLet (VarDec (Int, i, None, _), None, Const (IntVal start, _), _) as init) ::
  37. (While (
  38. Binop (
  39. Lt,
  40. VarUse (VarDec (Int, comp, None, _), None, _),
  41. Const (IntVal stop, _),
  42. _),
  43. Block body,
  44. _) as loop) :: tl
  45. when is_generated_id i && comp = i ->
  46. begin
  47. match get_body_step i [] body with
  48. | Some (step, rest) ->
  49. (* First unroll inner loops in body before the may_be_unrolled check *)
  50. let rest = flatten_blocks (unroll_body counters rest) in
  51. let i_values = range start stop step in
  52. if may_be_unrolled i_values rest then begin
  53. Hashtbl.add counters i true;
  54. let dup_body value =
  55. replace_var i (Const (IntVal value, [Type Int])) (Block rest)
  56. in
  57. Block (List.map dup_body i_values) :: (unroll_body counters tl)
  58. end else
  59. init :: (unroll counters loop) :: (unroll_body counters tl)
  60. | None -> init :: (unroll counters loop) :: (unroll_body counters tl)
  61. end
  62. | hd :: tl -> (unroll counters hd) :: (unroll_body counters tl)
  63. and unroll counters = function
  64. | Block stats -> Block (unroll_body counters stats)
  65. | node -> traverse_unit (unroll counters) node
  66. let rec prune_vardecs counters = function
  67. | VarDec (_, name, _, _) when Hashtbl.mem counters name -> DummyNode
  68. | node -> traverse_unit (prune_vardecs counters) node
  69. let phase = function
  70. | Ast node ->
  71. let counters = Hashtbl.create 10 in
  72. Ast (unroll counters node |> prune_vardecs counters)
  73. | _ -> raise InvalidInput