Files
mercury/compiler/ctgc.livedata.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

401 lines
14 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2006, 2010-2011 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: ctgc.livedata.m.
% Main author: nancy.
%
% Definition of the live_data type used to represent the set of datastructures
% that are probably (not definitely!) live at a given program point (goal).
% A data structure is said to be "live" if the memory used to represent
% that data structure may possibly be accessed during the further execution
% of the program w.r.t. the program point (goal) looked at.
%
%-----------------------------------------------------------------------------%
:- module transform_hlds.ctgc.livedata.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module parse_tree.
:- import_module parse_tree.prog_data.
:- import_module parse_tree.prog_data_pragma.
:- import_module parse_tree.var_table.
:- import_module transform_hlds.ctgc.structure_sharing.
:- import_module transform_hlds.ctgc.structure_sharing.domain.
:- import_module list.
%-----------------------------------------------------------------------------%
:- type livedata.
% Create an initial set of live data structure, possibly given a list
% of live variables or data structures.
%
:- func livedata_init = livedata. % Which assumes that nothing is live.
:- func livedata_init_from_vars(live_vars) = livedata.
:- func livedata_init_from_datastructs(live_datastructs) = livedata.
:- func livedata_init_as_top = livedata.
% Verify whether the liveness set is bottom resp. top.
:- pred livedata_is_bottom(livedata::in) is semidet.
:- pred livedata_is_top(livedata::in) is semidet.
% Return the list of live data structures represented by livedata.
% Returns a software error when the livedata set is top.
%
:- func livedata_get_datastructs(livedata) = list(datastruct).
% Least upper bound of all the livedata.
%
:- func livedata_least_upper_bound(module_info, var_table,
livedata, livedata) = livedata.
:- func livedata_least_upper_bound_list(module_info, var_table,
list(livedata)) = livedata.
% Subsumption predicates.
%
:- pred livedata_subsumes_prog_var(livedata::in, prog_var::in) is semidet.
:- pred livedata_subsumes_datastruct(module_info::in, var_table::in,
livedata::in, datastruct::in) is semidet.
% Projection operation.
%
:- func livedata_project(list(prog_var), livedata) = livedata.
%-----------------------------------------------------------------------------%
:- func livedata_init_at_goal(module_info, var_table, hlds_goal_info,
sharing_as) = livedata.
:- func livedata_add_liveness(module_info, proc_info, live_datastructs,
sharing_as, livedata) = livedata.
:- pred nodes_are_not_live(module_info::in, var_table::in,
list(datastruct)::in, livedata::in, nodes_are_not_live_result::out) is det.
:- type nodes_are_not_live_result
---> nodes_all_live
; nodes_are_live(list(datastruct)).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module parse_tree.set_of_var.
:- import_module transform_hlds.ctgc.datastruct.
:- import_module pair.
:- import_module require.
%-----------------------------------------------------------------------------%
:- type livedata
---> livedata_bottom % There are no live data structures.
; livedata_top % All data structures may be live.
; livedata_live(list(datastruct)).
% Only the listed datastructures can
% possibly be live.
%-----------------------------------------------------------------------------%
livedata_init = livedata_bottom.
livedata_init_from_vars(LiveVars) =
livedata_live(list.map(datastruct_init, LiveVars)).
livedata_init_from_datastructs(Data) =
livedata_live(Data).
livedata_init_as_top = livedata_top.
livedata_is_bottom(livedata_bottom).
livedata_is_top(livedata_top).
livedata_get_datastructs(livedata_bottom) = [].
livedata_get_datastructs(livedata_live(Data)) = Data.
livedata_get_datastructs(livedata_top) =
unexpected($pred, "livedata_get_datastructs: livedata is top.").
livedata_least_upper_bound(ModuleInfo, VarTable, LiveData1,
LiveData2) = LiveData :-
(
LiveData1 = livedata_bottom,
LiveData = LiveData2
;
LiveData1 = livedata_top,
LiveData = livedata_top
;
LiveData1 = livedata_live(Data1),
(
LiveData2 = livedata_bottom,
LiveData = LiveData1
;
LiveData2 = livedata_top,
LiveData = livedata_top
;
LiveData2 = livedata_live(Data2),
LiveData = livedata_live(
datastruct_lists_least_upper_bound(ModuleInfo,
VarTable, Data1, Data2))
)
).
livedata_least_upper_bound_list(ModuleInfo, VarTable, LiveDataList)
= list.foldl(livedata_least_upper_bound(ModuleInfo, VarTable),
LiveDataList, livedata_init).
livedata_subsumes_prog_var(LiveData, ProgVar) :-
livedata_subsumes_topcell(LiveData, datastruct_init(ProgVar)).
:- pred livedata_subsumes_topcell(livedata::in, datastruct::in) is semidet.
livedata_subsumes_topcell(LiveData, TopCell) :-
(
LiveData = livedata_top
;
LiveData = livedata_live(Data),
list.member(TopCell, Data)
).
livedata_subsumes_datastruct(ModuleInfo, VarTable, LiveData, Datastruct):-
( if datastruct_refers_to_topcell(Datastruct) then
livedata_subsumes_topcell(LiveData, Datastruct)
else
livedata_subsumes_datastruct_with_selector(ModuleInfo, VarTable,
LiveData, Datastruct)
).
:- pred livedata_subsumes_datastruct_with_selector(module_info::in,
var_table::in, livedata::in, datastruct::in) is semidet.
livedata_subsumes_datastruct_with_selector(ModuleInfo, VarTable, LiveData,
Datastruct) :-
(
LiveData = livedata_top
;
LiveData = livedata_live(Data),
datastruct_subsumed_by_list(ModuleInfo, VarTable, Datastruct, Data)
).
livedata_project(ProgVars, LiveData) = ProjectedLiveData :-
(
LiveData = livedata_bottom,
ProjectedLiveData = livedata_bottom
;
LiveData = livedata_top,
ProjectedLiveData = livedata_top
;
LiveData = livedata_live(Data),
list.filter(list_contains_datastruct_var(ProgVars),
Data, FilteredData),
(
FilteredData = [],
ProjectedLiveData = livedata_bottom
;
FilteredData = [_ | _],
ProjectedLiveData = livedata_live(FilteredData)
)
).
:- pred list_contains_datastruct_var(prog_vars::in, datastruct::in) is semidet.
list_contains_datastruct_var(ProgVars, Datastruct) :-
list.member(Datastruct ^ sc_var, ProgVars).
%-----------------------------------------------------------------------------%
livedata_init_at_goal(ModuleInfo, VarTable, GoalInfo, SharingAs) = LiveData :-
% Collect the
% (XXX collect the what?)
Lfu = goal_info_get_lfu(GoalInfo),
Lbu = goal_info_get_lbu(GoalInfo),
Lu = set_of_var.to_sorted_list(set_of_var.union(Lfu, Lbu)),
( if
% When there are no data structure in forward nor backward use,
% then the livedata set is empty.
Lu = []
then
LiveData = livedata_init
else if
% If Lu is not empty, and sharing is top, then all possible
% datastructures might possibly be live.
sharing_as_is_top(SharingAs)
then
LiveData = livedata_init_as_top
else if
% Lu not empty, and sharing is bottom... then only the
% datastructures in local use are live.
sharing_as_is_bottom(SharingAs)
then
LiveData = livedata_init_from_vars(Lu)
else
% Otherwise we have the most general case: Lu not empty, Sharing
% not top nor bottom.
SharingDomain = to_structure_sharing_domain(SharingAs),
(
SharingDomain = structure_sharing_real(SharingPairs),
LiveData = livedata_init_at_goal_2(ModuleInfo, VarTable, Lu,
SharingPairs)
;
( SharingDomain = structure_sharing_bottom
; SharingDomain = structure_sharing_top(_)
),
unexpected($pred, "unexpected SharingDomain")
)
).
% Preconditions: live_vars is not empty, sharing_as is not top.
%
% This function corresponds to pa_alias_as.live_2 in the old reuse branch
% which is significantly more involved so there may still be more code to
% port over. However, its caller always passes the empty set for the
% LIVE_0 argument which would make a lot of code unnecessary.
%
:- func livedata_init_at_goal_2(module_info, var_table, live_vars,
structure_sharing) = livedata.
livedata_init_at_goal_2(ModuleInfo, VarTable, Lu, SharingPairs) = LiveData :-
% LIVE1 = top-level datastructs from Lu
LuData = list.map(datastruct_init, Lu),
LIVE1 = LuData,
% LIVE2 = datastructs X^s such that X^s is aliased to Y^t,
% and Y is in Lu.
live_from_in_use(ModuleInfo, VarTable, LuData, SharingPairs, LIVE2),
% LIVE3 was some complicated thing.
% But since LIVE_0 is the empty set LIVE3 would be empty too.
LiveData = livedata_live(LIVE1 ++ LIVE2).
% In use information is stored as a set of datastructures. This
% procedure computes the set of live datastructure using the in-use set
% and extending it wrt the sharing information.
%
:- pred live_from_in_use(module_info::in, var_table::in, list(datastruct)::in,
structure_sharing::in, list(datastruct)::out) is det.
live_from_in_use(ModuleInfo, VarTable, InUseList, SharingPairs, Live):-
% Filter the list of sharing pairs, keeping only the ones that
% involve at least one variable from the IN_USE (LU) set.
list.map(one_of_vars_is_live(ModuleInfo, VarTable, InUseList),
SharingPairs, DatastructsLists),
list.condense(DatastructsLists, Live).
% one_of_vars_is_live(LIST, ALIAS, X^sx1)
% returns true if
% ALIAS = X^sx - Y^sy
% and Y^s1 \in LIST and
% sy = s1.s2 => sx1 = sx
% or
% sy.s2 = s1 => sx1 = sx.s2
%
:- pred one_of_vars_is_live(module_info::in, var_table::in,
list(datastruct)::in, structure_sharing_pair::in, list(datastruct)::out)
is det.
one_of_vars_is_live(ModuleInfo, VarTable, Datastructs0, PairXY,
Datastructs) :-
one_of_vars_is_live_ordered(ModuleInfo, VarTable, Datastructs0,
PairXY, L1),
% Switch order.
PairXY = X - Y,
PairYX = Y - X,
one_of_vars_is_live_ordered(ModuleInfo, VarTable, Datastructs0,
PairYX, L2),
Datastructs = L1 ++ L2.
:- pred one_of_vars_is_live_ordered(module_info::in, var_table::in,
list(datastruct)::in, structure_sharing_pair::in,
list(datastruct)::out) is det.
one_of_vars_is_live_ordered(ModuleInfo, VarTable, List, Pair, List_Xsx1) :-
Pair = Xsx - Ysy,
list.filter(datastruct_same_vars(Ysy), List, Y_List),
( if
% First try to find one of the found datastructs which is
% fully alive: so that Ysy is less or equal to at least one
% Ys1 in Y_List (sy = s1.s2)
list.filter(datastruct_subsumed_by(ModuleInfo, VarTable, Ysy),
Y_List, FY_List),
FY_List = [_ | _]
then
Xsx1 = Xsx,
List_Xsx1 = [Xsx1]
else
% Find all datastructs from Y_List which are less or
% equal to Ysy. Select the one with the shortest selector
% (Note that there should be only one solution. If more
% than one such datastruct is found, the initial live set
% is not minimal, while this should be somehow guaranteed).
list.filter_map(
datastruct_subsumed_by_return_selector(ModuleInfo, VarTable, Ysy),
Y_List, SelectorList),
% Each sx1 = sx.s2, where s2 is one of SelectorList.
list.map(
( pred(S2::in, Xsx1::out) is det :-
datastruct_termshift(ModuleInfo, VarTable, S2, Xsx) = Xsx1
), SelectorList, List_Xsx1)
).
livedata_add_liveness(ModuleInfo, ProcInfo, LuData, LocalSharing, LiveData0)
= LiveData :-
( if sharing_as_is_top(LocalSharing) then
LiveData = livedata_init_as_top
else if sharing_as_is_bottom(LocalSharing) then
proc_info_get_var_table(ProcInfo, VarTable),
LiveData = livedata_least_upper_bound(ModuleInfo, VarTable,
LiveData0, livedata_init_from_datastructs(LuData))
else
% most general case: normal sharing.
proc_info_get_var_table(ProcInfo, VarTable),
ExtendLuData = extend_datastructs(ModuleInfo, ProcInfo,
LocalSharing, LuData),
LuLiveData = livedata_init_from_datastructs(ExtendLuData),
ExtendLiveData = extend_livedata(ModuleInfo, ProcInfo,
LocalSharing, LiveData0),
LiveData = livedata_least_upper_bound(ModuleInfo, VarTable,
LuLiveData, ExtendLiveData)
).
:- func extend_livedata(module_info, proc_info, sharing_as, livedata)
= livedata.
extend_livedata(ModuleInfo, ProcInfo, SharingAs, LiveData0) = LiveData :-
(
LiveData0 = livedata_bottom,
LiveData = livedata_bottom
;
LiveData0 = livedata_top,
LiveData = livedata_top
;
LiveData0 = livedata_live(Data0),
LiveData = livedata_live(extend_datastructs(ModuleInfo, ProcInfo,
SharingAs, Data0))
).
nodes_are_not_live(ModuleInfo, VarTable, DeadNodes, LiveData, Result) :-
(
LiveData = livedata_top,
Result = nodes_all_live
;
LiveData = livedata_bottom,
Result = nodes_are_live([])
;
LiveData = livedata_live(LiveDatastructs),
datastructs_that_are_subsumed_by_list(ModuleInfo, VarTable,
DeadNodes, LiveDatastructs, SubsumedNodes),
Result = nodes_are_live(SubsumedNodes)
).
%-----------------------------------------------------------------------------%
:- end_module transform_hlds.ctgc.livedata.
%-----------------------------------------------------------------------------%