constprop.ml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. (* Constand folding *)
  32. let eval = function
  33. (* Binop - arithmetic *)
  34. | Binop (Add, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  35. Const (IntVal (left + right), ann)
  36. | Binop (Add, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  37. Const (FloatVal (left +. right), ann)
  38. | Binop (Sub, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  39. Const (IntVal (left - right), ann)
  40. | Binop (Sub, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  41. Const (FloatVal (left -. right), ann)
  42. | Binop (Mul, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  43. Const (IntVal (left * right), ann)
  44. | Binop (Mul, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  45. Const (FloatVal (left *. right), ann)
  46. | Binop (Div, Const (IntVal left, _), Const (IntVal right, _), ann) when right != 0 ->
  47. Const (IntVal (left / right), ann)
  48. | Binop (Div, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  49. Const (FloatVal (left /. right), ann)
  50. | Binop (Mod, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  51. Const (IntVal (left mod right), ann)
  52. (* Binop - relational *)
  53. | Binop (Eq, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  54. Const (BoolVal (left = right), ann)
  55. | Binop (Eq, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  56. Const (BoolVal (left = right), ann)
  57. | Binop (Ne, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  58. Const (BoolVal (left <> right), ann)
  59. | Binop (Ne, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  60. Const (BoolVal (left <> right), ann)
  61. | Binop (Gt, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  62. Const (BoolVal (left > right), ann)
  63. | Binop (Gt, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  64. Const (BoolVal (left > right), ann)
  65. | Binop (Lt, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  66. Const (BoolVal (left < right), ann)
  67. | Binop (Lt, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  68. Const (BoolVal (left < right), ann)
  69. | Binop (Ge, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  70. Const (BoolVal (left >= right), ann)
  71. | Binop (Ge, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  72. Const (BoolVal (left >= right), ann)
  73. | Binop (Le, Const (IntVal left, _), Const (IntVal right, _), ann) ->
  74. Const (BoolVal (left <= right), ann)
  75. | Binop (Le, Const (FloatVal left, _), Const (FloatVal right, _), ann) ->
  76. Const (BoolVal (left <= right), ann)
  77. (* Binop - logical *)
  78. | Binop (And, Const (BoolVal left, _), Const (BoolVal right, _), ann) ->
  79. Const (BoolVal (left && right), ann)
  80. | Binop (Or, Const (BoolVal left, _), Const (BoolVal right, _), ann) ->
  81. Const (BoolVal (left || right), ann)
  82. (* Monary operations *)
  83. | Monop (Not, Const (BoolVal value, _), ann) -> Const (BoolVal (not value), ann)
  84. | Monop (Neg, Const (IntVal value, _), ann) -> Const (IntVal (-value), ann)
  85. | Monop (Neg, Const (FloatVal value, _), ann) -> Const (FloatVal (-.value), ann)
  86. (* 0 * a --> 0 *)
  87. | Binop (Mul, Const (IntVal 0, _), other, ann)
  88. | Binop (Mul, other, Const (IntVal 0, _), ann) when no_side_effect other ->
  89. Const (IntVal 0, ann)
  90. (* 0 + a --> a *)
  91. | Binop (Add, Const (IntVal 0, _), other, _)
  92. | Binop (Add, other, Const (IntVal 0, _), _) ->
  93. other
  94. (* 1 * a --> a *)
  95. | Binop (Mul, Const (IntVal 1, _), other, _)
  96. | Binop (Mul, other, Const (IntVal 1, _), _) ->
  97. other
  98. (* true|false ? texp : fexp --> texp|fexp*)
  99. | Cond (Const (BoolVal value, _), texp, fexp, _) ->
  100. if value then texp else fexp
  101. | node -> node
  102. let rec propagate consts node =
  103. let propagate = propagate consts in
  104. match node with
  105. (* Constant assignments are added to constants table *)
  106. | Assign (name, None, value, ann) when is_const_id name ->
  107. let value = propagate value in
  108. if is_const value then begin
  109. Hashtbl.add consts name value;
  110. DummyNode
  111. end else
  112. Assign (name, None, value, ann)
  113. | VarLet (dec, None, value, ann) when is_const_id (nameof dec) ->
  114. let value = propagate value in
  115. if is_const value then begin
  116. Hashtbl.add consts (nameof dec) value;
  117. DummyNode
  118. end else
  119. VarLet (dec, None, value, ann)
  120. (* Variables that are in the constant table are replaced with their constant
  121. * value *)
  122. | Var (name, None, ann) when Hashtbl.mem consts name ->
  123. Hashtbl.find consts name
  124. | VarUse (dec, None, ann) when Hashtbl.mem consts (nameof dec) ->
  125. Hashtbl.find consts (nameof dec)
  126. | Dim (name, ann) when Hashtbl.mem consts name ->
  127. Hashtbl.find consts name
  128. (* Apply arithmetic simplification to constant operands *)
  129. | Monop (op, opnd, ann) ->
  130. eval (Monop (op, propagate opnd, ann))
  131. | Binop (op, left, right, ann) ->
  132. eval (Binop (op, propagate left, propagate right, ann))
  133. | Cond (cond, texp, fexp, ann) ->
  134. eval (Cond (propagate cond, propagate texp, propagate fexp, ann))
  135. | TypeCast (ctype, value, ann) ->
  136. let value = propagate value in
  137. begin match (ctype, value) with
  138. | (Bool, Const (BoolVal value, _)) -> Const (BoolVal value, ann)
  139. | (Bool, Const (IntVal value, _)) -> Const (BoolVal (value != 1), ann)
  140. | (Bool, Const (FloatVal value, _)) -> Const (BoolVal (value != 1.0), ann)
  141. | (Int, Const (BoolVal value, _)) -> Const (IntVal (if value then 1 else 0), ann)
  142. | (Int, Const (IntVal value, _)) -> Const (IntVal value, ann)
  143. | (Int, Const (FloatVal value, _)) -> Const (IntVal (int_of_float value), ann)
  144. | (Float, Const (BoolVal value, _)) -> Const (FloatVal (if value then 1. else 0.), ann)
  145. | (Float, Const (IntVal value, _)) -> Const (FloatVal (float_of_int value), ann)
  146. | (Float, Const (FloatVal value, _)) -> Const (FloatVal value, ann)
  147. | _ -> TypeCast (ctype, value, ann)
  148. end
  149. | _ -> transform_children propagate node
  150. let rec prune_vardecs consts = function
  151. | VarDec (_, name, _, _) when Hashtbl.mem consts name -> DummyNode
  152. | node -> transform_children (prune_vardecs consts) node
  153. let propagate_consts node =
  154. let consts = Hashtbl.create 32 in
  155. let node = propagate consts node in
  156. prune_vardecs consts node
  157. let phase = function
  158. | Ast node -> Ast (propagate_consts node)
  159. | _ -> raise (InvalidInput "constant propagation")