[m-rev.] diff: fix missed duplicate call warning

Simon Taylor stayl at cs.mu.OZ.AU
Mon Feb 17 17:23:21 AEDT 2003


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.

Index: compiler/common.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/common.m,v
retrieving revision 1.64
diff -u -u -r1.64 common.m
--- compiler/common.m	14 May 2002 14:34:23 -0000	1.64
+++ compiler/common.m	13 Feb 2003 07:29:51 -0000
@@ -155,12 +155,25 @@
 				construction, Info0, OldStruct)
 		->
 			OldStruct = structure(OldVar, _, _, _),
-			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)
+			( ArgVars = [] ->
+				% Constants don't use memory, so there's
+				% no point optimizing away their
+				% construction -- in fact, doing so
+				% could cause more stack usage.
+				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,
 			GoalInfo = GoalInfo0,
@@ -356,22 +369,15 @@
 common__record_cell(Var, ConsId, ArgVars, Info0, Info) :-
 	simplify_info_get_common_info(Info0, CommonInfo0),
 	simplify_info_get_var_types(Info0, VarTypes),
-	( ArgVars = [] ->
-		% Constants do not have memory cells to reuse,
-		% at least in the memory models we are interested in.
-		CommonInfo = CommonInfo0
-	;
-		CommonInfo0 = common(VarEqv, StructMapAll0,
+	CommonInfo0 = common(VarEqv, StructMapAll0,
 			StructMapLastCall0, SeenCalls),
-		map__lookup(VarTypes, Var, VarType),
-		Struct = structure(Var, VarType, ConsId, ArgVars),
-		common__do_record_cell(StructMapAll0, ConsId,
-			Struct, StructMapAll),
-		common__do_record_cell(StructMapLastCall0, ConsId, Struct,
+	map__lookup(VarTypes, Var, VarType),
+	Struct = structure(Var, VarType, ConsId, ArgVars),
+	common__do_record_cell(StructMapAll0, ConsId, Struct, StructMapAll),
+	common__do_record_cell(StructMapLastCall0, ConsId, Struct,
 			StructMapLastCall),
-		CommonInfo = common(VarEqv, StructMapAll,
-			StructMapLastCall, SeenCalls)
-	),
+	CommonInfo = common(VarEqv, StructMapAll,
+			StructMapLastCall, SeenCalls),
 	simplify_info_set_common_info(Info0, CommonInfo, Info).
 
 :- pred common__do_record_cell(struct_map, cons_id, structure, struct_map).
Index: tests/warnings/Mercury.options
===================================================================
RCS file: /home/mercury1/repository/tests/warnings/Mercury.options,v
retrieving revision 1.3
diff -u -u -r1.3 Mercury.options
--- tests/warnings/Mercury.options	13 Jan 2003 12:55:08 -0000	1.3
+++ tests/warnings/Mercury.options	17 Feb 2003 06:16:09 -0000
@@ -12,6 +12,7 @@
 					--trace-optimized
 
 MCFLAGS-duplicate_call		= --warn-duplicate-calls
+MCFLAGS-duplicate_const		= --warn-duplicate-calls
 MCFLAGS-unused_args_analysis    = --intermodule-analysis \
 				--optimize-unused-args --warn-unused-args
 MCFLAGS-unused_args_analysis2   = --intermodule-analysis \
Index: tests/warnings/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/warnings/Mmakefile,v
retrieving revision 1.28
diff -u -u -r1.28 Mmakefile
--- tests/warnings/Mmakefile	22 Jan 2003 13:44:47 -0000	1.28
+++ tests/warnings/Mmakefile	17 Feb 2003 06:15:58 -0000
@@ -12,6 +12,7 @@
 	det_infer_warning \
 	double_underscore \
 	duplicate_call \
+	duplicate_const \
 	inf_recursion_lambda \
 	infinite_recursion \
 	inference_test \
Index: tests/warnings/duplicate_const.exp
===================================================================
RCS file: tests/warnings/duplicate_const.exp
diff -N tests/warnings/duplicate_const.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/warnings/duplicate_const.exp	17 Feb 2003 06:16:36 -0000
@@ -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'.
Index: tests/warnings/duplicate_const.m
===================================================================
RCS file: tests/warnings/duplicate_const.m
diff -N tests/warnings/duplicate_const.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/warnings/duplicate_const.m	17 Feb 2003 06:15:51 -0000
@@ -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.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list