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