GCC Middle and Back End API Reference
call-string.h
Go to the documentation of this file.
1/* Call stacks at program points.
2 Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_ANALYZER_CALL_STRING_H
22#define GCC_ANALYZER_CALL_STRING_H
23
24namespace ana {
25
26class supergraph;
27class supernode;
29
30/* A string of "elements" representing a call stack at a program point.
31
32 This is used to ensure that we generate interprocedurally valid paths
33 i.e. that we return to the same callsite that called us.
34
35 Instances of call_string are consolidated by the region_model_manager,
36 which effectively owns them: it owns the root/empty call_string, and each
37 call_string instance tracks its children, lazily creating them on demand,
38 so that the call_string instances form a tree-like hierarchy in memory. */
39
41{
42public:
43 /* A struct representing an element in the call_string.
44
45 Each element represents a "call superedge" within the caller for which
46 the op was a call_and_return_op.
47 Returning from the callee to the caller will involve creating a custom
48 exploded edge from the exit supernode in the callee to the destination
49 of the call superedge within the caller. */
50
51 struct element_t
52 {
53 element_t (const superedge *call_sedge,
54 const call_and_return_op *call_op,
55 function *called_fun)
56 : m_call_sedge (call_sedge), m_call_op (call_op),
57 m_called_fun (called_fun)
58 {
59 }
60
61 bool operator== (const element_t &other) const;
62 bool operator!= (const element_t &other) const;
63
64 static int cmp (const element_t &a, const element_t &b);
65
66 /* Accessors */
71 const gcall &get_call_stmt () const;
72
76 };
77
78 void print (pretty_printer *pp) const;
79
80 std::unique_ptr<json::value> to_json () const;
81
82 bool empty_p () const { return m_elements.is_empty (); }
83
84 const call_string *push_call (const superedge &call_sedge,
85 const call_and_return_op &call_op,
86 function &called_fun) const;
87 const call_string *get_parent () const { return m_parent; }
88
90
91 static int cmp (const call_string &a,
92 const call_string &b);
93
94 static int cmp_ptr_ptr (const void *, const void *);
95
96 /* Accessors */
98 unsigned length () const { return m_elements.length (); }
99 element_t operator[] (unsigned idx) const
100 {
101 return m_elements[idx];
102 }
104 {
105 gcc_assert (m_elements.length () > 0);
106 return m_elements[m_elements.length () - 1];
107 }
108
110
111 void validate () const;
112
113private:
115 {
117 typedef const call_string *value_type;
118
119 static const bool maybe_mx = false;
120 static inline hashval_t hash (const key_type &k)
121 {
122 inchash::hash hstate;
123 hstate.add_ptr (k.m_call_sedge);
124 return hstate.end ();
125 }
126 static inline bool equal_keys (const key_type &k1, const key_type &k2)
127 {
128 return k1 == k2;
129 }
130 template <typename T> static inline void remove (T &entry)
131 {
132 entry.m_key = element_t (nullptr, nullptr, nullptr);
133 }
134 static const bool empty_zero_p = true;
135 template <typename T> static inline bool is_empty (const T &entry)
136 {
137 return entry.m_key.m_call_sedge == nullptr;
138 }
139 template <typename T> static inline bool is_deleted (const T &entry)
140 {
141 return entry.m_key.m_call_sedge == reinterpret_cast<const superedge *> (1);
142 }
143 template <typename T> static inline void mark_empty (T &entry)
144 {
145 entry.m_key = element_t (nullptr, nullptr, nullptr);
146 entry.m_value = nullptr;
147 }
148 template <typename T> static inline void mark_deleted (T &entry)
149 {
150 entry.m_key.m_call_sedge = reinterpret_cast<const superedge *> (1);
151 }
152 };
153
155
157
159 call_string (const call_string &parent, const element_t &to_push);
161
163
167};
168
169} // namespace ana
170
171#endif /* GCC_ANALYZER_CALL_STRING_H */
Definition ops.h:382
const call_string * get_parent() const
Definition call-string.h:87
static int cmp(const call_string &a, const call_string &b)
void recursive_log(logger *logger) const
void print(pretty_printer *pp) const
DISABLE_COPY_AND_ASSIGN(call_string)
element_t operator[](unsigned idx) const
Definition call-string.h:99
int calc_recursion_depth() const
friend class region_model_manager
Definition call-string.h:154
hash_map< element_t, const call_string *, hashmap_traits_t > m_children
Definition call-string.h:166
void validate() const
auto_vec< element_t > m_elements
Definition call-string.h:165
const call_string * push_call(const superedge &call_sedge, const call_and_return_op &call_op, function &called_fun) const
int count_occurrences_of_function(function *) const
const supernode * get_return_node_in_caller() const
call_string(const call_string &parent, const element_t &to_push)
const call_string * m_parent
Definition call-string.h:164
const element_t & get_top_of_stack() const
Definition call-string.h:103
bool empty_p() const
Definition call-string.h:82
static int cmp_ptr_ptr(const void *, const void *)
unsigned length() const
Definition call-string.h:98
std::unique_ptr< json::value > to_json() const
Definition analyzer-logging.h:34
Definition supergraph.h:281
Definition supergraph.h:105
Definition supergraph.h:224
Definition vec.h:1667
Definition hash-map.h:40
Definition inchash.h:38
hashval_t end() const
Definition inchash.h:49
void add_ptr(const void *ptr)
Definition inchash.h:94
Definition pretty-print.h:241
static struct token T
Definition gengtype-parse.cc:45
Definition access-diagram.h:30
Ca const poly_int< N, Cb > & b
Definition poly-int.h:771
Ca & a
Definition poly-int.h:770
Definition call-string.h:52
const gcall & get_call_stmt() const
bool operator==(const element_t &other) const
const superedge * m_call_sedge
Definition call-string.h:73
function * m_called_fun
Definition call-string.h:75
function * get_callee_function() const
Definition call-string.h:68
bool operator!=(const element_t &other) const
element_t(const superedge *call_sedge, const call_and_return_op *call_op, function *called_fun)
Definition call-string.h:53
const supernode * get_return_snode_in_caller() const
const call_and_return_op * m_call_op
Definition call-string.h:74
const supernode * get_call_snode_in_caller() const
function * get_caller_function() const
static int cmp(const element_t &a, const element_t &b)
Definition call-string.h:115
static void mark_deleted(T &entry)
Definition call-string.h:148
static void mark_empty(T &entry)
Definition call-string.h:143
static bool is_deleted(const T &entry)
Definition call-string.h:139
static hashval_t hash(const key_type &k)
Definition call-string.h:120
static const bool empty_zero_p
Definition call-string.h:134
static void remove(T &entry)
Definition call-string.h:130
static bool equal_keys(const key_type &k1, const key_type &k2)
Definition call-string.h:126
static bool is_empty(const T &entry)
Definition call-string.h:135
const call_string * value_type
Definition call-string.h:117
static const bool maybe_mx
Definition call-string.h:119
element_t key_type
Definition call-string.h:116
Definition function.h:249
Definition gimple.h:352
#define gcc_assert(EXPR)
Definition system.h:817