PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/share/doc/gccint/IPA.html

https://gitlab.com/infected_/linaro_aarch64-linux-android-5.3.x
HTML | 292 lines | 225 code | 21 blank | 46 comment | 0 complexity | 1145de8299af9d442605ef34dded736d MD5 | raw file
  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-2015 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: IPA</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: IPA">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: IPA">
  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="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.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="IPA"></a>
  63. <div class="header">
  64. <p>
  65. 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>
  66. </div>
  67. <hr>
  68. <a name="Using-summary-information-in-IPA-passes"></a>
  69. <h3 class="section">24.3 Using summary information in IPA passes</h3>
  70. <p>Programs are represented internally as a <em>callgraph</em> (a
  71. multi-graph where nodes are functions and edges are call sites)
  72. and a <em>varpool</em> (a list of static and external variables in
  73. the program).
  74. </p>
  75. <p>The inter-procedural optimization is organized as a sequence of
  76. individual passes, which operate on the callgraph and the
  77. varpool. To make the implementation of WHOPR possible, every
  78. inter-procedural optimization pass is split into several stages
  79. that are executed at different times during WHOPR compilation:
  80. </p>
  81. <ul>
  82. <li> LGEN time
  83. <ol>
  84. <li> <em>Generate summary</em> (<code>generate_summary</code> in
  85. <code>struct ipa_opt_pass_d</code>). This stage analyzes every function
  86. body and variable initializer is examined and stores relevant
  87. information into a pass-specific data structure.
  88. </li><li> <em>Write summary</em> (<code>write_summary</code> in
  89. <code>struct ipa_opt_pass_d</code>). This stage writes all the
  90. pass-specific information generated by <code>generate_summary</code>.
  91. Summaries go into their own <code>LTO_section_*</code> sections that
  92. have to be declared in <samp>lto-streamer.h</samp>:<code>enum
  93. lto_section_type</code>. A new section is created by calling
  94. <code>create_output_block</code> and data can be written using the
  95. <code>lto_output_*</code> routines.
  96. </li></ol>
  97. </li><li> WPA time
  98. <ol>
  99. <li> <em>Read summary</em> (<code>read_summary</code> in
  100. <code>struct ipa_opt_pass_d</code>). This stage reads all the
  101. pass-specific information in exactly the same order that it was
  102. written by <code>write_summary</code>.
  103. </li><li> <em>Execute</em> (<code>execute</code> in <code>struct
  104. opt_pass</code>). This performs inter-procedural propagation. This
  105. must be done without actual access to the individual function
  106. bodies or variable initializers. Typically, this results in a
  107. transitive closure operation over the summary information of all
  108. the nodes in the callgraph.
  109. </li><li> <em>Write optimization summary</em>
  110. (<code>write_optimization_summary</code> in <code>struct
  111. ipa_opt_pass_d</code>). This writes the result of the inter-procedural
  112. propagation into the object file. This can use the same data
  113. structures and helper routines used in <code>write_summary</code>.
  114. </li></ol>
  115. </li><li> LTRANS time
  116. <ol>
  117. <li> <em>Read optimization summary</em>
  118. (<code>read_optimization_summary</code> in <code>struct
  119. ipa_opt_pass_d</code>). The counterpart to
  120. <code>write_optimization_summary</code>. This reads the interprocedural
  121. optimization decisions in exactly the same format emitted by
  122. <code>write_optimization_summary</code>.
  123. </li><li> <em>Transform</em> (<code>function_transform</code> and
  124. <code>variable_transform</code> in <code>struct ipa_opt_pass_d</code>).
  125. The actual function bodies and variable initializers are updated
  126. based on the information passed down from the <em>Execute</em> stage.
  127. </li></ol>
  128. </li></ul>
  129. <p>The implementation of the inter-procedural passes are shared
  130. between LTO, WHOPR and classic non-LTO compilation.
  131. </p>
  132. <ul>
  133. <li> During the traditional file-by-file mode every pass executes its
  134. own <em>Generate summary</em>, <em>Execute</em>, and <em>Transform</em>
  135. stages within the single execution context of the compiler.
  136. </li><li> In LTO compilation mode, every pass uses <em>Generate
  137. summary</em> and <em>Write summary</em> stages at compilation time,
  138. while the <em>Read summary</em>, <em>Execute</em>, and
  139. <em>Transform</em> stages are executed at link time.
  140. </li><li> In WHOPR mode all stages are used.
  141. </li></ul>
  142. <p>To simplify development, the GCC pass manager differentiates
  143. between normal inter-procedural passes and small inter-procedural
  144. passes. A <em>small inter-procedural pass</em>
  145. (<code>SIMPLE_IPA_PASS</code>) is a pass that does
  146. everything at once and thus it can not 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">24.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">24.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">24.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>