소스 검색

Eliminate unused vars on inlining (#8771)

* eliminate unised vars on inlining

* move vars elimination to the end of inlining process
Aleksandr Kuzmenko 6 년 전
부모
커밋
8ceb9f331d
4개의 변경된 파일44개의 추가작업 그리고 4개의 파일을 삭제
  1. 14 1
      src/optimization/inline.ml
  2. 1 1
      tests/misc/projects/Issue8654/Check.hx
  3. 9 2
      tests/optimization/src/issues/Issue6715.hx
  4. 20 0
      tests/optimization/src/issues/Issue8770.hx

+ 14 - 1
src/optimization/inline.ml

@@ -570,7 +570,20 @@ class inline_state ctx ethis params cf f p = object(self)
 			in
 			Type.map_expr_type (map_expr_type map_type) map_type (map_var map_type) e
 		in
-		map_expr_type map_type e
+		let e = map_expr_type map_type e in
+		let rec drop_unused_vars e =
+			match e.eexpr with
+			| TVar (v, Some { eexpr = TConst _ }) ->
+				(try
+					let data = Hashtbl.find locals v.v_id in
+					if data.i_read = 0 && not data.i_write then mk (TBlock []) e.etype e.epos
+					else Type.map_expr drop_unused_vars e
+				with Not_found ->
+					Type.map_expr drop_unused_vars e
+				)
+			| _ -> Type.map_expr drop_unused_vars e
+		in
+		drop_unused_vars e
 end
 
 let rec type_inline ctx cf f ethis params tret config p ?(self_calling_closure=false) force =

+ 1 - 1
tests/misc/projects/Issue8654/Check.hx

@@ -31,7 +31,7 @@ class Check {
 														//success;
 														return;
 													case _:
-														Context.error('Invalid invalid type of "f(get())" in Arr.map: $t', exprs[0].pos);
+														Context.error('Invalid type of "f(get())" in Arr.map: $t', exprs[0].pos);
 												}
 											case _:
 												Context.error('Invalid expression of Arr.map block', exprs[0].pos);

+ 9 - 2
tests/optimization/src/issues/Issue6715.hx

@@ -4,27 +4,34 @@ class Issue6715 {
 	@:js('
 		var f = function() {
 			var x = 1;
+			x = 2;
 		};
 		issues_Issue6715.x = f;
 		issues_Issue6715.x = f;
 		issues_Issue6715.x = f;
 		var x1 = 1;
+		x1 = 2;
 		var x2 = 1;
+		x2 = 2;
 		var x3 = 1;
+		x3 = 2;
 	')
 	@:analyzer(no_local_dce)
 	static public function test1() {
-		insanity(function() var x = 1);
+		insanity(function() { var x = 1; x = 2; });
 	}
 
 	@:js('
 		var x = 1;
+		x = 2;
 		var x1 = 1;
+		x1 = 2;
 		var x2 = 1;
+		x2 = 2;
 	')
 	@:analyzer(no_local_dce)
 	static public function test2() {
-		insanity2(function() var x = 1);
+		insanity2(function() { var x = 1; x = 2; });
 	}
 
 	@:js('

+ 20 - 0
tests/optimization/src/issues/Issue8770.hx

@@ -0,0 +1,20 @@
+package issues;
+
+class Issue8770 {
+	@:pure(false) static var testStatic:String;
+	static var pureStatic:String;
+
+	@:pure(false) var testInstance:String;
+	var pureInstance:String;
+
+	@:js('
+		var m_h;
+		m_h = { };
+	')
+	@:analyzer(ignore)
+	static function test() {
+		var m = new Map<Int,Bool>();
+	}
+
+	public function new() {}
+}