Files
mercury/compiler/rbmm.region_arguments.m
Zoltan Somogyi 1d3cd2d0b1 Delete invalid procs from their pred_info.
compiler/hlds_pred.m:
    This module used to maintain a distinction between valid and invalid
    procedures in a pred_info. The distinction was based on whether the
    proc_info field containing a list of mode_error_infos was empty
    (such procedures were valid) or nonempty (such procedures were invalid).

    This field was used only during the early phases of the compiler
    from mode analysis to unique mode analysis, but all later passes
    had to check whether the field was empty before processing the procedure.

    This diff deletes this field from proc_infos. The information that this
    field used to contain is now stored in *temporary* data structures
    maintained and used only by the mode and unique mode analysis phases.
    These phases use the code they share in modes.m to delete all invalid
    procedures from the HLDS before they hand over that HLDS to other phases.
    This means that outside these two compiler phases, *all* procedures in the
    HLDS will be valid.

    Delete all service predicates and functions that tested procedures
    for validity, since this has now become a meaningless test. In one case,
    where there was no non-validity-testing equivalent, make one.

compiler/mode_info.m:
    Define the proc_mode_error_map, which effectively replaces the fields
    deleted from proc_infos. Define the operations on it that we need.

compiler/modes.m:
    Initialize proc_mode_error_maps to empty, and then pass them through
    mode analysis as part of the mode_info, allowing mode analysis code
    to use it to check procedure validity.

    When a mode analysis phase (either ordinary or unique mode analysis)
    is done, delete the procedures that we have now detected are invalid.
    However, before we do, print any inference messages about them.

compiler/unique_modes.m:
    Use the new field in mode_info to check procedures' validity.

    Delete the unique_modes_check_proc predicate, because it had
    only one caller in modes.m, which called another predicate in modes.m
    through it.

compiler/modecheck_call.m:
compiler/modecheck_unify.m:
    Use the new field in mode_info to check procedures' validity.

compiler/try_expand.m:
    Conform to the changes above.

    When a mode check of a procedure repeated after try expansion finds
    an error, delete the now-detected-to-be-invalid procedure.

compiler/cse_detection.m:
    Conform to the changes above.

    When a mode check of a procedure repeated after common subexpression
    eliminate finds an error, don't bother to delete the now-detected-to-be-
    invalid procedure, because the code on that path throws an exception
    anyway.

    Fix an old incongruity: one trace goal created a new ProgressStream
    in a predicate that would have been given existing one if needed.

compiler/direct_arg_in_out.m:
    Conform to the changes above by deleting a validity test.

    Delete a predicate that had no job *except* that validity test.

compiler/style_checks.m:
    Use missing procedure ids to detect invalid procedures. Add an XXX
    about a limitation of this approach.

compiler/bytecode_gen.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/distance_granularity.m:
compiler/exception_analysis.m:
compiler/float_regs.m:
compiler/goal_mode.m:
compiler/higher_order.m:
compiler/hlds_call_tree.m:
compiler/hlds_dependency_graph.m:
compiler/hlds_out_pred.m:
compiler/intermod.m:
compiler/intermod_analysis.m:
compiler/introduce_parallelism.m:
compiler/lambda.m:
compiler/liveness.m:
compiler/mark_tail_calls.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/ml_proc_gen.m:
compiler/passes_aux.m:
compiler/pd_util.m:
compiler/polymorphism.m:
compiler/polymorphism_goal.m:
compiler/proc_gen.m:
compiler/purity.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_path.m:
compiler/rbmm.live_region_analysis.m:
compiler/rbmm.live_variable_analysis.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_arguments.m:
compiler/rbmm.region_instruction.m:
compiler/rbmm.region_transformation.m:
compiler/ssdebug.m:
compiler/stm_expand.m:
compiler/stratify.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.direct.m:
compiler/structure_reuse.domain.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_initial.m:
compiler/termination.m:
compiler/trailing_analysis.m:
compiler/untupling.m:
compiler/unused_args.m:
    Conform to the changes above, mostly by deleting validity tests.
2023-07-18 14:03:14 +02:00

348 lines
15 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 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.region_arguments.m.
% Main author: qph.
%
% We will pass regions as extra arguments in procedure calls.
% After the region liveness analysis we can decide on what region variables
% need to be region arguments (for procedures and calls).
% This module derives the formal region arguments for procedures and
% the actual region arguments at call sites in each procedure.
% This information will be used to extend the argument lists of procedures
% and calls in the HLDS.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- module transform_hlds.rbmm.region_arguments.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module transform_hlds.rbmm.points_to_graph.
:- import_module transform_hlds.rbmm.points_to_info.
:- import_module transform_hlds.rbmm.region_liveness_info.
:- import_module transform_hlds.smm_common.
:- import_module list.
:- import_module map.
:- type proc_formal_region_args_table
== map(
pred_proc_id,
region_args
).
:- type proc_pp_actual_region_args_table
== map(
pred_proc_id,
pp_actual_region_args_table
).
:- type pp_actual_region_args_table
== map(
program_point,
region_args
).
:- type region_args
---> region_args(
list(rptg_node), % constant (carried) region arguments.
list(rptg_node), % inputs (removed).
list(rptg_node) % outputs (created).
).
:- pred record_region_arguments(module_info::in, rpta_info_table::in,
proc_region_set_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_formal_region_args_table::out,
proc_pp_actual_region_args_table::out) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module hlds.goal_path.
:- import_module hlds.hlds_goal.
:- import_module libs.
:- import_module libs.globals.
:- import_module libs.options.
:- import_module bool.
:- import_module require.
:- import_module set.
record_region_arguments(ModuleInfo, RptaInfoTable, ConstantRTable,
DeadRTable, BornRTable, FormalRegionArgTable, ActualRegionArgTable) :-
module_info_get_valid_pred_ids(ModuleInfo, PredIds),
list.foldl2(record_actual_region_arguments_pred(ModuleInfo,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable),
PredIds, map.init, FormalRegionArgTable,
map.init, ActualRegionArgTable).
:- pred record_actual_region_arguments_pred(module_info::in,
rpta_info_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_region_set_table::in, pred_id::in,
proc_formal_region_args_table::in,
proc_formal_region_args_table::out,
proc_pp_actual_region_args_table::in,
proc_pp_actual_region_args_table::out) is det.
record_actual_region_arguments_pred(ModuleInfo, RptaInfoTable,
ConstantRTable, DeadRTable, BornRTable, PredId,
!FormalRegionArgTable, !ActualRegionArgTable) :-
module_info_pred_info(ModuleInfo, PredId, PredInfo),
ProcIds = pred_info_all_non_imported_procids(PredInfo),
list.foldl2(record_region_arguments_proc(ModuleInfo, PredId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable), ProcIds,
!FormalRegionArgTable, !ActualRegionArgTable).
:- pred record_region_arguments_proc(module_info::in, pred_id::in,
rpta_info_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_region_set_table::in, proc_id::in,
proc_formal_region_args_table::in,
proc_formal_region_args_table::out,
proc_pp_actual_region_args_table::in,
proc_pp_actual_region_args_table::out) is det.
record_region_arguments_proc(ModuleInfo, PredId, RptaInfoTable,
ConstantRTable, DeadRTable, BornRTable, ProcId,
!FormalRegionArgTable, !ActualRegionArgTable) :-
PPId = proc(PredId, ProcId),
( if some_are_special_preds([PPId], ModuleInfo) then
true
else
record_formal_region_arguments_proc(ModuleInfo, PPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable,
!FormalRegionArgTable),
module_info_proc_info(ModuleInfo, PPId, ProcInfo0),
fill_goal_path_slots_in_proc(ModuleInfo, ProcInfo0, ProcInfo),
proc_info_get_goal(ProcInfo, Body),
record_actual_region_arguments_goal(ModuleInfo, PPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable, Body,
!FormalRegionArgTable, map.init, ActualRegionArgProc),
map.set(PPId, ActualRegionArgProc, !ActualRegionArgTable)
).
:- pred record_formal_region_arguments_proc(module_info::in, pred_proc_id::in,
rpta_info_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_region_set_table::in,
proc_formal_region_args_table::in, proc_formal_region_args_table::out)
is det.
record_formal_region_arguments_proc(ModuleInfo, PPId, RptaInfoTable,
ConstantRTable, DeadRTable, BornRTable, !FormalRegionArgTable) :-
( if map.search(!.FormalRegionArgTable, PPId, _) then
true
else
map.lookup(ConstantRTable, PPId, ConstantR),
map.lookup(DeadRTable, PPId, DeadR),
map.lookup(BornRTable, PPId, BornR),
map.lookup(RptaInfoTable, PPId, RptaInfo),
RptaInfo = rpta_info(Graph, _),
% Formal constant, allocated-into region arguments.
set.to_sorted_list(ConstantR, LConstantR),
% When emulating the rbmm2 system in the paper and in Quan's thesis,
% we omit the test for allocated-into for constant regions.
module_info_get_globals(ModuleInfo, Globals),
globals.lookup_bool_option(Globals, use_alloc_regions,
UseAllocRegions),
(
UseAllocRegions = yes,
list.filter(rptg_is_allocated_node(Graph),
LConstantR, LFormalConstantAllocR)
;
UseAllocRegions = no,
LFormalConstantAllocR = LConstantR
),
% Formal dead region arguments.
set.to_sorted_list(DeadR, FormalDeadR),
% Formal born region arguments.
set.to_sorted_list(BornR, FormalBornR),
RegionArgs =
region_args(LFormalConstantAllocR, FormalDeadR, FormalBornR),
map.det_insert(PPId, RegionArgs, !FormalRegionArgTable)
).
:- pred record_actual_region_arguments_goal(module_info::in,
pred_proc_id::in, rpta_info_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_region_set_table::in, hlds_goal::in,
proc_formal_region_args_table::in, proc_formal_region_args_table::out,
pp_actual_region_args_table::in, pp_actual_region_args_table::out) is det.
record_actual_region_arguments_goal(ModuleInfo, PPId, RptaInfoTable,
ConstantRTable, DeadRTable, BornRTable, Goal,
!FormalRegionArgTable, !ActualRegionArgProc) :-
Goal = hlds_goal(Expr, Info),
record_actual_region_arguments_expr(ModuleInfo, Expr, Info, PPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable,
!FormalRegionArgTable, !ActualRegionArgProc).
:- pred record_actual_region_arguments_expr(module_info::in,
hlds_goal_expr::in, hlds_goal_info::in, pred_proc_id::in,
rpta_info_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_region_set_table::in,
proc_formal_region_args_table::in, proc_formal_region_args_table::out,
pp_actual_region_args_table::in, pp_actual_region_args_table::out) is det.
record_actual_region_arguments_expr(ModuleInfo, GoalExpr, GoalInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable,
!FormalRegionArgTable, !ActualRegionArgProc) :-
(
GoalExpr = plain_call(PredId, ProcId, _, _, _, _),
CalleePPId = proc(PredId, ProcId),
( if some_are_special_preds([CalleePPId], ModuleInfo) then
true
else
CallSite = program_point_init(GoalInfo),
record_actual_region_arguments_call_site(ModuleInfo, CallerPPId,
CallSite, CalleePPId, RptaInfoTable, ConstantRTable,
DeadRTable, BornRTable,
!FormalRegionArgTable, !ActualRegionArgProc)
)
;
GoalExpr = conj(_, Conjuncts),
list.foldl2(
record_actual_region_arguments_goal(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable),
Conjuncts, !FormalRegionArgTable, !ActualRegionArgProc)
;
GoalExpr = disj(Disjuncts),
list.foldl2(
record_actual_region_arguments_goal(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable),
Disjuncts, !FormalRegionArgTable, !ActualRegionArgProc)
;
GoalExpr = if_then_else(_, Cond, Then, Else),
record_actual_region_arguments_goal(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable, Cond,
!FormalRegionArgTable, !ActualRegionArgProc),
record_actual_region_arguments_goal(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable, Then,
!FormalRegionArgTable, !ActualRegionArgProc),
record_actual_region_arguments_goal(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable, Else,
!FormalRegionArgTable, !ActualRegionArgProc)
;
GoalExpr = switch(_, _, Cases),
list.foldl2(
record_actual_region_arguments_case(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable),
Cases, !FormalRegionArgTable, !ActualRegionArgProc)
;
GoalExpr = generic_call(_, _, _, _, _),
sorry($pred, "generic_call NYI")
;
GoalExpr = call_foreign_proc(_, _, _, _, _, _, _),
sorry($pred, "call_foreign_proc NYI")
;
GoalExpr = negation(SubGoal),
record_actual_region_arguments_goal(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable, SubGoal,
!FormalRegionArgTable, !ActualRegionArgProc)
;
GoalExpr = unify(_, _, _, _, _)
;
GoalExpr = scope(_, SubGoal),
% XXX We should special-case the handling of from_ground_term_construct
% scopes.
record_actual_region_arguments_goal(ModuleInfo, CallerPPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable, SubGoal,
!FormalRegionArgTable, !ActualRegionArgProc)
;
GoalExpr = shorthand(_),
unexpected($pred, "shorthand")
).
:- pred record_actual_region_arguments_case(module_info::in,
pred_proc_id::in, rpta_info_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_region_set_table::in, case::in,
proc_formal_region_args_table::in, proc_formal_region_args_table::out,
pp_actual_region_args_table::in, pp_actual_region_args_table::out) is det.
record_actual_region_arguments_case(ModuleInfo, PPId, RptaInfoTable,
ConstantRTable, DeadRTable, BornRTable, Case,
!FormalRegionArgTable, !ActualRegionArgProc) :-
Case = case(_, _, Goal),
record_actual_region_arguments_goal(ModuleInfo, PPId, RptaInfoTable,
ConstantRTable, DeadRTable, BornRTable, Goal,
!FormalRegionArgTable, !ActualRegionArgProc).
% Region variables in deadR and in bornR are passed as arguments.
% Out of the region variables in constantR (constant in the sense that
% their bindings will not change during the call) we only pass ones that
% may be allocated into as arguments. The actual region arguments are
% computed according to these lines.
%
:- pred record_actual_region_arguments_call_site(module_info::in,
pred_proc_id::in, program_point::in, pred_proc_id::in,
rpta_info_table::in, proc_region_set_table::in,
proc_region_set_table::in, proc_region_set_table::in,
proc_formal_region_args_table::in, proc_formal_region_args_table::out,
pp_actual_region_args_table::in, pp_actual_region_args_table::out) is det.
record_actual_region_arguments_call_site(ModuleInfo, CallerPPId, CallSite,
CalleePPId, RptaInfoTable, ConstantRTable, DeadRTable,
BornRTable, !FormalRegionArgTable, !ActualRegionArgProc) :-
( if
map.search(!.FormalRegionArgTable, CalleePPId, FormalRegionArgCallee)
then
% If the formal region arguments of the called procedure have been
% computed, the corresponding actual ones can be straightforwardly
% derived using the call site's alpha mapping.
FormalRegionArgCallee =
region_args(FormalConstants, FormalDeads, FormalBorns),
map.lookup(RptaInfoTable, CallerPPId, CallerRptaInfo),
CallerRptaInfo = rpta_info(_CallerGraph, CallerAlpha),
map.lookup(CallerAlpha, CallSite, AlphaAtCallSite),
list.foldr(find_actual_param(AlphaAtCallSite), FormalConstants,
[], ActualConstants),
list.foldr(find_actual_param(AlphaAtCallSite), FormalDeads,
[], ActualDeads),
list.foldr(find_actual_param(AlphaAtCallSite), FormalBorns,
[], ActualBorns),
map.det_insert(CallSite,
region_args(ActualConstants, ActualDeads, ActualBorns),
!ActualRegionArgProc)
else
% The formal region arguments of the called procedure haven't been
% recorded, so do it now.
record_formal_region_arguments_proc(ModuleInfo, CalleePPId,
RptaInfoTable, ConstantRTable, DeadRTable, BornRTable,
!FormalRegionArgTable),
% We try again at this call site after the formal region arguments
% are recorded.
disable_warning [suspicious_recursion] (
record_actual_region_arguments_call_site(ModuleInfo, CallerPPId,
CallSite, CalleePPId, RptaInfoTable, ConstantRTable,
DeadRTable, BornRTable,
!FormalRegionArgTable, !ActualRegionArgProc)
)
).
:- pred find_actual_param(rpt_call_alpha_mapping::in, rptg_node::in,
list(rptg_node)::in, list(rptg_node)::out) is det.
find_actual_param(Alpha_PP, Formal, Actuals0, Actuals) :-
map.lookup(Alpha_PP, Formal, Actual),
Actuals = [Actual | Actuals0].
%---------------------------------------------------------------------------%
:- end_module transform_hlds.rbmm.region_arguments.
%---------------------------------------------------------------------------%