Browse Source

[analyzer] make local DCE perfect

It's not about local DCE and it's not perfect, but we have to think about the marketing value here.

closes #7874
Simon Krajewski 6 years ago
parent
commit
d10dea1c72
2 changed files with 11 additions and 2 deletions
  1. 3 1
      src/optimization/analyzerTexpr.ml
  2. 8 1
      tests/optimization/src/TestJs.hx

+ 3 - 1
src/optimization/analyzerTexpr.ml

@@ -671,7 +671,9 @@ module Fusion = struct
 				block_element acc (el1 @ el2)
 			| {eexpr = TObjectDecl fl} :: el ->
 				block_element acc ((List.map snd fl) @ el)
-			| {eexpr = TIf(e1,{eexpr = TBlock []},(Some {eexpr = TBlock []} | None))} :: el ->
+			| {eexpr = TIf(e1,e2,None)} :: el when not (has_side_effect e2) ->
+				block_element acc (e1 :: el)
+			| {eexpr = TIf(e1,e2,Some e3)} :: el when not (has_side_effect e2) && not (has_side_effect e3) ->
 				block_element acc (e1 :: el)
 			| {eexpr = TBlock [e1]} :: el ->
 				block_element acc (e1 :: el)

+ 8 - 1
tests/optimization/src/TestJs.hx

@@ -73,7 +73,7 @@ class TestJs {
 		return try a[i] catch (e:Dynamic) null;
 	}
 
-	@:js("var a_v_0_b = 1;a_v_0_b == 1;")
+	@:js("var a_v_0_b = 1;a_v_0_b;")
 	@:analyzer(no_const_propagation, no_local_dce)
 	static function testDeepMatchingWithoutClosures() {
 		var a = {v: [{b: 1}]};
@@ -540,6 +540,13 @@ class TestJs {
 		var closure = Extern.test;
 		closure("baz");
 	}
+
+	static var v = false;
+
+	@:js('')
+	static function testIssue7874() {
+		if (v && v) {}
+	}
 }
 
 extern class Extern {