[m-rev.] diff: two fixes for structure_sharing domain

Peter Wang novalazy at gmail.com
Mon Jan 21 13:03:18 AEDT 2008


Confirmed by Nancy.

Branches: main

compiler/structure_sharing.domain.m:
	Make `sharing_set_rename' handle the case where two variables in a
	sharing set are renamed to the same variable, which is a valid thing
	to do.  This code is based on code from on the `reuse' branch.

	Fix a bug where `sharing_set_extend_datastruct' was returning only the
	data structures which may share with a given datastruct X.  X itself
	should be part of that list.

Index: compiler/structure_sharing.domain.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/structure_sharing.domain.m,v
retrieving revision 1.21
diff -u -r1.21 structure_sharing.domain.m
--- compiler/structure_sharing.domain.m	2 Aug 2007 01:24:01 -0000	1.21
+++ compiler/structure_sharing.domain.m	21 Jan 2008 01:51:02 -0000
@@ -1075,23 +1075,16 @@
     map(prog_var, selector_sharing_set)::out) is det.
 
 do_sharing_set_rename(Dict, TypeSubst, Var0, SelectorSet0, !Map) :-
-    selector_sharing_set_rename(Dict, TypeSubst, SelectorSet0, SelectorSet),
+    selector_sharing_set_rename(Dict, TypeSubst, SelectorSet0, SelectorSet1),
     map.lookup(Dict, Var0, Var),
-    % XXX old code pretends that 2 vars can be renamed to
-    % one and the same new variable. Is that so?
-    % To check.
-    % (
-    %   map.search(!.Map, Var, SelectorSet2)
-    % ->
-    %   % can occur when 2 vars are renamed to
-    %   % the same var (call: append(X,X,Y))
-    %   selector_sharing_set_add(SelectorSet1,
-    %       SelectorSet2, SelectorSet),
-    %   map.det_update(!.Map, Var, SelectorSet, !:Map)
-    % ;
-    %   map.det_insert(!.Map, Var, SelectorSet1, !:Map)
-    % )
-    svmap.det_insert(Var, SelectorSet, !Map).
+    % Two variables can be renamed to the same new variable,
+    % e.g. append(X, X, Y).
+    ( map.search(!.Map, Var, SelectorSet2) ->
+        selector_sharing_set_add(SelectorSet1, SelectorSet2, SelectorSet),
+        svmap.det_update(Var, SelectorSet, !Map)
+    ;
+        svmap.det_insert(Var, SelectorSet1, !Map)
+    ).
 
     % The implementation for combining sharing sets is to compute the
     % alternating closure of those sets.
@@ -1258,7 +1251,7 @@
         ListSharingSet, sharing_set_init).
 
 sharing_set_extend_datastruct(ModuleInfo, ProcInfo, Datastruct, SharingSet)
-        = Datastructures :-
+        = [Datastruct | Datastructures] :-
     SharingSet = sharing_set(_, SharingMap),
     Var = Datastruct ^ sc_var,
     Selector = Datastruct ^ sc_selector,
@@ -1625,6 +1618,9 @@
 :- pred selector_sharing_set_rename(prog_var_renaming::in,
     tsubst::in, selector_sharing_set::in, selector_sharing_set::out) is det.
 
+:- pred selector_sharing_set_add(selector_sharing_set::in,
+    selector_sharing_set::in, selector_sharing_set::out) is det.
+
     % selector_sharing_set_new_entry(Selector, Datastruct, SS0, SS):
     % Adds Datastruct into SS0 using Selector as a key. Fails if that
     % Datastructs is already present with that selector.
@@ -1644,7 +1640,7 @@
 
 selector_sharing_set_init = selector_sharing_set(0, map.init).
 selector_sharing_set_is_empty(selector_sharing_set(0, _Map)).
-selector_sharing_set_size(selector_sharing_set(Size,_)) = Size.
+selector_sharing_set_size(selector_sharing_set(Size, _)) = Size.
 
 selector_sharing_set_project(ProjectionType, Vars,
         SelSharingSet0, SelSharingSet):-
@@ -1681,6 +1677,30 @@
     data_set_rename(Dict, Subst, DataSet0, DataSet),
     svmap.det_insert(Selector, DataSet, !Map).
 
+selector_sharing_set_add(SelectorSetA, SelectorSetB, SelectorSet):-
+    SelectorSetA = selector_sharing_set(_, MapA),
+    SelectorSetB = selector_sharing_set(_, MapB),
+    map.foldl(selector_sharing_set_add_2, MapA, MapB, Map),
+    map.foldl(sum_data_set_sizes, Map, 0, Size),
+    SelectorSet = selector_sharing_set(Size, Map).
+
+:- pred selector_sharing_set_add_2(selector::in, data_set::in,
+    map(selector, data_set)::in, map(selector, data_set)::out) is det.
+
+selector_sharing_set_add_2(Sel, DataSet0, !Map) :-
+    ( map.search(!.Map, Sel, DataSet1) ->
+        data_set_add(DataSet0, DataSet1, DataSet),
+        svmap.det_update(Sel, DataSet, !Map)
+    ;
+        svmap.det_insert(Sel, DataSet0, !Map)
+    ).
+
+:- pred sum_data_set_sizes(selector::in, data_set::in,
+    int::in, int::out) is det.
+
+sum_data_set_sizes(_, DataSet, Size0, Size) :-
+    Size = Size0 + data_set_size(DataSet).
+
 selector_sharing_set_new_entry(Selector, Datastruct,
         SelSharingSet0, SelSharingSet) :-
     SelSharingSet0 = selector_sharing_set(Size0, Map0),
@@ -1854,6 +1874,8 @@
 
 :- pred data_set_termshift(data_set::in, selector::in, data_set::out) is det.
 
+:- pred data_set_add(data_set::in, data_set::in, data_set::out) is det.
+
 :- pred data_set_new_entry(datastruct::in, data_set::in, data_set::out)
     is semidet.
 
@@ -1897,6 +1919,12 @@
     Set = set.map(datastruct_termshift(Selector), Set0),
     DataSet = datastructures(Size, Set).
 
+data_set_add(DataSetA, DataSetB, DataSet) :-
+    DataSetA = datastructures(_, DataA),
+    DataSetB = datastructures(_, DataB),
+    Data = set.union(DataA, DataB),
+    DataSet = datastructures(set.count(Data), Data).
+
 data_set_new_entry(Datastruct,  DataSet0, DataSet) :-
     DataSet0 = datastructures(Size0, Datastructs0),
     \+ set.member(Datastruct, Datastructs0),

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list