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 ();
50 // Ensure the not_executable flag is clear everywhere.
54 FOR_ALL_BB_FN (bb
, cfun
)
58 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
59 gcc_checking_assert ((e
->flags
& non_executable_edge_flag
) == 0);
65 gimple_ranger::range_of_expr (irange
&r
, tree expr
, gimple
*stmt
)
68 if (!gimple_range_ssa_p (expr
))
69 return get_tree_range (r
, expr
, stmt
);
71 if ((idx
= tracer
.header ("range_of_expr(")))
73 print_generic_expr (dump_file
, expr
, TDF_SLIM
);
74 fputs (")", dump_file
);
77 fputs (" at stmt ", dump_file
);
78 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
81 fputs ("\n", dump_file
);
84 // If there is no statement, just get the global value.
88 if (!m_cache
.get_global_range (r
, expr
))
89 r
= gimple_range_global (expr
);
90 // Pick up implied context information from the on-entry cache
91 // if current_bb is set.
92 if (current_bb
&& m_cache
.block_range (tmp
, current_bb
, expr
))
96 sprintf (str
, "picked up range from bb %d\n",current_bb
->index
);
98 tracer
.print (idx
, str
);
101 // For a debug stmt, pick the best value currently available, do not
102 // trigger new value calculations. PR 100781.
103 else if (is_gimple_debug (stmt
))
104 m_cache
.range_of_expr (r
, expr
, stmt
);
107 basic_block bb
= gimple_bb (stmt
);
108 gimple
*def_stmt
= SSA_NAME_DEF_STMT (expr
);
110 // If name is defined in this block, try to get an range from S.
111 if (def_stmt
&& gimple_bb (def_stmt
) == bb
)
113 range_of_stmt (r
, def_stmt
, expr
);
114 m_cache
.m_non_null
.adjust_range (r
, expr
, bb
, true);
116 // Otherwise OP comes from outside this block, use range on entry.
118 range_on_entry (r
, bb
, expr
);
121 tracer
.trailer (idx
, "range_of_expr", true, expr
, r
);
125 // Return the range of NAME on entry to block BB in R.
128 gimple_ranger::range_on_entry (irange
&r
, basic_block bb
, tree name
)
130 int_range_max entry_range
;
131 gcc_checking_assert (gimple_range_ssa_p (name
));
134 if ((idx
= tracer
.header ("range_on_entry (")))
136 print_generic_expr (dump_file
, name
, TDF_SLIM
);
137 fprintf (dump_file
, ") to BB %d\n", bb
->index
);
140 // Start with any known range
141 range_of_stmt (r
, SSA_NAME_DEF_STMT (name
), name
);
143 // Now see if there is any on_entry value which may refine it.
144 if (m_cache
.block_range (entry_range
, bb
, name
))
145 r
.intersect (entry_range
);
147 m_cache
.m_non_null
.adjust_range (r
, name
, bb
, true);
150 tracer
.trailer (idx
, "range_on_entry", true, name
, r
);
153 // Calculate the range for NAME at the end of block BB and return it in R.
154 // Return false if no range can be calculated.
157 gimple_ranger::range_on_exit (irange
&r
, basic_block bb
, tree name
)
159 // on-exit from the exit block?
160 gcc_checking_assert (bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
));
161 gcc_checking_assert (gimple_range_ssa_p (name
));
164 if ((idx
= tracer
.header ("range_on_exit (")))
166 print_generic_expr (dump_file
, name
, TDF_SLIM
);
167 fprintf (dump_file
, ") from BB %d\n", bb
->index
);
170 gimple
*s
= SSA_NAME_DEF_STMT (name
);
171 basic_block def_bb
= gimple_bb (s
);
172 // If this is not the definition block, get the range on the last stmt in
173 // the block... if there is one.
176 // If there is no statement provided, get the range_on_entry for this block.
178 range_of_expr (r
, name
, s
);
180 range_on_entry (r
, bb
, name
);
181 gcc_checking_assert (r
.undefined_p ()
182 || range_compatible_p (r
.type (), TREE_TYPE (name
)));
185 tracer
.trailer (idx
, "range_on_exit", true, name
, r
);
188 // Calculate a range for NAME on edge E and return it in R.
191 gimple_ranger::range_on_edge (irange
&r
, edge e
, tree name
)
193 int_range_max edge_range
;
194 gcc_checking_assert (irange::supports_type_p (TREE_TYPE (name
)));
196 // Do not process values along abnormal edges.
197 if (e
->flags
& EDGE_ABNORMAL
)
198 return get_tree_range (r
, name
, NULL
);
201 if ((idx
= tracer
.header ("range_on_edge (")))
203 print_generic_expr (dump_file
, name
, TDF_SLIM
);
204 fprintf (dump_file
, ") on edge %d->%d\n", e
->src
->index
, e
->dest
->index
);
207 // Check to see if the edge is executable.
208 if ((e
->flags
& non_executable_edge_flag
))
212 tracer
.trailer (idx
, "range_on_edge [Unexecutable] ", true,
218 if (!gimple_range_ssa_p (name
))
219 return get_tree_range (r
, name
, NULL
);
222 range_on_exit (r
, e
->src
, name
);
223 gcc_checking_assert (r
.undefined_p ()
224 || range_compatible_p (r
.type(), TREE_TYPE (name
)));
226 // Check to see if NAME is defined on edge e.
227 if (m_cache
.range_on_edge (edge_range
, e
, name
))
228 r
.intersect (edge_range
);
232 tracer
.trailer (idx
, "range_on_edge", res
, name
, r
);
236 // fold_range wrapper for range_of_stmt to use as an internal client.
239 gimple_ranger::fold_range_internal (irange
&r
, gimple
*s
, tree name
)
242 fur_depend
src (s
, &(gori ()), this);
243 return f
.fold_stmt (r
, s
, src
, name
);
246 // Calculate a range for statement S and return it in R. If NAME is
247 // provided it represents the SSA_NAME on the LHS of the statement.
248 // It is only required if there is more than one lhs/output. Check
249 // the global cache for NAME first to see if the evaluation can be
250 // avoided. If a range cannot be calculated, return false and UNDEFINED.
253 gimple_ranger::range_of_stmt (irange
&r
, gimple
*s
, tree name
)
259 if ((idx
= tracer
.header ("range_of_stmt (")))
262 print_generic_expr (dump_file
, name
, TDF_SLIM
);
263 fputs (") at stmt ", dump_file
);
264 print_gimple_stmt (dump_file
, s
, 0, TDF_SLIM
);
268 name
= gimple_get_lhs (s
);
270 // If no name, simply call the base routine.
273 res
= fold_range_internal (r
, s
, NULL_TREE
);
274 if (res
&& is_a
<gcond
*> (s
))
276 // Update any exports in the cache if this is a gimple cond statement.
278 basic_block bb
= gimple_bb (s
);
279 FOR_EACH_GORI_EXPORT_NAME (m_cache
.m_gori
, bb
, exp
)
280 m_cache
.propagate_updated_value (exp
, bb
);
283 else if (!gimple_range_ssa_p (name
))
284 res
= get_tree_range (r
, name
, NULL
);
285 // Check if the stmt has already been processed, and is not stale.
286 else if (m_cache
.get_non_stale_global_range (r
, name
))
289 tracer
.trailer (idx
, " cached", true, name
, r
);
294 // Otherwise calculate a new value.
296 fold_range_internal (tmp
, s
, name
);
298 // Combine the new value with the old value. This is required because
299 // the way value propagation works, when the IL changes on the fly we
300 // can sometimes get different results. See PR 97741.
302 m_cache
.set_global_range (name
, r
);
307 tracer
.trailer (idx
, "range_of_stmt", res
, name
, r
);
311 // This routine will invoke the gimple fold_stmt routine, providing context to
312 // range_of_expr calls via an private interal API.
315 gimple_ranger::fold_stmt (gimple_stmt_iterator
*gsi
, tree (*valueize
) (tree
))
317 gimple
*stmt
= gsi_stmt (*gsi
);
318 current_bb
= gimple_bb (stmt
);
319 bool ret
= ::fold_stmt (gsi
, valueize
);
324 // This routine will export whatever global ranges are known to GCC
325 // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
328 gimple_ranger::export_global_ranges ()
330 /* Cleared after the table header has been printed. */
331 bool print_header
= true;
332 for (unsigned x
= 1; x
< num_ssa_names
; x
++)
335 tree name
= ssa_name (x
);
336 if (name
&& !SSA_NAME_IN_FREE_LIST (name
)
337 && gimple_range_ssa_p (name
)
338 && m_cache
.get_global_range (r
, name
)
341 bool updated
= update_global_range (r
, name
);
342 if (!updated
|| !dump_file
)
347 /* Print the header only when there's something else
349 fprintf (dump_file
, "Exported global range table:\n");
350 fprintf (dump_file
, "============================\n");
351 print_header
= false;
355 print_generic_expr (dump_file
, name
, TDF_SLIM
);
356 fprintf (dump_file
, " : ");
358 fprintf (dump_file
, "\n");
359 int_range_max same
= vr
;
362 fprintf (dump_file
, " irange : ");
364 fprintf (dump_file
, "\n");
370 // Print the known table values to file F.
373 gimple_ranger::dump_bb (FILE *f
, basic_block bb
)
378 int_range_max range
, tmp_range
;
379 fprintf (f
, "\n=========== BB %d ============\n", bb
->index
);
380 m_cache
.dump_bb (f
, bb
);
382 ::dump_bb (f
, bb
, 4, TDF_NONE
);
384 // Now find any globals defined in this block.
385 for (x
= 1; x
< num_ssa_names
; x
++)
387 tree name
= ssa_name (x
);
388 if (gimple_range_ssa_p (name
) && SSA_NAME_DEF_STMT (name
) &&
389 gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
&&
390 m_cache
.get_global_range (range
, name
))
392 if (!range
.varying_p ())
394 print_generic_expr (f
, name
, TDF_SLIM
);
403 // And now outgoing edges, if they define anything.
404 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
406 for (x
= 1; x
< num_ssa_names
; x
++)
408 tree name
= gimple_range_ssa_p (ssa_name (x
));
409 if (name
&& gori ().has_edge_range_p (name
, e
)
410 && m_cache
.range_on_edge (range
, e
, name
))
412 gimple
*s
= SSA_NAME_DEF_STMT (name
);
413 // Only print the range if this is the def block, or
414 // the on entry cache for either end of the edge is
416 if ((s
&& bb
== gimple_bb (s
)) ||
417 m_cache
.block_range (tmp_range
, bb
, name
, false) ||
418 m_cache
.block_range (tmp_range
, e
->dest
, name
, false))
420 if (!range
.varying_p ())
422 fprintf (f
, "%d->%d ", e
->src
->index
,
425 if (e
->flags
& EDGE_TRUE_VALUE
)
426 fprintf (f
, " (T)%c", c
);
427 else if (e
->flags
& EDGE_FALSE_VALUE
)
428 fprintf (f
, " (F)%c", c
);
431 print_generic_expr (f
, name
, TDF_SLIM
);
442 // Print the known table values to file F.
445 gimple_ranger::dump (FILE *f
)
449 FOR_EACH_BB_FN (bb
, cfun
)
456 gimple_ranger::debug ()
461 /* Create a new ranger instance and associate it with function FUN.
462 Each call must be paired with a call to disable_ranger to release
466 enable_ranger (struct function
*fun
)
470 r
= new gimple_ranger
;
471 fun
->x_range_query
= r
;
476 /* Destroy and release the ranger instance associated with function FUN
477 and replace it the global ranger. */
480 disable_ranger (struct function
*fun
)
482 delete fun
->x_range_query
;
484 fun
->x_range_query
= &global_ranges
;