Skip to content

Commit

Permalink
[analyzer] semi propagate null values
Browse files Browse the repository at this point in the history
We don't want to actually move them within the AST as that would likely cause problems with native compilation in may cases. We can however keep track of them and use them for data flow analysis, so something like `var x = null; if ("foo" == x) { }` can get optimized.
  • Loading branch information
Simn committed Sep 25, 2017
1 parent f4d27f3 commit e41aa34
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/optimization/analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ module ConstPropagation = DataFlow(struct
type t =
| Top
| Bottom
| Null of Type.t
| Const of tconstant
| EnumValue of int * t list

Expand All @@ -365,22 +366,26 @@ module ConstPropagation = DataFlow(struct
let equals lat1 lat2 = match lat1,lat2 with
| Top,Top | Bottom,Bottom -> true
| Const ct1,Const ct2 -> ct1 = ct2
| Null t1,Null t2 -> t1 == t2
| EnumValue(i1,_),EnumValue(i2,_) -> i1 = i2
| _ -> false

let transfer ctx bb e =
let rec eval bb e =
let wrap = function
| Const ct -> mk (TConst ct) t_dynamic null_pos
| Null t -> mk (TConst TNull) t e.epos
| _ -> raise Exit
in
let unwrap e = match e.eexpr with
| TConst ct -> Const ct
| _ -> raise Exit
in
match e.eexpr with
| TConst (TSuper | TThis | TNull) ->
| TConst (TSuper | TThis) ->
Bottom
| TConst TNull ->
Null e.etype
| TConst ct ->
Const ct
| TLocal v ->
Expand Down Expand Up @@ -465,7 +470,7 @@ module ConstPropagation = DataFlow(struct

let commit ctx =
let inline e i = match get_cell i with
| Top | Bottom | EnumValue _ ->
| Top | Bottom | EnumValue _ | Null _ ->
raise Not_found
| Const ct ->
let e' = Codegen.type_constant ctx.com (tconst_to_const ct) e.epos in
Expand Down

0 comments on commit e41aa34

Please sign in to comment.