ipa-utils.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Utilities for ipa analysis.
  2. Copyright (C) 2004-2019 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. void build_type_inheritance_graph (void);
  47. void rebuild_type_inheritance_graph (void);
  48. void update_type_inheritance_graph (void);
  49. vec <cgraph_node *>
  50. possible_polymorphic_call_targets (tree, HOST_WIDE_INT,
  51. ipa_polymorphic_call_context,
  52. bool *copletep = NULL,
  53. void **cache_token = NULL,
  54. bool speuclative = false);
  55. odr_type get_odr_type (tree, bool insert = false);
  56. bool odr_type_p (const_tree);
  57. bool possible_polymorphic_call_target_p (tree ref, gimple *stmt, struct cgraph_node *n);
  58. void dump_possible_polymorphic_call_targets (FILE *, tree, HOST_WIDE_INT,
  59. const ipa_polymorphic_call_context &,
  60. bool verbose = true);
  61. bool possible_polymorphic_call_target_p (tree, HOST_WIDE_INT,
  62. const ipa_polymorphic_call_context &,
  63. struct cgraph_node *);
  64. tree polymorphic_ctor_dtor_p (tree, bool);
  65. tree inlined_polymorphic_ctor_dtor_block_p (tree, bool);
  66. bool decl_maybe_in_construction_p (tree, tree, gimple *, tree);
  67. tree vtable_pointer_value_to_binfo (const_tree);
  68. bool vtable_pointer_value_to_vtable (const_tree, tree *, unsigned HOST_WIDE_INT *);
  69. tree subbinfo_with_vtable_at_offset (tree, unsigned HOST_WIDE_INT, tree);
  70. void compare_virtual_tables (varpool_node *, varpool_node *);
  71. bool type_all_derivations_known_p (const_tree);
  72. bool type_known_to_have_no_derivations_p (tree);
  73. bool contains_polymorphic_type_p (const_tree);
  74. void register_odr_type (tree);
  75. bool types_must_be_same_for_odr (tree, tree);
  76. bool types_odr_comparable (tree, tree);
  77. cgraph_node *try_speculative_devirtualization (tree, HOST_WIDE_INT,
  78. ipa_polymorphic_call_context);
  79. void warn_types_mismatch (tree t1, tree t2, location_t loc1 = UNKNOWN_LOCATION,
  80. location_t loc2 = UNKNOWN_LOCATION);
  81. bool odr_or_derived_type_p (const_tree t);
  82. bool odr_types_equivalent_p (tree type1, tree type2);
  83. bool odr_type_violation_reported_p (tree type);
  84. /* Return vector containing possible targets of polymorphic call E.
  85. If COMPLETEP is non-NULL, store true if the list is complete.
  86. CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
  87. in the target cache. If user needs to visit every target list
  88. just once, it can memoize them.
  89. Returned vector is placed into cache. It is NOT caller's responsibility
  90. to free it. The vector can be freed on cgraph_remove_node call if
  91. the particular node is a virtual function present in the cache. */
  92. inline vec <cgraph_node *>
  93. possible_polymorphic_call_targets (struct cgraph_edge *e,
  94. bool *completep = NULL,
  95. void **cache_token = NULL,
  96. bool speculative = false)
  97. {
  98. ipa_polymorphic_call_context context(e);
  99. return possible_polymorphic_call_targets (e->indirect_info->otr_type,
  100. e->indirect_info->otr_token,
  101. context,
  102. completep, cache_token,
  103. speculative);
  104. }
  105. /* Same as above but taking OBJ_TYPE_REF as an parameter. */
  106. inline vec <cgraph_node *>
  107. possible_polymorphic_call_targets (tree ref,
  108. gimple *call,
  109. bool *completep = NULL,
  110. void **cache_token = NULL)
  111. {
  112. ipa_polymorphic_call_context context (current_function_decl, ref, call);
  113. return possible_polymorphic_call_targets (obj_type_ref_class (ref),
  114. tree_to_uhwi
  115. (OBJ_TYPE_REF_TOKEN (ref)),
  116. context,
  117. completep, cache_token);
  118. }
  119. /* Dump possible targets of a polymorphic call E into F. */
  120. inline void
  121. dump_possible_polymorphic_call_targets (FILE *f, struct cgraph_edge *e,
  122. bool verbose = true)
  123. {
  124. ipa_polymorphic_call_context context(e);
  125. dump_possible_polymorphic_call_targets (f, e->indirect_info->otr_type,
  126. e->indirect_info->otr_token,
  127. context, verbose);
  128. }
  129. /* Return true if N can be possibly target of a polymorphic call of
  130. E. */
  131. inline bool
  132. possible_polymorphic_call_target_p (struct cgraph_edge *e,
  133. struct cgraph_node *n)
  134. {
  135. ipa_polymorphic_call_context context(e);
  136. return possible_polymorphic_call_target_p (e->indirect_info->otr_type,
  137. e->indirect_info->otr_token,
  138. context, n);
  139. }
  140. /* Return true if BINFO corresponds to a type with virtual methods.
  141. Every type has several BINFOs. One is the BINFO associated by the type
  142. while other represents bases of derived types. The BINFOs representing
  143. bases do not have BINFO_VTABLE pointer set when this is the single
  144. inheritance (because vtables are shared). Look up the BINFO of type
  145. and check presence of its vtable. */
  146. inline bool
  147. polymorphic_type_binfo_p (const_tree binfo)
  148. {
  149. return (BINFO_TYPE (binfo) && TYPE_BINFO (BINFO_TYPE (binfo))
  150. && BINFO_VTABLE (TYPE_BINFO (BINFO_TYPE (binfo))));
  151. }
  152. /* Return true if T is a type with linkage defined. */
  153. inline bool
  154. type_with_linkage_p (const_tree t)
  155. {
  156. gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
  157. if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
  158. return false;
  159. /* To support -fno-lto-odr-type-merigng recognize types with vtables
  160. to have linkage. */
  161. if (RECORD_OR_UNION_TYPE_P (t)
  162. && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)))
  163. return true;
  164. /* After free_lang_data was run and -flto-odr-type-merging we can recongize
  165. types with linkage by presence of mangled name. */
  166. if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
  167. return true;
  168. if (in_lto_p)
  169. return false;
  170. /* We used to check for TYPE_STUB_DECL but that is set to NULL for forward
  171. declarations. */
  172. if (!RECORD_OR_UNION_TYPE_P (t) && TREE_CODE (t) != ENUMERAL_TYPE)
  173. return false;
  174. /* Builtin types do not define linkage, their TYPE_CONTEXT is NULL. */
  175. if (!TYPE_CONTEXT (t))
  176. return false;
  177. return true;
  178. }
  179. /* Return true if T is in anonymous namespace.
  180. This works only on those C++ types with linkage defined. */
  181. inline bool
  182. type_in_anonymous_namespace_p (const_tree t)
  183. {
  184. gcc_checking_assert (type_with_linkage_p (t));
  185. /* free_lang_data clears TYPE_STUB_DECL but sets assembler name to
  186. "<anon>" */
  187. if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
  188. return !strcmp ("<anon>",
  189. IDENTIFIER_POINTER
  190. (DECL_ASSEMBLER_NAME (TYPE_NAME (t))));
  191. else if (!TYPE_STUB_DECL (t))
  192. return false;
  193. else
  194. return !TREE_PUBLIC (TYPE_STUB_DECL (t));
  195. }
  196. /* Return true of T is type with One Definition Rule info attached.
  197. It means that either it is anonymous type or it has assembler name
  198. set. */
  199. inline bool
  200. odr_type_p (const_tree t)
  201. {
  202. /* We do not have this information when not in LTO, but we do not need
  203. to care, since it is used only for type merging. */
  204. gcc_checking_assert (in_lto_p || flag_lto);
  205. if (!type_with_linkage_p (t))
  206. return false;
  207. /* To support -fno-lto-odr-type-merging consider types with vtables ODR. */
  208. if (type_in_anonymous_namespace_p (t))
  209. return true;
  210. if (TYPE_NAME (t) && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)))
  211. {
  212. /* C++ FE uses magic <anon> as assembler names of anonymous types.
  213. verify that this match with type_in_anonymous_namespace_p. */
  214. gcc_checking_assert (strcmp ("<anon>",
  215. IDENTIFIER_POINTER
  216. (DECL_ASSEMBLER_NAME (TYPE_NAME (t)))));
  217. return true;
  218. }
  219. return false;
  220. }
  221. #endif /* GCC_IPA_UTILS_H */