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