1 /* Support routines for value queries.
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com> and
4 Andrew MacLeod <amacleod@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 "tree-pretty-print.h"
30 #include "fold-const.h"
31 #include "value-range-equiv.h"
32 #include "value-query.h"
33 #include "alloc-pool.h"
34 #include "gimple-range.h"
35 #include "value-range-storage.h"
37 // value_query default methods.
40 value_query::value_on_edge (edge
, tree expr
)
42 return value_of_expr (expr
);
46 value_query::value_of_stmt (gimple
*stmt
, tree name
)
49 name
= gimple_get_lhs (stmt
);
51 gcc_checking_assert (!name
|| name
== gimple_get_lhs (stmt
));
54 return value_of_expr (name
);
58 // range_query default methods.
61 range_query::range_on_edge (vrange
&r
, edge
, tree expr
)
63 return range_of_expr (r
, expr
);
67 range_query::range_of_stmt (vrange
&r
, gimple
*stmt
, tree name
)
70 name
= gimple_get_lhs (stmt
);
72 gcc_checking_assert (!name
|| name
== gimple_get_lhs (stmt
));
75 return range_of_expr (r
, name
);
80 range_query::value_of_expr (tree expr
, gimple
*stmt
)
84 if (!Value_Range::supports_type_p (TREE_TYPE (expr
)))
87 Value_Range
r (TREE_TYPE (expr
));
89 if (range_of_expr (r
, expr
, stmt
))
91 // A constant used in an unreachable block oftens returns as UNDEFINED.
92 // If the result is undefined, check the global value for a constant.
94 range_of_expr (r
, expr
);
95 if (r
.singleton_p (&t
))
102 range_query::value_on_edge (edge e
, tree expr
)
106 if (!Value_Range::supports_type_p (TREE_TYPE (expr
)))
108 Value_Range
r (TREE_TYPE (expr
));
109 if (range_on_edge (r
, e
, expr
))
111 // A constant used in an unreachable block oftens returns as UNDEFINED.
112 // If the result is undefined, check the global value for a constant.
113 if (r
.undefined_p ())
114 range_of_expr (r
, expr
);
115 if (r
.singleton_p (&t
))
123 range_query::value_of_stmt (gimple
*stmt
, tree name
)
128 name
= gimple_get_lhs (stmt
);
130 gcc_checking_assert (!name
|| name
== gimple_get_lhs (stmt
));
132 if (!name
|| !Value_Range::supports_type_p (TREE_TYPE (name
)))
134 Value_Range
r (TREE_TYPE (name
));
135 if (range_of_stmt (r
, stmt
, name
) && r
.singleton_p (&t
))
142 range_query::dump (FILE *)
146 // valuation_query support routines for value_range_equiv's.
148 class equiv_allocator
: public object_allocator
<value_range_equiv
>
152 : object_allocator
<value_range_equiv
> ("equiv_allocator pool") { }
156 range_query::allocate_value_range_equiv ()
158 return new (equiv_alloc
->allocate ()) value_range_equiv
;
162 range_query::free_value_range_equiv (value_range_equiv
*v
)
164 equiv_alloc
->remove (v
);
167 const class value_range_equiv
*
168 range_query::get_value_range (const_tree expr
, gimple
*stmt
)
171 if (range_of_expr (r
, const_cast<tree
> (expr
), stmt
))
172 return new (equiv_alloc
->allocate ()) value_range_equiv (r
);
173 return new (equiv_alloc
->allocate ()) value_range_equiv (TREE_TYPE (expr
));
176 range_query::range_query ()
178 equiv_alloc
= new equiv_allocator
;
182 range_query::~range_query ()
184 equiv_alloc
->release ();
188 // Return a range in R for the tree EXPR. Return true if a range is
189 // representable, and UNDEFINED/false if not.
192 range_query::get_tree_range (vrange
&r
, tree expr
, gimple
*stmt
)
198 type
= TREE_TYPE (expr
);
200 if (!Value_Range::supports_type_p (type
))
207 r
.set_varying (type
);
210 switch (TREE_CODE (expr
))
213 if (TREE_OVERFLOW_P (expr
))
214 expr
= drop_tree_overflow (expr
);
220 frange
&f
= as_a
<frange
> (r
);
222 if (!real_isnan (TREE_REAL_CST_PTR (expr
)))
228 gimple_range_global (r
, expr
);
233 // Handle &var which can show up in phi arguments.
235 if (tree_single_nonzero_warnv_p (expr
, &ov
))
237 r
.set_nonzero (type
);
246 if (BINARY_CLASS_P (expr
))
248 range_op_handler
op (TREE_CODE (expr
), type
);
251 Value_Range
r0 (TREE_TYPE (TREE_OPERAND (expr
, 0)));
252 Value_Range
r1 (TREE_TYPE (TREE_OPERAND (expr
, 1)));
253 range_of_expr (r0
, TREE_OPERAND (expr
, 0), stmt
);
254 range_of_expr (r1
, TREE_OPERAND (expr
, 1), stmt
);
255 if (!op
.fold_range (r
, type
, r0
, r1
))
256 r
.set_varying (type
);
259 r
.set_varying (type
);
262 if (UNARY_CLASS_P (expr
))
264 range_op_handler
op (TREE_CODE (expr
), type
);
265 tree op0_type
= TREE_TYPE (TREE_OPERAND (expr
, 0));
266 if (op
&& Value_Range::supports_type_p (op0_type
))
268 Value_Range
r0 (TREE_TYPE (TREE_OPERAND (expr
, 0)));
269 Value_Range
r1 (type
);
270 r1
.set_varying (type
);
271 range_of_expr (r0
, TREE_OPERAND (expr
, 0), stmt
);
272 if (!op
.fold_range (r
, type
, r0
, r1
))
273 r
.set_varying (type
);
276 r
.set_varying (type
);
279 r
.set_varying (type
);
283 // Return the range for NAME from SSA_NAME_RANGE_INFO.
286 get_ssa_name_range_info (vrange
&r
, const_tree name
)
288 tree type
= TREE_TYPE (name
);
289 gcc_checking_assert (!POINTER_TYPE_P (type
));
290 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
292 void *ri
= SSA_NAME_RANGE_INFO (name
);
296 vrange_storage
vstore (NULL
);
297 vstore
.get_vrange (ri
, r
, TREE_TYPE (name
));
300 r
.set_varying (type
);
303 // Return nonnull attribute of pointer NAME from SSA_NAME_PTR_INFO.
306 get_ssa_name_ptr_info_nonnull (const_tree name
)
308 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name
)));
309 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (name
);
312 /* TODO Now pt->null is conservatively set to true in PTA
313 analysis. vrp is the only pass (including ipa-vrp)
314 that clears pt.null via set_ptr_nonnull when it knows
315 for sure. PTA will preserves the pt.null value set by VRP.
317 When PTA analysis is improved, pt.anything, pt.nonlocal
318 and pt.escaped may also has to be considered before
319 deciding that pointer cannot point to NULL. */
323 // Update the global range for NAME into the SSA_RANGE_NAME_INFO and
324 // Return the legacy global range for NAME if it has one, otherwise
328 get_range_global (vrange
&r
, tree name
)
330 tree type
= TREE_TYPE (name
);
332 if (SSA_NAME_IS_DEFAULT_DEF (name
))
334 tree sym
= SSA_NAME_VAR (name
);
335 // Adapted from vr_values::get_lattice_entry().
336 // Use a range from an SSA_NAME's available range.
337 if (TREE_CODE (sym
) == PARM_DECL
)
339 // Try to use the "nonnull" attribute to create ~[0, 0]
340 // anti-ranges for pointers. Note that this is only valid with
341 // default definitions of PARM_DECLs.
342 if (POINTER_TYPE_P (type
)
343 && ((cfun
&& nonnull_arg_p (sym
))
344 || get_ssa_name_ptr_info_nonnull (name
)))
345 r
.set_nonzero (type
);
346 else if (INTEGRAL_TYPE_P (type
))
348 get_ssa_name_range_info (r
, name
);
349 if (r
.undefined_p ())
350 r
.set_varying (type
);
353 r
.set_varying (type
);
355 // If this is a local automatic with no definition, use undefined.
356 else if (TREE_CODE (sym
) != RESULT_DECL
)
359 r
.set_varying (type
);
361 else if (!POINTER_TYPE_P (type
) && SSA_NAME_RANGE_INFO (name
))
363 get_ssa_name_range_info (r
, name
);
364 if (r
.undefined_p ())
365 r
.set_varying (type
);
367 else if (POINTER_TYPE_P (type
) && SSA_NAME_PTR_INFO (name
))
369 if (get_ssa_name_ptr_info_nonnull (name
))
370 r
.set_nonzero (type
);
372 r
.set_varying (type
);
375 r
.set_varying (type
);
378 // This is where the ranger picks up global info to seed initial
379 // requests. It is a slightly restricted version of
380 // get_range_global() above.
382 // The reason for the difference is that we can always pick the
383 // default definition of an SSA with no adverse effects, but for other
384 // SSAs, if we pick things up to early, we may prematurely eliminate
385 // builtin_unreachables.
387 // Without this restriction, the test in g++.dg/tree-ssa/pr61034.C has
388 // all of its unreachable calls removed too early.
390 // See discussion here:
391 // https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571709.html
394 gimple_range_global (vrange
&r
, tree name
)
396 tree type
= TREE_TYPE (name
);
397 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
399 if (SSA_NAME_IS_DEFAULT_DEF (name
) || (cfun
&& cfun
->after_inlining
)
400 || is_a
<gphi
*> (SSA_NAME_DEF_STMT (name
)))
402 get_range_global (r
, name
);
405 r
.set_varying (type
);
408 // ----------------------------------------------
409 // global_range_query implementation.
411 global_range_query global_ranges
;
414 global_range_query::range_of_expr (vrange
&r
, tree expr
, gimple
*stmt
)
416 if (!gimple_range_ssa_p (expr
))
417 return get_tree_range (r
, expr
, stmt
);
419 get_range_global (r
, expr
);
424 // Return any known relation between SSA1 and SSA2 before stmt S is executed.
425 // If GET_RANGE is true, query the range of both operands first to ensure
426 // the defintions have been processed and any relations have be created.
429 range_query::query_relation (gimple
*s
, tree ssa1
, tree ssa2
, bool get_range
)
431 if (!m_oracle
|| TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
434 // Ensure ssa1 and ssa2 have both been evaluated.
437 Value_Range
tmp1 (TREE_TYPE (ssa1
));
438 Value_Range
tmp2 (TREE_TYPE (ssa2
));
439 range_of_expr (tmp1
, ssa1
, s
);
440 range_of_expr (tmp2
, ssa2
, s
);
442 return m_oracle
->query_relation (gimple_bb (s
), ssa1
, ssa2
);
445 // Return any known relation between SSA1 and SSA2 on edge E.
446 // If GET_RANGE is true, query the range of both operands first to ensure
447 // the defintions have been processed and any relations have be created.
450 range_query::query_relation (edge e
, tree ssa1
, tree ssa2
, bool get_range
)
453 if (!m_oracle
|| TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
456 // Use destination block if it has a single predecessor, and this picks
457 // up any relation on the edge.
458 // Otherwise choose the src edge and the result is the same as on-exit.
459 if (!single_pred_p (e
->dest
))
464 // Ensure ssa1 and ssa2 have both been evaluated.
467 Value_Range
tmp (TREE_TYPE (ssa1
));
468 range_on_edge (tmp
, e
, ssa1
);
469 range_on_edge (tmp
, e
, ssa2
);
471 return m_oracle
->query_relation (bb
, ssa1
, ssa2
);