c++: over-eager friend matching [PR109649]
[official-gcc.git] / gcc / gimple-range-gori.cc
bloba1c8d51e484664c0259ad8509c2447523a61a674
1 /* Gimple range GORI functions.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "gimple-range.h"
32 // Return TRUE if GS is a logical && or || expression.
34 static inline bool
35 is_gimple_logical_p (const gimple *gs)
37 // Look for boolean and/or condition.
38 if (is_gimple_assign (gs))
39 switch (gimple_expr_code (gs))
41 case TRUTH_AND_EXPR:
42 case TRUTH_OR_EXPR:
43 return true;
45 case BIT_AND_EXPR:
46 case BIT_IOR_EXPR:
47 // Bitwise operations on single bits are logical too.
48 if (types_compatible_p (TREE_TYPE (gimple_assign_rhs1 (gs)),
49 boolean_type_node))
50 return true;
51 break;
53 default:
54 break;
56 return false;
59 /* RANGE_DEF_CHAIN is used to determine which SSA names in a block can
60 have range information calculated for them, and what the
61 dependencies on each other are.
63 Information for a basic block is calculated once and stored. It is
64 only calculated the first time a query is made, so if no queries
65 are made, there is little overhead.
67 The def_chain bitmap is indexed by SSA_NAME_VERSION. Bits are set
68 within this bitmap to indicate SSA names that are defined in the
69 SAME block and used to calculate this SSA name.
72 <bb 2> :
73 _1 = x_4(D) + -2;
74 _2 = _1 * 4;
75 j_7 = foo ();
76 q_5 = _2 + 3;
77 if (q_5 <= 13)
79 _1 : x_4(D)
80 _2 : 1 x_4(D)
81 q_5 : _1 _2 x_4(D)
83 This dump indicates the bits set in the def_chain vector.
84 as well as demonstrates the def_chain bits for the related ssa_names.
86 Checking the chain for _2 indicates that _1 and x_4 are used in
87 its evaluation.
89 Def chains also only include statements which are valid gimple
90 so a def chain will only span statements for which the range
91 engine implements operations for. */
94 // Construct a range_def_chain.
96 range_def_chain::range_def_chain ()
98 bitmap_obstack_initialize (&m_bitmaps);
99 m_def_chain.create (0);
100 m_def_chain.safe_grow_cleared (num_ssa_names);
101 m_logical_depth = 0;
104 // Destruct a range_def_chain.
106 range_def_chain::~range_def_chain ()
108 m_def_chain.release ();
109 bitmap_obstack_release (&m_bitmaps);
112 // Return true if NAME is in the def chain of DEF. If BB is provided,
113 // only return true if the defining statement of DEF is in BB.
115 bool
116 range_def_chain::in_chain_p (tree name, tree def)
118 gcc_checking_assert (gimple_range_ssa_p (def));
119 gcc_checking_assert (gimple_range_ssa_p (name));
121 // Get the definition chain for DEF.
122 bitmap chain = get_def_chain (def);
124 if (chain == NULL)
125 return false;
126 return bitmap_bit_p (chain, SSA_NAME_VERSION (name));
129 // Add either IMP or the import list B to the import set of DATA.
131 void
132 range_def_chain::set_import (struct rdc &data, tree imp, bitmap b)
134 // If there are no imports, just return
135 if (imp == NULL_TREE && !b)
136 return;
137 if (!data.m_import)
138 data.m_import = BITMAP_ALLOC (&m_bitmaps);
139 if (imp != NULL_TREE)
140 bitmap_set_bit (data.m_import, SSA_NAME_VERSION (imp));
141 else
142 bitmap_ior_into (data.m_import, b);
145 // Return the import list for NAME.
147 bitmap
148 range_def_chain::get_imports (tree name)
150 if (!has_def_chain (name))
151 get_def_chain (name);
152 bitmap i = m_def_chain[SSA_NAME_VERSION (name)].m_import;
153 return i;
156 // Return true if IMPORT is an import to NAMEs def chain.
158 bool
159 range_def_chain::chain_import_p (tree name, tree import)
161 bitmap b = get_imports (name);
162 if (b)
163 return bitmap_bit_p (b, SSA_NAME_VERSION (import));
164 return false;
167 // Build def_chains for NAME if it is in BB. Copy the def chain into RESULT.
169 void
170 range_def_chain::register_dependency (tree name, tree dep, basic_block bb)
172 if (!gimple_range_ssa_p (dep))
173 return;
175 unsigned v = SSA_NAME_VERSION (name);
176 if (v >= m_def_chain.length ())
177 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
178 struct rdc &src = m_def_chain[v];
179 gimple *def_stmt = SSA_NAME_DEF_STMT (dep);
180 unsigned dep_v = SSA_NAME_VERSION (dep);
181 bitmap b;
183 // Set the direct dependency cache entries.
184 if (!src.ssa1)
185 src.ssa1 = SSA_NAME_VERSION (dep);
186 else if (!src.ssa2 && src.ssa1 != SSA_NAME_VERSION (dep))
187 src.ssa2 = SSA_NAME_VERSION (dep);
189 // Don't calculate imports or export/dep chains if BB is not provided.
190 // This is usually the case for when the temporal cache wants the direct
191 // dependencies of a stmt.
192 if (!bb)
193 return;
195 if (!src.bm)
196 src.bm = BITMAP_ALLOC (&m_bitmaps);
198 // Add this operand into the result.
199 bitmap_set_bit (src.bm, dep_v);
201 if (gimple_bb (def_stmt) == bb && !is_a<gphi *>(def_stmt))
203 // Get the def chain for the operand.
204 b = get_def_chain (dep);
205 // If there was one, copy it into result. Access def_chain directly
206 // as the get_def_chain request above could reallocate the vector.
207 if (b)
208 bitmap_ior_into (m_def_chain[v].bm, b);
209 // And copy the import list.
210 set_import (m_def_chain[v], NULL_TREE, get_imports (dep));
212 else
213 // Originated outside the block, so it is an import.
214 set_import (src, dep, NULL);
217 bool
218 range_def_chain::def_chain_in_bitmap_p (tree name, bitmap b)
220 bitmap a = get_def_chain (name);
221 if (a && b)
222 return bitmap_intersect_p (a, b);
223 return false;
226 void
227 range_def_chain::add_def_chain_to_bitmap (bitmap b, tree name)
229 bitmap r = get_def_chain (name);
230 if (r)
231 bitmap_ior_into (b, r);
235 // Return TRUE if NAME has been processed for a def_chain.
237 inline bool
238 range_def_chain::has_def_chain (tree name)
240 // Ensure there is an entry in the internal vector.
241 unsigned v = SSA_NAME_VERSION (name);
242 if (v >= m_def_chain.length ())
243 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
244 return (m_def_chain[v].ssa1 != 0);
249 // Calculate the def chain for NAME and all of its dependent
250 // operands. Only using names in the same BB. Return the bitmap of
251 // all names in the m_def_chain. This only works for supported range
252 // statements.
254 bitmap
255 range_def_chain::get_def_chain (tree name)
257 tree ssa[3];
258 unsigned v = SSA_NAME_VERSION (name);
260 // If it has already been processed, just return the cached value.
261 if (has_def_chain (name) && m_def_chain[v].bm)
262 return m_def_chain[v].bm;
264 // No definition chain for default defs.
265 if (SSA_NAME_IS_DEFAULT_DEF (name))
267 // A Default def is always an import.
268 set_import (m_def_chain[v], name, NULL);
269 return NULL;
272 gimple *stmt = SSA_NAME_DEF_STMT (name);
273 unsigned count = gimple_range_ssa_names (ssa, 3, stmt);
274 if (count == 0)
276 // Stmts not understood or with no operands are always imports.
277 set_import (m_def_chain[v], name, NULL);
278 return NULL;
281 // Terminate the def chains if we see too many cascading stmts.
282 if (m_logical_depth == param_ranger_logical_depth)
283 return NULL;
285 // Increase the depth if we have a pair of ssa-names.
286 if (count > 1)
287 m_logical_depth++;
289 for (unsigned x = 0; x < count; x++)
290 register_dependency (name, ssa[x], gimple_bb (stmt));
292 if (count > 1)
293 m_logical_depth--;
295 return m_def_chain[v].bm;
298 // Dump what we know for basic block BB to file F.
300 void
301 range_def_chain::dump (FILE *f, basic_block bb, const char *prefix)
303 unsigned x, y;
304 bitmap_iterator bi;
306 // Dump the def chain for each SSA_NAME defined in BB.
307 for (x = 1; x < num_ssa_names; x++)
309 tree name = ssa_name (x);
310 if (!name)
311 continue;
312 gimple *stmt = SSA_NAME_DEF_STMT (name);
313 if (!stmt || (bb && gimple_bb (stmt) != bb))
314 continue;
315 bitmap chain = (has_def_chain (name) ? get_def_chain (name) : NULL);
316 if (chain && !bitmap_empty_p (chain))
318 fprintf (f, prefix);
319 print_generic_expr (f, name, TDF_SLIM);
320 fprintf (f, " : ");
322 bitmap imports = get_imports (name);
323 EXECUTE_IF_SET_IN_BITMAP (chain, 0, y, bi)
325 print_generic_expr (f, ssa_name (y), TDF_SLIM);
326 if (imports && bitmap_bit_p (imports, y))
327 fprintf (f, "(I)");
328 fprintf (f, " ");
330 fprintf (f, "\n");
336 // -------------------------------------------------------------------
338 /* GORI_MAP is used to accumulate what SSA names in a block can
339 generate range information, and provides tools for the block ranger
340 to enable it to efficiently calculate these ranges.
342 GORI stands for "Generates Outgoing Range Information."
344 It utilizes the range_def_chain class to construct def_chains.
345 Information for a basic block is calculated once and stored. It is
346 only calculated the first time a query is made. If no queries are
347 made, there is little overhead.
349 one bitmap is maintained for each basic block:
350 m_outgoing : a set bit indicates a range can be generated for a name.
352 Generally speaking, the m_outgoing vector is the union of the
353 entire def_chain of all SSA names used in the last statement of the
354 block which generate ranges. */
357 // Initialize a gori-map structure.
359 gori_map::gori_map ()
361 m_outgoing.create (0);
362 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
363 m_incoming.create (0);
364 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
365 m_maybe_variant = BITMAP_ALLOC (&m_bitmaps);
368 // Free any memory the GORI map allocated.
370 gori_map::~gori_map ()
372 m_incoming.release ();
373 m_outgoing.release ();
376 // Return the bitmap vector of all export from BB. Calculate if necessary.
378 bitmap
379 gori_map::exports (basic_block bb)
381 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
382 calculate_gori (bb);
383 return m_outgoing[bb->index];
386 // Return the bitmap vector of all imports to BB. Calculate if necessary.
388 bitmap
389 gori_map::imports (basic_block bb)
391 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
392 calculate_gori (bb);
393 return m_incoming[bb->index];
396 // Return true if NAME is can have ranges generated for it from basic
397 // block BB.
399 bool
400 gori_map::is_export_p (tree name, basic_block bb)
402 // If no BB is specified, test if it is exported anywhere in the IL.
403 if (!bb)
404 return bitmap_bit_p (m_maybe_variant, SSA_NAME_VERSION (name));
405 return bitmap_bit_p (exports (bb), SSA_NAME_VERSION (name));
408 // Set or clear the m_maybe_variant bit to determine if ranges will be tracked
409 // for NAME. A clear bit means they will NOT be tracked.
411 void
412 gori_map::set_range_invariant (tree name, bool invariant)
414 if (invariant)
415 bitmap_clear_bit (m_maybe_variant, SSA_NAME_VERSION (name));
416 else
417 bitmap_set_bit (m_maybe_variant, SSA_NAME_VERSION (name));
420 // Return true if NAME is an import to block BB.
422 bool
423 gori_map::is_import_p (tree name, basic_block bb)
425 // If no BB is specified, test if it is exported anywhere in the IL.
426 return bitmap_bit_p (imports (bb), SSA_NAME_VERSION (name));
429 // If NAME is non-NULL and defined in block BB, calculate the def
430 // chain and add it to m_outgoing.
432 void
433 gori_map::maybe_add_gori (tree name, basic_block bb)
435 if (name)
437 // Check if there is a def chain, regardless of the block.
438 add_def_chain_to_bitmap (m_outgoing[bb->index], name);
439 // Check for any imports.
440 bitmap imp = get_imports (name);
441 // If there were imports, add them so we can recompute
442 if (imp)
443 bitmap_ior_into (m_incoming[bb->index], imp);
444 // This name is always an import.
445 if (gimple_bb (SSA_NAME_DEF_STMT (name)) != bb)
446 bitmap_set_bit (m_incoming[bb->index], SSA_NAME_VERSION (name));
448 // Def chain doesn't include itself, and even if there isn't a
449 // def chain, this name should be added to exports.
450 bitmap_set_bit (m_outgoing[bb->index], SSA_NAME_VERSION (name));
454 // Calculate all the required information for BB.
456 void
457 gori_map::calculate_gori (basic_block bb)
459 tree name;
460 if (bb->index >= (signed int)m_outgoing.length ())
462 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
463 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
465 gcc_checking_assert (m_outgoing[bb->index] == NULL);
466 m_outgoing[bb->index] = BITMAP_ALLOC (&m_bitmaps);
467 m_incoming[bb->index] = BITMAP_ALLOC (&m_bitmaps);
469 if (single_succ_p (bb))
470 return;
472 // If this block's last statement may generate range information, go
473 // calculate it.
474 gimple *stmt = gimple_outgoing_range_stmt_p (bb);
475 if (!stmt)
476 return;
477 if (is_a<gcond *> (stmt))
479 gcond *gc = as_a<gcond *>(stmt);
480 name = gimple_range_ssa_p (gimple_cond_lhs (gc));
481 maybe_add_gori (name, gimple_bb (stmt));
483 name = gimple_range_ssa_p (gimple_cond_rhs (gc));
484 maybe_add_gori (name, gimple_bb (stmt));
486 else
488 // Do not process switches if they are too large.
489 if (EDGE_COUNT (bb->succs) > (unsigned)param_vrp_switch_limit)
490 return;
491 gswitch *gs = as_a<gswitch *>(stmt);
492 name = gimple_range_ssa_p (gimple_switch_index (gs));
493 maybe_add_gori (name, gimple_bb (stmt));
495 // Add this bitmap to the aggregate list of all outgoing names.
496 bitmap_ior_into (m_maybe_variant, m_outgoing[bb->index]);
499 // Dump the table information for BB to file F.
501 void
502 gori_map::dump (FILE *f, basic_block bb, bool verbose)
504 // BB was not processed.
505 if (!m_outgoing[bb->index] || bitmap_empty_p (m_outgoing[bb->index]))
506 return;
508 tree name;
510 bitmap imp = imports (bb);
511 if (!bitmap_empty_p (imp))
513 if (verbose)
514 fprintf (f, "bb<%u> Imports: ",bb->index);
515 else
516 fprintf (f, "Imports: ");
517 FOR_EACH_GORI_IMPORT_NAME (*this, bb, name)
519 print_generic_expr (f, name, TDF_SLIM);
520 fprintf (f, " ");
522 fputc ('\n', f);
525 if (verbose)
526 fprintf (f, "bb<%u> Exports: ",bb->index);
527 else
528 fprintf (f, "Exports: ");
529 // Dump the export vector.
530 FOR_EACH_GORI_EXPORT_NAME (*this, bb, name)
532 print_generic_expr (f, name, TDF_SLIM);
533 fprintf (f, " ");
535 fputc ('\n', f);
537 range_def_chain::dump (f, bb, " ");
540 // Dump the entire GORI map structure to file F.
542 void
543 gori_map::dump (FILE *f)
545 basic_block bb;
546 FOR_EACH_BB_FN (bb, cfun)
547 dump (f, bb);
550 DEBUG_FUNCTION void
551 debug (gori_map &g)
553 g.dump (stderr);
556 // -------------------------------------------------------------------
558 // Construct a gori_compute object.
560 gori_compute::gori_compute (int not_executable_flag)
561 : outgoing (param_vrp_switch_limit), tracer ("GORI ")
563 m_not_executable_flag = not_executable_flag;
564 // Create a boolean_type true and false range.
565 m_bool_zero = range_false ();
566 m_bool_one = range_true ();
567 if (dump_file && (param_ranger_debug & RANGER_DEBUG_GORI))
568 tracer.enable_trace ();
571 // Given the switch S, return an evaluation in R for NAME when the lhs
572 // evaluates to LHS. Returning false means the name being looked for
573 // was not resolvable.
575 bool
576 gori_compute::compute_operand_range_switch (vrange &r, gswitch *s,
577 const vrange &lhs,
578 tree name, fur_source &src)
580 tree op1 = gimple_switch_index (s);
582 // If name matches, the range is simply the range from the edge.
583 // Empty ranges are viral as they are on a path which isn't
584 // executable.
585 if (op1 == name || lhs.undefined_p ())
587 r = lhs;
588 return true;
591 // If op1 is in the definition chain, pass lhs back.
592 if (gimple_range_ssa_p (op1) && in_chain_p (name, op1))
593 return compute_operand_range (r, SSA_NAME_DEF_STMT (op1), lhs, name, src);
595 return false;
599 // Return an evaluation for NAME as it would appear in STMT when the
600 // statement's lhs evaluates to LHS. If successful, return TRUE and
601 // store the evaluation in R, otherwise return FALSE.
603 bool
604 gori_compute::compute_operand_range (vrange &r, gimple *stmt,
605 const vrange &lhs, tree name,
606 fur_source &src, value_relation *rel)
608 value_relation vrel;
609 value_relation *vrel_ptr = rel;
610 // Empty ranges are viral as they are on an unexecutable path.
611 if (lhs.undefined_p ())
613 r.set_undefined ();
614 return true;
616 if (is_a<gswitch *> (stmt))
617 return compute_operand_range_switch (r, as_a<gswitch *> (stmt), lhs, name,
618 src);
619 gimple_range_op_handler handler (stmt);
620 if (!handler)
621 return false;
623 tree op1 = gimple_range_ssa_p (handler.operand1 ());
624 tree op2 = gimple_range_ssa_p (handler.operand2 ());
626 // Handle end of lookup first.
627 if (op1 == name)
628 return compute_operand1_range (r, handler, lhs, name, src, vrel_ptr);
629 if (op2 == name)
630 return compute_operand2_range (r, handler, lhs, name, src, vrel_ptr);
632 // NAME is not in this stmt, but one of the names in it ought to be
633 // derived from it.
634 bool op1_in_chain = op1 && in_chain_p (name, op1);
635 bool op2_in_chain = op2 && in_chain_p (name, op2);
637 // If neither operand is derived, then this stmt tells us nothing.
638 if (!op1_in_chain && !op2_in_chain)
639 return false;
641 bool res = false;
642 // If the lhs doesn't tell us anything only a relation can possibly enhance
643 // the result.
644 if (lhs.varying_p ())
646 if (!vrel_ptr)
647 return false;
648 // If there is a relation (ie: x != y) , it can only be relevant if
649 // a) both elements are in the defchain
650 // c = x > y // (x and y are in c's defchain)
651 if (op1_in_chain)
652 res = in_chain_p (vrel_ptr->op1 (), op1)
653 && in_chain_p (vrel_ptr->op2 (), op1);
654 if (!res && op2_in_chain)
655 res = in_chain_p (vrel_ptr->op1 (), op2)
656 || in_chain_p (vrel_ptr->op2 (), op2);
657 if (!res)
659 // or b) one relation element is in the defchain of the other and the
660 // other is the LHS of this stmt.
661 // x = y + 2
662 if (vrel_ptr->op1 () == handler.lhs ()
663 && (vrel_ptr->op2 () == op1 || vrel_ptr->op2 () == op2))
664 res = true;
665 else if (vrel_ptr->op2 () == handler.lhs ()
666 && (vrel_ptr->op1 () == op1 || vrel_ptr->op1 () == op2))
667 res = true;
669 if (!res)
670 return false;
673 // Process logicals as they have special handling.
674 if (is_gimple_logical_p (stmt))
676 // If the lhs doesn't tell us anything, neither will combining operands.
677 if (lhs.varying_p ())
678 return false;
680 unsigned idx;
681 if ((idx = tracer.header ("compute_operand ")))
683 print_generic_expr (dump_file, name, TDF_SLIM);
684 fprintf (dump_file, " with LHS = ");
685 lhs.dump (dump_file);
686 fprintf (dump_file, " at stmt ");
687 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
690 tree type = TREE_TYPE (name);
691 Value_Range op1_trange (type), op1_frange (type);
692 Value_Range op2_trange (type), op2_frange (type);
693 compute_logical_operands (op1_trange, op1_frange, handler,
694 as_a <irange> (lhs),
695 name, src, op1, op1_in_chain);
696 compute_logical_operands (op2_trange, op2_frange, handler,
697 as_a <irange> (lhs),
698 name, src, op2, op2_in_chain);
699 res = logical_combine (r,
700 gimple_expr_code (stmt),
701 as_a <irange> (lhs),
702 op1_trange, op1_frange, op2_trange, op2_frange);
703 if (idx)
704 tracer.trailer (idx, "compute_operand", res, name, r);
706 // Follow the appropriate operands now.
707 else if (op1_in_chain && op2_in_chain)
708 res = compute_operand1_and_operand2_range (r, handler, lhs, name, src,
709 vrel_ptr);
710 else if (op1_in_chain)
711 res = compute_operand1_range (r, handler, lhs, name, src, vrel_ptr);
712 else if (op2_in_chain)
713 res = compute_operand2_range (r, handler, lhs, name, src, vrel_ptr);
714 else
715 gcc_unreachable ();
717 // If neither operand is derived, this statement tells us nothing.
718 return res;
722 // Return TRUE if range R is either a true or false compatible range.
724 static bool
725 range_is_either_true_or_false (const irange &r)
727 if (r.undefined_p ())
728 return false;
730 // This is complicated by the fact that Ada has multi-bit booleans,
731 // so true can be ~[0, 0] (i.e. [1,MAX]).
732 tree type = r.type ();
733 gcc_checking_assert (range_compatible_p (type, boolean_type_node));
734 return (r.singleton_p ()
735 || !r.contains_p (wi::zero (TYPE_PRECISION (type))));
738 // Evaluate a binary logical expression by combining the true and
739 // false ranges for each of the operands based on the result value in
740 // the LHS.
742 bool
743 gori_compute::logical_combine (vrange &r, enum tree_code code,
744 const irange &lhs,
745 const vrange &op1_true, const vrange &op1_false,
746 const vrange &op2_true, const vrange &op2_false)
748 if (op1_true.varying_p () && op1_false.varying_p ()
749 && op2_true.varying_p () && op2_false.varying_p ())
750 return false;
752 unsigned idx;
753 if ((idx = tracer.header ("logical_combine")))
755 switch (code)
757 case TRUTH_OR_EXPR:
758 case BIT_IOR_EXPR:
759 fprintf (dump_file, " || ");
760 break;
761 case TRUTH_AND_EXPR:
762 case BIT_AND_EXPR:
763 fprintf (dump_file, " && ");
764 break;
765 default:
766 break;
768 fprintf (dump_file, " with LHS = ");
769 lhs.dump (dump_file);
770 fputc ('\n', dump_file);
772 tracer.print (idx, "op1_true = ");
773 op1_true.dump (dump_file);
774 fprintf (dump_file, " op1_false = ");
775 op1_false.dump (dump_file);
776 fputc ('\n', dump_file);
777 tracer.print (idx, "op2_true = ");
778 op2_true.dump (dump_file);
779 fprintf (dump_file, " op2_false = ");
780 op2_false.dump (dump_file);
781 fputc ('\n', dump_file);
784 // This is not a simple fold of a logical expression, rather it
785 // determines ranges which flow through the logical expression.
787 // Assuming x_8 is an unsigned char, and relational statements:
788 // b_1 = x_8 < 20
789 // b_2 = x_8 > 5
790 // consider the logical expression and branch:
791 // c_2 = b_1 && b_2
792 // if (c_2)
794 // To determine the range of x_8 on either edge of the branch, one
795 // must first determine what the range of x_8 is when the boolean
796 // values of b_1 and b_2 are both true and false.
797 // b_1 TRUE x_8 = [0, 19]
798 // b_1 FALSE x_8 = [20, 255]
799 // b_2 TRUE x_8 = [6, 255]
800 // b_2 FALSE x_8 = [0,5].
802 // These ranges are then combined based on the expected outcome of
803 // the branch. The range on the TRUE side of the branch must satisfy
804 // b_1 == true && b_2 == true
806 // In terms of x_8, that means both x_8 == [0, 19] and x_8 = [6, 255]
807 // must be true. The range of x_8 on the true side must be the
808 // intersection of both ranges since both must be true. Thus the
809 // range of x_8 on the true side is [6, 19].
811 // To determine the ranges on the FALSE side, all 3 combinations of
812 // failing ranges must be considered, and combined as any of them
813 // can cause the false result.
815 // If the LHS can be TRUE or FALSE, then evaluate both a TRUE and
816 // FALSE results and combine them. If we fell back to VARYING any
817 // range restrictions that have been discovered up to this point
818 // would be lost.
819 if (!range_is_either_true_or_false (lhs))
821 bool res;
822 Value_Range r1 (r);
823 if (logical_combine (r1, code, m_bool_zero, op1_true, op1_false,
824 op2_true, op2_false)
825 && logical_combine (r, code, m_bool_one, op1_true, op1_false,
826 op2_true, op2_false))
828 r.union_ (r1);
829 res = true;
831 else
832 res = false;
833 if (idx && res)
835 tracer.print (idx, "logical_combine produced ");
836 r.dump (dump_file);
837 fputc ('\n', dump_file);
841 switch (code)
843 // A logical AND combines ranges from 2 boolean conditions.
844 // c_2 = b_1 && b_2
845 case TRUTH_AND_EXPR:
846 case BIT_AND_EXPR:
847 if (!lhs.zero_p ())
849 // The TRUE side is the intersection of the 2 true ranges.
850 r = op1_true;
851 r.intersect (op2_true);
853 else
855 // The FALSE side is the union of the other 3 cases.
856 Value_Range ff (op1_false);
857 ff.intersect (op2_false);
858 Value_Range tf (op1_true);
859 tf.intersect (op2_false);
860 Value_Range ft (op1_false);
861 ft.intersect (op2_true);
862 r = ff;
863 r.union_ (tf);
864 r.union_ (ft);
866 break;
867 // A logical OR combines ranges from 2 boolean conditions.
868 // c_2 = b_1 || b_2
869 case TRUTH_OR_EXPR:
870 case BIT_IOR_EXPR:
871 if (lhs.zero_p ())
873 // An OR operation will only take the FALSE path if both
874 // operands are false simultaneously, which means they should
875 // be intersected. !(x || y) == !x && !y
876 r = op1_false;
877 r.intersect (op2_false);
879 else
881 // The TRUE side of an OR operation will be the union of
882 // the other three combinations.
883 Value_Range tt (op1_true);
884 tt.intersect (op2_true);
885 Value_Range tf (op1_true);
886 tf.intersect (op2_false);
887 Value_Range ft (op1_false);
888 ft.intersect (op2_true);
889 r = tt;
890 r.union_ (tf);
891 r.union_ (ft);
893 break;
894 default:
895 gcc_unreachable ();
898 if (idx)
899 tracer.trailer (idx, "logical_combine", true, NULL_TREE, r);
900 return true;
904 // Given a logical STMT, calculate true and false ranges for each
905 // potential path of NAME, assuming NAME came through the OP chain if
906 // OP_IN_CHAIN is true.
908 void
909 gori_compute::compute_logical_operands (vrange &true_range, vrange &false_range,
910 gimple_range_op_handler &handler,
911 const irange &lhs,
912 tree name, fur_source &src,
913 tree op, bool op_in_chain)
915 gimple *stmt = handler.stmt ();
916 gimple *src_stmt = gimple_range_ssa_p (op) ? SSA_NAME_DEF_STMT (op) : NULL;
917 if (!op_in_chain || !src_stmt || chain_import_p (handler.lhs (), op))
919 // If op is not in the def chain, or defined in this block,
920 // use its known value on entry to the block.
921 src.get_operand (true_range, name);
922 false_range = true_range;
923 unsigned idx;
924 if ((idx = tracer.header ("logical_operand")))
926 print_generic_expr (dump_file, op, TDF_SLIM);
927 fprintf (dump_file, " not in computation chain. Queried.\n");
928 tracer.trailer (idx, "logical_operand", true, NULL_TREE, true_range);
930 return;
933 enum tree_code code = gimple_expr_code (stmt);
934 // Optimize [0 = x | y], since neither operand can ever be non-zero.
935 if ((code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) && lhs.zero_p ())
937 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name,
938 src))
939 src.get_operand (false_range, name);
940 true_range = false_range;
941 return;
944 // Optimize [1 = x & y], since neither operand can ever be zero.
945 if ((code == BIT_AND_EXPR || code == TRUTH_AND_EXPR) && lhs == m_bool_one)
947 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
948 src.get_operand (true_range, name);
949 false_range = true_range;
950 return;
953 // Calculate ranges for true and false on both sides, since the false
954 // path is not always a simple inversion of the true side.
955 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
956 src.get_operand (true_range, name);
957 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name, src))
958 src.get_operand (false_range, name);
962 // This routine will try to refine the ranges of OP1 and OP2 given a relation
963 // K between them. In order to perform this refinement, one of the operands
964 // must be in the definition chain of the other. The use is refined using
965 // op1/op2_range on the statement, and the definition is then recalculated
966 // using the relation.
968 bool
969 gori_compute::refine_using_relation (tree op1, vrange &op1_range,
970 tree op2, vrange &op2_range,
971 fur_source &src, relation_kind k)
973 gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
974 gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
976 if (k == VREL_VARYING || k == VREL_EQ || k == VREL_UNDEFINED)
977 return false;
979 bool change = false;
980 bool op1_def_p = in_chain_p (op2, op1);
981 if (!op1_def_p)
982 if (!in_chain_p (op1, op2))
983 return false;
985 tree def_op = op1_def_p ? op1 : op2;
986 tree use_op = op1_def_p ? op2 : op1;
988 if (!op1_def_p)
989 k = relation_swap (k);
991 // op1_def is true if we want to look up op1, otherwise we want op2.
992 // if neither is the case, we returned in the above check.
994 gimple *def_stmt = SSA_NAME_DEF_STMT (def_op);
995 gimple_range_op_handler op_handler (def_stmt);
996 if (!op_handler)
997 return false;
998 tree def_op1 = op_handler.operand1 ();
999 tree def_op2 = op_handler.operand2 ();
1000 // if the def isn't binary, the relation will not be useful.
1001 if (!def_op2)
1002 return false;
1004 // Determine if op2 is directly referenced as an operand.
1005 if (def_op1 == use_op)
1007 // def_stmt has op1 in the 1st operand position.
1008 Value_Range other_op (TREE_TYPE (def_op2));
1009 src.get_operand (other_op, def_op2);
1011 // Using op1_range as the LHS, and relation REL, evaluate op2.
1012 tree type = TREE_TYPE (def_op1);
1013 Value_Range new_result (type);
1014 if (!op_handler.op1_range (new_result, type,
1015 op1_def_p ? op1_range : op2_range,
1016 other_op, relation_trio::lhs_op1 (k)))
1017 return false;
1018 if (op1_def_p)
1020 change |= op2_range.intersect (new_result);
1021 // Recalculate op2.
1022 if (op_handler.fold_range (new_result, type, op2_range, other_op))
1024 change |= op1_range.intersect (new_result);
1027 else
1029 change |= op1_range.intersect (new_result);
1030 // Recalculate op1.
1031 if (op_handler.fold_range (new_result, type, op1_range, other_op))
1033 change |= op2_range.intersect (new_result);
1037 else if (def_op2 == use_op)
1039 // def_stmt has op1 in the 1st operand position.
1040 Value_Range other_op (TREE_TYPE (def_op1));
1041 src.get_operand (other_op, def_op1);
1043 // Using op1_range as the LHS, and relation REL, evaluate op2.
1044 tree type = TREE_TYPE (def_op2);
1045 Value_Range new_result (type);
1046 if (!op_handler.op2_range (new_result, type,
1047 op1_def_p ? op1_range : op2_range,
1048 other_op, relation_trio::lhs_op2 (k)))
1049 return false;
1050 if (op1_def_p)
1052 change |= op2_range.intersect (new_result);
1053 // Recalculate op1.
1054 if (op_handler.fold_range (new_result, type, other_op, op2_range))
1056 change |= op1_range.intersect (new_result);
1059 else
1061 change |= op1_range.intersect (new_result);
1062 // Recalculate op2.
1063 if (op_handler.fold_range (new_result, type, other_op, op1_range))
1065 change |= op2_range.intersect (new_result);
1069 return change;
1072 // Calculate a range for NAME from the operand 1 position of STMT
1073 // assuming the result of the statement is LHS. Return the range in
1074 // R, or false if no range could be calculated.
1076 bool
1077 gori_compute::compute_operand1_range (vrange &r,
1078 gimple_range_op_handler &handler,
1079 const vrange &lhs, tree name,
1080 fur_source &src, value_relation *rel)
1082 value_relation local_rel;
1083 gimple *stmt = handler.stmt ();
1084 tree op1 = handler.operand1 ();
1085 tree op2 = handler.operand2 ();
1086 tree lhs_name = gimple_get_lhs (stmt);
1088 relation_trio trio;
1089 if (rel)
1090 trio = rel->create_trio (lhs_name, op1, op2);
1091 relation_kind op_op = trio.op1_op2 ();
1093 Value_Range op1_range (TREE_TYPE (op1));
1094 Value_Range tmp (TREE_TYPE (op1));
1095 Value_Range op2_range (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
1097 // Fetch the known range for op1 in this block.
1098 src.get_operand (op1_range, op1);
1100 // Now range-op calculate and put that result in r.
1101 if (op2)
1103 src.get_operand (op2_range, op2);
1105 // If there is a relation betwen op1 and op2, use it instead.
1106 // This allows multiple relations to be processed in compound logicals.
1107 if (gimple_range_ssa_p (op1) && gimple_range_ssa_p (op2))
1109 relation_kind k = handler.op1_op2_relation (lhs);
1110 if (k != VREL_VARYING)
1112 op_op = k;
1113 local_rel.set_relation (op_op, op1, op2);
1114 rel = &local_rel;
1118 if (op_op != VREL_VARYING)
1119 refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
1121 // If op1 == op2, create a new trio for just this call.
1122 if (op1 == op2 && gimple_range_ssa_p (op1))
1123 trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
1124 if (!handler.calc_op1 (tmp, lhs, op2_range, trio))
1125 return false;
1127 else
1129 // We pass op1_range to the unary operation. Normally it's a
1130 // hidden range_for_type parameter, but sometimes having the
1131 // actual range can result in better information.
1132 if (!handler.calc_op1 (tmp, lhs, op1_range, trio))
1133 return false;
1136 unsigned idx;
1137 if ((idx = tracer.header ("compute op 1 (")))
1139 print_generic_expr (dump_file, op1, TDF_SLIM);
1140 fprintf (dump_file, ") at ");
1141 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1142 tracer.print (idx, "LHS =");
1143 lhs.dump (dump_file);
1144 if (op2 && TREE_CODE (op2) == SSA_NAME)
1146 fprintf (dump_file, ", ");
1147 print_generic_expr (dump_file, op2, TDF_SLIM);
1148 fprintf (dump_file, " = ");
1149 op2_range.dump (dump_file);
1151 fprintf (dump_file, "\n");
1152 tracer.print (idx, "Computes ");
1153 print_generic_expr (dump_file, op1, TDF_SLIM);
1154 fprintf (dump_file, " = ");
1155 tmp.dump (dump_file);
1156 fprintf (dump_file, " intersect Known range : ");
1157 op1_range.dump (dump_file);
1158 fputc ('\n', dump_file);
1160 // Intersect the calculated result with the known result and return if done.
1161 if (op1 == name)
1163 tmp.intersect (op1_range);
1164 r = tmp;
1165 if (idx)
1166 tracer.trailer (idx, "produces ", true, name, r);
1167 return true;
1169 // If the calculation continues, we're using op1_range as the new LHS.
1170 op1_range.intersect (tmp);
1172 if (idx)
1173 tracer.trailer (idx, "produces ", true, op1, op1_range);
1174 gimple *src_stmt = SSA_NAME_DEF_STMT (op1);
1175 gcc_checking_assert (src_stmt);
1177 // Then feed this range back as the LHS of the defining statement.
1178 return compute_operand_range (r, src_stmt, op1_range, name, src, rel);
1182 // Calculate a range for NAME from the operand 2 position of S
1183 // assuming the result of the statement is LHS. Return the range in
1184 // R, or false if no range could be calculated.
1186 bool
1187 gori_compute::compute_operand2_range (vrange &r,
1188 gimple_range_op_handler &handler,
1189 const vrange &lhs, tree name,
1190 fur_source &src, value_relation *rel)
1192 value_relation local_rel;
1193 gimple *stmt = handler.stmt ();
1194 tree op1 = handler.operand1 ();
1195 tree op2 = handler.operand2 ();
1196 tree lhs_name = gimple_get_lhs (stmt);
1198 Value_Range op1_range (TREE_TYPE (op1));
1199 Value_Range op2_range (TREE_TYPE (op2));
1200 Value_Range tmp (TREE_TYPE (op2));
1202 src.get_operand (op1_range, op1);
1203 src.get_operand (op2_range, op2);
1205 relation_trio trio;
1206 if (rel)
1207 trio = rel->create_trio (lhs_name, op1, op2);
1208 relation_kind op_op = trio.op1_op2 ();
1210 // If there is a relation betwen op1 and op2, use it instead.
1211 // This allows multiple relations to be processed in compound logicals.
1212 if (gimple_range_ssa_p (op1) && gimple_range_ssa_p (op2))
1214 relation_kind k = handler.op1_op2_relation (lhs);
1215 if (k != VREL_VARYING)
1217 op_op = k;
1218 local_rel.set_relation (op_op, op1, op2);
1219 rel = &local_rel;
1223 if (op_op != VREL_VARYING)
1224 refine_using_relation (op1, op1_range, op2, op2_range, src, op_op);
1226 // If op1 == op2, create a new trio for this stmt.
1227 if (op1 == op2 && gimple_range_ssa_p (op1))
1228 trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
1229 // Intersect with range for op2 based on lhs and op1.
1230 if (!handler.calc_op2 (tmp, lhs, op1_range, trio))
1231 return false;
1233 unsigned idx;
1234 if ((idx = tracer.header ("compute op 2 (")))
1236 print_generic_expr (dump_file, op2, TDF_SLIM);
1237 fprintf (dump_file, ") at ");
1238 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1239 tracer.print (idx, "LHS = ");
1240 lhs.dump (dump_file);
1241 if (TREE_CODE (op1) == SSA_NAME)
1243 fprintf (dump_file, ", ");
1244 print_generic_expr (dump_file, op1, TDF_SLIM);
1245 fprintf (dump_file, " = ");
1246 op1_range.dump (dump_file);
1248 fprintf (dump_file, "\n");
1249 tracer.print (idx, "Computes ");
1250 print_generic_expr (dump_file, op2, TDF_SLIM);
1251 fprintf (dump_file, " = ");
1252 tmp.dump (dump_file);
1253 fprintf (dump_file, " intersect Known range : ");
1254 op2_range.dump (dump_file);
1255 fputc ('\n', dump_file);
1257 // Intersect the calculated result with the known result and return if done.
1258 if (op2 == name)
1260 tmp.intersect (op2_range);
1261 r = tmp;
1262 if (idx)
1263 tracer.trailer (idx, " produces ", true, NULL_TREE, r);
1264 return true;
1266 // If the calculation continues, we're using op2_range as the new LHS.
1267 op2_range.intersect (tmp);
1269 if (idx)
1270 tracer.trailer (idx, " produces ", true, op2, op2_range);
1271 gimple *src_stmt = SSA_NAME_DEF_STMT (op2);
1272 gcc_checking_assert (src_stmt);
1273 // gcc_checking_assert (!is_import_p (op2, find.bb));
1275 // Then feed this range back as the LHS of the defining statement.
1276 return compute_operand_range (r, src_stmt, op2_range, name, src, rel);
1279 // Calculate a range for NAME from both operand positions of S
1280 // assuming the result of the statement is LHS. Return the range in
1281 // R, or false if no range could be calculated.
1283 bool
1284 gori_compute::compute_operand1_and_operand2_range (vrange &r,
1285 gimple_range_op_handler
1286 &handler,
1287 const vrange &lhs,
1288 tree name,
1289 fur_source &src,
1290 value_relation *rel)
1292 Value_Range op_range (TREE_TYPE (name));
1294 // Calculate a good a range for op2. Since op1 == op2, this will
1295 // have already included whatever the actual range of name is.
1296 if (!compute_operand2_range (op_range, handler, lhs, name, src, rel))
1297 return false;
1299 // Now get the range thru op1.
1300 if (!compute_operand1_range (r, handler, lhs, name, src, rel))
1301 return false;
1303 // Both operands have to be simultaneously true, so perform an intersection.
1304 r.intersect (op_range);
1305 return true;
1308 // Return TRUE if NAME can be recomputed on any edge exiting BB. If any
1309 // direct dependent is exported, it may also change the computed value of NAME.
1311 bool
1312 gori_compute::may_recompute_p (tree name, basic_block bb, int depth)
1314 tree dep1 = depend1 (name);
1315 tree dep2 = depend2 (name);
1317 // If the first dependency is not set, there is no recomputation.
1318 // Dependencies reflect original IL, not current state. Check if the
1319 // SSA_NAME is still valid as well.
1320 if (!dep1)
1321 return false;
1323 // Don't recalculate PHIs or statements with side_effects.
1324 gimple *s = SSA_NAME_DEF_STMT (name);
1325 if (is_a<gphi *> (s) || gimple_has_side_effects (s))
1326 return false;
1328 if (!dep2)
1330 // -1 indicates a default param, convert it to the real default.
1331 if (depth == -1)
1333 depth = (int)param_ranger_recompute_depth;
1334 gcc_checking_assert (depth >= 1);
1337 bool res = (bb ? is_export_p (dep1, bb) : is_export_p (dep1));
1338 if (res || depth <= 1)
1339 return res;
1340 // Check another level of recomputation.
1341 return may_recompute_p (dep1, bb, --depth);
1343 // Two dependencies terminate the depth of the search.
1344 if (bb)
1345 return is_export_p (dep1, bb) || is_export_p (dep2, bb);
1346 else
1347 return is_export_p (dep1) || is_export_p (dep2);
1350 // Return TRUE if NAME can be recomputed on edge E. If any direct dependent
1351 // is exported on edge E, it may change the computed value of NAME.
1353 bool
1354 gori_compute::may_recompute_p (tree name, edge e, int depth)
1356 gcc_checking_assert (e);
1357 return may_recompute_p (name, e->src, depth);
1361 // Return TRUE if a range can be calculated or recomputed for NAME on any
1362 // edge exiting BB.
1364 bool
1365 gori_compute::has_edge_range_p (tree name, basic_block bb)
1367 // Check if NAME is an export or can be recomputed.
1368 if (bb)
1369 return is_export_p (name, bb) || may_recompute_p (name, bb);
1371 // If no block is specified, check for anywhere in the IL.
1372 return is_export_p (name) || may_recompute_p (name);
1375 // Return TRUE if a range can be calculated or recomputed for NAME on edge E.
1377 bool
1378 gori_compute::has_edge_range_p (tree name, edge e)
1380 gcc_checking_assert (e);
1381 return has_edge_range_p (name, e->src);
1384 // Calculate a range on edge E and return it in R. Try to evaluate a
1385 // range for NAME on this edge. Return FALSE if this is either not a
1386 // control edge or NAME is not defined by this edge.
1388 bool
1389 gori_compute::outgoing_edge_range_p (vrange &r, edge e, tree name,
1390 range_query &q)
1392 unsigned idx;
1394 if ((e->flags & m_not_executable_flag))
1396 r.set_undefined ();
1397 if (dump_file && (dump_flags & TDF_DETAILS))
1398 fprintf (dump_file, "Outgoing edge %d->%d unexecutable.\n",
1399 e->src->index, e->dest->index);
1400 return true;
1403 gcc_checking_assert (gimple_range_ssa_p (name));
1404 int_range_max lhs;
1405 // Determine if there is an outgoing edge.
1406 gimple *stmt = outgoing.edge_range_p (lhs, e);
1407 if (!stmt)
1408 return false;
1410 fur_stmt src (stmt, &q);
1411 // If NAME can be calculated on the edge, use that.
1412 if (is_export_p (name, e->src))
1414 bool res;
1415 if ((idx = tracer.header ("outgoing_edge")))
1417 fprintf (dump_file, " for ");
1418 print_generic_expr (dump_file, name, TDF_SLIM);
1419 fprintf (dump_file, " on edge %d->%d\n",
1420 e->src->index, e->dest->index);
1422 if ((res = compute_operand_range (r, stmt, lhs, name, src)))
1424 // Sometimes compatible types get interchanged. See PR97360.
1425 // Make sure we are returning the type of the thing we asked for.
1426 if (!r.undefined_p () && r.type () != TREE_TYPE (name))
1428 gcc_checking_assert (range_compatible_p (r.type (),
1429 TREE_TYPE (name)));
1430 range_cast (r, TREE_TYPE (name));
1433 if (idx)
1434 tracer.trailer (idx, "outgoing_edge", res, name, r);
1435 return res;
1437 // If NAME isn't exported, check if it can be recomputed.
1438 else if (may_recompute_p (name, e))
1440 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1442 if ((idx = tracer.header ("recomputation")))
1444 fprintf (dump_file, " attempt on edge %d->%d for ",
1445 e->src->index, e->dest->index);
1446 print_gimple_stmt (dump_file, def_stmt, 0, TDF_SLIM);
1448 // Simply calculate DEF_STMT on edge E using the range query Q.
1449 fold_range (r, def_stmt, e, &q);
1450 if (idx)
1451 tracer.trailer (idx, "recomputation", true, name, r);
1452 return true;
1454 return false;
1457 // Given COND ? OP1 : OP2 with ranges R1 for OP1 and R2 for OP2, Use gori
1458 // to further resolve R1 and R2 if there are any dependencies between
1459 // OP1 and COND or OP2 and COND. All values can are to be calculated using SRC
1460 // as the origination source location for operands..
1461 // Effectively, use COND an the edge condition and solve for OP1 on the true
1462 // edge and OP2 on the false edge.
1464 bool
1465 gori_compute::condexpr_adjust (vrange &r1, vrange &r2, gimple *, tree cond,
1466 tree op1, tree op2, fur_source &src)
1468 tree ssa1 = gimple_range_ssa_p (op1);
1469 tree ssa2 = gimple_range_ssa_p (op2);
1470 if (!ssa1 && !ssa2)
1471 return false;
1472 if (TREE_CODE (cond) != SSA_NAME)
1473 return false;
1474 gassign *cond_def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (cond));
1475 if (!cond_def
1476 || TREE_CODE_CLASS (gimple_assign_rhs_code (cond_def)) != tcc_comparison)
1477 return false;
1478 tree type = TREE_TYPE (gimple_assign_rhs1 (cond_def));
1479 if (!range_compatible_p (type, TREE_TYPE (gimple_assign_rhs2 (cond_def))))
1480 return false;
1481 range_op_handler hand (gimple_assign_rhs_code (cond_def), type);
1482 if (!hand)
1483 return false;
1485 tree c1 = gimple_range_ssa_p (gimple_assign_rhs1 (cond_def));
1486 tree c2 = gimple_range_ssa_p (gimple_assign_rhs2 (cond_def));
1488 // Only solve if there is one SSA name in the condition.
1489 if ((!c1 && !c2) || (c1 && c2))
1490 return false;
1492 // Pick up the current values of each part of the condition.
1493 tree rhs1 = gimple_assign_rhs1 (cond_def);
1494 tree rhs2 = gimple_assign_rhs2 (cond_def);
1495 Value_Range cl (TREE_TYPE (rhs1));
1496 Value_Range cr (TREE_TYPE (rhs2));
1497 src.get_operand (cl, rhs1);
1498 src.get_operand (cr, rhs2);
1500 tree cond_name = c1 ? c1 : c2;
1501 gimple *def_stmt = SSA_NAME_DEF_STMT (cond_name);
1503 // Evaluate the value of COND_NAME on the true and false edges, using either
1504 // the op1 or op2 routines based on its location.
1505 Value_Range cond_true (type), cond_false (type);
1506 if (c1)
1508 if (!hand.op1_range (cond_false, type, m_bool_zero, cr))
1509 return false;
1510 if (!hand.op1_range (cond_true, type, m_bool_one, cr))
1511 return false;
1512 cond_false.intersect (cl);
1513 cond_true.intersect (cl);
1515 else
1517 if (!hand.op2_range (cond_false, type, m_bool_zero, cl))
1518 return false;
1519 if (!hand.op2_range (cond_true, type, m_bool_one, cl))
1520 return false;
1521 cond_false.intersect (cr);
1522 cond_true.intersect (cr);
1525 unsigned idx;
1526 if ((idx = tracer.header ("cond_expr evaluation : ")))
1528 fprintf (dump_file, " range1 = ");
1529 r1.dump (dump_file);
1530 fprintf (dump_file, ", range2 = ");
1531 r1.dump (dump_file);
1532 fprintf (dump_file, "\n");
1535 // Now solve for SSA1 or SSA2 if they are in the dependency chain.
1536 if (ssa1 && in_chain_p (ssa1, cond_name))
1538 Value_Range tmp1 (TREE_TYPE (ssa1));
1539 if (compute_operand_range (tmp1, def_stmt, cond_true, ssa1, src))
1540 r1.intersect (tmp1);
1542 if (ssa2 && in_chain_p (ssa2, cond_name))
1544 Value_Range tmp2 (TREE_TYPE (ssa2));
1545 if (compute_operand_range (tmp2, def_stmt, cond_false, ssa2, src))
1546 r2.intersect (tmp2);
1548 if (idx)
1550 tracer.print (idx, "outgoing: range1 = ");
1551 r1.dump (dump_file);
1552 fprintf (dump_file, ", range2 = ");
1553 r1.dump (dump_file);
1554 fprintf (dump_file, "\n");
1555 tracer.trailer (idx, "cond_expr", true, cond_name, cond_true);
1557 return true;
1560 // Dump what is known to GORI computes to listing file F.
1562 void
1563 gori_compute::dump (FILE *f)
1565 gori_map::dump (f);
1568 // ------------------------------------------------------------------------
1569 // GORI iterator. Although we have bitmap iterators, don't expose that it
1570 // is currently a bitmap. Use an export iterator to hide future changes.
1572 // Construct a basic iterator over an export bitmap.
1574 gori_export_iterator::gori_export_iterator (bitmap b)
1576 bm = b;
1577 if (b)
1578 bmp_iter_set_init (&bi, b, 1, &y);
1582 // Move to the next export bitmap spot.
1584 void
1585 gori_export_iterator::next ()
1587 bmp_iter_next (&bi, &y);
1591 // Fetch the name of the next export in the export list. Return NULL if
1592 // iteration is done.
1594 tree
1595 gori_export_iterator::get_name ()
1597 if (!bm)
1598 return NULL_TREE;
1600 while (bmp_iter_set (&bi, &y))
1602 tree t = ssa_name (y);
1603 if (t)
1604 return t;
1605 next ();
1607 return NULL_TREE;