xref: /netbsd-src/external/gpl3/gcc/dist/gcc/ipa-utils.h (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1 /* Utilities for ipa analysis.
2    Copyright (C) 2004-2022 Free Software Foundation, Inc.
3    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4 
5 This file is part of GCC.
6 
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
10 version.
11 
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
15 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 #ifndef GCC_IPA_UTILS_H
22 #define GCC_IPA_UTILS_H
23 
24 struct ipa_dfs_info {
25   int dfn_number;
26   int low_link;
27   /* This field will have the samy value for any two nodes in the same strongly
28      connected component.  */
29   int scc_no;
30   bool new_node;
31   bool on_stack;
32   struct cgraph_node* next_cycle;
33   PTR aux;
34 };
35 
36 
37 /* In ipa-utils.cc  */
38 void ipa_print_order (FILE*, const char *, struct cgraph_node**, int);
39 int ipa_reduced_postorder (struct cgraph_node **, bool,
40 			  bool (*ignore_edge) (struct cgraph_edge *));
41 void ipa_free_postorder_info (void);
42 vec<cgraph_node *> ipa_get_nodes_in_cycle (struct cgraph_node *);
43 bool ipa_edge_within_scc (struct cgraph_edge *);
44 int ipa_reverse_postorder (struct cgraph_node **);
45 tree get_base_var (tree);
46 void ipa_merge_profiles (struct cgraph_node *dst,
47 			 struct cgraph_node *src, bool preserve_body = false);
48 bool recursive_call_p (tree, tree);
49 
50 /* In ipa-pure-const.cc  */
51 bool finite_function_p ();
52 bool builtin_safe_for_const_function_p (bool *, tree);
53 bool ipa_make_function_const (cgraph_node *, bool, bool);
54 bool ipa_make_function_pure (cgraph_node *, bool, bool);
55 
56 /* In ipa-profile.cc  */
57 bool ipa_propagate_frequency (struct cgraph_node *node);
58 
59 /* In ipa-devirt.cc  */
60 
61 struct odr_type_d;
62 typedef odr_type_d *odr_type;
63 extern bool thunk_expansion;
64 void build_type_inheritance_graph (void);
65 void rebuild_type_inheritance_graph (void);
66 void update_type_inheritance_graph (void);
67 vec <cgraph_node *>
68 possible_polymorphic_call_targets (tree, HOST_WIDE_INT,
69 				   ipa_polymorphic_call_context,
70 				   bool *copletep = NULL,
71 				   void **cache_token = NULL,
72 				   bool speuclative = false);
73 odr_type get_odr_type (tree, bool insert = false);
74 bool odr_type_p (const_tree);
75 bool possible_polymorphic_call_target_p (tree ref, gimple *stmt, struct cgraph_node *n);
76 void dump_possible_polymorphic_call_targets (FILE *, tree, HOST_WIDE_INT,
77 					     const ipa_polymorphic_call_context &,
78 					     bool verbose = true);
79 bool possible_polymorphic_call_target_p (tree, HOST_WIDE_INT,
80 				         const ipa_polymorphic_call_context &,
81 					 struct cgraph_node *);
82 tree polymorphic_ctor_dtor_p (tree, bool);
83 tree inlined_polymorphic_ctor_dtor_block_p (tree, bool);
84 bool decl_maybe_in_construction_p (tree, tree, gimple *, tree);
85 tree vtable_pointer_value_to_binfo (const_tree);
86 bool vtable_pointer_value_to_vtable (const_tree, tree *, unsigned HOST_WIDE_INT *);
87 tree subbinfo_with_vtable_at_offset (tree, unsigned HOST_WIDE_INT, tree);
88 void compare_virtual_tables (varpool_node *, varpool_node *);
89 bool type_all_derivations_known_p (const_tree);
90 bool type_known_to_have_no_derivations_p (tree);
91 bool contains_polymorphic_type_p (const_tree);
92 void register_odr_type (tree);
93 bool types_must_be_same_for_odr (tree, tree);
94 bool types_odr_comparable (tree, tree);
95 cgraph_node *try_speculative_devirtualization (tree, HOST_WIDE_INT,
96 					       ipa_polymorphic_call_context);
97 void warn_types_mismatch (tree t1, tree t2, location_t loc1 = UNKNOWN_LOCATION,
98 			  location_t loc2 = UNKNOWN_LOCATION);
99 bool odr_or_derived_type_p (const_tree t);
100 bool odr_types_equivalent_p (tree type1, tree type2);
101 bool odr_type_violation_reported_p (tree type);
102 tree prevailing_odr_type (tree type);
103 void enable_odr_based_tbaa (tree type);
104 bool odr_based_tbaa_p (const_tree type);
105 void set_type_canonical_for_odr_type (tree type, tree canonical);
106 
107 void register_odr_enum (tree type);
108 
109 /* Return vector containing possible targets of polymorphic call E.
110    If COMPLETEP is non-NULL, store true if the list is complete.
111    CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
112    in the target cache.  If user needs to visit every target list
113    just once, it can memoize them.
114 
115    Returned vector is placed into cache.  It is NOT caller's responsibility
116    to free it.  The vector can be freed on cgraph_remove_node call if
117    the particular node is a virtual function present in the cache.  */
118 
119 inline vec <cgraph_node *>
120 possible_polymorphic_call_targets (struct cgraph_edge *e,
121 				   bool *completep = NULL,
122 				   void **cache_token = NULL,
123 				   bool speculative = false)
124 {
125   ipa_polymorphic_call_context context(e);
126 
127   return possible_polymorphic_call_targets (e->indirect_info->otr_type,
128 					    e->indirect_info->otr_token,
129 					    context,
130 					    completep, cache_token,
131 					    speculative);
132 }
133 
134 /* Same as above but taking OBJ_TYPE_REF as an parameter.  */
135 
136 inline vec <cgraph_node *>
137 possible_polymorphic_call_targets (tree ref,
138 				   gimple *call,
139 				   bool *completep = NULL,
140 				   void **cache_token = NULL)
141 {
142   ipa_polymorphic_call_context context (current_function_decl, ref, call);
143 
144   return possible_polymorphic_call_targets (obj_type_ref_class (ref),
145 					    tree_to_uhwi
146 					      (OBJ_TYPE_REF_TOKEN (ref)),
147 					    context,
148 					    completep, cache_token);
149 }
150 
151 /* Dump possible targets of a polymorphic call E into F.  */
152 
153 inline void
154 dump_possible_polymorphic_call_targets (FILE *f, struct cgraph_edge *e,
155 					bool verbose = true)
156 {
157   ipa_polymorphic_call_context context(e);
158 
159   dump_possible_polymorphic_call_targets (f, e->indirect_info->otr_type,
160 					  e->indirect_info->otr_token,
161 					  context, verbose);
162 }
163 
164 /* Return true if N can be possibly target of a polymorphic call of
165    E.  */
166 
167 inline bool
possible_polymorphic_call_target_p(struct cgraph_edge * e,struct cgraph_node * n)168 possible_polymorphic_call_target_p (struct cgraph_edge *e,
169 				    struct cgraph_node *n)
170 {
171   ipa_polymorphic_call_context context(e);
172 
173   return possible_polymorphic_call_target_p (e->indirect_info->otr_type,
174 					     e->indirect_info->otr_token,
175 					     context, n);
176 }
177 
178 /* Return true if BINFO corresponds to a type with virtual methods.
179 
180    Every type has several BINFOs.  One is the BINFO associated by the type
181    while other represents bases of derived types.  The BINFOs representing
182    bases do not have BINFO_VTABLE pointer set when this is the single
183    inheritance (because vtables are shared).  Look up the BINFO of type
184    and check presence of its vtable.  */
185 
186 inline bool
polymorphic_type_binfo_p(const_tree binfo)187 polymorphic_type_binfo_p (const_tree binfo)
188 {
189   return (BINFO_TYPE (binfo) && TYPE_BINFO (BINFO_TYPE (binfo))
190 	  && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (binfo))));
191 }
192 
193 /* Return true if T is a type with linkage defined.  */
194 
195 inline bool
type_with_linkage_p(const_tree t)196 type_with_linkage_p (const_tree t)
197 {
198   gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
199   if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
200     return false;
201 
202   /* After free_lang_data was run we can recongize
203      types with linkage by presence of mangled name.  */
204   if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
205     return true;
206 
207   if (in_lto_p)
208     return false;
209 
210   /* We used to check for TYPE_STUB_DECL but that is set to NULL for forward
211      declarations.  */
212 
213   if (!RECORD_OR_UNION_TYPE_P (t) && TREE_CODE (t) != ENUMERAL_TYPE)
214     return false;
215 
216   /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL.  */
217   if (!TYPE_CONTEXT (t))
218     return false;
219 
220   return true;
221 }
222 
223 /* Return true if T is in anonymous namespace.
224    This works only on those C++ types with linkage defined.  */
225 
226 inline bool
type_in_anonymous_namespace_p(const_tree t)227 type_in_anonymous_namespace_p (const_tree t)
228 {
229   gcc_checking_assert (type_with_linkage_p (t));
230 
231   /* free_lang_data clears TYPE_STUB_DECL but sets assembler name to
232      "<anon>"  */
233   if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
234     return !strcmp ("<anon>",
235 		    IDENTIFIER_POINTER
236 		    (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
237   else if (!TYPE_STUB_DECL (t))
238     return false;
239   else
240     return !TREE_PUBLIC (TYPE_STUB_DECL (t));
241 }
242 
243 /* Return true of T is type with One Definition Rule info attached.
244    It means that either it is anonymous type or it has assembler name
245    set.  */
246 
247 inline bool
odr_type_p(const_tree t)248 odr_type_p (const_tree t)
249 {
250   /* We do not have this information when not in LTO, but we do not need
251      to care, since it is used only for type merging.  */
252   gcc_checking_assert (in_lto_p || flag_lto || flag_generate_offload);
253   return TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
254          && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t));
255 }
256 
257 /* If TYPE has mangled ODR name, return it.  Otherwise return NULL.
258    The function works only when free_lang_data is run.  */
259 
260 inline const char *
get_odr_name_for_type(tree type)261 get_odr_name_for_type (tree type)
262 {
263   tree type_name = TYPE_NAME (type);
264   if (type_name == NULL_TREE
265       || TREE_CODE (type_name) != TYPE_DECL
266       || !DECL_ASSEMBLER_NAME_SET_P (type_name))
267     return NULL;
268 
269   return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (type_name));
270 }
271 
272 /* Return true if we are going to do LTO streaming.  */
273 
274 inline bool
lto_streaming_expected_p()275 lto_streaming_expected_p ()
276 {
277   /* Compilation before LTO stremaing.  */
278   if (flag_lto && !in_lto_p && symtab->state < IPA_SSA_AFTER_INLINING)
279     return true;
280   /* WPA or incremental link.  */
281   return (flag_wpa || flag_incremental_link == INCREMENTAL_LINK_LTO);
282 }
283 
284 #endif  /* GCC_IPA_UTILS_H  */
285