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 518332766 : fur_source::fur_source (range_query *q)
58 : {
59 518332766 : if (q)
60 518331814 : m_query = q;
61 : else
62 1904 : m_query = get_range_query (cfun);
63 518332766 : m_depend_p = false;
64 518332766 : }
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 6885897 : fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
87 : tree op2 ATTRIBUTE_UNUSED)
88 : {
89 6885897 : return VREL_VARYING;
90 : }
91 :
92 : // Default registers nothing and returns false meaning nothing changed.
93 :
94 : bool
95 27607527 : 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 27607527 : return false;
101 : }
102 :
103 : // Default registers nothing and returns false meaning nothing changed.
104 :
105 : bool
106 7664963 : 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 7664963 : return false;
112 : }
113 :
114 : // Get the value of EXPR on edge m_edge.
115 :
116 : bool
117 65100318 : fur_edge::get_operand (vrange &r, tree expr)
118 : {
119 65100318 : 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 458308642 : fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
136 : {
137 458308642 : m_stmt = s;
138 458308642 : }
139 :
140 : // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
141 :
142 : bool
143 632173220 : fur_stmt::get_operand (vrange &r, tree expr)
144 : {
145 632173220 : 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 49632828 : fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
153 : {
154 : // Pick up the range of expr from edge E.
155 49632828 : fur_edge e_src (e, m_query);
156 49632828 : return e_src.get_operand (r, expr);
157 : }
158 :
159 : // Return relation based from m_stmt.
160 :
161 : relation_kind
162 111320251 : fur_stmt::query_relation (tree op1, tree op2)
163 : {
164 111320251 : 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 249827951 : fur_depend::fur_depend (gimple *s, range_query *q, ranger_cache *c)
170 249827951 : : fur_stmt (s, q), m_cache (c)
171 : {
172 249827951 : m_depend_p = true;
173 249827951 : }
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 34426173 : fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
180 : {
181 34426173 : 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 25800194 : if (m_cache)
187 : {
188 25800023 : m_cache->update_consumers (op1);
189 25800023 : 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 6941650 : fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
199 : {
200 6941650 : 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 6923472 : if (m_cache)
206 : {
207 6923470 : m_cache->update_consumers (op1);
208 6923470 : 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 908751 : fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
234 : {
235 908751 : m_list = m_local;
236 908751 : m_index = 0;
237 908751 : m_limit = 1;
238 908751 : m_local[0] = &r1;
239 908751 : }
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 1809709 : 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 1809709 : if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
269 900958 : return m_query->range_of_expr (r, expr);
270 908751 : r = *m_list[m_index++];
271 908751 : 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 908751 : fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
286 : {
287 908751 : fold_using_range f;
288 908751 : fur_list src (r1, q);
289 908751 : 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 77097462 : fold_range (vrange &r, gimple *s, range_query *q)
318 : {
319 77097462 : fold_using_range f;
320 77097462 : fur_stmt src (s, q);
321 77097462 : 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 9482539 : fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
328 : {
329 9482539 : fold_using_range f;
330 9482539 : fur_edge src (on_edge, q);
331 9482539 : 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 1100744 : fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
423 : {
424 1100744 : def_op1 = def_op2 = op1_op2 = VREL_VARYING;
425 1100744 : }
426 :
427 : // Construct a trio from what is known.
428 :
429 : relation_trio
430 1100744 : fur_relation::trio () const
431 : {
432 1100744 : 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 1081837 : fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
449 : tree op2)
450 : {
451 1081837 : tree lhs = gimple_get_lhs (stmt);
452 1081837 : tree a1 = NULL_TREE;
453 1081837 : tree a2 = NULL_TREE;
454 1081837 : 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 1081837 : case GIMPLE_ASSIGN:
461 1081837 : a1 = gimple_assign_rhs1 (stmt);
462 1081837 : if (gimple_num_ops (stmt) >= 3)
463 1081837 : 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 1081837 : if (op1 == lhs)
471 : {
472 1081837 : if (op2 == a1)
473 1081837 : 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 1081837 : && op1_op2 == VREL_VARYING;
493 : }
494 :
495 : // Return the relation trio for stmt S using query Q.
496 :
497 : relation_trio
498 1100744 : fold_relations (gimple *s, range_query *q)
499 : {
500 1100744 : fold_using_range f;
501 1100744 : fur_relation src (s, q);
502 1100744 : tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
503 1100744 : if (lhs)
504 : {
505 1100744 : value_range vr(TREE_TYPE (lhs));
506 1100744 : if (f.fold_stmt (vr, s, src))
507 1100744 : return src.trio ();
508 1100744 : }
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 3060936 : adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
526 : {
527 3060936 : tree op0 = gimple_assign_rhs1 (diff_stmt);
528 3060936 : tree op1 = gimple_assign_rhs2 (diff_stmt);
529 3060936 : tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
530 3060936 : tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
531 3060936 : gimple *call;
532 :
533 3060936 : if (TREE_CODE (op0) == SSA_NAME
534 3027320 : && TREE_CODE (op1) == SSA_NAME
535 2980162 : && (call = SSA_NAME_DEF_STMT (op0))
536 2980162 : && is_gimple_call (call)
537 98203 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
538 77714 : && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
539 77473 : && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
540 77473 : && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
541 76895 : && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
542 76895 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
543 76895 : && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
544 3107082 : && 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 3060936 : }
552 :
553 : // Adjust the range for an IMAGPART_EXPR.
554 :
555 : static void
556 984050 : adjust_imagpart_expr (vrange &res, const gimple *stmt)
557 : {
558 984050 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
559 :
560 984050 : if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
561 : return;
562 :
563 854939 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
564 854939 : if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
565 : {
566 404725 : switch (gimple_call_internal_fn (def_stmt))
567 : {
568 387293 : case IFN_ADD_OVERFLOW:
569 387293 : case IFN_SUB_OVERFLOW:
570 387293 : case IFN_MUL_OVERFLOW:
571 387293 : case IFN_UADDC:
572 387293 : case IFN_USUBC:
573 387293 : case IFN_ATOMIC_COMPARE_EXCHANGE:
574 387293 : {
575 387293 : int_range<2> r;
576 387293 : r.set_varying (boolean_type_node);
577 387293 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
578 387293 : range_cast (r, type);
579 387293 : res.intersect (r);
580 387293 : }
581 : default:
582 : break;
583 : }
584 : return;
585 : }
586 450214 : if (is_gimple_assign (def_stmt)
587 450214 : && 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 935911 : adjust_realpart_expr (vrange &res, const gimple *stmt)
604 : {
605 935911 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
606 :
607 935911 : if (TREE_CODE (name) != SSA_NAME)
608 : return;
609 :
610 798970 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
611 798970 : if (!SSA_NAME_DEF_STMT (name))
612 : return;
613 :
614 798970 : if (is_gimple_assign (def_stmt)
615 798970 : && 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 200868719 : gimple_range_adjustment (vrange &res, const gimple *stmt)
634 : {
635 200868719 : switch (gimple_expr_code (stmt))
636 : {
637 3060936 : case POINTER_DIFF_EXPR:
638 3060936 : adjust_pointer_diff_expr (as_a <irange> (res), stmt);
639 3060936 : return;
640 :
641 984050 : case IMAGPART_EXPR:
642 984050 : adjust_imagpart_expr (res, stmt);
643 984050 : return;
644 :
645 935911 : case REALPART_EXPR:
646 935911 : adjust_realpart_expr (res, stmt);
647 935911 : return;
648 :
649 : default:
650 : break;
651 : }
652 : }
653 :
654 : // Provide context to the gimple fold callback.
655 :
656 : static struct
657 : {
658 : gimple *m_stmt;
659 : range_query *m_query;
660 : } x_fold_context;
661 :
662 : // Gimple fold callback.
663 :
664 : static tree
665 90162015 : pta_valueize (tree name)
666 : {
667 90162015 : tree ret
668 90162015 : = x_fold_context.m_query->value_of_expr (name, x_fold_context.m_stmt);
669 :
670 90162015 : return ret ? ret : name;
671 : }
672 :
673 : // Calculate a range for statement S and return it in R. If NAME is provided it
674 : // represents the SSA_NAME on the LHS of the statement. It is only required
675 : // if there is more than one lhs/output. If a range cannot
676 : // be calculated, return false.
677 :
678 : bool
679 313915045 : fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
680 : {
681 313915045 : bool res = false;
682 : // If name and S are specified, make sure it is an LHS of S.
683 313915045 : gcc_checking_assert (!name || !gimple_get_lhs (s) ||
684 : name == gimple_get_lhs (s));
685 :
686 : if (!name)
687 169516764 : name = gimple_get_lhs (s);
688 :
689 : // Process addresses and loads from static constructors.
690 313915045 : if (gimple_code (s) == GIMPLE_ASSIGN && range_from_readonly_var (r, s, src))
691 : return true;
692 :
693 : // Save the current range query and restore it before returning.
694 : // If the specified query is different, make it the current one.
695 : // PR 125854 - The fold machinery may make a query call.
696 : // PR 126814 - tree_expr_nonnegative_p may make a call.
697 : // PR 126942 - path_ranger queries should never be the current query.
698 : // set_range_query will revert to a global query for this.
699 313800671 : range_query *saved_query = set_range_query (cfun, src.query ());
700 :
701 313800671 : gimple_range_op_handler handler (s);
702 313800671 : if (gimple_code (s) == GIMPLE_ASSIGN
703 313800671 : && gimple_assign_rhs_code (s) == ADDR_EXPR)
704 4256474 : res = range_of_address (as_a <prange> (r), s, src);
705 309544197 : else if (handler)
706 200891460 : res = range_of_range_op (r, handler, src);
707 108652737 : else if (is_a<gphi *>(s))
708 24353935 : res = range_of_phi (r, as_a<gphi *> (s), src);
709 84298802 : else if (is_a<gcall *>(s))
710 13703095 : res = range_of_call (r, as_a<gcall *> (s), src);
711 70595707 : else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
712 182230 : res = range_of_cond_expr (r, as_a<gassign *> (s), src);
713 :
714 : // If the result is varying, use the type's min/max if either is not
715 : // the same as the full precision min/max. This helps with strict enum
716 : // e.g. `g++.dg/warn/pr33738.C`.
717 243387194 : if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ()))
718 : {
719 123125848 : irange &ir = as_a <irange> (r);
720 123125848 : tree type = r.type ();
721 123125848 : auto typemax = wi::to_wide (TYPE_MAX_VALUE (type));
722 123125848 : auto typemin = wi::to_wide (TYPE_MIN_VALUE (type));
723 123125848 : auto precisionmax = wi::max_value (TYPE_PRECISION (type),
724 246251696 : TYPE_SIGN (type));
725 123125848 : auto precisionmin = wi::min_value (TYPE_PRECISION (type),
726 246251696 : TYPE_SIGN (type));
727 246181654 : if (typemax != precisionmax || typemin != precisionmin)
728 70042 : ir.set (type, typemin, typemax);
729 123126732 : }
730 :
731 313800671 : if (!res)
732 : {
733 : // Restore the original query.
734 70413477 : if (saved_query)
735 14988641 : set_range_query (cfun, saved_query);
736 : // If no name specified or range is unsupported, bail.
737 70413477 : if (!name || !gimple_range_ssa_p (name))
738 : return false;
739 : // We don't understand the stmt, so return the global range.
740 70363272 : gimple_range_global (r, name);
741 70363272 : return true;
742 : }
743 :
744 243387194 : if (r.undefined_p ())
745 : {
746 : // Restore the original query.
747 62490 : if (saved_query)
748 1560 : set_range_query (cfun, saved_query);
749 : return true;
750 : }
751 :
752 : // We sometimes get compatible types copied from operands, make sure
753 : // the correct type is being returned.
754 243324704 : if (name && TREE_TYPE (name) != r.type ())
755 : {
756 3918403 : gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
757 3918403 : range_cast (r, TREE_TYPE (name));
758 : }
759 :
760 243324704 : if (is_a <prange> (r))
761 : {
762 28094709 : prange &p = as_a <prange> (r);
763 : // Check to see if points_to should be set.
764 28094709 : if (p.pt_unknown_p () && name && gimple_code (s) == GIMPLE_ASSIGN)
765 : {
766 17080807 : tree rhs = gimple_assign_rhs1 (s);
767 17080807 : tree_code code = gimple_assign_rhs_code (s);
768 : // If code is SSA_NAME, any points to would already be copied.
769 17080807 : if (code != SSA_NAME
770 15001423 : && get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
771 21585012 : && TREE_CODE (rhs) == ADDR_EXPR)
772 4254326 : p.set_pt (rhs, true);
773 : else
774 : {
775 : // If we couldn't find anything, try fold.
776 12826481 : x_fold_context = { s, get_range_query (cfun) };
777 12826481 : rhs = gimple_fold_stmt_to_constant_1 (s, pta_valueize,
778 : pta_valueize);
779 12826481 : if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
780 23677 : p.set_pt (rhs, true);
781 : }
782 : }
783 : }
784 : // Restore the original query.
785 243324704 : if (saved_query)
786 57819558 : set_range_query (cfun, saved_query);
787 : return true;
788 : }
789 :
790 : // Calculate a range for range_op statement S and return it in R. If any
791 : // If a range cannot be calculated, return false.
792 :
793 : bool
794 200891460 : fold_using_range::range_of_range_op (vrange &r,
795 : gimple_range_op_handler &handler,
796 : fur_source &src)
797 : {
798 200891460 : gcc_checking_assert (handler);
799 200891460 : gimple *s = handler.stmt ();
800 200891460 : tree type = gimple_range_type (s);
801 200891460 : if (!type)
802 : return false;
803 :
804 200891460 : tree lhs = handler.lhs ();
805 200891460 : tree op1 = handler.operand1 ();
806 200891460 : tree op2 = handler.operand2 ();
807 :
808 : // Certain types of builtin functions may have no arguments.
809 200891460 : if (!op1)
810 : {
811 22741 : value_range r1 (type);
812 22741 : if (!handler.fold_range (r, type, r1, r1))
813 0 : r.set_varying (type);
814 22741 : return true;
815 22741 : }
816 :
817 200868719 : value_range range1 (TREE_TYPE (op1));
818 200868719 : value_range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
819 :
820 200868719 : if (src.get_operand (range1, op1))
821 : {
822 200868719 : if (!op2)
823 : {
824 : // Fold range, and register any dependency if available.
825 42207957 : value_range r2 (type);
826 42207957 : r2.set_varying (type);
827 42207957 : if (!handler.fold_range (r, type, range1, r2))
828 327672 : r.set_varying (type);
829 42207957 : if (lhs && gimple_range_ssa_p (op1))
830 : {
831 61115922 : if (src.gori_ssa ())
832 22921053 : src.gori_ssa ()->register_dependency (lhs, op1);
833 38194836 : relation_kind rel;
834 38194836 : rel = handler.lhs_op1_relation (r, range1, range1);
835 38194836 : if (rel != VREL_VARYING)
836 28534554 : src.register_relation (s, rel, lhs, op1);
837 : }
838 42207957 : }
839 158660762 : else if (src.get_operand (range2, op2))
840 : {
841 158660762 : relation_kind rel = src.query_relation (op1, op2);
842 158660762 : if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
843 : {
844 160 : fprintf (dump_file, " folding with relation ");
845 160 : print_generic_expr (dump_file, op1, TDF_SLIM);
846 160 : print_relation (dump_file, rel);
847 160 : print_generic_expr (dump_file, op2, TDF_SLIM);
848 160 : fputc ('\n', dump_file);
849 : }
850 : // Fold range, and register any dependency if available.
851 158660762 : if (!handler.fold_range (r, type, range1, range2,
852 : relation_trio::op1_op2 (rel)))
853 0 : r.set_varying (type);
854 158660762 : if (irange::supports_p (type))
855 145453489 : relation_fold_and_or (as_a <irange> (r), s, src, range1, range2);
856 158660762 : if (lhs)
857 : {
858 160394849 : if (src.gori_ssa ())
859 : {
860 62008030 : src.gori_ssa ()->register_dependency (lhs, op1);
861 124016060 : src.gori_ssa ()->register_dependency (lhs, op2);
862 : }
863 98386486 : if (gimple_range_ssa_p (op1))
864 : {
865 95671790 : relation_kind rel2 = handler.lhs_op1_relation (r, range1,
866 95671790 : range2, rel);
867 95671790 : if (rel2 != VREL_VARYING)
868 41357194 : src.register_relation (s, rel2, lhs, op1);
869 : }
870 98386486 : if (gimple_range_ssa_p (op2))
871 : {
872 39084285 : relation_kind rel2 = handler.lhs_op2_relation (r, range1,
873 39084285 : range2, rel);
874 39084285 : if (rel2 != VREL_VARYING)
875 2579253 : src.register_relation (s, rel2, lhs, op2);
876 : }
877 : }
878 : // Check for an existing BB, as we maybe asked to fold an
879 : // artificial statement not in the CFG.
880 60274276 : else if (is_a<gcond *> (s) && gimple_bb (s))
881 : {
882 51414798 : basic_block bb = gimple_bb (s);
883 51414798 : edge e0 = EDGE_SUCC (bb, 0);
884 : /* During RTL expansion one of the edges can be removed
885 : if expansion proves the jump is unconditional. */
886 51414798 : edge e1 = single_succ_p (bb) ? NULL : EDGE_SUCC (bb, 1);
887 :
888 51414798 : gcc_checking_assert (e1 || currently_expanding_to_rtl);
889 51414798 : if (!single_pred_p (e0->dest))
890 12395280 : e0 = NULL;
891 51414798 : if (e1 && !single_pred_p (e1->dest))
892 : e1 = NULL;
893 51414798 : src.register_outgoing_edges (as_a<gcond *> (s),
894 : as_a <irange> (r), e0, e1);
895 : }
896 : }
897 : else
898 0 : r.set_varying (type);
899 : }
900 : else
901 0 : r.set_varying (type);
902 : // Make certain range-op adjustments that aren't handled any other way.
903 200868719 : gimple_range_adjustment (r, s);
904 200868719 : return true;
905 200868719 : }
906 :
907 : // Calculate the range of an assignment containing an ADDR_EXPR.
908 : // Return the range in R.
909 : // If a range cannot be calculated, set it to VARYING and return true.
910 :
911 : bool
912 4256474 : fold_using_range::range_of_address (prange &r, gimple *stmt, fur_source &src)
913 : {
914 4256474 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
915 4256474 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
916 :
917 4256474 : tree expr = gimple_assign_rhs1 (stmt);
918 4256474 : poly_int64 bitsize, bitpos;
919 4256474 : tree offset;
920 4256474 : machine_mode mode;
921 4256474 : int unsignedp, reversep, volatilep;
922 4256474 : tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
923 : &bitpos, &offset, &mode, &unsignedp,
924 : &reversep, &volatilep);
925 :
926 :
927 4256474 : if (base != NULL_TREE
928 4256474 : && TREE_CODE (base) == MEM_REF
929 8285630 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
930 : {
931 4029095 : tree ssa = TREE_OPERAND (base, 0);
932 4029095 : tree lhs = gimple_get_lhs (stmt);
933 6664337 : if (lhs && gimple_range_ssa_p (ssa) && src.gori_ssa ())
934 2635242 : src.gori_ssa ()->register_dependency (lhs, ssa);
935 4029095 : src.get_operand (r, ssa);
936 4029095 : range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
937 :
938 4029095 : poly_offset_int off = 0;
939 4029095 : bool off_cst = false;
940 4029095 : if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
941 : {
942 3943646 : off = mem_ref_offset (base);
943 3943646 : if (offset)
944 48 : off += poly_offset_int::from (wi::to_poly_wide (offset),
945 48 : SIGNED);
946 3943646 : off <<= LOG2_BITS_PER_UNIT;
947 3943646 : off += bitpos;
948 : off_cst = true;
949 : }
950 : /* If &X->a is equal to X, the range of X is the result. */
951 3943646 : if (off_cst && known_eq (off, 0))
952 1407787 : return true;
953 2621308 : else if (flag_delete_null_pointer_checks
954 2621308 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))
955 : {
956 : /* For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
957 : allow going from non-NULL pointer to NULL. */
958 2619826 : if (r.undefined_p ()
959 5239652 : || !r.contains_p (wi::zero (TYPE_PRECISION (TREE_TYPE (expr)))))
960 : {
961 : /* We could here instead adjust r by off >> LOG2_BITS_PER_UNIT
962 : using POINTER_PLUS_EXPR if off_cst and just fall back to
963 : this. */
964 1913423 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
965 1913423 : return true;
966 : }
967 : }
968 : /* If MEM_REF has a "positive" offset, consider it non-NULL
969 : always, for -fdelete-null-pointer-checks also "negative"
970 : ones. Punt for unknown offsets (e.g. variable ones). */
971 707885 : if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
972 707669 : && off_cst
973 644852 : && known_ne (off, 0)
974 1352737 : && (flag_delete_null_pointer_checks || known_gt (off, 0)))
975 : {
976 644852 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
977 644852 : return true;
978 : }
979 63033 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
980 63033 : return true;
981 : }
982 :
983 : // Handle "= &a".
984 227379 : if (tree_single_nonzero_p (expr))
985 : {
986 226310 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
987 226310 : return true;
988 : }
989 :
990 : // Otherwise return varying.
991 1069 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
992 1069 : return true;
993 : }
994 :
995 : // Does R contain anything in [LO, HI]?
996 :
997 : static bool
998 796612 : intersects_p (const irange &r, const wide_int &lo, const wide_int &hi)
999 : {
1000 796612 : int_range_max tmp (r);
1001 796612 : tmp.intersect (int_range<1> (r.type (), lo, hi));
1002 796612 : return !tmp.undefined_p ();
1003 796612 : }
1004 :
1005 : // The range of a load from a read-only aggregate: walk its initializer along
1006 : // the component path of the reference, COMPONENT_PATH, from the aggregate out,
1007 : // unioning into R the constants the load can reach. For example:
1008 : //
1009 : // static const struct { int a, b[2]; } tbl[2] = { { 1, { 2, 3 } },
1010 : // { 4, { 5, 6 } } };
1011 : // ... = tbl[i].b[1];
1012 : //
1013 : // COMPONENT_PATH is { tbl[i], tbl[i].b, tbl[i].b[1] } and R is [3, 3][6, 6].
1014 : // The methods return false when a range cannot be determined.
1015 :
1016 : class ctor_ref_range
1017 : {
1018 : public:
1019 116687 : ctor_ref_range (vrange &r, tree type, const vec<tree> &component_path,
1020 : fur_source &src)
1021 116687 : : m_r (r), m_type (type), m_component_path (component_path),
1022 116687 : m_src (src) { }
1023 : bool accumulate_range (tree init, unsigned i);
1024 : private:
1025 : bool accumulate_range_from_scalar (tree init);
1026 : bool accumulate_range_from_component_ref (tree init, unsigned i, tree fld);
1027 : bool accumulate_range_from_array_ref (tree init, unsigned i, tree idx);
1028 : void accumulate_range_from_missing_constructor_elt ();
1029 :
1030 : vrange &m_r;
1031 : tree m_type;
1032 : const vec<tree> &m_component_path;
1033 : fur_source &m_src;
1034 : };
1035 :
1036 : // Accumulate the range of the load from INIT, the initializer reached after
1037 : // I steps of the component path.
1038 :
1039 : bool
1040 826911 : ctor_ref_range::accumulate_range (tree init, unsigned i)
1041 : {
1042 1653822 : if (i == m_component_path.length ())
1043 692646 : return accumulate_range_from_scalar (init);
1044 : /* TODO: Perhaps handle RAW_DATA_CST too. */
1045 134265 : if (TREE_CODE (init) != CONSTRUCTOR)
1046 : return false;
1047 :
1048 133518 : tree ref = m_component_path[i];
1049 133518 : if (TREE_CODE (ref) == COMPONENT_REF)
1050 13604 : return accumulate_range_from_component_ref (init, i + 1,
1051 27208 : TREE_OPERAND (ref, 1));
1052 :
1053 119914 : gcc_assert (TREE_CODE (ref) == ARRAY_REF);
1054 119914 : return accumulate_range_from_array_ref (init, i + 1, TREE_OPERAND (ref, 1));
1055 : }
1056 :
1057 : // Accumulate the range from the scalar INIT at the end of the component path.
1058 : // For example, for t[1] with static const int t[2] = { 4, 5 }, INIT is 5 and
1059 : // the range is [5, 5].
1060 :
1061 : bool
1062 692646 : ctor_ref_range::accumulate_range_from_scalar (tree init)
1063 : {
1064 692646 : if (!useless_type_conversion_p (m_type, TREE_TYPE (init)))
1065 : return false;
1066 :
1067 692646 : if (POINTER_TYPE_P (m_type))
1068 : {
1069 157392 : prange elt;
1070 157392 : if (integer_zerop (init))
1071 102 : elt.set_zero (m_type);
1072 157290 : else if (tree_single_nonzero_p (init))
1073 157262 : elt.set_nonzero (m_type);
1074 : else
1075 : return false;
1076 157364 : m_r.union_ (elt);
1077 157364 : return true;
1078 157392 : }
1079 :
1080 535254 : if (TREE_CODE (init) == REAL_CST)
1081 : {
1082 66477 : const REAL_VALUE_TYPE *rv = TREE_REAL_CST_PTR (init);
1083 66477 : frange elt;
1084 66477 : if (real_isnan (rv))
1085 599 : elt.set_nan (m_type, real_isneg (rv));
1086 : else
1087 65878 : elt.set (m_type, *rv, *rv, nan_state (false));
1088 66477 : m_r.union_ (elt);
1089 66477 : return true;
1090 66477 : }
1091 :
1092 468777 : if (TREE_CODE (init) != INTEGER_CST)
1093 : return false;
1094 :
1095 468706 : wide_int wi_cst = wi::to_wide (init);
1096 468706 : m_r.union_ (int_range<1> (m_type, wi_cst, wi_cst));
1097 468706 : return true;
1098 468706 : }
1099 :
1100 : // Accumulate the range from field FLD of the record or union initializer
1101 : // INIT. FLD is the field of a COMPONENT_REF in the component path and I the
1102 : // step after it. For example, when walking s.b in:
1103 : //
1104 : // static const struct { int a, b; } s = { 1, 2 };
1105 : //
1106 : // INIT is { 1, 2 }, FLD is b, and I is 1, the end of the path, and the range
1107 : // is [2, 2].
1108 :
1109 : bool
1110 13604 : ctor_ref_range::accumulate_range_from_component_ref (tree init, unsigned i,
1111 : tree fld)
1112 : {
1113 13604 : unsigned ix;
1114 13604 : tree index, val;
1115 21589 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix, index, val)
1116 : {
1117 21538 : if (index != fld)
1118 7985 : continue;
1119 13553 : return accumulate_range (val, i);
1120 : }
1121 51 : if (TREE_CODE (TREE_TYPE (init)) == RECORD_TYPE)
1122 : {
1123 9 : accumulate_range_from_missing_constructor_elt ();
1124 9 : return true;
1125 : }
1126 : else
1127 : /* Missing constructor of a union field just isn't like other missing
1128 : constructor parts. */
1129 : return false;
1130 : }
1131 :
1132 : // Accumulate the range from the element at IDX of the array initializer INIT,
1133 : // or from every element when IDX is not a constant. IDX is the index of an
1134 : // ARRAY_REF in the component path and I the step after it. For example,
1135 : // when walking t[1] or t[n] in:
1136 : //
1137 : // static const int t[2] = { 4, 5 };
1138 : //
1139 : // INIT is { 4, 5 } and I is 1, the end of the path. For t[1] the range is
1140 : // [5, 5] and for t[n] it is [4, 5]. When every element is loaded, whatever
1141 : // the initializer leaves out reads as zero: take each element's indices out
1142 : // of the domain to see if anything is left.
1143 :
1144 : bool
1145 119914 : ctor_ref_range::accumulate_range_from_array_ref (tree init, unsigned i,
1146 : tree idx)
1147 : {
1148 119914 : tree domain = TYPE_DOMAIN (TREE_TYPE (init));
1149 119914 : if (!TYPE_MIN_VALUE (domain)
1150 119914 : || !TYPE_MAX_VALUE (domain)
1151 119914 : || !tree_fits_uhwi_p (TYPE_MIN_VALUE (domain))
1152 239828 : || !tree_fits_uhwi_p (TYPE_MAX_VALUE (domain)))
1153 : return false;
1154 :
1155 : // Unsigned bounds that look reversed are signed, as they are for
1156 : // get_array_ctor_element_at_index: Ada's -1 .. 5 has a lower bound of
1157 : // SIZE_MAX.
1158 119773 : tree type = TREE_TYPE (TYPE_MIN_VALUE (domain));
1159 119773 : if (TYPE_UNSIGNED (type)
1160 119773 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain), TYPE_MIN_VALUE (domain)))
1161 0 : type = signed_type_for (type);
1162 119773 : wide_int min = wi::to_wide (TYPE_MIN_VALUE (domain));
1163 119773 : wide_int max = wi::to_wide (TYPE_MAX_VALUE (domain));
1164 :
1165 : // Ada represents an empty array with reversed bounds.
1166 119773 : if (wi::gt_p (min, max, TYPE_SIGN (type)))
1167 : return true;
1168 :
1169 119773 : unsigned prec = TYPE_PRECISION (type);
1170 119773 : int_range<1> domain_r (type, min, max);
1171 :
1172 : // Narrow IDX to the ranges it can actually take.
1173 119773 : int_range_max idx_r;
1174 119773 : m_src.get_operand (idx_r, idx);
1175 119773 : range_cast (idx_r, type);
1176 119773 : idx_r.intersect (domain_r);
1177 119773 : if (idx_r.undefined_p ())
1178 : return false;
1179 :
1180 119773 : int_range_max missing (idx_r);
1181 119773 : wide_int next = domain_r.lower_bound ();
1182 119773 : unsigned ix;
1183 119773 : tree index, val;
1184 :
1185 : // Build a range with the entire domain, and take out any elements which are
1186 : // initialized. If there's anything left in that range, it means there are
1187 : // missing elements and we should add a [0, 0] to the final range.
1188 1035276 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix, index, val)
1189 : {
1190 796612 : wide_int lo, hi;
1191 :
1192 : // A null index means the element follows the previous one.
1193 796612 : if (!index)
1194 0 : lo = hi = next;
1195 796612 : else if (TREE_CODE (index) == RANGE_EXPR)
1196 : {
1197 80 : lo = wi::to_wide (TREE_OPERAND (index, 0), prec);
1198 80 : hi = wi::to_wide (TREE_OPERAND (index, 1), prec);
1199 : }
1200 : else
1201 : {
1202 796532 : gcc_checking_assert (TREE_CODE (index) == INTEGER_CST);
1203 796532 : lo = hi = wi::to_wide (index, prec);
1204 : }
1205 : // A RAW_DATA_CST holds one element per byte.
1206 796612 : if (TREE_CODE (val) == RAW_DATA_CST)
1207 0 : hi = wi::add (hi, RAW_DATA_LENGTH (val) - 1);
1208 796612 : missing.intersect (int_range<2> (type, lo, hi, VR_ANTI_RANGE));
1209 796612 : next = wi::add (hi, 1);
1210 :
1211 796612 : if (intersects_p (idx_r, lo, hi) && !accumulate_range (val, i))
1212 882 : return false;
1213 796612 : }
1214 :
1215 118891 : if (!missing.undefined_p ())
1216 1020 : accumulate_range_from_missing_constructor_elt ();
1217 : return true;
1218 239546 : }
1219 :
1220 : // Accumulate [0, 0] for a constructor element the initializer leaves out.
1221 : // For example, when loading t[1] in:
1222 : //
1223 : // static const int t[2] = { 4 };
1224 :
1225 : void
1226 1029 : ctor_ref_range::accumulate_range_from_missing_constructor_elt ()
1227 : {
1228 1029 : value_range zero (m_type);
1229 1029 : zero.set_zero (m_type);
1230 1029 : m_r.union_ (zero);
1231 1029 : }
1232 :
1233 : // Attempt to calculate the range of value loaded by STMT (which must be an
1234 : // assignment) if it is a load from a read-only aggregate variable. If
1235 : // successful, return true and set the discovered range in R. Otherwise return
1236 : // false and leave R untouched.
1237 :
1238 : bool
1239 203586660 : fold_using_range::range_from_readonly_var (vrange &r, gimple *stmt,
1240 : fur_source &src)
1241 : {
1242 203586660 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
1243 203586660 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1244 203586660 : if (!irange::supports_p (type)
1245 51248796 : && !prange::supports_p (type)
1246 213693386 : && !frange::supports_p (type))
1247 : return false;
1248 :
1249 203586660 : unsigned HOST_WIDE_INT limit = param_vrp_cstload_limit;
1250 203586660 : if (!limit)
1251 : return false;
1252 :
1253 203565977 : tree t = gimple_assign_rhs1 (stmt);
1254 203565977 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
1255 : return false;
1256 203565977 : limit *= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
1257 :
1258 : // The component path of the reference, from the aggregate out.
1259 203565977 : auto_vec <tree, 4> component_path;
1260 203565977 : while (TREE_CODE (t) == ARRAY_REF
1261 256604039 : || TREE_CODE (t) == COMPONENT_REF)
1262 : {
1263 53038062 : component_path.safe_push (t);
1264 53038062 : t = TREE_OPERAND (t, 0);
1265 : }
1266 203565977 : component_path.reverse ();
1267 407131954 : if (component_path.is_empty ()
1268 34128787 : || (TREE_CODE (t) != VAR_DECL
1269 34128787 : && TREE_CODE (t) != CONST_DECL))
1270 : return false;
1271 :
1272 9678119 : if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (t))
1273 9678119 : || tree_to_uhwi (DECL_SIZE_UNIT (t)) > limit)
1274 : return false;
1275 :
1276 : /* TODO: We perhaps should try to handle at least some cases when the
1277 : declaration is wrapped in a MEM_REF, but we need to be careful to look at
1278 : the right part of the constructor then. */
1279 8490137 : tree ctor = ctor_for_folding (t);
1280 8490137 : if (!ctor
1281 8490130 : || TREE_CODE (ctor) != CONSTRUCTOR)
1282 : return false;
1283 :
1284 116687 : value_range tmp (type);
1285 116687 : ctor_ref_range load (tmp, type, component_path, src);
1286 116687 : bool res = load.accumulate_range (ctor, 0) && !tmp.varying_p ();
1287 114374 : if (res)
1288 114374 : r = tmp;
1289 116687 : return res;
1290 203682664 : }
1291 :
1292 : // Calculate a range for phi statement S and return it in R.
1293 : // If a range cannot be calculated, return false.
1294 :
1295 : bool
1296 24353935 : fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
1297 : {
1298 24353935 : tree phi_def = gimple_phi_result (phi);
1299 24353935 : tree type = gimple_range_type (phi);
1300 24353935 : value_range arg_range (type);
1301 24353935 : value_range equiv_range (type);
1302 24353935 : unsigned x;
1303 :
1304 24353935 : if (!type)
1305 : return false;
1306 :
1307 : // Track if all executable arguments are the same.
1308 24353935 : tree single_arg = NULL_TREE;
1309 24353935 : bool seen_arg = false;
1310 :
1311 24353935 : relation_oracle *oracle = &(src.query()->relation ());
1312 : // Start with an empty range, unioning in each argument's range.
1313 24353935 : r.set_undefined ();
1314 87177103 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1315 : {
1316 49654620 : tree arg = gimple_phi_arg_def (phi, x);
1317 : // An argument that is the same as the def provides no new range.
1318 49654620 : if (arg == phi_def)
1319 21792 : continue;
1320 :
1321 49632828 : edge e = gimple_phi_arg_edge (phi, x);
1322 :
1323 : // Get the range of the argument on its edge.
1324 49632828 : src.get_phi_operand (arg_range, arg, e);
1325 :
1326 49632828 : if (!arg_range.undefined_p ())
1327 : {
1328 : // Register potential dependencies for stale value tracking.
1329 : // Likewise, if the incoming PHI argument is equivalent to this
1330 : // PHI definition, it provides no new info. Accumulate these ranges
1331 : // in case all arguments are equivalences.
1332 49372810 : if (oracle->query (e, arg, phi_def) == VREL_EQ)
1333 269641 : equiv_range.union_(arg_range);
1334 : else
1335 49103169 : r.union_ (arg_range);
1336 :
1337 83543518 : if (gimple_range_ssa_p (arg) && src.gori_ssa ())
1338 34170696 : src.gori_ssa ()->register_dependency (phi_def, arg);
1339 : }
1340 :
1341 : // Track if all arguments are the same.
1342 49632828 : if (!seen_arg)
1343 : {
1344 : seen_arg = true;
1345 : single_arg = arg;
1346 : }
1347 25278893 : else if (!vrp_operand_equal_p (single_arg, arg))
1348 24003199 : single_arg = NULL_TREE;
1349 :
1350 : // Once the value reaches varying, stop looking.
1351 49632828 : if (r.varying_p () && single_arg == NULL_TREE)
1352 : break;
1353 : }
1354 :
1355 : // If all arguments were equivalences, use the equivalence ranges as no
1356 : // arguments were processed.
1357 24353935 : if (r.undefined_p () && !equiv_range.undefined_p ())
1358 120020 : r = equiv_range;
1359 :
1360 : // If the PHI boils down to a single effective argument, look at it.
1361 24353935 : if (single_arg)
1362 : {
1363 : // Symbolic arguments can be equivalences.
1364 2952072 : if (gimple_range_ssa_p (single_arg))
1365 : {
1366 : // Only allow the equivalence if the PHI definition does not
1367 : // dominate any incoming edge for SINGLE_ARG.
1368 : // See PR 108139 and 109462.
1369 2219538 : basic_block bb = gimple_bb (phi);
1370 2219538 : if (!dom_info_available_p (CDI_DOMINATORS))
1371 : single_arg = NULL;
1372 : else
1373 4756935 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1374 2541878 : if (gimple_phi_arg_def (phi, x) == single_arg
1375 5072559 : && dominated_by_p (CDI_DOMINATORS,
1376 2530681 : gimple_phi_arg_edge (phi, x)->src,
1377 : bb))
1378 : {
1379 : single_arg = NULL;
1380 : break;
1381 : }
1382 2218643 : if (single_arg)
1383 2215057 : src.register_relation (phi, VREL_EQ, phi_def, single_arg);
1384 : }
1385 732534 : else if (src.get_operand (arg_range, single_arg))
1386 : {
1387 : // Check if the single argument points to a specific object.
1388 732534 : if (is_a <prange> (arg_range))
1389 : {
1390 68254 : prange &ptr = as_a <prange> (arg_range);
1391 : // If it doesn't already point at something, set points to.
1392 68254 : if (ptr.pt_unknown_p () && TREE_CODE (single_arg) == ADDR_EXPR)
1393 0 : ptr.set_pt (single_arg, true);
1394 68254 : r = ptr;
1395 68254 : return true;
1396 : }
1397 : // Numerical arguments that are a constant can be returned as
1398 : // the constant. This can help fold later cases where even this
1399 : // constant might have been UNDEFINED via an unreachable edge.
1400 664280 : if (arg_range.singleton_p ())
1401 : {
1402 662616 : r = arg_range;
1403 662616 : return true;
1404 : }
1405 : }
1406 : }
1407 :
1408 : // Incorporate any global value. If a PHI analysis phase was run, there may
1409 : // be a restricted global range already. Query the range with no context
1410 : // to get a global range.
1411 :
1412 : // If SCEV is available, query if this PHI has any known values.
1413 23623065 : if (scev_initialized_p ()
1414 23623065 : && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
1415 : {
1416 9865305 : class loop *l = loop_containing_stmt (phi);
1417 9865305 : if (l && loop_outer (l))
1418 : {
1419 7243666 : value_range loop_range (type);
1420 7243666 : range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
1421 7243666 : if (!loop_range.varying_p ())
1422 : {
1423 2457951 : if (dump_file && (dump_flags & TDF_DETAILS))
1424 : {
1425 14575 : fprintf (dump_file, "Loops range found for ");
1426 14575 : print_generic_expr (dump_file, phi_def, TDF_SLIM);
1427 14575 : fprintf (dump_file, ": ");
1428 14575 : loop_range.dump (dump_file);
1429 14575 : fprintf (dump_file, " and calculated range :");
1430 14575 : r.dump (dump_file);
1431 14575 : fprintf (dump_file, "\n");
1432 : }
1433 2457951 : r.intersect (loop_range);
1434 : }
1435 7243666 : }
1436 : }
1437 :
1438 : return true;
1439 24353935 : }
1440 :
1441 : // Calculate a range for call statement S and return it in R.
1442 : // If a range cannot be calculated, return false.
1443 :
1444 : bool
1445 13703095 : fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1446 : {
1447 13703095 : tree type = gimple_range_type (call);
1448 13703095 : if (!type)
1449 : return false;
1450 :
1451 13703095 : tree lhs = gimple_call_lhs (call);
1452 :
1453 13703095 : if (gimple_stmt_nonnegative_p (call))
1454 16167 : r.set_nonnegative (type);
1455 13686928 : else if (gimple_call_nonnull_result_p (call)
1456 13686928 : || gimple_call_nonnull_arg (call))
1457 696942 : r.set_nonzero (type);
1458 : else
1459 12989986 : r.set_varying (type);
1460 :
1461 13703095 : tree callee = gimple_call_fndecl (call);
1462 13703095 : if (callee
1463 13703095 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)), type))
1464 : {
1465 12529751 : value_range val;
1466 12529751 : if (ipa_return_value_range (val, callee))
1467 : {
1468 647851 : r.intersect (val);
1469 647851 : if (dump_file && (dump_flags & TDF_DETAILS))
1470 : {
1471 28 : fprintf (dump_file, "Using return value range of ");
1472 28 : print_generic_expr (dump_file, callee, TDF_SLIM);
1473 28 : fprintf (dump_file, ": ");
1474 28 : val.dump (dump_file);
1475 28 : fprintf (dump_file, "\n");
1476 : }
1477 : }
1478 12529751 : }
1479 :
1480 : // If there is an LHS, intersect that with what is known.
1481 13703095 : if (gimple_range_ssa_p (lhs))
1482 : {
1483 13703095 : value_range def (TREE_TYPE (lhs));
1484 13703095 : gimple_range_global (def, lhs);
1485 13703095 : r.intersect (def);
1486 13703095 : }
1487 : return true;
1488 : }
1489 :
1490 : // Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
1491 : // to further resolve R1 and R2 if there are any dependencies between
1492 : // OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
1493 : // as the origination source location for operands..
1494 : // Effectively, use COND an the edge condition and solve for OP1 on the true
1495 : // edge and OP2 on the false edge.
1496 :
1497 : bool
1498 182230 : fold_using_range::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
1499 : tree op1, tree op2, fur_source &src)
1500 : {
1501 182230 : if (!src.gori () || !src.gori_ssa ())
1502 : return false;
1503 :
1504 130408 : tree ssa1 = gimple_range_ssa_p (op1);
1505 130408 : tree ssa2 = gimple_range_ssa_p (op2);
1506 130408 : if (!ssa1 && !ssa2)
1507 : return false;
1508 118868 : if (TREE_CODE (cond) != SSA_NAME)
1509 : return false;
1510 118868 : gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1511 118713 : if (!cond_def
1512 118713 : || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
1513 : return false;
1514 113859 : tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1515 113859 : if (!value_range::supports_type_p (type)
1516 227718 : || !range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1517 : return false;
1518 113859 : range_op_handler hand (gimple_assign_rhs_code (cond_def));
1519 113859 : if (!hand)
1520 : return false;
1521 :
1522 113859 : tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
1523 227718 : tree c2 = gimple_range_ssa_p (gimple_assign_rhs2 (cond_def));
1524 :
1525 : // Only solve if there is one SSA name in the condition.
1526 113859 : if ((!c1 && !c2) || (c1 && c2))
1527 : return false;
1528 :
1529 : // Pick up the current values of each part of the condition.
1530 29704 : tree rhs1 = gimple_assign_rhs1 (cond_def);
1531 29704 : tree rhs2 = gimple_assign_rhs2 (cond_def);
1532 29704 : value_range cl (TREE_TYPE (rhs1));
1533 29704 : value_range cr (TREE_TYPE (rhs2));
1534 29704 : src.get_operand (cl, rhs1);
1535 29704 : src.get_operand (cr, rhs2);
1536 :
1537 29704 : tree cond_name = c1 ? c1 : c2;
1538 29704 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
1539 :
1540 : // Evaluate the value of COND_NAME on the true and false edges, using either
1541 : // the op1 or op2 routines based on its location.
1542 29704 : value_range cond_true (type), cond_false (type);
1543 29704 : if (c1)
1544 : {
1545 29704 : if (!hand.op1_range (cond_false, type, range_false (), cr))
1546 : return false;
1547 29704 : if (!hand.op1_range (cond_true, type, range_true (), cr))
1548 : return false;
1549 29704 : cond_false.intersect (cl);
1550 29704 : cond_true.intersect (cl);
1551 : }
1552 : else
1553 : {
1554 0 : if (!hand.op2_range (cond_false, type, range_false (), cl))
1555 : return false;
1556 0 : if (!hand.op2_range (cond_true, type, range_true (), cl))
1557 : return false;
1558 0 : cond_false.intersect (cr);
1559 0 : cond_true.intersect (cr);
1560 : }
1561 :
1562 : // Now solve for SSA1 or SSA2 if they are in the dependency chain.
1563 54810 : if (ssa1 && src.gori_ssa()->in_chain_p (ssa1, cond_name))
1564 : {
1565 1968 : value_range tmp1 (TREE_TYPE (ssa1));
1566 3936 : if (src.gori ()->compute_operand_range (tmp1, def_stmt, cond_true,
1567 : ssa1, src))
1568 1390 : r1.intersect (tmp1);
1569 1968 : }
1570 48880 : if (ssa2 && src.gori_ssa ()->in_chain_p (ssa2, cond_name))
1571 : {
1572 261 : value_range tmp2 (TREE_TYPE (ssa2));
1573 522 : if (src.gori ()->compute_operand_range (tmp2, def_stmt, cond_false,
1574 : ssa2, src))
1575 217 : r2.intersect (tmp2);
1576 261 : }
1577 : // If the same name is specified in the condition and COND_EXPR,
1578 : // combine the calculated condition range and the other one provided. ie:
1579 : // c_1 = b_2 < 10
1580 : // f_3 = c_1 ? 0 : b_2
1581 : // With b_2 providing the false value, the value of f_3 will be
1582 : // either 0 UNION (0 = b_2 < 10), which is [-INF, 9].
1583 : // COND_EXPR is
1584 29704 : if (ssa1 && cond_name == ssa1)
1585 2375 : r1 = cond_true;
1586 27329 : else if (ssa2 && cond_name == ssa2)
1587 3118 : r2 = cond_false;
1588 : return true;
1589 29704 : }
1590 :
1591 : // Calculate a range for COND_EXPR statement S and return it in R.
1592 : // If a range cannot be calculated, return false.
1593 :
1594 : bool
1595 182230 : fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1596 : {
1597 182230 : tree cond = gimple_assign_rhs1 (s);
1598 182230 : tree op1 = gimple_assign_rhs2 (s);
1599 182230 : tree op2 = gimple_assign_rhs3 (s);
1600 :
1601 182230 : tree type = gimple_range_type (s);
1602 182230 : if (!type)
1603 : return false;
1604 :
1605 182230 : value_range range1 (TREE_TYPE (op1));
1606 182230 : value_range range2 (TREE_TYPE (op2));
1607 182230 : value_range cond_range (TREE_TYPE (cond));
1608 182230 : gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1609 182230 : gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1610 182230 : src.get_operand (cond_range, cond);
1611 182230 : src.get_operand (range1, op1);
1612 182230 : src.get_operand (range2, op2);
1613 :
1614 : // Try to see if there is a dependence between the COND and either operand
1615 182230 : if (condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1616 29704 : if (dump_file && (dump_flags & TDF_DETAILS))
1617 : {
1618 622 : fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1619 622 : range1.dump(dump_file);
1620 622 : fprintf (dump_file, " and Range op2: ");
1621 622 : range2.dump(dump_file);
1622 622 : fprintf (dump_file, "\n");
1623 : }
1624 :
1625 : // If the condition is known, choose the appropriate expression.
1626 182230 : if (cond_range.singleton_p ())
1627 : {
1628 : // False, pick second operand.
1629 2511 : if (cond_range.zero_p ())
1630 1185 : r = range2;
1631 : else
1632 1326 : r = range1;
1633 : }
1634 : else
1635 : {
1636 179719 : r = range1;
1637 179719 : r.union_ (range2);
1638 : }
1639 182230 : gcc_checking_assert (r.undefined_p ()
1640 : || range_compatible_p (r.type (), type));
1641 182230 : return true;
1642 182230 : }
1643 :
1644 : // If SCEV has any information about phi node NAME, return it as a range in R.
1645 :
1646 : void
1647 7243666 : fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1648 : class loop *l, gphi *phi,
1649 : fur_source &src)
1650 : {
1651 7243666 : static bool in_scev_call = false;
1652 7243666 : gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1653 : // Avoid SCEV callbacks causing infinite recursion.
1654 7243666 : if (in_scev_call)
1655 417434 : r.set_varying (TREE_TYPE (name));
1656 : // SCEV currently invokes get_range_query () for values. If the query
1657 : // being passed in is not the same SCEV will use, do not invoke SCEV.
1658 : // This can be remove if/when SCEV uses a passed in range-query.
1659 13652464 : else if (src.query () != get_range_query (cfun))
1660 : {
1661 748493 : r.set_varying (TREE_TYPE (name));
1662 : // Report the msmatch if SRC is not the global query. The cache
1663 : // uses a global query and would provide numerous false positives.
1664 46 : if (dump_file && (dump_flags & TDF_DETAILS)
1665 748515 : && src.query () != get_global_range_query ())
1666 22 : fprintf (dump_file,
1667 : "fold_using-range:: SCEV not invoked due to mismatched queries\n");
1668 : }
1669 : else
1670 : {
1671 6077739 : in_scev_call = true;
1672 6077739 : if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1673 848 : r.set_varying (TREE_TYPE (name));
1674 6077739 : in_scev_call = false;
1675 : }
1676 7243666 : }
1677 :
1678 : // -----------------------------------------------------------------------
1679 :
1680 : // Check if an && or || expression can be folded based on relations. ie
1681 : // c_2 = a_6 > b_7
1682 : // c_3 = a_6 < b_7
1683 : // c_4 = c_2 && c_3
1684 : // c_2 and c_3 can never be true at the same time,
1685 : // Therefore c_4 can always resolve to false based purely on the relations.
1686 :
1687 : void
1688 145453489 : fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1689 : fur_source &src, vrange &op1,
1690 : vrange &op2)
1691 : {
1692 : // No queries or already folded.
1693 145453489 : if (!src.gori () || lhs_range.singleton_p ())
1694 : return;
1695 :
1696 : // Only care about AND and OR expressions.
1697 93845524 : enum tree_code code = gimple_expr_code (s);
1698 93845524 : bool is_and = false;
1699 93845524 : if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1700 : is_and = true;
1701 90007557 : else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1702 : return;
1703 :
1704 5407789 : gimple_range_op_handler handler (s);
1705 5407789 : tree lhs = handler.lhs ();
1706 5407789 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1707 5407789 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1708 :
1709 : // Deal with || and && only when there is a full set of symbolics.
1710 5407777 : if (!lhs || !ssa1 || !ssa2
1711 2938125 : || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1712 1962078 : || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1713 7368475 : || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1714 : return;
1715 :
1716 : // Now we know its a boolean AND or OR expression with boolean operands.
1717 : // Ideally we search dependencies for common names, and see what pops out.
1718 : // until then, simply try to resolve direct dependencies.
1719 :
1720 1957905 : gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1721 1957905 : gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1722 :
1723 1957905 : gimple_range_op_handler handler1 (ssa1_stmt);
1724 1957905 : gimple_range_op_handler handler2 (ssa2_stmt);
1725 :
1726 : // If either handler is not present, no relation can be found.
1727 1957905 : if (!handler1 || !handler2)
1728 : return;
1729 :
1730 : // Both stmts will need to have 2 ssa names in the stmt.
1731 1829971 : tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1732 1829971 : tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1733 1829971 : tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1734 1829971 : tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1735 :
1736 1829971 : if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1737 : return;
1738 :
1739 172166 : if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1740 : return;
1741 :
1742 : // Make sure they are the same dependencies, and detect the order of the
1743 : // relationship.
1744 157132 : bool reverse_op2 = true;
1745 157132 : if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1746 : reverse_op2 = false;
1747 157064 : else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1748 : return;
1749 :
1750 73 : int_range<2> bool_one = range_true ();
1751 73 : relation_kind relation1 = handler1.op1_op2_relation (bool_one, op1, op2);
1752 73 : relation_kind relation2 = handler2.op1_op2_relation (bool_one, op1, op2);
1753 73 : if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1754 : return;
1755 :
1756 37 : if (reverse_op2)
1757 5 : relation2 = relation_swap (relation2);
1758 :
1759 : // x && y is false if the relation intersection of the true cases is NULL.
1760 37 : if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1761 0 : lhs_range = range_false (boolean_type_node);
1762 : // x || y is true if the union of the true cases is NO-RELATION..
1763 : // ie, one or the other being true covers the full range of possibilities.
1764 37 : else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1765 0 : lhs_range = bool_one;
1766 : else
1767 : return;
1768 :
1769 0 : range_cast (lhs_range, TREE_TYPE (lhs));
1770 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1771 : {
1772 0 : fprintf (dump_file, " Relation adjustment: ");
1773 0 : print_generic_expr (dump_file, ssa1, TDF_SLIM);
1774 0 : fprintf (dump_file, " and ");
1775 0 : print_generic_expr (dump_file, ssa2, TDF_SLIM);
1776 0 : fprintf (dump_file, " combine to produce ");
1777 0 : lhs_range.dump (dump_file);
1778 0 : fputc ('\n', dump_file);
1779 : }
1780 :
1781 : return;
1782 73 : }
1783 :
1784 : // Register any outgoing edge relations from a conditional branch.
1785 :
1786 : void
1787 75917121 : fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1788 : edge e0, edge e1)
1789 : {
1790 75917121 : int_range<2> e0_range, e1_range;
1791 75917121 : tree name;
1792 75917121 : basic_block bb = gimple_bb (s);
1793 :
1794 75917121 : gimple_range_op_handler handler (s);
1795 75917121 : if (!handler)
1796 : return;
1797 :
1798 75906198 : if (e0)
1799 : {
1800 : // If this edge is never taken, ignore it.
1801 63510918 : gcond_edge_range (e0_range, e0);
1802 63510918 : e0_range.intersect (lhs_range);
1803 63510918 : if (e0_range.undefined_p ())
1804 27462246 : e0 = NULL;
1805 : }
1806 :
1807 75906198 : if (e1)
1808 : {
1809 : // If this edge is never taken, ignore it.
1810 55288543 : gcond_edge_range (e1_range, e1);
1811 55288543 : e1_range.intersect (lhs_range);
1812 55288543 : if (e1_range.undefined_p ())
1813 32481304 : e1 = NULL;
1814 : }
1815 :
1816 75906198 : if (!e0 && !e1)
1817 : return;
1818 :
1819 : // First, register the gcond itself. This will catch statements like
1820 : // if (a_2 < b_5)
1821 72386855 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1822 72386855 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1823 72386855 : value_range r1,r2;
1824 72386855 : if (ssa1 && ssa2)
1825 : {
1826 22582983 : r1.set_varying (TREE_TYPE (ssa1));
1827 22582983 : r2.set_varying (TREE_TYPE (ssa2));
1828 22582983 : if (e0)
1829 : {
1830 15593254 : relation_kind relation = handler.op1_op2_relation (e0_range, r1, r2);
1831 15593254 : if (relation != VREL_VARYING)
1832 15522318 : register_relation (e0, relation, ssa1, ssa2);
1833 : }
1834 22582983 : if (e1)
1835 : {
1836 13190668 : relation_kind relation = handler.op1_op2_relation (e1_range, r1, r2);
1837 13190668 : if (relation != VREL_VARYING)
1838 13133045 : register_relation (e1, relation, ssa1, ssa2);
1839 : }
1840 : }
1841 :
1842 : // Outgoing relations of GORI exports require a gori engine.
1843 130136185 : if (!gori_ssa ())
1844 14637543 : return;
1845 :
1846 : // Now look for other relations in the exports. This will find stmts
1847 : // leading to the condition such as:
1848 : // c_2 = a_4 < b_7
1849 : // if (c_2)
1850 : // as well as relations implied by a known result, such as:
1851 : // c_2 = a_4 - b_7
1852 : // if (c_2 == 0)
1853 183944801 : FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb, name)
1854 : {
1855 126195489 : if (!INTEGRAL_TYPE_P (TREE_TYPE (name)))
1856 82275597 : continue;
1857 99909366 : gimple *stmt = SSA_NAME_DEF_STMT (name);
1858 99909366 : gimple_range_op_handler handler (stmt);
1859 99909366 : if (!handler)
1860 55989474 : continue;
1861 43919892 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1862 43919892 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1863 43919892 : value_range r (TREE_TYPE (name));
1864 43919892 : if (ssa1 && ssa2)
1865 : {
1866 11328062 : r1.set_varying (TREE_TYPE (ssa1));
1867 11328062 : r2.set_varying (TREE_TYPE (ssa2));
1868 6869715 : if (e0 && gori ()->edge_range_p (r, e0, name, *m_query)
1869 6054568 : && !r.undefined_p ()
1870 17352241 : && !r.varying_p ())
1871 : {
1872 5251751 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1873 5251751 : if (relation != VREL_VARYING)
1874 929750 : register_relation (e0, relation, ssa1, ssa2);
1875 : }
1876 6725531 : if (e1 && gori ()->edge_range_p (r, e1, name, *m_query)
1877 5910256 : && !r.undefined_p ()
1878 17214241 : && !r.varying_p ())
1879 : {
1880 4625707 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1881 4625707 : if (relation != VREL_VARYING)
1882 509538 : register_relation (e1, relation, ssa1, ssa2);
1883 : }
1884 : }
1885 43919892 : }
1886 75917121 : }
|