Files
mercury/compiler/mode_info.m
Simon Taylor 75354e38bb Deforestation.
Estimated hours taken: 400

Deforestation.

This increases the code size of the compiler by ~80k when compiling
with --intermodule-optimization --deforestation.

The improvement from deforestation is not measurable for mmc -C make_hlds.m.
Compile time for make_hlds.m increased from 50.7 seconds to 52.2 seconds
when running deforestation.

compiler/simplify.m
compiler/common.m
	Provide a nicer interface for simplifying a goal,
	not an entire procedure.
	Rework the interface to avoid manipulating lots of booleans.
	Return an estimate of the improvement in cost from simplification.
	Remove failing cases and disjuncts.
	Add an option to optimize common structures even across calls.
	Remove code to merge branched goals, since that is now
	done by deforestation.

	Fix a bug: the code to collect instmap_deltas for cases was not
	including the switched-on variable in the instmap_delta,
	which caused an abort in merge_instmap_delta if the switched
	on variable was further instantiated in the switch.
	This came up while compiling the compiler with --deforestation.

compiler/det_report.
	Output duplicate call warnings even if --warn-simple-code is not set.
	XXX fix the same problem with `:- pragma obsolete'.

compiler/code_aux.m
	Update code_aux__cannot_loop to use termination information.

compiler/hlds_pred.m
compiler/dnf.m
	Pass the type_info_varmap and typeclass_info_varmap
	into hlds_pred__define_new_pred.
	Restrict the variables of the new procedure onto the variables
	of the goal.
	Make sure all relevant type_infos are passed into the new
	procedure if --typeinfo-liveness is set.

compiler/modes.m
compiler/unique_modes.m
compiler/mode_info.m
compiler/modecheck_unify.m
	Put `how_to_check_goal' into the mode_info, rather
	than passing it around.
	Add a field to the `check_unique_modes' case which
	controls whether unique modes is allowed to choose
	a different procedure. For deforestation, this is
	not allowed, since it could result in choosing a less
	efficient procedure after generalisation.

compiler/options.m
	New options:
	--deforestation
	--deforestation-depth-limit
		Safety net for termination of the algorithm.
	--deforestation-cost-factor
		Fudge factor for working out whether deforestation
		was worthwhile.
	--deforestation-vars-threshold
		Like --inline-vars-threshold.
	Enable deforestation at -O3.

	Removed an unnecessary mode for option_defaults_2, since it
	resulted in a warning about disjuncts which cannot succeed.

compiler/handle_options.m
	--no-reorder-conj implies --no-deforestation.

compiler/inlining.m
	Separate code to rename goals into inlining__do_inline_call.

compiler/hlds_goal.m
	Added predicates goal_list_nonlocals, goal_list_instmap_delta
	and goal_list_determinism to approximate information about
	conjunctions.

compiler/hlds_module.m
	Added module_info_set_pred_proc_info to put an updated
	pred_info and proc_info back into the module_info.

compiler/hlds_out.m
	Exported hlds_out__write_instmap for debugging of deforestation.
	Bracket module names on constructors where necessary.

compiler/mercury_compile.m
	Call deforestation.
	Use the new interface to simplify.m.

compiler/intermod.m
	Put recursive predicates with a top-level branched goal
	into `.opt' files.

goal_util.m
	Added goal_calls_pred_id to work out if a predicate is
	recursive before mode analysis.
	Export goal_util__goals_goal_vars for use by deforestation.
	Give a better message for a missing variable in a substitution.

compiler/instmap.m
	Give a better message for inst_merge failing.

compiler/notes/compiler_design.m
	Document the new modules.

library/varset.m
	Add varset__select to project a varset's names and values
	onto a set of variables.

doc/user_guide.texi
	Document deforestation.
	Remove a reference to a non-existent option, --no-specialize.

util/mdemangle.c
profiler/demangle.m
tests/misc_tests/mdemangle_test.{exp,inp}
	Handle the `DeforestationIn__' predicate names introduced by
	deforestation, similar to the `IntroducedFrom__' for lambda goals.

New files:

deforest.m	Deforestation.
pd_cost.m	Cost estimation.
pd_debug.m	Debugging output.
pd_info.m	State type and version control.
pd_term.m	Termination checking.
pd_util.m	Utility predicates
1998-04-27 04:05:12 +00:00

755 lines
27 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1994-1998 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: mode_info.m.
% Main author: fjh.
% This file defines the mode_info data structure, which is used to hold
% the information we need during mode analysis.
%-----------------------------------------------------------------------------%
:- module mode_info.
:- interface.
:- import_module hlds_module, hlds_pred, hlds_goal, hlds_data, instmap.
:- import_module prog_data, mode_errors, delay_info, (inst).
:- import_module map, list, varset, set, bool, term, assoc_list.
:- interface.
% The mode_info data structure and access predicates.
% XXX `side' is not used
:- type mode_context
---> call(
pred_id, % pred name / arity
int % argument number
)
; higher_order_call(
pred_or_func, % is it call/N (higher-order pred call)
% or apply/N (higher-order func call)?
int % argument number
)
; unify(
unify_context, % original source of the unification
side % LHS or RHS
)
/**** Not yet used
; unify_arg(
unify_context,
side,
cons_id,
int
)
****/
; uninitialized.
:- type side ---> left ; right.
:- type call_context
---> unify(unify_context)
; call(pred_id)
; higher_order_call(pred_or_func).
:- type var_lock_reason
---> negation
; if_then_else
; lambda(pred_or_func).
% Specify how to process goals - using either
% modes.m or unique_modes.m.
:- type how_to_check_goal
---> check_modes
; check_unique_modes(may_change_called_proc).
% Is unique modes allowed to change which procedure of a predicate
% is called. It may not change the called procedure after deforestation
% has performed a generalisation step, since that could result
% in selecting a less efficient mode, or one which doesn't even
% have the same termination behaviour.
:- type may_change_called_proc
---> may_change_called_proc
; may_not_change_called_proc.
:- type locked_vars == assoc_list(var_lock_reason, set(var)).
:- type mode_info.
:- pred mode_info_init(io__state, module_info, pred_id, proc_id, term__context,
set(var), instmap, how_to_check_goal, mode_info).
:- mode mode_info_init(di, in, in, in, in, in, in, in, mode_info_uo) is det.
:- pred mode_info_get_io_state(mode_info, io__state).
:- mode mode_info_get_io_state(mode_info_get_io_state, uo) is det.
:- pred mode_info_set_io_state(mode_info, io__state, mode_info).
:- mode mode_info_set_io_state(mode_info_set_io_state, di, mode_info_uo) is det.
:- pred mode_info_get_module_info(mode_info, module_info).
:- mode mode_info_get_module_info(mode_info_ui, out) is det.
:- pred mode_info_set_module_info(mode_info, module_info, mode_info).
:- mode mode_info_set_module_info(mode_info_di, in, mode_info_uo) is det.
:- pred mode_info_get_preds(mode_info, pred_table).
:- mode mode_info_get_preds(mode_info_ui, out) is det.
:- pred mode_info_get_modes(mode_info, mode_table).
:- mode mode_info_get_modes(mode_info_ui, out) is det.
:- pred mode_info_get_insts(mode_info, inst_table).
:- mode mode_info_get_insts(mode_info_ui, out) is det.
:- pred mode_info_get_predid(mode_info, pred_id).
:- mode mode_info_get_predid(mode_info_ui, out) is det.
:- pred mode_info_get_procid(mode_info, proc_id).
:- mode mode_info_get_procid(mode_info_ui, out) is det.
:- pred mode_info_get_context(mode_info, term__context).
:- mode mode_info_get_context(mode_info_ui, out) is det.
:- pred mode_info_set_context(term__context, mode_info, mode_info).
:- mode mode_info_set_context(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_mode_context(mode_info, mode_context).
:- mode mode_info_get_mode_context(mode_info_ui, out) is det.
:- pred mode_info_set_mode_context(mode_context, mode_info, mode_info).
:- mode mode_info_set_mode_context(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_set_call_context(call_context, mode_info, mode_info).
:- mode mode_info_set_call_context(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_set_call_arg_context(int, mode_info, mode_info).
:- mode mode_info_set_call_arg_context(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_unset_call_context(mode_info, mode_info).
:- mode mode_info_unset_call_context(mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_instmap(mode_info, instmap).
:- mode mode_info_get_instmap(mode_info_ui, out) is det.
:- pred mode_info_dcg_get_instmap(instmap, mode_info, mode_info).
:- mode mode_info_dcg_get_instmap(out, mode_info_di, mode_info_uo) is det.
:- pred mode_info_set_instmap(instmap, mode_info, mode_info).
:- mode mode_info_set_instmap(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_locked_vars(mode_info, locked_vars).
:- mode mode_info_get_locked_vars(mode_info_ui, out) is det.
:- pred mode_info_set_locked_vars(mode_info, locked_vars, mode_info).
:- mode mode_info_set_locked_vars(mode_info_di, in, mode_info_uo) is det.
:- pred mode_info_get_errors(mode_info, list(mode_error_info)).
:- mode mode_info_get_errors(mode_info_ui, out) is det.
:- pred mode_info_get_num_errors(mode_info, int).
:- mode mode_info_get_num_errors(mode_info_ui, out) is det.
:- pred mode_info_set_errors(list(mode_error_info), mode_info, mode_info).
:- mode mode_info_set_errors(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_add_live_vars(set(var), mode_info, mode_info).
:- mode mode_info_add_live_vars(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_remove_live_vars(set(var), mode_info, mode_info).
:- mode mode_info_remove_live_vars(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_var_list_is_live(list(var), mode_info, list(is_live)).
:- mode mode_info_var_list_is_live(in, mode_info_ui, out) is det.
:- pred mode_info_var_is_live(mode_info, var, is_live).
:- mode mode_info_var_is_live(mode_info_ui, in, out) is det.
:- pred mode_info_var_is_nondet_live(mode_info, var, is_live).
:- mode mode_info_var_is_nondet_live(mode_info_ui, in, out) is det.
:- pred mode_info_get_liveness(mode_info, set(var)).
:- mode mode_info_get_liveness(mode_info_ui, out) is det.
:- pred mode_info_get_liveness_2(list(set(var)), set(var), set(var)).
:- mode mode_info_get_liveness_2(in, in, out) is det.
:- pred mode_info_get_varset(mode_info, varset).
:- mode mode_info_get_varset(mode_info_ui, out) is det.
:- pred mode_info_set_varset(varset, mode_info, mode_info).
:- mode mode_info_set_varset(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_instvarset(mode_info, varset).
:- mode mode_info_get_instvarset(mode_info_ui, out) is det.
:- pred mode_info_get_var_types(mode_info, map(var,type)).
:- mode mode_info_get_var_types(mode_info_ui, out) is det.
:- pred mode_info_set_var_types(map(var, type), mode_info, mode_info).
:- mode mode_info_set_var_types(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_types_of_vars(mode_info, list(var), list(type)).
:- mode mode_info_get_types_of_vars(mode_info_ui, in, out) is det.
:- pred mode_info_lock_vars(var_lock_reason, set(var), mode_info, mode_info).
:- mode mode_info_lock_vars(in, in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_unlock_vars(var_lock_reason, set(var), mode_info, mode_info).
:- mode mode_info_unlock_vars(in, in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_var_is_locked(mode_info, var, var_lock_reason).
:- mode mode_info_var_is_locked(mode_info_ui, in, out) is semidet.
:- pred mode_info_var_is_locked_2(locked_vars, var, var_lock_reason).
:- mode mode_info_var_is_locked_2(in, in, out) is semidet.
:- pred mode_info_get_delay_info(mode_info, delay_info).
:- mode mode_info_get_delay_info(mode_info_no_io, out) is det.
:- pred mode_info_set_delay_info(delay_info, mode_info, mode_info).
:- mode mode_info_set_delay_info(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_live_vars(mode_info, list(set(var))).
:- mode mode_info_get_live_vars(mode_info_ui, out) is det.
:- pred mode_info_set_live_vars(list(set(var)), mode_info, mode_info).
:- mode mode_info_set_live_vars(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_nondet_live_vars(mode_info, list(set(var))).
:- mode mode_info_get_nondet_live_vars(mode_info_no_io, out) is det.
:- pred mode_info_set_nondet_live_vars(list(set(var)), mode_info, mode_info).
:- mode mode_info_set_nondet_live_vars(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_last_checkpoint_insts(mode_info, assoc_list(var, inst)).
:- mode mode_info_get_last_checkpoint_insts(mode_info_no_io, out) is det.
:- pred mode_info_set_last_checkpoint_insts(assoc_list(var, inst),
mode_info, mode_info).
:- mode mode_info_set_last_checkpoint_insts(in, mode_info_di, mode_info_uo)
is det.
:- pred mode_info_get_changed_flag(mode_info, bool).
:- mode mode_info_get_changed_flag(mode_info_no_io, out) is det.
:- pred mode_info_set_changed_flag(bool, mode_info, mode_info).
:- mode mode_info_set_changed_flag(in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_get_how_to_check(mode_info, how_to_check_goal).
:- mode mode_info_get_how_to_check(mode_info_ui, out) is det.
:- pred mode_info_set_how_to_check(how_to_check_goal, mode_info, mode_info).
:- mode mode_info_set_how_to_check(in, mode_info_di, mode_info_uo) is det.
/*
:- inst uniq_mode_info = bound_unique(
mode_info(
unique,
ground, ground, ground,
ground, ground, ground, ground,
ground, ground, ground, ground,
ground, ground, ground
)
).
*/
:- inst uniq_mode_info = ground.
:- mode mode_info_uo :: free -> uniq_mode_info.
:- mode mode_info_ui :: uniq_mode_info -> uniq_mode_info.
:- mode mode_info_di :: uniq_mode_info -> dead.
% Some fiddly modes used when we want to extract
% the io_state from a mode_info struct and then put it back again.
/*
:- inst mode_info_no_io = bound_unique(
mode_info(
dead, ground, ground, ground,
ground, ground, ground, ground,
ground, ground, ground, ground,
ground, ground, ground
)
).
*/
:- inst mode_info_no_io = ground.
:- mode mode_info_get_io_state :: uniq_mode_info -> mode_info_no_io.
:- mode mode_info_no_io :: mode_info_no_io -> mode_info_no_io.
:- mode mode_info_set_io_state :: mode_info_no_io -> dead.
%-----------------------------------------------------------------------------%
% record a mode error (and associated context info) in the mode_info.
:- pred mode_info_error(set(var), mode_error, mode_info, mode_info).
:- mode mode_info_error(in, in, mode_info_di, mode_info_uo) is det.
:- pred mode_info_add_error(mode_error_info, mode_info, mode_info).
:- mode mode_info_add_error(in, mode_info_di, mode_info_uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module delay_info, mode_errors, mode_util.
:- import_module require, std_util, queue.
:- type mode_info
---> mode_info(
io__state,
module_info,
pred_id, % The pred we are checking
proc_id, % The mode which we are checking
varset, % The variables in the current proc
map(var, type), % The types of the variables
term__context, % The line number of the subgoal we
% are currently checking
mode_context, % A description of where in the
% goal the error occurred
instmap, % The current instantiatedness
% of the variables
locked_vars, % The "locked" variables,
% i.e. variables which cannot be
% further instantiated inside a
% negated context
delay_info, % info about delayed goals
list(mode_error_info),
% The mode errors found
list(set(var)), % The live variables,
% i.e. those variables which may be referenced again on forward
% execution or after shallow backtracking. (By shallow
% backtracking, I mean semidet backtracking in a negation,
% if-then-else, or semidet disjunction within the current
% predicate.)
list(set(var)), % The nondet-live variables,
% i.e. those variables which may be referenced again after deep
% backtracking TO THE CURRENT EXECUTION POINT. These are the
% variables which need to be made mostly_unique rather than
% unique when we get to a nondet disjunction or a nondet call.
% We do not include variables which may be referenced again
% after backtracking to a point EARLIER THAN the current
% execution point, since those variables will *already* have
% been marked as mostly_unique rather than unique.)
assoc_list(var, inst),
% This field is used by the checkpoint code when debug_modes is on.
% It has the instmap that was current at the last mode checkpoint,
% so that checkpoints do not print out the insts of variables
% whose insts have not changed since the last checkpoint.
% This field will always contain an empty list if debug_modes is off,
% since its information is not needed then.
bool, % Changed flag
% If `yes', then we may need
% to repeat mode inference.
how_to_check_goal
).
% The normal inst of a mode_info struct: ground, with
% the io_state and the struct itself unique, but with
% multiple references allowed for the other parts.
%-----------------------------------------------------------------------------%
% Initialize the mode_info
mode_info_init(IOState, ModuleInfo, PredId, ProcId, Context,
LiveVars, InstMapping0, HowToCheck, ModeInfo) :-
mode_context_init(ModeContext),
LockedVars = [],
delay_info__init(DelayInfo),
ErrorList = [],
% look up the varset and var types
module_info_preds(ModuleInfo, Preds),
map__lookup(Preds, PredId, PredInfo),
pred_info_procedures(PredInfo, Procs),
map__lookup(Procs, ProcId, ProcInfo),
proc_info_varset(ProcInfo, VarSet),
proc_info_vartypes(ProcInfo, VarTypes),
LiveVarsList = [LiveVars],
NondetLiveVarsList = [LiveVars],
Changed = no,
ModeInfo = mode_info(
IOState, ModuleInfo, PredId, ProcId, VarSet, VarTypes,
Context, ModeContext, InstMapping0, LockedVars, DelayInfo,
ErrorList, LiveVarsList, NondetLiveVarsList, [],
Changed, HowToCheck
).
%-----------------------------------------------------------------------------%
% Lots of very boring access predicates.
mode_info_get_io_state(mode_info(IOState0,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_),
IOState) :-
% XXX
unsafe_promise_unique(IOState0, IOState).
%-----------------------------------------------------------------------------%
mode_info_set_io_state( mode_info(_,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q), IOState0,
mode_info(IOState,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q)) :-
% XXX
unsafe_promise_unique(IOState0, IOState).
%-----------------------------------------------------------------------------%
mode_info_get_module_info(mode_info(_,ModuleInfo,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_),
ModuleInfo).
%-----------------------------------------------------------------------------%
mode_info_set_module_info(mode_info(A,_,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q),
ModuleInfo,
mode_info(A,ModuleInfo,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q)).
%-----------------------------------------------------------------------------%
mode_info_get_preds(mode_info(_,ModuleInfo,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_),
Preds) :-
module_info_preds(ModuleInfo, Preds).
%-----------------------------------------------------------------------------%
mode_info_get_modes(mode_info(_,ModuleInfo,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_),
Modes) :-
module_info_modes(ModuleInfo, Modes).
%-----------------------------------------------------------------------------%
mode_info_get_insts(mode_info(_,ModuleInfo,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_),
Insts) :-
module_info_insts(ModuleInfo, Insts).
%-----------------------------------------------------------------------------%
mode_info_get_predid(mode_info(_,_,PredId,_,_,_,_,_,_,_,_,_,_,_,_,_,_),
PredId).
%-----------------------------------------------------------------------------%
mode_info_get_procid(mode_info(_,_,_,ProcId,_,_,_,_,_,_,_,_,_,_,_,_,_), ProcId).
%-----------------------------------------------------------------------------%
mode_info_get_varset(mode_info(_,_,_,_,VarSet,_,_,_,_,_,_,_,_,_,_,_,_), VarSet).
%-----------------------------------------------------------------------------%
mode_info_set_varset(VarSet, mode_info(A,B,C,D,_,F,G,H,I,J,K,L,M,N,O,P,Q),
mode_info(A,B,C,D,VarSet,F,G,H,I,J,K,L,M,N,O,P,Q)).
%-----------------------------------------------------------------------------%
mode_info_get_var_types(mode_info(_,_,_,_,_,VarTypes,_,_,_,_,_,_,_,_,_,_,_),
VarTypes).
%-----------------------------------------------------------------------------%
mode_info_set_var_types(VarTypes, mode_info(A,B,C,D,E,_,G,H,I,J,K,L,M,N,O,P,Q),
mode_info(A,B,C,D,E,VarTypes,G,H,I,J,K,L,M,N,O,P,Q)).
%-----------------------------------------------------------------------------%
mode_info_get_context(mode_info(_,_,_,_,_,_,Context,_,_,_,_,_,_,_,_,_,_),
Context).
%-----------------------------------------------------------------------------%
mode_info_set_context(Context, mode_info(A,B,C,D,E,F,_,H,I,J,K,L,M,N,O,P,Q),
mode_info(A,B,C,D,E,F,Context,H,I,J,K,L,M,N,O,P,Q)).
%-----------------------------------------------------------------------------%
mode_info_get_mode_context(
mode_info(_,_,_,_,_,_,_,ModeContext,_,_,_,_,_,_,_,_,_),
ModeContext).
%-----------------------------------------------------------------------------%
mode_info_set_mode_context(ModeContext,
mode_info(A,B,C,D,E,F,G,_,I,J,K,L,M,N,O,P,Q),
mode_info(A,B,C,D,E,F,G,ModeContext,I,J,K,L,M,N,O,P,Q)).
%-----------------------------------------------------------------------------%
mode_info_set_call_context(unify(UnifyContext)) -->
mode_info_set_mode_context(unify(UnifyContext, left)).
mode_info_set_call_context(call(PredId)) -->
mode_info_set_mode_context(call(PredId, 0)).
mode_info_set_call_context(higher_order_call(PredOrFunc)) -->
mode_info_set_mode_context(higher_order_call(PredOrFunc, 0)).
mode_info_set_call_arg_context(ArgNum, ModeInfo0, ModeInfo) :-
mode_info_get_mode_context(ModeInfo0, ModeContext0),
( ModeContext0 = call(PredId, _) ->
mode_info_set_mode_context(call(PredId, ArgNum),
ModeInfo0, ModeInfo)
; ModeContext0 = higher_order_call(PredOrFunc, _) ->
mode_info_set_mode_context(
higher_order_call(PredOrFunc, ArgNum),
ModeInfo0, ModeInfo)
;
error("mode_info_set_call_arg_context")
).
mode_info_unset_call_context -->
mode_info_set_mode_context(uninitialized).
%-----------------------------------------------------------------------------%
mode_info_get_instmap(mode_info(_,_,_,_,_,_,_,_,InstMap,_,_,_,_,_,_,_,_),
InstMap).
% mode_info_dcg_get_instmap/3 is the same as mode_info_get_instmap/2
% except that it's easier to use inside a DCG.
mode_info_dcg_get_instmap(InstMap, ModeInfo, ModeInfo) :-
mode_info_get_instmap(ModeInfo, InstMap).
%-----------------------------------------------------------------------------%
mode_info_set_instmap( InstMap,
mode_info(A,B,C,D,E,F,G,H,InstMap0,J,DelayInfo0,L,M,N,O,P,Q),
mode_info(A,B,C,D,E,F,G,H,InstMap,J,DelayInfo,L,M,N,O,P,Q)) :-
( instmap__is_unreachable(InstMap), instmap__is_reachable(InstMap0) ->
delay_info__bind_all_vars(DelayInfo0, DelayInfo)
;
DelayInfo = DelayInfo0
).
%-----------------------------------------------------------------------------%
mode_info_get_locked_vars(mode_info(_,_,_,_,_,_,_,_,_,LockedVars,_,_,_,_,_,_,_),
LockedVars).
%-----------------------------------------------------------------------------%
mode_info_set_locked_vars( mode_info(A,B,C,D,E,F,G,H,I,_,K,L,M,N,O,P,Q),
LockedVars, mode_info(A,B,C,D,E,F,G,H,I,LockedVars,K,L,M,N,O,P,Q)).
%-----------------------------------------------------------------------------%
mode_info_get_errors(mode_info(_,_,_,_,_,_,_,_,_,_,_,Errors,_,_,_,_,_), Errors).
%-----------------------------------------------------------------------------%
mode_info_get_num_errors(mode_info(_,_,_,_,_,_,_,_,_,_,_,Errors,_,_,_,_,_),
NumErrors) :-
list__length(Errors, NumErrors).
%-----------------------------------------------------------------------------%
mode_info_set_errors( Errors, mode_info(A,B,C,D,E,F,G,H,I,J,K,_,M,N,O,P,Q),
mode_info(A,B,C,D,E,F,G,H,I,J,K,Errors,M,N,O,P,Q)).
%-----------------------------------------------------------------------------%
% We keep track of the live variables and the nondet-live variables
% a bag, represented as a list of sets of vars.
% This allows us to easily add and remove sets of variables.
% It's probably not maximally efficient.
% Add a set of vars to the bag of live vars and
% the bag of nondet-live vars.
mode_info_add_live_vars(NewLiveVars,
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,
LiveVars0,NondetLiveVars0,O,P,Q),
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,
LiveVars,NondetLiveVars,O,P,Q)) :-
LiveVars = [NewLiveVars | LiveVars0],
NondetLiveVars = [NewLiveVars | NondetLiveVars0].
% Remove a set of vars from the bag of live vars and
% the bag of nondet-live vars.
mode_info_remove_live_vars(OldLiveVars, ModeInfo0, ModeInfo) :-
ModeInfo0 = mode_info(A,B,C,D,E,F,G,H,I,J,K,L,
LiveVars0, NondetLiveVars0,O,P,Q),
ModeInfo1 = mode_info(A,B,C,D,E,F,G,H,I,J,K,L,
LiveVars, NondetLiveVars,O,P,Q),
(
list__delete_first(LiveVars0, OldLiveVars, LiveVars1),
list__delete_first(NondetLiveVars0, OldLiveVars,
NondetLiveVars1)
->
LiveVars = LiveVars1,
NondetLiveVars = NondetLiveVars1
;
error("mode_info_remove_live_vars: failed")
),
% when a variable becomes dead, we may be able to wake
% up a goal which is waiting on that variable
set__to_sorted_list(OldLiveVars, VarList),
mode_info_get_delay_info(ModeInfo1, DelayInfo0),
delay_info__bind_var_list(VarList, DelayInfo0, DelayInfo),
mode_info_set_delay_info(DelayInfo, ModeInfo1, ModeInfo).
% Check whether a list of variables are live or not
mode_info_var_list_is_live([], _, []).
mode_info_var_list_is_live([Var | Vars], ModeInfo, [Live | Lives]) :-
mode_info_var_is_live(ModeInfo, Var, Live),
mode_info_var_list_is_live(Vars, ModeInfo, Lives).
% Check whether a variable is live or not
mode_info_var_is_live(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,LiveVarsList,_,_,_,_),
Var, Result) :-
(
% some [LiveVars]
list__member(LiveVars, LiveVarsList),
set__member(Var, LiveVars)
->
Result = live
;
Result = dead
).
% Check whether a variable is nondet_live or not.
mode_info_var_is_nondet_live(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,_,
NondetLiveVarsList,_,_,_), Var, Result) :-
(
% some [LiveVars]
list__member(LiveVars, NondetLiveVarsList),
set__member(Var, LiveVars)
->
Result = live
;
Result = dead
).
mode_info_get_liveness(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,LiveVarsList,_,_,_,_),
LiveVars) :-
set__init(LiveVars0),
mode_info_get_liveness_2(LiveVarsList, LiveVars0, LiveVars).
mode_info_get_liveness_2([], LiveVars, LiveVars).
mode_info_get_liveness_2([LiveVarsSet | LiveVarsList], LiveVars0, LiveVars) :-
set__union(LiveVars0, LiveVarsSet, LiveVars1),
mode_info_get_liveness_2(LiveVarsList, LiveVars1, LiveVars).
mode_info_get_live_vars(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,LiveVarsList,_,_,_,_),
LiveVarsList).
mode_info_set_live_vars(LiveVarsList,
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,_,N,O,P,Q),
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,LiveVarsList,N,O,P,Q)).
%-----------------------------------------------------------------------------%
% Since we don't yet handle polymorphic modes, the inst varset
% is always empty.
mode_info_get_instvarset(_ModeInfo, InstVarSet) :-
varset__init(InstVarSet).
mode_info_get_types_of_vars(ModeInfo, Vars, TypesOfVars) :-
mode_info_get_var_types(ModeInfo, VarTypes),
map__apply_to_list(Vars, VarTypes, TypesOfVars).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% The locked variables are stored as a stack
% of sets of variables. A variable is locked if it is
% a member of any of the sets. To lock a set of vars, we just
% push them on the stack, and to unlock a set of vars, we just
% pop them off the stack. The stack is implemented as a list.
mode_info_lock_vars(Reason, Vars, ModeInfo0, ModeInfo) :-
mode_info_get_locked_vars(ModeInfo0, LockedVars),
mode_info_set_locked_vars(ModeInfo0, [Reason - Vars | LockedVars],
ModeInfo).
mode_info_unlock_vars(Reason, Vars, ModeInfo0, ModeInfo) :-
mode_info_get_locked_vars(ModeInfo0, LockedVars0),
(
LockedVars0 = [Reason - TheseVars | LockedVars1],
set__equal(TheseVars, Vars)
->
LockedVars = LockedVars1
;
error("mode_info_unlock_vars: some kind of nesting error")
),
mode_info_set_locked_vars(ModeInfo0, LockedVars, ModeInfo).
mode_info_var_is_locked(ModeInfo, Var, Reason) :-
mode_info_get_locked_vars(ModeInfo, LockedVarsList),
mode_info_var_is_locked_2(LockedVarsList, Var, Reason).
mode_info_var_is_locked_2([ThisReason - Set | Sets], Var, Reason) :-
(
set__member(Var, Set)
->
Reason = ThisReason
;
mode_info_var_is_locked_2(Sets, Var, Reason)
).
mode_info_get_delay_info(mode_info(_,_,_,_,_,_,_,_,_,_,DelayInfo,_,_,_,_,_,_),
DelayInfo).
mode_info_set_delay_info(DelayInfo,
mode_info(A,B,C,D,E,F,G,H,I,J,_,L,M,N,O,P,Q),
mode_info(A,B,C,D,E,F,G,H,I,J,DelayInfo,L,M,N,O,P,Q)).
mode_info_get_nondet_live_vars(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,_,
NondetLiveVars,_,_,_), NondetLiveVars).
mode_info_set_nondet_live_vars(NondetLiveVars,
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,_,O,P,Q),
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,NondetLiveVars,O,P,Q)).
mode_info_get_last_checkpoint_insts(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,_,_,
LastCheckpointInsts,_,_), LastCheckpointInsts).
mode_info_set_last_checkpoint_insts(LastCheckpointInsts,
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,N,_,P,Q),
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,N,
LastCheckpointInsts,P,Q)).
mode_info_get_changed_flag(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,Changed,_),
Changed).
mode_info_set_changed_flag(Changed,
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,_,Q),
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,Changed,Q)).
mode_info_get_how_to_check(mode_info(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,How), How).
mode_info_set_how_to_check(How, mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,_),
mode_info(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,How)).
%-----------------------------------------------------------------------------%
mode_info_error(Vars, ModeError, ModeInfo0, ModeInfo) :-
mode_info_get_context(ModeInfo0, Context),
mode_info_get_mode_context(ModeInfo0, ModeContext),
ModeErrorInfo = mode_error_info(Vars, ModeError, Context, ModeContext),
mode_info_add_error(ModeErrorInfo, ModeInfo0, ModeInfo).
mode_info_add_error(ModeErrorInfo, ModeInfo0, ModeInfo) :-
mode_info_get_errors(ModeInfo0, Errors0),
list__append(Errors0, [ModeErrorInfo], Errors),
mode_info_set_errors(Errors, ModeInfo0, ModeInfo).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%