Line data Source code
1 : /* Code for GIMPLE range related routines.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : Contributed by Andrew MacLeod <amacleod@redhat.com>
4 : and Aldy Hernandez <aldyh@redhat.com>.
5 :
6 : This file is part of GCC.
7 :
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)
11 : any later version.
12 :
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.
17 :
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/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "backend.h"
26 : #include "insn-codes.h"
27 : #include "tree.h"
28 : #include "gimple.h"
29 : #include "ssa.h"
30 : #include "gimple-pretty-print.h"
31 : #include "optabs-tree.h"
32 : #include "gimple-iterator.h"
33 : #include "gimple-fold.h"
34 : #include "wide-int.h"
35 : #include "fold-const.h"
36 : #include "case-cfn-macros.h"
37 : #include "omp-general.h"
38 : #include "cfgloop.h"
39 : #include "tree-ssa-loop.h"
40 : #include "tree-scalar-evolution.h"
41 : #include "langhooks.h"
42 : #include "vr-values.h"
43 : #include "range.h"
44 : #include "value-query.h"
45 : #include "gimple-range-op.h"
46 : #include "gimple-range.h"
47 : #include "cgraph.h"
48 : #include "alloc-pool.h"
49 : #include "symbol-summary.h"
50 : #include "ipa-utils.h"
51 : #include "sreal.h"
52 : #include "ipa-cp.h"
53 : #include "ipa-prop.h"
54 : #include "rtl.h"
55 : // Construct a fur_source, and set the m_query field.
56 :
57 490627161 : fur_source::fur_source (range_query *q)
58 : {
59 490627161 : if (q)
60 490626194 : m_query = q;
61 : else
62 1934 : m_query = get_range_query (cfun);
63 490627161 : m_depend_p = false;
64 490627161 : }
65 :
66 : // Invoke range_of_expr on EXPR.
67 :
68 : bool
69 0 : fur_source::get_operand (vrange &r, tree expr)
70 : {
71 0 : return m_query->range_of_expr (r, expr);
72 : }
73 :
74 : // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
75 : // range_query to get the range on the edge.
76 :
77 : bool
78 0 : fur_source::get_phi_operand (vrange &r, tree expr, edge e)
79 : {
80 0 : return m_query->range_on_edge (r, e, expr);
81 : }
82 :
83 : // Default is no relation.
84 :
85 : relation_kind
86 5499041 : fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
87 : tree op2 ATTRIBUTE_UNUSED)
88 : {
89 5499041 : return VREL_VARYING;
90 : }
91 :
92 : // Default registers nothing and returns false meaning nothing changed.
93 :
94 : bool
95 26066688 : fur_source::register_relation (gimple *s ATTRIBUTE_UNUSED,
96 : relation_kind k ATTRIBUTE_UNUSED,
97 : tree op1 ATTRIBUTE_UNUSED,
98 : tree op2 ATTRIBUTE_UNUSED)
99 : {
100 26066688 : return false;
101 : }
102 :
103 : // Default registers nothing and returns false meaning nothing changed.
104 :
105 : bool
106 6476597 : fur_source::register_relation (edge e ATTRIBUTE_UNUSED,
107 : relation_kind k ATTRIBUTE_UNUSED,
108 : tree op1 ATTRIBUTE_UNUSED,
109 : tree op2 ATTRIBUTE_UNUSED)
110 : {
111 6476597 : return false;
112 : }
113 :
114 : // Get the value of EXPR on edge m_edge.
115 :
116 : bool
117 68154966 : fur_edge::get_operand (vrange &r, tree expr)
118 : {
119 68154966 : return m_query->range_on_edge (r, m_edge, expr);
120 : }
121 :
122 : // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
123 : // range_query to get the range on the edge.
124 :
125 : bool
126 0 : fur_edge::get_phi_operand (vrange &r, tree expr, edge e)
127 : {
128 : // Edge to edge recalculations not supported yet, until we sort it out.
129 0 : gcc_checking_assert (e == m_edge);
130 0 : return m_query->range_on_edge (r, e, expr);
131 : }
132 :
133 : // Instantiate a stmt based fur_source.
134 :
135 426158596 : fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
136 : {
137 426158596 : m_stmt = s;
138 426158596 : }
139 :
140 : // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
141 :
142 : bool
143 562268106 : fur_stmt::get_operand (vrange &r, tree expr)
144 : {
145 562268106 : return m_query->range_of_expr (r, expr, m_stmt);
146 : }
147 :
148 : // Evaluate EXPR for this stmt as a PHI argument on edge E. Use the current
149 : // range_query to get the range on the edge.
150 :
151 : bool
152 56239587 : fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
153 : {
154 : // Pick up the range of expr from edge E.
155 56239587 : fur_edge e_src (e, m_query);
156 56239587 : return e_src.get_operand (r, expr);
157 : }
158 :
159 : // Return relation based from m_stmt.
160 :
161 : relation_kind
162 108798302 : fur_stmt::query_relation (tree op1, tree op2)
163 : {
164 108798302 : return m_query->relation ().query (m_stmt, op1, op2);
165 : }
166 :
167 : // Instantiate a stmt based fur_source with a GORI object and a ranger cache.
168 :
169 237121021 : fur_depend::fur_depend (gimple *s, range_query *q, ranger_cache *c)
170 237121021 : : fur_stmt (s, q), m_cache (c)
171 : {
172 237121021 : m_depend_p = true;
173 237121021 : }
174 :
175 : // Register a relation on a stmt if there is an oracle. Return false if
176 : // no new relation is registered.
177 :
178 : bool
179 33308747 : fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
180 : {
181 33308747 : if (!m_query->relation ().record (s, k, op1, op2))
182 : return false;
183 :
184 : // This new relation could cause different calculations, so mark the operands
185 : // with a new timestamp, forcing recalculations.
186 24678430 : if (m_cache)
187 : {
188 24678398 : m_cache->update_consumers (op1);
189 24678398 : m_cache->update_consumers (op2);
190 : }
191 : return true;
192 : }
193 :
194 : // Register a relation on an edge if there is an oracle. Return false if
195 : // no new relation is registered.
196 :
197 : bool
198 6540004 : fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
199 : {
200 6540004 : if (!m_query->relation ().record (e, k, op1, op2))
201 : return false;
202 :
203 : // This new relation could cause different calculations, so mark the operands
204 : // with a new timestamp, forcing recalculations.
205 6522850 : if (m_cache)
206 : {
207 6522848 : m_cache->update_consumers (op1);
208 6522848 : m_cache->update_consumers (op2);
209 : }
210 : return true;
211 : }
212 :
213 : // This version of fur_source will pick a range up from a list of ranges
214 : // supplied by the caller.
215 :
216 : class fur_list : public fur_source
217 : {
218 : public:
219 : fur_list (vrange &r1, range_query *q = NULL);
220 : fur_list (vrange &r1, vrange &r2, range_query *q = NULL);
221 : fur_list (unsigned num, vrange **list, range_query *q = NULL);
222 : virtual bool get_operand (vrange &r, tree expr) override;
223 : virtual bool get_phi_operand (vrange &r, tree expr, edge e) override;
224 : private:
225 : vrange *m_local[2];
226 : vrange **m_list;
227 : unsigned m_index;
228 : unsigned m_limit;
229 : };
230 :
231 : // One range supplied for unary operations.
232 :
233 910121 : fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
234 : {
235 910121 : m_list = m_local;
236 910121 : m_index = 0;
237 910121 : m_limit = 1;
238 910121 : m_local[0] = &r1;
239 910121 : }
240 :
241 : // Two ranges supplied for binary operations.
242 :
243 0 : fur_list::fur_list (vrange &r1, vrange &r2, range_query *q) : fur_source (q)
244 : {
245 0 : m_list = m_local;
246 0 : m_index = 0;
247 0 : m_limit = 2;
248 0 : m_local[0] = &r1;
249 0 : m_local[1] = &r2;
250 0 : }
251 :
252 : // Arbitrary number of ranges in a vector.
253 :
254 0 : fur_list::fur_list (unsigned num, vrange **list, range_query *q)
255 0 : : fur_source (q)
256 : {
257 0 : m_list = list;
258 0 : m_index = 0;
259 0 : m_limit = num;
260 0 : }
261 :
262 : // Get the next operand from the vector, ensure types are compatible.
263 :
264 : bool
265 1812646 : fur_list::get_operand (vrange &r, tree expr)
266 : {
267 : // Do not use the vector for non-ssa-names, or if it has been emptied.
268 1812646 : if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
269 902525 : return m_query->range_of_expr (r, expr);
270 910121 : r = *m_list[m_index++];
271 910121 : gcc_checking_assert (range_compatible_p (TREE_TYPE (expr), r.type ()));
272 : return true;
273 : }
274 :
275 : // This will simply pick the next operand from the vector.
276 : bool
277 0 : fur_list::get_phi_operand (vrange &r, tree expr, edge e ATTRIBUTE_UNUSED)
278 : {
279 0 : return get_operand (r, expr);
280 : }
281 :
282 : // Fold stmt S into range R using R1 as the first operand.
283 :
284 : bool
285 910121 : fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
286 : {
287 910121 : fold_using_range f;
288 910121 : fur_list src (r1, q);
289 910121 : return f.fold_stmt (r, s, src);
290 : }
291 :
292 : // Fold stmt S into range R using R1 and R2 as the first two operands.
293 :
294 : bool
295 0 : fold_range (vrange &r, gimple *s, vrange &r1, vrange &r2, range_query *q)
296 : {
297 0 : fold_using_range f;
298 0 : fur_list src (r1, r2, q);
299 0 : return f.fold_stmt (r, s, src);
300 : }
301 :
302 : // Fold stmt S into range R using NUM_ELEMENTS from VECTOR as the initial
303 : // operands encountered.
304 :
305 : bool
306 0 : fold_range (vrange &r, gimple *s, unsigned num_elements, vrange **vector,
307 : range_query *q)
308 : {
309 0 : fold_using_range f;
310 0 : fur_list src (num_elements, vector, q);
311 0 : return f.fold_stmt (r, s, src);
312 : }
313 :
314 : // Fold stmt S into range R using range query Q.
315 :
316 : bool
317 78120293 : fold_range (vrange &r, gimple *s, range_query *q)
318 : {
319 78120293 : fold_using_range f;
320 78120293 : fur_stmt src (s, q);
321 78120293 : return f.fold_stmt (r, s, src);
322 : }
323 :
324 : // Recalculate stmt S into R using range query Q as if it were on edge ON_EDGE.
325 :
326 : bool
327 7318852 : fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
328 : {
329 7318852 : fold_using_range f;
330 7318852 : fur_edge src (on_edge, q);
331 7318852 : return f.fold_stmt (r, s, src);
332 : }
333 :
334 : // Calculate op1 on statetemt S with LHS into range R using range query Q
335 : // to resolve any other operands.
336 :
337 : bool
338 0 : op1_range (vrange &r, gimple *s, const vrange &lhs, range_query *q)
339 : {
340 0 : gimple_range_op_handler handler (s);
341 0 : if (!handler)
342 : return false;
343 :
344 0 : fur_stmt src (s, q);
345 :
346 0 : tree op2_expr = handler.operand2 ();
347 0 : if (!op2_expr)
348 0 : return handler.calc_op1 (r, lhs);
349 :
350 0 : value_range op2 (TREE_TYPE (op2_expr));
351 0 : if (!src.get_operand (op2, op2_expr))
352 : return false;
353 :
354 0 : return handler.calc_op1 (r, lhs, op2);
355 0 : }
356 :
357 : // Calculate op1 on statetemt S into range R using range query Q.
358 : // LHS is set to VARYING in this case.
359 :
360 : bool
361 0 : op1_range (vrange &r, gimple *s, range_query *q)
362 : {
363 0 : tree lhs_type = gimple_range_type (s);
364 0 : if (!lhs_type)
365 : return false;
366 0 : value_range lhs_range;
367 0 : lhs_range.set_varying (lhs_type);
368 0 : return op1_range (r, s, lhs_range, q);
369 0 : }
370 :
371 : // Calculate op2 on statetemt S with LHS into range R using range query Q
372 : // to resolve any other operands.
373 :
374 : bool
375 0 : op2_range (vrange &r, gimple *s, const vrange &lhs, range_query *q)
376 : {
377 :
378 0 : gimple_range_op_handler handler (s);
379 0 : if (!handler)
380 : return false;
381 :
382 0 : fur_stmt src (s, q);
383 :
384 0 : value_range op1 (TREE_TYPE (handler.operand1 ()));
385 0 : if (!src.get_operand (op1, handler.operand1 ()))
386 : return false;
387 :
388 0 : return handler.calc_op2 (r, lhs, op1);
389 0 : }
390 :
391 : // Calculate op2 on statetemt S into range R using range query Q.
392 : // LHS is set to VARYING in this case.
393 :
394 : bool
395 0 : op2_range (vrange &r, gimple *s, range_query *q)
396 : {
397 0 : tree lhs_type = gimple_range_type (s);
398 0 : if (!lhs_type)
399 : return false;
400 0 : value_range lhs_range;
401 0 : lhs_range.set_varying (lhs_type);
402 0 : return op2_range (r, s, lhs_range, q);
403 0 : }
404 :
405 : // Provide a fur_source which can be used to determine any relations on
406 : // a statement. It manages the callback from fold_using_ranges to determine
407 : // a relation_trio for a statement.
408 :
409 : class fur_relation : public fur_stmt
410 : {
411 : public:
412 : fur_relation (gimple *s, range_query *q = NULL);
413 : virtual bool register_relation (gimple *stmt, relation_kind k, tree op1,
414 : tree op2);
415 : virtual bool register_relation (edge e, relation_kind k, tree op1,
416 : tree op2);
417 : relation_trio trio() const;
418 : private:
419 : relation_kind def_op1, def_op2, op1_op2;
420 : };
421 :
422 1067047 : fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
423 : {
424 1067047 : def_op1 = def_op2 = op1_op2 = VREL_VARYING;
425 1067047 : }
426 :
427 : // Construct a trio from what is known.
428 :
429 : relation_trio
430 1067047 : fur_relation::trio () const
431 : {
432 1067047 : return relation_trio (def_op1, def_op2, op1_op2);
433 : }
434 :
435 : // Don't support edges, but avoid a compiler warning by providing the routine.
436 : // Return false indicating nothing has changed.
437 :
438 : bool
439 0 : fur_relation::register_relation (edge, relation_kind, tree, tree)
440 : {
441 0 : return false;
442 : }
443 :
444 : // Register relation K between OP1 and OP2 on STMT. Return false if there
445 : // is no relation.
446 :
447 : bool
448 1049491 : fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
449 : tree op2)
450 : {
451 1049491 : tree lhs = gimple_get_lhs (stmt);
452 1049491 : tree a1 = NULL_TREE;
453 1049491 : tree a2 = NULL_TREE;
454 1049491 : switch (gimple_code (stmt))
455 : {
456 0 : case GIMPLE_COND:
457 0 : a1 = gimple_cond_lhs (stmt);
458 0 : a2 = gimple_cond_rhs (stmt);
459 0 : break;
460 1049491 : case GIMPLE_ASSIGN:
461 1049491 : a1 = gimple_assign_rhs1 (stmt);
462 1049491 : if (gimple_num_ops (stmt) >= 3)
463 1049491 : a2 = gimple_assign_rhs2 (stmt);
464 : break;
465 : default:
466 : break;
467 : }
468 : // STMT is of the form LHS = A1 op A2, now map the relation to these
469 : // operands, if possible.
470 1049491 : if (op1 == lhs)
471 : {
472 1049491 : if (op2 == a1)
473 1049491 : def_op1 = k;
474 0 : else if (op2 == a2)
475 0 : def_op2 = k;
476 : }
477 0 : else if (op2 == lhs)
478 : {
479 0 : if (op1 == a1)
480 0 : def_op1 = relation_swap (k);
481 0 : else if (op1 == a2)
482 0 : def_op2 = relation_swap (k);
483 : }
484 : else
485 : {
486 0 : if (op1 == a1 && op2 == a2)
487 0 : op1_op2 = k;
488 0 : else if (op2 == a1 && op1 == a2)
489 0 : op1_op2 = relation_swap (k);
490 : }
491 0 : return def_op1 == VREL_VARYING && def_op2 == VREL_VARYING
492 1049491 : && op1_op2 == VREL_VARYING;
493 : }
494 :
495 : // Return the relation trio for stmt S using query Q.
496 :
497 : relation_trio
498 1067047 : fold_relations (gimple *s, range_query *q)
499 : {
500 1067047 : fold_using_range f;
501 1067047 : fur_relation src (s, q);
502 1067047 : tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
503 1067047 : if (lhs)
504 : {
505 1067047 : value_range vr(TREE_TYPE (lhs));
506 1067047 : if (f.fold_stmt (vr, s, src))
507 1067047 : return src.trio ();
508 1067047 : }
509 0 : return TRIO_VARYING;
510 : }
511 :
512 : // -------------------------------------------------------------------------
513 :
514 : // Adjust the range for a pointer difference where the operands came
515 : // from a memchr.
516 : //
517 : // This notices the following sequence:
518 : //
519 : // def = __builtin_memchr (arg, 0, sz)
520 : // n = def - arg
521 : //
522 : // The range for N can be narrowed to [0, PTRDIFF_MAX - 1].
523 :
524 : static void
525 2618668 : adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
526 : {
527 2618668 : tree op0 = gimple_assign_rhs1 (diff_stmt);
528 2618668 : tree op1 = gimple_assign_rhs2 (diff_stmt);
529 2618668 : tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
530 2618668 : tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
531 2618668 : gimple *call;
532 :
533 2618668 : if (TREE_CODE (op0) == SSA_NAME
534 2585795 : && TREE_CODE (op1) == SSA_NAME
535 2540160 : && (call = SSA_NAME_DEF_STMT (op0))
536 2540160 : && is_gimple_call (call)
537 107838 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
538 85001 : && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
539 84756 : && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
540 84756 : && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
541 84672 : && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
542 84672 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
543 84672 : && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
544 2679942 : && integer_zerop (gimple_call_arg (call, 1)))
545 : {
546 30 : wide_int maxm1 = irange_val_max (ptrdiff_type_node) - 1;
547 30 : res.intersect (int_range<2> (ptrdiff_type_node,
548 60 : wi::zero (TYPE_PRECISION (ptrdiff_type_node)),
549 30 : maxm1));
550 30 : }
551 2618668 : }
552 :
553 : // Adjust the range for an IMAGPART_EXPR.
554 :
555 : static void
556 653465 : adjust_imagpart_expr (vrange &res, const gimple *stmt)
557 : {
558 653465 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
559 :
560 653465 : if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
561 : return;
562 :
563 527693 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
564 527693 : if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
565 : {
566 398791 : switch (gimple_call_internal_fn (def_stmt))
567 : {
568 381213 : case IFN_ADD_OVERFLOW:
569 381213 : case IFN_SUB_OVERFLOW:
570 381213 : case IFN_MUL_OVERFLOW:
571 381213 : case IFN_UADDC:
572 381213 : case IFN_USUBC:
573 381213 : case IFN_ATOMIC_COMPARE_EXCHANGE:
574 381213 : {
575 381213 : int_range<2> r;
576 381213 : r.set_varying (boolean_type_node);
577 381213 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
578 381213 : range_cast (r, type);
579 381213 : res.intersect (r);
580 381213 : }
581 398791 : default:
582 398791 : break;
583 : }
584 398791 : return;
585 : }
586 128902 : if (is_gimple_assign (def_stmt)
587 128902 : && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
588 : {
589 15 : tree cst = gimple_assign_rhs1 (def_stmt);
590 15 : if (TREE_CODE (cst) == COMPLEX_CST
591 15 : && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
592 : {
593 4 : wide_int w = wi::to_wide (TREE_IMAGPART (cst));
594 4 : int_range<1> imag (TREE_TYPE (TREE_IMAGPART (cst)), w, w);
595 4 : res.intersect (imag);
596 4 : }
597 : }
598 : }
599 :
600 : // Adjust the range for a REALPART_EXPR.
601 :
602 : static void
603 655111 : adjust_realpart_expr (vrange &res, const gimple *stmt)
604 : {
605 655111 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
606 :
607 655111 : if (TREE_CODE (name) != SSA_NAME)
608 : return;
609 :
610 522038 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
611 522038 : if (!SSA_NAME_DEF_STMT (name))
612 : return;
613 :
614 522038 : if (is_gimple_assign (def_stmt)
615 522038 : && gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
616 : {
617 10 : tree cst = gimple_assign_rhs1 (def_stmt);
618 10 : if (TREE_CODE (cst) == COMPLEX_CST
619 10 : && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
620 : {
621 0 : wide_int imag = wi::to_wide (TREE_REALPART (cst));
622 0 : int_range<2> tmp (TREE_TYPE (TREE_REALPART (cst)), imag, imag);
623 0 : res.intersect (tmp);
624 0 : }
625 : }
626 : }
627 :
628 : // This function looks for situations when walking the use/def chains
629 : // may provide additional contextual range information not exposed on
630 : // this statement.
631 :
632 : static void
633 188737835 : gimple_range_adjustment (vrange &res, const gimple *stmt)
634 : {
635 188737835 : switch (gimple_expr_code (stmt))
636 : {
637 2618668 : case POINTER_DIFF_EXPR:
638 2618668 : adjust_pointer_diff_expr (as_a <irange> (res), stmt);
639 2618668 : return;
640 :
641 653465 : case IMAGPART_EXPR:
642 653465 : adjust_imagpart_expr (res, stmt);
643 653465 : return;
644 :
645 655111 : case REALPART_EXPR:
646 655111 : adjust_realpart_expr (res, stmt);
647 655111 : return;
648 :
649 : default:
650 : break;
651 : }
652 : }
653 :
654 : // Calculate a range for statement S and return it in R. If NAME is provided it
655 : // represents the SSA_NAME on the LHS of the statement. It is only required
656 : // if there is more than one lhs/output. If a range cannot
657 : // be calculated, return false.
658 :
659 : bool
660 300945343 : fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
661 : {
662 300945343 : bool res = false;
663 : // If name and S are specified, make sure it is an LHS of S.
664 300945343 : gcc_checking_assert (!name || !gimple_get_lhs (s) ||
665 : name == gimple_get_lhs (s));
666 :
667 162436692 : if (!name)
668 162436692 : name = gimple_get_lhs (s);
669 :
670 : // Process addresses and loads from static constructors.
671 300945343 : if (gimple_code (s) == GIMPLE_ASSIGN)
672 : {
673 192504028 : if (gimple_assign_rhs_code (s) == ADDR_EXPR)
674 4054152 : return range_of_address (as_a <prange> (r), s, src);
675 188449876 : if (range_from_readonly_var (r, s))
676 : return true;
677 : }
678 :
679 296789674 : gimple_range_op_handler handler (s);
680 296789674 : if (handler)
681 188738935 : res = range_of_range_op (r, handler, src);
682 108050739 : else if (is_a<gphi *>(s))
683 27388899 : res = range_of_phi (r, as_a<gphi *> (s), src);
684 80661840 : else if (is_a<gcall *>(s))
685 13032272 : res = range_of_call (r, as_a<gcall *> (s), src);
686 67629568 : else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
687 155177 : res = range_of_cond_expr (r, as_a<gassign *> (s), src);
688 :
689 : // If the result is varying, check for basic nonnegativeness.
690 : // Specifically this helps for now with strict enum in cases like
691 : // g++.dg/warn/pr33738.C.
692 229315283 : bool so_p;
693 229315283 : if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ())
694 348871130 : && gimple_stmt_nonnegative_warnv_p (s, &so_p))
695 39656787 : r.set_nonnegative (r.type ());
696 :
697 296789674 : if (!res)
698 : {
699 : // If no name specified or range is unsupported, bail.
700 67474391 : if (!name || !gimple_range_ssa_p (name))
701 50715 : return false;
702 : // We don't understand the stmt, so return the global range.
703 67423676 : gimple_range_global (r, name);
704 67423676 : return true;
705 : }
706 :
707 229315283 : if (r.undefined_p ())
708 : return true;
709 :
710 : // We sometimes get compatible types copied from operands, make sure
711 : // the correct type is being returned.
712 229264172 : if (name && TREE_TYPE (name) != r.type ())
713 : {
714 3297168 : gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
715 3297168 : range_cast (r, TREE_TYPE (name));
716 : }
717 : return true;
718 : }
719 :
720 : // Calculate a range for range_op statement S and return it in R. If any
721 : // If a range cannot be calculated, return false.
722 :
723 : bool
724 188738935 : fold_using_range::range_of_range_op (vrange &r,
725 : gimple_range_op_handler &handler,
726 : fur_source &src)
727 : {
728 188738935 : gcc_checking_assert (handler);
729 188738935 : gimple *s = handler.stmt ();
730 188738935 : tree type = gimple_range_type (s);
731 188738935 : if (!type)
732 : return false;
733 :
734 188738935 : tree lhs = handler.lhs ();
735 188738935 : tree op1 = handler.operand1 ();
736 188738935 : tree op2 = handler.operand2 ();
737 :
738 : // Certain types of builtin functions may have no arguments.
739 188738935 : if (!op1)
740 : {
741 1100 : value_range r1 (type);
742 1100 : if (!handler.fold_range (r, type, r1, r1))
743 0 : r.set_varying (type);
744 1100 : return true;
745 1100 : }
746 :
747 188737835 : value_range range1 (TREE_TYPE (op1));
748 188737835 : value_range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
749 :
750 188737835 : if (src.get_operand (range1, op1))
751 : {
752 188737835 : if (!op2)
753 : {
754 : // Fold range, and register any dependency if available.
755 37175500 : value_range r2 (type);
756 37175500 : r2.set_varying (type);
757 37175500 : if (!handler.fold_range (r, type, range1, r2))
758 262506 : r.set_varying (type);
759 37175500 : if (lhs && gimple_range_ssa_p (op1))
760 : {
761 54766541 : if (src.gori_ssa ())
762 20371682 : src.gori_ssa ()->register_dependency (lhs, op1);
763 34394826 : relation_kind rel;
764 34394826 : rel = handler.lhs_op1_relation (r, range1, range1);
765 34394826 : if (rel != VREL_VARYING)
766 25294744 : src.register_relation (s, rel, lhs, op1);
767 : }
768 37175500 : }
769 151562335 : else if (src.get_operand (range2, op2))
770 : {
771 151562335 : relation_kind rel = src.query_relation (op1, op2);
772 151562335 : if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
773 : {
774 123 : fprintf (dump_file, " folding with relation ");
775 123 : print_generic_expr (dump_file, op1, TDF_SLIM);
776 123 : print_relation (dump_file, rel);
777 123 : print_generic_expr (dump_file, op2, TDF_SLIM);
778 123 : fputc ('\n', dump_file);
779 : }
780 : // Fold range, and register any dependency if available.
781 151562335 : if (!handler.fold_range (r, type, range1, range2,
782 : relation_trio::op1_op2 (rel)))
783 0 : r.set_varying (type);
784 151562335 : if (irange::supports_p (type))
785 137897750 : relation_fold_and_or (as_a <irange> (r), s, src, range1, range2);
786 151562335 : if (lhs)
787 : {
788 155067740 : if (src.gori_ssa ())
789 : {
790 60055007 : src.gori_ssa ()->register_dependency (lhs, op1);
791 120110014 : src.gori_ssa ()->register_dependency (lhs, op2);
792 : }
793 95012673 : if (gimple_range_ssa_p (op1))
794 : {
795 92410015 : relation_kind rel2 = handler.lhs_op1_relation (r, range1,
796 92410015 : range2, rel);
797 92410015 : if (rel2 != VREL_VARYING)
798 40983753 : src.register_relation (s, rel2, lhs, op1);
799 : }
800 95012673 : if (gimple_range_ssa_p (op2))
801 : {
802 37131565 : relation_kind rel2 = handler.lhs_op2_relation (r, range1,
803 37131565 : range2, rel);
804 37131565 : if (rel2 != VREL_VARYING)
805 2411240 : src.register_relation (s, rel2, lhs, op2);
806 : }
807 : }
808 : // Check for an existing BB, as we maybe asked to fold an
809 : // artificial statement not in the CFG.
810 56549662 : else if (is_a<gcond *> (s) && gimple_bb (s))
811 : {
812 47940842 : basic_block bb = gimple_bb (s);
813 47940842 : edge e0 = EDGE_SUCC (bb, 0);
814 : /* During RTL expansion one of the edges can be removed
815 : if expansion proves the jump is unconditional. */
816 47940842 : edge e1 = single_succ_p (bb) ? NULL : EDGE_SUCC (bb, 1);
817 :
818 47940842 : gcc_checking_assert (e1 || currently_expanding_to_rtl);
819 47940842 : if (!single_pred_p (e0->dest))
820 12033463 : e0 = NULL;
821 47940842 : if (e1 && !single_pred_p (e1->dest))
822 : e1 = NULL;
823 47940842 : src.register_outgoing_edges (as_a<gcond *> (s),
824 : as_a <irange> (r), e0, e1);
825 : }
826 : }
827 : else
828 0 : r.set_varying (type);
829 : }
830 : else
831 0 : r.set_varying (type);
832 : // Make certain range-op adjustments that aren't handled any other way.
833 188737835 : gimple_range_adjustment (r, s);
834 188737835 : return true;
835 188737835 : }
836 :
837 : // Calculate the range of an assignment containing an ADDR_EXPR.
838 : // Return the range in R.
839 : // If a range cannot be calculated, set it to VARYING and return true.
840 :
841 : bool
842 4054152 : fold_using_range::range_of_address (prange &r, gimple *stmt, fur_source &src)
843 : {
844 4054152 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
845 4054152 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
846 :
847 4054152 : bool strict_overflow_p;
848 4054152 : tree expr = gimple_assign_rhs1 (stmt);
849 4054152 : poly_int64 bitsize, bitpos;
850 4054152 : tree offset;
851 4054152 : machine_mode mode;
852 4054152 : int unsignedp, reversep, volatilep;
853 4054152 : tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
854 : &bitpos, &offset, &mode, &unsignedp,
855 : &reversep, &volatilep);
856 :
857 :
858 4054152 : if (base != NULL_TREE
859 4054152 : && TREE_CODE (base) == MEM_REF
860 7965487 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
861 : {
862 3911289 : tree ssa = TREE_OPERAND (base, 0);
863 3911289 : tree lhs = gimple_get_lhs (stmt);
864 6328443 : if (lhs && gimple_range_ssa_p (ssa) && src.gori_ssa ())
865 2417154 : src.gori_ssa ()->register_dependency (lhs, ssa);
866 3911289 : src.get_operand (r, ssa);
867 3911289 : range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
868 :
869 3911289 : poly_offset_int off = 0;
870 3911289 : bool off_cst = false;
871 3911289 : if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
872 : {
873 3830055 : off = mem_ref_offset (base);
874 3830055 : if (offset)
875 48 : off += poly_offset_int::from (wi::to_poly_wide (offset),
876 48 : SIGNED);
877 3830055 : off <<= LOG2_BITS_PER_UNIT;
878 3830055 : off += bitpos;
879 : off_cst = true;
880 : }
881 : /* If &X->a is equal to X, the range of X is the result. */
882 3830055 : if (off_cst && known_eq (off, 0))
883 1398980 : return true;
884 2512309 : else if (flag_delete_null_pointer_checks
885 2512309 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
886 : {
887 : /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
888 : allow going from non-NULL pointer to NULL. */
889 2510921 : if (r.undefined_p ()
890 5021842 : || !r.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr)))))
891 : {
892 : /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
893 : using POINTER_PLUS_EXPR if off_cst and just fall back to
894 : this. */
895 1834306 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
896 1834306 : return true;
897 : }
898 : }
899 : /* If MEM_REF has a "positive" offset, consider it non-NULL
900 : always, for -fdelete-null-pointer-checks also "negative"
901 : ones. Punt for unknown offsets (e.g. variable ones). */
902 678003 : if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
903 677790 : && off_cst
904 618857 : && known_ne (off, 0)
905 1296860 : && (flag_delete_null_pointer_checks || known_gt (off, 0)))
906 : {
907 618857 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
908 618857 : return true;
909 : }
910 59146 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
911 59146 : return true;
912 : }
913 :
914 : // Handle "= &a".
915 142863 : if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
916 : {
917 141718 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
918 141718 : return true;
919 : }
920 :
921 : // Otherwise return varying.
922 1145 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
923 1145 : return true;
924 : }
925 :
926 : /* If TYPE is a pointer, return false. Otherwise, add zero of TYPE (which must
927 : be an integer) to R and return true. */
928 :
929 : static bool
930 1334 : range_from_missing_constructor_part (vrange &r, tree type)
931 : {
932 1334 : if (POINTER_TYPE_P (type))
933 : return false;
934 1047 : gcc_checking_assert (irange::supports_p (type));
935 1047 : wide_int zero = wi::zero (TYPE_PRECISION (type));
936 1047 : r.union_ (int_range<1> (type, zero, zero));
937 1047 : return true;
938 1047 : }
939 :
940 : // One step of fold_using_range::range_from_readonly_var. Process expressions
941 : // in COMPS which together load a value of TYPE, from index I to 0 according to
942 : // the corresponding static initializer in CST which should be either a scalar
943 : // invariant or a constructor. Currently TYPE must be either a pointer or an
944 : // integer. If TYPE is a pointer, return true if all potentially loaded values
945 : // are known not to be zero and false if any of them can be zero. Otherwise
946 : // return true if it is possible to add all constants which can be loaded from
947 : // CST (which must be storable to TYPE) to R and do so.
948 : // TODO: Add support for franges.
949 :
950 : static bool
951 788114 : range_from_readonly_load (vrange &r, tree type, tree cst,
952 : const vec <tree> &comps, unsigned i)
953 : {
954 799613 : if (i == 0)
955 : {
956 681138 : if (!useless_type_conversion_p (type, TREE_TYPE (cst)))
957 : return false;
958 :
959 681138 : if (POINTER_TYPE_P (type))
960 : {
961 145179 : bool strict_overflow_p;
962 145179 : return tree_single_nonzero_warnv_p (cst, &strict_overflow_p);
963 : }
964 :
965 535959 : if (TREE_CODE (cst) != INTEGER_CST)
966 : return false;
967 :
968 535888 : wide_int wi_cst = wi::to_wide (cst);
969 535888 : r.union_ (int_range<1> (type, wi_cst, wi_cst));
970 535888 : return true;
971 535888 : }
972 : /* TODO: Perhaps handle RAW_DATA_CST too. */
973 118475 : if (TREE_CODE (cst) != CONSTRUCTOR)
974 : return false;
975 :
976 117728 : i--;
977 117728 : tree expr = comps[i];
978 117728 : unsigned ix;
979 117728 : tree index, val;
980 :
981 117728 : if (TREE_CODE (expr) == COMPONENT_REF)
982 : {
983 11548 : tree ref_fld = TREE_OPERAND (expr, 1);
984 18684 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (cst), ix, index, val)
985 : {
986 18635 : if (index != ref_fld)
987 7136 : continue;
988 : return range_from_readonly_load (r, type, val, comps, i);
989 : }
990 49 : if (TREE_CODE (TREE_TYPE (cst)) == RECORD_TYPE)
991 9 : return range_from_missing_constructor_part (r, type);
992 : else
993 : /* Missing constructor of a union field just isn't like other missing
994 : constructor parts. */
995 : return false;
996 : }
997 :
998 106180 : gcc_assert (TREE_CODE (expr) == ARRAY_REF);
999 106180 : tree op1 = TREE_OPERAND (expr, 1);
1000 :
1001 106180 : if (TREE_CODE (op1) == INTEGER_CST)
1002 : {
1003 3356 : unsigned ctor_idx;
1004 3356 : val = get_array_ctor_element_at_index (cst, wi::to_offset (op1),
1005 : &ctor_idx);
1006 3356 : if (!val)
1007 : {
1008 96 : if (ctor_idx < CONSTRUCTOR_NELTS (cst))
1009 : return false;
1010 96 : return range_from_missing_constructor_part (r, type);
1011 : }
1012 3260 : return range_from_readonly_load (r, type, val, comps, i);
1013 : }
1014 :
1015 102824 : tree arr_type = TREE_TYPE (cst);
1016 102824 : tree domain = TYPE_DOMAIN (arr_type);
1017 102824 : if (!TYPE_MIN_VALUE (domain)
1018 102824 : || !TYPE_MAX_VALUE (domain)
1019 102824 : || !tree_fits_uhwi_p (TYPE_MIN_VALUE (domain))
1020 205648 : || !tree_fits_uhwi_p (TYPE_MAX_VALUE (domain)))
1021 : return false;
1022 102749 : unsigned HOST_WIDE_INT needed_count
1023 102749 : = (tree_to_uhwi (TYPE_MAX_VALUE (domain))
1024 102749 : - tree_to_uhwi (TYPE_MIN_VALUE (domain)) + 1);
1025 205438 : if (CONSTRUCTOR_NELTS (cst) < needed_count)
1026 : {
1027 1229 : if (!range_from_missing_constructor_part (r, type))
1028 : return false;
1029 : }
1030 :
1031 783604 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (cst), ix, index, val)
1032 : {
1033 : /* TODO: If the array index in the expr is an SSA_NAME with a known
1034 : range, we could use just values loaded from the corresponding array
1035 : elements. */
1036 682019 : if (!range_from_readonly_load (r, type, val, comps, i))
1037 : return false;
1038 : }
1039 :
1040 : return true;
1041 : }
1042 :
1043 : // Attempt to calculate the range of value loaded by STMT (which must be an
1044 : // assignment) if it is a load from a read-only aggregate variable. If
1045 : // successful, return true and set the discovered range in R. Otherwise return
1046 : // false and leave R untouched.
1047 :
1048 : bool
1049 188449876 : fold_using_range::range_from_readonly_var (vrange &r, gimple *stmt)
1050 : {
1051 188449876 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
1052 188449876 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1053 : /* TODO: Add support for frange. */
1054 188449876 : if (!irange::supports_p (type)
1055 188449876 : && !prange::supports_p (type))
1056 : return false;
1057 :
1058 178256880 : unsigned HOST_WIDE_INT limit = param_vrp_cstload_limit;
1059 178256880 : if (!limit)
1060 : return false;
1061 :
1062 178237670 : tree t = gimple_assign_rhs1 (stmt);
1063 178237670 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
1064 : return false;
1065 178237670 : limit *= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
1066 :
1067 178237670 : unsigned count = 0;
1068 178237670 : while (TREE_CODE (t) == ARRAY_REF
1069 227074104 : || TREE_CODE (t) == COMPONENT_REF)
1070 : {
1071 48836434 : count++;
1072 48836434 : t = TREE_OPERAND (t, 0);
1073 : }
1074 178237670 : if (!count
1075 31768216 : || (TREE_CODE (t) != VAR_DECL
1076 31768216 : && TREE_CODE (t) != CONST_DECL))
1077 : return false;
1078 :
1079 9088730 : if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (t))
1080 9088730 : || tree_to_uhwi (DECL_SIZE_UNIT (t)) > limit)
1081 : return false;
1082 :
1083 : /* TODO: We perhaps should try to handle at least some cases when the
1084 : declaration is wrapped in a MEM_REF, but we need to be careful to look at
1085 : the right part of the constructor then. */
1086 8087250 : tree ctor = ctor_for_folding (t);
1087 8087250 : if (!ctor
1088 8087243 : || TREE_CODE (ctor) != CONSTRUCTOR)
1089 : return false;
1090 :
1091 102835 : t = gimple_assign_rhs1 (stmt);
1092 102835 : auto_vec <tree, 4> comps;
1093 102835 : comps.safe_grow (count, true);
1094 102835 : int i = 0;
1095 102835 : while (TREE_CODE (t) == ARRAY_REF
1096 210718 : || TREE_CODE (t) == COMPONENT_REF)
1097 : {
1098 107883 : comps[i] = t;
1099 107883 : t = TREE_OPERAND (t, 0);
1100 107883 : i++;
1101 : }
1102 :
1103 102835 : value_range tmp (type);
1104 102835 : bool res = range_from_readonly_load (tmp, type, ctor, comps, count);
1105 102835 : if (res)
1106 : {
1107 101517 : if (POINTER_TYPE_P (type))
1108 24251 : r.set_nonzero (type);
1109 : else
1110 77266 : r = tmp;
1111 : }
1112 102835 : return res;
1113 102835 : }
1114 :
1115 : // Calculate a range for phi statement S and return it in R.
1116 : // If a range cannot be calculated, return false.
1117 :
1118 : bool
1119 27388899 : fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
1120 : {
1121 27388899 : tree phi_def = gimple_phi_result (phi);
1122 27388899 : tree type = gimple_range_type (phi);
1123 27388899 : value_range arg_range (type);
1124 27388899 : value_range equiv_range (type);
1125 27388899 : unsigned x;
1126 :
1127 27388899 : if (!type)
1128 : return false;
1129 :
1130 : // Track if all executable arguments are the same.
1131 27388899 : tree single_arg = NULL_TREE;
1132 27388899 : bool seen_arg = false;
1133 :
1134 27388899 : relation_oracle *oracle = &(src.query()->relation ());
1135 : // Start with an empty range, unioning in each argument's range.
1136 27388899 : r.set_undefined ();
1137 70290405 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1138 : {
1139 56258998 : tree arg = gimple_phi_arg_def (phi, x);
1140 : // An argument that is the same as the def provides no new range.
1141 56258998 : if (arg == phi_def)
1142 19411 : continue;
1143 :
1144 56239587 : edge e = gimple_phi_arg_edge (phi, x);
1145 :
1146 : // Get the range of the argument on its edge.
1147 56239587 : src.get_phi_operand (arg_range, arg, e);
1148 :
1149 56239587 : if (!arg_range.undefined_p ())
1150 : {
1151 : // Register potential dependencies for stale value tracking.
1152 : // Likewise, if the incoming PHI argument is equivalent to this
1153 : // PHI definition, it provides no new info. Accumulate these ranges
1154 : // in case all arguments are equivalences.
1155 55975205 : if (oracle->query (e, arg, phi_def) == VREL_EQ)
1156 430476 : equiv_range.union_(arg_range);
1157 : else
1158 55544729 : r.union_ (arg_range);
1159 :
1160 92209138 : if (gimple_range_ssa_p (arg) && src.gori_ssa ())
1161 36233927 : src.gori_ssa ()->register_dependency (phi_def, arg);
1162 : }
1163 :
1164 : // Track if all arguments are the same.
1165 56239587 : if (!seen_arg)
1166 : {
1167 : seen_arg = true;
1168 : single_arg = arg;
1169 : }
1170 28850688 : else if (single_arg != arg)
1171 27622535 : single_arg = NULL_TREE;
1172 :
1173 : // Once the value reaches varying, stop looking.
1174 56239587 : if (r.varying_p () && single_arg == NULL_TREE)
1175 : break;
1176 : }
1177 :
1178 : // If all arguments were equivalences, use the equivalence ranges as no
1179 : // arguments were processed.
1180 27388899 : if (r.undefined_p () && !equiv_range.undefined_p ())
1181 281944 : r = equiv_range;
1182 :
1183 : // If the PHI boils down to a single effective argument, look at it.
1184 27388899 : if (single_arg)
1185 : {
1186 : // Symbolic arguments can be equivalences.
1187 2537123 : if (gimple_range_ssa_p (single_arg))
1188 : {
1189 : // Only allow the equivalence if the PHI definition does not
1190 : // dominate any incoming edge for SINGLE_ARG.
1191 : // See PR 108139 and 109462.
1192 2043894 : basic_block bb = gimple_bb (phi);
1193 2043894 : if (!dom_info_available_p (CDI_DOMINATORS))
1194 : single_arg = NULL;
1195 : else
1196 4313859 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1197 2275232 : if (gimple_phi_arg_def (phi, x) == single_arg
1198 4538965 : && dominated_by_p (CDI_DOMINATORS,
1199 2263733 : gimple_phi_arg_edge (phi, x)->src,
1200 : bb))
1201 : {
1202 : single_arg = NULL;
1203 : break;
1204 : }
1205 2042885 : if (single_arg)
1206 2038627 : src.register_relation (phi, VREL_EQ, phi_def, single_arg);
1207 : }
1208 493229 : else if (src.get_operand (arg_range, single_arg)
1209 986458 : && arg_range.singleton_p ())
1210 : {
1211 : // Numerical arguments that are a constant can be returned as
1212 : // the constant. This can help fold later cases where even this
1213 : // constant might have been UNDEFINED via an unreachable edge.
1214 474848 : r = arg_range;
1215 474848 : return true;
1216 : }
1217 : }
1218 :
1219 : // Incorporate any global value. If a PHI analysis phase was run, there may
1220 : // be a restricted global range already. Query the range with no context
1221 : // to get a global range.
1222 :
1223 : // If SCEV is available, query if this PHI has any known values.
1224 26914051 : if (scev_initialized_p ()
1225 26914051 : && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
1226 : {
1227 11432785 : class loop *l = loop_containing_stmt (phi);
1228 11432785 : if (l && loop_outer (l))
1229 : {
1230 8832582 : value_range loop_range (type);
1231 8832582 : range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
1232 8832582 : if (!loop_range.varying_p ())
1233 : {
1234 2478311 : if (dump_file && (dump_flags & TDF_DETAILS))
1235 : {
1236 14220 : fprintf (dump_file, "Loops range found for ");
1237 14220 : print_generic_expr (dump_file, phi_def, TDF_SLIM);
1238 14220 : fprintf (dump_file, ": ");
1239 14220 : loop_range.dump (dump_file);
1240 14220 : fprintf (dump_file, " and calculated range :");
1241 14220 : r.dump (dump_file);
1242 14220 : fprintf (dump_file, "\n");
1243 : }
1244 2478311 : r.intersect (loop_range);
1245 : }
1246 8832582 : }
1247 : }
1248 :
1249 : return true;
1250 27388899 : }
1251 :
1252 : // Calculate a range for call statement S and return it in R.
1253 : // If a range cannot be calculated, return false.
1254 :
1255 : bool
1256 13032272 : fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1257 : {
1258 13032272 : tree type = gimple_range_type (call);
1259 13032272 : if (!type)
1260 : return false;
1261 :
1262 13032272 : tree lhs = gimple_call_lhs (call);
1263 13032272 : bool strict_overflow_p;
1264 :
1265 13032272 : if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
1266 43056 : r.set_nonnegative (type);
1267 12989216 : else if (gimple_call_nonnull_result_p (call)
1268 12989216 : || gimple_call_nonnull_arg (call))
1269 650978 : r.set_nonzero (type);
1270 : else
1271 12338238 : r.set_varying (type);
1272 :
1273 13032272 : tree callee = gimple_call_fndecl (call);
1274 13032272 : if (callee
1275 13032272 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)), type))
1276 : {
1277 11937475 : value_range val;
1278 11937475 : if (ipa_return_value_range (val, callee))
1279 : {
1280 611122 : r.intersect (val);
1281 611122 : if (dump_file && (dump_flags & TDF_DETAILS))
1282 : {
1283 28 : fprintf (dump_file, "Using return value range of ");
1284 28 : print_generic_expr (dump_file, callee, TDF_SLIM);
1285 28 : fprintf (dump_file, ": ");
1286 28 : val.dump (dump_file);
1287 28 : fprintf (dump_file, "\n");
1288 : }
1289 : }
1290 11937475 : }
1291 :
1292 : // If there is an LHS, intersect that with what is known.
1293 13032272 : if (gimple_range_ssa_p (lhs))
1294 : {
1295 13032272 : value_range def (TREE_TYPE (lhs));
1296 13032272 : gimple_range_global (def, lhs);
1297 13032272 : r.intersect (def);
1298 13032272 : }
1299 : return true;
1300 : }
1301 :
1302 : // Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
1303 : // to further resolve R1 and R2 if there are any dependencies between
1304 : // OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
1305 : // as the origination source location for operands..
1306 : // Effectively, use COND an the edge condition and solve for OP1 on the true
1307 : // edge and OP2 on the false edge.
1308 :
1309 : bool
1310 155177 : fold_using_range::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
1311 : tree op1, tree op2, fur_source &src)
1312 : {
1313 155177 : if (!src.gori () || !src.gori_ssa ())
1314 : return false;
1315 :
1316 115702 : tree ssa1 = gimple_range_ssa_p (op1);
1317 115702 : tree ssa2 = gimple_range_ssa_p (op2);
1318 115702 : if (!ssa1 && !ssa2)
1319 : return false;
1320 105644 : if (TREE_CODE (cond) != SSA_NAME)
1321 : return false;
1322 234439 : gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1323 105551 : if (!cond_def
1324 105551 : || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
1325 : return false;
1326 100773 : tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1327 100773 : if (!value_range::supports_type_p (type)
1328 201542 : || !range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1329 : return false;
1330 100769 : range_op_handler hand (gimple_assign_rhs_code (cond_def));
1331 100769 : if (!hand)
1332 : return false;
1333 :
1334 100769 : tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
1335 201538 : tree c2 = gimple_range_ssa_p (gimple_assign_rhs2 (cond_def));
1336 :
1337 : // Only solve if there is one SSA name in the condition.
1338 100769 : if ((!c1 && !c2) || (c1 && c2))
1339 : return false;
1340 :
1341 : // Pick up the current values of each part of the condition.
1342 26289 : tree rhs1 = gimple_assign_rhs1 (cond_def);
1343 26289 : tree rhs2 = gimple_assign_rhs2 (cond_def);
1344 26289 : value_range cl (TREE_TYPE (rhs1));
1345 26289 : value_range cr (TREE_TYPE (rhs2));
1346 26289 : src.get_operand (cl, rhs1);
1347 26289 : src.get_operand (cr, rhs2);
1348 :
1349 26289 : tree cond_name = c1 ? c1 : c2;
1350 26289 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
1351 :
1352 : // Evaluate the value of COND_NAME on the true and false edges, using either
1353 : // the op1 or op2 routines based on its location.
1354 26289 : value_range cond_true (type), cond_false (type);
1355 26289 : if (c1)
1356 : {
1357 26289 : if (!hand.op1_range (cond_false, type, range_false (), cr))
1358 : return false;
1359 26289 : if (!hand.op1_range (cond_true, type, range_true (), cr))
1360 : return false;
1361 26289 : cond_false.intersect (cl);
1362 26289 : cond_true.intersect (cl);
1363 : }
1364 : else
1365 : {
1366 0 : if (!hand.op2_range (cond_false, type, range_false (), cl))
1367 : return false;
1368 0 : if (!hand.op2_range (cond_true, type, range_true (), cl))
1369 : return false;
1370 0 : cond_false.intersect (cr);
1371 0 : cond_true.intersect (cr);
1372 : }
1373 :
1374 : // Now solve for SSA1 or SSA2 if they are in the dependency chain.
1375 48470 : if (ssa1 && src.gori_ssa()->in_chain_p (ssa1, cond_name))
1376 : {
1377 885 : value_range tmp1 (TREE_TYPE (ssa1));
1378 1770 : if (src.gori ()->compute_operand_range (tmp1, def_stmt, cond_true,
1379 : ssa1, src))
1380 545 : r1.intersect (tmp1);
1381 885 : }
1382 43443 : if (ssa2 && src.gori_ssa ()->in_chain_p (ssa2, cond_name))
1383 : {
1384 265 : value_range tmp2 (TREE_TYPE (ssa2));
1385 530 : if (src.gori ()->compute_operand_range (tmp2, def_stmt, cond_false,
1386 : ssa2, src))
1387 213 : r2.intersect (tmp2);
1388 265 : }
1389 : // If the same name is specified in the condition and COND_EXPR,
1390 : // combine the calculated condition range and the other one provided. ie:
1391 : // c_1 = b_2 < 10
1392 : // f_3 = c_1 ? 0 : b_2
1393 : // With b_2 providing the false value, the value of f_3 will be
1394 : // either 0 UNION (0 = b_2 < 10), which is [-INF, 9].
1395 : // COND_EXPR is
1396 26289 : if (ssa1 && cond_name == ssa1)
1397 1998 : r1 = cond_true;
1398 24291 : else if (ssa2 && cond_name == ssa2)
1399 2870 : r2 = cond_false;
1400 : return true;
1401 26289 : }
1402 :
1403 : // Calculate a range for COND_EXPR statement S and return it in R.
1404 : // If a range cannot be calculated, return false.
1405 :
1406 : bool
1407 155177 : fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1408 : {
1409 155177 : tree cond = gimple_assign_rhs1 (s);
1410 155177 : tree op1 = gimple_assign_rhs2 (s);
1411 155177 : tree op2 = gimple_assign_rhs3 (s);
1412 :
1413 155177 : tree type = gimple_range_type (s);
1414 155177 : if (!type)
1415 : return false;
1416 :
1417 155177 : value_range range1 (TREE_TYPE (op1));
1418 155177 : value_range range2 (TREE_TYPE (op2));
1419 155177 : value_range cond_range (TREE_TYPE (cond));
1420 155177 : gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1421 155177 : gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1422 155177 : src.get_operand (cond_range, cond);
1423 155177 : src.get_operand (range1, op1);
1424 155177 : src.get_operand (range2, op2);
1425 :
1426 : // Try to see if there is a dependence between the COND and either operand
1427 155177 : if (condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1428 26289 : if (dump_file && (dump_flags & TDF_DETAILS))
1429 : {
1430 564 : fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1431 564 : range1.dump(dump_file);
1432 564 : fprintf (dump_file, " and Range op2: ");
1433 564 : range2.dump(dump_file);
1434 564 : fprintf (dump_file, "\n");
1435 : }
1436 :
1437 : // If the condition is known, choose the appropriate expression.
1438 155177 : if (cond_range.singleton_p ())
1439 : {
1440 : // False, pick second operand.
1441 2119 : if (cond_range.zero_p ())
1442 1064 : r = range2;
1443 : else
1444 1055 : r = range1;
1445 : }
1446 : else
1447 : {
1448 153058 : r = range1;
1449 153058 : r.union_ (range2);
1450 : }
1451 155177 : gcc_checking_assert (r.undefined_p ()
1452 : || range_compatible_p (r.type (), type));
1453 155177 : return true;
1454 155177 : }
1455 :
1456 : // If SCEV has any information about phi node NAME, return it as a range in R.
1457 :
1458 : void
1459 8832582 : fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1460 : class loop *l, gphi *phi,
1461 : fur_source &src)
1462 : {
1463 8832582 : static bool in_scev_call = false;
1464 8832582 : gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1465 : // Avoid SCEV callbacks causing infinite recursion.
1466 8832582 : if (in_scev_call)
1467 383544 : r.set_varying (TREE_TYPE (name));
1468 : // SCEV currently invokes get_range_query () for values. If the query
1469 : // being passed in is not the same SCEV will use, do not invoke SCEV.
1470 : // This can be remove if/when SCEV uses a passed in range-query.
1471 16898076 : else if (src.query () != get_range_query (cfun))
1472 : {
1473 1770125 : r.set_varying (TREE_TYPE (name));
1474 : // Report the msmatch if SRC is not the global query. The cache
1475 : // uses a global query and would provide numerous false positives.
1476 114 : if (dump_file && (dump_flags & TDF_DETAILS)
1477 1770191 : && src.query () != get_global_range_query ())
1478 39 : fprintf (dump_file,
1479 : "fold_using-range:: SCEV not invoked due to mismatched queries\n");
1480 : }
1481 : else
1482 : {
1483 6678913 : in_scev_call = true;
1484 6678913 : if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1485 2464 : r.set_varying (TREE_TYPE (name));
1486 6678913 : in_scev_call = false;
1487 : }
1488 8832582 : }
1489 :
1490 : // -----------------------------------------------------------------------
1491 :
1492 : // Check if an && or || expression can be folded based on relations. ie
1493 : // c_2 = a_6 > b_7
1494 : // c_3 = a_6 < b_7
1495 : // c_4 = c_2 && c_3
1496 : // c_2 and c_3 can never be true at the same time,
1497 : // Therefore c_4 can always resolve to false based purely on the relations.
1498 :
1499 : void
1500 137897750 : fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1501 : fur_source &src, vrange &op1,
1502 : vrange &op2)
1503 : {
1504 : // No queries or already folded.
1505 137897750 : if (!src.gori () || lhs_range.singleton_p ())
1506 47412358 : return;
1507 :
1508 : // Only care about AND and OR expressions.
1509 90485392 : enum tree_code code = gimple_expr_code (s);
1510 90485392 : bool is_and = false;
1511 90485392 : if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1512 : is_and = true;
1513 86780792 : else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1514 : return;
1515 :
1516 5195666 : gimple_range_op_handler handler (s);
1517 5195666 : tree lhs = handler.lhs ();
1518 5195666 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1519 5195666 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1520 :
1521 : // Deal with || and && only when there is a full set of symbolics.
1522 5195480 : if (!lhs || !ssa1 || !ssa2
1523 2789133 : || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1524 1914501 : || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1525 7108931 : || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1526 : return;
1527 :
1528 : // Now we know its a boolean AND or OR expression with boolean operands.
1529 : // Ideally we search dependencies for common names, and see what pops out.
1530 : // until then, simply try to resolve direct dependencies.
1531 :
1532 1910586 : gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1533 1910586 : gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1534 :
1535 1910586 : gimple_range_op_handler handler1 (ssa1_stmt);
1536 1910586 : gimple_range_op_handler handler2 (ssa2_stmt);
1537 :
1538 : // If either handler is not present, no relation can be found.
1539 1910586 : if (!handler1 || !handler2)
1540 135686 : return;
1541 :
1542 : // Both stmts will need to have 2 ssa names in the stmt.
1543 1774900 : tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1544 1774900 : tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1545 1774900 : tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1546 1774900 : tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1547 :
1548 1774900 : if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1549 : return;
1550 :
1551 196411 : if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1552 : return;
1553 :
1554 : // Make sure they are the same dependencies, and detect the order of the
1555 : // relationship.
1556 184692 : bool reverse_op2 = true;
1557 184692 : if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1558 : reverse_op2 = false;
1559 184570 : else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1560 : return;
1561 :
1562 122 : int_range<2> bool_one = range_true ();
1563 122 : relation_kind relation1 = handler1.op1_op2_relation (bool_one, op1, op2);
1564 122 : relation_kind relation2 = handler2.op1_op2_relation (bool_one, op1, op2);
1565 122 : if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1566 : return;
1567 :
1568 86 : if (reverse_op2)
1569 0 : relation2 = relation_negate (relation2);
1570 :
1571 : // x && y is false if the relation intersection of the true cases is NULL.
1572 86 : if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1573 0 : lhs_range = range_false (boolean_type_node);
1574 : // x || y is true if the union of the true cases is NO-RELATION..
1575 : // ie, one or the other being true covers the full range of possibilities.
1576 86 : else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1577 0 : lhs_range = bool_one;
1578 : else
1579 86 : return;
1580 :
1581 0 : range_cast (lhs_range, TREE_TYPE (lhs));
1582 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1583 : {
1584 0 : fprintf (dump_file, " Relation adjustment: ");
1585 0 : print_generic_expr (dump_file, ssa1, TDF_SLIM);
1586 0 : fprintf (dump_file, " and ");
1587 0 : print_generic_expr (dump_file, ssa2, TDF_SLIM);
1588 0 : fprintf (dump_file, " combine to produce ");
1589 0 : lhs_range.dump (dump_file);
1590 0 : fputc ('\n', dump_file);
1591 : }
1592 :
1593 : return;
1594 122 : }
1595 :
1596 : // Register any outgoing edge relations from a conditional branch.
1597 :
1598 : void
1599 71532756 : fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1600 : edge e0, edge e1)
1601 : {
1602 71532756 : int_range<2> e0_range, e1_range;
1603 71532756 : tree name;
1604 71532756 : basic_block bb = gimple_bb (s);
1605 :
1606 71532756 : gimple_range_op_handler handler (s);
1607 71532756 : if (!handler)
1608 : return;
1609 :
1610 71521968 : if (e0)
1611 : {
1612 : // If this edge is never taken, ignore it.
1613 59488505 : gcond_edge_range (e0_range, e0);
1614 59488505 : e0_range.intersect (lhs_range);
1615 59488505 : if (e0_range.undefined_p ())
1616 26239455 : e0 = NULL;
1617 : }
1618 :
1619 71521968 : if (e1)
1620 : {
1621 : // If this edge is never taken, ignore it.
1622 52286083 : gcond_edge_range (e1_range, e1);
1623 52286083 : e1_range.intersect (lhs_range);
1624 52286083 : if (e1_range.undefined_p ())
1625 30018077 : e1 = NULL;
1626 : }
1627 :
1628 71521968 : if (!e0 && !e1)
1629 : return;
1630 :
1631 : // First, register the gcond itself. This will catch statements like
1632 : // if (a_2 < b_5)
1633 68318599 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1634 68318599 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1635 68318599 : value_range r1,r2;
1636 68318599 : if (ssa1 && ssa2)
1637 : {
1638 20710452 : r1.set_varying (TREE_TYPE (ssa1));
1639 20710452 : r2.set_varying (TREE_TYPE (ssa2));
1640 20710452 : if (e0)
1641 : {
1642 13942551 : relation_kind relation = handler.op1_op2_relation (e0_range, r1, r2);
1643 13942551 : if (relation != VREL_VARYING)
1644 13866381 : register_relation (e0, relation, ssa1, ssa2);
1645 : }
1646 20710452 : if (e1)
1647 : {
1648 12083560 : relation_kind relation = handler.op1_op2_relation (e1_range, r1, r2);
1649 12083560 : if (relation != VREL_VARYING)
1650 12025143 : register_relation (e1, relation, ssa1, ssa2);
1651 : }
1652 : }
1653 :
1654 : // Outgoing relations of GORI exports require a gori engine.
1655 123375160 : if (!gori_ssa ())
1656 13262050 : return;
1657 :
1658 : // Now look for other relations in the exports. This will find stmts
1659 : // leading to the condition such as:
1660 : // c_2 = a_4 < b_7
1661 : // if (c_2)
1662 175716566 : FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb, name)
1663 : {
1664 120660017 : if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1665 115205950 : continue;
1666 9081622 : gimple *stmt = SSA_NAME_DEF_STMT (name);
1667 9081622 : gimple_range_op_handler handler (stmt);
1668 9081622 : if (!handler)
1669 3627555 : continue;
1670 5454067 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1671 5454067 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1672 5454067 : value_range r (TREE_TYPE (name));
1673 5454067 : if (ssa1 && ssa2)
1674 : {
1675 2338827 : r1.set_varying (TREE_TYPE (ssa1));
1676 2338827 : r2.set_varying (TREE_TYPE (ssa2));
1677 1432723 : if (e0 && gori ()->edge_range_p (r, e0, name, *m_query)
1678 3737794 : && r.singleton_p ())
1679 : {
1680 1280623 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1681 1280623 : if (relation != VREL_VARYING)
1682 431299 : register_relation (e0, relation, ssa1, ssa2);
1683 : }
1684 1478127 : if (e1 && gori ()->edge_range_p (r, e1, name, *m_query)
1685 3771682 : && r.singleton_p ())
1686 : {
1687 1053248 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1688 1053248 : if (relation != VREL_VARYING)
1689 149570 : register_relation (e1, relation, ssa1, ssa2);
1690 : }
1691 : }
1692 5454067 : }
1693 71532756 : }
|