constant_propagation.ml 8.0 KB

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