Basic-Blocks.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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: Basic Blocks</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Basic Blocks">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Basic Blocks">
  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="Edges.html#Edges" rel="next" title="Edges">
  31. <link href="Control-Flow.html#Control-Flow" rel="prev" title="Control Flow">
  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="Basic-Blocks"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Edges.html#Edges" accesskey="n" rel="next">Edges</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="Basic-Blocks-1"></a>
  69. <h3 class="section">14.1 Basic Blocks</h3>
  70. <a name="index-basic-block"></a>
  71. <a name="index-basic_005fblock"></a>
  72. <p>A basic block is a straight-line sequence of code with only one entry
  73. point and only one exit. In GCC, basic blocks are represented using
  74. the <code>basic_block</code> data type.
  75. </p>
  76. <a name="index-ENTRY_005fBLOCK_005fPTR_002c-EXIT_005fBLOCK_005fPTR"></a>
  77. <p>Special basic blocks represent possible entry and exit points of a
  78. function. These blocks are called <code>ENTRY_BLOCK_PTR</code> and
  79. <code>EXIT_BLOCK_PTR</code>. These blocks do not contain any code.
  80. </p>
  81. <a name="index-BASIC_005fBLOCK"></a>
  82. <p>The <code>BASIC_BLOCK</code> array contains all basic blocks in an
  83. unspecified order. Each <code>basic_block</code> structure has a field
  84. that holds a unique integer identifier <code>index</code> that is the
  85. index of the block in the <code>BASIC_BLOCK</code> array.
  86. The total number of basic blocks in the function is
  87. <code>n_basic_blocks</code>. Both the basic block indices and
  88. the total number of basic blocks may vary during the compilation
  89. process, as passes reorder, create, duplicate, and destroy basic
  90. blocks. The index for any block should never be greater than
  91. <code>last_basic_block</code>. The indices 0 and 1 are special codes
  92. reserved for <code>ENTRY_BLOCK</code> and <code>EXIT_BLOCK</code>, the
  93. indices of <code>ENTRY_BLOCK_PTR</code> and <code>EXIT_BLOCK_PTR</code>.
  94. </p>
  95. <a name="index-next_005fbb_002c-prev_005fbb_002c-FOR_005fEACH_005fBB_002c-FOR_005fALL_005fBB"></a>
  96. <p>Two pointer members of the <code>basic_block</code> structure are the
  97. pointers <code>next_bb</code> and <code>prev_bb</code>. These are used to keep
  98. doubly linked chain of basic blocks in the same order as the
  99. underlying instruction stream. The chain of basic blocks is updated
  100. transparently by the provided API for manipulating the CFG. The macro
  101. <code>FOR_EACH_BB</code> can be used to visit all the basic blocks in
  102. lexicographical order, except <code>ENTRY_BLOCK</code> and <code>EXIT_BLOCK</code>.
  103. The macro <code>FOR_ALL_BB</code> also visits all basic blocks in
  104. lexicographical order, including <code>ENTRY_BLOCK</code> and <code>EXIT_BLOCK</code>.
  105. </p>
  106. <a name="index-post_005forder_005fcompute_002c-inverted_005fpost_005forder_005fcompute_002c-walk_005fdominator_005ftree"></a>
  107. <p>The functions <code>post_order_compute</code> and <code>inverted_post_order_compute</code>
  108. can be used to compute topological orders of the CFG. The orders are
  109. stored as vectors of basic block indices. The <code>BASIC_BLOCK</code> array
  110. can be used to iterate each basic block by index.
  111. Dominator traversals are also possible using
  112. <code>walk_dominator_tree</code>. Given two basic blocks A and B, block A
  113. dominates block B if A is <em>always</em> executed before B.
  114. </p>
  115. <p>Each <code>basic_block</code> also contains pointers to the first
  116. instruction (the <em>head</em>) and the last instruction (the <em>tail</em>)
  117. or <em>end</em> of the instruction stream contained in a basic block. In
  118. fact, since the <code>basic_block</code> data type is used to represent
  119. blocks in both major intermediate representations of GCC (<code>GIMPLE</code>
  120. and RTL), there are pointers to the head and end of a basic block for
  121. both representations, stored in intermediate representation specific
  122. data in the <code>il</code> field of <code>struct basic_block_def</code>.
  123. </p>
  124. <a name="index-CODE_005fLABEL"></a>
  125. <a name="index-NOTE_005fINSN_005fBASIC_005fBLOCK"></a>
  126. <p>For RTL, these pointers are <code>BB_HEAD</code> and <code>BB_END</code>.
  127. </p>
  128. <a name="index-insn-notes_002c-notes"></a>
  129. <a name="index-NOTE_005fINSN_005fBASIC_005fBLOCK-1"></a>
  130. <p>In the RTL representation of a function, the instruction stream
  131. contains not only the &ldquo;real&rdquo; instructions, but also <em>notes</em>
  132. or <em>insn notes</em> (to distinguish them from <em>reg notes</em>).
  133. Any function that moves or duplicates the basic blocks needs
  134. to take care of updating of these notes. Many of these notes expect
  135. that the instruction stream consists of linear regions, so updating
  136. can sometimes be tedious. All types of insn notes are defined
  137. in <samp>insn-notes.def</samp>.
  138. </p>
  139. <p>In the RTL function representation, the instructions contained in a
  140. basic block always follow a <code>NOTE_INSN_BASIC_BLOCK</code>, but zero
  141. or more <code>CODE_LABEL</code> nodes can precede the block note.
  142. A basic block ends with a control flow instruction or with the last
  143. instruction before the next <code>CODE_LABEL</code> or
  144. <code>NOTE_INSN_BASIC_BLOCK</code>.
  145. By definition, a <code>CODE_LABEL</code> cannot appear in the middle of
  146. the instruction stream of a basic block.
  147. </p>
  148. <a name="index-can_005ffallthru"></a>
  149. <a name="index-table-jump"></a>
  150. <p>In addition to notes, the jump table vectors are also represented as
  151. &ldquo;pseudo-instructions&rdquo; inside the insn stream. These vectors never
  152. appear in the basic block and should always be placed just after the
  153. table jump instructions referencing them. After removing the
  154. table-jump it is often difficult to eliminate the code computing the
  155. address and referencing the vector, so cleaning up these vectors is
  156. postponed until after liveness analysis. Thus the jump table vectors
  157. may appear in the insn stream unreferenced and without any purpose.
  158. Before any edge is made <em>fall-thru</em>, the existence of such
  159. construct in the way needs to be checked by calling
  160. <code>can_fallthru</code> function.
  161. </p>
  162. <a name="index-GIMPLE-statement-iterators"></a>
  163. <p>For the <code>GIMPLE</code> representation, the PHI nodes and statements
  164. contained in a basic block are in a <code>gimple_seq</code> pointed to by
  165. the basic block intermediate language specific pointers.
  166. Abstract containers and iterators are used to access the PHI nodes
  167. and statements in a basic blocks. These iterators are called
  168. <em>GIMPLE statement iterators</em> (GSIs). Grep for <code>^gsi</code>
  169. in the various <samp>gimple-*</samp> and <samp>tree-*</samp> files.
  170. There is a <code>gimple_stmt_iterator</code> type for iterating over
  171. all kinds of statement, and a <code>gphi_iterator</code> subclass for
  172. iterating over PHI nodes.
  173. The following snippet will pretty-print all PHI nodes the statements
  174. of the current function in the GIMPLE representation.
  175. </p>
  176. <div class="smallexample">
  177. <pre class="smallexample">basic_block bb;
  178. FOR_EACH_BB (bb)
  179. {
  180. gphi_iterator pi;
  181. gimple_stmt_iterator si;
  182. for (pi = gsi_start_phis (bb); !gsi_end_p (pi); gsi_next (&amp;pi))
  183. {
  184. gphi *phi = pi.phi ();
  185. print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
  186. }
  187. for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&amp;si))
  188. {
  189. gimple stmt = gsi_stmt (si);
  190. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  191. }
  192. }
  193. </pre></div>
  194. <hr>
  195. <div class="header">
  196. <p>
  197. Next: <a href="Edges.html#Edges" accesskey="n" rel="next">Edges</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>
  198. </div>
  199. </body>
  200. </html>