2005-09-26 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / gcc / tree-ssa-operands.c
blob787f2c4d7e10f75e91acbdda4592cb1e77a6c789
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, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, 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 "tree-flow.h"
30 #include "tree-inline.h"
31 #include "tree-pass.h"
32 #include "ggc.h"
33 #include "timevar.h"
34 #include "toplev.h"
35 #include "langhooks.h"
36 #include "ipa-reference.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 The operand tree is the parsed by the various get_* routines which look
54 through the stmt tree for the occurrence of operands which may be of
55 interest, and calls are made to the append_* routines whenever one is
56 found. There are 5 of these routines, each representing one of the
57 5 types of operands. Defs, Uses, Virtual Uses, Virtual May Defs, and
58 Virtual Must Defs.
60 The append_* routines check for duplication, and simply keep a list of
61 unique objects for each operand type in the build_* extendable vectors.
63 Once the stmt tree is completely parsed, the finalize_ssa_operands()
64 routine is called, which proceeds to perform the finalization routine
65 on each of the 5 operand vectors which have been built up.
67 If the stmt had a previous operand cache, the finalization routines
68 attempt to match up the new operands with the old ones. If it's a perfect
69 match, the old vector is simply reused. If it isn't a perfect match, then
70 a new vector is created and the new operands are placed there. For
71 virtual operands, if the previous cache had SSA_NAME version of a
72 variable, and that same variable occurs in the same operands cache, then
73 the new cache vector will also get the same SSA_NAME.
75 i.e., if a stmt had a VUSE of 'a_5', and 'a' occurs in the new operand
76 vector for VUSE, then the new vector will also be modified such that
77 it contains 'a_5' rather than 'a'.
82 /* Flags to describe operand properties in helpers. */
84 /* By default, operands are loaded. */
85 #define opf_none 0
87 /* Operand is the target of an assignment expression or a
88 call-clobbered variable */
89 #define opf_is_def (1 << 0)
91 /* Operand is the target of an assignment expression. */
92 #define opf_kill_def (1 << 1)
94 /* No virtual operands should be created in the expression. This is used
95 when traversing ADDR_EXPR nodes which have different semantics than
96 other expressions. Inside an ADDR_EXPR node, the only operands that we
97 need to consider are indices into arrays. For instance, &a.b[i] should
98 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
99 VUSE for 'b'. */
100 #define opf_no_vops (1 << 2)
102 /* Operand is a "non-specific" kill for call-clobbers and such. This is used
103 to distinguish "reset the world" events from explicit MODIFY_EXPRs. */
104 #define opf_non_specific (1 << 3)
106 /* This structure maintain a sorted list of operands which is created by
107 parse_ssa_operand. */
108 struct opbuild_list_d GTY (())
110 varray_type vars; /* The VAR_DECLS tree. */
111 varray_type uid; /* The sort value for virtual symbols. */
112 varray_type next; /* The next index in the sorted list. */
113 int first; /* First element in list. */
114 unsigned num; /* Number of elements. */
117 #define OPBUILD_LAST -1
120 /* Array for building all the def operands. */
121 static GTY (()) struct opbuild_list_d build_defs;
123 /* Array for building all the use operands. */
124 static GTY (()) struct opbuild_list_d build_uses;
126 /* Array for building all the v_may_def operands. */
127 static GTY (()) struct opbuild_list_d build_v_may_defs;
129 /* Array for building all the vuse operands. */
130 static GTY (()) struct opbuild_list_d build_vuses;
132 /* Array for building all the v_must_def operands. */
133 static GTY (()) struct opbuild_list_d build_v_must_defs;
135 /* True if the operands for call clobbered vars are cached and valid. */
136 bool ssa_call_clobbered_cache_valid;
137 bool ssa_ro_call_cache_valid;
139 /* These arrays are the cached operand vectors for call clobbered calls. */
140 static VEC(tree,heap) *clobbered_v_may_defs;
141 static VEC(tree,heap) *clobbered_vuses;
142 static VEC(tree,heap) *ro_call_vuses;
143 static bool clobbered_aliased_loads;
144 static bool clobbered_aliased_stores;
145 static bool ro_call_aliased_loads;
146 static bool ops_active = false;
148 static GTY (()) struct ssa_operand_memory_d *operand_memory = NULL;
149 static unsigned operand_memory_index;
151 static void get_expr_operands (tree, tree *, int);
152 static void get_asm_expr_operands (tree);
153 static void get_indirect_ref_operands (tree, tree, int);
154 static void get_tmr_operands (tree, tree, int);
155 static void get_call_expr_operands (tree, tree);
156 static inline void append_def (tree *);
157 static inline void append_use (tree *);
158 static void append_v_may_def (tree);
159 static void append_v_must_def (tree);
160 static void add_call_clobber_ops (tree, tree);
161 static void add_call_read_ops (tree);
162 static void add_stmt_operand (tree *, stmt_ann_t, int);
163 static void build_ssa_operands (tree stmt);
165 static def_optype_p free_defs = NULL;
166 static use_optype_p free_uses = NULL;
167 static vuse_optype_p free_vuses = NULL;
168 static maydef_optype_p free_maydefs = NULL;
169 static mustdef_optype_p free_mustdefs = NULL;
171 /* Initialize a virtual operand build LIST called NAME with NUM elements. */
173 static inline void
174 opbuild_initialize_virtual (struct opbuild_list_d *list, int num,
175 const char *name)
177 list->first = OPBUILD_LAST;
178 list->num = 0;
179 VARRAY_TREE_INIT (list->vars, num, name);
180 VARRAY_UINT_INIT (list->uid, num, "List UID");
181 VARRAY_INT_INIT (list->next, num, "List NEXT");
185 /* Initialize a real operand build LIST called NAME with NUM elements. */
187 static inline void
188 opbuild_initialize_real (struct opbuild_list_d *list, int num, const char *name)
190 list->first = OPBUILD_LAST;
191 list->num = 0;
192 VARRAY_TREE_PTR_INIT (list->vars, num, name);
193 VARRAY_INT_INIT (list->next, num, "List NEXT");
194 /* The UID field is not needed since we sort based on the pointer value. */
195 list->uid = NULL;
199 /* Free memory used in virtual operand build object LIST. */
201 static inline void
202 opbuild_free (struct opbuild_list_d *list)
204 list->vars = NULL;
205 list->uid = NULL;
206 list->next = NULL;
210 /* Number of elements in an opbuild list. */
212 static inline unsigned
213 opbuild_num_elems (struct opbuild_list_d *list)
215 return list->num;
219 /* Add VAR to the real operand list LIST, keeping it sorted and avoiding
220 duplicates. The actual sort value is the tree pointer value. */
222 static inline void
223 opbuild_append_real (struct opbuild_list_d *list, tree *var)
225 int index;
227 #ifdef ENABLE_CHECKING
228 /* Ensure the real operand doesn't exist already. */
229 for (index = list->first;
230 index != OPBUILD_LAST;
231 index = VARRAY_INT (list->next, index))
232 gcc_assert (VARRAY_TREE_PTR (list->vars, index) != var);
233 #endif
235 /* First item in the list. */
236 index = VARRAY_ACTIVE_SIZE (list->vars);
237 if (index == 0)
238 list->first = index;
239 else
240 VARRAY_INT (list->next, index - 1) = index;
241 VARRAY_PUSH_INT (list->next, OPBUILD_LAST);
242 VARRAY_PUSH_TREE_PTR (list->vars, var);
243 list->num++;
247 /* Add VAR to the virtual operand list LIST, keeping it sorted and avoiding
248 duplicates. The actual sort value is the DECL UID of the base variable. */
250 static inline void
251 opbuild_append_virtual (struct opbuild_list_d *list, tree var)
253 int index, curr, last;
254 unsigned int var_uid;
256 if (TREE_CODE (var) != SSA_NAME)
257 var_uid = DECL_UID (var);
258 else
259 var_uid = DECL_UID (SSA_NAME_VAR (var));
261 index = VARRAY_ACTIVE_SIZE (list->vars);
263 if (index == 0)
265 VARRAY_PUSH_TREE (list->vars, var);
266 VARRAY_PUSH_UINT (list->uid, var_uid);
267 VARRAY_PUSH_INT (list->next, OPBUILD_LAST);
268 list->first = 0;
269 list->num = 1;
270 return;
273 last = OPBUILD_LAST;
274 /* Find the correct spot in the sorted list. */
275 for (curr = list->first;
276 curr != OPBUILD_LAST;
277 last = curr, curr = VARRAY_INT (list->next, curr))
279 if (VARRAY_UINT (list->uid, curr) > var_uid)
280 break;
283 if (last == OPBUILD_LAST)
285 /* First item in the list. */
286 VARRAY_PUSH_INT (list->next, list->first);
287 list->first = index;
289 else
291 /* Don't enter duplicates at all. */
292 if (VARRAY_UINT (list->uid, last) == var_uid)
293 return;
295 VARRAY_PUSH_INT (list->next, VARRAY_INT (list->next, last));
296 VARRAY_INT (list->next, last) = index;
298 VARRAY_PUSH_TREE (list->vars, var);
299 VARRAY_PUSH_UINT (list->uid, var_uid);
300 list->num++;
304 /* Return the first element index in LIST. OPBUILD_LAST means there are no
305 more elements. */
307 static inline int
308 opbuild_first (struct opbuild_list_d *list)
310 if (list->num > 0)
311 return list->first;
312 else
313 return OPBUILD_LAST;
317 /* Return the next element after PREV in LIST. */
319 static inline int
320 opbuild_next (struct opbuild_list_d *list, int prev)
322 return VARRAY_INT (list->next, prev);
326 /* Return the real element at index ELEM in LIST. */
328 static inline tree *
329 opbuild_elem_real (struct opbuild_list_d *list, int elem)
331 return VARRAY_TREE_PTR (list->vars, elem);
335 /* Return the virtual element at index ELEM in LIST. */
337 static inline tree
338 opbuild_elem_virtual (struct opbuild_list_d *list, int elem)
340 return VARRAY_TREE (list->vars, elem);
344 /* Return the virtual element uid at index ELEM in LIST. */
345 static inline unsigned int
346 opbuild_elem_uid (struct opbuild_list_d *list, int elem)
348 return VARRAY_UINT (list->uid, elem);
352 /* Reset an operand build list. */
354 static inline void
355 opbuild_clear (struct opbuild_list_d *list)
357 list->first = OPBUILD_LAST;
358 VARRAY_POP_ALL (list->vars);
359 VARRAY_POP_ALL (list->next);
360 if (list->uid)
361 VARRAY_POP_ALL (list->uid);
362 list->num = 0;
366 /* Remove ELEM from LIST where PREV is the previous element. Return the next
367 element. */
369 static inline int
370 opbuild_remove_elem (struct opbuild_list_d *list, int elem, int prev)
372 int ret;
373 if (prev != OPBUILD_LAST)
375 gcc_assert (VARRAY_INT (list->next, prev) == elem);
376 ret = VARRAY_INT (list->next, prev) = VARRAY_INT (list->next, elem);
378 else
380 gcc_assert (list->first == elem);
381 ret = list->first = VARRAY_INT (list->next, elem);
383 list->num--;
384 return ret;
388 /* Return true if the ssa operands cache is active. */
390 bool
391 ssa_operands_active (void)
393 return ops_active;
397 /* Initialize the operand cache routines. */
399 void
400 init_ssa_operands (void)
402 opbuild_initialize_real (&build_defs, 5, "build defs");
403 opbuild_initialize_real (&build_uses, 10, "build uses");
404 opbuild_initialize_virtual (&build_vuses, 25, "build_vuses");
405 opbuild_initialize_virtual (&build_v_may_defs, 25, "build_v_may_defs");
406 opbuild_initialize_virtual (&build_v_must_defs, 25, "build_v_must_defs");
407 gcc_assert (operand_memory == NULL);
408 operand_memory_index = SSA_OPERAND_MEMORY_SIZE;
409 ops_active = true;
413 /* Dispose of anything required by the operand routines. */
415 void
416 fini_ssa_operands (void)
418 struct ssa_operand_memory_d *ptr;
419 opbuild_free (&build_defs);
420 opbuild_free (&build_uses);
421 opbuild_free (&build_v_must_defs);
422 opbuild_free (&build_v_may_defs);
423 opbuild_free (&build_vuses);
424 free_defs = NULL;
425 free_uses = NULL;
426 free_vuses = NULL;
427 free_maydefs = NULL;
428 free_mustdefs = NULL;
429 while ((ptr = operand_memory) != NULL)
431 operand_memory = operand_memory->next;
432 ggc_free (ptr);
435 VEC_free (tree, heap, clobbered_v_may_defs);
436 VEC_free (tree, heap, clobbered_vuses);
437 VEC_free (tree, heap, ro_call_vuses);
438 ops_active = false;
442 /* Return memory for operands of SIZE chunks. */
444 static inline void *
445 ssa_operand_alloc (unsigned size)
447 char *ptr;
448 if (operand_memory_index + size >= SSA_OPERAND_MEMORY_SIZE)
450 struct ssa_operand_memory_d *ptr;
451 ptr = ggc_alloc (sizeof (struct ssa_operand_memory_d));
452 ptr->next = operand_memory;
453 operand_memory = ptr;
454 operand_memory_index = 0;
456 ptr = &(operand_memory->mem[operand_memory_index]);
457 operand_memory_index += size;
458 return ptr;
462 /* Make sure PTR is in the correct immediate use list. Since uses are simply
463 pointers into the stmt TREE, there is no way of telling if anyone has
464 changed what this pointer points to via TREE_OPERANDS (exp, 0) = <...>.
465 The contents are different, but the pointer is still the same. This
466 routine will check to make sure PTR is in the correct list, and if it isn't
467 put it in the correct list. We cannot simply check the previous node
468 because all nodes in the same stmt might have be changed. */
470 static inline void
471 correct_use_link (use_operand_p ptr, tree stmt)
473 use_operand_p prev;
474 tree root;
476 /* Fold_stmt () may have changed the stmt pointers. */
477 if (ptr->stmt != stmt)
478 ptr->stmt = stmt;
480 prev = ptr->prev;
481 if (prev)
483 bool stmt_mod = true;
484 /* Find the first element which isn't a SAFE iterator, is in a different
485 stmt, and is not a modified stmt. That node is in the correct list,
486 see if we are too. */
488 while (stmt_mod)
490 while (prev->stmt == stmt || prev->stmt == NULL)
491 prev = prev->prev;
492 if (prev->use == NULL)
493 stmt_mod = false;
494 else
495 if ((stmt_mod = stmt_modified_p (prev->stmt)))
496 prev = prev->prev;
499 /* Get the ssa_name of the list the node is in. */
500 if (prev->use == NULL)
501 root = prev->stmt;
502 else
503 root = *(prev->use);
504 /* If it's the right list, simply return. */
505 if (root == *(ptr->use))
506 return;
508 /* Its in the wrong list if we reach here. */
509 delink_imm_use (ptr);
510 link_imm_use (ptr, *(ptr->use));
514 #define FINALIZE_OPBUILD build_defs
515 #define FINALIZE_OPBUILD_BASE(I) opbuild_elem_real (&build_defs, (I))
516 #define FINALIZE_OPBUILD_ELEM(I) opbuild_elem_real (&build_defs, (I))
517 #define FINALIZE_FUNC finalize_ssa_def_ops
518 #define FINALIZE_ALLOC alloc_def
519 #define FINALIZE_FREE free_defs
520 #define FINALIZE_TYPE struct def_optype_d
521 #define FINALIZE_ELEM(PTR) ((PTR)->def_ptr)
522 #define FINALIZE_OPS DEF_OPS
523 #define FINALIZE_BASE(VAR) VAR
524 #define FINALIZE_BASE_TYPE tree *
525 #define FINALIZE_BASE_ZERO NULL
526 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) FINALIZE_ELEM (PTR) = (VAL)
527 #include "tree-ssa-opfinalize.h"
530 /* This routine will create stmt operands for STMT from the def build list. */
532 static void
533 finalize_ssa_defs (tree stmt)
535 unsigned int num = opbuild_num_elems (&build_defs);
536 /* There should only be a single real definition per assignment. */
537 gcc_assert ((stmt && TREE_CODE (stmt) != MODIFY_EXPR) || num <= 1);
539 /* If there is an old list, often the new list is identical, or close, so
540 find the elements at the beginning that are the same as the vector. */
542 finalize_ssa_def_ops (stmt);
543 opbuild_clear (&build_defs);
546 #define FINALIZE_OPBUILD build_uses
547 #define FINALIZE_OPBUILD_BASE(I) opbuild_elem_real (&build_uses, (I))
548 #define FINALIZE_OPBUILD_ELEM(I) opbuild_elem_real (&build_uses, (I))
549 #define FINALIZE_FUNC finalize_ssa_use_ops
550 #define FINALIZE_ALLOC alloc_use
551 #define FINALIZE_FREE free_uses
552 #define FINALIZE_TYPE struct use_optype_d
553 #define FINALIZE_ELEM(PTR) ((PTR)->use_ptr.use)
554 #define FINALIZE_OPS USE_OPS
555 #define FINALIZE_USE_PTR(PTR) USE_OP_PTR (PTR)
556 #define FINALIZE_BASE(VAR) VAR
557 #define FINALIZE_BASE_TYPE tree *
558 #define FINALIZE_BASE_ZERO NULL
559 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
560 (PTR)->use_ptr.use = (VAL); \
561 link_imm_use_stmt (&((PTR)->use_ptr), \
562 *(VAL), (STMT))
563 #include "tree-ssa-opfinalize.h"
565 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P. */
567 static void
568 finalize_ssa_uses (tree stmt)
570 #ifdef ENABLE_CHECKING
572 unsigned x;
573 unsigned num = opbuild_num_elems (&build_uses);
575 /* If the pointer to the operand is the statement itself, something is
576 wrong. It means that we are pointing to a local variable (the
577 initial call to get_stmt_operands does not pass a pointer to a
578 statement). */
579 for (x = 0; x < num; x++)
580 gcc_assert (*(opbuild_elem_real (&build_uses, x)) != stmt);
582 #endif
583 finalize_ssa_use_ops (stmt);
584 opbuild_clear (&build_uses);
588 /* Return a new v_may_def operand vector for STMT, comparing to OLD_OPS_P. */
589 #define FINALIZE_OPBUILD build_v_may_defs
590 #define FINALIZE_OPBUILD_ELEM(I) opbuild_elem_virtual (&build_v_may_defs, (I))
591 #define FINALIZE_OPBUILD_BASE(I) opbuild_elem_uid (&build_v_may_defs, (I))
592 #define FINALIZE_FUNC finalize_ssa_v_may_def_ops
593 #define FINALIZE_ALLOC alloc_maydef
594 #define FINALIZE_FREE free_maydefs
595 #define FINALIZE_TYPE struct maydef_optype_d
596 #define FINALIZE_ELEM(PTR) MAYDEF_RESULT (PTR)
597 #define FINALIZE_OPS MAYDEF_OPS
598 #define FINALIZE_USE_PTR(PTR) MAYDEF_OP_PTR (PTR)
599 #define FINALIZE_BASE_ZERO 0
600 #define FINALIZE_BASE(VAR) ((TREE_CODE (VAR) == SSA_NAME) \
601 ? DECL_UID (SSA_NAME_VAR (VAR)) : DECL_UID ((VAR)))
602 #define FINALIZE_BASE_TYPE unsigned
603 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
604 (PTR)->def_var = (VAL); \
605 (PTR)->use_var = (VAL); \
606 (PTR)->use_ptr.use = &((PTR)->use_var); \
607 link_imm_use_stmt (&((PTR)->use_ptr), \
608 (VAL), (STMT))
609 #include "tree-ssa-opfinalize.h"
612 static void
613 finalize_ssa_v_may_defs (tree stmt)
615 finalize_ssa_v_may_def_ops (stmt);
619 /* Clear the in_list bits and empty the build array for v_may_defs. */
621 static inline void
622 cleanup_v_may_defs (void)
624 unsigned x, num;
625 num = opbuild_num_elems (&build_v_may_defs);
627 for (x = 0; x < num; x++)
629 tree t = opbuild_elem_virtual (&build_v_may_defs, x);
630 if (TREE_CODE (t) != SSA_NAME)
632 var_ann_t ann = var_ann (t);
633 ann->in_v_may_def_list = 0;
636 opbuild_clear (&build_v_may_defs);
640 #define FINALIZE_OPBUILD build_vuses
641 #define FINALIZE_OPBUILD_ELEM(I) opbuild_elem_virtual (&build_vuses, (I))
642 #define FINALIZE_OPBUILD_BASE(I) opbuild_elem_uid (&build_vuses, (I))
643 #define FINALIZE_FUNC finalize_ssa_vuse_ops
644 #define FINALIZE_ALLOC alloc_vuse
645 #define FINALIZE_FREE free_vuses
646 #define FINALIZE_TYPE struct vuse_optype_d
647 #define FINALIZE_ELEM(PTR) VUSE_OP (PTR)
648 #define FINALIZE_OPS VUSE_OPS
649 #define FINALIZE_USE_PTR(PTR) VUSE_OP_PTR (PTR)
650 #define FINALIZE_BASE_ZERO 0
651 #define FINALIZE_BASE(VAR) ((TREE_CODE (VAR) == SSA_NAME) \
652 ? DECL_UID (SSA_NAME_VAR (VAR)) : DECL_UID ((VAR)))
653 #define FINALIZE_BASE_TYPE unsigned
654 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
655 (PTR)->use_var = (VAL); \
656 (PTR)->use_ptr.use = &((PTR)->use_var); \
657 link_imm_use_stmt (&((PTR)->use_ptr), \
658 (VAL), (STMT))
659 #include "tree-ssa-opfinalize.h"
662 /* Return a new vuse operand vector, comparing to OLD_OPS_P. */
664 static void
665 finalize_ssa_vuses (tree stmt)
667 unsigned num, num_v_may_defs;
668 int vuse_index;
670 /* Remove superfluous VUSE operands. If the statement already has a
671 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
672 needed because V_MAY_DEFs imply a VUSE of the variable. For instance,
673 suppose that variable 'a' is aliased:
675 # VUSE <a_2>
676 # a_3 = V_MAY_DEF <a_2>
677 a = a + 1;
679 The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
680 operation. */
682 num = opbuild_num_elems (&build_vuses);
683 num_v_may_defs = opbuild_num_elems (&build_v_may_defs);
685 if (num > 0 && num_v_may_defs > 0)
687 int last = OPBUILD_LAST;
688 vuse_index = opbuild_first (&build_vuses);
689 for ( ; vuse_index != OPBUILD_LAST; )
691 tree vuse;
692 vuse = opbuild_elem_virtual (&build_vuses, vuse_index);
693 if (TREE_CODE (vuse) != SSA_NAME)
695 var_ann_t ann = var_ann (vuse);
696 ann->in_vuse_list = 0;
697 if (ann->in_v_may_def_list)
699 vuse_index = opbuild_remove_elem (&build_vuses, vuse_index,
700 last);
701 continue;
704 last = vuse_index;
705 vuse_index = opbuild_next (&build_vuses, vuse_index);
708 else
709 /* Clear out the in_list bits. */
710 for (vuse_index = opbuild_first (&build_vuses);
711 vuse_index != OPBUILD_LAST;
712 vuse_index = opbuild_next (&build_vuses, vuse_index))
714 tree t = opbuild_elem_virtual (&build_vuses, vuse_index);
715 if (TREE_CODE (t) != SSA_NAME)
717 var_ann_t ann = var_ann (t);
718 ann->in_vuse_list = 0;
722 finalize_ssa_vuse_ops (stmt);
723 /* The v_may_def build vector wasn't cleaned up because we needed it. */
724 cleanup_v_may_defs ();
726 /* Free the vuses build vector. */
727 opbuild_clear (&build_vuses);
731 /* Return a new v_must_def operand vector for STMT, comparing to OLD_OPS_P. */
733 #define FINALIZE_OPBUILD build_v_must_defs
734 #define FINALIZE_OPBUILD_ELEM(I) opbuild_elem_virtual (&build_v_must_defs, (I))
735 #define FINALIZE_OPBUILD_BASE(I) opbuild_elem_uid (&build_v_must_defs, (I))
736 #define FINALIZE_FUNC finalize_ssa_v_must_def_ops
737 #define FINALIZE_ALLOC alloc_mustdef
738 #define FINALIZE_FREE free_mustdefs
739 #define FINALIZE_TYPE struct mustdef_optype_d
740 #define FINALIZE_ELEM(PTR) MUSTDEF_RESULT (PTR)
741 #define FINALIZE_OPS MUSTDEF_OPS
742 #define FINALIZE_USE_PTR(PTR) MUSTDEF_KILL_PTR (PTR)
743 #define FINALIZE_BASE_ZERO 0
744 #define FINALIZE_BASE(VAR) ((TREE_CODE (VAR) == SSA_NAME) \
745 ? DECL_UID (SSA_NAME_VAR (VAR)) : DECL_UID ((VAR)))
746 #define FINALIZE_BASE_TYPE unsigned
747 #define FINALIZE_INITIALIZE(PTR, VAL, STMT) \
748 (PTR)->def_var = (VAL); \
749 (PTR)->kill_var = (VAL); \
750 (PTR)->use_ptr.use = &((PTR)->kill_var);\
751 link_imm_use_stmt (&((PTR)->use_ptr), \
752 (VAL), (STMT))
753 #include "tree-ssa-opfinalize.h"
756 static void
757 finalize_ssa_v_must_defs (tree stmt)
759 /* In the presence of subvars, there may be more than one V_MUST_DEF per
760 statement (one for each subvar). It is a bit expensive to verify that
761 all must-defs in a statement belong to subvars if there is more than one
762 MUST-def, so we don't do it. Suffice to say, if you reach here without
763 having subvars, and have num >1, you have hit a bug. */
765 finalize_ssa_v_must_def_ops (stmt);
766 opbuild_clear (&build_v_must_defs);
770 /* Finalize all the build vectors, fill the new ones into INFO. */
772 static inline void
773 finalize_ssa_stmt_operands (tree stmt)
775 finalize_ssa_defs (stmt);
776 finalize_ssa_uses (stmt);
777 finalize_ssa_v_must_defs (stmt);
778 finalize_ssa_v_may_defs (stmt);
779 finalize_ssa_vuses (stmt);
783 /* Start the process of building up operands vectors in INFO. */
785 static inline void
786 start_ssa_stmt_operands (void)
788 gcc_assert (opbuild_num_elems (&build_defs) == 0);
789 gcc_assert (opbuild_num_elems (&build_uses) == 0);
790 gcc_assert (opbuild_num_elems (&build_vuses) == 0);
791 gcc_assert (opbuild_num_elems (&build_v_may_defs) == 0);
792 gcc_assert (opbuild_num_elems (&build_v_must_defs) == 0);
796 /* Add DEF_P to the list of pointers to operands. */
798 static inline void
799 append_def (tree *def_p)
801 opbuild_append_real (&build_defs, def_p);
805 /* Add USE_P to the list of pointers to operands. */
807 static inline void
808 append_use (tree *use_p)
810 opbuild_append_real (&build_uses, use_p);
814 /* Add a new virtual may def for variable VAR to the build array. */
816 static inline void
817 append_v_may_def (tree var)
819 if (TREE_CODE (var) != SSA_NAME)
821 var_ann_t ann = get_var_ann (var);
823 /* Don't allow duplicate entries. */
824 if (ann->in_v_may_def_list)
825 return;
826 ann->in_v_may_def_list = 1;
829 opbuild_append_virtual (&build_v_may_defs, var);
833 /* Add VAR to the list of virtual uses. */
835 static inline void
836 append_vuse (tree var)
839 /* Don't allow duplicate entries. */
840 if (TREE_CODE (var) != SSA_NAME)
842 var_ann_t ann = get_var_ann (var);
844 if (ann->in_vuse_list || ann->in_v_may_def_list)
845 return;
846 ann->in_vuse_list = 1;
849 opbuild_append_virtual (&build_vuses, var);
853 /* Add VAR to the list of virtual must definitions for INFO. */
855 static inline void
856 append_v_must_def (tree var)
858 unsigned i;
860 /* Don't allow duplicate entries. */
861 for (i = 0; i < opbuild_num_elems (&build_v_must_defs); i++)
862 if (var == opbuild_elem_virtual (&build_v_must_defs, i))
863 return;
865 opbuild_append_virtual (&build_v_must_defs, var);
869 /* Parse STMT looking for operands. OLD_OPS is the original stmt operand
870 cache for STMT, if it existed before. When finished, the various build_*
871 operand vectors will have potential operands. in them. */
873 static void
874 parse_ssa_operands (tree stmt)
876 enum tree_code code;
878 code = TREE_CODE (stmt);
879 switch (code)
881 case MODIFY_EXPR:
882 /* First get operands from the RHS. For the LHS, we use a V_MAY_DEF if
883 either only part of LHS is modified or if the RHS might throw,
884 otherwise, use V_MUST_DEF.
886 ??? If it might throw, we should represent somehow that it is killed
887 on the fallthrough path. */
889 tree lhs = TREE_OPERAND (stmt, 0);
890 int lhs_flags = opf_is_def;
892 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none);
894 /* If the LHS is a VIEW_CONVERT_EXPR, it isn't changing whether
895 or not the entire LHS is modified; that depends on what's
896 inside the VIEW_CONVERT_EXPR. */
897 if (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
898 lhs = TREE_OPERAND (lhs, 0);
900 if (TREE_CODE (lhs) != ARRAY_REF
901 && TREE_CODE (lhs) != ARRAY_RANGE_REF
902 && TREE_CODE (lhs) != BIT_FIELD_REF
903 && TREE_CODE (lhs) != REALPART_EXPR
904 && TREE_CODE (lhs) != IMAGPART_EXPR)
905 lhs_flags |= opf_kill_def;
907 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), lhs_flags);
909 break;
911 case COND_EXPR:
912 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
913 break;
915 case SWITCH_EXPR:
916 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
917 break;
919 case ASM_EXPR:
920 get_asm_expr_operands (stmt);
921 break;
923 case RETURN_EXPR:
924 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
925 break;
927 case GOTO_EXPR:
928 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
929 break;
931 case LABEL_EXPR:
932 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
933 break;
935 /* These nodes contain no variable references. */
936 case BIND_EXPR:
937 case CASE_LABEL_EXPR:
938 case TRY_CATCH_EXPR:
939 case TRY_FINALLY_EXPR:
940 case EH_FILTER_EXPR:
941 case CATCH_EXPR:
942 case RESX_EXPR:
943 break;
945 default:
946 /* Notice that if get_expr_operands tries to use &STMT as the operand
947 pointer (which may only happen for USE operands), we will fail in
948 append_use. This default will handle statements like empty
949 statements, or CALL_EXPRs that may appear on the RHS of a statement
950 or as statements themselves. */
951 get_expr_operands (stmt, &stmt, opf_none);
952 break;
956 /* Create an operands cache for STMT, returning it in NEW_OPS. OLD_OPS are the
957 original operands, and if ANN is non-null, appropriate stmt flags are set
958 in the stmt's annotation. If ANN is NULL, this is not considered a "real"
959 stmt, and none of the operands will be entered into their respective
960 immediate uses tables. This is to allow stmts to be processed when they
961 are not actually in the CFG.
963 Note that some fields in old_ops may change to NULL, although none of the
964 memory they originally pointed to will be destroyed. It is appropriate
965 to call free_stmt_operands() on the value returned in old_ops.
967 The rationale for this: Certain optimizations wish to examine the difference
968 between new_ops and old_ops after processing. If a set of operands don't
969 change, new_ops will simply assume the pointer in old_ops, and the old_ops
970 pointer will be set to NULL, indicating no memory needs to be cleared.
971 Usage might appear something like:
973 old_ops_copy = old_ops = stmt_ann(stmt)->operands;
974 build_ssa_operands (stmt, NULL, &old_ops, &new_ops);
975 <* compare old_ops_copy and new_ops *>
976 free_ssa_operands (old_ops); */
978 static void
979 build_ssa_operands (tree stmt)
981 stmt_ann_t ann = get_stmt_ann (stmt);
983 /* Initially assume that the statement has no volatile operands, nor
984 makes aliased loads or stores. */
985 if (ann)
987 ann->has_volatile_ops = false;
988 ann->makes_aliased_stores = false;
989 ann->makes_aliased_loads = false;
992 start_ssa_stmt_operands ();
994 parse_ssa_operands (stmt);
996 finalize_ssa_stmt_operands (stmt);
1000 /* Free any operands vectors in OPS. */
1001 void
1002 free_ssa_operands (stmt_operands_p ops)
1004 ops->def_ops = NULL;
1005 ops->use_ops = NULL;
1006 ops->maydef_ops = NULL;
1007 ops->mustdef_ops = NULL;
1008 ops->vuse_ops = NULL;
1012 /* Get the operands of statement STMT. Note that repeated calls to
1013 get_stmt_operands for the same statement will do nothing until the
1014 statement is marked modified by a call to mark_stmt_modified(). */
1016 void
1017 update_stmt_operands (tree stmt)
1019 stmt_ann_t ann = get_stmt_ann (stmt);
1020 /* If get_stmt_operands is called before SSA is initialized, dont
1021 do anything. */
1022 if (!ssa_operands_active ())
1023 return;
1024 /* The optimizers cannot handle statements that are nothing but a
1025 _DECL. This indicates a bug in the gimplifier. */
1026 gcc_assert (!SSA_VAR_P (stmt));
1028 gcc_assert (ann->modified);
1030 timevar_push (TV_TREE_OPS);
1032 build_ssa_operands (stmt);
1034 /* Clear the modified bit for STMT. Subsequent calls to
1035 get_stmt_operands for this statement will do nothing until the
1036 statement is marked modified by a call to mark_stmt_modified(). */
1037 ann->modified = 0;
1039 timevar_pop (TV_TREE_OPS);
1043 /* Copies virtual operands from SRC to DST. */
1045 void
1046 copy_virtual_operands (tree dest, tree src)
1048 tree t;
1049 ssa_op_iter iter, old_iter;
1050 use_operand_p use_p, u2;
1051 def_operand_p def_p, d2;
1053 build_ssa_operands (dest);
1055 /* Copy all the virtual fields. */
1056 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VUSE)
1057 append_vuse (t);
1058 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMAYDEF)
1059 append_v_may_def (t);
1060 FOR_EACH_SSA_TREE_OPERAND (t, src, iter, SSA_OP_VMUSTDEF)
1061 append_v_must_def (t);
1063 if (opbuild_num_elems (&build_vuses) == 0
1064 && opbuild_num_elems (&build_v_may_defs) == 0
1065 && opbuild_num_elems (&build_v_must_defs) == 0)
1066 return;
1068 /* Now commit the virtual operands to this stmt. */
1069 finalize_ssa_v_must_defs (dest);
1070 finalize_ssa_v_may_defs (dest);
1071 finalize_ssa_vuses (dest);
1073 /* Finally, set the field to the same values as then originals. */
1076 t = op_iter_init_tree (&old_iter, src, SSA_OP_VUSE);
1077 FOR_EACH_SSA_USE_OPERAND (use_p, dest, iter, SSA_OP_VUSE)
1079 gcc_assert (!op_iter_done (&old_iter));
1080 SET_USE (use_p, t);
1081 t = op_iter_next_tree (&old_iter);
1083 gcc_assert (op_iter_done (&old_iter));
1085 op_iter_init_maydef (&old_iter, src, &u2, &d2);
1086 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, dest, iter)
1088 gcc_assert (!op_iter_done (&old_iter));
1089 SET_USE (use_p, USE_FROM_PTR (u2));
1090 SET_DEF (def_p, DEF_FROM_PTR (d2));
1091 op_iter_next_maymustdef (&u2, &d2, &old_iter);
1093 gcc_assert (op_iter_done (&old_iter));
1095 op_iter_init_mustdef (&old_iter, src, &u2, &d2);
1096 FOR_EACH_SSA_MUSTDEF_OPERAND (def_p, use_p, dest, iter)
1098 gcc_assert (!op_iter_done (&old_iter));
1099 SET_USE (use_p, USE_FROM_PTR (u2));
1100 SET_DEF (def_p, DEF_FROM_PTR (d2));
1101 op_iter_next_maymustdef (&u2, &d2, &old_iter);
1103 gcc_assert (op_iter_done (&old_iter));
1108 /* Specifically for use in DOM's expression analysis. Given a store, we
1109 create an artificial stmt which looks like a load from the store, this can
1110 be used to eliminate redundant loads. OLD_OPS are the operands from the
1111 store stmt, and NEW_STMT is the new load which represents a load of the
1112 values stored. */
1114 void
1115 create_ssa_artficial_load_stmt (tree new_stmt, tree old_stmt)
1117 stmt_ann_t ann;
1118 tree op;
1119 ssa_op_iter iter;
1120 use_operand_p use_p;
1121 unsigned x;
1123 ann = get_stmt_ann (new_stmt);
1125 /* process the stmt looking for operands. */
1126 start_ssa_stmt_operands ();
1127 parse_ssa_operands (new_stmt);
1129 for (x = 0; x < opbuild_num_elems (&build_vuses); x++)
1131 tree t = opbuild_elem_virtual (&build_vuses, x);
1132 if (TREE_CODE (t) != SSA_NAME)
1134 var_ann_t ann = var_ann (t);
1135 ann->in_vuse_list = 0;
1139 for (x = 0; x < opbuild_num_elems (&build_v_may_defs); x++)
1141 tree t = opbuild_elem_virtual (&build_v_may_defs, x);
1142 if (TREE_CODE (t) != SSA_NAME)
1144 var_ann_t ann = var_ann (t);
1145 ann->in_v_may_def_list = 0;
1148 /* Remove any virtual operands that were found. */
1149 opbuild_clear (&build_v_may_defs);
1150 opbuild_clear (&build_v_must_defs);
1151 opbuild_clear (&build_vuses);
1153 /* For each VDEF on the original statement, we want to create a
1154 VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new
1155 statement. */
1156 FOR_EACH_SSA_TREE_OPERAND (op, old_stmt, iter,
1157 (SSA_OP_VMAYDEF | SSA_OP_VMUSTDEF))
1158 append_vuse (op);
1160 /* Now build the operands for this new stmt. */
1161 finalize_ssa_stmt_operands (new_stmt);
1163 /* All uses in this fake stmt must not be in the immediate use lists. */
1164 FOR_EACH_SSA_USE_OPERAND (use_p, new_stmt, iter, SSA_OP_ALL_USES)
1165 delink_imm_use (use_p);
1168 void
1169 swap_tree_operands (tree stmt, tree *exp0, tree *exp1)
1171 tree op0, op1;
1172 op0 = *exp0;
1173 op1 = *exp1;
1175 /* If the operand cache is active, attempt to preserve the relative positions
1176 of these two operands in their respective immediate use lists. */
1177 if (ssa_operands_active () && op0 != op1)
1179 use_optype_p use0, use1, ptr;
1180 use0 = use1 = NULL;
1181 /* Find the 2 operands in the cache, if they are there. */
1182 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
1183 if (USE_OP_PTR (ptr)->use == exp0)
1185 use0 = ptr;
1186 break;
1188 for (ptr = USE_OPS (stmt); ptr; ptr = ptr->next)
1189 if (USE_OP_PTR (ptr)->use == exp1)
1191 use1 = ptr;
1192 break;
1194 /* If both uses don't have operand entries, there isn't much we can do
1195 at this point. Presumably we dont need to worry about it. */
1196 if (use0 && use1)
1198 tree *tmp = USE_OP_PTR (use1)->use;
1199 USE_OP_PTR (use1)->use = USE_OP_PTR (use0)->use;
1200 USE_OP_PTR (use0)->use = tmp;
1204 /* Now swap the data. */
1205 *exp0 = op1;
1206 *exp1 = op0;
1210 /* Recursively scan the expression pointed to by EXPR_P in statement referred
1211 to by INFO. FLAGS is one of the OPF_* constants modifying how to interpret
1212 the operands found. */
1214 static void
1215 get_expr_operands (tree stmt, tree *expr_p, int flags)
1217 enum tree_code code;
1218 enum tree_code_class class;
1219 tree expr = *expr_p;
1220 stmt_ann_t s_ann = stmt_ann (stmt);
1222 if (expr == NULL)
1223 return;
1225 code = TREE_CODE (expr);
1226 class = TREE_CODE_CLASS (code);
1228 switch (code)
1230 case ADDR_EXPR:
1231 /* We could have the address of a component, array member,
1232 etc which has interesting variable references. */
1233 /* Taking the address of a variable does not represent a
1234 reference to it, but the fact that the stmt takes its address will be
1235 of interest to some passes (e.g. alias resolution). */
1236 add_stmt_operand (expr_p, s_ann, 0);
1238 /* If the address is invariant, there may be no interesting variable
1239 references inside. */
1240 if (is_gimple_min_invariant (expr))
1241 return;
1243 /* There should be no VUSEs created, since the referenced objects are
1244 not really accessed. The only operands that we should find here
1245 are ARRAY_REF indices which will always be real operands (GIMPLE
1246 does not allow non-registers as array indices). */
1247 flags |= opf_no_vops;
1249 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1250 return;
1252 case SSA_NAME:
1253 case STRUCT_FIELD_TAG:
1254 case TYPE_MEMORY_TAG:
1255 case NAME_MEMORY_TAG:
1256 case VAR_DECL:
1257 case PARM_DECL:
1258 case RESULT_DECL:
1259 case CONST_DECL:
1261 subvar_t svars;
1263 /* Add the subvars for a variable if it has subvars, to DEFS or USES.
1264 Otherwise, add the variable itself.
1265 Whether it goes to USES or DEFS depends on the operand flags. */
1266 if (var_can_have_subvars (expr)
1267 && (svars = get_subvars_for_var (expr)))
1269 subvar_t sv;
1270 for (sv = svars; sv; sv = sv->next)
1271 add_stmt_operand (&sv->var, s_ann, flags);
1273 else
1275 add_stmt_operand (expr_p, s_ann, flags);
1277 return;
1279 case MISALIGNED_INDIRECT_REF:
1280 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1281 /* fall through */
1283 case ALIGN_INDIRECT_REF:
1284 case INDIRECT_REF:
1285 get_indirect_ref_operands (stmt, expr, flags);
1286 return;
1288 case TARGET_MEM_REF:
1289 get_tmr_operands (stmt, expr, flags);
1290 return;
1292 case ARRAY_REF:
1293 case ARRAY_RANGE_REF:
1294 /* Treat array references as references to the virtual variable
1295 representing the array. The virtual variable for an ARRAY_REF
1296 is the VAR_DECL for the array. */
1298 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1299 according to the value of IS_DEF. Recurse if the LHS of the
1300 ARRAY_REF node is not a regular variable. */
1301 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1302 add_stmt_operand (expr_p, s_ann, flags);
1303 else
1304 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1306 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1307 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1308 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1309 return;
1311 case COMPONENT_REF:
1312 case REALPART_EXPR:
1313 case IMAGPART_EXPR:
1315 tree ref;
1316 unsigned HOST_WIDE_INT offset, size;
1317 bool none = true;
1318 /* This component ref becomes an access to all of the subvariables
1319 it can touch, if we can determine that, but *NOT* the real one.
1320 If we can't determine which fields we could touch, the recursion
1321 will eventually get to a variable and add *all* of its subvars, or
1322 whatever is the minimum correct subset. */
1324 ref = okay_component_ref_for_subvars (expr, &offset, &size);
1325 if (ref)
1327 subvar_t svars = get_subvars_for_var (ref);
1328 subvar_t sv;
1329 for (sv = svars; sv; sv = sv->next)
1331 bool exact;
1332 if (overlap_subvar (offset, size, sv, &exact))
1334 int subvar_flags = flags;
1335 none = false;
1336 if (!exact)
1337 subvar_flags &= ~opf_kill_def;
1338 add_stmt_operand (&sv->var, s_ann, subvar_flags);
1341 if (!none)
1342 flags |= opf_no_vops;
1344 /* Even if we found subvars above we need to ensure to see
1345 immediate uses for d in s.a[d]. In case of s.a having
1346 a subvar we'd miss it otherwise. */
1347 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
1348 flags & ~opf_kill_def);
1350 if (code == COMPONENT_REF)
1352 if (s_ann && TREE_THIS_VOLATILE (TREE_OPERAND (expr, 1)))
1353 s_ann->has_volatile_ops = true;
1354 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1356 return;
1358 case WITH_SIZE_EXPR:
1359 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
1360 and an rvalue reference to its second argument. */
1361 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1362 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1363 return;
1365 case CALL_EXPR:
1366 get_call_expr_operands (stmt, expr);
1367 return;
1369 case COND_EXPR:
1370 case VEC_COND_EXPR:
1371 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1372 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1373 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1374 return;
1376 case MODIFY_EXPR:
1378 int subflags;
1379 tree op;
1381 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1383 op = TREE_OPERAND (expr, 0);
1384 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1385 op = TREE_OPERAND (expr, 0);
1386 if (TREE_CODE (op) == ARRAY_REF
1387 || TREE_CODE (op) == ARRAY_RANGE_REF
1388 || TREE_CODE (op) == REALPART_EXPR
1389 || TREE_CODE (op) == IMAGPART_EXPR)
1390 subflags = opf_is_def;
1391 else
1392 subflags = opf_is_def | opf_kill_def;
1394 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), subflags);
1395 return;
1398 case CONSTRUCTOR:
1400 /* General aggregate CONSTRUCTORs have been decomposed, but they
1401 are still in use as the COMPLEX_EXPR equivalent for vectors. */
1402 constructor_elt *ce;
1403 unsigned HOST_WIDE_INT idx;
1405 for (idx = 0;
1406 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (expr), idx, ce);
1407 idx++)
1408 get_expr_operands (stmt, &ce->value, opf_none);
1410 return;
1413 case TRUTH_NOT_EXPR:
1414 case BIT_FIELD_REF:
1415 case VIEW_CONVERT_EXPR:
1416 do_unary:
1417 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1418 return;
1420 case TRUTH_AND_EXPR:
1421 case TRUTH_OR_EXPR:
1422 case TRUTH_XOR_EXPR:
1423 case COMPOUND_EXPR:
1424 case OBJ_TYPE_REF:
1425 case ASSERT_EXPR:
1426 do_binary:
1428 tree op0 = TREE_OPERAND (expr, 0);
1429 tree op1 = TREE_OPERAND (expr, 1);
1431 /* If it would be profitable to swap the operands, then do so to
1432 canonicalize the statement, enabling better optimization.
1434 By placing canonicalization of such expressions here we
1435 transparently keep statements in canonical form, even
1436 when the statement is modified. */
1437 if (tree_swap_operands_p (op0, op1, false))
1439 /* For relationals we need to swap the operands
1440 and change the code. */
1441 if (code == LT_EXPR
1442 || code == GT_EXPR
1443 || code == LE_EXPR
1444 || code == GE_EXPR)
1446 TREE_SET_CODE (expr, swap_tree_comparison (code));
1447 swap_tree_operands (stmt,
1448 &TREE_OPERAND (expr, 0),
1449 &TREE_OPERAND (expr, 1));
1452 /* For a commutative operator we can just swap the operands. */
1453 else if (commutative_tree_code (code))
1455 swap_tree_operands (stmt,
1456 &TREE_OPERAND (expr, 0),
1457 &TREE_OPERAND (expr, 1));
1461 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1462 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1463 return;
1466 case REALIGN_LOAD_EXPR:
1468 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1469 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1470 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1471 return;
1474 case BLOCK:
1475 case FUNCTION_DECL:
1476 case EXC_PTR_EXPR:
1477 case FILTER_EXPR:
1478 case LABEL_DECL:
1479 /* Expressions that make no memory references. */
1480 return;
1482 default:
1483 if (class == tcc_unary)
1484 goto do_unary;
1485 if (class == tcc_binary || class == tcc_comparison)
1486 goto do_binary;
1487 if (class == tcc_constant || class == tcc_type)
1488 return;
1491 /* If we get here, something has gone wrong. */
1492 #ifdef ENABLE_CHECKING
1493 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1494 debug_tree (expr);
1495 fputs ("\n", stderr);
1496 internal_error ("internal error");
1497 #endif
1498 gcc_unreachable ();
1502 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
1504 static void
1505 get_asm_expr_operands (tree stmt)
1507 stmt_ann_t s_ann = stmt_ann (stmt);
1508 int noutputs = list_length (ASM_OUTPUTS (stmt));
1509 const char **oconstraints
1510 = (const char **) alloca ((noutputs) * sizeof (const char *));
1511 int i;
1512 tree link;
1513 const char *constraint;
1514 bool allows_mem, allows_reg, is_inout;
1516 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1518 oconstraints[i] = constraint
1519 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1520 parse_output_constraint (&constraint, i, 0, 0,
1521 &allows_mem, &allows_reg, &is_inout);
1523 /* This should have been split in gimplify_asm_expr. */
1524 gcc_assert (!allows_reg || !is_inout);
1526 /* Memory operands are addressable. Note that STMT needs the
1527 address of this operand. */
1528 if (!allows_reg && allows_mem)
1530 tree t = get_base_address (TREE_VALUE (link));
1531 if (t && DECL_P (t) && s_ann)
1532 add_to_addressable_set (t, &s_ann->addresses_taken);
1535 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1538 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1540 constraint
1541 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1542 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1543 oconstraints, &allows_mem, &allows_reg);
1545 /* Memory operands are addressable. Note that STMT needs the
1546 address of this operand. */
1547 if (!allows_reg && allows_mem)
1549 tree t = get_base_address (TREE_VALUE (link));
1550 if (t && DECL_P (t) && s_ann)
1551 add_to_addressable_set (t, &s_ann->addresses_taken);
1554 get_expr_operands (stmt, &TREE_VALUE (link), 0);
1558 /* Clobber memory for asm ("" : : : "memory"); */
1559 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1560 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1562 unsigned i;
1563 bitmap_iterator bi;
1565 /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1566 decided to group them). */
1567 if (global_var)
1568 add_stmt_operand (&global_var, s_ann, opf_is_def);
1569 else
1570 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1572 tree var = referenced_var (i);
1573 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1576 /* Now clobber all addressables. */
1577 EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i, bi)
1579 tree var = referenced_var (i);
1581 /* Subvars are explicitly represented in this list, so
1582 we don't need the original to be added to the clobber
1583 ops, but the original *will* be in this list because
1584 we keep the addressability of the original
1585 variable up-to-date so we don't screw up the rest of
1586 the backend. */
1587 if (var_can_have_subvars (var)
1588 && get_subvars_for_var (var) != NULL)
1589 continue;
1591 add_stmt_operand (&var, s_ann, opf_is_def | opf_non_specific);
1594 break;
1598 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1599 ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF. */
1601 static void
1602 get_indirect_ref_operands (tree stmt, tree expr, int flags)
1604 tree *pptr = &TREE_OPERAND (expr, 0);
1605 tree ptr = *pptr;
1606 stmt_ann_t s_ann = stmt_ann (stmt);
1608 /* Stores into INDIRECT_REF operands are never killing definitions. */
1609 flags &= ~opf_kill_def;
1611 if (SSA_VAR_P (ptr))
1613 struct ptr_info_def *pi = NULL;
1615 /* If PTR has flow-sensitive points-to information, use it. */
1616 if (TREE_CODE (ptr) == SSA_NAME
1617 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1618 && pi->name_mem_tag)
1620 /* PTR has its own memory tag. Use it. */
1621 add_stmt_operand (&pi->name_mem_tag, s_ann, flags);
1623 else
1625 /* If PTR is not an SSA_NAME or it doesn't have a name
1626 tag, use its type memory tag. */
1627 var_ann_t v_ann;
1629 /* If we are emitting debugging dumps, display a warning if
1630 PTR is an SSA_NAME with no flow-sensitive alias
1631 information. That means that we may need to compute
1632 aliasing again. */
1633 if (dump_file
1634 && TREE_CODE (ptr) == SSA_NAME
1635 && pi == NULL)
1637 fprintf (dump_file,
1638 "NOTE: no flow-sensitive alias info for ");
1639 print_generic_expr (dump_file, ptr, dump_flags);
1640 fprintf (dump_file, " in ");
1641 print_generic_stmt (dump_file, stmt, dump_flags);
1644 if (TREE_CODE (ptr) == SSA_NAME)
1645 ptr = SSA_NAME_VAR (ptr);
1646 v_ann = var_ann (ptr);
1647 if (v_ann->type_mem_tag)
1648 add_stmt_operand (&v_ann->type_mem_tag, s_ann, flags);
1652 /* If a constant is used as a pointer, we can't generate a real
1653 operand for it but we mark the statement volatile to prevent
1654 optimizations from messing things up. */
1655 else if (TREE_CODE (ptr) == INTEGER_CST)
1657 if (s_ann)
1658 s_ann->has_volatile_ops = true;
1659 return;
1662 /* Everything else *should* have been folded elsewhere, but users
1663 are smarter than we in finding ways to write invalid code. We
1664 cannot just assert here. If we were absolutely certain that we
1665 do handle all valid cases, then we could just do nothing here.
1666 That seems optimistic, so attempt to do something logical... */
1667 else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1668 && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1669 && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1671 /* Make sure we know the object is addressable. */
1672 pptr = &TREE_OPERAND (ptr, 0);
1673 add_stmt_operand (pptr, s_ann, 0);
1675 /* Mark the object itself with a VUSE. */
1676 pptr = &TREE_OPERAND (*pptr, 0);
1677 get_expr_operands (stmt, pptr, flags);
1678 return;
1681 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1682 else
1683 gcc_unreachable ();
1685 /* Add a USE operand for the base pointer. */
1686 get_expr_operands (stmt, pptr, opf_none);
1689 /* A subroutine of get_expr_operands to handle TARGET_MEM_REF. */
1691 static void
1692 get_tmr_operands (tree stmt, tree expr, int flags)
1694 tree tag = TMR_TAG (expr);
1696 /* First record the real operands. */
1697 get_expr_operands (stmt, &TMR_BASE (expr), opf_none);
1698 get_expr_operands (stmt, &TMR_INDEX (expr), opf_none);
1700 /* MEM_REFs should never be killing. */
1701 flags &= ~opf_kill_def;
1703 if (TMR_SYMBOL (expr))
1705 stmt_ann_t ann = stmt_ann (stmt);
1706 add_to_addressable_set (TMR_SYMBOL (expr), &ann->addresses_taken);
1709 if (tag)
1710 get_expr_operands (stmt, &tag, flags);
1711 else
1712 /* Something weird, so ensure that we will be careful. */
1713 stmt_ann (stmt)->has_volatile_ops = true;
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' function never call-clobbers 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, get_callee_fndecl (expr));
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 && s_ann)
1778 add_to_addressable_set (TREE_OPERAND (var, 0), &s_ann->addresses_taken);
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 the variable cannot be modified and this is a V_MAY_DEF change
1802 it into a VUSE. This happens when read-only variables are marked
1803 call-clobbered and/or aliased to writable variables. So we only
1804 check that this only happens on non-specific stores.
1806 Note that if this is a specific store, i.e. associated with a
1807 modify_expr, then we can't suppress the V_DEF, lest we run into
1808 validation problems.
1810 This can happen when programs cast away const, leaving us with a
1811 store to read-only memory. If the statement is actually executed
1812 at runtime, then the program is ill formed. If the statement is
1813 not executed then all is well. At the very least, we cannot ICE. */
1814 if ((flags & opf_non_specific) && unmodifiable_var_p (var))
1816 gcc_assert (!is_real_op);
1817 flags &= ~(opf_is_def | opf_kill_def);
1820 if (is_real_op)
1822 /* The variable is a GIMPLE register. Add it to real operands. */
1823 if (flags & opf_is_def)
1824 append_def (var_p);
1825 else
1826 append_use (var_p);
1828 else
1830 varray_type aliases;
1832 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1833 virtual operands, unless the caller has specifically requested
1834 not to add virtual operands (used when adding operands inside an
1835 ADDR_EXPR expression). */
1836 if (flags & opf_no_vops)
1837 return;
1839 aliases = v_ann->may_aliases;
1841 if (aliases == NULL)
1843 /* The variable is not aliased or it is an alias tag. */
1844 if (flags & opf_is_def)
1846 if (flags & opf_kill_def)
1848 /* Only regular variables or struct fields may get a
1849 V_MUST_DEF operand. */
1850 gcc_assert (!MTAG_P (var)
1851 || TREE_CODE (var) == STRUCT_FIELD_TAG);
1852 /* V_MUST_DEF for non-aliased, non-GIMPLE register
1853 variable definitions. */
1854 append_v_must_def (var);
1856 else
1858 /* Add a V_MAY_DEF for call-clobbered variables and
1859 memory tags. */
1860 append_v_may_def (var);
1863 else
1865 append_vuse (var);
1866 if (s_ann && v_ann->is_alias_tag)
1867 s_ann->makes_aliased_loads = 1;
1870 else
1872 size_t i;
1874 /* The variable is aliased. Add its aliases to the virtual
1875 operands. */
1876 gcc_assert (VARRAY_ACTIVE_SIZE (aliases) != 0);
1878 if (flags & opf_is_def)
1880 /* If the variable is also an alias tag, add a virtual
1881 operand for it, otherwise we will miss representing
1882 references to the members of the variable's alias set.
1883 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1884 if (v_ann->is_alias_tag)
1885 append_v_may_def (var);
1887 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1888 append_v_may_def (VARRAY_TREE (aliases, i));
1890 if (s_ann)
1891 s_ann->makes_aliased_stores = 1;
1893 else
1895 /* Similarly, append a virtual uses for VAR itself, when
1896 it is an alias tag. */
1897 if (v_ann->is_alias_tag)
1898 append_vuse (var);
1900 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1901 append_vuse (VARRAY_TREE (aliases, i));
1903 if (s_ann)
1904 s_ann->makes_aliased_loads = 1;
1911 /* Add the base address of REF to the set *ADDRESSES_TAKEN. If
1912 *ADDRESSES_TAKEN is NULL, a new set is created. REF may be
1913 a single variable whose address has been taken or any other valid
1914 GIMPLE memory reference (structure reference, array, etc). If the
1915 base address of REF is a decl that has sub-variables, also add all
1916 of its sub-variables. */
1918 void
1919 add_to_addressable_set (tree ref, bitmap *addresses_taken)
1921 tree var;
1922 subvar_t svars;
1924 gcc_assert (addresses_taken);
1926 /* Note that it is *NOT OKAY* to use the target of a COMPONENT_REF
1927 as the only thing we take the address of. If VAR is a structure,
1928 taking the address of a field means that the whole structure may
1929 be referenced using pointer arithmetic. See PR 21407 and the
1930 ensuing mailing list discussion. */
1931 var = get_base_address (ref);
1932 if (var && SSA_VAR_P (var))
1934 if (*addresses_taken == NULL)
1935 *addresses_taken = BITMAP_GGC_ALLOC ();
1937 if (var_can_have_subvars (var)
1938 && (svars = get_subvars_for_var (var)))
1940 subvar_t sv;
1941 for (sv = svars; sv; sv = sv->next)
1943 bitmap_set_bit (*addresses_taken, DECL_UID (sv->var));
1944 TREE_ADDRESSABLE (sv->var) = 1;
1947 else
1949 bitmap_set_bit (*addresses_taken, DECL_UID (var));
1950 TREE_ADDRESSABLE (var) = 1;
1956 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1957 clobbered variables in the function. */
1959 static void
1960 add_call_clobber_ops (tree stmt, tree callee)
1962 int i;
1963 unsigned u;
1964 tree t;
1965 bitmap_iterator bi;
1966 stmt_ann_t s_ann = stmt_ann (stmt);
1967 struct stmt_ann_d empty_ann;
1968 bitmap not_read_b, not_written_b;
1970 /* Functions that are not const, pure or never return may clobber
1971 call-clobbered variables. */
1972 if (s_ann)
1973 s_ann->makes_clobbering_call = true;
1975 /* If we created .GLOBAL_VAR earlier, just use it. See compute_may_aliases
1976 for the heuristic used to decide whether to create .GLOBAL_VAR or not. */
1977 if (global_var)
1979 add_stmt_operand (&global_var, s_ann, opf_is_def);
1980 return;
1983 /* FIXME - if we have better information from the static vars
1984 analysis, we need to make the cache call site specific. This way
1985 we can have the performance benefits even if we are doing good
1986 optimization. */
1988 /* Get info for local and module level statics. There is a bit
1989 set for each static if the call being processed does not read
1990 or write that variable. */
1992 not_read_b = callee ? ipa_reference_get_not_read_global (callee) : NULL;
1993 not_written_b = callee ? ipa_reference_get_not_written_global (callee) : NULL;
1995 /* If cache is valid, copy the elements into the build vectors. */
1996 if (ssa_call_clobbered_cache_valid
1997 && (!not_read_b || bitmap_empty_p (not_read_b))
1998 && (!not_written_b || bitmap_empty_p (not_written_b)))
2000 /* Process the caches in reverse order so we are always inserting at
2001 the head of the list. */
2002 for (i = VEC_length (tree, clobbered_vuses) - 1; i >=0; i--)
2004 t = VEC_index (tree, clobbered_vuses, i);
2005 gcc_assert (TREE_CODE (t) != SSA_NAME);
2006 var_ann (t)->in_vuse_list = 1;
2007 opbuild_append_virtual (&build_vuses, t);
2009 for (i = VEC_length (tree, clobbered_v_may_defs) - 1; i >= 0; i--)
2011 t = VEC_index (tree, clobbered_v_may_defs, i);
2012 gcc_assert (TREE_CODE (t) != SSA_NAME);
2013 var_ann (t)->in_v_may_def_list = 1;
2014 opbuild_append_virtual (&build_v_may_defs, t);
2016 if (s_ann)
2018 s_ann->makes_aliased_loads = clobbered_aliased_loads;
2019 s_ann->makes_aliased_stores = clobbered_aliased_stores;
2021 return;
2024 memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
2026 /* Add a V_MAY_DEF operand for every call clobbered variable. */
2027 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
2029 tree var = referenced_var (u);
2030 gcc_assert (!unmodifiable_var_p (var));
2032 bool not_read
2033 = not_read_b ? bitmap_bit_p (not_read_b, u) : false;
2034 bool not_written
2035 = not_written_b ? bitmap_bit_p (not_written_b, u) : false;
2037 if (not_written)
2039 if (!not_read)
2040 add_stmt_operand (&var, &empty_ann, opf_none);
2042 else
2043 add_stmt_operand (&var, &empty_ann, opf_is_def);
2047 if ((!not_read_b || bitmap_empty_p (not_read_b))
2048 && (!not_written_b || bitmap_empty_p (not_written_b)))
2050 clobbered_aliased_loads = empty_ann.makes_aliased_loads;
2051 clobbered_aliased_stores = empty_ann.makes_aliased_stores;
2053 /* Set the flags for a stmt's annotation. */
2054 if (s_ann)
2056 s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
2057 s_ann->makes_aliased_stores = empty_ann.makes_aliased_stores;
2060 /* Prepare empty cache vectors. */
2061 VEC_truncate (tree, clobbered_vuses, 0);
2062 VEC_truncate (tree, clobbered_v_may_defs, 0);
2064 /* Now fill the clobbered cache with the values that have been found. */
2065 for (i = opbuild_first (&build_vuses);
2066 i != OPBUILD_LAST;
2067 i = opbuild_next (&build_vuses, i))
2068 VEC_safe_push (tree, heap, clobbered_vuses,
2069 opbuild_elem_virtual (&build_vuses, i));
2071 gcc_assert (opbuild_num_elems (&build_vuses)
2072 == VEC_length (tree, clobbered_vuses));
2074 for (i = opbuild_first (&build_v_may_defs);
2075 i != OPBUILD_LAST;
2076 i = opbuild_next (&build_v_may_defs, i))
2077 VEC_safe_push (tree, heap, clobbered_v_may_defs,
2078 opbuild_elem_virtual (&build_v_may_defs, i));
2080 gcc_assert (opbuild_num_elems (&build_v_may_defs)
2081 == VEC_length (tree, clobbered_v_may_defs));
2083 ssa_call_clobbered_cache_valid = true;
2088 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
2089 function. */
2091 static void
2092 add_call_read_ops (tree stmt)
2094 int i;
2095 unsigned u;
2096 tree t;
2097 bitmap_iterator bi;
2098 stmt_ann_t s_ann = stmt_ann (stmt);
2099 struct stmt_ann_d empty_ann;
2101 /* if the function is not pure, it may reference memory. Add
2102 a VUSE for .GLOBAL_VAR if it has been created. See add_referenced_var
2103 for the heuristic used to decide whether to create .GLOBAL_VAR. */
2104 if (global_var)
2106 add_stmt_operand (&global_var, s_ann, opf_none);
2107 return;
2110 /* If cache is valid, copy the elements into the build vector. */
2111 if (ssa_ro_call_cache_valid)
2113 for (i = VEC_length (tree, ro_call_vuses) - 1; i >=0 ; i--)
2115 /* Process the caches in reverse order so we are always inserting at
2116 the head of the list. */
2117 t = VEC_index (tree, ro_call_vuses, i);
2118 gcc_assert (TREE_CODE (t) != SSA_NAME);
2119 var_ann (t)->in_vuse_list = 1;
2120 opbuild_append_virtual (&build_vuses, t);
2122 if (s_ann)
2123 s_ann->makes_aliased_loads = ro_call_aliased_loads;
2124 return;
2127 memset (&empty_ann, 0, sizeof (struct stmt_ann_d));
2129 /* Add a VUSE for each call-clobbered variable. */
2130 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, u, bi)
2132 tree var = referenced_var (u);
2133 add_stmt_operand (&var, &empty_ann, opf_none | opf_non_specific);
2136 ro_call_aliased_loads = empty_ann.makes_aliased_loads;
2137 if (s_ann)
2138 s_ann->makes_aliased_loads = empty_ann.makes_aliased_loads;
2140 /* Prepare empty cache vectors. */
2141 VEC_truncate (tree, ro_call_vuses, 0);
2143 /* Now fill the clobbered cache with the values that have been found. */
2144 for (i = opbuild_first (&build_vuses);
2145 i != OPBUILD_LAST;
2146 i = opbuild_next (&build_vuses, i))
2147 VEC_safe_push (tree, heap, ro_call_vuses,
2148 opbuild_elem_virtual (&build_vuses, i));
2150 gcc_assert (opbuild_num_elems (&build_vuses)
2151 == VEC_length (tree, ro_call_vuses));
2153 ssa_ro_call_cache_valid = true;
2157 /* Scan the immediate_use list for VAR making sure its linked properly.
2158 return RTUE iof there is a problem. */
2160 bool
2161 verify_imm_links (FILE *f, tree var)
2163 use_operand_p ptr, prev, list;
2164 int count;
2166 gcc_assert (TREE_CODE (var) == SSA_NAME);
2168 list = &(SSA_NAME_IMM_USE_NODE (var));
2169 gcc_assert (list->use == NULL);
2171 if (list->prev == NULL)
2173 gcc_assert (list->next == NULL);
2174 return false;
2177 prev = list;
2178 count = 0;
2179 for (ptr = list->next; ptr != list; )
2181 if (prev != ptr->prev)
2182 goto error;
2184 if (ptr->use == NULL)
2185 goto error; /* 2 roots, or SAFE guard node. */
2186 else if (*(ptr->use) != var)
2187 goto error;
2189 prev = ptr;
2190 ptr = ptr->next;
2191 /* Avoid infinite loops. */
2192 if (count++ > 30000)
2193 goto error;
2196 /* Verify list in the other direction. */
2197 prev = list;
2198 for (ptr = list->prev; ptr != list; )
2200 if (prev != ptr->next)
2201 goto error;
2202 prev = ptr;
2203 ptr = ptr->prev;
2204 if (count-- < 0)
2205 goto error;
2208 if (count != 0)
2209 goto error;
2211 return false;
2213 error:
2214 if (ptr->stmt && stmt_modified_p (ptr->stmt))
2216 fprintf (f, " STMT MODIFIED. - <%p> ", (void *)ptr->stmt);
2217 print_generic_stmt (f, ptr->stmt, TDF_SLIM);
2219 fprintf (f, " IMM ERROR : (use_p : tree - %p:%p)", (void *)ptr,
2220 (void *)ptr->use);
2221 print_generic_expr (f, USE_FROM_PTR (ptr), TDF_SLIM);
2222 fprintf(f, "\n");
2223 return true;
2227 /* Dump all the immediate uses to FILE. */
2229 void
2230 dump_immediate_uses_for (FILE *file, tree var)
2232 imm_use_iterator iter;
2233 use_operand_p use_p;
2235 gcc_assert (var && TREE_CODE (var) == SSA_NAME);
2237 print_generic_expr (file, var, TDF_SLIM);
2238 fprintf (file, " : -->");
2239 if (has_zero_uses (var))
2240 fprintf (file, " no uses.\n");
2241 else
2242 if (has_single_use (var))
2243 fprintf (file, " single use.\n");
2244 else
2245 fprintf (file, "%d uses.\n", num_imm_uses (var));
2247 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
2249 if (!is_gimple_reg (USE_FROM_PTR (use_p)))
2250 print_generic_stmt (file, USE_STMT (use_p), TDF_VOPS);
2251 else
2252 print_generic_stmt (file, USE_STMT (use_p), TDF_SLIM);
2254 fprintf(file, "\n");
2257 /* Dump all the immediate uses to FILE. */
2259 void
2260 dump_immediate_uses (FILE *file)
2262 tree var;
2263 unsigned int x;
2265 fprintf (file, "Immediate_uses: \n\n");
2266 for (x = 1; x < num_ssa_names; x++)
2268 var = ssa_name(x);
2269 if (!var)
2270 continue;
2271 dump_immediate_uses_for (file, var);
2276 /* Dump def-use edges on stderr. */
2278 void
2279 debug_immediate_uses (void)
2281 dump_immediate_uses (stderr);
2284 /* Dump def-use edges on stderr. */
2286 void
2287 debug_immediate_uses_for (tree var)
2289 dump_immediate_uses_for (stderr, var);
2291 #include "gt-tree-ssa-operands.h"