Line data Source code
1 : /* Header file for SSA iterators.
2 : Copyright (C) 2013-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #ifndef GCC_SSA_ITERATORS_H
21 : #define GCC_SSA_ITERATORS_H
22 :
23 : /* Immediate use lists are used to directly access all uses for an SSA
24 : name and get pointers to the statement for each use.
25 :
26 : The structure ssa_use_operand_t consists of PREV and NEXT pointers
27 : to maintain the list. A USE pointer, which points to address where
28 : the use is located and a LOC pointer which can point to the
29 : statement where the use is located, or, in the case of the root
30 : node, it points to the SSA name itself.
31 :
32 : The list is anchored by an occurrence of ssa_operand_d *in* the
33 : ssa_name node itself (named 'imm_uses'). This node is uniquely
34 : identified by having a NULL USE pointer. and the LOC pointer
35 : pointing back to the ssa_name node itself. This node forms the
36 : base for a circular list, and initially this is the only node in
37 : the list.
38 :
39 : Fast iteration via FOR_EACH_IMM_USE_FAST allows each use to be
40 : examined, but does not allow any modifications to the uses or stmts.
41 :
42 : Safe iteration via FOR_EACH_IMM_USE_STMT and FOR_EACH_IMM_USE_ON_STMT
43 : allows insertion, deletion, and modification of SSA operands within
44 : the current stmt iterated. The iterator manages this by re-sorting
45 : the immediate uses to batch uses on a single stmt after each other.
46 : If using an inner FOR_EACH_IMM_USE_ON_STMT iteration only the active
47 : use may be manipulated. Safety relies on new immediate uses being
48 : inserted at the front of immediate use lists. */
49 :
50 : struct imm_use_iterator
51 : {
52 : /* This is the current use the iterator is processing. */
53 : ssa_use_operand_t *imm_use;
54 : /* This marks the last use in the list (use node from SSA_NAME) */
55 : ssa_use_operand_t *end_p;
56 : /* This is the next ssa_name to visit in an outer FOR_EACH_IMM_USE_STMT.
57 : Also used for fast imm use iterator checking. */
58 : ssa_use_operand_t *next_stmt_use;
59 : /* This is the next ssa_name to visit. IMM_USE may get removed before
60 : the next one is traversed to, so it must be cached early. */
61 : ssa_use_operand_t *next_imm_name;
62 : /* This is the SSA name iterated over. */
63 : tree name;
64 : };
65 :
66 :
67 : /* Use this iterator when simply looking at stmts. Adding, deleting or
68 : modifying stmts will cause this iterator to malfunction. */
69 :
70 : #if ! defined ENABLE_GIMPLE_CHECKING
71 : #define FOR_EACH_IMM_USE_FAST(DEST, ITER, SSAVAR) \
72 : for ((DEST) = first_readonly_imm_use (&(ITER), (SSAVAR)); \
73 : !end_readonly_imm_use_p (&(ITER)); \
74 : (void) ((DEST) = next_readonly_imm_use (&(ITER))))
75 : #else
76 :
77 : /* arrange to automatically call, upon descruction, with a given pointer
78 : to imm_use_iterator. */
79 : struct auto_end_imm_use_fast_traverse
80 : {
81 : imm_use_iterator *imm;
82 1059308668 : auto_end_imm_use_fast_traverse (imm_use_iterator *imm)
83 1050790956 : : imm (imm) {}
84 1059308668 : ~auto_end_imm_use_fast_traverse ()
85 1045257549 : { imm->name->ssa_name.fast_iteration_depth--; }
86 : };
87 :
88 : #define FOR_EACH_IMM_USE_FAST(DEST, ITER, SSAVAR) \
89 : for (struct auto_end_imm_use_fast_traverse \
90 : auto_end_imm_use_fast_traverse \
91 : ((((DEST) = first_readonly_imm_use (&(ITER), (SSAVAR))), \
92 : &(ITER))); \
93 : !end_readonly_imm_use_p (&(ITER)); \
94 : (void) ((DEST) = next_readonly_imm_use (&(ITER))))
95 : #endif
96 :
97 : /* Forward declare for use in the class below. */
98 : inline void end_imm_use_stmt_traverse (imm_use_iterator *);
99 :
100 : /* arrange to automatically call, upon descruction, end_imm_use_stmt_traverse
101 : with a given pointer to imm_use_iterator. */
102 : struct auto_end_imm_use_stmt_traverse
103 : {
104 : imm_use_iterator *imm;
105 113085733 : auto_end_imm_use_stmt_traverse (imm_use_iterator *imm)
106 113085733 : : imm (imm) {}
107 113085733 : ~auto_end_imm_use_stmt_traverse ()
108 113085733 : { end_imm_use_stmt_traverse (imm); }
109 : };
110 :
111 : /* Use this iterator to visit each stmt which has a use of SSAVAR. The
112 : destructor of the auto_end_imm_use_stmt_traverse object deals with removing
113 : ITER from SSAVAR's IMM_USE list even when leaving the scope early. */
114 :
115 : #define FOR_EACH_IMM_USE_STMT(STMT, ITER, SSAVAR) \
116 : for (struct auto_end_imm_use_stmt_traverse \
117 : auto_end_imm_use_stmt_traverse \
118 : ((((STMT) = first_imm_use_stmt (&(ITER), (SSAVAR))), \
119 : &(ITER))); \
120 : !end_imm_use_stmt_p (&(ITER)); \
121 : (void) ((STMT) = next_imm_use_stmt (&(ITER))))
122 :
123 : /* Use this iterator in combination with FOR_EACH_IMM_USE_STMT to
124 : get access to each occurrence of ssavar on the stmt returned by
125 : that iterator.. for instance:
126 :
127 : FOR_EACH_IMM_USE_STMT (stmt, iter, ssavar)
128 : {
129 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
130 : {
131 : SET_USE (use_p, blah);
132 : }
133 : update_stmt (stmt);
134 : } */
135 :
136 : #define FOR_EACH_IMM_USE_ON_STMT(DEST, ITER) \
137 : for ((DEST) = first_imm_use_on_stmt (&(ITER)); \
138 : !end_imm_use_on_stmt_p (&(ITER)); \
139 : (void) ((DEST) = next_imm_use_on_stmt (&(ITER))))
140 :
141 :
142 : /* Use this to get a vector of all gimple stmts using SSAVAR without
143 : duplicates. It's cheaper than FOR_EACH_IMM_USE_STMT and has no
144 : constraints on what you are allowed to do inside an iteration
145 : over the vector. */
146 : extern auto_vec<gimple *, 2> gather_imm_use_stmts (tree ssavar);
147 :
148 : extern bool single_imm_use_1 (const ssa_use_operand_t *head,
149 : use_operand_p *use_p, gimple **stmt);
150 :
151 :
152 : enum ssa_op_iter_type {
153 : ssa_op_iter_none = 0,
154 : ssa_op_iter_tree,
155 : ssa_op_iter_use,
156 : ssa_op_iter_def
157 : };
158 :
159 : /* This structure is used in the operand iterator loops. It contains the
160 : items required to determine which operand is retrieved next. During
161 : optimization, this structure is scalarized, and any unused fields are
162 : optimized away, resulting in little overhead. */
163 :
164 : struct ssa_op_iter
165 : {
166 : enum ssa_op_iter_type iter_type;
167 : bool done;
168 : int flags;
169 : unsigned i;
170 : unsigned numops;
171 : use_optype_p uses;
172 : gimple *stmt;
173 : };
174 :
175 : /* NOTE: Keep these in sync with doc/tree-ssa.texi. */
176 : /* These flags are used to determine which operands are returned during
177 : execution of the loop. */
178 : #define SSA_OP_USE 0x01 /* Real USE operands. */
179 : #define SSA_OP_DEF 0x02 /* Real DEF operands. */
180 : #define SSA_OP_VUSE 0x04 /* VUSE operands. */
181 : #define SSA_OP_VDEF 0x08 /* VDEF operands. */
182 : /* These are commonly grouped operand flags. */
183 : #define SSA_OP_VIRTUAL_USES (SSA_OP_VUSE)
184 : #define SSA_OP_VIRTUAL_DEFS (SSA_OP_VDEF)
185 : #define SSA_OP_ALL_VIRTUALS (SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_DEFS)
186 : #define SSA_OP_ALL_USES (SSA_OP_VIRTUAL_USES | SSA_OP_USE)
187 : #define SSA_OP_ALL_DEFS (SSA_OP_VIRTUAL_DEFS | SSA_OP_DEF)
188 : #define SSA_OP_ALL_OPERANDS (SSA_OP_ALL_USES | SSA_OP_ALL_DEFS)
189 :
190 : /* This macro executes a loop over the operands of STMT specified in FLAG,
191 : returning each operand as a 'tree' in the variable TREEVAR. ITER is an
192 : ssa_op_iter structure used to control the loop. */
193 : #define FOR_EACH_SSA_TREE_OPERAND(TREEVAR, STMT, ITER, FLAGS) \
194 : for (TREEVAR = op_iter_init_tree (&(ITER), STMT, FLAGS); \
195 : !op_iter_done (&(ITER)); \
196 : (void) (TREEVAR = op_iter_next_tree (&(ITER))))
197 :
198 : /* This macro executes a loop over the operands of STMT specified in FLAG,
199 : returning each operand as a 'use_operand_p' in the variable USEVAR.
200 : ITER is an ssa_op_iter structure used to control the loop. */
201 : #define FOR_EACH_SSA_USE_OPERAND(USEVAR, STMT, ITER, FLAGS) \
202 : for (USEVAR = op_iter_init_use (&(ITER), STMT, FLAGS); \
203 : !op_iter_done (&(ITER)); \
204 : USEVAR = op_iter_next_use (&(ITER)))
205 :
206 : /* This macro executes a loop over the operands of STMT specified in FLAG,
207 : returning each operand as a 'def_operand_p' in the variable DEFVAR.
208 : ITER is an ssa_op_iter structure used to control the loop. */
209 : #define FOR_EACH_SSA_DEF_OPERAND(DEFVAR, STMT, ITER, FLAGS) \
210 : for (DEFVAR = op_iter_init_def (&(ITER), STMT, FLAGS); \
211 : !op_iter_done (&(ITER)); \
212 : DEFVAR = op_iter_next_def (&(ITER)))
213 :
214 : /* This macro will execute a loop over all the arguments of a PHI which
215 : match FLAGS. A use_operand_p is always returned via USEVAR. FLAGS
216 : can be either SSA_OP_USE or SSA_OP_VIRTUAL_USES or SSA_OP_ALL_USES. */
217 : #define FOR_EACH_PHI_ARG(USEVAR, STMT, ITER, FLAGS) \
218 : for ((USEVAR) = op_iter_init_phiuse (&(ITER), STMT, FLAGS); \
219 : !op_iter_done (&(ITER)); \
220 : (USEVAR) = op_iter_next_use (&(ITER)))
221 :
222 :
223 : /* This macro will execute a loop over a stmt, regardless of whether it is
224 : a real stmt or a PHI node, looking at the USE nodes matching FLAGS. */
225 : #define FOR_EACH_PHI_OR_STMT_USE(USEVAR, STMT, ITER, FLAGS) \
226 : for ((USEVAR) = (gimple_code (STMT) == GIMPLE_PHI \
227 : ? op_iter_init_phiuse (&(ITER), \
228 : as_a <gphi *> (STMT), \
229 : FLAGS) \
230 : : op_iter_init_use (&(ITER), STMT, FLAGS)); \
231 : !op_iter_done (&(ITER)); \
232 : (USEVAR) = op_iter_next_use (&(ITER)))
233 :
234 : /* This macro will execute a loop over a stmt, regardless of whether it is
235 : a real stmt or a PHI node, looking at the DEF nodes matching FLAGS. */
236 : #define FOR_EACH_PHI_OR_STMT_DEF(DEFVAR, STMT, ITER, FLAGS) \
237 : for ((DEFVAR) = (gimple_code (STMT) == GIMPLE_PHI \
238 : ? op_iter_init_phidef (&(ITER), \
239 : as_a <gphi *> (STMT), \
240 : FLAGS) \
241 : : op_iter_init_def (&(ITER), STMT, FLAGS)); \
242 : !op_iter_done (&(ITER)); \
243 : (DEFVAR) = op_iter_next_def (&(ITER)))
244 :
245 : /* This macro returns an operand in STMT as a tree if it is the ONLY
246 : operand matching FLAGS. If there are 0 or more than 1 operand matching
247 : FLAGS, then NULL_TREE is returned. */
248 : #define SINGLE_SSA_TREE_OPERAND(STMT, FLAGS) \
249 : single_ssa_tree_operand (STMT, FLAGS)
250 :
251 : /* This macro returns an operand in STMT as a use_operand_p if it is the ONLY
252 : operand matching FLAGS. If there are 0 or more than 1 operand matching
253 : FLAGS, then NULL_USE_OPERAND_P is returned. */
254 : #define SINGLE_SSA_USE_OPERAND(STMT, FLAGS) \
255 : single_ssa_use_operand (STMT, FLAGS)
256 :
257 : /* This macro returns an operand in STMT as a def_operand_p if it is the ONLY
258 : operand matching FLAGS. If there are 0 or more than 1 operand matching
259 : FLAGS, then NULL_DEF_OPERAND_P is returned. */
260 : #define SINGLE_SSA_DEF_OPERAND(STMT, FLAGS) \
261 : single_ssa_def_operand (STMT, FLAGS)
262 :
263 : /* This macro returns TRUE if there are no operands matching FLAGS in STMT. */
264 : #define ZERO_SSA_OPERANDS(STMT, FLAGS) zero_ssa_operands (STMT, FLAGS)
265 :
266 : /* This macro counts the number of operands in STMT matching FLAGS. */
267 : #define NUM_SSA_OPERANDS(STMT, FLAGS) num_ssa_operands (STMT, FLAGS)
268 :
269 :
270 : /* Delink an immediate_uses node from its chain. */
271 : inline void
272 772758749 : delink_imm_use (ssa_use_operand_t *linknode)
273 : {
274 : /* Return if this node is not in a list. */
275 772758749 : if (linknode->prev == NULL)
276 : return;
277 :
278 : #if defined ENABLE_GIMPLE_CHECKING
279 464845688 : if (linknode->loc.stmt
280 : /* update_stmt on constant/removed uses. */
281 464845688 : && USE_FROM_PTR (linknode)
282 921226653 : && TREE_CODE (USE_FROM_PTR (linknode)) == SSA_NAME)
283 : {
284 451924750 : tree var = USE_FROM_PTR (linknode);
285 451924750 : gcc_assert (var->ssa_name.fast_iteration_depth == 0
286 : && (var->ssa_name.active_iterated_stmt == NULL
287 : || (var->ssa_name.active_iterated_stmt
288 : == linknode->loc.stmt)));
289 : }
290 : #endif
291 :
292 464845688 : linknode->prev->next = linknode->next;
293 464845688 : linknode->next->prev = linknode->prev;
294 464845688 : linknode->prev = NULL;
295 464845688 : linknode->next = NULL;
296 : }
297 :
298 : /* Link ssa_imm_use node LINKNODE into the chain for LIST. */
299 : inline void
300 567199651 : link_imm_use_to_list (ssa_use_operand_t *linknode, ssa_use_operand_t *list)
301 : {
302 : /* Link the new node at the head of the list. If we are in the process of
303 : traversing the list, we won't visit any new nodes added to it. */
304 567199651 : linknode->prev = list;
305 567199651 : linknode->next = list->next;
306 567199651 : list->next->prev = linknode;
307 567199651 : list->next = linknode;
308 : }
309 :
310 : /* Link ssa_imm_use node LINKNODE into the chain for DEF. */
311 : inline void
312 766425487 : link_imm_use (ssa_use_operand_t *linknode, tree def)
313 : {
314 766425487 : ssa_use_operand_t *root;
315 :
316 766425487 : if (!def || TREE_CODE (def) != SSA_NAME)
317 200647153 : linknode->prev = NULL;
318 : else
319 : {
320 565778334 : root = &(SSA_NAME_IMM_USE_NODE (def));
321 565778334 : if (linknode->use)
322 565778334 : gcc_checking_assert (*(linknode->use) == def);
323 565778334 : link_imm_use_to_list (linknode, root);
324 : }
325 766425487 : }
326 :
327 : /* Set the value of a use pointed to by USE to VAL. */
328 : inline void
329 424215082 : set_ssa_use_from_ptr (use_operand_p use, tree val)
330 : {
331 424215082 : delink_imm_use (use);
332 424215082 : *(use->use) = val;
333 424215082 : link_imm_use (use, val);
334 424215082 : }
335 :
336 : /* Link ssa_imm_use node LINKNODE into the chain for DEF, with use occurring
337 : in STMT. */
338 : inline void
339 342210405 : link_imm_use_stmt (ssa_use_operand_t *linknode, tree def, gimple *stmt)
340 : {
341 342210405 : if (stmt)
342 342210405 : link_imm_use (linknode, def);
343 : else
344 0 : link_imm_use (linknode, NULL);
345 342210405 : linknode->loc.stmt = stmt;
346 : }
347 :
348 : /* Relink a new node in place of an old node in the list. */
349 : inline void
350 60000589 : relink_imm_use (ssa_use_operand_t *node, ssa_use_operand_t *old)
351 : {
352 : /* The node one had better be in the same list. */
353 60000589 : gcc_checking_assert (*(old->use) == *(node->use));
354 60000589 : node->prev = old->prev;
355 60000589 : node->next = old->next;
356 60000589 : if (old->prev)
357 : {
358 48183150 : old->prev->next = node;
359 48183150 : old->next->prev = node;
360 : /* Remove the old node from the list. */
361 48183150 : old->prev = NULL;
362 : }
363 60000589 : }
364 :
365 : /* Relink ssa_imm_use node LINKNODE into the chain for OLD, with use occurring
366 : in STMT. */
367 : inline void
368 4819155 : relink_imm_use_stmt (ssa_use_operand_t *linknode, ssa_use_operand_t *old,
369 : gimple *stmt)
370 : {
371 4819155 : if (stmt)
372 4819155 : relink_imm_use (linknode, old);
373 : else
374 : link_imm_use (linknode, NULL);
375 4819155 : linknode->loc.stmt = stmt;
376 : }
377 :
378 :
379 : /* Return true is IMM has reached the end of the immediate use list. */
380 : inline bool
381 4369728690 : end_readonly_imm_use_p (const imm_use_iterator *imm)
382 : {
383 2714518679 : return (imm->imm_use == imm->end_p);
384 : }
385 :
386 : /* Initialize iterator IMM to process the list for VAR. */
387 : inline use_operand_p
388 1059308668 : first_readonly_imm_use (imm_use_iterator *imm, tree var)
389 : {
390 : #if defined ENABLE_GIMPLE_CHECKING
391 1059308668 : var->ssa_name.fast_iteration_depth++;
392 : #endif
393 1059308668 : imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
394 1059308668 : imm->imm_use = imm->end_p->next;
395 1059308668 : imm->next_stmt_use = imm->imm_use->next;
396 1059308668 : imm->name = var;
397 1059308668 : if (end_readonly_imm_use_p (imm))
398 99210730 : return NULL_USE_OPERAND_P;
399 : return imm->imm_use;
400 : }
401 :
402 : /* Bump IMM to the next use in the list. */
403 : inline use_operand_p
404 1655210011 : next_readonly_imm_use (imm_use_iterator *imm)
405 : {
406 1655210011 : use_operand_p old = imm->imm_use;
407 :
408 : /* If this assertion fails, it indicates the 'next' pointer has changed
409 : since the last bump. This indicates that the list is being modified
410 : via stmt changes, or SET_USE, or somesuch thing, and you need to be
411 : using the SAFE version of the iterator. */
412 1655210011 : if (flag_checking)
413 : {
414 1655205183 : gcc_assert (imm->next_stmt_use == old->next);
415 1655205183 : imm->next_stmt_use = old->next->next;
416 : }
417 :
418 1655210011 : imm->imm_use = old->next;
419 1655210011 : if (end_readonly_imm_use_p (imm))
420 868533142 : return NULL_USE_OPERAND_P;
421 : return imm->imm_use;
422 : }
423 :
424 :
425 : /* Return true if VAR has no nondebug uses. */
426 : inline bool
427 835630243 : has_zero_uses (const_tree var)
428 : {
429 835630243 : const ssa_use_operand_t *const head = &(SSA_NAME_IMM_USE_NODE (var));
430 835630243 : const ssa_use_operand_t *ptr;
431 :
432 875024371 : for (ptr = head->next; ptr != head; ptr = ptr->next)
433 826109709 : if (USE_STMT (ptr) && !is_gimple_debug (USE_STMT (ptr)))
434 : return false;
435 :
436 : return true;
437 : }
438 :
439 : /* Return true if VAR has a single nondebug use. */
440 : inline bool
441 1040955427 : has_single_use (const_tree var)
442 : {
443 1040955427 : const ssa_use_operand_t *const head = &(SSA_NAME_IMM_USE_NODE (var));
444 1040955427 : const ssa_use_operand_t *ptr;
445 1040955427 : bool single = false;
446 :
447 2290148802 : for (ptr = head->next; ptr != head; ptr = ptr->next)
448 1779741444 : if (USE_STMT(ptr) && !is_gimple_debug (USE_STMT (ptr)))
449 : {
450 1570877879 : if (single)
451 : return false;
452 : else
453 : single = true;
454 : }
455 :
456 : return single;
457 : }
458 :
459 : /* If VAR has only a single immediate nondebug use, return true, and
460 : set USE_P and STMT to the use pointer and stmt of occurrence. */
461 : inline bool
462 345335362 : single_imm_use (const_tree var, use_operand_p *use_p, gimple **stmt)
463 : {
464 345335362 : const ssa_use_operand_t *const ptr = &(SSA_NAME_IMM_USE_NODE (var));
465 :
466 : /* If there aren't any uses whatsoever, we're done. */
467 345335362 : if (ptr == ptr->next)
468 : {
469 155222 : return_false:
470 160179 : *use_p = NULL_USE_OPERAND_P;
471 160179 : *stmt = NULL;
472 160179 : return false;
473 : }
474 :
475 : /* If there's a single use, check that it's not a debug stmt. */
476 345180140 : if (ptr == ptr->next->next)
477 : {
478 294303449 : if (USE_STMT (ptr->next) && !is_gimple_debug (USE_STMT (ptr->next)))
479 : {
480 294298492 : *use_p = ptr->next;
481 294298492 : *stmt = ptr->next->loc.stmt;
482 294298492 : return true;
483 : }
484 : else
485 4957 : goto return_false;
486 : }
487 :
488 50876691 : return single_imm_use_1 (ptr, use_p, stmt);
489 : }
490 :
491 : /* Return the number of nondebug immediate uses of VAR. */
492 : inline unsigned int
493 4274 : num_imm_uses (const_tree var)
494 : {
495 4274 : const ssa_use_operand_t *const start = &(SSA_NAME_IMM_USE_NODE (var));
496 4274 : const ssa_use_operand_t *ptr;
497 4274 : unsigned int num = 0;
498 :
499 4274 : if (!MAY_HAVE_DEBUG_BIND_STMTS)
500 : {
501 11385 : for (ptr = start->next; ptr != start; ptr = ptr->next)
502 7783 : if (USE_STMT (ptr))
503 7783 : num++;
504 : }
505 : else
506 2076 : for (ptr = start->next; ptr != start; ptr = ptr->next)
507 1404 : if (USE_STMT (ptr) && !is_gimple_debug (USE_STMT (ptr)))
508 1404 : num++;
509 :
510 4274 : return num;
511 : }
512 :
513 : /* ----------------------------------------------------------------------- */
514 :
515 : /* The following set of routines are used to iterator over various type of
516 : SSA operands. */
517 :
518 : /* Return true if PTR is finished iterating. */
519 : inline bool
520 68564905372 : op_iter_done (const ssa_op_iter *ptr)
521 : {
522 67700542819 : return ptr->done;
523 : }
524 :
525 : /* Get the next iterator use value for PTR. */
526 : inline use_operand_p
527 45361153273 : op_iter_next_use (ssa_op_iter *ptr)
528 : {
529 45361153273 : use_operand_p use_p;
530 45361153273 : gcc_checking_assert (ptr->iter_type == ssa_op_iter_use);
531 45361153273 : if (ptr->uses)
532 : {
533 18297049258 : use_p = USE_OP_PTR (ptr->uses);
534 18297049258 : ptr->uses = ptr->uses->next;
535 18297049258 : return use_p;
536 : }
537 27064104015 : if (ptr->i < ptr->numops)
538 : {
539 218051778 : return PHI_ARG_DEF_PTR (ptr->stmt, (ptr->i)++);
540 : }
541 26846052237 : ptr->done = true;
542 26846052237 : return NULL_USE_OPERAND_P;
543 : }
544 :
545 : /* Get the next iterator def value for PTR. */
546 : inline def_operand_p
547 1772041200 : op_iter_next_def (ssa_op_iter *ptr)
548 : {
549 1772041200 : gcc_checking_assert (ptr->iter_type == ssa_op_iter_def);
550 1772041200 : if (ptr->flags & SSA_OP_VDEF)
551 : {
552 166726347 : tree *p;
553 166726347 : ptr->flags &= ~SSA_OP_VDEF;
554 166726347 : p = gimple_vdef_ptr (ptr->stmt);
555 166726347 : if (p && *p)
556 : return p;
557 : }
558 1664286641 : if (ptr->flags & SSA_OP_DEF)
559 : {
560 941366947 : while (ptr->i < ptr->numops)
561 : {
562 471259216 : tree *val = gimple_op_ptr (ptr->stmt, ptr->i);
563 471259216 : ptr->i++;
564 471259216 : if (*val)
565 : {
566 423970528 : if (TREE_CODE (*val) == TREE_LIST)
567 911959 : val = &TREE_VALUE (*val);
568 423970528 : if (TREE_CODE (*val) == SSA_NAME
569 423970528 : || is_gimple_reg (*val))
570 : return val;
571 : }
572 : }
573 470107731 : ptr->flags &= ~SSA_OP_DEF;
574 : }
575 :
576 1372626457 : ptr->done = true;
577 1372626457 : return NULL_DEF_OPERAND_P;
578 : }
579 :
580 : /* Get the next iterator tree value for PTR. */
581 : inline tree
582 21416566714 : op_iter_next_tree (ssa_op_iter *ptr)
583 : {
584 21416566714 : tree val;
585 21416566714 : gcc_checking_assert (ptr->iter_type == ssa_op_iter_tree);
586 21416566714 : if (ptr->uses)
587 : {
588 1195313683 : val = USE_OP (ptr->uses);
589 1195313683 : ptr->uses = ptr->uses->next;
590 1195313683 : return val;
591 : }
592 20221253031 : if (ptr->flags & SSA_OP_VDEF)
593 : {
594 5596068882 : ptr->flags &= ~SSA_OP_VDEF;
595 11192137764 : if ((val = gimple_vdef (ptr->stmt)))
596 : return val;
597 : }
598 17792171985 : if (ptr->flags & SSA_OP_DEF)
599 : {
600 12262409155 : while (ptr->i < ptr->numops)
601 : {
602 6131148554 : val = gimple_op (ptr->stmt, ptr->i);
603 6131148554 : ptr->i++;
604 6131148554 : if (val)
605 : {
606 5444740073 : if (TREE_CODE (val) == TREE_LIST)
607 13098313 : val = TREE_VALUE (val);
608 5444740073 : if (TREE_CODE (val) == SSA_NAME
609 5444740073 : || is_gimple_reg (val))
610 : return val;
611 : }
612 : }
613 6131260601 : ptr->flags &= ~SSA_OP_DEF;
614 : }
615 :
616 14096988594 : ptr->done = true;
617 14096988594 : return NULL_TREE;
618 : }
619 :
620 :
621 : /* This functions clears the iterator PTR, and marks it done. This is normally
622 : used to prevent warnings in the compile about might be uninitialized
623 : components. */
624 :
625 : inline void
626 61423603 : clear_and_done_ssa_iter (ssa_op_iter *ptr)
627 : {
628 61423603 : ptr->i = 0;
629 61423603 : ptr->numops = 0;
630 61423603 : ptr->uses = NULL;
631 61423603 : ptr->iter_type = ssa_op_iter_none;
632 61423603 : ptr->stmt = NULL;
633 61423603 : ptr->done = true;
634 61423603 : ptr->flags = 0;
635 : }
636 :
637 : /* Initialize the iterator PTR to the virtual defs in STMT. */
638 : inline void
639 42446040523 : op_iter_init (ssa_op_iter *ptr, gimple *stmt, int flags)
640 : {
641 : /* PHI nodes require a different iterator initialization path. We
642 : do not support iterating over virtual defs or uses without
643 : iterating over defs or uses at the same time. */
644 42446040523 : gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI
645 : && (!(flags & SSA_OP_VDEF) || (flags & SSA_OP_DEF))
646 : && (!(flags & SSA_OP_VUSE) || (flags & SSA_OP_USE)));
647 42446040523 : ptr->numops = 0;
648 42446040523 : if (flags & (SSA_OP_DEF | SSA_OP_VDEF))
649 : {
650 14402803632 : switch (gimple_code (stmt))
651 : {
652 6588657371 : case GIMPLE_ASSIGN:
653 6588657371 : case GIMPLE_CALL:
654 6588657371 : ptr->numops = 1;
655 6588657371 : break;
656 14392741 : case GIMPLE_ASM:
657 14392741 : ptr->numops = gimple_asm_noutputs (as_a <gasm *> (stmt));
658 14392741 : break;
659 32759 : case GIMPLE_TRANSACTION:
660 32759 : ptr->numops = 0;
661 32759 : flags &= ~SSA_OP_DEF;
662 32759 : break;
663 7799720761 : default:
664 7799720761 : ptr->numops = 0;
665 7799720761 : flags &= ~(SSA_OP_DEF | SSA_OP_VDEF);
666 7799720761 : break;
667 : }
668 : }
669 42446040523 : ptr->uses = (flags & (SSA_OP_USE|SSA_OP_VUSE)) ? gimple_use_ops (stmt) : NULL;
670 42446040523 : if (!(flags & SSA_OP_VUSE)
671 30201723843 : && ptr->uses
672 49120097837 : && gimple_vuse (stmt) != NULL_TREE)
673 4246628451 : ptr->uses = ptr->uses->next;
674 42446040523 : ptr->done = false;
675 42446040523 : ptr->i = 0;
676 :
677 42446040523 : ptr->stmt = stmt;
678 42446040523 : ptr->flags = flags;
679 42446040523 : }
680 :
681 : /* Initialize iterator PTR to the use operands in STMT based on FLAGS. Return
682 : the first use. */
683 : inline use_operand_p
684 26934873201 : op_iter_init_use (ssa_op_iter *ptr, gimple *stmt, int flags)
685 : {
686 26934873201 : gcc_checking_assert ((flags & SSA_OP_ALL_DEFS) == 0
687 : && (flags & SSA_OP_USE));
688 26934873201 : op_iter_init (ptr, stmt, flags);
689 26934873201 : ptr->iter_type = ssa_op_iter_use;
690 26934873201 : return op_iter_next_use (ptr);
691 : }
692 :
693 : /* Initialize iterator PTR to the def operands in STMT based on FLAGS. Return
694 : the first def. */
695 : inline def_operand_p
696 1366206391 : op_iter_init_def (ssa_op_iter *ptr, gimple *stmt, int flags)
697 : {
698 1366206391 : gcc_checking_assert ((flags & SSA_OP_ALL_USES) == 0
699 : && (flags & SSA_OP_DEF));
700 1366206391 : op_iter_init (ptr, stmt, flags);
701 1366206391 : ptr->iter_type = ssa_op_iter_def;
702 1366206391 : return op_iter_next_def (ptr);
703 : }
704 :
705 : /* Initialize iterator PTR to the operands in STMT based on FLAGS. Return
706 : the first operand as a tree. */
707 : inline tree
708 14144960931 : op_iter_init_tree (ssa_op_iter *ptr, gimple *stmt, int flags)
709 : {
710 14144960931 : op_iter_init (ptr, stmt, flags);
711 14144960931 : ptr->iter_type = ssa_op_iter_tree;
712 14144960931 : return op_iter_next_tree (ptr);
713 : }
714 :
715 :
716 : /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
717 : return NULL. */
718 : inline tree
719 86723976 : single_ssa_tree_operand (gimple *stmt, int flags)
720 : {
721 86723976 : tree var;
722 86723976 : ssa_op_iter iter;
723 :
724 86723976 : var = op_iter_init_tree (&iter, stmt, flags);
725 86723976 : if (op_iter_done (&iter))
726 : return NULL_TREE;
727 79577961 : op_iter_next_tree (&iter);
728 79577961 : if (op_iter_done (&iter))
729 73090951 : return var;
730 : return NULL_TREE;
731 : }
732 :
733 :
734 : /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
735 : return NULL. */
736 : inline use_operand_p
737 90969 : single_ssa_use_operand (gimple *stmt, int flags)
738 : {
739 90969 : use_operand_p var;
740 90969 : ssa_op_iter iter;
741 :
742 90969 : var = op_iter_init_use (&iter, stmt, flags);
743 90969 : if (op_iter_done (&iter))
744 : return NULL_USE_OPERAND_P;
745 90463 : op_iter_next_use (&iter);
746 90463 : if (op_iter_done (&iter))
747 39792 : return var;
748 : return NULL_USE_OPERAND_P;
749 : }
750 :
751 : /* Return the single virtual use operand in STMT if present. Otherwise
752 : return NULL. */
753 : inline use_operand_p
754 207226 : ssa_vuse_operand (gimple *stmt)
755 : {
756 360328 : if (! gimple_vuse (stmt))
757 : return NULL_USE_OPERAND_P;
758 14470 : return USE_OP_PTR (gimple_use_ops (stmt));
759 : }
760 :
761 :
762 : /* If there is a single operand in STMT matching FLAGS, return it. Otherwise
763 : return NULL. */
764 : inline def_operand_p
765 395762766 : single_ssa_def_operand (gimple *stmt, int flags)
766 : {
767 395762766 : def_operand_p var;
768 395762766 : ssa_op_iter iter;
769 :
770 395762766 : var = op_iter_init_def (&iter, stmt, flags);
771 395762766 : if (op_iter_done (&iter))
772 : return NULL_DEF_OPERAND_P;
773 132997091 : op_iter_next_def (&iter);
774 132997091 : if (op_iter_done (&iter))
775 132916508 : return var;
776 : return NULL_DEF_OPERAND_P;
777 : }
778 :
779 :
780 : /* Return true if there are zero operands in STMT matching the type
781 : given in FLAGS. */
782 : inline bool
783 11755068 : zero_ssa_operands (gimple *stmt, int flags)
784 : {
785 11755068 : ssa_op_iter iter;
786 :
787 11755068 : op_iter_init_tree (&iter, stmt, flags);
788 11755068 : return op_iter_done (&iter);
789 : }
790 :
791 :
792 : /* Return the number of operands matching FLAGS in STMT. */
793 : inline int
794 48799338 : num_ssa_operands (gimple *stmt, int flags)
795 : {
796 48799338 : ssa_op_iter iter;
797 48799338 : tree t;
798 48799338 : int num = 0;
799 :
800 48799338 : gcc_checking_assert (gimple_code (stmt) != GIMPLE_PHI);
801 119142625 : FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, flags)
802 70343287 : num++;
803 48799338 : return num;
804 : }
805 :
806 : /* If there is a single DEF in the PHI node which matches FLAG, return it.
807 : Otherwise return NULL_DEF_OPERAND_P. */
808 : inline tree
809 : single_phi_def (gphi *stmt, int flags)
810 : {
811 : tree def = gimple_phi_result (stmt);
812 : if ((flags & SSA_OP_DEF) && is_gimple_reg (def))
813 : return def;
814 : if ((flags & SSA_OP_VIRTUAL_DEFS) && !is_gimple_reg (def))
815 : return def;
816 : return NULL_TREE;
817 : }
818 :
819 : /* Initialize the iterator PTR for uses matching FLAGS in PHI. FLAGS should
820 : be either SSA_OP_USES or SSA_OP_VIRTUAL_USES. */
821 : inline use_operand_p
822 48401590 : op_iter_init_phiuse (ssa_op_iter *ptr, gphi *phi, int flags)
823 : {
824 48401590 : tree phi_def = gimple_phi_result (phi);
825 48401590 : int comp;
826 :
827 48401590 : clear_and_done_ssa_iter (ptr);
828 48401590 : ptr->done = false;
829 :
830 48401590 : gcc_checking_assert ((flags & (SSA_OP_USE | SSA_OP_VIRTUAL_USES)) != 0);
831 :
832 48401590 : comp = (is_gimple_reg (phi_def) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
833 :
834 : /* If the PHI node doesn't the operand type we care about, we're done. */
835 48401590 : if ((flags & comp) == 0)
836 : {
837 3871298 : ptr->done = true;
838 3871298 : return NULL_USE_OPERAND_P;
839 : }
840 :
841 44530292 : ptr->stmt = phi;
842 44530292 : ptr->numops = gimple_phi_num_args (phi);
843 44530292 : ptr->iter_type = ssa_op_iter_use;
844 44530292 : ptr->flags = flags;
845 44530292 : return op_iter_next_use (ptr);
846 : }
847 :
848 :
849 : /* Start an iterator for a PHI definition. */
850 :
851 : inline def_operand_p
852 13022013 : op_iter_init_phidef (ssa_op_iter *ptr, gphi *phi, int flags)
853 : {
854 13022013 : tree phi_def = gimple_phi_result (phi);
855 13022013 : int comp;
856 :
857 13022013 : clear_and_done_ssa_iter (ptr);
858 13022013 : ptr->done = false;
859 :
860 13022013 : gcc_checking_assert ((flags & (SSA_OP_DEF | SSA_OP_VIRTUAL_DEFS)) != 0);
861 :
862 13022013 : comp = (is_gimple_reg (phi_def) ? SSA_OP_DEF : SSA_OP_VIRTUAL_DEFS);
863 :
864 : /* If the PHI node doesn't have the operand type we care about,
865 : we're done. */
866 13022013 : if ((flags & comp) == 0)
867 : {
868 5588005 : ptr->done = true;
869 5588005 : return NULL_DEF_OPERAND_P;
870 : }
871 :
872 7434008 : ptr->iter_type = ssa_op_iter_def;
873 : /* The first call to op_iter_next_def will terminate the iterator since
874 : all the fields are NULL. Simply return the result here as the first and
875 : therefore only result. */
876 7434008 : return gimple_phi_result_ptr (phi);
877 : }
878 :
879 : /* Return true is IMM has reached the end of the immediate use stmt list. */
880 :
881 : inline bool
882 428760433 : end_imm_use_stmt_p (const imm_use_iterator *imm)
883 : {
884 270923083 : return (imm->imm_use == imm->end_p);
885 : }
886 :
887 : /* Finished the traverse of an immediate use stmt list IMM by removing the
888 : placeholder node from the list. */
889 :
890 : inline void
891 113085733 : end_imm_use_stmt_traverse (imm_use_iterator * ARG_UNUSED (imm))
892 : {
893 : #if defined ENABLE_GIMPLE_CHECKING
894 102621340 : imm->name->ssa_name.active_iterated_stmt = NULL;
895 : #endif
896 : }
897 :
898 : /* Immediate use traversal of uses within a stmt require that all the
899 : uses on a stmt be sequentially listed. This routine is used to build up
900 : this sequential list by adding USE_P to the end of the current list
901 : currently delimited by HEAD and LAST_P. The new LAST_P value is
902 : returned. */
903 :
904 : inline use_operand_p
905 174461916 : move_use_after_head (use_operand_p use_p, use_operand_p head,
906 : use_operand_p last_p)
907 : {
908 174461916 : gcc_checking_assert (USE_FROM_PTR (use_p) == USE_FROM_PTR (head));
909 : /* Skip head when we find it. */
910 174461916 : if (use_p != head)
911 : {
912 : /* If use_p is already linked in after last_p, continue. */
913 2423239 : if (last_p->next == use_p)
914 : last_p = use_p;
915 : else
916 : {
917 : /* Delink from current location, and link in at last_p. */
918 1421317 : delink_imm_use (use_p);
919 1421317 : link_imm_use_to_list (use_p, last_p);
920 1421317 : last_p = use_p;
921 : }
922 : }
923 174461916 : return last_p;
924 : }
925 :
926 :
927 : /* This routine will relink all uses with the same stmt as HEAD into the list
928 : immediately following HEAD for iterator IMM and returns the last use on
929 : that stmt. */
930 :
931 : inline use_operand_p
932 172038677 : link_use_stmts_after (use_operand_p head, imm_use_iterator *)
933 : {
934 172038677 : use_operand_p use_p;
935 172038677 : use_operand_p last_p = head;
936 172038677 : gimple *head_stmt = USE_STMT (head);
937 172038677 : tree use = USE_FROM_PTR (head);
938 172038677 : ssa_op_iter op_iter;
939 172038677 : int flag;
940 :
941 : /* Only look at virtual or real uses, depending on the type of HEAD. */
942 172038677 : flag = (is_gimple_reg (use) ? SSA_OP_USE : SSA_OP_VIRTUAL_USES);
943 :
944 172038677 : if (gphi *phi = dyn_cast <gphi *> (head_stmt))
945 : {
946 179564786 : FOR_EACH_PHI_ARG (use_p, phi, op_iter, flag)
947 161535015 : if (USE_FROM_PTR (use_p) == use)
948 20185323 : last_p = move_use_after_head (use_p, head, last_p);
949 : }
950 : else
951 : {
952 154008906 : if (flag == SSA_OP_USE)
953 : {
954 200503219 : FOR_EACH_SSA_USE_OPERAND (use_p, head_stmt, op_iter, flag)
955 120489648 : if (USE_FROM_PTR (use_p) == use)
956 80281258 : last_p = move_use_after_head (use_p, head, last_p);
957 : }
958 73995335 : else if ((use_p = gimple_vuse_op (head_stmt)) != NULL_USE_OPERAND_P)
959 : {
960 73995335 : if (USE_FROM_PTR (use_p) == use)
961 73995335 : last_p = move_use_after_head (use_p, head, last_p);
962 : }
963 : }
964 172038677 : return last_p;
965 : }
966 :
967 : /* Initialize IMM to traverse over uses of VAR. Return the first statement. */
968 : inline gimple *
969 113085733 : first_imm_use_stmt (imm_use_iterator *imm, tree var)
970 : {
971 : #if defined ENABLE_GIMPLE_CHECKING
972 113085733 : gcc_assert (var->ssa_name.active_iterated_stmt == NULL
973 : && var->ssa_name.fast_iteration_depth == 0);
974 : #endif
975 113085733 : imm->end_p = &(SSA_NAME_IMM_USE_NODE (var));
976 113085733 : imm->imm_use = imm->end_p->next;
977 113085733 : imm->next_imm_name = NULL_USE_OPERAND_P;
978 :
979 : /* next_stmt_use is used to point to the immediate use node after
980 : the set of uses for the current stmt. */
981 113085733 : imm->next_stmt_use = NULL_USE_OPERAND_P;
982 113085733 : imm->name = var;
983 :
984 113085733 : if (end_imm_use_stmt_p (imm))
985 : return NULL;
986 :
987 109695756 : imm->next_stmt_use = link_use_stmts_after (imm->imm_use, imm)->next;
988 :
989 : #if defined ENABLE_GIMPLE_CHECKING
990 109695756 : var->ssa_name.active_iterated_stmt = USE_STMT (imm->imm_use);
991 : #endif
992 109695756 : return USE_STMT (imm->imm_use);
993 : }
994 :
995 : /* Bump IMM to the next stmt which has a use of var. */
996 :
997 : inline gimple *
998 157837350 : next_imm_use_stmt (imm_use_iterator *imm)
999 : {
1000 157837350 : imm->imm_use = imm->next_stmt_use;
1001 157837350 : if (end_imm_use_stmt_p (imm))
1002 : return NULL;
1003 :
1004 : #if defined ENABLE_GIMPLE_CHECKING
1005 62342921 : imm->name->ssa_name.active_iterated_stmt = USE_STMT (imm->imm_use);
1006 : #endif
1007 62342921 : imm->next_stmt_use = link_use_stmts_after (imm->imm_use, imm)->next;
1008 62342921 : return USE_STMT (imm->imm_use);
1009 : }
1010 :
1011 : /* This routine will return the first use on the stmt IMM currently refers
1012 : to. */
1013 :
1014 : inline use_operand_p
1015 48111856 : first_imm_use_on_stmt (imm_use_iterator *imm)
1016 : {
1017 48111856 : imm->next_imm_name = imm->imm_use->next;
1018 48108975 : return imm->imm_use;
1019 : }
1020 :
1021 : /* Return TRUE if the last use on the stmt IMM refers to has been visited. */
1022 :
1023 : inline bool
1024 97634804 : end_imm_use_on_stmt_p (const imm_use_iterator *imm)
1025 : {
1026 97634804 : return (imm->imm_use == imm->next_stmt_use);
1027 : }
1028 :
1029 : /* Bump to the next use on the stmt IMM refers to, return NULL if done. */
1030 :
1031 : inline use_operand_p
1032 49522948 : next_imm_use_on_stmt (imm_use_iterator *imm)
1033 : {
1034 49522948 : imm->imm_use = imm->next_imm_name;
1035 49522948 : if (end_imm_use_on_stmt_p (imm))
1036 : return NULL_USE_OPERAND_P;
1037 : else
1038 : {
1039 1413973 : imm->next_imm_name = imm->imm_use->next;
1040 1413973 : return imm->imm_use;
1041 : }
1042 : }
1043 :
1044 : /* Delink all immediate_use information for STMT. */
1045 : inline void
1046 181651680 : delink_stmt_imm_use (gimple *stmt)
1047 : {
1048 181651680 : ssa_op_iter iter;
1049 181651680 : use_operand_p use_p;
1050 :
1051 181651680 : if (ssa_operands_active (cfun))
1052 389099342 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_ALL_USES)
1053 82884558 : delink_imm_use (use_p);
1054 181651680 : }
1055 :
1056 : #endif /* GCC_TREE_SSA_ITERATORS_H */
|