1 /* Classes for representing locations within the program.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
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/>. */
22 #define INCLUDE_MEMORY
23 #define INCLUDE_VECTOR
25 #include "coretypes.h"
27 #include "gimple-pretty-print.h"
28 #include "gcc-rich-location.h"
29 #include "ordered-hash-map.h"
34 #include "basic-block.h"
36 #include "gimple-iterator.h"
38 #include "analyzer/analyzer.h"
39 #include "analyzer/analyzer-logging.h"
40 #include "analyzer/call-string.h"
41 #include "analyzer/supergraph.h"
42 #include "analyzer/program-point.h"
46 #include "analyzer/store.h"
47 #include "analyzer/region-model.h"
48 #include "analyzer/sm.h"
49 #include "analyzer/program-state.h"
50 #include "diagnostic-event-id.h"
51 #include "analyzer/pending-diagnostic.h"
52 #include "analyzer/diagnostic-manager.h"
53 #include "shortest-paths.h"
54 #include "analyzer/exploded-graph.h"
55 #include "analyzer/analysis-plan.h"
56 #include "analyzer/inlining-iterator.h"
62 /* Get a string for PK. */
65 point_kind_to_string (enum point_kind pk
)
73 case PK_BEFORE_SUPERNODE
:
74 return "PK_BEFORE_SUPERNODE";
76 return "PK_BEFORE_STMT";
77 case PK_AFTER_SUPERNODE
:
78 return "PK_AFTER_SUPERNODE";
86 /* class function_point. */
88 function_point::function_point (const supernode
*supernode
,
89 const superedge
*from_edge
,
92 : m_supernode (supernode
), m_from_edge (from_edge
),
93 m_stmt_idx (stmt_idx
), m_kind (kind
)
97 gcc_checking_assert (m_kind
== PK_BEFORE_SUPERNODE
);
98 gcc_checking_assert (from_edge
->get_kind () == SUPEREDGE_CFG_EDGE
);
101 gcc_checking_assert (m_kind
== PK_BEFORE_STMT
);
104 /* Print this function_point to PP. */
107 function_point::print (pretty_printer
*pp
, const format
&f
) const
115 pp_printf (pp
, "origin");
120 case PK_BEFORE_SUPERNODE
:
124 if (basic_block bb
= m_from_edge
->m_src
->m_bb
)
125 pp_printf (pp
, "before SN: %i (from SN: %i (bb: %i))",
126 m_supernode
->m_index
, m_from_edge
->m_src
->m_index
,
129 pp_printf (pp
, "before SN: %i (from SN: %i)",
130 m_supernode
->m_index
, m_from_edge
->m_src
->m_index
);
133 pp_printf (pp
, "before SN: %i (NULL from-edge)",
134 m_supernode
->m_index
);
136 for (gphi_iterator gpi
137 = const_cast<supernode
*>(get_supernode ())->start_phis ();
138 !gsi_end_p (gpi
); gsi_next (&gpi
))
140 const gphi
*phi
= gpi
.phi ();
141 pp_gimple_stmt_1 (pp
, phi
, 0, (dump_flags_t
)0);
147 pp_printf (pp
, "before (SN: %i stmt: %i): ", m_supernode
->m_index
,
150 pp_gimple_stmt_1 (pp
, get_stmt (), 0, (dump_flags_t
)0);
154 print_source_line (pp
);
158 case PK_AFTER_SUPERNODE
:
159 pp_printf (pp
, "after SN: %i", m_supernode
->m_index
);
166 /* Generate a hash value for this function_point. */
169 function_point::hash () const
171 inchash::hash hstate
;
173 hstate
.add_int (m_supernode
->m_index
);
174 hstate
.add_ptr (m_from_edge
);
175 hstate
.add_int (m_stmt_idx
);
176 hstate
.add_int (m_kind
);
177 return hstate
.end ();
180 /* Get the function at this point, if any. */
183 function_point::get_function () const
186 return m_supernode
->m_fun
;
191 /* Get the gimple stmt for this function_point, if any. */
194 function_point::get_stmt () const
196 if (m_kind
== PK_BEFORE_STMT
)
197 return m_supernode
->m_stmts
[m_stmt_idx
];
198 else if (m_kind
== PK_AFTER_SUPERNODE
)
199 return m_supernode
->get_last_stmt ();
204 /* Get a location for this function_point, if any. */
207 function_point::get_location () const
209 const gimple
*stmt
= get_stmt ();
211 return stmt
->location
;
212 if (m_kind
== PK_BEFORE_SUPERNODE
)
213 return m_supernode
->get_start_location ();
214 else if (m_kind
== PK_AFTER_SUPERNODE
)
215 return m_supernode
->get_end_location ();
217 return UNKNOWN_LOCATION
;
220 /* Return true if this function_point is a before_stmt for
221 the final stmt in its supernode. */
224 function_point::final_stmt_p () const
226 if (m_kind
!= PK_BEFORE_STMT
)
228 return m_stmt_idx
== get_supernode ()->m_stmts
.length () - 1;
231 /* Create a function_point representing the entrypoint of function FUN. */
234 function_point::from_function_entry (const supergraph
&sg
, const function
&fun
)
236 return before_supernode (sg
.get_node_for_function_entry (fun
), NULL
);
239 /* Create a function_point representing entering supernode SUPERNODE,
240 having reached it via FROM_EDGE (which could be NULL). */
243 function_point::before_supernode (const supernode
*supernode
,
244 const superedge
*from_edge
)
246 if (from_edge
&& from_edge
->get_kind () != SUPEREDGE_CFG_EDGE
)
248 return function_point (supernode
, from_edge
, 0, PK_BEFORE_SUPERNODE
);
251 /* A subclass of diagnostic_context for use by
252 program_point::print_source_line. */
254 class debug_diagnostic_context
: public diagnostic_context
257 debug_diagnostic_context ()
259 diagnostic_initialize (this, 0);
260 m_source_printing
.show_line_numbers_p
= true;
261 m_source_printing
.enabled
= true;
263 ~debug_diagnostic_context ()
265 diagnostic_finish (this);
269 /* Print the source line (if any) for this function_point to PP. */
272 function_point::print_source_line (pretty_printer
*pp
) const
274 const gimple
*stmt
= get_stmt ();
277 // TODO: monospace font
278 debug_diagnostic_context tmp_dc
;
279 gcc_rich_location
richloc (stmt
->location
);
280 diagnostic_show_locus (&tmp_dc
, &richloc
, DK_ERROR
);
281 pp_string (pp
, pp_formatted_text (tmp_dc
.m_printer
));
284 /* class program_point. */
286 /* Print this program_point to PP. */
289 program_point::print (pretty_printer
*pp
, const format
&f
) const
291 pp_string (pp
, "callstring: ");
292 m_call_string
->print (pp
);
295 m_function_point
.print (pp
, f
);
298 /* Dump this point to stderr. */
301 program_point::dump () const
303 tree_dump_pretty_printer
pp (stderr
);
304 print (&pp
, format (true));
307 /* Return a new json::object of the form
309 "snode_idx" : int (optional), the index of the supernode,
310 "from_edge_snode_idx" : int (only for kind=='PK_BEFORE_SUPERNODE'),
311 "stmt_idx": int (only for kind=='PK_BEFORE_STMT',
312 "call_string": object for the call_string}. */
315 program_point::to_json () const
317 json::object
*point_obj
= new json::object ();
319 point_obj
->set_string ("kind", point_kind_to_string (get_kind ()));
321 if (get_supernode ())
322 point_obj
->set_integer ("snode_idx", get_supernode ()->m_index
);
327 case PK_BEFORE_SUPERNODE
:
328 if (const superedge
*sedge
= get_from_edge ())
329 point_obj
->set_integer ("from_edge_snode_idx", sedge
->m_src
->m_index
);
332 point_obj
->set_integer ("stmt_idx", get_stmt_idx ());
336 point_obj
->set ("call_string", m_call_string
->to_json ());
341 /* Update the callstack to represent a call from caller to callee.
343 Generally used to push a custom call to a perticular program point
344 where we don't have a superedge representing the call. */
346 program_point::push_to_call_stack (const supernode
*caller
,
347 const supernode
*callee
)
349 m_call_string
= m_call_string
->push_call (callee
, caller
);
352 /* Pop the topmost call from the current callstack. */
354 program_point::pop_from_call_stack ()
356 m_call_string
= m_call_string
->get_parent ();
357 gcc_assert (m_call_string
);
360 /* Generate a hash value for this program_point. */
363 program_point::hash () const
365 inchash::hash hstate
;
366 hstate
.merge_hash (m_function_point
.hash ());
367 hstate
.add_ptr (m_call_string
);
368 return hstate
.end ();
371 /* Get the function * at DEPTH within the call stack. */
374 program_point::get_function_at_depth (unsigned depth
) const
376 gcc_assert (depth
<= m_call_string
->length ());
377 if (depth
== m_call_string
->length ())
378 return m_function_point
.get_function ();
380 return get_call_string ()[depth
].get_caller_function ();
383 /* Assert that this object is sane. */
386 program_point::validate () const
388 /* Skip this in a release build. */
393 m_call_string
->validate ();
394 /* The "callee" of the final entry in the callstring should be the
395 function of the m_function_point. */
396 if (m_call_string
->length () > 0)
398 ((*m_call_string
)[m_call_string
->length () - 1].get_callee_function ()
402 /* Check to see if SUCC is a valid edge to take (ensuring that we have
403 interprocedurally valid paths in the exploded graph, and enforcing
406 Update the call string if SUCC is a call or a return.
408 Return true if SUCC can be taken, or false otherwise.
410 This is the "point" half of exploded_node::on_edge. */
413 program_point::on_edge (exploded_graph
&eg
,
414 const superedge
*succ
)
416 logger
* const logger
= eg
.get_logger ();
418 switch (succ
->m_kind
)
420 case SUPEREDGE_CFG_EDGE
:
422 const cfg_superedge
*cfg_sedge
= as_a
<const cfg_superedge
*> (succ
);
424 if (cfg_sedge
->get_flags () & EDGE_ABNORMAL
)
426 const supernode
*src_snode
= cfg_sedge
->m_src
;
427 if (gimple
*last_stmt
= src_snode
->get_last_stmt ())
428 if (last_stmt
->code
== GIMPLE_GOTO
)
430 /* For the program_point aspect here, consider all
431 out-edges from goto stmts to be valid; we'll
432 consider state separately. */
436 /* Reject other kinds of abnormal edges;
437 we special-case setjmp/longjmp. */
445 const call_superedge
*call_sedge
= as_a
<const call_superedge
*> (succ
);
447 if (eg
.get_analysis_plan ().use_summary_p (call_sedge
->m_cedge
))
450 logger
->log ("rejecting call edge: using summary instead");
454 /* Add the callsite to the call string. */
455 m_call_string
= m_call_string
->push_call (eg
.get_supergraph (),
458 /* Impose a maximum recursion depth and don't analyze paths
459 that exceed it further.
460 This is something of a blunt workaround, but it only
461 applies to recursion (and mutual recursion), not to
462 general call stacks. */
463 if (m_call_string
->calc_recursion_depth ()
464 > param_analyzer_max_recursion_depth
)
467 logger
->log ("rejecting call edge: recursion limit exceeded");
468 // TODO: issue a sorry for this?
474 case SUPEREDGE_RETURN
:
476 /* Require that we return to the call site in the call string. */
477 if (m_call_string
->empty_p ())
480 logger
->log ("rejecting return edge: empty call string");
483 const call_string::element_t
&top_of_stack
484 = m_call_string
->get_top_of_stack ();
485 m_call_string
= m_call_string
->get_parent ();
486 call_string::element_t
current_call_string_element (succ
->m_dest
,
488 if (top_of_stack
!= current_call_string_element
)
491 logger
->log ("rejecting return edge: return to wrong callsite");
497 case SUPEREDGE_INTRAPROCEDURAL_CALL
:
499 const callgraph_superedge
*cg_sedge
500 = as_a
<const callgraph_superedge
*> (succ
);
501 /* Consider turning this edge into a use of an
502 interprocedural summary. */
503 if (eg
.get_analysis_plan ().use_summary_p (cg_sedge
->m_cedge
))
506 logger
->log ("using function summary for %qE in %qE",
507 cg_sedge
->get_callee_decl (),
508 cg_sedge
->get_caller_decl ());
513 /* Otherwise, we ignore these edges */
515 logger
->log ("rejecting interprocedural edge");
524 /* Comparator for program points within the same supernode,
525 for implementing worklist::key_t comparison operators.
526 Return negative if POINT_A is before POINT_B
527 Return positive if POINT_A is after POINT_B
528 Return 0 if they are equal. */
531 function_point::cmp_within_supernode_1 (const function_point
&point_a
,
532 const function_point
&point_b
)
534 gcc_assert (point_a
.get_supernode () == point_b
.get_supernode ());
536 switch (point_a
.m_kind
)
540 case PK_BEFORE_SUPERNODE
:
541 switch (point_b
.m_kind
)
545 case PK_BEFORE_SUPERNODE
:
549 if (point_a
.m_from_edge
)
550 a_src_idx
= point_a
.m_from_edge
->m_src
->m_index
;
551 if (point_b
.m_from_edge
)
552 b_src_idx
= point_b
.m_from_edge
->m_src
->m_index
;
553 return a_src_idx
- b_src_idx
;
558 case PK_AFTER_SUPERNODE
:
563 switch (point_b
.m_kind
)
567 case PK_BEFORE_SUPERNODE
:
571 return point_a
.m_stmt_idx
- point_b
.m_stmt_idx
;
573 case PK_AFTER_SUPERNODE
:
577 case PK_AFTER_SUPERNODE
:
578 switch (point_b
.m_kind
)
582 case PK_BEFORE_SUPERNODE
:
586 case PK_AFTER_SUPERNODE
:
593 /* Comparator for program points within the same supernode,
594 for implementing worklist::key_t comparison operators.
595 Return negative if POINT_A is before POINT_B
596 Return positive if POINT_A is after POINT_B
597 Return 0 if they are equal. */
600 function_point::cmp_within_supernode (const function_point
&point_a
,
601 const function_point
&point_b
)
603 int result
= cmp_within_supernode_1 (point_a
, point_b
);
605 /* Check that the ordering is symmetric */
607 int reversed
= cmp_within_supernode_1 (point_b
, point_a
);
608 gcc_assert (reversed
== -result
);
614 /* Comparator for imposing an order on function_points. */
617 function_point::cmp (const function_point
&point_a
,
618 const function_point
&point_b
)
620 int idx_a
= point_a
.m_supernode
? point_a
.m_supernode
->m_index
: -1;
621 int idx_b
= point_b
.m_supernode
? point_b
.m_supernode
->m_index
: -1;
622 if (int cmp_idx
= idx_a
- idx_b
)
624 gcc_assert (point_a
.m_supernode
== point_b
.m_supernode
);
625 if (point_a
.m_supernode
)
626 return cmp_within_supernode (point_a
, point_b
);
631 /* Comparator for use by vec<function_point>::qsort. */
634 function_point::cmp_ptr (const void *p1
, const void *p2
)
636 const function_point
*fp1
= (const function_point
*)p1
;
637 const function_point
*fp2
= (const function_point
*)p2
;
638 return cmp (*fp1
, *fp2
);
641 /* For PK_BEFORE_STMT, go to next stmt (or to PK_AFTER_SUPERNODE). */
644 function_point::next_stmt ()
646 gcc_assert (m_kind
== PK_BEFORE_STMT
);
647 if (++m_stmt_idx
== m_supernode
->m_stmts
.length ())
649 m_kind
= PK_AFTER_SUPERNODE
;
654 /* For those function points for which there is a uniquely-defined
655 successor, return it. */
658 function_point::get_next () const
665 case PK_AFTER_SUPERNODE
:
666 gcc_unreachable (); /* Not uniquely defined. */
667 case PK_BEFORE_SUPERNODE
:
668 if (get_supernode ()->m_stmts
.length () > 0)
669 return before_stmt (get_supernode (), 0);
671 return after_supernode (get_supernode ());
674 unsigned next_idx
= get_stmt_idx () + 1;
675 if (next_idx
< get_supernode ()->m_stmts
.length ())
676 return before_stmt (get_supernode (), next_idx
);
678 return after_supernode (get_supernode ());
683 /* class program_point. */
686 program_point::origin (const region_model_manager
&mgr
)
688 return program_point (function_point (NULL
, NULL
,
690 mgr
.get_empty_call_string ());
694 program_point::from_function_entry (const region_model_manager
&mgr
,
695 const supergraph
&sg
,
698 return program_point (function_point::from_function_entry (sg
, fun
),
699 mgr
.get_empty_call_string ());
702 /* For those program points for which there is a uniquely-defined
703 successor, return it. */
706 program_point::get_next () const
708 switch (m_function_point
.get_kind ())
713 case PK_AFTER_SUPERNODE
:
714 gcc_unreachable (); /* Not uniquely defined. */
715 case PK_BEFORE_SUPERNODE
:
716 if (get_supernode ()->m_stmts
.length () > 0)
717 return before_stmt (get_supernode (), 0, get_call_string ());
719 return after_supernode (get_supernode (), get_call_string ());
722 unsigned next_idx
= get_stmt_idx () + 1;
723 if (next_idx
< get_supernode ()->m_stmts
.length ())
724 return before_stmt (get_supernode (), next_idx
, get_call_string ());
726 return after_supernode (get_supernode (), get_call_string ());
731 /* Return true iff POINT_A and POINT_B share the same function and
732 call_string, both directly, and when attempting to undo inlining
736 program_point::effectively_intraprocedural_p (const program_point
&point_a
,
737 const program_point
&point_b
)
739 /* First, compare without considering inlining info. */
740 if (point_a
.get_function ()
741 != point_b
.get_function ())
743 if (&point_a
.get_call_string ()
744 != &point_b
.get_call_string ())
747 /* Consider inlining info; they must have originally come from
748 the same function and have been inlined in the same way. */
749 location_t loc_a
= point_a
.get_location ();
750 location_t loc_b
= point_b
.get_location ();
751 inlining_iterator
iter_a (loc_a
);
752 inlining_iterator
iter_b (loc_b
);
753 while (!(iter_a
.done_p () || iter_b
.done_p ()))
755 if (iter_a
.done_p () || iter_b
.done_p ())
758 if (iter_a
.get_fndecl () != iter_b
.get_fndecl ())
760 if (iter_a
.get_callsite () != iter_b
.get_callsite ())
762 if (iter_a
.get_block () != iter_b
.get_block ())
776 /* Verify that function_point::operator== works as expected. */
779 test_function_point_equality ()
781 const supernode
*snode
= NULL
;
783 function_point a
= function_point (snode
, NULL
, 0,
784 PK_BEFORE_SUPERNODE
);
785 function_point b
= function_point::before_supernode (snode
, NULL
);
789 /* Verify that function_point::cmp_within_supernode works as expected. */
792 test_function_point_ordering ()
794 const supernode
*snode
= NULL
;
796 /* Populate an array with various points within the same
798 auto_vec
<function_point
> points
;
799 points
.safe_push (function_point::before_supernode (snode
, NULL
));
800 points
.safe_push (function_point::before_stmt (snode
, 0));
801 points
.safe_push (function_point::before_stmt (snode
, 1));
802 points
.safe_push (function_point::after_supernode (snode
));
804 /* Check all pairs. */
806 function_point
*point_a
;
807 FOR_EACH_VEC_ELT (points
, i
, point_a
)
810 function_point
*point_b
;
811 FOR_EACH_VEC_ELT (points
, j
, point_b
)
813 int cmp
= function_point::cmp_within_supernode (*point_a
, *point_b
);
817 ASSERT_TRUE (cmp
< 0);
819 ASSERT_TRUE (cmp
> 0);
824 /* Verify that program_point::operator== works as expected. */
827 test_program_point_equality ()
829 region_model_manager mgr
;
831 const supernode
*snode
= NULL
;
833 const call_string
&cs
= mgr
.get_empty_call_string ();
835 program_point a
= program_point::before_supernode (snode
, NULL
,
838 program_point b
= program_point::before_supernode (snode
, NULL
,
842 // TODO: verify with non-empty callstrings, with different edges
845 /* Run all of the selftests within this file. */
848 analyzer_program_point_cc_tests ()
850 test_function_point_equality ();
851 test_function_point_ordering ();
852 test_program_point_equality ();
855 } // namespace selftest
857 #endif /* CHECKING_P */
861 #endif /* #if ENABLE_ANALYZER */