mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-13 04:44:39 +00:00
Fix a bug reported by Zoltan which caused warning/optimization
Estimated hours taken: 0.5
Branches: main
compiler/common.m:
Fix a bug reported by Zoltan which caused warning/optimization
of duplicate calls to fail if the arguments of the calls were
duplicated constants.
tests/valid/Mmakefile:
tests/valid/Mercury.options:
tests/valid/duplicate_const.{m,exp}:
Test case.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
%---------------------------------------------------------------------------%
|
%---------------------------------------------------------------------------%
|
||||||
% Copyright (C) 1995-2002 The University of Melbourne.
|
% Copyright (C) 1995-2003 The University of Melbourne.
|
||||||
% This file may only be copied under the terms of the GNU General
|
% This file may only be copied under the terms of the GNU General
|
||||||
% Public License - see the file COPYING in the Mercury distribution.
|
% Public License - see the file COPYING in the Mercury distribution.
|
||||||
%---------------------------------------------------------------------------%
|
%---------------------------------------------------------------------------%
|
||||||
@@ -155,12 +155,25 @@ common__optimise_unification(Unification0, _Left0, _Right0, Mode, _Context,
|
|||||||
construction, Info0, OldStruct)
|
construction, Info0, OldStruct)
|
||||||
->
|
->
|
||||||
OldStruct = structure(OldVar, _, _, _),
|
OldStruct = structure(OldVar, _, _, _),
|
||||||
UniMode = ((free - Inst) -> (Inst - Inst)),
|
( ArgVars = [] ->
|
||||||
common__generate_assign(Var, OldVar, UniMode,
|
% Constants don't use memory, so there's
|
||||||
GoalInfo0, Goal - GoalInfo, Info0, Info1),
|
% no point optimizing away their
|
||||||
simplify_info_set_requantify(Info1, Info2),
|
% construction -- in fact, doing so
|
||||||
pd_cost__goal(Goal0 - GoalInfo0, Cost),
|
% could cause more stack usage.
|
||||||
simplify_info_incr_cost_delta(Info2, Cost, Info)
|
common__record_equivalence(Var, OldVar,
|
||||||
|
Info0, Info),
|
||||||
|
Goal = Goal0,
|
||||||
|
GoalInfo = GoalInfo0
|
||||||
|
;
|
||||||
|
UniMode = ((free - Inst) -> (Inst - Inst)),
|
||||||
|
common__generate_assign(Var, OldVar, UniMode,
|
||||||
|
GoalInfo0, Goal - GoalInfo,
|
||||||
|
Info0, Info1),
|
||||||
|
simplify_info_set_requantify(Info1, Info2),
|
||||||
|
pd_cost__goal(Goal0 - GoalInfo0, Cost),
|
||||||
|
simplify_info_incr_cost_delta(Info2,
|
||||||
|
Cost, Info)
|
||||||
|
)
|
||||||
;
|
;
|
||||||
Goal = Goal0,
|
Goal = Goal0,
|
||||||
GoalInfo = GoalInfo0,
|
GoalInfo = GoalInfo0,
|
||||||
@@ -356,22 +369,15 @@ common__vars_are_equiv(X, Y, VarEqv) :-
|
|||||||
common__record_cell(Var, ConsId, ArgVars, Info0, Info) :-
|
common__record_cell(Var, ConsId, ArgVars, Info0, Info) :-
|
||||||
simplify_info_get_common_info(Info0, CommonInfo0),
|
simplify_info_get_common_info(Info0, CommonInfo0),
|
||||||
simplify_info_get_var_types(Info0, VarTypes),
|
simplify_info_get_var_types(Info0, VarTypes),
|
||||||
( ArgVars = [] ->
|
CommonInfo0 = common(VarEqv, StructMapAll0,
|
||||||
% Constants do not have memory cells to reuse,
|
|
||||||
% at least in the memory models we are interested in.
|
|
||||||
CommonInfo = CommonInfo0
|
|
||||||
;
|
|
||||||
CommonInfo0 = common(VarEqv, StructMapAll0,
|
|
||||||
StructMapLastCall0, SeenCalls),
|
StructMapLastCall0, SeenCalls),
|
||||||
map__lookup(VarTypes, Var, VarType),
|
map__lookup(VarTypes, Var, VarType),
|
||||||
Struct = structure(Var, VarType, ConsId, ArgVars),
|
Struct = structure(Var, VarType, ConsId, ArgVars),
|
||||||
common__do_record_cell(StructMapAll0, ConsId,
|
common__do_record_cell(StructMapAll0, ConsId, Struct, StructMapAll),
|
||||||
Struct, StructMapAll),
|
common__do_record_cell(StructMapLastCall0, ConsId, Struct,
|
||||||
common__do_record_cell(StructMapLastCall0, ConsId, Struct,
|
|
||||||
StructMapLastCall),
|
StructMapLastCall),
|
||||||
CommonInfo = common(VarEqv, StructMapAll,
|
CommonInfo = common(VarEqv, StructMapAll,
|
||||||
StructMapLastCall, SeenCalls)
|
StructMapLastCall, SeenCalls),
|
||||||
),
|
|
||||||
simplify_info_set_common_info(Info0, CommonInfo, Info).
|
simplify_info_set_common_info(Info0, CommonInfo, Info).
|
||||||
|
|
||||||
:- pred common__do_record_cell(struct_map, cons_id, structure, struct_map).
|
:- pred common__do_record_cell(struct_map, cons_id, structure, struct_map).
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ MCFLAGS-arg_order_rearrangment = --introduce-accumulators \
|
|||||||
--trace-optimized
|
--trace-optimized
|
||||||
|
|
||||||
MCFLAGS-duplicate_call = --warn-duplicate-calls
|
MCFLAGS-duplicate_call = --warn-duplicate-calls
|
||||||
|
MCFLAGS-duplicate_const = --warn-duplicate-calls
|
||||||
MCFLAGS-unused_args_analysis = --intermodule-analysis \
|
MCFLAGS-unused_args_analysis = --intermodule-analysis \
|
||||||
--optimize-unused-args --warn-unused-args
|
--optimize-unused-args --warn-unused-args
|
||||||
MCFLAGS-unused_args_analysis2 = --intermodule-analysis \
|
MCFLAGS-unused_args_analysis2 = --intermodule-analysis \
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ ERRORCHECK_PROGS= \
|
|||||||
det_infer_warning \
|
det_infer_warning \
|
||||||
double_underscore \
|
double_underscore \
|
||||||
duplicate_call \
|
duplicate_call \
|
||||||
|
duplicate_const \
|
||||||
inf_recursion_lambda \
|
inf_recursion_lambda \
|
||||||
infinite_recursion \
|
infinite_recursion \
|
||||||
inference_test \
|
inference_test \
|
||||||
|
|||||||
2
tests/warnings/duplicate_const.exp
Normal file
2
tests/warnings/duplicate_const.exp
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
duplicate_const.m:016: Warning: redundant call to predicate `duplicate_const.called/4'.
|
||||||
|
duplicate_const.m:015: Here is the previous call to predicate `duplicate_const.called/4'.
|
||||||
20
tests/warnings/duplicate_const.m
Normal file
20
tests/warnings/duplicate_const.m
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
% Test the warning for duplicate calls where some of the arguments are
|
||||||
|
% duplicated constants.
|
||||||
|
:- module duplicate_const.
|
||||||
|
|
||||||
|
:- interface.
|
||||||
|
|
||||||
|
:- pred dup_call(int::in, int::in, int::out) is det.
|
||||||
|
|
||||||
|
:- pred called(T::in, int::in, int::in, int::out) is det.
|
||||||
|
|
||||||
|
:- implementation.
|
||||||
|
:- import_module int.
|
||||||
|
|
||||||
|
dup_call(Int1, Int2, Int) :-
|
||||||
|
called(1, Int1, Int2, Int3),
|
||||||
|
called(1, Int1, Int2, Int4),
|
||||||
|
Int is Int3 + Int4.
|
||||||
|
|
||||||
|
called(_, Int1, Int2, Int) :-
|
||||||
|
Int is Int1 + Int2.
|
||||||
Reference in New Issue
Block a user