Line data Source code
1 : /* Gimple range inference implementation.
2 : Copyright (C) 2022-2026 Free Software Foundation, Inc.
3 : Contributed by Andrew MacLeod <amacleod@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "insn-codes.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "ssa.h"
29 : #include "gimple-pretty-print.h"
30 : #include "gimple-range.h"
31 : #include "value-range-storage.h"
32 : #include "tree-cfg.h"
33 : #include "target.h"
34 : #include "attribs.h"
35 : #include "gimple-iterator.h"
36 : #include "gimple-walk.h"
37 : #include "cfganal.h"
38 : #include "tree-dfa.h"
39 : #include "fold-const.h"
40 :
41 : // Create the global oracle.
42 :
43 : infer_range_oracle infer_oracle;
44 :
45 : // This class is merely an accessor which is granted internals to
46 : // gimple_infer_range such that non_null_loadstore as a static callback can
47 : // call the protected add_nonzero ().
48 : // Static functions ccannot be friends, so we do it through a class wrapper.
49 :
50 : class non_null_wrapper
51 : {
52 : public:
53 29014635 : inline non_null_wrapper (gimple_infer_range *infer) : m_infer (infer) { }
54 29014635 : inline void add_nonzero (tree name) { m_infer->add_nonzero (name); }
55 : inline void add_range (tree t, vrange &r) { m_infer->add_range (t, r); }
56 : private:
57 : gimple_infer_range *m_infer;
58 : };
59 :
60 : // Adapted from infer_nonnull_range_by_dereference and check_loadstore
61 : // to process nonnull ssa_name OP in S. DATA contains a pointer to a
62 : // stmt range inference instance.
63 :
64 : static bool
65 53168571 : non_null_loadstore (gimple *stmt, tree op, tree, void *data)
66 : {
67 106337142 : if (TREE_CODE (op) == MEM_REF
68 53168571 : || (TREE_CODE (op) == TARGET_MEM_REF
69 1683556 : && !TMR_INDEX2 (op)
70 1650624 : && (!TMR_INDEX (op)
71 832245 : || (TMR_STEP (op)
72 785678 : && expr_not_equal_to (TMR_STEP (op),
73 53954249 : wi::one (TYPE_PRECISION (TREE_TYPE
74 : (TMR_STEP (op)))),
75 : stmt)))))
76 : {
77 : /* Some address spaces may legitimately dereference zero. */
78 29015135 : addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (op));
79 29015135 : if (!targetm.addr_space.zero_address_valid (as))
80 : {
81 29014635 : non_null_wrapper wrapper ((gimple_infer_range *)data);
82 29014635 : wrapper.add_nonzero (TREE_OPERAND (op, 0));
83 : }
84 : }
85 53168571 : return false;
86 : }
87 :
88 : // Process an ASSUME call to see if there are any inferred ranges available.
89 :
90 : void
91 423 : gimple_infer_range::check_assume_func (gcall *call)
92 : {
93 423 : tree arg;
94 423 : unsigned i;
95 423 : tree assume_id = TREE_OPERAND (gimple_call_arg (call, 0), 0);
96 423 : if (!assume_id)
97 : return;
98 423 : struct function *fun = DECL_STRUCT_FUNCTION (assume_id);
99 423 : if (!fun)
100 : return;
101 : // Loop over arguments, matching them to the assume parameters.
102 423 : for (arg = DECL_ARGUMENTS (assume_id), i = 1;
103 960 : arg && i < gimple_call_num_args (call);
104 537 : i++, arg = DECL_CHAIN (arg))
105 : {
106 537 : tree op = gimple_call_arg (call, i);
107 537 : tree type = TREE_TYPE (op);
108 537 : if (gimple_range_ssa_p (op) && value_range::supports_type_p (type))
109 : {
110 402 : tree default_def = ssa_default_def (fun, arg);
111 402 : if (!default_def || type != TREE_TYPE (default_def))
112 3 : continue;
113 : // Query the global range of the default def in the assume function.
114 399 : value_range assume_range (type);
115 399 : gimple_range_global (assume_range, default_def, fun);
116 : // If there is a non-varying result, add it as an inferred range.
117 399 : if (!assume_range.varying_p ())
118 : {
119 218 : add_range (op, assume_range);
120 218 : if (dump_file)
121 : {
122 48 : print_generic_expr (dump_file, assume_id, TDF_SLIM);
123 48 : fprintf (dump_file, " assume inferred range of ");
124 48 : print_generic_expr (dump_file, op, TDF_SLIM);
125 48 : fprintf (dump_file, " (param ");
126 48 : print_generic_expr (dump_file, arg, TDF_SLIM);
127 48 : fprintf (dump_file, ") = ");
128 48 : assume_range.dump (dump_file);
129 48 : fputc ('\n', dump_file);
130 : }
131 : }
132 399 : }
133 : }
134 : }
135 :
136 : // Add NAME and RANGE to the range inference summary.
137 :
138 : void
139 23995569 : gimple_infer_range::add_range (tree name, vrange &range)
140 : {
141 : // Do not add an inferred range if it is VARYING.
142 23995569 : if (range.varying_p ())
143 : return;
144 23994489 : m_names[num_args] = name;
145 23994489 : m_ranges[num_args] = range;
146 23994489 : if (num_args < size_limit - 1)
147 23994489 : num_args++;
148 : }
149 :
150 : // Add a nonzero range for NAME to the range inference summary.
151 :
152 : void
153 34743369 : gimple_infer_range::add_nonzero (tree name)
154 : {
155 34743369 : if (!gimple_range_ssa_p (name))
156 : return;
157 23969644 : prange nz;
158 23969644 : nz.set_nonzero (TREE_TYPE (name));
159 23969644 : add_range (name, nz);
160 23969644 : }
161 :
162 : // Process S for range inference and fill in the summary list.
163 : // This is the routine where any new inferred ranges should be added.
164 : // If USE_RANGEOPS is true, invoke range-ops on stmts with a single
165 : // ssa-name a constant to reflect an inferred range. ie
166 : // x_2 = y_3 + 1 will provide an inferred range for y_3 of [-INF, +INF - 1].
167 : // This defaults to FALSE as it can be expensive.,
168 :
169 378323530 : gimple_infer_range::gimple_infer_range (gimple *s, range_query *q,
170 4161558830 : bool use_rangeops)
171 : {
172 378323530 : num_args = 0;
173 :
174 378323530 : if (is_a<gphi *> (s))
175 378323530 : return;
176 :
177 : // Default to the global query if none provided.
178 341002358 : if (!q)
179 0 : q = get_global_range_query ();
180 :
181 341002358 : if (is_a<gcall *> (s) && flag_delete_null_pointer_checks)
182 : {
183 20884949 : tree fntype = gimple_call_fntype (s);
184 20884949 : bitmap nonnullargs = get_nonnull_args (fntype);
185 : // Process any non-null arguments
186 20884949 : if (nonnullargs)
187 : {
188 13675345 : for (unsigned i = 0; i < gimple_call_num_args (s); i++)
189 : {
190 9694213 : if (bitmap_empty_p (nonnullargs)
191 9694213 : || bitmap_bit_p (nonnullargs, i))
192 : {
193 4922360 : tree op = gimple_call_arg (s, i);
194 4922360 : if (POINTER_TYPE_P (TREE_TYPE (op)))
195 4888899 : add_nonzero (op);
196 : }
197 : }
198 3981132 : BITMAP_FREE (nonnullargs);
199 : }
200 20884949 : if (fntype)
201 20221321 : for (tree attrs = TYPE_ATTRIBUTES (fntype);
202 22102184 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
203 1880863 : attrs = TREE_CHAIN (attrs))
204 : {
205 1880863 : tree args = TREE_VALUE (attrs);
206 1880863 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
207 1880863 : unsigned int idx2
208 1880863 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
209 1880863 : unsigned int idx3 = idx2;
210 1880863 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
211 13836 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
212 1880863 : if (idx < gimple_call_num_args (s)
213 1880827 : && idx2 < gimple_call_num_args (s)
214 3761657 : && idx3 < gimple_call_num_args (s))
215 : {
216 1880794 : tree arg = gimple_call_arg (s, idx);
217 1880794 : tree arg2 = gimple_call_arg (s, idx2);
218 1880794 : tree arg3 = gimple_call_arg (s, idx3);
219 1882709 : if (!POINTER_TYPE_P (TREE_TYPE (arg))
220 1880533 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg2))
221 1880518 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg3))
222 1880518 : || integer_zerop (arg2)
223 3760365 : || integer_zerop (arg3))
224 1407 : continue;
225 1879387 : if (integer_nonzerop (arg2) && integer_nonzerop (arg3))
226 345824 : add_nonzero (arg);
227 : else
228 : {
229 1533563 : value_range r (TREE_TYPE (arg2));
230 1533563 : if (q->range_of_expr (r, arg2, s)
231 1533563 : && !r.contains_p (build_zero_cst (TREE_TYPE (arg2))))
232 : {
233 494568 : if (idx2 == idx3)
234 493784 : add_nonzero (arg);
235 : else
236 : {
237 784 : value_range r2 (TREE_TYPE (arg3));
238 784 : tree zero3 = build_zero_cst (TREE_TYPE (arg3));
239 784 : if (q->range_of_expr (r2, arg3, s)
240 1568 : && !r2.contains_p (zero3))
241 227 : add_nonzero (arg);
242 784 : }
243 : }
244 1533563 : }
245 : }
246 : }
247 : // Fallthru and walk load/store ops now.
248 : }
249 :
250 : // Check for inferred ranges from ASSUME calls.
251 341002358 : if (is_a<gcall *> (s) && gimple_call_internal_p (s)
252 341679004 : && gimple_call_internal_fn (s) == IFN_ASSUME)
253 423 : check_assume_func (as_a<gcall *> (s));
254 :
255 : // Look for possible non-null values.
256 340869923 : if (flag_delete_null_pointer_checks && gimple_code (s) != GIMPLE_ASM
257 681692168 : && !gimple_clobber_p (s))
258 335084340 : walk_stmt_load_store_ops (s, (void *)this, non_null_loadstore,
259 : non_null_loadstore);
260 :
261 : // Gated by flag.
262 341002358 : if (!use_rangeops)
263 : return;
264 :
265 : // Check if there are any inferred ranges from range-ops.
266 0 : gimple_range_op_handler handler (s);
267 0 : if (!handler)
268 : return;
269 :
270 : // Only proceed if ONE operand is an SSA_NAME, This may provide an
271 : // inferred range for 'y + 3' , but will bypass expressions like
272 : // 'y + z' as it depends on symbolic values.
273 0 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
274 0 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
275 0 : if ((ssa1 != NULL) == (ssa2 != NULL))
276 : return;
277 :
278 : // The other operand should be a constant, so just use the global range
279 : // query to pick up any other values.
280 0 : if (ssa1)
281 : {
282 0 : value_range op1 (TREE_TYPE (ssa1));
283 0 : if (op1_range (op1, s, q) && !op1.varying_p ())
284 0 : add_range (ssa1, op1);
285 0 : }
286 : else
287 : {
288 0 : gcc_checking_assert (ssa2);
289 0 : value_range op2 (TREE_TYPE (ssa2));
290 0 : if (op2_range (op2, s, q) && !op2.varying_p ())
291 0 : add_range (ssa2, op2);
292 0 : }
293 : }
294 :
295 : // Create an single inferred range for NAMe using range R.
296 :
297 282777 : gimple_infer_range::gimple_infer_range (tree name, vrange &r)
298 : {
299 25707 : num_args = 0;
300 25707 : add_range (name, r);
301 25707 : }
302 :
303 : // -------------------------------------------------------------------------
304 :
305 : // This class is an element in the list of inferred ranges.
306 :
307 : class exit_range
308 : {
309 : public:
310 : tree name;
311 : unsigned bbi;
312 : vrange_storage *range;
313 : exit_range *next;
314 : exit_range *name_link;
315 : };
316 :
317 :
318 : // If there is an element which matches SSA, return a pointer to the element.
319 : // Otherwise return NULL.
320 :
321 : exit_range *
322 16835430 : infer_range_manager::exit_range_head::find_ptr (tree ssa)
323 : {
324 : // Return NULL if SSA is not in this list.
325 33670860 : if (!m_names || !bitmap_bit_p (m_names, SSA_NAME_VERSION (ssa)))
326 : return NULL;
327 8336746 : for (exit_range *ptr = head; ptr != NULL; ptr = ptr->next)
328 8336746 : if (ptr->name == ssa)
329 : return ptr;
330 : // Should be unreachable.
331 0 : gcc_unreachable ();
332 : return NULL;
333 : }
334 :
335 : // Construct a range infer manager. DO_SEARCH indicates whether an immediate
336 : // use scan should be made the first time a name is processed. This is for
337 : // on-demand clients who may not visit every statement and may miss uses.
338 : // Q is the range_query to use for any lookups. Default is NULL which maps
339 : // to the global_range_query.
340 :
341 29449302 : infer_range_manager::infer_range_manager (bool do_search, range_query *q)
342 : {
343 : // Set the range query to use.
344 29449302 : m_query = q ? q : get_global_range_query ();
345 :
346 29449302 : bitmap_obstack_initialize (&m_bitmaps);
347 29449302 : m_on_exit.create (0);
348 29449302 : m_on_exit.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
349 : // m_seen == NULL indicates no scanning. Otherwise the bit indicates a
350 : // scan has been performed on NAME.
351 29449302 : if (do_search)
352 23536804 : m_seen = BITMAP_ALLOC (&m_bitmaps);
353 : else
354 5912498 : m_seen = NULL;
355 29449302 : obstack_init (&m_list_obstack);
356 : // Non-zero elements are very common, so cache them for each ssa-name.
357 29449302 : m_name_info.create (0);
358 58898604 : m_name_info.safe_grow_cleared (num_ssa_names + 1);
359 29449302 : m_range_allocator = new vrange_allocator;
360 29449302 : }
361 :
362 : // Destruct a range infer manager.
363 :
364 58898604 : infer_range_manager::~infer_range_manager ()
365 : {
366 29449302 : m_name_info.release ();
367 29449302 : obstack_free (&m_list_obstack, NULL);
368 29449302 : m_on_exit.release ();
369 29449302 : bitmap_obstack_release (&m_bitmaps);
370 29449302 : delete m_range_allocator;
371 58898604 : }
372 :
373 : // Return a non-zero range value of the appropriate type for NAME from
374 : // the cache, creating it if necessary.
375 :
376 : const vrange&
377 0 : infer_range_manager::get_nonzero (tree name)
378 : {
379 0 : unsigned v = SSA_NAME_VERSION (name);
380 0 : if (v >= m_name_info.length ())
381 0 : m_name_info.safe_grow_cleared (num_ssa_names + 20);
382 0 : if (!m_name_info[v].nonzero)
383 : {
384 0 : m_name_info[v].nonzero
385 0 : = (irange *) m_range_allocator->alloc (sizeof (int_range <2>));
386 0 : m_name_info[v].nonzero->set_nonzero (TREE_TYPE (name));
387 : }
388 0 : return *(m_name_info[v].nonzero);
389 : }
390 :
391 : // Return TRUE if NAME has a range inference in block BB. If NAME is NULL,
392 : // return TRUE if there are any name sin BB.
393 :
394 : bool
395 746531704 : infer_range_manager::has_range_p (basic_block bb, tree name)
396 : {
397 : // Check if this is an immediate use search model.
398 1181091146 : if (name && m_seen && !bitmap_bit_p (m_seen, SSA_NAME_VERSION (name)))
399 28545567 : register_all_uses (name);
400 :
401 1493063408 : if (bb->index >= (int)m_on_exit.length ())
402 : return false;
403 :
404 746188253 : bitmap b = m_on_exit[bb->index].m_names;
405 746188253 : if (!b)
406 : return false;
407 :
408 64387938 : if (name)
409 58115359 : return bitmap_bit_p (m_on_exit[bb->index].m_names, SSA_NAME_VERSION (name));
410 6272579 : return !bitmap_empty_p (b);
411 : }
412 :
413 : // Return TRUE if NAME has a range inference in block BB, and adjust range R
414 : // to include it.
415 :
416 : bool
417 649109914 : infer_range_manager::maybe_adjust_range (vrange &r, tree name, basic_block bb)
418 : {
419 649109914 : if (!has_range_p (bb, name))
420 : return false;
421 5892715 : exit_range *ptr = m_on_exit[bb->index].find_ptr (name);
422 5892715 : gcc_checking_assert (ptr);
423 : // Return true if this exit range changes R, otherwise false.
424 5892715 : tree type = TREE_TYPE (name);
425 5892715 : value_range tmp (type);
426 5892715 : ptr->range->get_vrange (tmp, type);
427 5892715 : return r.intersect (tmp);
428 5892715 : }
429 :
430 : // Add all inferred ranges in INFER at stmt S.
431 :
432 : void
433 16600508 : infer_range_manager::add_ranges (gimple *s, gimple_infer_range &infer)
434 : {
435 33501587 : for (unsigned x = 0; x < infer.num (); x++)
436 : {
437 16901079 : tree arg = infer.name (x);
438 16901079 : value_range r (TREE_TYPE (arg));
439 16901079 : m_query->range_of_expr (r, arg, s);
440 : // Only add the inferred range if it changes the current range.
441 16901079 : if (r.intersect (infer.range (x)))
442 5622632 : add_range (arg, s, infer.range (x));
443 16901079 : }
444 16600508 : }
445 :
446 : // Add range R as an inferred range for NAME on stmt S.
447 :
448 : void
449 10942715 : infer_range_manager::add_range (tree name, gimple *s, const vrange &r)
450 : {
451 10942715 : basic_block bb = gimple_bb (s);
452 10942715 : if (!bb)
453 : return;
454 21885430 : if (bb->index >= (int)m_on_exit.length ())
455 38 : m_on_exit.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
456 :
457 10942715 : if (SSA_NAME_VERSION (name) >= m_name_info.length ())
458 4 : m_name_info.safe_grow_cleared (num_ssa_names + 20);
459 :
460 : // Create the summary list bitmap if it doesn't exist.
461 10942715 : if (!m_on_exit[bb->index].m_names)
462 7563381 : m_on_exit[bb->index].m_names = BITMAP_ALLOC (&m_bitmaps);
463 :
464 10942715 : if (dump_file && (dump_flags & TDF_DETAILS))
465 : {
466 88 : fprintf (dump_file, " on-exit update ");
467 88 : print_generic_expr (dump_file, name, TDF_SLIM);
468 88 : fprintf (dump_file, " in BB%d : ",bb->index);
469 88 : r.dump (dump_file);
470 88 : fprintf (dump_file, "\n");
471 : }
472 :
473 21885430 : get_range_query (cfun)->update_range_info (name);
474 :
475 : // If NAME already has a range, intersect them and done.
476 10942715 : exit_range *ptr = m_on_exit[bb->index].find_ptr (name);
477 10942715 : if (ptr)
478 : {
479 1864003 : tree type = TREE_TYPE (name);
480 1864003 : value_range cur (r), name_range (type);
481 1864003 : ptr->range->get_vrange (name_range, type);
482 : // If no new info is added, just return.
483 1864003 : if (!cur.intersect (name_range))
484 : return;
485 21 : if (ptr->range->fits_p (cur))
486 21 : ptr->range->set_vrange (cur);
487 : else
488 0 : ptr->range = m_range_allocator->clone (cur);
489 : return;
490 1864003 : }
491 :
492 : // Otherwise create a record.
493 9078712 : bitmap_set_bit (m_on_exit[bb->index].m_names, SSA_NAME_VERSION (name));
494 9078712 : ptr = (exit_range *)obstack_alloc (&m_list_obstack, sizeof (exit_range));
495 9078712 : ptr->range = m_range_allocator->clone (r);
496 9078712 : ptr->name = name;
497 9078712 : ptr->bbi = bb->index;
498 9078712 : ptr->next = m_on_exit[bb->index].head;
499 9078712 : ptr->name_link = m_name_info[SSA_NAME_VERSION (name)].name_link;
500 9078712 : m_name_info[SSA_NAME_VERSION (name)].name_link = ptr;
501 9078712 : m_on_exit[bb->index].head = ptr;
502 : }
503 :
504 : // Add a non-zero inferred range for NAME at stmt S.
505 :
506 : void
507 0 : infer_range_manager::add_nonzero (tree name, gimple *s)
508 : {
509 0 : add_range (name, s, get_nonzero (name));
510 0 : }
511 :
512 : // Follow immediate use chains and find all inferred ranges for NAME.
513 :
514 : void
515 28545567 : infer_range_manager::register_all_uses (tree name)
516 : {
517 28545567 : gcc_checking_assert (m_seen);
518 :
519 : // Check if we've already processed this name.
520 28545567 : unsigned v = SSA_NAME_VERSION (name);
521 28545567 : if (bitmap_bit_p (m_seen, v))
522 0 : return;
523 28545567 : bitmap_set_bit (m_seen, v);
524 :
525 28545567 : use_operand_p use_p;
526 28545567 : imm_use_iterator iter;
527 :
528 : // Loop over each immediate use and see if it has an inferred range.
529 132635184 : FOR_EACH_IMM_USE_FAST (use_p, iter, name)
530 : {
531 104089617 : gimple *s = USE_STMT (use_p);
532 104089617 : gimple_infer_range infer (s, m_query);
533 215272644 : for (unsigned x = 0; x < infer.num (); x++)
534 : {
535 7093410 : if (name == infer.name (x))
536 5320083 : add_range (name, s, infer.range (x));
537 : }
538 28545567 : }
539 : }
540 :
541 : // Clear all inferred ranges for NAME.
542 :
543 : void
544 440 : infer_range_manager::clear(tree name)
545 : {
546 : // Check if this name has any inferred ranges.
547 440 : unsigned v = SSA_NAME_VERSION (name);
548 440 : if (v >= m_name_info.length ())
549 : return;
550 :
551 440 : exit_range *ptr = m_name_info[v].name_link;
552 449 : for ( ; ptr ; ptr = ptr->name_link)
553 : {
554 9 : bitmap_clear_bit (m_on_exit[ptr->bbi].m_names, v);
555 9 : ptr->name = NULL;
556 : }
557 :
558 440 : m_name_info[v].name_link = NULL;
559 440 : if (m_seen)
560 0 : bitmap_clear_bit (m_seen, v);
561 : }
|