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 472281038 : fur_source::fur_source (range_query *q)
58 : {
59 472281038 : if (q)
60 472280073 : m_query = q;
61 : else
62 1930 : m_query = get_range_query (cfun);
63 472281038 : m_depend_p = false;
64 472281038 : }
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 5323184 : fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
87 : tree op2 ATTRIBUTE_UNUSED)
88 : {
89 5323184 : return VREL_VARYING;
90 : }
91 :
92 : // Default registers nothing and returns false meaning nothing changed.
93 :
94 : bool
95 24221846 : 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 24221846 : return false;
101 : }
102 :
103 : // Default registers nothing and returns false meaning nothing changed.
104 :
105 : bool
106 5934966 : 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 5934966 : return false;
112 : }
113 :
114 : // Get the value of EXPR on edge m_edge.
115 :
116 : bool
117 65639348 : fur_edge::get_operand (vrange &r, tree expr)
118 : {
119 65639348 : 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 410261152 : fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
136 : {
137 410261152 : m_stmt = s;
138 410261152 : }
139 :
140 : // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
141 :
142 : bool
143 536341687 : fur_stmt::get_operand (vrange &r, tree expr)
144 : {
145 536341687 : 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 54091913 : fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
153 : {
154 : // Pick up the range of expr from edge E.
155 54091913 : fur_edge e_src (e, m_query);
156 54091913 : return e_src.get_operand (r, expr);
157 : }
158 :
159 : // Return relation based from m_stmt.
160 :
161 : relation_kind
162 104897207 : fur_stmt::query_relation (tree op1, tree op2)
163 : {
164 104897207 : 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 230543753 : fur_depend::fur_depend (gimple *s, range_query *q, ranger_cache *c)
170 230543753 : : fur_stmt (s, q), m_cache (c)
171 : {
172 230543753 : m_depend_p = true;
173 230543753 : }
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 32313359 : fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
180 : {
181 32313359 : 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 23812079 : if (m_cache)
187 : {
188 23812047 : m_cache->update_consumers (op1);
189 23812047 : 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 6454243 : fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
199 : {
200 6454243 : 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 6437691 : if (m_cache)
206 : {
207 6437689 : m_cache->update_consumers (op1);
208 6437689 : 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 855662 : fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
234 : {
235 855662 : m_list = m_local;
236 855662 : m_index = 0;
237 855662 : m_limit = 1;
238 855662 : m_local[0] = &r1;
239 855662 : }
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 1703728 : 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 1703728 : if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
269 848066 : return m_query->range_of_expr (r, expr);
270 855662 : r = *m_list[m_index++];
271 855662 : 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 855662 : fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
286 : {
287 855662 : fold_using_range f;
288 855662 : fur_list src (r1, q);
289 855662 : 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 74580089 : fold_range (vrange &r, gimple *s, range_query *q)
318 : {
319 74580089 : fold_using_range f;
320 74580089 : fur_stmt src (s, q);
321 74580089 : 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 7072306 : fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
328 : {
329 7072306 : fold_using_range f;
330 7072306 : fur_edge src (on_edge, q);
331 7072306 : 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 1070970 : fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
423 : {
424 1070970 : def_op1 = def_op2 = op1_op2 = VREL_VARYING;
425 1070970 : }
426 :
427 : // Construct a trio from what is known.
428 :
429 : relation_trio
430 1070970 : fur_relation::trio () const
431 : {
432 1070970 : 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 1053098 : fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
449 : tree op2)
450 : {
451 1053098 : tree lhs = gimple_get_lhs (stmt);
452 1053098 : tree a1 = NULL_TREE;
453 1053098 : tree a2 = NULL_TREE;
454 1053098 : 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 1053098 : case GIMPLE_ASSIGN:
461 1053098 : a1 = gimple_assign_rhs1 (stmt);
462 1053098 : if (gimple_num_ops (stmt) >= 3)
463 1053098 : 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 1053098 : if (op1 == lhs)
471 : {
472 1053098 : if (op2 == a1)
473 1053098 : 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 1053098 : && op1_op2 == VREL_VARYING;
493 : }
494 :
495 : // Return the relation trio for stmt S using query Q.
496 :
497 : relation_trio
498 1070970 : fold_relations (gimple *s, range_query *q)
499 : {
500 1070970 : fold_using_range f;
501 1070970 : fur_relation src (s, q);
502 1070970 : tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
503 1070970 : if (lhs)
504 : {
505 1070970 : value_range vr(TREE_TYPE (lhs));
506 1070970 : if (f.fold_stmt (vr, s, src))
507 1070970 : return src.trio ();
508 1070970 : }
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 2537488 : adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
526 : {
527 2537488 : tree op0 = gimple_assign_rhs1 (diff_stmt);
528 2537488 : tree op1 = gimple_assign_rhs2 (diff_stmt);
529 2537488 : tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
530 2537488 : tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
531 2537488 : gimple *call;
532 :
533 2537488 : if (TREE_CODE (op0) == SSA_NAME
534 2511733 : && TREE_CODE (op1) == SSA_NAME
535 2467757 : && (call = SSA_NAME_DEF_STMT (op0))
536 2467757 : && is_gimple_call (call)
537 78220 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
538 60051 : && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
539 59806 : && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
540 59806 : && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
541 59722 : && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
542 59722 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
543 59722 : && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
544 2575687 : && 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 2537488 : }
552 :
553 : // Adjust the range for an IMAGPART_EXPR.
554 :
555 : static void
556 653498 : adjust_imagpart_expr (vrange &res, const gimple *stmt)
557 : {
558 653498 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
559 :
560 653498 : if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
561 : return;
562 :
563 527879 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
564 527879 : if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
565 : {
566 399007 : switch (gimple_call_internal_fn (def_stmt))
567 : {
568 381428 : case IFN_ADD_OVERFLOW:
569 381428 : case IFN_SUB_OVERFLOW:
570 381428 : case IFN_MUL_OVERFLOW:
571 381428 : case IFN_UADDC:
572 381428 : case IFN_USUBC:
573 381428 : case IFN_ATOMIC_COMPARE_EXCHANGE:
574 381428 : {
575 381428 : int_range<2> r;
576 381428 : r.set_varying (boolean_type_node);
577 381428 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
578 381428 : range_cast (r, type);
579 381428 : res.intersect (r);
580 381428 : }
581 399007 : default:
582 399007 : break;
583 : }
584 399007 : return;
585 : }
586 128872 : if (is_gimple_assign (def_stmt)
587 128872 : && 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 632341 : adjust_realpart_expr (vrange &res, const gimple *stmt)
604 : {
605 632341 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
606 :
607 632341 : if (TREE_CODE (name) != SSA_NAME)
608 : return;
609 :
610 499416 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
611 499416 : if (!SSA_NAME_DEF_STMT (name))
612 : return;
613 :
614 499416 : if (is_gimple_assign (def_stmt)
615 499416 : && 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 181369933 : gimple_range_adjustment (vrange &res, const gimple *stmt)
634 : {
635 181369933 : switch (gimple_expr_code (stmt))
636 : {
637 2537488 : case POINTER_DIFF_EXPR:
638 2537488 : adjust_pointer_diff_expr (as_a <irange> (res), stmt);
639 2537488 : return;
640 :
641 653498 : case IMAGPART_EXPR:
642 653498 : adjust_imagpart_expr (res, stmt);
643 653498 : return;
644 :
645 632341 : case REALPART_EXPR:
646 632341 : adjust_realpart_expr (res, stmt);
647 632341 : 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 291672356 : fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
661 : {
662 291672356 : bool res = false;
663 : // If name and S are specified, make sure it is an LHS of S.
664 291672356 : gcc_checking_assert (!name || !gimple_get_lhs (s) ||
665 : name == gimple_get_lhs (s));
666 :
667 155706244 : if (!name)
668 155706244 : name = gimple_get_lhs (s);
669 :
670 : // Process addresses and loads from static constructors.
671 291672356 : if (gimple_code (s) == GIMPLE_ASSIGN && range_from_readonly_var (r, s))
672 : return true;
673 :
674 291572113 : gimple_range_op_handler handler (s);
675 291572113 : if (gimple_code (s) == GIMPLE_ASSIGN
676 291572113 : && gimple_assign_rhs_code (s) == ADDR_EXPR)
677 4080858 : res = range_of_address (as_a <prange> (r), s, src);
678 287491255 : else if (handler)
679 181371033 : res = range_of_range_op (r, handler, src);
680 106120222 : else if (is_a<gphi *>(s))
681 26346222 : res = range_of_phi (r, as_a<gphi *> (s), src);
682 79774000 : else if (is_a<gcall *>(s))
683 13011430 : res = range_of_call (r, as_a<gcall *> (s), src);
684 66762570 : else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
685 151274 : res = range_of_cond_expr (r, as_a<gassign *> (s), src);
686 :
687 : // If the result is varying, check for basic nonnegativeness.
688 : // Specifically this helps for now with strict enum in cases like
689 : // g++.dg/warn/pr33738.C.
690 224960817 : bool so_p;
691 224960817 : if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ())
692 340058529 : && gimple_stmt_nonnegative_warnv_p (s, &so_p))
693 37519132 : r.set_nonnegative (r.type ());
694 :
695 291572113 : if (!res)
696 : {
697 : // If no name specified or range is unsupported, bail.
698 66611296 : if (!name || !gimple_range_ssa_p (name))
699 50772 : return false;
700 : // We don't understand the stmt, so return the global range.
701 66560524 : gimple_range_global (r, name);
702 66560524 : return true;
703 : }
704 :
705 224960817 : if (r.undefined_p ())
706 : return true;
707 :
708 : // We sometimes get compatible types copied from operands, make sure
709 : // the correct type is being returned.
710 224909975 : if (name && TREE_TYPE (name) != r.type ())
711 : {
712 3619187 : gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
713 3619187 : range_cast (r, TREE_TYPE (name));
714 : }
715 : return true;
716 : }
717 :
718 : // Calculate a range for range_op statement S and return it in R. If any
719 : // If a range cannot be calculated, return false.
720 :
721 : bool
722 181371033 : fold_using_range::range_of_range_op (vrange &r,
723 : gimple_range_op_handler &handler,
724 : fur_source &src)
725 : {
726 181371033 : gcc_checking_assert (handler);
727 181371033 : gimple *s = handler.stmt ();
728 181371033 : tree type = gimple_range_type (s);
729 181371033 : if (!type)
730 : return false;
731 :
732 181371033 : tree lhs = handler.lhs ();
733 181371033 : tree op1 = handler.operand1 ();
734 181371033 : tree op2 = handler.operand2 ();
735 :
736 : // Certain types of builtin functions may have no arguments.
737 181371033 : if (!op1)
738 : {
739 1100 : value_range r1 (type);
740 1100 : if (!handler.fold_range (r, type, r1, r1))
741 0 : r.set_varying (type);
742 1100 : return true;
743 1100 : }
744 :
745 181369933 : value_range range1 (TREE_TYPE (op1));
746 181369933 : value_range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
747 :
748 181369933 : if (src.get_operand (range1, op1))
749 : {
750 181369933 : if (!op2)
751 : {
752 : // Fold range, and register any dependency if available.
753 35974625 : value_range r2 (type);
754 35974625 : r2.set_varying (type);
755 35974625 : if (!handler.fold_range (r, type, range1, r2))
756 248954 : r.set_varying (type);
757 35974625 : if (lhs && gimple_range_ssa_p (op1))
758 : {
759 53048304 : if (src.gori_ssa ())
760 19805797 : src.gori_ssa ()->register_dependency (lhs, op1);
761 33242474 : relation_kind rel;
762 33242474 : rel = handler.lhs_op1_relation (r, range1, range1);
763 33242474 : if (rel != VREL_VARYING)
764 24405982 : src.register_relation (s, rel, lhs, op1);
765 : }
766 35974625 : }
767 145395308 : else if (src.get_operand (range2, op2))
768 : {
769 145395308 : relation_kind rel = src.query_relation (op1, op2);
770 145395308 : if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
771 : {
772 123 : fprintf (dump_file, " folding with relation ");
773 123 : print_generic_expr (dump_file, op1, TDF_SLIM);
774 123 : print_relation (dump_file, rel);
775 123 : print_generic_expr (dump_file, op2, TDF_SLIM);
776 123 : fputc ('\n', dump_file);
777 : }
778 : // Fold range, and register any dependency if available.
779 145395308 : if (!handler.fold_range (r, type, range1, range2,
780 : relation_trio::op1_op2 (rel)))
781 0 : r.set_varying (type);
782 145395308 : if (irange::supports_p (type))
783 132277237 : relation_fold_and_or (as_a <irange> (r), s, src, range1, range2);
784 145395308 : if (lhs)
785 : {
786 148659545 : if (src.gori_ssa ())
787 : {
788 57844672 : src.gori_ssa ()->register_dependency (lhs, op1);
789 115689344 : src.gori_ssa ()->register_dependency (lhs, op2);
790 : }
791 90814813 : if (gimple_range_ssa_p (op1))
792 : {
793 88374748 : relation_kind rel2 = handler.lhs_op1_relation (r, range1,
794 88374748 : range2, rel);
795 88374748 : if (rel2 != VREL_VARYING)
796 38137111 : src.register_relation (s, rel2, lhs, op1);
797 : }
798 90814813 : if (gimple_range_ssa_p (op2))
799 : {
800 36142072 : relation_kind rel2 = handler.lhs_op2_relation (r, range1,
801 36142072 : range2, rel);
802 36142072 : if (rel2 != VREL_VARYING)
803 2367505 : src.register_relation (s, rel2, lhs, op2);
804 : }
805 : }
806 : // Check for an existing BB, as we maybe asked to fold an
807 : // artificial statement not in the CFG.
808 54580495 : else if (is_a<gcond *> (s) && gimple_bb (s))
809 : {
810 46184606 : basic_block bb = gimple_bb (s);
811 46184606 : edge e0 = EDGE_SUCC (bb, 0);
812 : /* During RTL expansion one of the edges can be removed
813 : if expansion proves the jump is unconditional. */
814 46184606 : edge e1 = single_succ_p (bb) ? NULL : EDGE_SUCC (bb, 1);
815 :
816 46184606 : gcc_checking_assert (e1 || currently_expanding_to_rtl);
817 46184606 : if (!single_pred_p (e0->dest))
818 11760078 : e0 = NULL;
819 46184606 : if (e1 && !single_pred_p (e1->dest))
820 : e1 = NULL;
821 46184606 : src.register_outgoing_edges (as_a<gcond *> (s),
822 : as_a <irange> (r), e0, e1);
823 : }
824 : }
825 : else
826 0 : r.set_varying (type);
827 : }
828 : else
829 0 : r.set_varying (type);
830 : // Make certain range-op adjustments that aren't handled any other way.
831 181369933 : gimple_range_adjustment (r, s);
832 181369933 : return true;
833 181369933 : }
834 :
835 : // Calculate the range of an assignment containing an ADDR_EXPR.
836 : // Return the range in R.
837 : // If a range cannot be calculated, set it to VARYING and return true.
838 :
839 : bool
840 4080858 : fold_using_range::range_of_address (prange &r, gimple *stmt, fur_source &src)
841 : {
842 4080858 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
843 4080858 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
844 :
845 4080858 : bool strict_overflow_p;
846 4080858 : tree expr = gimple_assign_rhs1 (stmt);
847 4080858 : poly_int64 bitsize, bitpos;
848 4080858 : tree offset;
849 4080858 : machine_mode mode;
850 4080858 : int unsignedp, reversep, volatilep;
851 4080858 : tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
852 : &bitpos, &offset, &mode, &unsignedp,
853 : &reversep, &volatilep);
854 :
855 :
856 4080858 : if (base != NULL_TREE
857 4080858 : && TREE_CODE (base) == MEM_REF
858 8018421 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
859 : {
860 3937514 : tree ssa = TREE_OPERAND (base, 0);
861 3937514 : tree lhs = gimple_get_lhs (stmt);
862 6364411 : if (lhs && gimple_range_ssa_p (ssa) && src.gori_ssa ())
863 2426897 : src.gori_ssa ()->register_dependency (lhs, ssa);
864 3937514 : src.get_operand (r, ssa);
865 3937514 : range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
866 :
867 3937514 : poly_offset_int off = 0;
868 3937514 : bool off_cst = false;
869 3937514 : if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
870 : {
871 3856430 : off = mem_ref_offset (base);
872 3856430 : if (offset)
873 48 : off += poly_offset_int::from (wi::to_poly_wide (offset),
874 48 : SIGNED);
875 3856430 : off <<= LOG2_BITS_PER_UNIT;
876 3856430 : off += bitpos;
877 : off_cst = true;
878 : }
879 : /* If &X->a is equal to X, the range of X is the result. */
880 3856430 : if (off_cst && known_eq (off, 0))
881 1398152 : return true;
882 2539362 : else if (flag_delete_null_pointer_checks
883 2539362 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
884 : {
885 : /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
886 : allow going from non-NULL pointer to NULL. */
887 2537977 : if (r.undefined_p ()
888 5075954 : || !r.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr)))))
889 : {
890 : /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
891 : using POINTER_PLUS_EXPR if off_cst and just fall back to
892 : this. */
893 1867209 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
894 1867209 : return true;
895 : }
896 : }
897 : /* If MEM_REF has a "positive" offset, consider it non-NULL
898 : always, for -fdelete-null-pointer-checks also "negative"
899 : ones. Punt for unknown offsets (e.g. variable ones). */
900 672153 : if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
901 671943 : && off_cst
902 613145 : && known_ne (off, 0)
903 1285298 : && (flag_delete_null_pointer_checks || known_gt (off, 0)))
904 : {
905 613145 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
906 613145 : return true;
907 : }
908 59008 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
909 59008 : return true;
910 : }
911 :
912 : // Handle "= &a".
913 143344 : if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
914 : {
915 142194 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
916 142194 : return true;
917 : }
918 :
919 : // Otherwise return varying.
920 1150 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
921 1150 : return true;
922 : }
923 :
924 : /* If TYPE is a pointer, return false. Otherwise, add zero of TYPE (which must
925 : be an integer) to R and return true. */
926 :
927 : static bool
928 1316 : range_from_missing_constructor_part (vrange &r, tree type)
929 : {
930 1316 : if (POINTER_TYPE_P (type))
931 : return false;
932 1047 : gcc_checking_assert (irange::supports_p (type));
933 1047 : wide_int zero = wi::zero (TYPE_PRECISION (type));
934 1047 : r.union_ (int_range<1> (type, zero, zero));
935 1047 : return true;
936 1047 : }
937 :
938 : // One step of fold_using_range::range_from_readonly_var. Process expressions
939 : // in COMPS which together load a value of TYPE, from index I to 0 according to
940 : // the corresponding static initializer in CST which should be either a scalar
941 : // invariant or a constructor. Currently TYPE must be either a pointer or an
942 : // integer. If TYPE is a pointer, return true if all potentially loaded values
943 : // are known not to be zero and false if any of them can be zero. Otherwise
944 : // return true if it is possible to add all constants which can be loaded from
945 : // CST (which must be storable to TYPE) to R and do so.
946 : // TODO: Add support for franges.
947 :
948 : static bool
949 766867 : range_from_readonly_load (vrange &r, tree type, tree cst,
950 : const vec <tree> &comps, unsigned i)
951 : {
952 778246 : if (i == 0)
953 : {
954 661485 : if (!useless_type_conversion_p (type, TREE_TYPE (cst)))
955 : return false;
956 :
957 661485 : if (POINTER_TYPE_P (type))
958 : {
959 146407 : bool strict_overflow_p;
960 146407 : return tree_single_nonzero_warnv_p (cst, &strict_overflow_p);
961 : }
962 :
963 515078 : if (TREE_CODE (cst) != INTEGER_CST)
964 : return false;
965 :
966 515007 : wide_int wi_cst = wi::to_wide (cst);
967 515007 : r.union_ (int_range<1> (type, wi_cst, wi_cst));
968 515007 : return true;
969 515007 : }
970 : /* TODO: Perhaps handle RAW_DATA_CST too. */
971 116761 : if (TREE_CODE (cst) != CONSTRUCTOR)
972 : return false;
973 :
974 116014 : i--;
975 116014 : tree expr = comps[i];
976 116014 : unsigned ix;
977 116014 : tree index, val;
978 :
979 116014 : if (TREE_CODE (expr) == COMPONENT_REF)
980 : {
981 11426 : tree ref_fld = TREE_OPERAND (expr, 1);
982 18502 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (cst), ix, index, val)
983 : {
984 18455 : if (index != ref_fld)
985 7076 : continue;
986 : return range_from_readonly_load (r, type, val, comps, i);
987 : }
988 47 : if (TREE_CODE (TREE_TYPE (cst)) == RECORD_TYPE)
989 7 : return range_from_missing_constructor_part (r, type);
990 : else
991 : /* Missing constructor of a union field just isn't like other missing
992 : constructor parts. */
993 : return false;
994 : }
995 :
996 104588 : gcc_assert (TREE_CODE (expr) == ARRAY_REF);
997 104588 : tree op1 = TREE_OPERAND (expr, 1);
998 :
999 104588 : if (TREE_CODE (op1) == INTEGER_CST)
1000 : {
1001 3320 : unsigned ctor_idx;
1002 3320 : val = get_array_ctor_element_at_index (cst, wi::to_offset (op1),
1003 : &ctor_idx);
1004 3320 : if (!val)
1005 : {
1006 96 : if (ctor_idx < CONSTRUCTOR_NELTS (cst))
1007 : return false;
1008 96 : return range_from_missing_constructor_part (r, type);
1009 : }
1010 3224 : return range_from_readonly_load (r, type, val, comps, i);
1011 : }
1012 :
1013 101268 : tree arr_type = TREE_TYPE (cst);
1014 101268 : tree domain = TYPE_DOMAIN (arr_type);
1015 101268 : if (!TYPE_MIN_VALUE (domain)
1016 101268 : || !TYPE_MAX_VALUE (domain)
1017 101268 : || !tree_fits_uhwi_p (TYPE_MIN_VALUE (domain))
1018 202536 : || !tree_fits_uhwi_p (TYPE_MAX_VALUE (domain)))
1019 : return false;
1020 101193 : unsigned HOST_WIDE_INT needed_count
1021 101193 : = (tree_to_uhwi (TYPE_MAX_VALUE (domain))
1022 101193 : - tree_to_uhwi (TYPE_MIN_VALUE (domain)) + 1);
1023 202326 : if (CONSTRUCTOR_NELTS (cst) < needed_count)
1024 : {
1025 1213 : if (!range_from_missing_constructor_part (r, type))
1026 : return false;
1027 : }
1028 :
1029 762147 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (cst), ix, index, val)
1030 : {
1031 : /* TODO: If the array index in the expr is an SSA_NAME with a known
1032 : range, we could use just values loaded from the corresponding array
1033 : elements. */
1034 662100 : if (!range_from_readonly_load (r, type, val, comps, i))
1035 : return false;
1036 : }
1037 :
1038 : return true;
1039 : }
1040 :
1041 : // Attempt to calculate the range of value loaded by STMT (which must be an
1042 : // assignment) if it is a load from a read-only aggregate variable. If
1043 : // successful, return true and set the discovered range in R. Otherwise return
1044 : // false and leave R untouched.
1045 :
1046 : bool
1047 186351038 : fold_using_range::range_from_readonly_var (vrange &r, gimple *stmt)
1048 : {
1049 186351038 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
1050 186351038 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1051 : /* TODO: Add support for frange. */
1052 186351038 : if (!irange::supports_p (type)
1053 186351038 : && !prange::supports_p (type))
1054 : return false;
1055 :
1056 176322772 : unsigned HOST_WIDE_INT limit = param_vrp_cstload_limit;
1057 176322772 : if (!limit)
1058 : return false;
1059 :
1060 176303705 : tree t = gimple_assign_rhs1 (stmt);
1061 176303705 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
1062 : return false;
1063 176303705 : limit *= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
1064 :
1065 176303705 : unsigned count = 0;
1066 176303705 : while (TREE_CODE (t) == ARRAY_REF
1067 224297371 : || TREE_CODE (t) == COMPONENT_REF)
1068 : {
1069 47993666 : count++;
1070 47993666 : t = TREE_OPERAND (t, 0);
1071 : }
1072 176303705 : if (!count
1073 31142385 : || (TREE_CODE (t) != VAR_DECL
1074 31142385 : && TREE_CODE (t) != CONST_DECL))
1075 : return false;
1076 :
1077 8794178 : if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (t))
1078 8794178 : || tree_to_uhwi (DECL_SIZE_UNIT (t)) > limit)
1079 : return false;
1080 :
1081 : /* TODO: We perhaps should try to handle at least some cases when the
1082 : declaration is wrapped in a MEM_REF, but we need to be careful to look at
1083 : the right part of the constructor then. */
1084 7821991 : tree ctor = ctor_for_folding (t);
1085 7821991 : if (!ctor
1086 7821984 : || TREE_CODE (ctor) != CONSTRUCTOR)
1087 : return false;
1088 :
1089 101543 : t = gimple_assign_rhs1 (stmt);
1090 101543 : auto_vec <tree, 4> comps;
1091 101543 : comps.safe_grow (count, true);
1092 101543 : int i = 0;
1093 101543 : while (TREE_CODE (t) == ARRAY_REF
1094 207944 : || TREE_CODE (t) == COMPONENT_REF)
1095 : {
1096 106401 : comps[i] = t;
1097 106401 : t = TREE_OPERAND (t, 0);
1098 106401 : i++;
1099 : }
1100 :
1101 101543 : value_range tmp (type);
1102 101543 : bool res = range_from_readonly_load (tmp, type, ctor, comps, count);
1103 101543 : if (res)
1104 : {
1105 100243 : if (POINTER_TYPE_P (type))
1106 24495 : r.set_nonzero (type);
1107 : else
1108 75748 : r = tmp;
1109 : }
1110 101543 : return res;
1111 101543 : }
1112 :
1113 : // Calculate a range for phi statement S and return it in R.
1114 : // If a range cannot be calculated, return false.
1115 :
1116 : bool
1117 26346222 : fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
1118 : {
1119 26346222 : tree phi_def = gimple_phi_result (phi);
1120 26346222 : tree type = gimple_range_type (phi);
1121 26346222 : value_range arg_range (type);
1122 26346222 : value_range equiv_range (type);
1123 26346222 : unsigned x;
1124 :
1125 26346222 : if (!type)
1126 : return false;
1127 :
1128 : // Track if all executable arguments are the same.
1129 26346222 : tree single_arg = NULL_TREE;
1130 26346222 : bool seen_arg = false;
1131 :
1132 26346222 : relation_oracle *oracle = &(src.query()->relation ());
1133 : // Start with an empty range, unioning in each argument's range.
1134 26346222 : r.set_undefined ();
1135 67561017 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1136 : {
1137 54110836 : tree arg = gimple_phi_arg_def (phi, x);
1138 : // An argument that is the same as the def provides no new range.
1139 54110836 : if (arg == phi_def)
1140 18923 : continue;
1141 :
1142 54091913 : edge e = gimple_phi_arg_edge (phi, x);
1143 :
1144 : // Get the range of the argument on its edge.
1145 54091913 : src.get_phi_operand (arg_range, arg, e);
1146 :
1147 54091913 : if (!arg_range.undefined_p ())
1148 : {
1149 : // Register potential dependencies for stale value tracking.
1150 : // Likewise, if the incoming PHI argument is equivalent to this
1151 : // PHI definition, it provides no new info. Accumulate these ranges
1152 : // in case all arguments are equivalences.
1153 53830505 : if (oracle->query (e, arg, phi_def) == VREL_EQ)
1154 389510 : equiv_range.union_(arg_range);
1155 : else
1156 53440995 : r.union_ (arg_range);
1157 :
1158 88716087 : if (gimple_range_ssa_p (arg) && src.gori_ssa ())
1159 34885576 : src.gori_ssa ()->register_dependency (phi_def, arg);
1160 : }
1161 :
1162 : // Track if all arguments are the same.
1163 54091913 : if (!seen_arg)
1164 : {
1165 : seen_arg = true;
1166 : single_arg = arg;
1167 : }
1168 27745691 : else if (single_arg != arg)
1169 26600028 : single_arg = NULL_TREE;
1170 :
1171 : // Once the value reaches varying, stop looking.
1172 54091913 : if (r.varying_p () && single_arg == NULL_TREE)
1173 : break;
1174 : }
1175 :
1176 : // If all arguments were equivalences, use the equivalence ranges as no
1177 : // arguments were processed.
1178 26346222 : if (r.undefined_p () && !equiv_range.undefined_p ())
1179 245427 : r = equiv_range;
1180 :
1181 : // If the PHI boils down to a single effective argument, look at it.
1182 26346222 : if (single_arg)
1183 : {
1184 : // Symbolic arguments can be equivalences.
1185 2385708 : if (gimple_range_ssa_p (single_arg))
1186 : {
1187 : // Only allow the equivalence if the PHI definition does not
1188 : // dominate any incoming edge for SINGLE_ARG.
1189 : // See PR 108139 and 109462.
1190 1930941 : basic_block bb = gimple_bb (phi);
1191 1930941 : if (!dom_info_available_p (CDI_DOMINATORS))
1192 : single_arg = NULL;
1193 : else
1194 4075710 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1195 2149847 : if (gimple_phi_arg_def (phi, x) == single_arg
1196 4288895 : && dominated_by_p (CDI_DOMINATORS,
1197 2139048 : gimple_phi_arg_edge (phi, x)->src,
1198 : bb))
1199 : {
1200 : single_arg = NULL;
1201 : break;
1202 : }
1203 1929938 : if (single_arg)
1204 1925863 : src.register_relation (phi, VREL_EQ, phi_def, single_arg);
1205 : }
1206 454767 : else if (src.get_operand (arg_range, single_arg)
1207 909534 : && arg_range.singleton_p ())
1208 : {
1209 : // Numerical arguments that are a constant can be returned as
1210 : // the constant. This can help fold later cases where even this
1211 : // constant might have been UNDEFINED via an unreachable edge.
1212 440285 : r = arg_range;
1213 440285 : return true;
1214 : }
1215 : }
1216 :
1217 : // Incorporate any global value. If a PHI analysis phase was run, there may
1218 : // be a restricted global range already. Query the range with no context
1219 : // to get a global range.
1220 :
1221 : // If SCEV is available, query if this PHI has any known values.
1222 25905937 : if (scev_initialized_p ()
1223 25905937 : && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
1224 : {
1225 11292476 : class loop *l = loop_containing_stmt (phi);
1226 11292476 : if (l && loop_outer (l))
1227 : {
1228 8808117 : value_range loop_range (type);
1229 8808117 : range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
1230 8808117 : if (!loop_range.varying_p ())
1231 : {
1232 2481369 : if (dump_file && (dump_flags & TDF_DETAILS))
1233 : {
1234 14230 : fprintf (dump_file, "Loops range found for ");
1235 14230 : print_generic_expr (dump_file, phi_def, TDF_SLIM);
1236 14230 : fprintf (dump_file, ": ");
1237 14230 : loop_range.dump (dump_file);
1238 14230 : fprintf (dump_file, " and calculated range :");
1239 14230 : r.dump (dump_file);
1240 14230 : fprintf (dump_file, "\n");
1241 : }
1242 2481369 : r.intersect (loop_range);
1243 : }
1244 8808117 : }
1245 : }
1246 :
1247 : return true;
1248 26346222 : }
1249 :
1250 : // Calculate a range for call statement S and return it in R.
1251 : // If a range cannot be calculated, return false.
1252 :
1253 : bool
1254 13011430 : fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1255 : {
1256 13011430 : tree type = gimple_range_type (call);
1257 13011430 : if (!type)
1258 : return false;
1259 :
1260 13011430 : tree lhs = gimple_call_lhs (call);
1261 13011430 : bool strict_overflow_p;
1262 :
1263 13011430 : if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
1264 43199 : r.set_nonnegative (type);
1265 12968231 : else if (gimple_call_nonnull_result_p (call)
1266 12968231 : || gimple_call_nonnull_arg (call))
1267 685274 : r.set_nonzero (type);
1268 : else
1269 12282957 : r.set_varying (type);
1270 :
1271 13011430 : tree callee = gimple_call_fndecl (call);
1272 13011430 : if (callee
1273 13011430 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)), type))
1274 : {
1275 11926092 : value_range val;
1276 11926092 : if (ipa_return_value_range (val, callee))
1277 : {
1278 614038 : r.intersect (val);
1279 614038 : if (dump_file && (dump_flags & TDF_DETAILS))
1280 : {
1281 28 : fprintf (dump_file, "Using return value range of ");
1282 28 : print_generic_expr (dump_file, callee, TDF_SLIM);
1283 28 : fprintf (dump_file, ": ");
1284 28 : val.dump (dump_file);
1285 28 : fprintf (dump_file, "\n");
1286 : }
1287 : }
1288 11926092 : }
1289 :
1290 : // If there is an LHS, intersect that with what is known.
1291 13011430 : if (gimple_range_ssa_p (lhs))
1292 : {
1293 13011430 : value_range def (TREE_TYPE (lhs));
1294 13011430 : gimple_range_global (def, lhs);
1295 13011430 : r.intersect (def);
1296 13011430 : }
1297 : return true;
1298 : }
1299 :
1300 : // Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
1301 : // to further resolve R1 and R2 if there are any dependencies between
1302 : // OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
1303 : // as the origination source location for operands..
1304 : // Effectively, use COND an the edge condition and solve for OP1 on the true
1305 : // edge and OP2 on the false edge.
1306 :
1307 : bool
1308 151274 : fold_using_range::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
1309 : tree op1, tree op2, fur_source &src)
1310 : {
1311 151274 : if (!src.gori () || !src.gori_ssa ())
1312 : return false;
1313 :
1314 112249 : tree ssa1 = gimple_range_ssa_p (op1);
1315 112249 : tree ssa2 = gimple_range_ssa_p (op2);
1316 112249 : if (!ssa1 && !ssa2)
1317 : return false;
1318 101185 : if (TREE_CODE (cond) != SSA_NAME)
1319 : return false;
1320 225969 : gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1321 101092 : if (!cond_def
1322 101092 : || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
1323 : return false;
1324 96250 : tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1325 96250 : if (!value_range::supports_type_p (type)
1326 192496 : || !range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1327 : return false;
1328 96246 : range_op_handler hand (gimple_assign_rhs_code (cond_def));
1329 96246 : if (!hand)
1330 : return false;
1331 :
1332 96246 : tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
1333 192492 : tree c2 = gimple_range_ssa_p (gimple_assign_rhs2 (cond_def));
1334 :
1335 : // Only solve if there is one SSA name in the condition.
1336 96246 : if ((!c1 && !c2) || (c1 && c2))
1337 : return false;
1338 :
1339 : // Pick up the current values of each part of the condition.
1340 26397 : tree rhs1 = gimple_assign_rhs1 (cond_def);
1341 26397 : tree rhs2 = gimple_assign_rhs2 (cond_def);
1342 26397 : value_range cl (TREE_TYPE (rhs1));
1343 26397 : value_range cr (TREE_TYPE (rhs2));
1344 26397 : src.get_operand (cl, rhs1);
1345 26397 : src.get_operand (cr, rhs2);
1346 :
1347 26397 : tree cond_name = c1 ? c1 : c2;
1348 26397 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
1349 :
1350 : // Evaluate the value of COND_NAME on the true and false edges, using either
1351 : // the op1 or op2 routines based on its location.
1352 26397 : value_range cond_true (type), cond_false (type);
1353 26397 : if (c1)
1354 : {
1355 26397 : if (!hand.op1_range (cond_false, type, range_false (), cr))
1356 : return false;
1357 26397 : if (!hand.op1_range (cond_true, type, range_true (), cr))
1358 : return false;
1359 26397 : cond_false.intersect (cl);
1360 26397 : cond_true.intersect (cl);
1361 : }
1362 : else
1363 : {
1364 0 : if (!hand.op2_range (cond_false, type, range_false (), cl))
1365 : return false;
1366 0 : if (!hand.op2_range (cond_true, type, range_true (), cl))
1367 : return false;
1368 0 : cond_false.intersect (cr);
1369 0 : cond_true.intersect (cr);
1370 : }
1371 :
1372 : // Now solve for SSA1 or SSA2 if they are in the dependency chain.
1373 48601 : if (ssa1 && src.gori_ssa()->in_chain_p (ssa1, cond_name))
1374 : {
1375 887 : value_range tmp1 (TREE_TYPE (ssa1));
1376 1774 : if (src.gori ()->compute_operand_range (tmp1, def_stmt, cond_true,
1377 : ssa1, src))
1378 545 : r1.intersect (tmp1);
1379 887 : }
1380 43685 : if (ssa2 && src.gori_ssa ()->in_chain_p (ssa2, cond_name))
1381 : {
1382 262 : value_range tmp2 (TREE_TYPE (ssa2));
1383 524 : if (src.gori ()->compute_operand_range (tmp2, def_stmt, cond_false,
1384 : ssa2, src))
1385 210 : r2.intersect (tmp2);
1386 262 : }
1387 : // If the same name is specified in the condition and COND_EXPR,
1388 : // combine the calculated condition range and the other one provided. ie:
1389 : // c_1 = b_2 < 10
1390 : // f_3 = c_1 ? 0 : b_2
1391 : // With b_2 providing the false value, the value of f_3 will be
1392 : // either 0 UNION (0 = b_2 < 10), which is [-INF, 9].
1393 : // COND_EXPR is
1394 26397 : if (ssa1 && cond_name == ssa1)
1395 2037 : r1 = cond_true;
1396 24360 : else if (ssa2 && cond_name == ssa2)
1397 2863 : r2 = cond_false;
1398 : return true;
1399 26397 : }
1400 :
1401 : // Calculate a range for COND_EXPR statement S and return it in R.
1402 : // If a range cannot be calculated, return false.
1403 :
1404 : bool
1405 151274 : fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1406 : {
1407 151274 : tree cond = gimple_assign_rhs1 (s);
1408 151274 : tree op1 = gimple_assign_rhs2 (s);
1409 151274 : tree op2 = gimple_assign_rhs3 (s);
1410 :
1411 151274 : tree type = gimple_range_type (s);
1412 151274 : if (!type)
1413 : return false;
1414 :
1415 151274 : value_range range1 (TREE_TYPE (op1));
1416 151274 : value_range range2 (TREE_TYPE (op2));
1417 151274 : value_range cond_range (TREE_TYPE (cond));
1418 151274 : gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1419 151274 : gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1420 151274 : src.get_operand (cond_range, cond);
1421 151274 : src.get_operand (range1, op1);
1422 151274 : src.get_operand (range2, op2);
1423 :
1424 : // Try to see if there is a dependence between the COND and either operand
1425 151274 : if (condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1426 26397 : if (dump_file && (dump_flags & TDF_DETAILS))
1427 : {
1428 562 : fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1429 562 : range1.dump(dump_file);
1430 562 : fprintf (dump_file, " and Range op2: ");
1431 562 : range2.dump(dump_file);
1432 562 : fprintf (dump_file, "\n");
1433 : }
1434 :
1435 : // If the condition is known, choose the appropriate expression.
1436 151274 : if (cond_range.singleton_p ())
1437 : {
1438 : // False, pick second operand.
1439 2117 : if (cond_range.zero_p ())
1440 1059 : r = range2;
1441 : else
1442 1058 : r = range1;
1443 : }
1444 : else
1445 : {
1446 149157 : r = range1;
1447 149157 : r.union_ (range2);
1448 : }
1449 151274 : gcc_checking_assert (r.undefined_p ()
1450 : || range_compatible_p (r.type (), type));
1451 151274 : return true;
1452 151274 : }
1453 :
1454 : // If SCEV has any information about phi node NAME, return it as a range in R.
1455 :
1456 : void
1457 8808117 : fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1458 : class loop *l, gphi *phi,
1459 : fur_source &src)
1460 : {
1461 8808117 : static bool in_scev_call = false;
1462 8808117 : gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1463 : // Avoid SCEV callbacks causing infinite recursion.
1464 8808117 : if (in_scev_call)
1465 381862 : r.set_varying (TREE_TYPE (name));
1466 : // SCEV currently invokes get_range_query () for values. If the query
1467 : // being passed in is not the same SCEV will use, do not invoke SCEV.
1468 : // This can be remove if/when SCEV uses a passed in range-query.
1469 16852510 : else if (src.query () != get_range_query (cfun))
1470 : {
1471 1757651 : r.set_varying (TREE_TYPE (name));
1472 : // Report the msmatch if SRC is not the global query. The cache
1473 : // uses a global query and would provide numerous false positives.
1474 114 : if (dump_file && (dump_flags & TDF_DETAILS)
1475 1757717 : && src.query () != get_global_range_query ())
1476 39 : fprintf (dump_file,
1477 : "fold_using-range:: SCEV not invoked due to mismatched queries\n");
1478 : }
1479 : else
1480 : {
1481 6668604 : in_scev_call = true;
1482 6668604 : if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1483 318 : r.set_varying (TREE_TYPE (name));
1484 6668604 : in_scev_call = false;
1485 : }
1486 8808117 : }
1487 :
1488 : // -----------------------------------------------------------------------
1489 :
1490 : // Check if an && or || expression can be folded based on relations. ie
1491 : // c_2 = a_6 > b_7
1492 : // c_3 = a_6 < b_7
1493 : // c_4 = c_2 && c_3
1494 : // c_2 and c_3 can never be true at the same time,
1495 : // Therefore c_4 can always resolve to false based purely on the relations.
1496 :
1497 : void
1498 132277237 : fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1499 : fur_source &src, vrange &op1,
1500 : vrange &op2)
1501 : {
1502 : // No queries or already folded.
1503 132277237 : if (!src.gori () || lhs_range.singleton_p ())
1504 44784611 : return;
1505 :
1506 : // Only care about AND and OR expressions.
1507 87492626 : enum tree_code code = gimple_expr_code (s);
1508 87492626 : bool is_and = false;
1509 87492626 : if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1510 : is_and = true;
1511 83966740 : else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1512 : return;
1513 :
1514 5024695 : gimple_range_op_handler handler (s);
1515 5024695 : tree lhs = handler.lhs ();
1516 5024695 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1517 5024695 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1518 :
1519 : // Deal with || and && only when there is a full set of symbolics.
1520 5024509 : if (!lhs || !ssa1 || !ssa2
1521 2729940 : || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1522 1858705 : || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1523 6882164 : || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1524 : return;
1525 :
1526 : // Now we know its a boolean AND or OR expression with boolean operands.
1527 : // Ideally we search dependencies for common names, and see what pops out.
1528 : // until then, simply try to resolve direct dependencies.
1529 :
1530 1854790 : gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1531 1854790 : gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1532 :
1533 1854790 : gimple_range_op_handler handler1 (ssa1_stmt);
1534 1854790 : gimple_range_op_handler handler2 (ssa2_stmt);
1535 :
1536 : // If either handler is not present, no relation can be found.
1537 1854790 : if (!handler1 || !handler2)
1538 130715 : return;
1539 :
1540 : // Both stmts will need to have 2 ssa names in the stmt.
1541 1724075 : tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1542 1724075 : tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1543 1724075 : tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1544 1724075 : tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1545 :
1546 1724075 : if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1547 : return;
1548 :
1549 195807 : if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1550 : return;
1551 :
1552 : // Make sure they are the same dependencies, and detect the order of the
1553 : // relationship.
1554 184106 : bool reverse_op2 = true;
1555 184106 : if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1556 : reverse_op2 = false;
1557 184012 : else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1558 : return;
1559 :
1560 94 : int_range<2> bool_one = range_true ();
1561 94 : relation_kind relation1 = handler1.op1_op2_relation (bool_one, op1, op2);
1562 94 : relation_kind relation2 = handler2.op1_op2_relation (bool_one, op1, op2);
1563 94 : if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1564 : return;
1565 :
1566 58 : if (reverse_op2)
1567 0 : relation2 = relation_negate (relation2);
1568 :
1569 : // x && y is false if the relation intersection of the true cases is NULL.
1570 58 : if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1571 0 : lhs_range = range_false (boolean_type_node);
1572 : // x || y is true if the union of the true cases is NO-RELATION..
1573 : // ie, one or the other being true covers the full range of possibilities.
1574 58 : else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1575 0 : lhs_range = bool_one;
1576 : else
1577 58 : return;
1578 :
1579 0 : range_cast (lhs_range, TREE_TYPE (lhs));
1580 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1581 : {
1582 0 : fprintf (dump_file, " Relation adjustment: ");
1583 0 : print_generic_expr (dump_file, ssa1, TDF_SLIM);
1584 0 : fprintf (dump_file, " and ");
1585 0 : print_generic_expr (dump_file, ssa2, TDF_SLIM);
1586 0 : fprintf (dump_file, " combine to produce ");
1587 0 : lhs_range.dump (dump_file);
1588 0 : fputc ('\n', dump_file);
1589 : }
1590 :
1591 : return;
1592 94 : }
1593 :
1594 : // Register any outgoing edge relations from a conditional branch.
1595 :
1596 : void
1597 68634953 : fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1598 : edge e0, edge e1)
1599 : {
1600 68634953 : int_range<2> e0_range, e1_range;
1601 68634953 : tree name;
1602 68634953 : basic_block bb = gimple_bb (s);
1603 :
1604 68634953 : gimple_range_op_handler handler (s);
1605 68634953 : if (!handler)
1606 : return;
1607 :
1608 68624873 : if (e0)
1609 : {
1610 : // If this edge is never taken, ignore it.
1611 56864795 : gcond_edge_range (e0_range, e0);
1612 56864795 : e0_range.intersect (lhs_range);
1613 56864795 : if (e0_range.undefined_p ())
1614 25430027 : e0 = NULL;
1615 : }
1616 :
1617 68624873 : if (e1)
1618 : {
1619 : // If this edge is never taken, ignore it.
1620 50162604 : gcond_edge_range (e1_range, e1);
1621 50162604 : e1_range.intersect (lhs_range);
1622 50162604 : if (e1_range.undefined_p ())
1623 28595365 : e1 = NULL;
1624 : }
1625 :
1626 68624873 : if (!e0 && !e1)
1627 : return;
1628 :
1629 : // First, register the gcond itself. This will catch statements like
1630 : // if (a_2 < b_5)
1631 65512249 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1632 65512249 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1633 65512249 : value_range r1,r2;
1634 65512249 : if (ssa1 && ssa2)
1635 : {
1636 19449449 : r1.set_varying (TREE_TYPE (ssa1));
1637 19449449 : r2.set_varying (TREE_TYPE (ssa2));
1638 19449449 : if (e0)
1639 : {
1640 13016045 : relation_kind relation = handler.op1_op2_relation (e0_range, r1, r2);
1641 13016045 : if (relation != VREL_VARYING)
1642 12940075 : register_relation (e0, relation, ssa1, ssa2);
1643 : }
1644 19449449 : if (e1)
1645 : {
1646 11389538 : relation_kind relation = handler.op1_op2_relation (e1_range, r1, r2);
1647 11389538 : if (relation != VREL_VARYING)
1648 11331286 : register_relation (e1, relation, ssa1, ssa2);
1649 : }
1650 : }
1651 :
1652 : // Outgoing relations of GORI exports require a gori engine.
1653 118609218 : if (!gori_ssa ())
1654 12415292 : return;
1655 :
1656 : // Now look for other relations in the exports. This will find stmts
1657 : // leading to the condition such as:
1658 : // c_2 = a_4 < b_7
1659 : // if (c_2)
1660 168395613 : FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb, name)
1661 : {
1662 115298656 : if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1663 109911561 : continue;
1664 8921085 : gimple *stmt = SSA_NAME_DEF_STMT (name);
1665 8921085 : gimple_range_op_handler handler (stmt);
1666 8921085 : if (!handler)
1667 3533990 : continue;
1668 5387095 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1669 5387095 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1670 5387095 : value_range r (TREE_TYPE (name));
1671 5387095 : if (ssa1 && ssa2)
1672 : {
1673 2299089 : r1.set_varying (TREE_TYPE (ssa1));
1674 2299089 : r2.set_varying (TREE_TYPE (ssa2));
1675 1408602 : if (e0 && gori ()->edge_range_p (r, e0, name, *m_query)
1676 3674214 : && r.singleton_p ())
1677 : {
1678 1258273 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1679 1258273 : if (relation != VREL_VARYING)
1680 418352 : register_relation (e0, relation, ssa1, ssa2);
1681 : }
1682 1458062 : if (e1 && gori ()->edge_range_p (r, e1, name, *m_query)
1683 3712243 : && r.singleton_p ())
1684 : {
1685 1040889 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1686 1040889 : if (relation != VREL_VARYING)
1687 151553 : register_relation (e1, relation, ssa1, ssa2);
1688 : }
1689 : }
1690 5387095 : }
1691 68634953 : }
|