Selaa lähdekoodia

[analyzer] allow more local DCE and add some tests

Simon Krajewski 11 vuotta sitten
vanhempi
commit
6a5eb7bd30
3 muutettua tiedostoa jossa 128 lisäystä ja 4 poistoa
  1. 3 4
      analyzer.ml
  2. 1 0
      tests/optimization/run.hxml
  3. 124 0
      tests/optimization/src/TestLocalDce.hx

+ 3 - 4
analyzer.ml

@@ -1062,10 +1062,9 @@ module LocalDce = struct
 					e2
 				else
 					{e with eexpr = TBinop(OpAssign,{e1 with eexpr = TLocal v},e2)}
-			(* Cannot do that at the moment due to https://github.com/HaxeFoundation/haxe/issues/3440 *)
-(* 			| TVar(v,Some e1) when not (is_used v) ->
+			| TVar(v,Some e1) when not (is_used v) ->
 				let e1 = loop e1 in
-				e1 *)
+				e1
 			| TWhile(e1,e2,flag) ->
 				collect e2;
 				let e2 = loop e2 in
@@ -1144,7 +1143,7 @@ let run_ssa com config is_var_expression e =
 		else
 			e
 		in
-		let e = if config.local_dce then with_timer "analyzer-local-dce" (fun () -> LocalDce.apply e) else e in
+		let e = if config.local_dce && config.analyzer_use then with_timer "analyzer-local-dce" (fun () -> LocalDce.apply e) else e in
 		e
 	with Exit ->
 		e

+ 1 - 0
tests/optimization/run.hxml

@@ -13,4 +13,5 @@
 -js testopt.js
 --macro Macro.register('Test')
 --macro Macro.register('TestJs')
+--macro Macro.register('TestLocalDce')
 -dce std

+ 124 - 0
tests/optimization/src/TestLocalDce.hx

@@ -0,0 +1,124 @@
+private class InlineCtor {
+	public var x:Int;
+	public var y:String;
+
+	public inline function new(x, y) {
+		this.x = x;
+		this.y = y;
+	}
+}
+
+@:enum
+private abstract MyEnum(String) to String {
+	var A = "a";
+}
+
+@:analyzer(no_check_has_effect)
+class TestLocalDce {
+	@:js('3;')
+	static function testNoOpRemoval() {
+		1;
+		2;
+		{}
+		3;
+	}
+
+	@:js('
+		27;
+	')
+	static function testConstMath() {
+		var a = 1 + 2;
+		var b = 9 * 3;
+	}
+
+	@:js('
+		"foo";
+	')
+	static function testInlineCtor1() {
+		var c = new InlineCtor(12, "foo");
+		var x = c.x;
+		c.x = 13;
+		x = c.x;
+		var y = c.y;
+	}
+
+	@:js('
+		12;
+	')
+	static function testInlineCtor2() {
+		var a = 0;
+		var c = {
+			a = 1;
+			a = 2;
+			new InlineCtor(12, "foo");
+		}
+		a = c.x;
+	}
+
+	@:js('
+		1;
+	')
+	static function testInlineCtor3() {
+		var a = 0;
+		var b = {
+			var c = new InlineCtor(1, "c");
+			a = 1;
+			new InlineCtor(2, "b");
+		}
+		b.x = a;
+	}
+
+	@:js('
+		2;
+	')
+	static function testStructureInline1() {
+		var x = {
+			foo: 1,
+			bar: 2
+		}
+		var y = x.foo;
+		var z = x.bar;
+	}
+
+	@:js('
+		"god";
+	')
+	static function testStructureInlineInvalidField() {
+        var x = {
+            "oh-my": "god"
+        };
+	}
+
+	@:js('
+		2;
+	')
+	static function testArrayInline() {
+		var a = [1, 2];
+		var b = a.length;
+	}
+
+	@:js('
+		var a = [1,2];
+		a[-1];
+	')
+	static function testArrayInlineCancelNegative() {
+		var a = [1, 2];
+		a[-1];
+	}
+
+	@:js('
+		var a = [1,2];
+		a[2];
+	')
+	static function testArrayInlineCancelExceeds() {
+		var a = [1, 2];
+		a[2];
+	}
+
+	@:js('
+		"" + "a";
+	')
+	static function testAbstractOverStringBinop() {
+		var s = "" + A;
+	}
+}