Fix typo in t-dimode
[official-gcc.git] / gcc / gimple-range-gori.cc
blob911d7ac4ec8be83f2d810cd2781b28bbf5b9358d
1 /* Gimple range GORI functions.
2 Copyright (C) 2017-2021 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 // Calculate what we can determine of the range of this unary
33 // statement's operand if the lhs of the expression has the range
34 // LHS_RANGE. Return false if nothing can be determined.
36 bool
37 gimple_range_calc_op1 (irange &r, const gimple *stmt, const irange &lhs_range)
39 gcc_checking_assert (gimple_num_ops (stmt) < 3);
40 // Give up on empty ranges.
41 if (lhs_range.undefined_p ())
42 return false;
44 // Unary operations require the type of the first operand in the
45 // second range position.
46 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
47 int_range<2> type_range (type);
48 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
49 type_range);
52 // Calculate what we can determine of the range of this statement's
53 // first operand if the lhs of the expression has the range LHS_RANGE
54 // and the second operand has the range OP2_RANGE. Return false if
55 // nothing can be determined.
57 bool
58 gimple_range_calc_op1 (irange &r, const gimple *stmt,
59 const irange &lhs_range, const irange &op2_range)
61 // Give up on empty ranges.
62 if (lhs_range.undefined_p ())
63 return false;
65 // Unary operation are allowed to pass a range in for second operand
66 // as there are often additional restrictions beyond the type which
67 // can be imposed. See operator_cast::op1_range().
68 tree type = TREE_TYPE (gimple_range_operand1 (stmt));
69 // If op2 is undefined, solve as if it is varying.
70 if (op2_range.undefined_p ())
72 // This is sometimes invoked on single operand stmts.
73 if (gimple_num_ops (stmt) < 3)
74 return false;
75 int_range<2> trange (TREE_TYPE (gimple_range_operand2 (stmt)));
76 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
77 trange);
79 return gimple_range_handler (stmt)->op1_range (r, type, lhs_range,
80 op2_range);
83 // Calculate what we can determine of the range of this statement's
84 // second operand if the lhs of the expression has the range LHS_RANGE
85 // and the first operand has the range OP1_RANGE. Return false if
86 // nothing can be determined.
88 bool
89 gimple_range_calc_op2 (irange &r, const gimple *stmt,
90 const irange &lhs_range, const irange &op1_range)
92 // Give up on empty ranges.
93 if (lhs_range.undefined_p ())
94 return false;
96 tree type = TREE_TYPE (gimple_range_operand2 (stmt));
97 // If op1 is undefined, solve as if it is varying.
98 if (op1_range.undefined_p ())
100 int_range<2> trange (TREE_TYPE (gimple_range_operand1 (stmt)));
101 return gimple_range_handler (stmt)->op2_range (r, type, lhs_range,
102 trange);
104 return gimple_range_handler (stmt)->op2_range (r, type, lhs_range,
105 op1_range);
108 // Return TRUE if GS is a logical && or || expression.
110 static inline bool
111 is_gimple_logical_p (const gimple *gs)
113 // Look for boolean and/or condition.
114 if (is_gimple_assign (gs))
115 switch (gimple_expr_code (gs))
117 case TRUTH_AND_EXPR:
118 case TRUTH_OR_EXPR:
119 return true;
121 case BIT_AND_EXPR:
122 case BIT_IOR_EXPR:
123 // Bitwise operations on single bits are logical too.
124 if (types_compatible_p (TREE_TYPE (gimple_assign_rhs1 (gs)),
125 boolean_type_node))
126 return true;
127 break;
129 default:
130 break;
132 return false;
135 /* RANGE_DEF_CHAIN is used to determine which SSA names in a block can
136 have range information calculated for them, and what the
137 dependencies on each other are.
139 Information for a basic block is calculated once and stored. It is
140 only calculated the first time a query is made, so if no queries
141 are made, there is little overhead.
143 The def_chain bitmap is indexed by SSA_NAME_VERSION. Bits are set
144 within this bitmap to indicate SSA names that are defined in the
145 SAME block and used to calculate this SSA name.
148 <bb 2> :
149 _1 = x_4(D) + -2;
150 _2 = _1 * 4;
151 j_7 = foo ();
152 q_5 = _2 + 3;
153 if (q_5 <= 13)
155 _1 : x_4(D)
156 _2 : 1 x_4(D)
157 q_5 : _1 _2 x_4(D)
159 This dump indicates the bits set in the def_chain vector.
160 as well as demonstrates the def_chain bits for the related ssa_names.
162 Checking the chain for _2 indicates that _1 and x_4 are used in
163 its evaluation.
165 Def chains also only include statements which are valid gimple
166 so a def chain will only span statements for which the range
167 engine implements operations for. */
170 // Construct a range_def_chain.
172 range_def_chain::range_def_chain ()
174 bitmap_obstack_initialize (&m_bitmaps);
175 m_def_chain.create (0);
176 m_def_chain.safe_grow_cleared (num_ssa_names);
177 m_logical_depth = 0;
180 // Destruct a range_def_chain.
182 range_def_chain::~range_def_chain ()
184 m_def_chain.release ();
185 bitmap_obstack_release (&m_bitmaps);
188 // Return true if NAME is in the def chain of DEF. If BB is provided,
189 // only return true if the defining statement of DEF is in BB.
191 bool
192 range_def_chain::in_chain_p (tree name, tree def)
194 gcc_checking_assert (gimple_range_ssa_p (def));
195 gcc_checking_assert (gimple_range_ssa_p (name));
197 // Get the defintion chain for DEF.
198 bitmap chain = get_def_chain (def);
200 if (chain == NULL)
201 return false;
202 return bitmap_bit_p (chain, SSA_NAME_VERSION (name));
205 // Add either IMP or the import list B to the import set of DATA.
207 void
208 range_def_chain::set_import (struct rdc &data, tree imp, bitmap b)
210 // If there are no imports, just return
211 if (imp == NULL_TREE && !b)
212 return;
213 if (!data.m_import)
214 data.m_import = BITMAP_ALLOC (&m_bitmaps);
215 if (imp != NULL_TREE)
216 bitmap_set_bit (data.m_import, SSA_NAME_VERSION (imp));
217 else
218 bitmap_ior_into (data.m_import, b);
221 // Return the import list for NAME.
223 bitmap
224 range_def_chain::get_imports (tree name)
226 if (!has_def_chain (name))
227 get_def_chain (name);
228 bitmap i = m_def_chain[SSA_NAME_VERSION (name)].m_import;
229 return i;
232 // Return true if IMPORT is an import to NAMEs def chain.
234 bool
235 range_def_chain::chain_import_p (tree name, tree import)
237 bitmap b = get_imports (name);
238 if (b)
239 return bitmap_bit_p (b, SSA_NAME_VERSION (import));
240 return false;
243 // Build def_chains for NAME if it is in BB. Copy the def chain into RESULT.
245 void
246 range_def_chain::register_dependency (tree name, tree dep, basic_block bb)
248 if (!gimple_range_ssa_p (dep))
249 return;
251 unsigned v = SSA_NAME_VERSION (name);
252 if (v >= m_def_chain.length ())
253 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
254 struct rdc &src = m_def_chain[v];
255 gimple *def_stmt = SSA_NAME_DEF_STMT (dep);
256 unsigned dep_v = SSA_NAME_VERSION (dep);
257 bitmap b;
259 // Set the direct dependency cache entries.
260 if (!src.ssa1)
261 src.ssa1 = dep;
262 else if (!src.ssa2 && src.ssa1 != dep)
263 src.ssa2 = dep;
265 // Don't calculate imports or export/dep chains if BB is not provided.
266 // This is usually the case for when the temporal cache wants the direct
267 // dependencies of a stmt.
268 if (!bb)
269 return;
271 if (!src.bm)
272 src.bm = BITMAP_ALLOC (&m_bitmaps);
274 // Add this operand into the result.
275 bitmap_set_bit (src.bm, dep_v);
277 if (gimple_bb (def_stmt) == bb && !is_a<gphi *>(def_stmt))
279 // Get the def chain for the operand.
280 b = get_def_chain (dep);
281 // If there was one, copy it into result.
282 if (b)
283 bitmap_ior_into (src.bm, b);
284 // And copy the import list.
285 set_import (src, NULL_TREE, get_imports (dep));
287 else
288 // Originated outside the block, so it is an import.
289 set_import (src, dep, NULL);
292 bool
293 range_def_chain::def_chain_in_bitmap_p (tree name, bitmap b)
295 bitmap a = get_def_chain (name);
296 if (a && b)
297 return bitmap_intersect_p (a, b);
298 return false;
301 void
302 range_def_chain::add_def_chain_to_bitmap (bitmap b, tree name)
304 bitmap r = get_def_chain (name);
305 if (r)
306 bitmap_ior_into (b, r);
310 // Return TRUE if NAME has been processed for a def_chain.
312 inline bool
313 range_def_chain::has_def_chain (tree name)
315 // Ensure there is an entry in the internal vector.
316 unsigned v = SSA_NAME_VERSION (name);
317 if (v >= m_def_chain.length ())
318 m_def_chain.safe_grow_cleared (num_ssa_names + 1);
319 return (m_def_chain[v].ssa1 != 0);
324 // Calculate the def chain for NAME and all of its dependent
325 // operands. Only using names in the same BB. Return the bitmap of
326 // all names in the m_def_chain. This only works for supported range
327 // statements.
329 bitmap
330 range_def_chain::get_def_chain (tree name)
332 tree ssa1, ssa2, ssa3;
333 unsigned v = SSA_NAME_VERSION (name);
335 // If it has already been processed, just return the cached value.
336 if (has_def_chain (name))
337 return m_def_chain[v].bm;
339 // No definition chain for default defs.
340 if (SSA_NAME_IS_DEFAULT_DEF (name))
342 // A Default def is always an import.
343 set_import (m_def_chain[v], name, NULL);
344 return NULL;
347 gimple *stmt = SSA_NAME_DEF_STMT (name);
348 if (gimple_range_handler (stmt))
350 ssa1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
351 ssa2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
352 ssa3 = NULL_TREE;
354 else if (is_a<gassign *> (stmt)
355 && gimple_assign_rhs_code (stmt) == COND_EXPR)
357 gassign *st = as_a<gassign *> (stmt);
358 ssa1 = gimple_range_ssa_p (gimple_assign_rhs1 (st));
359 ssa2 = gimple_range_ssa_p (gimple_assign_rhs2 (st));
360 ssa3 = gimple_range_ssa_p (gimple_assign_rhs3 (st));
362 else
364 // Stmts not understood are always imports.
365 set_import (m_def_chain[v], name, NULL);
366 return NULL;
369 // Terminate the def chains if we see too many cascading stmts.
370 if (m_logical_depth == param_ranger_logical_depth)
371 return NULL;
373 // Increase the depth if we have a pair of ssa-names.
374 if (ssa1 && ssa2)
375 m_logical_depth++;
377 register_dependency (name, ssa1, gimple_bb (stmt));
378 register_dependency (name, ssa2, gimple_bb (stmt));
379 register_dependency (name, ssa3, gimple_bb (stmt));
380 // Stmts with no understandable operands are also imports.
381 if (!ssa1 && !ssa2 & !ssa3)
382 set_import (m_def_chain[v], name, NULL);
384 if (ssa1 && ssa2)
385 m_logical_depth--;
387 return m_def_chain[v].bm;
390 // Dump what we know for basic block BB to file F.
392 void
393 range_def_chain::dump (FILE *f, basic_block bb, const char *prefix)
395 unsigned x, y;
396 bitmap_iterator bi;
398 // Dump the def chain for each SSA_NAME defined in BB.
399 for (x = 1; x < num_ssa_names; x++)
401 tree name = ssa_name (x);
402 if (!name)
403 continue;
404 gimple *stmt = SSA_NAME_DEF_STMT (name);
405 if (!stmt || (bb && gimple_bb (stmt) != bb))
406 continue;
407 bitmap chain = (has_def_chain (name) ? get_def_chain (name) : NULL);
408 if (chain && !bitmap_empty_p (chain))
410 fprintf (f, prefix);
411 print_generic_expr (f, name, TDF_SLIM);
412 fprintf (f, " : ");
414 bitmap imports = get_imports (name);
415 EXECUTE_IF_SET_IN_BITMAP (chain, 0, y, bi)
417 print_generic_expr (f, ssa_name (y), TDF_SLIM);
418 if (imports && bitmap_bit_p (imports, y))
419 fprintf (f, "(I)");
420 fprintf (f, " ");
422 fprintf (f, "\n");
428 // -------------------------------------------------------------------
430 /* GORI_MAP is used to accumulate what SSA names in a block can
431 generate range information, and provides tools for the block ranger
432 to enable it to efficiently calculate these ranges.
434 GORI stands for "Generates Outgoing Range Information."
436 It utilizes the range_def_chain class to contruct def_chains.
437 Information for a basic block is calculated once and stored. It is
438 only calculated the first time a query is made. If no queries are
439 made, there is little overhead.
441 one bitmap is maintained for each basic block:
442 m_outgoing : a set bit indicates a range can be generated for a name.
444 Generally speaking, the m_outgoing vector is the union of the
445 entire def_chain of all SSA names used in the last statement of the
446 block which generate ranges. */
449 // Initialize a gori-map structure.
451 gori_map::gori_map ()
453 m_outgoing.create (0);
454 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
455 m_incoming.create (0);
456 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
457 m_maybe_variant = BITMAP_ALLOC (&m_bitmaps);
460 // Free any memory the GORI map allocated.
462 gori_map::~gori_map ()
464 m_incoming.release ();
465 m_outgoing.release ();
468 // Return the bitmap vector of all export from BB. Calculate if necessary.
470 bitmap
471 gori_map::exports (basic_block bb)
473 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
474 calculate_gori (bb);
475 return m_outgoing[bb->index];
478 // Return the bitmap vector of all imports to BB. Calculate if necessary.
480 bitmap
481 gori_map::imports (basic_block bb)
483 if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
484 calculate_gori (bb);
485 return m_incoming[bb->index];
488 // Return true if NAME is can have ranges generated for it from basic
489 // block BB.
491 bool
492 gori_map::is_export_p (tree name, basic_block bb)
494 // If no BB is specified, test if it is exported anywhere in the IL.
495 if (!bb)
496 return bitmap_bit_p (m_maybe_variant, SSA_NAME_VERSION (name));
497 return bitmap_bit_p (exports (bb), SSA_NAME_VERSION (name));
500 // Clear the m_maybe_variant bit so ranges will not be tracked for NAME.
502 void
503 gori_map::set_range_invariant (tree name)
505 bitmap_clear_bit (m_maybe_variant, SSA_NAME_VERSION (name));
508 // Return true if NAME is an import to block BB.
510 bool
511 gori_map::is_import_p (tree name, basic_block bb)
513 // If no BB is specified, test if it is exported anywhere in the IL.
514 return bitmap_bit_p (imports (bb), SSA_NAME_VERSION (name));
517 // If NAME is non-NULL and defined in block BB, calculate the def
518 // chain and add it to m_outgoing.
520 void
521 gori_map::maybe_add_gori (tree name, basic_block bb)
523 if (name)
525 // Check if there is a def chain, regardless of the block.
526 add_def_chain_to_bitmap (m_outgoing[bb->index], name);
527 // Check for any imports.
528 bitmap imp = get_imports (name);
529 // If there were imports, add them so we can recompute
530 if (imp)
531 bitmap_ior_into (m_incoming[bb->index], imp);
532 // This name is always an import.
533 if (gimple_bb (SSA_NAME_DEF_STMT (name)) != bb)
534 bitmap_set_bit (m_incoming[bb->index], SSA_NAME_VERSION (name));
536 // Def chain doesn't include itself, and even if there isn't a
537 // def chain, this name should be added to exports.
538 bitmap_set_bit (m_outgoing[bb->index], SSA_NAME_VERSION (name));
542 // Calculate all the required information for BB.
544 void
545 gori_map::calculate_gori (basic_block bb)
547 tree name;
548 if (bb->index >= (signed int)m_outgoing.length ())
550 m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
551 m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
553 gcc_checking_assert (m_outgoing[bb->index] == NULL);
554 m_outgoing[bb->index] = BITMAP_ALLOC (&m_bitmaps);
555 m_incoming[bb->index] = BITMAP_ALLOC (&m_bitmaps);
557 // If this block's last statement may generate range informaiton, go
558 // calculate it.
559 gimple *stmt = gimple_outgoing_range_stmt_p (bb);
560 if (!stmt)
561 return;
562 if (is_a<gcond *> (stmt))
564 gcond *gc = as_a<gcond *>(stmt);
565 name = gimple_range_ssa_p (gimple_cond_lhs (gc));
566 maybe_add_gori (name, gimple_bb (stmt));
568 name = gimple_range_ssa_p (gimple_cond_rhs (gc));
569 maybe_add_gori (name, gimple_bb (stmt));
571 else
573 // Do not process switches if they are too large.
574 if (EDGE_COUNT (bb->succs) > (unsigned)param_evrp_switch_limit)
575 return;
576 gswitch *gs = as_a<gswitch *>(stmt);
577 name = gimple_range_ssa_p (gimple_switch_index (gs));
578 maybe_add_gori (name, gimple_bb (stmt));
580 // Add this bitmap to the aggregate list of all outgoing names.
581 bitmap_ior_into (m_maybe_variant, m_outgoing[bb->index]);
584 // Dump the table information for BB to file F.
586 void
587 gori_map::dump (FILE *f, basic_block bb, bool verbose)
589 // BB was not processed.
590 if (!m_outgoing[bb->index] || bitmap_empty_p (m_outgoing[bb->index]))
591 return;
593 tree name;
595 bitmap imp = imports (bb);
596 if (!bitmap_empty_p (imp))
598 if (verbose)
599 fprintf (f, "bb<%u> Imports: ",bb->index);
600 else
601 fprintf (f, "Imports: ");
602 FOR_EACH_GORI_IMPORT_NAME (*this, bb, name)
604 print_generic_expr (f, name, TDF_SLIM);
605 fprintf (f, " ");
607 fputc ('\n', f);
610 if (verbose)
611 fprintf (f, "bb<%u> Exports: ",bb->index);
612 else
613 fprintf (f, "Exports: ");
614 // Dump the export vector.
615 FOR_EACH_GORI_EXPORT_NAME (*this, bb, name)
617 print_generic_expr (f, name, TDF_SLIM);
618 fprintf (f, " ");
620 fputc ('\n', f);
622 range_def_chain::dump (f, bb, " ");
625 // Dump the entire GORI map structure to file F.
627 void
628 gori_map::dump (FILE *f)
630 basic_block bb;
631 FOR_EACH_BB_FN (bb, cfun)
632 dump (f, bb);
635 DEBUG_FUNCTION void
636 debug (gori_map &g)
638 g.dump (stderr);
641 // -------------------------------------------------------------------
643 // Construct a gori_compute object.
645 gori_compute::gori_compute (int not_executable_flag)
646 : outgoing (param_evrp_switch_limit), tracer ("GORI ")
648 m_not_executable_flag = not_executable_flag;
649 // Create a boolean_type true and false range.
650 m_bool_zero = int_range<2> (boolean_false_node, boolean_false_node);
651 m_bool_one = int_range<2> (boolean_true_node, boolean_true_node);
652 if (dump_file && (param_ranger_debug & RANGER_DEBUG_GORI))
653 tracer.enable_trace ();
656 // Given the switch S, return an evaluation in R for NAME when the lhs
657 // evaluates to LHS. Returning false means the name being looked for
658 // was not resolvable.
660 bool
661 gori_compute::compute_operand_range_switch (irange &r, gswitch *s,
662 const irange &lhs,
663 tree name, fur_source &src)
665 tree op1 = gimple_switch_index (s);
667 // If name matches, the range is simply the range from the edge.
668 // Empty ranges are viral as they are on a path which isn't
669 // executable.
670 if (op1 == name || lhs.undefined_p ())
672 r = lhs;
673 return true;
676 // If op1 is in the defintion chain, pass lhs back.
677 if (gimple_range_ssa_p (op1) && in_chain_p (name, op1))
678 return compute_operand_range (r, SSA_NAME_DEF_STMT (op1), lhs, name, src);
680 return false;
684 // Return an evaluation for NAME as it would appear in STMT when the
685 // statement's lhs evaluates to LHS. If successful, return TRUE and
686 // store the evaluation in R, otherwise return FALSE.
688 bool
689 gori_compute::compute_operand_range (irange &r, gimple *stmt,
690 const irange &lhs, tree name,
691 fur_source &src)
693 // If the lhs doesn't tell us anything, neither will unwinding further.
694 if (lhs.varying_p ())
695 return false;
697 // Empty ranges are viral as they are on an unexecutable path.
698 if (lhs.undefined_p ())
700 r.set_undefined ();
701 return true;
703 if (is_a<gswitch *> (stmt))
704 return compute_operand_range_switch (r, as_a<gswitch *> (stmt), lhs, name,
705 src);
706 if (!gimple_range_handler (stmt))
707 return false;
709 tree op1 = gimple_range_ssa_p (gimple_range_operand1 (stmt));
710 tree op2 = gimple_range_ssa_p (gimple_range_operand2 (stmt));
712 // Handle end of lookup first.
713 if (op1 == name)
714 return compute_operand1_range (r, stmt, lhs, name, src);
715 if (op2 == name)
716 return compute_operand2_range (r, stmt, lhs, name, src);
718 // NAME is not in this stmt, but one of the names in it ought to be
719 // derived from it.
720 bool op1_in_chain = op1 && in_chain_p (name, op1);
721 bool op2_in_chain = op2 && in_chain_p (name, op2);
723 // If neither operand is derived, then this stmt tells us nothing.
724 if (!op1_in_chain && !op2_in_chain)
725 return false;
727 bool res;
728 // Process logicals as they have special handling.
729 if (is_gimple_logical_p (stmt))
731 unsigned idx;
732 if ((idx = tracer.header ("compute_operand ")))
734 print_generic_expr (dump_file, name, TDF_SLIM);
735 fprintf (dump_file, " with LHS = ");
736 lhs.dump (dump_file);
737 fprintf (dump_file, " at stmt ");
738 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
741 int_range_max op1_trange, op1_frange;
742 int_range_max op2_trange, op2_frange;
743 compute_logical_operands (op1_trange, op1_frange, stmt, lhs,
744 name, src, op1, op1_in_chain);
745 compute_logical_operands (op2_trange, op2_frange, stmt, lhs,
746 name, src, op2, op2_in_chain);
747 res = logical_combine (r, gimple_expr_code (stmt), lhs,
748 op1_trange, op1_frange, op2_trange, op2_frange);
749 if (idx)
750 tracer.trailer (idx, "compute_operand", res, name, r);
752 // Follow the appropriate operands now.
753 else if (op1_in_chain && op2_in_chain)
754 res = compute_operand1_and_operand2_range (r, stmt, lhs, name, src);
755 else if (op1_in_chain)
756 res = compute_operand1_range (r, stmt, lhs, name, src);
757 else if (op2_in_chain)
758 res = compute_operand2_range (r, stmt, lhs, name, src);
759 else
760 gcc_unreachable ();
762 // If neither operand is derived, this statement tells us nothing.
763 return res;
767 // Return TRUE if range R is either a true or false compatible range.
769 static bool
770 range_is_either_true_or_false (const irange &r)
772 if (r.undefined_p ())
773 return false;
775 // This is complicated by the fact that Ada has multi-bit booleans,
776 // so true can be ~[0, 0] (i.e. [1,MAX]).
777 tree type = r.type ();
778 gcc_checking_assert (range_compatible_p (type, boolean_type_node));
779 return (r.singleton_p () || !r.contains_p (build_zero_cst (type)));
782 // Evaluate a binary logical expression by combining the true and
783 // false ranges for each of the operands based on the result value in
784 // the LHS.
786 bool
787 gori_compute::logical_combine (irange &r, enum tree_code code,
788 const irange &lhs,
789 const irange &op1_true, const irange &op1_false,
790 const irange &op2_true, const irange &op2_false)
792 if (op1_true.varying_p () && op1_false.varying_p ()
793 && op2_true.varying_p () && op2_false.varying_p ())
794 return false;
796 unsigned idx;
797 if ((idx = tracer.header ("logical_combine")))
799 switch (code)
801 case TRUTH_OR_EXPR:
802 case BIT_IOR_EXPR:
803 fprintf (dump_file, " || ");
804 break;
805 case TRUTH_AND_EXPR:
806 case BIT_AND_EXPR:
807 fprintf (dump_file, " && ");
808 break;
809 default:
810 break;
812 fprintf (dump_file, " with LHS = ");
813 lhs.dump (dump_file);
814 fputc ('\n', dump_file);
816 tracer.print (idx, "op1_true = ");
817 op1_true.dump (dump_file);
818 fprintf (dump_file, " op1_false = ");
819 op1_false.dump (dump_file);
820 fputc ('\n', dump_file);
821 tracer.print (idx, "op2_true = ");
822 op2_true.dump (dump_file);
823 fprintf (dump_file, " op2_false = ");
824 op2_false.dump (dump_file);
825 fputc ('\n', dump_file);
828 // This is not a simple fold of a logical expression, rather it
829 // determines ranges which flow through the logical expression.
831 // Assuming x_8 is an unsigned char, and relational statements:
832 // b_1 = x_8 < 20
833 // b_2 = x_8 > 5
834 // consider the logical expression and branch:
835 // c_2 = b_1 && b_2
836 // if (c_2)
838 // To determine the range of x_8 on either edge of the branch, one
839 // must first determine what the range of x_8 is when the boolean
840 // values of b_1 and b_2 are both true and false.
841 // b_1 TRUE x_8 = [0, 19]
842 // b_1 FALSE x_8 = [20, 255]
843 // b_2 TRUE x_8 = [6, 255]
844 // b_2 FALSE x_8 = [0,5].
846 // These ranges are then combined based on the expected outcome of
847 // the branch. The range on the TRUE side of the branch must satisfy
848 // b_1 == true && b_2 == true
850 // In terms of x_8, that means both x_8 == [0, 19] and x_8 = [6, 255]
851 // must be true. The range of x_8 on the true side must be the
852 // intersection of both ranges since both must be true. Thus the
853 // range of x_8 on the true side is [6, 19].
855 // To determine the ranges on the FALSE side, all 3 combinations of
856 // failing ranges must be considered, and combined as any of them
857 // can cause the false result.
859 // If the LHS can be TRUE or FALSE, then evaluate both a TRUE and
860 // FALSE results and combine them. If we fell back to VARYING any
861 // range restrictions that have been discovered up to this point
862 // would be lost.
863 if (!range_is_either_true_or_false (lhs))
865 bool res;
866 int_range_max r1;
867 if (logical_combine (r1, code, m_bool_zero, op1_true, op1_false,
868 op2_true, op2_false)
869 && logical_combine (r, code, m_bool_one, op1_true, op1_false,
870 op2_true, op2_false))
872 r.union_ (r1);
873 res = true;
875 else
876 res = false;
877 if (idx)
878 tracer.trailer (idx, "logical_combine", res, NULL_TREE, r);
881 switch (code)
883 // A logical AND combines ranges from 2 boolean conditions.
884 // c_2 = b_1 && b_2
885 case TRUTH_AND_EXPR:
886 case BIT_AND_EXPR:
887 if (!lhs.zero_p ())
889 // The TRUE side is the intersection of the the 2 true ranges.
890 r = op1_true;
891 r.intersect (op2_true);
893 else
895 // The FALSE side is the union of the other 3 cases.
896 int_range_max ff (op1_false);
897 ff.intersect (op2_false);
898 int_range_max tf (op1_true);
899 tf.intersect (op2_false);
900 int_range_max ft (op1_false);
901 ft.intersect (op2_true);
902 r = ff;
903 r.union_ (tf);
904 r.union_ (ft);
906 break;
907 // A logical OR combines ranges from 2 boolean conditons.
908 // c_2 = b_1 || b_2
909 case TRUTH_OR_EXPR:
910 case BIT_IOR_EXPR:
911 if (lhs.zero_p ())
913 // An OR operation will only take the FALSE path if both
914 // operands are false simlulateously, which means they should
915 // be intersected. !(x || y) == !x && !y
916 r = op1_false;
917 r.intersect (op2_false);
919 else
921 // The TRUE side of an OR operation will be the union of
922 // the other three combinations.
923 int_range_max tt (op1_true);
924 tt.intersect (op2_true);
925 int_range_max tf (op1_true);
926 tf.intersect (op2_false);
927 int_range_max ft (op1_false);
928 ft.intersect (op2_true);
929 r = tt;
930 r.union_ (tf);
931 r.union_ (ft);
933 break;
934 default:
935 gcc_unreachable ();
938 if (idx)
939 tracer.trailer (idx, "logical_combine", true, NULL_TREE, r);
940 return true;
944 // Given a logical STMT, calculate true and false ranges for each
945 // potential path of NAME, assuming NAME came through the OP chain if
946 // OP_IN_CHAIN is true.
948 void
949 gori_compute::compute_logical_operands (irange &true_range, irange &false_range,
950 gimple *stmt,
951 const irange &lhs,
952 tree name, fur_source &src,
953 tree op, bool op_in_chain)
955 gimple *src_stmt = gimple_range_ssa_p (op) ? SSA_NAME_DEF_STMT (op) : NULL;
956 if (!op_in_chain || !src_stmt || chain_import_p (gimple_get_lhs (stmt), op))
958 // If op is not in the def chain, or defined in this block,
959 // use its known value on entry to the block.
960 src.get_operand (true_range, name);
961 false_range = true_range;
962 unsigned idx;
963 if ((idx = tracer.header ("logical_operand")))
965 print_generic_expr (dump_file, op, TDF_SLIM);
966 fprintf (dump_file, " not in computation chain. Queried.\n");
967 tracer.trailer (idx, "logical_operand", true, NULL_TREE, true_range);
969 return;
972 enum tree_code code = gimple_expr_code (stmt);
973 // Optimize [0 = x | y], since neither operand can ever be non-zero.
974 if ((code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) && lhs.zero_p ())
976 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name,
977 src))
978 src.get_operand (false_range, name);
979 true_range = false_range;
980 return;
983 // Optimize [1 = x & y], since neither operand can ever be zero.
984 if ((code == BIT_AND_EXPR || code == TRUTH_AND_EXPR) && lhs == m_bool_one)
986 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
987 src.get_operand (true_range, name);
988 false_range = true_range;
989 return;
992 // Calculate ranges for true and false on both sides, since the false
993 // path is not always a simple inversion of the true side.
994 if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
995 src.get_operand (true_range, name);
996 if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name, src))
997 src.get_operand (false_range, name);
1000 // Calculate a range for NAME from the operand 1 position of STMT
1001 // assuming the result of the statement is LHS. Return the range in
1002 // R, or false if no range could be calculated.
1004 bool
1005 gori_compute::compute_operand1_range (irange &r, gimple *stmt,
1006 const irange &lhs, tree name,
1007 fur_source &src)
1009 int_range_max op1_range, op2_range;
1010 tree op1 = gimple_range_operand1 (stmt);
1011 tree op2 = gimple_range_operand2 (stmt);
1013 // Fetch the known range for op1 in this block.
1014 src.get_operand (op1_range, op1);
1016 // Now range-op calcuate and put that result in r.
1017 if (op2)
1019 src.get_operand (op2_range, op2);
1020 if (!gimple_range_calc_op1 (r, stmt, lhs, op2_range))
1021 return false;
1023 else
1025 // We pass op1_range to the unary operation. Nomally it's a
1026 // hidden range_for_type parameter, but sometimes having the
1027 // actual range can result in better information.
1028 if (!gimple_range_calc_op1 (r, stmt, lhs, op1_range))
1029 return false;
1032 unsigned idx;
1033 if ((idx = tracer.header ("compute op 1 (")))
1035 print_generic_expr (dump_file, op1, TDF_SLIM);
1036 fprintf (dump_file, ") at ");
1037 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1038 tracer.print (idx, "LHS =");
1039 lhs.dump (dump_file);
1040 if (op2 && TREE_CODE (op2) == SSA_NAME)
1042 fprintf (dump_file, ", ");
1043 print_generic_expr (dump_file, op2, TDF_SLIM);
1044 fprintf (dump_file, " = ");
1045 op2_range.dump (dump_file);
1047 fprintf (dump_file, "\n");
1048 tracer.print (idx, "Computes ");
1049 print_generic_expr (dump_file, op1, TDF_SLIM);
1050 fprintf (dump_file, " = ");
1051 r.dump (dump_file);
1052 fprintf (dump_file, " intersect Known range : ");
1053 op1_range.dump (dump_file);
1054 fputc ('\n', dump_file);
1056 // Intersect the calculated result with the known result and return if done.
1057 if (op1 == name)
1059 r.intersect (op1_range);
1060 if (idx)
1061 tracer.trailer (idx, "produces ", true, name, r);
1062 return true;
1064 // If the calculation continues, we're using op1_range as the new LHS.
1065 op1_range.intersect (r);
1067 if (idx)
1068 tracer.trailer (idx, "produces ", true, op1, op1_range);
1069 gimple *src_stmt = SSA_NAME_DEF_STMT (op1);
1070 gcc_checking_assert (src_stmt);
1072 // Then feed this range back as the LHS of the defining statement.
1073 return compute_operand_range (r, src_stmt, op1_range, name, src);
1077 // Calculate a range for NAME from the operand 2 position of S
1078 // assuming the result of the statement is LHS. Return the range in
1079 // R, or false if no range could be calculated.
1081 bool
1082 gori_compute::compute_operand2_range (irange &r, gimple *stmt,
1083 const irange &lhs, tree name,
1084 fur_source &src)
1086 int_range_max op1_range, op2_range;
1087 tree op1 = gimple_range_operand1 (stmt);
1088 tree op2 = gimple_range_operand2 (stmt);
1090 src.get_operand (op1_range, op1);
1091 src.get_operand (op2_range, op2);
1093 // Intersect with range for op2 based on lhs and op1.
1094 if (!gimple_range_calc_op2 (r, stmt, lhs, op1_range))
1095 return false;
1097 unsigned idx;
1098 if ((idx = tracer.header ("compute op 2 (")))
1100 print_generic_expr (dump_file, op2, TDF_SLIM);
1101 fprintf (dump_file, ") at ");
1102 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1103 tracer.print (idx, "LHS = ");
1104 lhs.dump (dump_file);
1105 if (TREE_CODE (op1) == SSA_NAME)
1107 fprintf (dump_file, ", ");
1108 print_generic_expr (dump_file, op1, TDF_SLIM);
1109 fprintf (dump_file, " = ");
1110 op1_range.dump (dump_file);
1112 fprintf (dump_file, "\n");
1113 tracer.print (idx, "Computes ");
1114 print_generic_expr (dump_file, op2, TDF_SLIM);
1115 fprintf (dump_file, " = ");
1116 r.dump (dump_file);
1117 fprintf (dump_file, " intersect Known range : ");
1118 op2_range.dump (dump_file);
1119 fputc ('\n', dump_file);
1121 // Intersect the calculated result with the known result and return if done.
1122 if (op2 == name)
1124 r.intersect (op2_range);
1125 if (idx)
1126 tracer.trailer (idx, " produces ", true, NULL_TREE, r);
1127 return true;
1129 // If the calculation continues, we're using op2_range as the new LHS.
1130 op2_range.intersect (r);
1132 if (idx)
1133 tracer.trailer (idx, " produces ", true, op2, op2_range);
1134 gimple *src_stmt = SSA_NAME_DEF_STMT (op2);
1135 gcc_checking_assert (src_stmt);
1136 // gcc_checking_assert (!is_import_p (op2, find.bb));
1138 // Then feed this range back as the LHS of the defining statement.
1139 return compute_operand_range (r, src_stmt, op2_range, name, src);
1142 // Calculate a range for NAME from both operand positions of S
1143 // assuming the result of the statement is LHS. Return the range in
1144 // R, or false if no range could be calculated.
1146 bool
1147 gori_compute::compute_operand1_and_operand2_range (irange &r,
1148 gimple *stmt,
1149 const irange &lhs,
1150 tree name,
1151 fur_source &src)
1153 int_range_max op_range;
1155 // Calculate a good a range for op2. Since op1 == op2, this will
1156 // have already included whatever the actual range of name is.
1157 if (!compute_operand2_range (op_range, stmt, lhs, name, src))
1158 return false;
1160 // Now get the range thru op1.
1161 if (!compute_operand1_range (r, stmt, lhs, name, src))
1162 return false;
1164 // Both operands have to be simultaneously true, so perform an intersection.
1165 r.intersect (op_range);
1166 return true;
1168 // Return TRUE if a range can be calculated or recomputed for NAME on edge E.
1170 bool
1171 gori_compute::has_edge_range_p (tree name, edge e)
1173 // Check if NAME is an export or can be recomputed.
1174 if (e)
1175 return is_export_p (name, e->src) || may_recompute_p (name, e);
1177 // If no edge is specified, check if NAME can have a range calculated
1178 // on any edge.
1179 return is_export_p (name) || may_recompute_p (name);
1182 // Dump what is known to GORI computes to listing file F.
1184 void
1185 gori_compute::dump (FILE *f)
1187 gori_map::dump (f);
1190 // Return TRUE if NAME can be recomputed on edge E. If any direct dependant
1191 // is exported on edge E, it may change the computed value of NAME.
1193 bool
1194 gori_compute::may_recompute_p (tree name, edge e)
1196 tree dep1 = depend1 (name);
1197 tree dep2 = depend2 (name);
1199 // If the first dependency is not set, there is no recompuation.
1200 if (!dep1)
1201 return false;
1203 // Don't recalculate PHIs or statements with side_effects.
1204 gimple *s = SSA_NAME_DEF_STMT (name);
1205 if (is_a<gphi *> (s) || gimple_has_side_effects (s))
1206 return false;
1208 // If edge is specified, check if NAME can be recalculated on that edge.
1209 if (e)
1210 return ((is_export_p (dep1, e->src))
1211 || (dep2 && is_export_p (dep2, e->src)));
1213 return (is_export_p (dep1)) || (dep2 && is_export_p (dep2));
1216 // Calculate a range on edge E and return it in R. Try to evaluate a
1217 // range for NAME on this edge. Return FALSE if this is either not a
1218 // control edge or NAME is not defined by this edge.
1220 bool
1221 gori_compute::outgoing_edge_range_p (irange &r, edge e, tree name,
1222 range_query &q)
1224 int_range_max lhs;
1225 unsigned idx;
1227 if ((e->flags & m_not_executable_flag))
1229 r.set_undefined ();
1230 if (dump_file && (dump_flags & TDF_DETAILS))
1231 fprintf (dump_file, "Outgoing edge %d->%d unexecutable.\n",
1232 e->src->index, e->dest->index);
1233 return true;
1236 gcc_checking_assert (gimple_range_ssa_p (name));
1237 // Determine if there is an outgoing edge.
1238 gimple *stmt = outgoing.edge_range_p (lhs, e);
1239 if (!stmt)
1240 return false;
1242 fur_stmt src (stmt, &q);
1243 // If NAME can be calculated on the edge, use that.
1244 if (is_export_p (name, e->src))
1246 bool res;
1247 if ((idx = tracer.header ("outgoing_edge")))
1249 fprintf (dump_file, " for ");
1250 print_generic_expr (dump_file, name, TDF_SLIM);
1251 fprintf (dump_file, " on edge %d->%d\n",
1252 e->src->index, e->dest->index);
1254 if ((res = compute_operand_range (r, stmt, lhs, name, src)))
1256 // Sometimes compatible types get interchanged. See PR97360.
1257 // Make sure we are returning the type of the thing we asked for.
1258 if (!r.undefined_p () && r.type () != TREE_TYPE (name))
1260 gcc_checking_assert (range_compatible_p (r.type (),
1261 TREE_TYPE (name)));
1262 range_cast (r, TREE_TYPE (name));
1265 if (idx)
1266 tracer.trailer (idx, "outgoing_edge", res, name, r);
1267 return res;
1269 // If NAME isn't exported, check if it can be recomputed.
1270 else if (may_recompute_p (name, e))
1272 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1274 if ((idx = tracer.header ("recomputation")))
1276 fprintf (dump_file, " attempt on edge %d->%d for ",
1277 e->src->index, e->dest->index);
1278 print_gimple_stmt (dump_file, def_stmt, 0, TDF_SLIM);
1280 // Simply calculate DEF_STMT on edge E using the range query Q.
1281 fold_range (r, def_stmt, e, &q);
1282 if (idx)
1283 tracer.trailer (idx, "recomputation", true, name, r);
1284 return true;
1286 return false;
1290 // ------------------------------------------------------------------------
1291 // GORI iterator. Although we have bitmap iterators, don't expose that it
1292 // is currently a bitmap. Use an export iterator to hide future changes.
1294 // Construct a basic iterator over an export bitmap.
1296 gori_export_iterator::gori_export_iterator (bitmap b)
1298 bm = b;
1299 if (b)
1300 bmp_iter_set_init (&bi, b, 1, &y);
1304 // Move to the next export bitmap spot.
1306 void
1307 gori_export_iterator::next ()
1309 bmp_iter_next (&bi, &y);
1313 // Fetch the name of the next export in the export list. Return NULL if
1314 // iteration is done.
1316 tree
1317 gori_export_iterator::get_name ()
1319 if (!bm)
1320 return NULL_TREE;
1322 while (bmp_iter_set (&bi, &y))
1324 tree t = ssa_name (y);
1325 if (t)
1326 return t;
1327 next ();
1329 return NULL_TREE;