]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gimple-range-cache.cc
Fix profile update in tree_transform_and_unroll_loop
[thirdparty/gcc.git] / gcc / gimple-range-cache.cc
1 /* Gimple ranger SSA cache implementation.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
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 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 ssa_block_ranges::dump (FILE *f)
62 {
63 basic_block bb;
64 Value_Range r (m_type);
65
66 FOR_EACH_BB_FN (bb, cfun)
67 if (get_bb_range (r, bb))
68 {
69 fprintf (f, "BB%d -> ", bb->index);
70 r.dump (f);
71 fprintf (f, "\n");
72 }
73 }
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 sbr_vector::sbr_vector (tree t, vrange_allocator *allocator, bool zero_p)
102 : ssa_block_ranges (t)
103 {
104 gcc_checking_assert (TYPE_P (t));
105 m_type = t;
106 m_zero_p = zero_p;
107 m_range_allocator = allocator;
108 m_tab_size = last_basic_block_for_fn (cfun) + 1;
109 m_tab = static_cast <vrange_storage **>
110 (allocator->alloc (m_tab_size * sizeof (vrange_storage *)));
111 if (zero_p)
112 memset (m_tab, 0, m_tab_size * sizeof (vrange *));
113
114 // Create the cached type range.
115 m_varying = m_range_allocator->clone_varying (t);
116 m_undefined = m_range_allocator->clone_undefined (t);
117 }
118
119 // Grow the vector when the CFG has increased in size.
120
121 void
122 sbr_vector::grow ()
123 {
124 int curr_bb_size = last_basic_block_for_fn (cfun);
125 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 int inc = MAX ((curr_bb_size - m_tab_size) * 2, 128);
129 inc = MAX (inc, curr_bb_size / 10);
130 int new_size = inc + curr_bb_size;
131
132 // Allocate new memory, copy the old vector and clear the new space.
133 vrange_storage **t = static_cast <vrange_storage **>
134 (m_range_allocator->alloc (new_size * sizeof (vrange_storage *)));
135 memcpy (t, m_tab, m_tab_size * sizeof (vrange_storage *));
136 if (m_zero_p)
137 memset (t + m_tab_size, 0, (new_size - m_tab_size) * sizeof (vrange_storage *));
138
139 m_tab = t;
140 m_tab_size = new_size;
141 }
142
143 // Set the range for block BB to be R.
144
145 bool
146 sbr_vector::set_bb_range (const_basic_block bb, const vrange &r)
147 {
148 vrange_storage *m;
149 if (bb->index >= m_tab_size)
150 grow ();
151 if (r.varying_p ())
152 m = m_varying;
153 else if (r.undefined_p ())
154 m = m_undefined;
155 else
156 m = m_range_allocator->clone (r);
157 m_tab[bb->index] = m;
158 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 sbr_vector::get_bb_range (vrange &r, const_basic_block bb)
166 {
167 if (bb->index >= m_tab_size)
168 return false;
169 vrange_storage *m = m_tab[bb->index];
170 if (m)
171 {
172 m->get_vrange (r, m_type);
173 return true;
174 }
175 return false;
176 }
177
178 // Return true if a range is present.
179
180 bool
181 sbr_vector::bb_range_p (const_basic_block bb)
182 {
183 if (bb->index < m_tab_size)
184 return m_tab[bb->index] != NULL;
185 return false;
186 }
187
188 // Like an sbr_vector, except it uses a bitmap to manage whetehr vale 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 sbr_lazy_vector::sbr_lazy_vector (tree t, vrange_allocator *allocator,
204 bitmap_obstack *bm)
205 : sbr_vector (t, allocator, false)
206 {
207 m_has_value = BITMAP_ALLOC (bm);
208 }
209
210 bool
211 sbr_lazy_vector::set_bb_range (const_basic_block bb, const vrange &r)
212 {
213 sbr_vector::set_bb_range (bb, r);
214 bitmap_set_bit (m_has_value, bb->index);
215 return true;
216 }
217
218 bool
219 sbr_lazy_vector::get_bb_range (vrange &r, const_basic_block bb)
220 {
221 if (bitmap_bit_p (m_has_value, bb->index))
222 return sbr_vector::get_bb_range (r, bb);
223 return false;
224 }
225
226 bool
227 sbr_lazy_vector::bb_range_p (const_basic_block bb)
228 {
229 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 sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, vrange_allocator *allocator,
264 bitmap_obstack *bm)
265 : ssa_block_ranges (t)
266 {
267 gcc_checking_assert (TYPE_P (t));
268 m_type = t;
269 bitmap_initialize (&bitvec, bm);
270 bitmap_tree_view (&bitvec);
271 m_range_allocator = allocator;
272 // Pre-cache varying.
273 m_range[0] = m_range_allocator->clone_varying (t);
274 // Pre-cache zero and non-zero values for pointers.
275 if (POINTER_TYPE_P (t))
276 {
277 int_range<2> nonzero;
278 nonzero.set_nonzero (t);
279 m_range[1] = m_range_allocator->clone (nonzero);
280 int_range<2> zero;
281 zero.set_zero (t);
282 m_range[2] = m_range_allocator->clone (zero);
283 }
284 else
285 m_range[1] = m_range[2] = NULL;
286 // Clear SBR_NUM entries.
287 for (int x = 3; x < SBR_NUM; x++)
288 m_range[x] = 0;
289 }
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 sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
297 {
298 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 sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
306 {
307 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 sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const vrange &r)
314 {
315 if (r.undefined_p ())
316 {
317 bitmap_set_quad (&bitvec, bb->index, SBR_UNDEF);
318 return true;
319 }
320
321 // Loop thru the values to see if R is already present.
322 for (int x = 0; x < SBR_NUM; x++)
323 if (!m_range[x] || m_range[x]->equal_p (r))
324 {
325 if (!m_range[x])
326 m_range[x] = m_range_allocator->clone (r);
327 bitmap_set_quad (&bitvec, bb->index, x + 1);
328 return true;
329 }
330 // All values are taken, default to VARYING.
331 bitmap_set_quad (&bitvec, bb->index, SBR_VARYING);
332 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 sbr_sparse_bitmap::get_bb_range (vrange &r, const_basic_block bb)
340 {
341 int value = bitmap_get_quad (&bitvec, bb->index);
342
343 if (!value)
344 return false;
345
346 gcc_checking_assert (value <= SBR_UNDEF);
347 if (value == SBR_UNDEF)
348 r.set_undefined ();
349 else
350 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 sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
358 {
359 return (bitmap_get_quad (&bitvec, bb->index) != 0);
360 }
361
362 // -------------------------------------------------------------------------
363
364 // Initialize the block cache.
365
366 block_range_cache::block_range_cache ()
367 {
368 bitmap_obstack_initialize (&m_bitmaps);
369 m_ssa_ranges.create (0);
370 m_ssa_ranges.safe_grow_cleared (num_ssa_names);
371 m_range_allocator = new vrange_allocator;
372 }
373
374 // Remove any m_block_caches which have been created.
375
376 block_range_cache::~block_range_cache ()
377 {
378 delete m_range_allocator;
379 // Release the vector itself.
380 m_ssa_ranges.release ();
381 bitmap_obstack_release (&m_bitmaps);
382 }
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 block_range_cache::set_bb_range (tree name, const_basic_block bb,
389 const vrange &r)
390 {
391 unsigned v = SSA_NAME_VERSION (name);
392 if (v >= m_ssa_ranges.length ())
393 m_ssa_ranges.safe_grow_cleared (num_ssa_names + 1);
394
395 if (!m_ssa_ranges[v])
396 {
397 // Use sparse bitmap representation if there are too many basic blocks.
398 if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
399 {
400 void *r = m_range_allocator->alloc (sizeof (sbr_sparse_bitmap));
401 m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
402 m_range_allocator,
403 &m_bitmaps);
404 }
405 else if (last_basic_block_for_fn (cfun) < param_vrp_vector_threshold)
406 {
407 // For small CFGs use the basic vector implemntation.
408 void *r = m_range_allocator->alloc (sizeof (sbr_vector));
409 m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
410 m_range_allocator);
411 }
412 else
413 {
414 // Otherwise use the sparse vector implementation.
415 void *r = m_range_allocator->alloc (sizeof (sbr_lazy_vector));
416 m_ssa_ranges[v] = new (r) sbr_lazy_vector (TREE_TYPE (name),
417 m_range_allocator,
418 &m_bitmaps);
419 }
420 }
421 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 block_range_cache::query_block_ranges (tree name)
430 {
431 unsigned v = SSA_NAME_VERSION (name);
432 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 block_range_cache::get_bb_range (vrange &r, tree name, const_basic_block bb)
444 {
445 ssa_block_ranges *ptr = query_block_ranges (name);
446 if (ptr)
447 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 block_range_cache::bb_range_p (tree name, const_basic_block bb)
455 {
456 ssa_block_ranges *ptr = query_block_ranges (name);
457 if (ptr)
458 return ptr->bb_range_p (bb);
459 return false;
460 }
461
462 // Print all known block caches to file F.
463
464 void
465 block_range_cache::dump (FILE *f)
466 {
467 unsigned x;
468 for (x = 0; x < m_ssa_ranges.length (); ++x)
469 {
470 if (m_ssa_ranges[x])
471 {
472 fprintf (f, " Ranges for ");
473 print_generic_expr (f, ssa_name (x), TDF_NONE);
474 fprintf (f, ":\n");
475 m_ssa_ranges[x]->dump (f);
476 fprintf (f, "\n");
477 }
478 }
479 }
480
481 // Print all known ranges on entry to block BB to file F.
482
483 void
484 block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
485 {
486 unsigned x;
487 bool summarize_varying = false;
488 for (x = 1; x < m_ssa_ranges.length (); ++x)
489 {
490 if (!gimple_range_ssa_p (ssa_name (x)))
491 continue;
492
493 Value_Range r (TREE_TYPE (ssa_name (x)));
494 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
495 {
496 if (!print_varying && r.varying_p ())
497 {
498 summarize_varying = true;
499 continue;
500 }
501 print_generic_expr (f, ssa_name (x), TDF_NONE);
502 fprintf (f, "\t");
503 r.dump(f);
504 fprintf (f, "\n");
505 }
506 }
507 // If there were any varying entries, lump them all together.
508 if (summarize_varying)
509 {
510 fprintf (f, "VARYING_P on entry : ");
511 for (x = 1; x < num_ssa_names; ++x)
512 {
513 if (!gimple_range_ssa_p (ssa_name (x)))
514 continue;
515
516 Value_Range r (TREE_TYPE (ssa_name (x)));
517 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
518 {
519 if (r.varying_p ())
520 {
521 print_generic_expr (f, ssa_name (x), TDF_NONE);
522 fprintf (f, " ");
523 }
524 }
525 }
526 fprintf (f, "\n");
527 }
528 }
529
530 // -------------------------------------------------------------------------
531
532 // Initialize an ssa cache.
533
534 ssa_cache::ssa_cache ()
535 {
536 m_tab.create (0);
537 m_range_allocator = new vrange_allocator;
538 }
539
540 // Deconstruct an ssa cache.
541
542 ssa_cache::~ssa_cache ()
543 {
544 m_tab.release ();
545 delete m_range_allocator;
546 }
547
548 // Enable a query to evaluate staements/ramnges based on picking up ranges
549 // from just an ssa-cache.
550
551 bool
552 ssa_cache::range_of_expr (vrange &r, tree expr, gimple *stmt)
553 {
554 if (!gimple_range_ssa_p (expr))
555 return get_tree_range (r, expr, stmt);
556
557 if (!get_range (r, expr))
558 gimple_range_global (r, expr, cfun);
559 return true;
560 }
561
562 // Return TRUE if the global range of NAME has a cache entry.
563
564 bool
565 ssa_cache::has_range (tree name) const
566 {
567 unsigned v = SSA_NAME_VERSION (name);
568 if (v >= m_tab.length ())
569 return false;
570 return m_tab[v] != NULL;
571 }
572
573 // Retrieve the global range of NAME from cache memory if it exists.
574 // Return the value in R.
575
576 bool
577 ssa_cache::get_range (vrange &r, tree name) const
578 {
579 unsigned v = SSA_NAME_VERSION (name);
580 if (v >= m_tab.length ())
581 return false;
582
583 vrange_storage *stow = m_tab[v];
584 if (!stow)
585 return false;
586 stow->get_vrange (r, TREE_TYPE (name));
587 return true;
588 }
589
590 // Set the range for NAME to R in the ssa cache.
591 // Return TRUE if there was already a range set, otherwise false.
592
593 bool
594 ssa_cache::set_range (tree name, const vrange &r)
595 {
596 unsigned v = SSA_NAME_VERSION (name);
597 if (v >= m_tab.length ())
598 m_tab.safe_grow_cleared (num_ssa_names + 1);
599
600 vrange_storage *m = m_tab[v];
601 if (m && m->fits_p (r))
602 m->set_vrange (r);
603 else
604 m_tab[v] = m_range_allocator->clone (r);
605 return m != NULL;
606 }
607
608 // Set the range for NAME to R in the ssa cache.
609
610 void
611 ssa_cache::clear_range (tree name)
612 {
613 unsigned v = SSA_NAME_VERSION (name);
614 if (v >= m_tab.length ())
615 return;
616 m_tab[v] = NULL;
617 }
618
619 // Clear the ssa cache.
620
621 void
622 ssa_cache::clear ()
623 {
624 if (m_tab.address ())
625 memset (m_tab.address(), 0, m_tab.length () * sizeof (vrange *));
626 }
627
628 // Dump the contents of the ssa cache to F.
629
630 void
631 ssa_cache::dump (FILE *f)
632 {
633 /* Cleared after the table header has been printed. */
634 bool print_header = true;
635 for (unsigned x = 1; x < num_ssa_names; x++)
636 {
637 if (!gimple_range_ssa_p (ssa_name (x)))
638 continue;
639 Value_Range r (TREE_TYPE (ssa_name (x)));
640 // Invoke dump_range_query which is a private virtual version of
641 // get_range. This avoids performance impacts on general queries,
642 // but allows sharing of the dump routine.
643 if (get_range (r, ssa_name (x)) && !r.varying_p ())
644 {
645 if (print_header)
646 {
647 /* Print the header only when there's something else
648 to print below. */
649 fprintf (f, "Non-varying global ranges:\n");
650 fprintf (f, "=========================:\n");
651 print_header = false;
652 }
653
654 print_generic_expr (f, ssa_name (x), TDF_NONE);
655 fprintf (f, " : ");
656 r.dump (f);
657 fprintf (f, "\n");
658 }
659 }
660
661 if (!print_header)
662 fputc ('\n', f);
663 }
664
665 // Return true if NAME has an active range in the cache.
666
667 bool
668 ssa_lazy_cache::has_range (tree name) const
669 {
670 return bitmap_bit_p (active_p, SSA_NAME_VERSION (name));
671 }
672
673 // Set range of NAME to R in a lazy cache. Return FALSE if it did not already
674 // have a range.
675
676 bool
677 ssa_lazy_cache::set_range (tree name, const vrange &r)
678 {
679 unsigned v = SSA_NAME_VERSION (name);
680 if (!bitmap_set_bit (active_p, v))
681 {
682 // There is already an entry, simply set it.
683 gcc_checking_assert (v < m_tab.length ());
684 return ssa_cache::set_range (name, r);
685 }
686 if (v >= m_tab.length ())
687 m_tab.safe_grow (num_ssa_names + 1);
688 m_tab[v] = m_range_allocator->clone (r);
689 return false;
690 }
691
692 // Return TRUE if NAME has a range, and return it in R.
693
694 bool
695 ssa_lazy_cache::get_range (vrange &r, tree name) const
696 {
697 if (!bitmap_bit_p (active_p, SSA_NAME_VERSION (name)))
698 return false;
699 return ssa_cache::get_range (r, name);
700 }
701
702 // Remove NAME from the active range list.
703
704 void
705 ssa_lazy_cache::clear_range (tree name)
706 {
707 bitmap_clear_bit (active_p, SSA_NAME_VERSION (name));
708 }
709
710 // Remove all ranges from the active range list.
711
712 void
713 ssa_lazy_cache::clear ()
714 {
715 bitmap_clear (active_p);
716 }
717
718 // --------------------------------------------------------------------------
719
720
721 // This class will manage the timestamps for each ssa_name.
722 // When a value is calculated, the timestamp is set to the current time.
723 // Current time is then incremented. Any dependencies will already have
724 // been calculated, and will thus have older timestamps.
725 // If one of those values is ever calculated again, it will get a newer
726 // timestamp, and the "current_p" check will fail.
727
728 class temporal_cache
729 {
730 public:
731 temporal_cache ();
732 ~temporal_cache ();
733 bool current_p (tree name, tree dep1, tree dep2) const;
734 void set_timestamp (tree name);
735 void set_always_current (tree name, bool value);
736 bool always_current_p (tree name) const;
737 private:
738 int temporal_value (unsigned ssa) const;
739 int m_current_time;
740 vec <int> m_timestamp;
741 };
742
743 inline
744 temporal_cache::temporal_cache ()
745 {
746 m_current_time = 1;
747 m_timestamp.create (0);
748 m_timestamp.safe_grow_cleared (num_ssa_names);
749 }
750
751 inline
752 temporal_cache::~temporal_cache ()
753 {
754 m_timestamp.release ();
755 }
756
757 // Return the timestamp value for SSA, or 0 if there isn't one.
758
759 inline int
760 temporal_cache::temporal_value (unsigned ssa) const
761 {
762 if (ssa >= m_timestamp.length ())
763 return 0;
764 return abs (m_timestamp[ssa]);
765 }
766
767 // Return TRUE if the timestamp for NAME is newer than any of its dependents.
768 // Up to 2 dependencies can be checked.
769
770 bool
771 temporal_cache::current_p (tree name, tree dep1, tree dep2) const
772 {
773 if (always_current_p (name))
774 return true;
775
776 // Any non-registered dependencies will have a value of 0 and thus be older.
777 // Return true if time is newer than either dependent.
778 int ts = temporal_value (SSA_NAME_VERSION (name));
779 if (dep1 && ts < temporal_value (SSA_NAME_VERSION (dep1)))
780 return false;
781 if (dep2 && ts < temporal_value (SSA_NAME_VERSION (dep2)))
782 return false;
783
784 return true;
785 }
786
787 // This increments the global timer and sets the timestamp for NAME.
788
789 inline void
790 temporal_cache::set_timestamp (tree name)
791 {
792 unsigned v = SSA_NAME_VERSION (name);
793 if (v >= m_timestamp.length ())
794 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
795 m_timestamp[v] = ++m_current_time;
796 }
797
798 // Set the timestamp to 0, marking it as "always up to date".
799
800 inline void
801 temporal_cache::set_always_current (tree name, bool value)
802 {
803 unsigned v = SSA_NAME_VERSION (name);
804 if (v >= m_timestamp.length ())
805 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
806
807 int ts = abs (m_timestamp[v]);
808 // If this does not have a timestamp, create one.
809 if (ts == 0)
810 ts = ++m_current_time;
811 m_timestamp[v] = value ? -ts : ts;
812 }
813
814 // Return true if NAME is always current.
815
816 inline bool
817 temporal_cache::always_current_p (tree name) const
818 {
819 unsigned v = SSA_NAME_VERSION (name);
820 if (v >= m_timestamp.length ())
821 return false;
822 return m_timestamp[v] <= 0;
823 }
824
825 // --------------------------------------------------------------------------
826
827 // This class provides an abstraction of a list of blocks to be updated
828 // by the cache. It is currently a stack but could be changed. It also
829 // maintains a list of blocks which have failed propagation, and does not
830 // enter any of those blocks into the list.
831
832 // A vector over the BBs is maintained, and an entry of 0 means it is not in
833 // a list. Otherwise, the entry is the next block in the list. -1 terminates
834 // the list. m_head points to the top of the list, -1 if the list is empty.
835
836 class update_list
837 {
838 public:
839 update_list ();
840 ~update_list ();
841 void add (basic_block bb);
842 basic_block pop ();
843 inline bool empty_p () { return m_update_head == -1; }
844 inline void clear_failures () { bitmap_clear (m_propfail); }
845 inline void propagation_failed (basic_block bb)
846 { bitmap_set_bit (m_propfail, bb->index); }
847 private:
848 vec<int> m_update_list;
849 int m_update_head;
850 bitmap m_propfail;
851 };
852
853 // Create an update list.
854
855 update_list::update_list ()
856 {
857 m_update_list.create (0);
858 m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun) + 64);
859 m_update_head = -1;
860 m_propfail = BITMAP_ALLOC (NULL);
861 }
862
863 // Destroy an update list.
864
865 update_list::~update_list ()
866 {
867 m_update_list.release ();
868 BITMAP_FREE (m_propfail);
869 }
870
871 // Add BB to the list of blocks to update, unless it's already in the list.
872
873 void
874 update_list::add (basic_block bb)
875 {
876 int i = bb->index;
877 // If propagation has failed for BB, or its already in the list, don't
878 // add it again.
879 if ((unsigned)i >= m_update_list.length ())
880 m_update_list.safe_grow_cleared (i + 64);
881 if (!m_update_list[i] && !bitmap_bit_p (m_propfail, i))
882 {
883 if (empty_p ())
884 {
885 m_update_head = i;
886 m_update_list[i] = -1;
887 }
888 else
889 {
890 gcc_checking_assert (m_update_head > 0);
891 m_update_list[i] = m_update_head;
892 m_update_head = i;
893 }
894 }
895 }
896
897 // Remove a block from the list.
898
899 basic_block
900 update_list::pop ()
901 {
902 gcc_checking_assert (!empty_p ());
903 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, m_update_head);
904 int pop = m_update_head;
905 m_update_head = m_update_list[pop];
906 m_update_list[pop] = 0;
907 return bb;
908 }
909
910 // --------------------------------------------------------------------------
911
912 ranger_cache::ranger_cache (int not_executable_flag, bool use_imm_uses)
913 : m_gori (not_executable_flag),
914 m_exit (use_imm_uses)
915 {
916 m_workback.create (0);
917 m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
918 m_workback.truncate (0);
919 m_temporal = new temporal_cache;
920 // If DOM info is available, spawn an oracle as well.
921 if (dom_info_available_p (CDI_DOMINATORS))
922 m_oracle = new dom_oracle ();
923 else
924 m_oracle = NULL;
925
926 unsigned x, lim = last_basic_block_for_fn (cfun);
927 // Calculate outgoing range info upfront. This will fully populate the
928 // m_maybe_variant bitmap which will help eliminate processing of names
929 // which never have their ranges adjusted.
930 for (x = 0; x < lim ; x++)
931 {
932 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
933 if (bb)
934 m_gori.exports (bb);
935 }
936 m_update = new update_list ();
937 }
938
939 ranger_cache::~ranger_cache ()
940 {
941 delete m_update;
942 if (m_oracle)
943 delete m_oracle;
944 delete m_temporal;
945 m_workback.release ();
946 }
947
948 // Dump the global caches to file F. if GORI_DUMP is true, dump the
949 // gori map as well.
950
951 void
952 ranger_cache::dump (FILE *f)
953 {
954 m_globals.dump (f);
955 fprintf (f, "\n");
956 }
957
958 // Dump the caches for basic block BB to file F.
959
960 void
961 ranger_cache::dump_bb (FILE *f, basic_block bb)
962 {
963 m_gori.gori_map::dump (f, bb, false);
964 m_on_entry.dump (f, bb);
965 if (m_oracle)
966 m_oracle->dump (f, bb);
967 }
968
969 // Get the global range for NAME, and return in R. Return false if the
970 // global range is not set, and return the legacy global value in R.
971
972 bool
973 ranger_cache::get_global_range (vrange &r, tree name) const
974 {
975 if (m_globals.get_range (r, name))
976 return true;
977 gimple_range_global (r, name);
978 return false;
979 }
980
981 // Get the global range for NAME, and return in R. Return false if the
982 // global range is not set, and R will contain the legacy global value.
983 // CURRENT_P is set to true if the value was in cache and not stale.
984 // Otherwise, set CURRENT_P to false and mark as it always current.
985 // If the global cache did not have a value, initialize it as well.
986 // After this call, the global cache will have a value.
987
988 bool
989 ranger_cache::get_global_range (vrange &r, tree name, bool &current_p)
990 {
991 bool had_global = get_global_range (r, name);
992
993 // If there was a global value, set current flag, otherwise set a value.
994 current_p = false;
995 if (had_global)
996 current_p = r.singleton_p ()
997 || m_temporal->current_p (name, m_gori.depend1 (name),
998 m_gori.depend2 (name));
999 else
1000 {
1001 // If no global value has been set and value is VARYING, fold the stmt
1002 // using just global ranges to get a better initial value.
1003 // After inlining we tend to decide some things are constant, so
1004 // so not do this evaluation after inlining.
1005 if (r.varying_p () && !cfun->after_inlining)
1006 {
1007 gimple *s = SSA_NAME_DEF_STMT (name);
1008 if (gimple_get_lhs (s) == name)
1009 {
1010 if (!fold_range (r, s, get_global_range_query ()))
1011 gimple_range_global (r, name);
1012 }
1013 }
1014 m_globals.set_range (name, r);
1015 }
1016
1017 // If the existing value was not current, mark it as always current.
1018 if (!current_p)
1019 m_temporal->set_always_current (name, true);
1020 return had_global;
1021 }
1022
1023 // Set the global range of NAME to R and give it a timestamp.
1024
1025 void
1026 ranger_cache::set_global_range (tree name, const vrange &r, bool changed)
1027 {
1028 // Setting a range always clears the always_current flag.
1029 m_temporal->set_always_current (name, false);
1030 if (!changed)
1031 {
1032 // If there are dependencies, make sure this is not out of date.
1033 if (!m_temporal->current_p (name, m_gori.depend1 (name),
1034 m_gori.depend2 (name)))
1035 m_temporal->set_timestamp (name);
1036 return;
1037 }
1038 if (m_globals.set_range (name, r))
1039 {
1040 // If there was already a range set, propagate the new value.
1041 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1042 if (!bb)
1043 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1044
1045 if (DEBUG_RANGE_CACHE)
1046 fprintf (dump_file, " GLOBAL :");
1047
1048 propagate_updated_value (name, bb);
1049 }
1050 // Constants no longer need to tracked. Any further refinement has to be
1051 // undefined. Propagation works better with constants. PR 100512.
1052 // Pointers which resolve to non-zero also do not need
1053 // tracking in the cache as they will never change. See PR 98866.
1054 // Timestamp must always be updated, or dependent calculations may
1055 // not include this latest value. PR 100774.
1056
1057 if (r.singleton_p ()
1058 || (POINTER_TYPE_P (TREE_TYPE (name)) && r.nonzero_p ()))
1059 m_gori.set_range_invariant (name);
1060 m_temporal->set_timestamp (name);
1061 }
1062
1063 // Provide lookup for the gori-computes class to access the best known range
1064 // of an ssa_name in any given basic block. Note, this does no additional
1065 // lookups, just accesses the data that is already known.
1066
1067 // Get the range of NAME when the def occurs in block BB. If BB is NULL
1068 // get the best global value available.
1069
1070 void
1071 ranger_cache::range_of_def (vrange &r, tree name, basic_block bb)
1072 {
1073 gcc_checking_assert (gimple_range_ssa_p (name));
1074 gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
1075
1076 // Pick up the best global range available.
1077 if (!m_globals.get_range (r, name))
1078 {
1079 // If that fails, try to calculate the range using just global values.
1080 gimple *s = SSA_NAME_DEF_STMT (name);
1081 if (gimple_get_lhs (s) == name)
1082 fold_range (r, s, get_global_range_query ());
1083 else
1084 gimple_range_global (r, name);
1085 }
1086 }
1087
1088 // Get the range of NAME as it occurs on entry to block BB. Use MODE for
1089 // lookups.
1090
1091 void
1092 ranger_cache::entry_range (vrange &r, tree name, basic_block bb,
1093 enum rfd_mode mode)
1094 {
1095 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1096 {
1097 gimple_range_global (r, name);
1098 return;
1099 }
1100
1101 // Look for the on-entry value of name in BB from the cache.
1102 // Otherwise pick up the best available global value.
1103 if (!m_on_entry.get_bb_range (r, name, bb))
1104 if (!range_from_dom (r, name, bb, mode))
1105 range_of_def (r, name);
1106 }
1107
1108 // Get the range of NAME as it occurs on exit from block BB. Use MODE for
1109 // lookups.
1110
1111 void
1112 ranger_cache::exit_range (vrange &r, tree name, basic_block bb,
1113 enum rfd_mode mode)
1114 {
1115 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1116 {
1117 gimple_range_global (r, name);
1118 return;
1119 }
1120
1121 gimple *s = SSA_NAME_DEF_STMT (name);
1122 basic_block def_bb = gimple_bb (s);
1123 if (def_bb == bb)
1124 range_of_def (r, name, bb);
1125 else
1126 entry_range (r, name, bb, mode);
1127 }
1128
1129 // Get the range of NAME on edge E using MODE, return the result in R.
1130 // Always returns a range and true.
1131
1132 bool
1133 ranger_cache::edge_range (vrange &r, edge e, tree name, enum rfd_mode mode)
1134 {
1135 exit_range (r, name, e->src, mode);
1136 // If this is not an abnormal edge, check for inferred ranges on exit.
1137 if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
1138 m_exit.maybe_adjust_range (r, name, e->src);
1139 Value_Range er (TREE_TYPE (name));
1140 if (m_gori.outgoing_edge_range_p (er, e, name, *this))
1141 r.intersect (er);
1142 return true;
1143 }
1144
1145
1146
1147 // Implement range_of_expr.
1148
1149 bool
1150 ranger_cache::range_of_expr (vrange &r, tree name, gimple *stmt)
1151 {
1152 if (!gimple_range_ssa_p (name))
1153 {
1154 get_tree_range (r, name, stmt);
1155 return true;
1156 }
1157
1158 basic_block bb = gimple_bb (stmt);
1159 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1160 basic_block def_bb = gimple_bb (def_stmt);
1161
1162 if (bb == def_bb)
1163 range_of_def (r, name, bb);
1164 else
1165 entry_range (r, name, bb, RFD_NONE);
1166 return true;
1167 }
1168
1169
1170 // Implement range_on_edge. Always return the best available range using
1171 // the current cache values.
1172
1173 bool
1174 ranger_cache::range_on_edge (vrange &r, edge e, tree expr)
1175 {
1176 if (gimple_range_ssa_p (expr))
1177 return edge_range (r, e, expr, RFD_NONE);
1178 return get_tree_range (r, expr, NULL);
1179 }
1180
1181 // Return a static range for NAME on entry to basic block BB in R. If
1182 // calc is true, fill any cache entries required between BB and the
1183 // def block for NAME. Otherwise, return false if the cache is empty.
1184
1185 bool
1186 ranger_cache::block_range (vrange &r, basic_block bb, tree name, bool calc)
1187 {
1188 gcc_checking_assert (gimple_range_ssa_p (name));
1189
1190 // If there are no range calculations anywhere in the IL, global range
1191 // applies everywhere, so don't bother caching it.
1192 if (!m_gori.has_edge_range_p (name))
1193 return false;
1194
1195 if (calc)
1196 {
1197 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1198 basic_block def_bb = NULL;
1199 if (def_stmt)
1200 def_bb = gimple_bb (def_stmt);;
1201 if (!def_bb)
1202 {
1203 // If we get to the entry block, this better be a default def
1204 // or range_on_entry was called for a block not dominated by
1205 // the def.
1206 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
1207 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1208 }
1209
1210 // There is no range on entry for the definition block.
1211 if (def_bb == bb)
1212 return false;
1213
1214 // Otherwise, go figure out what is known in predecessor blocks.
1215 fill_block_cache (name, bb, def_bb);
1216 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1217 }
1218 return m_on_entry.get_bb_range (r, name, bb);
1219 }
1220
1221 // If there is anything in the propagation update_list, continue
1222 // processing NAME until the list of blocks is empty.
1223
1224 void
1225 ranger_cache::propagate_cache (tree name)
1226 {
1227 basic_block bb;
1228 edge_iterator ei;
1229 edge e;
1230 tree type = TREE_TYPE (name);
1231 Value_Range new_range (type);
1232 Value_Range current_range (type);
1233 Value_Range e_range (type);
1234
1235 // Process each block by seeing if its calculated range on entry is
1236 // the same as its cached value. If there is a difference, update
1237 // the cache to reflect the new value, and check to see if any
1238 // successors have cache entries which may need to be checked for
1239 // updates.
1240
1241 while (!m_update->empty_p ())
1242 {
1243 bb = m_update->pop ();
1244 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
1245 m_on_entry.get_bb_range (current_range, name, bb);
1246
1247 if (DEBUG_RANGE_CACHE)
1248 {
1249 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
1250 print_generic_expr (dump_file, name, TDF_SLIM);
1251 fprintf (dump_file, " starting range : ");
1252 current_range.dump (dump_file);
1253 fprintf (dump_file, "\n");
1254 }
1255
1256 // Calculate the "new" range on entry by unioning the pred edges.
1257 new_range.set_undefined ();
1258 FOR_EACH_EDGE (e, ei, bb->preds)
1259 {
1260 edge_range (e_range, e, name, RFD_READ_ONLY);
1261 if (DEBUG_RANGE_CACHE)
1262 {
1263 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
1264 e_range.dump (dump_file);
1265 fprintf (dump_file, "\n");
1266 }
1267 new_range.union_ (e_range);
1268 if (new_range.varying_p ())
1269 break;
1270 }
1271
1272 // If the range on entry has changed, update it.
1273 if (new_range != current_range)
1274 {
1275 bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
1276 // If the cache couldn't set the value, mark it as failed.
1277 if (!ok_p)
1278 m_update->propagation_failed (bb);
1279 if (DEBUG_RANGE_CACHE)
1280 {
1281 if (!ok_p)
1282 {
1283 fprintf (dump_file, " Cache failure to store value:");
1284 print_generic_expr (dump_file, name, TDF_SLIM);
1285 fprintf (dump_file, " ");
1286 }
1287 else
1288 {
1289 fprintf (dump_file, " Updating range to ");
1290 new_range.dump (dump_file);
1291 }
1292 fprintf (dump_file, "\n Updating blocks :");
1293 }
1294 // Mark each successor that has a range to re-check its range
1295 FOR_EACH_EDGE (e, ei, bb->succs)
1296 if (m_on_entry.bb_range_p (name, e->dest))
1297 {
1298 if (DEBUG_RANGE_CACHE)
1299 fprintf (dump_file, " bb%d",e->dest->index);
1300 m_update->add (e->dest);
1301 }
1302 if (DEBUG_RANGE_CACHE)
1303 fprintf (dump_file, "\n");
1304 }
1305 }
1306 if (DEBUG_RANGE_CACHE)
1307 {
1308 fprintf (dump_file, "DONE visiting blocks for ");
1309 print_generic_expr (dump_file, name, TDF_SLIM);
1310 fprintf (dump_file, "\n");
1311 }
1312 m_update->clear_failures ();
1313 }
1314
1315 // Check to see if an update to the value for NAME in BB has any effect
1316 // on values already in the on-entry cache for successor blocks.
1317 // If it does, update them. Don't visit any blocks which don't have a cache
1318 // entry.
1319
1320 void
1321 ranger_cache::propagate_updated_value (tree name, basic_block bb)
1322 {
1323 edge e;
1324 edge_iterator ei;
1325
1326 // The update work list should be empty at this point.
1327 gcc_checking_assert (m_update->empty_p ());
1328 gcc_checking_assert (bb);
1329
1330 if (DEBUG_RANGE_CACHE)
1331 {
1332 fprintf (dump_file, " UPDATE cache for ");
1333 print_generic_expr (dump_file, name, TDF_SLIM);
1334 fprintf (dump_file, " in BB %d : successors : ", bb->index);
1335 }
1336 FOR_EACH_EDGE (e, ei, bb->succs)
1337 {
1338 // Only update active cache entries.
1339 if (m_on_entry.bb_range_p (name, e->dest))
1340 {
1341 m_update->add (e->dest);
1342 if (DEBUG_RANGE_CACHE)
1343 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
1344 }
1345 }
1346 if (!m_update->empty_p ())
1347 {
1348 if (DEBUG_RANGE_CACHE)
1349 fprintf (dump_file, "\n");
1350 propagate_cache (name);
1351 }
1352 else
1353 {
1354 if (DEBUG_RANGE_CACHE)
1355 fprintf (dump_file, " : No updates!\n");
1356 }
1357 }
1358
1359 // Make sure that the range-on-entry cache for NAME is set for block BB.
1360 // Work back through the CFG to DEF_BB ensuring the range is calculated
1361 // on the block/edges leading back to that point.
1362
1363 void
1364 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
1365 {
1366 edge_iterator ei;
1367 edge e;
1368 tree type = TREE_TYPE (name);
1369 Value_Range block_result (type);
1370 Value_Range undefined (type);
1371
1372 // At this point we shouldn't be looking at the def, entry block.
1373 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
1374 gcc_checking_assert (m_workback.length () == 0);
1375
1376 // If the block cache is set, then we've already visited this block.
1377 if (m_on_entry.bb_range_p (name, bb))
1378 return;
1379
1380 if (DEBUG_RANGE_CACHE)
1381 {
1382 fprintf (dump_file, "\n");
1383 print_generic_expr (dump_file, name, TDF_SLIM);
1384 fprintf (dump_file, " : ");
1385 }
1386
1387 // Check if a dominators can supply the range.
1388 if (range_from_dom (block_result, name, bb, RFD_FILL))
1389 {
1390 if (DEBUG_RANGE_CACHE)
1391 {
1392 fprintf (dump_file, "Filled from dominator! : ");
1393 block_result.dump (dump_file);
1394 fprintf (dump_file, "\n");
1395 }
1396 // See if any equivalences can refine it.
1397 // PR 109462, like 108139 below, a one way equivalence introduced
1398 // by a PHI node can also be through the definition side. Disallow it.
1399 if (m_oracle)
1400 {
1401 tree equiv_name;
1402 relation_kind rel;
1403 int prec = TYPE_PRECISION (type);
1404 FOR_EACH_PARTIAL_AND_FULL_EQUIV (m_oracle, bb, name, equiv_name, rel)
1405 {
1406 basic_block equiv_bb = gimple_bb (SSA_NAME_DEF_STMT (equiv_name));
1407
1408 // Ignore partial equivs that are smaller than this object.
1409 if (rel != VREL_EQ && prec > pe_to_bits (rel))
1410 continue;
1411
1412 // Check if the equiv has any ranges calculated.
1413 if (!m_gori.has_edge_range_p (equiv_name))
1414 continue;
1415
1416 // Check if the equiv definition dominates this block
1417 if (equiv_bb == bb ||
1418 (equiv_bb && !dominated_by_p (CDI_DOMINATORS, bb, equiv_bb)))
1419 continue;
1420
1421 if (DEBUG_RANGE_CACHE)
1422 {
1423 if (rel == VREL_EQ)
1424 fprintf (dump_file, "Checking Equivalence (");
1425 else
1426 fprintf (dump_file, "Checking Partial equiv (");
1427 print_relation (dump_file, rel);
1428 fprintf (dump_file, ") ");
1429 print_generic_expr (dump_file, equiv_name, TDF_SLIM);
1430 fprintf (dump_file, "\n");
1431 }
1432 Value_Range equiv_range (TREE_TYPE (equiv_name));
1433 if (range_from_dom (equiv_range, equiv_name, bb, RFD_READ_ONLY))
1434 {
1435 if (rel != VREL_EQ)
1436 range_cast (equiv_range, type);
1437 if (block_result.intersect (equiv_range))
1438 {
1439 if (DEBUG_RANGE_CACHE)
1440 {
1441 if (rel == VREL_EQ)
1442 fprintf (dump_file, "Equivalence update! : ");
1443 else
1444 fprintf (dump_file, "Partial equiv update! : ");
1445 print_generic_expr (dump_file, equiv_name, TDF_SLIM);
1446 fprintf (dump_file, " has range : ");
1447 equiv_range.dump (dump_file);
1448 fprintf (dump_file, " refining range to :");
1449 block_result.dump (dump_file);
1450 fprintf (dump_file, "\n");
1451 }
1452 }
1453 }
1454 }
1455 }
1456
1457 m_on_entry.set_bb_range (name, bb, block_result);
1458 gcc_checking_assert (m_workback.length () == 0);
1459 return;
1460 }
1461
1462 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1463 // m_visited at the end will contain all the blocks that we needed to set
1464 // the range_on_entry cache for.
1465 m_workback.quick_push (bb);
1466 undefined.set_undefined ();
1467 m_on_entry.set_bb_range (name, bb, undefined);
1468 gcc_checking_assert (m_update->empty_p ());
1469
1470 while (m_workback.length () > 0)
1471 {
1472 basic_block node = m_workback.pop ();
1473 if (DEBUG_RANGE_CACHE)
1474 {
1475 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1476 print_generic_expr (dump_file, name, TDF_SLIM);
1477 fprintf (dump_file, "\n");
1478 }
1479
1480 FOR_EACH_EDGE (e, ei, node->preds)
1481 {
1482 basic_block pred = e->src;
1483 Value_Range r (TREE_TYPE (name));
1484
1485 if (DEBUG_RANGE_CACHE)
1486 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1487
1488 // If the pred block is the def block add this BB to update list.
1489 if (pred == def_bb)
1490 {
1491 m_update->add (node);
1492 continue;
1493 }
1494
1495 // If the pred is entry but NOT def, then it is used before
1496 // defined, it'll get set to [] and no need to update it.
1497 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1498 {
1499 if (DEBUG_RANGE_CACHE)
1500 fprintf (dump_file, "entry: bail.");
1501 continue;
1502 }
1503
1504 // Regardless of whether we have visited pred or not, if the
1505 // pred has inferred ranges, revisit this block.
1506 // Don't search the DOM tree.
1507 if (m_exit.has_range_p (name, pred))
1508 {
1509 if (DEBUG_RANGE_CACHE)
1510 fprintf (dump_file, "Inferred range: update ");
1511 m_update->add (node);
1512 }
1513
1514 // If the pred block already has a range, or if it can contribute
1515 // something new. Ie, the edge generates a range of some sort.
1516 if (m_on_entry.get_bb_range (r, name, pred))
1517 {
1518 if (DEBUG_RANGE_CACHE)
1519 {
1520 fprintf (dump_file, "has cache, ");
1521 r.dump (dump_file);
1522 fprintf (dump_file, ", ");
1523 }
1524 if (!r.undefined_p () || m_gori.has_edge_range_p (name, e))
1525 {
1526 m_update->add (node);
1527 if (DEBUG_RANGE_CACHE)
1528 fprintf (dump_file, "update. ");
1529 }
1530 continue;
1531 }
1532
1533 if (DEBUG_RANGE_CACHE)
1534 fprintf (dump_file, "pushing undefined pred block.\n");
1535 // If the pred hasn't been visited (has no range), add it to
1536 // the list.
1537 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1538 m_on_entry.set_bb_range (name, pred, undefined);
1539 m_workback.quick_push (pred);
1540 }
1541 }
1542
1543 if (DEBUG_RANGE_CACHE)
1544 fprintf (dump_file, "\n");
1545
1546 // Now fill in the marked blocks with values.
1547 propagate_cache (name);
1548 if (DEBUG_RANGE_CACHE)
1549 fprintf (dump_file, " Propagation update done.\n");
1550 }
1551
1552 // Resolve the range of BB if the dominators range is R by calculating incoming
1553 // edges to this block. All lead back to the dominator so should be cheap.
1554 // The range for BB is set and returned in R.
1555
1556 void
1557 ranger_cache::resolve_dom (vrange &r, tree name, basic_block bb)
1558 {
1559 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1560 basic_block dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1561
1562 // if it doesn't already have a value, store the incoming range.
1563 if (!m_on_entry.bb_range_p (name, dom_bb) && def_bb != dom_bb)
1564 {
1565 // If the range can't be store, don't try to accumulate
1566 // the range in PREV_BB due to excessive recalculations.
1567 if (!m_on_entry.set_bb_range (name, dom_bb, r))
1568 return;
1569 }
1570 // With the dominator set, we should be able to cheaply query
1571 // each incoming edge now and accumulate the results.
1572 r.set_undefined ();
1573 edge e;
1574 edge_iterator ei;
1575 Value_Range er (TREE_TYPE (name));
1576 FOR_EACH_EDGE (e, ei, bb->preds)
1577 {
1578 // If the predecessor is dominated by this block, then there is a back
1579 // edge, and won't provide anything useful. We'll actually end up with
1580 // VARYING as we will not resolve this node.
1581 if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
1582 continue;
1583 edge_range (er, e, name, RFD_READ_ONLY);
1584 r.union_ (er);
1585 }
1586 // Set the cache in PREV_BB so it is not calculated again.
1587 m_on_entry.set_bb_range (name, bb, r);
1588 }
1589
1590 // Get the range of NAME from dominators of BB and return it in R. Search the
1591 // dominator tree based on MODE.
1592
1593 bool
1594 ranger_cache::range_from_dom (vrange &r, tree name, basic_block start_bb,
1595 enum rfd_mode mode)
1596 {
1597 if (mode == RFD_NONE || !dom_info_available_p (CDI_DOMINATORS))
1598 return false;
1599
1600 // Search back to the definition block or entry block.
1601 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
1602 if (def_bb == NULL)
1603 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1604
1605 basic_block bb;
1606 basic_block prev_bb = start_bb;
1607
1608 // Track any inferred ranges seen.
1609 Value_Range infer (TREE_TYPE (name));
1610 infer.set_varying (TREE_TYPE (name));
1611
1612 // Range on entry to the DEF block should not be queried.
1613 gcc_checking_assert (start_bb != def_bb);
1614 unsigned start_limit = m_workback.length ();
1615
1616 // Default value is global range.
1617 get_global_range (r, name);
1618
1619 // The dominator of EXIT_BLOCK doesn't seem to be set, so at least handle
1620 // the common single exit cases.
1621 if (start_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) && single_pred_p (start_bb))
1622 bb = single_pred_edge (start_bb)->src;
1623 else
1624 bb = get_immediate_dominator (CDI_DOMINATORS, start_bb);
1625
1626 // Search until a value is found, pushing blocks which may need calculating.
1627 for ( ; bb; prev_bb = bb, bb = get_immediate_dominator (CDI_DOMINATORS, bb))
1628 {
1629 // Accumulate any block exit inferred ranges.
1630 m_exit.maybe_adjust_range (infer, name, bb);
1631
1632 // This block has an outgoing range.
1633 if (m_gori.has_edge_range_p (name, bb))
1634 m_workback.quick_push (prev_bb);
1635 else
1636 {
1637 // Normally join blocks don't carry any new range information on
1638 // incoming edges. If the first incoming edge to this block does
1639 // generate a range, calculate the ranges if all incoming edges
1640 // are also dominated by the dominator. (Avoids backedges which
1641 // will break the rule of moving only upward in the dominator tree).
1642 // If the first pred does not generate a range, then we will be
1643 // using the dominator range anyway, so that's all the check needed.
1644 if (EDGE_COUNT (prev_bb->preds) > 1
1645 && m_gori.has_edge_range_p (name, EDGE_PRED (prev_bb, 0)->src))
1646 {
1647 edge e;
1648 edge_iterator ei;
1649 bool all_dom = true;
1650 FOR_EACH_EDGE (e, ei, prev_bb->preds)
1651 if (e->src != bb
1652 && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
1653 {
1654 all_dom = false;
1655 break;
1656 }
1657 if (all_dom)
1658 m_workback.quick_push (prev_bb);
1659 }
1660 }
1661
1662 if (def_bb == bb)
1663 break;
1664
1665 if (m_on_entry.get_bb_range (r, name, bb))
1666 break;
1667 }
1668
1669 if (DEBUG_RANGE_CACHE)
1670 {
1671 fprintf (dump_file, "CACHE: BB %d DOM query for ", start_bb->index);
1672 print_generic_expr (dump_file, name, TDF_SLIM);
1673 fprintf (dump_file, ", found ");
1674 r.dump (dump_file);
1675 if (bb)
1676 fprintf (dump_file, " at BB%d\n", bb->index);
1677 else
1678 fprintf (dump_file, " at function top\n");
1679 }
1680
1681 // Now process any blocks wit incoming edges that nay have adjustments.
1682 while (m_workback.length () > start_limit)
1683 {
1684 Value_Range er (TREE_TYPE (name));
1685 prev_bb = m_workback.pop ();
1686 if (!single_pred_p (prev_bb))
1687 {
1688 // Non single pred means we need to cache a value in the dominator
1689 // so we can cheaply calculate incoming edges to this block, and
1690 // then store the resulting value. If processing mode is not
1691 // RFD_FILL, then the cache cant be stored to, so don't try.
1692 // Otherwise this becomes a quadratic timed calculation.
1693 if (mode == RFD_FILL)
1694 resolve_dom (r, name, prev_bb);
1695 continue;
1696 }
1697
1698 edge e = single_pred_edge (prev_bb);
1699 bb = e->src;
1700 if (m_gori.outgoing_edge_range_p (er, e, name, *this))
1701 {
1702 r.intersect (er);
1703 // If this is a normal edge, apply any inferred ranges.
1704 if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
1705 m_exit.maybe_adjust_range (r, name, bb);
1706
1707 if (DEBUG_RANGE_CACHE)
1708 {
1709 fprintf (dump_file, "CACHE: Adjusted edge range for %d->%d : ",
1710 bb->index, prev_bb->index);
1711 r.dump (dump_file);
1712 fprintf (dump_file, "\n");
1713 }
1714 }
1715 }
1716
1717 // Apply non-null if appropriate.
1718 if (!has_abnormal_call_or_eh_pred_edge_p (start_bb))
1719 r.intersect (infer);
1720
1721 if (DEBUG_RANGE_CACHE)
1722 {
1723 fprintf (dump_file, "CACHE: Range for DOM returns : ");
1724 r.dump (dump_file);
1725 fprintf (dump_file, "\n");
1726 }
1727 return true;
1728 }
1729
1730 // This routine will register an inferred value in block BB, and possibly
1731 // update the on-entry cache if appropriate.
1732
1733 void
1734 ranger_cache::register_inferred_value (const vrange &ir, tree name,
1735 basic_block bb)
1736 {
1737 Value_Range r (TREE_TYPE (name));
1738 if (!m_on_entry.get_bb_range (r, name, bb))
1739 exit_range (r, name, bb, RFD_READ_ONLY);
1740 if (r.intersect (ir))
1741 {
1742 m_on_entry.set_bb_range (name, bb, r);
1743 // If this range was invariant before, remove invariant.
1744 if (!m_gori.has_edge_range_p (name))
1745 m_gori.set_range_invariant (name, false);
1746 }
1747 }
1748
1749 // This routine is used during a block walk to adjust any inferred ranges
1750 // of operands on stmt S.
1751
1752 void
1753 ranger_cache::apply_inferred_ranges (gimple *s)
1754 {
1755 bool update = true;
1756
1757 basic_block bb = gimple_bb (s);
1758 gimple_infer_range infer(s);
1759 if (infer.num () == 0)
1760 return;
1761
1762 // Do not update the on-entry cache for block ending stmts.
1763 if (stmt_ends_bb_p (s))
1764 {
1765 edge_iterator ei;
1766 edge e;
1767 FOR_EACH_EDGE (e, ei, gimple_bb (s)->succs)
1768 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
1769 break;
1770 if (e == NULL)
1771 update = false;
1772 }
1773
1774 for (unsigned x = 0; x < infer.num (); x++)
1775 {
1776 tree name = infer.name (x);
1777 m_exit.add_range (name, bb, infer.range (x));
1778 if (update)
1779 register_inferred_value (infer.range (x), name, bb);
1780 }
1781 }