[m-rev.] diff: MR_succeed_discard

Zoltan Somogyi zs at cs.mu.OZ.AU
Wed Oct 5 09:33:18 AEST 2005


Fix an optimization that tries to turn MR_succeed() into MR_succeed_discard(),
which throws away nondet stack frames at the last success of the relevant
procedure.

compiler/opt_util.m:
	Fix the overly conservative approximation here that prevented
	peephole.m from applying that optimization. It was considering
	livevals() instructions as potentially affecting redoip slots,
	which cannot be farther from the truth.

	Change the relevant predicate from an if-then-else with a default
	to a switch, to prevent similar problems in future.

compiler/peephole.m:
	Don't apply the existing transformation pattern unless we find an
	assertion that no other stack frame exists on top of the current one,
	since it isn't valid then.

compiler/code_util.m:
	Include that assertion in the comment of the instruction that assigns
	to the redoip slot, which is where peephole.m looks for it.

	We could store the assertion in an extra slot to assignment statements
	in the LLDS, but that would require changes in lots of places and lead
	to significant space overhead. This way is simpler, and has no overhead
	to speak of. Any LLDS transformation that modifies the comment may
	destroy the assertion, but the chance that it will incorrectly add or
	even keep the assertion is about zero.

Zoltan.

cvs diff: Diffing .
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/code_info.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/code_info.m,v
retrieving revision 1.306
diff -u -r1.306 code_info.m
--- compiler/code_info.m	30 Sep 2005 08:08:16 -0000	1.306
+++ compiler/code_info.m	4 Oct 2005 04:52:19 -0000
@@ -1527,9 +1527,10 @@
         stack__top_det(ResumePoints, ResumePoint),
         code_info__pick_stack_resume_point(ResumePoint, _, StackLabel),
         LabelConst = const(code_addr_const(StackLabel)),
+        % peephole.m looks for the "curfr==maxfr" pattern in the comment.
         Code = node([
             assign(redoip(lval(curfr)), LabelConst)
-                - "restore redoip for quarter disj hijack"
+                - "restore redoip for quarter disj hijack (curfr==maxfr)"
         ])
     ;
         HijackInfo = disj_half_hijack(RedoipSlot),
@@ -1537,9 +1538,10 @@
             "resume point known in disj_half_hijack"),
         require(unify(CurfrMaxfr, must_be_equal),
             "maxfr may differ from curfr in disj_half_hijack"),
+        % peephole.m looks for the "curfr==maxfr" pattern in the comment.
         Code = node([
             assign(redoip(lval(curfr)), lval(RedoipSlot))
-                - "restore redoip for half disj hijack"
+                - "restore redoip for half disj hijack (curfr==maxfr)"
         ])
     ;
         HijackInfo = disj_full_hijack(RedoipSlot, RedofrSlot),
Index: compiler/opt_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/opt_util.m,v
retrieving revision 1.135
diff -u -r1.135 opt_util.m
--- compiler/opt_util.m	14 Sep 2005 01:29:09 -0000	1.135
+++ compiler/opt_util.m	4 Oct 2005 01:15:45 -0000
@@ -567,9 +567,17 @@
 :- pred straight_alternative_2(list(instruction)::in, list(instruction)::in,
     list(instruction)::out, list(instruction)::out) is semidet.
 
-straight_alternative_2([Instr0 | Instrs0], Between0, Between, After) :-
+straight_alternative_2([Instr0 | Instrs0], !Between, After) :-
     Instr0 = Uinstr0 - _,
     (
+        Uinstr0 = label(_)
+    ->
+        fail
+    ;
+        Uinstr0 = goto(do_succeed(no))
+    ->
+        After = Instrs0
+    ;
         (
             can_instr_branch_away(Uinstr0, no),
             touches_nondet_ctrl_instr(Uinstr0, no)
@@ -578,13 +586,8 @@
             ( CodeAddr = do_fail ; CodeAddr = do_redo )
         )
     ->
-        straight_alternative_2(Instrs0, [Instr0 | Between0],
-            Between, After)
-    ;
-        Uinstr0 = goto(do_succeed(no))
-    ->
-        Between = Between0,
-        After = Instrs0
+        !:Between = [Instr0 | !.Between],
+        straight_alternative_2(Instrs0, !Between, After)
     ;
         fail
     ).
@@ -1485,35 +1488,72 @@
     has_both_incr_decr_sp_2(Instrs, !HasIncr, !HasDecr).
 
 touches_nondet_ctrl([], no).
-touches_nondet_ctrl([Uinstr - _ | Instrs], Touch) :-
-    touches_nondet_ctrl_instr(Uinstr, Touch0),
+touches_nondet_ctrl([Uinstr - _ | Instrs], !:Touch) :-
+    touches_nondet_ctrl_instr(Uinstr, !:Touch),
     (
-        Touch0 = yes,
-        Touch = yes
+        !.Touch = yes
     ;
-        Touch0 = no,
-        touches_nondet_ctrl(Instrs, Touch)
+        !.Touch = no,
+        touches_nondet_ctrl(Instrs, !:Touch)
     ).
 
 :- pred touches_nondet_ctrl_instr(instr::in, bool::out) is det.
 
 touches_nondet_ctrl_instr(Uinstr, Touch) :-
-    ( Uinstr = assign(Lval, Rval) ->
+    (
+        ( Uinstr = comment(_)
+        ; Uinstr = livevals(_)
+        ; Uinstr = label(_)
+        ; Uinstr = free_heap(_)
+        ; Uinstr = store_ticket(_)
+        ; Uinstr = reset_ticket(_, _)
+        ; Uinstr = prune_ticket
+        ; Uinstr = discard_ticket
+        ; Uinstr = prune_tickets_to(_)
+        ; Uinstr = mark_ticket_stack(_)
+        ; Uinstr = incr_sp(_, _)
+        ; Uinstr = decr_sp(_)
+        ; Uinstr = decr_sp_and_return(_)
+        ),
+        Touch = no
+    ;
+        ( Uinstr = mkframe(_, _)
+        ; Uinstr = goto(_)
+        ; Uinstr = computed_goto(_, _)
+        ; Uinstr = call(_, _, _, _, _, _)   % This is a safe approximation.
+        ; Uinstr = if_val(_, _)
+        ; Uinstr = c_code(_, _)
+        ; Uinstr = save_maxfr(_)
+        ; Uinstr = restore_maxfr(_)
+        ; Uinstr = init_sync_term(_, _)     % This is a safe approximation.
+        ; Uinstr = fork(_, _, _)            % This is a safe approximation.
+        ; Uinstr = join_and_terminate(_)    % This is a safe approximation.
+        ; Uinstr = join_and_continue(_, _)  % This is a safe approximation.
+        ),
+        Touch = yes
+    ;
+        Uinstr = block(_, _, _),
+        % Blocks aren't introduced until after the last user of this predicate.
+        unexpected(this_file, "touches_nondet_ctrl_instr: block")
+    ;
+        Uinstr = assign(Lval, Rval),
         touches_nondet_ctrl_lval(Lval, TouchLval),
         touches_nondet_ctrl_rval(Rval, TouchRval),
         bool__or(TouchLval, TouchRval, Touch)
-    ; Uinstr = incr_hp(Lval, _, _, Rval, _) ->
+    ;
+        Uinstr = incr_hp(Lval, _, _, Rval, _),
         touches_nondet_ctrl_lval(Lval, TouchLval),
         touches_nondet_ctrl_rval(Rval, TouchRval),
         bool__or(TouchLval, TouchRval, Touch)
-    ; Uinstr = mark_hp(Lval) ->
+    ;
+        Uinstr = mark_hp(Lval),
         touches_nondet_ctrl_lval(Lval, Touch)
-    ; Uinstr = restore_hp(Rval) ->
+    ;
+        Uinstr = restore_hp(Rval),
         touches_nondet_ctrl_rval(Rval, Touch)
-    ; Uinstr = pragma_c(_, Components, _, _, _, _, _, _, _) ->
-        touches_nondet_ctrl_components(Components, Touch)
     ;
-        Touch = yes
+        Uinstr = pragma_c(_, Components, _, _, _, _, _, _, _),
+        touches_nondet_ctrl_components(Components, Touch)
     ).
 
 :- pred touches_nondet_ctrl_lval(lval::in, bool::out) is det.
Index: compiler/peephole.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/peephole.m,v
retrieving revision 1.85
diff -u -r1.85 peephole.m
--- compiler/peephole.m	14 Sep 2005 01:29:10 -0000	1.85
+++ compiler/peephole.m	4 Oct 2005 04:55:00 -0000
@@ -344,7 +344,8 @@
         Base = curfr,
         Redoip = const(code_addr_const(do_fail)),
         opt_util__straight_alternative(Instrs0, Between, After),
-        opt_util__touches_nondet_ctrl(Between, no)
+        opt_util__touches_nondet_ctrl(Between, no),
+        string__sub_string_search(Comment, "curfr==maxfr", _)
     ->
         list__condense([Between,
             [goto(do_succeed(yes)) - "early discard"], After], Instrs)
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing debian/patches
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
cvs diff: Diffing extras
cvs diff: Diffing extras/aditi
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/concurrency
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/easyx
cvs diff: Diffing extras/graphics/easyx/samples
cvs diff: Diffing extras/graphics/mercury_glut
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/gears
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/moose/tests
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/solver_types
cvs diff: Diffing extras/solver_types/library
cvs diff: Diffing extras/stream
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing extras/xml_stylesheets
cvs diff: Diffing java
cvs diff: Diffing java/runtime
cvs diff: Diffing library
cvs diff: Diffing mdbcomp
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing slice
cvs diff: Diffing tests
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list