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

293 lines
12 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2005-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.execution_path.m.
% Main author: Quan Phan.
%
% This module collects all execution paths (ExecPath) of procedures.
%
%---------------------------------------------------------------------------%
:- module transform_hlds.rbmm.execution_path.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_module.
:- import_module transform_hlds.rbmm.region_liveness_info.
% Collects execution paths for each procedure.
%
:- pred execution_path_analysis(module_info::in, execution_path_table::out)
is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module hlds.goal_form.
:- import_module hlds.goal_path.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_pred.
:- import_module parse_tree.
:- import_module parse_tree.prog_data.
:- import_module transform_hlds.smm_common.
:- import_module list.
:- import_module map.
:- import_module pair.
:- import_module require.
%---------------------------------------------------------------------------%
%
% Execution path analysis
%
execution_path_analysis(ModuleInfo, ExecPathTable) :-
module_info_get_valid_pred_ids(ModuleInfo, PredIds),
map.init(ExecPathTable0),
list.foldl(execution_path_analysis_pred(ModuleInfo), PredIds,
ExecPathTable0, ExecPathTable).
:- pred execution_path_analysis_pred(module_info::in, pred_id::in,
execution_path_table::in, execution_path_table::out)
is det.
execution_path_analysis_pred(ModuleInfo, PredId, !ExecPathTable) :-
module_info_pred_info(ModuleInfo, PredId, PredInfo),
ProcIds = pred_info_all_non_imported_procids(PredInfo),
list.foldl(execution_path_analysis_proc(ModuleInfo, PredId), ProcIds,
!ExecPathTable).
:- pred execution_path_analysis_proc(module_info::in, pred_id::in,
proc_id::in, execution_path_table::in, execution_path_table::out) is det.
execution_path_analysis_proc(ModuleInfo, PredId, ProcId, !ExecPathTable) :-
PPId = proc(PredId, ProcId),
( if some_are_special_preds([PPId], ModuleInfo) then
true
else
module_info_proc_info(ModuleInfo, PPId, ProcInfo),
compute_execution_paths(ProcInfo, ModuleInfo, ExecPaths),
map.set(PPId, ExecPaths, !ExecPathTable)
).
% Compute all execution paths in the procedure.
%
:- pred compute_execution_paths(proc_info::in, module_info::in,
list(execution_path)::out) is det.
compute_execution_paths(ProcInfo0, ModuleInfo, ExecPaths) :-
% Fill the goals with program point information.
fill_goal_path_slots_in_proc(ModuleInfo, ProcInfo0, ProcInfo),
proc_info_get_goal(ProcInfo, Goal),
ExecPaths0 = [[]],
execution_paths_covered_goal(ProcInfo, Goal, ExecPaths0, ExecPaths).
% Extend the given execution paths to cover this goal.
%
:- pred execution_paths_covered_goal(proc_info::in, hlds_goal::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_goal(ProcInfo, Goal, !ExecPaths) :-
Goal = hlds_goal(GoalExpr, GoalInfo),
HasSubGoals = goal_expr_has_subgoals(GoalExpr),
(
HasSubGoals = does_not_have_subgoals,
( if
( GoalExpr = unify(_, _, _, _, _)
; GoalExpr = plain_call(_, _, _, _, _, _)
; GoalExpr = conj(_ConjType, [])
; GoalExpr = disj([])
)
then
% Retrieve the program point of this goal.
ProgPoint = program_point_init(GoalInfo),
append_to_each_execution_path(!.ExecPaths,
[[pair(ProgPoint, Goal)]], !:ExecPaths)
else
% XXX: other kinds of atomic calls (generic_call,
% foreign_proc), TEMPORARILY ignored their corresponding pps.
% XXX: handle event_call and unsafe_cast generic_calls
append_to_each_execution_path(!.ExecPaths, [[]], !:ExecPaths)
)
;
HasSubGoals = has_subgoals,
execution_paths_covered_compound_goal(ProcInfo, Goal, !ExecPaths)
).
% Extend current execution paths to cover this compound goal.
%
:- pred execution_paths_covered_compound_goal(proc_info::in, hlds_goal::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_compound_goal(ProcInfo, CompoundGoal, !ExecPaths) :-
CompoundGoal = hlds_goal(Expr, _),
(
Expr = conj(_ConjType, [Conj | Conjs]),
execution_paths_covered_conj(ProcInfo, [Conj | Conjs], !ExecPaths)
;
Expr = switch(_, _, Cases),
execution_paths_covered_cases(ProcInfo, CompoundGoal, Cases,
!ExecPaths)
;
Expr = disj([Disj | Disjs]),
execution_paths_covered_disj(ProcInfo, [Disj | Disjs],
!ExecPaths)
;
Expr = negation(Goal),
execution_paths_covered_goal(ProcInfo, Goal, !ExecPaths)
;
Expr = scope(_, Goal),
% XXX We should special-case the handling of from_ground_term_construct
% scopes.
execution_paths_covered_goal(ProcInfo, Goal, !ExecPaths)
;
Expr = if_then_else(_V, Cond, Then, Else),
execution_paths_covered_goal(ProcInfo, Cond,
!.ExecPaths, ExecPathsCond),
execution_paths_covered_goal(ProcInfo, Then,
ExecPathsCond, ExecPathsCondThen),
execution_paths_covered_goal(ProcInfo, Else,
!.ExecPaths, ExecPathsElse),
!:ExecPaths = ExecPathsCondThen ++ ExecPathsElse
;
( Expr = unify(_, _, _, _, _)
; Expr = plain_call(_, _, _, _, _, _)
; Expr = conj(_, [])
; Expr = disj([])
; Expr = call_foreign_proc(_, _, _, _, _, _, _)
; Expr = generic_call(_, _, _, _, _)
; Expr = shorthand(_)
),
unexpected($pred, "encountered atomic or unsupported goal")
).
% Extend execution paths to cover the goals in this conjunction.
%
:- pred execution_paths_covered_conj(proc_info::in, list(hlds_goal)::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_conj(_, [], !ExecPaths).
execution_paths_covered_conj(ProcInfo, [Conj | Conjs], !ExecPaths) :-
execution_paths_covered_goal(ProcInfo, Conj, !ExecPaths),
execution_paths_covered_conj(ProcInfo, Conjs, !ExecPaths).
% Extend execution paths to cover a disjunction.
% To do this we extend the execution paths from the beginning of the
% disjunction for each disjunct to obtain a set of execution paths
% for each disjuct. At the end of the disjunction we combine these.
%
:- pred execution_paths_covered_disj(proc_info::in, list(hlds_goal)::in,
list(execution_path)::in, list(execution_path)::out) is det.
execution_paths_covered_disj(_, [], _, []).
execution_paths_covered_disj(ProcInfo, [Disj | Disjs], !ExecPaths) :-
execution_paths_covered_goal(ProcInfo, Disj, !.ExecPaths,
ExecPathsDisj),
execution_paths_covered_disj(ProcInfo, Disjs, !.ExecPaths,
ExecPathsDisjs),
!:ExecPaths = ExecPathsDisj ++ ExecPathsDisjs.
% Extend execution paths to cover a switch.
% Switches are handled like disjunctions except that unifications
% involving the switch var sometimes need special handling.
% Unifications between the switch vars and a constant or a functor
% of arity zero are not explicitly present in the goal. This causes
% the execution paths to have fewer program points than they should.
% If this happens we need to add a program point for the removed
% unification. The goal corresponding to this introduced program
% point is the switch goal itself. This is so that we can get
% information about the switch var in the live variable analysis.
%
:- pred execution_paths_covered_cases(proc_info::in, hlds_goal::in,
list(case)::in, list(execution_path)::in, list(execution_path)::out)
is det.
execution_paths_covered_cases(_, _, [], _, []).
execution_paths_covered_cases(ProcInfo, Switch, [Case | Cases], !ExecPaths) :-
Case = case(MainConsId, OtherConsIds, CaseGoal),
expect(unify(OtherConsIds, []), $pred, "NYI: multi-cons-id cases"),
Switch = hlds_goal(_SwitchExpr, Info),
ProgPoint = program_point_init(Info),
% Handle the unification on the switch var if it has been removed.
% We add a dummy program point for this unification.
(
MainConsId = cons(_SymName, Arity, _),
( if Arity = 0 then
append_to_each_execution_path(!.ExecPaths,
[[pair(ProgPoint, Switch)]], ExecPathsBeforeCase)
else
ExecPathsBeforeCase = !.ExecPaths
)
;
( MainConsId = some_int_const(_)
; MainConsId = float_const(_Float)
; MainConsId = char_const(_Char)
; MainConsId = string_const(_String)
),
% need to add a dummy pp
append_to_each_execution_path(!.ExecPaths,
[[pair(ProgPoint, Switch)]], ExecPathsBeforeCase)
;
( MainConsId = tuple_cons(_)
; MainConsId = closure_cons(_, _)
; MainConsId = impl_defined_const(_)
; MainConsId = type_ctor_info_const(_, _, _)
; MainConsId = base_typeclass_info_const(_, _, _, _)
; MainConsId = type_info_cell_constructor(_)
; MainConsId = typeclass_info_cell_constructor
; MainConsId = type_info_const(_)
; MainConsId = typeclass_info_const(_)
; MainConsId = ground_term_const(_, _)
; MainConsId = tabling_info_const(_)
; MainConsId = table_io_entry_desc(_)
; MainConsId = deep_profiling_proc_layout(_)
),
unexpected($pred, "unexpected cons_id")
),
execution_paths_covered_goal(ProcInfo, CaseGoal,
ExecPathsBeforeCase, ExecPathsCase),
execution_paths_covered_cases(ProcInfo, Switch, Cases,
!.ExecPaths, ExecPathsCases),
!:ExecPaths = ExecPathsCase ++ ExecPathsCases.
% Extend each execution path in the first list with each in the
% second list, all the extended execution paths are put in the third list.
%
:- pred append_to_each_execution_path(list(execution_path)::in,
list(execution_path)::in, list(execution_path)::out) is det.
append_to_each_execution_path([], _, []).
append_to_each_execution_path([ExecPath | ExecPaths], Extensions,
ExtendedExecPaths) :-
extend_exectution_path(ExecPath, Extensions, ExtendedExecPaths0),
append_to_each_execution_path(ExecPaths, Extensions,
ExtendedExecPaths1),
ExtendedExecPaths = ExtendedExecPaths0 ++ ExtendedExecPaths1.
% extend_exectution_path(ExecPath, Extensions, ExtendedExecPaths):
%
% ExtendedExecPaths is the list created by appending each extension
% in Extensions to ExecPath.
%
:- pred extend_exectution_path(execution_path::in, list(execution_path)::in,
list(execution_path)::out) is det.
extend_exectution_path(_, [], []).
extend_exectution_path(ExecPath, [Extension | Extensions],
ExtendedExecPaths) :-
ExtendedExecPath = ExecPath ++ Extension,
extend_exectution_path(ExecPath, Extensions, ExtendedExecPaths0),
ExtendedExecPaths = [ExtendedExecPath | ExtendedExecPaths0].
%---------------------------------------------------------------------------%
:- end_module transform_hlds.rbmm.execution_path.
%---------------------------------------------------------------------------%