Skip -fwhole-program when merging LTO options.
[official-gcc.git] / gcc / value-relation.cc
blob178a245f41a6347cf4f8c19c832eeca18206d124
1 /* Header file for the value range relational processing.
2 Copyright (C) 2020-2022 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 *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 relation_kind 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 rr_negate_table [r];
60 // This table is used to swap the operands. op1 REL op2 -> op2 REL op1.
61 relation_kind 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 rr_swap_table [r];
73 // This table is used to perform an intersection between 2 relations.
75 relation_kind 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 rr_intersect_table[r1][r2];
111 // This table is used to perform a union between 2 relations.
113 relation_kind 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_LT, VREL_LE, VREL_GT, VREL_GE, VREL_UNDEFINED,
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 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 relation_kind 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 rr_transitive_table[r1][r2];
186 // This vector maps a relation to the equivalent tree code.
188 tree_code relation_to_code [VREL_LAST] = {
189 ERROR_MARK, ERROR_MARK, LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EQ_EXPR,
190 NE_EXPR };
192 // This routine validates that a relation can be applied to a specific set of
193 // ranges. In particular, floating point x == x may not be true if the NaN bit
194 // is set in the range. Symbolically the oracle will determine x == x,
195 // but specific range instances may override this.
196 // To verify, attempt to fold the relation using the supplied ranges.
197 // One would expect [1,1] to be returned, anything else means there is something
198 // in the range preventing the relation from applying.
199 // If there is no mechanism to verify, assume the relation is acceptable.
201 relation_kind
202 relation_oracle::validate_relation (relation_kind rel, vrange &op1, vrange &op2)
204 // If there is no mapping to a tree code, leave the relation as is.
205 tree_code code = relation_to_code [rel];
206 if (code == ERROR_MARK)
207 return rel;
209 // Undefined ranges cannot be checked either.
210 if (op1.undefined_p () || op2.undefined_p ())
211 return rel;
213 tree t1 = op1.type ();
214 tree t2 = op2.type ();
216 // If the range types are not compatible, no relation can exist.
217 if (!range_compatible_p (t1, t2))
218 return VREL_VARYING;
220 // If there is no handler, leave the relation as is.
221 range_op_handler handler (code, t1);
222 if (!handler)
223 return rel;
225 // If the relation cannot be folded for any reason, leave as is.
226 Value_Range result (boolean_type_node);
227 if (!handler.fold_range (result, boolean_type_node, op1, op2,
228 relation_trio::op1_op2 (rel)))
229 return rel;
231 // The expression op1 REL op2 using REL should fold to [1,1].
232 // Any other result means the relation is not verified to be true.
233 if (result.varying_p () || result.zero_p ())
234 return VREL_VARYING;
236 return rel;
239 // If no range is available, create a varying range for each SSA name and
240 // verify.
242 relation_kind
243 relation_oracle::validate_relation (relation_kind rel, tree ssa1, tree ssa2)
245 Value_Range op1, op2;
246 op1.set_varying (TREE_TYPE (ssa1));
247 op2.set_varying (TREE_TYPE (ssa2));
249 return validate_relation (rel, op1, op2);
252 // Given an equivalence set EQUIV, set all the bits in B that are still valid
253 // members of EQUIV in basic block BB.
255 void
256 relation_oracle::valid_equivs (bitmap b, const_bitmap equivs, basic_block bb)
258 unsigned i;
259 bitmap_iterator bi;
260 EXECUTE_IF_SET_IN_BITMAP (equivs, 0, i, bi)
262 tree ssa = ssa_name (i);
263 const_bitmap ssa_equiv = equiv_set (ssa, bb);
264 if (ssa_equiv == equivs)
265 bitmap_set_bit (b, i);
269 // -------------------------------------------------------------------------
271 // The very first element in the m_equiv chain is actually just a summary
272 // element in which the m_names bitmap is used to indicate that an ssa_name
273 // has an equivalence set in this block.
274 // This allows for much faster traversal of the DOM chain, as a search for
275 // SSA_NAME simply requires walking the DOM chain until a block is found
276 // which has the bit for SSA_NAME set. Then scan for the equivalency set in
277 // that block. No previous lists need be searched.
279 // If SSA has an equivalence in this list, find and return it.
280 // Otherwise return NULL.
282 equiv_chain *
283 equiv_chain::find (unsigned ssa)
285 equiv_chain *ptr = NULL;
286 // If there are equiv sets and SSA is in one in this list, find it.
287 // Otherwise return NULL.
288 if (bitmap_bit_p (m_names, ssa))
290 for (ptr = m_next; ptr; ptr = ptr->m_next)
291 if (bitmap_bit_p (ptr->m_names, ssa))
292 break;
294 return ptr;
297 // Dump the names in this equivalence set.
299 void
300 equiv_chain::dump (FILE *f) const
302 bitmap_iterator bi;
303 unsigned i;
305 if (!m_names || bitmap_empty_p (m_names))
306 return;
307 fprintf (f, "Equivalence set : [");
308 unsigned c = 0;
309 EXECUTE_IF_SET_IN_BITMAP (m_names, 0, i, bi)
311 if (ssa_name (i))
313 if (c++)
314 fprintf (f, ", ");
315 print_generic_expr (f, ssa_name (i), TDF_SLIM);
318 fprintf (f, "]\n");
321 // Instantiate an equivalency oracle.
323 equiv_oracle::equiv_oracle ()
325 bitmap_obstack_initialize (&m_bitmaps);
326 m_equiv.create (0);
327 m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
328 m_equiv_set = BITMAP_ALLOC (&m_bitmaps);
329 obstack_init (&m_chain_obstack);
330 m_self_equiv.create (0);
331 m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
332 m_partial.create (0);
333 m_partial.safe_grow_cleared (num_ssa_names + 1);
336 // Destruct an equivalency oracle.
338 equiv_oracle::~equiv_oracle ()
340 m_partial.release ();
341 m_self_equiv.release ();
342 obstack_free (&m_chain_obstack, NULL);
343 m_equiv.release ();
344 bitmap_obstack_release (&m_bitmaps);
347 // Add a partial equivalence R between OP1 and OP2.
349 void
350 equiv_oracle::add_partial_equiv (relation_kind r, tree op1, tree op2)
352 int v1 = SSA_NAME_VERSION (op1);
353 int v2 = SSA_NAME_VERSION (op2);
354 int prec2 = TYPE_PRECISION (TREE_TYPE (op2));
355 int bits = pe_to_bits (r);
356 gcc_checking_assert (bits && prec2 >= bits);
358 if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
359 m_partial.safe_grow_cleared (num_ssa_names + 1);
360 gcc_checking_assert (v1 < (int)m_partial.length ()
361 && v2 < (int)m_partial.length ());
363 pe_slice &pe1 = m_partial[v1];
364 pe_slice &pe2 = m_partial[v2];
366 if (pe1.members)
368 // If the definition pe1 already has an entry, either the stmt is
369 // being re-evaluated, or the def was used before being registered.
370 // In either case, if PE2 has an entry, we simply do nothing.
371 if (pe2.members)
372 return;
373 // PE1 is the LHS and already has members, so everything in the set
374 // should be a slice of PE2 rather than PE1.
375 pe2.code = pe_min (r, pe1.code);
376 pe2.ssa_base = op2;
377 pe2.members = pe1.members;
378 bitmap_iterator bi;
379 unsigned x;
380 EXECUTE_IF_SET_IN_BITMAP (pe1.members, 0, x, bi)
382 m_partial[x].ssa_base = op2;
383 m_partial[x].code = pe_min (m_partial[x].code, pe2.code);
385 bitmap_set_bit (pe1.members, v2);
386 return;
388 if (pe2.members)
390 pe1.ssa_base = pe2.ssa_base;
391 // If pe2 is a 16 bit value, but only an 8 bit copy, we can't be any
392 // more than an 8 bit equivalence here, so choose MIN value.
393 pe1.code = pe_min (r, pe2.code);
394 pe1.members = pe2.members;
395 bitmap_set_bit (pe1.members, v1);
397 else
399 // Neither name has an entry, simply create op1 as slice of op2.
400 pe2.code = bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2)));
401 if (pe2.code == VREL_VARYING)
402 return;
403 pe2.ssa_base = op2;
404 pe2.members = BITMAP_ALLOC (&m_bitmaps);
405 bitmap_set_bit (pe2.members, v2);
406 pe1.ssa_base = op2;
407 pe1.code = r;
408 pe1.members = pe2.members;
409 bitmap_set_bit (pe1.members, v1);
413 // Return the set of partial equivalences associated with NAME. The bitmap
414 // will be NULL if there are none.
416 const pe_slice *
417 equiv_oracle::partial_equiv_set (tree name)
419 int v = SSA_NAME_VERSION (name);
420 if (v >= (int)m_partial.length ())
421 return NULL;
422 return &m_partial[v];
425 // Query if there is a partial equivalence between SSA1 and SSA2. Return
426 // VREL_VARYING if there is not one. If BASE is non-null, return the base
427 // ssa-name this is a slice of.
429 relation_kind
430 equiv_oracle::partial_equiv (tree ssa1, tree ssa2, tree *base) const
432 int v1 = SSA_NAME_VERSION (ssa1);
433 int v2 = SSA_NAME_VERSION (ssa2);
435 if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
436 return VREL_VARYING;
438 const pe_slice &pe1 = m_partial[v1];
439 const pe_slice &pe2 = m_partial[v2];
440 if (pe1.members && pe2.members == pe1.members)
442 if (base)
443 *base = pe1.ssa_base;
444 return pe_min (pe1.code, pe2.code);
446 return VREL_VARYING;
450 // Find and return the equivalency set for SSA along the dominators of BB.
451 // This is the external API.
453 const_bitmap
454 equiv_oracle::equiv_set (tree ssa, basic_block bb)
456 // Search the dominator tree for an equivalency.
457 equiv_chain *equiv = find_equiv_dom (ssa, bb);
458 if (equiv)
459 return equiv->m_names;
461 // Otherwise return a cached equiv set containing just this SSA.
462 unsigned v = SSA_NAME_VERSION (ssa);
463 if (v >= m_self_equiv.length ())
464 m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
466 if (!m_self_equiv[v])
468 m_self_equiv[v] = BITMAP_ALLOC (&m_bitmaps);
469 bitmap_set_bit (m_self_equiv[v], v);
471 return m_self_equiv[v];
474 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
476 relation_kind
477 equiv_oracle::query_relation (basic_block bb, tree ssa1, tree ssa2)
479 // If the 2 ssa names share the same equiv set, they are equal.
480 if (equiv_set (ssa1, bb) == equiv_set (ssa2, bb))
481 return VREL_EQ;
483 // Check if there is a partial equivalence.
484 return partial_equiv (ssa1, ssa2);
487 // Query if thre is a relation (equivalence) between 2 SSA_NAMEs.
489 relation_kind
490 equiv_oracle::query_relation (basic_block bb ATTRIBUTE_UNUSED, const_bitmap e1,
491 const_bitmap e2)
493 // If the 2 ssa names share the same equiv set, they are equal.
494 if (bitmap_equal_p (e1, e2))
495 return VREL_EQ;
496 return VREL_VARYING;
499 // If SSA has an equivalence in block BB, find and return it.
500 // Otherwise return NULL.
502 equiv_chain *
503 equiv_oracle::find_equiv_block (unsigned ssa, int bb) const
505 if (bb >= (int)m_equiv.length () || !m_equiv[bb])
506 return NULL;
508 return m_equiv[bb]->find (ssa);
511 // Starting at block BB, walk the dominator chain looking for the nearest
512 // equivalence set containing NAME.
514 equiv_chain *
515 equiv_oracle::find_equiv_dom (tree name, basic_block bb) const
517 unsigned v = SSA_NAME_VERSION (name);
518 // Short circuit looking for names which have no equivalences.
519 // Saves time looking for something which does not exist.
520 if (!bitmap_bit_p (m_equiv_set, v))
521 return NULL;
523 // NAME has at least once equivalence set, check to see if it has one along
524 // the dominator tree.
525 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
527 equiv_chain *ptr = find_equiv_block (v, bb->index);
528 if (ptr)
529 return ptr;
531 return NULL;
534 // Register equivalance between ssa_name V and set EQUIV in block BB,
536 bitmap
537 equiv_oracle::register_equiv (basic_block bb, unsigned v, equiv_chain *equiv)
539 // V will have an equivalency now.
540 bitmap_set_bit (m_equiv_set, v);
542 // If that equiv chain is in this block, simply use it.
543 if (equiv->m_bb == bb)
545 bitmap_set_bit (equiv->m_names, v);
546 bitmap_set_bit (m_equiv[bb->index]->m_names, v);
547 return NULL;
550 // Otherwise create an equivalence for this block which is a copy
551 // of equiv, the add V to the set.
552 bitmap b = BITMAP_ALLOC (&m_bitmaps);
553 valid_equivs (b, equiv->m_names, bb);
554 bitmap_set_bit (b, v);
555 return b;
558 // Register equivalence between set equiv_1 and equiv_2 in block BB.
559 // Return NULL if either name can be merged with the other. Otherwise
560 // return a pointer to the combined bitmap of names. This allows the
561 // caller to do any setup required for a new element.
563 bitmap
564 equiv_oracle::register_equiv (basic_block bb, equiv_chain *equiv_1,
565 equiv_chain *equiv_2)
567 // If equiv_1 is already in BB, use it as the combined set.
568 if (equiv_1->m_bb == bb)
570 valid_equivs (equiv_1->m_names, equiv_2->m_names, bb);
571 // Its hard to delete from a single linked list, so
572 // just clear the second one.
573 if (equiv_2->m_bb == bb)
574 bitmap_clear (equiv_2->m_names);
575 else
576 // Ensure the new names are in the summary for BB.
577 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_1->m_names);
578 return NULL;
580 // If equiv_2 is in BB, use it for the combined set.
581 if (equiv_2->m_bb == bb)
583 valid_equivs (equiv_2->m_names, equiv_1->m_names, bb);
584 // Ensure the new names are in the summary.
585 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_2->m_names);
586 return NULL;
589 // At this point, neither equivalence is from this block.
590 bitmap b = BITMAP_ALLOC (&m_bitmaps);
591 valid_equivs (b, equiv_1->m_names, bb);
592 valid_equivs (b, equiv_2->m_names, bb);
593 return b;
596 // Create an equivalency set containing only SSA in its definition block.
597 // This is done the first time SSA is registered in an equivalency and blocks
598 // any DOM searches past the definition.
600 void
601 equiv_oracle::register_initial_def (tree ssa)
603 if (SSA_NAME_IS_DEFAULT_DEF (ssa))
604 return;
605 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (ssa));
606 gcc_checking_assert (bb && !find_equiv_dom (ssa, bb));
608 unsigned v = SSA_NAME_VERSION (ssa);
609 bitmap_set_bit (m_equiv_set, v);
610 bitmap equiv_set = BITMAP_ALLOC (&m_bitmaps);
611 bitmap_set_bit (equiv_set, v);
612 add_equiv_to_block (bb, equiv_set);
615 // Register an equivalence between SSA1 and SSA2 in block BB.
616 // The equivalence oracle maintains a vector of equivalencies indexed by basic
617 // block. When an equivalence bteween SSA1 and SSA2 is registered in block BB,
618 // a query is made as to what equivalences both names have already, and
619 // any preexisting equivalences are merged to create a single equivalence
620 // containing all the ssa_names in this basic block.
622 void
623 equiv_oracle::register_relation (basic_block bb, relation_kind k, tree ssa1,
624 tree ssa2)
626 // Process partial equivalencies.
627 if (relation_partial_equiv_p (k))
629 add_partial_equiv (k, ssa1, ssa2);
630 return;
632 // Only handle equality relations.
633 if (k != VREL_EQ)
634 return;
636 unsigned v1 = SSA_NAME_VERSION (ssa1);
637 unsigned v2 = SSA_NAME_VERSION (ssa2);
639 // If this is the first time an ssa_name has an equivalency registered
640 // create a self-equivalency record in the def block.
641 if (!bitmap_bit_p (m_equiv_set, v1))
642 register_initial_def (ssa1);
643 if (!bitmap_bit_p (m_equiv_set, v2))
644 register_initial_def (ssa2);
646 equiv_chain *equiv_1 = find_equiv_dom (ssa1, bb);
647 equiv_chain *equiv_2 = find_equiv_dom (ssa2, bb);
649 // Check if they are the same set
650 if (equiv_1 && equiv_1 == equiv_2)
651 return;
653 bitmap equiv_set;
655 // Case where we have 2 SSA_NAMEs that are not in any set.
656 if (!equiv_1 && !equiv_2)
658 bitmap_set_bit (m_equiv_set, v1);
659 bitmap_set_bit (m_equiv_set, v2);
661 equiv_set = BITMAP_ALLOC (&m_bitmaps);
662 bitmap_set_bit (equiv_set, v1);
663 bitmap_set_bit (equiv_set, v2);
665 else if (!equiv_1 && equiv_2)
666 equiv_set = register_equiv (bb, v1, equiv_2);
667 else if (equiv_1 && !equiv_2)
668 equiv_set = register_equiv (bb, v2, equiv_1);
669 else
670 equiv_set = register_equiv (bb, equiv_1, equiv_2);
672 // A non-null return is a bitmap that is to be added to the current
673 // block as a new equivalence.
674 if (!equiv_set)
675 return;
677 add_equiv_to_block (bb, equiv_set);
680 // Add an equivalency record in block BB containing bitmap EQUIV_SET.
681 // Note the internal caller is responible for allocating EQUIV_SET properly.
683 void
684 equiv_oracle::add_equiv_to_block (basic_block bb, bitmap equiv_set)
686 equiv_chain *ptr;
688 // Check if this is the first time a block has an equivalence added.
689 // and create a header block. And set the summary for this block.
690 if (!m_equiv[bb->index])
692 ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
693 sizeof (equiv_chain));
694 ptr->m_names = BITMAP_ALLOC (&m_bitmaps);
695 bitmap_copy (ptr->m_names, equiv_set);
696 ptr->m_bb = bb;
697 ptr->m_next = NULL;
698 m_equiv[bb->index] = ptr;
701 // Now create the element for this equiv set and initialize it.
702 ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack, sizeof (equiv_chain));
703 ptr->m_names = equiv_set;
704 ptr->m_bb = bb;
705 gcc_checking_assert (bb->index < (int)m_equiv.length ());
706 ptr->m_next = m_equiv[bb->index]->m_next;
707 m_equiv[bb->index]->m_next = ptr;
708 bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_set);
711 // Make sure the BB vector is big enough and grow it if needed.
713 void
714 equiv_oracle::limit_check (basic_block bb)
716 int i = (bb) ? bb->index : last_basic_block_for_fn (cfun);
717 if (i >= (int)m_equiv.length ())
718 m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
721 // Dump the equivalence sets in BB to file F.
723 void
724 equiv_oracle::dump (FILE *f, basic_block bb) const
726 if (bb->index >= (int)m_equiv.length ())
727 return;
728 // Process equivalences.
729 if (m_equiv[bb->index])
731 equiv_chain *ptr = m_equiv[bb->index]->m_next;
732 for (; ptr; ptr = ptr->m_next)
733 ptr->dump (f);
735 // Look for partial equivalences defined in this block..
736 for (unsigned i = 0; i < num_ssa_names; i++)
738 tree name = ssa_name (i);
739 if (!gimple_range_ssa_p (name) || !SSA_NAME_DEF_STMT (name))
740 continue;
741 if (i >= m_partial.length ())
742 break;
743 tree base = m_partial[i].ssa_base;
744 if (base && name != base && gimple_bb (SSA_NAME_DEF_STMT (name)) == bb)
746 relation_kind k = partial_equiv (name, base);
747 if (k != VREL_VARYING)
749 value_relation vr (k, name, base);
750 fprintf (f, "Partial equiv ");
751 vr.dump (f);
752 fputc ('\n',f);
758 // Dump all equivalence sets known to the oracle.
760 void
761 equiv_oracle::dump (FILE *f) const
763 fprintf (f, "Equivalency dump\n");
764 for (unsigned i = 0; i < m_equiv.length (); i++)
765 if (m_equiv[i] && BASIC_BLOCK_FOR_FN (cfun, i))
767 fprintf (f, "BB%d\n", i);
768 dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
773 // --------------------------------------------------------------------------
774 // Negate the current relation.
776 void
777 value_relation::negate ()
779 related = relation_negate (related);
782 // Perform an intersection between 2 relations. *this &&= p.
784 bool
785 value_relation::intersect (value_relation &p)
787 // Save previous value
788 relation_kind old = related;
790 if (p.op1 () == op1 () && p.op2 () == op2 ())
791 related = relation_intersect (kind (), p.kind ());
792 else if (p.op2 () == op1 () && p.op1 () == op2 ())
793 related = relation_intersect (kind (), relation_swap (p.kind ()));
794 else
795 return false;
797 return old != related;
800 // Perform a union between 2 relations. *this ||= p.
802 bool
803 value_relation::union_ (value_relation &p)
805 // Save previous value
806 relation_kind old = related;
808 if (p.op1 () == op1 () && p.op2 () == op2 ())
809 related = relation_union (kind(), p.kind());
810 else if (p.op2 () == op1 () && p.op1 () == op2 ())
811 related = relation_union (kind(), relation_swap (p.kind ()));
812 else
813 return false;
815 return old != related;
818 // Identify and apply any transitive relations between REL
819 // and THIS. Return true if there was a transformation.
821 bool
822 value_relation::apply_transitive (const value_relation &rel)
824 relation_kind k = VREL_VARYING;
826 // Idenity any common operand, and notrmalize the relations to
827 // the form : A < B B < C produces A < C
828 if (rel.op1 () == name2)
830 // A < B B < C
831 if (rel.op2 () == name1)
832 return false;
833 k = relation_transitive (kind (), rel.kind ());
834 if (k != VREL_VARYING)
836 related = k;
837 name2 = rel.op2 ();
838 return true;
841 else if (rel.op1 () == name1)
843 // B > A B < C
844 if (rel.op2 () == name2)
845 return false;
846 k = relation_transitive (relation_swap (kind ()), rel.kind ());
847 if (k != VREL_VARYING)
849 related = k;
850 name1 = name2;
851 name2 = rel.op2 ();
852 return true;
855 else if (rel.op2 () == name2)
857 // A < B C > B
858 if (rel.op1 () == name1)
859 return false;
860 k = relation_transitive (kind (), relation_swap (rel.kind ()));
861 if (k != VREL_VARYING)
863 related = k;
864 name2 = rel.op1 ();
865 return true;
868 else if (rel.op2 () == name1)
870 // B > A C > B
871 if (rel.op1 () == name2)
872 return false;
873 k = relation_transitive (relation_swap (kind ()),
874 relation_swap (rel.kind ()));
875 if (k != VREL_VARYING)
877 related = k;
878 name1 = name2;
879 name2 = rel.op1 ();
880 return true;
883 return false;
886 // Dump the relation to file F.
888 void
889 value_relation::dump (FILE *f) const
891 if (!name1 || !name2)
893 fprintf (f, "no relation registered");
894 return;
896 fputc ('(', f);
897 print_generic_expr (f, op1 (), TDF_SLIM);
898 print_relation (f, kind ());
899 print_generic_expr (f, op2 (), TDF_SLIM);
900 fputc(')', f);
903 // This container is used to link relations in a chain.
905 class relation_chain : public value_relation
907 public:
908 relation_chain *m_next;
911 // ------------------------------------------------------------------------
913 // Find the relation between any ssa_name in B1 and any name in B2 in LIST.
914 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
916 relation_kind
917 relation_chain_head::find_relation (const_bitmap b1, const_bitmap b2) const
919 if (!m_names)
920 return VREL_VARYING;
922 // If both b1 and b2 aren't referenced in thie block, cant be a relation
923 if (!bitmap_intersect_p (m_names, b1) || !bitmap_intersect_p (m_names, b2))
924 return VREL_VARYING;
926 // Search for the fiorst relation that contains BOTH an element from B1
927 // and B2, and return that relation.
928 for (relation_chain *ptr = m_head; ptr ; ptr = ptr->m_next)
930 unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
931 unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
932 if (bitmap_bit_p (b1, op1) && bitmap_bit_p (b2, op2))
933 return ptr->kind ();
934 if (bitmap_bit_p (b1, op2) && bitmap_bit_p (b2, op1))
935 return relation_swap (ptr->kind ());
938 return VREL_VARYING;
941 // Instantiate a relation oracle.
943 dom_oracle::dom_oracle ()
945 m_relations.create (0);
946 m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
947 m_relation_set = BITMAP_ALLOC (&m_bitmaps);
948 m_tmp = BITMAP_ALLOC (&m_bitmaps);
949 m_tmp2 = BITMAP_ALLOC (&m_bitmaps);
952 // Destruct a relation oracle.
954 dom_oracle::~dom_oracle ()
956 m_relations.release ();
959 // Register relation K between ssa_name OP1 and OP2 on STMT.
961 void
962 relation_oracle::register_stmt (gimple *stmt, relation_kind k, tree op1,
963 tree op2)
965 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
966 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
967 gcc_checking_assert (stmt && gimple_bb (stmt));
969 // Don't register lack of a relation.
970 if (k == VREL_VARYING)
971 return;
973 if (dump_file && (dump_flags & TDF_DETAILS))
975 value_relation vr (k, op1, op2);
976 fprintf (dump_file, " Registering value_relation ");
977 vr.dump (dump_file);
978 fprintf (dump_file, " (bb%d) at ", gimple_bb (stmt)->index);
979 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
982 // If an equivalence is being added between a PHI and one of its arguments
983 // make sure that that argument is not defined in the same block.
984 // This can happen along back edges and the equivalence will not be
985 // applicable as it would require a use before def.
986 if (k == VREL_EQ && is_a<gphi *> (stmt))
988 tree phi_def = gimple_phi_result (stmt);
989 gcc_checking_assert (phi_def == op1 || phi_def == op2);
990 tree arg = op2;
991 if (phi_def == op2)
992 arg = op1;
993 if (gimple_bb (stmt) == gimple_bb (SSA_NAME_DEF_STMT (arg)))
995 if (dump_file && (dump_flags & TDF_DETAILS))
997 fprintf (dump_file, " Not registered due to ");
998 print_generic_expr (dump_file, arg, TDF_SLIM);
999 fprintf (dump_file, " being defined in the same block.\n");
1001 return;
1004 register_relation (gimple_bb (stmt), k, op1, op2);
1007 // Register relation K between ssa_name OP1 and OP2 on edge E.
1009 void
1010 relation_oracle::register_edge (edge e, relation_kind k, tree op1, tree op2)
1012 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1013 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1015 // Do not register lack of relation, or blocks which have more than
1016 // edge E for a predecessor.
1017 if (k == VREL_VARYING || !single_pred_p (e->dest))
1018 return;
1020 if (dump_file && (dump_flags & TDF_DETAILS))
1022 value_relation vr (k, op1, op2);
1023 fprintf (dump_file, " Registering value_relation ");
1024 vr.dump (dump_file);
1025 fprintf (dump_file, " on (%d->%d)\n", e->src->index, e->dest->index);
1028 register_relation (e->dest, k, op1, op2);
1031 // Register relation K between OP! and OP2 in block BB.
1032 // This creates the record and searches for existing records in the dominator
1033 // tree to merge with.
1035 void
1036 dom_oracle::register_relation (basic_block bb, relation_kind k, tree op1,
1037 tree op2)
1039 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1040 // and no other relation makes sense.
1041 if (op1 == op2)
1042 return;
1044 // Equivalencies are handled by the equivalence oracle.
1045 if (relation_equiv_p (k))
1046 equiv_oracle::register_relation (bb, k, op1, op2);
1047 else
1049 // if neither op1 nor op2 are in a relation before this is registered,
1050 // there will be no transitive.
1051 bool check = bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op1))
1052 || bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op2));
1053 relation_chain *ptr = set_one_relation (bb, k, op1, op2);
1054 if (ptr && check)
1055 register_transitives (bb, *ptr);
1059 // Register relation K between OP! and OP2 in block BB.
1060 // This creates the record and searches for existing records in the dominator
1061 // tree to merge with. Return the record, or NULL if no record was created.
1063 relation_chain *
1064 dom_oracle::set_one_relation (basic_block bb, relation_kind k, tree op1,
1065 tree op2)
1067 gcc_checking_assert (k != VREL_VARYING && k != VREL_EQ);
1069 value_relation vr(k, op1, op2);
1070 int bbi = bb->index;
1072 if (bbi >= (int)m_relations.length())
1073 m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1075 // Summary bitmap indicating what ssa_names have relations in this BB.
1076 bitmap bm = m_relations[bbi].m_names;
1077 if (!bm)
1078 bm = m_relations[bbi].m_names = BITMAP_ALLOC (&m_bitmaps);
1079 unsigned v1 = SSA_NAME_VERSION (op1);
1080 unsigned v2 = SSA_NAME_VERSION (op2);
1082 relation_kind curr;
1083 relation_chain *ptr;
1084 curr = find_relation_block (bbi, v1, v2, &ptr);
1085 // There is an existing relation in this block, just intersect with it.
1086 if (curr != VREL_VARYING)
1088 if (dump_file && (dump_flags & TDF_DETAILS))
1090 fprintf (dump_file, " Intersecting with existing ");
1091 ptr->dump (dump_file);
1093 // Check into whether we can simply replace the relation rather than
1094 // intersecting it. This may help with some optimistic iterative
1095 // updating algorithms.
1096 bool new_rel = ptr->intersect (vr);
1097 if (dump_file && (dump_flags & TDF_DETAILS))
1099 fprintf (dump_file, " to produce ");
1100 ptr->dump (dump_file);
1101 fprintf (dump_file, " %s.\n", new_rel ? "Updated" : "No Change");
1103 // If there was no change, return no record..
1104 if (!new_rel)
1105 return NULL;
1107 else
1109 if (m_relations[bbi].m_num_relations >= param_relation_block_limit)
1111 if (dump_file && (dump_flags & TDF_DETAILS))
1112 fprintf (dump_file, " Not registered due to bb being full\n");
1113 return NULL;
1115 m_relations[bbi].m_num_relations++;
1116 // Check for an existing relation further up the DOM chain.
1117 // By including dominating relations, The first one found in any search
1118 // will be the aggregate of all the previous ones.
1119 curr = find_relation_dom (bb, v1, v2);
1120 if (curr != VREL_VARYING)
1121 k = relation_intersect (curr, k);
1123 bitmap_set_bit (bm, v1);
1124 bitmap_set_bit (bm, v2);
1125 bitmap_set_bit (m_relation_set, v1);
1126 bitmap_set_bit (m_relation_set, v2);
1128 ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1129 sizeof (relation_chain));
1130 ptr->set_relation (k, op1, op2);
1131 ptr->m_next = m_relations[bbi].m_head;
1132 m_relations[bbi].m_head = ptr;
1134 return ptr;
1137 // Starting at ROOT_BB search the DOM tree looking for relations which
1138 // may produce transitive relations to RELATION. EQUIV1 and EQUIV2 are
1139 // bitmaps for op1/op2 and any of their equivalences that should also be
1140 // considered.
1142 void
1143 dom_oracle::register_transitives (basic_block root_bb,
1144 const value_relation &relation)
1146 basic_block bb;
1147 // Only apply transitives to certain kinds of operations.
1148 switch (relation.kind ())
1150 case VREL_LE:
1151 case VREL_LT:
1152 case VREL_GT:
1153 case VREL_GE:
1154 break;
1155 default:
1156 return;
1159 const_bitmap equiv1 = equiv_set (relation.op1 (), root_bb);
1160 const_bitmap equiv2 = equiv_set (relation.op2 (), root_bb);
1162 for (bb = root_bb; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1164 int bbi = bb->index;
1165 if (bbi >= (int)m_relations.length())
1166 continue;
1167 const_bitmap bm = m_relations[bbi].m_names;
1168 if (!bm)
1169 continue;
1170 if (!bitmap_intersect_p (bm, equiv1) && !bitmap_intersect_p (bm, equiv2))
1171 continue;
1172 // At least one of the 2 ops has a relation in this block.
1173 relation_chain *ptr;
1174 for (ptr = m_relations[bbi].m_head; ptr ; ptr = ptr->m_next)
1176 // In the presence of an equivalence, 2 operands may do not
1177 // naturally match. ie with equivalence a_2 == b_3
1178 // given c_1 < a_2 && b_3 < d_4
1179 // convert the second relation (b_3 < d_4) to match any
1180 // equivalences to found in the first relation.
1181 // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
1182 // transitive operation: c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
1184 tree r1, r2;
1185 tree p1 = ptr->op1 ();
1186 tree p2 = ptr->op2 ();
1187 // Find which equivalence is in the first operand.
1188 if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p1)))
1189 r1 = p1;
1190 else if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p2)))
1191 r1 = p2;
1192 else
1193 r1 = NULL_TREE;
1195 // Find which equivalence is in the second operand.
1196 if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p1)))
1197 r2 = p1;
1198 else if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p2)))
1199 r2 = p2;
1200 else
1201 r2 = NULL_TREE;
1203 // Ignore if both NULL (not relevant relation) or the same,
1204 if (r1 == r2)
1205 continue;
1207 // Any operand not an equivalence, just take the real operand.
1208 if (!r1)
1209 r1 = relation.op1 ();
1210 if (!r2)
1211 r2 = relation.op2 ();
1213 value_relation nr (relation.kind (), r1, r2);
1214 if (nr.apply_transitive (*ptr))
1216 if (!set_one_relation (root_bb, nr.kind (), nr.op1 (), nr.op2 ()))
1217 return;
1218 if (dump_file && (dump_flags & TDF_DETAILS))
1220 fprintf (dump_file, " Registering transitive relation ");
1221 nr.dump (dump_file);
1222 fputc ('\n', dump_file);
1230 // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
1231 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1233 relation_kind
1234 dom_oracle::find_relation_block (unsigned bb, const_bitmap b1,
1235 const_bitmap b2) const
1237 if (bb >= m_relations.length())
1238 return VREL_VARYING;
1240 return m_relations[bb].find_relation (b1, b2);
1243 // Search the DOM tree for a relation between an element of equivalency set B1
1244 // and B2, starting with block BB.
1246 relation_kind
1247 dom_oracle::query_relation (basic_block bb, const_bitmap b1,
1248 const_bitmap b2)
1250 relation_kind r;
1251 if (bitmap_equal_p (b1, b2))
1252 return VREL_EQ;
1254 // If either name does not occur in a relation anywhere, there isnt one.
1255 if (!bitmap_intersect_p (m_relation_set, b1)
1256 || !bitmap_intersect_p (m_relation_set, b2))
1257 return VREL_VARYING;
1259 // Search each block in the DOM tree checking.
1260 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1262 r = find_relation_block (bb->index, b1, b2);
1263 if (r != VREL_VARYING)
1264 return r;
1266 return VREL_VARYING;
1270 // Find a relation in block BB between ssa version V1 and V2. If a relation
1271 // is found, return a pointer to the chain object in OBJ.
1273 relation_kind
1274 dom_oracle::find_relation_block (int bb, unsigned v1, unsigned v2,
1275 relation_chain **obj) const
1277 if (bb >= (int)m_relations.length())
1278 return VREL_VARYING;
1280 const_bitmap bm = m_relations[bb].m_names;
1281 if (!bm)
1282 return VREL_VARYING;
1284 // If both b1 and b2 aren't referenced in thie block, cant be a relation
1285 if (!bitmap_bit_p (bm, v1) || !bitmap_bit_p (bm, v2))
1286 return VREL_VARYING;
1288 relation_chain *ptr;
1289 for (ptr = m_relations[bb].m_head; ptr ; ptr = ptr->m_next)
1291 unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1292 unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1293 if (v1 == op1 && v2 == op2)
1295 if (obj)
1296 *obj = ptr;
1297 return ptr->kind ();
1299 if (v1 == op2 && v2 == op1)
1301 if (obj)
1302 *obj = ptr;
1303 return relation_swap (ptr->kind ());
1307 return VREL_VARYING;
1310 // Find a relation between SSA version V1 and V2 in the dominator tree
1311 // starting with block BB
1313 relation_kind
1314 dom_oracle::find_relation_dom (basic_block bb, unsigned v1, unsigned v2) const
1316 relation_kind r;
1317 // IF either name does not occur in a relation anywhere, there isnt one.
1318 if (!bitmap_bit_p (m_relation_set, v1) || !bitmap_bit_p (m_relation_set, v2))
1319 return VREL_VARYING;
1321 for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1323 r = find_relation_block (bb->index, v1, v2);
1324 if (r != VREL_VARYING)
1325 return r;
1327 return VREL_VARYING;
1331 // Query if there is a relation between SSA1 and SS2 in block BB or a
1332 // dominator of BB
1334 relation_kind
1335 dom_oracle::query_relation (basic_block bb, tree ssa1, tree ssa2)
1337 relation_kind kind;
1338 unsigned v1 = SSA_NAME_VERSION (ssa1);
1339 unsigned v2 = SSA_NAME_VERSION (ssa2);
1340 if (v1 == v2)
1341 return VREL_EQ;
1343 // Check for equivalence first. They must be in each equivalency set.
1344 const_bitmap equiv1 = equiv_set (ssa1, bb);
1345 const_bitmap equiv2 = equiv_set (ssa2, bb);
1346 if (bitmap_bit_p (equiv1, v2) && bitmap_bit_p (equiv2, v1))
1347 return VREL_EQ;
1349 kind = partial_equiv (ssa1, ssa2);
1350 if (kind != VREL_VARYING)
1351 return kind;
1353 // Initially look for a direct relationship and just return that.
1354 kind = find_relation_dom (bb, v1, v2);
1355 if (kind != VREL_VARYING)
1356 return kind;
1358 // Query using the equivalence sets.
1359 kind = query_relation (bb, equiv1, equiv2);
1360 return kind;
1363 // Dump all the relations in block BB to file F.
1365 void
1366 dom_oracle::dump (FILE *f, basic_block bb) const
1368 equiv_oracle::dump (f,bb);
1370 if (bb->index >= (int)m_relations.length ())
1371 return;
1372 if (!m_relations[bb->index].m_names)
1373 return;
1375 relation_chain *ptr = m_relations[bb->index].m_head;
1376 for (; ptr; ptr = ptr->m_next)
1378 fprintf (f, "Relational : ");
1379 ptr->dump (f);
1380 fprintf (f, "\n");
1384 // Dump all the relations known to file F.
1386 void
1387 dom_oracle::dump (FILE *f) const
1389 fprintf (f, "Relation dump\n");
1390 for (unsigned i = 0; i < m_relations.length (); i++)
1391 if (BASIC_BLOCK_FOR_FN (cfun, i))
1393 fprintf (f, "BB%d\n", i);
1394 dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
1398 void
1399 relation_oracle::debug () const
1401 dump (stderr);
1404 path_oracle::path_oracle (relation_oracle *oracle)
1406 set_root_oracle (oracle);
1407 bitmap_obstack_initialize (&m_bitmaps);
1408 obstack_init (&m_chain_obstack);
1410 // Initialize header records.
1411 m_equiv.m_names = BITMAP_ALLOC (&m_bitmaps);
1412 m_equiv.m_bb = NULL;
1413 m_equiv.m_next = NULL;
1414 m_relations.m_names = BITMAP_ALLOC (&m_bitmaps);
1415 m_relations.m_head = NULL;
1416 m_killed_defs = BITMAP_ALLOC (&m_bitmaps);
1419 path_oracle::~path_oracle ()
1421 obstack_free (&m_chain_obstack, NULL);
1422 bitmap_obstack_release (&m_bitmaps);
1425 // Return the equiv set for SSA, and if there isn't one, check for equivs
1426 // starting in block BB.
1428 const_bitmap
1429 path_oracle::equiv_set (tree ssa, basic_block bb)
1431 // Check the list first.
1432 equiv_chain *ptr = m_equiv.find (SSA_NAME_VERSION (ssa));
1433 if (ptr)
1434 return ptr->m_names;
1436 // Otherwise defer to the root oracle.
1437 if (m_root)
1438 return m_root->equiv_set (ssa, bb);
1440 // Allocate a throw away bitmap if there isn't a root oracle.
1441 bitmap tmp = BITMAP_ALLOC (&m_bitmaps);
1442 bitmap_set_bit (tmp, SSA_NAME_VERSION (ssa));
1443 return tmp;
1446 // Register an equivalence between SSA1 and SSA2 resolving unkowns from
1447 // block BB.
1449 void
1450 path_oracle::register_equiv (basic_block bb, tree ssa1, tree ssa2)
1452 const_bitmap equiv_1 = equiv_set (ssa1, bb);
1453 const_bitmap equiv_2 = equiv_set (ssa2, bb);
1455 // Check if they are the same set, if so, we're done.
1456 if (bitmap_equal_p (equiv_1, equiv_2))
1457 return;
1459 // Don't mess around, simply create a new record and insert it first.
1460 bitmap b = BITMAP_ALLOC (&m_bitmaps);
1461 valid_equivs (b, equiv_1, bb);
1462 valid_equivs (b, equiv_2, bb);
1464 equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1465 sizeof (equiv_chain));
1466 ptr->m_names = b;
1467 ptr->m_bb = NULL;
1468 ptr->m_next = m_equiv.m_next;
1469 m_equiv.m_next = ptr;
1470 bitmap_ior_into (m_equiv.m_names, b);
1473 // Register killing definition of an SSA_NAME.
1475 void
1476 path_oracle::killing_def (tree ssa)
1478 if (dump_file && (dump_flags & TDF_DETAILS))
1480 fprintf (dump_file, " Registering killing_def (path_oracle) ");
1481 print_generic_expr (dump_file, ssa, TDF_SLIM);
1482 fprintf (dump_file, "\n");
1485 unsigned v = SSA_NAME_VERSION (ssa);
1487 bitmap_set_bit (m_killed_defs, v);
1488 bitmap_set_bit (m_equiv.m_names, v);
1490 // Now add an equivalency with itself so we don't look to the root oracle.
1491 bitmap b = BITMAP_ALLOC (&m_bitmaps);
1492 bitmap_set_bit (b, v);
1493 equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1494 sizeof (equiv_chain));
1495 ptr->m_names = b;
1496 ptr->m_bb = NULL;
1497 ptr->m_next = m_equiv.m_next;
1498 m_equiv.m_next = ptr;
1500 // Walk the relation list and remove SSA from any relations.
1501 if (!bitmap_bit_p (m_relations.m_names, v))
1502 return;
1504 bitmap_clear_bit (m_relations.m_names, v);
1505 relation_chain **prev = &(m_relations.m_head);
1506 relation_chain *next = NULL;
1507 for (relation_chain *ptr = m_relations.m_head; ptr; ptr = next)
1509 gcc_checking_assert (*prev == ptr);
1510 next = ptr->m_next;
1511 if (SSA_NAME_VERSION (ptr->op1 ()) == v
1512 || SSA_NAME_VERSION (ptr->op2 ()) == v)
1513 *prev = ptr->m_next;
1514 else
1515 prev = &(ptr->m_next);
1519 // Register relation K between SSA1 and SSA2, resolving unknowns by
1520 // querying from BB.
1522 void
1523 path_oracle::register_relation (basic_block bb, relation_kind k, tree ssa1,
1524 tree ssa2)
1526 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1527 // and no other relation makes sense.
1528 if (ssa1 == ssa2)
1529 return;
1531 if (dump_file && (dump_flags & TDF_DETAILS))
1533 value_relation vr (k, ssa1, ssa2);
1534 fprintf (dump_file, " Registering value_relation (path_oracle) ");
1535 vr.dump (dump_file);
1536 fprintf (dump_file, " (root: bb%d)\n", bb->index);
1539 relation_kind curr = query_relation (bb, ssa1, ssa2);
1540 if (curr != VREL_VARYING)
1541 k = relation_intersect (curr, k);
1543 if (k == VREL_EQ)
1545 register_equiv (bb, ssa1, ssa2);
1546 return;
1549 bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa1));
1550 bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa2));
1551 relation_chain *ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1552 sizeof (relation_chain));
1553 ptr->set_relation (k, ssa1, ssa2);
1554 ptr->m_next = m_relations.m_head;
1555 m_relations.m_head = ptr;
1558 // Query for a relationship between equiv set B1 and B2, resolving unknowns
1559 // starting at block BB.
1561 relation_kind
1562 path_oracle::query_relation (basic_block bb, const_bitmap b1, const_bitmap b2)
1564 if (bitmap_equal_p (b1, b2))
1565 return VREL_EQ;
1567 relation_kind k = m_relations.find_relation (b1, b2);
1569 // Do not look at the root oracle for names that have been killed
1570 // along the path.
1571 if (bitmap_intersect_p (m_killed_defs, b1)
1572 || bitmap_intersect_p (m_killed_defs, b2))
1573 return k;
1575 if (k == VREL_VARYING && m_root)
1576 k = m_root->query_relation (bb, b1, b2);
1578 return k;
1581 // Query for a relationship between SSA1 and SSA2, resolving unknowns
1582 // starting at block BB.
1584 relation_kind
1585 path_oracle::query_relation (basic_block bb, tree ssa1, tree ssa2)
1587 unsigned v1 = SSA_NAME_VERSION (ssa1);
1588 unsigned v2 = SSA_NAME_VERSION (ssa2);
1590 if (v1 == v2)
1591 return VREL_EQ;
1593 const_bitmap equiv_1 = equiv_set (ssa1, bb);
1594 const_bitmap equiv_2 = equiv_set (ssa2, bb);
1595 if (bitmap_bit_p (equiv_1, v2) && bitmap_bit_p (equiv_2, v1))
1596 return VREL_EQ;
1598 return query_relation (bb, equiv_1, equiv_2);
1601 // Reset any relations registered on this path. ORACLE is the root
1602 // oracle to use.
1604 void
1605 path_oracle::reset_path (relation_oracle *oracle)
1607 set_root_oracle (oracle);
1608 m_equiv.m_next = NULL;
1609 bitmap_clear (m_equiv.m_names);
1610 m_relations.m_head = NULL;
1611 bitmap_clear (m_relations.m_names);
1612 bitmap_clear (m_killed_defs);
1615 // Dump relation in basic block... Do nothing here.
1617 void
1618 path_oracle::dump (FILE *, basic_block) const
1622 // Dump the relations and equivalencies found in the path.
1624 void
1625 path_oracle::dump (FILE *f) const
1627 equiv_chain *ptr = m_equiv.m_next;
1628 relation_chain *ptr2 = m_relations.m_head;
1630 if (ptr || ptr2)
1631 fprintf (f, "\npath_oracle:\n");
1633 for (; ptr; ptr = ptr->m_next)
1634 ptr->dump (f);
1636 for (; ptr2; ptr2 = ptr2->m_next)
1638 fprintf (f, "Relational : ");
1639 ptr2->dump (f);
1640 fprintf (f, "\n");
1644 // ------------------------------------------------------------------------
1645 // EQUIV iterator. Although we have bitmap iterators, don't expose that it
1646 // is currently a bitmap. Use an export iterator to hide future changes.
1648 // Construct a basic iterator over an equivalence bitmap.
1650 equiv_relation_iterator::equiv_relation_iterator (relation_oracle *oracle,
1651 basic_block bb, tree name,
1652 bool full, bool partial)
1654 m_name = name;
1655 m_oracle = oracle;
1656 m_pe = partial ? oracle->partial_equiv_set (name) : NULL;
1657 m_bm = NULL;
1658 if (full)
1659 m_bm = oracle->equiv_set (name, bb);
1660 if (!m_bm && m_pe)
1661 m_bm = m_pe->members;
1662 if (m_bm)
1663 bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1666 // Move to the next export bitmap spot.
1668 void
1669 equiv_relation_iterator::next ()
1671 bmp_iter_next (&m_bi, &m_y);
1674 // Fetch the name of the next export in the export list. Return NULL if
1675 // iteration is done.
1677 tree
1678 equiv_relation_iterator::get_name (relation_kind *rel)
1680 if (!m_bm)
1681 return NULL_TREE;
1683 while (bmp_iter_set (&m_bi, &m_y))
1685 // Do not return self.
1686 tree t = ssa_name (m_y);
1687 if (t && t != m_name)
1689 relation_kind k = VREL_EQ;
1690 if (m_pe && m_bm == m_pe->members)
1692 const pe_slice *equiv_pe = m_oracle->partial_equiv_set (t);
1693 if (equiv_pe && equiv_pe->members == m_pe->members)
1694 k = pe_min (m_pe->code, equiv_pe->code);
1695 else
1696 k = VREL_VARYING;
1698 if (relation_equiv_p (k))
1700 if (rel)
1701 *rel = k;
1702 return t;
1705 next ();
1708 // Process partial equivs after full equivs if both were requested.
1709 if (m_pe && m_bm != m_pe->members)
1711 m_bm = m_pe->members;
1712 if (m_bm)
1714 // Recursively call back to process First PE.
1715 bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1716 return get_name (rel);
1719 return NULL_TREE;