Files
mercury/compiler/optimize.m
Zoltan Somogyi 5f50259d16 Write to explicitly named streams in many modules.
Right now, most parts of the compiler write to the "current output stream".
This was a pragmatic choice at the time, but has not aged well. The problem
is that the answer to the question "where is the current output stream going?"
is not obvious in *all* places in the compiler (although it is obvious in
most). When using such implicit streams, finding where the output is going
to in a given predicate requires inspecting not just the ancestors of that
predicate, but also all their older siblings (since any of them could have
changed the current stream), *including* their entire call trees. This is
usually an infeasible task. By constrast, if we explicitly pass streams
to all output operations, we need only follow the places where the variable
representing that stream is bound, which the mode system makes easy.

This diff switches large parts of the compiler over to doing output only
to explicitly passed streams, never to the implicit "current output stream".
The parts it switches over are the parts that rely to a significant degree
on the innermost change, which is to the "output" typeclass in
parse_tree_out_info.m. This is the part that has to be switched over to
explicit streams first, because (a) many modules such as mercury_to_mercury.m
rely on the output typeclass, and (b) most other modules that do output
call predicates in these modules. Starting anywhere else would be like
building a skyscraper starting at the top.

This typeclass, output(U), has two instances: output(io), and output(string),
so you could output either to the current output stream, or to a string.
To allow the specification of the destination stream in the first case,
this diff changes the typeclass to output(S, U) with a functional dependency
from U to S, with the two instances being output(io.text_output_stream, io)
and output(unit, string). (The unit arg is ignored in the second case.)

There is a complication with the output typeclass method, add_list, that
outputs a list of items. The complication is that each item is output
by a predicate supplied by the caller, but the separator between the items
(usually a comma) is output by add_list itself. We don't want to give
callers of this method the opportunity to screw up by specifying (possibly
implicitly) two different output streams for these two purposes, so we want
(a) the caller to tell add_list where to put the separators, and then
(b) for add_list, not its caller, tell the user-supplied predicate what
stream to write to. This works only if the stream argument is just before
the di,uo pair of I/O state arguments, which differs from our usual practice
of passing the stream at or near the left edge of the argument list,
not near the right. The result of this complication is that two categories
of predicates that are and are not used to print items in a list differ
in where they put the stream in their argument lists. This makes it easy
to pass the stream in the wrong argument position if you call a predicate
without looking up its signature, and may require *changing* the argument
order when a predicate is used to print an item in a list for the first time.
A complete switch over to always passing the stream just before !IO
would fix this inconsistency, but is far to big a change to make all at once.

compiler/parse_tree_out_info.m:
    Make the changes described above.

    Add write_out_list, which is a variant of io.write_list specifically
    designed to address the "complication" described above. It also has
    the arguments in an order that is better suited for higher-order use.

    Make the same change to argument order in the class method add_list
    as well.

Almost all of the following changes consist of passing an extra stream
argument to output predicates. In some places, where I thought this would
aid readability, I replaced sequences of calls to output predicates
with a single io.format.

compiler/prog_out.m:
    This module had many predicates that wrote things to the current output
    stream. This diff adds versions of these predicates that take an
    explicit stream argument.

    If the originals are still needed after the changes to the other modules,
    keep them, but add "_to_cur_stream" to the end of their names.
    Otherwise, delete them. (Many of the changes below replace
    write_xyz(..., !IO) with io.write_string(Stream, xyz_to_string(...), !IO),
    especially when write_xyz did nothing except call xyz_to_string
    and wrote out the result.)

compiler/c_util.m:
    Add either an explicit stream argument to the argument list, or a
    "_current_stream" suffix to the name, of every predicate defined
    in this module that does output.

    Add a new predicate to print out the block comment containing
    input for mkinit. This factors out common code in the LLDS and MLDS
    backends.

compiler/name_mangle.m:
    Delete all predicates that used to write to the current output stream,
    after replacing them if necessary with functions that return a string,
    which the caller can print to wherever it wants. (The "if necessary"
    part is there because some of the "replacement" functions already
    existed.)

    When converting a proc_label to a string, *always* require the caller
    to say whether the label prefix should be added to the string,
    instead of silently assuming "yes, add it", as calls to one of the old,
    now deleted predicates had it.

compiler/file_util.m:
    Add output_to_file_stream, a version of output_to_file which
    simply passes the output file stream it opens to the predicate
    that is intended to define the contents of the newly created or
    updated file. The existing output_to_file, which instead sets
    and resets the current output stream around the equivalent
    predicate call, is still needed e.g. by the MLDS backend,
    but hopefully for not too long.

compiler/mercury_to_mercury.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_clause.m:
compiler/parse_tree_out_inst.m:
compiler/parse_tree_out_pragma.m:
compiler/parse_tree_out_pred_decl.m:
compiler/parse_tree_out_term.m:
compiler/parse_tree_out_type_repn.m:
    Change the code writing out parse trees to explicitly pass a stream
    to every predicate that does output.

    In some places, this allows us to avoid changing the identity
    of the current output stream.

compiler/hlds_out.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
    Change the code writing out HLDS code to explicitly pass a stream
    to every predicate that does output. (The changes to these modules
    belong in this diff because these modules call many of the output
    predicates in the parse tree package.)

    In hlds_out_util.m, delete some write_to_xyz(...) predicates that wrote
    the result of xyz_to_string(...) to the current output stream.
    Replace calls to the deleted predicates with calls to io.write_string
    with the string being written being computed by xyz_to_string.

    Add a predicate to hlds_out_util.m that outputs a comment containing
    the current context, if it is valid. This factors out code that used
    to be common to several of the other modules.

    In a few places in hlds_out_module.m, the new code generates a
    slighly different set of blank lines, but this should not be a problem.

compiler/layout_out.m:
compiler/llds_out_code_addr.m:
compiler/llds_out_data.m:
compiler/llds_out_file.m:
compiler/llds_out_global.m:
compiler/llds_out_instr.m:
compiler/llds_out_util.m:
compiler/opt_debug.m:
compiler/rtti_out.m:
    Change the code writing out the LLDS to explicitly pass a stream
    to every predicate that does output. (The changes to these modules
    belong in this diff because layout_out.m and rtti_out.m call
    many of the output predicates in the parse tree package,
    and through them, the rest of the LLDS backend is affected as well.)

compiler/make.module_dep_file.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_middle_passes.m:
    Replace code that sets and resets the current output stream
    with code that simply passes an explicit output stream to a
    predicate that now *takes* an explicit stream as an argument.

compiler/accumulator.m:
compiler/add_clause.m:
compiler/code_gen.m:
compiler/code_loc_dep.m:
compiler/cse_detection.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/error_msg_inst.m:
compiler/export.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/ite_gen.m:
compiler/lco.m:
compiler/liveness.m:
compiler/lp_rational.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_c_global.m:
compiler/mode_debug.m:
compiler/mode_errors.m:
compiler/modes.m:
compiler/optimize.m:
compiler/passes_aux.m:
compiler/pd_debug.m:
compiler/pragma_c_gen.m:
compiler/proc_gen.m:
compiler/prog_ctgc.m:
compiler/push_goals_together.m:
compiler/rat.m:
compiler/recompilation.m:
compiler/recompilation.usage.m:
compiler/recompilation.version.m:
compiler/rtti.m:
compiler/saved_vars.m:
compiler/simplify_goal_conj.m:
compiler/stack_opt.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.domain.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/superhomogeneous.m:
compiler/term_constr_build.m:
compiler/term_constr_data.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_pass2.m:
compiler/term_constr_util.m:
compiler/tupling.m:
compiler/type_assign.m:
compiler/unneeded_code.m:
compiler/write_deps_file.m:
    Conform to the changes above, mostly by passing streams explicitly.

compiler/hlds_dependency_graph.m:
    Conform to the changes above, mostly by passing streams explicitly.
    Move a predicate's definition next it only use.

compiler/Mercury.options:
    Specify --warn-implicit-stream-calls for all the modules in which
    this diff has replaced all implicit streams with explicit streams.
    (Unfortunately, debugging this diff has shown that --warn-implicit-
    stream-calls detects only *some*, and not *all*, uses of implicit
    streams.)

library/term_io.m:
    Fix documentation.
2020-11-14 15:07:55 +11:00

847 lines
30 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1996-2012 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: optimize.m.
% Main author: zs.
%
% This module contains LLDS to LLDS optimizations.
%
%-----------------------------------------------------------------------------%
:- module ll_backend.optimize.
:- interface.
:- import_module libs.
:- import_module libs.globals.
:- import_module ll_backend.global_data.
:- import_module ll_backend.llds.
:- import_module list.
%-----------------------------------------------------------------------------%
:- pred optimize_procs(globals::in, global_data::in,
list(c_procedure)::in, list(c_procedure)::out) is det.
:- pred optimize_proc(globals::in, global_data::in,
c_procedure::in, c_procedure::out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module hlds.
:- import_module hlds.code_model.
:- import_module hlds.hlds_pred.
:- import_module libs.file_util.
:- import_module libs.optimization_options.
:- import_module libs.options.
:- import_module ll_backend.continuation_info.
:- import_module ll_backend.delay_slot.
:- import_module ll_backend.dupelim.
:- import_module ll_backend.frameopt.
:- import_module ll_backend.jumpopt.
:- import_module ll_backend.labelopt.
:- import_module ll_backend.stdlabel.
:- import_module ll_backend.opt_debug.
:- import_module ll_backend.opt_util.
:- import_module ll_backend.peephole.
:- import_module ll_backend.reassign.
:- import_module ll_backend.use_local_vars.
:- import_module ll_backend.wrap_blocks.
:- import_module mdbcomp.
:- import_module mdbcomp.prim_data.
:- import_module bool.
:- import_module char.
:- import_module counter.
:- import_module dir.
:- import_module int.
:- import_module io.
:- import_module map.
:- import_module maybe.
:- import_module require.
:- import_module set_tree234.
:- import_module string.
%-----------------------------------------------------------------------------%
optimize_procs(_, _, [], []).
optimize_procs(Globals, GlobalData, [Proc0 | Procs0], [Proc | Procs]) :-
optimize_proc(Globals, GlobalData, Proc0, Proc),
optimize_procs(Globals, GlobalData, Procs0, Procs).
optimize_proc(Globals, GlobalData, CProc0, CProc) :-
Info = init_llds_opt_info(Globals),
some [!OptDebugInfo, !LabelNumCounter, !Instrs] (
CProc0 = c_procedure(Name, Arity, PredProcId, ProcLabel, CodeModel,
!:Instrs, !:LabelNumCounter, MayAlterRtti, CGlobalVars),
need_opt_debug_info(Info, Name, Arity, PredProcId, MaybeBaseName),
(
MaybeBaseName = no,
!:OptDebugInfo = no_opt_debug_info
;
MaybeBaseName = yes(BaseName),
FirstFileName = BaseName ++ ".opt" ++ num_to_str(0),
trace [io(!IO)] (
output_first_opt_debug(Info, FirstFileName, ProcLabel,
!.Instrs, !.LabelNumCounter, !IO)
),
!:OptDebugInfo = opt_debug_info(BaseName, 0, FirstFileName, 0,
FirstFileName, !.Instrs)
),
Repeat = Info ^ lopt_opt_repeat,
( if
global_data_maybe_get_proc_layout(GlobalData,
PredProcId, ProcLayout)
then
LabelMap = ProcLayout ^ pli_internal_map,
map.sorted_keys(LabelMap, LayoutLabelNums),
LayoutLabels = list.map(
make_internal_label_for_proc_label(ProcLabel),
LayoutLabelNums),
set_tree234.sorted_list_to_set(LayoutLabels, LayoutLabelSet)
else
LayoutLabelSet = set_tree234.init
),
Statistics = Info ^ lopt_detailed_statistics,
optimize_initial(Info, LayoutLabelSet, ProcLabel, CodeModel,
MayAlterRtti, !LabelNumCounter, !OptDebugInfo, !Instrs),
optimize_repeat(Info, Repeat, LayoutLabelSet, ProcLabel,
MayAlterRtti, !LabelNumCounter, !OptDebugInfo, !Instrs),
trace [io(!IO)] (
maybe_report_stats(Statistics, !IO)
),
optimize_middle(Info, yes, LayoutLabelSet, ProcLabel, CodeModel,
MayAlterRtti, !LabelNumCounter, !OptDebugInfo, !Instrs),
trace [io(!IO)] (
maybe_report_stats(Statistics, !IO)
),
optimize_last(Info, LayoutLabelSet, ProcLabel,
!LabelNumCounter, !.OptDebugInfo, !Instrs),
trace [io(!IO)] (
maybe_report_stats(Statistics, !IO)
),
CProc = c_procedure(Name, Arity, PredProcId, ProcLabel, CodeModel,
!.Instrs, !.LabelNumCounter, MayAlterRtti, CGlobalVars)
).
:- func make_internal_label_for_proc_label(proc_label, int) = label.
make_internal_label_for_proc_label(ProcLabel, LabelNum)
= internal_label(LabelNum, ProcLabel).
%-----------------------------------------------------------------------------%
:- type opt_debug_info
---> opt_debug_info(
% Base file name for the dump files.
odi_dump_file_base_name :: string,
% The number of the last dump file written.
odi_last_dump_num :: int,
% The name of the last dump file written.
odi_last_dump_file_name :: string,
% The number of the last dump file written that has
% the instruction sequence in it.
odi_last_instrs_dump_num :: int,
% The name of the last dump file written that has
% the instruction sequence in it.
odi_last_instrs_dump_file_name :: string,
% The instruction sequence at the time the last dump file
% was written.
odi_last_dump_instr_seq :: list(instruction)
)
; no_opt_debug_info.
:- pred need_opt_debug_info(llds_opt_info::in, string::in, int::in,
pred_proc_id::in, maybe(string)::out) is det.
need_opt_debug_info(Info, Name, Arity, PredProcId, MaybeBaseName) :-
DebugOpt = Info ^ lopt_debug_opt,
DebugOptPredIdStrs = Info ^ lopt_debug_opt_pred_ids,
DebugOptPredNames = Info ^ lopt_debug_opt_pred_names,
PredProcId = proc(PredId, ProcId),
pred_id_to_int(PredId, PredIdInt),
proc_id_to_int(ProcId, ProcIdInt),
( if
DebugOpt = yes,
(
DebugOptPredIdStrs = [_ | _],
DebugOptPredNames = [_ | _],
(
some [DebugOptPredIdStr, DebugOptPredId] (
list.member(DebugOptPredIdStr, DebugOptPredIdStrs),
string.to_int(DebugOptPredIdStr, DebugOptPredId),
DebugOptPredId = PredIdInt
)
;
list.member(Name, DebugOptPredNames)
)
;
DebugOptPredIdStrs = [_ | _],
DebugOptPredNames = [],
some [DebugOptPredIdStr, DebugOptPredId] (
list.member(DebugOptPredIdStr, DebugOptPredIdStrs),
string.to_int(DebugOptPredIdStr, DebugOptPredId),
DebugOptPredId = PredIdInt
)
;
DebugOptPredIdStrs = [],
DebugOptPredNames = [_ | _],
list.member(Name, DebugOptPredNames)
;
DebugOptPredIdStrs = [],
DebugOptPredNames = []
)
then
BaseName = opt_subdir_name ++ "/"
++ mangle_name_as_filename(Name) ++ "_" ++ int_to_string(Arity)
++ ".pred" ++ int_to_string(PredIdInt)
++ ".proc" ++ int_to_string(ProcIdInt),
MaybeBaseName = yes(BaseName)
else
MaybeBaseName = no
).
:- pred output_first_opt_debug(llds_opt_info::in, string::in, proc_label::in,
list(instruction)::in, counter::in, io::di, io::uo) is det.
output_first_opt_debug(Info, FileName, ProcLabel, Instrs0, Counter, !IO) :-
io.call_system("mkdir -p " ++ opt_subdir_name, MkdirRes, !IO),
( if MkdirRes = ok(0) then
io.open_output(FileName, Res, !IO),
(
Res = ok(FileStream),
counter.allocate(NextLabel, Counter, _),
opt_debug.msg(FileStream, yes, NextLabel,
"before optimization", !IO),
AutoComments = Info ^ lopt_auto_comments,
opt_debug.maybe_write_instrs(FileStream, yes, AutoComments,
yes(ProcLabel), Instrs0, !IO),
io.close_output(FileStream, !IO)
;
Res = error(_),
unexpected($pred, "cannot open " ++ FileName)
)
else
unexpected($pred, "cannot make " ++ opt_subdir_name)
).
:- func opt_subdir_name = string.
opt_subdir_name = "OptSubdir".
:- func num_to_str(int) = string.
num_to_str(N) =
( if N < 10 then
"0" ++ string.int_to_string(N)
else
string.int_to_string(N)
).
:- pred maybe_opt_debug(llds_opt_info::in, list(instruction)::in, counter::in,
string::in, string::in, proc_label::in,
opt_debug_info::in, opt_debug_info::out) is det.
maybe_opt_debug(Info, Instrs, Counter, Suffix, Msg, ProcLabel,
!OptDebugInfo) :-
(
!.OptDebugInfo = opt_debug_info(BaseName, OptNum0, _OptFileName0,
PrevNum, PrevFileName, PrevInstrs),
OptNum = OptNum0 + 1,
OptFileName = BaseName ++ ".opt" ++ num_to_str(OptNum)
++ "." ++ Suffix,
DiffFileName = BaseName ++ ".diff" ++ num_to_str(OptNum)
++ "." ++ Suffix,
( if Instrs = PrevInstrs then
Same = yes,
!:OptDebugInfo = opt_debug_info(BaseName, OptNum, OptFileName,
PrevNum, PrevFileName, Instrs)
else
Same = no,
!:OptDebugInfo = opt_debug_info(BaseName, OptNum, OptFileName,
OptNum, OptFileName, Instrs)
),
trace [io(!IO)] (
io.open_output(OptFileName, Res, !IO),
(
Res = ok(FileStream),
counter.allocate(NextLabel, Counter, _),
opt_debug.msg(FileStream, yes, NextLabel, Msg, !IO),
(
Same = yes,
io.write_string("same as previous version\n", !IO)
;
Same = no,
AutoComments = Info ^ lopt_auto_comments,
opt_debug.maybe_write_instrs(FileStream, yes, AutoComments,
yes(ProcLabel), Instrs, !IO)
),
io.close_output(FileStream, !IO)
;
Res = error(_),
unexpected($pred, "cannot open " ++ OptFileName)
),
(
Same = yes
;
Same = no,
% Although the -u is not fully portable, it is available on
% all the systems we intend to use it on, and the main user
% of --debug-opt (zs) strongly prefers -u to -c.
DiffCommand = "diff -u '" ++ PrevFileName ++ "' '" ++
OptFileName ++ "' > '" ++ DiffFileName ++ "'",
io.call_system(DiffCommand, _, !IO)
)
)
;
!.OptDebugInfo = no_opt_debug_info
).
%-----------------------------------------------------------------------------%
:- pred optimize_initial(llds_opt_info::in, set_tree234(label)::in,
proc_label::in, code_model::in, may_alter_rtti::in,
counter::in, counter::out, opt_debug_info::in, opt_debug_info::out,
list(instruction)::in, list(instruction)::out) is det.
optimize_initial(Info, LayoutLabelSet, ProcLabel, CodeModel, MayAlterRtti,
!LabelNumCounter, !OptDebugInfo, !Instrs) :-
LabelStr = opt_util.format_proc_label(ProcLabel),
OptFrames = Info ^ lopt_opt_frames,
( if
OptFrames = opt_frames,
MayAlterRtti = may_alter_rtti,
CodeModel = model_non
then
VeryVerbose = Info ^ lopt_very_verbose,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing nondet frames for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
frameopt_keep_nondet_frame(ProcLabel, LayoutLabelSet,
!LabelNumCounter, !Instrs, _Mod),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter, "ndframeopt",
"after nondet frame opt", ProcLabel, !OptDebugInfo)
else
true
).
%-----------------------------------------------------------------------------%
:- pred optimize_repeat(llds_opt_info::in, int::in, set_tree234(label)::in,
proc_label::in, may_alter_rtti::in, counter::in, counter::out,
opt_debug_info::in, opt_debug_info::out,
list(instruction)::in, list(instruction)::out) is det.
optimize_repeat(Info, CurIter, LayoutLabelSet, ProcLabel,
MayAlterRtti, !LabelNumCounter, !OptDebugInfo, !Instrs) :-
( if CurIter > 0 then
NextIter = CurIter - 1,
( if NextIter = 0 then
Final = yes
else
Final = no
),
optimize_repeated(Info, Final, LayoutLabelSet, ProcLabel, MayAlterRtti,
!LabelNumCounter, !OptDebugInfo, !Instrs, Mod),
(
Mod = yes,
optimize_repeat(Info, NextIter, LayoutLabelSet, ProcLabel,
MayAlterRtti, !LabelNumCounter, !OptDebugInfo, !Instrs)
;
Mod = no
)
else
true
).
% We short-circuit jump sequences before normal peepholing
% to create more opportunities for use of the tailcall macro.
%
:- pred optimize_repeated(llds_opt_info::in, bool::in, set_tree234(label)::in,
proc_label::in, may_alter_rtti::in, counter::in, counter::out,
opt_debug_info::in, opt_debug_info::out,
list(instruction)::in, list(instruction)::out, bool::out) is det.
optimize_repeated(Info, Final, LayoutLabelSet, ProcLabel, MayAlterRtti,
!LabelNumCounter, !OptDebugInfo, !Instrs, Mod) :-
InstrsAtStart = !.Instrs,
LabelStr = opt_util.format_proc_label(ProcLabel),
VeryVerbose = Info ^ lopt_very_verbose,
OptJump = Info ^ lopt_opt_jumps,
OptFullJump = Info ^ lopt_opt_fulljumps,
PessimizeTailCalls = Info ^ lopt_pes_tailcalls,
CheckedNondetTailCalls = Info ^ lopt_checked_nondet_tailcalls,
(
OptJump = opt_jumps,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing jumps for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
optimize_jumps_in_proc(LayoutLabelSet, MayAlterRtti, ProcLabel,
OptFullJump, Final, PessimizeTailCalls, CheckedNondetTailCalls,
!LabelNumCounter, !Instrs, Mod1),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"jump", "after jump opt", ProcLabel, !OptDebugInfo)
;
OptJump = do_not_opt_jumps,
Mod1 = no
),
Peephole = Info ^ lopt_opt_peep,
(
Peephole = opt_peep,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing locally for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
GC_Method = Info ^ lopt_gc_method,
OptPeepMkword = Info ^ lopt_opt_peep_mkword,
peephole_optimize(GC_Method, OptPeepMkword, !Instrs, Mod2),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"peep", "after peephole", ProcLabel, !OptDebugInfo)
;
Peephole = do_not_opt_peep,
Mod2 = no
),
OptLabels = Info ^ lopt_opt_labels,
(
OptLabels = opt_labels,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing labels for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
labelopt_main(Final, LayoutLabelSet, !Instrs, Mod3),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"label", "after label opt", ProcLabel, !OptDebugInfo)
;
OptLabels = do_not_opt_labels,
Mod3 = no
),
DupElim = Info ^ lopt_opt_dups,
(
DupElim = opt_dups,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing duplicates for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
dupelim_main(ProcLabel, !LabelNumCounter, !Instrs),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"dup", "after duplicates", ProcLabel, !OptDebugInfo)
;
DupElim = do_not_opt_dups
),
( if Mod1 = no, Mod2 = no, Mod3 = no, !.Instrs = InstrsAtStart then
Mod = no
else
Mod = yes
),
trace [io(!IO)] (
Statistics = Info ^ lopt_detailed_statistics,
maybe_report_stats(Statistics, !IO)
).
:- pred optimize_middle(llds_opt_info::in, bool::in, set_tree234(label)::in,
proc_label::in, code_model::in, may_alter_rtti::in,
counter::in, counter::out, opt_debug_info::in, opt_debug_info::out,
list(instruction)::in, list(instruction)::out) is det.
optimize_middle(Info, Final, LayoutLabelSet, ProcLabel, CodeModel,
MayAlterRtti, !LabelNumCounter, !OptDebugInfo, !Instrs) :-
VeryVerbose = Info ^ lopt_very_verbose,
LabelStr = opt_util.format_proc_label(ProcLabel),
OptFrames = Info ^ lopt_opt_frames,
(
OptFrames = opt_frames,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing frames for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
FrameOptComments = Info ^ lopt_frameopt_comments,
(
( CodeModel = model_det
; CodeModel = model_semi
),
frameopt_main_det_stack(ProcLabel, !LabelNumCounter, !Instrs,
FrameOptComments, Mod1)
;
CodeModel = model_non,
frameopt_main_nondet_stack(ProcLabel, !LabelNumCounter, !Instrs,
FrameOptComments, Mod1)
),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"frame", "after frame opt", ProcLabel, !OptDebugInfo),
Statistics = Info ^ lopt_detailed_statistics,
trace [io(!IO)] (
maybe_report_stats(Statistics, !IO)
),
OptFullJump = Info ^ lopt_opt_fulljumps,
PessimizeTailCalls = Info ^ lopt_pes_tailcalls,
CheckedNondetTailCalls = Info ^ lopt_checked_nondet_tailcalls,
( if
( OptFullJump = opt_fulljumps
; Mod1 = yes
)
then
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing jumps for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
optimize_jumps_in_proc(LayoutLabelSet, MayAlterRtti, ProcLabel,
OptFullJump, Final, PessimizeTailCalls, CheckedNondetTailCalls,
!LabelNumCounter, !Instrs, _Mod2),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"jump", "after jumps", ProcLabel, !OptDebugInfo)
else
true
),
(
Mod1 = yes,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing labels for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
labelopt_main(Final, LayoutLabelSet, !Instrs, _Mod3),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"label", "after labels", ProcLabel, !OptDebugInfo)
;
Mod1 = no
),
(
Mod1 = yes,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing locally for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
GC_Method = Info ^ lopt_gc_method,
OptPeepMkword = Info ^ lopt_opt_peep_mkword,
peephole_optimize(GC_Method, OptPeepMkword, !Instrs, _Mod),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"peep", "after peephole", ProcLabel, !OptDebugInfo)
;
Mod1 = no
)
;
OptFrames = do_not_opt_frames
),
UseLocalVars = Info ^ lopt_use_local_vars,
(
UseLocalVars = use_local_vars,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing local vars for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
NumRealRRegs = Info ^ lopt_num_real_r_regs,
AccessThreshold = Info ^ lopt_local_vars_access_threshold,
AutoComments = Info ^ lopt_auto_comments,
use_local_vars_proc(!Instrs, NumRealRRegs, AccessThreshold,
AutoComments, ProcLabel, !LabelNumCounter),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter, "use_local",
"after use_local_vars", ProcLabel, !OptDebugInfo)
;
UseLocalVars = do_not_use_local_vars
).
:- pred optimize_last(llds_opt_info::in, set_tree234(label)::in,
proc_label::in, counter::in, counter::out, opt_debug_info::in,
list(instruction)::in, list(instruction)::out) is det.
optimize_last(Info, LayoutLabelSet, ProcLabel,
!LabelNumCounter, !.OptDebugInfo, !Instrs) :-
VeryVerbose = Info ^ lopt_very_verbose,
LabelStr = opt_util.format_proc_label(ProcLabel),
Reassign = Info ^ lopt_opt_reassign,
DelaySlot = Info ^ lopt_opt_delay_slots,
UseLocalVars = Info ^ lopt_use_local_vars,
StdLabels = Info ^ lopt_std_labels,
( if
( Reassign = opt_reassign
; DelaySlot = opt_delay_slot
; UseLocalVars = use_local_vars
; StdLabels = standardize_labels
)
then
% We must get rid of any extra labels added by other passes,
% since they can confuse reassign, wrap_blocks and delay_slot.
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing labels for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
labelopt_main(no, LayoutLabelSet, !Instrs, _Mod1),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"label", "after label opt", ProcLabel, !OptDebugInfo)
else
true
),
(
Reassign = opt_reassign,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing reassign for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
remove_reassign(!Instrs),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"reassign", "after reassign", ProcLabel, !OptDebugInfo)
;
Reassign = do_not_opt_reassign
),
(
DelaySlot = opt_delay_slot,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing delay slot for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
fill_branch_delay_slot(!Instrs),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"delay_slot", "after delay slots", ProcLabel, !OptDebugInfo)
;
DelaySlot = do_not_opt_delay_slot
),
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Optimizing returns for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
combine_decr_sp(!Instrs),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"decr_sp", "after combine decr_sp", ProcLabel, !OptDebugInfo),
(
StdLabels = standardize_labels,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Standardizing labels for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
standardize_labels(!Instrs, !LabelNumCounter),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"stdlabel", "after standard labels", ProcLabel, !OptDebugInfo)
;
StdLabels = do_not_standardize_labels
),
(
UseLocalVars = use_local_vars,
(
VeryVerbose = yes,
trace [io(!IO)] (
io.write_string("% Wrapping blocks for ", !IO),
io.write_string(LabelStr, !IO),
io.write_string("\n", !IO)
)
;
VeryVerbose = no
),
wrap_blocks(!Instrs),
maybe_opt_debug(Info, !.Instrs, !.LabelNumCounter,
"wrapblocks", "after wrap blocks", ProcLabel, !.OptDebugInfo, _)
;
UseLocalVars = do_not_use_local_vars
).
%-----------------------------------------------------------------------------%
% Mangle the given name just sufficiently to make it acceptable as a
% filename.
%
:- func mangle_name_as_filename(string) = string.
mangle_name_as_filename(Str0) = Str :-
string.foldl(escape_dir_char, Str0, "", Str).
:- pred escape_dir_char(char::in, string::in, string::out) is det.
escape_dir_char(Char, !Str) :-
( if dir.is_directory_separator(Char) then
!:Str = !.Str ++ "_slash_"
else
!:Str = !.Str ++ char_to_string(Char)
).
%-----------------------------------------------------------------------------%
:- type llds_opt_info
---> llds_opt_info(
lopt_debug_opt_pred_ids :: list(string),
lopt_debug_opt_pred_names :: list(string),
lopt_num_real_r_regs :: int,
lopt_local_vars_access_threshold :: int,
lopt_opt_repeat :: int,
lopt_gc_method :: gc_method,
lopt_debug_opt :: bool,
lopt_auto_comments :: maybe_auto_comments,
lopt_frameopt_comments :: bool,
lopt_detailed_statistics :: bool,
lopt_very_verbose :: bool,
lopt_checked_nondet_tailcalls ::
maybe_opt_checked_nondet_tailcalls,
lopt_opt_delay_slots :: maybe_opt_delay_slot,
lopt_opt_dups :: maybe_opt_dups,
lopt_opt_frames :: maybe_opt_frames,
lopt_opt_jumps :: maybe_opt_jumps,
lopt_opt_fulljumps :: maybe_opt_fulljumps,
lopt_opt_labels :: maybe_opt_labels,
lopt_opt_peep :: maybe_opt_peep,
lopt_opt_peep_mkword :: maybe_opt_peep_mkword,
lopt_opt_reassign :: maybe_opt_reassign,
lopt_pes_tailcalls :: maybe_pessimize_tailcalls,
lopt_std_labels :: maybe_standardize_labels,
lopt_use_local_vars :: maybe_use_local_vars
).
:- func init_llds_opt_info(globals) = llds_opt_info.
init_llds_opt_info(Globals) = Info :-
globals.lookup_accumulating_option(Globals, debug_opt_pred_id,
DebugOptPredIdStrs),
globals.lookup_accumulating_option(Globals, debug_opt_pred_name,
DebugOptPredNames),
globals.lookup_int_option(Globals, num_real_r_regs, NumRealRRegs),
globals.get_opt_tuple(Globals, OptTuple),
LocalVarAccessThreshold = OptTuple ^ ot_local_var_access_threshold,
OptRepeat = OptTuple ^ ot_opt_repeat,
globals.get_gc_method(Globals, GCMethod),
globals.lookup_bool_option(Globals, debug_opt, DebugOpt),
globals.lookup_bool_option(Globals, auto_comments, AutoCommentsOption),
( AutoCommentsOption = no, AutoComments = no_auto_comments
; AutoCommentsOption = yes, AutoComments = auto_comments
),
globals.lookup_bool_option(Globals, frameopt_comments, FrameOptComments),
globals.lookup_bool_option(Globals, detailed_statistics,
DetailedStatistics),
globals.lookup_bool_option(Globals, very_verbose, VeryVerbose),
CheckedNondetTailCalls = OptTuple ^ ot_opt_checked_nondet_tailcalls,
OptDelaySlots = OptTuple ^ ot_opt_delay_slot,
OptDups = OptTuple ^ ot_opt_dups,
OptFrames = OptTuple ^ ot_opt_frames,
OptJumps = OptTuple ^ ot_opt_jumps,
OptFullJumps = OptTuple ^ ot_opt_fulljumps,
OptLabels = OptTuple ^ ot_opt_labels,
OptPeep = OptTuple ^ ot_opt_peep,
OptPeepMkword = OptTuple ^ ot_opt_peep_mkword,
OptReassign = OptTuple ^ ot_opt_reassign,
PessimizeTailCalls = OptTuple ^ ot_pessimize_tailcalls,
StdLabels = OptTuple ^ ot_standardize_labels,
UseLocalVars = OptTuple ^ ot_use_local_vars,
Info = llds_opt_info(DebugOptPredIdStrs, DebugOptPredNames,
NumRealRRegs, LocalVarAccessThreshold, OptRepeat, GCMethod,
DebugOpt, AutoComments, FrameOptComments,
DetailedStatistics, VeryVerbose,
CheckedNondetTailCalls, OptDelaySlots, OptDups, OptFrames,
OptJumps, OptFullJumps, OptLabels, OptPeep, OptPeepMkword, OptReassign,
PessimizeTailCalls, StdLabels, UseLocalVars).
%-----------------------------------------------------------------------------%
:- end_module ll_backend.optimize.
%-----------------------------------------------------------------------------%