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 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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>IPA (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="IPA (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="IPA (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="LTO.html#LTO" rel="up" title="LTO">
  30. <link href="WHOPR.html#WHOPR" rel="next" title="WHOPR">
  31. <link href="LTO-object-file-layout.html#LTO-object-file-layout" rel="prev" title="LTO object file layout">
  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="IPA"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="WHOPR.html#WHOPR" accesskey="n" rel="next">WHOPR</a>, Previous: <a href="LTO-object-file-layout.html#LTO-object-file-layout" accesskey="p" rel="prev">LTO object file layout</a>, Up: <a href="LTO.html#LTO" accesskey="u" rel="up">LTO</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="Using-summary-information-in-IPA-passes"></a>
  68. <h3 class="section">25.3 Using summary information in IPA passes</h3>
  69. <p>Programs are represented internally as a <em>callgraph</em> (a
  70. multi-graph where nodes are functions and edges are call sites)
  71. and a <em>varpool</em> (a list of static and external variables in
  72. the program).
  73. </p>
  74. <p>The inter-procedural optimization is organized as a sequence of
  75. individual passes, which operate on the callgraph and the
  76. varpool. To make the implementation of WHOPR possible, every
  77. inter-procedural optimization pass is split into several stages
  78. that are executed at different times during WHOPR compilation:
  79. </p>
  80. <ul>
  81. <li> LGEN time
  82. <ol>
  83. <li> <em>Generate summary</em> (<code>generate_summary</code> in
  84. <code>struct ipa_opt_pass_d</code>). This stage analyzes every function
  85. body and variable initializer is examined and stores relevant
  86. information into a pass-specific data structure.
  87. </li><li> <em>Write summary</em> (<code>write_summary</code> in
  88. <code>struct ipa_opt_pass_d</code>). This stage writes all the
  89. pass-specific information generated by <code>generate_summary</code>.
  90. Summaries go into their own <code>LTO_section_*</code> sections that
  91. have to be declared in <samp>lto-streamer.h</samp>:<code>enum
  92. lto_section_type</code>. A new section is created by calling
  93. <code>create_output_block</code> and data can be written using the
  94. <code>lto_output_*</code> routines.
  95. </li></ol>
  96. </li><li> WPA time
  97. <ol>
  98. <li> <em>Read summary</em> (<code>read_summary</code> in
  99. <code>struct ipa_opt_pass_d</code>). This stage reads all the
  100. pass-specific information in exactly the same order that it was
  101. written by <code>write_summary</code>.
  102. </li><li> <em>Execute</em> (<code>execute</code> in <code>struct
  103. opt_pass</code>). This performs inter-procedural propagation. This
  104. must be done without actual access to the individual function
  105. bodies or variable initializers. Typically, this results in a
  106. transitive closure operation over the summary information of all
  107. the nodes in the callgraph.
  108. </li><li> <em>Write optimization summary</em>
  109. (<code>write_optimization_summary</code> in <code>struct
  110. ipa_opt_pass_d</code>). This writes the result of the inter-procedural
  111. propagation into the object file. This can use the same data
  112. structures and helper routines used in <code>write_summary</code>.
  113. </li></ol>
  114. </li><li> LTRANS time
  115. <ol>
  116. <li> <em>Read optimization summary</em>
  117. (<code>read_optimization_summary</code> in <code>struct
  118. ipa_opt_pass_d</code>). The counterpart to
  119. <code>write_optimization_summary</code>. This reads the interprocedural
  120. optimization decisions in exactly the same format emitted by
  121. <code>write_optimization_summary</code>.
  122. </li><li> <em>Transform</em> (<code>function_transform</code> and
  123. <code>variable_transform</code> in <code>struct ipa_opt_pass_d</code>).
  124. The actual function bodies and variable initializers are updated
  125. based on the information passed down from the <em>Execute</em> stage.
  126. </li></ol>
  127. </li></ul>
  128. <p>The implementation of the inter-procedural passes are shared
  129. between LTO, WHOPR and classic non-LTO compilation.
  130. </p>
  131. <ul>
  132. <li> During the traditional file-by-file mode every pass executes its
  133. own <em>Generate summary</em>, <em>Execute</em>, and <em>Transform</em>
  134. stages within the single execution context of the compiler.
  135. </li><li> In LTO compilation mode, every pass uses <em>Generate
  136. summary</em> and <em>Write summary</em> stages at compilation time,
  137. while the <em>Read summary</em>, <em>Execute</em>, and
  138. <em>Transform</em> stages are executed at link time.
  139. </li><li> In WHOPR mode all stages are used.
  140. </li></ul>
  141. <p>To simplify development, the GCC pass manager differentiates
  142. between normal inter-procedural passes (see <a href="Regular-IPA-passes.html#Regular-IPA-passes">Regular IPA passes</a>),
  143. small inter-procedural passes (see <a href="Small-IPA-passes.html#Small-IPA-passes">Small IPA passes</a>)
  144. and late inter-procedural passes (see <a href="Late-IPA-passes.html#Late-IPA-passes">Late IPA passes</a>).
  145. A small or late IPA pass (<code>SIMPLE_IPA_PASS</code>) does
  146. everything at once and thus cannot be executed during WPA in
  147. WHOPR mode. It defines only the <em>Execute</em> stage and during
  148. this stage it accesses and modifies the function bodies. Such
  149. passes are useful for optimization at LGEN or LTRANS time and are
  150. used, for example, to implement early optimization before writing
  151. object files. The simple inter-procedural passes can also be used
  152. for easier prototyping and development of a new inter-procedural
  153. pass.
  154. </p>
  155. <a name="Virtual-clones"></a>
  156. <h4 class="subsection">25.3.1 Virtual clones</h4>
  157. <p>One of the main challenges of introducing the WHOPR compilation
  158. mode was addressing the interactions between optimization passes.
  159. In LTO compilation mode, the passes are executed in a sequence,
  160. each of which consists of analysis (or <em>Generate summary</em>),
  161. propagation (or <em>Execute</em>) and <em>Transform</em> stages.
  162. Once the work of one pass is finished, the next pass sees the
  163. updated program representation and can execute. This makes the
  164. individual passes dependent on each other.
  165. </p>
  166. <p>In WHOPR mode all passes first execute their <em>Generate
  167. summary</em> stage. Then summary writing marks the end of the LGEN
  168. stage. At WPA time,
  169. the summaries are read back into memory and all passes run the
  170. <em>Execute</em> stage. Optimization summaries are streamed and
  171. sent to LTRANS, where all the passes execute the <em>Transform</em>
  172. stage.
  173. </p>
  174. <p>Most optimization passes split naturally into analysis,
  175. propagation and transformation stages. But some do not. The
  176. main problem arises when one pass performs changes and the
  177. following pass gets confused by seeing different callgraphs
  178. between the <em>Transform</em> stage and the <em>Generate summary</em>
  179. or <em>Execute</em> stage. This means that the passes are required
  180. to communicate their decisions with each other.
  181. </p>
  182. <p>To facilitate this communication, the GCC callgraph
  183. infrastructure implements <em>virtual clones</em>, a method of
  184. representing the changes performed by the optimization passes in
  185. the callgraph without needing to update function bodies.
  186. </p>
  187. <p>A <em>virtual clone</em> in the callgraph is a function that has no
  188. associated body, just a description of how to create its body based
  189. on a different function (which itself may be a virtual clone).
  190. </p>
  191. <p>The description of function modifications includes adjustments to
  192. the function&rsquo;s signature (which allows, for example, removing or
  193. adding function arguments), substitutions to perform on the
  194. function body, and, for inlined functions, a pointer to the
  195. function that it will be inlined into.
  196. </p>
  197. <p>It is also possible to redirect any edge of the callgraph from a
  198. function to its virtual clone. This implies updating of the call
  199. site to adjust for the new function signature.
  200. </p>
  201. <p>Most of the transformations performed by inter-procedural
  202. optimizations can be represented via virtual clones. For
  203. instance, a constant propagation pass can produce a virtual clone
  204. of the function which replaces one of its arguments by a
  205. constant. The inliner can represent its decisions by producing a
  206. clone of a function whose body will be later integrated into
  207. a given function.
  208. </p>
  209. <p>Using <em>virtual clones</em>, the program can be easily updated
  210. during the <em>Execute</em> stage, solving most of pass interactions
  211. problems that would otherwise occur during <em>Transform</em>.
  212. </p>
  213. <p>Virtual clones are later materialized in the LTRANS stage and
  214. turned into real functions. Passes executed after the virtual
  215. clone were introduced also perform their <em>Transform</em> stage
  216. on new functions, so for a pass there is no significant
  217. difference between operating on a real function or a virtual
  218. clone introduced before its <em>Execute</em> stage.
  219. </p>
  220. <p>Optimization passes then work on virtual clones introduced before
  221. their <em>Execute</em> stage as if they were real functions. The
  222. only difference is that clones are not visible during the
  223. <em>Generate Summary</em> stage.
  224. </p>
  225. <p>To keep function summaries updated, the callgraph interface
  226. allows an optimizer to register a callback that is called every
  227. time a new clone is introduced as well as when the actual
  228. function or variable is generated or when a function or variable
  229. is removed. These hooks are registered in the <em>Generate
  230. summary</em> stage and allow the pass to keep its information intact
  231. until the <em>Execute</em> stage. The same hooks can also be
  232. registered during the <em>Execute</em> stage to keep the
  233. optimization summaries updated for the <em>Transform</em> stage.
  234. </p>
  235. <a name="IPA-references"></a>
  236. <h4 class="subsection">25.3.2 IPA references</h4>
  237. <p>GCC represents IPA references in the callgraph. For a function
  238. or variable <code>A</code>, the <em>IPA reference</em> is a list of all
  239. locations where the address of <code>A</code> is taken and, when
  240. <code>A</code> is a variable, a list of all direct stores and reads
  241. to/from <code>A</code>. References represent an oriented multi-graph on
  242. the union of nodes of the callgraph and the varpool. See
  243. <samp>ipa-reference.c</samp>:<code>ipa_reference_write_optimization_summary</code>
  244. and
  245. <samp>ipa-reference.c</samp>:<code>ipa_reference_read_optimization_summary</code>
  246. for details.
  247. </p>
  248. <a name="Jump-functions"></a>
  249. <h4 class="subsection">25.3.3 Jump functions</h4>
  250. <p>Suppose that an optimization pass sees a function <code>A</code> and it
  251. knows the values of (some of) its arguments. The <em>jump
  252. function</em> describes the value of a parameter of a given function
  253. call in function <code>A</code> based on this knowledge.
  254. </p>
  255. <p>Jump functions are used by several optimizations, such as the
  256. inter-procedural constant propagation pass and the
  257. devirtualization pass. The inliner also uses jump functions to
  258. perform inlining of callbacks.
  259. </p>
  260. <hr>
  261. <div class="header">
  262. <p>
  263. Next: <a href="WHOPR.html#WHOPR" accesskey="n" rel="next">WHOPR</a>, Previous: <a href="LTO-object-file-layout.html#LTO-object-file-layout" accesskey="p" rel="prev">LTO object file layout</a>, Up: <a href="LTO.html#LTO" accesskey="u" rel="up">LTO</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>
  264. </div>
  265. </body>
  266. </html>