[m-rev.] diff: stdlabel.m

Zoltan Somogyi zs at cs.mu.OZ.AU
Mon Apr 10 14:21:05 AEST 2006


compiler/stdlabel.m:
	A new module that contains code to standardize labels in the LLDS.

compiler/ll_backend.m:
	Include the new module in this package.

compiler/options.m:
	Add an option that governs whether stdlabel.m is invoked or not.

compiler/optimize.m:
	If the option is set, invoke stdlabel.m.

compiler/opt_util.m:
	Add an option to opt_util.replace_labels_instruction_list to allow
	it to replace labels in label instructions themselves.

compiler/dupelim.m:
	Conform to the changes in opt_util.m

compiler/notes/compiler_design.html:
	Document the new module.

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/dupelim.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/dupelim.m,v
retrieving revision 1.76
diff -u -b -r1.76 dupelim.m
--- compiler/dupelim.m	29 Mar 2006 08:06:43 -0000	1.76
+++ compiler/dupelim.m	8 Apr 2006 15:27:12 -0000
@@ -110,7 +110,7 @@
             ReplMap0, ReplMap),
         flatten_basic_blocks(LabelSeq, BlockMap, Instrs1),
         opt_util.replace_labels_instruction_list(Instrs1,
-            ReplMap, yes, Instrs2),
+            ReplMap, yes, no, Instrs2),
         list.append(Comments, Instrs2, Instrs)
     ).
 
Index: compiler/ll_backend.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ll_backend.m,v
retrieving revision 1.13
diff -u -b -r1.13 ll_backend.m
--- compiler/ll_backend.m	23 Feb 2006 09:36:54 -0000	1.13
+++ compiler/ll_backend.m	8 Apr 2006 14:20:03 -0000
@@ -74,6 +74,7 @@
    :- include_module frameopt.
    :- include_module delay_slot.
    :- include_module labelopt.
+   :- include_module stdlabel.
    :- include_module peephole.
    :- include_module use_local_vars.
    :- include_module wrap_blocks.
Index: compiler/opt_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/opt_util.m,v
retrieving revision 1.144
diff -u -b -r1.144 opt_util.m
--- compiler/opt_util.m	29 Mar 2006 08:07:11 -0000	1.144
+++ compiler/opt_util.m	8 Apr 2006 15:27:46 -0000
@@ -285,6 +285,9 @@
     % called procedure) are always replaced; references that treat the label
     % as data are replaced iff the third argument is set to "yes".
     %
+    % With replace_labels_instruction_list, the fourth arg says whether
+    % it is OK to replace a label in a label instruction itself.
+    %
 :- pred replace_labels_instr(instr::in, map(label, label)::in,
     bool::in, instr::out) is det.
 
@@ -292,7 +295,7 @@
     map(label, label)::in, bool::in, instruction::out) is det.
 
 :- pred replace_labels_instruction_list(list(instruction)::in,
-    map(label, label)::in, bool::in, list(instruction)::out) is det.
+    map(label, label)::in, bool::in, bool::in, list(instruction)::out) is det.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -1730,11 +1733,20 @@
 % The code in this section is concerned with replacing all references
 % to one given label with a reference to another given label.
 
-replace_labels_instruction_list([], _, _, []).
+replace_labels_instruction_list([], _, _, _, []).
 replace_labels_instruction_list([Instr0 | Instrs0], ReplMap, ReplData,
-        [Instr | Instrs]) :-
-    replace_labels_instruction(Instr0, ReplMap, ReplData, Instr),
-    replace_labels_instruction_list(Instrs0, ReplMap, ReplData, Instrs).
+        ReplLabel, [Instr | Instrs]) :-
+    (
+        Instr0 = label(InstrLabel) - Comment,
+        ReplLabel = yes
+    ->
+        replace_labels_label(InstrLabel, ReplMap, ReplInstrLabel),
+        Instr = label(ReplInstrLabel) - Comment
+    ;
+        replace_labels_instruction(Instr0, ReplMap, ReplData, Instr)
+    ),
+    replace_labels_instruction_list(Instrs0, ReplMap, ReplData, ReplLabel,
+        Instrs).
 
 replace_labels_instruction(Instr0 - Comment, ReplMap, ReplData,
         Instr - Comment) :-
@@ -1744,7 +1756,8 @@
 replace_labels_instr(livevals(Livevals), _, _, livevals(Livevals)).
 replace_labels_instr(block(R, F, Instrs0), ReplMap, ReplData,
         block(R, F, Instrs)) :-
-    replace_labels_instruction_list(Instrs0, ReplMap, ReplData, Instrs).
+    % There should be no labels in Instrs0.
+    replace_labels_instruction_list(Instrs0, ReplMap, ReplData, no, Instrs).
 replace_labels_instr(assign(Lval0, Rval0), ReplMap, ReplData,
         assign(Lval, Rval)) :-
     (
Index: compiler/optimize.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/optimize.m,v
retrieving revision 1.54
diff -u -b -r1.54 optimize.m
--- compiler/optimize.m	29 Mar 2006 08:07:11 -0000	1.54
+++ compiler/optimize.m	8 Apr 2006 15:17:18 -0000
@@ -48,6 +48,7 @@
 :- import_module ll_backend.frameopt.
 :- import_module ll_backend.jumpopt.
 :- import_module ll_backend.labelopt.
+:- import_module ll_backend.stdlabel.
 :- import_module ll_backend.llds_out.
 :- import_module ll_backend.opt_debug.
 :- import_module ll_backend.opt_util.
@@ -98,7 +99,7 @@
             !OptDebugInfo, !Instrs, !IO),
         optimize_middle(yes, LayoutLabelSet, ProcLabel, MayAlterRtti, !C,
             !OptDebugInfo, !Instrs, !IO),
-        optimize_last(LayoutLabelSet, ProcLabel, !.C, !.OptDebugInfo, !Instrs,
+        optimize_last(LayoutLabelSet, ProcLabel, !C, !.OptDebugInfo, !Instrs,
             !IO),
         CProc = c_procedure(Name, Arity, PredProcId, !.Instrs, ProcLabel,
             !.C, MayAlterRtti)
@@ -476,21 +477,23 @@
         UseLocalVars = no
     ).
 
-:- pred optimize_last(set(label)::in, proc_label::in, counter::in,
-    opt_debug_info::in, list(instruction)::in, list(instruction)::out,
-    io::di, io::uo) is det.
+:- pred optimize_last(set(label)::in, proc_label::in,
+    counter::in, counter::out, opt_debug_info::in,
+    list(instruction)::in, list(instruction)::out, io::di, io::uo) is det.
 
-optimize_last(LayoutLabelSet, ProcLabel, C, !.OptDebugInfo, !Instrs, !IO) :-
+optimize_last(LayoutLabelSet, ProcLabel, !C, !.OptDebugInfo, !Instrs, !IO) :-
     globals.io_lookup_bool_option(very_verbose, VeryVerbose, !IO),
     LabelStr = opt_util.format_proc_label(ProcLabel),
 
     globals.io_lookup_bool_option(optimize_reassign, Reassign, !IO),
     globals.io_lookup_bool_option(optimize_delay_slot, DelaySlot, !IO),
     globals.io_lookup_bool_option(use_local_vars, UseLocalVars, !IO),
+    globals.io_lookup_bool_option(standardize_labels, StdLabels, !IO),
     (
         ( Reassign = yes
         ; DelaySlot = yes
         ; UseLocalVars = yes
+        ; StdLabels = yes
         )
     ->
         % We must get rid of any extra labels added by other passes,
@@ -504,7 +507,7 @@
             VeryVerbose = no
         ),
         labelopt_main(no, LayoutLabelSet, !Instrs, _Mod1),
-        maybe_opt_debug(!.Instrs, C, "after label opt",
+        maybe_opt_debug(!.Instrs, !.C, "after label opt",
             ProcLabel, !OptDebugInfo, !IO)
     ;
         true
@@ -520,7 +523,7 @@
             VeryVerbose = no
         ),
         remove_reassign(!Instrs),
-        maybe_opt_debug(!.Instrs, C, "after reassign",
+        maybe_opt_debug(!.Instrs, !.C, "after reassign",
             ProcLabel, !OptDebugInfo, !IO)
     ;
         Reassign = no
@@ -536,7 +539,7 @@
             VeryVerbose = no
         ),
         fill_branch_delay_slot(!Instrs),
-        maybe_opt_debug(!.Instrs, C, "after delay slots",
+        maybe_opt_debug(!.Instrs, !.C, "after delay slots",
             ProcLabel, !OptDebugInfo, !IO)
     ;
         DelaySlot = no
@@ -550,9 +553,25 @@
         VeryVerbose = no
     ),
     combine_decr_sp(!Instrs),
-    maybe_opt_debug(!.Instrs, C, "after combine decr_sp",
+    maybe_opt_debug(!.Instrs, !.C, "after combine decr_sp",
         ProcLabel, !OptDebugInfo, !IO),
     (
+        StdLabels = yes,
+        (
+            VeryVerbose = yes,
+            io.write_string("% Standardizing labels for ", !IO),
+            io.write_string(LabelStr, !IO),
+            io.write_string("\n", !IO)
+        ;
+            VeryVerbose = no
+        ),
+        standardize_labels(!Instrs, !C),
+        maybe_opt_debug(!.Instrs, !.C, "after standard labels",
+            ProcLabel, !OptDebugInfo, !IO)
+    ;
+        StdLabels = no
+    ),
+    (
         UseLocalVars = yes,
         (
             VeryVerbose = yes,
@@ -563,7 +582,7 @@
             VeryVerbose = no
         ),
         wrap_blocks(!Instrs),
-        maybe_opt_debug(!.Instrs, C, "after wrap blocks",
+        maybe_opt_debug(!.Instrs, !.C, "after wrap blocks",
             ProcLabel, !.OptDebugInfo, _OptDebugInfo, !IO)
     ;
         UseLocalVars = no
Index: compiler/options.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/options.m,v
retrieving revision 1.509
diff -u -b -r1.509 options.m
--- compiler/options.m	29 Mar 2006 08:07:12 -0000	1.509
+++ compiler/options.m	8 Apr 2006 14:22:28 -0000
@@ -580,6 +580,7 @@
     ;       checked_nondet_tailcalls
     ;       use_local_vars
     ;       local_var_access_threshold
+    ;       standardize_labels
     ;       optimize_labels
     ;       optimize_dups
     ;       optimize_proc_dups
@@ -1279,6 +1280,7 @@
     checked_nondet_tailcalls            -  bool(no),
     use_local_vars                      -   bool(no),
     local_var_access_threshold          -    int(2),
+    standardize_labels                  -   bool(no),
     optimize_labels                     -   bool(no),
     optimize_dups                       -   bool(no),
     optimize_proc_dups                  -   bool(no),
@@ -2018,6 +2020,8 @@
 long_option("checked-nondet-tailcalls", checked_nondet_tailcalls).
 long_option("use-local-vars",       use_local_vars).
 long_option("local-var-access-threshold", local_var_access_threshold).
+long_option("standardise-labels",   standardize_labels).
+long_option("standardize-labels",   standardize_labels).
 long_option("optimize-labels",      optimize_labels).
 long_option("optimise-labels",      optimize_labels).
 long_option("optimize-dups",        optimize_dups).
@@ -4114,6 +4118,9 @@
         "--no-use-local-vars",
         "\tDisable the transformation to use local variables in C code",
         "\tblocks wherever possible.",
+% This is useful for developers only.
+%       "--standardize-labels",
+%       "\tStandardize internal labels in the generated code.",
         "--no-optimize-labels",
         "\tDisable elimination of dead labels and code.",
         "--optimize-dups",
Index: compiler/stdlabel.m
===================================================================
RCS file: compiler/stdlabel.m
diff -N compiler/stdlabel.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ compiler/stdlabel.m	9 Apr 2006 01:10:05 -0000
@@ -0,0 +1,94 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
+% Copyright (C) 2006 The University of Melbourne.
+% This file may only be copied under the terms of the GNU General
+% Public License - see the file COPYING in the Mercury distribution.
+%-----------------------------------------------------------------------------%
+%
+% File: stdlabel.m.
+% Author: zs.
+%
+% Module to rename (actually renumber) all the labels in a procedure so that
+% the label numbers are in a dense ascending sequence.
+%
+% This transformation leaves the performance of the program unaffected.
+% However, one can use it make the effect of an optimization much more readily
+% apparent. The idea is that enabling --standardize-labels when compiling
+% a program both with and without a new or modified optimization cleans up
+% the output of the diff between the two sets of resulting .c files, by
+% eliminating incidental differences in labels that could otherwise overwhelm
+% the real differences made by the optimization.
+%
+%-----------------------------------------------------------------------------%
+
+:- module ll_backend.stdlabel.
+:- interface.
+
+:- import_module ll_backend.llds.
+
+:- import_module counter.
+:- import_module list.
+
+%-----------------------------------------------------------------------------%
+
+:- pred standardize_labels(list(instruction)::in, list(instruction)::out,
+    counter::in, counter::out) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module libs.compiler_util.
+:- import_module ll_backend.opt_util.
+:- import_module mdbcomp.prim_data.
+
+:- import_module bool.
+:- import_module int.
+:- import_module map.
+:- import_module pair.
+:- import_module svmap.
+
+%-----------------------------------------------------------------------------%
+
+standardize_labels(Instrs0, Instrs, _, !:ProcCounter) :-
+    opt_util.get_prologue(Instrs0, LabelInstr, Comments, Instrs1),
+    (
+        LabelInstr = label(FirstLabel) - _,
+        FirstLabel = entry(_, ProcLabel)
+    ->
+        build_std_map(Instrs1, ProcLabel, counter.init(1), !:ProcCounter,
+            map.init, Map),
+        replace_labels_instruction_list(Instrs1, Map, yes, yes, Instrs2),
+        Instrs = [LabelInstr | Comments] ++ Instrs2
+    ;
+        unexpected(this_file, "standardize_labels: no proc_label")
+    ).
+
+%-----------------------------------------------------------------------------%
+
+:- pred build_std_map(list(instruction)::in, proc_label::in,
+    counter::in, counter::out,
+    map(label, label)::in, map(label, label)::out) is det.
+
+build_std_map([], _, !Counter, !Map).
+build_std_map([Instr | Instrs], ProcLabel, !Counter, !Map) :-
+    ( Instr = label(Label) - _ ->
+        counter.allocate(LabelNum, !Counter),
+        StdLabel = internal(LabelNum, ProcLabel),
+        svmap.det_insert(Label, StdLabel, !Map)
+    ;
+        true
+    ),
+    build_std_map(Instrs, ProcLabel, !Counter, !Map).
+
+%-----------------------------------------------------------------------------%
+
+:- func this_file = string.
+
+this_file = "stdlabel.m".
+
+%-----------------------------------------------------------------------------%
+:- end_module stdlabel.
+%-----------------------------------------------------------------------------%
cvs diff: Diffing compiler/notes
Index: compiler/notes/compiler_design.html
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/notes/compiler_design.html,v
retrieving revision 1.115
diff -u -b -r1.115 compiler_design.html
--- compiler/notes/compiler_design.html	24 Mar 2006 03:04:20 -0000	1.115
+++ compiler/notes/compiler_design.html	9 Apr 2006 00:56:11 -0000
@@ -1248,6 +1248,10 @@
 
 </ul>
 
+In addition, stdlabel.m performs standardization of labels.
+This is not an optimization itself,
+but it allows other optimizations to be evaluated more easily.
+
 <p>
 
 The module opt_debug.m contains utility routines used for debugging
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/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/gator
cvs diff: Diffing extras/gator/generations
cvs diff: Diffing extras/gator/generations/1
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/windows_installer_generator
cvs diff: Diffing extras/windows_installer_generator/sample
cvs diff: Diffing extras/windows_installer_generator/sample/images
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/trailing
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