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.

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