cfghooks.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Hooks for cfg representation specific functions.
  2. Copyright (C) 2003-2019 Free Software Foundation, Inc.
  3. Contributed by Sebastian Pop <s.pop@laposte.net>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License 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_CFGHOOKS_H
  17. #define GCC_CFGHOOKS_H
  18. #include "predict.h"
  19. /* Structure to gather statistic about profile consistency, per pass.
  20. An array of this structure, indexed by pass static number, is allocated
  21. in passes.c. The structure is defined here so that different CFG modes
  22. can do their book-keeping via CFG hooks.
  23. For every field[2], field[0] is the count before the pass runs, and
  24. field[1] is the post-pass count. This allows us to monitor the effect
  25. of each individual pass on the profile consistency.
  26. This structure is not supposed to be used by anything other than passes.c
  27. and one CFG hook per CFG mode. */
  28. struct profile_record
  29. {
  30. /* The number of basic blocks where sum(freq) of the block's predecessors
  31. doesn't match reasonably well with the incoming frequency. */
  32. int num_mismatched_freq_in;
  33. /* Likewise for a basic block's successors. */
  34. int num_mismatched_freq_out;
  35. /* The number of basic blocks where sum(count) of the block's predecessors
  36. doesn't match reasonably well with the incoming frequency. */
  37. int num_mismatched_count_in;
  38. /* Likewise for a basic block's successors. */
  39. int num_mismatched_count_out;
  40. /* A weighted cost of the run-time of the function body. */
  41. gcov_type_unsigned time;
  42. /* A weighted cost of the size of the function body. */
  43. int size;
  44. /* True iff this pass actually was run. */
  45. bool run;
  46. };
  47. typedef int_hash <unsigned short, 0> dependence_hash;
  48. /* Optional data for duplicate_block. */
  49. struct copy_bb_data
  50. {
  51. copy_bb_data() : dependence_map (NULL) {}
  52. ~copy_bb_data () { delete dependence_map; }
  53. /* A map from the copied BBs dependence info cliques to
  54. equivalents in the BBs duplicated to. */
  55. hash_map<dependence_hash, unsigned short> *dependence_map;
  56. };
  57. struct cfg_hooks
  58. {
  59. /* Name of the corresponding ir. */
  60. const char *name;
  61. /* Debugging. */
  62. int (*verify_flow_info) (void);
  63. void (*dump_bb) (FILE *, basic_block, int, dump_flags_t);
  64. void (*dump_bb_for_graph) (pretty_printer *, basic_block);
  65. /* Basic CFG manipulation. */
  66. /* Return new basic block. */
  67. basic_block (*create_basic_block) (void *head, void *end, basic_block after);
  68. /* Redirect edge E to the given basic block B and update underlying program
  69. representation. Returns edge representing redirected branch (that may not
  70. be equivalent to E in the case of duplicate edges being removed) or NULL
  71. if edge is not easily redirectable for whatever reason. */
  72. edge (*redirect_edge_and_branch) (edge e, basic_block b);
  73. /* Same as the above but allows redirecting of fallthru edges. In that case
  74. newly created forwarder basic block is returned. The edge must
  75. not be abnormal. */
  76. basic_block (*redirect_edge_and_branch_force) (edge, basic_block);
  77. /* Returns true if it is possible to remove the edge by redirecting it
  78. to the destination of the other edge going from its source. */
  79. bool (*can_remove_branch_p) (const_edge);
  80. /* Remove statements corresponding to a given basic block. */
  81. void (*delete_basic_block) (basic_block);
  82. /* Creates a new basic block just after basic block B by splitting
  83. everything after specified instruction I. */
  84. basic_block (*split_block) (basic_block b, void * i);
  85. /* Move block B immediately after block A. */
  86. bool (*move_block_after) (basic_block b, basic_block a);
  87. /* Return true when blocks A and B can be merged into single basic block. */
  88. bool (*can_merge_blocks_p) (basic_block a, basic_block b);
  89. /* Merge blocks A and B. */
  90. void (*merge_blocks) (basic_block a, basic_block b);
  91. /* Predict edge E using PREDICTOR to given PROBABILITY. */
  92. void (*predict_edge) (edge e, enum br_predictor predictor, int probability);
  93. /* Return true if the one of outgoing edges is already predicted by
  94. PREDICTOR. */
  95. bool (*predicted_by_p) (const_basic_block bb, enum br_predictor predictor);
  96. /* Return true when block A can be duplicated. */
  97. bool (*can_duplicate_block_p) (const_basic_block a);
  98. /* Duplicate block A. */
  99. basic_block (*duplicate_block) (basic_block a, copy_bb_data *);
  100. /* Higher level functions representable by primitive operations above if
  101. we didn't have some oddities in RTL and Tree representations. */
  102. basic_block (*split_edge) (edge);
  103. void (*make_forwarder_block) (edge);
  104. /* Try to make the edge fallthru. */
  105. void (*tidy_fallthru_edge) (edge);
  106. /* Make the edge non-fallthru. */
  107. basic_block (*force_nonfallthru) (edge);
  108. /* Say whether a block ends with a call, possibly followed by some
  109. other code that must stay with the call. */
  110. bool (*block_ends_with_call_p) (basic_block);
  111. /* Say whether a block ends with a conditional branch. Switches
  112. and unconditional branches do not qualify. */
  113. bool (*block_ends_with_condjump_p) (const_basic_block);
  114. /* Add fake edges to the function exit for any non constant and non noreturn
  115. calls, volatile inline assembly in the bitmap of blocks specified by
  116. BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
  117. that were split.
  118. The goal is to expose cases in which entering a basic block does not imply
  119. that all subsequent instructions must be executed. */
  120. int (*flow_call_edges_add) (sbitmap);
  121. /* This function is called immediately after edge E is added to the
  122. edge vector E->dest->preds. */
  123. void (*execute_on_growing_pred) (edge);
  124. /* This function is called immediately before edge E is removed from
  125. the edge vector E->dest->preds. */
  126. void (*execute_on_shrinking_pred) (edge);
  127. /* A hook for duplicating loop in CFG, currently this is used
  128. in loop versioning. */
  129. bool (*cfg_hook_duplicate_loop_to_header_edge) (struct loop *, edge,
  130. unsigned, sbitmap,
  131. edge, vec<edge> *,
  132. int);
  133. /* Add condition to new basic block and update CFG used in loop
  134. versioning. */
  135. void (*lv_add_condition_to_bb) (basic_block, basic_block, basic_block,
  136. void *);
  137. /* Update the PHI nodes in case of loop versioning. */
  138. void (*lv_adjust_loop_header_phi) (basic_block, basic_block,
  139. basic_block, edge);
  140. /* Given a condition BB extract the true/false taken/not taken edges
  141. (depending if we are on tree's or RTL). */
  142. void (*extract_cond_bb_edges) (basic_block, edge *, edge *);
  143. /* Add PHI arguments queued in PENDINT_STMT list on edge E to edge
  144. E->dest (only in tree-ssa loop versioning. */
  145. void (*flush_pending_stmts) (edge);
  146. /* True if a block contains no executable instructions. */
  147. bool (*empty_block_p) (basic_block);
  148. /* Split a basic block if it ends with a conditional branch and if
  149. the other part of the block is not empty. */
  150. basic_block (*split_block_before_cond_jump) (basic_block);
  151. /* Do book-keeping of a basic block for the profile consistency checker. */
  152. void (*account_profile_record) (basic_block, struct profile_record *);
  153. };
  154. extern void verify_flow_info (void);
  155. /* Check control flow invariants, if internal consistency checks are
  156. enabled. */
  157. static inline void
  158. checking_verify_flow_info (void)
  159. {
  160. /* TODO: Add a separate option for -fchecking=cfg. */
  161. if (flag_checking)
  162. verify_flow_info ();
  163. }
  164. extern void dump_bb (FILE *, basic_block, int, dump_flags_t);
  165. extern void dump_bb_for_graph (pretty_printer *, basic_block);
  166. extern void dump_flow_info (FILE *, dump_flags_t);
  167. extern edge redirect_edge_and_branch (edge, basic_block);
  168. extern basic_block redirect_edge_and_branch_force (edge, basic_block);
  169. extern edge redirect_edge_succ_nodup (edge, basic_block);
  170. extern bool can_remove_branch_p (const_edge);
  171. extern void remove_branch (edge);
  172. extern void remove_edge (edge);
  173. extern edge split_block (basic_block, rtx);
  174. extern edge split_block (basic_block, gimple *);
  175. extern edge split_block_after_labels (basic_block);
  176. extern bool move_block_after (basic_block, basic_block);
  177. extern void delete_basic_block (basic_block);
  178. extern basic_block split_edge (edge);
  179. extern basic_block create_basic_block (rtx, rtx, basic_block);
  180. extern basic_block create_basic_block (gimple_seq, basic_block);
  181. extern basic_block create_empty_bb (basic_block);
  182. extern bool can_merge_blocks_p (basic_block, basic_block);
  183. extern void merge_blocks (basic_block, basic_block);
  184. extern edge make_forwarder_block (basic_block, bool (*)(edge),
  185. void (*) (basic_block));
  186. extern basic_block force_nonfallthru (edge);
  187. extern void tidy_fallthru_edge (edge);
  188. extern void tidy_fallthru_edges (void);
  189. extern void predict_edge (edge e, enum br_predictor predictor, int probability);
  190. extern bool predicted_by_p (const_basic_block bb, enum br_predictor predictor);
  191. extern bool can_duplicate_block_p (const_basic_block);
  192. extern basic_block duplicate_block (basic_block, edge, basic_block,
  193. copy_bb_data * = NULL);
  194. extern bool block_ends_with_call_p (basic_block bb);
  195. extern bool empty_block_p (basic_block);
  196. extern basic_block split_block_before_cond_jump (basic_block);
  197. extern bool block_ends_with_condjump_p (const_basic_block bb);
  198. extern int flow_call_edges_add (sbitmap);
  199. extern void execute_on_growing_pred (edge);
  200. extern void execute_on_shrinking_pred (edge);
  201. extern bool cfg_hook_duplicate_loop_to_header_edge (struct loop *loop, edge,
  202. unsigned int ndupl,
  203. sbitmap wont_exit,
  204. edge orig,
  205. vec<edge> *to_remove,
  206. int flags);
  207. extern void lv_flush_pending_stmts (edge);
  208. extern void extract_cond_bb_edges (basic_block, edge *, edge*);
  209. extern void lv_adjust_loop_header_phi (basic_block, basic_block, basic_block,
  210. edge);
  211. extern void lv_add_condition_to_bb (basic_block, basic_block, basic_block,
  212. void *);
  213. extern bool can_copy_bbs_p (basic_block *, unsigned);
  214. extern void copy_bbs (basic_block *, unsigned, basic_block *,
  215. edge *, unsigned, edge *, struct loop *,
  216. basic_block, bool);
  217. void profile_record_check_consistency (profile_record *);
  218. void profile_record_account_profile (profile_record *);
  219. /* Hooks containers. */
  220. extern struct cfg_hooks gimple_cfg_hooks;
  221. extern struct cfg_hooks rtl_cfg_hooks;
  222. extern struct cfg_hooks cfg_layout_rtl_cfg_hooks;
  223. /* Declarations. */
  224. extern enum ir_type current_ir_type (void);
  225. extern void rtl_register_cfg_hooks (void);
  226. extern void cfg_layout_rtl_register_cfg_hooks (void);
  227. extern void gimple_register_cfg_hooks (void);
  228. extern struct cfg_hooks get_cfg_hooks (void);
  229. extern void set_cfg_hooks (struct cfg_hooks);
  230. #endif /* GCC_CFGHOOKS_H */