Files
mercury/compiler/structure_reuse.domain.m
Zoltan Somogyi b56885be93 Fix a bug that caused bootchecks with --optimize-constructor-last-call to fail.
Estimated hours taken: 12
Branches: main

Fix a bug that caused bootchecks with --optimize-constructor-last-call to fail.

The problem was not in lco.m, but in follow_code.m. In some cases,
(specifically, the LCMC version of insert_2 in sparse_bitset.m),
follow_code.m moved an impure goal (store_at_ref) into the arms of an
if-then-else without marking those arms, or the if-then-else, as impure.
The next pass, simplify, then deleted the entire if-then-else, since it
had no outputs. (The store_at_ref that originally appeared after the
if-then-else was the only consumer of its only output.)

The fix is to get follow_code.m to make branched control structures such as
if-then-elses, as well as their arms, semipure or impure if a goal being moved
into them is semipure or impure, or if they came from an semipure or impure
conjunction.

Improve the optimization of the LCMC version of sparse_bitset.insert_2, which
had a foreign_proc invocation of bits_per_int in it: replace such invocations
with a unification of the bits_per_int constant if not cross compiling.

Add a new option, --optimize-constructor-last-call-null. When set, LCMC will
assign NULLs to the fields not yet filled in, to avoid any junk happens to be
there from being followed by the garbage collector's mark phase.

This diff also makes several other changes that helped me to track down
the bug above.

compiler/follow_code.m:
	Make the fix described above.

	Delete all the provisions for --prev-code; it won't be implemented.

	Don't export a predicate that is not now used anywhere else.

compiler/simplify.m:
	Make the optimization described above.

compiler/lco.m:
	Make sure that the LCMC specialized procedure is a predicate, not a
	function: having a function with the mode LCMC_insert_2(in, in) = in
	looks wrong.

	To avoid name collisions when a function and a predicate with the same
	name and arity have LCMC applied to them, include the predicate vs
	function status of the original procedure included in the name of the
	new procedure.

	Update the sym_name of calls to LCMC variants, not just the pred_id,
	because without that, the HLDS dump looks misleading.

compiler/pred_table.m:
	Don't have optimizations like LCMC insert new predicates at the front
	of the list of predicates. Maintain the list of predicates in the
	module as a two part list, to allow efficient addition of new pred_ids
	at the (logical) end without using O(N^2) algorithms. Having predicates
	in chronological order makes it easier to look at HLDS dumps and
	.c files.

compiler/hlds_module.m:
	Make module_info_predids return a module_info that is physically
	updated though logically unchanged.

compiler/options.m:
	Add --optimize-constructor-last-call-null.

	Make the options --dump-hlds-pred-id, --debug-opt-pred-id and
	--debug-opt-pred-name into accumulating options, to allow the user
	to specify more than one predicate to be dumped (e.g. insert_2 and
	its LCMC variant).

	Delete --prev-code.

doc/user_guide.texi:
	Document the changes in options.m.

compiler/code_info.m:
	Record the value of --optimize-constructor-last-call-null in the
	code_info, to avoid lookup at every cell construction.

compiler/unify_gen.m:
compiler/var_locn.m:
	When deciding whether a cell can be static or not, make sure that
	we never make static a cell that has some fields initialized with
	dummy zeros, to be filled in for real later.

compiler/hlds_out.m:
	For goals that are semipure or impure, note this fact. This info was
	lost when I changed the representation of impurity from markers to a
	field.

mdbcomp/prim_data.m:
	Rename some ambiguous function symbols.

compiler/intermod.m:
compiler/trans_opt.m:
	Rename the main predicates (and some function symbols) of these modules
	to avoid ambiguity and to make them more expressive.

compiler/llds.m:
	Don't print line numbers for foreign_code fragments if the user has
	specified --no-line-numbers.

compiler/make.dependencies.m:
compiler/mercury_to_mercury.m:
compiler/recompilation.usage.m:
	Don't use io.write to write out information to files we may need to
	parse again, because this is vulnerable to changes to the names of
	function symbols (e.g. the one to mdbcomp/prim_data.m).

	The compiler still contains some uses of io.write, but they are
	for debugging. I added an item to the todo list of the one exception,
	ilasm.m.

compiler/recompilation.m:
	Rename a misleading function symbol name.

compiler/parse_tree.m:
	Don't import recompilation.m here. It is not needed (all the components
	of parse_tree that need recompilation.m already import it themselves),
	and deleting the import avoids recompiling almost everything when
	recompilation.m changes.

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

compiler/*.m:
browser/*.m:
slice/*.m:
	Conform to the change to mdbcomp.

library/sparse_bitset.m:
	Use some better variable names.
2007-01-19 07:05:06 +00:00

747 lines
27 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2006-2007 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.domain.m.
% Main authors: nancy.
%
% Definition of the abstract domain for keeping track of opportunities for
% structure reuse.
%
%-----------------------------------------------------------------------------%
:- module transform_hlds.ctgc.structure_reuse.domain.
:- interface.
:- import_module hlds.goal_util.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module parse_tree.prog_data.
:- import_module transform_hlds.ctgc.livedata.
:- import_module transform_hlds.ctgc.structure_sharing.domain.
:- import_module bool.
:- import_module io.
:- import_module map.
:- import_module set.
:- import_module list.
%-----------------------------------------------------------------------------%
% A reuse condition stores all the necessary information to check if
% a procedure call is safe w.r.t. a structure reuse opportunity within
% the body of the called procedure.
%
:- type reuse_condition.
:- type reuse_conditions == list(reuse_condition).
% Abstract representation for a set of reuse conditions.
%
:- type reuse_as.
%-----------------------------------------------------------------------------%
%
% reuse_condition
%
% reuse_condition_init(ModuleInfo, ProcInfo, DeadVar, LocalForwardUse,
% LocalBackwardUse, SharingAs) = NewReuseCondition.
%
% Create a reuse condition for DeadVar, knowing the set of variables in
% local forward and backward use, as well as the local structure sharing.
%
:- func reuse_condition_init(module_info, proc_info, dead_var,
set(live_var), set(live_var), sharing_as) = reuse_condition.
:- pred reuse_condition_is_conditional(reuse_condition::in) is semidet.
% Renaming operation.
% This operation renames all occurrences of program variables and
% type variables according to a program and type variable mapping.
%
:- pred reuse_condition_rename(prog_var_renaming::in, tsubst::in,
reuse_condition::in, reuse_condition::out) is det.
% Succeeds if the first condition is subsumed by the second one, i.e.,
% if a procedure call verifies the second condition, then it also
% verifies the first condition.
%
:- pred reuse_condition_subsumed_by(module_info::in, proc_info::in,
reuse_condition::in, reuse_condition::in) is semidet.
%-----------------------------------------------------------------------------%
% reuse_as
%
% XXX The implementation of this type has changed wrt. its counterpart in the
% reuse branch (called memo_reuse). While memo_reuse's didn't always keep a
% minimal representation, reuse_as does.
%
% Create an initial set of reuse descriptions.
%
:- func reuse_as_init = reuse_as.
:- func reuse_as_init_with_one_condition(reuse_condition) = reuse_as.
% Return a short description of the reuse information.
%
:- func reuse_as_short_description(reuse_as) = string.
% Succeeds if the first reuses description is subsumed by the second
% description, i.e., if a procedure call satisfies all the conditions
% expressed by the second reuses description, then it also satisfies all
% the conditions expressed by the first reuses description.
%
:- pred reuse_as_subsumed_by(module_info::in, proc_info::in, reuse_as::in,
reuse_as::in) is semidet.
% Tests to see whether the reuses description describes no reuses at all,
% only unconditional reuses, or conditional reuses resp.
%
:- pred reuse_as_no_reuses(reuse_as::in) is semidet.
:- pred reuse_as_all_unconditional_reuses(reuse_as::in) is semidet.
:- pred reuse_as_conditional_reuses(reuse_as::in) is semidet.
% reuse_as_rename_using_module_info(ModuleInfo, PPId,
% ActualVars, ActualTypes, ActualTVarset, FormalReuse, ActualReuse):
%
% Renaming of the formal description of structure reuse conditions to the
% actual description of these conditions. The information about the formal
% variables needs to be extracted from the module information.
% The actual names are determined by the actual variables names, the
% actual types, and the type-variables occurring in those types.
%
:- pred reuse_as_rename_using_module_info(module_info::in,
pred_proc_id::in, prog_vars::in, list(mer_type)::in, tvarset::in,
reuse_as::in, reuse_as::out) is det.
% Given a variable and type variable mapping, rename the reuses
% conditions accordingly.
%
:- pred reuse_as_rename(prog_var_renaming::in, tsubst::in, reuse_as::in,
reuse_as::out) is det.
% Add a reuse condition to the reuses description. The information of
% module_info and proc_info are needed to verify subsumption before adding
% the new condition.
%
:- pred reuse_as_add_condition(module_info::in, proc_info::in,
reuse_condition::in, reuse_as::in, reuse_as::out) is det.
% A shortcut version of the above procedure when the additional condition
% is "unconditional".
%
:- pred reuse_as_add_unconditional(reuse_as::in, reuse_as::out) is det.
% Compute the least upper bound of two reuses descriptions. Module_info
% and proc_info are needed for verifying subsumption.
%
:- pred reuse_as_least_upper_bound(module_info::in, proc_info::in,
reuse_as::in, reuse_as::in, reuse_as::out) is det.
:- func reuse_as_least_upper_bound(module_info, proc_info, reuse_as,
reuse_as) = reuse_as.
% reuse_as_from_called_procedure_to_local_reuse_as(ModuleInfo,
% ProcInfo, HeadVars, InUseData, SharingAs, CalledReuseAs) =
% LocalReuseAs.
%
% Translate the reuse description of a called procedure to the
% environment of the caller. This means taking into account the local
% sets of in use variables, as well as the local sharing.
%
% Pre-condition: the reuse description of the called procedure is already
% correctly renamed to the caller's environment.
% Pre-condition: the reuse_as from the called procedure contains at
% least one conditional reuse condition.
%
:- func reuse_as_from_called_procedure_to_local_reuse_as(module_info,
proc_info, prog_vars, live_datastructs, sharing_as, reuse_as) = reuse_as.
% Succeeds if taking into account the live data and static variables the
% reuse conditions expressed by reuse_as are all satisfied, hence making
% the associated memory reuses safe for that particular calling
% environment.
%
:- pred reuse_as_satisfied(module_info::in, proc_info::in, livedata::in,
sharing_as::in, prog_vars::in, reuse_as::in) is semidet.
% Conversion procedures between the public (structure_reuse_domain)
% and private (reuse_as) representation for structure reuse conditions.
%
:- func from_structure_reuse_domain(structure_reuse_domain) = reuse_as.
:- func to_structure_reuse_domain(reuse_as) = structure_reuse_domain.
%-----------------------------------------------------------------------------%
%
% reuse_as_table
%
% Intermediate storage of the reuse results for individual procedures.
%
:- type reuse_as_table == map(pred_proc_id, reuse_as).
:- func reuse_as_table_init = reuse_as_table.
:- func reuse_as_table_search(pred_proc_id, reuse_as_table)
= reuse_as is semidet.
:- pred reuse_as_table_set(pred_proc_id::in, reuse_as::in,
reuse_as_table::in, reuse_as_table::out) is det.
:- pred reuse_as_table_maybe_dump(bool::in, reuse_as_table::in,
io::di, io::uo) is det.
% Load all the structure reuse information present in the HLDS into
% a reuse table.
%
:- func load_structure_reuse_table(module_info) = reuse_as_table.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module libs.compiler_util.
:- import_module parse_tree.prog_ctgc.
:- import_module transform_hlds.ctgc.datastruct.
:- import_module transform_hlds.ctgc.util.
:- import_module maybe.
:- import_module pair.
:- import_module set.
:- import_module string.
%-----------------------------------------------------------------------------%
:- type reuse_condition
---> always % The reuse is always safe and does not actually
% have a condition.
; condition(
reuseable_nodes :: dead_datastructs,
% Description of the datastructures pointing to the
% memory that can be reused within a procedure.
local_use_headvars :: live_datastructs,
% Set of (headvar-related) datastructures that are
% inherently live at the place where the reuse is
% decided.
local_sharing_headvars :: sharing_as
% Description of the (headvar-related) structure sharing
% that exists at the place where the reuse is decided.
).
:- type reuse_as
---> no_reuse
% = fictive bottom element representing the fact that no
% reuse has been detected so far.
; unconditional
% no_reuse < unconditional.
% = element representing the fact that all reuses detected
% so far are unconditional.
% Semantically equivalent to "conditional(Cs)" where every C in
% Cs is "always".
; conditional(reuse_conditions).
% no_reuse < unconditional < conditional(List)
% = element representing the collection of reuse conditions
% collected for the reuses detected so far.
%-----------------------------------------------------------------------------%
%
% reuse_condition
%
reuse_condition_init(ModuleInfo, ProcInfo, DeadVar, LFU, LBU,
Sharing) = Condition :-
proc_info_get_headvars(ProcInfo, HeadVars),
% First determine the nodes to which the reuse is related.
% There are two cases:
% 1. Var is a headvar, then it is sufficient to keep the top cell of that
% Var as only node. HeadVar-datastructures shared with that node will
% still be retraceable at the moment of verifying the condition
% 2. Var is a local var, then we must compute all the headvar-
% datastructures sharing the same memory representation as the top cell
% of this var (note that the datastructures that share with some
% substructure of this var are not relevant for the nodes). All the
% obtained datastructures are kept as the nodes for our condition.
TopCell = ctgc.datastruct.datastruct_init(DeadVar),
(
list.member(DeadVar, HeadVars)
->
Nodes = [TopCell]
;
SharedDatastructs = extend_datastruct(ModuleInfo, ProcInfo,
Sharing, TopCell),
Nodes = datastructs_project(HeadVars, SharedDatastructs)
),
% It is possible that the obtained set of nodes is empty, in that
% case the condition is always satisfied, independent of any calling
% environment.
(
Nodes = []
->
Condition = always
;
set.union(LFU, LBU, LU),
% XXX the old implementation did not bother about extending at
% this place, which was contrary to the theory. Check the effect
% of this change!
SharedLU = list.condense(
list.map(extend_datastruct(ModuleInfo, ProcInfo, Sharing),
list.map(datastruct_init, set.to_sorted_list(LU)))),
HeadVarSharedLU = datastructs_project(HeadVars, SharedLU),
structure_sharing.domain.sharing_as_project(HeadVars, Sharing,
HeadVarSharing),
Condition = condition(Nodes, HeadVarSharedLU, HeadVarSharing)
).
reuse_condition_is_conditional(condition(_, _, _)).
reuse_condition_subsumed_by(ModuleInfo, ProcInfo, Cond1, Cond2) :-
(
Cond1 = always
;
Cond1 = condition(Nodes1, LocalUse1, LocalSharing1),
Cond2 = condition(Nodes2, LocalUse2, LocalSharing2),
datastructs_subsumed_by_list(ModuleInfo, ProcInfo, Nodes1, Nodes2),
datastructs_subsumed_by_list(ModuleInfo, ProcInfo, LocalUse1,
LocalUse2),
sharing_as_is_subsumed_by(ModuleInfo,
ProcInfo, LocalSharing1, LocalSharing2)
).
:- pred reuse_condition_subsumed_by_list(module_info::in, proc_info::in,
reuse_condition::in, reuse_conditions::in) is semidet.
reuse_condition_subsumed_by_list(ModuleInfo, ProcInfo, Cond, [Cond1|Rest]) :-
(
reuse_condition_subsumed_by(ModuleInfo, ProcInfo, Cond, Cond1)
;
reuse_condition_subsumed_by_list(ModuleInfo, ProcInfo, Cond, Rest)
).
:- pred reuse_conditions_subsume_reuse_condition(module_info::in,
proc_info::in, reuse_conditions::in, reuse_condition::in) is semidet.
reuse_conditions_subsume_reuse_condition(ModuleInfo, ProcInfo, Conds, Cond):-
reuse_condition_subsumed_by_list(ModuleInfo, ProcInfo, Cond, Conds).
reuse_condition_rename(MapVar, TypeSubst, Condition, RenamedCondition):-
(
Condition = always,
RenamedCondition = always
;
Condition = condition(DeadNodes, InUseNodes, LocalSharing),
RenamedDeadNodes = list.map(rename_datastruct(MapVar, TypeSubst),
DeadNodes),
RenamedInUseNodes = list.map(rename_datastruct(MapVar, TypeSubst),
InUseNodes),
sharing_as_rename(MapVar, TypeSubst, LocalSharing,
RenamedLocalSharing),
RenamedCondition = condition(RenamedDeadNodes, RenamedInUseNodes,
RenamedLocalSharing)
).
%-----------------------------------------------------------------------------%
%
% reuse_as
%
reuse_as_init = no_reuse.
reuse_as_init_with_one_condition(ReuseCondition) = ReuseAs :-
(
reuse_condition_is_conditional(ReuseCondition)
->
ReuseAs = conditional([ReuseCondition])
;
ReuseAs = unconditional
).
reuse_as_short_description(no_reuse) = "n".
reuse_as_short_description(unconditional) = "u".
reuse_as_short_description(conditional(Conds)) = "c(" ++ Size ++ ")" :-
Size = string.int_to_string(list.length(Conds)).
reuse_as_subsumed_by(ModuleInfo, ProcInfo, FirstReuseAs, SecondReuseAs) :-
(
FirstReuseAs = no_reuse
;
FirstReuseAs = unconditional,
SecondReuseAs = conditional(_)
% Every calling environment satisfies the reuse conditions as all
% reuse is unconditional, hence also the calling environments that
% satisfy the conditions expressed by SecondReuseAs.
;
FirstReuseAs = unconditional,
SecondReuseAs = unconditional
;
FirstReuseAs = conditional(ReuseConditionsFirst),
SecondReuseAs = conditional(ReuseConditionsSecond),
list.takewhile(reuse_conditions_subsume_reuse_condition(ModuleInfo,
ProcInfo, ReuseConditionsSecond), ReuseConditionsFirst, _,
NotSubsumed),
NotSubsumed = []
).
reuse_as_no_reuses(no_reuse).
reuse_as_all_unconditional_reuses(unconditional).
reuse_as_conditional_reuses(conditional(_)).
reuse_as_rename_using_module_info(ModuleInfo, PPId, ActualArgs, ActualTypes,
ActualTVarset, FormalReuse, ActualReuse) :-
reuse_as_rename(
get_variable_renaming(ModuleInfo, PPId, ActualArgs),
get_type_substitution(ModuleInfo, PPId, ActualTypes, ActualTVarset),
FormalReuse, ActualReuse).
reuse_as_rename(MapVar, TypeSubst, ReuseAs, RenamedReuseAs) :-
(
ReuseAs = no_reuse,
RenamedReuseAs = no_reuse
;
ReuseAs = unconditional,
RenamedReuseAs = unconditional
;
ReuseAs = conditional(ReuseConditions),
list.map(reuse_condition_rename(MapVar, TypeSubst),
ReuseConditions, RenamedReuseConditions),
RenamedReuseAs = conditional(RenamedReuseConditions)
).
reuse_as_add_condition(ModuleInfo, ProcInfo, Condition, !ReuseAs) :-
(
Condition = always,
(
!.ReuseAs = no_reuse
->
!:ReuseAs = unconditional
;
true
)
;
Condition = condition(_, _, _),
(
!.ReuseAs = no_reuse,
!:ReuseAs = conditional([Condition])
;
!.ReuseAs = unconditional,
!:ReuseAs = conditional([Condition])
;
!.ReuseAs = conditional(Conditions),
reuse_conditions_add_condition(ModuleInfo, ProcInfo,
Condition, Conditions, NewConditions),
!:ReuseAs = conditional(NewConditions)
)
).
reuse_as_add_unconditional(!ReuseAs) :-
(
!.ReuseAs = no_reuse,
!:ReuseAs = unconditional
;
!.ReuseAs = unconditional
;
!.ReuseAs = conditional(_)
).
:- pred reuse_conditions_add_condition(module_info::in, proc_info::in,
reuse_condition::in, reuse_conditions::in, reuse_conditions::out) is det.
reuse_conditions_add_condition(ModuleInfo, ProcInfo, Condition, !Conds):-
(
reuse_condition_subsumed_by_list(ModuleInfo, ProcInfo,
Condition, !.Conds)
->
true
;
!:Conds = [Condition | !.Conds]
).
:- pred reuse_conditions_add_conditions(module_info::in, proc_info::in,
reuse_conditions::in, reuse_conditions::in, reuse_conditions::out) is det.
reuse_conditions_add_conditions(ModuleInfo, ProcInfo, NewConds, !Conds):-
(
NewConds = [Cond | RemainingConds],
reuse_conditions_add_condition(ModuleInfo, ProcInfo, Cond, !Conds),
reuse_conditions_add_conditions(ModuleInfo, ProcInfo,
RemainingConds, !Conds)
;
NewConds = []
).
reuse_as_least_upper_bound(ModuleInfo, ProcInfo, NewReuseAs, !ReuseAs) :-
(
NewReuseAs = no_reuse
;
NewReuseAs = unconditional,
(
!.ReuseAs = no_reuse
->
!:ReuseAs = unconditional
;
true
)
;
NewReuseAs = conditional(NewConditions),
(
!.ReuseAs = no_reuse,
!:ReuseAs = NewReuseAs
;
!.ReuseAs = unconditional,
!:ReuseAs = NewReuseAs
;
!.ReuseAs = conditional(Conditions),
reuse_conditions_add_conditions(ModuleInfo, ProcInfo,
NewConditions, Conditions, AllConditions),
!:ReuseAs = conditional(AllConditions)
)
).
reuse_as_least_upper_bound(ModuleInfo, ProcInfo, Reuse1, Reuse2) = Reuse :-
reuse_as_least_upper_bound(ModuleInfo, ProcInfo, Reuse1, Reuse2, Reuse).
reuse_as_from_called_procedure_to_local_reuse_as(ModuleInfo, ProcInfo,
HeadVars, LuData, SharingAs, CalledReuseAs) = LocalReuseAs :-
(
CalledReuseAs = no_reuse,
unexpected(this_file,
"reuse_as_from_called_procedure_to_local_reuse_as: " ++
"reuse_as does not specify any reuses.")
;
CalledReuseAs = unconditional,
unexpected(this_file,
"reuse_as_from_called_procedure_to_local_reuse_as: " ++
"reuse_as is unconditional.")
;
CalledReuseAs = conditional(ConditionsCaller),
ConditionsCallee =
list.map(reuse_condition_from_called_proc_to_local_condition(
ModuleInfo, ProcInfo, HeadVars, LuData, SharingAs),
ConditionsCaller),
list.foldl(reuse_as_add_condition(ModuleInfo, ProcInfo),
ConditionsCallee, reuse_as_init, LocalReuseAs)
).
:- func reuse_condition_from_called_proc_to_local_condition(module_info,
proc_info, prog_vars, live_datastructs, sharing_as, reuse_condition) =
reuse_condition.
reuse_condition_from_called_proc_to_local_condition(ModuleInfo, ProcInfo,
HeadVars, LuData, SharingAs, CalledCondition) = LocalCondition :-
(
CalledCondition = always,
unexpected(this_file,
"reuse_condition_from_called_proc_to_local_condition: " ++
"explicit condition expected.")
;
CalledCondition = condition(CalledDeadNodes,
CalledInUseNodes, CalledSharingAs),
% Translate the dead nodes:
AllDeadNodes = extend_datastructs(ModuleInfo, ProcInfo,
SharingAs, CalledDeadNodes),
AllDeadHeadVarNodes = datastructs_project(HeadVars, AllDeadNodes),
(
AllDeadHeadVarNodes = [],
LocalCondition = always
;
AllDeadHeadVarNodes = [_|_],
% Translate the in use nodes:
AllInUseNodes = extend_datastructs(ModuleInfo, ProcInfo,
SharingAs, list.append(LuData, CalledInUseNodes)),
AllInUseHeadVarNodes = datastructs_project(HeadVars,
AllInUseNodes),
% Translate the sharing information:
AllLocalSharing = sharing_as_comb(ModuleInfo, ProcInfo,
CalledSharingAs, SharingAs),
AllHeadVarLocalSharing = sharing_as_project(HeadVars,
AllLocalSharing),
LocalCondition = condition(AllDeadHeadVarNodes,
AllInUseHeadVarNodes, AllHeadVarLocalSharing)
)
).
reuse_as_satisfied(ModuleInfo, ProcInfo, LiveData, SharingAs, StaticVars,
ReuseAs) :-
(
ReuseAs = no_reuse,
fail
;
ReuseAs = unconditional,
true
;
ReuseAs = conditional(Conditions),
list.all_true(reuse_condition_satisfied(ModuleInfo, ProcInfo,
LiveData, SharingAs, StaticVars), Conditions)
% XXX something about reuse conditions pointing to the
% same datastructure to be reused...
).
:- pred reuse_condition_satisfied(module_info::in, proc_info::in,
livedata::in, sharing_as::in, prog_vars::in, reuse_condition::in)
is semidet.
reuse_condition_satisfied(ModuleInfo, ProcInfo, LiveData, SharingAs,
StaticVars, Condition) :-
(
Condition = always
;
Condition = condition(DeadNodes, InUseNodes, SharingNodes),
% Reuse of static vars is not allowed:
StaticDeadNodes = datastructs_project(StaticVars, DeadNodes),
StaticDeadNodes = [],
% Using the InUseNodes, and the sharing recorded by the condition,
% compute a new set of livedata that (safely) approximates the
% set of livedata that would have been obtained when looking at
% the program point from where the reuse condition actually comes from.
NewSharing = sharing_as_comb(ModuleInfo, ProcInfo, SharingNodes,
SharingAs),
UpdatedLiveData = livedata_add_liveness(ModuleInfo, ProcInfo,
InUseNodes, NewSharing, LiveData),
nodes_are_not_live(ModuleInfo, ProcInfo, DeadNodes,
UpdatedLiveData)
).
%-----------------------------------------------------------------------------%
from_structure_reuse_domain(ReuseDomain) = ReuseAs :-
(
ReuseDomain = has_no_reuse,
ReuseAs = no_reuse
;
ReuseDomain = has_only_unconditional_reuse,
ReuseAs = unconditional
;
ReuseDomain = has_conditional_reuse(PublicReuseConditions),
ReuseAs = conditional(
from_public_reuse_conditions(PublicReuseConditions))
).
:- func from_public_reuse_conditions(structure_reuse_conditions) =
reuse_conditions.
from_public_reuse_conditions(PublicReuseConditions) =
list.map(from_public_reuse_condition, PublicReuseConditions).
:- func from_public_reuse_condition(structure_reuse_condition) =
reuse_condition.
from_public_reuse_condition(PublicReuseCondition) = ReuseCondition :-
PublicReuseCondition = structure_reuse_condition(DeadNodes, LiveNodes,
PublicSharing),
ReuseCondition = condition(DeadNodes, LiveNodes,
from_structure_sharing_domain(PublicSharing)).
to_structure_reuse_domain(ReuseAs) = ReuseDomain :-
(
ReuseAs = no_reuse,
ReuseDomain = has_no_reuse
;
ReuseAs = unconditional,
ReuseDomain = has_only_unconditional_reuse
;
ReuseAs = conditional(ReuseConditions),
ReuseDomain = has_conditional_reuse(
to_structure_reuse_conditions(ReuseConditions))
).
:- func to_structure_reuse_conditions(reuse_conditions) =
structure_reuse_conditions.
to_structure_reuse_conditions(ReuseConditions) =
list.filter_map(to_structure_reuse_condition, ReuseConditions).
:- func to_structure_reuse_condition(reuse_condition) =
structure_reuse_condition is semidet.
to_structure_reuse_condition(Condition) = StructureReuseCondition :-
Condition = condition(DeadNodes, LiveNodes, SharingAs),
StructureReuseCondition = structure_reuse_condition(DeadNodes, LiveNodes,
to_structure_sharing_domain(SharingAs)).
%-----------------------------------------------------------------------------%
%
% reuse_as_table
%
reuse_as_table_init = map.init.
reuse_as_table_search(PPId, Table) = Table ^ elem(PPId).
reuse_as_table_set(PPId, ReuseAs, !Table) :-
!:Table = !.Table ^ elem(PPId) := ReuseAs.
reuse_as_table_maybe_dump(DoDump, Table, !IO) :-
(
DoDump = no
;
DoDump = yes,
reuse_as_table_dump(Table, !IO)
).
:- pred reuse_as_table_dump(reuse_as_table::in, io::di, io::uo) is det.
reuse_as_table_dump(Table, !IO) :-
(
map.is_empty(Table)
->
io.write_string("% ReuseTable: Empty", !IO)
;
io.write_string("% ReuseTable: PPId --> Reuse\n", !IO),
io.write_list(map.to_assoc_list(Table), "", dump_entries, !IO)
).
:- pred dump_entries(pair(pred_proc_id, reuse_as)::in, io::di, io::uo) is det.
dump_entries(PPId - ReuseAs, !IO) :-
PPId = proc(PredId, ProcId),
io.write_string(
"% " ++ string.int_to_string(pred_id_to_int(PredId)) ++ ", " ++
string.int_to_string(proc_id_to_int(ProcId)) ++ "\t-->" ++
reuse_as_short_description(ReuseAs) ++ "\n", !IO).
load_structure_reuse_table(ModuleInfo) = ReuseTable :-
module_info_predids(PredIds, ModuleInfo, _ModuleInfo),
list.foldl(load_structure_reuse_table_2(ModuleInfo), PredIds,
reuse_as_table_init, ReuseTable).
:- pred load_structure_reuse_table_2(module_info::in, pred_id::in,
reuse_as_table::in, reuse_as_table::out) is det.
load_structure_reuse_table_2(ModuleInfo, PredId, !ReuseTable) :-
module_info_pred_info(ModuleInfo, PredId, PredInfo),
ProcIds = pred_info_procids(PredInfo),
list.foldl(load_structure_reuse_table_3(ModuleInfo, PredId),
ProcIds, !ReuseTable).
:- pred load_structure_reuse_table_3(module_info::in, pred_id::in,
proc_id::in, reuse_as_table::in, reuse_as_table::out) is det.
load_structure_reuse_table_3(ModuleInfo, PredId, ProcId, !ReuseTable) :-
module_info_proc_info(ModuleInfo, PredId, ProcId, ProcInfo),
proc_info_get_structure_reuse(ProcInfo, MaybePublicReuse),
(
MaybePublicReuse = yes(PublicReuse),
PPId = proc(PredId, ProcId),
PrivateReuse = from_structure_reuse_domain(PublicReuse),
reuse_as_table_set(PPId, PrivateReuse, !ReuseTable)
;
MaybePublicReuse = no
).
%-----------------------------------------------------------------------------%
:- func this_file = string.
this_file = "structure_reuse.domain.m".
%-----------------------------------------------------------------------------%
:- end_module transform_hlds.ctgc.structure_reuse.domain.
%-----------------------------------------------------------------------------%