1 /* Code for GIMPLE range related routines.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "gimple-pretty-print.h"
30 #include "gimple-iterator.h"
32 #include "fold-const.h"
35 #include "tree-scalar-evolution.h"
36 #include "gimple-range.h"
38 gimple_ranger::gimple_ranger ()
40 // If the cache has a relation oracle, use it.
41 m_oracle
= m_cache
.oracle ();
45 gimple_ranger::range_of_expr (irange
&r
, tree expr
, gimple
*stmt
)
47 if (!gimple_range_ssa_p (expr
))
48 return get_tree_range (r
, expr
, stmt
);
50 // If there is no statement, just get the global value.
53 if (!m_cache
.get_global_range (r
, expr
))
54 r
= gimple_range_global (expr
);
58 // For a debug stmt, pick the best value currently available, do not
59 // trigger new value calculations. PR 100781.
60 if (is_gimple_debug (stmt
))
62 m_cache
.range_of_expr (r
, expr
, stmt
);
65 basic_block bb
= gimple_bb (stmt
);
66 gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
68 // If name is defined in this block, try to get an range from S.
69 if (def_stmt
&& gimple_bb (def_stmt
) == bb
)
71 range_of_stmt (r
, def_stmt
, expr
);
72 if (!cfun
->can_throw_non_call_exceptions
&& r
.varying_p () &&
73 m_cache
.m_non_null
.non_null_deref_p (expr
, bb
))
74 r
= range_nonzero (TREE_TYPE (expr
));
77 // Otherwise OP comes from outside this block, use range on entry.
78 range_on_entry (r
, bb
, expr
);
83 // Return the range of NAME on entry to block BB in R.
86 gimple_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
88 int_range_max entry_range
;
89 gcc_checking_assert (gimple_range_ssa_p (name
));
91 // Start with any known range
92 range_of_stmt (r
, SSA_NAME_DEF_STMT (name
), name
);
94 // Now see if there is any on_entry value which may refine it.
95 if (m_cache
.block_range (entry_range
, bb
, name
))
96 r
.intersect (entry_range
);
98 if (!cfun
->can_throw_non_call_exceptions
&& r
.varying_p () &&
99 m_cache
.m_non_null
.non_null_deref_p (name
, bb
))
100 r
= range_nonzero (TREE_TYPE (name
));
103 // Calculate the range for NAME at the end of block BB and return it in R.
104 // Return false if no range can be calculated.
107 gimple_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
109 // on-exit from the exit block?
110 gcc_checking_assert (bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
111 gcc_checking_assert (gimple_range_ssa_p (name
));
113 gimple
*s
= SSA_NAME_DEF_STMT (name
);
114 basic_block def_bb
= gimple_bb (s
);
115 // If this is not the definition block, get the range on the last stmt in
116 // the block... if there is one.
119 // If there is no statement provided, get the range_on_entry for this block.
121 range_of_expr (r
, name
, s
);
123 range_on_entry (r
, bb
, name
);
124 gcc_checking_assert (r
.undefined_p ()
125 || range_compatible_p (r
.type (), TREE_TYPE (name
)));
128 // Calculate a range for NAME on edge E and return it in R.
131 gimple_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
133 int_range_max edge_range
;
134 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name
)));
136 // PHI arguments can be constants, catch these here.
137 if (!gimple_range_ssa_p (name
))
138 return range_of_expr (r
, name
);
140 range_on_exit (r
, e
->src
, name
);
141 gcc_checking_assert (r
.undefined_p ()
142 || range_compatible_p (r
.type(), TREE_TYPE (name
)));
144 // Check to see if NAME is defined on edge e.
145 if (m_cache
.range_on_edge (edge_range
, e
, name
))
146 r
.intersect (edge_range
);
151 // fold_range wrapper for range_of_stmt to use as an internal client.
154 gimple_ranger::fold_range_internal (irange
&r
, gimple
*s
, tree name
)
157 fur_depend
src (s
, &(gori ()), this);
158 return f
.fold_stmt (r
, s
, src
, name
);
161 // Calculate a range for statement S and return it in R. If NAME is
162 // provided it represents the SSA_NAME on the LHS of the statement.
163 // It is only required if there is more than one lhs/output. Check
164 // the global cache for NAME first to see if the evaluation can be
165 // avoided. If a range cannot be calculated, return false and UNDEFINED.
168 gimple_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
173 name
= gimple_get_lhs (s
);
175 // If no name, simply call the base routine.
177 return fold_range_internal (r
, s
, NULL_TREE
);
179 if (!gimple_range_ssa_p (name
))
182 // Check if the stmt has already been processed, and is not stale.
183 if (m_cache
.get_non_stale_global_range (r
, name
))
186 // Otherwise calculate a new value.
188 fold_range_internal (tmp
, s
, name
);
190 // Combine the new value with the old value. This is required because
191 // the way value propagation works, when the IL changes on the fly we
192 // can sometimes get different results. See PR 97741.
194 m_cache
.set_global_range (name
, r
);
199 // This routine will export whatever global ranges are known to GCC
200 // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
203 gimple_ranger::export_global_ranges ()
209 fprintf (dump_file
, "Exported global range table\n");
210 fprintf (dump_file
, "===========================\n");
213 for ( x
= 1; x
< num_ssa_names
; x
++)
215 tree name
= ssa_name (x
);
216 if (name
&& !SSA_NAME_IN_FREE_LIST (name
)
217 && gimple_range_ssa_p (name
)
218 && m_cache
.get_global_range (r
, name
)
221 bool updated
= update_global_range (r
, name
);
223 if (updated
&& dump_file
)
226 print_generic_expr (dump_file
, name
, TDF_SLIM
);
227 fprintf (dump_file
, " --> ");
229 fprintf (dump_file
, "\n");
230 int_range_max same
= vr
;
233 fprintf (dump_file
, " irange : ");
235 fprintf (dump_file
, "\n");
242 // Print the known table values to file F.
245 gimple_ranger::dump_bb (FILE *f
, basic_block bb
)
251 fprintf (f
, "\n=========== BB %d ============\n", bb
->index
);
252 m_cache
.dump_bb (f
, bb
);
254 ::dump_bb (f
, bb
, 4, TDF_NONE
);
256 // Now find any globals defined in this block.
257 for (x
= 1; x
< num_ssa_names
; x
++)
259 tree name
= ssa_name (x
);
260 if (gimple_range_ssa_p (name
) && SSA_NAME_DEF_STMT (name
) &&
261 gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
&&
262 m_cache
.get_global_range (range
, name
))
264 if (!range
.varying_p ())
266 print_generic_expr (f
, name
, TDF_SLIM
);
275 // And now outgoing edges, if they define anything.
276 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
278 for (x
= 1; x
< num_ssa_names
; x
++)
280 tree name
= gimple_range_ssa_p (ssa_name (x
));
281 if (name
&& gori ().has_edge_range_p (name
, e
)
282 && m_cache
.range_on_edge (range
, e
, name
))
284 gimple
*s
= SSA_NAME_DEF_STMT (name
);
285 // Only print the range if this is the def block, or
286 // the on entry cache for either end of the edge is
288 if ((s
&& bb
== gimple_bb (s
)) ||
289 m_cache
.block_range (range
, bb
, name
, false) ||
290 m_cache
.block_range (range
, e
->dest
, name
, false))
292 m_cache
.range_on_edge (range
, e
, name
);
293 if (!range
.varying_p ())
295 fprintf (f
, "%d->%d ", e
->src
->index
,
298 if (e
->flags
& EDGE_TRUE_VALUE
)
299 fprintf (f
, " (T)%c", c
);
300 else if (e
->flags
& EDGE_FALSE_VALUE
)
301 fprintf (f
, " (F)%c", c
);
304 print_generic_expr (f
, name
, TDF_SLIM
);
315 // Print the known table values to file F.
318 gimple_ranger::dump (FILE *f
)
322 FOR_EACH_BB_FN (bb
, cfun
)
328 // trace_ranger implementation.
331 trace_ranger::trace_ranger ()
337 // If dumping, return true and print the prefix for the next output line.
340 trace_ranger::dumping (unsigned counter
, bool trailing
)
342 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
344 // Print counter index as well as INDENT spaces.
346 fprintf (dump_file
, " %-7u ", counter
);
348 fprintf (dump_file
, " ");
350 for (x
= 0; x
< indent
; x
++)
351 fputc (' ', dump_file
);
357 // After calling a routine, if dumping, print the CALLER, NAME, and RESULT,
361 trace_ranger::trailer (unsigned counter
, const char *caller
, bool result
,
362 tree name
, const irange
&r
)
364 if (dumping (counter
, true))
367 fputs(result
? "TRUE : " : "FALSE : ", dump_file
);
368 fprintf (dump_file
, "(%u) ", counter
);
369 fputs (caller
, dump_file
);
370 fputs (" (",dump_file
);
372 print_generic_expr (dump_file
, name
, TDF_SLIM
);
373 fputs (") ",dump_file
);
377 fputc('\n', dump_file
);
380 fputc('\n', dump_file
);
381 // Marks the end of a request.
383 fputc('\n', dump_file
);
388 // Tracing version of range_on_edge. Call it with printing wrappers.
391 trace_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
393 unsigned idx
= ++trace_count
;
396 fprintf (dump_file
, "range_on_edge (");
397 print_generic_expr (dump_file
, name
, TDF_SLIM
);
398 fprintf (dump_file
, ") on edge %d->%d\n", e
->src
->index
, e
->dest
->index
);
402 bool res
= gimple_ranger::range_on_edge (r
, e
, name
);
403 trailer (idx
, "range_on_edge", true, name
, r
);
407 // Tracing version of range_on_entry. Call it with printing wrappers.
410 trace_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
412 unsigned idx
= ++trace_count
;
415 fprintf (dump_file
, "range_on_entry (");
416 print_generic_expr (dump_file
, name
, TDF_SLIM
);
417 fprintf (dump_file
, ") to BB %d\n", bb
->index
);
421 gimple_ranger::range_on_entry (r
, bb
, name
);
423 trailer (idx
, "range_on_entry", true, name
, r
);
426 // Tracing version of range_on_exit. Call it with printing wrappers.
429 trace_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
431 unsigned idx
= ++trace_count
;
434 fprintf (dump_file
, "range_on_exit (");
435 print_generic_expr (dump_file
, name
, TDF_SLIM
);
436 fprintf (dump_file
, ") from BB %d\n", bb
->index
);
440 gimple_ranger::range_on_exit (r
, bb
, name
);
442 trailer (idx
, "range_on_exit", true, name
, r
);
445 // Tracing version of range_of_stmt. Call it with printing wrappers.
448 trace_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
451 unsigned idx
= ++trace_count
;
454 fprintf (dump_file
, "range_of_stmt (");
456 print_generic_expr (dump_file
, name
, TDF_SLIM
);
457 fputs (") at stmt ", dump_file
);
458 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
462 res
= gimple_ranger::range_of_stmt (r
, s
, name
);
464 return trailer (idx
, "range_of_stmt", res
, name
, r
);
467 // Tracing version of range_of_expr. Call it with printing wrappers.
470 trace_ranger::range_of_expr (irange
&r
, tree name
, gimple
*s
)
473 unsigned idx
= ++trace_count
;
476 fprintf (dump_file
, "range_of_expr(");
477 print_generic_expr (dump_file
, name
, TDF_SLIM
);
478 fputs (")", dump_file
);
481 fputs (" at stmt ", dump_file
);
482 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
485 fputs ("\n", dump_file
);
489 res
= gimple_ranger::range_of_expr (r
, name
, s
);
491 return trailer (idx
, "range_of_expr", res
, name
, r
);
495 enable_ranger (struct function
*fun
)
499 if (param_evrp_mode
& EVRP_MODE_TRACE
)
500 r
= new trace_ranger
;
502 r
= new gimple_ranger
;
504 fun
->x_range_query
= r
;
510 disable_ranger (struct function
*fun
)
512 delete fun
->x_range_query
;
514 fun
->x_range_query
= &global_ranges
;
517 // =========================================
518 // Debugging helpers.
519 // =========================================
521 // Query all statements in the IL to precalculate computable ranges in RANGER.
523 static DEBUG_FUNCTION
void
524 debug_seed_ranger (gimple_ranger
&ranger
)
526 // Recalculate SCEV to make sure the dump lists everything.
527 if (scev_initialized_p ())
535 gimple_stmt_iterator gsi
;
536 FOR_EACH_BB_FN (bb
, cfun
)
537 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
539 gimple
*stmt
= gsi_stmt (gsi
);
541 if (is_gimple_debug (stmt
))
544 ranger
.range_of_stmt (r
, stmt
);
548 // Dump all that ranger knows for the current function.
551 dump_ranger (FILE *out
)
553 gimple_ranger ranger
;
554 debug_seed_ranger (ranger
);
561 dump_ranger (stderr
);
564 // Dump all that ranger knows on a path of BBs.
566 // Note that the blocks are in reverse order, thus the exit block is
570 dump_ranger (FILE *dump_file
, const vec
<basic_block
> &path
)
572 if (path
.length () == 0)
574 fprintf (dump_file
, "empty\n");
578 gimple_ranger ranger
;
579 debug_seed_ranger (ranger
);
581 unsigned i
= path
.length ();
585 ranger
.dump_bb (dump_file
, path
[i
]);
591 debug_ranger (const vec
<basic_block
> &path
)
593 dump_ranger (stderr
, path
);
596 #include "gimple-range-tests.cc"