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