[m-dev.] for review: MLDS switch optimization

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Nov 8 18:18:06 AEDT 2000


On 07-Nov-2000, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> This change breaks the IL back-end, but the work-around is easy
> (compile with `--no-smart-indexing').  I'm hoping Tyson will review
> fix the following XXX in ilasm.m, which is the cause of that problem,
> 
>         % XXX we really should implement this since we will use it
> 	% eventually.
> 	output_instr(switch(_)) --> { error("output not implemented") }.
> 
> since "eventually" has now arrived ;-)
> I'm also hoping that Tyson and/or Zoltan will review this change.
> 
> Note that this change still doesn't optimize tag switches, string
> switches, or lookup switches.
> 
> ----------
> 
> Estimated hours taken: 12
> 
> Get the MLDS back-end to generate better code for switches.
> It now compiles Mercury switches on int/char/enums into C switches.

There were a couple of places where I forgot to add code to handle
the new `switch' statement.  So I needed to make the following
additional changes.  I'll go ahead and commit this now (if there
are any changes required for review comments I'll commit them
separately).

--- CHANGES.switch.old	Wed Nov  8 18:11:43 2000
+++ CHANGES.switch	Wed Nov  8 18:11:50 2000
@@ -10,6 +10,7 @@
 compiler/ml_elim_nested.m:
 compiler/ml_optimize.m:
 compiler/ml_tailcall.m:
+compiler/ml_util.m:
 	Minor changes to handle the new `switch' statement.
 
 compiler/ml_code_gen.m:

--- ml_elim_nested.m	Tue Nov  7 13:17:58 2000
+++ ml_elim_nested.m	Tue Nov  7 16:25:00 2000
@@ -1190,6 +1190,11 @@
 		; maybe_statement_contains_defn(MaybeElse, Defn)
 		)
 	;
+		Stmt = switch(_Type, _Val, Cases, Default),
+		( cases_contains_defn(Cases, Defn)
+		; default_contains_defn(Default, Defn)
+		)
+	;
 		Stmt = label(_Label),
 		fail
 	;
@@ -1217,6 +1222,22 @@
 		fail
 	).
 
+:- pred cases_contains_defn(list(mlds__switch_case), mlds__defn).
+:- mode cases_contains_defn(in, out) is nondet.
+
+cases_contains_defn(Cases, Defn) :-
+	list__member(Case, Cases),
+	Case = _MatchConds - Statement,
+	statement_contains_defn(Statement, Defn).
+
+:- pred default_contains_defn(mlds__switch_default, mlds__defn).
+:- mode default_contains_defn(in, out) is nondet.
+
+default_contains_defn(default_do_nothing, _) :- fail.
+default_contains_defn(default_is_unreachable, _) :- fail.
+default_contains_defn(default_case(Statement), Defn) :-
+	statement_contains_defn(Statement, Defn).
+
 %-----------------------------------------------------------------------------%
 
 %
@@ -1318,6 +1339,12 @@
 		; maybe_statement_contains_var(MaybeElse, Name)
 		)
 	;
+		Stmt = switch(_Type, Val, Cases, Default),
+		( rval_contains_var(Val, Name)
+		; cases_contains_var(Cases, Name)
+		; default_contains_var(Default, Name)
+		)
+	;
 		Stmt = label(_Label),
 		fail
 	;
@@ -1349,6 +1376,22 @@
 		Stmt = atomic(AtomicStmt),
 		atomic_stmt_contains_var(AtomicStmt, Name)
 	).
+
+:- pred cases_contains_var(list(mlds__switch_case), mlds__var).
+:- mode cases_contains_var(in, in) is semidet.
+
+cases_contains_var(Cases, Name) :-
+	list__member(Case, Cases),
+	Case = _MatchConds - Statement,
+	statement_contains_var(Statement, Name).
+
+:- pred default_contains_var(mlds__switch_default, mlds__var).
+:- mode default_contains_var(in, in) is semidet.
+
+default_contains_var(default_do_nothing, _) :- fail.
+default_contains_var(default_is_unreachable, _) :- fail.
+default_contains_var(default_case(Statement), Name) :-
+	statement_contains_var(Statement, Name).
 
 :- pred atomic_stmt_contains_var(mlds__atomic_statement, mlds__var).
 :- mode atomic_stmt_contains_var(in, in) is semidet.
Index: ml_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_util.m,v
retrieving revision 1.1
diff -u -d -u -r1.1 ml_util.m
--- ml_util.m	2000/08/31 03:00:21	1.1
+++ ml_util.m	2000/11/07 00:24:08
@@ -111,6 +111,11 @@
 		; maybe_statement_contains_statement(MaybeElse, SubStatement)
 		)
 	;
+		Stmt = switch(_Type, _Val, Cases, Default),
+		( cases_contains_statement(Cases, SubStatement)
+		; default_contains_statement(Default, SubStatement)
+		)
+	;
 		Stmt = label(_Label),
 		fail
 	;
@@ -137,5 +142,21 @@
 		Stmt = atomic(_AtomicStmt),
 		fail
 	).
+
+:- pred cases_contains_statement(list(mlds__switch_case), mlds__statement).
+:- mode cases_contains_statement(in, out) is nondet.
+
+cases_contains_statement(Cases, SubStatement) :-
+	list__member(Case, Cases),
+	Case = _MatchCond - Statement,
+	statement_contains_statement(Statement, SubStatement).
+
+:- pred default_contains_statement(mlds__switch_default, mlds__statement).
+:- mode default_contains_statement(in, out) is nondet.
+
+default_contains_statement(default_do_nothing, _) :- fail.
+default_contains_statement(default_is_unreachable, _) :- fail.
+default_contains_statement(default_case(Statement), SubStatement) :-
+	statement_contains_statement(Statement, SubStatement).
 
 %-----------------------------------------------------------------------------%

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- 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