ipa-utils.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* Utilities for ipa analysis.
  2. Copyright (C) 2004-2020 Free Software Foundation, Inc.
  3. Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_IPA_UTILS_H
  17. #define GCC_IPA_UTILS_H
  18. struct ipa_dfs_info {
  19. int dfn_number;
  20. int low_link;
  21. /* This field will have the samy value for any two nodes in the same strongly
  22. connected component. */
  23. int scc_no;
  24. bool new_node;
  25. bool on_stack;
  26. struct cgraph_node* next_cycle;
  27. PTR aux;
  28. };
  29. /* In ipa-utils.c */
  30. void ipa_print_order (FILE*, const char *, struct cgraph_node**, int);
  31. int ipa_reduced_postorder (struct cgraph_node **, bool,
  32. bool (*ignore_edge) (struct cgraph_edge *));
  33. void ipa_free_postorder_info (void);
  34. vec<cgraph_node *> ipa_get_nodes_in_cycle (struct cgraph_node *);
  35. bool ipa_edge_within_scc (struct cgraph_edge *);
  36. int ipa_reverse_postorder (struct cgraph_node **);
  37. tree get_base_var (tree);
  38. void ipa_merge_profiles (struct cgraph_node *dst,
  39. struct cgraph_node *src, bool preserve_body = false);
  40. bool recursive_call_p (tree, tree);
  41. /* In ipa-profile.c */
  42. bool ipa_propagate_frequency (struct cgraph_node *node);
  43. /* In ipa-devirt.c */
  44. struct odr_type_d;
  45. typedef odr_type_d *odr_type;
  46. extern bool thunk_expansion;
  47. void build_type_inheritance_graph (void);
  48. void rebuild_type_inheritance_graph (void);
  49. void update_type_inheritance_graph (void);
  50. vec <cgraph_node *>
  51. possible_polymorphic_call_targets (tree, HOST_WIDE_INT,
  52. ipa_polymorphic_call_context,
  53. bool *copletep = NULL,
  54. void **cache_token = NULL,
  55. bool speuclative = false);
  56. odr_type get_odr_type (tree, bool insert = false);
  57. bool odr_type_p (const_tree);
  58. bool possible_polymorphic_call_target_p (tree ref, gimple *stmt, struct cgraph_node *n);
  59. void dump_possible_polymorphic_call_targets (FILE *, tree, HOST_WIDE_INT,
  60. const ipa_polymorphic_call_context &,
  61. bool verbose = true);
  62. bool possible_polymorphic_call_target_p (tree, HOST_WIDE_INT,
  63. const ipa_polymorphic_call_context &,
  64. struct cgraph_node *);
  65. tree polymorphic_ctor_dtor_p (tree, bool);
  66. tree inlined_polymorphic_ctor_dtor_block_p (tree, bool);
  67. bool decl_maybe_in_construction_p (tree, tree, gimple *, tree);
  68. tree vtable_pointer_value_to_binfo (const_tree);
  69. bool vtable_pointer_value_to_vtable (const_tree, tree *, unsigned HOST_WIDE_INT *);
  70. tree subbinfo_with_vtable_at_offset (tree, unsigned HOST_WIDE_INT, tree);
  71. void compare_virtual_tables (varpool_node *, varpool_node *);
  72. bool type_all_derivations_known_p (const_tree);
  73. bool type_known_to_have_no_derivations_p (tree);
  74. bool contains_polymorphic_type_p (const_tree);
  75. void register_odr_type (tree);
  76. bool types_must_be_same_for_odr (tree, tree);
  77. bool types_odr_comparable (tree, tree);
  78. cgraph_node *try_speculative_devirtualization (tree, HOST_WIDE_INT,
  79. ipa_polymorphic_call_context);
  80. void warn_types_mismatch (tree t1, tree t2, location_t loc1 = UNKNOWN_LOCATION,
  81. location_t loc2 = UNKNOWN_LOCATION);
  82. bool odr_or_derived_type_p (const_tree t);
  83. bool odr_types_equivalent_p (tree type1, tree type2);
  84. bool odr_type_violation_reported_p (tree type);
  85. tree prevailing_odr_type (tree type);
  86. void enable_odr_based_tbaa (tree type);
  87. bool odr_based_tbaa_p (const_tree type);
  88. void set_type_canonical_for_odr_type (tree type, tree canonical);
  89. void register_odr_enum (tree type);
  90. /* Return vector containing possible targets of polymorphic call E.
  91. If COMPLETEP is non-NULL, store true if the list is complete.
  92. CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
  93. in the target cache. If user needs to visit every target list
  94. just once, it can memoize them.
  95. Returned vector is placed into cache. It is NOT caller's responsibility
  96. to free it. The vector can be freed on cgraph_remove_node call if
  97. the particular node is a virtual function present in the cache. */
  98. inline vec <cgraph_node *>
  99. possible_polymorphic_call_targets (struct cgraph_edge *e,
  100. bool *completep = NULL,
  101. void **cache_token = NULL,
  102. bool speculative = false)
  103. {
  104. ipa_polymorphic_call_context context(e);
  105. return possible_polymorphic_call_targets (e->indirect_info->otr_type,
  106. e->indirect_info->otr_token,
  107. context,
  108. completep, cache_token,
  109. speculative);
  110. }
  111. /* Same as above but taking OBJ_TYPE_REF as an parameter. */
  112. inline vec <cgraph_node *>
  113. possible_polymorphic_call_targets (tree ref,
  114. gimple *call,
  115. bool *completep = NULL,
  116. void **cache_token = NULL)
  117. {
  118. ipa_polymorphic_call_context context (current_function_decl, ref, call);
  119. return possible_polymorphic_call_targets (obj_type_ref_class (ref),
  120. tree_to_uhwi
  121. (OBJ_TYPE_REF_TOKEN (ref)),
  122. context,
  123. completep, cache_token);
  124. }
  125. /* Dump possible targets of a polymorphic call E into F. */
  126. inline void
  127. dump_possible_polymorphic_call_targets (FILE *f, struct cgraph_edge *e,
  128. bool verbose = true)
  129. {
  130. ipa_polymorphic_call_context context(e);
  131. dump_possible_polymorphic_call_targets (f, e->indirect_info->otr_type,
  132. e->indirect_info->otr_token,
  133. context, verbose);
  134. }
  135. /* Return true if N can be possibly target of a polymorphic call of
  136. E. */
  137. inline bool
  138. possible_polymorphic_call_target_p (struct cgraph_edge *e,
  139. struct cgraph_node *n)
  140. {
  141. ipa_polymorphic_call_context context(e);
  142. return possible_polymorphic_call_target_p (e->indirect_info->otr_type,
  143. e->indirect_info->otr_token,
  144. context, n);
  145. }
  146. /* Return true if BINFO corresponds to a type with virtual methods.
  147. Every type has several BINFOs. One is the BINFO associated by the type
  148. while other represents bases of derived types. The BINFOs representing
  149. bases do not have BINFO_VTABLE pointer set when this is the single
  150. inheritance (because vtables are shared). Look up the BINFO of type
  151. and check presence of its vtable. */
  152. inline bool
  153. polymorphic_type_binfo_p (const_tree binfo)
  154. {
  155. return (BINFO_TYPE (binfo) && TYPE_BINFO (BINFO_TYPE (binfo))
  156. && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (binfo))));
  157. }
  158. /* Return true if T is a type with linkage defined. */
  159. inline bool
  160. type_with_linkage_p (const_tree t)
  161. {
  162. gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
  163. if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
  164. return false;
  165. /* After free_lang_data was run we can recongize
  166. types with linkage by presence of mangled name. */
  167. if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
  168. return true;
  169. if (in_lto_p)
  170. return false;
  171. /* We used to check for TYPE_STUB_DECL but that is set to NULL for forward
  172. declarations. */
  173. if (!RECORD_OR_UNION_TYPE_P (t) && TREE_CODE (t) != ENUMERAL_TYPE)
  174. return false;
  175. /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL. */
  176. if (!TYPE_CONTEXT (t))
  177. return false;
  178. gcc_checking_assert (TREE_CODE (t) == ENUMERAL_TYPE || TYPE_CXX_ODR_P (t));
  179. return true;
  180. }
  181. /* Return true if T is in anonymous namespace.
  182. This works only on those C++ types with linkage defined. */
  183. inline bool
  184. type_in_anonymous_namespace_p (const_tree t)
  185. {
  186. gcc_checking_assert (type_with_linkage_p (t));
  187. /* free_lang_data clears TYPE_STUB_DECL but sets assembler name to
  188. "<anon>" */
  189. if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
  190. return !strcmp ("<anon>",
  191. IDENTIFIER_POINTER
  192. (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
  193. else if (!TYPE_STUB_DECL (t))
  194. return false;
  195. else
  196. return !TREE_PUBLIC (TYPE_STUB_DECL (t));
  197. }
  198. /* Return true of T is type with One Definition Rule info attached.
  199. It means that either it is anonymous type or it has assembler name
  200. set. */
  201. inline bool
  202. odr_type_p (const_tree t)
  203. {
  204. /* We do not have this information when not in LTO, but we do not need
  205. to care, since it is used only for type merging. */
  206. gcc_checking_assert (in_lto_p || flag_lto || flag_generate_offload);
  207. return TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
  208. && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t));
  209. }
  210. /* If TYPE has mangled ODR name, return it. Otherwise return NULL.
  211. The function works only when free_lang_data is run. */
  212. inline const char *
  213. get_odr_name_for_type (tree type)
  214. {
  215. tree type_name = TYPE_NAME (type);
  216. if (type_name == NULL_TREE
  217. || TREE_CODE (type_name) != TYPE_DECL
  218. || !DECL_ASSEMBLER_NAME_SET_P (type_name))
  219. return NULL;
  220. return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (type_name));
  221. }
  222. #endif /* GCC_IPA_UTILS_H */