* configure.ac: Don't test for [build] __cxa_atexit when building a
[official-gcc.git] / gcc / tree-ssa-operands.c
blob953ef8d71b7ecd1f00a09564bcc84bf9e3e98374
1 /* SSA operands management for trees.
2 Copyright (C) 2003, 2004 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;
120 #ifdef ENABLE_CHECKING
121 /* Used to make sure operand construction is working on the proper stmt. */
122 tree check_build_stmt;
123 #endif
125 def_operand_p NULL_DEF_OPERAND_P = { NULL };
126 use_operand_p NULL_USE_OPERAND_P = { NULL };
128 static void note_addressable (tree, stmt_ann_t);
129 static void get_expr_operands (tree, tree *, int);
130 static void get_asm_expr_operands (tree);
131 static void get_indirect_ref_operands (tree, tree, int);
132 static void get_call_expr_operands (tree, tree);
133 static inline void append_def (tree *);
134 static inline void append_use (tree *);
135 static void append_v_may_def (tree);
136 static void append_v_must_def (tree);
137 static void add_call_clobber_ops (tree);
138 static void add_call_read_ops (tree);
139 static void add_stmt_operand (tree *, tree, int);
141 /* Return a vector of contiguous memory for NUM def operands. */
143 static inline def_optype
144 allocate_def_optype (unsigned num)
146 def_optype def_ops;
147 unsigned size;
148 size = sizeof (struct def_optype_d) + sizeof (tree *) * (num - 1);
149 def_ops = ggc_alloc (size);
150 def_ops->num_defs = num;
151 return def_ops;
155 /* Return a vector of contiguous memory for NUM use operands. */
157 static inline use_optype
158 allocate_use_optype (unsigned num)
160 use_optype use_ops;
161 unsigned size;
162 size = sizeof (struct use_optype_d) + sizeof (tree *) * (num - 1);
163 use_ops = ggc_alloc (size);
164 use_ops->num_uses = num;
165 return use_ops;
169 /* Return a vector of contiguous memory for NUM v_may_def operands. */
171 static inline v_may_def_optype
172 allocate_v_may_def_optype (unsigned num)
174 v_may_def_optype v_may_def_ops;
175 unsigned size;
176 size = sizeof (struct v_may_def_optype_d)
177 + sizeof (v_may_def_operand_type_t) * (num - 1);
178 v_may_def_ops = ggc_alloc (size);
179 v_may_def_ops->num_v_may_defs = num;
180 return v_may_def_ops;
184 /* Return a vector of contiguous memory for NUM v_use operands. */
186 static inline vuse_optype
187 allocate_vuse_optype (unsigned num)
189 vuse_optype vuse_ops;
190 unsigned size;
191 size = sizeof (struct vuse_optype_d) + sizeof (tree) * (num - 1);
192 vuse_ops = ggc_alloc (size);
193 vuse_ops->num_vuses = num;
194 return vuse_ops;
198 /* Return a vector of contiguous memory for NUM v_must_def operands. */
200 static inline v_must_def_optype
201 allocate_v_must_def_optype (unsigned num)
203 v_must_def_optype v_must_def_ops;
204 unsigned size;
205 size = sizeof (struct v_must_def_optype_d) + sizeof (tree) * (num - 1);
206 v_must_def_ops = ggc_alloc (size);
207 v_must_def_ops->num_v_must_defs = num;
208 return v_must_def_ops;
212 /* Free memory for USES. */
214 static inline void
215 free_uses (use_optype *uses)
217 if (*uses)
219 ggc_free (*uses);
220 *uses = NULL;
225 /* Free memory for DEFS. */
227 static inline void
228 free_defs (def_optype *defs)
230 if (*defs)
232 ggc_free (*defs);
233 *defs = NULL;
238 /* Free memory for VUSES. */
240 static inline void
241 free_vuses (vuse_optype *vuses)
243 if (*vuses)
245 ggc_free (*vuses);
246 *vuses = NULL;
251 /* Free memory for V_MAY_DEFS. */
253 static inline void
254 free_v_may_defs (v_may_def_optype *v_may_defs)
256 if (*v_may_defs)
258 ggc_free (*v_may_defs);
259 *v_may_defs = NULL;
264 /* Free memory for V_MUST_DEFS. */
266 static inline void
267 free_v_must_defs (v_must_def_optype *v_must_defs)
269 if (*v_must_defs)
271 ggc_free (*v_must_defs);
272 *v_must_defs = NULL;
277 /* Initialize the operand cache routines. */
279 void
280 init_ssa_operands (void)
282 VARRAY_TREE_PTR_INIT (build_defs, 5, "build defs");
283 VARRAY_TREE_PTR_INIT (build_uses, 10, "build uses");
284 VARRAY_TREE_INIT (build_v_may_defs, 10, "build v_may_defs");
285 VARRAY_TREE_INIT (build_vuses, 10, "build vuses");
286 VARRAY_TREE_INIT (build_v_must_defs, 10, "build v_must_defs");
290 /* Dispose of anything required by the operand routines. */
292 void
293 fini_ssa_operands (void)
295 ggc_free (build_defs);
296 ggc_free (build_uses);
297 ggc_free (build_v_may_defs);
298 ggc_free (build_vuses);
299 ggc_free (build_v_must_defs);
300 build_defs = NULL;
301 build_uses = NULL;
302 build_v_may_defs = NULL;
303 build_vuses = NULL;
304 build_v_must_defs = NULL;
308 /* All the finalize_ssa_* routines do the work required to turn the build_
309 VARRAY into an operand_vector of the appropriate type. The original vector,
310 if any, is passed in for comparison and virtual SSA_NAME reuse. If the
311 old vector is reused, the pointer passed in is set to NULL so that
312 the memory is not freed when the old operands are freed. */
314 /* Return a new def operand vector for STMT, comparing to OLD_OPS_P. */
316 static def_optype
317 finalize_ssa_defs (def_optype *old_ops_p, tree stmt ATTRIBUTE_UNUSED)
319 unsigned num, x;
320 def_optype def_ops, old_ops;
321 bool build_diff;
323 num = VARRAY_ACTIVE_SIZE (build_defs);
324 if (num == 0)
325 return NULL;
327 /* There should only be a single real definition per assignment. */
328 gcc_assert (TREE_CODE (stmt) != MODIFY_EXPR || num <= 1);
330 old_ops = *old_ops_p;
332 /* Compare old vector and new array. */
333 build_diff = true;
334 if (old_ops && old_ops->num_defs == num)
336 build_diff = false;
337 for (x = 0; x < num; x++)
338 if (old_ops->defs[x].def != VARRAY_TREE_PTR (build_defs, x))
340 build_diff = true;
341 break;
345 if (!build_diff)
347 def_ops = old_ops;
348 *old_ops_p = NULL;
350 else
352 def_ops = allocate_def_optype (num);
353 for (x = 0; x < num ; x++)
354 def_ops->defs[x].def = VARRAY_TREE_PTR (build_defs, x);
357 VARRAY_POP_ALL (build_defs);
359 return def_ops;
363 /* Return a new use operand vector for STMT, comparing to OLD_OPS_P. */
365 static use_optype
366 finalize_ssa_uses (use_optype *old_ops_p, tree stmt ATTRIBUTE_UNUSED)
368 unsigned num, x;
369 use_optype use_ops, old_ops;
370 bool build_diff;
372 num = VARRAY_ACTIVE_SIZE (build_uses);
373 if (num == 0)
374 return NULL;
376 #ifdef ENABLE_CHECKING
378 unsigned x;
379 /* If the pointer to the operand is the statement itself, something is
380 wrong. It means that we are pointing to a local variable (the
381 initial call to get_stmt_operands does not pass a pointer to a
382 statement). */
383 for (x = 0; x < num; x++)
384 gcc_assert (*(VARRAY_TREE_PTR (build_uses, x)) != stmt);
386 #endif
387 old_ops = *old_ops_p;
389 /* Check if the old vector and the new array are the same. */
390 build_diff = true;
391 if (old_ops && old_ops->num_uses == num)
393 build_diff = false;
394 for (x = 0; x < num; x++)
395 if (old_ops->uses[x].use != VARRAY_TREE_PTR (build_uses, x))
397 build_diff = true;
398 break;
402 if (!build_diff)
404 use_ops = old_ops;
405 *old_ops_p = NULL;
407 else
409 use_ops = allocate_use_optype (num);
410 for (x = 0; x < num ; x++)
411 use_ops->uses[x].use = VARRAY_TREE_PTR (build_uses, x);
413 VARRAY_POP_ALL (build_uses);
415 return use_ops;
419 /* Return a new v_may_def operand vector for STMT, comparing to OLD_OPS_P. */
421 static v_may_def_optype
422 finalize_ssa_v_may_defs (v_may_def_optype *old_ops_p)
424 unsigned num, x, i, old_num;
425 v_may_def_optype v_may_def_ops, old_ops;
426 tree result, var;
427 bool build_diff;
429 num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
430 if (num == 0)
431 return NULL;
433 old_ops = *old_ops_p;
435 /* Check if the old vector and the new array are the same. */
436 build_diff = true;
437 if (old_ops && old_ops->num_v_may_defs == num)
439 old_num = num;
440 build_diff = false;
441 for (x = 0; x < num; x++)
443 var = old_ops->v_may_defs[x].def;
444 if (TREE_CODE (var) == SSA_NAME)
445 var = SSA_NAME_VAR (var);
446 if (var != VARRAY_TREE (build_v_may_defs, x))
448 build_diff = true;
449 break;
453 else
454 old_num = (old_ops ? old_ops->num_v_may_defs : 0);
456 if (!build_diff)
458 v_may_def_ops = old_ops;
459 *old_ops_p = NULL;
461 else
463 v_may_def_ops = allocate_v_may_def_optype (num);
464 for (x = 0; x < num; x++)
466 var = VARRAY_TREE (build_v_may_defs, x);
467 /* Look for VAR in the old operands vector. */
468 for (i = 0; i < old_num; i++)
470 result = old_ops->v_may_defs[i].def;
471 if (TREE_CODE (result) == SSA_NAME)
472 result = SSA_NAME_VAR (result);
473 if (result == var)
475 v_may_def_ops->v_may_defs[x] = old_ops->v_may_defs[i];
476 break;
479 if (i == old_num)
481 v_may_def_ops->v_may_defs[x].def = var;
482 v_may_def_ops->v_may_defs[x].use = var;
487 /* Empty the V_MAY_DEF build vector after VUSES have been processed. */
489 return v_may_def_ops;
493 /* Return a new vuse operand vector, comparing to OLD_OPS_P. */
495 static vuse_optype
496 finalize_ssa_vuses (vuse_optype *old_ops_p)
498 unsigned num, x, i, num_v_may_defs, old_num;
499 vuse_optype vuse_ops, old_ops;
500 bool build_diff;
502 num = VARRAY_ACTIVE_SIZE (build_vuses);
503 if (num == 0)
505 VARRAY_POP_ALL (build_v_may_defs);
506 return NULL;
509 /* Remove superfluous VUSE operands. If the statement already has a
510 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
511 needed because V_MAY_DEFs imply a VUSE of the variable. For instance,
512 suppose that variable 'a' is aliased:
514 # VUSE <a_2>
515 # a_3 = V_MAY_DEF <a_2>
516 a = a + 1;
518 The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
519 operation. */
521 num_v_may_defs = VARRAY_ACTIVE_SIZE (build_v_may_defs);
523 if (num_v_may_defs > 0)
525 size_t i, j;
526 tree vuse;
527 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
529 vuse = VARRAY_TREE (build_vuses, i);
530 for (j = 0; j < num_v_may_defs; j++)
532 if (vuse == VARRAY_TREE (build_v_may_defs, j))
533 break;
536 /* If we found a useless VUSE operand, remove it from the
537 operand array by replacing it with the last active element
538 in the operand array (unless the useless VUSE was the
539 last operand, in which case we simply remove it. */
540 if (j != num_v_may_defs)
542 if (i != VARRAY_ACTIVE_SIZE (build_vuses) - 1)
544 VARRAY_TREE (build_vuses, i)
545 = VARRAY_TREE (build_vuses,
546 VARRAY_ACTIVE_SIZE (build_vuses) - 1);
548 VARRAY_POP (build_vuses);
550 /* We want to rescan the element at this index, unless
551 this was the last element, in which case the loop
552 terminates. */
553 i--;
558 num = VARRAY_ACTIVE_SIZE (build_vuses);
559 /* We could have reduced the size to zero now, however. */
560 if (num == 0)
562 VARRAY_POP_ALL (build_v_may_defs);
563 return NULL;
566 old_ops = *old_ops_p;
568 /* Determine whether vuses is the same as the old vector. */
569 build_diff = true;
570 if (old_ops && old_ops->num_vuses == num)
572 old_num = num;
573 build_diff = false;
574 for (x = 0; x < num ; x++)
576 tree v;
577 v = old_ops->vuses[x];
578 if (TREE_CODE (v) == SSA_NAME)
579 v = SSA_NAME_VAR (v);
580 if (v != VARRAY_TREE (build_vuses, x))
582 build_diff = true;
583 break;
587 else
588 old_num = (old_ops ? old_ops->num_vuses : 0);
590 if (!build_diff)
592 vuse_ops = old_ops;
593 *old_ops_p = NULL;
595 else
597 vuse_ops = allocate_vuse_optype (num);
598 for (x = 0; x < num; x++)
600 tree result, var = VARRAY_TREE (build_vuses, x);
601 /* Look for VAR in the old vector, and use that SSA_NAME. */
602 for (i = 0; i < old_num; i++)
604 result = old_ops->vuses[i];
605 if (TREE_CODE (result) == SSA_NAME)
606 result = SSA_NAME_VAR (result);
607 if (result == var)
609 vuse_ops->vuses[x] = old_ops->vuses[i];
610 break;
613 if (i == old_num)
614 vuse_ops->vuses[x] = var;
618 /* The v_may_def build vector wasn't freed because we needed it here.
619 Free it now with the vuses build vector. */
620 VARRAY_POP_ALL (build_vuses);
621 VARRAY_POP_ALL (build_v_may_defs);
623 return vuse_ops;
626 /* Return a new v_must_def operand vector for STMT, comparing to OLD_OPS_P. */
628 static v_must_def_optype
629 finalize_ssa_v_must_defs (v_must_def_optype *old_ops_p,
630 tree stmt ATTRIBUTE_UNUSED)
632 unsigned num, x, i, old_num = 0;
633 v_must_def_optype v_must_def_ops, old_ops;
634 bool build_diff;
636 num = VARRAY_ACTIVE_SIZE (build_v_must_defs);
637 if (num == 0)
638 return NULL;
640 /* There should only be a single V_MUST_DEF per assignment. */
641 gcc_assert (TREE_CODE (stmt) != MODIFY_EXPR || num <= 1);
643 old_ops = *old_ops_p;
645 /* Check if the old vector and the new array are the same. */
646 build_diff = true;
647 if (old_ops && old_ops->num_v_must_defs == num)
649 old_num = num;
650 build_diff = false;
651 for (x = 0; x < num; x++)
653 tree var = old_ops->v_must_defs[x];
654 if (TREE_CODE (var) == SSA_NAME)
655 var = SSA_NAME_VAR (var);
656 if (var != VARRAY_TREE (build_v_must_defs, x))
658 build_diff = true;
659 break;
663 else
664 old_num = (old_ops ? old_ops->num_v_must_defs : 0);
666 if (!build_diff)
668 v_must_def_ops = old_ops;
669 *old_ops_p = NULL;
671 else
673 v_must_def_ops = allocate_v_must_def_optype (num);
674 for (x = 0; x < num ; x++)
676 tree result, var = VARRAY_TREE (build_v_must_defs, x);
677 /* Look for VAR in the original vector. */
678 for (i = 0; i < old_num; i++)
680 result = old_ops->v_must_defs[i];
681 if (TREE_CODE (result) == SSA_NAME)
682 result = SSA_NAME_VAR (result);
683 if (result == var)
685 v_must_def_ops->v_must_defs[x] = old_ops->v_must_defs[i];
686 break;
689 if (i == old_num)
690 v_must_def_ops->v_must_defs[x] = var;
693 VARRAY_POP_ALL (build_v_must_defs);
695 return v_must_def_ops;
699 /* Finalize all the build vectors, fill the new ones into INFO. */
701 static inline void
702 finalize_ssa_stmt_operands (tree stmt, stmt_operands_p old_ops,
703 stmt_operands_p new_ops)
705 new_ops->def_ops = finalize_ssa_defs (&(old_ops->def_ops), stmt);
706 new_ops->use_ops = finalize_ssa_uses (&(old_ops->use_ops), stmt);
707 new_ops->v_must_def_ops
708 = finalize_ssa_v_must_defs (&(old_ops->v_must_def_ops), stmt);
709 new_ops->v_may_def_ops = finalize_ssa_v_may_defs (&(old_ops->v_may_def_ops));
710 new_ops->vuse_ops = finalize_ssa_vuses (&(old_ops->vuse_ops));
714 /* Start the process of building up operands vectors in INFO. */
716 static inline void
717 start_ssa_stmt_operands (void)
719 gcc_assert (VARRAY_ACTIVE_SIZE (build_defs) == 0);
720 gcc_assert (VARRAY_ACTIVE_SIZE (build_uses) == 0);
721 gcc_assert (VARRAY_ACTIVE_SIZE (build_vuses) == 0);
722 gcc_assert (VARRAY_ACTIVE_SIZE (build_v_may_defs) == 0);
723 gcc_assert (VARRAY_ACTIVE_SIZE (build_v_must_defs) == 0);
727 /* Add DEF_P to the list of pointers to operands. */
729 static inline void
730 append_def (tree *def_p)
732 VARRAY_PUSH_TREE_PTR (build_defs, def_p);
736 /* Add USE_P to the list of pointers to operands. */
738 static inline void
739 append_use (tree *use_p)
741 VARRAY_PUSH_TREE_PTR (build_uses, use_p);
745 /* Add a new virtual may def for variable VAR to the build array. */
747 static inline void
748 append_v_may_def (tree var)
750 unsigned i;
752 /* Don't allow duplicate entries. */
753 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_may_defs); i++)
754 if (var == VARRAY_TREE (build_v_may_defs, i))
755 return;
757 VARRAY_PUSH_TREE (build_v_may_defs, var);
761 /* Add VAR to the list of virtual uses. */
763 static inline void
764 append_vuse (tree var)
766 size_t i;
768 /* Don't allow duplicate entries. */
769 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
770 if (var == VARRAY_TREE (build_vuses, i))
771 return;
773 VARRAY_PUSH_TREE (build_vuses, var);
777 /* Add VAR to the list of virtual must definitions for INFO. */
779 static inline void
780 append_v_must_def (tree var)
782 unsigned i;
784 /* Don't allow duplicate entries. */
785 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_must_defs); i++)
786 if (var == VARRAY_TREE (build_v_must_defs, i))
787 return;
789 VARRAY_PUSH_TREE (build_v_must_defs, var);
792 /* Create an operands cache for STMT, returning it in NEW_OPS. OLD_OPS are the
793 original operands, and if ANN is non-null, appropriate stmt flags are set
794 in the stmt's annotation. Note that some fields in old_ops may
795 change to NULL, although none of the memory they originally pointed to
796 will be destroyed. It is appropriate to call free_stmt_operands() on
797 the value returned in old_ops.
799 The rationale for this: Certain optimizations wish to examine the difference
800 between new_ops and old_ops after processing. If a set of operands don't
801 change, new_ops will simply assume the pointer in old_ops, and the old_ops
802 pointer will be set to NULL, indicating no memory needs to be cleared.
803 Usage might appear something like:
805 old_ops_copy = old_ops = stmt_ann(stmt)->operands;
806 build_ssa_operands (stmt, NULL, &old_ops, &new_ops);
807 <* compare old_ops_copy and new_ops *>
808 free_ssa_operands (old_ops); */
810 void
811 build_ssa_operands (tree stmt, stmt_ann_t ann, stmt_operands_p old_ops,
812 stmt_operands_p new_ops)
814 enum tree_code code;
815 tree_ann_t saved_ann = stmt->common.ann;
817 /* Replace stmt's annotation with the one passed in for the duration
818 of the operand building process. This allows "fake" stmts to be built
819 and not be included in other data structures which can be built here. */
820 stmt->common.ann = (tree_ann_t) ann;
822 /* Initially assume that the statement has no volatile operands, nor
823 makes aliased loads or stores. */
824 if (ann)
826 ann->has_volatile_ops = false;
827 ann->makes_aliased_stores = false;
828 ann->makes_aliased_loads = false;
831 start_ssa_stmt_operands ();
833 code = TREE_CODE (stmt);
834 switch (code)
836 case MODIFY_EXPR:
837 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none);
838 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF
839 || TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_RANGE_REF
840 || TREE_CODE (TREE_OPERAND (stmt, 0)) == COMPONENT_REF
841 || TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
842 || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR
843 /* Use a V_MAY_DEF if the RHS might throw, as the LHS won't be
844 modified in that case. FIXME we should represent somehow
845 that it is killed on the fallthrough path. */
846 || tree_could_throw_p (TREE_OPERAND (stmt, 1)))
847 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_is_def);
848 else
849 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0),
850 opf_is_def | opf_kill_def);
851 break;
853 case COND_EXPR:
854 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none);
855 break;
857 case SWITCH_EXPR:
858 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none);
859 break;
861 case ASM_EXPR:
862 get_asm_expr_operands (stmt);
863 break;
865 case RETURN_EXPR:
866 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none);
867 break;
869 case GOTO_EXPR:
870 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none);
871 break;
873 case LABEL_EXPR:
874 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none);
875 break;
877 /* These nodes contain no variable references. */
878 case BIND_EXPR:
879 case CASE_LABEL_EXPR:
880 case TRY_CATCH_EXPR:
881 case TRY_FINALLY_EXPR:
882 case EH_FILTER_EXPR:
883 case CATCH_EXPR:
884 case RESX_EXPR:
885 break;
887 default:
888 /* Notice that if get_expr_operands tries to use &STMT as the operand
889 pointer (which may only happen for USE operands), we will abort in
890 append_use. This default will handle statements like empty
891 statements, or CALL_EXPRs that may appear on the RHS of a statement
892 or as statements themselves. */
893 get_expr_operands (stmt, &stmt, opf_none);
894 break;
897 finalize_ssa_stmt_operands (stmt, old_ops, new_ops);
898 stmt->common.ann = saved_ann;
902 /* Free any operands vectors in OPS. */
904 static void
905 free_ssa_operands (stmt_operands_p ops)
907 if (ops->def_ops)
908 free_defs (&(ops->def_ops));
909 if (ops->use_ops)
910 free_uses (&(ops->use_ops));
911 if (ops->vuse_ops)
912 free_vuses (&(ops->vuse_ops));
913 if (ops->v_may_def_ops)
914 free_v_may_defs (&(ops->v_may_def_ops));
915 if (ops->v_must_def_ops)
916 free_v_must_defs (&(ops->v_must_def_ops));
920 /* Get the operands of statement STMT. Note that repeated calls to
921 get_stmt_operands for the same statement will do nothing until the
922 statement is marked modified by a call to modify_stmt(). */
924 void
925 get_stmt_operands (tree stmt)
927 stmt_ann_t ann;
928 stmt_operands_t old_operands;
930 /* The optimizers cannot handle statements that are nothing but a
931 _DECL. This indicates a bug in the gimplifier. */
932 gcc_assert (!SSA_VAR_P (stmt));
934 /* Ignore error statements. */
935 if (TREE_CODE (stmt) == ERROR_MARK)
936 return;
938 ann = get_stmt_ann (stmt);
940 /* If the statement has not been modified, the operands are still valid. */
941 if (!ann->modified)
942 return;
944 timevar_push (TV_TREE_OPS);
946 old_operands = ann->operands;
947 memset (&(ann->operands), 0, sizeof (stmt_operands_t));
949 build_ssa_operands (stmt, ann, &old_operands, &(ann->operands));
950 free_ssa_operands (&old_operands);
952 /* Clear the modified bit for STMT. Subsequent calls to
953 get_stmt_operands for this statement will do nothing until the
954 statement is marked modified by a call to modify_stmt(). */
955 ann->modified = 0;
957 timevar_pop (TV_TREE_OPS);
961 /* Recursively scan the expression pointed by EXPR_P in statement referred to
962 by INFO. FLAGS is one of the OPF_* constants modifying how to interpret the
963 operands found. */
965 static void
966 get_expr_operands (tree stmt, tree *expr_p, int flags)
968 enum tree_code code;
969 enum tree_code_class class;
970 tree expr = *expr_p;
972 if (expr == NULL || expr == error_mark_node)
973 return;
975 code = TREE_CODE (expr);
976 class = TREE_CODE_CLASS (code);
978 switch (code)
980 case ADDR_EXPR:
981 /* We could have the address of a component, array member,
982 etc which has interesting variable references. */
983 /* Taking the address of a variable does not represent a
984 reference to it, but the fact that the stmt takes its address will be
985 of interest to some passes (e.g. alias resolution). */
986 add_stmt_operand (expr_p, stmt, 0);
988 /* If the address is invariant, there may be no interesting variable
989 references inside. */
990 if (is_gimple_min_invariant (expr))
991 return;
993 /* There should be no VUSEs created, since the referenced objects are
994 not really accessed. The only operands that we should find here
995 are ARRAY_REF indices which will always be real operands (GIMPLE
996 does not allow non-registers as array indices). */
997 flags |= opf_no_vops;
999 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1000 return;
1002 case SSA_NAME:
1003 case VAR_DECL:
1004 case PARM_DECL:
1005 case RESULT_DECL:
1006 case CONST_DECL:
1007 /* If we found a variable, add it to DEFS or USES depending
1008 on the operand flags. */
1009 add_stmt_operand (expr_p, stmt, flags);
1010 return;
1012 case MISALIGNED_INDIRECT_REF:
1013 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1014 /* fall through */
1016 case ALIGN_INDIRECT_REF:
1017 case INDIRECT_REF:
1018 get_indirect_ref_operands (stmt, expr, flags);
1019 return;
1021 case ARRAY_REF:
1022 case ARRAY_RANGE_REF:
1023 /* Treat array references as references to the virtual variable
1024 representing the array. The virtual variable for an ARRAY_REF
1025 is the VAR_DECL for the array. */
1027 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1028 according to the value of IS_DEF. Recurse if the LHS of the
1029 ARRAY_REF node is not a regular variable. */
1030 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1031 add_stmt_operand (expr_p, stmt, flags);
1032 else
1033 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1035 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1036 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1037 get_expr_operands (stmt, &TREE_OPERAND (expr, 3), opf_none);
1038 return;
1040 case COMPONENT_REF:
1041 case REALPART_EXPR:
1042 case IMAGPART_EXPR:
1043 /* Similarly to arrays, references to compound variables (complex
1044 types and structures/unions) are globbed.
1046 FIXME: This means that
1048 a.x = 6;
1049 a.y = 7;
1050 foo (a.x, a.y);
1052 will not be constant propagated because the two partial
1053 definitions to 'a' will kill each other. Note that SRA may be
1054 able to fix this problem if 'a' can be scalarized. */
1056 /* If the LHS of the compound reference is not a regular variable,
1057 recurse to keep looking for more operands in the subexpression. */
1058 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1059 add_stmt_operand (expr_p, stmt, flags);
1060 else
1061 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1063 if (code == COMPONENT_REF)
1064 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1065 return;
1067 case WITH_SIZE_EXPR:
1068 /* WITH_SIZE_EXPR is a pass-through reference to its first argument,
1069 and an rvalue reference to its second argument. */
1070 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1071 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1072 return;
1074 case CALL_EXPR:
1075 get_call_expr_operands (stmt, expr);
1076 return;
1078 case COND_EXPR:
1079 case VEC_COND_EXPR:
1080 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1081 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1082 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1083 return;
1085 case MODIFY_EXPR:
1087 int subflags;
1088 tree op;
1090 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none);
1092 op = TREE_OPERAND (expr, 0);
1093 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1094 op = TREE_OPERAND (expr, 0);
1095 if (TREE_CODE (op) == ARRAY_REF
1096 || TREE_CODE (op) == ARRAY_RANGE_REF
1097 || TREE_CODE (op) == COMPONENT_REF
1098 || TREE_CODE (op) == REALPART_EXPR
1099 || TREE_CODE (op) == IMAGPART_EXPR)
1100 subflags = opf_is_def;
1101 else
1102 subflags = opf_is_def | opf_kill_def;
1104 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), subflags);
1105 return;
1108 case CONSTRUCTOR:
1110 /* General aggregate CONSTRUCTORs have been decomposed, but they
1111 are still in use as the COMPLEX_EXPR equivalent for vectors. */
1113 tree t;
1114 for (t = TREE_OPERAND (expr, 0); t ; t = TREE_CHAIN (t))
1115 get_expr_operands (stmt, &TREE_VALUE (t), opf_none);
1117 return;
1120 case TRUTH_NOT_EXPR:
1121 case BIT_FIELD_REF:
1122 case VIEW_CONVERT_EXPR:
1123 do_unary:
1124 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1125 return;
1127 case TRUTH_AND_EXPR:
1128 case TRUTH_OR_EXPR:
1129 case TRUTH_XOR_EXPR:
1130 case COMPOUND_EXPR:
1131 case OBJ_TYPE_REF:
1132 do_binary:
1134 tree op0 = TREE_OPERAND (expr, 0);
1135 tree op1 = TREE_OPERAND (expr, 1);
1137 /* If it would be profitable to swap the operands, then do so to
1138 canonicalize the statement, enabling better optimization.
1140 By placing canonicalization of such expressions here we
1141 transparently keep statements in canonical form, even
1142 when the statement is modified. */
1143 if (tree_swap_operands_p (op0, op1, false))
1145 /* For relationals we need to swap the operands
1146 and change the code. */
1147 if (code == LT_EXPR
1148 || code == GT_EXPR
1149 || code == LE_EXPR
1150 || code == GE_EXPR)
1152 TREE_SET_CODE (expr, swap_tree_comparison (code));
1153 TREE_OPERAND (expr, 0) = op1;
1154 TREE_OPERAND (expr, 1) = op0;
1157 /* For a commutative operator we can just swap the operands. */
1158 else if (commutative_tree_code (code))
1160 TREE_OPERAND (expr, 0) = op1;
1161 TREE_OPERAND (expr, 1) = op0;
1165 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1166 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1167 return;
1170 case REALIGN_LOAD_EXPR:
1172 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags);
1173 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags);
1174 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), flags);
1175 return;
1178 case BLOCK:
1179 case FUNCTION_DECL:
1180 case EXC_PTR_EXPR:
1181 case FILTER_EXPR:
1182 case LABEL_DECL:
1183 /* Expressions that make no memory references. */
1184 return;
1186 default:
1187 if (class == tcc_unary)
1188 goto do_unary;
1189 if (class == tcc_binary || class == tcc_comparison)
1190 goto do_binary;
1191 if (class == tcc_constant || class == tcc_type)
1192 return;
1195 /* If we get here, something has gone wrong. */
1196 #ifdef ENABLE_CHECKING
1197 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1198 debug_tree (expr);
1199 fputs ("\n", stderr);
1200 internal_error ("internal error");
1201 #endif
1202 gcc_unreachable ();
1206 /* Scan operands in the ASM_EXPR stmt referred to in INFO. */
1208 static void
1209 get_asm_expr_operands (tree stmt)
1211 stmt_ann_t s_ann = stmt_ann (stmt);
1212 int noutputs = list_length (ASM_OUTPUTS (stmt));
1213 const char **oconstraints
1214 = (const char **) alloca ((noutputs) * sizeof (const char *));
1215 int i;
1216 tree link;
1217 const char *constraint;
1218 bool allows_mem, allows_reg, is_inout;
1220 for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
1222 oconstraints[i] = constraint
1223 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1224 parse_output_constraint (&constraint, i, 0, 0,
1225 &allows_mem, &allows_reg, &is_inout);
1227 /* This should have been split in gimplify_asm_expr. */
1228 gcc_assert (!allows_reg || !is_inout);
1230 /* Memory operands are addressable. Note that STMT needs the
1231 address of this operand. */
1232 if (!allows_reg && allows_mem)
1234 tree t = get_base_address (TREE_VALUE (link));
1235 if (t && DECL_P (t))
1236 note_addressable (t, s_ann);
1239 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def);
1242 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
1244 constraint
1245 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
1246 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1247 oconstraints, &allows_mem, &allows_reg);
1249 /* Memory operands are addressable. Note that STMT needs the
1250 address of this operand. */
1251 if (!allows_reg && allows_mem)
1253 tree t = get_base_address (TREE_VALUE (link));
1254 if (t && DECL_P (t))
1255 note_addressable (t, s_ann);
1258 get_expr_operands (stmt, &TREE_VALUE (link), 0);
1262 /* Clobber memory for asm ("" : : : "memory"); */
1263 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
1264 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory") == 0)
1266 size_t i;
1267 bitmap_iterator bi;
1269 /* Clobber all call-clobbered variables (or .GLOBAL_VAR if we
1270 decided to group them). */
1271 if (global_var)
1272 add_stmt_operand (&global_var, stmt, opf_is_def);
1273 else
1274 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1276 tree var = referenced_var (i);
1277 add_stmt_operand (&var, stmt, opf_is_def);
1280 /* Now clobber all addressables. */
1281 EXECUTE_IF_SET_IN_BITMAP (addressable_vars, 0, i, bi)
1283 tree var = referenced_var (i);
1284 add_stmt_operand (&var, stmt, opf_is_def);
1287 break;
1291 /* A subroutine of get_expr_operands to handle INDIRECT_REF,
1292 ALIGN_INDIRECT_REF and MISALIGNED_INDIRECT_REF. */
1294 static void
1295 get_indirect_ref_operands (tree stmt, tree expr, int flags)
1297 tree *pptr = &TREE_OPERAND (expr, 0);
1298 tree ptr = *pptr;
1299 stmt_ann_t ann = stmt_ann (stmt);
1301 /* Stores into INDIRECT_REF operands are never killing definitions. */
1302 flags &= ~opf_kill_def;
1304 if (REF_ORIGINAL (expr))
1306 enum tree_code ocode = TREE_CODE (REF_ORIGINAL (expr));
1308 /* If we originally accessed part of a structure, we do it still. */
1309 if (ocode == ARRAY_REF
1310 || ocode == COMPONENT_REF
1311 || ocode == REALPART_EXPR
1312 || ocode == IMAGPART_EXPR)
1313 flags &= ~opf_kill_def;
1316 if (SSA_VAR_P (ptr))
1318 struct ptr_info_def *pi = NULL;
1320 /* If PTR has flow-sensitive points-to information, use it. */
1321 if (TREE_CODE (ptr) == SSA_NAME
1322 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1323 && pi->name_mem_tag)
1325 /* PTR has its own memory tag. Use it. */
1326 add_stmt_operand (&pi->name_mem_tag, stmt, flags);
1328 else
1330 /* If PTR is not an SSA_NAME or it doesn't have a name
1331 tag, use its type memory tag. */
1332 var_ann_t ann;
1334 /* If we are emitting debugging dumps, display a warning if
1335 PTR is an SSA_NAME with no flow-sensitive alias
1336 information. That means that we may need to compute
1337 aliasing again. */
1338 if (dump_file
1339 && TREE_CODE (ptr) == SSA_NAME
1340 && pi == NULL)
1342 fprintf (dump_file,
1343 "NOTE: no flow-sensitive alias info for ");
1344 print_generic_expr (dump_file, ptr, dump_flags);
1345 fprintf (dump_file, " in ");
1346 print_generic_stmt (dump_file, stmt, dump_flags);
1349 if (TREE_CODE (ptr) == SSA_NAME)
1350 ptr = SSA_NAME_VAR (ptr);
1351 ann = var_ann (ptr);
1352 if (ann->type_mem_tag)
1353 add_stmt_operand (&ann->type_mem_tag, stmt, flags);
1357 /* If a constant is used as a pointer, we can't generate a real
1358 operand for it but we mark the statement volatile to prevent
1359 optimizations from messing things up. */
1360 else if (TREE_CODE (ptr) == INTEGER_CST)
1362 if (ann)
1363 ann->has_volatile_ops = true;
1364 return;
1367 /* Everything else *should* have been folded elsewhere, but users
1368 are smarter than we in finding ways to write invalid code. We
1369 cannot just abort here. If we were absolutely certain that we
1370 do handle all valid cases, then we could just do nothing here.
1371 That seems optimistic, so attempt to do something logical... */
1372 else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1373 && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1374 && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1376 /* Make sure we know the object is addressable. */
1377 pptr = &TREE_OPERAND (ptr, 0);
1378 add_stmt_operand (pptr, stmt, 0);
1380 /* Mark the object itself with a VUSE. */
1381 pptr = &TREE_OPERAND (*pptr, 0);
1382 get_expr_operands (stmt, pptr, flags);
1383 return;
1386 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1387 else
1388 gcc_unreachable ();
1390 /* Add a USE operand for the base pointer. */
1391 get_expr_operands (stmt, pptr, opf_none);
1394 /* A subroutine of get_expr_operands to handle CALL_EXPR. */
1396 static void
1397 get_call_expr_operands (tree stmt, tree expr)
1399 tree op;
1400 int call_flags = call_expr_flags (expr);
1402 /* Find uses in the called function. */
1403 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none);
1405 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1406 get_expr_operands (stmt, &TREE_VALUE (op), opf_none);
1408 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none);
1410 if (bitmap_first_set_bit (call_clobbered_vars) >= 0)
1412 /* A 'pure' or a 'const' functions never call clobber anything.
1413 A 'noreturn' function might, but since we don't return anyway
1414 there is no point in recording that. */
1415 if (TREE_SIDE_EFFECTS (expr)
1416 && !(call_flags & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1417 add_call_clobber_ops (stmt);
1418 else if (!(call_flags & ECF_CONST))
1419 add_call_read_ops (stmt);
1424 /* Add *VAR_P to the appropriate operand array for INFO. FLAGS is as in
1425 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1426 the statement's real operands, otherwise it is added to virtual
1427 operands. */
1429 static void
1430 add_stmt_operand (tree *var_p, tree stmt, int flags)
1432 bool is_real_op;
1433 tree var, sym;
1434 stmt_ann_t s_ann = stmt_ann (stmt);
1435 var_ann_t v_ann;
1437 var = *var_p;
1438 STRIP_NOPS (var);
1440 /* If the operand is an ADDR_EXPR, add its operand to the list of
1441 variables that have had their address taken in this statement. */
1442 if (TREE_CODE (var) == ADDR_EXPR)
1444 note_addressable (TREE_OPERAND (var, 0), s_ann);
1445 return;
1448 /* If the original variable is not a scalar, it will be added to the list
1449 of virtual operands. In that case, use its base symbol as the virtual
1450 variable representing it. */
1451 is_real_op = is_gimple_reg (var);
1452 if (!is_real_op && !DECL_P (var))
1453 var = get_virtual_var (var);
1455 /* If VAR is not a variable that we care to optimize, do nothing. */
1456 if (var == NULL_TREE || !SSA_VAR_P (var))
1457 return;
1459 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1460 v_ann = var_ann (sym);
1462 /* Don't expose volatile variables to the optimizers. */
1463 if (TREE_THIS_VOLATILE (sym))
1465 if (s_ann)
1466 s_ann->has_volatile_ops = true;
1467 return;
1470 if (is_real_op)
1472 /* The variable is a GIMPLE register. Add it to real operands. */
1473 if (flags & opf_is_def)
1474 append_def (var_p);
1475 else
1476 append_use (var_p);
1478 else
1480 varray_type aliases;
1482 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1483 virtual operands, unless the caller has specifically requested
1484 not to add virtual operands (used when adding operands inside an
1485 ADDR_EXPR expression). */
1486 if (flags & opf_no_vops)
1487 return;
1489 aliases = v_ann->may_aliases;
1491 if (aliases == NULL)
1493 /* The variable is not aliased or it is an alias tag. */
1494 if (flags & opf_is_def)
1496 if (flags & opf_kill_def)
1498 /* Only regular variables may get a V_MUST_DEF
1499 operand. */
1500 gcc_assert (v_ann->mem_tag_kind == NOT_A_TAG);
1501 /* V_MUST_DEF for non-aliased, non-GIMPLE register
1502 variable definitions. */
1503 append_v_must_def (var);
1505 else
1507 /* Add a V_MAY_DEF for call-clobbered variables and
1508 memory tags. */
1509 append_v_may_def (var);
1512 else
1514 append_vuse (var);
1515 if (s_ann && v_ann->is_alias_tag)
1516 s_ann->makes_aliased_loads = 1;
1519 else
1521 size_t i;
1523 /* The variable is aliased. Add its aliases to the virtual
1524 operands. */
1525 gcc_assert (VARRAY_ACTIVE_SIZE (aliases) != 0);
1527 if (flags & opf_is_def)
1529 /* If the variable is also an alias tag, add a virtual
1530 operand for it, otherwise we will miss representing
1531 references to the members of the variable's alias set.
1532 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1533 if (v_ann->is_alias_tag)
1534 append_v_may_def (var);
1536 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1537 append_v_may_def (VARRAY_TREE (aliases, i));
1539 if (s_ann)
1540 s_ann->makes_aliased_stores = 1;
1542 else
1544 /* Similarly, append a virtual uses for VAR itself, when
1545 it is an alias tag. */
1546 if (v_ann->is_alias_tag)
1547 append_vuse (var);
1549 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1550 append_vuse (VARRAY_TREE (aliases, i));
1552 if (s_ann)
1553 s_ann->makes_aliased_loads = 1;
1560 /* Record that VAR had its address taken in the statement with annotations
1561 S_ANN. */
1563 static void
1564 note_addressable (tree var, stmt_ann_t s_ann)
1566 if (!s_ann)
1567 return;
1569 var = get_base_address (var);
1570 if (var && SSA_VAR_P (var))
1572 if (s_ann->addresses_taken == NULL)
1573 s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1574 bitmap_set_bit (s_ann->addresses_taken, var_ann (var)->uid);
1579 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1580 clobbered variables in the function. */
1582 static void
1583 add_call_clobber_ops (tree stmt)
1585 /* Functions that are not const, pure or never return may clobber
1586 call-clobbered variables. */
1587 if (stmt_ann (stmt))
1588 stmt_ann (stmt)->makes_clobbering_call = true;
1590 /* If we had created .GLOBAL_VAR earlier, use it. Otherwise, add
1591 a V_MAY_DEF operand for every call clobbered variable. See
1592 compute_may_aliases for the heuristic used to decide whether
1593 to create .GLOBAL_VAR or not. */
1594 if (global_var)
1595 add_stmt_operand (&global_var, stmt, opf_is_def);
1596 else
1598 size_t i;
1599 bitmap_iterator bi;
1601 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1603 tree var = referenced_var (i);
1604 if (TREE_READONLY (var)
1605 && (TREE_STATIC (var) || DECL_EXTERNAL (var)))
1606 add_stmt_operand (&var, stmt, opf_none);
1607 else
1608 add_stmt_operand (&var, stmt, opf_is_def);
1614 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1615 function. */
1617 static void
1618 add_call_read_ops (tree stmt)
1620 bitmap_iterator bi;
1622 /* Otherwise, if the function is not pure, it may reference memory. Add
1623 a VUSE for .GLOBAL_VAR if it has been created. Otherwise, add a VUSE
1624 for each call-clobbered variable. See add_referenced_var for the
1625 heuristic used to decide whether to create .GLOBAL_VAR. */
1626 if (global_var)
1627 add_stmt_operand (&global_var, stmt, opf_none);
1628 else
1630 size_t i;
1632 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i, bi)
1634 tree var = referenced_var (i);
1635 add_stmt_operand (&var, stmt, opf_none);
1640 /* Copies virtual operands from SRC to DST. */
1642 void
1643 copy_virtual_operands (tree dst, tree src)
1645 unsigned i;
1646 vuse_optype vuses = STMT_VUSE_OPS (src);
1647 v_may_def_optype v_may_defs = STMT_V_MAY_DEF_OPS (src);
1648 v_must_def_optype v_must_defs = STMT_V_MUST_DEF_OPS (src);
1649 vuse_optype *vuses_new = &stmt_ann (dst)->operands.vuse_ops;
1650 v_may_def_optype *v_may_defs_new = &stmt_ann (dst)->operands.v_may_def_ops;
1651 v_must_def_optype *v_must_defs_new = &stmt_ann (dst)->operands.v_must_def_ops;
1653 if (vuses)
1655 *vuses_new = allocate_vuse_optype (NUM_VUSES (vuses));
1656 for (i = 0; i < NUM_VUSES (vuses); i++)
1657 SET_VUSE_OP (*vuses_new, i, VUSE_OP (vuses, i));
1660 if (v_may_defs)
1662 *v_may_defs_new = allocate_v_may_def_optype (NUM_V_MAY_DEFS (v_may_defs));
1663 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
1665 SET_V_MAY_DEF_OP (*v_may_defs_new, i, V_MAY_DEF_OP (v_may_defs, i));
1666 SET_V_MAY_DEF_RESULT (*v_may_defs_new, i,
1667 V_MAY_DEF_RESULT (v_may_defs, i));
1671 if (v_must_defs)
1673 *v_must_defs_new = allocate_v_must_def_optype (NUM_V_MUST_DEFS (v_must_defs));
1674 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
1675 SET_V_MUST_DEF_OP (*v_must_defs_new, i, V_MUST_DEF_OP (v_must_defs, i));
1680 /* Specifically for use in DOM's expression analysis. Given a store, we
1681 create an artificial stmt which looks like a load from the store, this can
1682 be used to eliminate redundant loads. OLD_OPS are the operands from the
1683 store stmt, and NEW_STMT is the new load which represents a load of the
1684 values stored. */
1686 void
1687 create_ssa_artficial_load_stmt (stmt_operands_p old_ops, tree new_stmt)
1689 stmt_ann_t ann;
1690 tree op;
1691 stmt_operands_t tmp;
1692 unsigned j;
1694 memset (&tmp, 0, sizeof (stmt_operands_t));
1695 ann = get_stmt_ann (new_stmt);
1697 /* Free operands just in case is was an existing stmt. */
1698 free_ssa_operands (&(ann->operands));
1700 build_ssa_operands (new_stmt, NULL, &tmp, &(ann->operands));
1701 free_vuses (&(ann->operands.vuse_ops));
1702 free_v_may_defs (&(ann->operands.v_may_def_ops));
1703 free_v_must_defs (&(ann->operands.v_must_def_ops));
1705 /* For each VDEF on the original statement, we want to create a
1706 VUSE of the V_MAY_DEF result or V_MUST_DEF op on the new
1707 statement. */
1708 for (j = 0; j < NUM_V_MAY_DEFS (old_ops->v_may_def_ops); j++)
1710 op = V_MAY_DEF_RESULT (old_ops->v_may_def_ops, j);
1711 append_vuse (op);
1714 for (j = 0; j < NUM_V_MUST_DEFS (old_ops->v_must_def_ops); j++)
1716 op = V_MUST_DEF_OP (old_ops->v_must_def_ops, j);
1717 append_vuse (op);
1720 /* Now set the vuses for this new stmt. */
1721 ann->operands.vuse_ops = finalize_ssa_vuses (&(tmp.vuse_ops));
1724 #include "gt-tree-ssa-operands.h"