Maintaining-the-CFG.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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-2017 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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>GNU Compiler Collection (GCC) Internals: Maintaining the CFG</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Maintaining the CFG">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Maintaining the CFG">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Maintaining-the-CFG"></a>
  63. <div class="header">
  64. <p>
  65. 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>
  66. </div>
  67. <hr>
  68. <a name="Maintaining-the-CFG-1"></a>
  69. <h3 class="section">14.4 Maintaining the CFG</h3>
  70. <a name="index-cfghooks_002eh"></a>
  71. <p>An important task of each compiler pass is to keep both the control
  72. flow graph and all profile information up-to-date. Reconstruction of
  73. the control flow graph after each pass is not an option, since it may be
  74. very expensive and lost profile information cannot be reconstructed at
  75. all.
  76. </p>
  77. <p>GCC has two major intermediate representations, and both use the
  78. <code>basic_block</code> and <code>edge</code> data types to represent control
  79. flow. Both representations share as much of the CFG maintenance code
  80. as possible. For each representation, a set of <em>hooks</em> is defined
  81. so that each representation can provide its own implementation of CFG
  82. manipulation routines when necessary. These hooks are defined in
  83. <samp>cfghooks.h</samp>. There are hooks for almost all common CFG
  84. manipulations, including block splitting and merging, edge redirection
  85. and creating and deleting basic blocks. These hooks should provide
  86. everything you need to maintain and manipulate the CFG in both the RTL
  87. and <code>GIMPLE</code> representation.
  88. </p>
  89. <p>At the moment, the basic block boundaries are maintained transparently
  90. when modifying instructions, so there rarely is a need to move them
  91. manually (such as in case someone wants to output instruction outside
  92. basic block explicitly).
  93. </p>
  94. <a name="index-BLOCK_005fFOR_005fINSN_002c-gimple_005fbb"></a>
  95. <p>In the RTL representation, each instruction has a
  96. <code>BLOCK_FOR_INSN</code> value that represents pointer to the basic block
  97. that contains the instruction. In the <code>GIMPLE</code> representation, the
  98. function <code>gimple_bb</code> returns a pointer to the basic block
  99. containing the queried statement.
  100. </p>
  101. <a name="index-GIMPLE-statement-iterators-1"></a>
  102. <p>When changes need to be applied to a function in its <code>GIMPLE</code>
  103. representation, <em>GIMPLE statement iterators</em> should be used. These
  104. iterators provide an integrated abstraction of the flow graph and the
  105. instruction stream. Block statement iterators are constructed using
  106. the <code>gimple_stmt_iterator</code> data structure and several modifiers are
  107. available, including the following:
  108. </p>
  109. <dl compact="compact">
  110. <dt><code>gsi_start</code>
  111. <a name="index-gsi_005fstart-1"></a>
  112. </dt>
  113. <dd><p>This function initializes a <code>gimple_stmt_iterator</code> that points to
  114. the first non-empty statement in a basic block.
  115. </p>
  116. </dd>
  117. <dt><code>gsi_last</code>
  118. <a name="index-gsi_005flast-1"></a>
  119. </dt>
  120. <dd><p>This function initializes a <code>gimple_stmt_iterator</code> that points to
  121. the last statement in a basic block.
  122. </p>
  123. </dd>
  124. <dt><code>gsi_end_p</code>
  125. <a name="index-gsi_005fend_005fp-1"></a>
  126. </dt>
  127. <dd><p>This predicate is <code>true</code> if a <code>gimple_stmt_iterator</code>
  128. represents the end of a basic block.
  129. </p>
  130. </dd>
  131. <dt><code>gsi_next</code>
  132. <a name="index-gsi_005fnext-1"></a>
  133. </dt>
  134. <dd><p>This function takes a <code>gimple_stmt_iterator</code> and makes it point to
  135. its successor.
  136. </p>
  137. </dd>
  138. <dt><code>gsi_prev</code>
  139. <a name="index-gsi_005fprev-1"></a>
  140. </dt>
  141. <dd><p>This function takes a <code>gimple_stmt_iterator</code> and makes it point to
  142. its predecessor.
  143. </p>
  144. </dd>
  145. <dt><code>gsi_insert_after</code>
  146. <a name="index-gsi_005finsert_005fafter-1"></a>
  147. </dt>
  148. <dd><p>This function inserts a statement after the <code>gimple_stmt_iterator</code>
  149. passed in. The final parameter determines whether the statement
  150. iterator is updated to point to the newly inserted statement, or left
  151. pointing to the original statement.
  152. </p>
  153. </dd>
  154. <dt><code>gsi_insert_before</code>
  155. <a name="index-gsi_005finsert_005fbefore-1"></a>
  156. </dt>
  157. <dd><p>This function inserts a statement before the <code>gimple_stmt_iterator</code>
  158. passed in. The final parameter determines whether the statement
  159. iterator is updated to point to the newly inserted statement, or left
  160. pointing to the original statement.
  161. </p>
  162. </dd>
  163. <dt><code>gsi_remove</code>
  164. <a name="index-gsi_005fremove-1"></a>
  165. </dt>
  166. <dd><p>This function removes the <code>gimple_stmt_iterator</code> passed in and
  167. rechains the remaining statements in a basic block, if any.
  168. </p></dd>
  169. </dl>
  170. <a name="index-BB_005fHEAD_002c-BB_005fEND"></a>
  171. <p>In the RTL representation, the macros <code>BB_HEAD</code> and <code>BB_END</code>
  172. may be used to get the head and end <code>rtx</code> of a basic block. No
  173. abstract iterators are defined for traversing the insn chain, but you
  174. can just use <code>NEXT_INSN</code> and <code>PREV_INSN</code> instead. See <a href="Insns.html#Insns">Insns</a>.
  175. </p>
  176. <a name="index-purge_005fdead_005fedges-1"></a>
  177. <p>Usually a code manipulating pass simplifies the instruction stream and
  178. the flow of control, possibly eliminating some edges. This may for
  179. example happen when a conditional jump is replaced with an
  180. unconditional jump. Updating of edges
  181. is not transparent and each optimization pass is required to do so
  182. manually. However only few cases occur in practice. The pass may
  183. call <code>purge_dead_edges</code> on a given basic block to remove
  184. superfluous edges, if any.
  185. </p>
  186. <a name="index-redirect_005fedge_005fand_005fbranch_002c-redirect_005fjump"></a>
  187. <p>Another common scenario is redirection of branch instructions, but
  188. this is best modeled as redirection of edges in the control flow graph
  189. and thus use of <code>redirect_edge_and_branch</code> is preferred over more
  190. low level functions, such as <code>redirect_jump</code> that operate on RTL
  191. chain only. The CFG hooks defined in <samp>cfghooks.h</samp> should provide
  192. the complete API required for manipulating and maintaining the CFG.
  193. </p>
  194. <a name="index-split_005fblock"></a>
  195. <p>It is also possible that a pass has to insert control flow instruction
  196. into the middle of a basic block, thus creating an entry point in the
  197. middle of the basic block, which is impossible by definition: The
  198. block must be split to make sure it only has one entry point, i.e. the
  199. head of the basic block. The CFG hook <code>split_block</code> may be used
  200. when an instruction in the middle of a basic block has to become the
  201. target of a jump or branch instruction.
  202. </p>
  203. <a name="index-insert_005finsn_005fon_005fedge"></a>
  204. <a name="index-commit_005fedge_005finsertions"></a>
  205. <a name="index-gsi_005finsert_005fon_005fedge-1"></a>
  206. <a name="index-gsi_005fcommit_005fedge_005finserts-1"></a>
  207. <a name="index-edge-splitting"></a>
  208. <p>For a global optimizer, a common operation is to split edges in the
  209. flow graph and insert instructions on them. In the RTL
  210. representation, this can be easily done using the
  211. <code>insert_insn_on_edge</code> function that emits an instruction
  212. &ldquo;on the edge&rdquo;, caching it for a later <code>commit_edge_insertions</code>
  213. call that will take care of moving the inserted instructions off the
  214. edge into the instruction stream contained in a basic block. This
  215. includes the creation of new basic blocks where needed. In the
  216. <code>GIMPLE</code> representation, the equivalent functions are
  217. <code>gsi_insert_on_edge</code> which inserts a block statement
  218. iterator on an edge, and <code>gsi_commit_edge_inserts</code> which flushes
  219. the instruction to actual instruction stream.
  220. </p>
  221. <a name="index-verify_005fflow_005finfo"></a>
  222. <a name="index-CFG-verification"></a>
  223. <p>While debugging the optimization pass, the <code>verify_flow_info</code>
  224. function may be useful to find bugs in the control flow graph updating
  225. code.
  226. </p>
  227. <hr>
  228. <div class="header">
  229. <p>
  230. 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>
  231. </div>
  232. </body>
  233. </html>