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