summaryrefslogtreecommitdiff
path: root/src/psl/psl-cse.adb
blob: 5d6f3df13033f854c266bd99a286137201065693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
with Ada.Text_IO;
with PSL.Prints;
with Types; use Types;

package body PSL.CSE is
   function Is_X_And_Not_X (A, B : Node) return Boolean is
   begin
      return (Get_Kind (A) = N_Not_Bool
                and then Get_Boolean (A) = B)
        or else (Get_Kind (B) = N_Not_Bool
                   and then Get_Boolean (B) = A);
   end Is_X_And_Not_X;

   type Hash_Table_Type is array (Uns32 range 0 .. 128) of Node;
   Hash_Table : Hash_Table_Type := (others => Null_Node);

   function Compute_Hash (L, R : Node; Op : Uns32) return Uns32
   is
   begin
      return Shift_Left (Get_Hash (L), 12)
        xor Shift_Left (Get_Hash (R), 2)
        xor Op;
   end Compute_Hash;

   function Compute_Hash (L: Node; Op : Uns32) return Uns32
   is
   begin
      return Shift_Left (Get_Hash (L), 2) xor Op;
   end Compute_Hash;

   procedure Dump_Hash_Table (Level : Natural := 0)
   is
      use Ada.Text_IO;
      Cnt : Natural;
      Total : Natural;
      N : Node;
   begin
      Total := 0;
      for I in Hash_Table_Type'Range loop
         Cnt := 0;
         N := Hash_Table (I);
         while N /= Null_Node loop
            Cnt := Cnt + 1;
            N := Get_Hash_Link (N);
         end loop;
         Put_Line ("Hash_table(" & Uns32'Image (I)
                     & "):" & Natural'Image (Cnt));
         Total := Total + Cnt;
         if Level > 0 then
            Cnt := 0;
            N := Hash_Table (I);
            while N /= Null_Node loop
               Put (Uns32'Image (Get_Hash (N)));
               if Level > 1 then
                  Put (": ");
                  PSL.Prints.Dump_Expr (N);
                  New_Line;
               end if;
               Cnt := Cnt + 1;
               N := Get_Hash_Link (N);
            end loop;
            if Level = 1 and then Cnt > 0 then
               New_Line;
            end if;
         end if;
      end loop;
      Put_Line ("Total:" & Natural'Image (Total));
   end Dump_Hash_Table;

   function Build_Bool_And (L, R : Node) return Node
   is
      R1 : Node;
      Res : Node;
      Hash : Uns32;
      Head, H : Node;
   begin
      if L = True_Node then
         return R;
      elsif R = True_Node then
         return L;
      elsif L = False_Node or else R = False_Node then
         return False_Node;
      elsif L = R then
         return L;
      elsif Is_X_And_Not_X (L, R) then
         return False_Node;
      end if;

      --  More simple optimizations.
      if Get_Kind (R) = N_And_Bool then
         R1 := Get_Left (R);
         if L = R1 then
            return R;
         elsif Is_X_And_Not_X (L, R1) then
            return False_Node;
         end if;
      end if;

      Hash := Compute_Hash (L, R, 2);
      Head := Hash_Table (Hash mod Hash_Table'Length);
      H := Head;
      while H /= Null_Node loop
         if Get_Hash (H) = Hash
           and then Get_Kind (H) = N_And_Bool
           and then Get_Left (H) = L
           and then Get_Right (H) = R
         then
            return H;
         end if;
         H := Get_Hash_Link (H);
      end loop;

      Res := Create_Node (N_And_Bool);
      Set_Left (Res, L);
      Set_Right (Res, R);
      Set_Hash_Link (Res, Head);
      Set_Hash (Res, Hash);
      Hash_Table (Hash mod Hash_Table'Length) := Res;
      return Res;
   end Build_Bool_And;

   function Build_Bool_Or (L, R : Node) return Node
   is
      Res : Node;
      Hash : Uns32;
      Head, H : Node;
   begin
      if L = True_Node then
         return L;
      elsif R = True_Node then
         return R;
      elsif L = False_Node then
         return R;
      elsif R = False_Node then
         return L;
      elsif L = R then
         return L;
      elsif Is_X_And_Not_X (L, R) then
         return True_Node;
      end if;

      Hash := Compute_Hash (L, R, 3);
      Head := Hash_Table (Hash mod Hash_Table'Length);
      H := Head;
      while H /= Null_Node loop
         if Get_Hash (H) = Hash
           and then Get_Kind (H) = N_Or_Bool
           and then Get_Left (H) = L
           and then Get_Right (H) = R
         then
            return H;
         end if;
         H := Get_Hash_Link (H);
      end loop;

      Res := Create_Node (N_Or_Bool);
      Set_Left (Res, L);
      Set_Right (Res, R);
      Set_Hash_Link (Res, Head);
      Set_Hash (Res, Hash);
      Hash_Table (Hash mod Hash_Table'Length) := Res;
      return Res;
   end Build_Bool_Or;

   function Build_Bool_Not (N : Node) return Node is
      Res : Node;
      Hash : Uns32;
      Head : Node;
      H : Node;
   begin
      if N = True_Node then
         return False_Node;
      elsif N = False_Node then
         return True_Node;
      elsif Get_Kind (N) = N_Not_Bool then
         return Get_Boolean (N);
      end if;

      --  Find in hash table.
      Hash := Compute_Hash (N, 1);
      Head := Hash_Table (Hash mod Hash_Table'Length);
      H := Head;
      while H /= Null_Node loop
         if Get_Hash (H) = Hash
           and then Get_Kind (H) = N_Not_Bool
           and then Get_Boolean (H) = N
         then
            return H;
         end if;
         H := Get_Hash_Link (H);
      end loop;

      Res := Create_Node (N_Not_Bool);
      Set_Boolean (Res, N);
      Set_Hash_Link (Res, Head);
      Set_Hash (Res, Hash);
      Hash_Table (Hash mod Hash_Table'Length) := Res;

      return Res;
   end Build_Bool_Not;
end PSL.CSE;