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