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