Branch data Line data Source code
1 : : /* Code for GIMPLE range related routines.
2 : : Copyright (C) 2019-2025 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 : 495732448 : fur_source::fur_source (range_query *q)
58 : : {
59 : 495732448 : if (q)
60 : 495731467 : m_query = q;
61 : : else
62 : 1962 : m_query = get_range_query (cfun);
63 : 495732448 : m_depend_p = false;
64 : 495732448 : }
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 : 5300205 : fur_source::query_relation (tree op1 ATTRIBUTE_UNUSED,
87 : : tree op2 ATTRIBUTE_UNUSED)
88 : : {
89 : 5300205 : return VREL_VARYING;
90 : : }
91 : :
92 : : // Default registers nothing and returns false meaning nothing changed.
93 : :
94 : : bool
95 : 25680271 : 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 : 25680271 : return false;
101 : : }
102 : :
103 : : // Default registers nothing and returns false meaning nothing changed.
104 : :
105 : : bool
106 : 6450047 : 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 : 6450047 : return false;
112 : : }
113 : :
114 : : // Get the value of EXPR on edge m_edge.
115 : :
116 : : bool
117 : 69370791 : fur_edge::get_operand (vrange &r, tree expr)
118 : : {
119 : 69370791 : 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 : 429881390 : fur_stmt::fur_stmt (gimple *s, range_query *q) : fur_source (q)
136 : : {
137 : 429881390 : m_stmt = s;
138 : 429881390 : }
139 : :
140 : : // Retrieve range of EXPR as it occurs as a use on stmt M_STMT.
141 : :
142 : : bool
143 : 563195978 : fur_stmt::get_operand (vrange &r, tree expr)
144 : : {
145 : 563195978 : 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 : 57844940 : fur_stmt::get_phi_operand (vrange &r, tree expr, edge e)
153 : : {
154 : : // Pick up the range of expr from edge E.
155 : 57844940 : fur_edge e_src (e, m_query);
156 : 57844940 : return e_src.get_operand (r, expr);
157 : : }
158 : :
159 : : // Return relation based from m_stmt.
160 : :
161 : : relation_kind
162 : 109255861 : fur_stmt::query_relation (tree op1, tree op2)
163 : : {
164 : 109255861 : 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 : 240628479 : fur_depend::fur_depend (gimple *s, range_query *q, ranger_cache *c)
170 : 240628479 : : fur_stmt (s, q), m_cache (c)
171 : : {
172 : 240628479 : m_depend_p = true;
173 : 240628479 : }
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 : 33570911 : fur_depend::register_relation (gimple *s, relation_kind k, tree op1, tree op2)
180 : : {
181 : 33570911 : 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 : 24741131 : if (m_cache)
187 : : {
188 : 24741099 : m_cache->update_consumers (op1);
189 : 24741099 : 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 : 6623430 : fur_depend::register_relation (edge e, relation_kind k, tree op1, tree op2)
199 : : {
200 : 6623430 : 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 : 6605505 : if (m_cache)
206 : : {
207 : 6605503 : m_cache->update_consumers (op1);
208 : 6605503 : 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 : 901513 : fur_list::fur_list (vrange &r1, range_query *q) : fur_source (q)
234 : : {
235 : 901513 : m_list = m_local;
236 : 901513 : m_index = 0;
237 : 901513 : m_limit = 1;
238 : 901513 : m_local[0] = &r1;
239 : 901513 : }
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 : 1795461 : 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 : 1795461 : if (TREE_CODE (expr) != SSA_NAME || m_index >= m_limit)
269 : 893948 : return m_query->range_of_expr (r, expr);
270 : 901513 : r = *m_list[m_index++];
271 : 901513 : 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 : 901513 : fold_range (vrange &r, gimple *s, vrange &r1, range_query *q)
286 : : {
287 : 901513 : fold_using_range f;
288 : 901513 : fur_list src (r1, q);
289 : 901513 : 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 : 78495350 : fold_range (vrange &r, gimple *s, range_query *q)
318 : : {
319 : 78495350 : fold_using_range f;
320 : 78495350 : fur_stmt src (s, q);
321 : 78495350 : 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 : 7104600 : fold_range (vrange &r, gimple *s, edge on_edge, range_query *q)
328 : : {
329 : 7104600 : fold_using_range f;
330 : 7104600 : fur_edge src (on_edge, q);
331 : 7104600 : 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 : 1059168 : fur_relation::fur_relation (gimple *s, range_query *q) : fur_stmt (s, q)
423 : : {
424 : 1059168 : def_op1 = def_op2 = op1_op2 = VREL_VARYING;
425 : 1059168 : }
426 : :
427 : : // Construct a trio from what is known.
428 : :
429 : : relation_trio
430 : 1059168 : fur_relation::trio () const
431 : : {
432 : 1059168 : 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 : 1041901 : fur_relation::register_relation (gimple *stmt, relation_kind k, tree op1,
449 : : tree op2)
450 : : {
451 : 1041901 : tree lhs = gimple_get_lhs (stmt);
452 : 1041901 : tree a1 = NULL_TREE;
453 : 1041901 : tree a2 = NULL_TREE;
454 : 1041901 : 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 : 1041901 : case GIMPLE_ASSIGN:
461 : 1041901 : a1 = gimple_assign_rhs1 (stmt);
462 : 1041901 : if (gimple_num_ops (stmt) >= 3)
463 : 1041901 : 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 : 1041901 : if (op1 == lhs)
471 : : {
472 : 1041901 : if (op2 == a1)
473 : 1041901 : 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 : 1041901 : && op1_op2 == VREL_VARYING;
493 : : }
494 : :
495 : : // Return the relation trio for stmt S using query Q.
496 : :
497 : : relation_trio
498 : 1059168 : fold_relations (gimple *s, range_query *q)
499 : : {
500 : 1059168 : fold_using_range f;
501 : 1059168 : fur_relation src (s, q);
502 : 1059168 : tree lhs = gimple_range_ssa_p (gimple_get_lhs (s));
503 : 1059168 : if (lhs)
504 : : {
505 : 1059168 : value_range vr(TREE_TYPE (lhs));
506 : 1059168 : if (f.fold_stmt (vr, s, src))
507 : 1059168 : return src.trio ();
508 : 1059168 : }
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 : 2744930 : adjust_pointer_diff_expr (irange &res, const gimple *diff_stmt)
526 : : {
527 : 2744930 : tree op0 = gimple_assign_rhs1 (diff_stmt);
528 : 2744930 : tree op1 = gimple_assign_rhs2 (diff_stmt);
529 : 2744930 : tree op0_ptype = TREE_TYPE (TREE_TYPE (op0));
530 : 2744930 : tree op1_ptype = TREE_TYPE (TREE_TYPE (op1));
531 : 2744930 : gimple *call;
532 : :
533 : 2744930 : if (TREE_CODE (op0) == SSA_NAME
534 : 2712907 : && TREE_CODE (op1) == SSA_NAME
535 : 2657280 : && (call = SSA_NAME_DEF_STMT (op0))
536 : 2657280 : && is_gimple_call (call)
537 : 142044 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
538 : 112722 : && TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node)
539 : 112442 : && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node)
540 : 112442 : && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node)
541 : 112358 : && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node)
542 : 112358 : && gimple_call_builtin_p (call, BUILT_IN_MEMCHR)
543 : 112358 : && vrp_operand_equal_p (op1, gimple_call_arg (call, 0))
544 : 2831016 : && integer_zerop (gimple_call_arg (call, 1)))
545 : : {
546 : 10 : wide_int maxm1 = irange_val_max (ptrdiff_type_node) - 1;
547 : 10 : res.intersect (int_range<2> (ptrdiff_type_node,
548 : 20 : wi::zero (TYPE_PRECISION (ptrdiff_type_node)),
549 : 10 : maxm1));
550 : 10 : }
551 : 2744930 : }
552 : :
553 : : // Adjust the range for an IMAGPART_EXPR.
554 : :
555 : : static void
556 : 658713 : adjust_imagpart_expr (vrange &res, const gimple *stmt)
557 : : {
558 : 658713 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
559 : :
560 : 658713 : if (TREE_CODE (name) != SSA_NAME || !SSA_NAME_DEF_STMT (name))
561 : : return;
562 : :
563 : 533118 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
564 : 533118 : if (is_gimple_call (def_stmt) && gimple_call_internal_p (def_stmt))
565 : : {
566 : 404437 : switch (gimple_call_internal_fn (def_stmt))
567 : : {
568 : 386804 : case IFN_ADD_OVERFLOW:
569 : 386804 : case IFN_SUB_OVERFLOW:
570 : 386804 : case IFN_MUL_OVERFLOW:
571 : 386804 : case IFN_UADDC:
572 : 386804 : case IFN_USUBC:
573 : 386804 : case IFN_ATOMIC_COMPARE_EXCHANGE:
574 : 386804 : {
575 : 386804 : int_range<2> r;
576 : 386804 : r.set_varying (boolean_type_node);
577 : 386804 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
578 : 386804 : range_cast (r, type);
579 : 386804 : res.intersect (r);
580 : 386804 : }
581 : 404437 : default:
582 : 404437 : break;
583 : : }
584 : 404437 : return;
585 : : }
586 : 128681 : if (is_gimple_assign (def_stmt)
587 : 128681 : && 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 : 663387 : adjust_realpart_expr (vrange &res, const gimple *stmt)
604 : : {
605 : 663387 : tree name = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
606 : :
607 : 663387 : if (TREE_CODE (name) != SSA_NAME)
608 : : return;
609 : :
610 : 530419 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
611 : 530419 : if (!SSA_NAME_DEF_STMT (name))
612 : : return;
613 : :
614 : 530419 : if (is_gimple_assign (def_stmt)
615 : 530419 : && 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 : 189986784 : gimple_range_adjustment (vrange &res, const gimple *stmt)
634 : : {
635 : 189986784 : switch (gimple_expr_code (stmt))
636 : : {
637 : 2744930 : case POINTER_DIFF_EXPR:
638 : 2744930 : adjust_pointer_diff_expr (as_a <irange> (res), stmt);
639 : 2744930 : return;
640 : :
641 : 658713 : case IMAGPART_EXPR:
642 : 658713 : adjust_imagpart_expr (res, stmt);
643 : 658713 : return;
644 : :
645 : 663387 : case REALPART_EXPR:
646 : 663387 : adjust_realpart_expr (res, stmt);
647 : 663387 : 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 : 304547399 : fold_using_range::fold_stmt (vrange &r, gimple *s, fur_source &src, tree name)
661 : : {
662 : 304547399 : bool res = false;
663 : : // If name and S are specified, make sure it is an LHS of S.
664 : 304547399 : gcc_checking_assert (!name || !gimple_get_lhs (s) ||
665 : : name == gimple_get_lhs (s));
666 : :
667 : 163853618 : if (!name)
668 : 163853618 : name = gimple_get_lhs (s);
669 : :
670 : : // Process addresses and loads from static constructors.
671 : 304547399 : if (gimple_code (s) == GIMPLE_ASSIGN)
672 : : {
673 : 194631334 : if (gimple_assign_rhs_code (s) == ADDR_EXPR)
674 : 4132583 : return range_of_address (as_a <prange> (r), s, src);
675 : 190498751 : if (range_from_readonly_var (r, s))
676 : : return true;
677 : : }
678 : :
679 : 300309904 : gimple_range_op_handler handler (s);
680 : 300309904 : if (handler)
681 : 189987884 : res = range_of_range_op (r, handler, src);
682 : 110322020 : else if (is_a<gphi *>(s))
683 : 28027900 : res = range_of_phi (r, as_a<gphi *> (s), src);
684 : 82294120 : else if (is_a<gcall *>(s))
685 : 13121969 : res = range_of_call (r, as_a<gcall *> (s), src);
686 : 69172151 : else if (is_a<gassign *> (s) && gimple_assign_rhs_code (s) == COND_EXPR)
687 : 154887 : 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 : 231292640 : bool so_p;
693 : 231292640 : if (res && r.varying_p () && INTEGRAL_TYPE_P (r.type ())
694 : 352959446 : && gimple_stmt_nonnegative_warnv_p (s, &so_p))
695 : 41053834 : r.set_nonnegative (r.type ());
696 : :
697 : 300309904 : if (!res)
698 : : {
699 : : // If no name specified or range is unsupported, bail.
700 : 69017264 : if (!name || !gimple_range_ssa_p (name))
701 : 51487 : return false;
702 : : // We don't understand the stmt, so return the global range.
703 : 68965777 : gimple_range_global (r, name);
704 : 68965777 : return true;
705 : : }
706 : :
707 : 231292640 : 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 : 231242038 : if (name && TREE_TYPE (name) != r.type ())
713 : : {
714 : 3423633 : gcc_checking_assert (range_compatible_p (r.type (), TREE_TYPE (name)));
715 : 3423633 : 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 : 189987884 : fold_using_range::range_of_range_op (vrange &r,
725 : : gimple_range_op_handler &handler,
726 : : fur_source &src)
727 : : {
728 : 189987884 : gcc_checking_assert (handler);
729 : 189987884 : gimple *s = handler.stmt ();
730 : 189987884 : tree type = gimple_range_type (s);
731 : 189987884 : if (!type)
732 : : return false;
733 : :
734 : 189987884 : tree lhs = handler.lhs ();
735 : 189987884 : tree op1 = handler.operand1 ();
736 : 189987884 : tree op2 = handler.operand2 ();
737 : :
738 : : // Certain types of builtin functions may have no arguments.
739 : 189987884 : 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 : 189986784 : value_range range1 (TREE_TYPE (op1));
748 : 189986784 : value_range range2 (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
749 : :
750 : 189986784 : if (src.get_operand (range1, op1))
751 : : {
752 : 189986784 : if (!op2)
753 : : {
754 : : // Fold range, and register any dependency if available.
755 : 37636004 : value_range r2 (type);
756 : 37636004 : r2.set_varying (type);
757 : 37636004 : if (!handler.fold_range (r, type, range1, r2))
758 : 251628 : r.set_varying (type);
759 : 37636004 : if (lhs && gimple_range_ssa_p (op1))
760 : : {
761 : 55477423 : if (src.gori_ssa ())
762 : 20698007 : src.gori_ssa ()->register_dependency (lhs, op1);
763 : 34779383 : relation_kind rel;
764 : 34779383 : rel = handler.lhs_op1_relation (r, range1, range1);
765 : 34779383 : if (rel != VREL_VARYING)
766 : 25533949 : src.register_relation (s, rel, lhs, op1);
767 : : }
768 : 37636004 : }
769 : 152350780 : else if (src.get_operand (range2, op2))
770 : : {
771 : 152350780 : relation_kind rel = src.query_relation (op1, op2);
772 : 152350780 : 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 : 152350780 : if (!handler.fold_range (r, type, range1, range2,
782 : : relation_trio::op1_op2 (rel)))
783 : 0 : r.set_varying (type);
784 : 152350780 : if (irange::supports_p (type))
785 : 138558810 : relation_fold_and_or (as_a <irange> (r), s, src, range1, range2);
786 : 152350780 : if (lhs)
787 : : {
788 : 155824806 : if (src.gori_ssa ())
789 : : {
790 : 60595778 : src.gori_ssa ()->register_dependency (lhs, op1);
791 : 121191556 : src.gori_ssa ()->register_dependency (lhs, op2);
792 : : }
793 : 95228968 : if (gimple_range_ssa_p (op1))
794 : : {
795 : 92575000 : relation_kind rel2 = handler.lhs_op1_relation (r, range1,
796 : 92575000 : range2, rel);
797 : 92575000 : if (rel2 != VREL_VARYING)
798 : 40545703 : src.register_relation (s, rel2, lhs, op1);
799 : : }
800 : 95228968 : if (gimple_range_ssa_p (op2))
801 : : {
802 : 37755650 : relation_kind rel2 = handler.lhs_op2_relation (r, range1,
803 : 37755650 : range2, rel);
804 : 37755650 : if (rel2 != VREL_VARYING)
805 : 2328906 : 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 : 57121812 : else if (is_a<gcond *> (s) && gimple_bb (s))
811 : : {
812 : 48413578 : basic_block bb = gimple_bb (s);
813 : 48413578 : 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 : 48413578 : edge e1 = single_succ_p (bb) ? NULL : EDGE_SUCC (bb, 1);
817 : :
818 : 48413578 : gcc_checking_assert (e1 || currently_expanding_to_rtl);
819 : 48413578 : if (!single_pred_p (e0->dest))
820 : 12220610 : e0 = NULL;
821 : 48413578 : if (e1 && !single_pred_p (e1->dest))
822 : : e1 = NULL;
823 : 48413578 : 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 : 189986784 : gimple_range_adjustment (r, s);
834 : 189986784 : return true;
835 : 189986784 : }
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 : 4132583 : fold_using_range::range_of_address (prange &r, gimple *stmt, fur_source &src)
843 : : {
844 : 4132583 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
845 : 4132583 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == ADDR_EXPR);
846 : :
847 : 4132583 : bool strict_overflow_p;
848 : 4132583 : tree expr = gimple_assign_rhs1 (stmt);
849 : 4132583 : poly_int64 bitsize, bitpos;
850 : 4132583 : tree offset;
851 : 4132583 : machine_mode mode;
852 : 4132583 : int unsignedp, reversep, volatilep;
853 : 4132583 : tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize,
854 : : &bitpos, &offset, &mode, &unsignedp,
855 : : &reversep, &volatilep);
856 : :
857 : :
858 : 4132583 : if (base != NULL_TREE
859 : 4132583 : && TREE_CODE (base) == MEM_REF
860 : 8117061 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
861 : : {
862 : 3984435 : tree ssa = TREE_OPERAND (base, 0);
863 : 3984435 : tree lhs = gimple_get_lhs (stmt);
864 : 6440468 : if (lhs && gimple_range_ssa_p (ssa) && src.gori_ssa ())
865 : 2456033 : src.gori_ssa ()->register_dependency (lhs, ssa);
866 : 3984435 : src.get_operand (r, ssa);
867 : 3984435 : range_cast (r, TREE_TYPE (gimple_assign_rhs1 (stmt)));
868 : :
869 : 3984435 : poly_offset_int off = 0;
870 : 3984435 : bool off_cst = false;
871 : 3984435 : if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST)
872 : : {
873 : 3906505 : off = mem_ref_offset (base);
874 : 3906505 : if (offset)
875 : 48 : off += poly_offset_int::from (wi::to_poly_wide (offset),
876 : 48 : SIGNED);
877 : 3906505 : off <<= LOG2_BITS_PER_UNIT;
878 : 3906505 : off += bitpos;
879 : : off_cst = true;
880 : : }
881 : : /* If &X->a is equal to X, the range of X is the result. */
882 : 3906505 : if (off_cst && known_eq (off, 0))
883 : 1459082 : return true;
884 : 2525353 : else if (flag_delete_null_pointer_checks
885 : 2525353 : && !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 : 2523971 : if (r.undefined_p ()
890 : 5047942 : || !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 : 1843721 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
896 : 1843721 : 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 : 681632 : if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr))
903 : 681425 : && off_cst
904 : 625159 : && known_ne (off, 0)
905 : 1306791 : && (flag_delete_null_pointer_checks || known_gt (off, 0)))
906 : : {
907 : 625159 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
908 : 625159 : return true;
909 : : }
910 : 56473 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
911 : 56473 : return true;
912 : : }
913 : :
914 : : // Handle "= &a".
915 : 148148 : if (tree_single_nonzero_warnv_p (expr, &strict_overflow_p))
916 : : {
917 : 147206 : r.set_nonzero (TREE_TYPE (gimple_assign_rhs1 (stmt)));
918 : 147206 : return true;
919 : : }
920 : :
921 : : // Otherwise return varying.
922 : 942 : r.set_varying (TREE_TYPE (gimple_assign_rhs1 (stmt)));
923 : 942 : 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 : 1314 : range_from_missing_constructor_part (vrange &r, tree type)
931 : : {
932 : 1314 : 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 : 828392 : range_from_readonly_load (vrange &r, tree type, tree cst,
952 : : const vec <tree> &comps, unsigned i)
953 : : {
954 : 840757 : if (i == 0)
955 : : {
956 : 718907 : if (!useless_type_conversion_p (type, TREE_TYPE (cst)))
957 : : return false;
958 : :
959 : 718907 : if (POINTER_TYPE_P (type))
960 : : {
961 : 167393 : bool strict_overflow_p;
962 : 167393 : return tree_single_nonzero_warnv_p (cst, &strict_overflow_p);
963 : : }
964 : :
965 : 551514 : if (TREE_CODE (cst) != INTEGER_CST)
966 : : return false;
967 : :
968 : 551443 : wide_int wi_cst = wi::to_wide (cst);
969 : 551443 : r.union_ (int_range<1> (type, wi_cst, wi_cst));
970 : 551443 : return true;
971 : 551443 : }
972 : : /* TODO: Perhaps handle RAW_DATA_CST too. */
973 : 121850 : if (TREE_CODE (cst) != CONSTRUCTOR)
974 : : return false;
975 : :
976 : 121103 : i--;
977 : 121103 : tree expr = comps[i];
978 : 121103 : unsigned ix;
979 : 121103 : tree index, val;
980 : :
981 : 121103 : if (TREE_CODE (expr) == COMPONENT_REF)
982 : : {
983 : 12414 : tree ref_fld = TREE_OPERAND (expr, 1);
984 : 19545 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (cst), ix, index, val)
985 : : {
986 : 19496 : if (index != ref_fld)
987 : 7131 : 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 : 108689 : gcc_assert (TREE_CODE (expr) == ARRAY_REF);
999 : 108689 : tree op1 = TREE_OPERAND (expr, 1);
1000 : :
1001 : 108689 : 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 : 105333 : tree arr_type = TREE_TYPE (cst);
1016 : 105333 : tree domain = TYPE_DOMAIN (arr_type);
1017 : 105333 : if (!TYPE_MIN_VALUE (domain)
1018 : 105333 : || !TYPE_MAX_VALUE (domain)
1019 : 105333 : || !tree_fits_uhwi_p (TYPE_MIN_VALUE (domain))
1020 : 210666 : || !tree_fits_uhwi_p (TYPE_MAX_VALUE (domain)))
1021 : : return false;
1022 : 105258 : unsigned HOST_WIDE_INT needed_count
1023 : 105258 : = (tree_to_uhwi (TYPE_MAX_VALUE (domain))
1024 : 105258 : - tree_to_uhwi (TYPE_MIN_VALUE (domain)) + 1);
1025 : 210456 : if (CONSTRUCTOR_NELTS (cst) < needed_count)
1026 : : {
1027 : 1209 : if (!range_from_missing_constructor_part (r, type))
1028 : : return false;
1029 : : }
1030 : :
1031 : 823036 : 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 : 718922 : 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 : 190498751 : fold_using_range::range_from_readonly_var (vrange &r, gimple *stmt)
1050 : : {
1051 : 190498751 : gcc_checking_assert (gimple_code (stmt) == GIMPLE_ASSIGN);
1052 : 190498751 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1053 : : /* TODO: Add support for frange. */
1054 : 190498751 : if (!irange::supports_p (type)
1055 : 190498751 : && !prange::supports_p (type))
1056 : : return false;
1057 : :
1058 : 180262728 : unsigned HOST_WIDE_INT limit = param_vrp_cstload_limit;
1059 : 180262728 : if (!limit)
1060 : : return false;
1061 : :
1062 : 180243656 : tree t = gimple_assign_rhs1 (stmt);
1063 : 180243656 : if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
1064 : : return false;
1065 : 180243656 : limit *= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
1066 : :
1067 : 180243656 : unsigned count = 0;
1068 : 180243656 : while (TREE_CODE (t) == ARRAY_REF
1069 : 230309077 : || TREE_CODE (t) == COMPONENT_REF)
1070 : : {
1071 : 50065421 : count++;
1072 : 50065421 : t = TREE_OPERAND (t, 0);
1073 : : }
1074 : 180243656 : if (!count
1075 : 32738058 : || (TREE_CODE (t) != VAR_DECL
1076 : 32738058 : && TREE_CODE (t) != CONST_DECL))
1077 : : return false;
1078 : :
1079 : 9593769 : if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (t))
1080 : 9593769 : || 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 : 8566107 : tree ctor = ctor_for_folding (t);
1087 : 8566107 : if (!ctor
1088 : 8566100 : || TREE_CODE (ctor) != CONSTRUCTOR)
1089 : : return false;
1090 : :
1091 : 106210 : t = gimple_assign_rhs1 (stmt);
1092 : 106210 : auto_vec <tree, 4> comps;
1093 : 106210 : comps.safe_grow (count, true);
1094 : 106210 : int i = 0;
1095 : 106210 : while (TREE_CODE (t) == ARRAY_REF
1096 : 217468 : || TREE_CODE (t) == COMPONENT_REF)
1097 : : {
1098 : 111258 : comps[i] = t;
1099 : 111258 : t = TREE_OPERAND (t, 0);
1100 : 111258 : i++;
1101 : : }
1102 : :
1103 : 106210 : value_range tmp (type);
1104 : 106210 : bool res = range_from_readonly_load (tmp, type, ctor, comps, count);
1105 : 106210 : if (res)
1106 : : {
1107 : 104912 : if (POINTER_TYPE_P (type))
1108 : 26579 : r.set_nonzero (type);
1109 : : else
1110 : 78333 : r = tmp;
1111 : : }
1112 : 106210 : return res;
1113 : 106210 : }
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 : 28027900 : fold_using_range::range_of_phi (vrange &r, gphi *phi, fur_source &src)
1120 : : {
1121 : 28027900 : tree phi_def = gimple_phi_result (phi);
1122 : 28027900 : tree type = gimple_range_type (phi);
1123 : 28027900 : value_range arg_range (type);
1124 : 28027900 : value_range equiv_range (type);
1125 : 28027900 : unsigned x;
1126 : :
1127 : 28027900 : if (!type)
1128 : : return false;
1129 : :
1130 : : // Track if all executable arguments are the same.
1131 : 28027900 : tree single_arg = NULL_TREE;
1132 : 28027900 : bool seen_arg = false;
1133 : :
1134 : 28027900 : relation_oracle *oracle = &(src.query()->relation ());
1135 : : // Start with an empty range, unioning in each argument's range.
1136 : 28027900 : r.set_undefined ();
1137 : 72074575 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1138 : : {
1139 : 57877365 : tree arg = gimple_phi_arg_def (phi, x);
1140 : : // An argument that is the same as the def provides no new range.
1141 : 57877365 : if (arg == phi_def)
1142 : 32425 : continue;
1143 : :
1144 : 57844940 : edge e = gimple_phi_arg_edge (phi, x);
1145 : :
1146 : : // Get the range of the argument on its edge.
1147 : 57844940 : src.get_phi_operand (arg_range, arg, e);
1148 : :
1149 : 57844940 : 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 : 57581301 : if (oracle->query (e, arg, phi_def) == VREL_EQ)
1156 : 485481 : equiv_range.union_(arg_range);
1157 : : else
1158 : 57095820 : r.union_ (arg_range);
1159 : :
1160 : 94951773 : if (gimple_range_ssa_p (arg) && src.gori_ssa ())
1161 : 37370466 : src.gori_ssa ()->register_dependency (phi_def, arg);
1162 : : }
1163 : :
1164 : : // Track if all arguments are the same.
1165 : 57844940 : if (!seen_arg)
1166 : : {
1167 : : seen_arg = true;
1168 : : single_arg = arg;
1169 : : }
1170 : 29817040 : else if (single_arg != arg)
1171 : 28278531 : single_arg = NULL_TREE;
1172 : :
1173 : : // Once the value reaches varying, stop looking.
1174 : 57844940 : 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 : 28027900 : if (r.undefined_p () && !equiv_range.undefined_p ())
1181 : 318362 : r = equiv_range;
1182 : :
1183 : : // If the PHI boils down to a single effective argument, look at it.
1184 : 28027900 : if (single_arg)
1185 : : {
1186 : : // Symbolic arguments can be equivalences.
1187 : 2781959 : 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 : 2263083 : basic_block bb = gimple_bb (phi);
1193 : 2263083 : if (!dom_info_available_p (CDI_DOMINATORS))
1194 : : single_arg = NULL;
1195 : : else
1196 : 4911730 : for (x = 0; x < gimple_phi_num_args (phi); x++)
1197 : 2653828 : if (gimple_phi_arg_def (phi, x) == single_arg
1198 : 5294382 : && dominated_by_p (CDI_DOMINATORS,
1199 : 2640554 : gimple_phi_arg_edge (phi, x)->src,
1200 : : bb))
1201 : : {
1202 : : single_arg = NULL;
1203 : : break;
1204 : : }
1205 : 2262181 : if (single_arg)
1206 : 2257902 : src.register_relation (phi, VREL_EQ, phi_def, single_arg);
1207 : : }
1208 : 518876 : else if (src.get_operand (arg_range, single_arg)
1209 : 1037752 : && 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 : 499015 : r = arg_range;
1215 : 499015 : 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 : 27528885 : if (scev_initialized_p ()
1225 : 27528885 : && !POINTER_TYPE_P (TREE_TYPE (phi_def)))
1226 : : {
1227 : 11638250 : class loop *l = loop_containing_stmt (phi);
1228 : 11638250 : if (l && loop_outer (l))
1229 : : {
1230 : 8921960 : value_range loop_range (type);
1231 : 8921960 : range_of_ssa_name_with_loop_info (loop_range, phi_def, l, phi, src);
1232 : 8921960 : if (!loop_range.varying_p ())
1233 : : {
1234 : 2444905 : if (dump_file && (dump_flags & TDF_DETAILS))
1235 : : {
1236 : 13526 : fprintf (dump_file, "Loops range found for ");
1237 : 13526 : print_generic_expr (dump_file, phi_def, TDF_SLIM);
1238 : 13526 : fprintf (dump_file, ": ");
1239 : 13526 : loop_range.dump (dump_file);
1240 : 13526 : fprintf (dump_file, " and calculated range :");
1241 : 13526 : r.dump (dump_file);
1242 : 13526 : fprintf (dump_file, "\n");
1243 : : }
1244 : 2444905 : r.intersect (loop_range);
1245 : : }
1246 : 8921960 : }
1247 : : }
1248 : :
1249 : : return true;
1250 : 28027900 : }
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 : 13121969 : fold_using_range::range_of_call (vrange &r, gcall *call, fur_source &)
1257 : : {
1258 : 13121969 : tree type = gimple_range_type (call);
1259 : 13121969 : if (!type)
1260 : : return false;
1261 : :
1262 : 13121969 : tree lhs = gimple_call_lhs (call);
1263 : 13121969 : bool strict_overflow_p;
1264 : :
1265 : 13121969 : if (gimple_stmt_nonnegative_warnv_p (call, &strict_overflow_p))
1266 : 39917 : r.set_nonnegative (type);
1267 : 13082052 : else if (gimple_call_nonnull_result_p (call)
1268 : 13082052 : || gimple_call_nonnull_arg (call))
1269 : 477217 : r.set_nonzero (type);
1270 : : else
1271 : 12604835 : r.set_varying (type);
1272 : :
1273 : 13121969 : tree callee = gimple_call_fndecl (call);
1274 : 13121969 : if (callee
1275 : 13121969 : && useless_type_conversion_p (TREE_TYPE (TREE_TYPE (callee)), type))
1276 : : {
1277 : 11912865 : value_range val;
1278 : 11912865 : if (ipa_return_value_range (val, callee))
1279 : : {
1280 : 598612 : r.intersect (val);
1281 : 598612 : if (dump_file && (dump_flags & TDF_DETAILS))
1282 : : {
1283 : 27 : fprintf (dump_file, "Using return value range of ");
1284 : 27 : print_generic_expr (dump_file, callee, TDF_SLIM);
1285 : 27 : fprintf (dump_file, ": ");
1286 : 27 : val.dump (dump_file);
1287 : 27 : fprintf (dump_file, "\n");
1288 : : }
1289 : : }
1290 : 11912865 : }
1291 : :
1292 : : // If there is an LHS, intersect that with what is known.
1293 : 13121969 : if (gimple_range_ssa_p (lhs))
1294 : : {
1295 : 13121969 : value_range def (TREE_TYPE (lhs));
1296 : 13121969 : gimple_range_global (def, lhs);
1297 : 13121969 : r.intersect (def);
1298 : 13121969 : }
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 : 154887 : fold_using_range::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
1311 : : tree op1, tree op2, fur_source &src)
1312 : : {
1313 : 154887 : if (!src.gori () || !src.gori_ssa ())
1314 : : return false;
1315 : :
1316 : 107921 : tree ssa1 = gimple_range_ssa_p (op1);
1317 : 107921 : tree ssa2 = gimple_range_ssa_p (op2);
1318 : 107921 : if (!ssa1 && !ssa2)
1319 : : return false;
1320 : 98327 : if (TREE_CODE (cond) != SSA_NAME)
1321 : : return false;
1322 : 228699 : gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1323 : 98198 : if (!cond_def
1324 : 98198 : || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
1325 : : return false;
1326 : 93736 : tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1327 : 93736 : if (!value_range::supports_type_p (type)
1328 : 187468 : || !range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1329 : : return false;
1330 : 93732 : range_op_handler hand (gimple_assign_rhs_code (cond_def));
1331 : 93732 : if (!hand)
1332 : : return false;
1333 : :
1334 : 93732 : tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
1335 : 187464 : 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 : 93732 : if ((!c1 && !c2) || (c1 && c2))
1339 : : return false;
1340 : :
1341 : : // Pick up the current values of each part of the condition.
1342 : 24386 : tree rhs1 = gimple_assign_rhs1 (cond_def);
1343 : 24386 : tree rhs2 = gimple_assign_rhs2 (cond_def);
1344 : 24386 : value_range cl (TREE_TYPE (rhs1));
1345 : 24386 : value_range cr (TREE_TYPE (rhs2));
1346 : 24386 : src.get_operand (cl, rhs1);
1347 : 24386 : src.get_operand (cr, rhs2);
1348 : :
1349 : 24386 : tree cond_name = c1 ? c1 : c2;
1350 : 24386 : 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 : 24386 : value_range cond_true (type), cond_false (type);
1355 : 24386 : if (c1)
1356 : : {
1357 : 24386 : if (!hand.op1_range (cond_false, type, range_false (), cr))
1358 : : return false;
1359 : 24386 : if (!hand.op1_range (cond_true, type, range_true (), cr))
1360 : : return false;
1361 : 24386 : cond_false.intersect (cl);
1362 : 24386 : 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 : 45360 : if (ssa1 && src.gori_ssa()->in_chain_p (ssa1, cond_name))
1376 : : {
1377 : 858 : value_range tmp1 (TREE_TYPE (ssa1));
1378 : 1716 : if (src.gori ()->compute_operand_range (tmp1, def_stmt, cond_true,
1379 : : ssa1, src))
1380 : 539 : r1.intersect (tmp1);
1381 : 858 : }
1382 : 39578 : if (ssa2 && src.gori_ssa ()->in_chain_p (ssa2, cond_name))
1383 : : {
1384 : 248 : value_range tmp2 (TREE_TYPE (ssa2));
1385 : 496 : if (src.gori ()->compute_operand_range (tmp2, def_stmt, cond_false,
1386 : : ssa2, src))
1387 : 198 : r2.intersect (tmp2);
1388 : 248 : }
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 : 24386 : if (ssa1 && cond_name == ssa1)
1397 : 1927 : r1 = cond_true;
1398 : 22459 : else if (ssa2 && cond_name == ssa2)
1399 : 2812 : r2 = cond_false;
1400 : : return true;
1401 : 24386 : }
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 : 154887 : fold_using_range::range_of_cond_expr (vrange &r, gassign *s, fur_source &src)
1408 : : {
1409 : 154887 : tree cond = gimple_assign_rhs1 (s);
1410 : 154887 : tree op1 = gimple_assign_rhs2 (s);
1411 : 154887 : tree op2 = gimple_assign_rhs3 (s);
1412 : :
1413 : 154887 : tree type = gimple_range_type (s);
1414 : 154887 : if (!type)
1415 : : return false;
1416 : :
1417 : 154887 : value_range range1 (TREE_TYPE (op1));
1418 : 154887 : value_range range2 (TREE_TYPE (op2));
1419 : 154887 : value_range cond_range (TREE_TYPE (cond));
1420 : 154887 : gcc_checking_assert (gimple_assign_rhs_code (s) == COND_EXPR);
1421 : 154887 : gcc_checking_assert (range_compatible_p (TREE_TYPE (op1), TREE_TYPE (op2)));
1422 : 154887 : src.get_operand (cond_range, cond);
1423 : 154887 : src.get_operand (range1, op1);
1424 : 154887 : src.get_operand (range2, op2);
1425 : :
1426 : : // Try to see if there is a dependence between the COND and either operand
1427 : 154887 : if (condexpr_adjust (range1, range2, s, cond, op1, op2, src))
1428 : 24386 : if (dump_file && (dump_flags & TDF_DETAILS))
1429 : : {
1430 : 554 : fprintf (dump_file, "Possible COND_EXPR adjustment. Range op1 : ");
1431 : 554 : range1.dump(dump_file);
1432 : 554 : fprintf (dump_file, " and Range op2: ");
1433 : 554 : range2.dump(dump_file);
1434 : 554 : fprintf (dump_file, "\n");
1435 : : }
1436 : :
1437 : : // If the condition is known, choose the appropriate expression.
1438 : 154887 : if (cond_range.singleton_p ())
1439 : : {
1440 : : // False, pick second operand.
1441 : 2390 : if (cond_range.zero_p ())
1442 : 1208 : r = range2;
1443 : : else
1444 : 1182 : r = range1;
1445 : : }
1446 : : else
1447 : : {
1448 : 152497 : r = range1;
1449 : 152497 : r.union_ (range2);
1450 : : }
1451 : 154887 : gcc_checking_assert (r.undefined_p ()
1452 : : || range_compatible_p (r.type (), type));
1453 : 154887 : return true;
1454 : 154887 : }
1455 : :
1456 : : // If SCEV has any information about phi node NAME, return it as a range in R.
1457 : :
1458 : : void
1459 : 8921960 : 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 : 8921960 : static bool in_scev_call = false;
1464 : 8921960 : gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
1465 : : // Avoid SCEV callbacks causing infinite recursion.
1466 : 8921960 : if (in_scev_call)
1467 : 382042 : 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 : 17079836 : else if (src.query () != get_range_query (cfun))
1472 : : {
1473 : 1791513 : 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 : 108 : if (dump_file && (dump_flags & TDF_DETAILS)
1477 : 1791573 : && src.query () != get_global_range_query ())
1478 : 33 : fprintf (dump_file,
1479 : : "fold_using-range:: SCEV not invoked due to mismatched queries\n");
1480 : : }
1481 : : else
1482 : : {
1483 : 6748405 : in_scev_call = true;
1484 : 6748405 : if (!range_of_var_in_loop (r, name, l, phi, src.query ()))
1485 : 2734 : r.set_varying (TREE_TYPE (name));
1486 : 6748405 : in_scev_call = false;
1487 : : }
1488 : 8921960 : }
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 : 138558810 : 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 : 138558810 : if (!src.gori () || lhs_range.singleton_p ())
1506 : 47169736 : return;
1507 : :
1508 : : // Only care about AND and OR expressions.
1509 : 91389074 : enum tree_code code = gimple_expr_code (s);
1510 : 91389074 : bool is_and = false;
1511 : 91389074 : if (code == BIT_AND_EXPR || code == TRUTH_AND_EXPR)
1512 : : is_and = true;
1513 : 87426465 : else if (code != BIT_IOR_EXPR && code != TRUTH_OR_EXPR)
1514 : : return;
1515 : :
1516 : 5523124 : gimple_range_op_handler handler (s);
1517 : 5523124 : tree lhs = handler.lhs ();
1518 : 5523124 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1519 : 5523124 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1520 : :
1521 : : // Deal with || and && only when there is a full set of symbolics.
1522 : 5522938 : if (!lhs || !ssa1 || !ssa2
1523 : 2905927 : || (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE)
1524 : 2021584 : || (TREE_CODE (TREE_TYPE (ssa1)) != BOOLEAN_TYPE)
1525 : 7543472 : || (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 : 2017657 : gimple *ssa1_stmt = SSA_NAME_DEF_STMT (ssa1);
1533 : 2017657 : gimple *ssa2_stmt = SSA_NAME_DEF_STMT (ssa2);
1534 : :
1535 : 2017657 : gimple_range_op_handler handler1 (ssa1_stmt);
1536 : 2017657 : gimple_range_op_handler handler2 (ssa2_stmt);
1537 : :
1538 : : // If either handler is not present, no relation can be found.
1539 : 2017657 : if (!handler1 || !handler2)
1540 : 153778 : return;
1541 : :
1542 : : // Both stmts will need to have 2 ssa names in the stmt.
1543 : 1863879 : tree ssa1_dep1 = gimple_range_ssa_p (handler1.operand1 ());
1544 : 1863879 : tree ssa1_dep2 = gimple_range_ssa_p (handler1.operand2 ());
1545 : 1863879 : tree ssa2_dep1 = gimple_range_ssa_p (handler2.operand1 ());
1546 : 1863879 : tree ssa2_dep2 = gimple_range_ssa_p (handler2.operand2 ());
1547 : :
1548 : 1863879 : if (!ssa1_dep1 || !ssa1_dep2 || !ssa2_dep1 || !ssa2_dep2)
1549 : : return;
1550 : :
1551 : 241319 : 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 : 229502 : bool reverse_op2 = true;
1557 : 229502 : if (ssa1_dep1 == ssa2_dep1 && ssa1_dep2 == ssa2_dep2)
1558 : : reverse_op2 = false;
1559 : 229302 : else if (ssa1_dep1 != ssa2_dep2 || ssa1_dep2 != ssa2_dep1)
1560 : : return;
1561 : :
1562 : 200 : int_range<2> bool_one = range_true ();
1563 : 200 : relation_kind relation1 = handler1.op1_op2_relation (bool_one, op1, op2);
1564 : 200 : relation_kind relation2 = handler2.op1_op2_relation (bool_one, op1, op2);
1565 : 200 : if (relation1 == VREL_VARYING || relation2 == VREL_VARYING)
1566 : : return;
1567 : :
1568 : 152 : 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 : 152 : 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 : 152 : else if (!is_and && relation_union (relation1, relation2) == VREL_VARYING)
1577 : 0 : lhs_range = bool_one;
1578 : : else
1579 : 152 : 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 : 200 : }
1595 : :
1596 : : // Register any outgoing edge relations from a conditional branch.
1597 : :
1598 : : void
1599 : 72055212 : fur_source::register_outgoing_edges (gcond *s, irange &lhs_range,
1600 : : edge e0, edge e1)
1601 : : {
1602 : 72055212 : int_range<2> e0_range, e1_range;
1603 : 72055212 : tree name;
1604 : 72055212 : basic_block bb = gimple_bb (s);
1605 : :
1606 : 72055212 : gimple_range_op_handler handler (s);
1607 : 72055212 : if (!handler)
1608 : : return;
1609 : :
1610 : 72043251 : if (e0)
1611 : : {
1612 : : // If this edge is never taken, ignore it.
1613 : 59822641 : gcond_edge_range (e0_range, e0);
1614 : 59822641 : e0_range.intersect (lhs_range);
1615 : 59822641 : if (e0_range.undefined_p ())
1616 : 26466432 : e0 = NULL;
1617 : : }
1618 : :
1619 : 72043251 : if (e1)
1620 : : {
1621 : : // If this edge is never taken, ignore it.
1622 : 52587300 : gcond_edge_range (e1_range, e1);
1623 : 52587300 : e1_range.intersect (lhs_range);
1624 : 52587300 : if (e1_range.undefined_p ())
1625 : 30222729 : e1 = NULL;
1626 : : }
1627 : :
1628 : 72043251 : if (!e0 && !e1)
1629 : : return;
1630 : :
1631 : : // First, register the gcond itself. This will catch statements like
1632 : : // if (a_2 < b_5)
1633 : 68741205 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1634 : 68741205 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1635 : 68741205 : value_range r1,r2;
1636 : 68741205 : if (ssa1 && ssa2)
1637 : : {
1638 : 20428603 : r1.set_varying (TREE_TYPE (ssa1));
1639 : 20428603 : r2.set_varying (TREE_TYPE (ssa2));
1640 : 20428603 : if (e0)
1641 : : {
1642 : 13737978 : relation_kind relation = handler.op1_op2_relation (e0_range, r1, r2);
1643 : 13737978 : if (relation != VREL_VARYING)
1644 : 13662089 : register_relation (e0, relation, ssa1, ssa2);
1645 : : }
1646 : 20428603 : if (e1)
1647 : : {
1648 : 12039351 : relation_kind relation = handler.op1_op2_relation (e1_range, r1, r2);
1649 : 12039351 : if (relation != VREL_VARYING)
1650 : 11981353 : register_relation (e1, relation, ssa1, ssa2);
1651 : : }
1652 : : }
1653 : :
1654 : : // Outgoing relations of GORI exports require a gori engine.
1655 : 124160764 : if (!gori_ssa ())
1656 : 13321658 : 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 : 176019029 : FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb, name)
1663 : : {
1664 : 120599482 : if (TREE_CODE (TREE_TYPE (name)) != BOOLEAN_TYPE)
1665 : 114935442 : continue;
1666 : 9417104 : gimple *stmt = SSA_NAME_DEF_STMT (name);
1667 : 9417104 : gimple_range_op_handler handler (stmt);
1668 : 9417104 : if (!handler)
1669 : 3753064 : continue;
1670 : 5664040 : tree ssa1 = gimple_range_ssa_p (handler.operand1 ());
1671 : 5664040 : tree ssa2 = gimple_range_ssa_p (handler.operand2 ());
1672 : 5664040 : value_range r (TREE_TYPE (name));
1673 : 5664040 : if (ssa1 && ssa2)
1674 : : {
1675 : 2527202 : r1.set_varying (TREE_TYPE (ssa1));
1676 : 2527202 : r2.set_varying (TREE_TYPE (ssa2));
1677 : 1551417 : if (e0 && gori ()->edge_range_p (r, e0, name, *m_query)
1678 : 4043980 : && r.singleton_p ())
1679 : : {
1680 : 1384769 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1681 : 1384769 : if (relation != VREL_VARYING)
1682 : 488159 : register_relation (e0, relation, ssa1, ssa2);
1683 : : }
1684 : 1603977 : if (e1 && gori ()->edge_range_p (r, e1, name, *m_query)
1685 : 4085552 : && r.singleton_p ())
1686 : : {
1687 : 1140949 : relation_kind relation = handler.op1_op2_relation (r, r1, r2);
1688 : 1140949 : if (relation != VREL_VARYING)
1689 : 181626 : register_relation (e1, relation, ssa1, ssa2);
1690 : : }
1691 : : }
1692 : 5664040 : }
1693 : 72055212 : }
|