Files
mercury/compiler/code_util.m
Zoltan Somogyi cc88711d63 Implement true multi-cons_id arm switches, i.e. switches in which we associate
Estimated hours taken: 40
Branches: main

Implement true multi-cons_id arm switches, i.e. switches in which we associate
more than one cons_id with a switch arm. Previously, for switches like this:

	(
		X = a,
		goal1
	;
		( X = b
		; X = c
		),
		goal2
	)

we duplicated goal2. With this diff, goal2 won't be duplicated. We still
duplicate goals when that is necessary, i.e. in cases which the inner
disjunction contains code other than a functor test on the switched-on var,
like this:

	(
		X = a,
		goal1
	;
		(
			X = b,
			goalb
		;
			X = c
			goalc
		),
		goal2
	)

For now, true multi-cons_id arm switches are supported only by the LLDS
backend. Supporting them on the MLDS backend is trickier, because some MLDS
target languages (e.g. Java) don't support the concept at all. So when
compiling to MLDS, we still duplicate the goal in switch detection (although
we could delay the duplication to just before code generation, if we wanted.)

compiler/options.m:
	Add an internal option that tells switch detection whether to look for
	multi-cons_id switch arms.

compiler/handle_options.m:
	Set this option based on the back end.

	Add a version of the "trans" dump level that doesn't print unification
	details.

compiler/hlds_goal.m:
	Extend the representation of switch cases to allow more than one
	cons_id for a switch arm.

	Add a type for representing switches that also includes tag information
	(for use by the backends).

compiler/hlds_data.m:
	For du types, record whether it is possible to speed up tests for one
	cons_id (e.g. cons) by testing for the other (nil) and negating the
	result. Recording this information once is faster than having
	unify_gen.m trying to compute it from scratch for every single
	tag test.

	Add a type for representing a cons_id together with its tag.

compiler/hlds_out.m:
	Print out the cheaper_tag_test information for types, and possibly
	several cons_ids for each switch arm.

	Add some utility predicates for describing switch arms in terms of
	which cons_ids they are for.

	Replace some booleans with purpose-specific types.

	Make hlds_out honor is documentation, and not print out detailed
	information about unifications (e.g. uniqueness and static allocation)
	unless the right character ('u') is present in the control string.

compiler/add_type.m:
	Fill in the information about cheaper tag tests when adding a du type.

compiler/switch_detection.m:
	Extend the switch detection algorithm to detect multi-cons_id switch
	arms.

	When entering a switch arm, update the instmap to reflect that the
	switched-on variable can now be bound only to the cons_ids that this
	switch arm is for. We now need to do this, because if the arm contains
	another switch on the same variable, computing the can_fail field of
	that switch correctly requires us to know this information.
	(Obviously, an arm for a single cons_id is unlikely to have switch on
	the same variable, and for arms for several cons_ids, we previously
	duplicated the arm and left the unification with the cons_id in each
	copy, and this unification allowed the correct handling of any later
	switches. However, the code of a multi-cons_id switch arm obviously
	cannot have a unification with each cons_id in it, which is why
	we now need to get the binding information from the switch itself.)

	Replace some booleans with purpose-specific types, and give some
	predicates better names.

compiler/instmap.m:
	Provide predicates for recording that a switched-on variable has
	one of several given cons_ids, for use at the starts of switch arms.

	Give some predicates better names.

compiler/modes.m:
	Provide predicates for updating the mode_info at the start of a
	multi-cons_id switch arm.

compiler/det_report.m:
	Handle multi-cons_id switch arms.

	Update the instmap when entering each switch arm, since this is needed
	to provide good (i.e. non-misleading) error messages when one switch on
	a variable exists inside another switch on the same variable.

	Since updating the instmap requires updating the module_info (since
	the new inst may require a new entry in an inst table), thread the
	det_info through as updateable state.

	Replace some multi-clause predicate definitions with single clauses,
	to make it easier to print the arguments in mdb.

	Fix some misleading variable names.

compiler/det_analysis.m:
	Update the instmap when entering each switch arm and thread the
	det_info through as updateable state, since the predicates we call
	in det_report.m require this.

compiler/det_util.m:
	Handle multi-cons_id switch arms.

	Rationalize the argument order of some access predicates.

compiler/switch_util.m:
	Change the parts of this module that deal with string and tag switches
	to optionally convert each arm to an arbitrary representation of the
	arm. In the LLDS backend, the conversion process generated code for
	the arm, and the arm's representation is the label at the start of
	this code. This way, we can duplicate the label without duplicating
	the code.

	Add a new part of this module that associates each cons_id with its
	tag, and (during the same pass) checks whether all the cons_ids are
	integers, and if so what are min and max of these integers (needed
	for dense switches). This scan is needed because the old way of making
	this test had single-cons_id switch arms as one of its basic
	assumptions, and doing it while adding tags to each case reduces
	the number of traversals required.

	Give better names to some predicates.

compiler/switch_case.m:
	New module to handle the tasks associated with managing multi-cons_id
	switch arms, including representing them for switch_util.m.

compiler/ll_backend.m:
	Include the new module.

compiler/notes/compiler_design.html:
	Note the new module.

compiler/llds.m:
	Change the computed goto instruction to take a list of maybe labels
	instead of a list of labels, with any missing labels meaning "not
	reached".

compiler/string_switch.m:
compiler/tag_switch.m:
	Reorganize the way these modules work. We can't generate the code of
	each arm in place anymore, since it is now possible for more than one
	cons_id to call for the execution of the same code. Instead, in
	string_switch.m, we generate the codes of all the arms all at once,
	and construct the hash index afterwards. (This approach simplifies
	the code significantly.)

	In tag switches (unlike string switches), we can get locality benefits
	if the code testing for a cons_id is close to the code for that
	cons_id, so we still try to put them next to each other when such
	a locality benefit is available.

	In both modules, the new approach uses a utility predicate in
	switch_case.m to actually generate the code of each switch arm,
	eliminating several copies the same code in the old versions of these
	modules.

	In tag_switch.m, don't create a local label that simply jumps to the
	code address do_not_reached. Previously, we had to do this for
	positions in jump tables that corresponded to cons_ids that the switch
	variable could not be bound to. With the change to llds.m, we now
	simply generate a "no" instead.

compiler/lookup_switch.m:
	Get the info about int switch limits from our caller; don't compute it
	here.

	Give some variables better names.

compiler/dense_switch.m:
	Generate the codes of the cases all at once, then assemble the table,
	duplicate the labels as needed. This separation of concerns allows
	significant simplifications.

	Pack up all the information shared between the predicate that detects
	whether a dense switch is appropriate and the predicate that actually
	generates the dense switch.

	Move some utility predicates to switch_util.

compiler/switch_gen.m:
	Delete the code for tagging cons_ids, since that functionality is now
	in switch_util.m.

	The old version of this module could call the code generator to produce
	(i.e. materialize) the switched-on variable repeatedly. We now produce
	the variable once, and do the switch on the resulting rval.

compiler/unify_gen.m:
	Use the information about cheaper tag tests in the type constructor's
	entry in the HLDS type table, instead of trying to recompute it
	every time.

	Provide the predicates switch_gen.m now needs to perform tag tests
	on rvals, as opposed to variables, and against possible more than one
	cons_id.

	Allow the caller to provide the tag corresponding to the cons_id(s)
	in tag tests, since when we are generating code for switches, the
	required computations have already been done.

	Factor out some code to make all this possible.

	Give better names to some predicates.

compiler/code_info.m:
	Provide some utility predicates for the new code in other modules.
	Give better names to some existing predicates.

compiler/hlds_code_util.m:
	Rationalize the argument order of some predicates.

	Replace some multi-clause predicate definitions with single clauses,
	to make it easier to print the arguments in mdb.

compiler/accumulator.m:
compiler/add_heap_ops.m:
compiler/add_pragma.m:
compiler/add_trail_ops.m:
compiler/assertion.m:
compiler/build_mode_constraints.m:
compiler/check_typeclass.m:
compiler/closure_analysis.m:
compiler/code_util.m:
compiler/constraint.m:
compiler/cse_detection.m:
compiler/dead_proc_elim.m:
compiler/deep_profiling.m:
compiler/deforest.m:
compiler/delay_construct.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/distance_granularity.m:
compiler/dupproc.m:
compiler/equiv_type_hlds.m:
compiler/erl_code_gen.m:
compiler/exception_analysis.m:
compiler/export.m:
compiler/follow_code.m:
compiler/follow_vars.m:
compiler/foreign.m:
compiler/format_call.m:
compiler/frameopt.m:
compiler/goal_form.m:
compiler/goal_path.m:
compiler/goal_util.m:
compiler/granularity.m:
compiler/hhf.m:
compiler/higher_order.m:
compiler/implicit_parallelism.m:
compiler/inlining.m:
compiler/inst_check.m:
compiler/intermod.m:
compiler/interval.m:
compiler/lambda.m:
compiler/lambda.m:
compiler/lambda.m:
compiler/lco.m:
compiler/live_vars.m:
compiler/livemap.m:
compiler/liveness.m:
compiler/llds_out.m:
compiler/llds_to_x86_64.m:
compiler/loop_inv.m:
compiler/make_hlds_warn.m:
compiler/mark_static_terms.m:
compiler/middle_rec.m:
compiler/ml_tag_switch.m:
compiler/ml_type_gen.m:
compiler/ml_unify_gen.m:
compiler/mode_constraints.m:
compiler/mode_errors.m:
compiler/mode_util.m:
compiler/opt_debug.m:
compiler/opt_util.m:
compiler/pd_cost.m:
compiler/pd_into.m:
compiler/pd_util.m:
compiler/peephole.m:
compiler/polymorphism.m:
compiler/post_term_analysis.m:
compiler/post_typecheck.m:
compiler/purity.m:
compiler/quantification.m:
compiler/rbmm.actual_region_arguments.m:
compiler/rbmm.add_rbmm_goal_infos.m:
compiler/rbmm.condition_renaming.m:
compiler/rbmm.execution_paths.m:
compiler/rbmm.points_to_analysis.m:
compiler/rbmm.region_transformation.m:
compiler/recompilation.usage.m:
compiler/saved_vars.m:
compiler/simplify.m:
compiler/size_prof.m:
compiler/ssdebug.m:
compiler/store_alloc.m:
compiler/stratify.m:
compiler/structure_reuse.direct.choose_reuse.m:
compiler/structure_reuse.indirect.m:
compiler/structure_reuse.lbu.m:
compiler/structure_reuse.lfu.m:
compiler/structure_reuse.versions.m:
compiler/structure_sharing.analysis.m:
compiler/table_gen.m:
compiler/tabling_analysis.m:
compiler/term_constr_build.m:
compiler/term_norm.m:
compiler/term_pass1.m:
compiler/term_traversal.m:
compiler/trailing_analysis.m:
compiler/transform_llds.m:
compiler/tupling.m:
compiler/type_ctor_info.m:
compiler/type_util.m:
compiler/unify_proc.m:
compiler/unique_modes.m:
compiler/unneeded_code.m:
compiler/untupling.m:
compiler/unused_args.m:
compiler/unused_imports.m:
compiler/xml_documentation.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches.

compiler/ml_string_switch.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches.

	Give some predicates better names.

compiler/dependency_graph.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches. Change the order of arguments
	of some predicates to make this easier.

compiler/bytecode.m:
compiler/bytecode_data.m:
compiler/bytecode_gen.m:
	Make the changes necessary to conform to the changes above, principally
	to handle multi-cons_id arm switches. (The bytecode interpreter
	has not been updated.)

compiler/prog_rep.m:
mdbcomp/program_representation.m:
	Change the byte sequence representation of goals to allow switch arms
	with more than one cons_id. compiler/prog_rep.m now writes out the
	updated representation, while mdbcomp/program_representation.m reads in
	the updated representation.

deep_profiler/mdbprof_procrep.m:
	Conform to the updated program representation.

tools/binary:
	Fix a bug: if the -D option was given, the stage 2 directory wasn't
	being initialized.

	Abort if users try to give that option more than once.

compiler/Mercury.options:
	Work around bug #32 in Mantis.
2007-12-30 08:24:23 +00:00

442 lines
15 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1994-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: code_util.m.
%
% Various utilities routines for code generation and recognition of builtins.
%
%-----------------------------------------------------------------------------%
:- module ll_backend.code_util.
:- interface.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_llds.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module hlds.hlds_rtti.
:- import_module ll_backend.llds.
:- import_module mdbcomp.prim_data.
:- import_module parse_tree.prog_data.
:- import_module assoc_list.
:- import_module bool.
:- import_module list.
:- import_module maybe.
:- import_module pair.
%-----------------------------------------------------------------------------%
% Create a code address which holds the address of the specified procedure.
% The `immed' argument should be `no' if the the caller wants the returned
% address to be valid from everywhere in the program. If being valid from
% within the current procedure is enough, this argument should be `yes'
% wrapped around the value of the --procs-per-c-function option and the
% current procedure id. Using an address that is only valid from within
% the current procedure may make jumps more efficient.
%
:- type immed == maybe(pair(int, pred_proc_id)).
:- func make_entry_label(module_info, pred_id, proc_id, immed) = code_addr.
:- func make_entry_label_from_rtti(rtti_proc_label, immed) = code_addr.
% Create a label which holds the address of the specified procedure,
% which must be defined in the current module (procedures that are
% imported from other modules have representations only as code_addrs,
% not as labels, since their address is not known at C compilation time).
% The fourth argument has the same meaning as for make_entry_label.
%
:- func make_local_entry_label(module_info, pred_id, proc_id, immed) = label.
% Create a label internal to a Mercury procedure.
%
:- func make_internal_label(module_info, pred_id, proc_id, int) = label.
:- func extract_proc_label_from_code_addr(code_addr) = proc_label.
:- pred arg_loc_to_register(arg_loc::in, lval::out) is det.
:- pred max_mentioned_reg(list(lval)::in, int::out) is det.
:- pred max_mentioned_abs_reg(list(abs_locn)::in, int::out) is det.
:- pred goal_may_alloc_temp_frame(hlds_goal::in, bool::out) is det.
% Negate a condition.
% This is used mostly just to make the generated code more readable.
%
:- pred neg_rval(rval::in, rval::out) is det.
:- pred negate_the_test(list(instruction)::in, list(instruction)::out) is det.
% These predicates return the set of lvals referenced in an rval
% and an lval respectively. Lvals referenced indirectly through
% lvals of the form var(_) are not counted.
%
:- pred lvals_in_rval(rval::in, list(lval)::out) is det.
:- pred lvals_in_lval(lval::in, list(lval)::out) is det.
:- pred lvals_in_lvals(list(lval)::in, list(lval)::out) is det.
% Given a procedure that already has its arg_info field filled in,
% return a list giving its input variables and their initial locations.
%
:- pred build_input_arg_list(proc_info::in, assoc_list(prog_var, lval)::out)
is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module backend_libs.builtin_ops.
:- import_module backend_libs.proc_label.
:- import_module backend_libs.rtti.
:- import_module hlds.code_model.
:- import_module libs.compiler_util.
:- import_module int.
:- import_module term.
%---------------------------------------------------------------------------%
make_entry_label(ModuleInfo, PredId, ProcId, Immed) = ProcAddr :-
RttiProcLabel = make_rtti_proc_label(ModuleInfo, PredId, ProcId),
ProcAddr = make_entry_label_from_rtti(RttiProcLabel, Immed).
make_entry_label_from_rtti(RttiProcLabel, Immed) = ProcAddr :-
( RttiProcLabel ^ proc_is_imported = yes ->
ProcLabel = make_proc_label_from_rtti(RttiProcLabel),
ProcAddr = code_imported_proc(ProcLabel)
;
Label = make_local_entry_label_from_rtti(RttiProcLabel, Immed),
ProcAddr = code_label(Label)
).
make_local_entry_label(ModuleInfo, PredId, ProcId, Immed) = Label :-
RttiProcLabel = make_rtti_proc_label(ModuleInfo, PredId, ProcId),
Label = make_local_entry_label_from_rtti(RttiProcLabel, Immed).
:- func make_local_entry_label_from_rtti(rtti_proc_label, immed) = label.
make_local_entry_label_from_rtti(RttiProcLabel, Immed) = Label :-
ProcLabel = make_proc_label_from_rtti(RttiProcLabel),
(
Immed = no,
% If we want to define the label or use it to put it into a data
% structure, a label that is usable only within the current C module
% won't do.
( RttiProcLabel ^ proc_is_exported = yes ->
EntryType = entry_label_exported
;
EntryType = entry_label_local
),
Label = entry_label(EntryType, ProcLabel)
;
Immed = yes(ProcsPerFunc - proc(CurPredId, CurProcId)),
Label = choose_local_label_type(ProcsPerFunc, CurPredId, CurProcId,
RttiProcLabel ^ pred_id, RttiProcLabel ^ proc_id, ProcLabel)
).
:- func choose_local_label_type(int, pred_id, proc_id, pred_id, proc_id,
proc_label) = label.
choose_local_label_type(ProcsPerFunc, CurPredId, CurProcId,
PredId, ProcId, ProcLabel) = Label :-
(
% If we want to branch to the label now, we prefer a form that is
% usable only within the current C module, since it is likely to be
% faster.
(
ProcsPerFunc = 0
;
PredId = CurPredId,
ProcId = CurProcId
)
->
EntryType = entry_label_c_local
;
EntryType = entry_label_local
),
Label = entry_label(EntryType, ProcLabel).
%-----------------------------------------------------------------------------%
make_internal_label(ModuleInfo, PredId, ProcId, LabelNum) = Label :-
ProcLabel = make_proc_label(ModuleInfo, PredId, ProcId),
Label = internal_label(LabelNum, ProcLabel).
extract_proc_label_from_code_addr(CodeAddr) = ProcLabel :-
( CodeAddr = code_label(Label) ->
ProcLabel = get_proc_label(Label)
; CodeAddr = code_imported_proc(ProcLabelPrime) ->
ProcLabel = ProcLabelPrime
;
unexpected(this_file, "extract_label_from_code_addr failed")
).
%-----------------------------------------------------------------------------%
arg_loc_to_register(ArgLoc, reg(reg_r, ArgLoc)).
%-----------------------------------------------------------------------------%
max_mentioned_reg(Lvals, MaxRegNum) :-
max_mentioned_reg_2(Lvals, 0, MaxRegNum).
:- pred max_mentioned_reg_2(list(lval)::in, int::in, int::out) is det.
max_mentioned_reg_2([], !MaxRegNum).
max_mentioned_reg_2([Lval | Lvals], !MaxRegNum) :-
( Lval = reg(reg_r, N) ->
int.max(N, !MaxRegNum)
;
true
),
max_mentioned_reg_2(Lvals, !MaxRegNum).
max_mentioned_abs_reg(Lvals, MaxRegNum) :-
max_mentioned_abs_reg_2(Lvals, 0, MaxRegNum).
:- pred max_mentioned_abs_reg_2(list(abs_locn)::in, int::in, int::out) is det.
max_mentioned_abs_reg_2([], !MaxRegNum).
max_mentioned_abs_reg_2([Lval | Lvals], !MaxRegNum) :-
( Lval = abs_reg(N) ->
int.max(N, !MaxRegNum)
;
true
),
max_mentioned_abs_reg_2(Lvals, !MaxRegNum).
%-----------------------------------------------------------------------------%
goal_may_alloc_temp_frame(hlds_goal(GoalExpr, _GoalInfo), May) :-
goal_may_alloc_temp_frame_2(GoalExpr, May).
:- pred goal_may_alloc_temp_frame_2(hlds_goal_expr::in, bool::out)
is det.
goal_may_alloc_temp_frame_2(generic_call(_, _, _, _), no).
goal_may_alloc_temp_frame_2(plain_call(_, _, _, _, _, _), no).
goal_may_alloc_temp_frame_2(unify(_, _, _, _, _), no).
% We cannot safely say that a foreign code fragment does not allocate
% temporary nondet frames without knowing all the #defined macros
% that expand to mktempframe and variants thereof. The performance
% impact of being too conservative is probably not too bad.
goal_may_alloc_temp_frame_2(call_foreign_proc(_, _, _, _, _, _, _), yes).
goal_may_alloc_temp_frame_2(scope(_, Goal), May) :-
Goal = hlds_goal(_, GoalInfo),
CodeModel = goal_info_get_code_model(GoalInfo),
(
CodeModel = model_non,
May = yes
;
( CodeModel = model_det
; CodeModel = model_semi
),
goal_may_alloc_temp_frame(Goal, May)
).
goal_may_alloc_temp_frame_2(negation(Goal), May) :-
goal_may_alloc_temp_frame(Goal, May).
goal_may_alloc_temp_frame_2(conj(_ConjType, Goals), May) :-
goal_list_may_alloc_temp_frame(Goals, May).
goal_may_alloc_temp_frame_2(disj(Goals), May) :-
goal_list_may_alloc_temp_frame(Goals, May).
goal_may_alloc_temp_frame_2(switch(_Var, _Det, Cases), May) :-
cases_may_alloc_temp_frame(Cases, May).
goal_may_alloc_temp_frame_2(if_then_else(_Vars, C, T, E), May) :-
( goal_may_alloc_temp_frame(C, yes) ->
May = yes
; goal_may_alloc_temp_frame(T, yes) ->
May = yes
;
goal_may_alloc_temp_frame(E, May)
).
goal_may_alloc_temp_frame_2(shorthand(ShorthandGoal), May) :-
goal_may_alloc_temp_frame_2_shorthand(ShorthandGoal,May).
:- pred goal_may_alloc_temp_frame_2_shorthand(shorthand_goal_expr::in,
bool::out) is det.
goal_may_alloc_temp_frame_2_shorthand(bi_implication(G1, G2), May) :-
( goal_may_alloc_temp_frame(G1, yes) ->
May = yes
;
goal_may_alloc_temp_frame(G2, May)
).
:- pred goal_list_may_alloc_temp_frame(list(hlds_goal)::in, bool::out) is det.
goal_list_may_alloc_temp_frame([], no).
goal_list_may_alloc_temp_frame([Goal | Goals], May) :-
( goal_may_alloc_temp_frame(Goal, yes) ->
May = yes
;
goal_list_may_alloc_temp_frame(Goals, May)
).
:- pred cases_may_alloc_temp_frame(list(case)::in, bool::out) is det.
cases_may_alloc_temp_frame([], no).
cases_may_alloc_temp_frame([case(_, _, Goal) | Cases], May) :-
( goal_may_alloc_temp_frame(Goal, yes) ->
May = yes
;
cases_may_alloc_temp_frame(Cases, May)
).
%-----------------------------------------------------------------------------%
neg_rval(Rval, NegRval) :-
( neg_rval_2(Rval, NegRval0) ->
NegRval = NegRval0
;
NegRval = unop(logical_not, Rval)
).
:- pred neg_rval_2(rval::in, rval::out) is semidet.
neg_rval_2(const(Const), const(NegConst)) :-
(
Const = llconst_true,
NegConst = llconst_false
;
Const = llconst_false,
NegConst = llconst_true
).
neg_rval_2(unop(logical_not, Rval), Rval).
neg_rval_2(binop(Op, X, Y), binop(NegOp, X, Y)) :-
neg_op(Op, NegOp).
:- pred neg_op(binary_op::in, binary_op::out) is semidet.
neg_op(eq, ne).
neg_op(ne, eq).
neg_op(int_lt, int_ge).
neg_op(int_le, int_gt).
neg_op(int_gt, int_le).
neg_op(int_ge, int_lt).
neg_op(str_eq, str_ne).
neg_op(str_ne, str_eq).
neg_op(str_lt, str_ge).
neg_op(str_le, str_gt).
neg_op(str_gt, str_le).
neg_op(str_ge, str_lt).
neg_op(float_eq, float_ne).
neg_op(float_ne, float_eq).
neg_op(float_lt, float_ge).
neg_op(float_le, float_gt).
neg_op(float_gt, float_le).
neg_op(float_ge, float_lt).
negate_the_test([], _) :-
unexpected(this_file, "negate_the_test on empty list").
negate_the_test([Instr0 | Instrs0], Instrs) :-
( Instr0 = llds_instr(if_val(Test, Target), Comment) ->
neg_rval(Test, NewTest),
Instrs = [llds_instr(if_val(NewTest, Target), Comment)]
;
negate_the_test(Instrs0, Instrs1),
Instrs = [Instr0 | Instrs1]
).
%-----------------------------------------------------------------------------%
lvals_in_lvals([], []).
lvals_in_lvals([First | Rest], FirstLvals ++ RestLvals) :-
lvals_in_lval(First, FirstLvals),
lvals_in_lvals(Rest, RestLvals).
lvals_in_rval(lval(Lval), [Lval | Lvals]) :-
lvals_in_lval(Lval, Lvals).
lvals_in_rval(var(_), []).
lvals_in_rval(mkword(_, Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_rval(const(_), []).
lvals_in_rval(unop(_, Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_rval(binop(_, Rval1, Rval2), Lvals1 ++ Lvals2) :-
lvals_in_rval(Rval1, Lvals1),
lvals_in_rval(Rval2, Lvals2).
lvals_in_rval(mem_addr(MemRef), Lvals) :-
lvals_in_mem_ref(MemRef, Lvals).
lvals_in_lval(reg(_, _), []).
lvals_in_lval(stackvar(_), []).
lvals_in_lval(parent_stackvar(_), []).
lvals_in_lval(framevar(_), []).
lvals_in_lval(succip, []).
lvals_in_lval(maxfr, []).
lvals_in_lval(curfr, []).
lvals_in_lval(succip_slot(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_lval(redofr_slot(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_lval(redoip_slot(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_lval(succfr_slot(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_lval(prevfr_slot(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_lval(hp, []).
lvals_in_lval(sp, []).
lvals_in_lval(parent_sp, []).
lvals_in_lval(field(_, Rval1, Rval2), Lvals1 ++ Lvals2) :-
lvals_in_rval(Rval1, Lvals1),
lvals_in_rval(Rval2, Lvals2).
lvals_in_lval(lvar(_), []).
lvals_in_lval(temp(_, _), []).
lvals_in_lval(mem_ref(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_lval(global_var_ref(_), []).
:- pred lvals_in_mem_ref(mem_ref::in, list(lval)::out) is det.
lvals_in_mem_ref(stackvar_ref(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_mem_ref(framevar_ref(Rval), Lvals) :-
lvals_in_rval(Rval, Lvals).
lvals_in_mem_ref(heap_ref(Rval1, _, Rval2), Lvals1 ++ Lvals2) :-
lvals_in_rval(Rval1, Lvals1),
lvals_in_rval(Rval2, Lvals2).
%-----------------------------------------------------------------------------%
build_input_arg_list(ProcInfo, VarLvals) :-
proc_info_get_headvars(ProcInfo, HeadVars),
proc_info_arg_info(ProcInfo, ArgInfos),
assoc_list.from_corresponding_lists(HeadVars, ArgInfos, VarArgInfos),
build_input_arg_list_2(VarArgInfos, VarLvals).
:- pred build_input_arg_list_2(assoc_list(prog_var, arg_info)::in,
assoc_list(prog_var, lval)::out) is det.
build_input_arg_list_2([], []).
build_input_arg_list_2([V - Arg | Rest0], VarArgs) :-
Arg = arg_info(Loc, Mode),
(
Mode = top_in,
arg_loc_to_register(Loc, Reg),
VarArgs = [V - Reg | VarArgs0]
;
( Mode = top_out
; Mode = top_unused
),
VarArgs = VarArgs0
),
build_input_arg_list_2(Rest0, VarArgs0).
%---------------------------------------------------------------------------%
:- func this_file = string.
this_file = "code_util.m".
%-----------------------------------------------------------------------------%
:- end_module code_util.
%-----------------------------------------------------------------------------%