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"
37 #include "gimple-fold.h"
39 gimple_ranger::gimple_ranger () :
40 non_executable_edge_flag (cfun
),
41 m_cache (non_executable_edge_flag
),
45 // If the cache has a relation oracle, use it.
46 m_oracle
= m_cache
.oracle ();
47 if (dump_file
&& (param_ranger_debug
& RANGER_DEBUG_TRACE
))
48 tracer
.enable_trace ();
49 m_stmt_list
.create (0);
50 m_stmt_list
.safe_grow (num_ssa_names
);
51 m_stmt_list
.truncate (0);
53 // Ensure the not_executable flag is clear everywhere.
57 FOR_ALL_BB_FN (bb
, cfun
)
61 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
62 gcc_checking_assert ((e
->flags
& non_executable_edge_flag
) == 0);
67 gimple_ranger::~gimple_ranger ()
69 m_stmt_list
.release ();
73 gimple_ranger::range_of_expr (irange
&r
, tree expr
, gimple
*stmt
)
76 if (!gimple_range_ssa_p (expr
))
77 return get_tree_range (r
, expr
, stmt
);
79 if ((idx
= tracer
.header ("range_of_expr(")))
81 print_generic_expr (dump_file
, expr
, TDF_SLIM
);
82 fputs (")", dump_file
);
85 fputs (" at stmt ", dump_file
);
86 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
89 fputs ("\n", dump_file
);
92 // If there is no statement, just get the global value.
96 m_cache
.get_global_range (r
, expr
);
97 // Pick up implied context information from the on-entry cache
98 // if current_bb is set. Do not attempt any new calculations.
99 if (current_bb
&& m_cache
.block_range (tmp
, current_bb
, expr
, false))
103 sprintf (str
, "picked up range from bb %d\n",current_bb
->index
);
105 tracer
.print (idx
, str
);
108 // For a debug stmt, pick the best value currently available, do not
109 // trigger new value calculations. PR 100781.
110 else if (is_gimple_debug (stmt
))
111 m_cache
.range_of_expr (r
, expr
, stmt
);
114 basic_block bb
= gimple_bb (stmt
);
115 gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
117 // If name is defined in this block, try to get an range from S.
118 if (def_stmt
&& gimple_bb (def_stmt
) == bb
)
120 range_of_stmt (r
, def_stmt
, expr
);
121 m_cache
.m_non_null
.adjust_range (r
, expr
, bb
, true);
123 // Otherwise OP comes from outside this block, use range on entry.
125 range_on_entry (r
, bb
, expr
);
128 tracer
.trailer (idx
, "range_of_expr", true, expr
, r
);
132 // Return the range of NAME on entry to block BB in R.
135 gimple_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
137 int_range_max entry_range
;
138 gcc_checking_assert (gimple_range_ssa_p (name
));
141 if ((idx
= tracer
.header ("range_on_entry (")))
143 print_generic_expr (dump_file
, name
, TDF_SLIM
);
144 fprintf (dump_file
, ") to BB %d\n", bb
->index
);
147 // Start with any known range
148 range_of_stmt (r
, SSA_NAME_DEF_STMT (name
), name
);
150 // Now see if there is any on_entry value which may refine it.
151 if (m_cache
.block_range (entry_range
, bb
, name
))
152 r
.intersect (entry_range
);
154 m_cache
.m_non_null
.adjust_range (r
, name
, bb
, true);
157 tracer
.trailer (idx
, "range_on_entry", true, name
, r
);
160 // Calculate the range for NAME at the end of block BB and return it in R.
161 // Return false if no range can be calculated.
164 gimple_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
166 // on-exit from the exit block?
167 gcc_checking_assert (bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
168 gcc_checking_assert (gimple_range_ssa_p (name
));
171 if ((idx
= tracer
.header ("range_on_exit (")))
173 print_generic_expr (dump_file
, name
, TDF_SLIM
);
174 fprintf (dump_file
, ") from BB %d\n", bb
->index
);
177 gimple
*s
= SSA_NAME_DEF_STMT (name
);
178 basic_block def_bb
= gimple_bb (s
);
179 // If this is not the definition block, get the range on the last stmt in
180 // the block... if there is one.
183 // If there is no statement provided, get the range_on_entry for this block.
185 range_of_expr (r
, name
, s
);
187 range_on_entry (r
, bb
, name
);
188 gcc_checking_assert (r
.undefined_p ()
189 || range_compatible_p (r
.type (), TREE_TYPE (name
)));
192 tracer
.trailer (idx
, "range_on_exit", true, name
, r
);
195 // Calculate a range for NAME on edge E and return it in R.
198 gimple_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
200 int_range_max edge_range
;
201 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name
)));
203 // Do not process values along abnormal edges.
204 if (e
->flags
& EDGE_ABNORMAL
)
205 return get_tree_range (r
, name
, NULL
);
208 if ((idx
= tracer
.header ("range_on_edge (")))
210 print_generic_expr (dump_file
, name
, TDF_SLIM
);
211 fprintf (dump_file
, ") on edge %d->%d\n", e
->src
->index
, e
->dest
->index
);
214 // Check to see if the edge is executable.
215 if ((e
->flags
& non_executable_edge_flag
))
219 tracer
.trailer (idx
, "range_on_edge [Unexecutable] ", true,
225 if (!gimple_range_ssa_p (name
))
226 res
= get_tree_range (r
, name
, NULL
);
229 range_on_exit (r
, e
->src
, name
);
230 gcc_checking_assert (r
.undefined_p ()
231 || range_compatible_p (r
.type(), TREE_TYPE (name
)));
233 // Check to see if NAME is defined on edge e.
234 if (m_cache
.range_on_edge (edge_range
, e
, name
))
235 r
.intersect (edge_range
);
239 tracer
.trailer (idx
, "range_on_edge", res
, name
, r
);
243 // fold_range wrapper for range_of_stmt to use as an internal client.
246 gimple_ranger::fold_range_internal (irange
&r
, gimple
*s
, tree name
)
249 fur_depend
src (s
, &(gori ()), this);
250 return f
.fold_stmt (r
, s
, src
, name
);
253 // Calculate a range for statement S and return it in R. If NAME is
254 // provided it represents the SSA_NAME on the LHS of the statement.
255 // It is only required if there is more than one lhs/output. Check
256 // the global cache for NAME first to see if the evaluation can be
257 // avoided. If a range cannot be calculated, return false and UNDEFINED.
260 gimple_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
266 if ((idx
= tracer
.header ("range_of_stmt (")))
269 print_generic_expr (dump_file
, name
, TDF_SLIM
);
270 fputs (") at stmt ", dump_file
);
271 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
275 name
= gimple_get_lhs (s
);
277 // If no name, simply call the base routine.
280 res
= fold_range_internal (r
, s
, NULL_TREE
);
281 if (res
&& is_a
<gcond
*> (s
))
283 // Update any exports in the cache if this is a gimple cond statement.
285 basic_block bb
= gimple_bb (s
);
286 FOR_EACH_GORI_EXPORT_NAME (m_cache
.m_gori
, bb
, exp
)
287 m_cache
.propagate_updated_value (exp
, bb
);
290 else if (!gimple_range_ssa_p (name
))
291 res
= get_tree_range (r
, name
, NULL
);
295 // Check if the stmt has already been processed.
296 if (m_cache
.get_global_range (r
, name
, current
))
298 // If it isn't stale, use this cached value.
302 tracer
.trailer (idx
, " cached", true, name
, r
);
307 prefill_stmt_dependencies (name
);
309 // Calculate a new value.
311 fold_range_internal (tmp
, s
, name
);
313 // Combine the new value with the old value. This is required because
314 // the way value propagation works, when the IL changes on the fly we
315 // can sometimes get different results. See PR 97741.
317 m_cache
.set_global_range (name
, r
);
322 tracer
.trailer (idx
, "range_of_stmt", res
, name
, r
);
327 // Check if NAME is a dependency that needs resolving, and push it on the
328 // stack if so. R is a scratch range.
331 gimple_ranger::prefill_name (irange
&r
, tree name
)
333 if (!gimple_range_ssa_p (name
))
335 gimple
*stmt
= SSA_NAME_DEF_STMT (name
);
336 if (!gimple_range_handler (stmt
) && !is_a
<gphi
*> (stmt
))
340 // If this op has not been processed yet, then push it on the stack
341 if (!m_cache
.get_global_range (r
, name
, current
))
342 m_stmt_list
.safe_push (name
);
345 // This routine will seed the global cache with most of the depnedencies of
346 // NAME. This prevents excessive call depth through the normal API.
349 gimple_ranger::prefill_stmt_dependencies (tree ssa
)
351 if (SSA_NAME_IS_DEFAULT_DEF (ssa
))
356 gimple
*stmt
= SSA_NAME_DEF_STMT (ssa
);
357 gcc_checking_assert (stmt
&& gimple_bb (stmt
));
359 // Only pre-process range-ops and phis.
360 if (!gimple_range_handler (stmt
) && !is_a
<gphi
*> (stmt
))
363 // Mark where on the stack we are starting.
364 unsigned start
= m_stmt_list
.length ();
365 m_stmt_list
.safe_push (ssa
);
367 idx
= tracer
.header ("ROS dependence fill\n");
369 // Loop until back at the start point.
370 while (m_stmt_list
.length () > start
)
372 tree name
= m_stmt_list
.last ();
373 // NULL is a marker which indicates the next name in the stack has now
374 // been fully resolved, so we can fold it.
377 // Pop the NULL, then pop the name.
379 name
= m_stmt_list
.pop ();
380 // Don't fold initial request, it will be calculated upon return.
381 if (m_stmt_list
.length () > start
)
383 // Fold and save the value for NAME.
384 stmt
= SSA_NAME_DEF_STMT (name
);
385 fold_range_internal (r
, stmt
, name
);
386 m_cache
.set_global_range (name
, r
);
391 // Add marker indicating previous NAME in list should be folded
392 // when we get to this NULL.
393 m_stmt_list
.safe_push (NULL_TREE
);
394 stmt
= SSA_NAME_DEF_STMT (name
);
398 tracer
.print (idx
, "ROS dep fill (");
399 print_generic_expr (dump_file
, name
, TDF_SLIM
);
400 fputs (") at stmt ", dump_file
);
401 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
404 gphi
*phi
= dyn_cast
<gphi
*> (stmt
);
407 for (unsigned x
= 0; x
< gimple_phi_num_args (phi
); x
++)
408 prefill_name (r
, gimple_phi_arg_def (phi
, x
));
412 gcc_checking_assert (gimple_range_handler (stmt
));
413 tree op
= gimple_range_operand2 (stmt
);
415 prefill_name (r
, op
);
416 op
= gimple_range_operand1 (stmt
);
418 prefill_name (r
, op
);
422 tracer
.trailer (idx
, "ROS ", false, ssa
, r
);
426 // This routine will invoke the gimple fold_stmt routine, providing context to
427 // range_of_expr calls via an private interal API.
430 gimple_ranger::fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
432 gimple
*stmt
= gsi_stmt (*gsi
);
433 current_bb
= gimple_bb (stmt
);
434 bool ret
= ::fold_stmt (gsi
, valueize
);
439 // This routine will export whatever global ranges are known to GCC
440 // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
443 gimple_ranger::export_global_ranges ()
445 /* Cleared after the table header has been printed. */
446 bool print_header
= true;
447 for (unsigned x
= 1; x
< num_ssa_names
; x
++)
450 tree name
= ssa_name (x
);
451 if (name
&& !SSA_NAME_IN_FREE_LIST (name
)
452 && gimple_range_ssa_p (name
)
453 && m_cache
.get_global_range (r
, name
)
456 bool updated
= update_global_range (r
, name
);
457 if (!updated
|| !dump_file
)
462 /* Print the header only when there's something else
464 fprintf (dump_file
, "Exported global range table:\n");
465 fprintf (dump_file
, "============================\n");
466 print_header
= false;
470 print_generic_expr (dump_file
, name
, TDF_SLIM
);
471 fprintf (dump_file
, " : ");
473 fprintf (dump_file
, "\n");
474 int_range_max same
= vr
;
477 fprintf (dump_file
, " irange : ");
479 fprintf (dump_file
, "\n");
485 // Print the known table values to file F.
488 gimple_ranger::dump_bb (FILE *f
, basic_block bb
)
493 int_range_max range
, tmp_range
;
494 fprintf (f
, "\n=========== BB %d ============\n", bb
->index
);
495 m_cache
.dump_bb (f
, bb
);
497 ::dump_bb (f
, bb
, 4, TDF_NONE
);
499 // Now find any globals defined in this block.
500 for (x
= 1; x
< num_ssa_names
; x
++)
502 tree name
= ssa_name (x
);
503 if (gimple_range_ssa_p (name
) && SSA_NAME_DEF_STMT (name
) &&
504 gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
&&
505 m_cache
.get_global_range (range
, name
))
507 if (!range
.varying_p ())
509 print_generic_expr (f
, name
, TDF_SLIM
);
518 // And now outgoing edges, if they define anything.
519 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
521 for (x
= 1; x
< num_ssa_names
; x
++)
523 tree name
= gimple_range_ssa_p (ssa_name (x
));
524 if (name
&& gori ().has_edge_range_p (name
, e
)
525 && m_cache
.range_on_edge (range
, e
, name
))
527 gimple
*s
= SSA_NAME_DEF_STMT (name
);
528 // Only print the range if this is the def block, or
529 // the on entry cache for either end of the edge is
531 if ((s
&& bb
== gimple_bb (s
)) ||
532 m_cache
.block_range (tmp_range
, bb
, name
, false) ||
533 m_cache
.block_range (tmp_range
, e
->dest
, name
, false))
535 if (!range
.varying_p ())
537 fprintf (f
, "%d->%d ", e
->src
->index
,
540 if (e
->flags
& EDGE_TRUE_VALUE
)
541 fprintf (f
, " (T)%c", c
);
542 else if (e
->flags
& EDGE_FALSE_VALUE
)
543 fprintf (f
, " (F)%c", c
);
546 print_generic_expr (f
, name
, TDF_SLIM
);
557 // Print the known table values to file F.
560 gimple_ranger::dump (FILE *f
)
564 FOR_EACH_BB_FN (bb
, cfun
)
571 gimple_ranger::debug ()
576 /* Create a new ranger instance and associate it with function FUN.
577 Each call must be paired with a call to disable_ranger to release
581 enable_ranger (struct function
*fun
)
585 gcc_checking_assert (!fun
->x_range_query
);
586 r
= new gimple_ranger
;
587 fun
->x_range_query
= r
;
592 /* Destroy and release the ranger instance associated with function FUN
593 and replace it the global ranger. */
596 disable_ranger (struct function
*fun
)
598 gcc_checking_assert (fun
->x_range_query
);
599 delete fun
->x_range_query
;
600 fun
->x_range_query
= NULL
;