[m-rev.] for review: lco and constant constructions

Peter Wang novalazy at gmail.com
Fri Jul 20 14:24:54 AEST 2012


For review by Zoltan.

Branches: main

Allow the last-call-modulo-cons optimisation to move goals constructing
constants before a recursive call, thus allowing the call to be in the
tail position.

compiler/lco.m:
        As above.

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/lco_literal.exp:
tests/hard_coded/lco_literal.m:
        Add test case.

diff --git a/compiler/lco.m b/compiler/lco.m
index 0ba7142..1752673 100644
--- a/compiler/lco.m
+++ b/compiler/lco.m
@@ -620,6 +620,9 @@ lco_in_conj([RevGoal | RevGoals], !.Unifies, !.UnifyInputVars, MaybeGoals,
             ConsTag = unshared_tag(_)
         ;
             ConsTag = shared_remote_tag(_, _)
+        ;
+            % Constants can be moved.
+            ConstructArgs = []
         )
     ->
         trace [compiletime(flag("lco")), io(!IO)] (
diff --git a/tests/hard_coded/Mercury.options b/tests/hard_coded/Mercury.options
index 99726e0..b8acb47 100644
--- a/tests/hard_coded/Mercury.options
+++ b/tests/hard_coded/Mercury.options
@@ -59,6 +59,7 @@ MCFLAGS-intermod_type_qual2 =	--intermodule-optimization
 MCFLAGS-intermod_multimode =	--intermodule-optimization
 MCFLAGS-intermod_multimode_main = --intermodule-optimization
 MCFLAGS-lco_double	    =	--optimize-constructor-last-call
+MCFLAGS-lco_literal	    =	--optimize-constructor-last-call
 MCFLAGS-lco_mday_bug_1	    =	--optimize-constructor-last-call
 MCFLAGS-lco_mday_bug_2	    =	--optimize-constructor-last-call
 MCFLAGS-lco_no_inline	    =	--optimize-constructor-last-call --no-inline-builtins
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index a78eeac..6c376ea 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -166,6 +166,7 @@ ORDINARY_PROGS=	\
 	java_rtti_bug \
 	join_list \
 	lco_double \
+	lco_literal \
 	lco_mday_bug_1 \
 	lco_mday_bug_2 \
 	lco_no_inline \
diff --git a/tests/hard_coded/lco_literal.exp b/tests/hard_coded/lco_literal.exp
new file mode 100644
index 0000000..825319e
--- /dev/null
+++ b/tests/hard_coded/lco_literal.exp
@@ -0,0 +1 @@
+10000000
diff --git a/tests/hard_coded/lco_literal.m b/tests/hard_coded/lco_literal.m
new file mode 100644
index 0000000..aecc1ee
--- /dev/null
+++ b/tests/hard_coded/lco_literal.m
@@ -0,0 +1,45 @@
+%-----------------------------------------------------------------------------%
+
+:- module lco_literal.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module char.
+:- import_module list.
+:- import_module int.
+
+%-----------------------------------------------------------------------------%
+
+main(!IO) :-
+    Xs = dup(10000000),
+    Length = len(Xs, 0),
+    io.write_int(Length, !IO),
+    io.nl(!IO).
+
+:- func dup(int) = list(char).
+
+dup(N) = Xs :-
+    ( N > 0 ->
+        Xs0 = dup(N - 1),
+        % Previously the goal which constructs the literal would not be moved
+        % before the recursive goal.
+        Xs = ['A' | Xs0]
+    ;
+        Xs = []
+    ).
+
+:- func len(list(T), int) = int.
+
+len([], N) = N.
+len([_ | Xs], N) = len(Xs, N + 1).
+
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sts=4 sw=4 et

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list