]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gccint/generic/statements.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gccint / generic / statements.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. index:: Statements
7
8 .. _statements:
9
10 Statements
11 **********
12
13 Most statements in GIMPLE are assignment statements, represented by
14 ``GIMPLE_ASSIGN``. No other C expressions can appear at statement level;
15 a reference to a volatile object is converted into a
16 ``GIMPLE_ASSIGN``.
17
18 There are also several varieties of complex statements.
19
20 .. toctree::
21 :maxdepth: 2
22
23
24 .. index:: Basic Statements
25
26 .. _basic-statements:
27
28 Basic Statements
29 ^^^^^^^^^^^^^^^^
30
31 .. envvar:: ASM_EXPR
32
33 Used to represent an inline assembly statement. For an inline assembly
34 statement like:
35
36 .. code-block:: c++
37
38 asm ("mov x, y");
39
40 The ``ASM_STRING`` macro will return a ``STRING_CST`` node for
41 ``"mov x, y"``. If the original statement made use of the
42 extended-assembly syntax, then ``ASM_OUTPUTS``,
43 ``ASM_INPUTS``, and ``ASM_CLOBBERS`` will be the outputs, inputs,
44 and clobbers for the statement, represented as ``STRING_CST`` nodes.
45 The extended-assembly syntax looks like:
46
47 .. code-block:: c++
48
49 asm ("fsinx %1,%0" : "=f" (result) : "f" (angle));
50
51 The first string is the ``ASM_STRING``, containing the instruction
52 template. The next two strings are the output and inputs, respectively;
53 this statement has no clobbers. As this example indicates, 'plain'
54 assembly statements are merely a special case of extended assembly
55 statements; they have no cv-qualifiers, outputs, inputs, or clobbers.
56 All of the strings will be ``NUL`` -terminated, and will contain no
57 embedded ``NUL`` -characters.
58
59 If the assembly statement is declared ``volatile``, or if the
60 statement was not an extended assembly statement, and is therefore
61 implicitly volatile, then the predicate ``ASM_VOLATILE_P`` will hold
62 of the ``ASM_EXPR``.
63
64 .. envvar:: DECL_EXPR
65
66 Used to represent a local declaration. The ``DECL_EXPR_DECL`` macro
67 can be used to obtain the entity declared. This declaration may be a
68 ``LABEL_DECL``, indicating that the label declared is a local label.
69 (As an extension, GCC allows the declaration of labels with scope.) In
70 C, this declaration may be a ``FUNCTION_DECL``, indicating the
71 use of the GCC nested function extension. For more information,
72 see :ref:`functions`.
73
74 .. envvar:: LABEL_EXPR
75
76 Used to represent a label. The ``LABEL_DECL`` declared by this
77 statement can be obtained with the ``LABEL_EXPR_LABEL`` macro. The
78 ``IDENTIFIER_NODE`` giving the name of the label can be obtained from
79 the ``LABEL_DECL`` with ``DECL_NAME``.
80
81 .. envvar:: GOTO_EXPR
82
83 Used to represent a ``goto`` statement. The ``GOTO_DESTINATION`` will
84 usually be a ``LABEL_DECL``. However, if the 'computed goto' extension
85 has been used, the ``GOTO_DESTINATION`` will be an arbitrary expression
86 indicating the destination. This expression will always have pointer type.
87
88 .. envvar:: RETURN_EXPR
89
90 Used to represent a ``return`` statement. Operand 0 represents the
91 value to return. It should either be the ``RESULT_DECL`` for the
92 containing function, or a ``MODIFY_EXPR`` or ``INIT_EXPR``
93 setting the function's ``RESULT_DECL``. It will be
94 ``NULL_TREE`` if the statement was just
95
96 .. code-block:: c++
97
98 return;
99
100 .. envvar:: LOOP_EXPR
101
102 These nodes represent 'infinite' loops. The ``LOOP_EXPR_BODY``
103 represents the body of the loop. It should be executed forever, unless
104 an ``EXIT_EXPR`` is encountered.
105
106 .. envvar:: EXIT_EXPR
107
108 These nodes represent conditional exits from the nearest enclosing
109 ``LOOP_EXPR``. The single operand is the condition; if it is
110 nonzero, then the loop should be exited. An ``EXIT_EXPR`` will only
111 appear within a ``LOOP_EXPR``.
112
113 .. envvar:: SWITCH_EXPR
114
115 Used to represent a ``switch`` statement. The ``SWITCH_COND``
116 is the expression on which the switch is occurring. The
117 ``SWITCH_BODY`` is the body of the switch statement.
118 ``SWITCH_ALL_CASES_P`` is true if the switch includes a default
119 label or the case label ranges cover all possible values of the
120 condition expression.
121
122 Note that ``TREE_TYPE`` for a ``SWITCH_EXPR`` represents the
123 original type of switch expression as given in the source, before any
124 compiler conversions, instead of the type of the switch expression
125 itself (which is not meaningful).
126
127 .. envvar:: CASE_LABEL_EXPR
128
129 Use to represent a ``case`` label, range of ``case`` labels, or a
130 ``default`` label. If ``CASE_LOW`` is ``NULL_TREE``, then this is a
131 ``default`` label. Otherwise, if ``CASE_HIGH`` is ``NULL_TREE``, then
132 this is an ordinary ``case`` label. In this case, ``CASE_LOW`` is
133 an expression giving the value of the label. Both ``CASE_LOW`` and
134 ``CASE_HIGH`` are ``INTEGER_CST`` nodes. These values will have
135 the same type as the condition expression in the switch statement.
136
137 Otherwise, if both ``CASE_LOW`` and ``CASE_HIGH`` are defined, the
138 statement is a range of case labels. Such statements originate with the
139 extension that allows users to write things of the form:
140
141 .. code-block:: c++
142
143 case 2 ... 5:
144
145 The first value will be ``CASE_LOW``, while the second will be
146 ``CASE_HIGH``.
147
148 .. envvar:: DEBUG_BEGIN_STMT
149
150 Marks the beginning of a source statement, for purposes of debug
151 information generation.
152
153 .. index:: Blocks
154
155 .. _blocks:
156
157 Blocks
158 ^^^^^^
159
160 Block scopes and the variables they declare in GENERIC are
161 expressed using the ``BIND_EXPR`` code, which in previous
162 versions of GCC was primarily used for the C statement-expression
163 extension.
164
165 Variables in a block are collected into ``BIND_EXPR_VARS`` in
166 declaration order through their ``TREE_CHAIN`` field. Any runtime
167 initialization is moved out of ``DECL_INITIAL`` and into a
168 statement in the controlled block. When gimplifying from C or C++,
169 this initialization replaces the ``DECL_STMT``. These variables
170 will never require cleanups. The scope of these variables is just the
171 body
172
173 Variable-length arrays (VLAs) complicate this process, as their size
174 often refers to variables initialized earlier in the block and their
175 initialization involves an explicit stack allocation. To handle this,
176 we add an indirection and replace them with a pointer to stack space
177 allocated by means of ``alloca``. In most cases, we also arrange
178 for this space to be reclaimed when the enclosing ``BIND_EXPR`` is
179 exited, the exception to this being when there is an explicit call to
180 ``alloca`` in the source code, in which case the stack is left
181 depressed on exit of the ``BIND_EXPR``.
182
183 A C++ program will usually contain more ``BIND_EXPR`` s than
184 there are syntactic blocks in the source code, since several C++
185 constructs have implicit scopes associated with them. On the
186 other hand, although the C++ front end uses pseudo-scopes to
187 handle cleanups for objects with destructors, these don't
188 translate into the GIMPLE form; multiple declarations at the same
189 level use the same ``BIND_EXPR``.
190
191 .. index:: Statement Sequences
192
193 .. _statement-sequences:
194
195 Statement Sequences
196 ^^^^^^^^^^^^^^^^^^^
197
198 Multiple statements at the same nesting level are collected into
199 a ``STATEMENT_LIST``. Statement lists are modified and
200 traversed using the interface in :samp:`tree-iterator.h`.
201
202 .. index:: Empty Statements
203
204 .. _empty-statements:
205
206 Empty Statements
207 ^^^^^^^^^^^^^^^^
208
209 Whenever possible, statements with no effect are discarded. But
210 if they are nested within another construct which cannot be
211 discarded for some reason, they are instead replaced with an
212 empty statement, generated by ``build_empty_stmt``.
213 Initially, all empty statements were shared, after the pattern of
214 the Java front end, but this caused a lot of trouble in practice.
215
216 An empty statement is represented as ``(void)0``.
217
218 .. index:: Jumps
219
220 .. _jumps:
221
222 Jumps
223 ^^^^^
224
225 Other jumps are expressed by either ``GOTO_EXPR`` or
226 ``RETURN_EXPR``.
227
228 The operand of a ``GOTO_EXPR`` must be either a label or a
229 variable containing the address to jump to.
230
231 The operand of a ``RETURN_EXPR`` is either ``NULL_TREE``,
232 ``RESULT_DECL``, or a ``MODIFY_EXPR`` which sets the return
233 value. It would be nice to move the ``MODIFY_EXPR`` into a
234 separate statement, but the special return semantics in
235 ``expand_return`` make that difficult. It may still happen in
236 the future, perhaps by moving most of that logic into
237 ``expand_assignment``.
238
239 .. index:: Cleanups
240
241 .. _cleanups:
242
243 Cleanups
244 ^^^^^^^^
245
246 Destructors for local C++ objects and similar dynamic cleanups are
247 represented in GIMPLE by a ``TRY_FINALLY_EXPR``.
248 ``TRY_FINALLY_EXPR`` has two operands, both of which are a sequence
249 of statements to execute. The first sequence is executed. When it
250 completes the second sequence is executed.
251
252 The first sequence may complete in the following ways:
253
254 * Execute the last statement in the sequence and fall off the
255 end.
256
257 * Execute a goto statement (``GOTO_EXPR``) to an ordinary
258 label outside the sequence.
259
260 * Execute a return statement (``RETURN_EXPR``).
261
262 * Throw an exception. This is currently not explicitly represented in
263 GIMPLE.
264
265 The second sequence is not executed if the first sequence completes by
266 calling ``setjmp`` or ``exit`` or any other function that does
267 not return. The second sequence is also not executed if the first
268 sequence completes via a non-local goto or a computed goto (in general
269 the compiler does not know whether such a goto statement exits the
270 first sequence or not, so we assume that it doesn't).
271
272 After the second sequence is executed, if it completes normally by
273 falling off the end, execution continues wherever the first sequence
274 would have continued, by falling off the end, or doing a goto, etc.
275
276 If the second sequence is an ``EH_ELSE_EXPR`` selector, then the
277 sequence in its first operand is used when the first sequence completes
278 normally, and that in its second operand is used for exceptional
279 cleanups, i.e., when an exception propagates out of the first sequence.
280
281 ``TRY_FINALLY_EXPR`` complicates the flow graph, since the cleanup
282 needs to appear on every edge out of the controlled block; this
283 reduces the freedom to move code across these edges. Therefore, the
284 EH lowering pass which runs before most of the optimization passes
285 eliminates these expressions by explicitly adding the cleanup to each
286 edge. Rethrowing the exception is represented using ``RESX_EXPR``.
287
288 .. _openmp:
289
290 OpenMP
291 ^^^^^^
292
293 .. index:: OMP_PARALLEL, OMP_FOR, OMP_SECTIONS, OMP_SINGLE, OMP_SECTION, OMP_MASTER, OMP_ORDERED, OMP_CRITICAL, OMP_RETURN, OMP_CONTINUE, OMP_ATOMIC, OMP_CLAUSE
294
295 All the statements starting with ``OMP_`` represent directives and
296 clauses used by the OpenMP API https://www.openmp.org.
297
298 .. envvar:: OMP_PARALLEL
299
300 Represents ``#pragma omp parallel [clause1 ... clauseN]``. It
301 has four operands:
302
303 Operand ``OMP_PARALLEL_BODY`` is valid while in GENERIC and
304 High GIMPLE forms. It contains the body of code to be executed
305 by all the threads. During GIMPLE lowering, this operand becomes
306 ``NULL`` and the body is emitted linearly after
307 ``OMP_PARALLEL``.
308
309 Operand ``OMP_PARALLEL_CLAUSES`` is the list of clauses
310 associated with the directive.
311
312 Operand ``OMP_PARALLEL_FN`` is created by
313 ``pass_lower_omp``, it contains the ``FUNCTION_DECL``
314 for the function that will contain the body of the parallel
315 region.
316
317 Operand ``OMP_PARALLEL_DATA_ARG`` is also created by
318 ``pass_lower_omp``. If there are shared variables to be
319 communicated to the children threads, this operand will contain
320 the ``VAR_DECL`` that contains all the shared values and
321 variables.
322
323 .. envvar:: OMP_FOR
324
325 Represents ``#pragma omp for [clause1 ... clauseN]``. It has
326 six operands:
327
328 Operand ``OMP_FOR_BODY`` contains the loop body.
329
330 Operand ``OMP_FOR_CLAUSES`` is the list of clauses
331 associated with the directive.
332
333 Operand ``OMP_FOR_INIT`` is the loop initialization code of
334 the form ``VAR = N1``.
335
336 Operand ``OMP_FOR_COND`` is the loop conditional expression
337 of the form ``VAR {<,>,<=,>=} N2``.
338
339 Operand ``OMP_FOR_INCR`` is the loop index increment of the
340 form ``VAR {+=,-=} INCR``.
341
342 Operand ``OMP_FOR_PRE_BODY`` contains side effect code from
343 operands ``OMP_FOR_INIT``, ``OMP_FOR_COND`` and
344 ``OMP_FOR_INC``. These side effects are part of the
345 ``OMP_FOR`` block but must be evaluated before the start of
346 loop body.
347
348 The loop index variable ``VAR`` must be a signed integer variable,
349 which is implicitly private to each thread. Bounds
350 ``N1`` and ``N2`` and the increment expression
351 ``INCR`` are required to be loop invariant integer
352 expressions that are evaluated without any synchronization. The
353 evaluation order, frequency of evaluation and side effects are
354 unspecified by the standard.
355
356 .. envvar:: OMP_SECTIONS
357
358 Represents ``#pragma omp sections [clause1 ... clauseN]``.
359
360 Operand ``OMP_SECTIONS_BODY`` contains the sections body,
361 which in turn contains a set of ``OMP_SECTION`` nodes for
362 each of the concurrent sections delimited by ``#pragma omp
363 section``.
364
365 Operand ``OMP_SECTIONS_CLAUSES`` is the list of clauses
366 associated with the directive.
367
368 .. envvar:: OMP_SECTION
369
370 Section delimiter for ``OMP_SECTIONS``.
371
372 .. envvar:: OMP_SINGLE
373
374 Represents ``#pragma omp single``.
375
376 Operand ``OMP_SINGLE_BODY`` contains the body of code to be
377 executed by a single thread.
378
379 Operand ``OMP_SINGLE_CLAUSES`` is the list of clauses
380 associated with the directive.
381
382 .. envvar:: OMP_MASTER
383
384 Represents ``#pragma omp master``.
385
386 Operand ``OMP_MASTER_BODY`` contains the body of code to be
387 executed by the master thread.
388
389 .. envvar:: OMP_ORDERED
390
391 Represents ``#pragma omp ordered``.
392
393 Operand ``OMP_ORDERED_BODY`` contains the body of code to be
394 executed in the sequential order dictated by the loop index
395 variable.
396
397 .. envvar:: OMP_CRITICAL
398
399 Represents ``#pragma omp critical [name]``.
400
401 Operand ``OMP_CRITICAL_BODY`` is the critical section.
402
403 Operand ``OMP_CRITICAL_NAME`` is an optional identifier to
404 label the critical section.
405
406 .. envvar:: OMP_RETURN
407
408 This does not represent any OpenMP directive, it is an artificial
409 marker to indicate the end of the body of an OpenMP. It is used
410 by the flow graph (``tree-cfg.cc``) and OpenMP region
411 building code (``omp-low.cc``).
412
413 .. envvar:: OMP_CONTINUE
414
415 Similarly, this instruction does not represent an OpenMP
416 directive, it is used by ``OMP_FOR`` (and similar codes) as well as
417 ``OMP_SECTIONS`` to mark the place where the code needs to
418 loop to the next iteration, or the next section, respectively.
419
420 In some cases, ``OMP_CONTINUE`` is placed right before
421 ``OMP_RETURN``. But if there are cleanups that need to
422 occur right after the looping body, it will be emitted between
423 ``OMP_CONTINUE`` and ``OMP_RETURN``.
424
425 .. envvar:: OMP_ATOMIC
426
427 Represents ``#pragma omp atomic``.
428
429 Operand 0 is the address at which the atomic operation is to be
430 performed.
431
432 Operand 1 is the expression to evaluate. The gimplifier tries
433 three alternative code generation strategies. Whenever possible,
434 an atomic update built-in is used. If that fails, a
435 compare-and-swap loop is attempted. If that also fails, a
436 regular critical section around the expression is used.
437
438 .. envvar:: OMP_CLAUSE
439
440 Represents clauses associated with one of the ``OMP_`` directives.
441 Clauses are represented by separate subcodes defined in
442 :samp:`tree.h`. Clauses codes can be one of:
443 ``OMP_CLAUSE_PRIVATE``, ``OMP_CLAUSE_SHARED``,
444 ``OMP_CLAUSE_FIRSTPRIVATE``,
445 ``OMP_CLAUSE_LASTPRIVATE``, ``OMP_CLAUSE_COPYIN``,
446 ``OMP_CLAUSE_COPYPRIVATE``, ``OMP_CLAUSE_IF``,
447 ``OMP_CLAUSE_NUM_THREADS``, ``OMP_CLAUSE_SCHEDULE``,
448 ``OMP_CLAUSE_NOWAIT``, ``OMP_CLAUSE_ORDERED``,
449 ``OMP_CLAUSE_DEFAULT``, ``OMP_CLAUSE_REDUCTION``,
450 ``OMP_CLAUSE_COLLAPSE``, ``OMP_CLAUSE_UNTIED``,
451 ``OMP_CLAUSE_FINAL``, and ``OMP_CLAUSE_MERGEABLE``. Each code
452 represents the corresponding OpenMP clause.
453
454 Clauses associated with the same directive are chained together
455 via ``OMP_CLAUSE_CHAIN``. Those clauses that accept a list
456 of variables are restricted to exactly one, accessed with
457 ``OMP_CLAUSE_VAR``. Therefore, multiple variables under the
458 same clause ``C`` need to be represented as multiple ``C`` clauses
459 chained together. This facilitates adding new clauses during
460 compilation.
461
462 .. _openacc:
463
464 OpenACC
465 ^^^^^^^
466
467 .. index:: OACC_CACHE, OACC_DATA, OACC_DECLARE, OACC_ENTER_DATA, OACC_EXIT_DATA, OACC_HOST_DATA, OACC_KERNELS, OACC_LOOP, OACC_PARALLEL, OACC_SERIAL, OACC_UPDATE
468
469 All the statements starting with ``OACC_`` represent directives and
470 clauses used by the OpenACC API https://www.openacc.org.
471
472 .. envvar:: OACC_CACHE
473
474 Represents ``#pragma acc cache (var ...)``.
475
476 .. envvar:: OACC_DATA
477
478 Represents ``#pragma acc data [clause1 ... clauseN]``.
479
480 .. envvar:: OACC_DECLARE
481
482 Represents ``#pragma acc declare [clause1 ... clauseN]``.
483
484 .. envvar:: OACC_ENTER_DATA
485
486 Represents ``#pragma acc enter data [clause1 ... clauseN]``.
487
488 .. envvar:: OACC_EXIT_DATA
489
490 Represents ``#pragma acc exit data [clause1 ... clauseN]``.
491
492 .. envvar:: OACC_HOST_DATA
493
494 Represents ``#pragma acc host_data [clause1 ... clauseN]``.
495
496 .. envvar:: OACC_KERNELS
497
498 Represents ``#pragma acc kernels [clause1 ... clauseN]``.
499
500 .. envvar:: OACC_LOOP
501
502 Represents ``#pragma acc loop [clause1 ... clauseN]``.
503
504 See the description of the ``OMP_FOR`` code.
505
506 .. envvar:: OACC_PARALLEL
507
508 Represents ``#pragma acc parallel [clause1 ... clauseN]``.
509
510 .. envvar:: OACC_SERIAL
511
512 Represents ``#pragma acc serial [clause1 ... clauseN]``.
513
514 .. envvar:: OACC_UPDATE
515
516 Represents ``#pragma acc update [clause1 ... clauseN]``.