2005-04-08 Kelley Cook <kcook@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-operands.c
blob5d5f6bac4e25c2432caed9ebbc372c6f239ad5ee
1 /* SSA operands management for trees.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "flags.h"
27 #include "function.h"
28 #include "diagnostic.h"
29 #include "errors.h"
30 #include "tree-flow.h"
31 #include "tree-inline.h"
32 #include "tree-pass.h"
33 #include "ggc.h"
34 #include "timevar.h"
36 #include "langhooks.h"
38 /* This file contains the code required to manage the operands cache of the
39 SSA optimizer. For every stmt, we maintain an operand cache in the stmt
40 annotation. This cache contains operands that will be of interest to
41 optimizers and other passes wishing to manipulate the IL.
43 The operand type are broken up into REAL and VIRTUAL operands. The real
44 operands are represented as pointers into the stmt's operand tree. Thus
45 any manipulation of the real operands will be reflected in the actual tree.
46 Virtual operands are represented solely in the cache, although the base
47 variable for the SSA_NAME may, or may not occur in the stmt's tree.
48 Manipulation of the virtual operands will not be reflected in the stmt tree.
50 The routines in this file are concerned with creating this operand cache
51 from a stmt tree.
53 get_stmt_operands() in the primary entry point.
55 The operand tree is the parsed by the various get_* routines which look
56 through the stmt tree for the occurrence of operands which may be of
57 interest, and calls are made to the append_* routines whenever one is
58 found. There are 5 of these routines, each representing one of the
59 5 types of operands. Defs, Uses, Virtual Uses, Virtual May Defs, and
60 Virtual Must Defs.
62 The append_* routines check for duplication, and simply keep a list of
63 unique objects for each operand type in the build_* extendable vectors.
65 Once the stmt tree is completely parsed, the finalize_ssa_operands()
66 routine is called, which proceeds to perform the finalization routine
67 on each of the 5 operand vectors which have been built up.
69 If the stmt had a previous operand cache, the finalization routines
70 attempt to match up the new operands with the old ones. If its a perfect
71 match, the old vector is simply reused. If it isn't a perfect match, then
72 a new vector is created and the new operands are placed there. For
73 virtual operands, if the previous cache had SSA_NAME version of a
74 variable, and that same variable occurs in the same operands cache, then
75 the new cache vector will also get the same SSA_NAME.
77 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand
78 vector for VUSE, then the new vector will also be modified such that
79 it contains 'a_5' rather than 'a'.
84 /* Flags to describe operand properties in get_stmt_operands and helpers. */
86 /* By default, operands are loaded. */
87 #define opf_none 0
89 /* Operand is the target of an assignment expression or a
90 call-clobbered variable */
91 #define opf_is_def (1 << 0)
93 /* Operand is the target of an assignment expression. */
94 #define opf_kill_def (1 << 1)
96 /* No virtual operands should be created in the expression. This is used
97 when traversing ADDR_EXPR nodes which have different semantics than
98 other expressions. Inside an ADDR_EXPR node, the only operands that we
99 need to consider are indices into arrays. For instance, &a.b[i] should
100 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
101 VUSE for 'b'. */
102 #define opf_no_vops (1 << 2)
104 /* Array for building all the def operands. */
105 static GTY (()) varray_type build_defs;
107 /* Array for building all the use operands. */
108 static GTY (()) varray_type build_uses;
110 /* Array for building all the v_may_def operands. */
111 static GTY (()) varray_type build_v_may_defs;
113 /* Array for building all the vuse operands. */
114 static GTY (()) varray_type build_vuses;
116 /* Array for building all the v_must_def operands. */
117 static GTY (()) varray_type build_v_must_defs;
119 /* True if the operands for call clobbered vars are cached and valid. */
120 bool ssa_call_clobbered_cache_valid;
121 bool ssa_ro_call_cache_valid;
123 /* These arrays are the cached operand vectors for call clobbered calls. */
124 static GTY (()) varray_type clobbered_v_may_defs;
125 static GTY (()) varray_type clobbered_vuses;
126 static GTY (()) varray_type ro_call_vuses;
127 static bool clobbered_aliased_loads;
128 static bool clobbered_aliased_stores;
129 static bool ro_call_aliased_loads;
130 static stmt_operands_p parse_old_ops = NULL;
132 def_operand_p NULL_DEF_OPERAND_P = { NULL };
134 static void note_addressable (tree, stmt_ann_t);
135 static void get_expr_operands (tree, tree *, int);
136 static void get_asm_expr_operands (tree);
137 static void get_indirect_ref_operands (tree, tree, int);
138 static void get_call_expr_operands (tree, tree);
139 static inline void append_def (tree *);
140 static inline void append_use (tree *);
141 static void append_v_may_def (tree);
142 static void append_v_must_def (tree);
143 static void add_call_clobber_ops (tree);
144 static void add_call_read_ops (tree);
145 static void add_stmt_operand (tree *, stmt_ann_t, int);
147 /* Return a vector of contiguous memory for NUM def operands. */
149 static inline def_optype
150 allocate_def_optype (unsigned num)
152 def_optype def_ops;
153 unsigned size;
154 size = sizeof (struct def_optype_d) + sizeof (tree *) * (num - 1);
155 def_ops = ggc_alloc (size);
156 def_ops->num_defs = num;
157 return def_ops;
161 /* Return a vector of contiguous memory for NUM use operands. */
163 static inline use_optype
164 allocate_use_optype (unsigned num)
166 use_optype use_ops;
167 unsigned size;
168 size = sizeof (struct use_optype_d) + sizeof (use_operand_type_t) * (num - 1);
169 use_ops = ggc_alloc (size);
170 use_ops->num_uses = num;
171 return use_ops;
175 /* Return a vector of contiguous memory for NUM v_may_def operands. */
177 static inline v_may_def_optype
178 allocate_v_may_def_optype (unsigned num)
180 v_may_def_optype v_may_def_ops;
181 unsigned size;
182 size = sizeof (struct v_may_def_optype_d)
183 + sizeof (v_def_use_operand_type_t) * (num - 1);
184 v_may_def_ops = ggc_alloc (size);
185 v_may_def_ops->num_v_may_defs = num;
186 return v_may_def_ops;
190 /* Return a vector of contiguous memory for NUM v_use operands. */
192 static inline vuse_optype
193 allocate_vuse_optype (unsigned num)
195 vuse_optype vuse_ops;
196 unsigned size;
197 size = sizeof (struct vuse_optype_d)
198 + sizeof (vuse_operand_type_t) * (num - 1);
199 vuse_ops = ggc_alloc (size);
200 vuse_ops->num_vuses = num;
201 return vuse_ops;
205 /* Return a vector of contiguous memory for NUM v_must_def operands. */
207 static inline v_must_def_optype
208 allocate_v_must_def_optype (unsigned num)
210 v_must_def_optype v_must_def_ops;
211 unsigned size;
212 size = sizeof (struct v_must_def_optype_d) + sizeof (v_def_use_operand_type_t) * (num - 1);
213 v_must_def_ops = ggc_alloc (size);
214 v_must_def_ops->num_v_must_defs = num;
215 return v_must_def_ops;
219 /* Free memory for USES. */
221 static inline void
222 free_uses (use_optype *uses)
224 if (*uses)
226 unsigned int x;
227 use_optype use = *uses;
228 for (x = 0; x < use->num_uses; x++)
229 delink_imm_use (&(use->uses[x]));
230 ggc_free (*uses);
231 *uses = NULL;
236 /* Free memory for DEFS. */
238 static inline void
239 free_defs (def_optype *defs)
241 if (*defs)
243 ggc_free (*defs);
244 *defs = NULL;
249 /* Free memory for VUSES. */
251 static inline void
252 free_vuses (vuse_optype *vuses)
254 if (*vuses)
256 unsigned int x;
257 vuse_optype vuse = *vuses;
258 for (x = 0; x < vuse->num_vuses; x++)
259 delink_imm_use (&(vuse->vuses[x].imm_use));
260 ggc_free (*vuses);
261 *vuses = NULL;
266 /* Free memory for V_MAY_DEFS. */
268 static inline void
269 free_v_may_defs (v_may_def_optype *v_may_defs)
271 if (*v_may_defs)
273 unsigned int x;
274 v_may_def_optype v_may_def = *v_may_defs;
275 for (x = 0; x < v_may_def->num_v_may_defs; x++)
276 delink_imm_use (&(v_may_def->v_may_defs[x].imm_use));
277 ggc_free (*v_may_defs);
278 *v_may_defs = NULL;
283 /* Free memory for V_MUST_DEFS. */
285 static inline void
286 free_v_must_defs (v_must_def_optype *v_must_defs)
288 if (*v_must_defs)
290 unsigned int x;
291 v_must_def_optype v_must_def = *v_must_defs;
292 for (x = 0; x < v_must_def->num_v_must_defs; x++)
293 delink_imm_use (&(v_must_def->v_must_defs[x].imm_use));
294 ggc_free (*v_must_defs);
295 *v_must_defs = NULL;
300 /* Initialize the operand cache routines. */
302 void
303 init_ssa_operands (void)
305 VARRAY_TREE_PTR_INIT (build_defs, 5, "build defs");
306 VARRAY_TREE_PTR_INIT (build_uses, 10, "build uses");
307 VARRAY_TREE_INIT (build_v_may_defs, 10, "build v_may_defs");
308 VARRAY_TREE_INIT (build_vuses, 10, "build vuses");
309 VARRAY_TREE_INIT (build_v_must_defs, 10, "build v_must_defs");
313 /* Dispose of anything required by the operand routines. */
315 void
316 fini_ssa_operands (void)
318 ggc_free (build_defs);
319 ggc_free (build_uses);
320 ggc_free (build_v_may_defs);
321 ggc_free (build_vuses);
322 ggc_free (build_v_must_defs);
323 build_defs = NULL;
324 build_uses = NULL;
325 build_v_may_defs = NULL;
326 build_vuses = NULL;
327 build_v_must_defs = NULL;
328 if (clobbered_v_may_defs)
330 ggc_free (clobbered_v_may_defs);
331 ggc_free (clobbered_vuses);
332 clobbered_v_may_defs = NULL;
333 clobbered_vuses = NULL;
335 if (ro_call_vuses)
337 ggc_free (ro_call_vuses);
338 ro_call_vuses = NULL;
342 /* Initialize V_USES index INDEX to VAL for STMT. If OLD is present, preserve
343 the position of the may-def in the immediate_use list. */
345 static inline void
346 initialize_vuse_operand (vuse_optype vuses, unsigned int index, tree val,
347 tree stmt, ssa_imm_use_t *old)
349 vuse_operand_type_t *ptr;
350 ptr = &(vuses->vuses[index]);
351 ptr->use = val;
352 ptr->imm_use.use = &(ptr->use);
353 if (old)
354 relink_imm_use_stmt (&(ptr->imm_use), old, stmt);
355 else
356 link_imm_use_stmt (&(ptr->imm_use), ptr->use, stmt);
360 /* Initialize V_MAY_DEF_OPS index X to be DEF = MAY_DEF <USE> for STMT. If
361 OLD is present, preserve the position of the may-def in the immediate_use
362 list. */
364 static inline void
365 initialize_v_may_def_operand (v_may_def_optype v_may_def_ops, unsigned int x,
366 tree def, tree use, tree stmt, ssa_imm_use_t *old)
368 v_def_use_operand_type_t *ptr;
369 ptr = &(v_may_def_ops->v_may_defs[x]);
370 ptr->def = def;
371 ptr->use = use;
372 ptr->imm_use.use = &(ptr->use);
373 if (old)
374 relink_imm_use_stmt (&(ptr->imm_use), old, stmt);
375 else
376 link_imm_use_stmt (&(ptr->imm_use), ptr->use, stmt);
380 /* Initialize V_MUST_DEF_OPS index X to be DEF = MUST_DEF <USE> for STMT. If
381 OLD is present, preserve the position of the may-def in the immediate_use
382 list. */
384 static inline void
385 initialize_v_must_def_operand (v_must_def_optype v_must_def_ops, unsigned int x,
386 tree def, tree use, tree stmt, ssa_imm_use_t *old)
388 v_def_use_operand_type_t *ptr;
389 ptr = &(v_must_def_ops->v_must_defs[x]);
390 ptr->def = def;
391 ptr->use = use;
392 ptr->imm_use.use = &(ptr->use);
393 if (old)
394 relink_imm_use_stmt (&(ptr->imm_use), old, stmt);
395 else
396 link_imm_use_stmt (&(ptr->imm_use), ptr->use, stmt);
399 /* All the finalize_ssa_* routines do the work required to turn the build_
400 VARRAY into an operand_vector of the appropriate type. The original vector,
401 if any, is passed in for comparison and virtual SSA_NAME reuse. If the
402 old vector is reused, the pointer passed in is set to NULL so that
403 the memory is not freed when the old operands are freed. */
405 /* Return a new def operand vector for STMT, comparing to OLD_OPS_P. */
407 static def_optype
408 finalize_ssa_defs (def_optype *old_ops_p, tree stmt)
410 unsigned num, x;
411 def_optype def_ops, old_ops;
412 bool build_diff;
414 num = VARRAY_ACTIVE_SIZE (build_defs);
415 if (num == 0)
416 return NULL;
418 /* There should only be a single real definition per assignment. */
419 gcc_assert ((stmt && TREE_CODE (stmt) != MODIFY_EXPR) || num <= 1);
421 old_ops = *old_ops_p;
423 /* Compare old vector and new array. */
424 build_diff = true;
425 if (stmt && old_ops && old_ops->num_defs == num)
427 build_diff = false;
428 for (x = 0; x < num; x++)
429 if (old_ops->defs[x].def != VARRAY_TREE_PTR (build_defs, x))
431 build_diff = true;
432 break;
436 if (!build_diff)
438 def_ops = old_ops;
439 *old_ops_p = NULL;
441 else
443 def_ops = allocate_def_optype (num);
444 for (x = 0; x < num ; x++)
445 def_ops->defs[x].def = VARRAY_TREE_PTR (build_defs, x);
448 VARRAY_POP_ALL (build_defs);
450 return def_ops;
454 /* Make sure PTR is inn the correct immediate use list. Since uses are simply
455 pointers into the stmt TREE, there is no way of telling if anyone has
456 changed what this pointer points to via TREE_OPERANDS (exp, 0) = <...>.
457 THe contents are different, but the the pointer is still the same. This
458 routine will check to make sure PTR is in the correct list, and if it isn't
459 put it in the correct list. We cannot simply check the previous node
460 because all nodes in the same stmt might have be changed. */
462 static inline void
463 correct_use_link (ssa_imm_use_t *ptr, tree stmt)
465 ssa_imm_use_t *prev;
466 tree root;
468 /* Fold_stmt () may have changed the stmt pointers. */
469 if (ptr->stmt != stmt)
470 ptr->stmt = stmt;
472 prev = ptr->prev;
473 if (prev)
475 bool stmt_mod = true;
476 /* Find the first element which isn't a SAFE iterator, is in a sifferent
477 stmt, and is not a a modified stmt, That node is in the correct list,
478 see if we are too. */
480 while (stmt_mod)
482 while (prev->stmt == stmt || prev->stmt == NULL)
483 prev = prev->prev;
484 if (prev->use == NULL)
485 stmt_mod = false;
486 else
487 if ((stmt_mod = stmt_modified_p (prev->stmt)))
488 prev = prev->prev;
491 /* Get the ssa_name of the list the node is in. */
492 if (prev->use == NULL)
493 root = prev->stmt;
494 else
495 root = *(prev->use);
496 /* If its the right list, simply return. */
497 if (root == *(ptr->use))
498 return;
500 /* Its in the wrong list if we reach here. */
501 delink_imm_use (ptr);
502 link_imm_use (ptr, *(ptr->use));
506 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P. */
508 static use_optype
509 finalize_ssa_uses (use_optype *old_ops_p, tree stmt)
511 unsigned num, x, num_old, i;
512 use_optype use_ops, old_ops;
513 bool build_diff;
515 num = VARRAY_ACTIVE_SIZE (build_uses);
516 if (num == 0)
517 return NULL;
519 #ifdef ENABLE_CHECKING
521 unsigned x;
522 /* If the pointer to the operand is the statement itself, something is
523 wrong. It means that we are pointing to a local variable (the
524 initial call to get_stmt_operands does not pass a pointer to a
525 statement). */
526 for (x = 0; x < num; x++)
527 gcc_assert (*(VARRAY_TREE_PTR (build_uses, x)) != stmt);
529 #endif
530 old_ops = *old_ops_p;
531 num_old = ((stmt && old_ops) ? old_ops->num_uses : 0);
533 /* Check if the old vector and the new array are the same. */
534 build_diff = true;
535 if (stmt && old_ops && num_old == num)
537 build_diff = false;
538 for (x = 0; x < num; x++)
540 tree *var_p = VARRAY_TREE_PTR (build_uses, x);
541 tree *node = old_ops->uses[x].use;
542 /* Check the pointer values to see if they are the same. */
543 if (node != var_p)
545 build_diff = true;
546 break;
551 if (!build_diff)
553 use_ops = old_ops;
554 *old_ops_p = NULL;
555 for (i = 0; i < num_old; i++)
556 correct_use_link (&(use_ops->uses[i]), stmt);
558 else
560 use_ops = allocate_use_optype (num);
561 for (x = 0; x < num ; x++)
563 tree *var = VARRAY_TREE_PTR (build_uses, x);
564 use_ops->uses[x].use = var;
565 for (i = 0; i < num_old; i++)
567 ssa_imm_use_t *ptr = &(old_ops->uses[i]);
568 if (ptr->use == var)
570 relink_imm_use_stmt (&(use_ops->uses[x]), ptr, stmt);
571 correct_use_link (&(use_ops->uses[x]), stmt);
572 break;
575 if (i == num_old)
576 link_imm_use_stmt (&(use_ops->uses[x]), *var, stmt);
579 VARRAY_POP_ALL (build_uses);
581 return use_ops;
585 /* Return a new v_may_def operand vector for STMT, comparing to OLD_OPS_P. */
587 static v_may_def_optype
588 finalize_ssa_v_may_defs (v_may_def_optype *old_ops_p, tree stmt)
590 unsigned num, x, i, old_num;
591 v_may_def_optype v_may_def_ops, old_ops;
592 tree result, var;
593 bool build_diff;
595 num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
596 if (num == 0)
597 return NULL;
599 old_ops = *old_ops_p;
601 /* Check if the old vector and the new array are the same. */
602 build_diff = true;
603 if (stmt && old_ops && old_ops->num_v_may_defs == num)
605 old_num = num;
606 build_diff = false;
607 for (x = 0; x < num; x++)
609 var = old_ops->v_may_defs[x].def;
610 if (TREE_CODE (var) == SSA_NAME)
611 var = SSA_NAME_VAR (var);
612 if (var != VARRAY_TREE (build_v_may_defs, x))
614 build_diff = true;
615 break;
619 else
620 old_num = (old_ops ? old_ops->num_v_may_defs : 0);
622 if (!build_diff)
624 v_may_def_ops = old_ops;
625 *old_ops_p = NULL;
626 for (x = 0; x < num; x++)
627 correct_use_link (&(v_may_def_ops->v_may_defs[x].imm_use), stmt);
629 else
631 v_may_def_ops = allocate_v_may_def_optype (num);
632 for (x = 0; x < num; x++)
634 var = VARRAY_TREE (build_v_may_defs, x);
635 /* Look for VAR in the old operands vector. */
636 for (i = 0; i < old_num; i++)
638 result = old_ops->v_may_defs[i].def;
639 if (TREE_CODE (result) == SSA_NAME)
640 result = SSA_NAME_VAR (result);
641 if (result == var)
643 initialize_v_may_def_operand (v_may_def_ops, x,
644 old_ops->v_may_defs[i].def,
645 old_ops->v_may_defs[i].use,
646 stmt,
647 &(old_ops->v_may_defs[i].imm_use));
648 break;
651 if (i == old_num)
653 initialize_v_may_def_operand (v_may_def_ops, x, var, var, stmt,
654 NULL);
659 /* Empty the V_MAY_DEF build vector after VUSES have been processed. */
661 return v_may_def_ops;
665 /* Clear the in_list bits and empty the build array for v_may_defs. */
667 static inline void
668 cleanup_v_may_defs (void)
670 unsigned x, num;
671 num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
673 for (x = 0; x < num; x++)
675 tree t = VARRAY_TREE (build_v_may_defs, x);
676 var_ann_t ann = var_ann (t);
677 ann->in_v_may_def_list = 0;
679 VARRAY_POP_ALL (build_v_may_defs);
682 /* Return a new vuse operand vector, comparing to OLD_OPS_P. */
684 static vuse_optype
685 finalize_ssa_vuses (vuse_optype *old_ops_p, tree stmt)
687 unsigned num, x, i, num_v_may_defs, old_num;
688 vuse_optype vuse_ops, old_ops;
689 bool build_diff;
691 num = VARRAY_ACTIVE_SIZE (build_vuses);
692 if (num == 0)
694 cleanup_v_may_defs ();
695 return NULL;
698 /* Remove superfluous VUSE operands. If the statement already has a
699 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
700 needed because V_MAY_DEFs imply a VUSE of the variable. For instance,
701 suppose that variable 'a' is aliased:
703 # VUSE <a_2>
704 # a_3 = V_MAY_DEF <a_2>
705 a = a + 1;
707 The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
708 operation. */
710 num_v_may_defs = VARRAY_ACTIVE_SIZE (build_v_may_defs);
712 if (num_v_may_defs > 0)
714 size_t i;
715 tree vuse;
716 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
718 vuse = VARRAY_TREE (build_vuses, i);
719 if (TREE_CODE (vuse) != SSA_NAME)
721 var_ann_t ann = var_ann (vuse);
722 ann->in_vuse_list = 0;
723 if (ann->in_v_may_def_list)
725 /* If we found a useless VUSE operand, remove it from the
726 operand array by replacing it with the last active element
727 in the operand array (unless the useless VUSE was the
728 last operand, in which case we simply remove it. */
729 if (i != VARRAY_ACTIVE_SIZE (build_vuses) - 1)
731 VARRAY_TREE (build_vuses, i)
732 = VARRAY_TREE (build_vuses,
733 VARRAY_ACTIVE_SIZE (build_vuses) - 1);
735 VARRAY_POP (build_vuses);
737 /* We want to rescan the element at this index, unless
738 this was the last element, in which case the loop
739 terminates. */
740 i--;
745 else
746 /* Clear out the in_list bits. */
747 for (x = 0; x < num; x++)
749 tree t = VARRAY_TREE (build_vuses, x);
750 if (TREE_CODE (t) != SSA_NAME)
752 var_ann_t ann = var_ann (t);
753 ann->in_vuse_list = 0;
758 num = VARRAY_ACTIVE_SIZE (build_vuses);
759 /* We could have reduced the size to zero now, however. */
760 if (num == 0)
762 cleanup_v_may_defs ();
763 return NULL;
766 old_ops = *old_ops_p;
768 /* Determine whether vuses is the same as the old vector. */
769 build_diff = true;
770 if (stmt && old_ops && old_ops->num_vuses == num)
772 old_num = num;
773 build_diff = false;
774 for (x = 0; x < num ; x++)
776 tree v;
777 v = old_ops->vuses[x].use;
778 if (TREE_CODE (v) == SSA_NAME)
779 v = SSA_NAME_VAR (v);
780 if (v != VARRAY_TREE (build_vuses, x))
782 build_diff = true;
783 break;
787 else
788 old_num = (old_ops ? old_ops->num_vuses : 0);
790 if (!build_diff)
792 vuse_ops = old_ops;
793 *old_ops_p = NULL;
794 for (x = 0; x < num; x++)
795 correct_use_link (&(vuse_ops->vuses[x].imm_use), stmt);
797 else
799 vuse_ops = allocate_vuse_optype (num);
800 for (x = 0; x < num; x++)
802 tree result, var = VARRAY_TREE (build_vuses, x);
803 /* Look for VAR in the old vector, and use that SSA_NAME. */
804 for (i = 0; i < old_num; i++)
806 result = old_ops->vuses[i].use;
807 if (TREE_CODE (result) == SSA_NAME)
808 result = SSA_NAME_VAR (result);
809 if (result == var)
811 initialize_vuse_operand (vuse_ops, x, old_ops->vuses[i].use,
812 stmt, &(old_ops->vuses[i].imm_use));
813 break;
816 if (i == old_num)
817 initialize_vuse_operand (vuse_ops, x, var, stmt, NULL);
821 /* The v_may_def build vector wasn't freed because we needed it here.
822 Free it now with the vuses build vector. */
823 VARRAY_POP_ALL (build_vuses);
824 cleanup_v_may_defs ();
826 return vuse_ops;
829 /* Return a new v_must_def operand vector for STMT, comparing to OLD_OPS_P. */
831 static v_must_def_optype
832 finalize_ssa_v_must_defs (v_must_def_optype *old_ops_p, tree stmt)
834 unsigned num, x, i, old_num = 0;
835 v_must_def_optype v_must_def_ops, old_ops;
836 tree result, var;
837 bool build_diff;
839 num = VARRAY_ACTIVE_SIZE (build_v_must_defs);
840 if (num == 0)
841 return NULL;
843 /* In the presence of subvars, there may be more than one V_MUST_DEF per
844 statement (one for each subvar). It is a bit expensive to verify that
845 all must-defs in a statement belong to subvars if there is more than one
846 MUST-def, so we don't do it. Suffice to say, if you reach here without
847 having subvars, and have num >1, you have hit a bug. */
850 old_ops = *old_ops_p;
852 /* Check if the old vector and the new array are the same. */
853 build_diff = true;
854 if (stmt && old_ops && old_ops->num_v_must_defs == num)
856 old_num = num;
857 build_diff = false;
858 for (x = 0; x < num; x++)
860 tree var = old_ops->v_must_defs[x].def;
861 if (TREE_CODE (var) == SSA_NAME)
862 var = SSA_NAME_VAR (var);
863 if (var != VARRAY_TREE (build_v_must_defs, x))
865 build_diff = true;
866 break;
870 else
871 old_num = (old_ops ? old_ops->num_v_must_defs : 0);
873 if (!build_diff)
875 v_must_def_ops = old_ops;
876 *old_ops_p = NULL;
877 for (x = 0; x < num; x++)
878 correct_use_link (&(v_must_def_ops->v_must_defs[x].imm_use), stmt);
880 else
882 v_must_def_ops = allocate_v_must_def_optype (num);
883 for (x = 0; x < num ; x++)
885 var = VARRAY_TREE (build_v_must_defs, x);
886 /* Look for VAR in the original vector. */
887 for (i = 0; i < old_num; i++)
889 result = old_ops->v_must_defs[i].def;
890 if (TREE_CODE (result) == SSA_NAME)
891 result = SSA_NAME_VAR (result);
892 if (result == var)
894 initialize_v_must_def_operand (v_must_def_ops, x,
895 old_ops->v_must_defs[i].def,
896 old_ops->v_must_defs[i].use,
897 stmt,
898 &(old_ops->v_must_defs[i].imm_use));
899 break;
902 if (i == old_num)
904 initialize_v_must_def_operand (v_must_def_ops, x, var, var, stmt,
905 NULL);
909 VARRAY_POP_ALL (build_v_must_defs);
911 return v_must_def_ops;
915 /* Finalize all the build vectors, fill the new ones into INFO. */
917 static inline void
918 finalize_ssa_stmt_operands (tree stmt, stmt_operands_p old_ops,
919 stmt_operands_p new_ops)
921 new_ops->def_ops = finalize_ssa_defs (&(old_ops->def_ops), stmt);
922 new_ops->use_ops = finalize_ssa_uses (&(old_ops->use_ops), stmt);
923 new_ops->v_must_def_ops
924 = finalize_ssa_v_must_defs (&(old_ops->v_must_def_ops), stmt);
925 new_ops->v_may_def_ops
926 = finalize_ssa_v_may_defs (&(old_ops->v_may_def_ops), stmt);
927 new_ops->vuse_ops = finalize_ssa_vuses (&(old_ops->vuse_ops), stmt);
931 /* Start the process of building up operands vectors in INFO. */
933 static inline void
934 start_ssa_stmt_operands (void)
936 gcc_assert (VARRAY_ACTIVE_SIZE (build_defs) == 0);
937 gcc_assert (VARRAY_ACTIVE_SIZE (build_uses) == 0);
938 gcc_assert (VARRAY_ACTIVE_SIZE (build_vuses) == 0);
939 gcc_assert (VARRAY_ACTIVE_SIZE (build_v_may_defs) == 0);
940 gcc_assert (VARRAY_ACTIVE_SIZE (build_v_must_defs) == 0);
944 /* Add DEF_P to the list of pointers to operands. */
946 static inline void
947 append_def (tree *def_p)
949 VARRAY_PUSH_TREE_PTR (build_defs, def_p);
953 /* Add USE_P to the list of pointers to operands. */
955 static inline void
956 append_use (tree *use_p)
958 VARRAY_PUSH_TREE_PTR (build_uses, use_p);
962 /* Add a new virtual may def for variable VAR to the build array. */
964 static inline void
965 append_v_may_def (tree var)
967 var_ann_t ann = get_var_ann (var);
969 /* Don't allow duplicate entries. */
970 if (ann->in_v_may_def_list)
971 return;
972 ann->in_v_may_def_list = 1;
974 VARRAY_PUSH_TREE (build_v_may_defs, var);
978 /* Add VAR to the list of virtual uses. */
980 static inline void
981 append_vuse (tree var)
984 /* Don't allow duplicate entries. */
985 if (TREE_CODE (var) != SSA_NAME)
987 var_ann_t ann = get_var_ann (var);
989 if (ann->in_vuse_list || ann->in_v_may_def_list)
990 return;
991 ann->in_vuse_list = 1;
994 VARRAY_PUSH_TREE (build_vuses, var);
998 /* Add VAR to the list of virtual must definitions for INFO. */
1000 static inline void
1001 append_v_must_def (tree var)
1003 unsigned i;
1005 /* Don't allow duplicate entries. */
1006 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_must_defs); i++)
1007 if (var == VARRAY_TREE (build_v_must_defs, i))
1008 return;
1010 VARRAY_PUSH_TREE (build_v_must_defs, var);
1014 /* Parse STMT looking for operands. OLD_OPS is the original stmt operand
1015 cache for STMT, if it existed before. When finished, the various build_*
1016 operand vectors will have potential operands. in them. */
1018 static void
1019 parse_ssa_operands (tree stmt)
1021 enum tree_code code;
1023 code = TREE_CODE (stmt);
1024 switch (code)
1026 case MODIFY_EXPR:
1027 /* First get operands from the RHS. For the LHS, we use a V_MAY_DEF if
1028 either only part of LHS is modified or if the RHS might throw,
1029 otherwise, use V_MUST_DEF.
1031 ??? If it might throw, we should represent somehow that it is killed
1032 on the fallthrough path. */
1034 tree lhs = TREE_OPERAND (stmt, 0);
1035 int lhs_flags = opf_is_def;
1037 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none);
1039 /* If the LHS is a VIEW_CONVERT_EXPR, it isn't changing whether
1040 or not the entire LHS is modified; that depends on what's
1041 inside the VIEW_CONVERT_EXPR. */
1042 if (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
1043 lhs = TREE_OPERAND (lhs, 0);
1045 if (TREE_CODE (lhs) != ARRAY_REF && TREE_CODE (lhs) != ARRAY_RANGE_REF
1046 && TREE_CODE (lhs) != BIT_FIELD_REF
1047 && TREE_CODE (lhs) != REALPART_EXPR
1048 && TREE_CODE (lhs) != IMAGPART_EXPR)
1049 lhs_flags |= opf_kill_def;
1051 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), lhs_flags);
1053 break;
1055 case COND_EXPR:
1056 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
1057 break;
1059 case SWITCH_EXPR:
1060 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
1061 break;
1063 case ASM_EXPR:
1064 get_asm_expr_operands (stmt);
1065 break;
1067 case RETURN_EXPR:
1068 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
1069 break;
1071 case GOTO_EXPR:
1072 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
1073 break;
1075 case LABEL_EXPR:
1076 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
1077 break;
1079 /* These nodes contain no variable references. */
1080 case BIND_EXPR:
1081 case CASE_LABEL_EXPR:
1082 case TRY_CATCH_EXPR:
1083 case TRY_FINALLY_EXPR:
1084 case EH_FILTER_EXPR:
1085 case CATCH_EXPR:
1086 case RESX_EXPR:
1087 break;
1089 default:
1090 /* Notice that if get_expr_operands tries to use &STMT as the operand
1091 pointer (which may only happen for USE operands), we will abort in
1092 append_use. This default will handle statements like empty
1093 statements, or CALL_EXPRs that may appear on the RHS of a statement
1094 or as statements themselves. */
1095 get_expr_operands (stmt, &stmt, opf_none);
1096 break;
1100 /* Create an operands cache for STMT, returning it in NEW_OPS. OLD_OPS are the
1101 original operands, and if ANN is non-null, appropriate stmt flags are set
1102 in the stmt's annotation. If ANN is NULL, this is not considered a "real"
1103 stmt, and none of the operands will be entered into their respective
1104 immediate uses tables. This is to allow stmts to be processed when they
1105 are not actually in the CFG.
1107 Note that some fields in old_ops may change to NULL, although none of the
1108 memory they originally pointed to will be destroyed. It is appropriate
1109 to call free_stmt_operands() on the value returned in old_ops.
1111 The rationale for this: Certain optimizations wish to examine the difference
1112 between new_ops and old_ops after processing. If a set of operands don't
1113 change, new_ops will simply assume the pointer in old_ops, and the old_ops
1114 pointer will be set to NULL, indicating no memory needs to be cleared.
1115 Usage might appear something like:
1117 old_ops_copy = old_ops = stmt_ann(stmt)->operands;
1118 build_ssa_operands (stmt, NULL, &old_ops, &new_ops);
1119 <* compare old_ops_copy and new_ops *>
1120 free_ssa_operands (old_ops); */
1122 static void
1123 build_ssa_operands (tree stmt, stmt_ann_t ann, stmt_operands_p old_ops,
1124 stmt_operands_p new_ops)
1126 tree_ann_t saved_ann = stmt->common.ann;
1128 /* Replace stmt's annotation with the one passed in for the duration
1129 of the operand building process. This allows "fake" stmts to be built
1130 and not be included in other data structures which can be built here. */
1131 stmt->common.ann = (tree_ann_t) ann;
1133 parse_old_ops = old_ops;
1135 /* Initially assume that the statement has no volatile operands, nor
1136 makes aliased loads or stores. */
1137 if (ann)
1139 ann->has_volatile_ops = false;
1140 ann->makes_aliased_stores = false;
1141 ann->makes_aliased_loads = false;
1144 start_ssa_stmt_operands ();
1146 parse_ssa_operands (stmt);
1148 parse_old_ops = NULL;
1150 if (ann)
1151 finalize_ssa_stmt_operands (stmt, old_ops, new_ops);
1152 else
1153 finalize_ssa_stmt_operands (NULL, old_ops, new_ops);
1154 stmt->common.ann = saved_ann;
1158 /* Free any operands vectors in OPS. */
1160 static void
1161 free_ssa_operands (stmt_operands_p ops)
1163 if (ops->def_ops)
1164 free_defs (&(ops->def_ops));
1165 if (ops->use_ops)
1166 free_uses (&(ops->use_ops));
1167 if (ops->vuse_ops)
1168 free_vuses (&(ops->vuse_ops));
1169 if (ops->v_may_def_ops)
1170 free_v_may_defs (&(ops->v_may_def_ops));
1171 if (ops->v_must_def_ops)
1172 free_v_must_defs (&(ops->v_must_def_ops));
1176 /* Swap operands EXP0 and EXP1 in STMT. */
1178 static void
1179 swap_tree_operands (tree *exp0, tree *exp1)
1181 tree op0, op1;
1182 op0 = *exp0;
1183 op1 = *exp1;
1185 /* If the operand cache is active, attempt to preserve the relative positions
1186 of these two operands in their respective immediate use lists. */
1187 if (build_defs != NULL && op0 != op1 && parse_old_ops != NULL)
1189 unsigned x, use0, use1;
1190 use_optype uses = parse_old_ops->use_ops;
1191 use0 = use1 = NUM_USES (uses);
1192 /* Find the 2 operands in the cache, if they are there. */
1193 for (x = 0; x < NUM_USES (uses); x++)
1194 if (USE_OP_PTR (uses, x)->use == exp0)
1196 use0 = x;
1197 break;
1199 for (x = 0; x < NUM_USES (uses); x++)
1200 if (USE_OP_PTR (uses, x)->use == exp1)
1202 use1 = x;
1203 break;
1205 /* If both uses don't have operand entries, there isnt much we can do
1206 at this point. Presumably we dont need to worry about it. */
1207 if (use0 != NUM_USES (uses) && use1 != NUM_USES (uses))
1209 tree *tmp = USE_OP_PTR (uses, use1)->use;
1210 gcc_assert (use0 != use1);
1212 USE_OP_PTR (uses, use1)->use = USE_OP_PTR (uses, use0)->use;
1213 USE_OP_PTR (uses, use0)->use = tmp;
1217 /* Now swap the data. */
1218 *exp0 = op1;
1219 *exp1 = op0;
1222 /* Get the operands of statement STMT. Note that repeated calls to
1223 get_stmt_operands for the same statement will do nothing until the
1224 statement is marked modified by a call to mark_stmt_modified(). */
1226 void
1227 update_stmt_operands (tree stmt)
1229 stmt_ann_t ann;
1230 stmt_operands_t old_operands;
1232 /* If get_stmt_operands is called before SSA is initialized, dont
1233 do anything. */
1234 if (build_defs == NULL)
1235 return;
1236 /* The optimizers cannot handle statements that are nothing but a
1237 _DECL. This indicates a bug in the gimplifier. */
1238 gcc_assert (!SSA_VAR_P (stmt));
1240 ann = get_stmt_ann (stmt);
1242 gcc_assert (ann->modified);
1244 timevar_push (TV_TREE_OPS);
1246 old_operands = ann->operands;
1247 memset (&(ann->operands), 0, sizeof (stmt_operands_t));
1249 build_ssa_operands (stmt, ann, &old_operands, &(ann->operands));
1250 free_ssa_operands (&old_operands);
1252 /* Clear the modified bit for STMT. Subsequent calls to
1253 get_stmt_operands for this statement will do nothing until the
1254 statement is marked modified by a call to mark_stmt_modified(). */
1255 ann->modified = 0;
1257 timevar_pop (TV_TREE_OPS);
1261 /* Recursively scan the expression pointed by EXPR_P in statement referred to
1262 by INFO. FLAGS is one of the OPF_* constants modifying how to interpret the
1263 operands found. */
1265 static void
1266 get_expr_operands (tree stmt, tree *expr_p, int flags)
1268 enum tree_code code;
1269 enum tree_code_class class;
1270 tree expr = *expr_p;
1271 stmt_ann_t s_ann = stmt_ann (stmt);
1273 if (expr == NULL)
1274 return;
1276 code = TREE_CODE (expr);
1277 class = TREE_CODE_CLASS (code);
1279 switch (code)
1281 case ADDR_EXPR:
1282 /* We could have the address of a component, array member,
1283 etc which has interesting variable references. */
1284 /* Taking the address of a variable does not represent a
1285 reference to it, but the fact that the stmt takes its address will be
1286 of interest to some passes (e.g. alias resolution). */
1287 add_stmt_operand (expr_p, s_ann, 0);
1289 /* If the address is invariant, there may be no interesting variable
1290 references inside. */
1291 if (is_gimple_min_invariant (expr))
1292 return;
1294 /* There should be no VUSEs created, since the referenced objects are
1295 not really accessed. The only operands that we should find here
1296 are ARRAY_REF indices which will always be real operands (GIMPLE
1297 does not allow non-registers as array indices). */
1298 flags |= opf_no_vops;
1300 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1301 return;
1303 case SSA_NAME:
1304 case VAR_DECL:
1305 case PARM_DECL:
1306 case RESULT_DECL:
1307 case CONST_DECL:
1309 subvar_t svars;
1311 /* Add the subvars for a variable if it has subvars, to DEFS or USES.
1312 Otherwise, add the variable itself.
1313 Whether it goes to USES or DEFS depends on the operand flags. */
1314 if (var_can_have_subvars (expr)
1315 && (svars = get_subvars_for_var (expr)))
1317 subvar_t sv;
1318 for (sv = svars; sv; sv = sv->next)
1319 add_stmt_operand (&sv->var, s_ann, flags);
1321 else
1323 add_stmt_operand (expr_p, s_ann, flags);
1325 return;
1327 case MISALIGNED_INDIRECT_REF:
1328 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1329 /* fall through */
1331 case ALIGN_INDIRECT_REF:
1332 case INDIRECT_REF:
1333 get_indirect_ref_operands (stmt, expr, flags);
1334 return;
1336 case ARRAY_REF:
1337 case ARRAY_RANGE_REF:
1338 /* Treat array references as references to the virtual variable
1339 representing the array. The virtual variable for an ARRAY_REF
1340 is the VAR_DECL for the array. */
1342 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1343 according to the value of IS_DEF. Recurse if the LHS of the
1344 ARRAY_REF node is not a regular variable. */
1345 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1346 add_stmt_operand (expr_p, s_ann, flags);
1347 else
1348 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1350 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1351 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1352 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1353 return;
1355 case COMPONENT_REF:
1356 case REALPART_EXPR:
1357 case IMAGPART_EXPR:
1359 tree ref;
1360 HOST_WIDE_INT offset, size;
1361 /* This component ref becomes an access to all of the subvariables
1362 it can touch, if we can determine that, but *NOT* the real one.
1363 If we can't determine which fields we could touch, the recursion
1364 will eventually get to a variable and add *all* of its subvars, or
1365 whatever is the minimum correct subset. */
1367 ref = okay_component_ref_for_subvars (expr, &offset, &size);
1368 if (ref)
1370 subvar_t svars = get_subvars_for_var (ref);
1371 subvar_t sv;
1372 for (sv = svars; sv; sv = sv->next)
1374 bool exact;
1375 if (overlap_subvar (offset, size, sv, &exact))
1377 if (exact)
1378 flags &= ~opf_kill_def;
1379 add_stmt_operand (&sv->var, s_ann, flags);
1383 else
1384 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
1385 flags & ~opf_kill_def);
1387 if (code == COMPONENT_REF)
1388 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1389 return;
1391 case WITH_SIZE_EXPR:
1392 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
1393 and an rvalue reference to its second argument. */
1394 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1395 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1396 return;
1398 case CALL_EXPR:
1399 get_call_expr_operands (stmt, expr);
1400 return;
1402 case COND_EXPR:
1403 case VEC_COND_EXPR:
1404 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1405 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1406 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1407 return;
1409 case MODIFY_EXPR:
1411 int subflags;
1412 tree op;
1414 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1416 op = TREE_OPERAND (expr, 0);
1417 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1418 op = TREE_OPERAND (expr, 0);
1419 if (TREE_CODE (op) == ARRAY_REF
1420 || TREE_CODE (op) == ARRAY_RANGE_REF
1421 || TREE_CODE (op) == REALPART_EXPR
1422 || TREE_CODE (op) == IMAGPART_EXPR)
1423 subflags = opf_is_def;
1424 else
1425 subflags = opf_is_def | opf_kill_def;
1427 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), subflags);
1428 return;
1431 case CONSTRUCTOR:
1433 /* General aggregate CONSTRUCTORs have been decomposed, but they
1434 are still in use as the COMPLEX_EXPR equivalent for vectors. */
1436 tree t;
1437 for (t = TREE_OPERAND (expr, 0); t ; t = TREE_CHAIN (t))
1438 get_expr_operands (stmt, &TREE_VALUE (t), opf_none);
1440 return;
1443 case TRUTH_NOT_EXPR:
1444 case BIT_FIELD_REF:
1445 case VIEW_CONVERT_EXPR:
1446 do_unary:
1447 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1448 return;
1450 case TRUTH_AND_EXPR:
1451 case TRUTH_OR_EXPR:
1452 case TRUTH_XOR_EXPR:
1453 case COMPOUND_EXPR:
1454 case OBJ_TYPE_REF:
1455 do_binary:
1457 tree op0 = TREE_OPERAND (expr, 0);
1458 tree op1 = TREE_OPERAND (expr, 1);
1460 /* If it would be profitable to swap the operands, then do so to
1461 canonicalize the statement, enabling better optimization.
1463 By placing canonicalization of such expressions here we
1464 transparently keep statements in canonical form, even
1465 when the statement is modified. */
1466 if (tree_swap_operands_p (op0, op1, false))
1468 /* For relationals we need to swap the operands
1469 and change the code. */
1470 if (code == LT_EXPR
1471 || code == GT_EXPR
1472 || code == LE_EXPR
1473 || code == GE_EXPR)
1475 TREE_SET_CODE (expr, swap_tree_comparison (code));
1476 swap_tree_operands (&TREE_OPERAND (expr, 0),
1477 &TREE_OPERAND (expr, 1));
1480 /* For a commutative operator we can just swap the operands. */
1481 else if (commutative_tree_code (code))
1483 swap_tree_operands (&TREE_OPERAND (expr, 0),
1484 &TREE_OPERAND (expr, 1));
1488 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1489 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1490 return;
1493 case REALIGN_LOAD_EXPR:
1495 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1496 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1497 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1498 return;
1501 case BLOCK:
1502 case FUNCTION_DECL:
1503 case EXC_PTR_EXPR:
1504 case FILTER_EXPR:
1505 case LABEL_DECL:
1506 /* Expressions that make no memory references. */
1507 return;
1509 default:
1510 if (class == tcc_unary)
1511 goto do_unary;
1512 if (class == tcc_binary || class == tcc_comparison)
1513 goto do_binary;
1514 if (class == tcc_constant || class == tcc_type)
1515 return;
1518 /* If we get here, something has gone wrong. */
1519 #ifdef ENABLE_CHECKING
1520 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1521 debug_tree (expr);
1522 fputs ("\n", stderr);
1523 internal_error ("internal error");
1524 #endif
1525 gcc_unreachable ();
1529 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
1531 static void
1532 get_asm_expr_operands (tree stmt)
1534 stmt_ann_t s_ann = stmt_ann (stmt);
1535 int noutputs = list_length (ASM_OUTPUTS (stmt));
1536 const char **oconstraints
1537 = (const char **) alloca ((noutputs) * sizeof (const char *));
1538 int i;
1539 tree link;
1540 const char *constraint;
1541 bool allows_mem, allows_reg, is_inout;
1543 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1545 oconstraints[i] = constraint
1546 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1547 parse_output_constraint (&constraint, i, 0, 0,
1548 &allows_mem, &allows_reg, &is_inout);
1550 /* This should have been split in gimplify_asm_expr. */
1551 gcc_assert (!allows_reg || !is_inout);
1553 /* Memory operands are addressable. Note that STMT needs the
1554 address of this operand. */
1555 if (!allows_reg && allows_mem)
1557 tree t = get_base_address (TREE_VALUE (link));
1558 if (t && DECL_P (t))
1559 note_addressable (t, s_ann);
1562 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1565 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1567 constraint
1568 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1569 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1570 oconstraints, &allows_mem, &allows_reg);
1572 /* Memory operands are addressable. Note that STMT needs the
1573 address of this operand. */
1574 if (!allows_reg && allows_mem)
1576 tree t = get_base_address (TREE_VALUE (link));
1577 if (t && DECL_P (t))
1578 note_addressable (t, s_ann);
1581 get_expr_operands (stmt, &TREE_VALUE (link), 0);
1585 /* Clobber memory for asm ("" : : : "memory"); */
1586 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1587 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1589 unsigned i;
1590 bitmap_iterator bi;
1592 /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1593 decided to group them). */
1594 if (global_var)
1595 add_stmt_operand (&global_var, s_ann, opf_is_def);
1596 else
1597 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1599 tree var = referenced_var (i);
1600 add_stmt_operand (&var, s_ann, opf_is_def);
1603 /* Now clobber all addressables. */
1604 EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i, bi)
1606 tree var = referenced_var (i);
1608 /* Subvars are explicitly represented in this list, so
1609 we don't need the original to be added to the clobber
1610 ops, but the original *will* be in this list because
1611 we keep the addressability of the original
1612 variable up-to-date so we don't screw up the rest of
1613 the backend. */
1614 if (var_can_have_subvars (var)
1615 && get_subvars_for_var (var) != NULL)
1616 continue;
1618 add_stmt_operand (&var, s_ann, opf_is_def);
1621 break;
1625 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1626 ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF. */
1628 static void
1629 get_indirect_ref_operands (tree stmt, tree expr, int flags)
1631 tree *pptr = &TREE_OPERAND (expr, 0);
1632 tree ptr = *pptr;
1633 stmt_ann_t s_ann = stmt_ann (stmt);
1635 /* Stores into INDIRECT_REF operands are never killing definitions. */
1636 flags &= ~opf_kill_def;
1638 if (SSA_VAR_P (ptr))
1640 struct ptr_info_def *pi = NULL;
1642 /* If PTR has flow-sensitive points-to information, use it. */
1643 if (TREE_CODE (ptr) == SSA_NAME
1644 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1645 && pi->name_mem_tag)
1647 /* PTR has its own memory tag. Use it. */
1648 add_stmt_operand (&pi->name_mem_tag, s_ann, flags);
1650 else
1652 /* If PTR is not an SSA_NAME or it doesn't have a name
1653 tag, use its type memory tag. */
1654 var_ann_t v_ann;
1656 /* If we are emitting debugging dumps, display a warning if
1657 PTR is an SSA_NAME with no flow-sensitive alias
1658 information. That means that we may need to compute
1659 aliasing again. */
1660 if (dump_file
1661 && TREE_CODE (ptr) == SSA_NAME
1662 && pi == NULL)
1664 fprintf (dump_file,
1665 "NOTE: no flow-sensitive alias info for ");
1666 print_generic_expr (dump_file, ptr, dump_flags);
1667 fprintf (dump_file, " in ");
1668 print_generic_stmt (dump_file, stmt, dump_flags);
1671 if (TREE_CODE (ptr) == SSA_NAME)
1672 ptr = SSA_NAME_VAR (ptr);
1673 v_ann = var_ann (ptr);
1674 if (v_ann->type_mem_tag)
1675 add_stmt_operand (&v_ann->type_mem_tag, s_ann, flags);
1679 /* If a constant is used as a pointer, we can't generate a real
1680 operand for it but we mark the statement volatile to prevent
1681 optimizations from messing things up. */
1682 else if (TREE_CODE (ptr) == INTEGER_CST)
1684 if (s_ann)
1685 s_ann->has_volatile_ops = true;
1686 return;
1689 /* Everything else *should* have been folded elsewhere, but users
1690 are smarter than we in finding ways to write invalid code. We
1691 cannot just abort here. If we were absolutely certain that we
1692 do handle all valid cases, then we could just do nothing here.
1693 That seems optimistic, so attempt to do something logical... */
1694 else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1695 && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1696 && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1698 /* Make sure we know the object is addressable. */
1699 pptr = &TREE_OPERAND (ptr, 0);
1700 add_stmt_operand (pptr, s_ann, 0);
1702 /* Mark the object itself with a VUSE. */
1703 pptr = &TREE_OPERAND (*pptr, 0);
1704 get_expr_operands (stmt, pptr, flags);
1705 return;
1708 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1709 else
1710 gcc_unreachable ();
1712 /* Add a USE operand for the base pointer. */
1713 get_expr_operands (stmt, pptr, opf_none);
1716 /* A subroutine of get_expr_operands to handle CALL_EXPR. */
1718 static void
1719 get_call_expr_operands (tree stmt, tree expr)
1721 tree op;
1722 int call_flags = call_expr_flags (expr);
1724 /* If aliases have been computed already, add V_MAY_DEF or V_USE
1725 operands for all the symbols that have been found to be
1726 call-clobbered.
1728 Note that if aliases have not been computed, the global effects
1729 of calls will not be included in the SSA web. This is fine
1730 because no optimizer should run before aliases have been
1731 computed. By not bothering with virtual operands for CALL_EXPRs
1732 we avoid adding superfluous virtual operands, which can be a
1733 significant compile time sink (See PR 15855). */
1734 if (aliases_computed_p
1735 && !bitmap_empty_p (call_clobbered_vars)
1736 && !(call_flags & ECF_NOVOPS))
1738 /* A 'pure' or a 'const' functions never call clobber anything.
1739 A 'noreturn' function might, but since we don't return anyway
1740 there is no point in recording that. */
1741 if (TREE_SIDE_EFFECTS (expr)
1742 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1743 add_call_clobber_ops (stmt);
1744 else if (!(call_flags & ECF_CONST))
1745 add_call_read_ops (stmt);
1748 /* Find uses in the called function. */
1749 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1751 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1752 get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1754 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1759 /* Add *VAR_P to the appropriate operand array for INFO. FLAGS is as in
1760 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1761 the statement's real operands, otherwise it is added to virtual
1762 operands. */
1764 static void
1765 add_stmt_operand (tree *var_p, stmt_ann_t s_ann, int flags)
1767 bool is_real_op;
1768 tree var, sym;
1769 var_ann_t v_ann;
1771 var = *var_p;
1772 STRIP_NOPS (var);
1774 /* If the operand is an ADDR_EXPR, add its operand to the list of
1775 variables that have had their address taken in this statement. */
1776 if (TREE_CODE (var) == ADDR_EXPR)
1778 note_addressable (TREE_OPERAND (var, 0), s_ann);
1779 return;
1782 /* If the original variable is not a scalar, it will be added to the list
1783 of virtual operands. In that case, use its base symbol as the virtual
1784 variable representing it. */
1785 is_real_op = is_gimple_reg (var);
1786 if (!is_real_op && !DECL_P (var))
1787 var = get_virtual_var (var);
1789 /* If VAR is not a variable that we care to optimize, do nothing. */
1790 if (var == NULL_TREE || !SSA_VAR_P (var))
1791 return;
1793 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1794 v_ann = var_ann (sym);
1796 /* Mark statements with volatile operands. Optimizers should back
1797 off from statements having volatile operands. */
1798 if (TREE_THIS_VOLATILE (sym) && s_ann)
1799 s_ann->has_volatile_ops = true;
1801 if (is_real_op)
1803 /* The variable is a GIMPLE register. Add it to real operands. */
1804 if (flags & opf_is_def)
1805 append_def (var_p);
1806 else
1807 append_use (var_p);
1809 else
1811 varray_type aliases;
1813 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1814 virtual operands, unless the caller has specifically requested
1815 not to add virtual operands (used when adding operands inside an
1816 ADDR_EXPR expression). */
1817 if (flags & opf_no_vops)
1818 return;
1820 aliases = v_ann->may_aliases;
1822 if (aliases == NULL)
1824 /* The variable is not aliased or it is an alias tag. */
1825 if (flags & opf_is_def)
1827 if (flags & opf_kill_def)
1829 /* Only regular variables or struct fields may get a
1830 V_MUST_DEF operand. */
1831 gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG
1832 || v_ann->mem_tag_kind == STRUCT_FIELD);
1833 /* V_MUST_DEF for non-aliased, non-GIMPLE register
1834 variable definitions. */
1835 append_v_must_def (var);
1837 else
1839 /* Add a V_MAY_DEF for call-clobbered variables and
1840 memory tags. */
1841 append_v_may_def (var);
1844 else
1846 append_vuse (var);
1847 if (s_ann && v_ann->is_alias_tag)
1848 s_ann->makes_aliased_loads = 1;
1851 else
1853 size_t i;
1855 /* The variable is aliased. Add its aliases to the virtual
1856 operands. */
1857 gcc_assert (VARRAY_ACTIVE_SIZE (aliases) != 0);
1859 if (flags & opf_is_def)
1861 /* If the variable is also an alias tag, add a virtual
1862 operand for it, otherwise we will miss representing
1863 references to the members of the variable's alias set.
1864 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1865 if (v_ann->is_alias_tag)
1866 append_v_may_def (var);
1868 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1869 append_v_may_def (VARRAY_TREE (aliases, i));
1871 if (s_ann)
1872 s_ann->makes_aliased_stores = 1;
1874 else
1876 /* Similarly, append a virtual uses for VAR itself, when
1877 it is an alias tag. */
1878 if (v_ann->is_alias_tag)
1879 append_vuse (var);
1881 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1882 append_vuse (VARRAY_TREE (aliases, i));
1884 if (s_ann)
1885 s_ann->makes_aliased_loads = 1;
1892 /* Record that VAR had its address taken in the statement with annotations
1893 S_ANN. */
1895 static void
1896 note_addressable (tree var, stmt_ann_t s_ann)
1898 tree ref;
1899 subvar_t svars;
1900 HOST_WIDE_INT offset;
1901 HOST_WIDE_INT size;
1903 if (!s_ann)
1904 return;
1906 /* If this is a COMPONENT_REF, and we know exactly what it touches, we only
1907 take the address of the subvariables it will touch.
1908 Otherwise, we take the address of all the subvariables, plus the real
1909 ones. */
1911 if (var && TREE_CODE (var) == COMPONENT_REF
1912 && (ref = okay_component_ref_for_subvars (var, &offset, &size)))
1914 subvar_t sv;
1915 svars = get_subvars_for_var (ref);
1917 if (s_ann->addresses_taken == NULL)
1918 s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1920 for (sv = svars; sv; sv = sv->next)
1922 if (overlap_subvar (offset, size, sv, NULL))
1923 bitmap_set_bit (s_ann->addresses_taken, var_ann (sv->var)->uid);
1925 return;
1928 var = get_base_address (var);
1929 if (var && SSA_VAR_P (var))
1931 if (s_ann->addresses_taken == NULL)
1932 s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1935 if (var_can_have_subvars (var)
1936 && (svars = get_subvars_for_var (var)))
1938 subvar_t sv;
1939 for (sv = svars; sv; sv = sv->next)
1940 bitmap_set_bit (s_ann->addresses_taken, var_ann (sv->var)->uid);
1942 else
1943 bitmap_set_bit (s_ann->addresses_taken, var_ann (var)->uid);
1947 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1948 clobbered variables in the function. */
1950 static void
1951 add_call_clobber_ops (tree stmt)
1953 unsigned i;
1954 tree t;
1955 bitmap_iterator bi;
1956 stmt_ann_t s_ann = stmt_ann (stmt);
1957 struct stmt_ann_d empty_ann;
1959 /* Functions that are not const, pure or never return may clobber
1960 call-clobbered variables. */
1961 if (s_ann)
1962 s_ann->makes_clobbering_call = true;
1964 /* If we created .GLOBAL_VAR earlier, just use it. See compute_may_aliases
1965 for the heuristic used to decide whether to create .GLOBAL_VAR or not. */
1966 if (global_var)
1968 add_stmt_operand (&global_var, s_ann, opf_is_def);
1969 return;
1972 /* If cache is valid, copy the elements into the build vectors. */
1973 if (ssa_call_clobbered_cache_valid)
1975 for (i = 0; i < VARRAY_ACTIVE_SIZE (clobbered_vuses); i++)
1977 t = VARRAY_TREE (clobbered_vuses, i);
1978 gcc_assert (TREE_CODE (t) != SSA_NAME);
1979 var_ann (t)->in_vuse_list = 1;
1980 VARRAY_PUSH_TREE (build_vuses, t);
1982 for (i = 0; i < VARRAY_ACTIVE_SIZE (clobbered_v_may_defs); i++)
1984 t = VARRAY_TREE (clobbered_v_may_defs, i);
1985 gcc_assert (TREE_CODE (t) != SSA_NAME);
1986 var_ann (t)->in_v_may_def_list = 1;
1987 VARRAY_PUSH_TREE (build_v_may_defs, t);
1989 if (s_ann)
1991 s_ann->makes_aliased_loads = clobbered_aliased_loads;
1992 s_ann->makes_aliased_stores = clobbered_aliased_stores;
1994 return;
1997 memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
1999 /* Add a V_MAY_DEF operand for every call clobbered variable. */
2000 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
2002 tree var = referenced_var (i);
2003 if (TREE_READONLY (var)
2004 && (TREE_STATIC (var) || DECL_EXTERNAL (var)))
2005 add_stmt_operand (&var, &empty_ann, opf_none);
2006 else
2007 add_stmt_operand (&var, &empty_ann, opf_is_def);
2010 clobbered_aliased_loads = empty_ann.makes_aliased_loads;
2011 clobbered_aliased_stores = empty_ann.makes_aliased_stores;
2013 /* Set the flags for a stmt's annotation. */
2014 if (s_ann)
2016 s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
2017 s_ann->makes_aliased_stores = empty_ann.makes_aliased_stores;
2020 /* Prepare empty cache vectors. */
2021 if (clobbered_v_may_defs)
2023 VARRAY_POP_ALL (clobbered_vuses);
2024 VARRAY_POP_ALL (clobbered_v_may_defs);
2026 else
2028 VARRAY_TREE_INIT (clobbered_v_may_defs, 10, "clobbered_v_may_defs");
2029 VARRAY_TREE_INIT (clobbered_vuses, 10, "clobbered_vuses");
2032 /* Now fill the clobbered cache with the values that have been found. */
2033 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
2034 VARRAY_PUSH_TREE (clobbered_vuses, VARRAY_TREE (build_vuses, i));
2035 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_may_defs); i++)
2036 VARRAY_PUSH_TREE (clobbered_v_may_defs, VARRAY_TREE (build_v_may_defs, i));
2038 ssa_call_clobbered_cache_valid = true;
2042 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
2043 function. */
2045 static void
2046 add_call_read_ops (tree stmt)
2048 unsigned i;
2049 tree t;
2050 bitmap_iterator bi;
2051 stmt_ann_t s_ann = stmt_ann (stmt);
2052 struct stmt_ann_d empty_ann;
2054 /* if the function is not pure, it may reference memory. Add
2055 a VUSE for .GLOBAL_VAR if it has been created. See add_referenced_var
2056 for the heuristic used to decide whether to create .GLOBAL_VAR. */
2057 if (global_var)
2059 add_stmt_operand (&global_var, s_ann, opf_none);
2060 return;
2063 /* If cache is valid, copy the elements into the build vector. */
2064 if (ssa_ro_call_cache_valid)
2066 for (i = 0; i < VARRAY_ACTIVE_SIZE (ro_call_vuses); i++)
2068 t = VARRAY_TREE (ro_call_vuses, i);
2069 gcc_assert (TREE_CODE (t) != SSA_NAME);
2070 var_ann (t)->in_vuse_list = 1;
2071 VARRAY_PUSH_TREE (build_vuses, t);
2073 if (s_ann)
2074 s_ann->makes_aliased_loads = ro_call_aliased_loads;
2075 return;
2078 memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
2080 /* Add a VUSE for each call-clobbered variable. */
2081 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
2083 tree var = referenced_var (i);
2084 add_stmt_operand (&var, &empty_ann, opf_none);
2087 ro_call_aliased_loads = empty_ann.makes_aliased_loads;
2088 if (s_ann)
2089 s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
2091 /* Prepare empty cache vectors. */
2092 if (ro_call_vuses)
2093 VARRAY_POP_ALL (ro_call_vuses);
2094 else
2095 VARRAY_TREE_INIT (ro_call_vuses, 10, "ro_call_vuses");
2097 /* Now fill the clobbered cache with the values that have been found. */
2098 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
2099 VARRAY_PUSH_TREE (ro_call_vuses, VARRAY_TREE (build_vuses, i));
2101 ssa_ro_call_cache_valid = true;
2104 /* Copies virtual operands from SRC to DST. */
2106 void
2107 copy_virtual_operands (tree dst, tree src)
2109 unsigned i;
2110 vuse_optype vuses = STMT_VUSE_OPS (src);
2111 v_may_def_optype v_may_defs = STMT_V_MAY_DEF_OPS (src);
2112 v_must_def_optype v_must_defs = STMT_V_MUST_DEF_OPS (src);
2113 vuse_optype *vuses_new = &stmt_ann (dst)->operands.vuse_ops;
2114 v_may_def_optype *v_may_defs_new = &stmt_ann (dst)->operands.v_may_def_ops;
2115 v_must_def_optype *v_must_defs_new = &stmt_ann (dst)->operands.v_must_def_ops;
2117 if (vuses)
2119 *vuses_new = allocate_vuse_optype (NUM_VUSES (vuses));
2120 for (i = 0; i < NUM_VUSES (vuses); i++)
2121 initialize_vuse_operand (*vuses_new, i, VUSE_OP (vuses, i), dst, NULL);
2124 if (v_may_defs)
2126 *v_may_defs_new = allocate_v_may_def_optype (NUM_V_MAY_DEFS (v_may_defs));
2127 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
2129 initialize_v_may_def_operand (*v_may_defs_new, i,
2130 V_MAY_DEF_RESULT (v_may_defs, i),
2131 V_MAY_DEF_OP (v_may_defs, i), dst,
2132 NULL);
2136 if (v_must_defs)
2138 *v_must_defs_new
2139 = allocate_v_must_def_optype (NUM_V_MUST_DEFS (v_must_defs));
2140 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
2142 initialize_v_must_def_operand (*v_must_defs_new, i,
2143 V_MUST_DEF_RESULT (v_must_defs, i),
2144 V_MUST_DEF_KILL (v_must_defs, i), dst,
2145 NULL);
2151 /* Specifically for use in DOM's expression analysis. Given a store, we
2152 create an artificial stmt which looks like a load from the store, this can
2153 be used to eliminate redundant loads. OLD_OPS are the operands from the
2154 store stmt, and NEW_STMT is the new load which represents a load of the
2155 values stored. */
2157 void
2158 create_ssa_artficial_load_stmt (stmt_operands_p old_ops, tree new_stmt)
2160 stmt_ann_t ann;
2161 tree op;
2162 stmt_operands_t tmp;
2163 unsigned j;
2165 memset (&tmp, 0, sizeof (stmt_operands_t));
2166 ann = get_stmt_ann (new_stmt);
2168 /* Free operands just in case is was an existing stmt. */
2169 free_ssa_operands (&(ann->operands));
2171 build_ssa_operands (new_stmt, NULL, &tmp, &(ann->operands));
2172 free_vuses (&(ann->operands.vuse_ops));
2173 free_v_may_defs (&(ann->operands.v_may_def_ops));
2174 free_v_must_defs (&(ann->operands.v_must_def_ops));
2176 /* For each VDEF on the original statement, we want to create a
2177 VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new
2178 statement. */
2179 for (j = 0; j < NUM_V_MAY_DEFS (old_ops->v_may_def_ops); j++)
2181 op = V_MAY_DEF_RESULT (old_ops->v_may_def_ops, j);
2182 append_vuse (op);
2185 for (j = 0; j < NUM_V_MUST_DEFS (old_ops->v_must_def_ops); j++)
2187 op = V_MUST_DEF_RESULT (old_ops->v_must_def_ops, j);
2188 append_vuse (op);
2191 /* Now set the vuses for this new stmt. */
2192 ann->operands.vuse_ops = finalize_ssa_vuses (&(tmp.vuse_ops), NULL);
2197 /* Issue immediate use error for VAR to debug file F. */
2198 static void
2199 verify_abort (FILE *f, ssa_imm_use_t *var)
2201 tree stmt;
2202 stmt = var->stmt;
2203 if (stmt)
2205 if (stmt_modified_p(stmt))
2207 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)stmt);
2208 print_generic_stmt (f, stmt, TDF_SLIM);
2211 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)var,
2212 (void *)var->use);
2213 print_generic_expr (f, USE_FROM_PTR (var), TDF_SLIM);
2214 fprintf(f, "\n");
2218 /* Scan the immediate_use list for VAR making sure its linked properly.
2219 return RTUE iof there is a problem. */
2221 bool
2222 verify_imm_links (FILE *f, tree var)
2224 ssa_imm_use_t *ptr, *prev;
2225 ssa_imm_use_t *list;
2226 int count;
2228 gcc_assert (TREE_CODE (var) == SSA_NAME);
2230 list = &(SSA_NAME_IMM_USE_NODE (var));
2231 gcc_assert (list->use == NULL);
2233 if (list->prev == NULL)
2235 gcc_assert (list->next == NULL);
2236 return false;
2239 prev = list;
2240 count = 0;
2241 for (ptr = list->next; ptr != list; )
2243 if (prev != ptr->prev)
2245 verify_abort (f, ptr);
2246 return true;
2249 if (ptr->use == NULL)
2251 verify_abort (f, ptr); /* 2 roots, or SAFE guard node. */
2252 return true;
2254 else
2255 if (*(ptr->use) != var)
2257 verify_abort (f, ptr);
2258 return true;
2261 prev = ptr;
2262 ptr = ptr->next;
2263 /* Avoid infinite loops. */
2264 if (count++ > 30000)
2266 verify_abort (f, ptr);
2267 return true;
2271 /* Verify list in the other direction. */
2272 prev = list;
2273 for (ptr = list->prev; ptr != list; )
2275 if (prev != ptr->next)
2277 verify_abort (f, ptr);
2278 return true;
2280 prev = ptr;
2281 ptr = ptr->prev;
2282 if (count-- < 0)
2284 verify_abort (f, ptr);
2285 return true;
2289 if (count != 0)
2291 verify_abort (f, ptr);
2292 return true;
2295 return false;
2299 /* Dump all the immediate uses to FILE. */
2301 void
2302 dump_immediate_uses_for (FILE *file, tree var)
2304 imm_use_iterator iter;
2305 use_operand_p use_p;
2307 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2309 print_generic_expr (file, var, TDF_SLIM);
2310 fprintf (file, " : -->");
2311 if (has_zero_uses (var))
2312 fprintf (file, " no uses.\n");
2313 else
2314 if (has_single_use (var))
2315 fprintf (file, " single use.\n");
2316 else
2317 fprintf (file, "%d uses.\n", num_imm_uses (var));
2319 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2321 print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
2323 fprintf(file, "\n");
2326 /* Dump all the immediate uses to FILE. */
2328 void
2329 dump_immediate_uses (FILE *file)
2331 tree var;
2332 unsigned int x;
2334 fprintf (file, "Immediate_uses: \n\n");
2335 for (x = 1; x < num_ssa_names; x++)
2337 var = ssa_name(x);
2338 if (!var)
2339 continue;
2340 dump_immediate_uses_for (file, var);
2345 /* Dump def-use edges on stderr. */
2347 void
2348 debug_immediate_uses (void)
2350 dump_immediate_uses (stderr);
2353 /* Dump def-use edges on stderr. */
2355 void
2356 debug_immediate_uses_for (tree var)
2358 dump_immediate_uses_for (stderr, var);
2361 #include "gt-tree-ssa-operands.h"