[m-dev.] diff: simplify.m: fix a bug for MLDS back-end

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Sep 4 23:25:27 AEDT 2000


Estimated hours taken: 2

Fix a bug that broke tests/general/frameopt_mkframe_bug
with `--grade hlc.gc' and `-O4' or higher.

compiler/simplify.m:
	Rerun some simplifications, to ensure that the conditions of
	if-then-elses never have `cannot_fail' determinisms.
	The MLDS back-end assumes that invariant.

Workspace: /home/pgrad/fjh/ws/hg
Index: compiler/simplify.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/simplify.m,v
retrieving revision 1.80
diff -u -d -r1.80 simplify.m
--- compiler/simplify.m	2000/08/09 07:47:48	1.80
+++ compiler/simplify.m	2000/09/04 12:23:59
@@ -905,6 +905,8 @@
 	% cannot succeed, then we replace the if-then-else with the
 	% other disjunct. (We could also eliminate A, but we leave
 	% that to the recursive invocations.)
+	% Note that this simplification is required for the MLDS back-end,
+	% which assumes that conditions of if-then-elses are not model_det.
 	%
 	% The conjunction operator in the remaining disjunct ought to be
 	% a sequential conjunction, because Mercury's if-then-else always
@@ -919,7 +921,6 @@
 simplify__goal_2(if_then_else(Vars, Cond0, Then0, Else0, SM),
 		GoalInfo0, Goal, GoalInfo, Info0, Info) :-
 	Cond0 = _ - CondInfo0,
-
 	goal_info_get_determinism(CondInfo0, CondDetism0),
 	determinism_components(CondDetism0, CondCanFail0, CondSolns0),
 	( CondCanFail0 = cannot_fail ->
@@ -983,6 +984,10 @@
 		simplify__goal(conj(List) - GoalInfo0, Goal - GoalInfo,
 			Info0, Info)
 	;
+		%
+		% recursively simplify the sub-goals,
+		% and rebuild the resulting if-then-else
+		%
 		simplify_info_get_instmap(Info0, InstMap0),
 		simplify__goal(Cond0, Cond, Info0, Info1),
 		simplify_info_update_instmap(Info1, Cond, Info2),
@@ -1003,33 +1008,57 @@
 		merge_instmap_deltas(InstMap0, NonLocals,
 			[CondThenDelta, ElseDelta], NewDelta,
 			ModuleInfo0, ModuleInfo1),
-		simplify_info_set_module_info(Info6, ModuleInfo1, Info),
+		simplify_info_set_module_info(Info6, ModuleInfo1, Info7),
 		goal_info_set_instmap_delta(GoalInfo0, NewDelta, GoalInfo1),
 		IfThenElse = if_then_else(Vars, Cond, Then, Else, SM),
-		%
-		% If-then-elses that are det or semidet may nevertheless
-		% contain nondet or multidet conditions. If this happens, the
-		% if-then-else must be put inside a `some' to appease the code
-		% generator.
-		%
+
 		goal_info_get_determinism(GoalInfo0, IfThenElseDetism0),
 		determinism_components(IfThenElseDetism0, IfThenElseCanFail,
 			IfThenElseNumSolns),
+
+		goal_info_get_determinism(CondInfo, CondDetism),
+		determinism_components(CondDetism, CondCanFail, CondSolns),
 		(
-			simplify_do_once(Info),
-			goal_info_get_determinism(CondInfo, CondDetism),
-			determinism_components(CondDetism, _, at_most_many),
-			IfThenElseNumSolns \= at_most_many
+			%
+			% check again if we can apply one of the above
+			% simplifications after having simplified the 
+			% sub-goals (we need to do this to ensure that
+			% the goal is fully simplified, to maintain the
+			% invariants that the MLDS back-end depends on)
+			%
+			( CondCanFail = cannot_fail
+			; CondSolns = at_most_zero
+			; Else = disj([], _) - _ 
+			)
 		->
-			determinism_components(InnerDetism, IfThenElseCanFail,
-				at_most_many),
-			goal_info_set_determinism(GoalInfo1, InnerDetism,
-				InnerInfo),
-			Goal = some([], can_remove, IfThenElse - InnerInfo)
+			simplify_info_undo_goal_updates(Info0, Info7, Info8),
+			simplify__goal_2(IfThenElse, GoalInfo1,
+				Goal, GoalInfo, Info8, Info)
 		;
-			Goal = IfThenElse
-		),
-		GoalInfo = GoalInfo1
+			(
+				%
+				% If-then-elses that are det or semidet may
+				% nevertheless contain nondet or multidet
+				% conditions. If this happens, the if-then-else
+				% must be put inside a `some' to appease the
+				% code generator.
+				%
+				simplify_do_once(Info),
+				CondSolns = at_most_many,
+				IfThenElseNumSolns \= at_most_many
+			->
+				determinism_components(InnerDetism,
+					IfThenElseCanFail, at_most_many),
+				goal_info_set_determinism(GoalInfo1,
+					InnerDetism, InnerInfo),
+				Goal = some([], can_remove,
+					IfThenElse - InnerInfo)
+			;
+				Goal = IfThenElse
+			),
+			GoalInfo = GoalInfo1,
+			Info = Info7
+		)
 	).
 
 simplify__goal_2(not(Goal0), GoalInfo0, Goal, GoalInfo, Info0, Info) :-

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list