You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Maintaining-the-CFG.html 11KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Maintaining the CFG (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Maintaining the CFG (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Maintaining the CFG (GNU Compiler Collection (GCC) Internals)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Control-Flow.html#Control-Flow" rel="up" title="Control Flow">
  30. <link href="Liveness-information.html#Liveness-information" rel="next" title="Liveness information">
  31. <link href="Profile-information.html#Profile-information" rel="prev" title="Profile information">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Maintaining-the-CFG"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Liveness-information.html#Liveness-information" accesskey="n" rel="next">Liveness information</a>, Previous: <a href="Profile-information.html#Profile-information" accesskey="p" rel="prev">Profile information</a>, Up: <a href="Control-Flow.html#Control-Flow" accesskey="u" rel="up">Control Flow</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Maintaining-the-CFG-1"></a>
  68. <h3 class="section">15.4 Maintaining the CFG</h3>
  69. <a name="index-cfghooks_002eh"></a>
  70. <p>An important task of each compiler pass is to keep both the control
  71. flow graph and all profile information up-to-date. Reconstruction of
  72. the control flow graph after each pass is not an option, since it may be
  73. very expensive and lost profile information cannot be reconstructed at
  74. all.
  75. </p>
  76. <p>GCC has two major intermediate representations, and both use the
  77. <code>basic_block</code> and <code>edge</code> data types to represent control
  78. flow. Both representations share as much of the CFG maintenance code
  79. as possible. For each representation, a set of <em>hooks</em> is defined
  80. so that each representation can provide its own implementation of CFG
  81. manipulation routines when necessary. These hooks are defined in
  82. <samp>cfghooks.h</samp>. There are hooks for almost all common CFG
  83. manipulations, including block splitting and merging, edge redirection
  84. and creating and deleting basic blocks. These hooks should provide
  85. everything you need to maintain and manipulate the CFG in both the RTL
  86. and <code>GIMPLE</code> representation.
  87. </p>
  88. <p>At the moment, the basic block boundaries are maintained transparently
  89. when modifying instructions, so there rarely is a need to move them
  90. manually (such as in case someone wants to output instruction outside
  91. basic block explicitly).
  92. </p>
  93. <a name="index-BLOCK_005fFOR_005fINSN_002c-gimple_005fbb"></a>
  94. <p>In the RTL representation, each instruction has a
  95. <code>BLOCK_FOR_INSN</code> value that represents pointer to the basic block
  96. that contains the instruction. In the <code>GIMPLE</code> representation, the
  97. function <code>gimple_bb</code> returns a pointer to the basic block
  98. containing the queried statement.
  99. </p>
  100. <a name="index-GIMPLE-statement-iterators-1"></a>
  101. <p>When changes need to be applied to a function in its <code>GIMPLE</code>
  102. representation, <em>GIMPLE statement iterators</em> should be used. These
  103. iterators provide an integrated abstraction of the flow graph and the
  104. instruction stream. Block statement iterators are constructed using
  105. the <code>gimple_stmt_iterator</code> data structure and several modifiers are
  106. available, including the following:
  107. </p>
  108. <dl compact="compact">
  109. <dt><code>gsi_start</code>
  110. <a name="index-gsi_005fstart-1"></a>
  111. </dt>
  112. <dd><p>This function initializes a <code>gimple_stmt_iterator</code> that points to
  113. the first non-empty statement in a basic block.
  114. </p>
  115. </dd>
  116. <dt><code>gsi_last</code>
  117. <a name="index-gsi_005flast-1"></a>
  118. </dt>
  119. <dd><p>This function initializes a <code>gimple_stmt_iterator</code> that points to
  120. the last statement in a basic block.
  121. </p>
  122. </dd>
  123. <dt><code>gsi_end_p</code>
  124. <a name="index-gsi_005fend_005fp-1"></a>
  125. </dt>
  126. <dd><p>This predicate is <code>true</code> if a <code>gimple_stmt_iterator</code>
  127. represents the end of a basic block.
  128. </p>
  129. </dd>
  130. <dt><code>gsi_next</code>
  131. <a name="index-gsi_005fnext-1"></a>
  132. </dt>
  133. <dd><p>This function takes a <code>gimple_stmt_iterator</code> and makes it point to
  134. its successor.
  135. </p>
  136. </dd>
  137. <dt><code>gsi_prev</code>
  138. <a name="index-gsi_005fprev-1"></a>
  139. </dt>
  140. <dd><p>This function takes a <code>gimple_stmt_iterator</code> and makes it point to
  141. its predecessor.
  142. </p>
  143. </dd>
  144. <dt><code>gsi_insert_after</code>
  145. <a name="index-gsi_005finsert_005fafter-1"></a>
  146. </dt>
  147. <dd><p>This function inserts a statement after the <code>gimple_stmt_iterator</code>
  148. passed in. The final parameter determines whether the statement
  149. iterator is updated to point to the newly inserted statement, or left
  150. pointing to the original statement.
  151. </p>
  152. </dd>
  153. <dt><code>gsi_insert_before</code>
  154. <a name="index-gsi_005finsert_005fbefore-1"></a>
  155. </dt>
  156. <dd><p>This function inserts a statement before the <code>gimple_stmt_iterator</code>
  157. passed in. The final parameter determines whether the statement
  158. iterator is updated to point to the newly inserted statement, or left
  159. pointing to the original statement.
  160. </p>
  161. </dd>
  162. <dt><code>gsi_remove</code>
  163. <a name="index-gsi_005fremove-1"></a>
  164. </dt>
  165. <dd><p>This function removes the <code>gimple_stmt_iterator</code> passed in and
  166. rechains the remaining statements in a basic block, if any.
  167. </p></dd>
  168. </dl>
  169. <a name="index-BB_005fHEAD_002c-BB_005fEND"></a>
  170. <p>In the RTL representation, the macros <code>BB_HEAD</code> and <code>BB_END</code>
  171. may be used to get the head and end <code>rtx</code> of a basic block. No
  172. abstract iterators are defined for traversing the insn chain, but you
  173. can just use <code>NEXT_INSN</code> and <code>PREV_INSN</code> instead. See <a href="Insns.html#Insns">Insns</a>.
  174. </p>
  175. <a name="index-purge_005fdead_005fedges-1"></a>
  176. <p>Usually a code manipulating pass simplifies the instruction stream and
  177. the flow of control, possibly eliminating some edges. This may for
  178. example happen when a conditional jump is replaced with an
  179. unconditional jump. Updating of edges
  180. is not transparent and each optimization pass is required to do so
  181. manually. However only few cases occur in practice. The pass may
  182. call <code>purge_dead_edges</code> on a given basic block to remove
  183. superfluous edges, if any.
  184. </p>
  185. <a name="index-redirect_005fedge_005fand_005fbranch_002c-redirect_005fjump"></a>
  186. <p>Another common scenario is redirection of branch instructions, but
  187. this is best modeled as redirection of edges in the control flow graph
  188. and thus use of <code>redirect_edge_and_branch</code> is preferred over more
  189. low level functions, such as <code>redirect_jump</code> that operate on RTL
  190. chain only. The CFG hooks defined in <samp>cfghooks.h</samp> should provide
  191. the complete API required for manipulating and maintaining the CFG.
  192. </p>
  193. <a name="index-split_005fblock"></a>
  194. <p>It is also possible that a pass has to insert control flow instruction
  195. into the middle of a basic block, thus creating an entry point in the
  196. middle of the basic block, which is impossible by definition: The
  197. block must be split to make sure it only has one entry point, i.e. the
  198. head of the basic block. The CFG hook <code>split_block</code> may be used
  199. when an instruction in the middle of a basic block has to become the
  200. target of a jump or branch instruction.
  201. </p>
  202. <a name="index-insert_005finsn_005fon_005fedge"></a>
  203. <a name="index-commit_005fedge_005finsertions"></a>
  204. <a name="index-gsi_005finsert_005fon_005fedge-1"></a>
  205. <a name="index-gsi_005fcommit_005fedge_005finserts-1"></a>
  206. <a name="index-edge-splitting"></a>
  207. <p>For a global optimizer, a common operation is to split edges in the
  208. flow graph and insert instructions on them. In the RTL
  209. representation, this can be easily done using the
  210. <code>insert_insn_on_edge</code> function that emits an instruction
  211. &ldquo;on the edge&rdquo;, caching it for a later <code>commit_edge_insertions</code>
  212. call that will take care of moving the inserted instructions off the
  213. edge into the instruction stream contained in a basic block. This
  214. includes the creation of new basic blocks where needed. In the
  215. <code>GIMPLE</code> representation, the equivalent functions are
  216. <code>gsi_insert_on_edge</code> which inserts a block statement
  217. iterator on an edge, and <code>gsi_commit_edge_inserts</code> which flushes
  218. the instruction to actual instruction stream.
  219. </p>
  220. <a name="index-verify_005fflow_005finfo"></a>
  221. <a name="index-CFG-verification"></a>
  222. <p>While debugging the optimization pass, the <code>verify_flow_info</code>
  223. function may be useful to find bugs in the control flow graph updating
  224. code.
  225. </p>
  226. <hr>
  227. <div class="header">
  228. <p>
  229. Next: <a href="Liveness-information.html#Liveness-information" accesskey="n" rel="next">Liveness information</a>, Previous: <a href="Profile-information.html#Profile-information" accesskey="p" rel="prev">Profile information</a>, Up: <a href="Control-Flow.html#Control-Flow" accesskey="u" rel="up">Control Flow</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  230. </div>
  231. </body>
  232. </html>