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
);
219 gimple_range_global (r
, expr
);
224 // Handle &var which can show up in phi arguments.
226 if (tree_single_nonzero_warnv_p (expr
, &ov
))
228 r
.set_nonzero (type
);
237 if (BINARY_CLASS_P (expr
))
239 range_op_handler
op (TREE_CODE (expr
), type
);
242 Value_Range
r0 (TREE_TYPE (TREE_OPERAND (expr
, 0)));
243 Value_Range
r1 (TREE_TYPE (TREE_OPERAND (expr
, 1)));
244 range_of_expr (r0
, TREE_OPERAND (expr
, 0), stmt
);
245 range_of_expr (r1
, TREE_OPERAND (expr
, 1), stmt
);
246 op
.fold_range (r
, type
, r0
, r1
);
249 r
.set_varying (type
);
252 if (UNARY_CLASS_P (expr
))
254 range_op_handler
op (TREE_CODE (expr
), type
);
255 tree op0_type
= TREE_TYPE (TREE_OPERAND (expr
, 0));
256 if (op
&& Value_Range::supports_type_p (op0_type
))
258 Value_Range
r0 (TREE_TYPE (TREE_OPERAND (expr
, 0)));
259 Value_Range
r1 (type
);
260 r1
.set_varying (type
);
261 range_of_expr (r0
, TREE_OPERAND (expr
, 0), stmt
);
262 op
.fold_range (r
, type
, r0
, r1
);
265 r
.set_varying (type
);
268 r
.set_varying (type
);
272 // Return the range for NAME from SSA_NAME_RANGE_INFO.
275 get_ssa_name_range_info (vrange
&r
, const_tree name
)
277 tree type
= TREE_TYPE (name
);
278 gcc_checking_assert (!POINTER_TYPE_P (type
));
279 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
281 void *ri
= SSA_NAME_RANGE_INFO (name
);
283 // Return VR_VARYING for SSA_NAMEs with NULL RANGE_INFO or SSA_NAMEs
284 // with integral types width > 2 * HOST_BITS_PER_WIDE_INT precision.
285 if (!ri
|| (GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (TREE_TYPE (name
)))
286 > 2 * HOST_BITS_PER_WIDE_INT
))
287 r
.set_varying (type
);
290 vrange_storage
vstore (NULL
);
291 vstore
.get_vrange (ri
, r
, TREE_TYPE (name
));
295 // Return nonnull attribute of pointer NAME from SSA_NAME_PTR_INFO.
298 get_ssa_name_ptr_info_nonnull (const_tree name
)
300 gcc_assert (POINTER_TYPE_P (TREE_TYPE (name
)));
301 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (name
);
304 /* TODO Now pt->null is conservatively set to true in PTA
305 analysis. vrp is the only pass (including ipa-vrp)
306 that clears pt.null via set_ptr_nonnull when it knows
307 for sure. PTA will preserves the pt.null value set by VRP.
309 When PTA analysis is improved, pt.anything, pt.nonlocal
310 and pt.escaped may also has to be considered before
311 deciding that pointer cannot point to NULL. */
315 // Update the global range for NAME into the SSA_RANGE_NAME_INFO and
316 // Return the legacy global range for NAME if it has one, otherwise
320 get_range_global (vrange
&r
, tree name
)
322 tree type
= TREE_TYPE (name
);
324 if (SSA_NAME_IS_DEFAULT_DEF (name
))
326 tree sym
= SSA_NAME_VAR (name
);
327 // Adapted from vr_values::get_lattice_entry().
328 // Use a range from an SSA_NAME's available range.
329 if (TREE_CODE (sym
) == PARM_DECL
)
331 // Try to use the "nonnull" attribute to create ~[0, 0]
332 // anti-ranges for pointers. Note that this is only valid with
333 // default definitions of PARM_DECLs.
334 if (POINTER_TYPE_P (type
)
335 && ((cfun
&& nonnull_arg_p (sym
))
336 || get_ssa_name_ptr_info_nonnull (name
)))
337 r
.set_nonzero (type
);
338 else if (INTEGRAL_TYPE_P (type
))
340 get_ssa_name_range_info (r
, name
);
341 if (r
.undefined_p ())
342 r
.set_varying (type
);
345 r
.set_varying (type
);
347 // If this is a local automatic with no definition, use undefined.
348 else if (TREE_CODE (sym
) != RESULT_DECL
)
351 r
.set_varying (type
);
353 else if (!POINTER_TYPE_P (type
) && SSA_NAME_RANGE_INFO (name
))
355 get_ssa_name_range_info (r
, name
);
356 if (r
.undefined_p ())
357 r
.set_varying (type
);
359 else if (POINTER_TYPE_P (type
) && SSA_NAME_PTR_INFO (name
))
361 if (get_ssa_name_ptr_info_nonnull (name
))
362 r
.set_nonzero (type
);
364 r
.set_varying (type
);
367 r
.set_varying (type
);
370 // This is where the ranger picks up global info to seed initial
371 // requests. It is a slightly restricted version of
372 // get_range_global() above.
374 // The reason for the difference is that we can always pick the
375 // default definition of an SSA with no adverse effects, but for other
376 // SSAs, if we pick things up to early, we may prematurely eliminate
377 // builtin_unreachables.
379 // Without this restriction, the test in g++.dg/tree-ssa/pr61034.C has
380 // all of its unreachable calls removed too early.
382 // See discussion here:
383 // https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571709.html
386 gimple_range_global (vrange
&r
, tree name
)
388 tree type
= TREE_TYPE (name
);
389 gcc_checking_assert (TREE_CODE (name
) == SSA_NAME
);
391 if (SSA_NAME_IS_DEFAULT_DEF (name
) || (cfun
&& cfun
->after_inlining
)
392 || is_a
<gphi
*> (SSA_NAME_DEF_STMT (name
)))
394 get_range_global (r
, name
);
397 r
.set_varying (type
);
400 // ----------------------------------------------
401 // global_range_query implementation.
403 global_range_query global_ranges
;
406 global_range_query::range_of_expr (vrange
&r
, tree expr
, gimple
*stmt
)
408 if (!gimple_range_ssa_p (expr
))
409 return get_tree_range (r
, expr
, stmt
);
411 get_range_global (r
, expr
);
416 // Return any known relation between SSA1 and SSA2 before stmt S is executed.
417 // If GET_RANGE is true, query the range of both operands first to ensure
418 // the defintions have been processed and any relations have be created.
421 range_query::query_relation (gimple
*s
, tree ssa1
, tree ssa2
, bool get_range
)
423 if (!m_oracle
|| TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
426 // Ensure ssa1 and ssa2 have both been evaluated.
429 Value_Range
tmp1 (TREE_TYPE (ssa1
));
430 Value_Range
tmp2 (TREE_TYPE (ssa2
));
431 range_of_expr (tmp1
, ssa1
, s
);
432 range_of_expr (tmp2
, ssa2
, s
);
434 return m_oracle
->query_relation (gimple_bb (s
), ssa1
, ssa2
);
437 // Return any known relation between SSA1 and SSA2 on edge E.
438 // If GET_RANGE is true, query the range of both operands first to ensure
439 // the defintions have been processed and any relations have be created.
442 range_query::query_relation (edge e
, tree ssa1
, tree ssa2
, bool get_range
)
445 if (!m_oracle
|| TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
448 // Use destination block if it has a single predecessor, and this picks
449 // up any relation on the edge.
450 // Otherwise choose the src edge and the result is the same as on-exit.
451 if (!single_pred_p (e
->dest
))
456 // Ensure ssa1 and ssa2 have both been evaluated.
459 Value_Range
tmp (TREE_TYPE (ssa1
));
460 range_on_edge (tmp
, e
, ssa1
);
461 range_on_edge (tmp
, e
, ssa2
);
463 return m_oracle
->query_relation (bb
, ssa1
, ssa2
);