1 /* Basic block path solver.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "value-range.h"
29 #include "gimple-range.h"
30 #include "tree-pretty-print.h"
31 #include "gimple-range-path.h"
34 #include "gimple-iterator.h"
36 // Internal construct to help facilitate debugging of solver.
37 #define DEBUG_SOLVER (dump_file && (param_threader_debug == THREADER_DEBUG_ALL))
39 path_range_query::path_range_query (gimple_ranger
&ranger
,
40 const vec
<basic_block
> &path
,
41 const bitmap_head
*dependencies
,
48 // Override the relation oracle with a local path relation oracle.
49 m_relation
= new path_oracle (&(m_ranger
.relation ()));
51 reset_path (path
, dependencies
);
54 path_range_query::path_range_query (gimple_ranger
&ranger
, bool resolve
)
60 // Override the relation oracle with a local path relation oracle.
61 m_relation
= new path_oracle (&(m_ranger
.relation ()));
64 path_range_query::~path_range_query ()
70 // Return TRUE if NAME is an exit dependency for the path.
73 path_range_query::exit_dependency_p (tree name
)
75 return (TREE_CODE (name
) == SSA_NAME
76 && bitmap_bit_p (m_exit_dependencies
, SSA_NAME_VERSION (name
)));
79 // If NAME has a cache entry, return it in R, and return TRUE.
82 path_range_query::get_cache (vrange
&r
, tree name
)
84 if (!gimple_range_ssa_p (name
))
85 return get_global_range_query ()->range_of_expr (r
, name
);
87 return m_cache
.get_range (r
, name
);
91 path_range_query::dump (FILE *dump_file
)
93 push_dump_file
save (dump_file
, dump_flags
& ~TDF_DETAILS
);
95 if (m_path
.is_empty ())
101 dump_ranger (dump_file
, m_path
);
103 fprintf (dump_file
, "Exit dependencies:\n");
104 EXECUTE_IF_SET_IN_BITMAP (m_exit_dependencies
, 0, i
, bi
)
106 tree name
= ssa_name (i
);
107 print_generic_expr (dump_file
, name
, TDF_SLIM
);
108 fprintf (dump_file
, "\n");
111 m_cache
.dump (dump_file
);
115 path_range_query::debug ()
120 // Return TRUE if NAME is defined outside the current path.
123 path_range_query::defined_outside_path (tree name
)
125 gimple
*def
= SSA_NAME_DEF_STMT (name
);
126 basic_block bb
= gimple_bb (def
);
128 return !bb
|| !m_path
.contains (bb
);
131 // Return the range of NAME on entry to the path.
134 path_range_query::range_on_path_entry (vrange
&r
, tree name
)
136 gcc_checking_assert (defined_outside_path (name
));
137 basic_block entry
= entry_bb ();
138 m_ranger
.range_on_entry (r
, entry
, name
);
141 // Return the range of NAME at the end of the path being analyzed.
144 path_range_query::internal_range_of_expr (vrange
&r
, tree name
, gimple
*stmt
)
146 if (!r
.supports_type_p (TREE_TYPE (name
)))
149 if (get_cache (r
, name
))
152 if (m_resolve
&& defined_outside_path (name
))
154 range_on_path_entry (r
, name
);
155 m_cache
.set_range (name
, r
);
160 && range_defined_in_block (r
, name
, gimple_bb (stmt
)))
162 if (TREE_CODE (name
) == SSA_NAME
)
164 value_range
glob (TREE_TYPE (name
));
165 gimple_range_global (glob
, name
);
169 m_cache
.set_range (name
, r
);
173 gimple_range_global (r
, name
);
178 path_range_query::range_of_expr (vrange
&r
, tree name
, gimple
*stmt
)
180 if (internal_range_of_expr (r
, name
, stmt
))
182 if (r
.undefined_p ())
183 m_undefined_path
= true;
191 path_range_query::unreachable_path_p ()
193 return m_undefined_path
;
196 // Reset the current path to PATH.
199 path_range_query::reset_path (const vec
<basic_block
> &path
,
200 const bitmap_head
*dependencies
)
202 gcc_checking_assert (path
.length () > 1);
203 m_path
= path
.copy ();
204 m_pos
= m_path
.length () - 1;
205 m_undefined_path
= false;
208 compute_ranges (dependencies
);
212 path_range_query::ssa_defined_in_bb (tree name
, basic_block bb
)
214 return (TREE_CODE (name
) == SSA_NAME
215 && SSA_NAME_DEF_STMT (name
)
216 && gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
);
219 // Return the range of the result of PHI in R.
221 // Since PHIs are calculated in parallel at the beginning of the
222 // block, we must be careful to never save anything to the cache here.
223 // It is the caller's responsibility to adjust the cache. Also,
224 // calculating the PHI's range must not trigger additional lookups.
227 path_range_query::ssa_range_in_phi (vrange
&r
, gphi
*phi
)
229 tree name
= gimple_phi_result (phi
);
233 if (m_resolve
&& m_ranger
.range_of_expr (r
, name
, phi
))
236 // Try to fold the phi exclusively with global values.
237 // This will get things like PHI <5(99), 6(88)>. We do this by
238 // calling range_of_expr with no context.
239 unsigned nargs
= gimple_phi_num_args (phi
);
240 value_range
arg_range (TREE_TYPE (name
));
242 for (size_t i
= 0; i
< nargs
; ++i
)
244 tree arg
= gimple_phi_arg_def (phi
, i
);
245 if (m_ranger
.range_of_expr (arg_range
, arg
, /*stmt=*/NULL
))
246 r
.union_ (arg_range
);
249 r
.set_varying (TREE_TYPE (name
));
256 basic_block bb
= gimple_bb (phi
);
257 basic_block prev
= prev_bb ();
258 edge e_in
= find_edge (prev
, bb
);
259 tree arg
= PHI_ARG_DEF_FROM_EDGE (phi
, e_in
);
260 // Avoid using the cache for ARGs defined in this block, as
261 // that could create an ordering problem.
262 if (ssa_defined_in_bb (arg
, bb
) || !get_cache (r
, arg
))
266 value_range
tmp (TREE_TYPE (name
));
267 // Using both the range on entry to the path, and the
268 // range on this edge yields significantly better
270 if (TREE_CODE (arg
) == SSA_NAME
271 && defined_outside_path (arg
))
272 range_on_path_entry (r
, arg
);
274 r
.set_varying (TREE_TYPE (name
));
275 m_ranger
.range_on_edge (tmp
, e_in
, arg
);
279 r
.set_varying (TREE_TYPE (name
));
283 // If NAME is defined in BB, set R to the range of NAME, and return
284 // TRUE. Otherwise, return FALSE.
287 path_range_query::range_defined_in_block (vrange
&r
, tree name
, basic_block bb
)
289 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
290 basic_block def_bb
= gimple_bb (def_stmt
);
295 if (get_cache (r
, name
))
298 if (gimple_code (def_stmt
) == GIMPLE_PHI
)
299 ssa_range_in_phi (r
, as_a
<gphi
*> (def_stmt
));
303 get_path_oracle ()->killing_def (name
);
305 if (!range_of_stmt (r
, def_stmt
, name
))
306 r
.set_varying (TREE_TYPE (name
));
309 if (bb
&& POINTER_TYPE_P (TREE_TYPE (name
)))
310 infer_oracle ().maybe_adjust_range (r
, name
, bb
);
312 if (DEBUG_SOLVER
&& (bb
|| !r
.varying_p ()))
314 fprintf (dump_file
, "range_defined_in_block (BB%d) for ", bb
? bb
->index
: -1);
315 print_generic_expr (dump_file
, name
, TDF_SLIM
);
316 fprintf (dump_file
, " is ");
318 fprintf (dump_file
, "\n");
324 // Compute ranges defined in the PHIs in this block.
327 path_range_query::compute_ranges_in_phis (basic_block bb
)
329 // PHIs must be resolved simultaneously on entry to the block
330 // because any dependencies must be satisfied with values on entry.
331 // Thus, we calculate all PHIs first, and then update the cache at
334 for (auto iter
= gsi_start_phis (bb
); !gsi_end_p (iter
); gsi_next (&iter
))
336 gphi
*phi
= iter
.phi ();
337 tree name
= gimple_phi_result (phi
);
339 if (!exit_dependency_p (name
))
342 value_range
r (TREE_TYPE (name
));
343 if (range_defined_in_block (r
, name
, bb
))
344 m_cache
.set_range (name
, r
);
348 // Return TRUE if relations may be invalidated after crossing edge E.
351 path_range_query::relations_may_be_invalidated (edge e
)
353 // As soon as the path crosses a back edge, we can encounter
354 // definitions of SSA_NAMEs that may have had a use in the path
355 // already, so this will then be a new definition. The relation
356 // code is all designed around seeing things in dominator order, and
357 // crossing a back edge in the path violates this assumption.
358 return (e
->flags
& EDGE_DFS_BACK
);
361 // Compute ranges defined in the current block, or exported to the
365 path_range_query::compute_ranges_in_block (basic_block bb
)
370 if (m_resolve
&& !at_entry ())
371 compute_phi_relations (bb
, prev_bb ());
373 // Force recalculation of any names in the cache that are defined in
374 // this block. This can happen on interdependent SSA/phis in loops.
375 EXECUTE_IF_SET_IN_BITMAP (m_exit_dependencies
, 0, i
, bi
)
377 tree name
= ssa_name (i
);
378 if (ssa_defined_in_bb (name
, bb
))
379 m_cache
.clear_range (name
);
382 // Solve dependencies defined in this block, starting with the PHIs...
383 compute_ranges_in_phis (bb
);
384 // ...and then the rest of the dependencies.
385 EXECUTE_IF_SET_IN_BITMAP (m_exit_dependencies
, 0, i
, bi
)
387 tree name
= ssa_name (i
);
388 value_range
r (TREE_TYPE (name
));
390 if (gimple_code (SSA_NAME_DEF_STMT (name
)) != GIMPLE_PHI
391 && range_defined_in_block (r
, name
, bb
))
392 m_cache
.set_range (name
, r
);
398 // Solve dependencies that are exported to the next block.
399 basic_block next
= next_bb ();
400 edge e
= find_edge (bb
, next
);
402 if (m_resolve
&& relations_may_be_invalidated (e
))
406 "Resetting relations as they may be invalidated in %d->%d.\n",
407 e
->src
->index
, e
->dest
->index
);
409 path_oracle
*p
= get_path_oracle ();
410 // ?? Instead of nuking the root oracle altogether, we could
411 // reset the path oracle to search for relations from the top of
412 // the loop with the root oracle. Something for future development.
416 bitmap exports
= gori_ssa ()->exports (bb
);
417 EXECUTE_IF_AND_IN_BITMAP (m_exit_dependencies
, exports
, 0, i
, bi
)
419 tree name
= ssa_name (i
);
420 value_range
r (TREE_TYPE (name
));
421 if (gori ().edge_range_p (r
, e
, name
, *this))
423 value_range
cached_range (TREE_TYPE (name
));
424 if (get_cache (cached_range
, name
))
425 r
.intersect (cached_range
);
427 m_cache
.set_range (name
, r
);
430 fprintf (dump_file
, "edge_range_p for ");
431 print_generic_expr (dump_file
, name
, TDF_SLIM
);
432 fprintf (dump_file
, " on edge %d->%d ",
433 e
->src
->index
, e
->dest
->index
);
434 fprintf (dump_file
, "is ");
436 fprintf (dump_file
, "\n");
442 compute_outgoing_relations (bb
, next
);
445 // Adjust all pointer exit dependencies in BB with non-null information.
448 path_range_query::adjust_for_non_null_uses (basic_block bb
)
454 EXECUTE_IF_SET_IN_BITMAP (m_exit_dependencies
, 0, i
, bi
)
456 tree name
= ssa_name (i
);
458 if (!POINTER_TYPE_P (TREE_TYPE (name
)))
461 if (get_cache (r
, name
))
467 r
.set_varying (TREE_TYPE (name
));
469 if (infer_oracle ().maybe_adjust_range (r
, name
, bb
))
470 m_cache
.set_range (name
, r
);
474 // If NAME is a supported SSA_NAME, add it to the bitmap in dependencies.
477 path_range_query::add_to_exit_dependencies (tree name
, bitmap dependencies
)
479 if (TREE_CODE (name
) == SSA_NAME
480 && value_range::supports_type_p (TREE_TYPE (name
)))
481 return bitmap_set_bit (dependencies
, SSA_NAME_VERSION (name
));
485 // Compute the exit dependencies to PATH. These are essentially the
486 // SSA names used to calculate the final conditional along the path.
489 path_range_query::compute_exit_dependencies (bitmap dependencies
)
491 // Start with the imports from the exit block...
492 basic_block exit
= m_path
[0];
493 bitmap_copy (dependencies
, gori_ssa ()->imports (exit
));
495 auto_vec
<tree
> worklist (bitmap_count_bits (dependencies
));
498 EXECUTE_IF_SET_IN_BITMAP (dependencies
, 0, i
, bi
)
500 tree name
= ssa_name (i
);
501 worklist
.quick_push (name
);
504 // ...and add any operands used to define these imports.
505 while (!worklist
.is_empty ())
507 tree name
= worklist
.pop ();
508 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
509 if (SSA_NAME_IS_DEFAULT_DEF (name
)
510 || !m_path
.contains (gimple_bb (def_stmt
)))
513 if (gphi
*phi
= dyn_cast
<gphi
*> (def_stmt
))
515 for (size_t i
= 0; i
< gimple_phi_num_args (phi
); ++i
)
517 edge e
= gimple_phi_arg_edge (phi
, i
);
518 tree arg
= gimple_phi_arg (phi
, i
)->def
;
520 if (TREE_CODE (arg
) == SSA_NAME
521 && m_path
.contains (e
->src
)
522 && bitmap_set_bit (dependencies
, SSA_NAME_VERSION (arg
)))
523 worklist
.safe_push (arg
);
526 else if (gassign
*ass
= dyn_cast
<gassign
*> (def_stmt
))
529 unsigned count
= gimple_range_ssa_names (ssa
, 3, ass
);
530 for (unsigned j
= 0; j
< count
; ++j
)
531 if (add_to_exit_dependencies (ssa
[j
], dependencies
))
532 worklist
.safe_push (ssa
[j
]);
535 // Exported booleans along the path, may help conditionals.
537 for (i
= 0; i
< m_path
.length (); ++i
)
539 basic_block bb
= m_path
[i
];
541 FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb
, name
)
542 if (TREE_CODE (TREE_TYPE (name
)) == BOOLEAN_TYPE
)
543 bitmap_set_bit (dependencies
, SSA_NAME_VERSION (name
));
547 // Compute the ranges for DEPENDENCIES along PATH.
549 // DEPENDENCIES are path exit dependencies. They are the set of SSA
550 // names, any of which could potentially change the value of the final
551 // conditional in PATH. If none is given, the exit dependencies are
552 // calculated from the final conditional in the path.
555 path_range_query::compute_ranges (const bitmap_head
*dependencies
)
558 fprintf (dump_file
, "\n==============================================\n");
561 bitmap_copy (m_exit_dependencies
, dependencies
);
563 compute_exit_dependencies (m_exit_dependencies
);
567 path_oracle
*p
= get_path_oracle ();
568 p
->reset_path (&(m_ranger
.relation ()));
573 fprintf (dump_file
, "path_range_query: compute_ranges for path: ");
574 for (unsigned i
= m_path
.length (); i
> 0; --i
)
576 basic_block bb
= m_path
[i
- 1];
577 fprintf (dump_file
, "%d", bb
->index
);
579 fprintf (dump_file
, "->");
581 fprintf (dump_file
, "\n");
586 basic_block bb
= curr_bb ();
588 compute_ranges_in_block (bb
);
589 adjust_for_non_null_uses (bb
);
599 get_path_oracle ()->dump (dump_file
);
604 // A folding aid used to register and query relations along a path.
605 // When queried, it returns relations as they would appear on exit to
608 // Relations are registered on entry so the path_oracle knows which
609 // block to query the root oracle at when a relation lies outside the
610 // path. However, when queried we return the relation on exit to the
611 // path, since the root_oracle ignores the registered.
613 class jt_fur_source
: public fur_depend
616 jt_fur_source (gimple
*s
, path_range_query
*, const vec
<basic_block
> &);
617 relation_kind
query_relation (tree op1
, tree op2
) override
;
618 void register_relation (gimple
*, relation_kind
, tree op1
, tree op2
) override
;
619 void register_relation (edge
, relation_kind
, tree op1
, tree op2
) override
;
624 jt_fur_source::jt_fur_source (gimple
*s
,
625 path_range_query
*query
,
626 const vec
<basic_block
> &path
)
627 : fur_depend (s
, query
)
629 gcc_checking_assert (!path
.is_empty ());
631 m_entry
= path
[path
.length () - 1];
634 // Ignore statement and register relation on entry to path.
637 jt_fur_source::register_relation (gimple
*, relation_kind k
, tree op1
, tree op2
)
639 m_query
->relation ().record (m_entry
, k
, op1
, op2
);
642 // Ignore edge and register relation on entry to path.
645 jt_fur_source::register_relation (edge
, relation_kind k
, tree op1
, tree op2
)
647 m_query
->relation ().record (m_entry
, k
, op1
, op2
);
651 jt_fur_source::query_relation (tree op1
, tree op2
)
653 if (TREE_CODE (op1
) != SSA_NAME
|| TREE_CODE (op2
) != SSA_NAME
)
656 return m_query
->relation ().query (m_entry
, op1
, op2
);
659 // Return the range of STMT at the end of the path being analyzed.
662 path_range_query::range_of_stmt (vrange
&r
, gimple
*stmt
, tree
)
664 tree type
= gimple_range_type (stmt
);
666 if (!type
|| !r
.supports_type_p (type
))
669 // If resolving unknowns, fold the statement making use of any
670 // relations along the path.
674 jt_fur_source
src (stmt
, this, m_path
);
675 if (!f
.fold_stmt (r
, stmt
, src
))
676 r
.set_varying (type
);
678 // Otherwise, fold without relations.
679 else if (!fold_range (r
, stmt
, this))
680 r
.set_varying (type
);
685 // If possible, register the relation on the incoming edge E into PHI.
688 path_range_query::maybe_register_phi_relation (gphi
*phi
, edge e
)
690 tree arg
= gimple_phi_arg_def (phi
, e
->dest_idx
);
692 if (!gimple_range_ssa_p (arg
))
695 if (relations_may_be_invalidated (e
))
698 basic_block bb
= gimple_bb (phi
);
699 tree result
= gimple_phi_result (phi
);
701 // Avoid recording the equivalence if the arg is defined in this
702 // block, as that could create an ordering problem.
703 if (ssa_defined_in_bb (arg
, bb
))
706 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
707 fprintf (dump_file
, "maybe_register_phi_relation in bb%d:", bb
->index
);
709 get_path_oracle ()->killing_def (result
);
710 m_relation
->record (entry_bb (), VREL_EQ
, arg
, result
);
713 // Compute relations for each PHI in BB. For example:
715 // x_5 = PHI<y_9(5),...>
717 // If the path flows through BB5, we can register that x_5 == y_9.
720 path_range_query::compute_phi_relations (basic_block bb
, basic_block prev
)
725 edge e_in
= find_edge (prev
, bb
);
727 for (gphi_iterator iter
= gsi_start_phis (bb
); !gsi_end_p (iter
);
730 gphi
*phi
= iter
.phi ();
731 tree result
= gimple_phi_result (phi
);
732 unsigned nargs
= gimple_phi_num_args (phi
);
734 if (!exit_dependency_p (result
))
737 for (size_t i
= 0; i
< nargs
; ++i
)
738 if (e_in
== gimple_phi_arg_edge (phi
, i
))
740 maybe_register_phi_relation (phi
, e_in
);
746 // Compute outgoing relations from BB to NEXT.
749 path_range_query::compute_outgoing_relations (basic_block bb
, basic_block next
)
751 if (gcond
*cond
= safe_dyn_cast
<gcond
*> (*gsi_last_bb (bb
)))
754 edge e0
= EDGE_SUCC (bb
, 0);
755 edge e1
= EDGE_SUCC (bb
, 1);
757 if (e0
->dest
== next
)
758 gcond_edge_range (r
, e0
);
759 else if (e1
->dest
== next
)
760 gcond_edge_range (r
, e1
);
764 jt_fur_source
src (NULL
, this, m_path
);
765 src
.register_outgoing_edges (cond
, r
, e0
, e1
);