Branch data Line data Source code
1 : : /* Header file for the value range relational processing.
2 : : Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 : : Contributed by Andrew MacLeod <amacleod@redhat.com>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "tree.h"
26 : : #include "gimple.h"
27 : : #include "ssa.h"
28 : :
29 : : #include "gimple-range.h"
30 : : #include "tree-pretty-print.h"
31 : : #include "gimple-pretty-print.h"
32 : : #include "alloc-pool.h"
33 : : #include "dominance.h"
34 : :
35 : : static const char *const kind_string[VREL_LAST] =
36 : : { "varying", "undefined", "<", "<=", ">", ">=", "==", "!=", "pe8", "pe16",
37 : : "pe32", "pe64" };
38 : :
39 : : // Print a relation_kind REL to file F.
40 : :
41 : : void
42 : 1062 : print_relation (FILE *f, relation_kind rel)
43 : : {
44 : 1062 : fprintf (f, " %s ", kind_string[rel]);
45 : 1062 : }
46 : :
47 : : // This table is used to negate the operands. op1 REL op2 -> !(op1 REL op2).
48 : : static const unsigned char rr_negate_table[VREL_LAST] = {
49 : : VREL_VARYING, VREL_UNDEFINED, VREL_GE, VREL_GT, VREL_LE, VREL_LT, VREL_NE,
50 : : VREL_EQ };
51 : :
52 : : // Negate the relation, as in logical negation.
53 : :
54 : : relation_kind
55 : 0 : relation_negate (relation_kind r)
56 : : {
57 : 0 : return relation_kind (rr_negate_table [r]);
58 : : }
59 : :
60 : : // This table is used to swap the operands. op1 REL op2 -> op2 REL op1.
61 : : static const unsigned char rr_swap_table[VREL_LAST] = {
62 : : VREL_VARYING, VREL_UNDEFINED, VREL_GT, VREL_GE, VREL_LT, VREL_LE, VREL_EQ,
63 : : VREL_NE };
64 : :
65 : : // Return the relation as if the operands were swapped.
66 : :
67 : : relation_kind
68 : 17380600 : relation_swap (relation_kind r)
69 : : {
70 : 17380600 : return relation_kind (rr_swap_table [r]);
71 : : }
72 : :
73 : : // This table is used to perform an intersection between 2 relations.
74 : :
75 : : static const unsigned char rr_intersect_table[VREL_LAST][VREL_LAST] = {
76 : : // VREL_VARYING
77 : : { VREL_VARYING, VREL_UNDEFINED, VREL_LT, VREL_LE, VREL_GT, VREL_GE, VREL_EQ,
78 : : VREL_NE },
79 : : // VREL_UNDEFINED
80 : : { VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED,
81 : : VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED },
82 : : // VREL_LT
83 : : { VREL_LT, VREL_UNDEFINED, VREL_LT, VREL_LT, VREL_UNDEFINED, VREL_UNDEFINED,
84 : : VREL_UNDEFINED, VREL_LT },
85 : : // VREL_LE
86 : : { VREL_LE, VREL_UNDEFINED, VREL_LT, VREL_LE, VREL_UNDEFINED, VREL_EQ,
87 : : VREL_EQ, VREL_LT },
88 : : // VREL_GT
89 : : { VREL_GT, VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED, VREL_GT, VREL_GT,
90 : : VREL_UNDEFINED, VREL_GT },
91 : : // VREL_GE
92 : : { VREL_GE, VREL_UNDEFINED, VREL_UNDEFINED, VREL_EQ, VREL_GT, VREL_GE,
93 : : VREL_EQ, VREL_GT },
94 : : // VREL_EQ
95 : : { VREL_EQ, VREL_UNDEFINED, VREL_UNDEFINED, VREL_EQ, VREL_UNDEFINED, VREL_EQ,
96 : : VREL_EQ, VREL_UNDEFINED },
97 : : // VREL_NE
98 : : { VREL_NE, VREL_UNDEFINED, VREL_LT, VREL_LT, VREL_GT, VREL_GT,
99 : : VREL_UNDEFINED, VREL_NE } };
100 : :
101 : :
102 : : // Intersect relation R1 with relation R2 and return the resulting relation.
103 : :
104 : : relation_kind
105 : 90291653 : relation_intersect (relation_kind r1, relation_kind r2)
106 : : {
107 : 90291653 : return relation_kind (rr_intersect_table[r1][r2]);
108 : : }
109 : :
110 : :
111 : : // This table is used to perform a union between 2 relations.
112 : :
113 : : static const unsigned char rr_union_table[VREL_LAST][VREL_LAST] = {
114 : : // VREL_VARYING
115 : : { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
116 : : VREL_VARYING, VREL_VARYING, VREL_VARYING },
117 : : // VREL_UNDEFINED
118 : : { VREL_VARYING, VREL_UNDEFINED, VREL_LT, VREL_LE, VREL_GT, VREL_GE,
119 : : VREL_EQ, VREL_NE },
120 : : // VREL_LT
121 : : { VREL_VARYING, VREL_LT, VREL_LT, VREL_LE, VREL_NE, VREL_VARYING, VREL_LE,
122 : : VREL_NE },
123 : : // VREL_LE
124 : : { VREL_VARYING, VREL_LE, VREL_LE, VREL_LE, VREL_VARYING, VREL_VARYING,
125 : : VREL_LE, VREL_VARYING },
126 : : // VREL_GT
127 : : { VREL_VARYING, VREL_GT, VREL_NE, VREL_VARYING, VREL_GT, VREL_GE, VREL_GE,
128 : : VREL_NE },
129 : : // VREL_GE
130 : : { VREL_VARYING, VREL_GE, VREL_VARYING, VREL_VARYING, VREL_GE, VREL_GE,
131 : : VREL_GE, VREL_VARYING },
132 : : // VREL_EQ
133 : : { VREL_VARYING, VREL_EQ, VREL_LE, VREL_LE, VREL_GE, VREL_GE, VREL_EQ,
134 : : VREL_VARYING },
135 : : // VREL_NE
136 : : { VREL_VARYING, VREL_NE, VREL_NE, VREL_VARYING, VREL_NE, VREL_VARYING,
137 : : VREL_VARYING, VREL_NE } };
138 : :
139 : : // Union relation R1 with relation R2 and return the result.
140 : :
141 : : relation_kind
142 : 82483630 : relation_union (relation_kind r1, relation_kind r2)
143 : : {
144 : 82483630 : return relation_kind (rr_union_table[r1][r2]);
145 : : }
146 : :
147 : :
148 : : // This table is used to determine transitivity between 2 relations.
149 : : // (A relation0 B) and (B relation1 C) implies (A result C)
150 : :
151 : : static const unsigned char rr_transitive_table[VREL_LAST][VREL_LAST] = {
152 : : // VREL_VARYING
153 : : { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
154 : : VREL_VARYING, VREL_VARYING, VREL_VARYING },
155 : : // VREL_UNDEFINED
156 : : { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
157 : : VREL_VARYING, VREL_VARYING, VREL_VARYING },
158 : : // VREL_LT
159 : : { VREL_VARYING, VREL_VARYING, VREL_LT, VREL_LT, VREL_VARYING, VREL_VARYING,
160 : : VREL_LT, VREL_VARYING },
161 : : // VREL_LE
162 : : { VREL_VARYING, VREL_VARYING, VREL_LT, VREL_LE, VREL_VARYING, VREL_VARYING,
163 : : VREL_LE, VREL_VARYING },
164 : : // VREL_GT
165 : : { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_GT, VREL_GT,
166 : : VREL_GT, VREL_VARYING },
167 : : // VREL_GE
168 : : { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_GT, VREL_GE,
169 : : VREL_GE, VREL_VARYING },
170 : : // VREL_EQ
171 : : { VREL_VARYING, VREL_VARYING, VREL_LT, VREL_LE, VREL_GT, VREL_GE, VREL_EQ,
172 : : VREL_VARYING },
173 : : // VREL_NE
174 : : { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
175 : : VREL_VARYING, VREL_VARYING, VREL_VARYING } };
176 : :
177 : : // Apply transitive operation between relation R1 and relation R2, and
178 : : // return the resulting relation, if any.
179 : :
180 : : relation_kind
181 : 8361760 : relation_transitive (relation_kind r1, relation_kind r2)
182 : : {
183 : 8361760 : return relation_kind (rr_transitive_table[r1][r2]);
184 : : }
185 : :
186 : : // When one name is an equivalence of another, ensure the equivalence
187 : : // range is correct. Specifically for floating point, a +0 is also
188 : : // equivalent to a -0 which may not be reflected. See PR 111694.
189 : :
190 : : void
191 : 1613915 : adjust_equivalence_range (vrange &range)
192 : : {
193 : 1613915 : if (range.undefined_p () || !is_a<frange> (range))
194 : 1579924 : return;
195 : :
196 : 33991 : frange fr = as_a<frange> (range);
197 : : // If range includes 0 make sure both signs of zero are included.
198 : 33991 : if (fr.contains_p (dconst0) || fr.contains_p (dconstm0))
199 : : {
200 : 17748 : frange zeros (range.type (), dconstm0, dconst0);
201 : 17748 : range.union_ (zeros);
202 : 17748 : }
203 : 33991 : }
204 : :
205 : : // Given an equivalence set EQUIV, set all the bits in B that are still valid
206 : : // members of EQUIV in basic block BB.
207 : :
208 : : void
209 : 20722453 : relation_oracle::valid_equivs (bitmap b, const_bitmap equivs, basic_block bb)
210 : : {
211 : 20722453 : unsigned i;
212 : 20722453 : bitmap_iterator bi;
213 : 43305726 : EXECUTE_IF_SET_IN_BITMAP (equivs, 0, i, bi)
214 : : {
215 : 22583273 : tree ssa = ssa_name (i);
216 : 45166545 : if (ssa && !SSA_NAME_IN_FREE_LIST (ssa))
217 : : {
218 : 22583272 : const_bitmap ssa_equiv = equiv_set (ssa, bb);
219 : 22583272 : if (ssa_equiv == equivs)
220 : 22274545 : bitmap_set_bit (b, i);
221 : : }
222 : : }
223 : 20722453 : }
224 : :
225 : : // Return any known relation between SSA1 and SSA2 before stmt S is executed.
226 : : // If GET_RANGE is true, query the range of both operands first to ensure
227 : : // the definitions have been processed and any relations have be created.
228 : :
229 : : relation_kind
230 : 103937268 : relation_oracle::query (gimple *s, tree ssa1, tree ssa2)
231 : : {
232 : 103937268 : if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
233 : : return VREL_VARYING;
234 : 38439525 : return query (gimple_bb (s), ssa1, ssa2);
235 : : }
236 : :
237 : : // Return any known relation between SSA1 and SSA2 on edge E.
238 : : // If GET_RANGE is true, query the range of both operands first to ensure
239 : : // the definitions have been processed and any relations have be created.
240 : :
241 : : relation_kind
242 : 53288569 : relation_oracle::query (edge e, tree ssa1, tree ssa2)
243 : : {
244 : 53288569 : basic_block bb;
245 : 53288569 : if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
246 : : return VREL_VARYING;
247 : :
248 : : // Use destination block if it has a single predecessor, and this picks
249 : : // up any relation on the edge.
250 : : // Otherwise choose the src edge and the result is the same as on-exit.
251 : 39388579 : if (!single_pred_p (e->dest))
252 : 37458605 : bb = e->src;
253 : : else
254 : : bb = e->dest;
255 : :
256 : 39388579 : return query (bb, ssa1, ssa2);
257 : : }
258 : : // -------------------------------------------------------------------------
259 : :
260 : : // The very first element in the m_equiv chain is actually just a summary
261 : : // element in which the m_names bitmap is used to indicate that an ssa_name
262 : : // has an equivalence set in this block.
263 : : // This allows for much faster traversal of the DOM chain, as a search for
264 : : // SSA_NAME simply requires walking the DOM chain until a block is found
265 : : // which has the bit for SSA_NAME set. Then scan for the equivalency set in
266 : : // that block. No previous lists need be searched.
267 : :
268 : : // If SSA has an equivalence in this list, find and return it.
269 : : // Otherwise return NULL.
270 : :
271 : : equiv_chain *
272 : 176430109 : equiv_chain::find (unsigned ssa)
273 : : {
274 : 176430109 : equiv_chain *ptr = NULL;
275 : : // If there are equiv sets and SSA is in one in this list, find it.
276 : : // Otherwise return NULL.
277 : 176430109 : if (bitmap_bit_p (m_names, ssa))
278 : : {
279 : 171190815 : for (ptr = m_next; ptr; ptr = ptr->m_next)
280 : 171190815 : if (bitmap_bit_p (ptr->m_names, ssa))
281 : : break;
282 : : }
283 : 176430109 : return ptr;
284 : : }
285 : :
286 : : // Dump the names in this equivalence set.
287 : :
288 : : void
289 : 11 : equiv_chain::dump (FILE *f) const
290 : : {
291 : 11 : bitmap_iterator bi;
292 : 11 : unsigned i;
293 : :
294 : 11 : if (!m_names || bitmap_empty_p (m_names))
295 : 1 : return;
296 : 10 : fprintf (f, "Equivalence set : [");
297 : 10 : unsigned c = 0;
298 : 26 : EXECUTE_IF_SET_IN_BITMAP (m_names, 0, i, bi)
299 : : {
300 : 16 : if (ssa_name (i))
301 : : {
302 : 16 : if (c++)
303 : 6 : fprintf (f, ", ");
304 : 16 : print_generic_expr (f, ssa_name (i), TDF_SLIM);
305 : : }
306 : : }
307 : 10 : fprintf (f, "]\n");
308 : : }
309 : :
310 : : // Instantiate an equivalency oracle.
311 : :
312 : 24845791 : equiv_oracle::equiv_oracle ()
313 : : {
314 : 24845791 : bitmap_obstack_initialize (&m_bitmaps);
315 : 24845791 : m_equiv.create (0);
316 : 24845791 : m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
317 : 24845791 : m_equiv_set = BITMAP_ALLOC (&m_bitmaps);
318 : 24845791 : bitmap_tree_view (m_equiv_set);
319 : 24845791 : obstack_init (&m_chain_obstack);
320 : 24845791 : m_self_equiv.create (0);
321 : 49691582 : m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
322 : 24845791 : m_partial.create (0);
323 : 49691582 : m_partial.safe_grow_cleared (num_ssa_names + 1);
324 : 24845791 : }
325 : :
326 : : // Destruct an equivalency oracle.
327 : :
328 : 24845791 : equiv_oracle::~equiv_oracle ()
329 : : {
330 : 24845791 : m_partial.release ();
331 : 24845791 : m_self_equiv.release ();
332 : 24845791 : obstack_free (&m_chain_obstack, NULL);
333 : 24845791 : m_equiv.release ();
334 : 24845791 : bitmap_obstack_release (&m_bitmaps);
335 : 24845791 : }
336 : :
337 : : // Add a partial equivalence R between OP1 and OP2.
338 : :
339 : : void
340 : 9642732 : equiv_oracle::add_partial_equiv (relation_kind r, tree op1, tree op2)
341 : : {
342 : 9642732 : int v1 = SSA_NAME_VERSION (op1);
343 : 9642732 : int v2 = SSA_NAME_VERSION (op2);
344 : 9642732 : int prec2 = TYPE_PRECISION (TREE_TYPE (op2));
345 : 9642732 : int bits = pe_to_bits (r);
346 : 9642732 : gcc_checking_assert (bits && prec2 >= bits);
347 : :
348 : 19285464 : if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
349 : 26 : m_partial.safe_grow_cleared (num_ssa_names + 1);
350 : 19285464 : gcc_checking_assert (v1 < (int)m_partial.length ()
351 : : && v2 < (int)m_partial.length ());
352 : :
353 : 9642732 : pe_slice &pe1 = m_partial[v1];
354 : 9642732 : pe_slice &pe2 = m_partial[v2];
355 : :
356 : 9642732 : if (pe1.members)
357 : : {
358 : : // If the definition pe1 already has an entry, either the stmt is
359 : : // being re-evaluated, or the def was used before being registered.
360 : : // In either case, if PE2 has an entry, we simply do nothing.
361 : 159058 : if (pe2.members)
362 : : return;
363 : : // If there are no uses of op2, do not register.
364 : 128 : if (has_zero_uses (op2))
365 : : return;
366 : : // PE1 is the LHS and already has members, so everything in the set
367 : : // should be a slice of PE2 rather than PE1.
368 : 128 : pe2.code = pe_min (r, pe1.code);
369 : 128 : pe2.ssa_base = op2;
370 : 128 : pe2.members = pe1.members;
371 : 128 : bitmap_iterator bi;
372 : 128 : unsigned x;
373 : 384 : EXECUTE_IF_SET_IN_BITMAP (pe1.members, 0, x, bi)
374 : : {
375 : 256 : m_partial[x].ssa_base = op2;
376 : 256 : m_partial[x].code = pe_min (m_partial[x].code, pe2.code);
377 : : }
378 : 128 : bitmap_set_bit (pe1.members, v2);
379 : 128 : return;
380 : : }
381 : 9483674 : if (pe2.members)
382 : : {
383 : : // If there are no uses of op1, do not register.
384 : 711164 : if (has_zero_uses (op1))
385 : : return;
386 : 704408 : pe1.ssa_base = pe2.ssa_base;
387 : : // If pe2 is a 16 bit value, but only an 8 bit copy, we can't be any
388 : : // more than an 8 bit equivalence here, so choose MIN value.
389 : 704408 : pe1.code = pe_min (r, pe2.code);
390 : 704408 : pe1.members = pe2.members;
391 : 704408 : bitmap_set_bit (pe1.members, v1);
392 : : }
393 : : else
394 : : {
395 : : // If there are no uses of either operand, do not register.
396 : 8772510 : if (has_zero_uses (op1) || has_zero_uses (op2))
397 : : return;
398 : : // Neither name has an entry, simply create op1 as slice of op2.
399 : 8684683 : pe2.code = bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2)));
400 : 8684683 : if (pe2.code == VREL_VARYING)
401 : : return;
402 : 8636962 : pe2.ssa_base = op2;
403 : 8636962 : pe2.members = BITMAP_ALLOC (&m_bitmaps);
404 : 8636962 : bitmap_set_bit (pe2.members, v2);
405 : 8636962 : pe1.ssa_base = op2;
406 : 8636962 : pe1.code = r;
407 : 8636962 : pe1.members = pe2.members;
408 : 8636962 : bitmap_set_bit (pe1.members, v1);
409 : : }
410 : : }
411 : :
412 : : // Return the set of partial equivalences associated with NAME. The bitmap
413 : : // will be NULL if there are none.
414 : :
415 : : const pe_slice *
416 : 55136110 : equiv_oracle::partial_equiv_set (tree name)
417 : : {
418 : 55136110 : int v = SSA_NAME_VERSION (name);
419 : 110272220 : if (v >= (int)m_partial.length ())
420 : : return NULL;
421 : 55136110 : return &m_partial[v];
422 : : }
423 : :
424 : : // Query if there is a partial equivalence between SSA1 and SSA2. Return
425 : : // VREL_VARYING if there is not one. If BASE is non-null, return the base
426 : : // ssa-name this is a slice of.
427 : :
428 : : relation_kind
429 : 55926162 : equiv_oracle::partial_equiv (tree ssa1, tree ssa2, tree *base) const
430 : : {
431 : 55926162 : int v1 = SSA_NAME_VERSION (ssa1);
432 : 55926162 : int v2 = SSA_NAME_VERSION (ssa2);
433 : :
434 : 111852324 : if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
435 : : return VREL_VARYING;
436 : :
437 : 55926154 : const pe_slice &pe1 = m_partial[v1];
438 : 55926154 : const pe_slice &pe2 = m_partial[v2];
439 : 55926154 : if (pe1.members && pe2.members == pe1.members)
440 : : {
441 : 1041 : if (base)
442 : 0 : *base = pe1.ssa_base;
443 : 1041 : return pe_min (pe1.code, pe2.code);
444 : : }
445 : : return VREL_VARYING;
446 : : }
447 : :
448 : :
449 : : // Find and return the equivalency set for SSA along the dominators of BB.
450 : : // This is the external API.
451 : :
452 : : const_bitmap
453 : 140771666 : equiv_oracle::equiv_set (tree ssa, basic_block bb)
454 : : {
455 : : // Search the dominator tree for an equivalency.
456 : 140771666 : equiv_chain *equiv = find_equiv_dom (ssa, bb);
457 : 140771666 : if (equiv)
458 : 17325144 : return equiv->m_names;
459 : :
460 : : // Otherwise return a cached equiv set containing just this SSA.
461 : 123446522 : unsigned v = SSA_NAME_VERSION (ssa);
462 : 123446522 : if (v >= m_self_equiv.length ())
463 : 14 : m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
464 : :
465 : 123446522 : if (!m_self_equiv[v])
466 : : {
467 : 33789962 : m_self_equiv[v] = BITMAP_ALLOC (&m_bitmaps);
468 : 33789962 : bitmap_set_bit (m_self_equiv[v], v);
469 : : }
470 : 123446522 : return m_self_equiv[v];
471 : : }
472 : :
473 : : // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
474 : :
475 : : relation_kind
476 : 0 : equiv_oracle::query (basic_block bb, tree ssa1, tree ssa2)
477 : : {
478 : : // If the 2 ssa names share the same equiv set, they are equal.
479 : 0 : if (equiv_set (ssa1, bb) == equiv_set (ssa2, bb))
480 : : return VREL_EQ;
481 : :
482 : : // Check if there is a partial equivalence.
483 : 0 : return partial_equiv (ssa1, ssa2);
484 : : }
485 : :
486 : : // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
487 : :
488 : : relation_kind
489 : 0 : equiv_oracle::query (basic_block bb ATTRIBUTE_UNUSED, const_bitmap e1,
490 : : const_bitmap e2)
491 : : {
492 : : // If the 2 ssa names share the same equiv set, they are equal.
493 : 0 : if (bitmap_equal_p (e1, e2))
494 : 0 : return VREL_EQ;
495 : : return VREL_VARYING;
496 : : }
497 : :
498 : : // If SSA has an equivalence in block BB, find and return it.
499 : : // Otherwise return NULL.
500 : :
501 : : equiv_chain *
502 : 105520507 : equiv_oracle::find_equiv_block (unsigned ssa, int bb) const
503 : : {
504 : 211041014 : if (bb >= (int)m_equiv.length () || !m_equiv[bb])
505 : : return NULL;
506 : :
507 : 46958491 : return m_equiv[bb]->find (ssa);
508 : : }
509 : :
510 : : // Starting at block BB, walk the dominator chain looking for the nearest
511 : : // equivalence set containing NAME.
512 : :
513 : : equiv_chain *
514 : 156758493 : equiv_oracle::find_equiv_dom (tree name, basic_block bb) const
515 : : {
516 : 156758493 : unsigned v = SSA_NAME_VERSION (name);
517 : : // Short circuit looking for names which have no equivalences.
518 : : // Saves time looking for something which does not exist.
519 : 156758493 : if (!bitmap_bit_p (m_equiv_set, v))
520 : : return NULL;
521 : :
522 : : // NAME has at least once equivalence set, check to see if it has one along
523 : : // the dominator tree.
524 : 106631600 : for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
525 : : {
526 : 105520507 : equiv_chain *ptr = find_equiv_block (v, bb->index);
527 : 105520507 : if (ptr)
528 : : return ptr;
529 : : }
530 : : return NULL;
531 : : }
532 : :
533 : : // Register equivalence between ssa_name V and set EQUIV in block BB,
534 : :
535 : : bitmap
536 : 84745 : equiv_oracle::register_equiv (basic_block bb, unsigned v, equiv_chain *equiv)
537 : : {
538 : : // V will have an equivalency now.
539 : 84745 : bitmap_set_bit (m_equiv_set, v);
540 : :
541 : : // If that equiv chain is in this block, simply use it.
542 : 84745 : if (equiv->m_bb == bb)
543 : : {
544 : 25073 : bitmap_set_bit (equiv->m_names, v);
545 : 25073 : bitmap_set_bit (m_equiv[bb->index]->m_names, v);
546 : 25073 : return NULL;
547 : : }
548 : :
549 : : // Otherwise create an equivalence for this block which is a copy
550 : : // of equiv, the add V to the set.
551 : 59672 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
552 : 59672 : valid_equivs (b, equiv->m_names, bb);
553 : 59672 : bitmap_set_bit (b, v);
554 : 59672 : return b;
555 : : }
556 : :
557 : : // Register equivalence between set equiv_1 and equiv_2 in block BB.
558 : : // Return NULL if either name can be merged with the other. Otherwise
559 : : // return a pointer to the combined bitmap of names. This allows the
560 : : // caller to do any setup required for a new element.
561 : :
562 : : bitmap
563 : 3963501 : equiv_oracle::register_equiv (basic_block bb, equiv_chain *equiv_1,
564 : : equiv_chain *equiv_2)
565 : : {
566 : : // If equiv_1 is already in BB, use it as the combined set.
567 : 3963501 : if (equiv_1->m_bb == bb)
568 : : {
569 : 2291334 : valid_equivs (equiv_1->m_names, equiv_2->m_names, bb);
570 : : // Its hard to delete from a single linked list, so
571 : : // just clear the second one.
572 : 2291334 : if (equiv_2->m_bb == bb)
573 : 307654 : bitmap_clear (equiv_2->m_names);
574 : : else
575 : : // Ensure the new names are in the summary for BB.
576 : 1983680 : bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_1->m_names);
577 : 2291334 : return NULL;
578 : : }
579 : : // If equiv_2 is in BB, use it for the combined set.
580 : 1672167 : if (equiv_2->m_bb == bb)
581 : : {
582 : 3209 : valid_equivs (equiv_2->m_names, equiv_1->m_names, bb);
583 : : // Ensure the new names are in the summary.
584 : 3209 : bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_2->m_names);
585 : 3209 : return NULL;
586 : : }
587 : :
588 : : // At this point, neither equivalence is from this block.
589 : 1668958 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
590 : 1668958 : valid_equivs (b, equiv_1->m_names, bb);
591 : 1668958 : valid_equivs (b, equiv_2->m_names, bb);
592 : 1668958 : return b;
593 : : }
594 : :
595 : : // Create an equivalency set containing only SSA in its definition block.
596 : : // This is done the first time SSA is registered in an equivalency and blocks
597 : : // any DOM searches past the definition.
598 : :
599 : : void
600 : 7366599 : equiv_oracle::register_initial_def (tree ssa)
601 : : {
602 : 7366599 : if (SSA_NAME_IS_DEFAULT_DEF (ssa))
603 : : return;
604 : 7272628 : basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (ssa));
605 : :
606 : : // If defining stmt is not in the IL, simply return.
607 : 7272628 : if (!bb)
608 : : return;
609 : 7272627 : gcc_checking_assert (!find_equiv_dom (ssa, bb));
610 : :
611 : 7272627 : unsigned v = SSA_NAME_VERSION (ssa);
612 : 7272627 : bitmap_set_bit (m_equiv_set, v);
613 : 7272627 : bitmap equiv_set = BITMAP_ALLOC (&m_bitmaps);
614 : 7272627 : bitmap_set_bit (equiv_set, v);
615 : 7272627 : add_equiv_to_block (bb, equiv_set);
616 : : }
617 : :
618 : : // Register an equivalence between SSA1 and SSA2 in block BB.
619 : : // The equivalence oracle maintains a vector of equivalencies indexed by basic
620 : : // block. When an equivalence between SSA1 and SSA2 is registered in block BB,
621 : : // a query is made as to what equivalences both names have already, and
622 : : // any preexisting equivalences are merged to create a single equivalence
623 : : // containing all the ssa_names in this basic block.
624 : :
625 : : void
626 : 13999832 : equiv_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
627 : : {
628 : : // Process partial equivalencies.
629 : 13999832 : if (relation_partial_equiv_p (k))
630 : : {
631 : 9642732 : add_partial_equiv (k, ssa1, ssa2);
632 : 9642732 : return;
633 : : }
634 : : // Only handle equality relations.
635 : 4357100 : if (k != VREL_EQ)
636 : : return;
637 : :
638 : 4357100 : unsigned v1 = SSA_NAME_VERSION (ssa1);
639 : 4357100 : unsigned v2 = SSA_NAME_VERSION (ssa2);
640 : :
641 : : // If this is the first time an ssa_name has an equivalency registered
642 : : // create a self-equivalency record in the def block.
643 : 4357100 : if (!bitmap_bit_p (m_equiv_set, v1))
644 : 3930627 : register_initial_def (ssa1);
645 : 4357100 : if (!bitmap_bit_p (m_equiv_set, v2))
646 : 3435972 : register_initial_def (ssa2);
647 : :
648 : 4357100 : equiv_chain *equiv_1 = find_equiv_dom (ssa1, bb);
649 : 4357100 : equiv_chain *equiv_2 = find_equiv_dom (ssa2, bb);
650 : :
651 : : // Check if they are the same set
652 : 4357100 : if (equiv_1 && equiv_1 == equiv_2)
653 : : return;
654 : :
655 : 4059710 : bitmap equiv_set;
656 : :
657 : : // Case where we have 2 SSA_NAMEs that are not in any set.
658 : 4059710 : if (!equiv_1 && !equiv_2)
659 : : {
660 : 11464 : bitmap_set_bit (m_equiv_set, v1);
661 : 11464 : bitmap_set_bit (m_equiv_set, v2);
662 : :
663 : 11464 : equiv_set = BITMAP_ALLOC (&m_bitmaps);
664 : 11464 : bitmap_set_bit (equiv_set, v1);
665 : 11464 : bitmap_set_bit (equiv_set, v2);
666 : : }
667 : 4048246 : else if (!equiv_1 && equiv_2)
668 : 23175 : equiv_set = register_equiv (bb, v1, equiv_2);
669 : 4025071 : else if (equiv_1 && !equiv_2)
670 : 61570 : equiv_set = register_equiv (bb, v2, equiv_1);
671 : : else
672 : 3963501 : equiv_set = register_equiv (bb, equiv_1, equiv_2);
673 : :
674 : : // A non-null return is a bitmap that is to be added to the current
675 : : // block as a new equivalence.
676 : 4059710 : if (!equiv_set)
677 : : return;
678 : :
679 : 1740094 : add_equiv_to_block (bb, equiv_set);
680 : : }
681 : :
682 : : // Add an equivalency record in block BB containing bitmap EQUIV_SET.
683 : : // Note the internal caller is responsible for allocating EQUIV_SET properly.
684 : :
685 : : void
686 : 9012721 : equiv_oracle::add_equiv_to_block (basic_block bb, bitmap equiv_set)
687 : : {
688 : 9012721 : equiv_chain *ptr;
689 : :
690 : : // Check if this is the first time a block has an equivalence added.
691 : : // and create a header block. And set the summary for this block.
692 : 9012721 : limit_check (bb);
693 : 9012721 : if (!m_equiv[bb->index])
694 : : {
695 : 5704031 : ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
696 : : sizeof (equiv_chain));
697 : 5704031 : ptr->m_names = BITMAP_ALLOC (&m_bitmaps);
698 : 5704031 : bitmap_copy (ptr->m_names, equiv_set);
699 : 5704031 : ptr->m_bb = bb;
700 : 5704031 : ptr->m_next = NULL;
701 : 5704031 : m_equiv[bb->index] = ptr;
702 : : }
703 : :
704 : : // Now create the element for this equiv set and initialize it.
705 : 9012721 : ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack, sizeof (equiv_chain));
706 : 9012721 : ptr->m_names = equiv_set;
707 : 9012721 : ptr->m_bb = bb;
708 : 18025442 : gcc_checking_assert (bb->index < (int)m_equiv.length ());
709 : 9012721 : ptr->m_next = m_equiv[bb->index]->m_next;
710 : 9012721 : m_equiv[bb->index]->m_next = ptr;
711 : 9012721 : bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_set);
712 : 9012721 : }
713 : :
714 : : // Make sure the BB vector is big enough and grow it if needed.
715 : :
716 : : void
717 : 9012721 : equiv_oracle::limit_check (basic_block bb)
718 : : {
719 : 9012721 : int i = (bb) ? bb->index : last_basic_block_for_fn (cfun);
720 : 18025442 : if (i >= (int)m_equiv.length ())
721 : 1 : m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
722 : 9012721 : }
723 : :
724 : : // Dump the equivalence sets in BB to file F.
725 : :
726 : : void
727 : 251 : equiv_oracle::dump (FILE *f, basic_block bb) const
728 : : {
729 : 502 : if (bb->index >= (int)m_equiv.length ())
730 : : return;
731 : : // Process equivalences.
732 : 251 : if (m_equiv[bb->index])
733 : : {
734 : 9 : equiv_chain *ptr = m_equiv[bb->index]->m_next;
735 : 20 : for (; ptr; ptr = ptr->m_next)
736 : 11 : ptr->dump (f);
737 : : }
738 : : // Look for partial equivalences defined in this block..
739 : 13760 : for (unsigned i = 0; i < num_ssa_names; i++)
740 : : {
741 : 13509 : tree name = ssa_name (i);
742 : 18782 : if (!gimple_range_ssa_p (name) || !SSA_NAME_DEF_STMT (name))
743 : 8236 : continue;
744 : 5273 : if (i >= m_partial.length ())
745 : : break;
746 : 5273 : tree base = m_partial[i].ssa_base;
747 : 5273 : if (base && name != base && gimple_bb (SSA_NAME_DEF_STMT (name)) == bb)
748 : : {
749 : 40 : relation_kind k = partial_equiv (name, base);
750 : 40 : if (k != VREL_VARYING)
751 : : {
752 : 40 : value_relation vr (k, name, base);
753 : 40 : fprintf (f, "Partial equiv ");
754 : 40 : vr.dump (f);
755 : 40 : fputc ('\n',f);
756 : : }
757 : : }
758 : : }
759 : : }
760 : :
761 : : // Dump all equivalence sets known to the oracle.
762 : :
763 : : void
764 : 0 : equiv_oracle::dump (FILE *f) const
765 : : {
766 : 0 : fprintf (f, "Equivalency dump\n");
767 : 0 : for (unsigned i = 0; i < m_equiv.length (); i++)
768 : 0 : if (m_equiv[i] && BASIC_BLOCK_FOR_FN (cfun, i))
769 : : {
770 : 0 : fprintf (f, "BB%d\n", i);
771 : 0 : dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
772 : : }
773 : 0 : }
774 : :
775 : :
776 : : // --------------------------------------------------------------------------
777 : :
778 : : // Adjust the relation by Swapping the operands and relation.
779 : :
780 : : void
781 : 0 : value_relation::swap ()
782 : : {
783 : 0 : related = relation_swap (related);
784 : 0 : tree tmp = name1;
785 : 0 : name1 = name2;
786 : 0 : name2 = tmp;
787 : 0 : }
788 : :
789 : : // Perform an intersection between 2 relations. *this &&= p.
790 : : // Return false if the relations cannot be intersected.
791 : :
792 : : bool
793 : 3825128 : value_relation::intersect (value_relation &p)
794 : : {
795 : : // Save previous value
796 : 3825128 : relation_kind old = related;
797 : :
798 : 3825128 : if (p.op1 () == op1 () && p.op2 () == op2 ())
799 : 3824233 : related = relation_intersect (kind (), p.kind ());
800 : 895 : else if (p.op2 () == op1 () && p.op1 () == op2 ())
801 : 895 : related = relation_intersect (kind (), relation_swap (p.kind ()));
802 : : else
803 : : return false;
804 : :
805 : 3825128 : return old != related;
806 : : }
807 : :
808 : : // Perform a union between 2 relations. *this ||= p.
809 : :
810 : : bool
811 : 0 : value_relation::union_ (value_relation &p)
812 : : {
813 : : // Save previous value
814 : 0 : relation_kind old = related;
815 : :
816 : 0 : if (p.op1 () == op1 () && p.op2 () == op2 ())
817 : 0 : related = relation_union (kind(), p.kind());
818 : 0 : else if (p.op2 () == op1 () && p.op1 () == op2 ())
819 : 0 : related = relation_union (kind(), relation_swap (p.kind ()));
820 : : else
821 : : return false;
822 : :
823 : 0 : return old != related;
824 : : }
825 : :
826 : : // Identify and apply any transitive relations between REL
827 : : // and THIS. Return true if there was a transformation.
828 : :
829 : : bool
830 : 13071762 : value_relation::apply_transitive (const value_relation &rel)
831 : : {
832 : 13071762 : relation_kind k = VREL_VARYING;
833 : :
834 : : // Identify any common operand, and normalize the relations to
835 : : // the form : A < B B < C produces A < C
836 : 13071762 : if (rel.op1 () == name2)
837 : : {
838 : : // A < B B < C
839 : 2187966 : if (rel.op2 () == name1)
840 : : return false;
841 : 2118387 : k = relation_transitive (kind (), rel.kind ());
842 : 2118387 : if (k != VREL_VARYING)
843 : : {
844 : 836647 : related = k;
845 : 836647 : name2 = rel.op2 ();
846 : 836647 : return true;
847 : : }
848 : : }
849 : 10883796 : else if (rel.op1 () == name1)
850 : : {
851 : : // B > A B < C
852 : 6413244 : if (rel.op2 () == name2)
853 : : return false;
854 : 1772821 : k = relation_transitive (relation_swap (kind ()), rel.kind ());
855 : 1772821 : if (k != VREL_VARYING)
856 : : {
857 : 445344 : related = k;
858 : 445344 : name1 = name2;
859 : 445344 : name2 = rel.op2 ();
860 : 445344 : return true;
861 : : }
862 : : }
863 : 4470552 : else if (rel.op2 () == name2)
864 : : {
865 : : // A < B C > B
866 : 3858896 : if (rel.op1 () == name1)
867 : : return false;
868 : 3858896 : k = relation_transitive (kind (), relation_swap (rel.kind ()));
869 : 3858896 : if (k != VREL_VARYING)
870 : : {
871 : 510527 : related = k;
872 : 510527 : name2 = rel.op1 ();
873 : 510527 : return true;
874 : : }
875 : : }
876 : 611656 : else if (rel.op2 () == name1)
877 : : {
878 : : // B > A C > B
879 : 611656 : if (rel.op1 () == name2)
880 : : return false;
881 : 611656 : k = relation_transitive (relation_swap (kind ()),
882 : : relation_swap (rel.kind ()));
883 : 611656 : if (k != VREL_VARYING)
884 : : {
885 : 236886 : related = k;
886 : 236886 : name1 = name2;
887 : 236886 : name2 = rel.op1 ();
888 : 236886 : return true;
889 : : }
890 : : }
891 : : return false;
892 : : }
893 : :
894 : : // Create a trio from this value relation given LHS, OP1 and OP2.
895 : :
896 : : relation_trio
897 : 47469814 : value_relation::create_trio (tree lhs, tree op1, tree op2)
898 : : {
899 : 47469814 : relation_kind lhs_1;
900 : 47469814 : if (lhs == name1 && op1 == name2)
901 : 55927 : lhs_1 = related;
902 : 47413887 : else if (lhs == name2 && op1 == name1)
903 : 130927 : lhs_1 = relation_swap (related);
904 : : else
905 : : lhs_1 = VREL_VARYING;
906 : :
907 : 47469814 : relation_kind lhs_2;
908 : 47469814 : if (lhs == name1 && op2 == name2)
909 : 49386 : lhs_2 = related;
910 : 47420428 : else if (lhs == name2 && op2 == name1)
911 : 160711 : lhs_2 = relation_swap (related);
912 : : else
913 : : lhs_2 = VREL_VARYING;
914 : :
915 : 47469814 : relation_kind op_op;
916 : 47469814 : if (op1 == name1 && op2 == name2)
917 : 33947132 : op_op = related;
918 : 13522682 : else if (op1 == name2 && op2 == name1)
919 : 0 : op_op = relation_swap (related);
920 : 13522682 : else if (op1 == op2)
921 : : op_op = VREL_EQ;
922 : : else
923 : 13507404 : op_op = VREL_VARYING;
924 : :
925 : 47469814 : return relation_trio (lhs_1, lhs_2, op_op);
926 : : }
927 : :
928 : : // Dump the relation to file F.
929 : :
930 : : void
931 : 1037 : value_relation::dump (FILE *f) const
932 : : {
933 : 1037 : if (!name1 || !name2)
934 : : {
935 : 0 : fprintf (f, "no relation registered");
936 : 0 : return;
937 : : }
938 : 1037 : fputc ('(', f);
939 : 1037 : print_generic_expr (f, op1 (), TDF_SLIM);
940 : 1037 : print_relation (f, kind ());
941 : 1037 : print_generic_expr (f, op2 (), TDF_SLIM);
942 : 1037 : fputc(')', f);
943 : : }
944 : :
945 : : // This container is used to link relations in a chain.
946 : :
947 : : class relation_chain : public value_relation
948 : : {
949 : : public:
950 : : relation_chain *m_next;
951 : : };
952 : :
953 : : // Given relation record PTR in block BB, return the next relation in the
954 : : // list. If PTR is NULL, retreive the first relation in BB.
955 : : // If NAME is sprecified, return only relations which include NAME.
956 : : // Return NULL when there are no relations left.
957 : :
958 : : relation_chain *
959 : 84 : dom_oracle::next_relation (basic_block bb, relation_chain *ptr,
960 : : tree name) const
961 : : {
962 : 84 : relation_chain *p;
963 : : // No value_relation pointer is used to intialize the iterator.
964 : 84 : if (!ptr)
965 : : {
966 : 37 : int bbi = bb->index;
967 : 74 : if (bbi >= (int)m_relations.length())
968 : : return NULL;
969 : : else
970 : 37 : p = m_relations[bbi].m_head;
971 : : }
972 : : else
973 : 47 : p = ptr->m_next;
974 : :
975 : 84 : if (name)
976 : 0 : for ( ; p; p = p->m_next)
977 : 0 : if (p->op1 () == name || p->op2 () == name)
978 : : break;
979 : : return p;
980 : : }
981 : :
982 : : // Instatiate a block relation iterator to iterate over the relations
983 : : // on exit from block BB in ORACLE. Limit this to relations involving NAME
984 : : // if specified. Return the first such relation in VR if there is one.
985 : :
986 : 37 : block_relation_iterator::block_relation_iterator (const relation_oracle *oracle,
987 : : basic_block bb,
988 : : value_relation &vr,
989 : 37 : tree name)
990 : : {
991 : 37 : m_oracle = oracle;
992 : 37 : m_bb = bb;
993 : 37 : m_name = name;
994 : 37 : m_ptr = oracle->next_relation (bb, NULL, m_name);
995 : 37 : if (m_ptr)
996 : : {
997 : 37 : m_done = false;
998 : 37 : vr = *m_ptr;
999 : : }
1000 : : else
1001 : 0 : m_done = true;
1002 : 37 : }
1003 : :
1004 : : // Retreive the next relation from the iterator and return it in VR.
1005 : :
1006 : : void
1007 : 47 : block_relation_iterator::get_next_relation (value_relation &vr)
1008 : : {
1009 : 47 : m_ptr = m_oracle->next_relation (m_bb, m_ptr, m_name);
1010 : 47 : if (m_ptr)
1011 : : {
1012 : 10 : vr = *m_ptr;
1013 : 10 : if (m_name)
1014 : : {
1015 : 0 : if (vr.op1 () != m_name)
1016 : : {
1017 : 0 : gcc_checking_assert (vr.op2 () == m_name);
1018 : 0 : vr.swap ();
1019 : : }
1020 : : }
1021 : : }
1022 : : else
1023 : 37 : m_done = true;
1024 : 47 : }
1025 : :
1026 : : // ------------------------------------------------------------------------
1027 : :
1028 : : // Find the relation between any ssa_name in B1 and any name in B2 in LIST.
1029 : : // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1030 : :
1031 : : relation_kind
1032 : 281820035 : relation_chain_head::find_relation (const_bitmap b1, const_bitmap b2) const
1033 : : {
1034 : 281820035 : if (!m_names)
1035 : : return VREL_VARYING;
1036 : :
1037 : : // If both b1 and b2 aren't referenced in this block, cant be a relation
1038 : 173434522 : if (!bitmap_intersect_p (m_names, b1) || !bitmap_intersect_p (m_names, b2))
1039 : 169002903 : return VREL_VARYING;
1040 : :
1041 : : // Search for the first relation that contains BOTH an element from B1
1042 : : // and B2, and return that relation.
1043 : 14583479 : for (relation_chain *ptr = m_head; ptr ; ptr = ptr->m_next)
1044 : : {
1045 : 12846146 : unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1046 : 12846146 : unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1047 : 12846146 : if (bitmap_bit_p (b1, op1) && bitmap_bit_p (b2, op2))
1048 : 2566033 : return ptr->kind ();
1049 : 10280113 : if (bitmap_bit_p (b1, op2) && bitmap_bit_p (b2, op1))
1050 : 128253 : return relation_swap (ptr->kind ());
1051 : : }
1052 : :
1053 : : return VREL_VARYING;
1054 : : }
1055 : :
1056 : : // Instantiate a relation oracle.
1057 : :
1058 : 24845791 : dom_oracle::dom_oracle (bool do_trans_p)
1059 : : {
1060 : 24845791 : m_do_trans_p = do_trans_p;
1061 : 24845791 : m_relations.create (0);
1062 : 24845791 : m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1063 : 24845791 : m_relation_set = BITMAP_ALLOC (&m_bitmaps);
1064 : 24845791 : m_tmp = BITMAP_ALLOC (&m_bitmaps);
1065 : 24845791 : m_tmp2 = BITMAP_ALLOC (&m_bitmaps);
1066 : 24845791 : }
1067 : :
1068 : : // Destruct a relation oracle.
1069 : :
1070 : 49691582 : dom_oracle::~dom_oracle ()
1071 : : {
1072 : 24845791 : m_relations.release ();
1073 : 49691582 : }
1074 : :
1075 : : // Register relation K between ssa_name OP1 and OP2 on STMT.
1076 : :
1077 : : void
1078 : 30334329 : relation_oracle::record (gimple *stmt, relation_kind k, tree op1, tree op2)
1079 : : {
1080 : 30334329 : gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1081 : 30334329 : gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1082 : 30334329 : gcc_checking_assert (stmt && gimple_bb (stmt));
1083 : :
1084 : : // Don't register lack of a relation.
1085 : 30334329 : if (k == VREL_VARYING)
1086 : : return;
1087 : :
1088 : 30334329 : if (dump_file && (dump_flags & TDF_DETAILS))
1089 : : {
1090 : 510 : value_relation vr (k, op1, op2);
1091 : 510 : fprintf (dump_file, " Registering value_relation ");
1092 : 510 : vr.dump (dump_file);
1093 : 510 : fprintf (dump_file, " (bb%d) at ", gimple_bb (stmt)->index);
1094 : 510 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1095 : : }
1096 : :
1097 : : // If an equivalence is being added between a PHI and one of its arguments
1098 : : // make sure that that argument is not defined in the same block.
1099 : : // This can happen along back edges and the equivalence will not be
1100 : : // applicable as it would require a use before def.
1101 : 30334329 : if (k == VREL_EQ && is_a<gphi *> (stmt))
1102 : : {
1103 : 2200696 : tree phi_def = gimple_phi_result (stmt);
1104 : 2200696 : gcc_checking_assert (phi_def == op1 || phi_def == op2);
1105 : 2200696 : tree arg = op2;
1106 : 2200696 : if (phi_def == op2)
1107 : 0 : arg = op1;
1108 : 2200696 : if (gimple_bb (stmt) == gimple_bb (SSA_NAME_DEF_STMT (arg)))
1109 : : {
1110 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1111 : : {
1112 : 0 : fprintf (dump_file, " Not registered due to ");
1113 : 0 : print_generic_expr (dump_file, arg, TDF_SLIM);
1114 : 0 : fprintf (dump_file, " being defined in the same block.\n");
1115 : : }
1116 : 0 : return;
1117 : : }
1118 : : }
1119 : 30334329 : record (gimple_bb (stmt), k, op1, op2);
1120 : : }
1121 : :
1122 : : // Register relation K between ssa_name OP1 and OP2 on edge E.
1123 : :
1124 : : void
1125 : 6436721 : relation_oracle::record (edge e, relation_kind k, tree op1, tree op2)
1126 : : {
1127 : 6436721 : gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1128 : 6436721 : gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1129 : :
1130 : : // Do not register lack of relation, or blocks which have more than
1131 : : // edge E for a predecessor.
1132 : 6436721 : if (k == VREL_VARYING || !single_pred_p (e->dest))
1133 : : return;
1134 : :
1135 : 6436721 : if (dump_file && (dump_flags & TDF_DETAILS))
1136 : : {
1137 : 116 : value_relation vr (k, op1, op2);
1138 : 116 : fprintf (dump_file, " Registering value_relation ");
1139 : 116 : vr.dump (dump_file);
1140 : 116 : fprintf (dump_file, " on (%d->%d)\n", e->src->index, e->dest->index);
1141 : : }
1142 : :
1143 : 6436721 : record (e->dest, k, op1, op2);
1144 : : }
1145 : :
1146 : : // Register relation K between OP! and OP2 in block BB.
1147 : : // This creates the record and searches for existing records in the dominator
1148 : : // tree to merge with.
1149 : :
1150 : : void
1151 : 36203010 : dom_oracle::record (basic_block bb, relation_kind k, tree op1, tree op2)
1152 : : {
1153 : : // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1154 : : // and no other relation makes sense.
1155 : 36203010 : if (op1 == op2)
1156 : : return;
1157 : :
1158 : : // Equivalencies are handled by the equivalence oracle.
1159 : 36196186 : if (relation_equiv_p (k))
1160 : 13999832 : equiv_oracle::record (bb, k, op1, op2);
1161 : : else
1162 : : {
1163 : : // if neither op1 nor op2 are in a relation before this is registered,
1164 : : // there will be no transitive.
1165 : 22196354 : bool check = bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op1))
1166 : 37605013 : || bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op2));
1167 : 22196354 : relation_chain *ptr = set_one_relation (bb, k, op1, op2);
1168 : 22196354 : if (ptr && check
1169 : 22196354 : && (m_relations[bb->index].m_num_relations
1170 : 6134579 : < param_relation_block_limit))
1171 : 6134431 : register_transitives (bb, *ptr);
1172 : : }
1173 : : }
1174 : :
1175 : : // Register relation K between OP! and OP2 in block BB.
1176 : : // This creates the record and searches for existing records in the dominator
1177 : : // tree to merge with. Return the record, or NULL if no record was created.
1178 : :
1179 : : relation_chain *
1180 : 24225758 : dom_oracle::set_one_relation (basic_block bb, relation_kind k, tree op1,
1181 : : tree op2)
1182 : : {
1183 : 24225758 : gcc_checking_assert (k != VREL_VARYING && k != VREL_EQ);
1184 : :
1185 : 24225758 : value_relation vr(k, op1, op2);
1186 : 24225758 : int bbi = bb->index;
1187 : :
1188 : 48451516 : if (bbi >= (int)m_relations.length())
1189 : 25 : m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1190 : :
1191 : : // Summary bitmap indicating what ssa_names have relations in this BB.
1192 : 24225758 : bitmap bm = m_relations[bbi].m_names;
1193 : 24225758 : if (!bm)
1194 : 13732736 : bm = m_relations[bbi].m_names = BITMAP_ALLOC (&m_bitmaps);
1195 : 24225758 : unsigned v1 = SSA_NAME_VERSION (op1);
1196 : 24225758 : unsigned v2 = SSA_NAME_VERSION (op2);
1197 : :
1198 : 24225758 : relation_kind curr;
1199 : 24225758 : relation_chain *ptr;
1200 : 24225758 : curr = find_relation_block (bbi, v1, v2, &ptr);
1201 : : // There is an existing relation in this block, just intersect with it.
1202 : 24225758 : if (curr != VREL_VARYING)
1203 : : {
1204 : 3825128 : if (dump_file && (dump_flags & TDF_DETAILS))
1205 : : {
1206 : 18 : fprintf (dump_file, " Intersecting with existing ");
1207 : 18 : ptr->dump (dump_file);
1208 : : }
1209 : : // Check into whether we can simply replace the relation rather than
1210 : : // intersecting it. This may help with some optimistic iterative
1211 : : // updating algorithms.
1212 : 3825128 : bool new_rel = ptr->intersect (vr);
1213 : 3825128 : if (dump_file && (dump_flags & TDF_DETAILS))
1214 : : {
1215 : 18 : fprintf (dump_file, " to produce ");
1216 : 18 : ptr->dump (dump_file);
1217 : 32 : fprintf (dump_file, " %s.\n", new_rel ? "Updated" : "No Change");
1218 : : }
1219 : : // If there was no change, return no record..
1220 : 3825128 : if (!new_rel)
1221 : : return NULL;
1222 : : }
1223 : : else
1224 : : {
1225 : 20400630 : if (m_relations[bbi].m_num_relations >= param_relation_block_limit)
1226 : : {
1227 : 102852 : if (dump_file && (dump_flags & TDF_DETAILS))
1228 : 0 : fprintf (dump_file, " Not registered due to bb being full\n");
1229 : 102852 : return NULL;
1230 : : }
1231 : 20297778 : m_relations[bbi].m_num_relations++;
1232 : : // Check for an existing relation further up the DOM chain.
1233 : : // By including dominating relations, The first one found in any search
1234 : : // will be the aggregate of all the previous ones.
1235 : 20297778 : curr = find_relation_dom (bb, v1, v2);
1236 : 20297778 : if (curr != VREL_VARYING)
1237 : 624927 : k = relation_intersect (curr, k);
1238 : :
1239 : 20297778 : bitmap_set_bit (bm, v1);
1240 : 20297778 : bitmap_set_bit (bm, v2);
1241 : 20297778 : bitmap_set_bit (m_relation_set, v1);
1242 : 20297778 : bitmap_set_bit (m_relation_set, v2);
1243 : :
1244 : 20297778 : ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1245 : : sizeof (relation_chain));
1246 : 20297778 : ptr->set_relation (k, op1, op2);
1247 : 20297778 : ptr->m_next = m_relations[bbi].m_head;
1248 : 20297778 : m_relations[bbi].m_head = ptr;
1249 : : }
1250 : 20487943 : return ptr;
1251 : : }
1252 : :
1253 : : // Starting at ROOT_BB search the DOM tree looking for relations which
1254 : : // may produce transitive relations to RELATION. EQUIV1 and EQUIV2 are
1255 : : // bitmaps for op1/op2 and any of their equivalences that should also be
1256 : : // considered.
1257 : :
1258 : : void
1259 : 6134431 : dom_oracle::register_transitives (basic_block root_bb,
1260 : : const value_relation &relation)
1261 : : {
1262 : : // Only register transitives if they are requested.
1263 : 6134431 : if (!m_do_trans_p)
1264 : : return;
1265 : 6134421 : basic_block bb;
1266 : : // Only apply transitives to certain kinds of operations.
1267 : 6134421 : switch (relation.kind ())
1268 : : {
1269 : 4482267 : case VREL_LE:
1270 : 4482267 : case VREL_LT:
1271 : 4482267 : case VREL_GT:
1272 : 4482267 : case VREL_GE:
1273 : 4482267 : break;
1274 : : default:
1275 : : return;
1276 : : }
1277 : :
1278 : 4482267 : const_bitmap equiv1 = equiv_set (relation.op1 (), root_bb);
1279 : 4482267 : const_bitmap equiv2 = equiv_set (relation.op2 (), root_bb);
1280 : :
1281 : 4482267 : const unsigned work_budget = param_transitive_relations_work_bound;
1282 : 4482267 : unsigned avail_budget = work_budget;
1283 : 86222429 : for (bb = root_bb; bb;
1284 : : /* Advancing to the next immediate dominator eats from the budget,
1285 : : if none is left after that there's no point to continue. */
1286 : : bb = (--avail_budget > 0
1287 : 81795677 : ? get_immediate_dominator (CDI_DOMINATORS, bb) : nullptr))
1288 : : {
1289 : 81873703 : int bbi = bb->index;
1290 : 163747406 : if (bbi >= (int)m_relations.length())
1291 : 4666 : continue;
1292 : 81869037 : const_bitmap bm = m_relations[bbi].m_names;
1293 : 81869037 : if (!bm)
1294 : 57745816 : continue;
1295 : 24123221 : if (!bitmap_intersect_p (bm, equiv1) && !bitmap_intersect_p (bm, equiv2))
1296 : 16015519 : continue;
1297 : : // At least one of the 2 ops has a relation in this block.
1298 : 8107702 : relation_chain *ptr;
1299 : 38356408 : for (ptr = m_relations[bbi].m_head; ptr ; ptr = ptr->m_next)
1300 : : {
1301 : : // In the presence of an equivalence, 2 operands may do not
1302 : : // naturally match. ie with equivalence a_2 == b_3
1303 : : // given c_1 < a_2 && b_3 < d_4
1304 : : // convert the second relation (b_3 < d_4) to match any
1305 : : // equivalences to found in the first relation.
1306 : : // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
1307 : : // transitive operation: c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
1308 : :
1309 : 30326732 : tree r1, r2;
1310 : 30326732 : tree p1 = ptr->op1 ();
1311 : 30326732 : tree p2 = ptr->op2 ();
1312 : : // Find which equivalence is in the first operand.
1313 : 30326732 : if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p1)))
1314 : : r1 = p1;
1315 : 23912925 : else if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p2)))
1316 : : r1 = p2;
1317 : : else
1318 : 23231519 : r1 = NULL_TREE;
1319 : :
1320 : : // Find which equivalence is in the second operand.
1321 : 30326732 : if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p1)))
1322 : : r2 = p1;
1323 : 28138203 : else if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p2)))
1324 : : r2 = p2;
1325 : : else
1326 : 19638713 : r2 = NULL_TREE;
1327 : :
1328 : : // Ignore if both NULL (not relevant relation) or the same,
1329 : 30326732 : if (r1 == r2)
1330 : : ;
1331 : :
1332 : : else
1333 : : {
1334 : : // Any operand not an equivalence, just take the real operand.
1335 : 13071762 : if (!r1)
1336 : 5977283 : r1 = relation.op1 ();
1337 : 13071762 : if (!r2)
1338 : 2384477 : r2 = relation.op2 ();
1339 : :
1340 : 13071762 : value_relation nr (relation.kind (), r1, r2);
1341 : 13071762 : if (nr.apply_transitive (*ptr))
1342 : : {
1343 : : // If the new relation is already present we know any
1344 : : // further processing is already reflected above it.
1345 : : // When we ran into the limit of relations on root_bb
1346 : : // we can give up as well.
1347 : 2029404 : if (!set_one_relation (root_bb, nr.kind (),
1348 : : nr.op1 (), nr.op2 ()))
1349 : 71297 : return;
1350 : 1958107 : if (dump_file && (dump_flags & TDF_DETAILS))
1351 : : {
1352 : 2 : fprintf (dump_file,
1353 : : " Registering transitive relation ");
1354 : 2 : nr.dump (dump_file);
1355 : 2 : fputc ('\n', dump_file);
1356 : : }
1357 : : }
1358 : : }
1359 : : /* Processed one relation, abort if we've eaten up our budget. */
1360 : 30255435 : if (--avail_budget == 0)
1361 : : return;
1362 : : }
1363 : : }
1364 : : }
1365 : :
1366 : : // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
1367 : : // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1368 : :
1369 : : relation_kind
1370 : 233082888 : dom_oracle::find_relation_block (unsigned bb, const_bitmap b1,
1371 : : const_bitmap b2) const
1372 : : {
1373 : 233082888 : if (bb >= m_relations.length())
1374 : : return VREL_VARYING;
1375 : :
1376 : 233081397 : return m_relations[bb].find_relation (b1, b2);
1377 : : }
1378 : :
1379 : : // Search the DOM tree for a relation between an element of equivalency set B1
1380 : : // and B2, starting with block BB.
1381 : :
1382 : : relation_kind
1383 : 16921160 : dom_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1384 : : {
1385 : 16921160 : relation_kind r;
1386 : 16921160 : if (bitmap_equal_p (b1, b2))
1387 : : return VREL_EQ;
1388 : :
1389 : : // If either name does not occur in a relation anywhere, there isn't one.
1390 : 16921160 : if (!bitmap_intersect_p (m_relation_set, b1)
1391 : 16921160 : || !bitmap_intersect_p (m_relation_set, b2))
1392 : 8790073 : return VREL_VARYING;
1393 : :
1394 : : // Search each block in the DOM tree checking.
1395 : 240530252 : for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1396 : : {
1397 : 233082888 : r = find_relation_block (bb->index, b1, b2);
1398 : 233082888 : if (r != VREL_VARYING)
1399 : : return r;
1400 : : }
1401 : : return VREL_VARYING;
1402 : :
1403 : : }
1404 : :
1405 : : // Find a relation in block BB between ssa version V1 and V2. If a relation
1406 : : // is found, return a pointer to the chain object in OBJ.
1407 : :
1408 : : relation_kind
1409 : 545380309 : dom_oracle::find_relation_block (int bb, unsigned v1, unsigned v2,
1410 : : relation_chain **obj) const
1411 : : {
1412 : 1090760618 : if (bb >= (int)m_relations.length())
1413 : : return VREL_VARYING;
1414 : :
1415 : 545374683 : const_bitmap bm = m_relations[bb].m_names;
1416 : 545374683 : if (!bm)
1417 : : return VREL_VARYING;
1418 : :
1419 : : // If both b1 and b2 aren't referenced in this block, cant be a relation
1420 : 401853925 : if (!bitmap_bit_p (bm, v1) || !bitmap_bit_p (bm, v2))
1421 : 389039375 : return VREL_VARYING;
1422 : :
1423 : 12814550 : relation_chain *ptr;
1424 : 71835788 : for (ptr = m_relations[bb].m_head; ptr ; ptr = ptr->m_next)
1425 : : {
1426 : 69188787 : unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1427 : 69188787 : unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1428 : 69188787 : if (v1 == op1 && v2 == op2)
1429 : : {
1430 : 9900099 : if (obj)
1431 : 3824233 : *obj = ptr;
1432 : 9900099 : return ptr->kind ();
1433 : : }
1434 : 59288688 : if (v1 == op2 && v2 == op1)
1435 : : {
1436 : 267450 : if (obj)
1437 : 895 : *obj = ptr;
1438 : 267450 : return relation_swap (ptr->kind ());
1439 : : }
1440 : : }
1441 : :
1442 : : return VREL_VARYING;
1443 : : }
1444 : :
1445 : : // Find a relation between SSA version V1 and V2 in the dominator tree
1446 : : // starting with block BB
1447 : :
1448 : : relation_kind
1449 : 33172831 : dom_oracle::find_relation_dom (basic_block bb, unsigned v1, unsigned v2) const
1450 : : {
1451 : 33172831 : relation_kind r;
1452 : : // IF either name does not occur in a relation anywhere, there isn't one.
1453 : 33172831 : if (!bitmap_bit_p (m_relation_set, v1) || !bitmap_bit_p (m_relation_set, v2))
1454 : 17577463 : return VREL_VARYING;
1455 : :
1456 : 530407498 : for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1457 : : {
1458 : 521154551 : r = find_relation_block (bb->index, v1, v2);
1459 : 521154551 : if (r != VREL_VARYING)
1460 : : return r;
1461 : : }
1462 : : return VREL_VARYING;
1463 : :
1464 : : }
1465 : :
1466 : : // Query if there is a relation between SSA1 and SS2 in block BB or a
1467 : : // dominator of BB
1468 : :
1469 : : relation_kind
1470 : 56301292 : dom_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1471 : : {
1472 : 56301292 : relation_kind kind;
1473 : 56301292 : unsigned v1 = SSA_NAME_VERSION (ssa1);
1474 : 56301292 : unsigned v2 = SSA_NAME_VERSION (ssa2);
1475 : 56301292 : if (v1 == v2)
1476 : : return VREL_EQ;
1477 : :
1478 : : // If v1 or v2 do not have any relations or equivalences, a partial
1479 : : // equivalence is the only possibility.
1480 : 93558511 : if ((!bitmap_bit_p (m_relation_set, v1) && !has_equiv_p (v1))
1481 : 59355424 : || (!bitmap_bit_p (m_relation_set, v2) && !has_equiv_p (v2)))
1482 : 43050857 : return partial_equiv (ssa1, ssa2);
1483 : :
1484 : : // Check for equivalence first. They must be in each equivalency set.
1485 : 13155206 : const_bitmap equiv1 = equiv_set (ssa1, bb);
1486 : 13155206 : const_bitmap equiv2 = equiv_set (ssa2, bb);
1487 : 13155206 : if (bitmap_bit_p (equiv1, v2) && bitmap_bit_p (equiv2, v1))
1488 : : return VREL_EQ;
1489 : :
1490 : 12875265 : kind = partial_equiv (ssa1, ssa2);
1491 : 12875265 : if (kind != VREL_VARYING)
1492 : : return kind;
1493 : :
1494 : : // Initially look for a direct relationship and just return that.
1495 : 12875053 : kind = find_relation_dom (bb, v1, v2);
1496 : 12875053 : if (kind != VREL_VARYING)
1497 : : return kind;
1498 : :
1499 : : // Query using the equivalence sets.
1500 : 7157559 : kind = query (bb, equiv1, equiv2);
1501 : 7157559 : return kind;
1502 : : }
1503 : :
1504 : : // Dump all the relations in block BB to file F.
1505 : :
1506 : : void
1507 : 251 : dom_oracle::dump (FILE *f, basic_block bb) const
1508 : : {
1509 : 251 : equiv_oracle::dump (f,bb);
1510 : :
1511 : 502 : if (bb->index >= (int)m_relations.length ())
1512 : 214 : return;
1513 : 251 : if (!m_relations[bb->index].m_names)
1514 : : return;
1515 : :
1516 : 37 : value_relation vr;
1517 : 84 : FOR_EACH_RELATION_BB (this, bb, vr)
1518 : : {
1519 : 47 : fprintf (f, "Relational : ");
1520 : 47 : vr.dump (f);
1521 : 47 : fprintf (f, "\n");
1522 : : }
1523 : : }
1524 : :
1525 : : // Dump all the relations known to file F.
1526 : :
1527 : : void
1528 : 0 : dom_oracle::dump (FILE *f) const
1529 : : {
1530 : 0 : fprintf (f, "Relation dump\n");
1531 : 0 : for (unsigned i = 0; i < m_relations.length (); i++)
1532 : 0 : if (BASIC_BLOCK_FOR_FN (cfun, i))
1533 : : {
1534 : 0 : fprintf (f, "BB%d\n", i);
1535 : 0 : dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
1536 : : }
1537 : 0 : }
1538 : :
1539 : : void
1540 : 0 : relation_oracle::debug () const
1541 : : {
1542 : 0 : dump (stderr);
1543 : 0 : }
1544 : :
1545 : 27972119 : path_oracle::path_oracle (relation_oracle *oracle)
1546 : : {
1547 : 27972119 : set_root_oracle (oracle);
1548 : 27972119 : bitmap_obstack_initialize (&m_bitmaps);
1549 : 27972119 : obstack_init (&m_chain_obstack);
1550 : :
1551 : : // Initialize header records.
1552 : 27972119 : m_equiv.m_names = BITMAP_ALLOC (&m_bitmaps);
1553 : 27972119 : m_equiv.m_bb = NULL;
1554 : 27972119 : m_equiv.m_next = NULL;
1555 : 27972119 : m_relations.m_names = BITMAP_ALLOC (&m_bitmaps);
1556 : 27972119 : m_relations.m_head = NULL;
1557 : 27972119 : m_killed_defs = BITMAP_ALLOC (&m_bitmaps);
1558 : 27972119 : }
1559 : :
1560 : 55944238 : path_oracle::~path_oracle ()
1561 : : {
1562 : 27972119 : obstack_free (&m_chain_obstack, NULL);
1563 : 27972119 : bitmap_obstack_release (&m_bitmaps);
1564 : 55944238 : }
1565 : :
1566 : : // Return the equiv set for SSA, and if there isn't one, check for equivs
1567 : : // starting in block BB.
1568 : :
1569 : : const_bitmap
1570 : 129471618 : path_oracle::equiv_set (tree ssa, basic_block bb)
1571 : : {
1572 : : // Check the list first.
1573 : 129471618 : equiv_chain *ptr = m_equiv.find (SSA_NAME_VERSION (ssa));
1574 : 129471618 : if (ptr)
1575 : 71471874 : return ptr->m_names;
1576 : :
1577 : : // Otherwise defer to the root oracle.
1578 : 57999744 : if (m_root)
1579 : 50823764 : return m_root->equiv_set (ssa, bb);
1580 : :
1581 : : // Allocate a throw away bitmap if there isn't a root oracle.
1582 : 7175980 : bitmap tmp = BITMAP_ALLOC (&m_bitmaps);
1583 : 7175980 : bitmap_set_bit (tmp, SSA_NAME_VERSION (ssa));
1584 : 7175980 : return tmp;
1585 : : }
1586 : :
1587 : : // Register an equivalence between SSA1 and SSA2 resolving unknowns from
1588 : : // block BB.
1589 : :
1590 : : void
1591 : 7526998 : path_oracle::register_equiv (basic_block bb, tree ssa1, tree ssa2)
1592 : : {
1593 : 7526998 : const_bitmap equiv_1 = equiv_set (ssa1, bb);
1594 : 7526998 : const_bitmap equiv_2 = equiv_set (ssa2, bb);
1595 : :
1596 : : // Check if they are the same set, if so, we're done.
1597 : 7526998 : if (bitmap_equal_p (equiv_1, equiv_2))
1598 : : return;
1599 : :
1600 : : // Don't mess around, simply create a new record and insert it first.
1601 : 7515161 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
1602 : 7515161 : valid_equivs (b, equiv_1, bb);
1603 : 7515161 : valid_equivs (b, equiv_2, bb);
1604 : :
1605 : 7515161 : equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1606 : : sizeof (equiv_chain));
1607 : 7515161 : ptr->m_names = b;
1608 : 7515161 : ptr->m_bb = NULL;
1609 : 7515161 : ptr->m_next = m_equiv.m_next;
1610 : 7515161 : m_equiv.m_next = ptr;
1611 : 7515161 : bitmap_ior_into (m_equiv.m_names, b);
1612 : : }
1613 : :
1614 : : // Register killing definition of an SSA_NAME.
1615 : :
1616 : : void
1617 : 55765511 : path_oracle::killing_def (tree ssa)
1618 : : {
1619 : 55765511 : if (dump_file && (dump_flags & TDF_DETAILS))
1620 : : {
1621 : 818 : fprintf (dump_file, " Registering killing_def (path_oracle) ");
1622 : 818 : print_generic_expr (dump_file, ssa, TDF_SLIM);
1623 : 818 : fprintf (dump_file, "\n");
1624 : : }
1625 : :
1626 : 55765511 : unsigned v = SSA_NAME_VERSION (ssa);
1627 : :
1628 : 55765511 : bitmap_set_bit (m_killed_defs, v);
1629 : 55765511 : bitmap_set_bit (m_equiv.m_names, v);
1630 : :
1631 : : // Now add an equivalency with itself so we don't look to the root oracle.
1632 : 55765511 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
1633 : 55765511 : bitmap_set_bit (b, v);
1634 : 55765511 : equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1635 : : sizeof (equiv_chain));
1636 : 55765511 : ptr->m_names = b;
1637 : 55765511 : ptr->m_bb = NULL;
1638 : 55765511 : ptr->m_next = m_equiv.m_next;
1639 : 55765511 : m_equiv.m_next = ptr;
1640 : :
1641 : : // Walk the relation list and remove SSA from any relations.
1642 : 55765511 : if (!bitmap_bit_p (m_relations.m_names, v))
1643 : : return;
1644 : :
1645 : 122032 : bitmap_clear_bit (m_relations.m_names, v);
1646 : 122032 : relation_chain **prev = &(m_relations.m_head);
1647 : 122032 : relation_chain *next = NULL;
1648 : 415892 : for (relation_chain *ptr = m_relations.m_head; ptr; ptr = next)
1649 : : {
1650 : 293860 : gcc_checking_assert (*prev == ptr);
1651 : 293860 : next = ptr->m_next;
1652 : 293860 : if (SSA_NAME_VERSION (ptr->op1 ()) == v
1653 : 293860 : || SSA_NAME_VERSION (ptr->op2 ()) == v)
1654 : 120860 : *prev = ptr->m_next;
1655 : : else
1656 : 173000 : prev = &(ptr->m_next);
1657 : : }
1658 : : }
1659 : :
1660 : : // Register relation K between SSA1 and SSA2, resolving unknowns by
1661 : : // querying from BB.
1662 : :
1663 : : void
1664 : 27276278 : path_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
1665 : : {
1666 : : // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1667 : : // and no other relation makes sense.
1668 : 27276278 : if (ssa1 == ssa2)
1669 : : return;
1670 : :
1671 : 27263874 : if (dump_file && (dump_flags & TDF_DETAILS))
1672 : : {
1673 : 286 : value_relation vr (k, ssa1, ssa2);
1674 : 286 : fprintf (dump_file, " Registering value_relation (path_oracle) ");
1675 : 286 : vr.dump (dump_file);
1676 : 286 : fprintf (dump_file, " (root: bb%d)\n", bb->index);
1677 : : }
1678 : :
1679 : 27263874 : relation_kind curr = query (bb, ssa1, ssa2);
1680 : 27263874 : if (curr != VREL_VARYING)
1681 : 2068814 : k = relation_intersect (curr, k);
1682 : :
1683 : 27263874 : if (k == VREL_EQ)
1684 : : {
1685 : 7526998 : register_equiv (bb, ssa1, ssa2);
1686 : 7526998 : return;
1687 : : }
1688 : :
1689 : 19736876 : bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa1));
1690 : 19736876 : bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa2));
1691 : 19736876 : relation_chain *ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1692 : : sizeof (relation_chain));
1693 : 19736876 : ptr->set_relation (k, ssa1, ssa2);
1694 : 19736876 : ptr->m_next = m_relations.m_head;
1695 : 19736876 : m_relations.m_head = ptr;
1696 : : }
1697 : :
1698 : : // Query for a relationship between equiv set B1 and B2, resolving unknowns
1699 : : // starting at block BB.
1700 : :
1701 : : relation_kind
1702 : 48738638 : path_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1703 : : {
1704 : 48738638 : if (bitmap_equal_p (b1, b2))
1705 : : return VREL_EQ;
1706 : :
1707 : 48738638 : relation_kind k = m_relations.find_relation (b1, b2);
1708 : :
1709 : : // Do not look at the root oracle for names that have been killed
1710 : : // along the path.
1711 : 48738638 : if (bitmap_intersect_p (m_killed_defs, b1)
1712 : 48738638 : || bitmap_intersect_p (m_killed_defs, b2))
1713 : 36680794 : return k;
1714 : :
1715 : 12057844 : if (k == VREL_VARYING && m_root)
1716 : 9763601 : k = m_root->query (bb, b1, b2);
1717 : :
1718 : : return k;
1719 : : }
1720 : :
1721 : : // Query for a relationship between SSA1 and SSA2, resolving unknowns
1722 : : // starting at block BB.
1723 : :
1724 : : relation_kind
1725 : 49255019 : path_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1726 : : {
1727 : 49255019 : unsigned v1 = SSA_NAME_VERSION (ssa1);
1728 : 49255019 : unsigned v2 = SSA_NAME_VERSION (ssa2);
1729 : :
1730 : 49255019 : if (v1 == v2)
1731 : : return VREL_EQ;
1732 : :
1733 : 49188077 : const_bitmap equiv_1 = equiv_set (ssa1, bb);
1734 : 49188077 : const_bitmap equiv_2 = equiv_set (ssa2, bb);
1735 : 49188077 : if (bitmap_bit_p (equiv_1, v2) && bitmap_bit_p (equiv_2, v1))
1736 : : return VREL_EQ;
1737 : :
1738 : 48738638 : return query (bb, equiv_1, equiv_2);
1739 : : }
1740 : :
1741 : : // Reset any relations registered on this path. ORACLE is the root
1742 : : // oracle to use.
1743 : :
1744 : : void
1745 : 22835519 : path_oracle::reset_path (relation_oracle *oracle)
1746 : : {
1747 : 22835519 : set_root_oracle (oracle);
1748 : 22835519 : m_equiv.m_next = NULL;
1749 : 22835519 : bitmap_clear (m_equiv.m_names);
1750 : 22835519 : m_relations.m_head = NULL;
1751 : 22835519 : bitmap_clear (m_relations.m_names);
1752 : 22835519 : bitmap_clear (m_killed_defs);
1753 : 22835519 : }
1754 : :
1755 : : // Dump relation in basic block... Do nothing here.
1756 : :
1757 : : void
1758 : 0 : path_oracle::dump (FILE *, basic_block) const
1759 : : {
1760 : 0 : }
1761 : :
1762 : : // Dump the relations and equivalencies found in the path.
1763 : :
1764 : : void
1765 : 0 : path_oracle::dump (FILE *f) const
1766 : : {
1767 : 0 : equiv_chain *ptr = m_equiv.m_next;
1768 : 0 : relation_chain *ptr2 = m_relations.m_head;
1769 : :
1770 : 0 : if (ptr || ptr2)
1771 : 0 : fprintf (f, "\npath_oracle:\n");
1772 : :
1773 : 0 : for (; ptr; ptr = ptr->m_next)
1774 : 0 : ptr->dump (f);
1775 : :
1776 : 0 : for (; ptr2; ptr2 = ptr2->m_next)
1777 : : {
1778 : 0 : fprintf (f, "Relational : ");
1779 : 0 : ptr2->dump (f);
1780 : 0 : fprintf (f, "\n");
1781 : : }
1782 : 0 : }
1783 : :
1784 : : // ------------------------------------------------------------------------
1785 : : // EQUIV iterator. Although we have bitmap iterators, don't expose that it
1786 : : // is currently a bitmap. Use an export iterator to hide future changes.
1787 : :
1788 : : // Construct a basic iterator over an equivalence bitmap.
1789 : :
1790 : 48131152 : equiv_relation_iterator::equiv_relation_iterator (relation_oracle *oracle,
1791 : : basic_block bb, tree name,
1792 : 48131152 : bool full, bool partial)
1793 : : {
1794 : 48131152 : m_name = name;
1795 : 48131152 : m_oracle = oracle;
1796 : 48131152 : m_pe = partial ? oracle->partial_equiv_set (name) : NULL;
1797 : 48131152 : m_bm = NULL;
1798 : 48131152 : if (full)
1799 : 48131152 : m_bm = oracle->equiv_set (name, bb);
1800 : 48131152 : if (!m_bm && m_pe)
1801 : 0 : m_bm = m_pe->members;
1802 : 48131152 : if (m_bm)
1803 : 48131152 : bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1804 : 48131152 : }
1805 : :
1806 : : // Move to the next export bitmap spot.
1807 : :
1808 : : void
1809 : 62398462 : equiv_relation_iterator::next ()
1810 : : {
1811 : 62398462 : bmp_iter_next (&m_bi, &m_y);
1812 : 62398462 : }
1813 : :
1814 : : // Fetch the name of the next export in the export list. Return NULL if
1815 : : // iteration is done.
1816 : :
1817 : : tree
1818 : 56987212 : equiv_relation_iterator::get_name (relation_kind *rel)
1819 : : {
1820 : 62398406 : if (!m_bm)
1821 : : return NULL_TREE;
1822 : :
1823 : 115940808 : while (bmp_iter_set (&m_bi, &m_y))
1824 : : {
1825 : : // Do not return self.
1826 : 62398462 : tree t = ssa_name (m_y);
1827 : 62398462 : if (t && t != m_name)
1828 : : {
1829 : 8856060 : relation_kind k = VREL_EQ;
1830 : 8856060 : if (m_pe && m_bm == m_pe->members)
1831 : : {
1832 : 7004958 : const pe_slice *equiv_pe = m_oracle->partial_equiv_set (t);
1833 : 7004958 : if (equiv_pe && equiv_pe->members == m_pe->members)
1834 : 7004958 : k = pe_min (m_pe->code, equiv_pe->code);
1835 : : else
1836 : : k = VREL_VARYING;
1837 : : }
1838 : 7004958 : if (relation_equiv_p (k))
1839 : : {
1840 : 8856060 : if (rel)
1841 : 8856060 : *rel = k;
1842 : 8856060 : return t;
1843 : : }
1844 : : }
1845 : 53542402 : next ();
1846 : : }
1847 : :
1848 : : // Process partial equivs after full equivs if both were requested.
1849 : 53542346 : if (m_pe && m_bm != m_pe->members)
1850 : : {
1851 : 48131152 : m_bm = m_pe->members;
1852 : 48131152 : if (m_bm)
1853 : : {
1854 : : // Recursively call back to process First PE.
1855 : 5411194 : bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1856 : 5411194 : return get_name (rel);
1857 : : }
1858 : : }
1859 : : return NULL_TREE;
1860 : : }
1861 : :
1862 : : #if CHECKING_P
1863 : : #include "selftest.h"
1864 : :
1865 : : namespace selftest
1866 : : {
1867 : : void
1868 : 4 : relation_tests ()
1869 : : {
1870 : : // rr_*_table tables use unsigned char rather than relation_kind.
1871 : 4 : ASSERT_LT (VREL_LAST, UCHAR_MAX);
1872 : : // Verify commutativity of relation_intersect and relation_union.
1873 : 36 : for (relation_kind r1 = VREL_VARYING; r1 < VREL_PE8;
1874 : 32 : r1 = relation_kind (r1 + 1))
1875 : 288 : for (relation_kind r2 = VREL_VARYING; r2 < VREL_PE8;
1876 : 256 : r2 = relation_kind (r2 + 1))
1877 : : {
1878 : 256 : ASSERT_EQ (relation_intersect (r1, r2), relation_intersect (r2, r1));
1879 : 256 : ASSERT_EQ (relation_union (r1, r2), relation_union (r2, r1));
1880 : : }
1881 : 4 : }
1882 : :
1883 : : } // namespace selftest
1884 : :
1885 : : #endif // CHECKING_P
|