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 520836828 : fur_source::fur_source (range_query *q)
58 : {
59 520836828 : if (q)
60 520835642 : m_query = q;
61 : else
62 2372 : m_query = get_range_query (cfun);
63 520836828 : m_depend_p = false;
64 520836828 : }
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 4830785 : fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
87 : tree op2 ATTRIBUTE_UNUSED)
88 : {
89 4830785 : return VREL_VARYING;
90 : }
91 :
92 : // Default registers nothing and returns false meaning nothing changed.
93 :
94 : bool
95 24605843 : 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 24605843 : return false;
101 : }
102 :
103 : // Default registers nothing and returns false meaning nothing changed.
104 :
105 : bool
106 7537073 : 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 7537073 : return false;
112 : }
113 :
114 : // Get the value of EXPR on edge m_edge.
115 :
116 : bool
117 60263291 : fur_edge::get_operand (vrange &r, tree expr)
118 : {
119 60263291 : 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 463559943 : fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
136 : {
137 463559943 : m_stmt = s;
138 463559943 : }
139 :
140 : // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
141 :
142 : bool
143 658281442 : fur_stmt::get_operand (vrange &r, tree expr)
144 : {
145 658281442 : 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 49848341 : fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
153 : {
154 : // Pick up the range of expr from edge E.
155 49848341 : fur_edge e_src (e, m_query);
156 49848341 : return e_src.get_operand (r, expr);
157 : }
158 :
159 : // Return relation based from m_stmt.
160 :
161 : relation_kind
162 110064184 : fur_stmt::query_relation (tree op1, tree op2)
163 : {
164 110064184 : 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 249032693 : fur_depend::fur_depend (gimple *s, range_query *q, ranger_cache *c)
170 249032693 : : fur_stmt (s, q), m_cache (c)
171 : {
172 249032693 : m_depend_p = true;
173 249032693 : }
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 34785737 : fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
180 : {
181 34785737 : 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 26087742 : if (m_cache)
187 : {
188 26087571 : m_cache->update_consumers (op1);
189 26087571 : 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 7438406 : fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
199 : {
200 7438406 : 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 7420325 : if (m_cache)
206 : {
207 7420323 : m_cache->update_consumers (op1);
208 7420323 : 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 926101 : fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
234 : {
235 926101 : m_list = m_local;
236 926101 : m_index = 0;
237 926101 : m_limit = 1;
238 926101 : m_local[0] = &r1;
239 926101 : }
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 1844385 : 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 1844385 : if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
269 918284 : return m_query->range_of_expr (r, expr);
270 926101 : r = *m_list[m_index++];
271 926101 : 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 926101 : fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
286 : {
287 926101 : fold_using_range f;
288 926101 : fur_list src (r1, q);
289 926101 : 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 73542847 : fold_range (vrange &r, gimple *s, range_query *q)
318 : {
319 73542847 : fold_using_range f;
320 73542847 : fur_stmt src (s, q);
321 73542847 : 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 6502437 : fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
328 : {
329 6502437 : fold_using_range f;
330 6502437 : fur_edge src (on_edge, q);
331 6502437 : 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 1119522 : fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
423 : {
424 1119522 : def_op1 = def_op2 = op1_op2 = VREL_VARYING;
425 1119522 : }
426 :
427 : // Construct a trio from what is known.
428 :
429 : relation_trio
430 1119522 : fur_relation::trio () const
431 : {
432 1119522 : 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 1100617 : fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
449 : tree op2)
450 : {
451 1100617 : tree lhs = gimple_get_lhs (stmt);
452 1100617 : tree a1 = NULL_TREE;
453 1100617 : tree a2 = NULL_TREE;
454 1100617 : 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 1100617 : case GIMPLE_ASSIGN:
461 1100617 : a1 = gimple_assign_rhs1 (stmt);
462 1100617 : if (gimple_num_ops (stmt) >= 3)
463 1100617 : 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 1100617 : if (op1 == lhs)
471 : {
472 1100617 : if (op2 == a1)
473 1100617 : 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 1100617 : && op1_op2 == VREL_VARYING;
493 : }
494 :
495 : // Return the relation trio for stmt S using query Q.
496 :
497 : relation_trio
498 1119522 : fold_relations (gimple *s, range_query *q)
499 : {
500 1119522 : fold_using_range f;
501 1119522 : fur_relation src (s, q);
502 1119522 : tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
503 1119522 : if (lhs)
504 : {
505 1119522 : value_range vr(TREE_TYPE (lhs));
506 1119522 : if (f.fold_stmt (vr, s, src))
507 1119522 : return src.trio ();
508 1119522 : }
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 2841304 : adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
526 : {
527 2841304 : tree op0 = gimple_assign_rhs1 (diff_stmt);
528 2841304 : tree op1 = gimple_assign_rhs2 (diff_stmt);
529 2841304 : tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
530 2841304 : tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
531 2841304 : gimple *call;
532 :
533 2841304 : if (TREE_CODE (op0) == SSA_NAME
534 2813474 : && TREE_CODE (op1) == SSA_NAME
535 2768406 : && (call = SSA_NAME_DEF_STMT (op0))
536 2768406 : && is_gimple_call (call)
537 81024 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
538 60271 : && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
539 60020 : && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
540 60020 : && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
541 59442 : && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
542 59442 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
543 59442 : && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
544 2881745 : && 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 2841304 : }
552 :
553 : // Adjust the range for an IMAGPART_EXPR.
554 :
555 : static void
556 1022523 : adjust_imagpart_expr (vrange &res, const gimple *stmt)
557 : {
558 1022523 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
559 :
560 1022523 : if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
561 : return;
562 :
563 892901 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
564 892901 : if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
565 : {
566 412804 : switch (gimple_call_internal_fn (def_stmt))
567 : {
568 392274 : case IFN_ADD_OVERFLOW:
569 392274 : case IFN_SUB_OVERFLOW:
570 392274 : case IFN_MUL_OVERFLOW:
571 392274 : case IFN_UADDC:
572 392274 : case IFN_USUBC:
573 392274 : case IFN_ATOMIC_COMPARE_EXCHANGE:
574 392274 : {
575 392274 : int_range<2> r;
576 392274 : r.set_varying (boolean_type_node);
577 392274 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
578 392274 : range_cast (r, type);
579 392274 : res.intersect (r);
580 392274 : }
581 : default:
582 : break;
583 : }
584 : return;
585 : }
586 480097 : gassign *assign = dyn_cast<gassign *> (def_stmt);
587 197118 : if (assign && gimple_assign_rhs_code (assign) == COMPLEX_CST)
588 : {
589 18 : tree cst = gimple_assign_rhs1 (assign);
590 18 : if (TREE_CODE (cst) == COMPLEX_CST
591 18 : && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
592 : {
593 7 : wide_int w = wi::to_wide (TREE_IMAGPART (cst));
594 7 : int_range<1> imag (TREE_TYPE (TREE_IMAGPART (cst)), w, w);
595 7 : res.intersect (imag);
596 7 : }
597 : }
598 : }
599 :
600 : // Adjust the range for a REALPART_EXPR.
601 :
602 : static void
603 977190 : adjust_realpart_expr (vrange &res, const gimple *stmt)
604 : {
605 977190 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
606 :
607 977190 : if (TREE_CODE (name) != SSA_NAME)
608 : return;
609 :
610 839838 : auto *def_stmt = safe_dyn_cast<gassign *> (SSA_NAME_DEF_STMT (name));
611 100390 : if (!def_stmt)
612 : return;
613 :
614 195134 : if (gimple_assign_rhs_code (def_stmt) == COMPLEX_CST)
615 : {
616 10 : tree cst = gimple_assign_rhs1 (def_stmt);
617 10 : if (TREE_CODE (cst) == COMPLEX_CST
618 10 : && TREE_CODE (TREE_TYPE (TREE_TYPE (cst))) == INTEGER_TYPE)
619 : {
620 0 : wide_int imag = wi::to_wide (TREE_REALPART (cst));
621 0 : int_range<2> tmp (TREE_TYPE (TREE_REALPART (cst)), imag, imag);
622 0 : res.intersect (tmp);
623 0 : }
624 : }
625 : }
626 :
627 : // This function looks for situations when walking the use/def chains
628 : // may provide additional contextual range information not exposed on
629 : // this statement.
630 :
631 : static void
632 195803709 : gimple_range_adjustment (vrange &res, const gimple *stmt)
633 : {
634 195803709 : switch (gimple_expr_code (stmt))
635 : {
636 2841304 : case POINTER_DIFF_EXPR:
637 2841304 : adjust_pointer_diff_expr (as_a <irange> (res), stmt);
638 2841304 : return;
639 :
640 1022523 : case IMAGPART_EXPR:
641 1022523 : adjust_imagpart_expr (res, stmt);
642 1022523 : return;
643 :
644 977190 : case REALPART_EXPR:
645 977190 : adjust_realpart_expr (res, stmt);
646 977190 : return;
647 :
648 : default:
649 : break;
650 : }
651 : }
652 :
653 : // Provide context to the gimple fold callback.
654 :
655 : static struct
656 : {
657 : gimple *m_stmt;
658 : range_query *m_query;
659 : } x_fold_context;
660 :
661 : // Gimple fold callback.
662 :
663 : static tree
664 91434449 : pta_valueize (tree name)
665 : {
666 91434449 : tree ret
667 91434449 : = x_fold_context.m_query->value_of_expr (name, x_fold_context.m_stmt);
668 :
669 91434449 : return ret ? ret : name;
670 : }
671 :
672 : // Calculate a range for statement S and return it in R. If NAME is provided it
673 : // represents the SSA_NAME on the LHS of the statement. It is only required
674 : // if there is more than one lhs/output. If a range cannot
675 : // be calculated, return false.
676 :
677 : bool
678 307529444 : fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
679 : {
680 307529444 : bool res = false;
681 : // If name and S are specified, make sure it is an LHS of S.
682 307529444 : gcc_checking_assert (!name || !gimple_get_lhs (s) ||
683 : name == gimple_get_lhs (s));
684 :
685 : if (!name)
686 162203496 : name = gimple_get_lhs (s);
687 :
688 : // Process addresses and loads from static constructors.
689 307529444 : if (auto a = dyn_cast<gassign *> (s))
690 198563586 : if (range_from_readonly_var (r, a, 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 307399144 : range_query *saved_query = set_range_query (cfun, src.query ());
700 :
701 307399144 : gimple_range_op_handler handler (s);
702 307399144 : gassign *assign = dyn_cast<gassign *> (s);
703 268368774 : if (assign && gimple_assign_rhs_code (assign) == ADDR_EXPR)
704 3727888 : res = range_of_address (as_a <prange> (r), assign, src);
705 303671256 : else if (handler)
706 195826810 : res = range_of_range_op (r, handler, src);
707 107844446 : else if (is_a<gphi *>(s))
708 24477386 : res = range_of_phi (r, as_a<gphi *> (s), src);
709 83367060 : else if (is_a<gcall *>(s))
710 13458331 : res = range_of_call (r, as_a<gcall *> (s), src);
711 69908729 : else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
712 176601 : 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 237667016 : if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ()))
718 : {
719 121903944 : irange &ir = as_a <irange> (r);
720 121903944 : tree type = r.type ();
721 121903944 : auto typemax = wi::to_wide (TYPE_MAX_VALUE (type));
722 121903944 : auto typemin = wi::to_wide (TYPE_MIN_VALUE (type));
723 121903944 : auto precisionmax = wi::max_value (TYPE_PRECISION (type),
724 243807888 : TYPE_SIGN (type));
725 121903944 : auto precisionmin = wi::min_value (TYPE_PRECISION (type),
726 243807888 : TYPE_SIGN (type));
727 243731324 : if (typemax != precisionmax || typemin != precisionmin)
728 76564 : ir.set (type, typemin, typemax);
729 121905042 : }
730 :
731 307399144 : if (!res)
732 : {
733 : // Restore the original query.
734 69732128 : if (saved_query)
735 14760920 : set_range_query (cfun, saved_query);
736 : // If no name specified or range is unsupported, bail.
737 69732128 : if (!name || !gimple_range_ssa_p (name))
738 : return false;
739 : // We don't understand the stmt, so return the global range.
740 69681389 : gimple_range_global (r, name);
741 69681389 : return true;
742 : }
743 :
744 237667016 : if (r.undefined_p ())
745 : {
746 : // Restore the original query.
747 62712 : if (saved_query)
748 1565 : 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 237604304 : if (name && TREE_TYPE (name) != r.type ())
755 : {
756 3641411 : gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
757 3641411 : range_cast (r, TREE_TYPE (name));
758 : }
759 :
760 237604304 : if (is_a <prange> (r))
761 : {
762 26583974 : prange &p = as_a <prange> (r);
763 : // Check to see if points_to should be set.
764 26583974 : auto assign = dyn_cast<gassign *> (s);
765 26583974 : if (p.addr_unknown_p () && name && assign)
766 : {
767 15979427 : tree rhs = gimple_assign_rhs1 (assign);
768 15979427 : tree_code code = gimple_assign_rhs_code (assign);
769 : // If code is SSA_NAME, any points to would already be copied.
770 15979427 : if (code != SSA_NAME
771 14011112 : && get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
772 19940830 : && TREE_CODE (rhs) == ADDR_EXPR)
773 3725638 : p.set_addr (rhs, true);
774 : else
775 : {
776 : // If we couldn't find anything, try fold.
777 12253789 : x_fold_context = { s, get_range_query (cfun) };
778 12253789 : rhs = gimple_fold_stmt_to_constant_1 (s, pta_valueize,
779 : pta_valueize);
780 12253789 : if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
781 22482 : p.set_addr (rhs, true);
782 : }
783 : }
784 : }
785 : // Restore the original query.
786 237604304 : if (saved_query)
787 55348240 : set_range_query (cfun, saved_query);
788 : return true;
789 : }
790 :
791 : // Calculate a range for range_op statement S and return it in R. If any
792 : // If a range cannot be calculated, return false.
793 :
794 : bool
795 195826810 : fold_using_range::range_of_range_op (vrange &r,
796 : gimple_range_op_handler &handler,
797 : fur_source &src)
798 : {
799 195826810 : gcc_checking_assert (handler);
800 195826810 : gimple *s = handler.stmt ();
801 195826810 : tree type = gimple_range_type (s);
802 195826810 : if (!type)
803 : return false;
804 :
805 195826810 : tree lhs = handler.lhs ();
806 195826810 : tree op1 = handler.operand1 ();
807 195826810 : tree op2 = handler.operand2 ();
808 :
809 : // Certain types of builtin functions may have no arguments.
810 195826810 : if (!op1)
811 : {
812 23101 : value_range r1 (type);
813 23101 : if (!handler.fold_range (r, type, r1, r1))
814 0 : r.set_varying (type);
815 23101 : return true;
816 23101 : }
817 :
818 195803709 : value_range range1 (TREE_TYPE (op1));
819 195803709 : value_range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
820 :
821 195803709 : if (src.get_operand (range1, op1))
822 : {
823 195803709 : if (!op2)
824 : {
825 : // Fold range, and register any dependency if available.
826 40867341 : value_range r2 (type);
827 40867341 : r2.set_varying (type);
828 40867341 : if (!handler.fold_range (r, type, range1, r2))
829 334512 : r.set_varying (type);
830 40867341 : if (lhs && gimple_range_ssa_p (op1))
831 : {
832 59927681 : if (src.gori_ssa ())
833 23135588 : src.gori_ssa ()->register_dependency (lhs, op1);
834 36792060 : relation_kind rel;
835 36792060 : rel = handler.lhs_op1_relation (r, range1, range1);
836 36792060 : if (rel != VREL_VARYING)
837 27480131 : src.register_relation (s, rel, lhs, op1);
838 : }
839 40867341 : }
840 154936368 : else if (src.get_operand (range2, op2))
841 : {
842 154936368 : relation_kind rel = src.query_relation (op1, op2);
843 154936368 : if (dump_file && (dump_flags & TDF_DETAILS) && rel != VREL_VARYING)
844 : {
845 160 : fprintf (dump_file, " folding with relation ");
846 160 : print_generic_expr (dump_file, op1, TDF_SLIM);
847 160 : print_relation (dump_file, rel);
848 160 : print_generic_expr (dump_file, op2, TDF_SLIM);
849 160 : fputc ('\n', dump_file);
850 : }
851 : // Fold range, and register any dependency if available.
852 154936368 : if (!handler.fold_range (r, type, range1, range2,
853 : relation_trio::op1_op2 (rel)))
854 261 : r.set_varying (type);
855 154936368 : if (irange::supports_p (type))
856 142316836 : relation_fold_and_or (as_a <irange> (r), s, src, range1, range2);
857 154936368 : if (lhs)
858 : {
859 158635052 : if (src.gori_ssa ())
860 : {
861 62926104 : src.gori_ssa ()->register_dependency (lhs, op1);
862 125852208 : src.gori_ssa ()->register_dependency (lhs, op2);
863 : }
864 95708615 : if (gimple_range_ssa_p (op1))
865 : {
866 93143062 : relation_kind rel2 = handler.lhs_op1_relation (r, range1,
867 93143062 : range2, rel);
868 93143062 : if (rel2 != VREL_VARYING)
869 40004858 : src.register_relation (s, rel2, lhs, op1);
870 : }
871 95708615 : if (gimple_range_ssa_p (op2))
872 : {
873 38262283 : relation_kind rel2 = handler.lhs_op2_relation (r, range1,
874 38262283 : range2, rel);
875 38262283 : if (rel2 != VREL_VARYING)
876 2348818 : src.register_relation (s, rel2, lhs, op2);
877 : }
878 : }
879 : // Check for an existing BB, as we maybe asked to fold an
880 : // artificial statement not in the CFG.
881 59227753 : else if (is_a<gcond *> (s) && gimple_bb (s))
882 : {
883 50484272 : basic_block bb = gimple_bb (s);
884 50484272 : edge e0 = EDGE_SUCC (bb, 0);
885 : /* During RTL expansion one of the edges can be removed
886 : if expansion proves the jump is unconditional. */
887 50484272 : edge e1 = single_succ_p (bb) ? NULL : EDGE_SUCC (bb, 1);
888 :
889 50484272 : gcc_checking_assert (e1 || currently_expanding_to_rtl);
890 50484272 : if (!single_pred_p (e0->dest))
891 12172972 : e0 = NULL;
892 50484272 : if (e1 && !single_pred_p (e1->dest))
893 : e1 = NULL;
894 50484272 : src.register_outgoing_edges (as_a<gcond *> (s),
895 : as_a <irange> (r), e0, e1);
896 : }
897 : }
898 : else
899 0 : r.set_varying (type);
900 : }
901 : else
902 0 : r.set_varying (type);
903 : // Make certain range-op adjustments that aren't handled any other way.
904 195803709 : gimple_range_adjustment (r, s);
905 195803709 : return true;
906 195803709 : }
907 :
908 : // Calculate the range of an assignment containing an ADDR_EXPR.
909 : // Return the range in R.
910 : // If a range cannot be calculated, set it to VARYING and return true.
911 :
912 : bool
913 3727888 : fold_using_range::range_of_address (prange &r, gassign *stmt, fur_source &src)
914 : {
915 7455776 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
916 :
917 3727888 : tree expr = gimple_assign_rhs1 (stmt);
918 3727888 : poly_int64 bitsize, bitpos;
919 3727888 : tree offset;
920 3727888 : machine_mode mode;
921 3727888 : int unsignedp, reversep, volatilep;
922 3727888 : tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
923 : &bitpos, &offset, &mode, &unsignedp,
924 : &reversep, &volatilep);
925 :
926 :
927 3727888 : if (base != NULL_TREE
928 3727888 : && TREE_CODE (base) == MEM_REF
929 7239590 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
930 : {
931 3511613 : tree ssa = TREE_OPERAND (base, 0);
932 3511613 : tree lhs = gimple_get_lhs (stmt);
933 6009269 : if (lhs && gimple_range_ssa_p (ssa) && src.gori_ssa ())
934 2497656 : src.gori_ssa ()->register_dependency (lhs, ssa);
935 3511613 : src.get_operand (r, ssa);
936 3511613 : range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
937 :
938 3511613 : poly_offset_int off = 0;
939 3511613 : bool off_cst = false;
940 3511613 : if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
941 : {
942 3426064 : off = mem_ref_offset (base);
943 3426064 : if (offset)
944 48 : off += poly_offset_int::from (wi::to_poly_wide (offset),
945 48 : SIGNED);
946 3426064 : off <<= LOG2_BITS_PER_UNIT;
947 3426064 : off += bitpos;
948 : off_cst = true;
949 : }
950 : /* If &X->a is equal to X, the range of X is the result. */
951 3426064 : if (off_cst && known_eq (off, 0))
952 1264076 : return true;
953 2247537 : else if (flag_delete_null_pointer_checks
954 2247537 : && !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 2246066 : if (r.undefined_p ()
959 4492132 : || !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 1621102 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
965 1621102 : 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 626435 : if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
972 626219 : && off_cst
973 562621 : && known_ne (off, 0)
974 1189056 : && (flag_delete_null_pointer_checks || known_gt (off, 0)))
975 : {
976 562621 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
977 562621 : return true;
978 : }
979 63814 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
980 63814 : return true;
981 : }
982 :
983 : // Handle "= &a".
984 216275 : if (tree_single_nonzero_p (expr))
985 : {
986 215024 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
987 215024 : return true;
988 : }
989 :
990 : // Otherwise return varying.
991 1251 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
992 1251 : return true;
993 : }
994 :
995 : // Does R contain anything in [LO, HI]?
996 :
997 : static bool
998 88 : intersects_p (const irange &r, const wide_int &lo, const wide_int &hi)
999 : {
1000 88 : int_range_max tmp (r);
1001 88 : tmp.intersect (int_range<1> (r.type (), lo, hi));
1002 88 : return !tmp.undefined_p ();
1003 88 : }
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 132101 : ctor_ref_range (vrange &r, tree type, const vec<tree> &component_path,
1020 : fur_source &src)
1021 132101 : : m_r (r), m_type (type), m_component_path (component_path),
1022 132101 : 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 899234 : ctor_ref_range::accumulate_range (tree init, unsigned i)
1041 : {
1042 1798468 : if (i == m_component_path.length ())
1043 742384 : return accumulate_range_from_scalar (init);
1044 156850 : if (TREE_CODE (init) != CONSTRUCTOR && TREE_CODE (init) != STRING_CST)
1045 : return false;
1046 :
1047 156850 : tree ref = m_component_path[i];
1048 156850 : if (TREE_CODE (ref) == COMPONENT_REF)
1049 14564 : return accumulate_range_from_component_ref (init, i + 1,
1050 29128 : TREE_OPERAND (ref, 1));
1051 :
1052 142286 : gcc_assert (TREE_CODE (ref) == ARRAY_REF);
1053 142286 : return accumulate_range_from_array_ref (init, i + 1, TREE_OPERAND (ref, 1));
1054 : }
1055 :
1056 : // Accumulate the range from the scalar INIT at the end of the component path.
1057 : // For example, for t[1] with static const int t[2] = { 4, 5 }, INIT is 5 and
1058 : // the range is [5, 5].
1059 :
1060 : bool
1061 742384 : ctor_ref_range::accumulate_range_from_scalar (tree init)
1062 : {
1063 742384 : if (!useless_type_conversion_p (m_type, TREE_TYPE (init)))
1064 : return false;
1065 :
1066 742384 : if (POINTER_TYPE_P (m_type))
1067 : {
1068 173010 : prange elt;
1069 173010 : if (integer_zerop (init))
1070 102 : elt.set_zero (m_type);
1071 172908 : else if (tree_single_nonzero_p (init))
1072 172880 : elt.set_nonzero (m_type);
1073 : else
1074 : return false;
1075 172982 : m_r.union_ (elt);
1076 172982 : return true;
1077 173010 : }
1078 :
1079 569374 : if (TREE_CODE (init) == REAL_CST)
1080 : {
1081 68599 : const REAL_VALUE_TYPE *rv = TREE_REAL_CST_PTR (init);
1082 68599 : frange elt;
1083 68599 : if (real_isnan (rv))
1084 956 : elt.set_nan (m_type, real_isneg (rv));
1085 : else
1086 67643 : elt.set (m_type, *rv, *rv, nan_state (false));
1087 68599 : m_r.union_ (elt);
1088 68599 : return true;
1089 68599 : }
1090 :
1091 500775 : if (TREE_CODE (init) != INTEGER_CST)
1092 : return false;
1093 :
1094 500704 : wide_int wi_cst = wi::to_wide (init);
1095 500704 : m_r.union_ (int_range<1> (m_type, wi_cst, wi_cst));
1096 500704 : return true;
1097 500704 : }
1098 :
1099 : // Accumulate the range from field FLD of the record or union initializer
1100 : // INIT. FLD is the field of a COMPONENT_REF in the component path and I the
1101 : // step after it. For example, when walking s.b in:
1102 : //
1103 : // static const struct { int a, b; } s = { 1, 2 };
1104 : //
1105 : // INIT is { 1, 2 }, FLD is b, and I is 1, the end of the path, and the range
1106 : // is [2, 2].
1107 :
1108 : bool
1109 14564 : ctor_ref_range::accumulate_range_from_component_ref (tree init, unsigned i,
1110 : tree fld)
1111 : {
1112 14564 : unsigned ix;
1113 14564 : tree index, val;
1114 14564 : if (TREE_CODE (init) != CONSTRUCTOR)
1115 : return false;
1116 14564 : if (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == DECL_CONTEXT (fld))
1117 22913 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix, index, val)
1118 : {
1119 22862 : if (index != fld)
1120 8415 : continue;
1121 14447 : return accumulate_range (val, i);
1122 : }
1123 66 : else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (TREE_TYPE (init)))
1124 66 : == TYPE_CANONICAL (DECL_CONTEXT (fld)))
1125 : {
1126 : // If fld is from a different record/union than the CONSTRUCTOR,
1127 : // fallback to bit offset/size comparison. This happens e.g.
1128 : // with C++ pointer-to-members, where we can get different
1129 : // __ptrmemfunc_type RECORD_TYPEs with __pfn and __delta
1130 : // FIELD_DECLs at the same offsets.
1131 66 : tree off = bit_position (fld);
1132 66 : tree sz = DECL_SIZE (fld);
1133 66 : if (TREE_CODE (off) != INTEGER_CST
1134 66 : || TREE_CODE (sz) != INTEGER_CST
1135 132 : || int_size_in_bytes (TREE_TYPE (init)) == -1)
1136 66 : return false;
1137 66 : offset_int woff = wi::to_offset (off);
1138 66 : offset_int wsz = wi::to_offset (sz);
1139 80 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix, index, val)
1140 : {
1141 80 : off = bit_position (index);
1142 80 : sz = DECL_SIZE (index);
1143 80 : if (TREE_CODE (off) != INTEGER_CST || TREE_CODE (sz) != INTEGER_CST)
1144 : return false;
1145 146 : if (wi::to_offset (off) != woff || wi::to_offset (sz) != wsz)
1146 14 : continue;
1147 66 : return accumulate_range (val, i);
1148 : }
1149 : }
1150 : else
1151 : return false;
1152 51 : if (TREE_CODE (TREE_TYPE (init)) == RECORD_TYPE)
1153 : {
1154 9 : accumulate_range_from_missing_constructor_elt ();
1155 9 : return true;
1156 : }
1157 : else
1158 : /* Missing constructor of a union field just isn't like other missing
1159 : constructor parts. */
1160 : return false;
1161 : }
1162 :
1163 : // Accumulate the range from the element at IDX of the array initializer INIT,
1164 : // or from every element when IDX is not a constant. IDX is the index of an
1165 : // ARRAY_REF in the component path and I the step after it. For example,
1166 : // when walking t[1] or t[n] in:
1167 : //
1168 : // static const int t[2] = { 4, 5 };
1169 : //
1170 : // INIT is { 4, 5 } and I is 1, the end of the path. For t[1] the range is
1171 : // [5, 5] and for t[n] it is [4, 5]. When every element is loaded, whatever
1172 : // the initializer leaves out reads as zero: take each element's indices out
1173 : // of the domain to see if anything is left.
1174 :
1175 : bool
1176 142286 : ctor_ref_range::accumulate_range_from_array_ref (tree init, unsigned i,
1177 : tree idx)
1178 : {
1179 142286 : tree domain = TYPE_DOMAIN (TREE_TYPE (init));
1180 142286 : if (!TYPE_MIN_VALUE (domain)
1181 142286 : || !TYPE_MAX_VALUE (domain)
1182 142286 : || !tree_fits_uhwi_p (TYPE_MIN_VALUE (domain))
1183 284572 : || !tree_fits_uhwi_p (TYPE_MAX_VALUE (domain)))
1184 : return false;
1185 :
1186 : // Unsigned bounds that look reversed are signed, as they are for
1187 : // get_array_ctor_element_at_index: Ada's -1 .. 5 has a lower bound of
1188 : // SIZE_MAX.
1189 142145 : tree type = TREE_TYPE (TYPE_MIN_VALUE (domain));
1190 142145 : if (TYPE_UNSIGNED (type)
1191 142145 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain), TYPE_MIN_VALUE (domain)))
1192 0 : type = signed_type_for (type);
1193 142145 : wide_int min = wi::to_wide (TYPE_MIN_VALUE (domain));
1194 142145 : wide_int max = wi::to_wide (TYPE_MAX_VALUE (domain));
1195 :
1196 : // Ada represents an empty array with reversed bounds.
1197 142145 : if (wi::gt_p (min, max, TYPE_SIGN (type)))
1198 : return true;
1199 :
1200 142145 : unsigned prec = TYPE_PRECISION (type);
1201 142145 : int_range<1> domain_r (type, min, max);
1202 :
1203 : // Narrow IDX to the ranges it can actually take.
1204 142145 : int_range_max idx_r;
1205 142145 : m_src.get_operand (idx_r, idx);
1206 142145 : range_cast (idx_r, type);
1207 142145 : idx_r.intersect (domain_r);
1208 142145 : if (idx_r.undefined_p ())
1209 : return false;
1210 :
1211 142141 : int_range_max missing (idx_r);
1212 142141 : wide_int next = domain_r.lower_bound ();
1213 142141 : unsigned ix;
1214 142141 : tree index, val;
1215 :
1216 142141 : if (TREE_CODE (init) == STRING_CST)
1217 : {
1218 14785 : if (i != m_component_path.length ()
1219 14785 : || !INTEGRAL_TYPE_P (m_type)
1220 14785 : || TYPE_PRECISION (m_type) != CHAR_BIT
1221 14670 : || !useless_type_conversion_p (m_type, TREE_TYPE (TREE_TYPE (init)))
1222 44240 : || wi::gtu_p (max + 1 - min, TREE_STRING_LENGTH (init)))
1223 : return false;
1224 28923 : for (unsigned j = 0; j < idx_r.num_pairs (); ++j)
1225 : {
1226 14469 : wide_int l = idx_r.lower_bound (j);
1227 14469 : wide_int u = idx_r.upper_bound (j);
1228 14469 : if (wi::lt_p (u, min, TYPE_SIGN (type))
1229 14469 : || wi::gt_p (l, max, TYPE_SIGN (type)))
1230 0 : continue;
1231 14469 : l = wi::max (l, min, TYPE_SIGN (type));
1232 14469 : u = wi::min (u, max, TYPE_SIGN (type));
1233 14469 : unsigned n = (l - min).to_uhwi ();
1234 14469 : unsigned e = (u - min).to_uhwi ();
1235 : /* Note, if we'd have more than 256 iterations of the loops
1236 : below or even across all the outer loop iterations, it might
1237 : be more efficient to have a 256 bools array or 256-bit bitmask,
1238 : gather which bytes are seen and then union_ those which were
1239 : seen. Given the current param limits this can't ever happen. */
1240 14469 : wide_int wi_cst;
1241 14469 : if (TYPE_UNSIGNED (m_type))
1242 10567 : for (; n <= e; ++n)
1243 : {
1244 7229 : wi_cst
1245 7229 : = wi::uhwi ((unsigned char) TREE_STRING_POINTER (init)[n],
1246 7229 : CHAR_BIT);
1247 7229 : m_r.union_ (int_range<1> (m_type, wi_cst, wi_cst));
1248 : }
1249 : else
1250 177684 : for (; n <= e; ++n)
1251 : {
1252 166553 : wi_cst
1253 166553 : = wi::shwi ((signed char) TREE_STRING_POINTER (init)[n],
1254 166553 : CHAR_BIT);
1255 166553 : m_r.union_ (int_range<1> (m_type, wi_cst, wi_cst));
1256 : }
1257 14469 : }
1258 : return true;
1259 : }
1260 :
1261 : // Build a range with the entire domain, and take out any elements which are
1262 : // initialized. If there's anything left in that range, it means there are
1263 : // missing elements and we should add a [0, 0] to the final range.
1264 985248 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix, index, val)
1265 : {
1266 858351 : wide_int lo, hi;
1267 :
1268 : // A null index means the element follows the previous one.
1269 858351 : if (!index)
1270 0 : lo = hi = next;
1271 858351 : else if (TREE_CODE (index) == RANGE_EXPR)
1272 : {
1273 80 : lo = wi::to_wide (TREE_OPERAND (index, 0), prec);
1274 80 : hi = wi::to_wide (TREE_OPERAND (index, 1), prec);
1275 : }
1276 : else
1277 : {
1278 858271 : gcc_checking_assert (TREE_CODE (index) == INTEGER_CST);
1279 858271 : lo = hi = wi::to_wide (index, prec);
1280 : }
1281 : // A RAW_DATA_CST holds one element per byte.
1282 858351 : if (TREE_CODE (val) == RAW_DATA_CST)
1283 8 : hi = wi::add (hi, RAW_DATA_LENGTH (val) - 1);
1284 858351 : missing.intersect (int_range<2> (type, lo, hi, VR_ANTI_RANGE));
1285 858351 : next = wi::add (hi, 1);
1286 :
1287 858351 : if (TREE_CODE (val) == RAW_DATA_CST)
1288 : {
1289 8 : if (!intersects_p (idx_r, lo, hi))
1290 0 : continue;
1291 :
1292 8 : if (i != m_component_path.length ()
1293 8 : || !INTEGRAL_TYPE_P (m_type)
1294 8 : || TYPE_PRECISION (m_type) != CHAR_BIT
1295 16 : || !useless_type_conversion_p (m_type,
1296 8 : TREE_TYPE (val)))
1297 : return false;
1298 :
1299 16 : for (unsigned j = 0; j < idx_r.num_pairs (); ++j)
1300 : {
1301 8 : wide_int l = idx_r.lower_bound (j);
1302 8 : wide_int u = idx_r.upper_bound (j);
1303 8 : if (wi::lt_p (u, lo, TYPE_SIGN (type))
1304 8 : || wi::gt_p (l, hi, TYPE_SIGN (type)))
1305 0 : continue;
1306 8 : l = wi::max (l, lo, TYPE_SIGN (type));
1307 8 : u = wi::min (u, hi, TYPE_SIGN (type));
1308 8 : unsigned n = (l - lo).to_uhwi ();
1309 8 : unsigned e = (u - lo).to_uhwi ();
1310 : /* Note, if we'd have more than 256 iterations of the loops
1311 : below or even across all the outer loop iterations, it might
1312 : be more efficient to have a 256 bools array or 256-bit
1313 : bitmask, gather which bytes are seen and then union_ those
1314 : which were seen. Given the current param limits this can't
1315 : ever happen. */
1316 8 : wide_int wi_cst;
1317 8 : if (TYPE_UNSIGNED (m_type))
1318 1790 : for (; n <= e; ++n)
1319 : {
1320 1782 : wi_cst = wi::uhwi (RAW_DATA_UCHAR_ELT (val, n), CHAR_BIT);
1321 1782 : m_r.union_ (int_range<1> (m_type, wi_cst, wi_cst));
1322 : }
1323 : else
1324 0 : for (; n <= e; ++n)
1325 : {
1326 0 : wi_cst = wi::shwi (RAW_DATA_SCHAR_ELT (val, n), CHAR_BIT);
1327 0 : m_r.union_ (int_range<1> (m_type, wi_cst, wi_cst));
1328 : }
1329 8 : }
1330 8 : continue;
1331 8 : }
1332 :
1333 858343 : if (lo == hi)
1334 : {
1335 858263 : if (!idx_r.contains_p (lo))
1336 105723 : continue;
1337 : }
1338 80 : else if (!intersects_p (idx_r, lo, hi))
1339 0 : continue;
1340 :
1341 752620 : if (!accumulate_range (val, i))
1342 : return false;
1343 858351 : }
1344 :
1345 126897 : if (!missing.undefined_p ())
1346 1076 : accumulate_range_from_missing_constructor_elt ();
1347 : return true;
1348 284290 : }
1349 :
1350 : // Accumulate [0, 0] for a constructor element the initializer leaves out.
1351 : // For example, when loading t[1] in:
1352 : //
1353 : // static const int t[2] = { 4 };
1354 :
1355 : void
1356 1085 : ctor_ref_range::accumulate_range_from_missing_constructor_elt ()
1357 : {
1358 1085 : value_range zero (m_type);
1359 1085 : zero.set_zero (m_type);
1360 1085 : m_r.union_ (zero);
1361 1085 : }
1362 :
1363 : // Attempt to calculate the range of value loaded by STMT (which must be an
1364 : // assignment) if it is a load from a read-only aggregate variable. If
1365 : // successful, return true and set the discovered range in R. Otherwise return
1366 : // false and leave R untouched.
1367 :
1368 : bool
1369 198563586 : fold_using_range::range_from_readonly_var (vrange &r, gassign *stmt,
1370 : fur_source &src)
1371 : {
1372 198563586 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1373 198563586 : if (!irange::supports_p (type)
1374 49333542 : && !prange::supports_p (type)
1375 208350353 : && !frange::supports_p (type))
1376 : return false;
1377 :
1378 198563586 : unsigned HOST_WIDE_INT limit = param_vrp_cstload_limit;
1379 198563586 : if (!limit)
1380 : return false;
1381 :
1382 198544164 : tree t = gimple_assign_rhs1 (stmt);
1383 198544164 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
1384 : return false;
1385 198544164 : limit *= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
1386 :
1387 : // The component path of the reference, from the aggregate out.
1388 198544164 : auto_vec <tree, 4> component_path;
1389 198544164 : while (TREE_CODE (t) == ARRAY_REF
1390 250743388 : || TREE_CODE (t) == COMPONENT_REF)
1391 : {
1392 52199224 : component_path.safe_push (t);
1393 52199224 : t = TREE_OPERAND (t, 0);
1394 : }
1395 198544164 : component_path.reverse ();
1396 397088328 : if (component_path.is_empty ()
1397 33671886 : || (TREE_CODE (t) != VAR_DECL
1398 33671886 : && TREE_CODE (t) != CONST_DECL))
1399 : return false;
1400 :
1401 9609082 : if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (t))
1402 9609082 : || tree_to_uhwi (DECL_SIZE_UNIT (t)) > limit)
1403 : return false;
1404 :
1405 : /* TODO: We perhaps should try to handle at least some cases when the
1406 : declaration is wrapped in a MEM_REF, but we need to be careful to look at
1407 : the right part of the constructor then. */
1408 8422207 : tree ctor = ctor_for_folding (t);
1409 8422207 : if (!ctor
1410 8422197 : || (TREE_CODE (ctor) != CONSTRUCTOR
1411 8422197 : && TREE_CODE (ctor) != STRING_CST))
1412 : return false;
1413 :
1414 132101 : value_range tmp (type);
1415 132101 : ctor_ref_range load (tmp, type, component_path, src);
1416 132101 : bool res = load.accumulate_range (ctor, 0) && !tmp.varying_p ();
1417 130300 : if (res)
1418 130300 : r = tmp;
1419 132101 : return res;
1420 198676265 : }
1421 :
1422 : // Calculate a range for phi statement S and return it in R.
1423 : // If a range cannot be calculated, return false.
1424 :
1425 : bool
1426 24477386 : fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
1427 : {
1428 24477386 : tree phi_def = gimple_phi_result (phi);
1429 24477386 : tree type = gimple_range_type (phi);
1430 24477386 : value_range arg_range (type);
1431 24477386 : unsigned x;
1432 :
1433 24477386 : if (!type)
1434 : return false;
1435 :
1436 : // Track if all executable arguments are the same.
1437 24477386 : tree single_arg = NULL_TREE;
1438 24477386 : bool seen_arg = false;
1439 :
1440 : // Start with an empty range, unioning in each argument's range.
1441 24477386 : r.set_undefined ();
1442 87598178 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1443 : {
1444 49864421 : tree arg = gimple_phi_arg_def (phi, x);
1445 : // An argument that is the same as the def provides no new range.
1446 49864421 : if (arg == phi_def)
1447 16080 : continue;
1448 :
1449 49848341 : edge e = gimple_phi_arg_edge (phi, x);
1450 :
1451 : // Get the range of the argument on its edge.
1452 49848341 : src.get_phi_operand (arg_range, arg, e);
1453 :
1454 49848341 : if (!arg_range.undefined_p ())
1455 : {
1456 49582880 : r.union_ (arg_range);
1457 : // Register potential dependencies for stale value tracking.
1458 83973138 : if (gimple_range_ssa_p (arg) && src.gori_ssa ())
1459 34390246 : src.gori_ssa ()->register_dependency (phi_def, arg);
1460 : }
1461 :
1462 : // Track if all arguments are the same.
1463 49848341 : if (!seen_arg)
1464 : {
1465 : seen_arg = true;
1466 : single_arg = arg;
1467 : }
1468 25370955 : else if (!vrp_operand_equal_p (single_arg, arg))
1469 24126094 : single_arg = NULL_TREE;
1470 :
1471 : // Once the value reaches varying, stop looking.
1472 49848341 : if (r.varying_p () && single_arg == NULL_TREE)
1473 : break;
1474 : }
1475 :
1476 :
1477 : // If the PHI boils down to a single effective argument, look at it.
1478 24477386 : if (single_arg)
1479 : {
1480 : // Symbolic arguments can be equivalences.
1481 2938100 : if (gimple_range_ssa_p (single_arg))
1482 : {
1483 : // Only allow the equivalence if the PHI definition does not
1484 : // dominate any incoming edge for SINGLE_ARG.
1485 : // See PR 108139 and 109462.
1486 2214447 : basic_block bb = gimple_bb (phi);
1487 2214447 : if (!dom_info_available_p (CDI_DOMINATORS))
1488 : single_arg = NULL;
1489 : else
1490 4727195 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1491 2517535 : if (gimple_phi_arg_def (phi, x) == single_arg
1492 5025142 : && dominated_by_p (CDI_DOMINATORS,
1493 2507607 : gimple_phi_arg_edge (phi, x)->src,
1494 : bb))
1495 : {
1496 : single_arg = NULL;
1497 : break;
1498 : }
1499 2213494 : if (single_arg)
1500 2209660 : src.register_relation (phi, VREL_EQ, phi_def, single_arg);
1501 : }
1502 723653 : else if (src.get_operand (arg_range, single_arg))
1503 : {
1504 : // Check if the single argument points to a specific object.
1505 723653 : if (is_a <prange> (arg_range))
1506 : {
1507 62948 : prange &ptr = as_a <prange> (arg_range);
1508 : // If it doesn't already point at something, set points to.
1509 62948 : if (ptr.addr_unknown_p () && TREE_CODE (single_arg) == ADDR_EXPR)
1510 0 : ptr.set_addr (single_arg, true);
1511 62948 : r = ptr;
1512 62948 : return true;
1513 : }
1514 : // Numerical arguments that are a constant can be returned as
1515 : // the constant. This can help fold later cases where even this
1516 : // constant might have been UNDEFINED via an unreachable edge.
1517 660705 : if (arg_range.singleton_p ())
1518 : {
1519 659042 : r = arg_range;
1520 659042 : return true;
1521 : }
1522 : }
1523 : }
1524 :
1525 : // Incorporate any global value. If a PHI analysis phase was run, there may
1526 : // be a restricted global range already. Query the range with no context
1527 : // to get a global range.
1528 :
1529 : // If SCEV is available, query if this PHI has any known values.
1530 23755396 : if (scev_initialized_p ()
1531 23755396 : && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
1532 : {
1533 10048648 : class loop *l = loop_containing_stmt (phi);
1534 10048648 : if (l && loop_outer (l))
1535 : {
1536 7420684 : value_range loop_range (type);
1537 7420684 : range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
1538 7420684 : if (!loop_range.varying_p ())
1539 : {
1540 2499214 : if (dump_file && (dump_flags & TDF_DETAILS))
1541 : {
1542 14553 : fprintf (dump_file, "Loops range found for ");
1543 14553 : print_generic_expr (dump_file, phi_def, TDF_SLIM);
1544 14553 : fprintf (dump_file, ": ");
1545 14553 : loop_range.dump (dump_file);
1546 14553 : fprintf (dump_file, " and calculated range :");
1547 14553 : r.dump (dump_file);
1548 14553 : fprintf (dump_file, "\n");
1549 : }
1550 2499214 : r.intersect (loop_range);
1551 : }
1552 7420684 : }
1553 : }
1554 :
1555 : return true;
1556 24477386 : }
1557 :
1558 : // Calculate a range for call statement S and return it in R.
1559 : // If a range cannot be calculated, return false.
1560 :
1561 : bool
1562 13458331 : fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1563 : {
1564 13458331 : tree type = gimple_range_type (call);
1565 13458331 : if (!type)
1566 : return false;
1567 :
1568 13458331 : tree lhs = gimple_call_lhs (call);
1569 :
1570 13458331 : if (gimple_stmt_nonnegative_p (call))
1571 18109 : r.set_nonnegative (type);
1572 13440222 : else if (gimple_call_nonnull_result_p (call)
1573 13440222 : || gimple_call_nonnull_arg (call))
1574 642853 : r.set_nonzero (type);
1575 : else
1576 12797369 : r.set_varying (type);
1577 :
1578 13458331 : tree callee = gimple_call_fndecl (call);
1579 13458331 : if (callee
1580 13458331 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)), type))
1581 : {
1582 12270642 : value_range val;
1583 12270642 : if (ipa_return_value_range (val, callee))
1584 : {
1585 582345 : r.intersect (val);
1586 582345 : if (dump_file && (dump_flags & TDF_DETAILS))
1587 : {
1588 28 : fprintf (dump_file, "Using return value range of ");
1589 28 : print_generic_expr (dump_file, callee, TDF_SLIM);
1590 28 : fprintf (dump_file, ": ");
1591 28 : val.dump (dump_file);
1592 28 : fprintf (dump_file, "\n");
1593 : }
1594 : }
1595 12270642 : }
1596 :
1597 : // If there is an LHS, intersect that with what is known.
1598 13458331 : if (gimple_range_ssa_p (lhs))
1599 : {
1600 13458331 : value_range def (TREE_TYPE (lhs));
1601 13458331 : gimple_range_global (def, lhs);
1602 13458331 : r.intersect (def);
1603 13458331 : }
1604 : return true;
1605 : }
1606 :
1607 : // Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
1608 : // to further resolve R1 and R2 if there are any dependencies between
1609 : // OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
1610 : // as the origination source location for operands..
1611 : // Effectively, use COND an the edge condition and solve for OP1 on the true
1612 : // edge and OP2 on the false edge.
1613 :
1614 : bool
1615 176601 : fold_using_range::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
1616 : tree op1, tree op2, fur_source &src)
1617 : {
1618 176601 : if (!src.gori () || !src.gori_ssa ())
1619 : return false;
1620 :
1621 130393 : tree ssa1 = gimple_range_ssa_p (op1);
1622 130393 : tree ssa2 = gimple_range_ssa_p (op2);
1623 130393 : if (!ssa1 && !ssa2)
1624 : return false;
1625 118485 : if (TREE_CODE (cond) != SSA_NAME)
1626 : return false;
1627 118485 : gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1628 118326 : if (!cond_def
1629 118326 : || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
1630 : return false;
1631 113638 : tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1632 113638 : if (!value_range::supports_type_p (type)
1633 227276 : || !range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1634 : return false;
1635 113638 : range_op_handler hand (gimple_assign_rhs_code (cond_def));
1636 113638 : if (!hand)
1637 : return false;
1638 :
1639 113638 : tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
1640 227276 : tree c2 = gimple_range_ssa_p (gimple_assign_rhs2 (cond_def));
1641 :
1642 : // Only solve if there is one SSA name in the condition.
1643 113638 : if ((!c1 && !c2) || (c1 && c2))
1644 : return false;
1645 :
1646 : // Pick up the current values of each part of the condition.
1647 29063 : tree rhs1 = gimple_assign_rhs1 (cond_def);
1648 29063 : tree rhs2 = gimple_assign_rhs2 (cond_def);
1649 29063 : value_range cl (TREE_TYPE (rhs1));
1650 29063 : value_range cr (TREE_TYPE (rhs2));
1651 29063 : src.get_operand (cl, rhs1);
1652 29063 : src.get_operand (cr, rhs2);
1653 :
1654 29063 : tree cond_name = c1 ? c1 : c2;
1655 29063 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
1656 :
1657 : // Evaluate the value of COND_NAME on the true and false edges, using either
1658 : // the op1 or op2 routines based on its location.
1659 29063 : value_range cond_true (type), cond_false (type);
1660 29063 : if (c1)
1661 : {
1662 29063 : if (!hand.op1_range (cond_false, type, range_false (), cr))
1663 : return false;
1664 29063 : if (!hand.op1_range (cond_true, type, range_true (), cr))
1665 : return false;
1666 29063 : cond_false.intersect (cl);
1667 29063 : cond_true.intersect (cl);
1668 : }
1669 : else
1670 : {
1671 0 : if (!hand.op2_range (cond_false, type, range_false (), cl))
1672 : return false;
1673 0 : if (!hand.op2_range (cond_true, type, range_true (), cl))
1674 : return false;
1675 0 : cond_false.intersect (cr);
1676 0 : cond_true.intersect (cr);
1677 : }
1678 :
1679 : // Now solve for SSA1 or SSA2 if they are in the dependency chain.
1680 53969 : if (ssa1 && src.gori_ssa()->in_chain_p (ssa1, cond_name))
1681 : {
1682 1992 : value_range tmp1 (TREE_TYPE (ssa1));
1683 3984 : if (src.gori ()->compute_operand_range (tmp1, def_stmt, cond_true,
1684 : ssa1, src))
1685 351 : r1.intersect (tmp1);
1686 1992 : }
1687 47240 : if (ssa2 && src.gori_ssa ()->in_chain_p (ssa2, cond_name))
1688 : {
1689 305 : value_range tmp2 (TREE_TYPE (ssa2));
1690 610 : if (src.gori ()->compute_operand_range (tmp2, def_stmt, cond_false,
1691 : ssa2, src))
1692 214 : r2.intersect (tmp2);
1693 305 : }
1694 : // If the same name is specified in the condition and COND_EXPR,
1695 : // combine the calculated condition range and the other one provided. ie:
1696 : // c_1 = b_2 < 10
1697 : // f_3 = c_1 ? 0 : b_2
1698 : // With b_2 providing the false value, the value of f_3 will be
1699 : // either 0 UNION (0 = b_2 < 10), which is [-INF, 9].
1700 : // COND_EXPR is
1701 29063 : if (ssa1 && cond_name == ssa1)
1702 2455 : r1 = cond_true;
1703 26608 : else if (ssa2 && cond_name == ssa2)
1704 3142 : r2 = cond_false;
1705 : return true;
1706 29063 : }
1707 :
1708 : // Calculate a range for COND_EXPR statement S and return it in R.
1709 : // If a range cannot be calculated, return false.
1710 :
1711 : bool
1712 176601 : fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1713 : {
1714 176601 : tree cond = gimple_assign_rhs1 (s);
1715 176601 : tree op1 = gimple_assign_rhs2 (s);
1716 176601 : tree op2 = gimple_assign_rhs3 (s);
1717 :
1718 176601 : tree type = gimple_range_type (s);
1719 176601 : if (!type)
1720 : return false;
1721 :
1722 176601 : value_range range1 (TREE_TYPE (op1));
1723 176601 : value_range range2 (TREE_TYPE (op2));
1724 176601 : value_range cond_range (TREE_TYPE (cond));
1725 176601 : gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1726 176601 : gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1727 176601 : src.get_operand (cond_range, cond);
1728 176601 : src.get_operand (range1, op1);
1729 176601 : src.get_operand (range2, op2);
1730 :
1731 : // Try to see if there is a dependence between the COND and either operand
1732 176601 : if (condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1733 29063 : if (dump_file && (dump_flags & TDF_DETAILS))
1734 : {
1735 691 : fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1736 691 : range1.dump(dump_file);
1737 691 : fprintf (dump_file, " and Range op2: ");
1738 691 : range2.dump(dump_file);
1739 691 : fprintf (dump_file, "\n");
1740 : }
1741 :
1742 : // If the condition is known, choose the appropriate expression.
1743 176601 : if (cond_range.singleton_p ())
1744 : {
1745 : // False, pick second operand.
1746 2566 : if (cond_range.zero_p ())
1747 1218 : r = range2;
1748 : else
1749 1348 : r = range1;
1750 : }
1751 : else
1752 : {
1753 174035 : r = range1;
1754 174035 : r.union_ (range2);
1755 : }
1756 176601 : gcc_checking_assert (r.undefined_p ()
1757 : || range_compatible_p (r.type (), type));
1758 176601 : return true;
1759 176601 : }
1760 :
1761 : // If SCEV has any information about phi node NAME, return it as a range in R.
1762 :
1763 : void
1764 7420684 : fold_using_range::range_of_ssa_name_with_loop_info (vrange &r, tree name,
1765 : class loop *l, gphi *phi,
1766 : fur_source &src)
1767 : {
1768 7420684 : static bool in_scev_call = false;
1769 7420684 : gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1770 : // Avoid SCEV callbacks causing infinite recursion.
1771 7420684 : if (in_scev_call)
1772 446156 : r.set_varying (TREE_TYPE (name));
1773 : // SCEV currently invokes get_range_query () for values. If the query
1774 : // being passed in is not the same SCEV will use, do not invoke SCEV.
1775 : // This can be remove if/when SCEV uses a passed in range-query.
1776 13949056 : else if (src.query () != get_range_query (cfun))
1777 : {
1778 767064 : r.set_varying (TREE_TYPE (name));
1779 : // Report the msmatch if SRC is not the global query. The cache
1780 : // uses a global query and would provide numerous false positives.
1781 46 : if (dump_file && (dump_flags & TDF_DETAILS)
1782 767086 : && src.query () != get_global_range_query ())
1783 22 : fprintf (dump_file,
1784 : "fold_using-range:: SCEV not invoked due to mismatched queries\n");
1785 : }
1786 : else
1787 : {
1788 6207464 : in_scev_call = true;
1789 6207464 : if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1790 880 : r.set_varying (TREE_TYPE (name));
1791 6207464 : in_scev_call = false;
1792 : }
1793 7420684 : }
1794 :
1795 : // -----------------------------------------------------------------------
1796 :
1797 : // Check if an && or || expression can be folded based on relations. ie
1798 : // c_2 = a_6 > b_7
1799 : // c_3 = a_6 < b_7
1800 : // c_4 = c_2 && c_3
1801 : // c_2 and c_3 can never be true at the same time,
1802 : // Therefore c_4 can always resolve to false based purely on the relations.
1803 :
1804 : void
1805 142316836 : fold_using_range::relation_fold_and_or (irange& lhs_range, gimple *s,
1806 : fur_source &src, vrange &op1,
1807 : vrange &op2)
1808 : {
1809 : // No queries or already folded.
1810 142316836 : if (!src.gori () || lhs_range.singleton_p ())
1811 : return;
1812 :
1813 : // Only care about AND and OR expressions.
1814 94184742 : enum tree_code code = gimple_expr_code (s);
1815 94184742 : bool is_and = false;
1816 94184742 : if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1817 : is_and = true;
1818 90332969 : else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1819 : return;
1820 :
1821 5420887 : gimple_range_op_handler handler (s);
1822 5420887 : tree lhs = handler.lhs ();
1823 5420887 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1824 5420887 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1825 :
1826 : // Deal with || and && only when there is a full set of symbolics.
1827 5420875 : if (!lhs || !ssa1 || !ssa2
1828 2952824 : || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1829 1973414 : || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1830 7392900 : || (TREE_CODE (TREE_TYPE (ssa2)) != BOOLEAN_TYPE))
1831 : return;
1832 :
1833 : // Now we know its a boolean AND or OR expression with boolean operands.
1834 : // Ideally we search dependencies for common names, and see what pops out.
1835 : // until then, simply try to resolve direct dependencies.
1836 :
1837 1969204 : gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1838 1969204 : gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1839 :
1840 1969204 : gimple_range_op_handler handler1 (ssa1_stmt);
1841 1969204 : gimple_range_op_handler handler2 (ssa2_stmt);
1842 :
1843 : // If either handler is not present, no relation can be found.
1844 1969204 : if (!handler1 || !handler2)
1845 : return;
1846 :
1847 : // Both stmts will need to have 2 ssa names in the stmt.
1848 1837101 : tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1849 1837101 : tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1850 1837101 : tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1851 1837101 : tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1852 :
1853 1837101 : if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1854 : return;
1855 :
1856 172616 : if (HONOR_NANS (TREE_TYPE (ssa1_dep1)))
1857 : return;
1858 :
1859 : // Make sure they are the same dependencies, and detect the order of the
1860 : // relationship.
1861 156828 : bool reverse_op2 = true;
1862 156828 : if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1863 : reverse_op2 = false;
1864 156760 : else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1865 : return;
1866 :
1867 73 : int_range<2> bool_one = range_true ();
1868 73 : relation_kind relation1 = handler1.op1_op2_relation (bool_one, op1, op2);
1869 73 : relation_kind relation2 = handler2.op1_op2_relation (bool_one, op1, op2);
1870 73 : if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1871 : return;
1872 :
1873 37 : if (reverse_op2)
1874 5 : relation2 = relation_swap (relation2);
1875 :
1876 : // x && y is false if the relation intersection of the true cases is NULL.
1877 37 : if (is_and && relation_intersect (relation1, relation2) == VREL_UNDEFINED)
1878 0 : lhs_range = range_false (boolean_type_node);
1879 : // x || y is true if the union of the true cases is NO-RELATION..
1880 : // ie, one or the other being true covers the full range of possibilities.
1881 37 : else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1882 0 : lhs_range = bool_one;
1883 : else
1884 : return;
1885 :
1886 0 : range_cast (lhs_range, TREE_TYPE (lhs));
1887 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1888 : {
1889 0 : fprintf (dump_file, " Relation adjustment: ");
1890 0 : print_generic_expr (dump_file, ssa1, TDF_SLIM);
1891 0 : fprintf (dump_file, " and ");
1892 0 : print_generic_expr (dump_file, ssa2, TDF_SLIM);
1893 0 : fprintf (dump_file, " combine to produce ");
1894 0 : lhs_range.dump (dump_file);
1895 0 : fputc ('\n', dump_file);
1896 : }
1897 :
1898 : return;
1899 73 : }
1900 :
1901 : // Register any outgoing edge relations from a conditional branch.
1902 :
1903 : void
1904 74078285 : fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1905 : edge e0, edge e1)
1906 : {
1907 74078285 : int_range<2> e0_range, e1_range;
1908 74078285 : tree name;
1909 74078285 : basic_block bb = gimple_bb (s);
1910 :
1911 74078285 : gimple_range_op_handler handler (s);
1912 74078285 : if (!handler)
1913 : return;
1914 :
1915 74066957 : if (e0)
1916 : {
1917 : // If this edge is never taken, ignore it.
1918 61893985 : gcond_edge_range (e0_range, e0);
1919 61893985 : e0_range.intersect (lhs_range);
1920 61893985 : if (e0_range.undefined_p ())
1921 26815783 : e0 = NULL;
1922 : }
1923 :
1924 74066957 : if (e1)
1925 : {
1926 : // If this edge is never taken, ignore it.
1927 53979475 : gcond_edge_range (e1_range, e1);
1928 53979475 : e1_range.intersect (lhs_range);
1929 53979475 : if (e1_range.undefined_p ())
1930 31477702 : e1 = NULL;
1931 : }
1932 :
1933 74066957 : if (!e0 && !e1)
1934 : return;
1935 :
1936 : // First, register the gcond itself. This will catch statements like
1937 : // if (a_2 < b_5)
1938 70697543 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1939 70697543 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1940 70697543 : if (ssa1 && ssa2)
1941 : {
1942 22331372 : value_range r1,r2;
1943 22331372 : r1.set_varying (TREE_TYPE (ssa1));
1944 22331372 : r2.set_varying (TREE_TYPE (ssa2));
1945 22331372 : if (e0)
1946 : {
1947 15373598 : relation_kind relation = handler.op1_op2_relation (e0_range, r1, r2);
1948 15373598 : if (relation != VREL_VARYING)
1949 15301956 : register_relation (e0, relation, ssa1, ssa2);
1950 : }
1951 22331372 : if (e1)
1952 : {
1953 13068305 : relation_kind relation = handler.op1_op2_relation (e1_range, r1, r2);
1954 13068305 : if (relation != VREL_VARYING)
1955 13010310 : register_relation (e1, relation, ssa1, ssa2);
1956 : }
1957 22331372 : }
1958 :
1959 : // Outgoing relations of GORI exports require a gori engine.
1960 127078241 : if (!gori_ssa ())
1961 : return;
1962 :
1963 : // Now look for other relations in the exports. This will find stmts
1964 : // leading to the condition such as:
1965 : // c_2 = a_4 < b_7
1966 : // if (c_2)
1967 : // as well as relations implied by a known result, such as:
1968 : // c_2 = a_4 - b_7
1969 : // if (c_2 == 0)
1970 180924999 : FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb, name)
1971 : {
1972 124544319 : if (!INTEGRAL_TYPE_P (TREE_TYPE (name)))
1973 80558138 : continue;
1974 99349037 : gimple *stmt = SSA_NAME_DEF_STMT (name);
1975 99349037 : gimple_range_op_handler handler (stmt);
1976 99349037 : if (!handler)
1977 55362856 : continue;
1978 43986181 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1979 43986181 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1980 43986181 : value_range r (TREE_TYPE (name));
1981 43986181 : if (ssa1 && ssa2)
1982 : {
1983 6954371 : if (e0 && gori ()->edge_range_p (r, e0, name, *m_query)
1984 4142068 : && !r.undefined_p ()
1985 15539633 : && !r.varying_p ())
1986 : {
1987 4090511 : value_range r1,r2;
1988 4090511 : r1.set_varying (TREE_TYPE (ssa1));
1989 4090511 : r2.set_varying (TREE_TYPE (ssa2));
1990 8181022 : if (!gori ()->edge_range_p (r1, e0, ssa1, *m_query)
1991 4090511 : || r1.undefined_p ())
1992 2510681 : r1.set_varying (TREE_TYPE (ssa1));
1993 8181022 : if (!gori ()->edge_range_p (r2, e0, ssa2, *m_query)
1994 4090511 : || r2.undefined_p ())
1995 2493708 : r2.set_varying (TREE_TYPE (ssa2));
1996 4090511 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1997 4090511 : if (relation != VREL_VARYING)
1998 1424848 : register_relation (e0, relation, ssa1, ssa2);
1999 4090511 : }
2000 6758052 : if (e1 && gori ()->edge_range_p (r, e1, name, *m_query)
2001 3411093 : && !r.undefined_p ()
2002 14815093 : && !r.varying_p ())
2003 : {
2004 3338424 : value_range r1,r2;
2005 3338424 : r1.set_varying (TREE_TYPE (ssa1));
2006 3338424 : r2.set_varying (TREE_TYPE (ssa2));
2007 6676848 : if (!gori ()->edge_range_p (r1, e1, ssa1, *m_query)
2008 3338424 : || r1.undefined_p ())
2009 2448134 : r1.set_varying (TREE_TYPE (ssa1));
2010 6676848 : if (!gori ()->edge_range_p (r2, e1, ssa2, *m_query)
2011 3338424 : || r2.undefined_p ())
2012 2522322 : r2.set_varying (TREE_TYPE (ssa2));
2013 3338424 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
2014 3338424 : if (relation != VREL_VARYING)
2015 1050049 : register_relation (e1, relation, ssa1, ssa2);
2016 3338424 : }
2017 : }
2018 43986181 : }
2019 74078285 : }
|