constprop.ml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. (**
  2. * The compiler sometimes generates variables of the form __foo_1__, to make
  3. * sure 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 = function
  21. | Const _ -> true
  22. | VarUse (dec, None, _) -> is_const_id (nameof dec)
  23. | Var (name, _, _) -> is_const_id name
  24. | _ -> false
  25. (* Only assignments to variables local to this module can be removed, others
  26. * must stay for consistency when used by other modules *)
  27. let is_local_dec = function
  28. | VarDec _ -> true
  29. | GlobalDef (export, _, _, _, _) -> not export
  30. | _ -> false
  31. (* Play-it-safe side effect analysis: only return true for variables and
  32. * constants, since these are targeted in arithmetic simplification (in
  33. * particular targeting array indices that can be simplified after array
  34. * dimension reduction) *)
  35. let no_side_effect = function
  36. | Const _ | VarUse _ | Var _ -> true
  37. | _ -> false
  38. (* Constand folding + arithmetic popagation *)
  39. let eval node =
  40. (* Redefine integer operators within this module since they are only used on
  41. * IntVal values, which have type int32 *)
  42. let ( + ) = Int32.add in
  43. let ( - ) = Int32.sub in
  44. let ( / ) = Int32.div in
  45. let ( * ) = Int32.mul in
  46. let (mod) = Int32.rem in
  47. match node with
  48. (* Binop - arithmetic *)
  49. | Binop (Add, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  50. Const (IntVal (left + right), ann)
  51. | Binop (Add, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  52. Const (FloatVal (left +. right), ann)
  53. | Binop (Sub, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  54. Const (IntVal (left - right), ann)
  55. | Binop (Sub, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  56. Const (FloatVal (left -. right), ann)
  57. | Binop (Mul, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  58. Const (IntVal (left * right), ann)
  59. | Binop (Mul, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  60. Const (FloatVal (left *. right), ann)
  61. | Binop (Div, Const (IntVal left, _), Const (IntVal right, _), ann) when right <> 0l ->
  62. Const (IntVal (left / right), ann)
  63. | Binop (Div, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  64. Const (FloatVal (left /. right), ann)
  65. | Binop (Mod, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  66. Const (IntVal (left mod right), ann)
  67. (* Binop - relational *)
  68. | Binop (Eq, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  69. Const (BoolVal (left = right), ann)
  70. | Binop (Eq, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  71. Const (BoolVal (left = right), ann)
  72. | Binop (Ne, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  73. Const (BoolVal (left <> right), ann)
  74. | Binop (Ne, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  75. Const (BoolVal (left <> right), ann)
  76. | Binop (Gt, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  77. Const (BoolVal (left > right), ann)
  78. | Binop (Gt, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  79. Const (BoolVal (left > right), ann)
  80. | Binop (Lt, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  81. Const (BoolVal (left < right), ann)
  82. | Binop (Lt, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  83. Const (BoolVal (left < right), ann)
  84. | Binop (Ge, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  85. Const (BoolVal (left >= right), ann)
  86. | Binop (Ge, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  87. Const (BoolVal (left >= right), ann)
  88. | Binop (Le, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  89. Const (BoolVal (left <= right), ann)
  90. | Binop (Le, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  91. Const (BoolVal (left <= right), ann)
  92. (* Binop - logical *)
  93. | Binop (And, Const (BoolVal left, _), Const (BoolVal right, _), ann) ->
  94. Const (BoolVal (left && right), ann)
  95. | Binop (Or, Const (BoolVal left, _), Const (BoolVal right, _), ann) ->
  96. Const (BoolVal (left || right), ann)
  97. (* Monary operations *)
  98. | Monop (Not, Const (BoolVal value, _), ann) ->
  99. Const (BoolVal (not value), ann)
  100. | Monop (Neg, Const (IntVal value, _), ann) ->
  101. Const (IntVal (Int32.neg value), ann)
  102. | Monop (Neg, Const (FloatVal value, _), ann) ->
  103. Const (FloatVal (-.value), ann)
  104. (* 0 * a --> 0 *)
  105. | Binop (Mul, Const (IntVal 0l, _), other, ann)
  106. | Binop (Mul, other, Const (IntVal 0l, _), ann) when no_side_effect other ->
  107. Const (IntVal 0l, ann)
  108. (* 0 + a --> a *)
  109. | Binop (Add, Const (IntVal 0l, _), other, _)
  110. | Binop (Add, other, Const (IntVal 0l, _), _) ->
  111. other
  112. (* 1 * a --> a *)
  113. | Binop (Mul, Const (IntVal 1l, _), other, _)
  114. | Binop (Mul, other, Const (IntVal 1l, _), _) ->
  115. other
  116. (* true|false ? texp : fexp --> texp|fexp*)
  117. | Cond (Const (BoolVal value, _), texp, fexp, _) ->
  118. if value then texp else fexp
  119. | node -> node
  120. let rec propagate consts node =
  121. let propagate = propagate consts in
  122. match node with
  123. (* Constant assignments are added to constants table *)
  124. | Assign (name, None, value, ann) when is_const_id name ->
  125. let value = propagate value in
  126. if is_const value then begin
  127. Hashtbl.add consts name value;
  128. DummyNode
  129. end else
  130. Assign (name, None, value, ann)
  131. | VarLet (dec, None, value, ann) when is_const_id (nameof dec) ->
  132. let value = propagate value in
  133. if is_const value then begin
  134. Hashtbl.add consts (nameof dec) value;
  135. if is_local_dec dec then
  136. DummyNode
  137. else
  138. VarLet (dec, None, value, ann)
  139. end else
  140. VarLet (dec, None, value, ann)
  141. (* Variables that are in the constant table are replaced with their constant
  142. * value *)
  143. | Var (name, None, ann) when Hashtbl.mem consts name ->
  144. Hashtbl.find consts name
  145. | VarUse (dec, None, ann) when Hashtbl.mem consts (nameof dec) ->
  146. Hashtbl.find consts (nameof dec)
  147. | Dim (name, ann) when Hashtbl.mem consts name ->
  148. Hashtbl.find consts name
  149. (* Apply arithmetic simplification to constant operands *)
  150. | Monop (op, opnd, ann) ->
  151. eval (Monop (op, propagate opnd, ann))
  152. | Binop (op, left, right, ann) ->
  153. eval (Binop (op, propagate left, propagate right, ann))
  154. | Cond (cond, texp, fexp, ann) ->
  155. eval (Cond (propagate cond, propagate texp, propagate fexp, ann))
  156. | TypeCast (ctype, value, ann) ->
  157. let c v = Const (v, ann) in
  158. begin match ctype, propagate value with
  159. | Bool, (Const (BoolVal _, _) as v)
  160. | Int, (Const (IntVal _, _) as v)
  161. | Float, (Const (FloatVal _, _) as v) -> v
  162. | Bool, Const (IntVal v, _) -> c (BoolVal (v != 1l))
  163. | Bool, Const (FloatVal v, _) -> c (BoolVal (v != 1.0))
  164. | Int, Const (BoolVal v, _) -> c (IntVal (if v then 1l else 0l))
  165. | Int, Const (FloatVal v, _) -> c (IntVal (Int32.of_float v))
  166. | Float, Const (BoolVal v, _) -> c (FloatVal (if v then 1. else 0.))
  167. | Float, Const (IntVal v, _) -> c (FloatVal (Int32.to_float v))
  168. | _, v -> TypeCast (ctype, v, ann)
  169. end
  170. | _ -> traverse_unit propagate node
  171. let rec prune_vardecs consts = function
  172. | VarDec (_, name, _, _) when Hashtbl.mem consts name -> DummyNode
  173. | node -> traverse_unit (prune_vardecs consts) node
  174. let phase = function
  175. | Ast node ->
  176. let consts = Hashtbl.create 32 in
  177. let node = propagate consts node in
  178. Ast (prune_vardecs consts node)
  179. | _ -> raise InvalidInput