Browse Source

[analyzer] optimize constant switches during fusion

Simon Krajewski 5 years ago
parent
commit
2070c856a0
2 changed files with 26 additions and 17 deletions
  1. 5 0
      src/optimization/analyzerTexpr.ml
  2. 21 17
      src/optimization/optimizer.ml

+ 5 - 0
src/optimization/analyzerTexpr.ml

@@ -671,6 +671,11 @@ module Fusion = struct
 					| Some e ->
 						block_element acc (e :: el)
 				end
+			| ({eexpr = TSwitch(e1,cases,def)} as e) :: el ->
+				begin match Optimizer.check_constant_switch e1 cases def with
+				| Some e -> block_element acc (e :: el)
+				| None -> block_element (e :: acc) el
+				end
 			(* no-side-effect composites *)
 			| {eexpr = TParenthesis e1 | TMeta(_,e1) | TCast(e1,None) | TField(e1,_) | TUnop(_,_,e1)} :: el ->
 				block_element acc (e1 :: el)

+ 21 - 17
src/optimization/optimizer.ml

@@ -237,6 +237,23 @@ let check_enum_construction_args el i =
 	) (true,0) el in
 	b
 
+let check_constant_switch e1 cases def =
+	let rec loop e1 cases = match cases with
+		| (el,e) :: cases ->
+			if List.exists (Texpr.equal e1) el then Some e
+			else loop e1 cases
+		| [] ->
+			begin match def with
+			| None -> None
+			| Some e -> Some e
+			end
+	in
+	match Texpr.skip e1 with
+		| {eexpr = TConst ct} as e1 when (match ct with TSuper | TThis -> false | _ -> true) ->
+			loop e1 cases
+		| _ ->
+			None
+
 let reduce_control_flow ctx e = match e.eexpr with
 	| TIf ({ eexpr = TConst (TBool t) },e1,e2) ->
 		(if t then e1 else match e2 with None -> { e with eexpr = TBlock [] } | Some e -> e)
@@ -245,23 +262,10 @@ let reduce_control_flow ctx e = match e.eexpr with
 		| NormalWhile -> { e with eexpr = TBlock [] } (* erase sub *)
 		| DoWhile -> e) (* we cant remove while since sub can contain continue/break *)
 	| TSwitch (e1,cases,def) ->
-		let e = match Texpr.skip e1 with
-			| {eexpr = TConst ct} as e1 when (match ct with TSuper | TThis -> false | _ -> true) ->
-				let rec loop cases = match cases with
-					| (el,e) :: cases ->
-						if List.exists (Texpr.equal e1) el then e
-						else loop cases
-					| [] ->
-						begin match def with
-						| None -> e
-						| Some e -> e
-						end
-				in
-				loop cases
-			| _ ->
-				e
-		in
-		e
+		begin match check_constant_switch e1 cases def with
+		| Some e -> e
+		| None -> e
+		end
 	| TBinop (op,e1,e2) ->
 		optimize_binop e op e1 e2
 	| TUnop (op,flag,esub) ->