浏览代码

DCE now eliminate inline members that are not marked as @:keep

Franco Ponticelli 14 年之前
父节点
当前提交
74ab4a7e71
共有 3 个文件被更改,包括 35 次插入13 次删除
  1. 1 1
      main.ml
  2. 31 9
      optimizer.ml
  3. 3 3
      typeload.ml

+ 1 - 1
main.ml

@@ -585,7 +585,7 @@ try
 		let filters = (if not com.foptimize then filters else Optimizer.reduce_expression ctx :: filters) in
 		Codegen.post_process com filters;
 		if com.dead_code_elimination then
-			Common.add_filter com (fun() -> Optimizer.filter_dead_classes com);
+			Common.add_filter com (fun() -> Optimizer.filter_dead_code com);
 		Common.add_filter com (fun() -> List.iter (Codegen.on_generate ctx) com.types);
 		List.iter (fun f -> f()) (List.rev com.filters);
 		if Common.defined com "dump" then Codegen.dump_types com;

+ 31 - 9
optimizer.ml

@@ -497,24 +497,46 @@ let reduce_expression ctx e =
 	if ctx.com.foptimize then reduce_loop ctx false e else e
 	
 (* ---------------------------------------------------------------------- *)
-(* ELIMINATE DEAD CLASSES *)
+(* ELIMINATE DEAD CODE *)
 
 (*
-	if dead code elimination is on, any class without fields is eliminated from the output.
+	if dead code elimination is on, any class without fields is eliminated from the output. Also inline members 
+	are eliminated unless marked as @:keep
 *)
 	
-let filter_dead_classes com =
+let filter_dead_code com =
+	let s_class c = s_type_path c.cl_path in
+	let s_field c cf = (s_class c) ^ "." ^ cf.cf_name in
+	let remove_inlines c =
+		let remove_inline_fields lst = 
+			List.filter(fun cf ->
+				match cf.cf_kind with
+				| Var k when ((k.v_read = AccInline) && (not (has_meta ":keep" cf.cf_meta))) ->
+					if com.verbose then print_endline ("Remove inline var " ^ s_field c cf);
+					false;
+				| Method k when ((k = MethInline) && (not (has_meta ":keep" cf.cf_meta))) ->
+					if com.verbose then print_endline ("Remove inline method " ^ s_field c cf);
+					false;
+				| _ ->
+					true;
+			) lst
+		in
+		c.cl_ordered_statics <- remove_inline_fields c.cl_ordered_statics;
+		c.cl_ordered_fields <- remove_inline_fields c.cl_ordered_fields
+	in
 	com.types <- List.filter (fun t ->
 		match t with
 		| TClassDecl c ->
 			if (c.cl_extern or has_meta ":keep" c.cl_meta) then 
 				true 
-			else (match (c.cl_ordered_statics, c.cl_ordered_fields, c.cl_constructor) with
-			| ([], [], None) ->
-				if com.verbose then print_endline ("Remove class " ^ s_type_path c.cl_path);
-				false
-			| _ ->
-				true)
+			else (
+				remove_inlines c;
+				match (c.cl_ordered_statics, c.cl_ordered_fields, c.cl_constructor) with
+				| ([], [], None) ->
+					if com.verbose then print_endline ("Remove class " ^ s_class c);
+					false
+				| _ ->
+					true)
 		| _ ->
 			true
 	) com.types

+ 3 - 3
typeload.ml

@@ -713,7 +713,7 @@ let init_class ctx c p herits fields =
 		| _ -> ()) 
 	in
 	let remove_var_if_unreferenced cf stat = (fun () ->	
-		if not (has_meta ":keep" cf.cf_meta) then begin
+		if not (has_meta "?keep" cf.cf_meta) then begin
 			if ctx.com.verbose then print_endline ("Remove var " ^ (s_type_path c.cl_path) ^ "." ^ cf.cf_name);
 			remove_field cf stat
 		end)
@@ -782,7 +782,7 @@ let init_class ctx c p herits fields =
 				| None ->
 					let r = exc_protect (fun r ->
 						r := (fun() -> t);
-						cf.cf_meta <- if has_meta ":keep" cf.cf_meta then f.cff_meta else (":keep", [], p) :: f.cff_meta;
+						cf.cf_meta <- if has_meta "?keep" cf.cf_meta then f.cff_meta else ("?keep", [], p) :: f.cff_meta;
 						t
 					) in
 					cf.cf_type <- TLazy r;
@@ -797,7 +797,7 @@ let init_class ctx c p herits fields =
 					let r = exc_protect (fun r ->
 						r := (fun() -> t);
 						if ctx.com.verbose then print_endline ("Typing " ^ s_type_path c.cl_path ^ "." ^ name);
-						cf.cf_meta <- if has_meta ":keep" cf.cf_meta then f.cff_meta else (":keep", [], p) :: f.cff_meta;
+						cf.cf_meta <- if has_meta "?keep" cf.cf_meta then f.cff_meta else ("?keep", [], p) :: f.cff_meta;
 						cf.cf_expr <- Some (type_static_var ctx t e p);
 						t
 					) in