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 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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>SSA (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="SSA (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="SSA (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="Tree-SSA.html#Tree-SSA" rel="up" title="Tree SSA">
  30. <link href="Alias-analysis.html#Alias-analysis" rel="next" title="Alias analysis">
  31. <link href="SSA-Operands.html#SSA-Operands" rel="prev" title="SSA Operands">
  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="SSA"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Alias-analysis.html#Alias-analysis" accesskey="n" rel="next">Alias analysis</a>, Previous: <a href="SSA-Operands.html#SSA-Operands" accesskey="p" rel="prev">SSA Operands</a>, Up: <a href="Tree-SSA.html#Tree-SSA" accesskey="u" rel="up">Tree SSA</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="Static-Single-Assignment"></a>
  68. <h3 class="section">13.3 Static Single Assignment</h3>
  69. <a name="index-SSA"></a>
  70. <a name="index-static-single-assignment"></a>
  71. <p>Most of the tree optimizers rely on the data flow information provided
  72. by the Static Single Assignment (SSA) form. We implement the SSA form
  73. as described in <cite>R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and
  74. K. Zadeck. Efficiently Computing Static Single Assignment Form and the
  75. Control Dependence Graph. ACM Transactions on Programming Languages
  76. and Systems, 13(4):451-490, October 1991</cite>.
  77. </p>
  78. <p>The SSA form is based on the premise that program variables are
  79. assigned in exactly one location in the program. Multiple assignments
  80. to the same variable create new versions of that variable. Naturally,
  81. actual programs are seldom in SSA form initially because variables
  82. tend to be assigned multiple times. The compiler modifies the program
  83. representation so that every time a variable is assigned in the code,
  84. a new version of the variable is created. Different versions of the
  85. same variable are distinguished by subscripting the variable name with
  86. its version number. Variables used in the right-hand side of
  87. expressions are renamed so that their version number matches that of
  88. the most recent assignment.
  89. </p>
  90. <p>We represent variable versions using <code>SSA_NAME</code> nodes. The
  91. renaming process in <samp>tree-ssa.c</samp> wraps every real and
  92. virtual operand with an <code>SSA_NAME</code> node which contains
  93. the version number and the statement that created the
  94. <code>SSA_NAME</code>. Only definitions and virtual definitions may
  95. create new <code>SSA_NAME</code> nodes.
  96. </p>
  97. <a name="index-PHI-nodes"></a>
  98. <p>Sometimes, flow of control makes it impossible to determine the
  99. most recent version of a variable. In these cases, the compiler
  100. inserts an artificial definition for that variable called
  101. <em>PHI function</em> or <em>PHI node</em>. This new definition merges
  102. all the incoming versions of the variable to create a new name
  103. for it. For instance,
  104. </p>
  105. <div class="smallexample">
  106. <pre class="smallexample">if (&hellip;)
  107. a_1 = 5;
  108. else if (&hellip;)
  109. a_2 = 2;
  110. else
  111. a_3 = 13;
  112. # a_4 = PHI &lt;a_1, a_2, a_3&gt;
  113. return a_4;
  114. </pre></div>
  115. <p>Since it is not possible to determine which of the three branches
  116. will be taken at runtime, we don&rsquo;t know which of <code>a_1</code>,
  117. <code>a_2</code> or <code>a_3</code> to use at the return statement. So, the
  118. SSA renamer creates a new version <code>a_4</code> which is assigned
  119. the result of &ldquo;merging&rdquo; <code>a_1</code>, <code>a_2</code> and <code>a_3</code>.
  120. Hence, PHI nodes mean &ldquo;one of these operands. I don&rsquo;t know
  121. which&rdquo;.
  122. </p>
  123. <p>The following functions can be used to examine PHI nodes
  124. </p>
  125. <dl>
  126. <dt><a name="index-gimple_005fphi_005fresult-1"></a>Function: <strong>gimple_phi_result</strong> <em>(<var>phi</var>)</em></dt>
  127. <dd><p>Returns the <code>SSA_NAME</code> created by PHI node <var>phi</var> (i.e.,
  128. <var>phi</var>&rsquo;s LHS).
  129. </p></dd></dl>
  130. <dl>
  131. <dt><a name="index-gimple_005fphi_005fnum_005fargs-1"></a>Function: <strong>gimple_phi_num_args</strong> <em>(<var>phi</var>)</em></dt>
  132. <dd><p>Returns the number of arguments in <var>phi</var>. This number is exactly
  133. the number of incoming edges to the basic block holding <var>phi</var>.
  134. </p></dd></dl>
  135. <dl>
  136. <dt><a name="index-gimple_005fphi_005farg-1"></a>Function: <strong>gimple_phi_arg</strong> <em>(<var>phi</var>, <var>i</var>)</em></dt>
  137. <dd><p>Returns <var>i</var>th argument of <var>phi</var>.
  138. </p></dd></dl>
  139. <dl>
  140. <dt><a name="index-gimple_005fphi_005farg_005fedge"></a>Function: <strong>gimple_phi_arg_edge</strong> <em>(<var>phi</var>, <var>i</var>)</em></dt>
  141. <dd><p>Returns the incoming edge for the <var>i</var>th argument of <var>phi</var>.
  142. </p></dd></dl>
  143. <dl>
  144. <dt><a name="index-gimple_005fphi_005farg_005fdef"></a>Function: <strong>gimple_phi_arg_def</strong> <em>(<var>phi</var>, <var>i</var>)</em></dt>
  145. <dd><p>Returns the <code>SSA_NAME</code> for the <var>i</var>th argument of <var>phi</var>.
  146. </p></dd></dl>
  147. <a name="Preserving-the-SSA-form"></a>
  148. <h4 class="subsection">13.3.1 Preserving the SSA form</h4>
  149. <a name="index-update_005fssa"></a>
  150. <a name="index-preserving-SSA-form"></a>
  151. <p>Some optimization passes make changes to the function that
  152. invalidate the SSA property. This can happen when a pass has
  153. added new symbols or changed the program so that variables that
  154. were previously aliased aren&rsquo;t anymore. Whenever something like this
  155. happens, the affected symbols must be renamed into SSA form again.
  156. Transformations that emit new code or replicate existing statements
  157. will also need to update the SSA form.
  158. </p>
  159. <p>Since GCC implements two different SSA forms for register and virtual
  160. variables, keeping the SSA form up to date depends on whether you are
  161. updating register or virtual names. In both cases, the general idea
  162. behind incremental SSA updates is similar: when new SSA names are
  163. created, they typically are meant to replace other existing names in
  164. the program.
  165. </p>
  166. <p>For instance, given the following code:
  167. </p>
  168. <div class="smallexample">
  169. <pre class="smallexample"> 1 L0:
  170. 2 x_1 = PHI (0, x_5)
  171. 3 if (x_1 &lt; 10)
  172. 4 if (x_1 &gt; 7)
  173. 5 y_2 = 0
  174. 6 else
  175. 7 y_3 = x_1 + x_7
  176. 8 endif
  177. 9 x_5 = x_1 + 1
  178. 10 goto L0;
  179. 11 endif
  180. </pre></div>
  181. <p>Suppose that we insert new names <code>x_10</code> and <code>x_11</code> (lines
  182. <code>4</code> and <code>8</code>).
  183. </p>
  184. <div class="smallexample">
  185. <pre class="smallexample"> 1 L0:
  186. 2 x_1 = PHI (0, x_5)
  187. 3 if (x_1 &lt; 10)
  188. 4 x_10 = &hellip;
  189. 5 if (x_1 &gt; 7)
  190. 6 y_2 = 0
  191. 7 else
  192. 8 x_11 = &hellip;
  193. 9 y_3 = x_1 + x_7
  194. 10 endif
  195. 11 x_5 = x_1 + 1
  196. 12 goto L0;
  197. 13 endif
  198. </pre></div>
  199. <p>We want to replace all the uses of <code>x_1</code> with the new definitions
  200. of <code>x_10</code> and <code>x_11</code>. Note that the only uses that should
  201. be replaced are those at lines <code>5</code>, <code>9</code> and <code>11</code>.
  202. Also, the use of <code>x_7</code> at line <code>9</code> should <em>not</em> be
  203. replaced (this is why we cannot just mark symbol <code>x</code> for
  204. renaming).
  205. </p>
  206. <p>Additionally, we may need to insert a PHI node at line <code>11</code>
  207. because that is a merge point for <code>x_10</code> and <code>x_11</code>. So the
  208. use of <code>x_1</code> at line <code>11</code> will be replaced with the new PHI
  209. node. The insertion of PHI nodes is optional. They are not strictly
  210. necessary to preserve the SSA form, and depending on what the caller
  211. inserted, they may not even be useful for the optimizers.
  212. </p>
  213. <p>Updating the SSA form is a two step process. First, the pass has to
  214. identify which names need to be updated and/or which symbols need to
  215. be renamed into SSA form for the first time. When new names are
  216. introduced to replace existing names in the program, the mapping
  217. between the old and the new names are registered by calling
  218. <code>register_new_name_mapping</code> (note that if your pass creates new
  219. code by duplicating basic blocks, the call to <code>tree_duplicate_bb</code>
  220. will set up the necessary mappings automatically).
  221. </p>
  222. <p>After the replacement mappings have been registered and new symbols
  223. marked for renaming, a call to <code>update_ssa</code> makes the registered
  224. changes. This can be done with an explicit call or by creating
  225. <code>TODO</code> flags in the <code>tree_opt_pass</code> structure for your pass.
  226. There are several <code>TODO</code> flags that control the behavior of
  227. <code>update_ssa</code>:
  228. </p>
  229. <ul>
  230. <li> <code>TODO_update_ssa</code>. Update the SSA form inserting PHI nodes
  231. for newly exposed symbols and virtual names marked for updating.
  232. When updating real names, only insert PHI nodes for a real name
  233. <code>O_j</code> in blocks reached by all the new and old definitions for
  234. <code>O_j</code>. If the iterated dominance frontier for <code>O_j</code>
  235. is not pruned, we may end up inserting PHI nodes in blocks that
  236. have one or more edges with no incoming definition for
  237. <code>O_j</code>. This would lead to uninitialized warnings for
  238. <code>O_j</code>&rsquo;s symbol.
  239. </li><li> <code>TODO_update_ssa_no_phi</code>. Update the SSA form without
  240. inserting any new PHI nodes at all. This is used by passes that
  241. have either inserted all the PHI nodes themselves or passes that
  242. need only to patch use-def and def-def chains for virtuals
  243. (e.g., DCE).
  244. </li><li> <code>TODO_update_ssa_full_phi</code>. Insert PHI nodes everywhere
  245. they are needed. No pruning of the IDF is done. This is used
  246. by passes that need the PHI nodes for <code>O_j</code> even if it
  247. means that some arguments will come from the default definition
  248. of <code>O_j</code>&rsquo;s symbol (e.g., <code>pass_linear_transform</code>).
  249. <p>WARNING: If you need to use this flag, chances are that your
  250. pass may be doing something wrong. Inserting PHI nodes for an
  251. old name where not all edges carry a new replacement may lead to
  252. silent codegen errors or spurious uninitialized warnings.
  253. </p>
  254. </li><li> <code>TODO_update_ssa_only_virtuals</code>. Passes that update the
  255. SSA form on their own may want to delegate the updating of
  256. virtual names to the generic updater. Since FUD chains are
  257. easier to maintain, this simplifies the work they need to do.
  258. NOTE: If this flag is used, any OLD-&gt;NEW mappings for real names
  259. are explicitly destroyed and only the symbols marked for
  260. renaming are processed.
  261. </li></ul>
  262. <a name="Examining-SSA_005fNAME-nodes"></a>
  263. <h4 class="subsection">13.3.2 Examining <code>SSA_NAME</code> nodes</h4>
  264. <a name="index-examining-SSA_005fNAMEs"></a>
  265. <p>The following macros can be used to examine <code>SSA_NAME</code> nodes
  266. </p>
  267. <dl>
  268. <dt><a name="index-SSA_005fNAME_005fDEF_005fSTMT"></a>Macro: <strong>SSA_NAME_DEF_STMT</strong> <em>(<var>var</var>)</em></dt>
  269. <dd><p>Returns the statement <var>s</var> that creates the <code>SSA_NAME</code>
  270. <var>var</var>. If <var>s</var> is an empty statement (i.e., <code>IS_EMPTY_STMT
  271. (<var>s</var>)</code> returns <code>true</code>), it means that the first reference to
  272. this variable is a USE or a VUSE.
  273. </p></dd></dl>
  274. <dl>
  275. <dt><a name="index-SSA_005fNAME_005fVERSION"></a>Macro: <strong>SSA_NAME_VERSION</strong> <em>(<var>var</var>)</em></dt>
  276. <dd><p>Returns the version number of the <code>SSA_NAME</code> object <var>var</var>.
  277. </p></dd></dl>
  278. <a name="Walking-the-dominator-tree"></a>
  279. <h4 class="subsection">13.3.3 Walking the dominator tree</h4>
  280. <dl>
  281. <dt><a name="index-walk_005fdominator_005ftree"></a>Tree SSA function: <em>void</em> <strong>walk_dominator_tree</strong> <em>(<var>walk_data</var>, <var>bb</var>)</em></dt>
  282. <dd>
  283. <p>This function walks the dominator tree for the current CFG calling a
  284. set of callback functions defined in <var>struct dom_walk_data</var> in
  285. <samp>domwalk.h</samp>. The call back functions you need to define give you
  286. hooks to execute custom code at various points during traversal:
  287. </p>
  288. <ol>
  289. <li> Once to initialize any local data needed while processing
  290. <var>bb</var> and its children. This local data is pushed into an
  291. internal stack which is automatically pushed and popped as the
  292. walker traverses the dominator tree.
  293. </li><li> Once before traversing all the statements in the <var>bb</var>.
  294. </li><li> Once for every statement inside <var>bb</var>.
  295. </li><li> Once after traversing all the statements and before recursing
  296. into <var>bb</var>&rsquo;s dominator children.
  297. </li><li> It then recurses into all the dominator children of <var>bb</var>.
  298. </li><li> After recursing into all the dominator children of <var>bb</var> it
  299. can, optionally, traverse every statement in <var>bb</var> again
  300. (i.e., repeating steps 2 and 3).
  301. </li><li> Once after walking the statements in <var>bb</var> and <var>bb</var>&rsquo;s
  302. dominator children. At this stage, the block local data stack
  303. is popped.
  304. </li></ol>
  305. </dd></dl>
  306. <hr>
  307. <div class="header">
  308. <p>
  309. Next: <a href="Alias-analysis.html#Alias-analysis" accesskey="n" rel="next">Alias analysis</a>, Previous: <a href="SSA-Operands.html#SSA-Operands" accesskey="p" rel="prev">SSA Operands</a>, Up: <a href="Tree-SSA.html#Tree-SSA" accesskey="u" rel="up">Tree SSA</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>
  310. </div>
  311. </body>
  312. </html>