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