Files
mercury/compiler/structure_reuse.lbu.m
Zoltan Somogyi d787ee9355 Store var_tables in proc_infos.
This fixes the performance problem reported in Mantis bug #562.

compiler/hlds_pred.m:
    Instead of storing a varset and a vartypes in each proc_info,
    store just a var_table. Update the predicates that create
    or clone procedures accordingly.

    Where we had operations on proc_infos that had two versions,
    one operating on a varset/vartypes pair and one operating on var_table,
    keep only the latter, with the (shorter) name of the former.

    Delete the arity argument of proc_info_init, because the only
    valid value of that argument is the length of the list of the
    argument types. (In other words, this arg has been redundant
    all along.)

    Change the operations that create new variables in a procedure
    to get the caller to specify the (base) name of the new variable
    up front.

    Delete the unused predicate proc_info_ensure_unique_names.

compiler/type_util.m:
    Due to the change above, we now construct var_tables during the
    construction of the HLDS. The code that does that needs to fill in
    the field that says whether the type of each variable in the table
    is a dummy type or not. However, at this time, the pass that decides
    type representations has not been run yet. The code of is_type_a_dummy
    used to throw an exception in such situations.

    Change this so that in such situations, is_type_a_dummy returns
    a placeholder, not-guaranteed-to-be-correct value. Document why
    this is ok.

compiler/post_typecheck.m:
    Replace the placeholder values in vte_is_dummy fields in all
    the entries in the var_tables in all (valid) predicates with valid data.
    (If there are any invalid predicates, the compilation will fail anyway.)
    The clause_to_proc pass will copy these updated var_tables
    to be the initial var_tables in procedures.

compiler/make_goal.m:
    Change the operations that create new variables in a procedure
    to get the caller to specify the (base) name of the new variable
    up front. This is simpler than the old method, which created new
    variables without a name, and had the caller give them a name as
    a separate operation. And since var_tables need this info,
    get the caller to also specify whether the type is a dummy,
    if the type is not a builtin type which is known not to be a dummy.

compiler/var_table.m:
    Document the times when the types and is_dummy fields in var_table
    entries become meaningful.

    Fix a potential bug: when performing type substitutions in
    var_table entries, updating a variable's type may change whether
    that variable is a dummy or not, so recompute that info.
    It is quite possible that we *never* replace a nondummy type
    with a dummy type or vice versa, but in the absence of a convincing
    correctness argument for that proposition, better safe than sorry.

    Export the previously-private predicate transform_var_table
    to post_typecheck.

    Add code to implement the unused predicate deleted from hlds_pred.m:
    at the time I wrote it, I haven't yet realised that it was unused.
    The code I wrote here is therefore unused as well, so it is commented out.
    I did not delete it, because it may be useful later on.

compiler/direct_arg_in_out.m:
    Don't make and split var_tables, since it is no longer needed.

compiler/accumulator.m:
compiler/add_class.m:
compiler/add_clause.m:
compiler/add_heap_ops.m:
compiler/add_pred.m:
compiler/add_special_pred.m:
compiler/add_trail_ops.m:
compiler/arg_info.m:
compiler/build_mode_constraints.m:
compiler/bytecode_gen.m:
compiler/check_typeclass.m:
compiler/clause_to_proc.m:
compiler/closure_analysis.m:
compiler/code_gen.m:
compiler/code_loc_dep.m:
compiler/complexity.m:
compiler/continuation_info.m:
compiler/cse_detection.m:
compiler/ctgc.livedata.m:
compiler/deep_profiling.m:
compiler/default_func_mode.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/det_report.m:
compiler/distance_granularity.m:
compiler/equiv_type_hlds.m:
compiler/exception_analysis.m:
compiler/float_regs.m:
compiler/follow_code.m:
compiler/goal_mode.m:
compiler/goal_path.m:
compiler/higher_order.m:
compiler/hlds_out_pred.m:
compiler/hlds_rtti.m:
compiler/hlds_statistics.m:
compiler/inlining.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
compiler/introduce_exists_casts.m:
compiler/introduce_parallelism.m:
compiler/lambda.m:
compiler/lco.m:
compiler/live_vars.m:
compiler/liveness.m:
compiler/loop_inv.m:
compiler/mark_tail_calls.m:
compiler/ml_accurate_gc.m:
compiler/ml_args_util.m:
compiler/ml_closure_gen.m:
compiler/ml_gen_info.m:
compiler/ml_proc_gen.m:
compiler/mode_errors.m:
compiler/mode_info.m:
compiler/modecheck_goal.m:
compiler/par_loop_control.m:
compiler/pd_debug.m:
compiler/pd_info.m:
compiler/pd_util.m:
compiler/polymorphism_info.m:
compiler/post_typecheck.m:
compiler/proc_gen.m:
compiler/proc_requests.m:
compiler/purity.m:
compiler/push_goals_together.m:
compiler/quantification.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.live_variable_analysis.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.points_to_graph.m:
compiler/rbmm.points_to_info.m:
compiler/rbmm.region_liveness_info.m:
compiler/rbmm.region_transformation.m:
compiler/recompute_instmap_deltas.m:
compiler/saved_vars.m:
compiler/simplify_goal_unify.m:
compiler/simplify_info.m:
compiler/simplify_proc.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/stack_alloc.m:
compiler/stack_layout.m:
compiler/stack_opt.m:
compiler/stm_expand.m:
compiler/store_alloc.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.direct.detect_garbage.m:
compiler/structure_reuse.domain.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.lbu.m:
compiler/structure_reuse.lfu.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/structure_sharing.domain.m:
compiler/switch_detection.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_constr_initial.m:
compiler/term_errors.m:
compiler/term_pass1.m:
compiler/term_pass2.m:
compiler/trace_gen.m:
compiler/trailing_analysis.m:
compiler/try_expand.m:
compiler/tupling.m:
compiler/unneeded_code.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
    Conform to the changes above. Mostly this means

    - not passing a module_info to get a var_table out of a proc_info, but
    - having to pass a module_info to code that either constructs a var_table,
      or adds entries to a var_table (since we now need the type table
      to figure out whether variables' types are dummies).
2022-08-18 18:53:15 +10:00

238 lines
9.2 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2006-2008, 2010-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: structure_reuse.lbu.m.
% Main authors: nancy.
%
% Implementation of the process of annotating each program point within
% a procedure with local backward use information.
%
% Each program point (goal within a procedure definition) is annotated with a
% set of variables that are in Local Backward Use (LBU). A variable is said to
% be in LBU if it may be accessed upon backtracking. This information is
% computed based on the backtrack-vars (i.e. the input variables of the
% alternative goals of a disjunction), and forward use information.
%
% The implementation is based on the theory detailed in Nancy Mazur's PhD
% ("Instantiation 2", cf. Section 7.4).
% XXX Note: slight variations as to the treatment of disjunctions,
% switches and if-then-elses.
%
%-----------------------------------------------------------------------------%
:- module transform_hlds.ctgc.structure_reuse.lbu.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_pred.
:- pred backward_use_information(proc_info::in, proc_info::out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module check_hlds.
:- import_module check_hlds.type_util.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_llds.
:- import_module parse_tree.
:- import_module parse_tree.prog_data.
:- import_module parse_tree.set_of_var.
:- import_module parse_tree.var_table.
:- import_module list.
:- import_module require.
%-----------------------------------------------------------------------------%
backward_use_information(!ProcInfo):-
proc_info_get_goal(!.ProcInfo, Goal0),
proc_info_get_var_table(!.ProcInfo, VarTable),
% Before the first goal, the set of variables in LBU is empty.
LBU0 = set_of_var.init,
backward_use_in_goal(VarTable, Goal0, Goal, LBU0, _LBU),
proc_info_set_goal(Goal, !ProcInfo).
:- pred backward_use_in_goal(var_table::in, hlds_goal::in, hlds_goal::out,
set_of_progvar::in, set_of_progvar::out) is det.
backward_use_in_goal(VarTable, !TopGoal, !LBU) :-
!.TopGoal = hlds_goal(Expr0, Info0),
% Add resume_vars to the LBU-set.
set_of_var.union(get_backtrack_vars(VarTable, Info0), !LBU),
backward_use_in_goal_2(VarTable, Info0, Expr0, Expr, !LBU),
goal_info_set_lbu(!.LBU, Info0, Info),
!:TopGoal = hlds_goal(Expr, Info).
:- pred backward_use_in_goal_2(var_table::in, hlds_goal_info::in,
hlds_goal_expr::in, hlds_goal_expr::out,
set_of_progvar::in, set_of_progvar::out) is det.
backward_use_in_goal_2(VarTable, Info0, !Expr, !LBU) :-
% Handle each goal type separately:
(
!.Expr = unify(_, _, _, _, _)
;
!.Expr = plain_call(_,_, _, _, _, _),
Det = goal_info_get_determinism(Info0),
( if detism_allows_multiple_solns(Det) then
% Implementation of Instantiation 2 from Nancy's PhD.
% In this instantation, a non-deterministic procedure
% call only adds its LFU-variables to the current set
% of lbu-variables. Cf. PhD Nancy Mazur.
goal_info_get_pre_births(Info0, PreBirths),
goal_info_get_post_births(Info0, PostBirths),
!:LBU = set_of_var.union_list([goal_info_get_lfu(Info0),
remove_typeinfo_vars_from_set_of_var(VarTable, PreBirths),
remove_typeinfo_vars_from_set_of_var(VarTable, PostBirths),
!.LBU])
else
true
)
;
!.Expr = generic_call(_, _, _, _, _)
;
% XXX Can they be nondet? If so, LFU variables need to be added
% to !LBU.
!.Expr = call_foreign_proc(_, _, _, _, _, _, _)
;
!.Expr = conj(ConjType, Goals0),
backward_use_in_conj(VarTable, Goals0, Goals, !LBU),
!:Expr = conj(ConjType, Goals)
;
!.Expr = disj(Goals0),
backward_use_in_disj(VarTable, Goals0, Goals, !LBU),
!:Expr = disj(Goals)
;
!.Expr = switch(A, B, Cases0),
backward_use_in_cases(VarTable, Cases0, Cases, !LBU),
!:Expr = switch(A, B, Cases)
;
!.Expr = negation(SubGoal0),
% handled as: if SubGoal0 then fail else true
LBU0 = !.LBU,
backward_use_in_goal(VarTable, SubGoal0, SubGoal, !.LBU, _),
% A negation does not introduce any choice-points! Hence the
% negation itself is deterministic, and no new variables in LBU
% are introduced into the resulting LBU-set.
!:LBU = LBU0,
!:Expr = negation(SubGoal)
;
!.Expr = scope(Reason, SubGoal0),
( if Reason = from_ground_term(_, from_ground_term_construct) then
SubGoal = SubGoal0
else
% XXX We could treat from_ground_term_deconstruct specially
% as well.
backward_use_in_goal(VarTable, SubGoal0, SubGoal, !LBU)
),
!:Expr = scope(Reason, SubGoal)
;
% XXX The implementation for if-then-else is different from the theory
% in the thesis. We can obtain more precision when the Condition-goal
% is deterministic, which means that the Then-goal can be analysed with
% the initial LBU-set (instead of the set obtained after the analysis
% of the Condition-goal).
!.Expr = if_then_else(Vars, Cond0, Then0, Else0),
LBU0 = !.LBU,
% Annotate Cond-goal.
backward_use_in_goal(VarTable, Cond0, Cond, LBU0, _),
% Annotate Then-goal.
% When annotating the then-part, the lbu used for it should not
% contain the resume-vars due to the else part.
% trick: to calculate inital LBU for the Then-goal, we set the
% resume-point of the condition to no_resume_point.
Cond0 = hlds_goal(CondGoal0, CondInfo0),
goal_info_set_resume_point(no_resume_point, CondInfo0, InfoTmp),
CondTmp = hlds_goal(CondGoal0, InfoTmp),
backward_use_in_goal(VarTable, CondTmp, _, LBU0, LBU0T),
backward_use_in_goal(VarTable, Then0, Then, LBU0T, LBUT),
% Annotate Else-goal.
backward_use_in_goal(VarTable, Else0, Else, LBU0, LBUE),
set_of_var.union(LBUT, LBUE, !:LBU),
!:Expr = if_then_else(Vars, Cond, Then, Else)
;
!.Expr = shorthand(_),
% These should have been expanded out by now.
unexpected($pred, "shorthand")
).
:- func get_backtrack_vars(var_table, hlds_goal_info) = set_of_progvar.
get_backtrack_vars(VarTable, Info) = Vars :-
goal_info_get_resume_point(Info, ResPoint),
(
ResPoint = resume_point(ResVars, _),
Vars = remove_typeinfo_vars_from_set_of_var(VarTable, ResVars)
;
ResPoint = no_resume_point,
Vars = set_of_var.init
).
:- pred detism_allows_multiple_solns(prog_data__determinism::in) is semidet.
detism_allows_multiple_solns(detism_non).
detism_allows_multiple_solns(detism_multi).
detism_allows_multiple_solns(detism_cc_non).
detism_allows_multiple_solns(detism_cc_multi).
:- pred backward_use_in_conj(var_table::in, list(hlds_goal)::in,
list(hlds_goal)::out, set_of_progvar::in, set_of_progvar::out) is det.
backward_use_in_conj(VarTable, !Goals, !LBU) :-
list.map_foldl(backward_use_in_goal(VarTable), !Goals, !LBU).
:- pred backward_use_in_cases(var_table::in, list(case)::in, list(case)::out,
set_of_progvar::in, set_of_progvar::out) is det.
backward_use_in_cases(VarTable, !Cases, !LBU) :-
% Every case is analysed with the same initial set of LBU-vars.
LBU0 = !.LBU,
list.map_foldl(backward_use_in_case(LBU0, VarTable), !Cases, !LBU).
:- pred backward_use_in_case(set_of_progvar::in, var_table::in,
case::in, case::out, set_of_progvar::in, set_of_progvar::out) is det.
backward_use_in_case(LBU0, VarTable, !Case, !LBU):-
!.Case = case(MainConsId, OtherConsIds, Goal0),
backward_use_in_goal(VarTable, Goal0, Goal, LBU0, NewLBU),
!:Case = case(MainConsId, OtherConsIds, Goal),
set_of_var.union(NewLBU, !LBU).
:- pred backward_use_in_disj(var_table::in,
list(hlds_goal)::in, list(hlds_goal)::out,
set_of_progvar::in, set_of_progvar::out) is det.
backward_use_in_disj(VarTable, !Goals, !LBU) :-
% Every disj-goal is analysed with the same initial set of LBU-vars.
LBU0 = !.LBU,
list.map_foldl(backward_use_in_disj_goal(LBU0, VarTable), !Goals, !LBU).
:- pred backward_use_in_disj_goal(set_of_progvar::in, var_table::in,
hlds_goal::in, hlds_goal::out,
set_of_progvar::in, set_of_progvar::out) is det.
backward_use_in_disj_goal(LBU0, VarTable, !Goal, !LBU) :-
backward_use_in_goal(VarTable, !Goal, LBU0, NewLBU),
set_of_var.union(NewLBU, !LBU).
%-----------------------------------------------------------------------------%
:- end_module transform_hlds.ctgc.structure_reuse.lbu.
%-----------------------------------------------------------------------------%