1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef GCC_COMMON_H_INCLUDED
3 #define GCC_COMMON_H_INCLUDED
4 
5 #include "bversion.h"
6 #if BUILDING_GCC_VERSION >= 6000
7 #include "gcc-plugin.h"
8 #else
9 #include "plugin.h"
10 #endif
11 #include "plugin-version.h"
12 #include "config.h"
13 #include "system.h"
14 #include "coretypes.h"
15 #include "tm.h"
16 #include "line-map.h"
17 #include "input.h"
18 #include "tree.h"
19 
20 #include "tree-inline.h"
21 #include "version.h"
22 #include "rtl.h"
23 #include "tm_p.h"
24 #include "flags.h"
25 #include "hard-reg-set.h"
26 #include "output.h"
27 #include "except.h"
28 #include "function.h"
29 #include "toplev.h"
30 #include "expr.h"
31 #include "basic-block.h"
32 #include "intl.h"
33 #include "ggc.h"
34 #include "timevar.h"
35 
36 #if BUILDING_GCC_VERSION < 10000
37 #include "params.h"
38 #endif
39 
40 #include "hash-map.h"
41 
42 #if BUILDING_GCC_VERSION >= 7000
43 #include "memmodel.h"
44 #endif
45 #include "emit-rtl.h"
46 #include "debug.h"
47 #include "target.h"
48 #include "langhooks.h"
49 #include "cfgloop.h"
50 #include "cgraph.h"
51 #include "opts.h"
52 #include "tree-pretty-print.h"
53 #include "gimple-pretty-print.h"
54 #include "c-family/c-common.h"
55 #include "tree-cfgcleanup.h"
56 #include "tree-ssa-operands.h"
57 #include "tree-into-ssa.h"
58 #include "is-a.h"
59 #include "diagnostic.h"
60 #include "tree-dump.h"
61 #include "tree-pass.h"
62 #include "pass_manager.h"
63 #include "predict.h"
64 #include "ipa-utils.h"
65 #include "stringpool.h"
66 #include "attribs.h"
67 #include "varasm.h"
68 #include "stor-layout.h"
69 #include "internal-fn.h"
70 #include "gimple.h"
71 #include "gimple-expr.h"
72 #include "gimple-iterator.h"
73 #include "gimple-fold.h"
74 #include "context.h"
75 #include "tree-ssa-alias.h"
76 #include "tree-ssa.h"
77 #if BUILDING_GCC_VERSION >= 7000
78 #include "tree-vrp.h"
79 #endif
80 #include "tree-ssanames.h"
81 #include "print-tree.h"
82 #include "tree-eh.h"
83 #include "stmt.h"
84 #include "gimplify.h"
85 #include "tree-phinodes.h"
86 #include "tree-cfg.h"
87 #include "gimple-ssa.h"
88 #include "ssa-iterators.h"
89 
90 #include "builtins.h"
91 
92 /* missing from basic_block.h... */
93 void debug_dominance_info(enum cdi_direction dir);
94 void debug_dominance_tree(enum cdi_direction dir, basic_block root);
95 
96 #ifndef __unused
97 #define __unused __attribute__((__unused__))
98 #endif
99 #ifndef __visible
100 #define __visible __attribute__((visibility("default")))
101 #endif
102 
103 #define DECL_NAME_POINTER(node) IDENTIFIER_POINTER(DECL_NAME(node))
104 #define DECL_NAME_LENGTH(node) IDENTIFIER_LENGTH(DECL_NAME(node))
105 #define TYPE_NAME_POINTER(node) IDENTIFIER_POINTER(TYPE_NAME(node))
106 #define TYPE_NAME_LENGTH(node) IDENTIFIER_LENGTH(TYPE_NAME(node))
107 
108 /* should come from c-tree.h if only it were installed for gcc 4.5... */
109 #define C_TYPE_FIELDS_READONLY(TYPE) TREE_LANG_FLAG_1(TYPE)
110 
build_const_char_string(int len,const char * str)111 static inline tree build_const_char_string(int len, const char *str)
112 {
113 	tree cstr, elem, index, type;
114 
115 	cstr = build_string(len, str);
116 	elem = build_type_variant(char_type_node, 1, 0);
117 	index = build_index_type(size_int(len - 1));
118 	type = build_array_type(elem, index);
119 	TREE_TYPE(cstr) = type;
120 	TREE_CONSTANT(cstr) = 1;
121 	TREE_READONLY(cstr) = 1;
122 	TREE_STATIC(cstr) = 1;
123 	return cstr;
124 }
125 
__add_type_attr(tree type,const char * attr,tree args)126 static inline void __add_type_attr(tree type, const char *attr, tree args)
127 {
128 	tree oldattr;
129 
130 	if (type == NULL_TREE)
131 		return;
132 	oldattr = lookup_attribute(attr, TYPE_ATTRIBUTES(type));
133 	if (oldattr != NULL_TREE) {
134 		gcc_assert(TREE_VALUE(oldattr) == args || TREE_VALUE(TREE_VALUE(oldattr)) == TREE_VALUE(args));
135 		return;
136 	}
137 
138 	TYPE_ATTRIBUTES(type) = copy_list(TYPE_ATTRIBUTES(type));
139 	TYPE_ATTRIBUTES(type) = tree_cons(get_identifier(attr), args, TYPE_ATTRIBUTES(type));
140 }
141 
add_type_attr(tree type,const char * attr,tree args)142 static inline void add_type_attr(tree type, const char *attr, tree args)
143 {
144 	tree main_variant = TYPE_MAIN_VARIANT(type);
145 
146 	__add_type_attr(TYPE_CANONICAL(type), attr, args);
147 	__add_type_attr(TYPE_CANONICAL(main_variant), attr, args);
148 	__add_type_attr(main_variant, attr, args);
149 
150 	for (type = TYPE_NEXT_VARIANT(main_variant); type; type = TYPE_NEXT_VARIANT(type)) {
151 		if (!lookup_attribute(attr, TYPE_ATTRIBUTES(type)))
152 			TYPE_ATTRIBUTES(type) = TYPE_ATTRIBUTES(main_variant);
153 
154 		__add_type_attr(TYPE_CANONICAL(type), attr, args);
155 	}
156 }
157 
158 #define PASS_INFO(NAME, REF, ID, POS)		\
159 struct register_pass_info NAME##_pass_info = {	\
160 	.pass = make_##NAME##_pass(),		\
161 	.reference_pass_name = REF,		\
162 	.ref_pass_instance_number = ID,		\
163 	.pos_op = POS,				\
164 }
165 
166 #define add_referenced_var(var)
167 #define mark_sym_for_renaming(var)
168 #define varpool_mark_needed_node(node)
169 #define create_var_ann(var)
170 #define TODO_dump_func 0
171 #define TODO_dump_cgraph 0
172 
173 #define TODO_ggc_collect 0
174 #define NODE_SYMBOL(node) (node)
175 #define NODE_DECL(node) (node)->decl
176 #define cgraph_node_name(node) (node)->name()
177 #define NODE_IMPLICIT_ALIAS(node) (node)->cpp_implicit_alias
178 
get_pass_for_id(int id)179 static inline opt_pass *get_pass_for_id(int id)
180 {
181 	return g->get_passes()->get_pass_for_id(id);
182 }
183 
184 #if BUILDING_GCC_VERSION < 6000
185 /* gimple related */
186 template <>
187 template <>
test(const_gimple gs)188 inline bool is_a_helper<const gassign *>::test(const_gimple gs)
189 {
190 	return gs->code == GIMPLE_ASSIGN;
191 }
192 #endif
193 
194 #if BUILDING_GCC_VERSION < 16000
195 #define TODO_verify_ssa TODO_verify_il
196 #define TODO_verify_flow TODO_verify_il
197 #define TODO_verify_stmts TODO_verify_il
198 #define TODO_verify_rtl_sharing TODO_verify_il
199 #else
200 #define TODO_verify_ssa 0
201 #define TODO_verify_flow 0
202 #define TODO_verify_stmts 0
203 #define TODO_verify_rtl_sharing 0
204 #endif
205 
206 #define INSN_DELETED_P(insn) (insn)->deleted()
207 
get_decl_section_name(const_tree decl)208 static inline const char *get_decl_section_name(const_tree decl)
209 {
210 	return DECL_SECTION_NAME(decl);
211 }
212 
213 /* symtab/cgraph related */
214 #define debug_cgraph_node(node) (node)->debug()
215 #define cgraph_get_node(decl) cgraph_node::get(decl)
216 #define cgraph_get_create_node(decl) cgraph_node::get_create(decl)
217 #define cgraph_create_node(decl) cgraph_node::create(decl)
218 #define cgraph_n_nodes symtab->cgraph_count
219 #define cgraph_max_uid symtab->cgraph_max_uid
220 #define varpool_get_node(decl) varpool_node::get(decl)
221 #define dump_varpool_node(file, node) (node)->dump(file)
222 
223 #if BUILDING_GCC_VERSION >= 8000
224 #define cgraph_create_edge(caller, callee, call_stmt, count, freq) \
225 	(caller)->create_edge((callee), (call_stmt), (count))
226 
227 #define cgraph_create_edge_including_clones(caller, callee,	\
228 		old_call_stmt, call_stmt, count, freq, reason)	\
229 	(caller)->create_edge_including_clones((callee),	\
230 		(old_call_stmt), (call_stmt), (count), (reason))
231 #else
232 #define cgraph_create_edge(caller, callee, call_stmt, count, freq) \
233 	(caller)->create_edge((callee), (call_stmt), (count), (freq))
234 
235 #define cgraph_create_edge_including_clones(caller, callee,	\
236 		old_call_stmt, call_stmt, count, freq, reason)	\
237 	(caller)->create_edge_including_clones((callee),	\
238 		(old_call_stmt), (call_stmt), (count), (freq), (reason))
239 #endif
240 
241 typedef struct cgraph_node *cgraph_node_ptr;
242 typedef struct cgraph_edge *cgraph_edge_p;
243 typedef struct varpool_node *varpool_node_ptr;
244 
change_decl_assembler_name(tree decl,tree name)245 static inline void change_decl_assembler_name(tree decl, tree name)
246 {
247 	symtab->change_decl_assembler_name(decl, name);
248 }
249 
varpool_finalize_decl(tree decl)250 static inline void varpool_finalize_decl(tree decl)
251 {
252 	varpool_node::finalize_decl(decl);
253 }
254 
varpool_add_new_variable(tree decl)255 static inline void varpool_add_new_variable(tree decl)
256 {
257 	varpool_node::add(decl);
258 }
259 
rebuild_cgraph_edges(void)260 static inline unsigned int rebuild_cgraph_edges(void)
261 {
262 	return cgraph_edge::rebuild_edges();
263 }
264 
cgraph_function_node(cgraph_node_ptr node,enum availability * availability)265 static inline cgraph_node_ptr cgraph_function_node(cgraph_node_ptr node, enum availability *availability)
266 {
267 	return node->function_symbol(availability);
268 }
269 
270 static inline cgraph_node_ptr cgraph_function_or_thunk_node(cgraph_node_ptr node, enum availability *availability = NULL)
271 {
272 	return node->ultimate_alias_target(availability);
273 }
274 
cgraph_only_called_directly_p(cgraph_node_ptr node)275 static inline bool cgraph_only_called_directly_p(cgraph_node_ptr node)
276 {
277 	return node->only_called_directly_p();
278 }
279 
cgraph_function_body_availability(cgraph_node_ptr node)280 static inline enum availability cgraph_function_body_availability(cgraph_node_ptr node)
281 {
282 	return node->get_availability();
283 }
284 
cgraph_alias_target(cgraph_node_ptr node)285 static inline cgraph_node_ptr cgraph_alias_target(cgraph_node_ptr node)
286 {
287 	return node->get_alias_target();
288 }
289 
cgraph_for_node_and_aliases(cgraph_node_ptr node,bool (* callback)(cgraph_node_ptr,void *),void * data,bool include_overwritable)290 static inline bool cgraph_for_node_and_aliases(cgraph_node_ptr node, bool (*callback)(cgraph_node_ptr, void *), void *data, bool include_overwritable)
291 {
292 	return node->call_for_symbol_thunks_and_aliases(callback, data, include_overwritable);
293 }
294 
cgraph_add_function_insertion_hook(cgraph_node_hook hook,void * data)295 static inline struct cgraph_node_hook_list *cgraph_add_function_insertion_hook(cgraph_node_hook hook, void *data)
296 {
297 	return symtab->add_cgraph_insertion_hook(hook, data);
298 }
299 
cgraph_remove_function_insertion_hook(struct cgraph_node_hook_list * entry)300 static inline void cgraph_remove_function_insertion_hook(struct cgraph_node_hook_list *entry)
301 {
302 	symtab->remove_cgraph_insertion_hook(entry);
303 }
304 
cgraph_add_node_removal_hook(cgraph_node_hook hook,void * data)305 static inline struct cgraph_node_hook_list *cgraph_add_node_removal_hook(cgraph_node_hook hook, void *data)
306 {
307 	return symtab->add_cgraph_removal_hook(hook, data);
308 }
309 
cgraph_remove_node_removal_hook(struct cgraph_node_hook_list * entry)310 static inline void cgraph_remove_node_removal_hook(struct cgraph_node_hook_list *entry)
311 {
312 	symtab->remove_cgraph_removal_hook(entry);
313 }
314 
cgraph_add_node_duplication_hook(cgraph_2node_hook hook,void * data)315 static inline struct cgraph_2node_hook_list *cgraph_add_node_duplication_hook(cgraph_2node_hook hook, void *data)
316 {
317 	return symtab->add_cgraph_duplication_hook(hook, data);
318 }
319 
cgraph_remove_node_duplication_hook(struct cgraph_2node_hook_list * entry)320 static inline void cgraph_remove_node_duplication_hook(struct cgraph_2node_hook_list *entry)
321 {
322 	symtab->remove_cgraph_duplication_hook(entry);
323 }
324 
cgraph_call_node_duplication_hooks(cgraph_node_ptr node,cgraph_node_ptr node2)325 static inline void cgraph_call_node_duplication_hooks(cgraph_node_ptr node, cgraph_node_ptr node2)
326 {
327 	symtab->call_cgraph_duplication_hooks(node, node2);
328 }
329 
cgraph_call_edge_duplication_hooks(cgraph_edge * cs1,cgraph_edge * cs2)330 static inline void cgraph_call_edge_duplication_hooks(cgraph_edge *cs1, cgraph_edge *cs2)
331 {
332 	symtab->call_edge_duplication_hooks(cs1, cs2);
333 }
334 
335 #if BUILDING_GCC_VERSION >= 6000
336 typedef gimple *gimple_ptr;
337 typedef const gimple *const_gimple_ptr;
338 #define gimple gimple_ptr
339 #define const_gimple const_gimple_ptr
340 #undef CONST_CAST_GIMPLE
341 #define CONST_CAST_GIMPLE(X) CONST_CAST(gimple, (X))
342 #endif
343 
344 /* gimple related */
gimple_build_assign_with_ops(enum tree_code subcode,tree lhs,tree op1,tree op2 MEM_STAT_DECL)345 static inline gimple gimple_build_assign_with_ops(enum tree_code subcode, tree lhs, tree op1, tree op2 MEM_STAT_DECL)
346 {
347 	return gimple_build_assign(lhs, subcode, op1, op2 PASS_MEM_STAT);
348 }
349 
350 #if BUILDING_GCC_VERSION < 10000
351 template <>
352 template <>
test(const_gimple gs)353 inline bool is_a_helper<const ggoto *>::test(const_gimple gs)
354 {
355 	return gs->code == GIMPLE_GOTO;
356 }
357 
358 template <>
359 template <>
test(const_gimple gs)360 inline bool is_a_helper<const greturn *>::test(const_gimple gs)
361 {
362 	return gs->code == GIMPLE_RETURN;
363 }
364 #endif
365 
as_a_gasm(gimple stmt)366 static inline gasm *as_a_gasm(gimple stmt)
367 {
368 	return as_a<gasm *>(stmt);
369 }
370 
as_a_const_gasm(const_gimple stmt)371 static inline const gasm *as_a_const_gasm(const_gimple stmt)
372 {
373 	return as_a<const gasm *>(stmt);
374 }
375 
as_a_gassign(gimple stmt)376 static inline gassign *as_a_gassign(gimple stmt)
377 {
378 	return as_a<gassign *>(stmt);
379 }
380 
as_a_const_gassign(const_gimple stmt)381 static inline const gassign *as_a_const_gassign(const_gimple stmt)
382 {
383 	return as_a<const gassign *>(stmt);
384 }
385 
as_a_gcall(gimple stmt)386 static inline gcall *as_a_gcall(gimple stmt)
387 {
388 	return as_a<gcall *>(stmt);
389 }
390 
as_a_const_gcall(const_gimple stmt)391 static inline const gcall *as_a_const_gcall(const_gimple stmt)
392 {
393 	return as_a<const gcall *>(stmt);
394 }
395 
as_a_ggoto(gimple stmt)396 static inline ggoto *as_a_ggoto(gimple stmt)
397 {
398 	return as_a<ggoto *>(stmt);
399 }
400 
as_a_const_ggoto(const_gimple stmt)401 static inline const ggoto *as_a_const_ggoto(const_gimple stmt)
402 {
403 	return as_a<const ggoto *>(stmt);
404 }
405 
as_a_gphi(gimple stmt)406 static inline gphi *as_a_gphi(gimple stmt)
407 {
408 	return as_a<gphi *>(stmt);
409 }
410 
as_a_const_gphi(const_gimple stmt)411 static inline const gphi *as_a_const_gphi(const_gimple stmt)
412 {
413 	return as_a<const gphi *>(stmt);
414 }
415 
as_a_greturn(gimple stmt)416 static inline greturn *as_a_greturn(gimple stmt)
417 {
418 	return as_a<greturn *>(stmt);
419 }
420 
as_a_const_greturn(const_gimple stmt)421 static inline const greturn *as_a_const_greturn(const_gimple stmt)
422 {
423 	return as_a<const greturn *>(stmt);
424 }
425 
426 /* IPA/LTO related */
427 #define ipa_ref_list_referring_iterate(L, I, P)	\
428 	(L)->referring.iterate((I), &(P))
429 #define ipa_ref_list_reference_iterate(L, I, P)	\
430 	(L)->reference.iterate((I), &(P))
431 
ipa_ref_referring_node(struct ipa_ref * ref)432 static inline cgraph_node_ptr ipa_ref_referring_node(struct ipa_ref *ref)
433 {
434 	return dyn_cast<cgraph_node_ptr>(ref->referring);
435 }
436 
ipa_remove_stmt_references(symtab_node * referring_node,gimple stmt)437 static inline void ipa_remove_stmt_references(symtab_node *referring_node, gimple stmt)
438 {
439 	referring_node->remove_stmt_references(stmt);
440 }
441 
442 #if BUILDING_GCC_VERSION < 6000
443 #define get_inner_reference(exp, pbitsize, pbitpos, poffset, pmode, punsignedp, preversep, pvolatilep, keep_aligning)	\
444 	get_inner_reference(exp, pbitsize, pbitpos, poffset, pmode, punsignedp, pvolatilep, keep_aligning)
445 #define gen_rtx_set(ARG0, ARG1) gen_rtx_SET(VOIDmode, (ARG0), (ARG1))
446 #endif
447 
448 #if BUILDING_GCC_VERSION >= 6000
449 #define gen_rtx_set(ARG0, ARG1) gen_rtx_SET((ARG0), (ARG1))
450 #endif
451 
452 #ifdef __cplusplus
debug_tree(const_tree t)453 static inline void debug_tree(const_tree t)
454 {
455 	debug_tree(CONST_CAST_TREE(t));
456 }
457 
debug_gimple_stmt(const_gimple s)458 static inline void debug_gimple_stmt(const_gimple s)
459 {
460 	debug_gimple_stmt(CONST_CAST_GIMPLE(s));
461 }
462 #else
463 #define debug_tree(t) debug_tree(CONST_CAST_TREE(t))
464 #define debug_gimple_stmt(s) debug_gimple_stmt(CONST_CAST_GIMPLE(s))
465 #endif
466 
467 #if BUILDING_GCC_VERSION >= 7000
468 #define get_inner_reference(exp, pbitsize, pbitpos, poffset, pmode, punsignedp, preversep, pvolatilep, keep_aligning)	\
469 	get_inner_reference(exp, pbitsize, pbitpos, poffset, pmode, punsignedp, preversep, pvolatilep)
470 #endif
471 
472 #if BUILDING_GCC_VERSION < 7000
473 #define SET_DECL_ALIGN(decl, align)	DECL_ALIGN(decl) = (align)
474 #define SET_DECL_MODE(decl, mode)	DECL_MODE(decl) = (mode)
475 #endif
476 
477 #if BUILDING_GCC_VERSION >= 14000
478 #define last_stmt(x)			last_nondebug_stmt(x)
479 #endif
480 
481 #endif
482