Files
mercury/compiler/rbmm.points_to_info.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

167 lines
6.1 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2005-2007, 2009-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 rbmm.points_to_info.m.
% Main author: Quan Phan.
%
% This module defines the "rpta_info" and "rpta_info_table" types.
% rpta_info_table maps a procedure to its corresponding rpt information
% (i.e., the rpt graph and the alpha mappings (at the call sites in it)).
%
%---------------------------------------------------------------------------%
:- module transform_hlds.rbmm.points_to_info.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_pred.
:- import_module transform_hlds.rbmm.points_to_graph.
:- import_module transform_hlds.smm_common.
:- import_module map.
%---------------------------------------------------------------------------%
:- type rpta_info_table == map(pred_proc_id, rpta_info).
:- func rpta_info_table_init = rpta_info_table.
:- func rpta_info_table_search_rpta_info(pred_proc_id, rpta_info_table)
= rpta_info is semidet.
:- pred rpta_info_table_set_rpta_info(pred_proc_id::in, rpta_info::in,
rpta_info_table::in, rpta_info_table::out) is det.
:- type rpta_info
---> rpta_info(rpt_graph, rpt_alpha_mapping).
% The rpta_info will be for a specific procedure, at the beginning
% the alpha mapping is empty and the rpt graph contains all the nodes
% corresponding to all the variables appear in the procedure.
%
:- func rpta_info_init(proc_info) = rpta_info.
:- pred rpta_info_equal(rpta_info::in, rpta_info::in) is semidet.
%---------------------------------------------------------------------------%
% This type represents the alpha mapping of all call sites in a procedure.
% For documentation of alpha mappings, see Chapter 4 in Quan's thesis;
% basically, they map the nodes in the region points-to graph of the callee
% to the corresponding nodes in the caller.
%
:- type rpt_alpha_mapping == map(program_point, rpt_call_alpha_mapping).
% This type represents the alpha mapping at one call site.
%
:- type rpt_call_alpha_mapping == map(rptg_node, rptg_node).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module parse_tree.
:- import_module parse_tree.prog_data.
:- import_module parse_tree.var_table.
:- import_module bool.
:- import_module int.
:- import_module list.
:- import_module set.
:- import_module string.
%---------------------------------------------------------------------------%
rpta_info_table_init = map.init.
rpta_info_table_search_rpta_info(PredProcId, Table) = RptaInfo :-
Table ^ elem(PredProcId) = RptaInfo.
rpta_info_table_set_rpta_info(PredProcId, RptaInfo, !Table) :-
!Table ^ elem(PredProcId) := RptaInfo.
rpta_info_init(ProcInfo) = RptaInfo :-
proc_info_get_var_table(ProcInfo, VarTable),
var_table_vars(VarTable, Vars),
list.foldl2(add_node_from_var(VarTable), Vars, 1, _Reg,
rpt_graph_init, Graph),
map.init(AlphaMapping),
RptaInfo = rpta_info(Graph, AlphaMapping).
:- pred add_node_from_var(var_table::in, prog_var::in, int::in,
int::out, rpt_graph::in, rpt_graph::out) is det.
add_node_from_var(VarTable, Var, Reg0, Reg, !Graph) :-
lookup_var_type(VarTable, Var, NodeType),
set.init(Varset0),
set.insert(Var, Varset0, Varset),
Reg = Reg0 + 1,
string.append("R", string.int_to_string(Reg0), RegName),
NodeInfo = rptg_node_content(Varset, RegName, set.init, NodeType, bool.no),
rptg_add_node(NodeInfo, _Node, !Graph).
rpta_info_equal(RptaInfoA, RptaInfoB):-
RptaInfoA = rpta_info(GraphA, AlphaA),
RptaInfoB = rpta_info(GraphB, AlphaB),
rptg_equal(GraphA, GraphB),
rpt_alpha_mapping_equal(AlphaA, AlphaB).
%---------------------------------------------------------------------------%
%
% Alpha mapping at call sites
%
:- pred rpt_alpha_mapping_equal(rpt_alpha_mapping::in,
rpt_alpha_mapping::in) is semidet.
rpt_alpha_mapping_equal(AlphaMappingA, AlphaMappingB) :-
map.count(AlphaMappingA, CA),
map.count(AlphaMappingB, CB),
CA = CB,
map.keys(AlphaMappingA, CallSitesA),
rpt_alpha_mapping_equal_2(CallSitesA, AlphaMappingA, AlphaMappingB).
:- pred rpt_alpha_mapping_equal_2(list(program_point)::in,
rpt_alpha_mapping::in, rpt_alpha_mapping::in) is semidet.
rpt_alpha_mapping_equal_2([], _, _).
rpt_alpha_mapping_equal_2([CallSiteA | CallSiteAs],
AlphaMappingA, AlphaMappingB) :-
map.search(AlphaMappingB, CallSiteA, AlphaMappingAtCallSiteB),
map.lookup(AlphaMappingA, CallSiteA, AlphaMappingAtCallSiteA),
rpt_alpha_mapping_at_call_site_equal(
AlphaMappingAtCallSiteA,AlphaMappingAtCallSiteB),
rpt_alpha_mapping_equal_2(CallSiteAs, AlphaMappingA, AlphaMappingB).
:- pred rpt_alpha_mapping_at_call_site_equal(rpt_call_alpha_mapping::in,
rpt_call_alpha_mapping::in) is semidet.
rpt_alpha_mapping_at_call_site_equal(AMAtCallSiteA, AMAtCallSiteB) :-
map.count(AMAtCallSiteA, CA),
map.count(AMAtCallSiteB, CB),
CA = CB,
map.keys(AMAtCallSiteA, NodesA),
rpt_alpha_mapping_at_call_site_equal_2(NodesA, AMAtCallSiteA,
AMAtCallSiteB).
:- pred rpt_alpha_mapping_at_call_site_equal_2(list(rptg_node)::in,
rpt_call_alpha_mapping::in, rpt_call_alpha_mapping::in) is semidet.
rpt_alpha_mapping_at_call_site_equal_2([], _, _).
rpt_alpha_mapping_at_call_site_equal_2([N | Ns], AMAtCallSiteA,
AMAtCallSiteB) :-
map.search(AMAtCallSiteB, N, NPrimeB),
map.lookup(AMAtCallSiteA, N, NPrimeA),
NPrimeA = NPrimeB,
rpt_alpha_mapping_at_call_site_equal_2(Ns, AMAtCallSiteA, AMAtCallSiteB).
%---------------------------------------------------------------------------%
:- end_module transform_hlds.rbmm.points_to_info.
%---------------------------------------------------------------------------%