libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / value-relation.cc
blobd8a2ed920a82ed83d22079f358629a945767fb82
1 /* Header file for the value range relational processing.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
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"
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"
35 static const char *const kind_string[VREL_LAST] =
36 { "varying", "undefined", "<", "<=", ">", ">=", "==", "!=", "pe8", "pe16",
37 "pe32", "pe64" };
39 // Print a relation_kind REL to file F.
41 void
42 print_relation (FILE *f, relation_kind rel)
44 fprintf (f, " %s ", kind_string[rel]);
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 };
52 // Negate the relation, as in logical negation.
54 relation_kind
55 relation_negate (relation_kind r)
57 return relation_kind (rr_negate_table [r]);
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 };
65 // Return the relation as if the operands were swapped.
67 relation_kind
68 relation_swap (relation_kind r)
70 return relation_kind (rr_swap_table [r]);
73 // This table is used to perform an intersection between 2 relations.
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 } };
102 // Intersect relation R1 with relation R2 and return the resulting relation.
104 relation_kind
105 relation_intersect (relation_kind r1, relation_kind r2)
107 return relation_kind (rr_intersect_table[r1][r2]);
111 // This table is used to perform a union between 2 relations.
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 } };
139 // Union relation R1 with relation R2 and return the result.
141 relation_kind
142 relation_union (relation_kind r1, relation_kind r2)
144 return relation_kind (rr_union_table[r1][r2]);
148 // This table is used to determine transitivity between 2 relations.
149 // (A relation0 B) and (B relation1 C) implies (A result C)
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 } };
177 // Apply transitive operation between relation R1 and relation R2, and
178 // return the resulting relation, if any.
180 relation_kind
181 relation_transitive (relation_kind r1, relation_kind r2)
183 return relation_kind (rr_transitive_table[r1][r2]);
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.
190 void
191 adjust_equivalence_range (vrange &range)
193 if (range.undefined_p () || !is_a<frange> (range))
194 return;
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))
200 frange zeros (range.type (), dconstm0, dconst0);
201 range.union_ (zeros);
205 // This vector maps a relation to the equivalent tree code.
207 static const tree_code relation_to_code [VREL_LAST] = {
208 ERROR_MARK, ERROR_MARK, LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EQ_EXPR,
209 NE_EXPR };
211 // Given an equivalence set EQUIV, set all the bits in B that are still valid
212 // members of EQUIV in basic block BB.
214 void
215 relation_oracle::valid_equivs (bitmap b, const_bitmap equivs, basic_block bb)
217 unsigned i;
218 bitmap_iterator bi;
219 EXECUTE_IF_SET_IN_BITMAP (equivs, 0, i, bi)
221 tree ssa = ssa_name (i);
222 if (ssa && !SSA_NAME_IN_FREE_LIST (ssa))
224 const_bitmap ssa_equiv = equiv_set (ssa, bb);
225 if (ssa_equiv == equivs)
226 bitmap_set_bit (b, i);
231 // Return any known relation between SSA1 and SSA2 before stmt S is executed.
232 // If GET_RANGE is true, query the range of both operands first to ensure
233 // the definitions have been processed and any relations have be created.
235 relation_kind
236 relation_oracle::query (gimple *s, tree ssa1, tree ssa2)
238 if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
239 return VREL_VARYING;
240 return query (gimple_bb (s), ssa1, ssa2);
243 // Return any known relation between SSA1 and SSA2 on edge E.
244 // If GET_RANGE is true, query the range of both operands first to ensure
245 // the definitions have been processed and any relations have be created.
247 relation_kind
248 relation_oracle::query (edge e, tree ssa1, tree ssa2)
250 basic_block bb;
251 if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
252 return VREL_VARYING;
254 // Use destination block if it has a single predecessor, and this picks
255 // up any relation on the edge.
256 // Otherwise choose the src edge and the result is the same as on-exit.
257 if (!single_pred_p (e->dest))
258 bb = e->src;
259 else
260 bb = e->dest;
262 return query (bb, ssa1, ssa2);
264 // -------------------------------------------------------------------------
266 // The very first element in the m_equiv chain is actually just a summary
267 // element in which the m_names bitmap is used to indicate that an ssa_name
268 // has an equivalence set in this block.
269 // This allows for much faster traversal of the DOM chain, as a search for
270 // SSA_NAME simply requires walking the DOM chain until a block is found
271 // which has the bit for SSA_NAME set. Then scan for the equivalency set in
272 // that block. No previous lists need be searched.
274 // If SSA has an equivalence in this list, find and return it.
275 // Otherwise return NULL.
277 equiv_chain *
278 equiv_chain::find (unsigned ssa)
280 equiv_chain *ptr = NULL;
281 // If there are equiv sets and SSA is in one in this list, find it.
282 // Otherwise return NULL.
283 if (bitmap_bit_p (m_names, ssa))
285 for (ptr = m_next; ptr; ptr = ptr->m_next)
286 if (bitmap_bit_p (ptr->m_names, ssa))
287 break;
289 return ptr;
292 // Dump the names in this equivalence set.
294 void
295 equiv_chain::dump (FILE *f) const
297 bitmap_iterator bi;
298 unsigned i;
300 if (!m_names || bitmap_empty_p (m_names))
301 return;
302 fprintf (f, "Equivalence set : [");
303 unsigned c = 0;
304 EXECUTE_IF_SET_IN_BITMAP (m_names, 0, i, bi)
306 if (ssa_name (i))
308 if (c++)
309 fprintf (f, ", ");
310 print_generic_expr (f, ssa_name (i), TDF_SLIM);
313 fprintf (f, "]\n");
316 // Instantiate an equivalency oracle.
318 equiv_oracle::equiv_oracle ()
320 bitmap_obstack_initialize (&m_bitmaps);
321 m_equiv.create (0);
322 m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
323 m_equiv_set = BITMAP_ALLOC (&m_bitmaps);
324 bitmap_tree_view (m_equiv_set);
325 obstack_init (&m_chain_obstack);
326 m_self_equiv.create (0);
327 m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
328 m_partial.create (0);
329 m_partial.safe_grow_cleared (num_ssa_names + 1);
332 // Destruct an equivalency oracle.
334 equiv_oracle::~equiv_oracle ()
336 m_partial.release ();
337 m_self_equiv.release ();
338 obstack_free (&m_chain_obstack, NULL);
339 m_equiv.release ();
340 bitmap_obstack_release (&m_bitmaps);
343 // Add a partial equivalence R between OP1 and OP2.
345 void
346 equiv_oracle::add_partial_equiv (relation_kind r, tree op1, tree op2)
348 int v1 = SSA_NAME_VERSION (op1);
349 int v2 = SSA_NAME_VERSION (op2);
350 int prec2 = TYPE_PRECISION (TREE_TYPE (op2));
351 int bits = pe_to_bits (r);
352 gcc_checking_assert (bits && prec2 >= bits);
354 if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
355 m_partial.safe_grow_cleared (num_ssa_names + 1);
356 gcc_checking_assert (v1 < (int)m_partial.length ()
357 && v2 < (int)m_partial.length ());
359 pe_slice &pe1 = m_partial[v1];
360 pe_slice &pe2 = m_partial[v2];
362 if (pe1.members)
364 // If the definition pe1 already has an entry, either the stmt is
365 // being re-evaluated, or the def was used before being registered.
366 // In either case, if PE2 has an entry, we simply do nothing.
367 if (pe2.members)
368 return;
369 // If there are no uses of op2, do not register.
370 if (has_zero_uses (op2))
371 return;
372 // PE1 is the LHS and already has members, so everything in the set
373 // should be a slice of PE2 rather than PE1.
374 pe2.code = pe_min (r, pe1.code);
375 pe2.ssa_base = op2;
376 pe2.members = pe1.members;
377 bitmap_iterator bi;
378 unsigned x;
379 EXECUTE_IF_SET_IN_BITMAP (pe1.members, 0, x, bi)
381 m_partial[x].ssa_base = op2;
382 m_partial[x].code = pe_min (m_partial[x].code, pe2.code);
384 bitmap_set_bit (pe1.members, v2);
385 return;
387 if (pe2.members)
389 // If there are no uses of op1, do not register.
390 if (has_zero_uses (op1))
391 return;
392 pe1.ssa_base = pe2.ssa_base;
393 // If pe2 is a 16 bit value, but only an 8 bit copy, we can't be any
394 // more than an 8 bit equivalence here, so choose MIN value.
395 pe1.code = pe_min (r, pe2.code);
396 pe1.members = pe2.members;
397 bitmap_set_bit (pe1.members, v1);
399 else
401 // If there are no uses of either operand, do not register.
402 if (has_zero_uses (op1) || has_zero_uses (op2))
403 return;
404 // Neither name has an entry, simply create op1 as slice of op2.
405 pe2.code = bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2)));
406 if (pe2.code == VREL_VARYING)
407 return;
408 pe2.ssa_base = op2;
409 pe2.members = BITMAP_ALLOC (&m_bitmaps);
410 bitmap_set_bit (pe2.members, v2);
411 pe1.ssa_base = op2;
412 pe1.code = r;
413 pe1.members = pe2.members;
414 bitmap_set_bit (pe1.members, v1);
418 // Return the set of partial equivalences associated with NAME. The bitmap
419 // will be NULL if there are none.
421 const pe_slice *
422 equiv_oracle::partial_equiv_set (tree name)
424 int v = SSA_NAME_VERSION (name);
425 if (v >= (int)m_partial.length ())
426 return NULL;
427 return &m_partial[v];
430 // Query if there is a partial equivalence between SSA1 and SSA2. Return
431 // VREL_VARYING if there is not one. If BASE is non-null, return the base
432 // ssa-name this is a slice of.
434 relation_kind
435 equiv_oracle::partial_equiv (tree ssa1, tree ssa2, tree *base) const
437 int v1 = SSA_NAME_VERSION (ssa1);
438 int v2 = SSA_NAME_VERSION (ssa2);
440 if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
441 return VREL_VARYING;
443 const pe_slice &pe1 = m_partial[v1];
444 const pe_slice &pe2 = m_partial[v2];
445 if (pe1.members && pe2.members == pe1.members)
447 if (base)
448 *base = pe1.ssa_base;
449 return pe_min (pe1.code, pe2.code);
451 return VREL_VARYING;
455 // Find and return the equivalency set for SSA along the dominators of BB.
456 // This is the external API.
458 const_bitmap
459 equiv_oracle::equiv_set (tree ssa, basic_block bb)
461 // Search the dominator tree for an equivalency.
462 equiv_chain *equiv = find_equiv_dom (ssa, bb);
463 if (equiv)
464 return equiv->m_names;
466 // Otherwise return a cached equiv set containing just this SSA.
467 unsigned v = SSA_NAME_VERSION (ssa);
468 if (v >= m_self_equiv.length ())
469 m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
471 if (!m_self_equiv[v])
473 m_self_equiv[v] = BITMAP_ALLOC (&m_bitmaps);
474 bitmap_set_bit (m_self_equiv[v], v);
476 return m_self_equiv[v];
479 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
481 relation_kind
482 equiv_oracle::query (basic_block bb, tree ssa1, tree ssa2)
484 // If the 2 ssa names share the same equiv set, they are equal.
485 if (equiv_set (ssa1, bb) == equiv_set (ssa2, bb))
486 return VREL_EQ;
488 // Check if there is a partial equivalence.
489 return partial_equiv (ssa1, ssa2);
492 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
494 relation_kind
495 equiv_oracle::query (basic_block bb ATTRIBUTE_UNUSED, const_bitmap e1,
496 const_bitmap e2)
498 // If the 2 ssa names share the same equiv set, they are equal.
499 if (bitmap_equal_p (e1, e2))
500 return VREL_EQ;
501 return VREL_VARYING;
504 // If SSA has an equivalence in block BB, find and return it.
505 // Otherwise return NULL.
507 equiv_chain *
508 equiv_oracle::find_equiv_block (unsigned ssa, int bb) const
510 if (bb >= (int)m_equiv.length () || !m_equiv[bb])
511 return NULL;
513 return m_equiv[bb]->find (ssa);
516 // Starting at block BB, walk the dominator chain looking for the nearest
517 // equivalence set containing NAME.
519 equiv_chain *
520 equiv_oracle::find_equiv_dom (tree name, basic_block bb) const
522 unsigned v = SSA_NAME_VERSION (name);
523 // Short circuit looking for names which have no equivalences.
524 // Saves time looking for something which does not exist.
525 if (!bitmap_bit_p (m_equiv_set, v))
526 return NULL;
528 // NAME has at least once equivalence set, check to see if it has one along
529 // the dominator tree.
530 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
532 equiv_chain *ptr = find_equiv_block (v, bb->index);
533 if (ptr)
534 return ptr;
536 return NULL;
539 // Register equivalence between ssa_name V and set EQUIV in block BB,
541 bitmap
542 equiv_oracle::register_equiv (basic_block bb, unsigned v, equiv_chain *equiv)
544 // V will have an equivalency now.
545 bitmap_set_bit (m_equiv_set, v);
547 // If that equiv chain is in this block, simply use it.
548 if (equiv->m_bb == bb)
550 bitmap_set_bit (equiv->m_names, v);
551 bitmap_set_bit (m_equiv[bb->index]->m_names, v);
552 return NULL;
555 // Otherwise create an equivalence for this block which is a copy
556 // of equiv, the add V to the set.
557 bitmap b = BITMAP_ALLOC (&m_bitmaps);
558 valid_equivs (b, equiv->m_names, bb);
559 bitmap_set_bit (b, v);
560 return b;
563 // Register equivalence between set equiv_1 and equiv_2 in block BB.
564 // Return NULL if either name can be merged with the other. Otherwise
565 // return a pointer to the combined bitmap of names. This allows the
566 // caller to do any setup required for a new element.
568 bitmap
569 equiv_oracle::register_equiv (basic_block bb, equiv_chain *equiv_1,
570 equiv_chain *equiv_2)
572 // If equiv_1 is already in BB, use it as the combined set.
573 if (equiv_1->m_bb == bb)
575 valid_equivs (equiv_1->m_names, equiv_2->m_names, bb);
576 // Its hard to delete from a single linked list, so
577 // just clear the second one.
578 if (equiv_2->m_bb == bb)
579 bitmap_clear (equiv_2->m_names);
580 else
581 // Ensure the new names are in the summary for BB.
582 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_1->m_names);
583 return NULL;
585 // If equiv_2 is in BB, use it for the combined set.
586 if (equiv_2->m_bb == bb)
588 valid_equivs (equiv_2->m_names, equiv_1->m_names, bb);
589 // Ensure the new names are in the summary.
590 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_2->m_names);
591 return NULL;
594 // At this point, neither equivalence is from this block.
595 bitmap b = BITMAP_ALLOC (&m_bitmaps);
596 valid_equivs (b, equiv_1->m_names, bb);
597 valid_equivs (b, equiv_2->m_names, bb);
598 return b;
601 // Create an equivalency set containing only SSA in its definition block.
602 // This is done the first time SSA is registered in an equivalency and blocks
603 // any DOM searches past the definition.
605 void
606 equiv_oracle::register_initial_def (tree ssa)
608 if (SSA_NAME_IS_DEFAULT_DEF (ssa))
609 return;
610 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (ssa));
612 // If defining stmt is not in the IL, simply return.
613 if (!bb)
614 return;
615 gcc_checking_assert (!find_equiv_dom (ssa, bb));
617 unsigned v = SSA_NAME_VERSION (ssa);
618 bitmap_set_bit (m_equiv_set, v);
619 bitmap equiv_set = BITMAP_ALLOC (&m_bitmaps);
620 bitmap_set_bit (equiv_set, v);
621 add_equiv_to_block (bb, equiv_set);
624 // Register an equivalence between SSA1 and SSA2 in block BB.
625 // The equivalence oracle maintains a vector of equivalencies indexed by basic
626 // block. When an equivalence between SSA1 and SSA2 is registered in block BB,
627 // a query is made as to what equivalences both names have already, and
628 // any preexisting equivalences are merged to create a single equivalence
629 // containing all the ssa_names in this basic block.
631 void
632 equiv_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
634 // Process partial equivalencies.
635 if (relation_partial_equiv_p (k))
637 add_partial_equiv (k, ssa1, ssa2);
638 return;
640 // Only handle equality relations.
641 if (k != VREL_EQ)
642 return;
644 unsigned v1 = SSA_NAME_VERSION (ssa1);
645 unsigned v2 = SSA_NAME_VERSION (ssa2);
647 // If this is the first time an ssa_name has an equivalency registered
648 // create a self-equivalency record in the def block.
649 if (!bitmap_bit_p (m_equiv_set, v1))
650 register_initial_def (ssa1);
651 if (!bitmap_bit_p (m_equiv_set, v2))
652 register_initial_def (ssa2);
654 equiv_chain *equiv_1 = find_equiv_dom (ssa1, bb);
655 equiv_chain *equiv_2 = find_equiv_dom (ssa2, bb);
657 // Check if they are the same set
658 if (equiv_1 && equiv_1 == equiv_2)
659 return;
661 bitmap equiv_set;
663 // Case where we have 2 SSA_NAMEs that are not in any set.
664 if (!equiv_1 && !equiv_2)
666 bitmap_set_bit (m_equiv_set, v1);
667 bitmap_set_bit (m_equiv_set, v2);
669 equiv_set = BITMAP_ALLOC (&m_bitmaps);
670 bitmap_set_bit (equiv_set, v1);
671 bitmap_set_bit (equiv_set, v2);
673 else if (!equiv_1 && equiv_2)
674 equiv_set = register_equiv (bb, v1, equiv_2);
675 else if (equiv_1 && !equiv_2)
676 equiv_set = register_equiv (bb, v2, equiv_1);
677 else
678 equiv_set = register_equiv (bb, equiv_1, equiv_2);
680 // A non-null return is a bitmap that is to be added to the current
681 // block as a new equivalence.
682 if (!equiv_set)
683 return;
685 add_equiv_to_block (bb, equiv_set);
688 // Add an equivalency record in block BB containing bitmap EQUIV_SET.
689 // Note the internal caller is responsible for allocating EQUIV_SET properly.
691 void
692 equiv_oracle::add_equiv_to_block (basic_block bb, bitmap equiv_set)
694 equiv_chain *ptr;
696 // Check if this is the first time a block has an equivalence added.
697 // and create a header block. And set the summary for this block.
698 limit_check (bb);
699 if (!m_equiv[bb->index])
701 ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
702 sizeof (equiv_chain));
703 ptr->m_names = BITMAP_ALLOC (&m_bitmaps);
704 bitmap_copy (ptr->m_names, equiv_set);
705 ptr->m_bb = bb;
706 ptr->m_next = NULL;
707 m_equiv[bb->index] = ptr;
710 // Now create the element for this equiv set and initialize it.
711 ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack, sizeof (equiv_chain));
712 ptr->m_names = equiv_set;
713 ptr->m_bb = bb;
714 gcc_checking_assert (bb->index < (int)m_equiv.length ());
715 ptr->m_next = m_equiv[bb->index]->m_next;
716 m_equiv[bb->index]->m_next = ptr;
717 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_set);
720 // Make sure the BB vector is big enough and grow it if needed.
722 void
723 equiv_oracle::limit_check (basic_block bb)
725 int i = (bb) ? bb->index : last_basic_block_for_fn (cfun);
726 if (i >= (int)m_equiv.length ())
727 m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
730 // Dump the equivalence sets in BB to file F.
732 void
733 equiv_oracle::dump (FILE *f, basic_block bb) const
735 if (bb->index >= (int)m_equiv.length ())
736 return;
737 // Process equivalences.
738 if (m_equiv[bb->index])
740 equiv_chain *ptr = m_equiv[bb->index]->m_next;
741 for (; ptr; ptr = ptr->m_next)
742 ptr->dump (f);
744 // Look for partial equivalences defined in this block..
745 for (unsigned i = 0; i < num_ssa_names; i++)
747 tree name = ssa_name (i);
748 if (!gimple_range_ssa_p (name) || !SSA_NAME_DEF_STMT (name))
749 continue;
750 if (i >= m_partial.length ())
751 break;
752 tree base = m_partial[i].ssa_base;
753 if (base && name != base && gimple_bb (SSA_NAME_DEF_STMT (name)) == bb)
755 relation_kind k = partial_equiv (name, base);
756 if (k != VREL_VARYING)
758 value_relation vr (k, name, base);
759 fprintf (f, "Partial equiv ");
760 vr.dump (f);
761 fputc ('\n',f);
767 // Dump all equivalence sets known to the oracle.
769 void
770 equiv_oracle::dump (FILE *f) const
772 fprintf (f, "Equivalency dump\n");
773 for (unsigned i = 0; i < m_equiv.length (); i++)
774 if (m_equiv[i] && BASIC_BLOCK_FOR_FN (cfun, i))
776 fprintf (f, "BB%d\n", i);
777 dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
782 // --------------------------------------------------------------------------
783 // Negate the current relation.
785 void
786 value_relation::negate ()
788 related = relation_negate (related);
791 // Perform an intersection between 2 relations. *this &&= p.
793 bool
794 value_relation::intersect (value_relation &p)
796 // Save previous value
797 relation_kind old = related;
799 if (p.op1 () == op1 () && p.op2 () == op2 ())
800 related = relation_intersect (kind (), p.kind ());
801 else if (p.op2 () == op1 () && p.op1 () == op2 ())
802 related = relation_intersect (kind (), relation_swap (p.kind ()));
803 else
804 return false;
806 return old != related;
809 // Perform a union between 2 relations. *this ||= p.
811 bool
812 value_relation::union_ (value_relation &p)
814 // Save previous value
815 relation_kind old = related;
817 if (p.op1 () == op1 () && p.op2 () == op2 ())
818 related = relation_union (kind(), p.kind());
819 else if (p.op2 () == op1 () && p.op1 () == op2 ())
820 related = relation_union (kind(), relation_swap (p.kind ()));
821 else
822 return false;
824 return old != related;
827 // Identify and apply any transitive relations between REL
828 // and THIS. Return true if there was a transformation.
830 bool
831 value_relation::apply_transitive (const value_relation &rel)
833 relation_kind k = VREL_VARYING;
835 // Identify any common operand, and normalize the relations to
836 // the form : A < B B < C produces A < C
837 if (rel.op1 () == name2)
839 // A < B B < C
840 if (rel.op2 () == name1)
841 return false;
842 k = relation_transitive (kind (), rel.kind ());
843 if (k != VREL_VARYING)
845 related = k;
846 name2 = rel.op2 ();
847 return true;
850 else if (rel.op1 () == name1)
852 // B > A B < C
853 if (rel.op2 () == name2)
854 return false;
855 k = relation_transitive (relation_swap (kind ()), rel.kind ());
856 if (k != VREL_VARYING)
858 related = k;
859 name1 = name2;
860 name2 = rel.op2 ();
861 return true;
864 else if (rel.op2 () == name2)
866 // A < B C > B
867 if (rel.op1 () == name1)
868 return false;
869 k = relation_transitive (kind (), relation_swap (rel.kind ()));
870 if (k != VREL_VARYING)
872 related = k;
873 name2 = rel.op1 ();
874 return true;
877 else if (rel.op2 () == name1)
879 // B > A C > B
880 if (rel.op1 () == name2)
881 return false;
882 k = relation_transitive (relation_swap (kind ()),
883 relation_swap (rel.kind ()));
884 if (k != VREL_VARYING)
886 related = k;
887 name1 = name2;
888 name2 = rel.op1 ();
889 return true;
892 return false;
895 // Create a trio from this value relation given LHS, OP1 and OP2.
897 relation_trio
898 value_relation::create_trio (tree lhs, tree op1, tree op2)
900 relation_kind lhs_1;
901 if (lhs == name1 && op1 == name2)
902 lhs_1 = related;
903 else if (lhs == name2 && op1 == name1)
904 lhs_1 = relation_swap (related);
905 else
906 lhs_1 = VREL_VARYING;
908 relation_kind lhs_2;
909 if (lhs == name1 && op2 == name2)
910 lhs_2 = related;
911 else if (lhs == name2 && op2 == name1)
912 lhs_2 = relation_swap (related);
913 else
914 lhs_2 = VREL_VARYING;
916 relation_kind op_op;
917 if (op1 == name1 && op2 == name2)
918 op_op = related;
919 else if (op1 == name2 && op2 == name1)
920 op_op = relation_swap (related);
921 else if (op1 == op2)
922 op_op = VREL_EQ;
923 else
924 op_op = VREL_VARYING;
926 return relation_trio (lhs_1, lhs_2, op_op);
929 // Dump the relation to file F.
931 void
932 value_relation::dump (FILE *f) const
934 if (!name1 || !name2)
936 fprintf (f, "no relation registered");
937 return;
939 fputc ('(', f);
940 print_generic_expr (f, op1 (), TDF_SLIM);
941 print_relation (f, kind ());
942 print_generic_expr (f, op2 (), TDF_SLIM);
943 fputc(')', f);
946 // This container is used to link relations in a chain.
948 class relation_chain : public value_relation
950 public:
951 relation_chain *m_next;
954 // ------------------------------------------------------------------------
956 // Find the relation between any ssa_name in B1 and any name in B2 in LIST.
957 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
959 relation_kind
960 relation_chain_head::find_relation (const_bitmap b1, const_bitmap b2) const
962 if (!m_names)
963 return VREL_VARYING;
965 // If both b1 and b2 aren't referenced in this block, cant be a relation
966 if (!bitmap_intersect_p (m_names, b1) || !bitmap_intersect_p (m_names, b2))
967 return VREL_VARYING;
969 // Search for the first relation that contains BOTH an element from B1
970 // and B2, and return that relation.
971 for (relation_chain *ptr = m_head; ptr ; ptr = ptr->m_next)
973 unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
974 unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
975 if (bitmap_bit_p (b1, op1) && bitmap_bit_p (b2, op2))
976 return ptr->kind ();
977 if (bitmap_bit_p (b1, op2) && bitmap_bit_p (b2, op1))
978 return relation_swap (ptr->kind ());
981 return VREL_VARYING;
984 // Instantiate a relation oracle.
986 dom_oracle::dom_oracle (bool do_trans_p)
988 m_do_trans_p = do_trans_p;
989 m_relations.create (0);
990 m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
991 m_relation_set = BITMAP_ALLOC (&m_bitmaps);
992 m_tmp = BITMAP_ALLOC (&m_bitmaps);
993 m_tmp2 = BITMAP_ALLOC (&m_bitmaps);
996 // Destruct a relation oracle.
998 dom_oracle::~dom_oracle ()
1000 m_relations.release ();
1003 // Register relation K between ssa_name OP1 and OP2 on STMT.
1005 void
1006 relation_oracle::record (gimple *stmt, relation_kind k, tree op1, tree op2)
1008 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1009 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1010 gcc_checking_assert (stmt && gimple_bb (stmt));
1012 // Don't register lack of a relation.
1013 if (k == VREL_VARYING)
1014 return;
1016 if (dump_file && (dump_flags & TDF_DETAILS))
1018 value_relation vr (k, op1, op2);
1019 fprintf (dump_file, " Registering value_relation ");
1020 vr.dump (dump_file);
1021 fprintf (dump_file, " (bb%d) at ", gimple_bb (stmt)->index);
1022 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1025 // If an equivalence is being added between a PHI and one of its arguments
1026 // make sure that that argument is not defined in the same block.
1027 // This can happen along back edges and the equivalence will not be
1028 // applicable as it would require a use before def.
1029 if (k == VREL_EQ && is_a<gphi *> (stmt))
1031 tree phi_def = gimple_phi_result (stmt);
1032 gcc_checking_assert (phi_def == op1 || phi_def == op2);
1033 tree arg = op2;
1034 if (phi_def == op2)
1035 arg = op1;
1036 if (gimple_bb (stmt) == gimple_bb (SSA_NAME_DEF_STMT (arg)))
1038 if (dump_file && (dump_flags & TDF_DETAILS))
1040 fprintf (dump_file, " Not registered due to ");
1041 print_generic_expr (dump_file, arg, TDF_SLIM);
1042 fprintf (dump_file, " being defined in the same block.\n");
1044 return;
1047 record (gimple_bb (stmt), k, op1, op2);
1050 // Register relation K between ssa_name OP1 and OP2 on edge E.
1052 void
1053 relation_oracle::record (edge e, relation_kind k, tree op1, tree op2)
1055 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1056 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1058 // Do not register lack of relation, or blocks which have more than
1059 // edge E for a predecessor.
1060 if (k == VREL_VARYING || !single_pred_p (e->dest))
1061 return;
1063 if (dump_file && (dump_flags & TDF_DETAILS))
1065 value_relation vr (k, op1, op2);
1066 fprintf (dump_file, " Registering value_relation ");
1067 vr.dump (dump_file);
1068 fprintf (dump_file, " on (%d->%d)\n", e->src->index, e->dest->index);
1071 record (e->dest, k, op1, op2);
1074 // Register relation K between OP! and OP2 in block BB.
1075 // This creates the record and searches for existing records in the dominator
1076 // tree to merge with.
1078 void
1079 dom_oracle::record (basic_block bb, relation_kind k, tree op1, tree op2)
1081 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1082 // and no other relation makes sense.
1083 if (op1 == op2)
1084 return;
1086 // Equivalencies are handled by the equivalence oracle.
1087 if (relation_equiv_p (k))
1088 equiv_oracle::record (bb, k, op1, op2);
1089 else
1091 // if neither op1 nor op2 are in a relation before this is registered,
1092 // there will be no transitive.
1093 bool check = bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op1))
1094 || bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op2));
1095 relation_chain *ptr = set_one_relation (bb, k, op1, op2);
1096 if (ptr && check
1097 && (m_relations[bb->index].m_num_relations
1098 < param_relation_block_limit))
1099 register_transitives (bb, *ptr);
1103 // Register relation K between OP! and OP2 in block BB.
1104 // This creates the record and searches for existing records in the dominator
1105 // tree to merge with. Return the record, or NULL if no record was created.
1107 relation_chain *
1108 dom_oracle::set_one_relation (basic_block bb, relation_kind k, tree op1,
1109 tree op2)
1111 gcc_checking_assert (k != VREL_VARYING && k != VREL_EQ);
1113 value_relation vr(k, op1, op2);
1114 int bbi = bb->index;
1116 if (bbi >= (int)m_relations.length())
1117 m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1119 // Summary bitmap indicating what ssa_names have relations in this BB.
1120 bitmap bm = m_relations[bbi].m_names;
1121 if (!bm)
1122 bm = m_relations[bbi].m_names = BITMAP_ALLOC (&m_bitmaps);
1123 unsigned v1 = SSA_NAME_VERSION (op1);
1124 unsigned v2 = SSA_NAME_VERSION (op2);
1126 relation_kind curr;
1127 relation_chain *ptr;
1128 curr = find_relation_block (bbi, v1, v2, &ptr);
1129 // There is an existing relation in this block, just intersect with it.
1130 if (curr != VREL_VARYING)
1132 if (dump_file && (dump_flags & TDF_DETAILS))
1134 fprintf (dump_file, " Intersecting with existing ");
1135 ptr->dump (dump_file);
1137 // Check into whether we can simply replace the relation rather than
1138 // intersecting it. This may help with some optimistic iterative
1139 // updating algorithms.
1140 bool new_rel = ptr->intersect (vr);
1141 if (dump_file && (dump_flags & TDF_DETAILS))
1143 fprintf (dump_file, " to produce ");
1144 ptr->dump (dump_file);
1145 fprintf (dump_file, " %s.\n", new_rel ? "Updated" : "No Change");
1147 // If there was no change, return no record..
1148 if (!new_rel)
1149 return NULL;
1151 else
1153 if (m_relations[bbi].m_num_relations >= param_relation_block_limit)
1155 if (dump_file && (dump_flags & TDF_DETAILS))
1156 fprintf (dump_file, " Not registered due to bb being full\n");
1157 return NULL;
1159 m_relations[bbi].m_num_relations++;
1160 // Check for an existing relation further up the DOM chain.
1161 // By including dominating relations, The first one found in any search
1162 // will be the aggregate of all the previous ones.
1163 curr = find_relation_dom (bb, v1, v2);
1164 if (curr != VREL_VARYING)
1165 k = relation_intersect (curr, k);
1167 bitmap_set_bit (bm, v1);
1168 bitmap_set_bit (bm, v2);
1169 bitmap_set_bit (m_relation_set, v1);
1170 bitmap_set_bit (m_relation_set, v2);
1172 ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1173 sizeof (relation_chain));
1174 ptr->set_relation (k, op1, op2);
1175 ptr->m_next = m_relations[bbi].m_head;
1176 m_relations[bbi].m_head = ptr;
1178 return ptr;
1181 // Starting at ROOT_BB search the DOM tree looking for relations which
1182 // may produce transitive relations to RELATION. EQUIV1 and EQUIV2 are
1183 // bitmaps for op1/op2 and any of their equivalences that should also be
1184 // considered.
1186 void
1187 dom_oracle::register_transitives (basic_block root_bb,
1188 const value_relation &relation)
1190 // Only register transitives if they are requested.
1191 if (!m_do_trans_p)
1192 return;
1193 basic_block bb;
1194 // Only apply transitives to certain kinds of operations.
1195 switch (relation.kind ())
1197 case VREL_LE:
1198 case VREL_LT:
1199 case VREL_GT:
1200 case VREL_GE:
1201 break;
1202 default:
1203 return;
1206 const_bitmap equiv1 = equiv_set (relation.op1 (), root_bb);
1207 const_bitmap equiv2 = equiv_set (relation.op2 (), root_bb);
1209 const unsigned work_budget = param_transitive_relations_work_bound;
1210 unsigned avail_budget = work_budget;
1211 for (bb = root_bb; bb;
1212 /* Advancing to the next immediate dominator eats from the budget,
1213 if none is left after that there's no point to continue. */
1214 bb = (--avail_budget > 0
1215 ? get_immediate_dominator (CDI_DOMINATORS, bb) : nullptr))
1217 int bbi = bb->index;
1218 if (bbi >= (int)m_relations.length())
1219 continue;
1220 const_bitmap bm = m_relations[bbi].m_names;
1221 if (!bm)
1222 continue;
1223 if (!bitmap_intersect_p (bm, equiv1) && !bitmap_intersect_p (bm, equiv2))
1224 continue;
1225 // At least one of the 2 ops has a relation in this block.
1226 relation_chain *ptr;
1227 for (ptr = m_relations[bbi].m_head; ptr ; ptr = ptr->m_next)
1229 // In the presence of an equivalence, 2 operands may do not
1230 // naturally match. ie with equivalence a_2 == b_3
1231 // given c_1 < a_2 && b_3 < d_4
1232 // convert the second relation (b_3 < d_4) to match any
1233 // equivalences to found in the first relation.
1234 // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
1235 // transitive operation: c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
1237 tree r1, r2;
1238 tree p1 = ptr->op1 ();
1239 tree p2 = ptr->op2 ();
1240 // Find which equivalence is in the first operand.
1241 if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p1)))
1242 r1 = p1;
1243 else if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p2)))
1244 r1 = p2;
1245 else
1246 r1 = NULL_TREE;
1248 // Find which equivalence is in the second operand.
1249 if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p1)))
1250 r2 = p1;
1251 else if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p2)))
1252 r2 = p2;
1253 else
1254 r2 = NULL_TREE;
1256 // Ignore if both NULL (not relevant relation) or the same,
1257 if (r1 == r2)
1260 else
1262 // Any operand not an equivalence, just take the real operand.
1263 if (!r1)
1264 r1 = relation.op1 ();
1265 if (!r2)
1266 r2 = relation.op2 ();
1268 value_relation nr (relation.kind (), r1, r2);
1269 if (nr.apply_transitive (*ptr))
1271 // If the new relation is already present we know any
1272 // further processing is already reflected above it.
1273 // When we ran into the limit of relations on root_bb
1274 // we can give up as well.
1275 if (!set_one_relation (root_bb, nr.kind (),
1276 nr.op1 (), nr.op2 ()))
1277 return;
1278 if (dump_file && (dump_flags & TDF_DETAILS))
1280 fprintf (dump_file,
1281 " Registering transitive relation ");
1282 nr.dump (dump_file);
1283 fputc ('\n', dump_file);
1287 /* Processed one relation, abort if we've eaten up our budget. */
1288 if (--avail_budget == 0)
1289 return;
1294 // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
1295 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1297 relation_kind
1298 dom_oracle::find_relation_block (unsigned bb, const_bitmap b1,
1299 const_bitmap b2) const
1301 if (bb >= m_relations.length())
1302 return VREL_VARYING;
1304 return m_relations[bb].find_relation (b1, b2);
1307 // Search the DOM tree for a relation between an element of equivalency set B1
1308 // and B2, starting with block BB.
1310 relation_kind
1311 dom_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1313 relation_kind r;
1314 if (bitmap_equal_p (b1, b2))
1315 return VREL_EQ;
1317 // If either name does not occur in a relation anywhere, there isn't one.
1318 if (!bitmap_intersect_p (m_relation_set, b1)
1319 || !bitmap_intersect_p (m_relation_set, b2))
1320 return VREL_VARYING;
1322 // Search each block in the DOM tree checking.
1323 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1325 r = find_relation_block (bb->index, b1, b2);
1326 if (r != VREL_VARYING)
1327 return r;
1329 return VREL_VARYING;
1333 // Find a relation in block BB between ssa version V1 and V2. If a relation
1334 // is found, return a pointer to the chain object in OBJ.
1336 relation_kind
1337 dom_oracle::find_relation_block (int bb, unsigned v1, unsigned v2,
1338 relation_chain **obj) const
1340 if (bb >= (int)m_relations.length())
1341 return VREL_VARYING;
1343 const_bitmap bm = m_relations[bb].m_names;
1344 if (!bm)
1345 return VREL_VARYING;
1347 // If both b1 and b2 aren't referenced in this block, cant be a relation
1348 if (!bitmap_bit_p (bm, v1) || !bitmap_bit_p (bm, v2))
1349 return VREL_VARYING;
1351 relation_chain *ptr;
1352 for (ptr = m_relations[bb].m_head; ptr ; ptr = ptr->m_next)
1354 unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1355 unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1356 if (v1 == op1 && v2 == op2)
1358 if (obj)
1359 *obj = ptr;
1360 return ptr->kind ();
1362 if (v1 == op2 && v2 == op1)
1364 if (obj)
1365 *obj = ptr;
1366 return relation_swap (ptr->kind ());
1370 return VREL_VARYING;
1373 // Find a relation between SSA version V1 and V2 in the dominator tree
1374 // starting with block BB
1376 relation_kind
1377 dom_oracle::find_relation_dom (basic_block bb, unsigned v1, unsigned v2) const
1379 relation_kind r;
1380 // IF either name does not occur in a relation anywhere, there isn't one.
1381 if (!bitmap_bit_p (m_relation_set, v1) || !bitmap_bit_p (m_relation_set, v2))
1382 return VREL_VARYING;
1384 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1386 r = find_relation_block (bb->index, v1, v2);
1387 if (r != VREL_VARYING)
1388 return r;
1390 return VREL_VARYING;
1394 // Query if there is a relation between SSA1 and SS2 in block BB or a
1395 // dominator of BB
1397 relation_kind
1398 dom_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1400 relation_kind kind;
1401 unsigned v1 = SSA_NAME_VERSION (ssa1);
1402 unsigned v2 = SSA_NAME_VERSION (ssa2);
1403 if (v1 == v2)
1404 return VREL_EQ;
1406 // If v1 or v2 do not have any relations or equivalences, a partial
1407 // equivalence is the only possibility.
1408 if ((!bitmap_bit_p (m_relation_set, v1) && !has_equiv_p (v1))
1409 || (!bitmap_bit_p (m_relation_set, v2) && !has_equiv_p (v2)))
1410 return partial_equiv (ssa1, ssa2);
1412 // Check for equivalence first. They must be in each equivalency set.
1413 const_bitmap equiv1 = equiv_set (ssa1, bb);
1414 const_bitmap equiv2 = equiv_set (ssa2, bb);
1415 if (bitmap_bit_p (equiv1, v2) && bitmap_bit_p (equiv2, v1))
1416 return VREL_EQ;
1418 kind = partial_equiv (ssa1, ssa2);
1419 if (kind != VREL_VARYING)
1420 return kind;
1422 // Initially look for a direct relationship and just return that.
1423 kind = find_relation_dom (bb, v1, v2);
1424 if (kind != VREL_VARYING)
1425 return kind;
1427 // Query using the equivalence sets.
1428 kind = query (bb, equiv1, equiv2);
1429 return kind;
1432 // Dump all the relations in block BB to file F.
1434 void
1435 dom_oracle::dump (FILE *f, basic_block bb) const
1437 equiv_oracle::dump (f,bb);
1439 if (bb->index >= (int)m_relations.length ())
1440 return;
1441 if (!m_relations[bb->index].m_names)
1442 return;
1444 relation_chain *ptr = m_relations[bb->index].m_head;
1445 for (; ptr; ptr = ptr->m_next)
1447 fprintf (f, "Relational : ");
1448 ptr->dump (f);
1449 fprintf (f, "\n");
1453 // Dump all the relations known to file F.
1455 void
1456 dom_oracle::dump (FILE *f) const
1458 fprintf (f, "Relation dump\n");
1459 for (unsigned i = 0; i < m_relations.length (); i++)
1460 if (BASIC_BLOCK_FOR_FN (cfun, i))
1462 fprintf (f, "BB%d\n", i);
1463 dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
1467 void
1468 relation_oracle::debug () const
1470 dump (stderr);
1473 path_oracle::path_oracle (relation_oracle *oracle)
1475 set_root_oracle (oracle);
1476 bitmap_obstack_initialize (&m_bitmaps);
1477 obstack_init (&m_chain_obstack);
1479 // Initialize header records.
1480 m_equiv.m_names = BITMAP_ALLOC (&m_bitmaps);
1481 m_equiv.m_bb = NULL;
1482 m_equiv.m_next = NULL;
1483 m_relations.m_names = BITMAP_ALLOC (&m_bitmaps);
1484 m_relations.m_head = NULL;
1485 m_killed_defs = BITMAP_ALLOC (&m_bitmaps);
1488 path_oracle::~path_oracle ()
1490 obstack_free (&m_chain_obstack, NULL);
1491 bitmap_obstack_release (&m_bitmaps);
1494 // Return the equiv set for SSA, and if there isn't one, check for equivs
1495 // starting in block BB.
1497 const_bitmap
1498 path_oracle::equiv_set (tree ssa, basic_block bb)
1500 // Check the list first.
1501 equiv_chain *ptr = m_equiv.find (SSA_NAME_VERSION (ssa));
1502 if (ptr)
1503 return ptr->m_names;
1505 // Otherwise defer to the root oracle.
1506 if (m_root)
1507 return m_root->equiv_set (ssa, bb);
1509 // Allocate a throw away bitmap if there isn't a root oracle.
1510 bitmap tmp = BITMAP_ALLOC (&m_bitmaps);
1511 bitmap_set_bit (tmp, SSA_NAME_VERSION (ssa));
1512 return tmp;
1515 // Register an equivalence between SSA1 and SSA2 resolving unknowns from
1516 // block BB.
1518 void
1519 path_oracle::register_equiv (basic_block bb, tree ssa1, tree ssa2)
1521 const_bitmap equiv_1 = equiv_set (ssa1, bb);
1522 const_bitmap equiv_2 = equiv_set (ssa2, bb);
1524 // Check if they are the same set, if so, we're done.
1525 if (bitmap_equal_p (equiv_1, equiv_2))
1526 return;
1528 // Don't mess around, simply create a new record and insert it first.
1529 bitmap b = BITMAP_ALLOC (&m_bitmaps);
1530 valid_equivs (b, equiv_1, bb);
1531 valid_equivs (b, equiv_2, bb);
1533 equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1534 sizeof (equiv_chain));
1535 ptr->m_names = b;
1536 ptr->m_bb = NULL;
1537 ptr->m_next = m_equiv.m_next;
1538 m_equiv.m_next = ptr;
1539 bitmap_ior_into (m_equiv.m_names, b);
1542 // Register killing definition of an SSA_NAME.
1544 void
1545 path_oracle::killing_def (tree ssa)
1547 if (dump_file && (dump_flags & TDF_DETAILS))
1549 fprintf (dump_file, " Registering killing_def (path_oracle) ");
1550 print_generic_expr (dump_file, ssa, TDF_SLIM);
1551 fprintf (dump_file, "\n");
1554 unsigned v = SSA_NAME_VERSION (ssa);
1556 bitmap_set_bit (m_killed_defs, v);
1557 bitmap_set_bit (m_equiv.m_names, v);
1559 // Now add an equivalency with itself so we don't look to the root oracle.
1560 bitmap b = BITMAP_ALLOC (&m_bitmaps);
1561 bitmap_set_bit (b, v);
1562 equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1563 sizeof (equiv_chain));
1564 ptr->m_names = b;
1565 ptr->m_bb = NULL;
1566 ptr->m_next = m_equiv.m_next;
1567 m_equiv.m_next = ptr;
1569 // Walk the relation list and remove SSA from any relations.
1570 if (!bitmap_bit_p (m_relations.m_names, v))
1571 return;
1573 bitmap_clear_bit (m_relations.m_names, v);
1574 relation_chain **prev = &(m_relations.m_head);
1575 relation_chain *next = NULL;
1576 for (relation_chain *ptr = m_relations.m_head; ptr; ptr = next)
1578 gcc_checking_assert (*prev == ptr);
1579 next = ptr->m_next;
1580 if (SSA_NAME_VERSION (ptr->op1 ()) == v
1581 || SSA_NAME_VERSION (ptr->op2 ()) == v)
1582 *prev = ptr->m_next;
1583 else
1584 prev = &(ptr->m_next);
1588 // Register relation K between SSA1 and SSA2, resolving unknowns by
1589 // querying from BB.
1591 void
1592 path_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
1594 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1595 // and no other relation makes sense.
1596 if (ssa1 == ssa2)
1597 return;
1599 if (dump_file && (dump_flags & TDF_DETAILS))
1601 value_relation vr (k, ssa1, ssa2);
1602 fprintf (dump_file, " Registering value_relation (path_oracle) ");
1603 vr.dump (dump_file);
1604 fprintf (dump_file, " (root: bb%d)\n", bb->index);
1607 relation_kind curr = query (bb, ssa1, ssa2);
1608 if (curr != VREL_VARYING)
1609 k = relation_intersect (curr, k);
1611 if (k == VREL_EQ)
1613 register_equiv (bb, ssa1, ssa2);
1614 return;
1617 bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa1));
1618 bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa2));
1619 relation_chain *ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1620 sizeof (relation_chain));
1621 ptr->set_relation (k, ssa1, ssa2);
1622 ptr->m_next = m_relations.m_head;
1623 m_relations.m_head = ptr;
1626 // Query for a relationship between equiv set B1 and B2, resolving unknowns
1627 // starting at block BB.
1629 relation_kind
1630 path_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1632 if (bitmap_equal_p (b1, b2))
1633 return VREL_EQ;
1635 relation_kind k = m_relations.find_relation (b1, b2);
1637 // Do not look at the root oracle for names that have been killed
1638 // along the path.
1639 if (bitmap_intersect_p (m_killed_defs, b1)
1640 || bitmap_intersect_p (m_killed_defs, b2))
1641 return k;
1643 if (k == VREL_VARYING && m_root)
1644 k = m_root->query (bb, b1, b2);
1646 return k;
1649 // Query for a relationship between SSA1 and SSA2, resolving unknowns
1650 // starting at block BB.
1652 relation_kind
1653 path_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1655 unsigned v1 = SSA_NAME_VERSION (ssa1);
1656 unsigned v2 = SSA_NAME_VERSION (ssa2);
1658 if (v1 == v2)
1659 return VREL_EQ;
1661 const_bitmap equiv_1 = equiv_set (ssa1, bb);
1662 const_bitmap equiv_2 = equiv_set (ssa2, bb);
1663 if (bitmap_bit_p (equiv_1, v2) && bitmap_bit_p (equiv_2, v1))
1664 return VREL_EQ;
1666 return query (bb, equiv_1, equiv_2);
1669 // Reset any relations registered on this path. ORACLE is the root
1670 // oracle to use.
1672 void
1673 path_oracle::reset_path (relation_oracle *oracle)
1675 set_root_oracle (oracle);
1676 m_equiv.m_next = NULL;
1677 bitmap_clear (m_equiv.m_names);
1678 m_relations.m_head = NULL;
1679 bitmap_clear (m_relations.m_names);
1680 bitmap_clear (m_killed_defs);
1683 // Dump relation in basic block... Do nothing here.
1685 void
1686 path_oracle::dump (FILE *, basic_block) const
1690 // Dump the relations and equivalencies found in the path.
1692 void
1693 path_oracle::dump (FILE *f) const
1695 equiv_chain *ptr = m_equiv.m_next;
1696 relation_chain *ptr2 = m_relations.m_head;
1698 if (ptr || ptr2)
1699 fprintf (f, "\npath_oracle:\n");
1701 for (; ptr; ptr = ptr->m_next)
1702 ptr->dump (f);
1704 for (; ptr2; ptr2 = ptr2->m_next)
1706 fprintf (f, "Relational : ");
1707 ptr2->dump (f);
1708 fprintf (f, "\n");
1712 // ------------------------------------------------------------------------
1713 // EQUIV iterator. Although we have bitmap iterators, don't expose that it
1714 // is currently a bitmap. Use an export iterator to hide future changes.
1716 // Construct a basic iterator over an equivalence bitmap.
1718 equiv_relation_iterator::equiv_relation_iterator (relation_oracle *oracle,
1719 basic_block bb, tree name,
1720 bool full, bool partial)
1722 m_name = name;
1723 m_oracle = oracle;
1724 m_pe = partial ? oracle->partial_equiv_set (name) : NULL;
1725 m_bm = NULL;
1726 if (full)
1727 m_bm = oracle->equiv_set (name, bb);
1728 if (!m_bm && m_pe)
1729 m_bm = m_pe->members;
1730 if (m_bm)
1731 bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1734 // Move to the next export bitmap spot.
1736 void
1737 equiv_relation_iterator::next ()
1739 bmp_iter_next (&m_bi, &m_y);
1742 // Fetch the name of the next export in the export list. Return NULL if
1743 // iteration is done.
1745 tree
1746 equiv_relation_iterator::get_name (relation_kind *rel)
1748 if (!m_bm)
1749 return NULL_TREE;
1751 while (bmp_iter_set (&m_bi, &m_y))
1753 // Do not return self.
1754 tree t = ssa_name (m_y);
1755 if (t && t != m_name)
1757 relation_kind k = VREL_EQ;
1758 if (m_pe && m_bm == m_pe->members)
1760 const pe_slice *equiv_pe = m_oracle->partial_equiv_set (t);
1761 if (equiv_pe && equiv_pe->members == m_pe->members)
1762 k = pe_min (m_pe->code, equiv_pe->code);
1763 else
1764 k = VREL_VARYING;
1766 if (relation_equiv_p (k))
1768 if (rel)
1769 *rel = k;
1770 return t;
1773 next ();
1776 // Process partial equivs after full equivs if both were requested.
1777 if (m_pe && m_bm != m_pe->members)
1779 m_bm = m_pe->members;
1780 if (m_bm)
1782 // Recursively call back to process First PE.
1783 bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1784 return get_name (rel);
1787 return NULL_TREE;
1790 #if CHECKING_P
1791 #include "selftest.h"
1793 namespace selftest
1795 void
1796 relation_tests ()
1798 // rr_*_table tables use unsigned char rather than relation_kind.
1799 ASSERT_LT (VREL_LAST, UCHAR_MAX);
1800 // Verify commutativity of relation_intersect and relation_union.
1801 for (relation_kind r1 = VREL_VARYING; r1 < VREL_PE8;
1802 r1 = relation_kind (r1 + 1))
1803 for (relation_kind r2 = VREL_VARYING; r2 < VREL_PE8;
1804 r2 = relation_kind (r2 + 1))
1806 ASSERT_EQ (relation_intersect (r1, r2), relation_intersect (r2, r1));
1807 ASSERT_EQ (relation_union (r1, r2), relation_union (r2, r1));
1811 } // namespace selftest
1813 #endif // CHECKING_P