1*4c3eb207Smrg /* Call stacks at program points.
2*4c3eb207Smrg Copyright (C) 2019-2020 Free Software Foundation, Inc.
3*4c3eb207Smrg Contributed by David Malcolm <dmalcolm@redhat.com>.
4*4c3eb207Smrg
5*4c3eb207Smrg This file is part of GCC.
6*4c3eb207Smrg
7*4c3eb207Smrg GCC is free software; you can redistribute it and/or modify it
8*4c3eb207Smrg under the terms of the GNU General Public License as published by
9*4c3eb207Smrg the Free Software Foundation; either version 3, or (at your option)
10*4c3eb207Smrg any later version.
11*4c3eb207Smrg
12*4c3eb207Smrg GCC is distributed in the hope that it will be useful, but
13*4c3eb207Smrg WITHOUT ANY WARRANTY; without even the implied warranty of
14*4c3eb207Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15*4c3eb207Smrg General Public License for more details.
16*4c3eb207Smrg
17*4c3eb207Smrg You should have received a copy of the GNU General Public License
18*4c3eb207Smrg along with GCC; see the file COPYING3. If not see
19*4c3eb207Smrg <http://www.gnu.org/licenses/>. */
20*4c3eb207Smrg
21*4c3eb207Smrg #include "config.h"
22*4c3eb207Smrg #include "system.h"
23*4c3eb207Smrg #include "coretypes.h"
24*4c3eb207Smrg #include "pretty-print.h"
25*4c3eb207Smrg #include "tree.h"
26*4c3eb207Smrg #include "options.h"
27*4c3eb207Smrg #include "analyzer/call-string.h"
28*4c3eb207Smrg #include "ordered-hash-map.h"
29*4c3eb207Smrg #include "options.h"
30*4c3eb207Smrg #include "cgraph.h"
31*4c3eb207Smrg #include "function.h"
32*4c3eb207Smrg #include "cfg.h"
33*4c3eb207Smrg #include "basic-block.h"
34*4c3eb207Smrg #include "gimple.h"
35*4c3eb207Smrg #include "gimple-iterator.h"
36*4c3eb207Smrg #include "digraph.h"
37*4c3eb207Smrg #include "analyzer/supergraph.h"
38*4c3eb207Smrg
39*4c3eb207Smrg #if ENABLE_ANALYZER
40*4c3eb207Smrg
41*4c3eb207Smrg /* class call_string. */
42*4c3eb207Smrg
43*4c3eb207Smrg /* call_string's copy ctor. */
44*4c3eb207Smrg
call_string(const call_string & other)45*4c3eb207Smrg call_string::call_string (const call_string &other)
46*4c3eb207Smrg : m_return_edges (other.m_return_edges.length ())
47*4c3eb207Smrg {
48*4c3eb207Smrg const return_superedge *e;
49*4c3eb207Smrg int i;
50*4c3eb207Smrg FOR_EACH_VEC_ELT (other.m_return_edges, i, e)
51*4c3eb207Smrg m_return_edges.quick_push (e);
52*4c3eb207Smrg }
53*4c3eb207Smrg
54*4c3eb207Smrg /* call_string's assignment operator. */
55*4c3eb207Smrg
56*4c3eb207Smrg call_string&
operator =(const call_string & other)57*4c3eb207Smrg call_string::operator= (const call_string &other)
58*4c3eb207Smrg {
59*4c3eb207Smrg // would be much simpler if we could rely on vec<> assignment op
60*4c3eb207Smrg m_return_edges.truncate (0);
61*4c3eb207Smrg m_return_edges.reserve (other.m_return_edges.length (), true);
62*4c3eb207Smrg const return_superedge *e;
63*4c3eb207Smrg int i;
64*4c3eb207Smrg FOR_EACH_VEC_ELT (other.m_return_edges, i, e)
65*4c3eb207Smrg m_return_edges.quick_push (e);
66*4c3eb207Smrg return *this;
67*4c3eb207Smrg }
68*4c3eb207Smrg
69*4c3eb207Smrg /* call_string's equality operator. */
70*4c3eb207Smrg
71*4c3eb207Smrg bool
operator ==(const call_string & other) const72*4c3eb207Smrg call_string::operator== (const call_string &other) const
73*4c3eb207Smrg {
74*4c3eb207Smrg if (m_return_edges.length () != other.m_return_edges.length ())
75*4c3eb207Smrg return false;
76*4c3eb207Smrg const return_superedge *e;
77*4c3eb207Smrg int i;
78*4c3eb207Smrg FOR_EACH_VEC_ELT (m_return_edges, i, e)
79*4c3eb207Smrg if (e != other.m_return_edges[i])
80*4c3eb207Smrg return false;
81*4c3eb207Smrg return true;
82*4c3eb207Smrg }
83*4c3eb207Smrg
84*4c3eb207Smrg /* Print this to PP. */
85*4c3eb207Smrg
86*4c3eb207Smrg void
print(pretty_printer * pp) const87*4c3eb207Smrg call_string::print (pretty_printer *pp) const
88*4c3eb207Smrg {
89*4c3eb207Smrg pp_string (pp, "[");
90*4c3eb207Smrg
91*4c3eb207Smrg const return_superedge *e;
92*4c3eb207Smrg int i;
93*4c3eb207Smrg FOR_EACH_VEC_ELT (m_return_edges, i, e)
94*4c3eb207Smrg {
95*4c3eb207Smrg if (i > 0)
96*4c3eb207Smrg pp_string (pp, ", ");
97*4c3eb207Smrg pp_printf (pp, "(SN: %i -> SN: %i in %s)",
98*4c3eb207Smrg e->m_src->m_index, e->m_dest->m_index,
99*4c3eb207Smrg function_name (e->m_dest->m_fun));
100*4c3eb207Smrg }
101*4c3eb207Smrg
102*4c3eb207Smrg pp_string (pp, "]");
103*4c3eb207Smrg }
104*4c3eb207Smrg
105*4c3eb207Smrg /* Generate a hash value for this call_string. */
106*4c3eb207Smrg
107*4c3eb207Smrg hashval_t
hash() const108*4c3eb207Smrg call_string::hash () const
109*4c3eb207Smrg {
110*4c3eb207Smrg inchash::hash hstate;
111*4c3eb207Smrg int i;
112*4c3eb207Smrg const return_superedge *e;
113*4c3eb207Smrg FOR_EACH_VEC_ELT (m_return_edges, i, e)
114*4c3eb207Smrg hstate.add_ptr (e);
115*4c3eb207Smrg return hstate.end ();
116*4c3eb207Smrg }
117*4c3eb207Smrg
118*4c3eb207Smrg /* Push the return superedge for CALL_SEDGE onto the end of this
119*4c3eb207Smrg call_string. */
120*4c3eb207Smrg
121*4c3eb207Smrg void
push_call(const supergraph & sg,const call_superedge * call_sedge)122*4c3eb207Smrg call_string::push_call (const supergraph &sg,
123*4c3eb207Smrg const call_superedge *call_sedge)
124*4c3eb207Smrg {
125*4c3eb207Smrg gcc_assert (call_sedge);
126*4c3eb207Smrg const return_superedge *return_sedge = call_sedge->get_edge_for_return (sg);
127*4c3eb207Smrg gcc_assert (return_sedge);
128*4c3eb207Smrg m_return_edges.safe_push (return_sedge);
129*4c3eb207Smrg }
130*4c3eb207Smrg
131*4c3eb207Smrg /* Count the number of times the top-most call site appears in the
132*4c3eb207Smrg stack. */
133*4c3eb207Smrg
134*4c3eb207Smrg int
calc_recursion_depth() const135*4c3eb207Smrg call_string::calc_recursion_depth () const
136*4c3eb207Smrg {
137*4c3eb207Smrg if (m_return_edges.is_empty ())
138*4c3eb207Smrg return 0;
139*4c3eb207Smrg const return_superedge *top_return_sedge
140*4c3eb207Smrg = m_return_edges[m_return_edges.length () - 1];
141*4c3eb207Smrg
142*4c3eb207Smrg int result = 0;
143*4c3eb207Smrg const return_superedge *e;
144*4c3eb207Smrg int i;
145*4c3eb207Smrg FOR_EACH_VEC_ELT (m_return_edges, i, e)
146*4c3eb207Smrg if (e == top_return_sedge)
147*4c3eb207Smrg ++result;
148*4c3eb207Smrg return result;
149*4c3eb207Smrg }
150*4c3eb207Smrg
151*4c3eb207Smrg /* Comparator for call strings.
152*4c3eb207Smrg This implements a version of lexicographical order.
153*4c3eb207Smrg Return negative if A is before B.
154*4c3eb207Smrg Return positive if B is after A.
155*4c3eb207Smrg Return 0 if they are equal. */
156*4c3eb207Smrg
157*4c3eb207Smrg int
cmp(const call_string & a,const call_string & b)158*4c3eb207Smrg call_string::cmp (const call_string &a,
159*4c3eb207Smrg const call_string &b)
160*4c3eb207Smrg {
161*4c3eb207Smrg unsigned len_a = a.length ();
162*4c3eb207Smrg unsigned len_b = b.length ();
163*4c3eb207Smrg
164*4c3eb207Smrg unsigned i = 0;
165*4c3eb207Smrg while (1)
166*4c3eb207Smrg {
167*4c3eb207Smrg /* Consider index i; the strings have been equal up to it. */
168*4c3eb207Smrg
169*4c3eb207Smrg /* Have both strings run out? */
170*4c3eb207Smrg if (i >= len_a && i >= len_b)
171*4c3eb207Smrg return 0;
172*4c3eb207Smrg
173*4c3eb207Smrg /* Otherwise, has just one of the strings run out? */
174*4c3eb207Smrg if (i >= len_a)
175*4c3eb207Smrg return 1;
176*4c3eb207Smrg if (i >= len_b)
177*4c3eb207Smrg return -1;
178*4c3eb207Smrg
179*4c3eb207Smrg /* Otherwise, compare the edges. */
180*4c3eb207Smrg const return_superedge *edge_a = a[i];
181*4c3eb207Smrg const return_superedge *edge_b = b[i];
182*4c3eb207Smrg int src_cmp = edge_a->m_src->m_index - edge_b->m_src->m_index;
183*4c3eb207Smrg if (src_cmp)
184*4c3eb207Smrg return src_cmp;
185*4c3eb207Smrg int dest_cmp = edge_a->m_dest->m_index - edge_b->m_dest->m_index;
186*4c3eb207Smrg if (dest_cmp)
187*4c3eb207Smrg return dest_cmp;
188*4c3eb207Smrg i++;
189*4c3eb207Smrg // TODO: test coverage for this
190*4c3eb207Smrg }
191*4c3eb207Smrg }
192*4c3eb207Smrg
193*4c3eb207Smrg /* Assert that this object is sane. */
194*4c3eb207Smrg
195*4c3eb207Smrg void
validate() const196*4c3eb207Smrg call_string::validate () const
197*4c3eb207Smrg {
198*4c3eb207Smrg /* Skip this in a release build. */
199*4c3eb207Smrg #if !CHECKING_P
200*4c3eb207Smrg return;
201*4c3eb207Smrg #endif
202*4c3eb207Smrg
203*4c3eb207Smrg /* Each entry's "caller" should be the "callee" of the previous entry. */
204*4c3eb207Smrg const return_superedge *e;
205*4c3eb207Smrg int i;
206*4c3eb207Smrg FOR_EACH_VEC_ELT (m_return_edges, i, e)
207*4c3eb207Smrg if (i > 0)
208*4c3eb207Smrg gcc_assert (e->get_caller_function ()
209*4c3eb207Smrg == m_return_edges[i - 1]->get_callee_function ());
210*4c3eb207Smrg }
211*4c3eb207Smrg
212*4c3eb207Smrg #endif /* #if ENABLE_ANALYZER */
213