constprop.ml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. (**
  2. * The compiler sometimes generates variables of the form foo$$1, to make sure
  3. * that expressions are only executed once. In many cases, this leads to
  4. * over-complex constructions, for example when converting for-loops to
  5. * while-loops. We use the knowledge of these variables being constant by
  6. * propagation the constant values to their occurrences, and then apply
  7. * arithmetic simplification to operators to reduce the size and complexity of
  8. * the generated code. Note that this can only be applied to constants. For
  9. * variables in general, some form of liveness analysis would be required (e.g.
  10. * Static Single Assignment form). Expressions can only be propagated when they
  11. * have no side effects, i.e. when they do not contain function calls.
  12. *
  13. * Constant propagation is merged with some some arithmetic simplification here,
  14. * specifically targeting optimization oppertunities created bij earlier
  15. * constant propagation. This is utilized, for example, in array index
  16. * calculation when array dimensions are constant.
  17. *)
  18. open Types
  19. open Util
  20. let is_const_name name =
  21. Str.string_match (Str.regexp "^.+\\$\\$[0-9]+$") name 0
  22. let is_const = function
  23. | Const _
  24. | VarUse (_, None, _) -> true
  25. | _ -> false
  26. (* Play-it-safe side effect analysis: only return true for variables and
  27. * constants, since these are targeted in arithmetic simplification (in
  28. * particular targeting array indices that can be simplified after array
  29. * dimension reduction). *)
  30. let no_side_effect = function
  31. | VarUse _ | Const _ | Var _ -> true
  32. | _ -> false
  33. (* Constand folding *)
  34. let eval = function
  35. (* Binop - arithmetic *)
  36. | Binop (Add, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  37. Const (IntVal (left + right), ann)
  38. | Binop (Add, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  39. Const (FloatVal (left +. right), ann)
  40. | Binop (Sub, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  41. Const (IntVal (left - right), ann)
  42. | Binop (Sub, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  43. Const (FloatVal (left -. right), ann)
  44. | Binop (Mul, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  45. Const (IntVal (left * right), ann)
  46. | Binop (Mul, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  47. Const (FloatVal (left *. right), ann)
  48. | Binop (Div, Const (IntVal left, _), Const (IntVal right, _), ann) when right != 0 ->
  49. Const (IntVal (left / right), ann)
  50. | Binop (Div, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  51. Const (FloatVal (left /. right), ann)
  52. | Binop (Mod, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  53. Const (IntVal (left mod right), ann)
  54. (* Binop - relational *)
  55. | Binop (Eq, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  56. Const (BoolVal (left = right), ann)
  57. | Binop (Eq, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  58. Const (BoolVal (left = right), ann)
  59. | Binop (Ne, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  60. Const (BoolVal (left <> right), ann)
  61. | Binop (Ne, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  62. Const (BoolVal (left <> right), ann)
  63. | Binop (Gt, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  64. Const (BoolVal (left > right), ann)
  65. | Binop (Gt, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  66. Const (BoolVal (left > right), ann)
  67. | Binop (Lt, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  68. Const (BoolVal (left < right), ann)
  69. | Binop (Lt, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  70. Const (BoolVal (left < right), ann)
  71. | Binop (Ge, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  72. Const (BoolVal (left >= right), ann)
  73. | Binop (Ge, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  74. Const (BoolVal (left >= right), ann)
  75. | Binop (Le, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  76. Const (BoolVal (left <= right), ann)
  77. | Binop (Le, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  78. Const (BoolVal (left <= right), ann)
  79. (* Binop - logical *)
  80. | Binop (And, Const (BoolVal left, _), Const (BoolVal right, _), ann) ->
  81. Const (BoolVal (left && right), ann)
  82. | Binop (Or, Const (BoolVal left, _), Const (BoolVal right, _), ann) ->
  83. Const (BoolVal (left || right), ann)
  84. (* Monary operations *)
  85. | Monop (Not, Const (BoolVal value, _), ann) -> Const (BoolVal (not value), ann)
  86. | Monop (Neg, Const (IntVal value, _), ann) -> Const (IntVal (-value), ann)
  87. | Monop (Neg, Const (FloatVal value, _), ann) -> Const (FloatVal (-.value), ann)
  88. (* 0 * a --> 0 *)
  89. | Binop (Mul, Const (IntVal 0, _), other, ann)
  90. | Binop (Mul, other, Const (IntVal 0, _), ann) when no_side_effect other ->
  91. Const (IntVal 0, ann)
  92. (* 0 + a --> a *)
  93. | Binop (Add, Const (IntVal 0, _), other, _)
  94. | Binop (Add, other, Const (IntVal 0, _), _) ->
  95. other
  96. (* 1 * a --> a *)
  97. | Binop (Mul, Const (IntVal 1, _), other, _)
  98. | Binop (Mul, other, Const (IntVal 1, _), _) ->
  99. other
  100. (* true|false ? texp : fexp --> texp|fexp*)
  101. | Cond (Const (BoolVal value, _), texp, fexp, _) ->
  102. if value then texp else fexp
  103. | node -> node
  104. let rec propagate consts node =
  105. let propagate = propagate consts in
  106. match node with
  107. (* Constant assignments are added to constants table *)
  108. | Assign (name, None, value, ann) when is_const_name name ->
  109. let value = propagate value in
  110. if is_const value then begin
  111. Hashtbl.add consts name value;
  112. DummyNode
  113. end else
  114. Assign (name, None, value, ann)
  115. | VarLet (dec, None, value, ann) when is_const_name (nameof dec) ->
  116. let value = propagate value in
  117. if is_const value then begin
  118. Hashtbl.add consts (nameof dec) value;
  119. DummyNode
  120. end else
  121. VarLet (dec, None, value, ann)
  122. (* Variables that are in the constant table are replaced with their constant
  123. * value *)
  124. | Var (name, None, ann) when Hashtbl.mem consts name ->
  125. Hashtbl.find consts name
  126. | VarUse (dec, None, ann) when Hashtbl.mem consts (nameof dec) ->
  127. Hashtbl.find consts (nameof dec)
  128. | Dim (name, ann) when Hashtbl.mem consts name ->
  129. Hashtbl.find consts name
  130. (* Apply arithmetic simplification to constant operands *)
  131. | Monop (op, opnd, ann) ->
  132. eval (Monop (op, propagate opnd, ann))
  133. | Binop (op, left, right, ann) ->
  134. eval (Binop (op, propagate left, propagate right, ann))
  135. | Cond (cond, texp, fexp, ann) ->
  136. eval (Cond (propagate cond, propagate texp, propagate fexp, ann))
  137. | TypeCast (ctype, value, ann) ->
  138. let value = propagate value in
  139. begin match (ctype, value) with
  140. | (Bool, Const (BoolVal value, _)) -> Const (BoolVal value, ann)
  141. | (Bool, Const (IntVal value, _)) -> Const (BoolVal (value != 1), ann)
  142. | (Bool, Const (FloatVal value, _)) -> Const (BoolVal (value != 1.0), ann)
  143. | (Int, Const (BoolVal value, _)) -> Const (IntVal (if value then 1 else 0), ann)
  144. | (Int, Const (IntVal value, _)) -> Const (IntVal value, ann)
  145. | (Int, Const (FloatVal value, _)) -> Const (IntVal (int_of_float value), ann)
  146. | (Float, Const (BoolVal value, _)) -> Const (FloatVal (if value then 1. else 0.), ann)
  147. | (Float, Const (IntVal value, _)) -> Const (FloatVal (float_of_int value), ann)
  148. | (Float, Const (FloatVal value, _)) -> Const (FloatVal value, ann)
  149. | _ -> TypeCast (ctype, value, ann)
  150. end
  151. | _ -> transform_children propagate node
  152. let rec prune_vardecs consts = function
  153. | VarDec (_, name, _, _) when Hashtbl.mem consts name -> DummyNode
  154. | node -> transform_children (prune_vardecs consts) node
  155. let propagate_consts node =
  156. let consts = Hashtbl.create 32 in
  157. let node = propagate consts node in
  158. prune_vardecs consts node
  159. let phase = function
  160. | Ast node -> Ast (propagate_consts node)
  161. | _ -> raise (InvalidInput "constant propagation")