Преглед изворни кода

Add small constprop optimization for ternary expressions

Taddeus Kroes пре 10 година
родитељ
комит
08873082bf
1 измењених фајлова са 9 додато и 1 уклоњено
  1. 9 1
      phases/constprop.ml

+ 9 - 1
phases/constprop.ml

@@ -134,10 +134,18 @@ let eval node =
   | Binop (Mul, other, Const (IntVal 1l, _), _) ->
   | Binop (Mul, other, Const (IntVal 1l, _), _) ->
     other
     other
 
 
-  (* true|false ? texp : fexp --> texp|fexp*)
+  (* true|false ? texp : fexp --> texp|fexp *)
   | Cond (Const (BoolVal value, _), texp, fexp, _) ->
   | Cond (Const (BoolVal value, _), texp, fexp, _) ->
     if value then texp else fexp
     if value then texp else fexp
 
 
+  (* cond ? true : false --> cond *)
+  | Cond (cond, Const (BoolVal true, _), Const (BoolVal false, _), _) ->
+    cond
+
+  (* cond ? false : true --> !cond *)
+  | Cond (cond, Const (BoolVal false, _), Const (BoolVal true, _), ann) ->
+    Monop (Not, cond, ann)
+
   | node -> node
   | node -> node
 
 
 let rec propagate consts node =
 let rec propagate consts node =