Files
mercury/compiler/stack_alloc.m
Zoltan Somogyi d609181cb9 Consider types of the form
Estimated hours taken: 30
Branches: main

Consider types of the form

	:- type x ---> f.

to be dummy types, since they contain no information. Optimize them the same
way we currently optimize io.state and store.store.

runtime/mercury_type_info.h:
	Add a new type_ctor_rep for dummy types.

runtime/mercury_tabling.h:
	Add a representation for "tabled" dummy types, which don't actually
	have a level in the trie, so that the runtime system can handle that
	fact.

runtime/mercury_ml_expand_body.h:
	When deconstructing a value of a dummy type, ignore the actual value
	(since it will contain garbage) and instead return the only possible
	value of the type.

runtime/mercury_construct.c:
runtime/mercury_deconstruct.c:
runtime/mercury_deep_copy_body.c:
runtime/mercury_tabling.c:
runtime/mercury_unify_compare_body.h:
library/rtti_implementation.m:
	Handle the type_ctor_rep of dummy types.

runtime/mercury_builtin_types.c:
	Provide a place to record profiling information about unifications and
	comparisons for dummy types.

runtime/mercury_mcpp.h:
java/runtime/TypeCtorRep.java:
library/private_builtin.m:
	Add a new type_ctor_rep for dummy types, and fix some previous
	discrepancies in type_ctor_reps.

mdbcomp/prim_data.m:
	Move a bunch of predicates for manipulating special_pred_ids here from
	the browser and compiler directories.

	Rename the function symbols of the special_pred_id type to avoid the
	need to parenthesize the old `initialise' function symbol.

	Convert to four-space indentation.

mdbcomp/rtti_access.m:
	Don't hardcode the names of special preds: use the predicates in
	prim_data.m.

	Convert to four-space indentation.

browser/declarative_execution.m:
	Delete some predicates whose functionality is now in
	mdbcomp/prim_data.m.

compiler/hlds_data.m:
	Replace the part of du type that says whether a type an enum, which
	used to be a bool, with something that also says whether the type is a
	dummy type.

	Convert to four-space indentation.

compiler/make_tags.m:
	Compute the value for the new field of du type definitions.

compiler/hlds_out.m:
	Write out the new field of du type definitions.

compiler/rtti.m:
	Modify the data structures we use to create type_ctor_infos to allow
	for dummy types.

	Convert to four-space indentation.

compiler/type_ctor_info.m:
	Modify the code that generates type_ctor_infos to handle dummy types.

compiler/type_util.m:
	Provide predicates for recognizing dummy types.

	Convert to four-space indentation.

compiler/unify_proc.m:
	Generate the unify and compare predicates of dummy types using a new
	code scheme that avoids referencing arguments that contain garbage.

	When generating code for unifying or comparing other types, ignore
	any arguments of function symbols that are dummy types.

	Don't use DCG style access predicates.

compiler/higher_order.m:
	Specialize the unification and comparison of values of dummy types.

	Break up an excessively large predicate, and factor out common code
	from the conditions of a chain of if-then-elses.

compiler/llds.m:
	For each input and output of a foreign_proc, include a field saying
	whether the value is of a dummy type.

compiler/pragma_c_gen.m:
	Fill in the new fields in foreign_proc arguments.

compiler/hlds_goal.m:
	Rename some predicates for constructing unifications to avoid
	unnecessary ad-hoc overloading. Clarify their documentation.

	Rename a predicate to make clear the restriction on its use,
	and document the restriction.

	Add a predicate for creating simple tests.

	Add a utility predicate for setting the context of a goal directly.

compiler/modules.m:
	Include dummy types interface files, even if they are private to the
	module. This is necessary because with the MLDS backend, the generated
	code inside the module and outside the module must agree whether a
	function returning a value of the type returns a real value or a void
	value, and this requires them to agree on whether the type is dummy
	or not.

	The impact on interface files is minimal, since very few types are
	dummy types, and changing a type from a dummy type to a non-dummy type
	or vice versa is an ever rarer change.

compiler/hlds_pred.m:
	Provide a representation in the compiler of the trie step for dummy
	types.

compiler/layout_out.m:
	Print the trie step for dummy types.

compiler/table_gen.m:
	Don't table values of dummy types, and record the fact that we don't
	by including a dummy trie step in the list of trie steps.

compiler/add_pragma.m:
compiler/add_special_pred.m:
compiler/add_type.m:
compiler/aditi_builtin_ops.m:
compiler/bytecode.m:
compiler/bytecode_gen.m:
compiler/code_gen.m:
compiler/code_info.m:
compiler/continuation_info.m:
compiler/cse_detection.m:
compiler/det_report.m:
compiler/exception_analysis.m:
compiler/inst_match.m:
compiler/livemap.m:
compiler/llds_out.m:
compiler/llds_out.m:
compiler/middle_rec.m:
compiler/ml_call_gen.m:
compiler/ml_closure_gen.m:
compiler/ml_code_gen.m:
compiler/ml_code_util.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
compiler/mlds_to_il.m:
compiler/mlds_to_il.m:
compiler/modecheck_unify.m:
compiler/modes.m:
compiler/opt_util.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/qual_info.m:
compiler/rl.m:
compiler/rl_exprn.m:
compiler/rl_key.m:
compiler/rtti_out.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/term_constr_initial.m:
compiler/term_constr_util.m:
compiler/term_norm.m:
compiler/termination.m:
compiler/trace.m:
compiler/typecheck.m:
compiler/unify_gen.m:
	Conform to the changes above.

compiler/export.m:
compiler/exprn_aux.m:
compiler/foreign.m:
compiler/polymorphism.m:
compiler/proc_label.m:
compiler/rtti_to_mlds.m:
compiler/special_pred.m:
compiler/stack_alloc.m:
compiler/stack_layout.m:
compiler/state_var.m:
compiler/switch_util.m:
compiler/trace_params.m:
	Conform to the changes above.

	Convert to four-space indentation.

compiler/mlds_to_java.m:
compiler/var_locn.m:
	Conform to the changes above, which requires threading the module_info
	through the module.

	Convert to four-space indentation.

compiler/mercury_compile.m:
	Pass the module_info to mlds_to_java.m.

compiler/ml_util.m:
compiler/polymorphism.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
	Delete some previously missed references to the temporary types used
	to bootstrap the change to the type_info type's arity.

compiler/polymorphism.m:
	Turn back on an optimization that avoids passing parameters (such as
	type_infos) to foreign_procs if they are not actually referred to.

compiler/prog_data.m:
	Convert to four-space indentation.

library/svvarset.m:
	Add a missing predicate.

trace/mercury_trace.c:
	Delete the unused function that used to check for dummy types.

tests/debugger/field_names.{m,inp,exp}:
	Add to this test case a test of the handling of dummy types. Check that
	their values can be printed out during normal execution, and that the
	debugger doesn't consider them live nondummy variables, just as it
	doesn't consider I/O states live nondummy variables.
2005-10-05 06:34:27 +00:00

272 lines
11 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 2002-2005 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 stack_alloc.m
%
% Authors: zs, conway.
%
% This module allocates stack slots to the variables that need to be saved
% across a call, across a goal that may fail, or in a parallel conjunction.
%
% The jobs is done in two steps. First we traverse the predicate definition
% looking for sets of variables that must be saved on the stack at the same
% time. If --optimize-stack-slots is set, then this phase is done by
% stack_opt.m; if --optimize-stack-slots is not set, then it is done by
% this module. Then we use a graph colouring algorithm to find an allocation
% of stack slots (colours) to variables such that in each set of variables
% that must be saved at the same time, each variable has a different colour.
%-----------------------------------------------------------------------------%
:- module ll_backend__stack_alloc.
:- interface.
:- import_module hlds__hlds_module.
:- import_module hlds__hlds_pred.
:- import_module io.
:- pred allocate_stack_slots_in_proc(pred_id::in, proc_id::in, module_info::in,
proc_info::in, proc_info::out, io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module check_hlds__goal_path.
:- import_module check_hlds__type_util.
:- import_module hlds__code_model.
:- import_module hlds__hlds_data.
:- import_module hlds__hlds_goal.
:- import_module hlds__hlds_llds.
:- import_module libs__globals.
:- import_module libs__graph_colour.
:- import_module libs__options.
:- import_module libs__trace_params.
:- import_module ll_backend__live_vars.
:- import_module ll_backend__liveness.
:- import_module ll_backend__llds.
:- import_module ll_backend__stack_opt.
:- import_module ll_backend__trace.
:- import_module parse_tree__prog_data.
:- import_module assoc_list.
:- import_module bool.
:- import_module int.
:- import_module list.
:- import_module map.
:- import_module require.
:- import_module set.
:- import_module std_util.
%-----------------------------------------------------------------------------%
allocate_stack_slots_in_proc(PredId, _ProcId, ModuleInfo, !ProcInfo, !IO) :-
initial_liveness(!.ProcInfo, PredId, ModuleInfo, Liveness0),
module_info_pred_info(ModuleInfo, PredId, PredInfo),
module_info_get_globals(ModuleInfo, Globals),
globals__get_trace_level(Globals, TraceLevel),
(
eff_trace_level_needs_input_vars(PredInfo, !.ProcInfo, TraceLevel)
= yes
->
trace__fail_vars(ModuleInfo, !.ProcInfo, FailVars)
;
set__init(FailVars)
),
body_should_use_typeinfo_liveness(PredInfo, Globals, TypeInfoLiveness),
globals__lookup_bool_option(Globals, opt_no_return_calls,
OptNoReturnCalls),
AllocData = alloc_data(ModuleInfo, !.ProcInfo, TypeInfoLiveness,
OptNoReturnCalls),
set__init(NondetLiveness0),
SimpleStackAlloc0 = stack_alloc(set__make_singleton_set(FailVars)),
proc_info_goal(!.ProcInfo, Goal0),
build_live_sets_in_goal(Goal0, Goal, FailVars, AllocData,
SimpleStackAlloc0, SimpleStackAlloc, Liveness0, _Liveness,
NondetLiveness0, _NondetLiveness),
proc_info_set_goal(Goal, !ProcInfo),
SimpleStackAlloc = stack_alloc(LiveSets0),
trace__do_we_need_maxfr_slot(Globals, PredInfo, !ProcInfo),
trace__reserved_slots(ModuleInfo, PredInfo, !.ProcInfo, Globals,
NumReservedSlots, MaybeReservedVarInfo),
(
MaybeReservedVarInfo = yes(ResVar - _),
set__singleton_set(ResVarSet, ResVar),
set__insert(LiveSets0, ResVarSet, LiveSets1)
;
MaybeReservedVarInfo = no,
LiveSets1 = LiveSets0
),
proc_info_vartypes(!.ProcInfo, VarTypes),
filter_out_dummy_values(ModuleInfo, VarTypes, LiveSets1, LiveSets,
DummyVars),
graph_colour__group_elements(LiveSets, ColourSets),
set__to_sorted_list(ColourSets, ColourList),
proc_info_interface_code_model(!.ProcInfo, CodeModel),
allocate_stack_slots(ColourList, CodeModel, NumReservedSlots,
MaybeReservedVarInfo, StackSlots1),
allocate_dummy_stack_slots(DummyVars, CodeModel, -1,
StackSlots1, StackSlots),
proc_info_set_stack_slots(StackSlots, !ProcInfo).
:- pred filter_out_dummy_values(module_info::in, vartypes::in,
set(set(prog_var))::in, set(set(prog_var))::out,
list(prog_var)::out) is det.
filter_out_dummy_values(ModuleInfo, VarTypes, LiveSet0, LiveSet, DummyVars) :-
set__to_sorted_list(LiveSet0, LiveList0),
filter_out_dummy_values_2(ModuleInfo, VarTypes, LiveList0, LiveList,
set__init, Dummies),
set__list_to_set(LiveList, LiveSet),
set__to_sorted_list(Dummies, DummyVars).
:- pred filter_out_dummy_values_2(module_info::in, vartypes::in,
list(set(prog_var))::in, list(set(prog_var))::out,
set(prog_var)::in, set(prog_var)::out) is det.
filter_out_dummy_values_2(_, _VarTypes, [], [], !Dummies).
filter_out_dummy_values_2(ModuleInfo, VarTypes,
[LiveSet0 | LiveSets0], LiveSets, !Dummies) :-
filter_out_dummy_values_2(ModuleInfo, VarTypes, LiveSets0, LiveSets1,
!Dummies),
set__to_sorted_list(LiveSet0, LiveList0),
list__filter(var_is_of_dummy_type(ModuleInfo, VarTypes), LiveList0,
DummyVars, NonDummyVars),
set__insert_list(!.Dummies, DummyVars, !:Dummies),
(
NonDummyVars = [],
LiveSets = LiveSets1
;
NonDummyVars = [_ | _],
LiveSets = [list_to_set(NonDummyVars) | LiveSets1]
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- type stack_alloc
---> stack_alloc(
set(set(prog_var)) % The sets of vars that need to be
% on the stack at the same time.
).
:- instance stack_alloc_info(stack_alloc) where [
pred(at_call_site/4) is alloc_at_call_site,
pred(at_resume_site/4) is alloc_at_resume_site,
pred(at_par_conj/4) is alloc_at_par_conj
].
:- pred alloc_at_call_site(need_across_call::in, hlds_goal_info::in,
stack_alloc::in, stack_alloc::out) is det.
alloc_at_call_site(NeedAtCall, _GoalInfo, StackAlloc0, StackAlloc) :-
NeedAtCall = need_across_call(ForwardVars, ResumeVars, NondetLiveVars),
LiveSet = set__union_list([ForwardVars, ResumeVars, NondetLiveVars]),
StackAlloc0 = stack_alloc(LiveSets0),
LiveSets = set__insert(LiveSets0, LiveSet),
StackAlloc = stack_alloc(LiveSets).
:- pred alloc_at_resume_site(need_in_resume::in, hlds_goal_info::in,
stack_alloc::in, stack_alloc::out) is det.
alloc_at_resume_site(NeedAtResume, _GoalInfo, StackAlloc0, StackAlloc) :-
NeedAtResume = need_in_resume(ResumeOnStack, ResumeVars, NondetLiveVars),
(
ResumeOnStack = no,
StackAlloc = StackAlloc0
;
ResumeOnStack = yes,
LiveSet = set__union(ResumeVars, NondetLiveVars),
StackAlloc0 = stack_alloc(LiveSets0),
LiveSets = set__insert(LiveSets0, LiveSet),
StackAlloc = stack_alloc(LiveSets)
).
:- pred alloc_at_par_conj(need_in_par_conj::in, hlds_goal_info::in,
stack_alloc::in, stack_alloc::out) is det.
alloc_at_par_conj(NeedParConj, _GoalInfo, StackAlloc0, StackAlloc) :-
NeedParConj = need_in_par_conj(StackVars),
StackAlloc0 = stack_alloc(LiveSets0),
LiveSets = set__insert(LiveSets0, StackVars),
StackAlloc = stack_alloc(LiveSets).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- pred allocate_stack_slots(list(set(prog_var))::in, code_model::in, int::in,
maybe(pair(prog_var, int))::in, stack_slots::out) is det.
allocate_stack_slots(ColourList, CodeModel, NumReservedSlots,
MaybeReservedVarInfo, StackSlots) :-
% The reserved slots are referred to by fixed number
% (e.g. framevar(1)) in trace__setup.
FirstVarSlot = NumReservedSlots + 1,
allocate_stack_slots_2(ColourList, CodeModel, FirstVarSlot,
MaybeReservedVarInfo, map__init, StackSlots).
:- pred allocate_stack_slots_2(list(set(prog_var))::in, code_model::in,
int::in, maybe(pair(prog_var, int))::in,
stack_slots::in, stack_slots::out) is det.
allocate_stack_slots_2([], _, _, _, !StackSlots).
allocate_stack_slots_2([Vars | VarSets], CodeModel, N0, MaybeReservedVarInfo,
!StackSlots) :-
(
MaybeReservedVarInfo = yes(ResVar - ResSlotNum),
set__member(ResVar, Vars)
->
SlotNum = ResSlotNum,
N1 = N0
;
SlotNum = N0,
N1 = N0 + 1
),
set__to_sorted_list(Vars, VarList),
allocate_same_stack_slot(VarList, CodeModel, SlotNum, !StackSlots),
allocate_stack_slots_2(VarSets, CodeModel, N1, MaybeReservedVarInfo,
!StackSlots).
:- pred allocate_same_stack_slot(list(prog_var)::in, code_model::in, int::in,
stack_slots::in, stack_slots::out) is det.
allocate_same_stack_slot([], _CodeModel, _Slot, !StackSlots).
allocate_same_stack_slot([Var | Vars], CodeModel, Slot, !StackSlots) :-
( CodeModel = model_non ->
Locn = nondet_slot(Slot)
;
Locn = det_slot(Slot)
),
map__det_insert(!.StackSlots, Var, Locn, !:StackSlots),
allocate_same_stack_slot(Vars, CodeModel, Slot, !StackSlots).
% We must not allocate the same stack slot to dummy variables. If we do,
% then the code that saves variables on the stack at calls will get
% confused. After saving one dummy variable on the stack, it will try
% to save the next in the same stack slot; believing the first variable
% to still be live, it will move it away.
%
% In ordinary grades, it is possible to have one value of type io__state
% and another of type store__store live at the same time; in debugging
% grades, due to our policy of extending variable lifetimes, more than
% one io__state may be live at the same time.
:- pred allocate_dummy_stack_slots(list(prog_var)::in, code_model::in,
int::in, stack_slots::in, stack_slots::out) is det.
allocate_dummy_stack_slots([], _, _, !StackSlots).
allocate_dummy_stack_slots([Var | Vars], CodeModel, N0, !StackSlots) :-
allocate_same_stack_slot([Var], CodeModel, N0, !StackSlots),
allocate_dummy_stack_slots(Vars, CodeModel, N0 - 1, !StackSlots).
%-----------------------------------------------------------------------------%