mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-20 11:54:02 +00:00
Right now, most parts of the compiler write to the "current output stream".
This was a pragmatic choice at the time, but has not aged well. The problem
is that the answer to the question "where is the current output stream going?"
is not obvious in *all* places in the compiler (although it is obvious in
most). When using such implicit streams, finding where the output is going
to in a given predicate requires inspecting not just the ancestors of that
predicate, but also all their older siblings (since any of them could have
changed the current stream), *including* their entire call trees. This is
usually an infeasible task. By constrast, if we explicitly pass streams
to all output operations, we need only follow the places where the variable
representing that stream is bound, which the mode system makes easy.
This diff switches large parts of the compiler over to doing output only
to explicitly passed streams, never to the implicit "current output stream".
The parts it switches over are the parts that rely to a significant degree
on the innermost change, which is to the "output" typeclass in
parse_tree_out_info.m. This is the part that has to be switched over to
explicit streams first, because (a) many modules such as mercury_to_mercury.m
rely on the output typeclass, and (b) most other modules that do output
call predicates in these modules. Starting anywhere else would be like
building a skyscraper starting at the top.
This typeclass, output(U), has two instances: output(io), and output(string),
so you could output either to the current output stream, or to a string.
To allow the specification of the destination stream in the first case,
this diff changes the typeclass to output(S, U) with a functional dependency
from U to S, with the two instances being output(io.text_output_stream, io)
and output(unit, string). (The unit arg is ignored in the second case.)
There is a complication with the output typeclass method, add_list, that
outputs a list of items. The complication is that each item is output
by a predicate supplied by the caller, but the separator between the items
(usually a comma) is output by add_list itself. We don't want to give
callers of this method the opportunity to screw up by specifying (possibly
implicitly) two different output streams for these two purposes, so we want
(a) the caller to tell add_list where to put the separators, and then
(b) for add_list, not its caller, tell the user-supplied predicate what
stream to write to. This works only if the stream argument is just before
the di,uo pair of I/O state arguments, which differs from our usual practice
of passing the stream at or near the left edge of the argument list,
not near the right. The result of this complication is that two categories
of predicates that are and are not used to print items in a list differ
in where they put the stream in their argument lists. This makes it easy
to pass the stream in the wrong argument position if you call a predicate
without looking up its signature, and may require *changing* the argument
order when a predicate is used to print an item in a list for the first time.
A complete switch over to always passing the stream just before !IO
would fix this inconsistency, but is far to big a change to make all at once.
compiler/parse_tree_out_info.m:
Make the changes described above.
Add write_out_list, which is a variant of io.write_list specifically
designed to address the "complication" described above. It also has
the arguments in an order that is better suited for higher-order use.
Make the same change to argument order in the class method add_list
as well.
Almost all of the following changes consist of passing an extra stream
argument to output predicates. In some places, where I thought this would
aid readability, I replaced sequences of calls to output predicates
with a single io.format.
compiler/prog_out.m:
This module had many predicates that wrote things to the current output
stream. This diff adds versions of these predicates that take an
explicit stream argument.
If the originals are still needed after the changes to the other modules,
keep them, but add "_to_cur_stream" to the end of their names.
Otherwise, delete them. (Many of the changes below replace
write_xyz(..., !IO) with io.write_string(Stream, xyz_to_string(...), !IO),
especially when write_xyz did nothing except call xyz_to_string
and wrote out the result.)
compiler/c_util.m:
Add either an explicit stream argument to the argument list, or a
"_current_stream" suffix to the name, of every predicate defined
in this module that does output.
Add a new predicate to print out the block comment containing
input for mkinit. This factors out common code in the LLDS and MLDS
backends.
compiler/name_mangle.m:
Delete all predicates that used to write to the current output stream,
after replacing them if necessary with functions that return a string,
which the caller can print to wherever it wants. (The "if necessary"
part is there because some of the "replacement" functions already
existed.)
When converting a proc_label to a string, *always* require the caller
to say whether the label prefix should be added to the string,
instead of silently assuming "yes, add it", as calls to one of the old,
now deleted predicates had it.
compiler/file_util.m:
Add output_to_file_stream, a version of output_to_file which
simply passes the output file stream it opens to the predicate
that is intended to define the contents of the newly created or
updated file. The existing output_to_file, which instead sets
and resets the current output stream around the equivalent
predicate call, is still needed e.g. by the MLDS backend,
but hopefully for not too long.
compiler/mercury_to_mercury.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_clause.m:
compiler/parse_tree_out_inst.m:
compiler/parse_tree_out_pragma.m:
compiler/parse_tree_out_pred_decl.m:
compiler/parse_tree_out_term.m:
compiler/parse_tree_out_type_repn.m:
Change the code writing out parse trees to explicitly pass a stream
to every predicate that does output.
In some places, this allows us to avoid changing the identity
of the current output stream.
compiler/hlds_out.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
Change the code writing out HLDS code to explicitly pass a stream
to every predicate that does output. (The changes to these modules
belong in this diff because these modules call many of the output
predicates in the parse tree package.)
In hlds_out_util.m, delete some write_to_xyz(...) predicates that wrote
the result of xyz_to_string(...) to the current output stream.
Replace calls to the deleted predicates with calls to io.write_string
with the string being written being computed by xyz_to_string.
Add a predicate to hlds_out_util.m that outputs a comment containing
the current context, if it is valid. This factors out code that used
to be common to several of the other modules.
In a few places in hlds_out_module.m, the new code generates a
slighly different set of blank lines, but this should not be a problem.
compiler/layout_out.m:
compiler/llds_out_code_addr.m:
compiler/llds_out_data.m:
compiler/llds_out_file.m:
compiler/llds_out_global.m:
compiler/llds_out_instr.m:
compiler/llds_out_util.m:
compiler/opt_debug.m:
compiler/rtti_out.m:
Change the code writing out the LLDS to explicitly pass a stream
to every predicate that does output. (The changes to these modules
belong in this diff because layout_out.m and rtti_out.m call
many of the output predicates in the parse tree package,
and through them, the rest of the LLDS backend is affected as well.)
compiler/make.module_dep_file.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_middle_passes.m:
Replace code that sets and resets the current output stream
with code that simply passes an explicit output stream to a
predicate that now *takes* an explicit stream as an argument.
compiler/accumulator.m:
compiler/add_clause.m:
compiler/code_gen.m:
compiler/code_loc_dep.m:
compiler/cse_detection.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/error_msg_inst.m:
compiler/export.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/ite_gen.m:
compiler/lco.m:
compiler/liveness.m:
compiler/lp_rational.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_c_global.m:
compiler/mode_debug.m:
compiler/mode_errors.m:
compiler/modes.m:
compiler/optimize.m:
compiler/passes_aux.m:
compiler/pd_debug.m:
compiler/pragma_c_gen.m:
compiler/proc_gen.m:
compiler/prog_ctgc.m:
compiler/push_goals_together.m:
compiler/rat.m:
compiler/recompilation.m:
compiler/recompilation.usage.m:
compiler/recompilation.version.m:
compiler/rtti.m:
compiler/saved_vars.m:
compiler/simplify_goal_conj.m:
compiler/stack_opt.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.domain.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/superhomogeneous.m:
compiler/term_constr_build.m:
compiler/term_constr_data.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_pass2.m:
compiler/term_constr_util.m:
compiler/tupling.m:
compiler/type_assign.m:
compiler/unneeded_code.m:
compiler/write_deps_file.m:
Conform to the changes above, mostly by passing streams explicitly.
compiler/hlds_dependency_graph.m:
Conform to the changes above, mostly by passing streams explicitly.
Move a predicate's definition next it only use.
compiler/Mercury.options:
Specify --warn-implicit-stream-calls for all the modules in which
this diff has replaced all implicit streams with explicit streams.
(Unfortunately, debugging this diff has shown that --warn-implicit-
stream-calls detects only *some*, and not *all*, uses of implicit
streams.)
library/term_io.m:
Fix documentation.
487 lines
18 KiB
Mathematica
487 lines
18 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2002, 2005-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: term_constr_fixpoint.m.
|
|
% Main author: juliensf.
|
|
%
|
|
% TODO:
|
|
% * code for handling calls could do with a cleanup.
|
|
%
|
|
% NOTE: the code in this module should not refer to things in the HLDS
|
|
% (with the exception of the termination2_info slots in the
|
|
% proc_sub_info structure)
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module transform_hlds.term_constr_fixpoint.
|
|
:- interface.
|
|
|
|
:- import_module hlds.
|
|
:- import_module hlds.hlds_module.
|
|
:- import_module hlds.hlds_pred.
|
|
:- import_module transform_hlds.term_constr_data.
|
|
:- import_module transform_hlds.term_constr_errors.
|
|
|
|
:- import_module list.
|
|
:- import_module set.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Derive the argument size constraints for the procedures in this SCC
|
|
% that need them.
|
|
%
|
|
:- pred do_fixpoint_calculation(fixpoint_options::in, set(pred_proc_id)::in,
|
|
int::in, list(term2_error)::out, module_info::in, module_info::out) is det.
|
|
|
|
% This structure holds the values of options used to control
|
|
% the fixpoint calculation.
|
|
%
|
|
:- type fixpoint_options.
|
|
|
|
% fixpoint_options_init(Widening, MaxMatrixSize):
|
|
%
|
|
% Initialise the fixpoint_options structure. `Widening' is the threshold
|
|
% after which we invoke widening. `MaxMatrixSize' specifies the maximum
|
|
% number of constraints we allow a matrix to grow to before we abort
|
|
% and try other approximations.
|
|
%
|
|
:- func fixpoint_options_init(widening, int) = fixpoint_options.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module hlds.hlds_out.
|
|
:- import_module hlds.hlds_out.hlds_out_util.
|
|
:- import_module libs.
|
|
:- import_module libs.globals.
|
|
:- import_module libs.lp_rational.
|
|
:- import_module libs.options.
|
|
:- import_module libs.polyhedron.
|
|
:- import_module transform_hlds.term_constr_main_types.
|
|
:- import_module transform_hlds.term_constr_util.
|
|
|
|
:- import_module bool.
|
|
:- import_module int.
|
|
:- import_module io.
|
|
:- import_module maybe.
|
|
:- import_module require.
|
|
:- import_module string.
|
|
:- import_module term.
|
|
:- import_module varset.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% Perform the fixpoint calculation on the AR.
|
|
%
|
|
|
|
% The information for each procedure in the SCC returned by a single
|
|
% iteration of the fixpoint calculation.
|
|
%
|
|
:- type iteration_info
|
|
---> iteration_info(
|
|
ii_ppid :: pred_proc_id,
|
|
ii_arg_size_poly :: polyhedron,
|
|
ii_change_flag :: bool
|
|
).
|
|
|
|
:- type iteration_infos == list(iteration_info).
|
|
|
|
do_fixpoint_calculation(Options, SCC, Iteration, [], !ModuleInfo) :-
|
|
AbstractSCC = get_abstract_scc(!.ModuleInfo, SCC),
|
|
|
|
% Carry out one iteration of fixpoint computation. We need to do this
|
|
% for all SCCs at least once in order to obtain the argument size
|
|
% constraints for non-recursive procedures. We could do that during
|
|
% the build phase for non-recursive procedures (and in fact used to)
|
|
% but the code ends up being a horrible mess.
|
|
%
|
|
set.foldl(
|
|
term_iterate_over_abstract_proc(Iteration, Options, !.ModuleInfo),
|
|
AbstractSCC, [], IterationInfos),
|
|
ChangeFlag = or_flags(IterationInfos),
|
|
(
|
|
ChangeFlag = yes,
|
|
list.foldl(update_size_info, IterationInfos, !ModuleInfo),
|
|
do_fixpoint_calculation(Options, SCC, Iteration + 1,
|
|
_, !ModuleInfo)
|
|
;
|
|
ChangeFlag = no,
|
|
% If one of the polyhedra in the SCC has `false' as its
|
|
% argument size constraint then the analysis failed. In that
|
|
% case set the argument size constraints for every procedure
|
|
% in the SCC to `true'.
|
|
% XXX Should this be happening?
|
|
%
|
|
( if
|
|
list.member(OneInfo, IterationInfos),
|
|
polyhedron.is_empty(OneInfo ^ ii_arg_size_poly)
|
|
then
|
|
ChangePoly = (func(Info0) = Info :-
|
|
Identity = polyhedron.universe,
|
|
Info = Info0 ^ ii_arg_size_poly := Identity
|
|
),
|
|
list.foldl(update_size_info, list.map(ChangePoly, IterationInfos),
|
|
!ModuleInfo)
|
|
else
|
|
list.foldl(update_size_info, IterationInfos, !ModuleInfo)
|
|
)
|
|
).
|
|
|
|
:- func or_flags(iteration_infos) = bool.
|
|
|
|
or_flags([]) = no.
|
|
or_flags([Info | Infos]) = bool.or(Info ^ ii_change_flag, or_flags(Infos)).
|
|
|
|
:- pred update_size_info(iteration_info::in, module_info::in, module_info::out)
|
|
is det.
|
|
|
|
update_size_info(Info, !ModuleInfo) :-
|
|
Info = iteration_info(PPId, Poly, _),
|
|
update_arg_size_info(PPId, Poly, !ModuleInfo).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred term_iterate_over_abstract_proc(int::in, fixpoint_options::in,
|
|
module_info::in, abstract_proc::in,
|
|
iteration_infos::in, iteration_infos::out) is det.
|
|
|
|
term_iterate_over_abstract_proc(Iteration, Options, ModuleInfo, Proc,
|
|
!IterationInfo) :-
|
|
WideningInfo = Options ^ fo_widening,
|
|
MaxMatrixSize = Options ^ fo_max_size,
|
|
AbstractPPId = Proc ^ ap_ppid,
|
|
AbstractPPId = real(PPId),
|
|
SizeVarSet = Proc ^ ap_size_varset,
|
|
Zeros = Proc ^ ap_zeros,
|
|
HeadVars = Proc ^ ap_head_vars,
|
|
|
|
% Print out the debugging traces.
|
|
module_info_get_globals(ModuleInfo, Globals),
|
|
globals.lookup_bool_option(Globals, debug_term, DebugTerm),
|
|
(
|
|
DebugTerm = yes,
|
|
trace [io(!IO)] (
|
|
PPIdStr = pred_proc_id_to_string(ModuleInfo, PPId),
|
|
io.write(PPId, !IO),
|
|
io.write_string(": ", !IO),
|
|
io.write_string(PPIdStr, !IO),
|
|
io.write_string(" ", !IO),
|
|
write_size_vars(SizeVarSet, HeadVars, !IO),
|
|
io.format("\nIteration %d:\n", [i(Iteration)], !IO),
|
|
io.flush_output(!IO)
|
|
)
|
|
;
|
|
DebugTerm = no
|
|
),
|
|
|
|
% Begin by traversing the procedure and calculating the
|
|
% IR approximation for this iteration.
|
|
|
|
Info = init_fixpoint_info(ModuleInfo, SizeVarSet, PPId, MaxMatrixSize,
|
|
HeadVars, Zeros),
|
|
|
|
some [!Polyhedron] (
|
|
term_traverse_abstract_goal(Info, Proc ^ ap_body, polyhedron.universe,
|
|
!:Polyhedron),
|
|
polyhedron.optimize(SizeVarSet, !Polyhedron),
|
|
|
|
% XXX Bug workaround - the build pass sometimes stuffs up
|
|
% the local variable set for if-then-elses.
|
|
% (See comments in term_constr_build.m).
|
|
BugConstrs0 = polyhedron.constraints(!.Polyhedron),
|
|
ConstrVarsSet = get_vars_from_constraints(BugConstrs0),
|
|
HeadVarSet = set.list_to_set(HeadVars),
|
|
BadVarsSet = set.difference(ConstrVarsSet, HeadVarSet),
|
|
BadVars = set.to_sorted_list(BadVarsSet),
|
|
!:Polyhedron = polyhedron.project(BadVars, SizeVarSet, !.Polyhedron),
|
|
polyhedron.optimize(SizeVarSet, !Polyhedron),
|
|
% XXX End of bug workaround.
|
|
|
|
% Print out the polyhedron obtained during this iteration.
|
|
(
|
|
DebugTerm = yes,
|
|
trace [io(!IO)] (
|
|
polyhedron.write_polyhedron(!.Polyhedron, SizeVarSet, !IO),
|
|
io.nl(!IO),
|
|
io.flush_output(!IO)
|
|
)
|
|
;
|
|
DebugTerm = no
|
|
),
|
|
|
|
% Look up the constraints obtained during the previous iteration.
|
|
ArgSizeInfo = lookup_proc_constr_arg_size_info(ModuleInfo, PPId),
|
|
|
|
% NOTE: `!.Polyhedron' is the set of constraints obtained by
|
|
% *this* iteration. `OldPolyhedron' is the set of constraints
|
|
% obtained by the *previous* iteration -- which may in fact be `empty'
|
|
% (i.e. false).
|
|
(
|
|
% If there were no constraints for the procedure then
|
|
% we are at the beginning of the analysis.
|
|
ArgSizeInfo = no,
|
|
OldPolyhedron = polyhedron.empty
|
|
;
|
|
ArgSizeInfo = yes(SizeInfo),
|
|
OldPolyhedron = SizeInfo
|
|
),
|
|
( if polyhedron.is_empty(!.Polyhedron) then
|
|
( if polyhedron.is_empty(OldPolyhedron) then
|
|
ChangeFlag = no
|
|
else
|
|
unexpected($pred, "old polyhedron is empty")
|
|
)
|
|
else
|
|
% If the procedure is not recursive then we need only perform one
|
|
% pass over the AR - subsequent iterations will yield the same
|
|
% result.
|
|
( if Proc ^ ap_recursion = none then
|
|
ChangeFlag = no
|
|
else if polyhedron.is_empty(OldPolyhedron) then
|
|
ChangeFlag = yes
|
|
else
|
|
test_fixpoint_and_perhaps_widen(WideningInfo, SizeVarSet,
|
|
Iteration, OldPolyhedron, !Polyhedron, ChangeFlag)
|
|
)
|
|
),
|
|
ThisIterationInfo = iteration_info(PPId, !.Polyhedron, ChangeFlag)
|
|
),
|
|
!:IterationInfo = [ThisIterationInfo | !.IterationInfo].
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type fixpoint_info
|
|
---> fixpoint_info(
|
|
tcfi_module_info :: module_info,
|
|
tcfi_varset :: size_varset,
|
|
tcfi_ppid :: pred_proc_id,
|
|
tcfi_max_matrix_size :: int,
|
|
tcfi_curr_head_vars :: head_vars,
|
|
tcfi_zeros :: zero_vars
|
|
).
|
|
|
|
:- func init_fixpoint_info(module_info, size_varset, pred_proc_id, int,
|
|
head_vars, zero_vars) = fixpoint_info.
|
|
|
|
init_fixpoint_info(ModuleInfo, SizeVarSet, PPId, MaxMatrixSize, HeadVars,
|
|
Zeros) =
|
|
fixpoint_info(ModuleInfo, SizeVarSet, PPId, MaxMatrixSize, HeadVars,
|
|
Zeros).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred term_traverse_abstract_goal(fixpoint_info::in, abstract_goal::in,
|
|
polyhedron::in, polyhedron::out) is det.
|
|
|
|
term_traverse_abstract_goal(Info, Goal, !Polyhedron) :-
|
|
(
|
|
Goal = term_disj(Goals, _Size, Locals, _),
|
|
% There are number of possible improvements that should be made here:
|
|
%
|
|
% - Take the intersection each disjunct with the constraints
|
|
% before the disjunction and compute the convex hull of that.
|
|
% This is more accurate but slower. (XXX There is some code for this
|
|
% in term_constr_data.m but it needs to be moved here). To do this
|
|
% you need to add the constraints that occur to left of the
|
|
% disjunctions to `PriorConstraints'.
|
|
%
|
|
% - Try computing the convex hull of large disjunctions pairwise
|
|
% rather than linearly. There is code to do this below but we
|
|
% currently don't use it.
|
|
|
|
PriorConstraints = polyhedron.universe,
|
|
term_traverse_abstract_disj_linearly(Goals, Locals, Info,
|
|
PriorConstraints, Polyhedron0),
|
|
post_process_abstract_goal(Locals, Info, Polyhedron0, !Polyhedron)
|
|
;
|
|
Goal = term_conj(Goals, Locals, _),
|
|
list.foldl(
|
|
term_traverse_abstract_goal(Info), Goals, polyhedron.universe,
|
|
Polyhedron0),
|
|
post_process_abstract_goal(Locals, Info, Polyhedron0, !Polyhedron)
|
|
;
|
|
Goal = term_call(CallPPId0, _, CallVars, CallZeros, Locals, _,
|
|
CallArgsPoly),
|
|
CallPPId0 = real(CallPPId),
|
|
module_info_pred_proc_info(Info ^ tcfi_module_info, CallPPId, _,
|
|
CallProcInfo),
|
|
proc_info_get_termination2_info(CallProcInfo, CallTerm2Info),
|
|
CallArgSizeInfo = term2_info_get_success_constrs(CallTerm2Info),
|
|
(
|
|
CallArgSizeInfo = no,
|
|
!:Polyhedron = polyhedron.empty
|
|
;
|
|
CallArgSizeInfo = yes(SizeInfo),
|
|
( if polyhedron.is_empty(SizeInfo) then
|
|
!:Polyhedron = polyhedron.empty
|
|
else if polyhedron.is_universe(SizeInfo) then
|
|
% Constraint store += true
|
|
true
|
|
else
|
|
HeadVars = term2_info_get_head_vars(CallTerm2Info),
|
|
SubstMap = create_var_substitution(CallVars, HeadVars),
|
|
Polyhedron0 = polyhedron.substitute_vars(SubstMap,
|
|
SizeInfo),
|
|
Polyhedron1 = intersection(Polyhedron0, CallArgsPoly),
|
|
% Set any zero_vars in the constraints to zero
|
|
% (i.e. delete the terms). We need to do this
|
|
% when polymorphic arguments are zero sized.
|
|
Polyhedron2 = polyhedron.zero_vars(CallZeros, Polyhedron1),
|
|
post_process_abstract_goal(Locals, Info,
|
|
Polyhedron2, !Polyhedron)
|
|
)
|
|
)
|
|
;
|
|
Goal = term_primitive(Poly, Locals, _),
|
|
post_process_abstract_goal(Locals, Info, Poly, !Polyhedron)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- pred post_process_abstract_goal(size_vars::in, fixpoint_info::in,
|
|
polyhedron::in, polyhedron::in, polyhedron::out) is det.
|
|
|
|
post_process_abstract_goal(Locals, Info, GoalPolyhedron0, !Polyhedron) :-
|
|
( if polyhedron.is_empty(GoalPolyhedron0) then
|
|
GoalPolyhedron = polyhedron.empty
|
|
else
|
|
GoalPolyhedron = polyhedron.project(Locals, Info ^ tcfi_varset,
|
|
GoalPolyhedron0)
|
|
),
|
|
polyhedron.intersection(GoalPolyhedron, !Polyhedron).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% Predicates for handling disjunctions.
|
|
%
|
|
|
|
% This version computes the convex hull linearly.
|
|
% That is, ( A ; B ; C ; D) is processed as:
|
|
%
|
|
% ((((empty \/ A ) \/ B ) \/ C ) \/ D)
|
|
%
|
|
:- pred term_traverse_abstract_disj_linearly(abstract_goals::in,
|
|
size_vars::in, fixpoint_info::in, polyhedron::in, polyhedron::out) is det.
|
|
|
|
term_traverse_abstract_disj_linearly(Goals, Locals, Info, !Polyhedron) :-
|
|
list.foldl(term_traverse_abstract_disj_linearly_2(Info, Locals),
|
|
Goals, polyhedron.empty, ConvexUnion),
|
|
polyhedron.intersection(ConvexUnion, !Polyhedron).
|
|
|
|
:- pred term_traverse_abstract_disj_linearly_2(fixpoint_info::in,
|
|
size_vars::in, abstract_goal::in, polyhedron::in, polyhedron::out) is det.
|
|
|
|
term_traverse_abstract_disj_linearly_2(Info, Locals, Goal, !Polyhedron) :-
|
|
SizeVarSet = Info ^ tcfi_varset,
|
|
term_traverse_abstract_goal(Info, Goal, polyhedron.universe, Polyhedron0),
|
|
Polyhedron1 = polyhedron.project(Locals, SizeVarSet, Polyhedron0),
|
|
polyhedron.convex_union(SizeVarSet, yes(Info ^ tcfi_max_matrix_size),
|
|
Polyhedron1, !Polyhedron).
|
|
|
|
% This version computes the convex hull pairwise. That is
|
|
% ( A ; B ; C ; D) is processed as: (( A \/ B ) \/ ( C \/ D)).
|
|
%
|
|
% XXX This code is currently unused.
|
|
%
|
|
:- pred term_traverse_abstract_disj_pairwise(abstract_goals::in, size_vars::in,
|
|
fixpoint_info::in, polyhedron::in, polyhedron::out) is det.
|
|
:- pragma consider_used(term_traverse_abstract_disj_pairwise/5).
|
|
|
|
term_traverse_abstract_disj_pairwise(Goals, Locals, Info, !Polyhedron) :-
|
|
SizeVarSet = Info ^ tcfi_varset,
|
|
% XXX at the moment, could be !.Poly...
|
|
PolyToLeft = polyhedron.universe,
|
|
|
|
% First convert the list of goals into their corresponding polyhedra.
|
|
ToPoly = (func(Goal) = Poly :-
|
|
term_traverse_abstract_goal(Info, Goal, PolyToLeft, Poly0),
|
|
Poly = polyhedron.project(Locals, SizeVarSet, Poly0)
|
|
),
|
|
Polyhedra0 = list.map(ToPoly, Goals),
|
|
|
|
% Now pairwise convex hull them.
|
|
HullOp = (func(A, B) = C :-
|
|
polyhedron.convex_union(SizeVarSet, yes(Info ^ tcfi_max_matrix_size),
|
|
A, B, C)
|
|
),
|
|
ConvexUnion = pairwise_map(HullOp, [ polyhedron.empty | Polyhedra0]),
|
|
polyhedron.intersection(ConvexUnion, !Polyhedron).
|
|
|
|
% This assumes that the operation in question is associative and
|
|
% commutative.
|
|
%
|
|
:- func pairwise_map(func(T, T) = T, list(T)) = T.
|
|
|
|
pairwise_map(_, []) = _ :-
|
|
unexpected($pred, "empty list").
|
|
pairwise_map(_, [X]) = X.
|
|
pairwise_map(Op, List @ [_, _ | _]) = X :-
|
|
pairwise_map_2(Op, List, [], X0),
|
|
X = pairwise_map(Op, X0).
|
|
|
|
:- pred pairwise_map_2(func(T, T) = T, list(T), list(T), list(T)).
|
|
:- mode pairwise_map_2(func(in, in) = out is det, in, in, out) is det.
|
|
|
|
pairwise_map_2(_, [], !Acc).
|
|
pairwise_map_2(_, [X], Acc, [X | Acc]).
|
|
pairwise_map_2(Op, [X, Y | Rest], !Acc) :-
|
|
!:Acc = [Op(X, Y) | !.Acc],
|
|
pairwise_map_2(Op, Rest, !Acc).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% Fixpoint test.
|
|
%
|
|
|
|
:- pred test_fixpoint_and_perhaps_widen(widening::in, size_varset::in, int::in,
|
|
polyhedron::in, polyhedron::in, polyhedron::out, bool::out) is det.
|
|
|
|
test_fixpoint_and_perhaps_widen(after_fixed_cutoff(Threshold), SizeVarSet,
|
|
Iteration, OldPoly, NewPoly, ResultPoly, ChangeFlag) :-
|
|
( if Iteration > Threshold then
|
|
ResultPoly = widen(OldPoly, NewPoly, SizeVarSet)
|
|
else
|
|
ResultPoly = NewPoly
|
|
),
|
|
ChangeFlag = test_fixpoint(NewPoly, OldPoly, SizeVarSet).
|
|
|
|
:- func test_fixpoint(polyhedron, polyhedron, size_varset) = bool.
|
|
|
|
test_fixpoint(NewPoly, OldPoly, SizeVarSet) = ChangeFlag :-
|
|
% Constraints from this iteration.
|
|
NewConstraints = polyhedron.non_false_constraints(NewPoly),
|
|
% Constraints from previous iteration.
|
|
OldConstraints = polyhedron.non_false_constraints(OldPoly),
|
|
( if
|
|
some [OldConstraint] (
|
|
list.member(OldConstraint, OldConstraints),
|
|
not entailed(SizeVarSet, NewConstraints, OldConstraint)
|
|
)
|
|
then
|
|
ChangeFlag = yes
|
|
else
|
|
ChangeFlag = no
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type fixpoint_options
|
|
---> fixpoint_options(
|
|
fo_widening :: widening,
|
|
fo_max_size :: int
|
|
).
|
|
|
|
fixpoint_options_init(Widening, MaxMatrixSize) =
|
|
fixpoint_options(Widening, MaxMatrixSize).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module transform_hlds.term_constr_fixpoint.
|
|
%-----------------------------------------------------------------------------%
|