constprop.ml 7.8 KB

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