Daily bump.
[gcc.git] / gcc / gimple-range-cache.cc
1 /* Gimple ranger SSA cache implementation.
2 Copyright (C) 2017-2021 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
32 // During contructor, allocate the vector of ssa_names.
33
34 non_null_ref::non_null_ref ()
35 {
36 m_nn.create (0);
37 m_nn.safe_grow_cleared (num_ssa_names);
38 bitmap_obstack_initialize (&m_bitmaps);
39 }
40
41 // Free any bitmaps which were allocated,a swell as the vector itself.
42
43 non_null_ref::~non_null_ref ()
44 {
45 bitmap_obstack_release (&m_bitmaps);
46 m_nn.release ();
47 }
48
49 // Return true if NAME has a non-null dereference in block bb. If this is the
50 // first query for NAME, calculate the summary first.
51
52 bool
53 non_null_ref::non_null_deref_p (tree name, basic_block bb)
54 {
55 if (!POINTER_TYPE_P (TREE_TYPE (name)))
56 return false;
57
58 unsigned v = SSA_NAME_VERSION (name);
59 if (!m_nn[v])
60 process_name (name);
61
62 return bitmap_bit_p (m_nn[v], bb->index);
63 }
64
65 // Allocate an populate the bitmap for NAME. An ON bit for a block
66 // index indicates there is a non-null reference in that block. In
67 // order to populate the bitmap, a quick run of all the immediate uses
68 // are made and the statement checked to see if a non-null dereference
69 // is made on that statement.
70
71 void
72 non_null_ref::process_name (tree name)
73 {
74 unsigned v = SSA_NAME_VERSION (name);
75 use_operand_p use_p;
76 imm_use_iterator iter;
77 bitmap b;
78
79 // Only tracked for pointers.
80 if (!POINTER_TYPE_P (TREE_TYPE (name)))
81 return;
82
83 // Already processed if a bitmap has been allocated.
84 if (m_nn[v])
85 return;
86
87 b = BITMAP_ALLOC (&m_bitmaps);
88
89 // Loop over each immediate use and see if it implies a non-null value.
90 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
91 {
92 gimple *s = USE_STMT (use_p);
93 unsigned index = gimple_bb (s)->index;
94
95 // If bit is already set for this block, dont bother looking again.
96 if (bitmap_bit_p (b, index))
97 continue;
98
99 // If we can infer a nonnull range, then set the bit for this BB
100 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
101 && infer_nonnull_range (s, name))
102 bitmap_set_bit (b, index);
103 }
104
105 m_nn[v] = b;
106 }
107
108 // -------------------------------------------------------------------------
109
110 // This class implements a cache of ranges indexed by basic block. It
111 // represents all that is known about an SSA_NAME on entry to each
112 // block. It caches a range-for-type varying range so it doesn't need
113 // to be reformed all the time. If a range is ever always associated
114 // with a type, we can use that instead. Whenever varying is being
115 // set for a block, the cache simply points to this cached one rather
116 // than create a new one each time.
117
118 class ssa_block_ranges
119 {
120 public:
121 ssa_block_ranges (tree t, irange_allocator *allocator);
122 ~ssa_block_ranges ();
123
124 void set_bb_range (const basic_block bb, const irange &r);
125 void set_bb_varying (const basic_block bb);
126 bool get_bb_range (irange &r, const basic_block bb);
127 bool bb_range_p (const basic_block bb);
128
129 void dump(FILE *f);
130 private:
131 vec<irange *> m_tab;
132 irange *m_type_range;
133 tree m_type;
134 irange_allocator *m_irange_allocator;
135 };
136
137
138 // Initialize a block cache for an ssa_name of type T.
139
140 ssa_block_ranges::ssa_block_ranges (tree t, irange_allocator *allocator)
141 {
142 gcc_checking_assert (TYPE_P (t));
143 m_type = t;
144 m_irange_allocator = allocator;
145
146 m_tab.create (0);
147 m_tab.safe_grow_cleared (last_basic_block_for_fn (cfun));
148
149 // Create the cached type range.
150 m_type_range = m_irange_allocator->allocate (2);
151 m_type_range->set_varying (t);
152
153 m_tab[ENTRY_BLOCK_PTR_FOR_FN (cfun)->index] = m_type_range;
154 }
155
156 // Destruct block range.
157
158 ssa_block_ranges::~ssa_block_ranges ()
159 {
160 m_tab.release ();
161 }
162
163 // Set the range for block BB to be R.
164
165 void
166 ssa_block_ranges::set_bb_range (const basic_block bb, const irange &r)
167 {
168 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
169 irange *m = m_irange_allocator->allocate (r);
170 m_tab[bb->index] = m;
171 }
172
173 // Set the range for block BB to the range for the type.
174
175 void
176 ssa_block_ranges::set_bb_varying (const basic_block bb)
177 {
178 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
179 m_tab[bb->index] = m_type_range;
180 }
181
182 // Return the range associated with block BB in R. Return false if
183 // there is no range.
184
185 bool
186 ssa_block_ranges::get_bb_range (irange &r, const basic_block bb)
187 {
188 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
189 irange *m = m_tab[bb->index];
190 if (m)
191 {
192 r = *m;
193 return true;
194 }
195 return false;
196 }
197
198 // Return true if a range is present.
199
200 bool
201 ssa_block_ranges::bb_range_p (const basic_block bb)
202 {
203 gcc_checking_assert ((unsigned) bb->index < m_tab.length ());
204 return m_tab[bb->index] != NULL;
205 }
206
207
208 // Print the list of known ranges for file F in a nice format.
209
210 void
211 ssa_block_ranges::dump (FILE *f)
212 {
213 basic_block bb;
214 int_range_max r;
215
216 FOR_EACH_BB_FN (bb, cfun)
217 if (get_bb_range (r, bb))
218 {
219 fprintf (f, "BB%d -> ", bb->index);
220 r.dump (f);
221 fprintf (f, "\n");
222 }
223 }
224
225 // -------------------------------------------------------------------------
226
227 // Initialize the block cache.
228
229 block_range_cache::block_range_cache ()
230 {
231 m_ssa_ranges.create (0);
232 m_ssa_ranges.safe_grow_cleared (num_ssa_names);
233 m_irange_allocator = new irange_allocator;
234 }
235
236 // Remove any m_block_caches which have been created.
237
238 block_range_cache::~block_range_cache ()
239 {
240 unsigned x;
241 for (x = 0; x < m_ssa_ranges.length (); ++x)
242 {
243 if (m_ssa_ranges[x])
244 delete m_ssa_ranges[x];
245 }
246 delete m_irange_allocator;
247 // Release the vector itself.
248 m_ssa_ranges.release ();
249 }
250
251 // Return a reference to the ssa_block_cache for NAME. If it has not been
252 // accessed yet, allocate it first.
253
254 ssa_block_ranges &
255 block_range_cache::get_block_ranges (tree name)
256 {
257 unsigned v = SSA_NAME_VERSION (name);
258 if (v >= m_ssa_ranges.length ())
259 m_ssa_ranges.safe_grow_cleared (num_ssa_names + 1);
260
261 if (!m_ssa_ranges[v])
262 m_ssa_ranges[v] = new ssa_block_ranges (TREE_TYPE (name),
263 m_irange_allocator);
264 return *(m_ssa_ranges[v]);
265 }
266
267
268 // Return a pointer to the ssa_block_cache for NAME. If it has not been
269 // accessed yet, return NULL.
270
271 ssa_block_ranges *
272 block_range_cache::query_block_ranges (tree name)
273 {
274 unsigned v = SSA_NAME_VERSION (name);
275 if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
276 return NULL;
277 return m_ssa_ranges[v];
278 }
279
280 // Set the range for NAME on entry to block BB to R.
281
282 void
283 block_range_cache::set_bb_range (tree name, const basic_block bb,
284 const irange &r)
285 {
286 return get_block_ranges (name).set_bb_range (bb, r);
287 }
288
289 // Set the range for NAME on entry to block BB to varying.
290
291 void
292 block_range_cache::set_bb_varying (tree name, const basic_block bb)
293 {
294 return get_block_ranges (name).set_bb_varying (bb);
295 }
296
297 // Return the range for NAME on entry to BB in R. Return true if there
298 // is one.
299
300 bool
301 block_range_cache::get_bb_range (irange &r, tree name, const basic_block bb)
302 {
303 ssa_block_ranges *ptr = query_block_ranges (name);
304 if (ptr)
305 return ptr->get_bb_range (r, bb);
306 return false;
307 }
308
309 // Return true if NAME has a range set in block BB.
310
311 bool
312 block_range_cache::bb_range_p (tree name, const basic_block bb)
313 {
314 ssa_block_ranges *ptr = query_block_ranges (name);
315 if (ptr)
316 return ptr->bb_range_p (bb);
317 return false;
318 }
319
320 // Print all known block caches to file F.
321
322 void
323 block_range_cache::dump (FILE *f)
324 {
325 unsigned x;
326 for (x = 0; x < m_ssa_ranges.length (); ++x)
327 {
328 if (m_ssa_ranges[x])
329 {
330 fprintf (f, " Ranges for ");
331 print_generic_expr (f, ssa_name (x), TDF_NONE);
332 fprintf (f, ":\n");
333 m_ssa_ranges[x]->dump (f);
334 fprintf (f, "\n");
335 }
336 }
337 }
338
339 // Print all known ranges on entry to blobk BB to file F.
340
341 void
342 block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
343 {
344 unsigned x;
345 int_range_max r;
346 bool summarize_varying = false;
347 for (x = 1; x < m_ssa_ranges.length (); ++x)
348 {
349 if (!gimple_range_ssa_p (ssa_name (x)))
350 continue;
351 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
352 {
353 if (!print_varying && r.varying_p ())
354 {
355 summarize_varying = true;
356 continue;
357 }
358 print_generic_expr (f, ssa_name (x), TDF_NONE);
359 fprintf (f, "\t");
360 r.dump(f);
361 fprintf (f, "\n");
362 }
363 }
364 // If there were any varying entries, lump them all together.
365 if (summarize_varying)
366 {
367 fprintf (f, "VARYING_P on entry : ");
368 for (x = 1; x < num_ssa_names; ++x)
369 {
370 if (!gimple_range_ssa_p (ssa_name (x)))
371 continue;
372 if (m_ssa_ranges[x] && m_ssa_ranges[x]->get_bb_range (r, bb))
373 {
374 if (r.varying_p ())
375 {
376 print_generic_expr (f, ssa_name (x), TDF_NONE);
377 fprintf (f, " ");
378 }
379 }
380 }
381 fprintf (f, "\n");
382 }
383 }
384
385 // -------------------------------------------------------------------------
386
387 // Initialize a global cache.
388
389 ssa_global_cache::ssa_global_cache ()
390 {
391 m_tab.create (0);
392 m_tab.safe_grow_cleared (num_ssa_names);
393 m_irange_allocator = new irange_allocator;
394 }
395
396 // Deconstruct a global cache.
397
398 ssa_global_cache::~ssa_global_cache ()
399 {
400 m_tab.release ();
401 delete m_irange_allocator;
402 }
403
404 // Retrieve the global range of NAME from cache memory if it exists.
405 // Return the value in R.
406
407 bool
408 ssa_global_cache::get_global_range (irange &r, tree name) const
409 {
410 unsigned v = SSA_NAME_VERSION (name);
411 if (v >= m_tab.length ())
412 return false;
413
414 irange *stow = m_tab[v];
415 if (!stow)
416 return false;
417 r = *stow;
418 return true;
419 }
420
421 // Set the range for NAME to R in the global cache.
422 // Return TRUE if there was already a range set, otherwise false.
423
424 bool
425 ssa_global_cache::set_global_range (tree name, const irange &r)
426 {
427 unsigned v = SSA_NAME_VERSION (name);
428 if (v >= m_tab.length ())
429 m_tab.safe_grow_cleared (num_ssa_names + 1);
430
431 irange *m = m_tab[v];
432 if (m && m->fits_p (r))
433 *m = r;
434 else
435 m_tab[v] = m_irange_allocator->allocate (r);
436 return m != NULL;
437 }
438
439 // Set the range for NAME to R in the glonbal cache.
440
441 void
442 ssa_global_cache::clear_global_range (tree name)
443 {
444 unsigned v = SSA_NAME_VERSION (name);
445 if (v >= m_tab.length ())
446 m_tab.safe_grow_cleared (num_ssa_names + 1);
447 m_tab[v] = NULL;
448 }
449
450 // Clear the global cache.
451
452 void
453 ssa_global_cache::clear ()
454 {
455 memset (m_tab.address(), 0, m_tab.length () * sizeof (irange *));
456 }
457
458 // Dump the contents of the global cache to F.
459
460 void
461 ssa_global_cache::dump (FILE *f)
462 {
463 unsigned x;
464 int_range_max r;
465 fprintf (f, "Non-varying global ranges:\n");
466 fprintf (f, "=========================:\n");
467 for ( x = 1; x < num_ssa_names; x++)
468 if (gimple_range_ssa_p (ssa_name (x)) &&
469 get_global_range (r, ssa_name (x)) && !r.varying_p ())
470 {
471 print_generic_expr (f, ssa_name (x), TDF_NONE);
472 fprintf (f, " : ");
473 r.dump (f);
474 fprintf (f, "\n");
475 }
476 fputc ('\n', f);
477 }
478
479 // --------------------------------------------------------------------------
480
481
482 // This struct provides a timestamp for a global range calculation.
483 // it contains the time counter, as well as a limited number of ssa-names
484 // that it is dependent upon. If the timestamp for any of the dependent names
485 // Are newer, then this range could need updating.
486
487 struct range_timestamp
488 {
489 unsigned time;
490 unsigned ssa1;
491 unsigned ssa2;
492 };
493
494 // This class will manage the timestamps for each ssa_name.
495 // When a value is calcualted, its timestamp is set to the current time.
496 // The ssanames it is dependent on have already been calculated, so they will
497 // have older times. If one fo those values is ever calculated again, it
498 // will get a newer timestamp, and the "current_p" check will fail.
499
500 class temporal_cache
501 {
502 public:
503 temporal_cache ();
504 ~temporal_cache ();
505 bool current_p (tree name) const;
506 void set_timestamp (tree name);
507 void set_dependency (tree name, tree dep);
508 void set_always_current (tree name);
509 private:
510 unsigned temporal_value (unsigned ssa) const;
511 const range_timestamp *get_timestamp (unsigned ssa) const;
512 range_timestamp *get_timestamp (unsigned ssa);
513
514 unsigned m_current_time;
515 vec <range_timestamp> m_timestamp;
516 };
517
518
519 inline
520 temporal_cache::temporal_cache ()
521 {
522 m_current_time = 1;
523 m_timestamp.create (0);
524 m_timestamp.safe_grow_cleared (num_ssa_names);
525 }
526
527 inline
528 temporal_cache::~temporal_cache ()
529 {
530 m_timestamp.release ();
531 }
532
533 // Return a pointer to the timetamp for ssa-name at index SSA, if there is
534 // one, otherwise return NULL.
535
536 inline const range_timestamp *
537 temporal_cache::get_timestamp (unsigned ssa) const
538 {
539 if (ssa >= m_timestamp.length ())
540 return NULL;
541 return &(m_timestamp[ssa]);
542 }
543
544 // Return a reference to the timetamp for ssa-name at index SSA. If the index
545 // is past the end of the vector, extend the vector.
546
547 inline range_timestamp *
548 temporal_cache::get_timestamp (unsigned ssa)
549 {
550 if (ssa >= m_timestamp.length ())
551 m_timestamp.safe_grow_cleared (num_ssa_names + 20);
552 return &(m_timestamp[ssa]);
553 }
554
555 // This routine will fill NAME's next operand slot with DEP if DEP is a valid
556 // SSA_NAME and there is a free slot.
557
558 inline void
559 temporal_cache::set_dependency (tree name, tree dep)
560 {
561 if (dep && TREE_CODE (dep) == SSA_NAME)
562 {
563 gcc_checking_assert (get_timestamp (SSA_NAME_VERSION (name)));
564 range_timestamp& ts = *(get_timestamp (SSA_NAME_VERSION (name)));
565 if (!ts.ssa1)
566 ts.ssa1 = SSA_NAME_VERSION (dep);
567 else if (!ts.ssa2 && ts.ssa1 != SSA_NAME_VERSION (name))
568 ts.ssa2 = SSA_NAME_VERSION (dep);
569 }
570 }
571
572 // Return the timestamp value for SSA, or 0 if there isnt one.
573 inline unsigned
574 temporal_cache::temporal_value (unsigned ssa) const
575 {
576 const range_timestamp *ts = get_timestamp (ssa);
577 return ts ? ts->time : 0;
578 }
579
580 // Return TRUE if the timestampe for NAME is newer than any of its dependents.
581
582 bool
583 temporal_cache::current_p (tree name) const
584 {
585 const range_timestamp *ts = get_timestamp (SSA_NAME_VERSION (name));
586 if (!ts || ts->time == 0)
587 return true;
588 // Any non-registered dependencies will have a value of 0 and thus be older.
589 // Return true if time is newer than either dependent.
590 return ts->time > temporal_value (ts->ssa1)
591 && ts->time > temporal_value (ts->ssa2);
592 }
593
594 // This increments the global timer and sets the timestamp for NAME.
595
596 inline void
597 temporal_cache::set_timestamp (tree name)
598 {
599 gcc_checking_assert (get_timestamp (SSA_NAME_VERSION (name)));
600 get_timestamp (SSA_NAME_VERSION (name))->time = ++m_current_time;
601 }
602
603 // Set the timestamp to 0, marking it as "always up to date".
604
605 inline void
606 temporal_cache::set_always_current (tree name)
607 {
608 gcc_checking_assert (get_timestamp (SSA_NAME_VERSION (name)));
609 get_timestamp (SSA_NAME_VERSION (name))->time = 0;
610 }
611
612
613 // --------------------------------------------------------------------------
614
615 ranger_cache::ranger_cache (gimple_ranger &q) : query (q)
616 {
617 m_workback.create (0);
618 m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
619 m_update_list.create (0);
620 m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun));
621 m_update_list.truncate (0);
622 m_poor_value_list.create (0);
623 m_poor_value_list.safe_grow_cleared (20);
624 m_poor_value_list.truncate (0);
625 m_temporal = new temporal_cache;
626 }
627
628 ranger_cache::~ranger_cache ()
629 {
630 delete m_temporal;
631 m_poor_value_list.release ();
632 m_workback.release ();
633 m_update_list.release ();
634 }
635
636 // Dump the global caches to file F. if GORI_DUMP is true, dump the
637 // gori map as well.
638
639 void
640 ranger_cache::dump (FILE *f, bool gori_dump)
641 {
642 m_globals.dump (f);
643 if (gori_dump)
644 {
645 fprintf (f, "\nDUMPING GORI MAP\n");
646 gori_compute::dump (f);
647 }
648 fprintf (f, "\n");
649 }
650
651 // Dump the caches for basic block BB to file F.
652
653 void
654 ranger_cache::dump (FILE *f, basic_block bb)
655 {
656 m_on_entry.dump (f, bb);
657 }
658
659 // Get the global range for NAME, and return in R. Return false if the
660 // global range is not set.
661
662 bool
663 ranger_cache::get_global_range (irange &r, tree name) const
664 {
665 return m_globals.get_global_range (r, name);
666 }
667
668 // Get the global range for NAME, and return in R if the value is not stale.
669 // If the range is set, but is stale, mark it current and return false.
670 // If it is not set pick up the legacy global value, mark it current, and
671 // return false.
672 // Note there is always a value returned in R. The return value indicates
673 // whether that value is an up-to-date calculated value or not..
674
675 bool
676 ranger_cache::get_non_stale_global_range (irange &r, tree name)
677 {
678 if (m_globals.get_global_range (r, name))
679 {
680 if (m_temporal->current_p (name))
681 return true;
682 }
683 else
684 {
685 // Global has never been accessed, so pickup the legacy global value.
686 r = gimple_range_global (name);
687 m_globals.set_global_range (name, r);
688 }
689 // After a stale check failure, mark the value as always current until a
690 // new one is set.
691 m_temporal->set_always_current (name);
692 return false;
693 }
694 // Set the global range of NAME to R.
695
696 void
697 ranger_cache::set_global_range (tree name, const irange &r)
698 {
699 if (m_globals.set_global_range (name, r))
700 {
701 // If there was already a range set, propagate the new value.
702 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
703 if (!bb)
704 bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
705
706 if (DEBUG_RANGE_CACHE)
707 fprintf (dump_file, " GLOBAL :");
708
709 propagate_updated_value (name, bb);
710 }
711 // Mark the value as up-to-date.
712 m_temporal->set_timestamp (name);
713 }
714
715 // Register a dependency on DEP to name. If the timestamp for DEP is ever
716 // greateer than the timestamp for NAME, then it is newer and NAMEs value
717 // becomes stale.
718
719 void
720 ranger_cache::register_dependency (tree name, tree dep)
721 {
722 m_temporal->set_dependency (name, dep);
723 }
724
725 // Push a request for a new lookup in block BB of name. Return true if
726 // the request is actually made (ie, isn't a duplicate).
727
728 bool
729 ranger_cache::push_poor_value (basic_block bb, tree name)
730 {
731 if (m_poor_value_list.length ())
732 {
733 // Don't push anything else to the same block. If there are multiple
734 // things required, another request will come during a later evaluation
735 // and this prevents oscillation building uneccessary depth.
736 if ((m_poor_value_list.last ()).bb == bb)
737 return false;
738 }
739
740 struct update_record rec;
741 rec.bb = bb;
742 rec.calc = name;
743 m_poor_value_list.safe_push (rec);
744 return true;
745 }
746
747 // Provide lookup for the gori-computes class to access the best known range
748 // of an ssa_name in any given basic block. Note, this does no additonal
749 // lookups, just accesses the data that is already known.
750
751 void
752 ranger_cache::ssa_range_in_bb (irange &r, tree name, basic_block bb)
753 {
754 gimple *s = SSA_NAME_DEF_STMT (name);
755 basic_block def_bb = ((s && gimple_bb (s)) ? gimple_bb (s) :
756 ENTRY_BLOCK_PTR_FOR_FN (cfun));
757 if (bb == def_bb)
758 {
759 // NAME is defined in this block, so request its current value
760 if (!m_globals.get_global_range (r, name))
761 {
762 // If it doesn't have a value calculated, it means it's a
763 // "poor" value being used in some calculation. Queue it up
764 // as a poor value to be improved later.
765 r = gimple_range_global (name);
766 if (push_poor_value (bb, name))
767 {
768 if (DEBUG_RANGE_CACHE)
769 {
770 fprintf (dump_file,
771 "*CACHE* no global def in bb %d for ", bb->index);
772 print_generic_expr (dump_file, name, TDF_SLIM);
773 fprintf (dump_file, " depth : %d\n",
774 m_poor_value_list.length ());
775 }
776 }
777 }
778 }
779 // Look for the on-entry value of name in BB from the cache.
780 else if (!m_on_entry.get_bb_range (r, name, bb))
781 {
782 // If it has no entry but should, then mark this as a poor value.
783 // Its not a poor value if it does not have *any* edge ranges,
784 // Then global range is as good as it gets.
785 if (has_edge_range_p (name) && push_poor_value (bb, name))
786 {
787 if (DEBUG_RANGE_CACHE)
788 {
789 fprintf (dump_file,
790 "*CACHE* no on entry range in bb %d for ", bb->index);
791 print_generic_expr (dump_file, name, TDF_SLIM);
792 fprintf (dump_file, " depth : %d\n", m_poor_value_list.length ());
793 }
794 }
795 // Try to pick up any known global value as a best guess for now.
796 if (!m_globals.get_global_range (r, name))
797 r = gimple_range_global (name);
798 }
799
800 // Check if pointers have any non-null dereferences. Non-call
801 // exceptions mean we could throw in the middle of the block, so just
802 // punt for now on those.
803 if (r.varying_p () && m_non_null.non_null_deref_p (name, bb) &&
804 !cfun->can_throw_non_call_exceptions)
805 r = range_nonzero (TREE_TYPE (name));
806 }
807
808 // Return a static range for NAME on entry to basic block BB in R. If
809 // calc is true, fill any cache entries required between BB and the
810 // def block for NAME. Otherwise, return false if the cache is empty.
811
812 bool
813 ranger_cache::block_range (irange &r, basic_block bb, tree name, bool calc)
814 {
815 gcc_checking_assert (gimple_range_ssa_p (name));
816
817 // If there are no range calculations anywhere in the IL, global range
818 // applies everywhere, so don't bother caching it.
819 if (!has_edge_range_p (name))
820 return false;
821
822 if (calc)
823 {
824 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
825 basic_block def_bb = NULL;
826 if (def_stmt)
827 def_bb = gimple_bb (def_stmt);;
828 if (!def_bb)
829 {
830 // If we get to the entry block, this better be a default def
831 // or range_on_entry was called for a block not dominated by
832 // the def.
833 gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
834 def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
835 }
836
837 // There is no range on entry for the definition block.
838 if (def_bb == bb)
839 return false;
840
841 // Otherwise, go figure out what is known in predecessor blocks.
842 fill_block_cache (name, bb, def_bb);
843 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
844 }
845 return m_on_entry.get_bb_range (r, name, bb);
846 }
847
848 // Add BB to the list of blocks to update, unless it's already in the list.
849
850 void
851 ranger_cache::add_to_update (basic_block bb)
852 {
853 if (!m_update_list.contains (bb))
854 m_update_list.quick_push (bb);
855 }
856
857 // If there is anything in the propagation update_list, continue
858 // processing NAME until the list of blocks is empty.
859
860 void
861 ranger_cache::propagate_cache (tree name)
862 {
863 basic_block bb;
864 edge_iterator ei;
865 edge e;
866 int_range_max new_range;
867 int_range_max current_range;
868 int_range_max e_range;
869
870 // Process each block by seeing if its calculated range on entry is
871 // the same as its cached value. If there is a difference, update
872 // the cache to reflect the new value, and check to see if any
873 // successors have cache entries which may need to be checked for
874 // updates.
875
876 while (m_update_list.length () > 0)
877 {
878 bb = m_update_list.pop ();
879 gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
880 m_on_entry.get_bb_range (current_range, name, bb);
881
882 // Calculate the "new" range on entry by unioning the pred edges.
883 new_range.set_undefined ();
884 FOR_EACH_EDGE (e, ei, bb->preds)
885 {
886 if (DEBUG_RANGE_CACHE)
887 fprintf (dump_file, " edge %d->%d :", e->src->index, bb->index);
888 // Get whatever range we can for this edge.
889 if (!outgoing_edge_range_p (e_range, e, name))
890 {
891 ssa_range_in_bb (e_range, name, e->src);
892 if (DEBUG_RANGE_CACHE)
893 {
894 fprintf (dump_file, "No outgoing edge range, picked up ");
895 e_range.dump(dump_file);
896 fprintf (dump_file, "\n");
897 }
898 }
899 else
900 {
901 if (DEBUG_RANGE_CACHE)
902 {
903 fprintf (dump_file, "outgoing range :");
904 e_range.dump(dump_file);
905 fprintf (dump_file, "\n");
906 }
907 }
908 new_range.union_ (e_range);
909 if (new_range.varying_p ())
910 break;
911 }
912
913 if (DEBUG_RANGE_CACHE)
914 {
915 fprintf (dump_file, "FWD visiting block %d for ", bb->index);
916 print_generic_expr (dump_file, name, TDF_SLIM);
917 fprintf (dump_file, " starting range : ");
918 current_range.dump (dump_file);
919 fprintf (dump_file, "\n");
920 }
921
922 // If the range on entry has changed, update it.
923 if (new_range != current_range)
924 {
925 if (DEBUG_RANGE_CACHE)
926 {
927 fprintf (dump_file, " Updating range to ");
928 new_range.dump (dump_file);
929 fprintf (dump_file, "\n Updating blocks :");
930 }
931 m_on_entry.set_bb_range (name, bb, new_range);
932 // Mark each successor that has a range to re-check its range
933 FOR_EACH_EDGE (e, ei, bb->succs)
934 if (m_on_entry.bb_range_p (name, e->dest))
935 {
936 if (DEBUG_RANGE_CACHE)
937 fprintf (dump_file, " bb%d",e->dest->index);
938 add_to_update (e->dest);
939 }
940 if (DEBUG_RANGE_CACHE)
941 fprintf (dump_file, "\n");
942 }
943 }
944 if (DEBUG_RANGE_CACHE)
945 {
946 fprintf (dump_file, "DONE visiting blocks for ");
947 print_generic_expr (dump_file, name, TDF_SLIM);
948 fprintf (dump_file, "\n");
949 }
950 }
951
952 // Check to see if an update to the value for NAME in BB has any effect
953 // on values already in the on-entry cache for successor blocks.
954 // If it does, update them. Don't visit any blocks which dont have a cache
955 // entry.
956
957 void
958 ranger_cache::propagate_updated_value (tree name, basic_block bb)
959 {
960 edge e;
961 edge_iterator ei;
962
963 // The update work list should be empty at this point.
964 gcc_checking_assert (m_update_list.length () == 0);
965 gcc_checking_assert (bb);
966
967 if (DEBUG_RANGE_CACHE)
968 {
969 fprintf (dump_file, " UPDATE cache for ");
970 print_generic_expr (dump_file, name, TDF_SLIM);
971 fprintf (dump_file, " in BB %d : successors : ", bb->index);
972 }
973 FOR_EACH_EDGE (e, ei, bb->succs)
974 {
975 // Only update active cache entries.
976 if (m_on_entry.bb_range_p (name, e->dest))
977 {
978 add_to_update (e->dest);
979 if (DEBUG_RANGE_CACHE)
980 fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
981 }
982 }
983 if (m_update_list.length () != 0)
984 {
985 if (DEBUG_RANGE_CACHE)
986 fprintf (dump_file, "\n");
987 propagate_cache (name);
988 }
989 else
990 {
991 if (DEBUG_RANGE_CACHE)
992 fprintf (dump_file, " : No updates!\n");
993 }
994 }
995
996 // Make sure that the range-on-entry cache for NAME is set for block BB.
997 // Work back through the CFG to DEF_BB ensuring the range is calculated
998 // on the block/edges leading back to that point.
999
1000 void
1001 ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
1002 {
1003 edge_iterator ei;
1004 edge e;
1005 int_range_max block_result;
1006 int_range_max undefined;
1007 unsigned poor_list_start = m_poor_value_list.length ();
1008
1009 // At this point we shouldn't be looking at the def, entry or exit block.
1010 gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
1011 bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
1012
1013 // If the block cache is set, then we've already visited this block.
1014 if (m_on_entry.bb_range_p (name, bb))
1015 return;
1016
1017 // Visit each block back to the DEF. Initialize each one to UNDEFINED.
1018 // m_visited at the end will contain all the blocks that we needed to set
1019 // the range_on_entry cache for.
1020 m_workback.truncate (0);
1021 m_workback.quick_push (bb);
1022 undefined.set_undefined ();
1023 m_on_entry.set_bb_range (name, bb, undefined);
1024 gcc_checking_assert (m_update_list.length () == 0);
1025
1026 if (DEBUG_RANGE_CACHE)
1027 {
1028 fprintf (dump_file, "\n");
1029 print_generic_expr (dump_file, name, TDF_SLIM);
1030 fprintf (dump_file, " : ");
1031 }
1032
1033 while (m_workback.length () > 0)
1034 {
1035 basic_block node = m_workback.pop ();
1036 if (DEBUG_RANGE_CACHE)
1037 {
1038 fprintf (dump_file, "BACK visiting block %d for ", node->index);
1039 print_generic_expr (dump_file, name, TDF_SLIM);
1040 fprintf (dump_file, "\n");
1041 }
1042
1043 FOR_EACH_EDGE (e, ei, node->preds)
1044 {
1045 basic_block pred = e->src;
1046 int_range_max r;
1047
1048 if (DEBUG_RANGE_CACHE)
1049 fprintf (dump_file, " %d->%d ",e->src->index, e->dest->index);
1050
1051 // If the pred block is the def block add this BB to update list.
1052 if (pred == def_bb)
1053 {
1054 add_to_update (node);
1055 continue;
1056 }
1057
1058 // If the pred is entry but NOT def, then it is used before
1059 // defined, it'll get set to [] and no need to update it.
1060 if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1061 {
1062 if (DEBUG_RANGE_CACHE)
1063 fprintf (dump_file, "entry: bail.");
1064 continue;
1065 }
1066
1067 // Regardless of whether we have visited pred or not, if the
1068 // pred has a non-null reference, revisit this block.
1069 if (m_non_null.non_null_deref_p (name, pred))
1070 {
1071 if (DEBUG_RANGE_CACHE)
1072 fprintf (dump_file, "nonnull: update ");
1073 add_to_update (node);
1074 }
1075
1076 // If the pred block already has a range, or if it can contribute
1077 // something new. Ie, the edge generates a range of some sort.
1078 if (m_on_entry.get_bb_range (r, name, pred))
1079 {
1080 if (DEBUG_RANGE_CACHE)
1081 fprintf (dump_file, "has cache, ");
1082 if (!r.undefined_p () || has_edge_range_p (name, e))
1083 {
1084 add_to_update (node);
1085 if (DEBUG_RANGE_CACHE)
1086 fprintf (dump_file, "update. ");
1087 }
1088 continue;
1089 }
1090
1091 if (DEBUG_RANGE_CACHE)
1092 fprintf (dump_file, "pushing undefined pred block. ");
1093 // If the pred hasn't been visited (has no range), add it to
1094 // the list.
1095 gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
1096 m_on_entry.set_bb_range (name, pred, undefined);
1097 m_workback.quick_push (pred);
1098 }
1099 }
1100
1101 if (DEBUG_RANGE_CACHE)
1102 fprintf (dump_file, "\n");
1103
1104 // Now fill in the marked blocks with values.
1105 propagate_cache (name);
1106 if (DEBUG_RANGE_CACHE)
1107 fprintf (dump_file, " Propagation update done.\n");
1108
1109 // Now that the cache has been updated, check to see if there were any
1110 // SSA_NAMES used in filling the cache which were "poor values".
1111 // Evaluate them, and inject any new values into the propagation
1112 // list, and see if it improves any on-entry values.
1113 if (poor_list_start != m_poor_value_list.length ())
1114 {
1115 gcc_checking_assert (poor_list_start < m_poor_value_list.length ());
1116 while (poor_list_start < m_poor_value_list.length ())
1117 {
1118 // Find a range for this unresolved value.
1119 // Note, this may spawn new cache filling cycles, but by the time it
1120 // is finished, the work vectors will all be back to the same state
1121 // as before the call. The update record vector will always be
1122 // returned to the current state upon return.
1123 struct update_record rec = m_poor_value_list.pop ();
1124 basic_block calc_bb = rec.bb;
1125 int_range_max tmp;
1126
1127 if (DEBUG_RANGE_CACHE)
1128 {
1129 fprintf (dump_file, "(%d:%d)Calculating ",
1130 m_poor_value_list.length () + 1, poor_list_start);
1131 print_generic_expr (dump_file, name, TDF_SLIM);
1132 fprintf (dump_file, " used POOR VALUE for ");
1133 print_generic_expr (dump_file, rec.calc, TDF_SLIM);
1134 fprintf (dump_file, " in bb%d, trying to improve:\n",
1135 calc_bb->index);
1136 }
1137
1138 // Calculate a range at the exit from the block so the caches feeding
1139 // this block will be filled, and we'll get a "better" value.
1140 query.range_on_exit (tmp, calc_bb, rec.calc);
1141
1142 // Then ask for NAME to be re-evaluated on outgoing edges and
1143 // use any new values.
1144 propagate_updated_value (name, calc_bb);
1145 }
1146 }
1147 }
1148