Edges.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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: Edges</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Edges">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Edges">
  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="Profile-information.html#Profile-information" rel="next" title="Profile information">
  31. <link href="Basic-Blocks.html#Basic-Blocks" rel="prev" title="Basic Blocks">
  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="Edges"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Profile-information.html#Profile-information" accesskey="n" rel="next">Profile information</a>, Previous: <a href="Basic-Blocks.html#Basic-Blocks" accesskey="p" rel="prev">Basic Blocks</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="Edges-1"></a>
  69. <h3 class="section">14.2 Edges</h3>
  70. <a name="index-edge-in-the-flow-graph"></a>
  71. <a name="index-edge"></a>
  72. <p>Edges represent possible control flow transfers from the end of some
  73. basic block A to the head of another basic block B. We say that A is
  74. a predecessor of B, and B is a successor of A. Edges are represented
  75. in GCC with the <code>edge</code> data type. Each <code>edge</code> acts as a
  76. link between two basic blocks: The <code>src</code> member of an edge
  77. points to the predecessor basic block of the <code>dest</code> basic block.
  78. The members <code>preds</code> and <code>succs</code> of the <code>basic_block</code> data
  79. type point to type-safe vectors of edges to the predecessors and
  80. successors of the block.
  81. </p>
  82. <a name="index-edge-iterators"></a>
  83. <p>When walking the edges in an edge vector, <em>edge iterators</em> should
  84. be used. Edge iterators are constructed using the
  85. <code>edge_iterator</code> data structure and several methods are available
  86. to operate on them:
  87. </p>
  88. <dl compact="compact">
  89. <dt><code>ei_start</code>
  90. <a name="index-ei_005fstart"></a>
  91. </dt>
  92. <dd><p>This function initializes an <code>edge_iterator</code> that points to the
  93. first edge in a vector of edges.
  94. </p>
  95. </dd>
  96. <dt><code>ei_last</code>
  97. <a name="index-ei_005flast"></a>
  98. </dt>
  99. <dd><p>This function initializes an <code>edge_iterator</code> that points to the
  100. last edge in a vector of edges.
  101. </p>
  102. </dd>
  103. <dt><code>ei_end_p</code>
  104. <a name="index-ei_005fend_005fp"></a>
  105. </dt>
  106. <dd><p>This predicate is <code>true</code> if an <code>edge_iterator</code> represents
  107. the last edge in an edge vector.
  108. </p>
  109. </dd>
  110. <dt><code>ei_one_before_end_p</code>
  111. <a name="index-ei_005fone_005fbefore_005fend_005fp"></a>
  112. </dt>
  113. <dd><p>This predicate is <code>true</code> if an <code>edge_iterator</code> represents
  114. the second last edge in an edge vector.
  115. </p>
  116. </dd>
  117. <dt><code>ei_next</code>
  118. <a name="index-ei_005fnext"></a>
  119. </dt>
  120. <dd><p>This function takes a pointer to an <code>edge_iterator</code> and makes it
  121. point to the next edge in the sequence.
  122. </p>
  123. </dd>
  124. <dt><code>ei_prev</code>
  125. <a name="index-ei_005fprev"></a>
  126. </dt>
  127. <dd><p>This function takes a pointer to an <code>edge_iterator</code> and makes it
  128. point to the previous edge in the sequence.
  129. </p>
  130. </dd>
  131. <dt><code>ei_edge</code>
  132. <a name="index-ei_005fedge"></a>
  133. </dt>
  134. <dd><p>This function returns the <code>edge</code> currently pointed to by an
  135. <code>edge_iterator</code>.
  136. </p>
  137. </dd>
  138. <dt><code>ei_safe_safe</code>
  139. <a name="index-ei_005fsafe_005fsafe"></a>
  140. </dt>
  141. <dd><p>This function returns the <code>edge</code> currently pointed to by an
  142. <code>edge_iterator</code>, but returns <code>NULL</code> if the iterator is
  143. pointing at the end of the sequence. This function has been provided
  144. for existing code makes the assumption that a <code>NULL</code> edge
  145. indicates the end of the sequence.
  146. </p>
  147. </dd>
  148. </dl>
  149. <p>The convenience macro <code>FOR_EACH_EDGE</code> can be used to visit all of
  150. the edges in a sequence of predecessor or successor edges. It must
  151. not be used when an element might be removed during the traversal,
  152. otherwise elements will be missed. Here is an example of how to use
  153. the macro:
  154. </p>
  155. <div class="smallexample">
  156. <pre class="smallexample">edge e;
  157. edge_iterator ei;
  158. FOR_EACH_EDGE (e, ei, bb-&gt;succs)
  159. {
  160. if (e-&gt;flags &amp; EDGE_FALLTHRU)
  161. break;
  162. }
  163. </pre></div>
  164. <a name="index-fall_002dthru"></a>
  165. <p>There are various reasons why control flow may transfer from one block
  166. to another. One possibility is that some instruction, for example a
  167. <code>CODE_LABEL</code>, in a linearized instruction stream just always
  168. starts a new basic block. In this case a <em>fall-thru</em> edge links
  169. the basic block to the first following basic block. But there are
  170. several other reasons why edges may be created. The <code>flags</code>
  171. field of the <code>edge</code> data type is used to store information
  172. about the type of edge we are dealing with. Each edge is of one of
  173. the following types:
  174. </p>
  175. <dl compact="compact">
  176. <dt><em>jump</em></dt>
  177. <dd><p>No type flags are set for edges corresponding to jump instructions.
  178. These edges are used for unconditional or conditional jumps and in
  179. RTL also for table jumps. They are the easiest to manipulate as they
  180. may be freely redirected when the flow graph is not in SSA form.
  181. </p>
  182. </dd>
  183. <dt><em>fall-thru</em></dt>
  184. <dd><a name="index-EDGE_005fFALLTHRU_002c-force_005fnonfallthru"></a>
  185. <p>Fall-thru edges are present in case where the basic block may continue
  186. execution to the following one without branching. These edges have
  187. the <code>EDGE_FALLTHRU</code> flag set. Unlike other types of edges, these
  188. edges must come into the basic block immediately following in the
  189. instruction stream. The function <code>force_nonfallthru</code> is
  190. available to insert an unconditional jump in the case that redirection
  191. is needed. Note that this may require creation of a new basic block.
  192. </p>
  193. </dd>
  194. <dt><em>exception handling</em></dt>
  195. <dd><a name="index-exception-handling"></a>
  196. <a name="index-EDGE_005fABNORMAL_002c-EDGE_005fEH"></a>
  197. <p>Exception handling edges represent possible control transfers from a
  198. trapping instruction to an exception handler. The definition of
  199. &ldquo;trapping&rdquo; varies. In C++, only function calls can throw, but for
  200. Ada exceptions like division by zero or segmentation fault are
  201. defined and thus each instruction possibly throwing this kind of
  202. exception needs to be handled as control flow instruction. Exception
  203. edges have the <code>EDGE_ABNORMAL</code> and <code>EDGE_EH</code> flags set.
  204. </p>
  205. <a name="index-purge_005fdead_005fedges"></a>
  206. <p>When updating the instruction stream it is easy to change possibly
  207. trapping instruction to non-trapping, by simply removing the exception
  208. edge. The opposite conversion is difficult, but should not happen
  209. anyway. The edges can be eliminated via <code>purge_dead_edges</code> call.
  210. </p>
  211. <a name="index-REG_005fEH_005fREGION_002c-EDGE_005fABNORMAL_005fCALL"></a>
  212. <p>In the RTL representation, the destination of an exception edge is
  213. specified by <code>REG_EH_REGION</code> note attached to the insn.
  214. In case of a trapping call the <code>EDGE_ABNORMAL_CALL</code> flag is set
  215. too. In the <code>GIMPLE</code> representation, this extra flag is not set.
  216. </p>
  217. <a name="index-may_005ftrap_005fp_002c-tree_005fcould_005ftrap_005fp"></a>
  218. <p>In the RTL representation, the predicate <code>may_trap_p</code> may be used
  219. to check whether instruction still may trap or not. For the tree
  220. representation, the <code>tree_could_trap_p</code> predicate is available,
  221. but this predicate only checks for possible memory traps, as in
  222. dereferencing an invalid pointer location.
  223. </p>
  224. </dd>
  225. <dt><em>sibling calls</em></dt>
  226. <dd><a name="index-sibling-call"></a>
  227. <a name="index-EDGE_005fABNORMAL_002c-EDGE_005fSIBCALL"></a>
  228. <p>Sibling calls or tail calls terminate the function in a non-standard
  229. way and thus an edge to the exit must be present.
  230. <code>EDGE_SIBCALL</code> and <code>EDGE_ABNORMAL</code> are set in such case.
  231. These edges only exist in the RTL representation.
  232. </p>
  233. </dd>
  234. <dt><em>computed jumps</em></dt>
  235. <dd><a name="index-computed-jump"></a>
  236. <a name="index-EDGE_005fABNORMAL"></a>
  237. <p>Computed jumps contain edges to all labels in the function referenced
  238. from the code. All those edges have <code>EDGE_ABNORMAL</code> flag set.
  239. The edges used to represent computed jumps often cause compile time
  240. performance problems, since functions consisting of many taken labels
  241. and many computed jumps may have <em>very</em> dense flow graphs, so
  242. these edges need to be handled with special care. During the earlier
  243. stages of the compilation process, GCC tries to avoid such dense flow
  244. graphs by factoring computed jumps. For example, given the following
  245. series of jumps,
  246. </p>
  247. <div class="smallexample">
  248. <pre class="smallexample"> goto *x;
  249. [ &hellip; ]
  250. goto *x;
  251. [ &hellip; ]
  252. goto *x;
  253. [ &hellip; ]
  254. </pre></div>
  255. <p>factoring the computed jumps results in the following code sequence
  256. which has a much simpler flow graph:
  257. </p>
  258. <div class="smallexample">
  259. <pre class="smallexample"> goto y;
  260. [ &hellip; ]
  261. goto y;
  262. [ &hellip; ]
  263. goto y;
  264. [ &hellip; ]
  265. y:
  266. goto *x;
  267. </pre></div>
  268. <a name="index-pass_005fduplicate_005fcomputed_005fgotos"></a>
  269. <p>However, the classic problem with this transformation is that it has a
  270. runtime cost in there resulting code: An extra jump. Therefore, the
  271. computed jumps are un-factored in the later passes of the compiler
  272. (in the pass called <code>pass_duplicate_computed_gotos</code>).
  273. Be aware of that when you work on passes in that area. There have
  274. been numerous examples already where the compile time for code with
  275. unfactored computed jumps caused some serious headaches.
  276. </p>
  277. </dd>
  278. <dt><em>nonlocal goto handlers</em></dt>
  279. <dd><a name="index-nonlocal-goto-handler"></a>
  280. <a name="index-EDGE_005fABNORMAL_002c-EDGE_005fABNORMAL_005fCALL"></a>
  281. <p>GCC allows nested functions to return into caller using a <code>goto</code>
  282. to a label passed to as an argument to the callee. The labels passed
  283. to nested functions contain special code to cleanup after function
  284. call. Such sections of code are referred to as &ldquo;nonlocal goto
  285. receivers&rdquo;. If a function contains such nonlocal goto receivers, an
  286. edge from the call to the label is created with the
  287. <code>EDGE_ABNORMAL</code> and <code>EDGE_ABNORMAL_CALL</code> flags set.
  288. </p>
  289. </dd>
  290. <dt><em>function entry points</em></dt>
  291. <dd><a name="index-function-entry-point_002c-alternate-function-entry-point"></a>
  292. <a name="index-LABEL_005fALTERNATE_005fNAME"></a>
  293. <p>By definition, execution of function starts at basic block 0, so there
  294. is always an edge from the <code>ENTRY_BLOCK_PTR</code> to basic block 0.
  295. There is no <code>GIMPLE</code> representation for alternate entry points at
  296. this moment. In RTL, alternate entry points are specified by
  297. <code>CODE_LABEL</code> with <code>LABEL_ALTERNATE_NAME</code> defined. This
  298. feature is currently used for multiple entry point prologues and is
  299. limited to post-reload passes only. This can be used by back-ends to
  300. emit alternate prologues for functions called from different contexts.
  301. In future full support for multiple entry functions defined by Fortran
  302. 90 needs to be implemented.
  303. </p>
  304. </dd>
  305. <dt><em>function exits</em></dt>
  306. <dd><p>In the pre-reload representation a function terminates after the last
  307. instruction in the insn chain and no explicit return instructions are
  308. used. This corresponds to the fall-thru edge into exit block. After
  309. reload, optimal RTL epilogues are used that use explicit (conditional)
  310. return instructions that are represented by edges with no flags set.
  311. </p>
  312. </dd>
  313. </dl>
  314. <hr>
  315. <div class="header">
  316. <p>
  317. Next: <a href="Profile-information.html#Profile-information" accesskey="n" rel="next">Profile information</a>, Previous: <a href="Basic-Blocks.html#Basic-Blocks" accesskey="p" rel="prev">Basic Blocks</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>
  318. </div>
  319. </body>
  320. </html>