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 : 1062 : print_relation (FILE *f, relation_kind rel)
43 : : {
44 : 1062 : fprintf (f, " %s ", kind_string[rel]);
45 : 1062 : }
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 : 17469351 : relation_swap (relation_kind r)
69 : : {
70 : 17469351 : 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 : 91189296 : relation_intersect (relation_kind r1, relation_kind r2)
106 : : {
107 : 91189296 : 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 : 83352519 : relation_union (relation_kind r1, relation_kind r2)
143 : : {
144 : 83352519 : 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 : 8386198 : relation_transitive (relation_kind r1, relation_kind r2)
182 : : {
183 : 8386198 : 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 : 1653055 : adjust_equivalence_range (vrange &range)
192 : : {
193 : 1653055 : if (range.undefined_p () || !is_a<frange> (range))
194 : 1618826 : return;
195 : :
196 : 34229 : frange fr = as_a<frange> (range);
197 : : // If range includes 0 make sure both signs of zero are included.
198 : 34229 : if (fr.contains_p (dconst0) || fr.contains_p (dconstm0))
199 : : {
200 : 17986 : frange zeros (range.type (), dconstm0, dconst0);
201 : 17986 : range.union_ (zeros);
202 : 17986 : }
203 : 34229 : }
204 : :
205 : : // Given an equivalence set EQUIV, set all the bits in B that are still valid
206 : : // members of EQUIV in basic block BB.
207 : :
208 : : void
209 : 21060747 : relation_oracle::valid_equivs (bitmap b, const_bitmap equivs, basic_block bb)
210 : : {
211 : 21060747 : unsigned i;
212 : 21060747 : bitmap_iterator bi;
213 : 44060342 : EXECUTE_IF_SET_IN_BITMAP (equivs, 0, i, bi)
214 : : {
215 : 22999595 : tree ssa = ssa_name (i);
216 : 45999189 : if (ssa && !SSA_NAME_IN_FREE_LIST (ssa))
217 : : {
218 : 22999594 : const_bitmap ssa_equiv = equiv_set (ssa, bb);
219 : 22999594 : if (ssa_equiv == equivs)
220 : 22695227 : bitmap_set_bit (b, i);
221 : : }
222 : : }
223 : 21060747 : }
224 : :
225 : : // Return any known relation between SSA1 and SSA2 before stmt S is executed.
226 : : // If GET_RANGE is true, query the range of both operands first to ensure
227 : : // the definitions have been processed and any relations have be created.
228 : :
229 : : relation_kind
230 : 104845817 : relation_oracle::query (gimple *s, tree ssa1, tree ssa2)
231 : : {
232 : 104845817 : if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
233 : : return VREL_VARYING;
234 : 38804059 : return query (gimple_bb (s), ssa1, ssa2);
235 : : }
236 : :
237 : : // Return any known relation between SSA1 and SSA2 on edge E.
238 : : // If GET_RANGE is true, query the range of both operands first to ensure
239 : : // the definitions have been processed and any relations have be created.
240 : :
241 : : relation_kind
242 : 53768790 : relation_oracle::query (edge e, tree ssa1, tree ssa2)
243 : : {
244 : 53768790 : basic_block bb;
245 : 53768790 : if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
246 : : return VREL_VARYING;
247 : :
248 : : // Use destination block if it has a single predecessor, and this picks
249 : : // up any relation on the edge.
250 : : // Otherwise choose the src edge and the result is the same as on-exit.
251 : 39740748 : if (!single_pred_p (e->dest))
252 : 37785178 : bb = e->src;
253 : : else
254 : : bb = e->dest;
255 : :
256 : 39740748 : return query (bb, ssa1, ssa2);
257 : : }
258 : : // -------------------------------------------------------------------------
259 : :
260 : : // The very first element in the m_equiv chain is actually just a summary
261 : : // element in which the m_names bitmap is used to indicate that an ssa_name
262 : : // has an equivalence set in this block.
263 : : // This allows for much faster traversal of the DOM chain, as a search for
264 : : // SSA_NAME simply requires walking the DOM chain until a block is found
265 : : // which has the bit for SSA_NAME set. Then scan for the equivalency set in
266 : : // that block. No previous lists need be searched.
267 : :
268 : : // If SSA has an equivalence in this list, find and return it.
269 : : // Otherwise return NULL.
270 : :
271 : : equiv_chain *
272 : 178492547 : equiv_chain::find (unsigned ssa)
273 : : {
274 : 178492547 : equiv_chain *ptr = NULL;
275 : : // If there are equiv sets and SSA is in one in this list, find it.
276 : : // Otherwise return NULL.
277 : 178492547 : if (bitmap_bit_p (m_names, ssa))
278 : : {
279 : 173351160 : for (ptr = m_next; ptr; ptr = ptr->m_next)
280 : 173351160 : if (bitmap_bit_p (ptr->m_names, ssa))
281 : : break;
282 : : }
283 : 178492547 : return ptr;
284 : : }
285 : :
286 : : // Dump the names in this equivalence set.
287 : :
288 : : void
289 : 11 : equiv_chain::dump (FILE *f) const
290 : : {
291 : 11 : bitmap_iterator bi;
292 : 11 : unsigned i;
293 : :
294 : 11 : if (!m_names || bitmap_empty_p (m_names))
295 : 1 : return;
296 : 10 : fprintf (f, "Equivalence set : [");
297 : 10 : unsigned c = 0;
298 : 26 : EXECUTE_IF_SET_IN_BITMAP (m_names, 0, i, bi)
299 : : {
300 : 16 : if (ssa_name (i))
301 : : {
302 : 16 : if (c++)
303 : 6 : fprintf (f, ", ");
304 : 16 : print_generic_expr (f, ssa_name (i), TDF_SLIM);
305 : : }
306 : : }
307 : 10 : fprintf (f, "]\n");
308 : : }
309 : :
310 : : // Instantiate an equivalency oracle.
311 : :
312 : 25050555 : equiv_oracle::equiv_oracle ()
313 : : {
314 : 25050555 : bitmap_obstack_initialize (&m_bitmaps);
315 : 25050555 : m_equiv.create (0);
316 : 25050555 : m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
317 : 25050555 : m_equiv_set = BITMAP_ALLOC (&m_bitmaps);
318 : 25050555 : bitmap_tree_view (m_equiv_set);
319 : 25050555 : obstack_init (&m_chain_obstack);
320 : 25050555 : m_self_equiv.create (0);
321 : 50101110 : m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
322 : 25050555 : m_partial.create (0);
323 : 50101110 : m_partial.safe_grow_cleared (num_ssa_names + 1);
324 : 25050555 : }
325 : :
326 : : // Destruct an equivalency oracle.
327 : :
328 : 25050555 : equiv_oracle::~equiv_oracle ()
329 : : {
330 : 25050555 : m_partial.release ();
331 : 25050555 : m_self_equiv.release ();
332 : 25050555 : obstack_free (&m_chain_obstack, NULL);
333 : 25050555 : m_equiv.release ();
334 : 25050555 : bitmap_obstack_release (&m_bitmaps);
335 : 25050555 : }
336 : :
337 : : // Add a partial equivalence R between OP1 and OP2.
338 : :
339 : : void
340 : 9712092 : equiv_oracle::add_partial_equiv (relation_kind r, tree op1, tree op2)
341 : : {
342 : 9712092 : int v1 = SSA_NAME_VERSION (op1);
343 : 9712092 : int v2 = SSA_NAME_VERSION (op2);
344 : 9712092 : int prec2 = TYPE_PRECISION (TREE_TYPE (op2));
345 : 9712092 : int bits = pe_to_bits (r);
346 : 9712092 : gcc_checking_assert (bits && prec2 >= bits);
347 : :
348 : 19424184 : if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
349 : 26 : m_partial.safe_grow_cleared (num_ssa_names + 1);
350 : 19424184 : gcc_checking_assert (v1 < (int)m_partial.length ()
351 : : && v2 < (int)m_partial.length ());
352 : :
353 : 9712092 : pe_slice &pe1 = m_partial[v1];
354 : 9712092 : pe_slice &pe2 = m_partial[v2];
355 : :
356 : 9712092 : if (pe1.members)
357 : : {
358 : : // If the definition pe1 already has an entry, either the stmt is
359 : : // being re-evaluated, or the def was used before being registered.
360 : : // In either case, if PE2 has an entry, we simply do nothing.
361 : 159439 : if (pe2.members)
362 : : return;
363 : : // If there are no uses of op2, do not register.
364 : 140 : if (has_zero_uses (op2))
365 : : return;
366 : : // PE1 is the LHS and already has members, so everything in the set
367 : : // should be a slice of PE2 rather than PE1.
368 : 140 : pe2.code = pe_min (r, pe1.code);
369 : 140 : pe2.ssa_base = op2;
370 : 140 : pe2.members = pe1.members;
371 : 140 : bitmap_iterator bi;
372 : 140 : unsigned x;
373 : 420 : EXECUTE_IF_SET_IN_BITMAP (pe1.members, 0, x, bi)
374 : : {
375 : 280 : m_partial[x].ssa_base = op2;
376 : 280 : m_partial[x].code = pe_min (m_partial[x].code, pe2.code);
377 : : }
378 : 140 : bitmap_set_bit (pe1.members, v2);
379 : 140 : return;
380 : : }
381 : 9552653 : if (pe2.members)
382 : : {
383 : : // If there are no uses of op1, do not register.
384 : 712525 : if (has_zero_uses (op1))
385 : : return;
386 : 705816 : pe1.ssa_base = pe2.ssa_base;
387 : : // If pe2 is a 16 bit value, but only an 8 bit copy, we can't be any
388 : : // more than an 8 bit equivalence here, so choose MIN value.
389 : 705816 : pe1.code = pe_min (r, pe2.code);
390 : 705816 : pe1.members = pe2.members;
391 : 705816 : bitmap_set_bit (pe1.members, v1);
392 : : }
393 : : else
394 : : {
395 : : // If there are no uses of either operand, do not register.
396 : 8840128 : if (has_zero_uses (op1) || has_zero_uses (op2))
397 : : return;
398 : : // Neither name has an entry, simply create op1 as slice of op2.
399 : 8754717 : pe2.code = bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2)));
400 : 8754717 : if (pe2.code == VREL_VARYING)
401 : : return;
402 : 8706613 : pe2.ssa_base = op2;
403 : 8706613 : pe2.members = BITMAP_ALLOC (&m_bitmaps);
404 : 8706613 : bitmap_set_bit (pe2.members, v2);
405 : 8706613 : pe1.ssa_base = op2;
406 : 8706613 : pe1.code = r;
407 : 8706613 : pe1.members = pe2.members;
408 : 8706613 : bitmap_set_bit (pe1.members, v1);
409 : : }
410 : : }
411 : :
412 : : // Return the set of partial equivalences associated with NAME. The bitmap
413 : : // will be NULL if there are none.
414 : :
415 : : const pe_slice *
416 : 55617461 : equiv_oracle::partial_equiv_set (tree name)
417 : : {
418 : 55617461 : int v = SSA_NAME_VERSION (name);
419 : 111234922 : if (v >= (int)m_partial.length ())
420 : : return NULL;
421 : 55617461 : return &m_partial[v];
422 : : }
423 : :
424 : : // Query if there is a partial equivalence between SSA1 and SSA2. Return
425 : : // VREL_VARYING if there is not one. If BASE is non-null, return the base
426 : : // ssa-name this is a slice of.
427 : :
428 : : relation_kind
429 : 56403900 : equiv_oracle::partial_equiv (tree ssa1, tree ssa2, tree *base) const
430 : : {
431 : 56403900 : int v1 = SSA_NAME_VERSION (ssa1);
432 : 56403900 : int v2 = SSA_NAME_VERSION (ssa2);
433 : :
434 : 112807800 : if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
435 : : return VREL_VARYING;
436 : :
437 : 56403892 : const pe_slice &pe1 = m_partial[v1];
438 : 56403892 : const pe_slice &pe2 = m_partial[v2];
439 : 56403892 : if (pe1.members && pe2.members == pe1.members)
440 : : {
441 : 1045 : if (base)
442 : 0 : *base = pe1.ssa_base;
443 : 1045 : return pe_min (pe1.code, pe2.code);
444 : : }
445 : : return VREL_VARYING;
446 : : }
447 : :
448 : :
449 : : // Find and return the equivalency set for SSA along the dominators of BB.
450 : : // This is the external API.
451 : :
452 : : const_bitmap
453 : 142025198 : equiv_oracle::equiv_set (tree ssa, basic_block bb)
454 : : {
455 : : // Search the dominator tree for an equivalency.
456 : 142025198 : equiv_chain *equiv = find_equiv_dom (ssa, bb);
457 : 142025198 : if (equiv)
458 : 17565248 : return equiv->m_names;
459 : :
460 : : // Otherwise return a cached equiv set containing just this SSA.
461 : 124459950 : unsigned v = SSA_NAME_VERSION (ssa);
462 : 124459950 : if (v >= m_self_equiv.length ())
463 : 14 : m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
464 : :
465 : 124459950 : if (!m_self_equiv[v])
466 : : {
467 : 34054018 : m_self_equiv[v] = BITMAP_ALLOC (&m_bitmaps);
468 : 34054018 : bitmap_set_bit (m_self_equiv[v], v);
469 : : }
470 : 124459950 : return m_self_equiv[v];
471 : : }
472 : :
473 : : // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
474 : :
475 : : relation_kind
476 : 0 : equiv_oracle::query (basic_block bb, tree ssa1, tree ssa2)
477 : : {
478 : : // If the 2 ssa names share the same equiv set, they are equal.
479 : 0 : if (equiv_set (ssa1, bb) == equiv_set (ssa2, bb))
480 : : return VREL_EQ;
481 : :
482 : : // Check if there is a partial equivalence.
483 : 0 : return partial_equiv (ssa1, ssa2);
484 : : }
485 : :
486 : : // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
487 : :
488 : : relation_kind
489 : 0 : equiv_oracle::query (basic_block bb ATTRIBUTE_UNUSED, const_bitmap e1,
490 : : const_bitmap e2)
491 : : {
492 : : // If the 2 ssa names share the same equiv set, they are equal.
493 : 0 : if (bitmap_equal_p (e1, e2))
494 : 0 : return VREL_EQ;
495 : : return VREL_VARYING;
496 : : }
497 : :
498 : : // If SSA has an equivalence in block BB, find and return it.
499 : : // Otherwise return NULL.
500 : :
501 : : equiv_chain *
502 : 107497017 : equiv_oracle::find_equiv_block (unsigned ssa, int bb) const
503 : : {
504 : 214994034 : if (bb >= (int)m_equiv.length () || !m_equiv[bb])
505 : : return NULL;
506 : :
507 : 47525803 : return m_equiv[bb]->find (ssa);
508 : : }
509 : :
510 : : // Starting at block BB, walk the dominator chain looking for the nearest
511 : : // equivalence set containing NAME.
512 : :
513 : : equiv_chain *
514 : 158249138 : equiv_oracle::find_equiv_dom (tree name, basic_block bb) const
515 : : {
516 : 158249138 : unsigned v = SSA_NAME_VERSION (name);
517 : : // Short circuit looking for names which have no equivalences.
518 : : // Saves time looking for something which does not exist.
519 : 158249138 : if (!bitmap_bit_p (m_equiv_set, v))
520 : : return NULL;
521 : :
522 : : // NAME has at least once equivalence set, check to see if it has one along
523 : : // the dominator tree.
524 : 108618282 : for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
525 : : {
526 : 107497017 : equiv_chain *ptr = find_equiv_block (v, bb->index);
527 : 107497017 : if (ptr)
528 : : return ptr;
529 : : }
530 : : return NULL;
531 : : }
532 : :
533 : : // Register equivalence between ssa_name V and set EQUIV in block BB,
534 : :
535 : : bitmap
536 : 86130 : equiv_oracle::register_equiv (basic_block bb, unsigned v, equiv_chain *equiv)
537 : : {
538 : : // V will have an equivalency now.
539 : 86130 : bitmap_set_bit (m_equiv_set, v);
540 : :
541 : : // If that equiv chain is in this block, simply use it.
542 : 86130 : if (equiv->m_bb == bb)
543 : : {
544 : 25862 : bitmap_set_bit (equiv->m_names, v);
545 : 25862 : bitmap_set_bit (m_equiv[bb->index]->m_names, v);
546 : 25862 : return NULL;
547 : : }
548 : :
549 : : // Otherwise create an equivalence for this block which is a copy
550 : : // of equiv, the add V to the set.
551 : 60268 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
552 : 60268 : valid_equivs (b, equiv->m_names, bb);
553 : 60268 : bitmap_set_bit (b, v);
554 : 60268 : return b;
555 : : }
556 : :
557 : : // Register equivalence between set equiv_1 and equiv_2 in block BB.
558 : : // Return NULL if either name can be merged with the other. Otherwise
559 : : // return a pointer to the combined bitmap of names. This allows the
560 : : // caller to do any setup required for a new element.
561 : :
562 : : bitmap
563 : 4024851 : equiv_oracle::register_equiv (basic_block bb, equiv_chain *equiv_1,
564 : : equiv_chain *equiv_2)
565 : : {
566 : : // If equiv_1 is already in BB, use it as the combined set.
567 : 4024851 : if (equiv_1->m_bb == bb)
568 : : {
569 : 2328341 : valid_equivs (equiv_1->m_names, equiv_2->m_names, bb);
570 : : // Its hard to delete from a single linked list, so
571 : : // just clear the second one.
572 : 2328341 : if (equiv_2->m_bb == bb)
573 : 314510 : bitmap_clear (equiv_2->m_names);
574 : : else
575 : : // Ensure the new names are in the summary for BB.
576 : 2013831 : bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_1->m_names);
577 : 2328341 : return NULL;
578 : : }
579 : : // If equiv_2 is in BB, use it for the combined set.
580 : 1696510 : if (equiv_2->m_bb == bb)
581 : : {
582 : 3226 : valid_equivs (equiv_2->m_names, equiv_1->m_names, bb);
583 : : // Ensure the new names are in the summary.
584 : 3226 : bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_2->m_names);
585 : 3226 : return NULL;
586 : : }
587 : :
588 : : // At this point, neither equivalence is from this block.
589 : 1693284 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
590 : 1693284 : valid_equivs (b, equiv_1->m_names, bb);
591 : 1693284 : valid_equivs (b, equiv_2->m_names, bb);
592 : 1693284 : return b;
593 : : }
594 : :
595 : : // Create an equivalency set containing only SSA in its definition block.
596 : : // This is done the first time SSA is registered in an equivalency and blocks
597 : : // any DOM searches past the definition.
598 : :
599 : : void
600 : 7474513 : equiv_oracle::register_initial_def (tree ssa)
601 : : {
602 : 7474513 : if (SSA_NAME_IS_DEFAULT_DEF (ssa))
603 : : return;
604 : 7378997 : basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (ssa));
605 : :
606 : : // If defining stmt is not in the IL, simply return.
607 : 7378997 : if (!bb)
608 : : return;
609 : 7378996 : gcc_checking_assert (!find_equiv_dom (ssa, bb));
610 : :
611 : 7378996 : unsigned v = SSA_NAME_VERSION (ssa);
612 : 7378996 : bitmap_set_bit (m_equiv_set, v);
613 : 7378996 : bitmap equiv_set = BITMAP_ALLOC (&m_bitmaps);
614 : 7378996 : bitmap_set_bit (equiv_set, v);
615 : 7378996 : add_equiv_to_block (bb, equiv_set);
616 : : }
617 : :
618 : : // Register an equivalence between SSA1 and SSA2 in block BB.
619 : : // The equivalence oracle maintains a vector of equivalencies indexed by basic
620 : : // block. When an equivalence between SSA1 and SSA2 is registered in block BB,
621 : : // a query is made as to what equivalences both names have already, and
622 : : // any preexisting equivalences are merged to create a single equivalence
623 : : // containing all the ssa_names in this basic block.
624 : :
625 : : void
626 : 14134564 : equiv_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
627 : : {
628 : : // Process partial equivalencies.
629 : 14134564 : if (relation_partial_equiv_p (k))
630 : : {
631 : 9712092 : add_partial_equiv (k, ssa1, ssa2);
632 : 9712092 : return;
633 : : }
634 : : // Only handle equality relations.
635 : 4422472 : if (k != VREL_EQ)
636 : : return;
637 : :
638 : 4422472 : unsigned v1 = SSA_NAME_VERSION (ssa1);
639 : 4422472 : unsigned v2 = SSA_NAME_VERSION (ssa2);
640 : :
641 : : // If this is the first time an ssa_name has an equivalency registered
642 : : // create a self-equivalency record in the def block.
643 : 4422472 : if (!bitmap_bit_p (m_equiv_set, v1))
644 : 3991107 : register_initial_def (ssa1);
645 : 4422472 : if (!bitmap_bit_p (m_equiv_set, v2))
646 : 3483406 : register_initial_def (ssa2);
647 : :
648 : 4422472 : equiv_chain *equiv_1 = find_equiv_dom (ssa1, bb);
649 : 4422472 : equiv_chain *equiv_2 = find_equiv_dom (ssa2, bb);
650 : :
651 : : // Check if they are the same set
652 : 4422472 : if (equiv_1 && equiv_1 == equiv_2)
653 : : return;
654 : :
655 : 4122647 : bitmap equiv_set;
656 : :
657 : : // Case where we have 2 SSA_NAMEs that are not in any set.
658 : 4122647 : if (!equiv_1 && !equiv_2)
659 : : {
660 : 11666 : bitmap_set_bit (m_equiv_set, v1);
661 : 11666 : bitmap_set_bit (m_equiv_set, v2);
662 : :
663 : 11666 : equiv_set = BITMAP_ALLOC (&m_bitmaps);
664 : 11666 : bitmap_set_bit (equiv_set, v1);
665 : 11666 : bitmap_set_bit (equiv_set, v2);
666 : : }
667 : 4110981 : else if (!equiv_1 && equiv_2)
668 : 23394 : equiv_set = register_equiv (bb, v1, equiv_2);
669 : 4087587 : else if (equiv_1 && !equiv_2)
670 : 62736 : equiv_set = register_equiv (bb, v2, equiv_1);
671 : : else
672 : 4024851 : equiv_set = register_equiv (bb, equiv_1, equiv_2);
673 : :
674 : : // A non-null return is a bitmap that is to be added to the current
675 : : // block as a new equivalence.
676 : 4122647 : if (!equiv_set)
677 : : return;
678 : :
679 : 1765218 : add_equiv_to_block (bb, equiv_set);
680 : : }
681 : :
682 : : // Add an equivalency record in block BB containing bitmap EQUIV_SET.
683 : : // Note the internal caller is responsible for allocating EQUIV_SET properly.
684 : :
685 : : void
686 : 9144214 : equiv_oracle::add_equiv_to_block (basic_block bb, bitmap equiv_set)
687 : : {
688 : 9144214 : equiv_chain *ptr;
689 : :
690 : : // Check if this is the first time a block has an equivalence added.
691 : : // and create a header block. And set the summary for this block.
692 : 9144214 : limit_check (bb);
693 : 9144214 : if (!m_equiv[bb->index])
694 : : {
695 : 5778994 : ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
696 : : sizeof (equiv_chain));
697 : 5778994 : ptr->m_names = BITMAP_ALLOC (&m_bitmaps);
698 : 5778994 : bitmap_copy (ptr->m_names, equiv_set);
699 : 5778994 : ptr->m_bb = bb;
700 : 5778994 : ptr->m_next = NULL;
701 : 5778994 : m_equiv[bb->index] = ptr;
702 : : }
703 : :
704 : : // Now create the element for this equiv set and initialize it.
705 : 9144214 : ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack, sizeof (equiv_chain));
706 : 9144214 : ptr->m_names = equiv_set;
707 : 9144214 : ptr->m_bb = bb;
708 : 18288428 : gcc_checking_assert (bb->index < (int)m_equiv.length ());
709 : 9144214 : ptr->m_next = m_equiv[bb->index]->m_next;
710 : 9144214 : m_equiv[bb->index]->m_next = ptr;
711 : 9144214 : bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_set);
712 : 9144214 : }
713 : :
714 : : // Make sure the BB vector is big enough and grow it if needed.
715 : :
716 : : void
717 : 9144214 : equiv_oracle::limit_check (basic_block bb)
718 : : {
719 : 9144214 : int i = (bb) ? bb->index : last_basic_block_for_fn (cfun);
720 : 18288428 : if (i >= (int)m_equiv.length ())
721 : 1 : m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
722 : 9144214 : }
723 : :
724 : : // Dump the equivalence sets in BB to file F.
725 : :
726 : : void
727 : 251 : equiv_oracle::dump (FILE *f, basic_block bb) const
728 : : {
729 : 502 : if (bb->index >= (int)m_equiv.length ())
730 : : return;
731 : : // Process equivalences.
732 : 251 : if (m_equiv[bb->index])
733 : : {
734 : 9 : equiv_chain *ptr = m_equiv[bb->index]->m_next;
735 : 20 : for (; ptr; ptr = ptr->m_next)
736 : 11 : ptr->dump (f);
737 : : }
738 : : // Look for partial equivalences defined in this block..
739 : 13652 : for (unsigned i = 0; i < num_ssa_names; i++)
740 : : {
741 : 13401 : tree name = ssa_name (i);
742 : 18674 : if (!gimple_range_ssa_p (name) || !SSA_NAME_DEF_STMT (name))
743 : 8128 : continue;
744 : 5273 : if (i >= m_partial.length ())
745 : : break;
746 : 5273 : tree base = m_partial[i].ssa_base;
747 : 5273 : if (base && name != base && gimple_bb (SSA_NAME_DEF_STMT (name)) == bb)
748 : : {
749 : 40 : relation_kind k = partial_equiv (name, base);
750 : 40 : if (k != VREL_VARYING)
751 : : {
752 : 40 : value_relation vr (k, name, base);
753 : 40 : fprintf (f, "Partial equiv ");
754 : 40 : vr.dump (f);
755 : 40 : fputc ('\n',f);
756 : : }
757 : : }
758 : : }
759 : : }
760 : :
761 : : // Dump all equivalence sets known to the oracle.
762 : :
763 : : void
764 : 0 : equiv_oracle::dump (FILE *f) const
765 : : {
766 : 0 : fprintf (f, "Equivalency dump\n");
767 : 0 : for (unsigned i = 0; i < m_equiv.length (); i++)
768 : 0 : if (m_equiv[i] && BASIC_BLOCK_FOR_FN (cfun, i))
769 : : {
770 : 0 : fprintf (f, "BB%d\n", i);
771 : 0 : dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
772 : : }
773 : 0 : }
774 : :
775 : :
776 : : // --------------------------------------------------------------------------
777 : :
778 : : // Adjust the relation by Swapping the operands and relation.
779 : :
780 : : void
781 : 0 : value_relation::swap ()
782 : : {
783 : 0 : related = relation_swap (related);
784 : 0 : tree tmp = name1;
785 : 0 : name1 = name2;
786 : 0 : name2 = tmp;
787 : 0 : }
788 : :
789 : : // Perform an intersection between 2 relations. *this &&= p.
790 : : // Return false if the relations cannot be intersected.
791 : :
792 : : bool
793 : 3847858 : value_relation::intersect (value_relation &p)
794 : : {
795 : : // Save previous value
796 : 3847858 : relation_kind old = related;
797 : :
798 : 3847858 : if (p.op1 () == op1 () && p.op2 () == op2 ())
799 : 3846985 : related = relation_intersect (kind (), p.kind ());
800 : 873 : else if (p.op2 () == op1 () && p.op1 () == op2 ())
801 : 873 : related = relation_intersect (kind (), relation_swap (p.kind ()));
802 : : else
803 : : return false;
804 : :
805 : 3847858 : return old != related;
806 : : }
807 : :
808 : : // Perform a union between 2 relations. *this ||= p.
809 : :
810 : : bool
811 : 0 : value_relation::union_ (value_relation &p)
812 : : {
813 : : // Save previous value
814 : 0 : relation_kind old = related;
815 : :
816 : 0 : if (p.op1 () == op1 () && p.op2 () == op2 ())
817 : 0 : related = relation_union (kind(), p.kind());
818 : 0 : else if (p.op2 () == op1 () && p.op1 () == op2 ())
819 : 0 : related = relation_union (kind(), relation_swap (p.kind ()));
820 : : else
821 : : return false;
822 : :
823 : 0 : return old != related;
824 : : }
825 : :
826 : : // Identify and apply any transitive relations between REL
827 : : // and THIS. Return true if there was a transformation.
828 : :
829 : : bool
830 : 13121181 : value_relation::apply_transitive (const value_relation &rel)
831 : : {
832 : 13121181 : relation_kind k = VREL_VARYING;
833 : :
834 : : // Identify any common operand, and normalize the relations to
835 : : // the form : A < B B < C produces A < C
836 : 13121181 : if (rel.op1 () == name2)
837 : : {
838 : : // A < B B < C
839 : 2200059 : if (rel.op2 () == name1)
840 : : return false;
841 : 2130419 : k = relation_transitive (kind (), rel.kind ());
842 : 2130419 : if (k != VREL_VARYING)
843 : : {
844 : 839959 : related = k;
845 : 839959 : name2 = rel.op2 ();
846 : 839959 : return true;
847 : : }
848 : : }
849 : 10921122 : else if (rel.op1 () == name1)
850 : : {
851 : : // B > A B < C
852 : 6465804 : if (rel.op2 () == name2)
853 : : return false;
854 : 1800461 : k = relation_transitive (relation_swap (kind ()), rel.kind ());
855 : 1800461 : if (k != VREL_VARYING)
856 : : {
857 : 452781 : related = k;
858 : 452781 : name1 = name2;
859 : 452781 : name2 = rel.op2 ();
860 : 452781 : return true;
861 : : }
862 : : }
863 : 4455318 : else if (rel.op2 () == name2)
864 : : {
865 : : // A < B C > B
866 : 3840773 : if (rel.op1 () == name1)
867 : : return false;
868 : 3840773 : k = relation_transitive (kind (), relation_swap (rel.kind ()));
869 : 3840773 : if (k != VREL_VARYING)
870 : : {
871 : 508033 : related = k;
872 : 508033 : name2 = rel.op1 ();
873 : 508033 : return true;
874 : : }
875 : : }
876 : 614545 : else if (rel.op2 () == name1)
877 : : {
878 : : // B > A C > B
879 : 614545 : if (rel.op1 () == name2)
880 : : return false;
881 : 614545 : k = relation_transitive (relation_swap (kind ()),
882 : : relation_swap (rel.kind ()));
883 : 614545 : if (k != VREL_VARYING)
884 : : {
885 : 237488 : related = k;
886 : 237488 : name1 = name2;
887 : 237488 : name2 = rel.op1 ();
888 : 237488 : return true;
889 : : }
890 : : }
891 : : return false;
892 : : }
893 : :
894 : : // Create a trio from this value relation given LHS, OP1 and OP2.
895 : :
896 : : relation_trio
897 : 47747459 : value_relation::create_trio (tree lhs, tree op1, tree op2)
898 : : {
899 : 47747459 : relation_kind lhs_1;
900 : 47747459 : if (lhs == name1 && op1 == name2)
901 : 57450 : lhs_1 = related;
902 : 47690009 : else if (lhs == name2 && op1 == name1)
903 : 132016 : lhs_1 = relation_swap (related);
904 : : else
905 : : lhs_1 = VREL_VARYING;
906 : :
907 : 47747459 : relation_kind lhs_2;
908 : 47747459 : if (lhs == name1 && op2 == name2)
909 : 50321 : lhs_2 = related;
910 : 47697138 : else if (lhs == name2 && op2 == name1)
911 : 161614 : lhs_2 = relation_swap (related);
912 : : else
913 : : lhs_2 = VREL_VARYING;
914 : :
915 : 47747459 : relation_kind op_op;
916 : 47747459 : if (op1 == name1 && op2 == name2)
917 : 34213174 : op_op = related;
918 : 13534285 : else if (op1 == name2 && op2 == name1)
919 : 0 : op_op = relation_swap (related);
920 : 13534285 : else if (op1 == op2)
921 : : op_op = VREL_EQ;
922 : : else
923 : 13519371 : op_op = VREL_VARYING;
924 : :
925 : 47747459 : return relation_trio (lhs_1, lhs_2, op_op);
926 : : }
927 : :
928 : : // Dump the relation to file F.
929 : :
930 : : void
931 : 1037 : value_relation::dump (FILE *f) const
932 : : {
933 : 1037 : if (!name1 || !name2)
934 : : {
935 : 0 : fprintf (f, "no relation registered");
936 : 0 : return;
937 : : }
938 : 1037 : fputc ('(', f);
939 : 1037 : print_generic_expr (f, op1 (), TDF_SLIM);
940 : 1037 : print_relation (f, kind ());
941 : 1037 : print_generic_expr (f, op2 (), TDF_SLIM);
942 : 1037 : fputc(')', f);
943 : : }
944 : :
945 : : // This container is used to link relations in a chain.
946 : :
947 : : class relation_chain : public value_relation
948 : : {
949 : : public:
950 : : relation_chain *m_next;
951 : : };
952 : :
953 : : // Given relation record PTR in block BB, return the next relation in the
954 : : // list. If PTR is NULL, retreive the first relation in BB.
955 : : // If NAME is sprecified, return only relations which include NAME.
956 : : // Return NULL when there are no relations left.
957 : :
958 : : relation_chain *
959 : 84 : dom_oracle::next_relation (basic_block bb, relation_chain *ptr,
960 : : tree name) const
961 : : {
962 : 84 : relation_chain *p;
963 : : // No value_relation pointer is used to intialize the iterator.
964 : 84 : if (!ptr)
965 : : {
966 : 37 : int bbi = bb->index;
967 : 74 : if (bbi >= (int)m_relations.length())
968 : : return NULL;
969 : : else
970 : 37 : p = m_relations[bbi].m_head;
971 : : }
972 : : else
973 : 47 : p = ptr->m_next;
974 : :
975 : 84 : if (name)
976 : 0 : for ( ; p; p = p->m_next)
977 : 0 : if (p->op1 () == name || p->op2 () == name)
978 : : break;
979 : : return p;
980 : : }
981 : :
982 : : // Instatiate a block relation iterator to iterate over the relations
983 : : // on exit from block BB in ORACLE. Limit this to relations involving NAME
984 : : // if specified. Return the first such relation in VR if there is one.
985 : :
986 : 37 : block_relation_iterator::block_relation_iterator (const relation_oracle *oracle,
987 : : basic_block bb,
988 : : value_relation &vr,
989 : 37 : tree name)
990 : : {
991 : 37 : m_oracle = oracle;
992 : 37 : m_bb = bb;
993 : 37 : m_name = name;
994 : 37 : m_ptr = oracle->next_relation (bb, NULL, m_name);
995 : 37 : if (m_ptr)
996 : : {
997 : 37 : m_done = false;
998 : 37 : vr = *m_ptr;
999 : : }
1000 : : else
1001 : 0 : m_done = true;
1002 : 37 : }
1003 : :
1004 : : // Retreive the next relation from the iterator and return it in VR.
1005 : :
1006 : : void
1007 : 47 : block_relation_iterator::get_next_relation (value_relation &vr)
1008 : : {
1009 : 47 : m_ptr = m_oracle->next_relation (m_bb, m_ptr, m_name);
1010 : 47 : if (m_ptr)
1011 : : {
1012 : 10 : vr = *m_ptr;
1013 : 10 : if (m_name)
1014 : : {
1015 : 0 : if (vr.op1 () != m_name)
1016 : : {
1017 : 0 : gcc_checking_assert (vr.op2 () == m_name);
1018 : 0 : vr.swap ();
1019 : : }
1020 : : }
1021 : : }
1022 : : else
1023 : 37 : m_done = true;
1024 : 47 : }
1025 : :
1026 : : // ------------------------------------------------------------------------
1027 : :
1028 : : // Find the relation between any ssa_name in B1 and any name in B2 in LIST.
1029 : : // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1030 : :
1031 : : relation_kind
1032 : 284462637 : relation_chain_head::find_relation (const_bitmap b1, const_bitmap b2) const
1033 : : {
1034 : 284462637 : if (!m_names)
1035 : : return VREL_VARYING;
1036 : :
1037 : : // If both b1 and b2 aren't referenced in this block, cant be a relation
1038 : 174135078 : if (!bitmap_intersect_p (m_names, b1) || !bitmap_intersect_p (m_names, b2))
1039 : 169733309 : return VREL_VARYING;
1040 : :
1041 : : // Search for the first relation that contains BOTH an element from B1
1042 : : // and B2, and return that relation.
1043 : 14568970 : for (relation_chain *ptr = m_head; ptr ; ptr = ptr->m_next)
1044 : : {
1045 : 12837171 : unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1046 : 12837171 : unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1047 : 12837171 : if (bitmap_bit_p (b1, op1) && bitmap_bit_p (b2, op2))
1048 : 2540690 : return ptr->kind ();
1049 : 10296481 : if (bitmap_bit_p (b1, op2) && bitmap_bit_p (b2, op1))
1050 : 129280 : return relation_swap (ptr->kind ());
1051 : : }
1052 : :
1053 : : return VREL_VARYING;
1054 : : }
1055 : :
1056 : : // Instantiate a relation oracle.
1057 : :
1058 : 25050555 : dom_oracle::dom_oracle (bool do_trans_p)
1059 : : {
1060 : 25050555 : m_do_trans_p = do_trans_p;
1061 : 25050555 : m_relations.create (0);
1062 : 25050555 : m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1063 : 25050555 : m_relation_set = BITMAP_ALLOC (&m_bitmaps);
1064 : 25050555 : m_tmp = BITMAP_ALLOC (&m_bitmaps);
1065 : 25050555 : m_tmp2 = BITMAP_ALLOC (&m_bitmaps);
1066 : 25050555 : }
1067 : :
1068 : : // Destruct a relation oracle.
1069 : :
1070 : 50101110 : dom_oracle::~dom_oracle ()
1071 : : {
1072 : 25050555 : m_relations.release ();
1073 : 50101110 : }
1074 : :
1075 : : // Register relation K between ssa_name OP1 and OP2 on STMT.
1076 : :
1077 : : void
1078 : 30561298 : relation_oracle::record (gimple *stmt, relation_kind k, tree op1, tree op2)
1079 : : {
1080 : 30561298 : gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1081 : 30561298 : gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1082 : 30561298 : gcc_checking_assert (stmt && gimple_bb (stmt));
1083 : :
1084 : : // Don't register lack of a relation.
1085 : 30561298 : if (k == VREL_VARYING)
1086 : : return;
1087 : :
1088 : 30561298 : if (dump_file && (dump_flags & TDF_DETAILS))
1089 : : {
1090 : 510 : value_relation vr (k, op1, op2);
1091 : 510 : fprintf (dump_file, " Registering value_relation ");
1092 : 510 : vr.dump (dump_file);
1093 : 510 : fprintf (dump_file, " (bb%d) at ", gimple_bb (stmt)->index);
1094 : 510 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1095 : : }
1096 : :
1097 : : // If an equivalence is being added between a PHI and one of its arguments
1098 : : // make sure that that argument is not defined in the same block.
1099 : : // This can happen along back edges and the equivalence will not be
1100 : : // applicable as it would require a use before def.
1101 : 30561298 : if (k == VREL_EQ && is_a<gphi *> (stmt))
1102 : : {
1103 : 2230781 : tree phi_def = gimple_phi_result (stmt);
1104 : 2230781 : gcc_checking_assert (phi_def == op1 || phi_def == op2);
1105 : 2230781 : tree arg = op2;
1106 : 2230781 : if (phi_def == op2)
1107 : 0 : arg = op1;
1108 : 2230781 : if (gimple_bb (stmt) == gimple_bb (SSA_NAME_DEF_STMT (arg)))
1109 : : {
1110 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1111 : : {
1112 : 0 : fprintf (dump_file, " Not registered due to ");
1113 : 0 : print_generic_expr (dump_file, arg, TDF_SLIM);
1114 : 0 : fprintf (dump_file, " being defined in the same block.\n");
1115 : : }
1116 : 0 : return;
1117 : : }
1118 : : }
1119 : 30561298 : record (gimple_bb (stmt), k, op1, op2);
1120 : : }
1121 : :
1122 : : // Register relation K between ssa_name OP1 and OP2 on edge E.
1123 : :
1124 : : void
1125 : 6523491 : relation_oracle::record (edge e, relation_kind k, tree op1, tree op2)
1126 : : {
1127 : 6523491 : gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
1128 : 6523491 : gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
1129 : :
1130 : : // Do not register lack of relation, or blocks which have more than
1131 : : // edge E for a predecessor.
1132 : 6523491 : if (k == VREL_VARYING || !single_pred_p (e->dest))
1133 : : return;
1134 : :
1135 : 6523491 : if (dump_file && (dump_flags & TDF_DETAILS))
1136 : : {
1137 : 116 : value_relation vr (k, op1, op2);
1138 : 116 : fprintf (dump_file, " Registering value_relation ");
1139 : 116 : vr.dump (dump_file);
1140 : 116 : fprintf (dump_file, " on (%d->%d)\n", e->src->index, e->dest->index);
1141 : : }
1142 : :
1143 : 6523491 : record (e->dest, k, op1, op2);
1144 : : }
1145 : :
1146 : : // Register relation K between OP! and OP2 in block BB.
1147 : : // This creates the record and searches for existing records in the dominator
1148 : : // tree to merge with.
1149 : :
1150 : : void
1151 : 36511343 : dom_oracle::record (basic_block bb, relation_kind k, tree op1, tree op2)
1152 : : {
1153 : : // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1154 : : // and no other relation makes sense.
1155 : 36511343 : if (op1 == op2)
1156 : : return;
1157 : :
1158 : : // Equivalencies are handled by the equivalence oracle.
1159 : 36504495 : if (relation_equiv_p (k))
1160 : 14134564 : equiv_oracle::record (bb, k, op1, op2);
1161 : : else
1162 : : {
1163 : : // if neither op1 nor op2 are in a relation before this is registered,
1164 : : // there will be no transitive.
1165 : 22369931 : bool check = bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op1))
1166 : 37903032 : || bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op2));
1167 : 22369931 : relation_chain *ptr = set_one_relation (bb, k, op1, op2);
1168 : 22369931 : if (ptr && check
1169 : 22369931 : && (m_relations[bb->index].m_num_relations
1170 : 6176116 : < param_relation_block_limit))
1171 : 6175968 : register_transitives (bb, *ptr);
1172 : : }
1173 : : }
1174 : :
1175 : : // Register relation K between OP! and OP2 in block BB.
1176 : : // This creates the record and searches for existing records in the dominator
1177 : : // tree to merge with. Return the record, or NULL if no record was created.
1178 : :
1179 : : relation_chain *
1180 : 24408192 : dom_oracle::set_one_relation (basic_block bb, relation_kind k, tree op1,
1181 : : tree op2)
1182 : : {
1183 : 24408192 : gcc_checking_assert (k != VREL_VARYING && k != VREL_EQ);
1184 : :
1185 : 24408192 : value_relation vr(k, op1, op2);
1186 : 24408192 : int bbi = bb->index;
1187 : :
1188 : 48816384 : if (bbi >= (int)m_relations.length())
1189 : 25 : m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
1190 : :
1191 : : // Summary bitmap indicating what ssa_names have relations in this BB.
1192 : 24408192 : bitmap bm = m_relations[bbi].m_names;
1193 : 24408192 : if (!bm)
1194 : 13860747 : bm = m_relations[bbi].m_names = BITMAP_ALLOC (&m_bitmaps);
1195 : 24408192 : unsigned v1 = SSA_NAME_VERSION (op1);
1196 : 24408192 : unsigned v2 = SSA_NAME_VERSION (op2);
1197 : :
1198 : 24408192 : relation_kind curr;
1199 : 24408192 : relation_chain *ptr;
1200 : 24408192 : curr = find_relation_block (bbi, v1, v2, &ptr);
1201 : : // There is an existing relation in this block, just intersect with it.
1202 : 24408192 : if (curr != VREL_VARYING)
1203 : : {
1204 : 3847858 : if (dump_file && (dump_flags & TDF_DETAILS))
1205 : : {
1206 : 18 : fprintf (dump_file, " Intersecting with existing ");
1207 : 18 : ptr->dump (dump_file);
1208 : : }
1209 : : // Check into whether we can simply replace the relation rather than
1210 : : // intersecting it. This may help with some optimistic iterative
1211 : : // updating algorithms.
1212 : 3847858 : bool new_rel = ptr->intersect (vr);
1213 : 3847858 : if (dump_file && (dump_flags & TDF_DETAILS))
1214 : : {
1215 : 18 : fprintf (dump_file, " to produce ");
1216 : 18 : ptr->dump (dump_file);
1217 : 32 : fprintf (dump_file, " %s.\n", new_rel ? "Updated" : "No Change");
1218 : : }
1219 : : // If there was no change, return no record..
1220 : 3847858 : if (!new_rel)
1221 : : return NULL;
1222 : : }
1223 : : else
1224 : : {
1225 : 20560334 : if (m_relations[bbi].m_num_relations >= param_relation_block_limit)
1226 : : {
1227 : 102832 : if (dump_file && (dump_flags & TDF_DETAILS))
1228 : 0 : fprintf (dump_file, " Not registered due to bb being full\n");
1229 : 102832 : return NULL;
1230 : : }
1231 : 20457502 : m_relations[bbi].m_num_relations++;
1232 : : // Check for an existing relation further up the DOM chain.
1233 : : // By including dominating relations, The first one found in any search
1234 : : // will be the aggregate of all the previous ones.
1235 : 20457502 : curr = find_relation_dom (bb, v1, v2);
1236 : 20457502 : if (curr != VREL_VARYING)
1237 : 621328 : k = relation_intersect (curr, k);
1238 : :
1239 : 20457502 : bitmap_set_bit (bm, v1);
1240 : 20457502 : bitmap_set_bit (bm, v2);
1241 : 20457502 : bitmap_set_bit (m_relation_set, v1);
1242 : 20457502 : bitmap_set_bit (m_relation_set, v2);
1243 : :
1244 : 20457502 : ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1245 : : sizeof (relation_chain));
1246 : 20457502 : ptr->set_relation (k, op1, op2);
1247 : 20457502 : ptr->m_next = m_relations[bbi].m_head;
1248 : 20457502 : m_relations[bbi].m_head = ptr;
1249 : : }
1250 : 20648267 : return ptr;
1251 : : }
1252 : :
1253 : : // Starting at ROOT_BB search the DOM tree looking for relations which
1254 : : // may produce transitive relations to RELATION. EQUIV1 and EQUIV2 are
1255 : : // bitmaps for op1/op2 and any of their equivalences that should also be
1256 : : // considered.
1257 : :
1258 : : void
1259 : 6175968 : dom_oracle::register_transitives (basic_block root_bb,
1260 : : const value_relation &relation)
1261 : : {
1262 : : // Only register transitives if they are requested.
1263 : 6175968 : if (!m_do_trans_p)
1264 : : return;
1265 : 6175958 : basic_block bb;
1266 : : // Only apply transitives to certain kinds of operations.
1267 : 6175958 : switch (relation.kind ())
1268 : : {
1269 : 4512266 : case VREL_LE:
1270 : 4512266 : case VREL_LT:
1271 : 4512266 : case VREL_GT:
1272 : 4512266 : case VREL_GE:
1273 : 4512266 : break;
1274 : : default:
1275 : : return;
1276 : : }
1277 : :
1278 : 4512266 : const_bitmap equiv1 = equiv_set (relation.op1 (), root_bb);
1279 : 4512266 : const_bitmap equiv2 = equiv_set (relation.op2 (), root_bb);
1280 : :
1281 : 4512266 : const unsigned work_budget = param_transitive_relations_work_bound;
1282 : 4512266 : unsigned avail_budget = work_budget;
1283 : 86588220 : for (bb = root_bb; bb;
1284 : : /* Advancing to the next immediate dominator eats from the budget,
1285 : : if none is left after that there's no point to continue. */
1286 : : bb = (--avail_budget > 0
1287 : 82131464 : ? get_immediate_dominator (CDI_DOMINATORS, bb) : nullptr))
1288 : : {
1289 : 82209288 : int bbi = bb->index;
1290 : 164418576 : if (bbi >= (int)m_relations.length())
1291 : 4662 : continue;
1292 : 82204626 : const_bitmap bm = m_relations[bbi].m_names;
1293 : 82204626 : if (!bm)
1294 : 57966116 : continue;
1295 : 24238510 : if (!bitmap_intersect_p (bm, equiv1) && !bitmap_intersect_p (bm, equiv2))
1296 : 16082184 : continue;
1297 : : // At least one of the 2 ops has a relation in this block.
1298 : 8156326 : relation_chain *ptr;
1299 : 38537761 : for (ptr = m_relations[bbi].m_head; ptr ; ptr = ptr->m_next)
1300 : : {
1301 : : // In the presence of an equivalence, 2 operands may do not
1302 : : // naturally match. ie with equivalence a_2 == b_3
1303 : : // given c_1 < a_2 && b_3 < d_4
1304 : : // convert the second relation (b_3 < d_4) to match any
1305 : : // equivalences to found in the first relation.
1306 : : // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
1307 : : // transitive operation: c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
1308 : :
1309 : 30459259 : tree r1, r2;
1310 : 30459259 : tree p1 = ptr->op1 ();
1311 : 30459259 : tree p2 = ptr->op2 ();
1312 : : // Find which equivalence is in the first operand.
1313 : 30459259 : if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p1)))
1314 : : r1 = p1;
1315 : 23992900 : else if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p2)))
1316 : : r1 = p2;
1317 : : else
1318 : 23308541 : r1 = NULL_TREE;
1319 : :
1320 : : // Find which equivalence is in the second operand.
1321 : 30459259 : if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p1)))
1322 : : r2 = p1;
1323 : 28258645 : else if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p2)))
1324 : : r2 = p2;
1325 : : else
1326 : 19752355 : r2 = NULL_TREE;
1327 : :
1328 : : // Ignore if both NULL (not relevant relation) or the same,
1329 : 30459259 : if (r1 == r2)
1330 : : ;
1331 : :
1332 : : else
1333 : : {
1334 : : // Any operand not an equivalence, just take the real operand.
1335 : 13121181 : if (!r1)
1336 : 5971192 : r1 = relation.op1 ();
1337 : 13121181 : if (!r2)
1338 : 2415006 : r2 = relation.op2 ();
1339 : :
1340 : 13121181 : value_relation nr (relation.kind (), r1, r2);
1341 : 13121181 : if (nr.apply_transitive (*ptr))
1342 : : {
1343 : : // If the new relation is already present we know any
1344 : : // further processing is already reflected above it.
1345 : : // When we ran into the limit of relations on root_bb
1346 : : // we can give up as well.
1347 : 2038261 : if (!set_one_relation (root_bb, nr.kind (),
1348 : : nr.op1 (), nr.op2 ()))
1349 : 71095 : return;
1350 : 1967166 : if (dump_file && (dump_flags & TDF_DETAILS))
1351 : : {
1352 : 2 : fprintf (dump_file,
1353 : : " Registering transitive relation ");
1354 : 2 : nr.dump (dump_file);
1355 : 2 : fputc ('\n', dump_file);
1356 : : }
1357 : : }
1358 : : }
1359 : : /* Processed one relation, abort if we've eaten up our budget. */
1360 : 30388164 : if (--avail_budget == 0)
1361 : : return;
1362 : : }
1363 : : }
1364 : : }
1365 : :
1366 : : // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
1367 : : // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1368 : :
1369 : : relation_kind
1370 : 235295030 : dom_oracle::find_relation_block (unsigned bb, const_bitmap b1,
1371 : : const_bitmap b2) const
1372 : : {
1373 : 235295030 : if (bb >= m_relations.length())
1374 : : return VREL_VARYING;
1375 : :
1376 : 235293543 : return m_relations[bb].find_relation (b1, b2);
1377 : : }
1378 : :
1379 : : // Search the DOM tree for a relation between an element of equivalency set B1
1380 : : // and B2, starting with block BB.
1381 : :
1382 : : relation_kind
1383 : 17055603 : dom_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1384 : : {
1385 : 17055603 : relation_kind r;
1386 : 17055603 : if (bitmap_equal_p (b1, b2))
1387 : : return VREL_EQ;
1388 : :
1389 : : // If either name does not occur in a relation anywhere, there isn't one.
1390 : 17055603 : if (!bitmap_intersect_p (m_relation_set, b1)
1391 : 17055603 : || !bitmap_intersect_p (m_relation_set, b2))
1392 : 8825843 : return VREL_VARYING;
1393 : :
1394 : : // Search each block in the DOM tree checking.
1395 : 242837246 : for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1396 : : {
1397 : 235295030 : r = find_relation_block (bb->index, b1, b2);
1398 : 235295030 : if (r != VREL_VARYING)
1399 : : return r;
1400 : : }
1401 : : return VREL_VARYING;
1402 : :
1403 : : }
1404 : :
1405 : : // Find a relation in block BB between ssa version V1 and V2. If a relation
1406 : : // is found, return a pointer to the chain object in OBJ.
1407 : :
1408 : : relation_kind
1409 : 547840246 : dom_oracle::find_relation_block (int bb, unsigned v1, unsigned v2,
1410 : : relation_chain **obj) const
1411 : : {
1412 : 1095680492 : if (bb >= (int)m_relations.length())
1413 : : return VREL_VARYING;
1414 : :
1415 : 547834624 : const_bitmap bm = m_relations[bb].m_names;
1416 : 547834624 : if (!bm)
1417 : : return VREL_VARYING;
1418 : :
1419 : : // If both b1 and b2 aren't referenced in this block, cant be a relation
1420 : 402388418 : if (!bitmap_bit_p (bm, v1) || !bitmap_bit_p (bm, v2))
1421 : 389515278 : return VREL_VARYING;
1422 : :
1423 : 12873140 : relation_chain *ptr;
1424 : 71752703 : for (ptr = m_relations[bb].m_head; ptr ; ptr = ptr->m_next)
1425 : : {
1426 : 69092374 : unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
1427 : 69092374 : unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
1428 : 69092374 : if (v1 == op1 && v2 == op2)
1429 : : {
1430 : 9945151 : if (obj)
1431 : 3846985 : *obj = ptr;
1432 : 9945151 : return ptr->kind ();
1433 : : }
1434 : 59147223 : if (v1 == op2 && v2 == op1)
1435 : : {
1436 : 267660 : if (obj)
1437 : 873 : *obj = ptr;
1438 : 267660 : return relation_swap (ptr->kind ());
1439 : : }
1440 : : }
1441 : :
1442 : : return VREL_VARYING;
1443 : : }
1444 : :
1445 : : // Find a relation between SSA version V1 and V2 in the dominator tree
1446 : : // starting with block BB
1447 : :
1448 : : relation_kind
1449 : 33444425 : dom_oracle::find_relation_dom (basic_block bb, unsigned v1, unsigned v2) const
1450 : : {
1451 : 33444425 : relation_kind r;
1452 : : // IF either name does not occur in a relation anywhere, there isn't one.
1453 : 33444425 : if (!bitmap_bit_p (m_relation_set, v1) || !bitmap_bit_p (m_relation_set, v2))
1454 : 17725022 : return VREL_VARYING;
1455 : :
1456 : 532786504 : for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1457 : : {
1458 : 523432054 : r = find_relation_block (bb->index, v1, v2);
1459 : 523432054 : if (r != VREL_VARYING)
1460 : : return r;
1461 : : }
1462 : : return VREL_VARYING;
1463 : :
1464 : : }
1465 : :
1466 : : // Query if there is a relation between SSA1 and SS2 in block BB or a
1467 : : // dominator of BB
1468 : :
1469 : : relation_kind
1470 : 56779929 : dom_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1471 : : {
1472 : 56779929 : relation_kind kind;
1473 : 56779929 : unsigned v1 = SSA_NAME_VERSION (ssa1);
1474 : 56779929 : unsigned v2 = SSA_NAME_VERSION (ssa2);
1475 : 56779929 : if (v1 == v2)
1476 : : return VREL_EQ;
1477 : :
1478 : : // If v1 or v2 do not have any relations or equivalences, a partial
1479 : : // equivalence is the only possibility.
1480 : 94368455 : if ((!bitmap_bit_p (m_relation_set, v1) && !has_equiv_p (v1))
1481 : 59873275 : || (!bitmap_bit_p (m_relation_set, v2) && !has_equiv_p (v2)))
1482 : 43416718 : return partial_equiv (ssa1, ssa2);
1483 : :
1484 : : // Check for equivalence first. They must be in each equivalency set.
1485 : 13268565 : const_bitmap equiv1 = equiv_set (ssa1, bb);
1486 : 13268565 : const_bitmap equiv2 = equiv_set (ssa2, bb);
1487 : 13268565 : if (bitmap_bit_p (equiv1, v2) && bitmap_bit_p (equiv2, v1))
1488 : : return VREL_EQ;
1489 : :
1490 : 12987142 : kind = partial_equiv (ssa1, ssa2);
1491 : 12987142 : if (kind != VREL_VARYING)
1492 : : return kind;
1493 : :
1494 : : // Initially look for a direct relationship and just return that.
1495 : 12986923 : kind = find_relation_dom (bb, v1, v2);
1496 : 12986923 : if (kind != VREL_VARYING)
1497 : : return kind;
1498 : :
1499 : : // Query using the equivalence sets.
1500 : 7243298 : kind = query (bb, equiv1, equiv2);
1501 : 7243298 : return kind;
1502 : : }
1503 : :
1504 : : // Dump all the relations in block BB to file F.
1505 : :
1506 : : void
1507 : 251 : dom_oracle::dump (FILE *f, basic_block bb) const
1508 : : {
1509 : 251 : equiv_oracle::dump (f,bb);
1510 : :
1511 : 502 : if (bb->index >= (int)m_relations.length ())
1512 : 214 : return;
1513 : 251 : if (!m_relations[bb->index].m_names)
1514 : : return;
1515 : :
1516 : 37 : value_relation vr;
1517 : 84 : FOR_EACH_RELATION_BB (this, bb, vr)
1518 : : {
1519 : 47 : fprintf (f, "Relational : ");
1520 : 47 : vr.dump (f);
1521 : 47 : fprintf (f, "\n");
1522 : : }
1523 : : }
1524 : :
1525 : : // Dump all the relations known to file F.
1526 : :
1527 : : void
1528 : 0 : dom_oracle::dump (FILE *f) const
1529 : : {
1530 : 0 : fprintf (f, "Relation dump\n");
1531 : 0 : for (unsigned i = 0; i < m_relations.length (); i++)
1532 : 0 : if (BASIC_BLOCK_FOR_FN (cfun, i))
1533 : : {
1534 : 0 : fprintf (f, "BB%d\n", i);
1535 : 0 : dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
1536 : : }
1537 : 0 : }
1538 : :
1539 : : void
1540 : 0 : relation_oracle::debug () const
1541 : : {
1542 : 0 : dump (stderr);
1543 : 0 : }
1544 : :
1545 : 28360057 : path_oracle::path_oracle (relation_oracle *oracle)
1546 : : {
1547 : 28360057 : set_root_oracle (oracle);
1548 : 28360057 : bitmap_obstack_initialize (&m_bitmaps);
1549 : 28360057 : obstack_init (&m_chain_obstack);
1550 : :
1551 : : // Initialize header records.
1552 : 28360057 : m_equiv.m_names = BITMAP_ALLOC (&m_bitmaps);
1553 : 28360057 : m_equiv.m_bb = NULL;
1554 : 28360057 : m_equiv.m_next = NULL;
1555 : 28360057 : m_relations.m_names = BITMAP_ALLOC (&m_bitmaps);
1556 : 28360057 : m_relations.m_head = NULL;
1557 : 28360057 : m_killed_defs = BITMAP_ALLOC (&m_bitmaps);
1558 : 28360057 : }
1559 : :
1560 : 56720114 : path_oracle::~path_oracle ()
1561 : : {
1562 : 28360057 : obstack_free (&m_chain_obstack, NULL);
1563 : 28360057 : bitmap_obstack_release (&m_bitmaps);
1564 : 56720114 : }
1565 : :
1566 : : // Return the equiv set for SSA, and if there isn't one, check for equivs
1567 : : // starting in block BB.
1568 : :
1569 : : const_bitmap
1570 : 130966744 : path_oracle::equiv_set (tree ssa, basic_block bb)
1571 : : {
1572 : : // Check the list first.
1573 : 130966744 : equiv_chain *ptr = m_equiv.find (SSA_NAME_VERSION (ssa));
1574 : 130966744 : if (ptr)
1575 : 72482743 : return ptr->m_names;
1576 : :
1577 : : // Otherwise defer to the root oracle.
1578 : 58484001 : if (m_root)
1579 : 51260430 : return m_root->equiv_set (ssa, bb);
1580 : :
1581 : : // Allocate a throw away bitmap if there isn't a root oracle.
1582 : 7223571 : bitmap tmp = BITMAP_ALLOC (&m_bitmaps);
1583 : 7223571 : bitmap_set_bit (tmp, SSA_NAME_VERSION (ssa));
1584 : 7223571 : return tmp;
1585 : : }
1586 : :
1587 : : // Register an equivalence between SSA1 and SSA2 resolving unknowns from
1588 : : // block BB.
1589 : :
1590 : : void
1591 : 7652892 : path_oracle::register_equiv (basic_block bb, tree ssa1, tree ssa2)
1592 : : {
1593 : 7652892 : const_bitmap equiv_1 = equiv_set (ssa1, bb);
1594 : 7652892 : const_bitmap equiv_2 = equiv_set (ssa2, bb);
1595 : :
1596 : : // Check if they are the same set, if so, we're done.
1597 : 7652892 : if (bitmap_equal_p (equiv_1, equiv_2))
1598 : : return;
1599 : :
1600 : : // Don't mess around, simply create a new record and insert it first.
1601 : 7641172 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
1602 : 7641172 : valid_equivs (b, equiv_1, bb);
1603 : 7641172 : valid_equivs (b, equiv_2, bb);
1604 : :
1605 : 7641172 : equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1606 : : sizeof (equiv_chain));
1607 : 7641172 : ptr->m_names = b;
1608 : 7641172 : ptr->m_bb = NULL;
1609 : 7641172 : ptr->m_next = m_equiv.m_next;
1610 : 7641172 : m_equiv.m_next = ptr;
1611 : 7641172 : bitmap_ior_into (m_equiv.m_names, b);
1612 : : }
1613 : :
1614 : : // Register killing definition of an SSA_NAME.
1615 : :
1616 : : void
1617 : 56402104 : path_oracle::killing_def (tree ssa)
1618 : : {
1619 : 56402104 : if (dump_file && (dump_flags & TDF_DETAILS))
1620 : : {
1621 : 818 : fprintf (dump_file, " Registering killing_def (path_oracle) ");
1622 : 818 : print_generic_expr (dump_file, ssa, TDF_SLIM);
1623 : 818 : fprintf (dump_file, "\n");
1624 : : }
1625 : :
1626 : 56402104 : unsigned v = SSA_NAME_VERSION (ssa);
1627 : :
1628 : 56402104 : bitmap_set_bit (m_killed_defs, v);
1629 : 56402104 : bitmap_set_bit (m_equiv.m_names, v);
1630 : :
1631 : : // Now add an equivalency with itself so we don't look to the root oracle.
1632 : 56402104 : bitmap b = BITMAP_ALLOC (&m_bitmaps);
1633 : 56402104 : bitmap_set_bit (b, v);
1634 : 56402104 : equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
1635 : : sizeof (equiv_chain));
1636 : 56402104 : ptr->m_names = b;
1637 : 56402104 : ptr->m_bb = NULL;
1638 : 56402104 : ptr->m_next = m_equiv.m_next;
1639 : 56402104 : m_equiv.m_next = ptr;
1640 : :
1641 : : // Walk the relation list and remove SSA from any relations.
1642 : 56402104 : if (!bitmap_bit_p (m_relations.m_names, v))
1643 : : return;
1644 : :
1645 : 122499 : bitmap_clear_bit (m_relations.m_names, v);
1646 : 122499 : relation_chain **prev = &(m_relations.m_head);
1647 : 122499 : relation_chain *next = NULL;
1648 : 416871 : for (relation_chain *ptr = m_relations.m_head; ptr; ptr = next)
1649 : : {
1650 : 294372 : gcc_checking_assert (*prev == ptr);
1651 : 294372 : next = ptr->m_next;
1652 : 294372 : if (SSA_NAME_VERSION (ptr->op1 ()) == v
1653 : 294372 : || SSA_NAME_VERSION (ptr->op2 ()) == v)
1654 : 121330 : *prev = ptr->m_next;
1655 : : else
1656 : 173042 : prev = &(ptr->m_next);
1657 : : }
1658 : : }
1659 : :
1660 : : // Register relation K between SSA1 and SSA2, resolving unknowns by
1661 : : // querying from BB.
1662 : :
1663 : : void
1664 : 27550408 : path_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
1665 : : {
1666 : : // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1667 : : // and no other relation makes sense.
1668 : 27550408 : if (ssa1 == ssa2)
1669 : : return;
1670 : :
1671 : 27537899 : if (dump_file && (dump_flags & TDF_DETAILS))
1672 : : {
1673 : 286 : value_relation vr (k, ssa1, ssa2);
1674 : 286 : fprintf (dump_file, " Registering value_relation (path_oracle) ");
1675 : 286 : vr.dump (dump_file);
1676 : 286 : fprintf (dump_file, " (root: bb%d)\n", bb->index);
1677 : : }
1678 : :
1679 : 27537899 : relation_kind curr = query (bb, ssa1, ssa2);
1680 : 27537899 : if (curr != VREL_VARYING)
1681 : 2076659 : k = relation_intersect (curr, k);
1682 : :
1683 : 27537899 : if (k == VREL_EQ)
1684 : : {
1685 : 7652892 : register_equiv (bb, ssa1, ssa2);
1686 : 7652892 : return;
1687 : : }
1688 : :
1689 : 19885007 : bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa1));
1690 : 19885007 : bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa2));
1691 : 19885007 : relation_chain *ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
1692 : : sizeof (relation_chain));
1693 : 19885007 : ptr->set_relation (k, ssa1, ssa2);
1694 : 19885007 : ptr->m_next = m_relations.m_head;
1695 : 19885007 : m_relations.m_head = ptr;
1696 : : }
1697 : :
1698 : : // Query for a relationship between equiv set B1 and B2, resolving unknowns
1699 : : // starting at block BB.
1700 : :
1701 : : relation_kind
1702 : 49169094 : path_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
1703 : : {
1704 : 49169094 : if (bitmap_equal_p (b1, b2))
1705 : : return VREL_EQ;
1706 : :
1707 : 49169094 : relation_kind k = m_relations.find_relation (b1, b2);
1708 : :
1709 : : // Do not look at the root oracle for names that have been killed
1710 : : // along the path.
1711 : 49169094 : if (bitmap_intersect_p (m_killed_defs, b1)
1712 : 49169094 : || bitmap_intersect_p (m_killed_defs, b2))
1713 : 37054530 : return k;
1714 : :
1715 : 12114564 : if (k == VREL_VARYING && m_root)
1716 : 9812305 : k = m_root->query (bb, b1, b2);
1717 : :
1718 : : return k;
1719 : : }
1720 : :
1721 : : // Query for a relationship between SSA1 and SSA2, resolving unknowns
1722 : : // starting at block BB.
1723 : :
1724 : : relation_kind
1725 : 49715621 : path_oracle::query (basic_block bb, tree ssa1, tree ssa2)
1726 : : {
1727 : 49715621 : unsigned v1 = SSA_NAME_VERSION (ssa1);
1728 : 49715621 : unsigned v2 = SSA_NAME_VERSION (ssa2);
1729 : :
1730 : 49715621 : if (v1 == v2)
1731 : : return VREL_EQ;
1732 : :
1733 : 49649114 : const_bitmap equiv_1 = equiv_set (ssa1, bb);
1734 : 49649114 : const_bitmap equiv_2 = equiv_set (ssa2, bb);
1735 : 49649114 : if (bitmap_bit_p (equiv_1, v2) && bitmap_bit_p (equiv_2, v1))
1736 : : return VREL_EQ;
1737 : :
1738 : 49169094 : return query (bb, equiv_1, equiv_2);
1739 : : }
1740 : :
1741 : : // Reset any relations registered on this path. ORACLE is the root
1742 : : // oracle to use.
1743 : :
1744 : : void
1745 : 23138347 : path_oracle::reset_path (relation_oracle *oracle)
1746 : : {
1747 : 23138347 : set_root_oracle (oracle);
1748 : 23138347 : m_equiv.m_next = NULL;
1749 : 23138347 : bitmap_clear (m_equiv.m_names);
1750 : 23138347 : m_relations.m_head = NULL;
1751 : 23138347 : bitmap_clear (m_relations.m_names);
1752 : 23138347 : bitmap_clear (m_killed_defs);
1753 : 23138347 : }
1754 : :
1755 : : // Dump relation in basic block... Do nothing here.
1756 : :
1757 : : void
1758 : 0 : path_oracle::dump (FILE *, basic_block) const
1759 : : {
1760 : 0 : }
1761 : :
1762 : : // Dump the relations and equivalencies found in the path.
1763 : :
1764 : : void
1765 : 0 : path_oracle::dump (FILE *f) const
1766 : : {
1767 : 0 : equiv_chain *ptr = m_equiv.m_next;
1768 : 0 : relation_chain *ptr2 = m_relations.m_head;
1769 : :
1770 : 0 : if (ptr || ptr2)
1771 : 0 : fprintf (f, "\npath_oracle:\n");
1772 : :
1773 : 0 : for (; ptr; ptr = ptr->m_next)
1774 : 0 : ptr->dump (f);
1775 : :
1776 : 0 : for (; ptr2; ptr2 = ptr2->m_next)
1777 : : {
1778 : 0 : fprintf (f, "Relational : ");
1779 : 0 : ptr2->dump (f);
1780 : 0 : fprintf (f, "\n");
1781 : : }
1782 : 0 : }
1783 : :
1784 : : // ------------------------------------------------------------------------
1785 : : // EQUIV iterator. Although we have bitmap iterators, don't expose that it
1786 : : // is currently a bitmap. Use an export iterator to hide future changes.
1787 : :
1788 : : // Construct a basic iterator over an equivalence bitmap.
1789 : :
1790 : 48566245 : equiv_relation_iterator::equiv_relation_iterator (relation_oracle *oracle,
1791 : : basic_block bb, tree name,
1792 : 48566245 : bool full, bool partial)
1793 : : {
1794 : 48566245 : m_name = name;
1795 : 48566245 : m_oracle = oracle;
1796 : 48566245 : m_pe = partial ? oracle->partial_equiv_set (name) : NULL;
1797 : 48566245 : m_bm = NULL;
1798 : 48566245 : if (full)
1799 : 48566245 : m_bm = oracle->equiv_set (name, bb);
1800 : 48566245 : if (!m_bm && m_pe)
1801 : 0 : m_bm = m_pe->members;
1802 : 48566245 : if (m_bm)
1803 : 48566244 : bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1804 : 48566245 : }
1805 : :
1806 : : // Move to the next export bitmap spot.
1807 : :
1808 : : void
1809 : 62971919 : equiv_relation_iterator::next ()
1810 : : {
1811 : 62971919 : bmp_iter_next (&m_bi, &m_y);
1812 : 62971919 : }
1813 : :
1814 : : // Fetch the name of the next export in the export list. Return NULL if
1815 : : // iteration is done.
1816 : :
1817 : : tree
1818 : 57513024 : equiv_relation_iterator::get_name (relation_kind *rel)
1819 : : {
1820 : 62971863 : if (!m_bm)
1821 : : return NULL_TREE;
1822 : :
1823 : 116997002 : while (bmp_iter_set (&m_bi, &m_y))
1824 : : {
1825 : : // Do not return self.
1826 : 62971919 : tree t = ssa_name (m_y);
1827 : 62971919 : if (t && t != m_name)
1828 : : {
1829 : 8946779 : relation_kind k = VREL_EQ;
1830 : 8946779 : if (m_pe && m_bm == m_pe->members)
1831 : : {
1832 : 7051217 : const pe_slice *equiv_pe = m_oracle->partial_equiv_set (t);
1833 : 7051217 : if (equiv_pe && equiv_pe->members == m_pe->members)
1834 : 7051217 : k = pe_min (m_pe->code, equiv_pe->code);
1835 : : else
1836 : : k = VREL_VARYING;
1837 : : }
1838 : 7051217 : if (relation_equiv_p (k))
1839 : : {
1840 : 8946779 : if (rel)
1841 : 8946779 : *rel = k;
1842 : 8946779 : return t;
1843 : : }
1844 : : }
1845 : 54025140 : next ();
1846 : : }
1847 : :
1848 : : // Process partial equivs after full equivs if both were requested.
1849 : 54025083 : if (m_pe && m_bm != m_pe->members)
1850 : : {
1851 : 48566244 : m_bm = m_pe->members;
1852 : 48566244 : if (m_bm)
1853 : : {
1854 : : // Recursively call back to process First PE.
1855 : 5458839 : bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
1856 : 5458839 : return get_name (rel);
1857 : : }
1858 : : }
1859 : : return NULL_TREE;
1860 : : }
1861 : :
1862 : : #if CHECKING_P
1863 : : #include "selftest.h"
1864 : :
1865 : : namespace selftest
1866 : : {
1867 : : void
1868 : 4 : relation_tests ()
1869 : : {
1870 : : // rr_*_table tables use unsigned char rather than relation_kind.
1871 : 4 : ASSERT_LT (VREL_LAST, UCHAR_MAX);
1872 : : // Verify commutativity of relation_intersect and relation_union.
1873 : 36 : for (relation_kind r1 = VREL_VARYING; r1 < VREL_PE8;
1874 : 32 : r1 = relation_kind (r1 + 1))
1875 : 288 : for (relation_kind r2 = VREL_VARYING; r2 < VREL_PE8;
1876 : 256 : r2 = relation_kind (r2 + 1))
1877 : : {
1878 : 256 : ASSERT_EQ (relation_intersect (r1, r2), relation_intersect (r2, r1));
1879 : 256 : ASSERT_EQ (relation_union (r1, r2), relation_union (r2, r1));
1880 : : }
1881 : 4 : }
1882 : :
1883 : : } // namespace selftest
1884 : :
1885 : : #endif // CHECKING_P
|