Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

483 lines
19KB

  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 Operands (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="SSA Operands (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="SSA Operands (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="SSA.html#SSA" rel="next" title="SSA">
  31. <link href="Annotations.html#Annotations" rel="prev" title="Annotations">
  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-Operands"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="SSA.html#SSA" accesskey="n" rel="next">SSA</a>, Previous: <a href="Annotations.html#Annotations" accesskey="p" rel="prev">Annotations</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="SSA-Operands-1"></a>
  68. <h3 class="section">13.2 SSA Operands</h3>
  69. <a name="index-operands"></a>
  70. <a name="index-virtual-operands"></a>
  71. <a name="index-real-operands"></a>
  72. <a name="index-update_005fstmt-1"></a>
  73. <p>Almost every GIMPLE statement will contain a reference to a variable
  74. or memory location. Since statements come in different shapes and
  75. sizes, their operands are going to be located at various spots inside
  76. the statement&rsquo;s tree. To facilitate access to the statement&rsquo;s
  77. operands, they are organized into lists associated inside each
  78. statement&rsquo;s annotation. Each element in an operand list is a pointer
  79. to a <code>VAR_DECL</code>, <code>PARM_DECL</code> or <code>SSA_NAME</code> tree node.
  80. This provides a very convenient way of examining and replacing
  81. operands.
  82. </p>
  83. <p>Data flow analysis and optimization is done on all tree nodes
  84. representing variables. Any node for which <code>SSA_VAR_P</code> returns
  85. nonzero is considered when scanning statement operands. However, not
  86. all <code>SSA_VAR_P</code> variables are processed in the same way. For the
  87. purposes of optimization, we need to distinguish between references to
  88. local scalar variables and references to globals, statics, structures,
  89. arrays, aliased variables, etc. The reason is simple, the compiler
  90. can gather complete data flow information for a local scalar. On the
  91. other hand, a global variable may be modified by a function call, it
  92. may not be possible to keep track of all the elements of an array or
  93. the fields of a structure, etc.
  94. </p>
  95. <p>The operand scanner gathers two kinds of operands: <em>real</em> and
  96. <em>virtual</em>. An operand for which <code>is_gimple_reg</code> returns true
  97. is considered real, otherwise it is a virtual operand. We also
  98. distinguish between uses and definitions. An operand is used if its
  99. value is loaded by the statement (e.g., the operand at the RHS of an
  100. assignment). If the statement assigns a new value to the operand, the
  101. operand is considered a definition (e.g., the operand at the LHS of
  102. an assignment).
  103. </p>
  104. <p>Virtual and real operands also have very different data flow
  105. properties. Real operands are unambiguous references to the
  106. full object that they represent. For instance, given
  107. </p>
  108. <div class="smallexample">
  109. <pre class="smallexample">{
  110. int a, b;
  111. a = b
  112. }
  113. </pre></div>
  114. <p>Since <code>a</code> and <code>b</code> are non-aliased locals, the statement
  115. <code>a = b</code> will have one real definition and one real use because
  116. variable <code>a</code> is completely modified with the contents of
  117. variable <code>b</code>. Real definition are also known as <em>killing
  118. definitions</em>. Similarly, the use of <code>b</code> reads all its bits.
  119. </p>
  120. <p>In contrast, virtual operands are used with variables that can have
  121. a partial or ambiguous reference. This includes structures, arrays,
  122. globals, and aliased variables. In these cases, we have two types of
  123. definitions. For globals, structures, and arrays, we can determine from
  124. a statement whether a variable of these types has a killing definition.
  125. If the variable does, then the statement is marked as having a
  126. <em>must definition</em> of that variable. However, if a statement is only
  127. defining a part of the variable (i.e. a field in a structure), or if we
  128. know that a statement might define the variable but we cannot say for sure,
  129. then we mark that statement as having a <em>may definition</em>. For
  130. instance, given
  131. </p>
  132. <div class="smallexample">
  133. <pre class="smallexample">{
  134. int a, b, *p;
  135. if (&hellip;)
  136. p = &amp;a;
  137. else
  138. p = &amp;b;
  139. *p = 5;
  140. return *p;
  141. }
  142. </pre></div>
  143. <p>The assignment <code>*p = 5</code> may be a definition of <code>a</code> or
  144. <code>b</code>. If we cannot determine statically where <code>p</code> is
  145. pointing to at the time of the store operation, we create virtual
  146. definitions to mark that statement as a potential definition site for
  147. <code>a</code> and <code>b</code>. Memory loads are similarly marked with virtual
  148. use operands. Virtual operands are shown in tree dumps right before
  149. the statement that contains them. To request a tree dump with virtual
  150. operands, use the <samp>-vops</samp> option to <samp>-fdump-tree</samp>:
  151. </p>
  152. <div class="smallexample">
  153. <pre class="smallexample">{
  154. int a, b, *p;
  155. if (&hellip;)
  156. p = &amp;a;
  157. else
  158. p = &amp;b;
  159. # a = VDEF &lt;a&gt;
  160. # b = VDEF &lt;b&gt;
  161. *p = 5;
  162. # VUSE &lt;a&gt;
  163. # VUSE &lt;b&gt;
  164. return *p;
  165. }
  166. </pre></div>
  167. <p>Notice that <code>VDEF</code> operands have two copies of the referenced
  168. variable. This indicates that this is not a killing definition of
  169. that variable. In this case we refer to it as a <em>may definition</em>
  170. or <em>aliased store</em>. The presence of the second copy of the
  171. variable in the <code>VDEF</code> operand will become important when the
  172. function is converted into SSA form. This will be used to link all
  173. the non-killing definitions to prevent optimizations from making
  174. incorrect assumptions about them.
  175. </p>
  176. <p>Operands are updated as soon as the statement is finished via a call
  177. to <code>update_stmt</code>. If statement elements are changed via
  178. <code>SET_USE</code> or <code>SET_DEF</code>, then no further action is required
  179. (i.e., those macros take care of updating the statement). If changes
  180. are made by manipulating the statement&rsquo;s tree directly, then a call
  181. must be made to <code>update_stmt</code> when complete. Calling one of the
  182. <code>bsi_insert</code> routines or <code>bsi_replace</code> performs an implicit
  183. call to <code>update_stmt</code>.
  184. </p>
  185. <a name="Operand-Iterators-And-Access-Routines"></a>
  186. <h4 class="subsection">13.2.1 Operand Iterators And Access Routines</h4>
  187. <a name="index-Operand-Iterators"></a>
  188. <a name="index-Operand-Access-Routines"></a>
  189. <p>Operands are collected by <samp>tree-ssa-operands.c</samp>. They are stored
  190. inside each statement&rsquo;s annotation and can be accessed through either the
  191. operand iterators or an access routine.
  192. </p>
  193. <p>The following access routines are available for examining operands:
  194. </p>
  195. <ol>
  196. <li> <code>SINGLE_SSA_{USE,DEF,TREE}_OPERAND</code>: These accessors will return
  197. NULL unless there is exactly one operand matching the specified flags. If
  198. there is exactly one operand, the operand is returned as either a <code>tree</code>,
  199. <code>def_operand_p</code>, or <code>use_operand_p</code>.
  200. <div class="smallexample">
  201. <pre class="smallexample">tree t = SINGLE_SSA_TREE_OPERAND (stmt, flags);
  202. use_operand_p u = SINGLE_SSA_USE_OPERAND (stmt, SSA_ALL_VIRTUAL_USES);
  203. def_operand_p d = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_ALL_DEFS);
  204. </pre></div>
  205. </li><li> <code>ZERO_SSA_OPERANDS</code>: This macro returns true if there are no
  206. operands matching the specified flags.
  207. <div class="smallexample">
  208. <pre class="smallexample">if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
  209. return;
  210. </pre></div>
  211. </li><li> <code>NUM_SSA_OPERANDS</code>: This macro Returns the number of operands
  212. matching &rsquo;flags&rsquo;. This actually executes a loop to perform the count, so
  213. only use this if it is really needed.
  214. <div class="smallexample">
  215. <pre class="smallexample">int count = NUM_SSA_OPERANDS (stmt, flags)
  216. </pre></div>
  217. </li></ol>
  218. <p>If you wish to iterate over some or all operands, use the
  219. <code>FOR_EACH_SSA_{USE,DEF,TREE}_OPERAND</code> iterator. For example, to print
  220. all the operands for a statement:
  221. </p>
  222. <div class="smallexample">
  223. <pre class="smallexample">void
  224. print_ops (tree stmt)
  225. {
  226. ssa_op_iter;
  227. tree var;
  228. FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_OPERANDS)
  229. print_generic_expr (stderr, var, TDF_SLIM);
  230. }
  231. </pre></div>
  232. <p>How to choose the appropriate iterator:
  233. </p>
  234. <ol>
  235. <li> Determine whether you are need to see the operand pointers, or just the
  236. trees, and choose the appropriate macro:
  237. <div class="smallexample">
  238. <pre class="smallexample">Need Macro:
  239. ---- -------
  240. use_operand_p FOR_EACH_SSA_USE_OPERAND
  241. def_operand_p FOR_EACH_SSA_DEF_OPERAND
  242. tree FOR_EACH_SSA_TREE_OPERAND
  243. </pre></div>
  244. </li><li> You need to declare a variable of the type you are interested
  245. in, and an ssa_op_iter structure which serves as the loop controlling
  246. variable.
  247. </li><li> Determine which operands you wish to use, and specify the flags of
  248. those you are interested in. They are documented in
  249. <samp>tree-ssa-operands.h</samp>:
  250. <div class="smallexample">
  251. <pre class="smallexample">#define SSA_OP_USE 0x01 /* <span class="roman">Real USE operands.</span> */
  252. #define SSA_OP_DEF 0x02 /* <span class="roman">Real DEF operands.</span> */
  253. #define SSA_OP_VUSE 0x04 /* <span class="roman">VUSE operands.</span> */
  254. #define SSA_OP_VDEF 0x08 /* <span class="roman">VDEF operands.</span> */
  255. /* <span class="roman">These are commonly grouped operand flags.</span> */
  256. #define SSA_OP_VIRTUAL_USES (SSA_OP_VUSE)
  257. #define SSA_OP_VIRTUAL_DEFS (SSA_OP_VDEF)
  258. #define SSA_OP_ALL_VIRTUALS (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_DEFS)
  259. #define SSA_OP_ALL_USES (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
  260. #define SSA_OP_ALL_DEFS (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
  261. #define SSA_OP_ALL_OPERANDS (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
  262. </pre></div>
  263. </li></ol>
  264. <p>So if you want to look at the use pointers for all the <code>USE</code> and
  265. <code>VUSE</code> operands, you would do something like:
  266. </p>
  267. <div class="smallexample">
  268. <pre class="smallexample"> use_operand_p use_p;
  269. ssa_op_iter iter;
  270. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, (SSA_OP_USE | SSA_OP_VUSE))
  271. {
  272. process_use_ptr (use_p);
  273. }
  274. </pre></div>
  275. <p>The <code>TREE</code> macro is basically the same as the <code>USE</code> and
  276. <code>DEF</code> macros, only with the use or def dereferenced via
  277. <code>USE_FROM_PTR (use_p)</code> and <code>DEF_FROM_PTR (def_p)</code>. Since we
  278. aren&rsquo;t using operand pointers, use and defs flags can be mixed.
  279. </p>
  280. <div class="smallexample">
  281. <pre class="smallexample"> tree var;
  282. ssa_op_iter iter;
  283. FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_VUSE)
  284. {
  285. print_generic_expr (stderr, var, TDF_SLIM);
  286. }
  287. </pre></div>
  288. <p><code>VDEF</code>s are broken into two flags, one for the
  289. <code>DEF</code> portion (<code>SSA_OP_VDEF</code>) and one for the USE portion
  290. (<code>SSA_OP_VUSE</code>).
  291. </p>
  292. <p>There are many examples in the code, in addition to the documentation
  293. in <samp>tree-ssa-operands.h</samp> and <samp>ssa-iterators.h</samp>.
  294. </p>
  295. <p>There are also a couple of variants on the stmt iterators regarding PHI
  296. nodes.
  297. </p>
  298. <p><code>FOR_EACH_PHI_ARG</code> Works exactly like
  299. <code>FOR_EACH_SSA_USE_OPERAND</code>, except it works over <code>PHI</code> arguments
  300. instead of statement operands.
  301. </p>
  302. <div class="smallexample">
  303. <pre class="smallexample">/* Look at every virtual PHI use. */
  304. FOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_VIRTUAL_USES)
  305. {
  306. my_code;
  307. }
  308. /* Look at every real PHI use. */
  309. FOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_USES)
  310. my_code;
  311. /* Look at every PHI use. */
  312. FOR_EACH_PHI_ARG (use_p, phi_stmt, iter, SSA_OP_ALL_USES)
  313. my_code;
  314. </pre></div>
  315. <p><code>FOR_EACH_PHI_OR_STMT_{USE,DEF}</code> works exactly like
  316. <code>FOR_EACH_SSA_{USE,DEF}_OPERAND</code>, except it will function on
  317. either a statement or a <code>PHI</code> node. These should be used when it is
  318. appropriate but they are not quite as efficient as the individual
  319. <code>FOR_EACH_PHI</code> and <code>FOR_EACH_SSA</code> routines.
  320. </p>
  321. <div class="smallexample">
  322. <pre class="smallexample">FOR_EACH_PHI_OR_STMT_USE (use_operand_p, stmt, iter, flags)
  323. {
  324. my_code;
  325. }
  326. FOR_EACH_PHI_OR_STMT_DEF (def_operand_p, phi, iter, flags)
  327. {
  328. my_code;
  329. }
  330. </pre></div>
  331. <a name="Immediate-Uses"></a>
  332. <h4 class="subsection">13.2.2 Immediate Uses</h4>
  333. <a name="index-Immediate-Uses"></a>
  334. <p>Immediate use information is now always available. Using the immediate use
  335. iterators, you may examine every use of any <code>SSA_NAME</code>. For instance,
  336. to change each use of <code>ssa_var</code> to <code>ssa_var2</code> and call fold_stmt on
  337. each stmt after that is done:
  338. </p>
  339. <div class="smallexample">
  340. <pre class="smallexample"> use_operand_p imm_use_p;
  341. imm_use_iterator iterator;
  342. tree ssa_var, stmt;
  343. FOR_EACH_IMM_USE_STMT (stmt, iterator, ssa_var)
  344. {
  345. FOR_EACH_IMM_USE_ON_STMT (imm_use_p, iterator)
  346. SET_USE (imm_use_p, ssa_var_2);
  347. fold_stmt (stmt);
  348. }
  349. </pre></div>
  350. <p>There are 2 iterators which can be used. <code>FOR_EACH_IMM_USE_FAST</code> is
  351. used when the immediate uses are not changed, i.e., you are looking at the
  352. uses, but not setting them.
  353. </p>
  354. <p>If they do get changed, then care must be taken that things are not changed
  355. under the iterators, so use the <code>FOR_EACH_IMM_USE_STMT</code> and
  356. <code>FOR_EACH_IMM_USE_ON_STMT</code> iterators. They attempt to preserve the
  357. sanity of the use list by moving all the uses for a statement into
  358. a controlled position, and then iterating over those uses. Then the
  359. optimization can manipulate the stmt when all the uses have been
  360. processed. This is a little slower than the FAST version since it adds a
  361. placeholder element and must sort through the list a bit for each statement.
  362. This placeholder element must be also be removed if the loop is
  363. terminated early. The macro <code>BREAK_FROM_IMM_USE_STMT</code> is provided
  364. to do this :
  365. </p>
  366. <div class="smallexample">
  367. <pre class="smallexample"> FOR_EACH_IMM_USE_STMT (stmt, iterator, ssa_var)
  368. {
  369. if (stmt == last_stmt)
  370. BREAK_FROM_IMM_USE_STMT (iterator);
  371. FOR_EACH_IMM_USE_ON_STMT (imm_use_p, iterator)
  372. SET_USE (imm_use_p, ssa_var_2);
  373. fold_stmt (stmt);
  374. }
  375. </pre></div>
  376. <p>There are checks in <code>verify_ssa</code> which verify that the immediate use list
  377. is up to date, as well as checking that an optimization didn&rsquo;t break from the
  378. loop without using this macro. It is safe to simply &rsquo;break&rsquo;; from a
  379. <code>FOR_EACH_IMM_USE_FAST</code> traverse.
  380. </p>
  381. <p>Some useful functions and macros:
  382. </p><ol>
  383. <li> <code>has_zero_uses (ssa_var)</code> : Returns true if there are no uses of
  384. <code>ssa_var</code>.
  385. </li><li> <code>has_single_use (ssa_var)</code> : Returns true if there is only a
  386. single use of <code>ssa_var</code>.
  387. </li><li> <code>single_imm_use (ssa_var, use_operand_p *ptr, tree *stmt)</code> :
  388. Returns true if there is only a single use of <code>ssa_var</code>, and also returns
  389. the use pointer and statement it occurs in, in the second and third parameters.
  390. </li><li> <code>num_imm_uses (ssa_var)</code> : Returns the number of immediate uses of
  391. <code>ssa_var</code>. It is better not to use this if possible since it simply
  392. utilizes a loop to count the uses.
  393. </li><li> <code>PHI_ARG_INDEX_FROM_USE (use_p)</code> : Given a use within a <code>PHI</code>
  394. node, return the index number for the use. An assert is triggered if the use
  395. isn&rsquo;t located in a <code>PHI</code> node.
  396. </li><li> <code>USE_STMT (use_p)</code> : Return the statement a use occurs in.
  397. </li></ol>
  398. <p>Note that uses are not put into an immediate use list until their statement is
  399. actually inserted into the instruction stream via a <code>bsi_*</code> routine.
  400. </p>
  401. <p>It is also still possible to utilize lazy updating of statements, but this
  402. should be used only when absolutely required. Both alias analysis and the
  403. dominator optimizations currently do this.
  404. </p>
  405. <p>When lazy updating is being used, the immediate use information is out of date
  406. and cannot be used reliably. Lazy updating is achieved by simply marking
  407. statements modified via calls to <code>gimple_set_modified</code> instead of
  408. <code>update_stmt</code>. When lazy updating is no longer required, all the
  409. modified statements must have <code>update_stmt</code> called in order to bring them
  410. up to date. This must be done before the optimization is finished, or
  411. <code>verify_ssa</code> will trigger an abort.
  412. </p>
  413. <p>This is done with a simple loop over the instruction stream:
  414. </p><div class="smallexample">
  415. <pre class="smallexample"> block_stmt_iterator bsi;
  416. basic_block bb;
  417. FOR_EACH_BB (bb)
  418. {
  419. for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&amp;bsi))
  420. update_stmt_if_modified (bsi_stmt (bsi));
  421. }
  422. </pre></div>
  423. <hr>
  424. <div class="header">
  425. <p>
  426. Next: <a href="SSA.html#SSA" accesskey="n" rel="next">SSA</a>, Previous: <a href="Annotations.html#Annotations" accesskey="p" rel="prev">Annotations</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>
  427. </div>
  428. </body>
  429. </html>