]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/value-relation.cc
2ac7650fe5b4d5daaacf146eca307dfb0a8d2153
[thirdparty/gcc.git] / gcc / value-relation.cc
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 print_relation (FILE *f, relation_kind rel)
43 {
44 fprintf (f, " %s ", kind_string[rel]);
45 }
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 relation_negate (relation_kind r)
56 {
57 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 relation_swap (relation_kind r)
69 {
70 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 relation_intersect (relation_kind r1, relation_kind r2)
106 {
107 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 relation_union (relation_kind r1, relation_kind r2)
143 {
144 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 relation_transitive (relation_kind r1, relation_kind r2)
182 {
183 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 adjust_equivalence_range (vrange &range)
192 {
193 if (range.undefined_p () || !is_a<frange> (range))
194 return;
195
196 frange fr = as_a<frange> (range);
197 // If range includes 0 make sure both signs of zero are included.
198 if (fr.contains_p (dconst0) || fr.contains_p (dconstm0))
199 {
200 frange zeros (range.type (), dconstm0, dconst0);
201 range.union_ (zeros);
202 }
203 }
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 relation_oracle::valid_equivs (bitmap b, const_bitmap equivs, basic_block bb)
210 {
211 unsigned i;
212 bitmap_iterator bi;
213 EXECUTE_IF_SET_IN_BITMAP (equivs, 0, i, bi)
214 {
215 tree ssa = ssa_name (i);
216 if (ssa && !SSA_NAME_IN_FREE_LIST (ssa))
217 {
218 const_bitmap ssa_equiv = equiv_set (ssa, bb);
219 if (ssa_equiv == equivs)
220 bitmap_set_bit (b, i);
221 }
222 }
223 }
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 relation_oracle::query (gimple *s, tree ssa1, tree ssa2)
231 {
232 if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
233 return VREL_VARYING;
234 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 relation_oracle::query (edge e, tree ssa1, tree ssa2)
243 {
244 basic_block bb;
245 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 if (!single_pred_p (e->dest))
252 bb = e->src;
253 else
254 bb = e->dest;
255
256 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 equiv_chain::find (unsigned ssa)
273 {
274 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 if (bitmap_bit_p (m_names, ssa))
278 {
279 for (ptr = m_next; ptr; ptr = ptr->m_next)
280 if (bitmap_bit_p (ptr->m_names, ssa))
281 break;
282 }
283 return ptr;
284 }
285
286 // Dump the names in this equivalence set.
287
288 void
289 equiv_chain::dump (FILE *f) const
290 {
291 bitmap_iterator bi;
292 unsigned i;
293
294 if (!m_names || bitmap_empty_p (m_names))
295 return;
296 fprintf (f, "Equivalence set : [");
297 unsigned c = 0;
298 EXECUTE_IF_SET_IN_BITMAP (m_names, 0, i, bi)
299 {
300 if (ssa_name (i))
301 {
302 if (c++)
303 fprintf (f, ", ");
304 print_generic_expr (f, ssa_name (i), TDF_SLIM);
305 }
306 }
307 fprintf (f, "]\n");
308 }
309
310 // Instantiate an equivalency oracle.
311
312 equiv_oracle::equiv_oracle ()
313 {
314 bitmap_obstack_initialize (&m_bitmaps);
315 m_equiv.create (0);
316 m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
317 m_equiv_set = BITMAP_ALLOC (&m_bitmaps);
318 bitmap_tree_view (m_equiv_set);
319 obstack_init (&m_chain_obstack);
320 m_self_equiv.create (0);
321 m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
322 m_partial.create (0);
323 m_partial.safe_grow_cleared (num_ssa_names + 1);
324 }
325
326 // Destruct an equivalency oracle.
327
328 equiv_oracle::~equiv_oracle ()
329 {
330 m_partial.release ();
331 m_self_equiv.release ();
332 obstack_free (&m_chain_obstack, NULL);
333 m_equiv.release ();
334 bitmap_obstack_release (&m_bitmaps);
335 }
336
337 // Add a partial equivalence R between OP1 and OP2.
338
339 void
340 equiv_oracle::add_partial_equiv (relation_kind r, tree op1, tree op2)
341 {
342 int v1 = SSA_NAME_VERSION (op1);
343 int v2 = SSA_NAME_VERSION (op2);
344 int prec2 = TYPE_PRECISION (TREE_TYPE (op2));
345 int bits = pe_to_bits (r);
346 gcc_checking_assert (bits && prec2 >= bits);
347
348 if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
349 m_partial.safe_grow_cleared (num_ssa_names + 1);
350 gcc_checking_assert (v1 < (int)m_partial.length ()
351 && v2 < (int)m_partial.length ());
352
353 pe_slice &pe1 = m_partial[v1];
354 pe_slice &pe2 = m_partial[v2];
355
356 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 if (pe2.members)
362 return;
363 // If there are no uses of op2, do not register.
364 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 pe2.code = pe_min (r, pe1.code);
369 pe2.ssa_base = op2;
370 pe2.members = pe1.members;
371 bitmap_iterator bi;
372 unsigned x;
373 EXECUTE_IF_SET_IN_BITMAP (pe1.members, 0, x, bi)
374 {
375 m_partial[x].ssa_base = op2;
376 m_partial[x].code = pe_min (m_partial[x].code, pe2.code);
377 }
378 bitmap_set_bit (pe1.members, v2);
379 return;
380 }
381 if (pe2.members)
382 {
383 // If there are no uses of op1, do not register.
384 if (has_zero_uses (op1))
385 return;
386 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 pe1.code = pe_min (r, pe2.code);
390 pe1.members = pe2.members;
391 bitmap_set_bit (pe1.members, v1);
392 }
393 else
394 {
395 // If there are no uses of either operand, do not register.
396 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 pe2.code = bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2)));
400 if (pe2.code == VREL_VARYING)
401 return;
402 pe2.ssa_base = op2;
403 pe2.members = BITMAP_ALLOC (&m_bitmaps);
404 bitmap_set_bit (pe2.members, v2);
405 pe1.ssa_base = op2;
406 pe1.code = r;
407 pe1.members = pe2.members;
408 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 equiv_oracle::partial_equiv_set (tree name)
417 {
418 int v = SSA_NAME_VERSION (name);
419 if (v >= (int)m_partial.length ())
420 return NULL;
421 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 equiv_oracle::partial_equiv (tree ssa1, tree ssa2, tree *base) const
430 {
431 int v1 = SSA_NAME_VERSION (ssa1);
432 int v2 = SSA_NAME_VERSION (ssa2);
433
434 if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
435 return VREL_VARYING;
436
437 const pe_slice &pe1 = m_partial[v1];
438 const pe_slice &pe2 = m_partial[v2];
439 if (pe1.members && pe2.members == pe1.members)
440 {
441 if (base)
442 *base = pe1.ssa_base;
443 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 equiv_oracle::equiv_set (tree ssa, basic_block bb)
454 {
455 // Search the dominator tree for an equivalency.
456 equiv_chain *equiv = find_equiv_dom (ssa, bb);
457 if (equiv)
458 return equiv->m_names;
459
460 // Otherwise return a cached equiv set containing just this SSA.
461 unsigned v = SSA_NAME_VERSION (ssa);
462 if (v >= m_self_equiv.length ())
463 m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
464
465 if (!m_self_equiv[v])
466 {
467 m_self_equiv[v] = BITMAP_ALLOC (&m_bitmaps);
468 bitmap_set_bit (m_self_equiv[v], v);
469 }
470 return m_self_equiv[v];
471 }
472
473 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
474
475 relation_kind
476 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 if (equiv_set (ssa1, bb) == equiv_set (ssa2, bb))
480 return VREL_EQ;
481
482 // Check if there is a partial equivalence.
483 return partial_equiv (ssa1, ssa2);
484 }
485
486 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
487
488 relation_kind
489 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 if (bitmap_equal_p (e1, e2))
494 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 equiv_oracle::find_equiv_block (unsigned ssa, int bb) const
503 {
504 if (bb >= (int)m_equiv.length () || !m_equiv[bb])
505 return NULL;
506
507 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 equiv_oracle::find_equiv_dom (tree name, basic_block bb) const
515 {
516 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 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 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
525 {
526 equiv_chain *ptr = find_equiv_block (v, bb->index);
527 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 equiv_oracle::register_equiv (basic_block bb, unsigned v, equiv_chain *equiv)
537 {
538 // V will have an equivalency now.
539 bitmap_set_bit (m_equiv_set, v);
540
541 // If that equiv chain is in this block, simply use it.
542 if (equiv->m_bb == bb)
543 {
544 bitmap_set_bit (equiv->m_names, v);
545 bitmap_set_bit (m_equiv[bb->index]->m_names, v);
546 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 bitmap b = BITMAP_ALLOC (&m_bitmaps);
552 valid_equivs (b, equiv->m_names, bb);
553 bitmap_set_bit (b, v);
554 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 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 if (equiv_1->m_bb == bb)
568 {
569 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 if (equiv_2->m_bb == bb)
573 bitmap_clear (equiv_2->m_names);
574 else
575 // Ensure the new names are in the summary for BB.
576 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_1->m_names);
577 return NULL;
578 }
579 // If equiv_2 is in BB, use it for the combined set.
580 if (equiv_2->m_bb == bb)
581 {
582 valid_equivs (equiv_2->m_names, equiv_1->m_names, bb);
583 // Ensure the new names are in the summary.
584 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_2->m_names);
585 return NULL;
586 }
587
588 // At this point, neither equivalence is from this block.
589 bitmap b = BITMAP_ALLOC (&m_bitmaps);
590 valid_equivs (b, equiv_1->m_names, bb);
591 valid_equivs (b, equiv_2->m_names, bb);
592 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 equiv_oracle::register_initial_def (tree ssa)
601 {
602 if (SSA_NAME_IS_DEFAULT_DEF (ssa))
603 return;
604 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (ssa));
605
606 // If defining stmt is not in the IL, simply return.
607 if (!bb)
608 return;
609 gcc_checking_assert (!find_equiv_dom (ssa, bb));
610
611 unsigned v = SSA_NAME_VERSION (ssa);
612 bitmap_set_bit (m_equiv_set, v);
613 bitmap equiv_set = BITMAP_ALLOC (&m_bitmaps);
614 bitmap_set_bit (equiv_set, v);
615 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 equiv_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
627 {
628 // Process partial equivalencies.
629 if (relation_partial_equiv_p (k))
630 {
631 add_partial_equiv (k, ssa1, ssa2);
632 return;
633 }
634 // Only handle equality relations.
635 if (k != VREL_EQ)
636 return;
637
638 unsigned v1 = SSA_NAME_VERSION (ssa1);
639 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 if (!bitmap_bit_p (m_equiv_set, v1))
644 register_initial_def (ssa1);
645 if (!bitmap_bit_p (m_equiv_set, v2))
646 register_initial_def (ssa2);
647
648 equiv_chain *equiv_1 = find_equiv_dom (ssa1, bb);
649 equiv_chain *equiv_2 = find_equiv_dom (ssa2, bb);
650
651 // Check if they are the same set
652 if (equiv_1 && equiv_1 == equiv_2)
653 return;
654
655 bitmap equiv_set;
656
657 // Case where we have 2 SSA_NAMEs that are not in any set.
658 if (!equiv_1 && !equiv_2)
659 {
660 bitmap_set_bit (m_equiv_set, v1);
661 bitmap_set_bit (m_equiv_set, v2);
662
663 equiv_set = BITMAP_ALLOC (&m_bitmaps);
664 bitmap_set_bit (equiv_set, v1);
665 bitmap_set_bit (equiv_set, v2);
666 }
667 else if (!equiv_1 && equiv_2)
668 equiv_set = register_equiv (bb, v1, equiv_2);
669 else if (equiv_1 && !equiv_2)
670 equiv_set = register_equiv (bb, v2, equiv_1);
671 else
672 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 if (!equiv_set)
677 return;
678
679 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 equiv_oracle::add_equiv_to_block (basic_block bb, bitmap equiv_set)
687 {
688 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 limit_check (bb);
693 if (!m_equiv[bb->index])
694 {
695 ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
696 sizeof (equiv_chain));
697 ptr->m_names = BITMAP_ALLOC (&m_bitmaps);
698 bitmap_copy (ptr->m_names, equiv_set);
699 ptr->m_bb = bb;
700 ptr->m_next = NULL;
701 m_equiv[bb->index] = ptr;
702 }
703
704 // Now create the element for this equiv set and initialize it.
705 ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack, sizeof (equiv_chain));
706 ptr->m_names = equiv_set;
707 ptr->m_bb = bb;
708 gcc_checking_assert (bb->index < (int)m_equiv.length ());
709 ptr->m_next = m_equiv[bb->index]->m_next;
710 m_equiv[bb->index]->m_next = ptr;
711 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_set);
712 }
713
714 // Make sure the BB vector is big enough and grow it if needed.
715
716 void
717 equiv_oracle::limit_check (basic_block bb)
718 {
719 int i = (bb) ? bb->index : last_basic_block_for_fn (cfun);
720 if (i >= (int)m_equiv.length ())
721 m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
722 }
723
724 // Dump the equivalence sets in BB to file F.
725
726 void
727 equiv_oracle::dump (FILE *f, basic_block bb) const
728 {
729 if (bb->index >= (int)m_equiv.length ())
730 return;
731 // Process equivalences.
732 if (m_equiv[bb->index])
733 {
734 equiv_chain *ptr = m_equiv[bb->index]->m_next;
735 for (; ptr; ptr = ptr->m_next)
736 ptr->dump (f);
737 }
738 // Look for partial equivalences defined in this block..
739 for (unsigned i = 0; i < num_ssa_names; i++)
740 {
741 tree name = ssa_name (i);
742 if (!gimple_range_ssa_p (name) || !SSA_NAME_DEF_STMT (name))
743 continue;
744 if (i >= m_partial.length ())
745 break;
746 tree base = m_partial[i].ssa_base;
747 if (base && name != base && gimple_bb (SSA_NAME_DEF_STMT (name)) == bb)
748 {
749 relation_kind k = partial_equiv (name, base);
750 if (k != VREL_VARYING)
751 {
752 value_relation vr (k, name, base);
753 fprintf (f, "Partial equiv ");
754 vr.dump (f);
755 fputc ('\n',f);
756 }
757 }
758 }
759 }
760
761 // Dump all equivalence sets known to the oracle.
762
763 void
764 equiv_oracle::dump (FILE *f) const
765 {
766 fprintf (f, "Equivalency dump\n");
767 for (unsigned i = 0; i < m_equiv.length (); i++)
768 if (m_equiv[i] && BASIC_BLOCK_FOR_FN (cfun, i))
769 {
770 fprintf (f, "BB%d\n", i);
771 dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
772 }
773 }
774
775
776 // --------------------------------------------------------------------------
777
778 // Adjust the relation by Swapping the operands and relation.
779
780 void
781 value_relation::swap ()
782 {
783 related = relation_swap (related);
784 tree tmp = name1;
785 name1 = name2;
786 name2 = tmp;
787 }
788
789 // Perform an intersection between 2 relations. *this &&= p.
790 // Return false if the relations cannot be intersected.
791
792 bool
793 value_relation::intersect (value_relation &p)
794 {
795 // Save previous value
796 relation_kind old = related;
797
798 if (p.op1 () == op1 () && p.op2 () == op2 ())
799 related = relation_intersect (kind (), p.kind ());
800 else if (p.op2 () == op1 () && p.op1 () == op2 ())
801 related = relation_intersect (kind (), relation_swap (p.kind ()));
802 else
803 return false;
804
805 return old != related;
806 }
807
808 // Perform a union between 2 relations. *this ||= p.
809
810 bool
811 value_relation::union_ (value_relation &p)
812 {
813 // Save previous value
814 relation_kind old = related;
815
816 if (p.op1 () == op1 () && p.op2 () == op2 ())
817 related = relation_union (kind(), p.kind());
818 else if (p.op2 () == op1 () && p.op1 () == op2 ())
819 related = relation_union (kind(), relation_swap (p.kind ()));
820 else
821 return false;
822
823 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 value_relation::apply_transitive (const value_relation &rel)
831 {
832 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 if (rel.op1 () == name2)
837 {
838 // A < B B < C
839 if (rel.op2 () == name1)
840 return false;
841 k = relation_transitive (kind (), rel.kind ());
842 if (k != VREL_VARYING)
843 {
844 related = k;
845 name2 = rel.op2 ();
846 return true;
847 }
848 }
849 else if (rel.op1 () == name1)
850 {
851 // B > A B < C
852 if (rel.op2 () == name2)
853 return false;
854 k = relation_transitive (relation_swap (kind ()), rel.kind ());
855 if (k != VREL_VARYING)
856 {
857 related = k;
858 name1 = name2;
859 name2 = rel.op2 ();
860 return true;
861 }
862 }
863 else if (rel.op2 () == name2)
864 {
865 // A < B C > B
866 if (rel.op1 () == name1)
867 return false;
868 k = relation_transitive (kind (), relation_swap (rel.kind ()));
869 if (k != VREL_VARYING)
870 {
871 related = k;
872 name2 = rel.op1 ();
873 return true;
874 }
875 }
876 else if (rel.op2 () == name1)
877 {
878 // B > A C > B
879 if (rel.op1 () == name2)
880 return false;
881 k = relation_transitive (relation_swap (kind ()),
882 relation_swap (rel.kind ()));
883 if (k != VREL_VARYING)
884 {
885 related = k;
886 name1 = name2;
887 name2 = rel.op1 ();
888 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 value_relation::create_trio (tree lhs, tree op1, tree op2)
898 {
899 relation_kind lhs_1;
900 if (lhs == name1 && op1 == name2)
901 lhs_1 = related;
902 else if (lhs == name2 && op1 == name1)
903 lhs_1 = relation_swap (related);
904 else
905 lhs_1 = VREL_VARYING;
906
907 relation_kind lhs_2;
908 if (lhs == name1 && op2 == name2)
909 lhs_2 = related;
910 else if (lhs == name2 && op2 == name1)
911 lhs_2 = relation_swap (related);
912 else
913 lhs_2 = VREL_VARYING;
914
915 relation_kind op_op;
916 if (op1 == name1 && op2 == name2)
917 op_op = related;
918 else if (op1 == name2 && op2 == name1)
919 op_op = relation_swap (related);
920 else if (op1 == op2)
921 op_op = VREL_EQ;
922 else
923 op_op = VREL_VARYING;
924
925 return relation_trio (lhs_1, lhs_2, op_op);
926 }
927
928 // Dump the relation to file F.
929
930 void
931 value_relation::dump (FILE *f) const
932 {
933 if (!name1 || !name2)
934 {
935 fprintf (f, "no relation registered");
936 return;
937 }
938 fputc ('(', f);
939 print_generic_expr (f, op1 (), TDF_SLIM);
940 print_relation (f, kind ());
941 print_generic_expr (f, op2 (), TDF_SLIM);
942 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 dom_oracle::next_relation (basic_block bb, relation_chain *ptr,
960 tree name) const
961 {
962 relation_chain *p;
963 // No value_relation pointer is used to intialize the iterator.
964 if (!ptr)
965 {
966 int bbi = bb->index;
967 if (bbi >= (int)m_relations.length())
968 return NULL;
969 else
970 p = m_relations[bbi].m_head;
971 }
972 else
973 p = ptr->m_next;
974
975 if (name)
976 for ( ; p; p = p->m_next)
977 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 block_relation_iterator::block_relation_iterator (const relation_oracle *oracle,
987 basic_block bb,
988 value_relation &vr,
989 tree name)
990 {
991 m_oracle = oracle;
992 m_bb = bb;
993 m_name = name;
994 m_ptr = oracle->next_relation (bb, NULL, m_name);
995 if (m_ptr)
996 {
997 m_done = false;
998 vr = *m_ptr;
999 }
1000 else
1001 m_done = true;
1002 }
1003
1004 // Retreive the next relation from the iterator and return it in VR.
1005
1006 void
1007 block_relation_iterator::get_next_relation (value_relation &vr)
1008 {
1009 m_ptr = m_oracle->next_relation (m_bb, m_ptr, m_name);
1010 if (m_ptr)
1011 {
1012 vr = *m_ptr;
1013 if (m_name)
1014 {
1015 if (vr.op1 () != m_name)
1016 {
1017 gcc_checking_assert (vr.op2 () == m_name);
1018 vr.swap ();
1019 }
1020 }
1021 }
1022 else
1023 m_done = true;
1024 }
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 relation_chain_head::find_relation (const_bitmap b1, const_bitmap b2) const
1033 {
1034 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 if (!bitmap_intersect_p (m_names, b1) || !bitmap_intersect_p (m_names, b2))
1039 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 for (relation_chain *ptr = m_head; ptr ; ptr = ptr->m_next)
1044 {
1045 unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1046 unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1047 if (bitmap_bit_p (b1, op1) && bitmap_bit_p (b2, op2))
1048 return ptr->kind ();
1049 if (bitmap_bit_p (b1, op2) && bitmap_bit_p (b2, op1))
1050 return relation_swap (ptr->kind ());
1051 }
1052
1053 return VREL_VARYING;
1054 }
1055
1056 // Instantiate a relation oracle.
1057
1058 dom_oracle::dom_oracle (bool do_trans_p)
1059 {
1060 m_do_trans_p = do_trans_p;
1061 m_relations.create (0);
1062 m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1063 m_relation_set = BITMAP_ALLOC (&m_bitmaps);
1064 m_tmp = BITMAP_ALLOC (&m_bitmaps);
1065 m_tmp2 = BITMAP_ALLOC (&m_bitmaps);
1066 }
1067
1068 // Destruct a relation oracle.
1069
1070 dom_oracle::~dom_oracle ()
1071 {
1072 m_relations.release ();
1073 }
1074
1075 // Register relation K between ssa_name OP1 and OP2 on STMT.
1076
1077 void
1078 relation_oracle::record (gimple *stmt, relation_kind k, tree op1, tree op2)
1079 {
1080 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1081 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1082 gcc_checking_assert (stmt && gimple_bb (stmt));
1083
1084 // Don't register lack of a relation.
1085 if (k == VREL_VARYING)
1086 return;
1087
1088 if (dump_file && (dump_flags & TDF_DETAILS))
1089 {
1090 value_relation vr (k, op1, op2);
1091 fprintf (dump_file, " Registering value_relation ");
1092 vr.dump (dump_file);
1093 fprintf (dump_file, " (bb%d) at ", gimple_bb (stmt)->index);
1094 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 if (k == VREL_EQ && is_a<gphi *> (stmt))
1102 {
1103 tree phi_def = gimple_phi_result (stmt);
1104 gcc_checking_assert (phi_def == op1 || phi_def == op2);
1105 tree arg = op2;
1106 if (phi_def == op2)
1107 arg = op1;
1108 if (gimple_bb (stmt) == gimple_bb (SSA_NAME_DEF_STMT (arg)))
1109 {
1110 if (dump_file && (dump_flags & TDF_DETAILS))
1111 {
1112 fprintf (dump_file, " Not registered due to ");
1113 print_generic_expr (dump_file, arg, TDF_SLIM);
1114 fprintf (dump_file, " being defined in the same block.\n");
1115 }
1116 return;
1117 }
1118 }
1119 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 relation_oracle::record (edge e, relation_kind k, tree op1, tree op2)
1126 {
1127 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1128 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 if (k == VREL_VARYING || !single_pred_p (e->dest))
1133 return;
1134
1135 if (dump_file && (dump_flags & TDF_DETAILS))
1136 {
1137 value_relation vr (k, op1, op2);
1138 fprintf (dump_file, " Registering value_relation ");
1139 vr.dump (dump_file);
1140 fprintf (dump_file, " on (%d->%d)\n", e->src->index, e->dest->index);
1141 }
1142
1143 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 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 if (op1 == op2)
1156 return;
1157
1158 // Equivalencies are handled by the equivalence oracle.
1159 if (relation_equiv_p (k))
1160 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 bool check = bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op1))
1166 || bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op2));
1167 relation_chain *ptr = set_one_relation (bb, k, op1, op2);
1168 if (ptr && check
1169 && (m_relations[bb->index].m_num_relations
1170 < param_relation_block_limit))
1171 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 dom_oracle::set_one_relation (basic_block bb, relation_kind k, tree op1,
1181 tree op2)
1182 {
1183 gcc_checking_assert (k != VREL_VARYING && k != VREL_EQ);
1184
1185 value_relation vr(k, op1, op2);
1186 int bbi = bb->index;
1187
1188 if (bbi >= (int)m_relations.length())
1189 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 bitmap bm = m_relations[bbi].m_names;
1193 if (!bm)
1194 bm = m_relations[bbi].m_names = BITMAP_ALLOC (&m_bitmaps);
1195 unsigned v1 = SSA_NAME_VERSION (op1);
1196 unsigned v2 = SSA_NAME_VERSION (op2);
1197
1198 relation_kind curr;
1199 relation_chain *ptr;
1200 curr = find_relation_block (bbi, v1, v2, &ptr);
1201 // There is an existing relation in this block, just intersect with it.
1202 if (curr != VREL_VARYING)
1203 {
1204 if (dump_file && (dump_flags & TDF_DETAILS))
1205 {
1206 fprintf (dump_file, " Intersecting with existing ");
1207 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 bool new_rel = ptr->intersect (vr);
1213 if (dump_file && (dump_flags & TDF_DETAILS))
1214 {
1215 fprintf (dump_file, " to produce ");
1216 ptr->dump (dump_file);
1217 fprintf (dump_file, " %s.\n", new_rel ? "Updated" : "No Change");
1218 }
1219 // If there was no change, return no record..
1220 if (!new_rel)
1221 return NULL;
1222 }
1223 else
1224 {
1225 if (m_relations[bbi].m_num_relations >= param_relation_block_limit)
1226 {
1227 if (dump_file && (dump_flags & TDF_DETAILS))
1228 fprintf (dump_file, " Not registered due to bb being full\n");
1229 return NULL;
1230 }
1231 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 curr = find_relation_dom (bb, v1, v2);
1236 if (curr != VREL_VARYING)
1237 k = relation_intersect (curr, k);
1238
1239 bitmap_set_bit (bm, v1);
1240 bitmap_set_bit (bm, v2);
1241 bitmap_set_bit (m_relation_set, v1);
1242 bitmap_set_bit (m_relation_set, v2);
1243
1244 ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1245 sizeof (relation_chain));
1246 ptr->set_relation (k, op1, op2);
1247 ptr->m_next = m_relations[bbi].m_head;
1248 m_relations[bbi].m_head = ptr;
1249 }
1250 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 dom_oracle::register_transitives (basic_block root_bb,
1260 const value_relation &relation)
1261 {
1262 // Only register transitives if they are requested.
1263 if (!m_do_trans_p)
1264 return;
1265 basic_block bb;
1266 // Only apply transitives to certain kinds of operations.
1267 switch (relation.kind ())
1268 {
1269 case VREL_LE:
1270 case VREL_LT:
1271 case VREL_GT:
1272 case VREL_GE:
1273 break;
1274 default:
1275 return;
1276 }
1277
1278 const_bitmap equiv1 = equiv_set (relation.op1 (), root_bb);
1279 const_bitmap equiv2 = equiv_set (relation.op2 (), root_bb);
1280
1281 const unsigned work_budget = param_transitive_relations_work_bound;
1282 unsigned avail_budget = work_budget;
1283 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 ? get_immediate_dominator (CDI_DOMINATORS, bb) : nullptr))
1288 {
1289 int bbi = bb->index;
1290 if (bbi >= (int)m_relations.length())
1291 continue;
1292 const_bitmap bm = m_relations[bbi].m_names;
1293 if (!bm)
1294 continue;
1295 if (!bitmap_intersect_p (bm, equiv1) && !bitmap_intersect_p (bm, equiv2))
1296 continue;
1297 // At least one of the 2 ops has a relation in this block.
1298 relation_chain *ptr;
1299 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 tree r1, r2;
1310 tree p1 = ptr->op1 ();
1311 tree p2 = ptr->op2 ();
1312 // Find which equivalence is in the first operand.
1313 if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p1)))
1314 r1 = p1;
1315 else if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p2)))
1316 r1 = p2;
1317 else
1318 r1 = NULL_TREE;
1319
1320 // Find which equivalence is in the second operand.
1321 if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p1)))
1322 r2 = p1;
1323 else if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p2)))
1324 r2 = p2;
1325 else
1326 r2 = NULL_TREE;
1327
1328 // Ignore if both NULL (not relevant relation) or the same,
1329 if (r1 == r2)
1330 ;
1331
1332 else
1333 {
1334 // Any operand not an equivalence, just take the real operand.
1335 if (!r1)
1336 r1 = relation.op1 ();
1337 if (!r2)
1338 r2 = relation.op2 ();
1339
1340 value_relation nr (relation.kind (), r1, r2);
1341 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 if (!set_one_relation (root_bb, nr.kind (),
1348 nr.op1 (), nr.op2 ()))
1349 return;
1350 if (dump_file && (dump_flags & TDF_DETAILS))
1351 {
1352 fprintf (dump_file,
1353 " Registering transitive relation ");
1354 nr.dump (dump_file);
1355 fputc ('\n', dump_file);
1356 }
1357 }
1358 }
1359 /* Processed one relation, abort if we've eaten up our budget. */
1360 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 dom_oracle::find_relation_block (unsigned bb, const_bitmap b1,
1371 const_bitmap b2) const
1372 {
1373 if (bb >= m_relations.length())
1374 return VREL_VARYING;
1375
1376 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 dom_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1384 {
1385 relation_kind r;
1386 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 if (!bitmap_intersect_p (m_relation_set, b1)
1391 || !bitmap_intersect_p (m_relation_set, b2))
1392 return VREL_VARYING;
1393
1394 // Search each block in the DOM tree checking.
1395 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1396 {
1397 r = find_relation_block (bb->index, b1, b2);
1398 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 dom_oracle::find_relation_block (int bb, unsigned v1, unsigned v2,
1410 relation_chain **obj) const
1411 {
1412 if (bb >= (int)m_relations.length())
1413 return VREL_VARYING;
1414
1415 const_bitmap bm = m_relations[bb].m_names;
1416 if (!bm)
1417 return VREL_VARYING;
1418
1419 // If both b1 and b2 aren't referenced in this block, cant be a relation
1420 if (!bitmap_bit_p (bm, v1) || !bitmap_bit_p (bm, v2))
1421 return VREL_VARYING;
1422
1423 relation_chain *ptr;
1424 for (ptr = m_relations[bb].m_head; ptr ; ptr = ptr->m_next)
1425 {
1426 unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1427 unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1428 if (v1 == op1 && v2 == op2)
1429 {
1430 if (obj)
1431 *obj = ptr;
1432 return ptr->kind ();
1433 }
1434 if (v1 == op2 && v2 == op1)
1435 {
1436 if (obj)
1437 *obj = ptr;
1438 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 dom_oracle::find_relation_dom (basic_block bb, unsigned v1, unsigned v2) const
1450 {
1451 relation_kind r;
1452 // IF either name does not occur in a relation anywhere, there isn't one.
1453 if (!bitmap_bit_p (m_relation_set, v1) || !bitmap_bit_p (m_relation_set, v2))
1454 return VREL_VARYING;
1455
1456 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1457 {
1458 r = find_relation_block (bb->index, v1, v2);
1459 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 dom_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1471 {
1472 relation_kind kind;
1473 unsigned v1 = SSA_NAME_VERSION (ssa1);
1474 unsigned v2 = SSA_NAME_VERSION (ssa2);
1475 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 if ((!bitmap_bit_p (m_relation_set, v1) && !has_equiv_p (v1))
1481 || (!bitmap_bit_p (m_relation_set, v2) && !has_equiv_p (v2)))
1482 return partial_equiv (ssa1, ssa2);
1483
1484 // Check for equivalence first. They must be in each equivalency set.
1485 const_bitmap equiv1 = equiv_set (ssa1, bb);
1486 const_bitmap equiv2 = equiv_set (ssa2, bb);
1487 if (bitmap_bit_p (equiv1, v2) && bitmap_bit_p (equiv2, v1))
1488 return VREL_EQ;
1489
1490 kind = partial_equiv (ssa1, ssa2);
1491 if (kind != VREL_VARYING)
1492 return kind;
1493
1494 // Initially look for a direct relationship and just return that.
1495 kind = find_relation_dom (bb, v1, v2);
1496 if (kind != VREL_VARYING)
1497 return kind;
1498
1499 // Query using the equivalence sets.
1500 kind = query (bb, equiv1, equiv2);
1501 return kind;
1502 }
1503
1504 // Dump all the relations in block BB to file F.
1505
1506 void
1507 dom_oracle::dump (FILE *f, basic_block bb) const
1508 {
1509 equiv_oracle::dump (f,bb);
1510
1511 if (bb->index >= (int)m_relations.length ())
1512 return;
1513 if (!m_relations[bb->index].m_names)
1514 return;
1515
1516 value_relation vr;
1517 FOR_EACH_RELATION_BB (this, bb, vr)
1518 {
1519 fprintf (f, "Relational : ");
1520 vr.dump (f);
1521 fprintf (f, "\n");
1522 }
1523 }
1524
1525 // Dump all the relations known to file F.
1526
1527 void
1528 dom_oracle::dump (FILE *f) const
1529 {
1530 fprintf (f, "Relation dump\n");
1531 for (unsigned i = 0; i < m_relations.length (); i++)
1532 if (BASIC_BLOCK_FOR_FN (cfun, i))
1533 {
1534 fprintf (f, "BB%d\n", i);
1535 dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
1536 }
1537 }
1538
1539 void
1540 relation_oracle::debug () const
1541 {
1542 dump (stderr);
1543 }
1544
1545 path_oracle::path_oracle (relation_oracle *oracle)
1546 {
1547 set_root_oracle (oracle);
1548 bitmap_obstack_initialize (&m_bitmaps);
1549 obstack_init (&m_chain_obstack);
1550
1551 // Initialize header records.
1552 m_equiv.m_names = BITMAP_ALLOC (&m_bitmaps);
1553 m_equiv.m_bb = NULL;
1554 m_equiv.m_next = NULL;
1555 m_relations.m_names = BITMAP_ALLOC (&m_bitmaps);
1556 m_relations.m_head = NULL;
1557 m_killed_defs = BITMAP_ALLOC (&m_bitmaps);
1558 }
1559
1560 path_oracle::~path_oracle ()
1561 {
1562 obstack_free (&m_chain_obstack, NULL);
1563 bitmap_obstack_release (&m_bitmaps);
1564 }
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 path_oracle::equiv_set (tree ssa, basic_block bb)
1571 {
1572 // Check the list first.
1573 equiv_chain *ptr = m_equiv.find (SSA_NAME_VERSION (ssa));
1574 if (ptr)
1575 return ptr->m_names;
1576
1577 // Otherwise defer to the root oracle.
1578 if (m_root)
1579 return m_root->equiv_set (ssa, bb);
1580
1581 // Allocate a throw away bitmap if there isn't a root oracle.
1582 bitmap tmp = BITMAP_ALLOC (&m_bitmaps);
1583 bitmap_set_bit (tmp, SSA_NAME_VERSION (ssa));
1584 return tmp;
1585 }
1586
1587 // Register an equivalence between SSA1 and SSA2 resolving unknowns from
1588 // block BB.
1589
1590 void
1591 path_oracle::register_equiv (basic_block bb, tree ssa1, tree ssa2)
1592 {
1593 const_bitmap equiv_1 = equiv_set (ssa1, bb);
1594 const_bitmap equiv_2 = equiv_set (ssa2, bb);
1595
1596 // Check if they are the same set, if so, we're done.
1597 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 bitmap b = BITMAP_ALLOC (&m_bitmaps);
1602 valid_equivs (b, equiv_1, bb);
1603 valid_equivs (b, equiv_2, bb);
1604
1605 equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1606 sizeof (equiv_chain));
1607 ptr->m_names = b;
1608 ptr->m_bb = NULL;
1609 ptr->m_next = m_equiv.m_next;
1610 m_equiv.m_next = ptr;
1611 bitmap_ior_into (m_equiv.m_names, b);
1612 }
1613
1614 // Register killing definition of an SSA_NAME.
1615
1616 void
1617 path_oracle::killing_def (tree ssa)
1618 {
1619 if (dump_file && (dump_flags & TDF_DETAILS))
1620 {
1621 fprintf (dump_file, " Registering killing_def (path_oracle) ");
1622 print_generic_expr (dump_file, ssa, TDF_SLIM);
1623 fprintf (dump_file, "\n");
1624 }
1625
1626 unsigned v = SSA_NAME_VERSION (ssa);
1627
1628 bitmap_set_bit (m_killed_defs, v);
1629 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 bitmap b = BITMAP_ALLOC (&m_bitmaps);
1633 bitmap_set_bit (b, v);
1634 equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1635 sizeof (equiv_chain));
1636 ptr->m_names = b;
1637 ptr->m_bb = NULL;
1638 ptr->m_next = m_equiv.m_next;
1639 m_equiv.m_next = ptr;
1640
1641 // Walk the relation list and remove SSA from any relations.
1642 if (!bitmap_bit_p (m_relations.m_names, v))
1643 return;
1644
1645 bitmap_clear_bit (m_relations.m_names, v);
1646 relation_chain **prev = &(m_relations.m_head);
1647 relation_chain *next = NULL;
1648 for (relation_chain *ptr = m_relations.m_head; ptr; ptr = next)
1649 {
1650 gcc_checking_assert (*prev == ptr);
1651 next = ptr->m_next;
1652 if (SSA_NAME_VERSION (ptr->op1 ()) == v
1653 || SSA_NAME_VERSION (ptr->op2 ()) == v)
1654 *prev = ptr->m_next;
1655 else
1656 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 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 if (ssa1 == ssa2)
1669 return;
1670
1671 if (dump_file && (dump_flags & TDF_DETAILS))
1672 {
1673 value_relation vr (k, ssa1, ssa2);
1674 fprintf (dump_file, " Registering value_relation (path_oracle) ");
1675 vr.dump (dump_file);
1676 fprintf (dump_file, " (root: bb%d)\n", bb->index);
1677 }
1678
1679 relation_kind curr = query (bb, ssa1, ssa2);
1680 if (curr != VREL_VARYING)
1681 k = relation_intersect (curr, k);
1682
1683 if (k == VREL_EQ)
1684 {
1685 register_equiv (bb, ssa1, ssa2);
1686 return;
1687 }
1688
1689 bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa1));
1690 bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa2));
1691 relation_chain *ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1692 sizeof (relation_chain));
1693 ptr->set_relation (k, ssa1, ssa2);
1694 ptr->m_next = m_relations.m_head;
1695 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 path_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1703 {
1704 if (bitmap_equal_p (b1, b2))
1705 return VREL_EQ;
1706
1707 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 if (bitmap_intersect_p (m_killed_defs, b1)
1712 || bitmap_intersect_p (m_killed_defs, b2))
1713 return k;
1714
1715 if (k == VREL_VARYING && m_root)
1716 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 path_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1726 {
1727 unsigned v1 = SSA_NAME_VERSION (ssa1);
1728 unsigned v2 = SSA_NAME_VERSION (ssa2);
1729
1730 if (v1 == v2)
1731 return VREL_EQ;
1732
1733 const_bitmap equiv_1 = equiv_set (ssa1, bb);
1734 const_bitmap equiv_2 = equiv_set (ssa2, bb);
1735 if (bitmap_bit_p (equiv_1, v2) && bitmap_bit_p (equiv_2, v1))
1736 return VREL_EQ;
1737
1738 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 path_oracle::reset_path (relation_oracle *oracle)
1746 {
1747 set_root_oracle (oracle);
1748 m_equiv.m_next = NULL;
1749 bitmap_clear (m_equiv.m_names);
1750 m_relations.m_head = NULL;
1751 bitmap_clear (m_relations.m_names);
1752 bitmap_clear (m_killed_defs);
1753 }
1754
1755 // Dump relation in basic block... Do nothing here.
1756
1757 void
1758 path_oracle::dump (FILE *, basic_block) const
1759 {
1760 }
1761
1762 // Dump the relations and equivalencies found in the path.
1763
1764 void
1765 path_oracle::dump (FILE *f) const
1766 {
1767 equiv_chain *ptr = m_equiv.m_next;
1768 relation_chain *ptr2 = m_relations.m_head;
1769
1770 if (ptr || ptr2)
1771 fprintf (f, "\npath_oracle:\n");
1772
1773 for (; ptr; ptr = ptr->m_next)
1774 ptr->dump (f);
1775
1776 for (; ptr2; ptr2 = ptr2->m_next)
1777 {
1778 fprintf (f, "Relational : ");
1779 ptr2->dump (f);
1780 fprintf (f, "\n");
1781 }
1782 }
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 equiv_relation_iterator::equiv_relation_iterator (relation_oracle *oracle,
1791 basic_block bb, tree name,
1792 bool full, bool partial)
1793 {
1794 m_name = name;
1795 m_oracle = oracle;
1796 m_pe = partial ? oracle->partial_equiv_set (name) : NULL;
1797 m_bm = NULL;
1798 if (full)
1799 m_bm = oracle->equiv_set (name, bb);
1800 if (!m_bm && m_pe)
1801 m_bm = m_pe->members;
1802 if (m_bm)
1803 bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1804 }
1805
1806 // Move to the next export bitmap spot.
1807
1808 void
1809 equiv_relation_iterator::next ()
1810 {
1811 bmp_iter_next (&m_bi, &m_y);
1812 }
1813
1814 // Fetch the name of the next export in the export list. Return NULL if
1815 // iteration is done.
1816
1817 tree
1818 equiv_relation_iterator::get_name (relation_kind *rel)
1819 {
1820 if (!m_bm)
1821 return NULL_TREE;
1822
1823 while (bmp_iter_set (&m_bi, &m_y))
1824 {
1825 // Do not return self.
1826 tree t = ssa_name (m_y);
1827 if (t && t != m_name)
1828 {
1829 relation_kind k = VREL_EQ;
1830 if (m_pe && m_bm == m_pe->members)
1831 {
1832 const pe_slice *equiv_pe = m_oracle->partial_equiv_set (t);
1833 if (equiv_pe && equiv_pe->members == m_pe->members)
1834 k = pe_min (m_pe->code, equiv_pe->code);
1835 else
1836 k = VREL_VARYING;
1837 }
1838 if (relation_equiv_p (k))
1839 {
1840 if (rel)
1841 *rel = k;
1842 return t;
1843 }
1844 }
1845 next ();
1846 }
1847
1848 // Process partial equivs after full equivs if both were requested.
1849 if (m_pe && m_bm != m_pe->members)
1850 {
1851 m_bm = m_pe->members;
1852 if (m_bm)
1853 {
1854 // Recursively call back to process First PE.
1855 bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1856 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 relation_tests ()
1869 {
1870 // rr_*_table tables use unsigned char rather than relation_kind.
1871 ASSERT_LT (VREL_LAST, UCHAR_MAX);
1872 // Verify commutativity of relation_intersect and relation_union.
1873 for (relation_kind r1 = VREL_VARYING; r1 < VREL_PE8;
1874 r1 = relation_kind (r1 + 1))
1875 for (relation_kind r2 = VREL_VARYING; r2 < VREL_PE8;
1876 r2 = relation_kind (r2 + 1))
1877 {
1878 ASSERT_EQ (relation_intersect (r1, r2), relation_intersect (r2, r1));
1879 ASSERT_EQ (relation_union (r1, r2), relation_union (r2, r1));
1880 }
1881 }
1882
1883 } // namespace selftest
1884
1885 #endif // CHECKING_P