Line data Source code
1 : /* Gimple ranger SSA cache implementation.
2 : Copyright (C) 2017-2026 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
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License 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 "insn-codes.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "ssa.h"
29 : #include "gimple-pretty-print.h"
30 : #include "gimple-range.h"
31 : #include "value-range-storage.h"
32 : #include "tree-cfg.h"
33 : #include "target.h"
34 : #include "attribs.h"
35 : #include "gimple-iterator.h"
36 : #include "gimple-walk.h"
37 : #include "cfganal.h"
38 :
39 : #define DEBUG_RANGE_CACHE (dump_file \
40 : && (param_ranger_debug & RANGER_DEBUG_CACHE))
41 :
42 : // This class represents the API into a cache of ranges for an SSA_NAME.
43 : // Routines must be implemented to set, get, and query if a value is set.
44 :
45 : class ssa_block_ranges
46 : {
47 : public:
48 28716392 : ssa_block_ranges (tree t) : m_type (t) { }
49 : virtual bool set_bb_range (const_basic_block bb, const vrange &r) = 0;
50 : virtual bool get_bb_range (vrange &r, const_basic_block bb) = 0;
51 : virtual bool bb_range_p (const_basic_block bb) = 0;
52 :
53 : void dump(FILE *f);
54 : private:
55 : tree m_type;
56 : };
57 :
58 : // Print the list of known ranges for file F in a nice format.
59 :
60 : void
61 0 : ssa_block_ranges::dump (FILE *f)
62 : {
63 0 : basic_block bb;
64 0 : value_range r (m_type);
65 :
66 0 : FOR_EACH_BB_FN (bb, cfun)
67 0 : if (get_bb_range (r, bb))
68 : {
69 0 : fprintf (f, "BB%d -> ", bb->index);
70 0 : r.dump (f);
71 0 : fprintf (f, "\n");
72 : }
73 0 : }
74 :
75 : // This class implements the range cache as a linear vector, indexed by BB.
76 : // It caches a varying and undefined range which are used instead of
77 : // allocating new ones each time.
78 :
79 : class sbr_vector : public ssa_block_ranges
80 : {
81 : public:
82 : sbr_vector (tree t, vrange_allocator *allocator, bool zero_p = true);
83 :
84 : virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
85 : virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
86 : virtual bool bb_range_p (const_basic_block bb) override;
87 : protected:
88 : vrange_storage **m_tab; // Non growing vector.
89 : int m_tab_size;
90 : vrange_storage *m_varying;
91 : vrange_storage *m_undefined;
92 : tree m_type;
93 : vrange_allocator *m_range_allocator;
94 : bool m_zero_p;
95 : void grow ();
96 : };
97 :
98 :
99 : // Initialize a block cache for an ssa_name of type T.
100 :
101 28616858 : sbr_vector::sbr_vector (tree t, vrange_allocator *allocator, bool zero_p)
102 28616858 : : ssa_block_ranges (t)
103 : {
104 28616858 : gcc_checking_assert (TYPE_P (t));
105 28616858 : m_type = t;
106 28616858 : m_zero_p = zero_p;
107 28616858 : m_range_allocator = allocator;
108 28616858 : m_tab_size = last_basic_block_for_fn (cfun) + 1;
109 57233716 : m_tab = static_cast <vrange_storage **>
110 28616858 : (allocator->alloc (m_tab_size * sizeof (vrange_storage *)));
111 28616858 : if (zero_p)
112 25111043 : memset (m_tab, 0, m_tab_size * sizeof (vrange *));
113 :
114 : // Create the cached type range.
115 28616858 : m_varying = m_range_allocator->clone_varying (t);
116 28616858 : m_undefined = m_range_allocator->clone_undefined (t);
117 28616858 : }
118 :
119 : // Grow the vector when the CFG has increased in size.
120 :
121 : void
122 10371 : sbr_vector::grow ()
123 : {
124 10371 : int curr_bb_size = last_basic_block_for_fn (cfun);
125 10371 : gcc_checking_assert (curr_bb_size > m_tab_size);
126 :
127 : // Increase the max of a)128, b)needed increase * 2, c)10% of current_size.
128 10371 : int inc = MAX ((curr_bb_size - m_tab_size) * 2, 128);
129 10371 : inc = MAX (inc, curr_bb_size / 10);
130 10371 : int new_size = inc + curr_bb_size;
131 :
132 : // Allocate new memory, copy the old vector and clear the new space.
133 10371 : vrange_storage **t = static_cast <vrange_storage **>
134 10371 : (m_range_allocator->alloc (new_size * sizeof (vrange_storage *)));
135 10371 : memcpy (t, m_tab, m_tab_size * sizeof (vrange_storage *));
136 10371 : if (m_zero_p)
137 7963 : memset (t + m_tab_size, 0, (new_size - m_tab_size) * sizeof (vrange_storage *));
138 :
139 10371 : m_tab = t;
140 10371 : m_tab_size = new_size;
141 10371 : }
142 :
143 : // Set the range for block BB to be R.
144 :
145 : bool
146 74711926 : sbr_vector::set_bb_range (const_basic_block bb, const vrange &r)
147 : {
148 74711926 : vrange_storage *m;
149 74711926 : if (bb->index >= m_tab_size)
150 10371 : grow ();
151 74711926 : if (r.varying_p ())
152 23227091 : m = m_varying;
153 51484835 : else if (r.undefined_p ())
154 5289939 : m = m_undefined;
155 : else
156 46194896 : m = m_range_allocator->clone (r);
157 74711926 : m_tab[bb->index] = m;
158 74711926 : return true;
159 : }
160 :
161 : // Return the range associated with block BB in R. Return false if
162 : // there is no range.
163 :
164 : bool
165 327018498 : sbr_vector::get_bb_range (vrange &r, const_basic_block bb)
166 : {
167 327018498 : if (bb->index >= m_tab_size)
168 : return false;
169 327010630 : vrange_storage *m = m_tab[bb->index];
170 327010630 : if (m)
171 : {
172 245844630 : m->get_vrange (r, m_type);
173 245844630 : return true;
174 : }
175 : return false;
176 : }
177 :
178 : // Return true if a range is present.
179 :
180 : bool
181 240683301 : sbr_vector::bb_range_p (const_basic_block bb)
182 : {
183 240683301 : if (bb->index < m_tab_size)
184 240672428 : return m_tab[bb->index] != NULL;
185 : return false;
186 : }
187 :
188 : // Like an sbr_vector, except it uses a bitmap to manage whether value is set
189 : // or not rather than cleared memory.
190 :
191 : class sbr_lazy_vector : public sbr_vector
192 : {
193 : public:
194 : sbr_lazy_vector (tree t, vrange_allocator *allocator, bitmap_obstack *bm);
195 :
196 : virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
197 : virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
198 : virtual bool bb_range_p (const_basic_block bb) override;
199 : protected:
200 : bitmap m_has_value;
201 : };
202 :
203 3505815 : sbr_lazy_vector::sbr_lazy_vector (tree t, vrange_allocator *allocator,
204 3505815 : bitmap_obstack *bm)
205 3505815 : : sbr_vector (t, allocator, false)
206 : {
207 3505815 : m_has_value = BITMAP_ALLOC (bm);
208 3505815 : }
209 :
210 : bool
211 11902643 : sbr_lazy_vector::set_bb_range (const_basic_block bb, const vrange &r)
212 : {
213 11902643 : sbr_vector::set_bb_range (bb, r);
214 11902643 : bitmap_set_bit (m_has_value, bb->index);
215 11902643 : return true;
216 : }
217 :
218 : bool
219 265080554 : sbr_lazy_vector::get_bb_range (vrange &r, const_basic_block bb)
220 : {
221 265080554 : if (bitmap_bit_p (m_has_value, bb->index))
222 40808999 : return sbr_vector::get_bb_range (r, bb);
223 : return false;
224 : }
225 :
226 : bool
227 44678873 : sbr_lazy_vector::bb_range_p (const_basic_block bb)
228 : {
229 44678873 : return bitmap_bit_p (m_has_value, bb->index);
230 : }
231 :
232 : // This class implements the on entry cache via a sparse bitmap.
233 : // It uses the quad bit routines to access 4 bits at a time.
234 : // A value of 0 (the default) means there is no entry, and a value of
235 : // 1 thru SBR_NUM represents an element in the m_range vector.
236 : // Varying is given the first value (1) and pre-cached.
237 : // SBR_NUM + 1 represents the value of UNDEFINED, and is never stored.
238 : // SBR_NUM is the number of values that can be cached.
239 : // Indexes are 1..SBR_NUM and are stored locally at m_range[0..SBR_NUM-1]
240 :
241 : #define SBR_NUM 14
242 : #define SBR_UNDEF SBR_NUM + 1
243 : #define SBR_VARYING 1
244 :
245 : class sbr_sparse_bitmap : public ssa_block_ranges
246 : {
247 : public:
248 : sbr_sparse_bitmap (tree t, vrange_allocator *allocator, bitmap_obstack *bm);
249 : virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
250 : virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
251 : virtual bool bb_range_p (const_basic_block bb) override;
252 : private:
253 : void bitmap_set_quad (bitmap head, int quad, int quad_value);
254 : int bitmap_get_quad (const_bitmap head, int quad);
255 : vrange_allocator *m_range_allocator;
256 : vrange_storage *m_range[SBR_NUM];
257 : bitmap_head bitvec;
258 : tree m_type;
259 : };
260 :
261 : // Initialize a block cache for an ssa_name of type T.
262 :
263 99534 : sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, vrange_allocator *allocator,
264 99534 : bitmap_obstack *bm)
265 99534 : : ssa_block_ranges (t)
266 : {
267 99534 : gcc_checking_assert (TYPE_P (t));
268 99534 : m_type = t;
269 99534 : bitmap_initialize (&bitvec, bm);
270 99534 : bitmap_tree_view (&bitvec);
271 99534 : m_range_allocator = allocator;
272 : // Pre-cache varying.
273 99534 : m_range[0] = m_range_allocator->clone_varying (t);
274 : // Pre-cache zero and non-zero values for pointers.
275 99534 : if (POINTER_TYPE_P (t))
276 : {
277 1515 : prange nonzero;
278 1515 : nonzero.set_nonzero (t);
279 1515 : m_range[1] = m_range_allocator->clone (nonzero);
280 1515 : prange zero;
281 1515 : zero.set_zero (t);
282 1515 : m_range[2] = m_range_allocator->clone (zero);
283 1515 : }
284 : else
285 98019 : m_range[1] = m_range[2] = NULL;
286 : // Clear SBR_NUM entries.
287 1194408 : for (int x = 3; x < SBR_NUM; x++)
288 1094874 : m_range[x] = 0;
289 99534 : }
290 :
291 : // Set 4 bit values in a sparse bitmap. This allows a bitmap to
292 : // function as a sparse array of 4 bit values.
293 : // QUAD is the index, QUAD_VALUE is the 4 bit value to set.
294 :
295 : inline void
296 485223 : sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
297 : {
298 485223 : bitmap_set_aligned_chunk (head, quad, 4, (BITMAP_WORD) quad_value);
299 : }
300 :
301 : // Get a 4 bit value from a sparse bitmap. This allows a bitmap to
302 : // function as a sparse array of 4 bit values.
303 : // QUAD is the index.
304 : inline int
305 15392686 : sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
306 : {
307 30785372 : return (int) bitmap_get_aligned_chunk (head, quad, 4);
308 : }
309 :
310 : // Set the range on entry to basic block BB to R.
311 :
312 : bool
313 485223 : sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const vrange &r)
314 : {
315 485223 : if (r.undefined_p ())
316 : {
317 29005 : bitmap_set_quad (&bitvec, bb->index, SBR_UNDEF);
318 29005 : return true;
319 : }
320 :
321 : // Loop thru the values to see if R is already present.
322 851103 : for (int x = 0; x < SBR_NUM; x++)
323 840097 : if (!m_range[x] || m_range[x]->equal_p (r))
324 : {
325 445212 : if (!m_range[x])
326 110375 : m_range[x] = m_range_allocator->clone (r);
327 445212 : bitmap_set_quad (&bitvec, bb->index, x + 1);
328 445212 : return true;
329 : }
330 : // All values are taken, default to VARYING.
331 11006 : bitmap_set_quad (&bitvec, bb->index, SBR_VARYING);
332 11006 : return false;
333 : }
334 :
335 : // Return the range associated with block BB in R. Return false if
336 : // there is no range.
337 :
338 : bool
339 12935908 : sbr_sparse_bitmap::get_bb_range (vrange &r, const_basic_block bb)
340 : {
341 12935908 : int value = bitmap_get_quad (&bitvec, bb->index);
342 :
343 12935908 : if (!value)
344 : return false;
345 :
346 1924544 : gcc_checking_assert (value <= SBR_UNDEF);
347 1924544 : if (value == SBR_UNDEF)
348 70070 : r.set_undefined ();
349 : else
350 1854474 : m_range[value - 1]->get_vrange (r, m_type);
351 : return true;
352 : }
353 :
354 : // Return true if a range is present.
355 :
356 : bool
357 2456778 : sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
358 : {
359 2456778 : return (bitmap_get_quad (&bitvec, bb->index) != 0);
360 : }
361 :
362 : // -------------------------------------------------------------------------
363 :
364 : // Initialize the block cache.
365 :
366 28204919 : block_range_cache::block_range_cache ()
367 : {
368 28204919 : bitmap_obstack_initialize (&m_bitmaps);
369 28204919 : m_ssa_ranges.create (0);
370 56409838 : m_ssa_ranges.safe_grow_cleared (num_ssa_names);
371 28204919 : m_range_allocator = new vrange_allocator;
372 28204919 : }
373 :
374 : // Remove any m_block_caches which have been created.
375 :
376 28204919 : block_range_cache::~block_range_cache ()
377 : {
378 28204919 : delete m_range_allocator;
379 : // Release the vector itself.
380 28204919 : m_ssa_ranges.release ();
381 28204919 : bitmap_obstack_release (&m_bitmaps);
382 28204919 : }
383 :
384 : // Set the range for NAME on entry to block BB to R.
385 : // If it has not been accessed yet, allocate it first.
386 :
387 : bool
388 75197149 : block_range_cache::set_bb_range (tree name, const_basic_block bb,
389 : const vrange &r)
390 : {
391 75197149 : unsigned v = SSA_NAME_VERSION (name);
392 75197149 : if (v >= m_ssa_ranges.length ())
393 2 : m_ssa_ranges.safe_grow_cleared (num_ssa_names);
394 :
395 75197149 : if (!m_ssa_ranges[v])
396 : {
397 : // Use sparse bitmap representation if there are too many basic blocks.
398 28716392 : if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
399 : {
400 99534 : void *r = m_range_allocator->alloc (sizeof (sbr_sparse_bitmap));
401 99534 : m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
402 : m_range_allocator,
403 99534 : &m_bitmaps);
404 : }
405 28616858 : else if (last_basic_block_for_fn (cfun) < param_vrp_vector_threshold)
406 : {
407 : // For small CFGs use the basic vector implementation.
408 25111043 : void *r = m_range_allocator->alloc (sizeof (sbr_vector));
409 25111043 : m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
410 25111043 : m_range_allocator);
411 : }
412 : else
413 : {
414 : // Otherwise use the sparse vector implementation.
415 3505815 : void *r = m_range_allocator->alloc (sizeof (sbr_lazy_vector));
416 3505815 : m_ssa_ranges[v] = new (r) sbr_lazy_vector (TREE_TYPE (name),
417 : m_range_allocator,
418 3505815 : &m_bitmaps);
419 : }
420 : }
421 75197149 : return m_ssa_ranges[v]->set_bb_range (bb, r);
422 : }
423 :
424 :
425 : // Return a pointer to the ssa_block_cache for NAME. If it has not been
426 : // accessed yet, return NULL.
427 :
428 : inline ssa_block_ranges *
429 1152271025 : block_range_cache::query_block_ranges (tree name)
430 : {
431 1152271025 : unsigned v = SSA_NAME_VERSION (name);
432 1152271025 : if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
433 : return NULL;
434 : return m_ssa_ranges[v];
435 : }
436 :
437 :
438 :
439 : // Return the range for NAME on entry to BB in R. Return true if there
440 : // is one.
441 :
442 : bool
443 769333650 : block_range_cache::get_bb_range (vrange &r, tree name, const_basic_block bb)
444 : {
445 769333650 : ssa_block_ranges *ptr = query_block_ranges (name);
446 769333650 : if (ptr)
447 564224692 : return ptr->get_bb_range (r, bb);
448 : return false;
449 : }
450 :
451 : // Return true if NAME has a range set in block BB.
452 :
453 : bool
454 382937375 : block_range_cache::bb_range_p (tree name, const_basic_block bb)
455 : {
456 382937375 : ssa_block_ranges *ptr = query_block_ranges (name);
457 382937375 : if (ptr)
458 287818952 : return ptr->bb_range_p (bb);
459 : return false;
460 : }
461 :
462 : // Print all known block caches to file F.
463 :
464 : void
465 0 : block_range_cache::dump (FILE *f)
466 : {
467 0 : unsigned x;
468 0 : for (x = 1; x < m_ssa_ranges.length (); ++x)
469 : {
470 0 : if (m_ssa_ranges[x])
471 : {
472 0 : fprintf (f, " Ranges for ");
473 0 : print_generic_expr (f, ssa_name (x), TDF_NONE);
474 0 : fprintf (f, ":\n");
475 0 : m_ssa_ranges[x]->dump (f);
476 0 : fprintf (f, "\n");
477 : }
478 : }
479 0 : }
480 :
481 : // Print all known ranges on entry to block BB to file F.
482 :
483 : void
484 250 : block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
485 : {
486 250 : unsigned x;
487 250 : bool summarize_varying = false;
488 12462 : for (x = 1; x < m_ssa_ranges.length (); ++x)
489 : {
490 12212 : if (!m_ssa_ranges[x])
491 21886 : continue;
492 :
493 1269 : if (!gimple_range_ssa_p (ssa_name (x)))
494 0 : continue;
495 :
496 1269 : value_range r (TREE_TYPE (ssa_name (x)));
497 1269 : if (m_ssa_ranges[x]->get_bb_range (r, bb))
498 : {
499 224 : if (!print_varying && r.varying_p ())
500 : {
501 0 : summarize_varying = true;
502 0 : continue;
503 : }
504 224 : print_generic_expr (f, ssa_name (x), TDF_NONE);
505 224 : fprintf (f, "\t");
506 224 : r.dump(f);
507 224 : fprintf (f, "\n");
508 : }
509 1269 : }
510 : // If there were any varying entries, lump them all together.
511 250 : if (summarize_varying)
512 : {
513 0 : fprintf (f, "VARYING_P on entry : ");
514 0 : for (x = 1; x < m_ssa_ranges.length (); ++x)
515 : {
516 0 : if (!m_ssa_ranges[x])
517 0 : continue;
518 :
519 0 : if (!gimple_range_ssa_p (ssa_name (x)))
520 0 : continue;
521 :
522 0 : value_range r (TREE_TYPE (ssa_name (x)));
523 0 : if (m_ssa_ranges[x]->get_bb_range (r, bb))
524 : {
525 0 : if (r.varying_p ())
526 : {
527 0 : print_generic_expr (f, ssa_name (x), TDF_NONE);
528 0 : fprintf (f, " ");
529 : }
530 : }
531 0 : }
532 0 : fprintf (f, "\n");
533 : }
534 250 : }
535 :
536 : // -------------------------------------------------------------------------
537 :
538 : // Initialize an ssa cache.
539 :
540 56369540 : ssa_cache::ssa_cache ()
541 : {
542 56369540 : m_tab.create (0);
543 56369540 : m_range_allocator = new vrange_allocator;
544 56369540 : }
545 :
546 : // Deconstruct an ssa cache.
547 :
548 56369531 : ssa_cache::~ssa_cache ()
549 : {
550 56369531 : m_tab.release ();
551 56369531 : delete m_range_allocator;
552 56369531 : }
553 :
554 : // Enable a query to evaluate staements/ramnges based on picking up ranges
555 : // from just an ssa-cache.
556 :
557 : bool
558 585 : ssa_cache::range_of_expr (vrange &r, tree expr, gimple *stmt)
559 : {
560 585 : if (!gimple_range_ssa_p (expr))
561 0 : return get_tree_range (r, expr, stmt);
562 :
563 585 : if (!get_range (r, expr))
564 20 : gimple_range_global (r, expr, cfun);
565 : return true;
566 : }
567 :
568 : // Return TRUE if the global range of NAME has a cache entry.
569 :
570 : bool
571 4315201 : ssa_cache::has_range (tree name) const
572 : {
573 4315201 : unsigned v = SSA_NAME_VERSION (name);
574 4315201 : if (v >= m_tab.length ())
575 : return false;
576 3871666 : return m_tab[v] != NULL;
577 : }
578 :
579 : // Retrieve the global range of NAME from cache memory if it exists.
580 : // Return the value in R.
581 :
582 : bool
583 1171050168 : ssa_cache::get_range (vrange &r, tree name) const
584 : {
585 1171050168 : unsigned v = SSA_NAME_VERSION (name);
586 1171050168 : if (v >= m_tab.length ())
587 : return false;
588 :
589 1158979526 : vrange_storage *stow = m_tab[v];
590 1158979526 : if (!stow)
591 : return false;
592 943810224 : stow->get_vrange (r, TREE_TYPE (name));
593 943810224 : return true;
594 : }
595 :
596 : // Set the range for NAME to R in the ssa cache.
597 : // Return TRUE if there was already a range set, otherwise false.
598 :
599 : bool
600 153374329 : ssa_cache::set_range (tree name, const vrange &r)
601 : {
602 153374329 : unsigned v = SSA_NAME_VERSION (name);
603 153374329 : if (v >= m_tab.length ())
604 15986090 : m_tab.safe_grow_cleared (num_ssa_names + 1);
605 :
606 153374329 : vrange_storage *m = m_tab[v];
607 153374329 : if (m && m->fits_p (r))
608 21234427 : m->set_vrange (r);
609 : else
610 132139902 : m_tab[v] = m_range_allocator->clone (r);
611 153374329 : return m != NULL;
612 : }
613 :
614 : // If NAME has a range, intersect it with R, otherwise set it to R.
615 : // Return TRUE if the range is new or changes.
616 :
617 : bool
618 126 : ssa_cache::merge_range (tree name, const vrange &r)
619 : {
620 126 : unsigned v = SSA_NAME_VERSION (name);
621 126 : if (v >= m_tab.length ())
622 12 : m_tab.safe_grow_cleared (num_ssa_names + 1);
623 :
624 126 : vrange_storage *m = m_tab[v];
625 : // Check if this is a new value.
626 126 : if (!m)
627 125 : m_tab[v] = m_range_allocator->clone (r);
628 : else
629 : {
630 1 : value_range curr (TREE_TYPE (name));
631 1 : m->get_vrange (curr, TREE_TYPE (name));
632 : // If there is no change, return false.
633 1 : if (!curr.intersect (r))
634 1 : return false;
635 :
636 0 : if (m->fits_p (curr))
637 0 : m->set_vrange (curr);
638 : else
639 0 : m_tab[v] = m_range_allocator->clone (curr);
640 1 : }
641 : return true;
642 : }
643 :
644 : // Set the range for NAME to R in the ssa cache.
645 :
646 : void
647 0 : ssa_cache::clear_range (tree name)
648 : {
649 0 : unsigned v = SSA_NAME_VERSION (name);
650 0 : if (v >= m_tab.length ())
651 : return;
652 0 : m_tab[v] = NULL;
653 : }
654 :
655 : // Clear the ssa cache.
656 :
657 : void
658 0 : ssa_cache::clear ()
659 : {
660 0 : if (m_tab.address ())
661 0 : memset (m_tab.address(), 0, m_tab.length () * sizeof (vrange *));
662 0 : }
663 :
664 : // Dump the contents of the ssa cache to F.
665 :
666 : void
667 62 : ssa_cache::dump (FILE *f)
668 : {
669 3210 : for (unsigned x = 1; x < num_ssa_names; x++)
670 : {
671 3148 : if (!gimple_range_ssa_p (ssa_name (x)))
672 1268 : continue;
673 1880 : value_range r (TREE_TYPE (ssa_name (x)));
674 : // Dump all non-varying ranges.
675 1880 : if (get_range (r, ssa_name (x)) && !r.varying_p ())
676 : {
677 302 : print_generic_expr (f, ssa_name (x), TDF_NONE);
678 302 : fprintf (f, " : ");
679 302 : r.dump (f);
680 302 : fprintf (f, "\n");
681 : }
682 1880 : }
683 :
684 62 : }
685 :
686 : // Construct an ssa_lazy_cache. If OB is specified, us it, otherwise use
687 : // a local bitmap obstack.
688 :
689 28164615 : ssa_lazy_cache::ssa_lazy_cache (bitmap_obstack *ob)
690 : {
691 28164615 : if (!ob)
692 : {
693 28164606 : bitmap_obstack_initialize (&m_bitmaps);
694 28164606 : m_ob = &m_bitmaps;
695 : }
696 : else
697 9 : m_ob = ob;
698 28164615 : active_p = BITMAP_ALLOC (m_ob);
699 28164615 : }
700 :
701 : // Destruct an sa_lazy_cache. Free the bitmap if it came from a different
702 : // obstack, or release the obstack if it was a local one.
703 :
704 28164606 : ssa_lazy_cache::~ssa_lazy_cache ()
705 : {
706 28164606 : if (m_ob == &m_bitmaps)
707 28164606 : bitmap_obstack_release (&m_bitmaps);
708 : else
709 0 : BITMAP_FREE (active_p);
710 28164606 : }
711 :
712 : // Return true if NAME has an active range in the cache.
713 :
714 : bool
715 305 : ssa_lazy_cache::has_range (tree name) const
716 : {
717 305 : return bitmap_bit_p (active_p, SSA_NAME_VERSION (name));
718 : }
719 :
720 : // Set range of NAME to R in a lazy cache. Return FALSE if it did not already
721 : // have a range.
722 :
723 : bool
724 102660520 : ssa_lazy_cache::set_range (tree name, const vrange &r)
725 : {
726 102660520 : unsigned v = SSA_NAME_VERSION (name);
727 102660520 : if (!bitmap_set_bit (active_p, v))
728 : {
729 : // There is already an entry, simply set it.
730 12611064 : gcc_checking_assert (v < m_tab.length ());
731 12611064 : return ssa_cache::set_range (name, r);
732 : }
733 90049456 : if (v >= m_tab.length ())
734 47579866 : m_tab.safe_grow (num_ssa_names + 1);
735 90049456 : m_tab[v] = m_range_allocator->clone (r);
736 90049456 : return false;
737 : }
738 :
739 : // If NAME has a range, intersect it with R, otherwise set it to R.
740 : // Return TRUE if the range is new or changes.
741 :
742 : bool
743 213 : ssa_lazy_cache::merge_range (tree name, const vrange &r)
744 : {
745 213 : unsigned v = SSA_NAME_VERSION (name);
746 213 : if (!bitmap_set_bit (active_p, v))
747 : {
748 : // There is already an entry, simply merge it.
749 1 : gcc_checking_assert (v < m_tab.length ());
750 1 : return ssa_cache::merge_range (name, r);
751 : }
752 212 : if (v >= m_tab.length ())
753 160 : m_tab.safe_grow (num_ssa_names + 1);
754 212 : m_tab[v] = m_range_allocator->clone (r);
755 212 : return true;
756 : }
757 :
758 : // Merge all elements of CACHE with this cache.
759 : // Any names in CACHE that are not in this one are added.
760 : // Any names in both are merged via merge_range..
761 :
762 : void
763 7 : ssa_lazy_cache::merge (const ssa_lazy_cache &cache)
764 : {
765 7 : unsigned x;
766 7 : bitmap_iterator bi;
767 57 : EXECUTE_IF_SET_IN_BITMAP (cache.active_p, 0, x, bi)
768 : {
769 50 : tree name = ssa_name (x);
770 50 : value_range r(TREE_TYPE (name));
771 50 : cache.get_range (r, name);
772 50 : merge_range (ssa_name (x), r);
773 50 : }
774 7 : }
775 :
776 : // Return TRUE if NAME has a range, and return it in R.
777 :
778 : bool
779 267791787 : ssa_lazy_cache::get_range (vrange &r, tree name) const
780 : {
781 267791787 : if (!bitmap_bit_p (active_p, SSA_NAME_VERSION (name)))
782 : return false;
783 111936636 : return ssa_cache::get_range (r, name);
784 : }
785 :
786 : // Remove NAME from the active range list.
787 :
788 : void
789 51426027 : ssa_lazy_cache::clear_range (tree name)
790 : {
791 51426027 : bitmap_clear_bit (active_p, SSA_NAME_VERSION (name));
792 51426027 : }
793 :
794 : // Remove all ranges from the active range list.
795 :
796 : void
797 34788916 : ssa_lazy_cache::clear ()
798 : {
799 34788916 : bitmap_clear (active_p);
800 34788916 : }
801 :
802 : // --------------------------------------------------------------------------
803 :
804 : // A cache timestamp has two components.
805 : //
806 : // STORED and CALC are maintained separately. STORED is updated only when
807 : // the cached value actually changes, while CALC is updated every time the
808 : // value is recalculated.
809 : //
810 : // This allows stale values to be recalculated without forcing dependent
811 : // values to be recalculated as well. If a recalculation produces the same
812 : // value, only CALC changes and the STORED timestamp remains unchanged,
813 : // indicating that the observable value has not changed.
814 :
815 : struct time_stamp
816 : {
817 : unsigned stored; // Timestamp of last time value was SET.
818 : unsigned calc; // Timestamp when the value was calcuclated last.
819 : };
820 :
821 : // Manage dependency timestamps for SSA names.
822 : //
823 : // Each SSA name records when its value last changed (stored) and when it
824 : // was last recalculated (calc). Dependencies are current if their stored
825 : // timestamps are no newer than the dependent value. Recalculating a value
826 : // without changing it updates only the calc timestamp, avoiding unnecessary
827 : // invalidation of dependent values.
828 : // always_current is managed by setting the calcualted timestamp to 0.
829 :
830 : class temporal_cache
831 : {
832 : public:
833 : temporal_cache ();
834 : ~temporal_cache ();
835 : bool current_p (tree name, tree dep1, tree dep2) const;
836 : void set_timestamp_stored (tree name);
837 : void set_timestamp_calc (tree name);
838 : void set_always_current (tree name);
839 : bool always_current_p (tree name) const;
840 : private:
841 : unsigned temporal_value_stored (unsigned ssa) const;
842 : unsigned temporal_value_calc (unsigned ssa) const;
843 : unsigned m_current_time;
844 : vec <struct time_stamp> m_timestamp;
845 : };
846 :
847 : inline
848 28204919 : temporal_cache::temporal_cache ()
849 : {
850 28204919 : m_current_time = 1;
851 28204919 : m_timestamp.create (0);
852 56409838 : m_timestamp.safe_grow_cleared (num_ssa_names + 1);
853 28204919 : }
854 :
855 : inline
856 28204919 : temporal_cache::~temporal_cache ()
857 : {
858 28204919 : m_timestamp.release ();
859 28204919 : }
860 :
861 : // Return the timestamp value for SSA when it was last stored to
862 : // or 0 if there isn't one.
863 :
864 : inline unsigned
865 152016017 : temporal_cache::temporal_value_stored (unsigned ssa) const
866 : {
867 152016017 : if (ssa >= m_timestamp.length ())
868 : return 0;
869 152016017 : return m_timestamp[ssa].stored;
870 : }
871 :
872 : // Return the timestamp value for SSA when it was last calculated
873 : // or 0 if there isn't one.
874 :
875 : inline unsigned
876 214983096 : temporal_cache::temporal_value_calc (unsigned ssa) const
877 : {
878 214983096 : if (ssa >= m_timestamp.length ())
879 : return 0;
880 214983096 : return m_timestamp[ssa].calc;
881 : }
882 :
883 : // Return TRUE if the timestamp for when NAME was calculated is newer
884 : // than the last time any of its dependents were stored. This indicates
885 : // it dos not need to be calculated again.
886 : // Up to 2 dependencies can be checked.
887 :
888 : bool
889 221300254 : temporal_cache::current_p (tree name, tree dep1, tree dep2) const
890 : {
891 221300254 : if (always_current_p (name))
892 : return true;
893 :
894 : // Any non-registered dependencies will have a value of 0 and thus be older.
895 : // Return true if the last time this was calculated is newer than either
896 : // dependent value.
897 214983096 : unsigned ts = temporal_value_calc (SSA_NAME_VERSION (name));
898 328066960 : if (dep1 && ts < temporal_value_stored (SSA_NAME_VERSION (dep1)))
899 : return false;
900 249903289 : if (dep2 && ts < temporal_value_stored (SSA_NAME_VERSION (dep2)))
901 434390 : return false;
902 :
903 : return true;
904 : }
905 :
906 : // This increments the global timer and sets both timestamps for NAME.
907 :
908 : inline void
909 74916290 : temporal_cache::set_timestamp_stored (tree name)
910 : {
911 74916290 : unsigned v = SSA_NAME_VERSION (name);
912 74916290 : if (v >= m_timestamp.length ())
913 0 : m_timestamp.safe_grow_cleared (num_ssa_names + 20);
914 74916290 : m_timestamp[v].stored = ++m_current_time;
915 74916290 : m_timestamp[v].calc = m_current_time;
916 74916290 : }
917 :
918 : // This increments the global timer and sets the calculated timestamp for NAME.
919 :
920 : inline void
921 120950579 : temporal_cache::set_timestamp_calc (tree name)
922 : {
923 120950579 : unsigned v = SSA_NAME_VERSION (name);
924 120950579 : if (v >= m_timestamp.length ())
925 0 : m_timestamp.safe_grow_cleared (num_ssa_names + 20);
926 120950579 : m_timestamp[v].calc = ++m_current_time;
927 120950579 : }
928 :
929 : // Set the calculated timestamp to 0, marking it as "always up to date".
930 :
931 : inline void
932 133110535 : temporal_cache::set_always_current (tree name)
933 : {
934 133110535 : unsigned v = SSA_NAME_VERSION (name);
935 133110535 : if (v >= m_timestamp.length ())
936 1328 : m_timestamp.safe_grow_cleared (num_ssa_names + 20);
937 : // If stored timestamp hasn't been set, set it now.
938 133110535 : if (m_timestamp[v].stored == 0)
939 128246647 : m_timestamp[v].stored = ++m_current_time;
940 133110535 : m_timestamp[v].calc = 0;
941 133110535 : }
942 :
943 : // Return true if NAME is always current.
944 :
945 : inline bool
946 221300254 : temporal_cache::always_current_p (tree name) const
947 : {
948 221300254 : unsigned v = SSA_NAME_VERSION (name);
949 221300254 : if (v >= m_timestamp.length ())
950 : return false;
951 221300254 : return m_timestamp[v].calc == 0;
952 : }
953 :
954 : // --------------------------------------------------------------------------
955 :
956 : // This class provides an abstraction of a list of blocks to be updated
957 : // by the cache. It is currently a stack but could be changed. It also
958 : // maintains a list of blocks which have failed propagation, and does not
959 : // enter any of those blocks into the list.
960 :
961 : // A vector over the BBs is maintained, and an entry of 0 means it is not in
962 : // a list. Otherwise, the entry is the next block in the list. -1 terminates
963 : // the list. m_head points to the top of the list, -1 if the list is empty.
964 :
965 : class update_list
966 : {
967 : public:
968 : update_list ();
969 : ~update_list ();
970 : void add (basic_block bb);
971 : basic_block pop ();
972 156513472 : inline bool empty_p () { return m_update_head == -1; }
973 5907433 : inline void clear_failures () { bitmap_clear (m_propfail); }
974 3 : inline void propagation_failed (basic_block bb)
975 3 : { bitmap_set_bit (m_propfail, bb->index); }
976 : private:
977 : vec<int> m_update_list;
978 : int m_update_head;
979 : bitmap m_propfail;
980 : bitmap_obstack m_bitmaps;
981 : };
982 :
983 : // Create an update list.
984 :
985 28204919 : update_list::update_list ()
986 : {
987 28204919 : m_update_list.create (0);
988 28204919 : m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun) + 64);
989 28204919 : m_update_head = -1;
990 28204919 : bitmap_obstack_initialize (&m_bitmaps);
991 28204919 : m_propfail = BITMAP_ALLOC (&m_bitmaps);
992 28204919 : }
993 :
994 : // Destroy an update list.
995 :
996 28204919 : update_list::~update_list ()
997 : {
998 28204919 : m_update_list.release ();
999 28204919 : bitmap_obstack_release (&m_bitmaps);
1000 28204919 : }
1001 :
1002 : // Add BB to the list of blocks to update, unless it's already in the list.
1003 :
1004 : void
1005 13369507 : update_list::add (basic_block bb)
1006 : {
1007 13369507 : int i = bb->index;
1008 : // If propagation has failed for BB, or its already in the list, don't
1009 : // add it again.
1010 13369507 : if ((unsigned)i >= m_update_list.length ())
1011 80 : m_update_list.safe_grow_cleared (i + 64);
1012 13369507 : if (!m_update_list[i] && !bitmap_bit_p (m_propfail, i))
1013 : {
1014 12673301 : if (empty_p ())
1015 : {
1016 7264114 : m_update_head = i;
1017 7264114 : m_update_list[i] = -1;
1018 : }
1019 : else
1020 : {
1021 5409187 : gcc_checking_assert (m_update_head > 0);
1022 5409187 : m_update_list[i] = m_update_head;
1023 5409187 : m_update_head = i;
1024 : }
1025 : }
1026 13369507 : }
1027 :
1028 : // Remove a block from the list.
1029 :
1030 : basic_block
1031 12673301 : update_list::pop ()
1032 : {
1033 12673301 : gcc_checking_assert (!empty_p ());
1034 12673301 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, m_update_head);
1035 12673301 : int pop = m_update_head;
1036 12673301 : m_update_head = m_update_list[pop];
1037 12673301 : m_update_list[pop] = 0;
1038 12673301 : return bb;
1039 : }
1040 :
1041 : // --------------------------------------------------------------------------
1042 :
1043 28204919 : ranger_cache::ranger_cache (int not_executable_flag, bool use_imm_uses)
1044 : {
1045 28204919 : m_workback = vNULL;
1046 28204919 : m_temporal = new temporal_cache;
1047 :
1048 : // If DOM info is available, spawn an oracle as well.
1049 28204919 : create_relation_oracle ();
1050 : // Create an infer oracle using this cache as the range query. The cache
1051 : // version acts as a read-only query, and will spawn no additional lookups.
1052 : // It just ues what is already known.
1053 28204919 : create_infer_oracle (this, use_imm_uses);
1054 28204919 : create_gori (not_executable_flag, param_vrp_switch_limit);
1055 :
1056 28204919 : unsigned x, lim = last_basic_block_for_fn (cfun);
1057 : // Calculate outgoing range info upfront. This will fully populate the
1058 : // m_maybe_variant bitmap which will help eliminate processing of names
1059 : // which never have their ranges adjusted.
1060 360529033 : for (x = 0; x < lim ; x++)
1061 : {
1062 332324114 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
1063 332324114 : if (bb)
1064 313325754 : gori_ssa ()->exports (bb);
1065 : }
1066 28204919 : m_update = new update_list ();
1067 28204919 : m_stale = BITMAP_ALLOC (NULL);
1068 28204919 : }
1069 :
1070 28204919 : ranger_cache::~ranger_cache ()
1071 : {
1072 28204919 : BITMAP_FREE (m_stale);
1073 28204919 : delete m_update;
1074 28204919 : destroy_infer_oracle ();
1075 28204919 : destroy_relation_oracle ();
1076 56409838 : delete m_temporal;
1077 28204919 : m_workback.release ();
1078 28204919 : }
1079 :
1080 : // Dump the global caches to file F. if GORI_DUMP is true, dump the
1081 : // gori map as well.
1082 :
1083 : void
1084 46 : ranger_cache::dump (FILE *f)
1085 : {
1086 46 : fprintf (f, "Non-varying global ranges:\n");
1087 46 : fprintf (f, "=========================:\n");
1088 46 : m_globals.dump (f);
1089 46 : fprintf (f, "\n");
1090 46 : }
1091 :
1092 : // Dump the caches for basic block BB to file F.
1093 :
1094 : void
1095 250 : ranger_cache::dump_bb (FILE *f, basic_block bb)
1096 : {
1097 250 : gori_ssa ()->dump (f, bb, false);
1098 250 : m_on_entry.dump (f, bb);
1099 250 : m_relation->dump (f, bb);
1100 250 : }
1101 :
1102 : // Get the global range for NAME, and return in R. Return false if the
1103 : // global range is not set, and return the legacy global value in R.
1104 :
1105 : bool
1106 843451370 : ranger_cache::get_global_range (vrange &r, tree name) const
1107 : {
1108 843451370 : if (m_globals.get_range (r, name))
1109 : return true;
1110 196681224 : gimple_range_global (r, name);
1111 196681224 : return false;
1112 : }
1113 :
1114 : // Mark NAME as stale. The next query of NAME forces a recalculation.
1115 :
1116 : void
1117 4314957 : ranger_cache::mark_stale (tree name)
1118 : {
1119 : // Only mark it as stale if it has been processed. If it has no range
1120 : // it will be calculated at the next request anyway.
1121 4314957 : if (m_globals.has_range (name))
1122 1657320 : bitmap_set_bit (m_stale, SSA_NAME_VERSION (name));
1123 4314957 : }
1124 :
1125 : // Get the global range for NAME, and return in R. Return false if the
1126 : // global range is not set, and R will contain the legacy global value.
1127 : // CURRENT_P is set to true if the value was in cache and not stale.
1128 : // Otherwise, set CURRENT_P to false and mark as it always current.
1129 : // If the global cache did not have a value, initialize it as well.
1130 : // After this call, the global cache will have a value.
1131 :
1132 : bool
1133 349757454 : ranger_cache::get_global_range (vrange &r, tree name, bool ¤t_p)
1134 : {
1135 349757454 : bool had_global = get_global_range (r, name);
1136 :
1137 : // If there was a global value, set current flag, otherwise set a value.
1138 349757454 : current_p = false;
1139 349757454 : if (had_global)
1140 443021614 : current_p = r.singleton_p ()
1141 442811061 : || m_temporal->current_p (name, gori_ssa ()->depend1 (name),
1142 221300254 : gori_ssa ()->depend2 (name));
1143 : else
1144 : {
1145 : // If no global value has been set and value is VARYING, fold the stmt
1146 : // using just global ranges to get a better initial value.
1147 : // After inlining we tend to decide some things are constant, so
1148 : // so not do this evaluation after inlining.
1149 128246647 : if (r.varying_p () && !cfun->after_inlining)
1150 : {
1151 21007312 : gimple *s = SSA_NAME_DEF_STMT (name);
1152 : // Do not process PHIs as SCEV may be in use and it can
1153 : // spawn cyclic lookups.
1154 21007312 : if (gimple_get_lhs (s) == name && !is_a<gphi *> (s))
1155 : {
1156 16452079 : if (!fold_range (r, s, get_global_range_query ()))
1157 0 : gimple_range_global (r, name);
1158 : }
1159 : }
1160 128246647 : m_globals.set_range (name, r);
1161 : }
1162 :
1163 : // If NAME is out of date, clear the bit and mark as not current.
1164 349757454 : if (bitmap_bit_p (m_stale, SSA_NAME_VERSION (name)))
1165 : {
1166 445472 : bitmap_clear_bit (m_stale, SSA_NAME_VERSION (name));
1167 445472 : current_p = false;
1168 : }
1169 :
1170 : // If the existing value was not current, mark it as always current.
1171 349757454 : if (!current_p)
1172 133110535 : m_temporal->set_always_current (name);
1173 349757454 : return had_global;
1174 : }
1175 :
1176 : // Consumers of NAME that have already calculated values should recalculate.
1177 : // Accomplished by updating the timestamp.
1178 :
1179 : void
1180 62399672 : ranger_cache::update_consumers (tree name)
1181 : {
1182 62399672 : m_temporal->set_timestamp_stored (name);
1183 62399672 : }
1184 :
1185 : // Set the global range of NAME to R and give it a timestamp.
1186 :
1187 : void
1188 133467197 : ranger_cache::set_global_range (tree name, const vrange &r, bool changed)
1189 : {
1190 133467197 : if (!changed)
1191 : {
1192 : // If the value did not change, simply update the calculated timestamp.
1193 120950579 : m_temporal->set_timestamp_calc (name);
1194 120950579 : return;
1195 : }
1196 12516618 : if (m_globals.set_range (name, r))
1197 : {
1198 : // If there was already a range set, propagate the new value.
1199 12461331 : basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1200 12461331 : if (!bb)
1201 1534 : bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1202 :
1203 12461331 : if (DEBUG_RANGE_CACHE)
1204 0 : fprintf (dump_file, " GLOBAL :");
1205 :
1206 12461331 : propagate_updated_value (name, bb);
1207 : }
1208 : // Constants no longer need to tracked. Any further refinement has to be
1209 : // undefined. Propagation works better with constants. PR 100512.
1210 : // Pointers which resolve to non-zero also do not need
1211 : // tracking in the cache as they will never change. See PR 98866.
1212 : // Timestamp must always be updated, or dependent calculations may
1213 : // not include this latest value. PR 100774.
1214 :
1215 : // With Points_to info in prange now, it is no longer acceptable to make
1216 : // [1, +INF] invariant, as most points to values will have that range,
1217 : // and then we lose the ability to propagate points to info.
1218 :
1219 12516618 : if (r.singleton_p ())
1220 807427 : gori_ssa ()->set_range_invariant (name);
1221 :
1222 : // update the stored and calucalted timestamp now.
1223 12516618 : m_temporal->set_timestamp_stored (name);
1224 : }
1225 :
1226 : // Provide lookup for the gori-computes class to access the best known range
1227 : // of an ssa_name in any given basic block. Note, this does no additional
1228 : // lookups, just accesses the data that is already known.
1229 :
1230 : // Get the range of NAME when the def occurs in block BB. If BB is NULL
1231 : // get the best global value available.
1232 :
1233 : void
1234 215661111 : ranger_cache::range_of_def (vrange &r, tree name, basic_block bb)
1235 : {
1236 215661111 : gcc_checking_assert (gimple_range_ssa_p (name));
1237 361173496 : gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
1238 :
1239 : // Pick up the best global range available.
1240 215661111 : if (!m_globals.get_range (r, name))
1241 : {
1242 : // If that fails, try to calculate the range using just global values.
1243 30558699 : gimple *s = SSA_NAME_DEF_STMT (name);
1244 30558699 : if (gimple_get_lhs (s) == name)
1245 27141362 : fold_range (r, s, get_global_range_query ());
1246 : else
1247 3417337 : gimple_range_global (r, name);
1248 : }
1249 215661111 : }
1250 :
1251 : // Get the range of NAME as it occurs on entry to block BB. Use MODE for
1252 : // lookups.
1253 :
1254 : void
1255 154805350 : ranger_cache::entry_range (vrange &r, tree name, basic_block bb,
1256 : enum rfd_mode mode)
1257 : {
1258 154805350 : if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1259 : {
1260 0 : gimple_range_global (r, name);
1261 0 : return;
1262 : }
1263 :
1264 : // If NAME is invariant, simply return the defining range.
1265 154805350 : if (!gori ().has_edge_range_p (name))
1266 : {
1267 32589428 : range_of_def (r, name);
1268 32589428 : return;
1269 : }
1270 :
1271 : // Look for the on-entry value of name in BB from the cache.
1272 : // Otherwise pick up the best available global value.
1273 122215922 : if (!m_on_entry.get_bb_range (r, name, bb))
1274 44252979 : if (!range_from_dom (r, name, bb, mode))
1275 37559298 : range_of_def (r, name);
1276 : }
1277 :
1278 : // Get the range of NAME as it occurs on exit from block BB. Use MODE for
1279 : // lookups.
1280 :
1281 : void
1282 107780751 : ranger_cache::exit_range (vrange &r, tree name, basic_block bb,
1283 : enum rfd_mode mode)
1284 : {
1285 107780751 : if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1286 : {
1287 61244 : gimple_range_global (r, name);
1288 61244 : return;
1289 : }
1290 :
1291 107719507 : gimple *s = SSA_NAME_DEF_STMT (name);
1292 107719507 : basic_block def_bb = gimple_bb (s);
1293 107719507 : if (def_bb == bb)
1294 44043618 : range_of_def (r, name, bb);
1295 : else
1296 63675889 : entry_range (r, name, bb, mode);
1297 : }
1298 :
1299 : // Get the range of NAME on edge E using MODE, return the result in R.
1300 : // Always returns a range and true.
1301 :
1302 : bool
1303 97211041 : ranger_cache::edge_range (vrange &r, edge e, tree name, enum rfd_mode mode)
1304 : {
1305 97211041 : exit_range (r, name, e->src, mode);
1306 : // If this is not an abnormal edge, check for inferred ranges on exit.
1307 97211041 : if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
1308 96887794 : infer_oracle ().maybe_adjust_range (r, name, e->src);
1309 97211041 : value_range er (TREE_TYPE (name));
1310 97211041 : if (gori ().edge_range_p (er, e, name, *this))
1311 22831656 : r.intersect (er);
1312 194422082 : return true;
1313 97211041 : }
1314 :
1315 :
1316 :
1317 : // Implement range_of_expr.
1318 :
1319 : bool
1320 234555794 : ranger_cache::range_of_expr (vrange &r, tree name, gimple *stmt)
1321 : {
1322 234555794 : if (!gimple_range_ssa_p (name))
1323 41957566 : get_tree_range (r, name, stmt);
1324 : /* If no context is provided, pick up the global value. */
1325 192598228 : else if (!stmt)
1326 0 : get_global_range (r, name);
1327 : else
1328 : {
1329 192598228 : basic_block bb = gimple_bb (stmt);
1330 192598228 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1331 192598228 : basic_block def_bb = gimple_bb (def_stmt);
1332 :
1333 192598228 : if (bb == def_bb)
1334 101468767 : range_of_def (r, name, bb);
1335 : else
1336 91129461 : entry_range (r, name, bb, RFD_NONE);
1337 : }
1338 234555794 : return true;
1339 : }
1340 :
1341 :
1342 : // Implement range_on_edge. Always return the best available range using
1343 : // the current cache values.
1344 :
1345 : bool
1346 71792013 : ranger_cache::range_on_edge (vrange &r, edge e, tree expr)
1347 : {
1348 71792013 : if (gimple_range_ssa_p (expr))
1349 68826829 : return edge_range (r, e, expr, RFD_NONE);
1350 2965184 : return get_tree_range (r, expr, NULL);
1351 : }
1352 :
1353 : // Return a static range for NAME on entry to basic block BB in R. If
1354 : // calc is true, fill any cache entries required between BB and the
1355 : // def block for NAME. Otherwise, return false if the cache is empty.
1356 :
1357 : bool
1358 397627910 : ranger_cache::block_range (vrange &r, basic_block bb, tree name, bool calc)
1359 : {
1360 397627910 : gcc_checking_assert (gimple_range_ssa_p (name));
1361 :
1362 : // If there are no range calculations anywhere in the IL, global range
1363 : // applies everywhere, so don't bother caching it.
1364 397627910 : if (!gori ().has_edge_range_p (name))
1365 : return false;
1366 :
1367 250828487 : if (calc)
1368 : {
1369 122258171 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1370 122258171 : basic_block def_bb = NULL;
1371 122258171 : if (def_stmt)
1372 122258171 : def_bb = gimple_bb (def_stmt);
1373 122258171 : if (!def_bb)
1374 : {
1375 : // If we get to the entry block, this better be a default def
1376 : // or range_on_entry was called for a block not dominated by
1377 : // the def. But it could be also SSA_NAME defined by a statement
1378 : // not yet in the IL (such as queued edge insertion), in that case
1379 : // just punt.
1380 16690675 : if (!SSA_NAME_IS_DEFAULT_DEF (name))
1381 : return false;
1382 16690674 : def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1383 : }
1384 :
1385 : // There is no range on entry for the definition block.
1386 122258170 : if (def_bb == bb)
1387 : return false;
1388 :
1389 : // Otherwise, go figure out what is known in predecessor blocks.
1390 121894111 : fill_block_cache (name, bb, def_bb);
1391 121894111 : gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1392 : }
1393 250464427 : return m_on_entry.get_bb_range (r, name, bb);
1394 : }
1395 :
1396 : // If there is anything in the propagation update_list, continue
1397 : // processing NAME until the list of blocks is empty.
1398 :
1399 : void
1400 5907433 : ranger_cache::propagate_cache (tree name)
1401 : {
1402 5907433 : basic_block bb;
1403 5907433 : edge_iterator ei;
1404 5907433 : edge e;
1405 5907433 : tree type = TREE_TYPE (name);
1406 5907433 : value_range new_range (type);
1407 5907433 : value_range current_range (type);
1408 5907433 : value_range e_range (type);
1409 :
1410 : // Process each block by seeing if its calculated range on entry is
1411 : // the same as its cached value. If there is a difference, update
1412 : // the cache to reflect the new value, and check to see if any
1413 : // successors have cache entries which may need to be checked for
1414 : // updates.
1415 :
1416 24488167 : while (!m_update->empty_p ())
1417 : {
1418 12673301 : bb = m_update->pop ();
1419 12673301 : gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1420 12673301 : m_on_entry.get_bb_range (current_range, name, bb);
1421 :
1422 12673301 : if (DEBUG_RANGE_CACHE)
1423 : {
1424 0 : fprintf (dump_file, "FWD visiting block %d for ", bb->index);
1425 0 : print_generic_expr (dump_file, name, TDF_SLIM);
1426 0 : fprintf (dump_file, " starting range : ");
1427 0 : current_range.dump (dump_file);
1428 0 : fprintf (dump_file, "\n");
1429 : }
1430 :
1431 : // Calculate the "new" range on entry by unioning the pred edges.
1432 12673301 : new_range.set_undefined ();
1433 27027555 : FOR_EACH_EDGE (e, ei, bb->preds)
1434 : {
1435 17783758 : edge_range (e_range, e, name, RFD_READ_ONLY);
1436 17783758 : if (DEBUG_RANGE_CACHE)
1437 : {
1438 0 : fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
1439 0 : e_range.dump (dump_file);
1440 0 : fprintf (dump_file, "\n");
1441 : }
1442 17783758 : new_range.union_ (e_range);
1443 17783758 : if (new_range.varying_p ())
1444 : break;
1445 : }
1446 :
1447 : // If the range on entry has changed, update it.
1448 12673301 : if (new_range != current_range)
1449 : {
1450 7241269 : bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
1451 : // If the cache couldn't set the value, mark it as failed.
1452 7241269 : if (!ok_p)
1453 3 : m_update->propagation_failed (bb);
1454 7241269 : if (DEBUG_RANGE_CACHE)
1455 : {
1456 0 : if (!ok_p)
1457 : {
1458 0 : fprintf (dump_file, " Cache failure to store value:");
1459 0 : print_generic_expr (dump_file, name, TDF_SLIM);
1460 0 : fprintf (dump_file, " ");
1461 : }
1462 : else
1463 : {
1464 0 : fprintf (dump_file, " Updating range to ");
1465 0 : new_range.dump (dump_file);
1466 : }
1467 0 : fprintf (dump_file, "\n Updating blocks :");
1468 : }
1469 : // Mark each successor that has a range to re-check its range
1470 18617437 : FOR_EACH_EDGE (e, ei, bb->succs)
1471 11376168 : if (m_on_entry.bb_range_p (name, e->dest))
1472 : {
1473 6856226 : if (DEBUG_RANGE_CACHE)
1474 0 : fprintf (dump_file, " bb%d",e->dest->index);
1475 6856226 : m_update->add (e->dest);
1476 : }
1477 7241269 : if (DEBUG_RANGE_CACHE)
1478 0 : fprintf (dump_file, "\n");
1479 : }
1480 : }
1481 5907433 : if (DEBUG_RANGE_CACHE)
1482 : {
1483 0 : fprintf (dump_file, "DONE visiting blocks for ");
1484 0 : print_generic_expr (dump_file, name, TDF_SLIM);
1485 0 : fprintf (dump_file, "\n");
1486 : }
1487 5907433 : m_update->clear_failures ();
1488 5907433 : }
1489 :
1490 : // Check to see if an update to the value for NAME in BB has any effect
1491 : // on values already in the on-entry cache for successor blocks.
1492 : // If it does, update them. Don't visit any blocks which don't have a cache
1493 : // entry.
1494 :
1495 : void
1496 55827536 : ranger_cache::propagate_updated_value (tree name, basic_block bb)
1497 : {
1498 55827536 : edge e;
1499 55827536 : edge_iterator ei;
1500 :
1501 : // The update work list should be empty at this point.
1502 55827536 : gcc_checking_assert (m_update->empty_p ());
1503 55827536 : gcc_checking_assert (bb);
1504 :
1505 55827536 : if (DEBUG_RANGE_CACHE)
1506 : {
1507 0 : fprintf (dump_file, " UPDATE cache for ");
1508 0 : print_generic_expr (dump_file, name, TDF_SLIM);
1509 0 : fprintf (dump_file, " in BB %d : successors : ", bb->index);
1510 : }
1511 162115934 : FOR_EACH_EDGE (e, ei, bb->succs)
1512 : {
1513 : // Only update active cache entries.
1514 106288398 : if (m_on_entry.bb_range_p (name, e->dest))
1515 : {
1516 5044469 : m_update->add (e->dest);
1517 5044469 : if (DEBUG_RANGE_CACHE)
1518 0 : fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
1519 : }
1520 : }
1521 55827536 : if (!m_update->empty_p ())
1522 : {
1523 4976369 : if (DEBUG_RANGE_CACHE)
1524 0 : fprintf (dump_file, "\n");
1525 4976369 : propagate_cache (name);
1526 : }
1527 : else
1528 : {
1529 50851167 : if (DEBUG_RANGE_CACHE)
1530 0 : fprintf (dump_file, " : No updates!\n");
1531 : }
1532 55827536 : }
1533 :
1534 : // Make sure that the range-on-entry cache for NAME is set for block BB.
1535 : // Work back through the CFG to DEF_BB ensuring the range is calculated
1536 : // on the block/edges leading back to that point.
1537 :
1538 : void
1539 121894111 : ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
1540 : {
1541 121894111 : edge_iterator ei;
1542 121894111 : edge e;
1543 121894111 : tree type = TREE_TYPE (name);
1544 121894111 : value_range block_result (type);
1545 121894111 : value_range undefined (type);
1546 :
1547 : // At this point we shouldn't be looking at the def, entry block.
1548 121894111 : gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
1549 121894111 : unsigned start_length = m_workback.length ();
1550 :
1551 : // If the block cache is set, then we've already visited this block.
1552 121894111 : if (m_on_entry.bb_range_p (name, bb))
1553 : return;
1554 :
1555 52985455 : if (DEBUG_RANGE_CACHE)
1556 : {
1557 0 : fprintf (dump_file, "\n");
1558 0 : print_generic_expr (dump_file, name, TDF_SLIM);
1559 0 : fprintf (dump_file, " : ");
1560 : }
1561 :
1562 : // Check if a dominators can supply the range.
1563 52985455 : if (range_from_dom (block_result, name, bb, RFD_FILL))
1564 : {
1565 52054391 : if (DEBUG_RANGE_CACHE)
1566 : {
1567 0 : fprintf (dump_file, "Filled from dominator! : ");
1568 0 : block_result.dump (dump_file);
1569 0 : fprintf (dump_file, "\n");
1570 : }
1571 : // See if any equivalences can refine it.
1572 : // PR 109462, like 108139 below, a one way equivalence introduced
1573 : // by a PHI node can also be through the definition side. Disallow it.
1574 52054391 : tree equiv_name;
1575 52054391 : relation_kind rel;
1576 52054391 : int prec = TYPE_PRECISION (type);
1577 : // If there are too many basic blocks, do not attempt to process
1578 : // equivalencies.
1579 52054391 : if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
1580 : {
1581 408275 : m_on_entry.set_bb_range (name, bb, block_result);
1582 816518 : gcc_checking_assert (m_workback.length () == start_length);
1583 : return;
1584 : }
1585 61352442 : FOR_EACH_PARTIAL_AND_FULL_EQUIV (m_relation, bb, name, equiv_name, rel)
1586 : {
1587 9706326 : basic_block equiv_bb = gimple_bb (SSA_NAME_DEF_STMT (equiv_name));
1588 :
1589 : // Ignore partial equivs that are smaller than this object.
1590 17288938 : if (rel != VREL_EQ && prec > pe_to_bits (rel))
1591 3643969 : continue;
1592 :
1593 : // Check if the equiv has any ranges calculated.
1594 8663633 : if (!gori ().has_edge_range_p (equiv_name))
1595 380066 : continue;
1596 :
1597 : // Check if the equiv definition dominates this block
1598 8283567 : if (equiv_bb == bb ||
1599 8065116 : (equiv_bb && !dominated_by_p (CDI_DOMINATORS, bb, equiv_bb)))
1600 2221210 : continue;
1601 :
1602 6062357 : if (DEBUG_RANGE_CACHE)
1603 : {
1604 0 : if (rel == VREL_EQ)
1605 0 : fprintf (dump_file, "Checking Equivalence (");
1606 : else
1607 0 : fprintf (dump_file, "Checking Partial equiv (");
1608 0 : print_relation (dump_file, rel);
1609 0 : fprintf (dump_file, ") ");
1610 0 : print_generic_expr (dump_file, equiv_name, TDF_SLIM);
1611 0 : fprintf (dump_file, "\n");
1612 : }
1613 6062357 : value_range equiv_range (TREE_TYPE (equiv_name));
1614 6062357 : if (range_from_dom (equiv_range, equiv_name, bb, RFD_READ_ONLY))
1615 : {
1616 6062357 : if (rel != VREL_EQ)
1617 4209032 : range_cast (equiv_range, type);
1618 : else
1619 1853325 : adjust_equivalence_range (equiv_range);
1620 :
1621 6062357 : if (block_result.intersect (equiv_range))
1622 : {
1623 344533 : if (DEBUG_RANGE_CACHE)
1624 : {
1625 0 : if (rel == VREL_EQ)
1626 0 : fprintf (dump_file, "Equivalence update! : ");
1627 : else
1628 0 : fprintf (dump_file, "Partial equiv update! : ");
1629 0 : print_generic_expr (dump_file, equiv_name, TDF_SLIM);
1630 0 : fprintf (dump_file, " has range : ");
1631 0 : equiv_range.dump (dump_file);
1632 0 : fprintf (dump_file, " refining range to :");
1633 0 : block_result.dump (dump_file);
1634 0 : fprintf (dump_file, "\n");
1635 : }
1636 : }
1637 : }
1638 6062357 : }
1639 :
1640 51646116 : m_on_entry.set_bb_range (name, bb, block_result);
1641 100643909 : gcc_checking_assert (m_workback.length () == start_length);
1642 : return;
1643 : }
1644 :
1645 : // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1646 : // m_visited at the end will contain all the blocks that we needed to set
1647 : // the range_on_entry cache for.
1648 931064 : m_workback.safe_push (bb);
1649 931064 : undefined.set_undefined ();
1650 931064 : m_on_entry.set_bb_range (name, bb, undefined);
1651 931064 : gcc_checking_assert (m_update->empty_p ());
1652 :
1653 6182692 : while (m_workback.length () > start_length)
1654 : {
1655 5251628 : basic_block node = m_workback.pop ();
1656 5251628 : if (DEBUG_RANGE_CACHE)
1657 : {
1658 0 : fprintf (dump_file, "BACK visiting block %d for ", node->index);
1659 0 : print_generic_expr (dump_file, name, TDF_SLIM);
1660 0 : fprintf (dump_file, "\n");
1661 : }
1662 :
1663 12557526 : FOR_EACH_EDGE (e, ei, node->preds)
1664 : {
1665 7305898 : basic_block pred = e->src;
1666 7305898 : value_range r (TREE_TYPE (name));
1667 :
1668 7305898 : if (DEBUG_RANGE_CACHE)
1669 0 : fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1670 :
1671 : // If the pred block is the def block add this BB to update list.
1672 7305898 : if (pred == def_bb)
1673 : {
1674 877262 : m_update->add (node);
1675 877262 : continue;
1676 : }
1677 :
1678 : // If the pred is entry but NOT def, then it is used before
1679 : // defined, it'll get set to [] and no need to update it.
1680 6428636 : if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1681 : {
1682 0 : if (DEBUG_RANGE_CACHE)
1683 0 : fprintf (dump_file, "entry: bail.");
1684 0 : continue;
1685 : }
1686 :
1687 : // Regardless of whether we have visited pred or not, if the
1688 : // pred has inferred ranges, revisit this block.
1689 : // Don't search the DOM tree.
1690 6428636 : if (infer_oracle ().has_range_p (pred, name))
1691 : {
1692 13777 : if (DEBUG_RANGE_CACHE)
1693 0 : fprintf (dump_file, "Inferred range: update ");
1694 13777 : m_update->add (node);
1695 : }
1696 :
1697 : // If the pred block already has a range, or if it can contribute
1698 : // something new. Ie, the edge generates a range of some sort.
1699 6428636 : if (m_on_entry.get_bb_range (r, name, pred))
1700 : {
1701 2108072 : if (DEBUG_RANGE_CACHE)
1702 : {
1703 0 : fprintf (dump_file, "has cache, ");
1704 0 : r.dump (dump_file);
1705 0 : fprintf (dump_file, ", ");
1706 : }
1707 2108072 : if (!r.undefined_p () || gori ().has_edge_range_p (name, e))
1708 : {
1709 577773 : m_update->add (node);
1710 577773 : if (DEBUG_RANGE_CACHE)
1711 0 : fprintf (dump_file, "update. ");
1712 : }
1713 2108072 : continue;
1714 : }
1715 :
1716 4320564 : if (DEBUG_RANGE_CACHE)
1717 0 : fprintf (dump_file, "pushing undefined pred block.\n");
1718 : // If the pred hasn't been visited (has no range), add it to
1719 : // the list.
1720 4320564 : gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1721 4320564 : m_on_entry.set_bb_range (name, pred, undefined);
1722 4320564 : m_workback.safe_push (pred);
1723 7305898 : }
1724 : }
1725 :
1726 931064 : if (DEBUG_RANGE_CACHE)
1727 0 : fprintf (dump_file, "\n");
1728 :
1729 : // Now fill in the marked blocks with values.
1730 931064 : propagate_cache (name);
1731 931064 : if (DEBUG_RANGE_CACHE)
1732 0 : fprintf (dump_file, " Propagation update done.\n");
1733 121894111 : }
1734 :
1735 : // Resolve the range of BB if the dominators range is R by calculating incoming
1736 : // edges to this block. All lead back to the dominator so should be cheap.
1737 : // The range for BB is set and returned in R.
1738 :
1739 : void
1740 4490722 : ranger_cache::resolve_dom (vrange &r, tree name, basic_block bb)
1741 : {
1742 4490722 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1743 4490722 : basic_block dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1744 :
1745 : // if it doesn't already have a value, store the incoming range.
1746 4490722 : if (!m_on_entry.bb_range_p (name, dom_bb) && def_bb != dom_bb)
1747 : {
1748 : // If the range can't be store, don't try to accumulate
1749 : // the range in PREV_BB due to excessive recalculations.
1750 1191531 : if (!m_on_entry.set_bb_range (name, dom_bb, r))
1751 0 : return;
1752 : }
1753 : // With the dominator set, we should be able to cheaply query
1754 : // each incoming edge now and accumulate the results.
1755 4490722 : r.set_undefined ();
1756 4490722 : edge e;
1757 4490722 : edge_iterator ei;
1758 4490722 : value_range er (TREE_TYPE (name));
1759 15113743 : FOR_EACH_EDGE (e, ei, bb->preds)
1760 : {
1761 : // If the predecessor is dominated by this block, then there is a back
1762 : // edge, and won't provide anything useful. We'll actually end up with
1763 : // VARYING as we will not resolve this node.
1764 10623021 : if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
1765 22567 : continue;
1766 10600454 : edge_range (er, e, name, RFD_READ_ONLY);
1767 10600454 : r.union_ (er);
1768 : }
1769 : // Set the cache in PREV_BB so it is not calculated again.
1770 4490722 : m_on_entry.set_bb_range (name, bb, r);
1771 4490722 : }
1772 :
1773 : // Get the range of NAME from dominators of BB and return it in R. Search the
1774 : // dominator tree based on MODE.
1775 :
1776 : bool
1777 103300791 : ranger_cache::range_from_dom (vrange &r, tree name, basic_block start_bb,
1778 : enum rfd_mode mode)
1779 : {
1780 103300791 : if (mode == RFD_NONE || !dom_info_available_p (CDI_DOMINATORS))
1781 38490362 : return false;
1782 :
1783 : // Search back to the definition block or entry block.
1784 64810429 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1785 64810429 : if (def_bb == NULL)
1786 8155608 : def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1787 :
1788 64810429 : basic_block bb;
1789 64810429 : basic_block prev_bb = start_bb;
1790 :
1791 : // Track any inferred ranges seen.
1792 64810429 : value_range infer (TREE_TYPE (name));
1793 64810429 : infer.set_varying (TREE_TYPE (name));
1794 :
1795 : // Range on entry to the DEF block should not be queried.
1796 64810429 : gcc_checking_assert (start_bb != def_bb);
1797 64810429 : unsigned start_limit = m_workback.length ();
1798 :
1799 : // Default value is global range.
1800 64810429 : get_global_range (r, name);
1801 :
1802 : // The dominator of EXIT_BLOCK doesn't seem to be set, so at least handle
1803 : // the common single exit cases.
1804 64959110 : if (start_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) && single_pred_p (start_bb))
1805 148432 : bb = single_pred_edge (start_bb)->src;
1806 : else
1807 64661997 : bb = get_immediate_dominator (CDI_DOMINATORS, start_bb);
1808 :
1809 : // Search until a value is found, pushing blocks which may need calculating.
1810 401368919 : for ( ; bb; prev_bb = bb, bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1811 : {
1812 : // Accumulate any block exit inferred ranges.
1813 400531569 : infer_oracle ().maybe_adjust_range (infer, name, bb);
1814 :
1815 : // This block has an outgoing range.
1816 400531569 : if (gori ().has_edge_range_p (name, bb))
1817 46074001 : m_workback.safe_push (prev_bb);
1818 : else
1819 : {
1820 : // Normally join blocks don't carry any new range information on
1821 : // incoming edges. If the first incoming edge to this block does
1822 : // generate a range, calculate the ranges if all incoming edges
1823 : // are also dominated by the dominator. (Avoids backedges which
1824 : // will break the rule of moving only upward in the dominator tree).
1825 : // If the first pred does not generate a range, then we will be
1826 : // using the dominator range anyway, so that's all the check needed.
1827 354457568 : if (EDGE_COUNT (prev_bb->preds) > 1
1828 354457568 : && gori ().has_edge_range_p (name, EDGE_PRED (prev_bb, 0)->src))
1829 : {
1830 744529 : edge e;
1831 744529 : edge_iterator ei;
1832 744529 : bool all_dom = true;
1833 2529185 : FOR_EACH_EDGE (e, ei, prev_bb->preds)
1834 1784656 : if (e->src != bb
1835 1784656 : && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
1836 : {
1837 : all_dom = false;
1838 : break;
1839 : }
1840 744529 : if (all_dom)
1841 744529 : m_workback.safe_push (prev_bb);
1842 : }
1843 : }
1844 :
1845 400531569 : if (def_bb == bb)
1846 : break;
1847 :
1848 360652117 : if (m_on_entry.get_bb_range (r, name, bb))
1849 : break;
1850 : }
1851 :
1852 64810429 : if (DEBUG_RANGE_CACHE)
1853 : {
1854 0 : fprintf (dump_file, "CACHE: BB %d DOM query for ", start_bb->index);
1855 0 : print_generic_expr (dump_file, name, TDF_SLIM);
1856 0 : fprintf (dump_file, ", found ");
1857 0 : r.dump (dump_file);
1858 0 : if (bb)
1859 0 : fprintf (dump_file, " at BB%d\n", bb->index);
1860 : else
1861 0 : fprintf (dump_file, " at function top\n");
1862 : }
1863 :
1864 : // Now process any blocks wit incoming edges that nay have adjustments.
1865 111628959 : while (m_workback.length () > start_limit)
1866 : {
1867 46818530 : value_range er (TREE_TYPE (name));
1868 46818530 : prev_bb = m_workback.pop ();
1869 46818530 : if (!single_pred_p (prev_bb))
1870 : {
1871 : // Non single pred means we need to cache a value in the dominator
1872 : // so we can cheaply calculate incoming edges to this block, and
1873 : // then store the resulting value. If processing mode is not
1874 : // RFD_FILL, then the cache cant be stored to, so don't try.
1875 : // Otherwise this becomes a quadratic timed calculation.
1876 6733514 : if (mode == RFD_FILL)
1877 4490722 : resolve_dom (r, name, prev_bb);
1878 6733514 : continue;
1879 : }
1880 :
1881 40085016 : edge e = single_pred_edge (prev_bb);
1882 40085016 : bb = e->src;
1883 40085016 : if (gori ().edge_range_p (er, e, name, *this))
1884 : {
1885 36257757 : r.intersect (er);
1886 : // If this is a normal edge, apply any inferred ranges.
1887 36257757 : if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
1888 36257757 : infer_oracle ().maybe_adjust_range (r, name, bb);
1889 :
1890 36257757 : if (DEBUG_RANGE_CACHE)
1891 : {
1892 0 : fprintf (dump_file, "CACHE: Adjusted edge range for %d->%d : ",
1893 : bb->index, prev_bb->index);
1894 0 : r.dump (dump_file);
1895 0 : fprintf (dump_file, "\n");
1896 : }
1897 : }
1898 46818530 : }
1899 :
1900 : // Apply non-null if appropriate.
1901 64810429 : if (!has_abnormal_call_or_eh_pred_edge_p (start_bb))
1902 64613830 : r.intersect (infer);
1903 :
1904 64810429 : if (DEBUG_RANGE_CACHE)
1905 : {
1906 0 : fprintf (dump_file, "CACHE: Range for DOM returns : ");
1907 0 : r.dump (dump_file);
1908 0 : fprintf (dump_file, "\n");
1909 : }
1910 64810429 : return true;
1911 64810429 : }
1912 :
1913 : // This routine will register an inferred value in block BB, and possibly
1914 : // update the on-entry cache if appropriate.
1915 :
1916 : void
1917 16899247 : ranger_cache::register_inferred_value (const vrange &ir, tree name,
1918 : basic_block bb)
1919 : {
1920 16899247 : value_range r (TREE_TYPE (name));
1921 16899247 : if (!m_on_entry.get_bb_range (r, name, bb))
1922 10569710 : exit_range (r, name, bb, RFD_READ_ONLY);
1923 16899247 : if (r.intersect (ir))
1924 : {
1925 4967608 : m_on_entry.set_bb_range (name, bb, r);
1926 : // If this range was invariant before, remove invariant.
1927 4967608 : if (!gori ().has_edge_range_p (name))
1928 4136035 : gori_ssa ()->set_range_invariant (name, false);
1929 : }
1930 16899247 : }
1931 :
1932 : // This routine is used during a block walk to adjust any inferred ranges
1933 : // of operands on stmt S.
1934 :
1935 : void
1936 268619587 : ranger_cache::apply_inferred_ranges (gimple *s)
1937 : {
1938 268619587 : bool update = true;
1939 :
1940 268619587 : basic_block bb = gimple_bb (s);
1941 268619587 : gimple_infer_range infer(s, this);
1942 268619587 : if (infer.num () == 0)
1943 : return;
1944 :
1945 : // Do not update the on-entry cache for block ending stmts.
1946 16576730 : if (stmt_ends_bb_p (s))
1947 : {
1948 1182015 : edge_iterator ei;
1949 1182015 : edge e;
1950 2133022 : FOR_EACH_EDGE (e, ei, gimple_bb (s)->succs)
1951 2127209 : if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
1952 : break;
1953 1182015 : if (e == NULL)
1954 5813 : update = false;
1955 : }
1956 :
1957 16576730 : infer_oracle ().add_ranges (s, infer);
1958 16576730 : if (update)
1959 33440056 : for (unsigned x = 0; x < infer.num (); x++)
1960 16869139 : register_inferred_value (infer.range (x), infer.name (x), bb);
1961 : }
|