1 /* Call stacks at program points.
2    Copyright (C) 2019-2021 Free Software Foundation, Inc.
3    Contributed by David Malcolm <dmalcolm@redhat.com>.
4 
5 This file is part of GCC.
6 
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)
10 any later version.
11 
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.
16 
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/>.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "pretty-print.h"
25 #include "tree.h"
26 #include "options.h"
27 #include "json.h"
28 #include "analyzer/call-string.h"
29 #include "ordered-hash-map.h"
30 #include "options.h"
31 #include "cgraph.h"
32 #include "function.h"
33 #include "cfg.h"
34 #include "basic-block.h"
35 #include "gimple.h"
36 #include "gimple-iterator.h"
37 #include "digraph.h"
38 #include "analyzer/supergraph.h"
39 
40 #if ENABLE_ANALYZER
41 
42 #if __GNUC__ >= 10
43 #pragma GCC diagnostic ignored "-Wformat-diag"
44 #endif
45 
46 /* class call_string.  */
47 
48 /* call_string's copy ctor.  */
49 
call_string(const call_string & other)50 call_string::call_string (const call_string &other)
51 : m_return_edges (other.m_return_edges.length ())
52 {
53   const return_superedge *e;
54   int i;
55   FOR_EACH_VEC_ELT (other.m_return_edges, i, e)
56     m_return_edges.quick_push (e);
57 }
58 
59 /* call_string's assignment operator.  */
60 
61 call_string&
operator =(const call_string & other)62 call_string::operator= (const call_string &other)
63 {
64   // would be much simpler if we could rely on vec<> assignment op
65   m_return_edges.truncate (0);
66   m_return_edges.reserve (other.m_return_edges.length (), true);
67   const return_superedge *e;
68   int i;
69   FOR_EACH_VEC_ELT (other.m_return_edges, i, e)
70     m_return_edges.quick_push (e);
71   return *this;
72 }
73 
74 /* call_string's equality operator.  */
75 
76 bool
operator ==(const call_string & other) const77 call_string::operator== (const call_string &other) const
78 {
79   if (m_return_edges.length () != other.m_return_edges.length ())
80     return false;
81   const return_superedge *e;
82   int i;
83   FOR_EACH_VEC_ELT (m_return_edges, i, e)
84     if (e != other.m_return_edges[i])
85       return false;
86   return true;
87 }
88 
89 /* Print this to PP.  */
90 
91 void
print(pretty_printer * pp) const92 call_string::print (pretty_printer *pp) const
93 {
94   pp_string (pp, "[");
95 
96   const return_superedge *e;
97   int i;
98   FOR_EACH_VEC_ELT (m_return_edges, i, e)
99     {
100       if (i > 0)
101 	pp_string (pp, ", ");
102       pp_printf (pp, "(SN: %i -> SN: %i in %s)",
103 		 e->m_src->m_index, e->m_dest->m_index,
104 		 function_name (e->m_dest->m_fun));
105     }
106 
107   pp_string (pp, "]");
108 }
109 
110 /* Return a new json::array of the form
111    [{"src_snode_idx" : int,
112      "dst_snode_idx" : int,
113      "funcname" : str},
114      ...for each return_superedge in the callstring].  */
115 
116 json::value *
to_json() const117 call_string::to_json () const
118 {
119   json::array *arr = new json::array ();
120 
121   const return_superedge *e;
122   int i;
123   FOR_EACH_VEC_ELT (m_return_edges, i, e)
124     {
125       json::object *e_obj = new json::object ();
126       e_obj->set ("src_snode_idx",
127 		  new json::integer_number (e->m_src->m_index));
128       e_obj->set ("dst_snode_idx",
129 		  new json::integer_number (e->m_dest->m_index));
130       e_obj->set ("funcname",
131 		  new json::string (function_name (e->m_dest->m_fun)));
132       arr->append (e_obj);
133     }
134 
135   return arr;
136 }
137 
138 /* Generate a hash value for this call_string.  */
139 
140 hashval_t
hash() const141 call_string::hash () const
142 {
143   inchash::hash hstate;
144   int i;
145   const return_superedge *e;
146   FOR_EACH_VEC_ELT (m_return_edges, i, e)
147     hstate.add_ptr (e);
148   return hstate.end ();
149 }
150 
151 /* Push the return superedge for CALL_SEDGE onto the end of this
152    call_string.  */
153 
154 void
push_call(const supergraph & sg,const call_superedge * call_sedge)155 call_string::push_call (const supergraph &sg,
156 			const call_superedge *call_sedge)
157 {
158   gcc_assert (call_sedge);
159   const return_superedge *return_sedge = call_sedge->get_edge_for_return (sg);
160   gcc_assert (return_sedge);
161   m_return_edges.safe_push (return_sedge);
162 }
163 
164 /* Count the number of times the top-most call site appears in the
165    stack.  */
166 
167 int
calc_recursion_depth() const168 call_string::calc_recursion_depth () const
169 {
170   if (m_return_edges.is_empty ())
171     return 0;
172   const return_superedge *top_return_sedge
173     = m_return_edges[m_return_edges.length () - 1];
174 
175   int result = 0;
176   const return_superedge *e;
177   int i;
178   FOR_EACH_VEC_ELT (m_return_edges, i, e)
179     if (e == top_return_sedge)
180       ++result;
181   return result;
182 }
183 
184 /* Comparator for call strings.
185    This implements a version of lexicographical order.
186    Return negative if A is before B.
187    Return positive if B is after A.
188    Return 0 if they are equal.  */
189 
190 int
cmp(const call_string & a,const call_string & b)191 call_string::cmp (const call_string &a,
192 		  const call_string &b)
193 {
194   unsigned len_a = a.length ();
195   unsigned len_b = b.length ();
196 
197   unsigned i = 0;
198   while (1)
199     {
200       /* Consider index i; the strings have been equal up to it.  */
201 
202       /* Have both strings run out?  */
203       if (i >= len_a && i >= len_b)
204 	return 0;
205 
206       /* Otherwise, has just one of the strings run out?  */
207       if (i >= len_a)
208 	return 1;
209       if (i >= len_b)
210 	return -1;
211 
212       /* Otherwise, compare the edges.  */
213       const return_superedge *edge_a = a[i];
214       const return_superedge *edge_b = b[i];
215       int src_cmp = edge_a->m_src->m_index - edge_b->m_src->m_index;
216       if (src_cmp)
217 	return src_cmp;
218       int dest_cmp = edge_a->m_dest->m_index - edge_b->m_dest->m_index;
219       if (dest_cmp)
220 	return dest_cmp;
221       i++;
222       // TODO: test coverage for this
223     }
224 }
225 
226 /* Assert that this object is sane.  */
227 
228 void
validate() const229 call_string::validate () const
230 {
231   /* Skip this in a release build.  */
232 #if !CHECKING_P
233   return;
234 #endif
235 
236   /* Each entry's "caller" should be the "callee" of the previous entry.  */
237   const return_superedge *e;
238   int i;
239   FOR_EACH_VEC_ELT (m_return_edges, i, e)
240     if (i > 0)
241       gcc_assert (e->get_caller_function ()
242 		  == m_return_edges[i - 1]->get_callee_function ());
243 }
244 
245 #endif /* #if ENABLE_ANALYZER */
246