1 /* SSA operands management for trees.
2 Copyright (C) 2003 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)
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. */
23 #include "coretypes.h"
28 #include "diagnostic.h"
29 #include "tree-flow.h"
30 #include "tree-inline.h"
31 #include "tree-pass.h"
35 /* Flags to describe operand properties in get_stmt_operands and helpers. */
37 /* By default, operands are loaded. */
40 /* Operand is the target of an assignment expression or a
41 call-clobbered variable */
42 #define opf_is_def (1 << 0)
44 /* Operand is the target of an assignment expression. */
45 #define opf_kill_def (1 << 2)
47 /* No virtual operands should be created in the expression. This is used
48 when traversing ADDR_EXPR nodes which have different semantics than
49 other expressions. Inside an ADDR_EXPR node, the only operands that we
50 need to consider are indices into arrays. For instance, &a.b[i] should
51 generate a USE of 'i' but it should not generate a VUSE for 'a' nor a
53 #define opf_no_vops (1 << 1)
55 /* Array for building all the def operands. */
56 static GTY (()) varray_type build_defs;
58 /* Array for building all the use operands. */
59 static GTY (()) varray_type build_uses;
61 /* Array for building all the v_may_def operands. */
62 static GTY (()) varray_type build_v_may_defs;
64 /* Array for building all the vuse operands. */
65 static GTY (()) varray_type build_vuses;
67 /* Array for building all the v_must_def operands. */
68 static GTY (()) varray_type build_v_must_defs;
70 #ifdef ENABLE_CHECKING
71 tree check_build_stmt;
74 typedef struct voperands_d
76 v_may_def_optype v_may_def_ops;
78 v_must_def_optype v_must_def_ops;
81 static void note_addressable (tree, stmt_ann_t);
82 static void get_expr_operands (tree, tree *, int, voperands_t);
83 static inline void append_def (tree *, tree);
84 static inline void append_use (tree *, tree);
85 static void append_v_may_def (tree, tree, voperands_t);
86 static void append_v_must_def (tree, tree, voperands_t);
87 static void add_call_clobber_ops (tree, voperands_t);
88 static void add_call_read_ops (tree, voperands_t);
89 static void add_stmt_operand (tree *, tree, int, voperands_t);
92 struct freelist_d GTY((chain_next ("%h.next")))
94 struct freelist_d *next;
98 static GTY ((length ("NUM_FREE"))) struct freelist_d optype_freelist[NUM_FREE] = { {0}, {0}, {0}, {0}, {0} };
102 check_optype_freelist (size_t num ATTRIBUTE_UNUSED)
108 if (num <= NUM_FREE && optype_freelist[num - 1].next)
110 vec = (void *)optype_freelist[num - 1].next;
111 optype_freelist[num - 1].next = optype_freelist[num - 1].next->next;
116 /* Return a vector of contiguous memory of a specified size. */
120 add_optype_freelist (void *vec ATTRIBUTE_UNUSED, size_t size ATTRIBUTE_UNUSED)
123 struct freelist_d *ptr;
124 #ifdef ENABLE_CHECKING
129 /* if its bigger than one of our lists, simply let it go and let GC
135 ptr->next = optype_freelist[size - 1].next;;
136 optype_freelist[size - 1].next = ptr;
141 static inline def_optype
142 allocate_def_optype (unsigned num)
146 size = sizeof (struct def_optype_d) + sizeof (tree *) * (num - 1);
147 def_ops = check_optype_freelist (num);
149 def_ops = ggc_alloc (size);
150 def_ops->num_defs = num;
154 static inline use_optype
155 allocate_use_optype (unsigned num)
159 size = sizeof (struct use_optype_d) + sizeof (tree *) * (num - 1);
160 use_ops = check_optype_freelist (num);
162 use_ops = ggc_alloc (size);
163 use_ops->num_uses = num;
167 static inline v_may_def_optype
168 allocate_v_may_def_optype (unsigned num)
170 v_may_def_optype v_may_def_ops;
172 size = sizeof (struct v_may_def_optype_d) + sizeof (tree) * ((num * 2) - 1);
173 v_may_def_ops = check_optype_freelist (num * 2);
175 v_may_def_ops = ggc_alloc (size);
176 v_may_def_ops->num_v_may_defs = num;
177 return v_may_def_ops;
180 static inline vuse_optype
181 allocate_vuse_optype (unsigned num)
183 vuse_optype vuse_ops;
185 size = sizeof (struct vuse_optype_d) + sizeof (tree) * (num - 1);
186 vuse_ops = check_optype_freelist (num);
188 vuse_ops = ggc_alloc (size);
189 vuse_ops->num_vuses = num;
193 static inline v_must_def_optype
194 allocate_v_must_def_optype (unsigned num)
196 v_must_def_optype v_must_def_ops;
198 size = sizeof (struct v_must_def_optype_d) + sizeof (tree *) * (num - 1);
199 v_must_def_ops = check_optype_freelist (num);
201 v_must_def_ops = ggc_alloc (size);
202 v_must_def_ops->num_v_must_defs = num;
203 return v_must_def_ops;
207 free_uses (use_optype *uses, bool dealloc)
212 add_optype_freelist (*uses, (*uses)->num_uses);
218 free_defs (def_optype *defs, bool dealloc)
223 add_optype_freelist (*defs, (*defs)->num_defs);
229 free_vuses (vuse_optype *vuses, bool dealloc)
234 add_optype_freelist (*vuses, (*vuses)->num_vuses);
240 free_v_may_defs (v_may_def_optype *v_may_defs, bool dealloc)
245 add_optype_freelist (*v_may_defs, (*v_may_defs)->num_v_may_defs);
251 free_v_must_defs (v_must_def_optype *v_must_defs, bool dealloc)
256 add_optype_freelist (*v_must_defs, (*v_must_defs)->num_v_must_defs);
262 remove_vuses (tree stmt)
266 ann = stmt_ann (stmt);
268 free_vuses (&(ann->vuse_ops), true);
272 remove_v_may_defs (tree stmt)
276 ann = stmt_ann (stmt);
278 free_v_may_defs (&(ann->v_may_def_ops), true);
282 remove_v_must_defs (tree stmt)
286 ann = stmt_ann (stmt);
288 free_v_must_defs (&(ann->v_must_def_ops), true);
292 init_ssa_operands (void)
296 VARRAY_TREE_PTR_INIT (build_defs, 5, "build defs");
297 VARRAY_TREE_PTR_INIT (build_uses, 10, "build uses");
298 VARRAY_TREE_INIT (build_v_may_defs, 10, "build v_may_defs");
299 VARRAY_TREE_INIT (build_vuses, 10, "build vuses");
300 VARRAY_TREE_INIT (build_v_must_defs, 10, "build v_must_defs");
302 for (x = 0; x < NUM_FREE; x++)
303 optype_freelist[x].next = NULL;
307 fini_ssa_operands (void)
310 for (x = 0; x < NUM_FREE; x++)
311 optype_freelist[x].next = NULL;
315 finalize_ssa_defs (tree stmt)
321 num = VARRAY_ACTIVE_SIZE (build_defs);
325 #ifdef ENABLE_CHECKING
326 /* There should only be a single real definition per assignment. */
327 if (TREE_CODE (stmt) == MODIFY_EXPR && num > 1)
331 def_ops = allocate_def_optype (num);
332 for (x = 0; x < num ; x++)
333 def_ops->defs[x] = VARRAY_TREE_PTR (build_defs, x);
334 VARRAY_POP_ALL (build_defs);
336 ann = stmt_ann (stmt);
337 ann->def_ops = def_ops;
341 finalize_ssa_uses (tree stmt)
347 num = VARRAY_ACTIVE_SIZE (build_uses);
351 #ifdef ENABLE_CHECKING
354 /* If the pointer to the operand is the statement itself, something is
355 wrong. It means that we are pointing to a local variable (the
356 initial call to get_stmt_operands does not pass a pointer to a
358 for (x = 0; x < num; x++)
359 if (*(VARRAY_TREE_PTR (build_uses, x)) == stmt)
364 use_ops = allocate_use_optype (num);
365 for (x = 0; x < num ; x++)
366 use_ops->uses[x] = VARRAY_TREE_PTR (build_uses, x);
367 VARRAY_POP_ALL (build_uses);
369 ann = stmt_ann (stmt);
370 ann->use_ops = use_ops;
374 finalize_ssa_v_may_defs (tree stmt)
377 v_may_def_optype v_may_def_ops;
380 num = VARRAY_ACTIVE_SIZE (build_v_may_defs);
384 #ifdef ENABLE_CHECKING
385 /* V_MAY_DEFs must be entered in pairs of result/uses. */
390 v_may_def_ops = allocate_v_may_def_optype (num / 2);
391 for (x = 0; x < num; x++)
392 v_may_def_ops->v_may_defs[x] = VARRAY_TREE (build_v_may_defs, x);
393 VARRAY_CLEAR (build_v_may_defs);
395 ann = stmt_ann (stmt);
396 ann->v_may_def_ops = v_may_def_ops;
400 finalize_ssa_vuses (tree stmt)
404 vuse_optype vuse_ops;
405 v_may_def_optype v_may_defs;
407 #ifdef ENABLE_CHECKING
408 if (VARRAY_ACTIVE_SIZE (build_v_may_defs) > 0)
410 fprintf (stderr, "Please finalize V_MAY_DEFs before finalize VUSES.\n");
415 num = VARRAY_ACTIVE_SIZE (build_vuses);
419 /* Remove superfluous VUSE operands. If the statement already has a
420 V_MAY_DEF operation for a variable 'a', then a VUSE for 'a' is not
421 needed because V_MAY_DEFs imply a VUSE of the variable. For instance,
422 suppose that variable 'a' is aliased:
425 # a_3 = V_MAY_DEF <a_2>
428 The VUSE <a_2> is superfluous because it is implied by the V_MAY_DEF
431 ann = stmt_ann (stmt);
432 v_may_defs = V_MAY_DEF_OPS (ann);
433 if (NUM_V_MAY_DEFS (v_may_defs) > 0)
436 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
439 for (j = 0; j < NUM_V_MAY_DEFS (v_may_defs); j++)
441 tree vuse_var, v_may_def_var;
442 tree vuse = VARRAY_TREE (build_vuses, i);
443 tree v_may_def = V_MAY_DEF_OP (v_may_defs, j);
445 if (TREE_CODE (vuse) == SSA_NAME)
446 vuse_var = SSA_NAME_VAR (vuse);
450 if (TREE_CODE (v_may_def) == SSA_NAME)
451 v_may_def_var = SSA_NAME_VAR (v_may_def);
453 v_may_def_var = v_may_def;
455 if (vuse_var == v_may_def_var)
462 /* If we found a useless VUSE operand, remove it from the
463 operand array by replacing it with the last active element
464 in the operand array (unless the useless VUSE was the
465 last operand, in which case we simply remove it. */
468 if (i != VARRAY_ACTIVE_SIZE (build_vuses) - 1)
470 VARRAY_TREE (build_vuses, i)
471 = VARRAY_TREE (build_vuses,
472 VARRAY_ACTIVE_SIZE (build_vuses) - 1);
474 VARRAY_POP (build_vuses);
476 /* We want to rescan the element at this index, unless
477 this was the last element, in which case the loop
484 num = VARRAY_ACTIVE_SIZE (build_vuses);
485 /* We could have reduced the size to zero now, however. */
489 vuse_ops = allocate_vuse_optype (num);
490 for (x = 0; x < num; x++)
491 vuse_ops->vuses[x] = VARRAY_TREE (build_vuses, x);
492 VARRAY_CLEAR (build_vuses);
493 ann->vuse_ops = vuse_ops;
497 finalize_ssa_v_must_defs (tree stmt)
501 v_must_def_optype v_must_def_ops;
503 num = VARRAY_ACTIVE_SIZE (build_v_must_defs);
507 #ifdef ENABLE_CHECKING
508 /* There should only be a single V_MUST_DEF per assignment. */
509 if (TREE_CODE (stmt) == MODIFY_EXPR && num > 1)
513 v_must_def_ops = allocate_v_must_def_optype (num);
514 for (x = 0; x < num ; x++)
515 v_must_def_ops->v_must_defs[x] = VARRAY_TREE (build_v_must_defs, x);
516 VARRAY_POP_ALL (build_v_must_defs);
518 ann = stmt_ann (stmt);
519 ann->v_must_def_ops = v_must_def_ops;
523 finalize_ssa_stmt_operands (tree stmt)
525 #ifdef ENABLE_CHECKING
526 if (check_build_stmt == NULL)
530 finalize_ssa_defs (stmt);
531 finalize_ssa_uses (stmt);
532 finalize_ssa_v_must_defs (stmt);
533 finalize_ssa_v_may_defs (stmt);
534 finalize_ssa_vuses (stmt);
536 #ifdef ENABLE_CHECKING
537 check_build_stmt = NULL;
543 verify_start_operands (tree stmt ATTRIBUTE_UNUSED)
545 #ifdef ENABLE_CHECKING
546 if (VARRAY_ACTIVE_SIZE (build_defs) > 0
547 || VARRAY_ACTIVE_SIZE (build_uses) > 0
548 || VARRAY_ACTIVE_SIZE (build_vuses) > 0
549 || VARRAY_ACTIVE_SIZE (build_v_may_defs) > 0
550 || VARRAY_ACTIVE_SIZE (build_v_must_defs) > 0)
552 if (check_build_stmt != NULL)
554 check_build_stmt = stmt;
559 /* Add DEF_P to the list of pointers to operands defined by STMT. */
562 append_def (tree *def_p, tree stmt ATTRIBUTE_UNUSED)
564 #ifdef ENABLE_CHECKING
565 if (check_build_stmt != stmt)
568 VARRAY_PUSH_TREE_PTR (build_defs, def_p);
572 /* Add USE_P to the list of pointers to operands used by STMT. */
575 append_use (tree *use_p, tree stmt ATTRIBUTE_UNUSED)
577 #ifdef ENABLE_CHECKING
578 if (check_build_stmt != stmt)
581 VARRAY_PUSH_TREE_PTR (build_uses, use_p);
585 /* Add a new virtual def for variable VAR to statement STMT. If PREV_VOPS
586 is not NULL, the existing entries are preserved and no new entries are
587 added here. This is done to preserve the SSA numbering of virtual
591 append_v_may_def (tree var, tree stmt, voperands_t prev_vops)
597 #ifdef ENABLE_CHECKING
598 if (check_build_stmt != stmt)
602 ann = stmt_ann (stmt);
604 /* Don't allow duplicate entries. */
606 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_may_defs); i += 2)
608 tree result = VARRAY_TREE (build_v_may_defs, i);
610 || (TREE_CODE (result) == SSA_NAME
611 && var == SSA_NAME_VAR (result)))
615 /* If the statement already had virtual definitions, see if any of the
616 existing V_MAY_DEFs matches VAR. If so, re-use it, otherwise add a new
617 V_MAY_DEF for VAR. */
621 for (i = 0; i < NUM_V_MAY_DEFS (prev_vops->v_may_def_ops); i++)
623 result = V_MAY_DEF_RESULT (prev_vops->v_may_def_ops, i);
625 || (TREE_CODE (result) == SSA_NAME
626 && SSA_NAME_VAR (result) == var))
628 source = V_MAY_DEF_OP (prev_vops->v_may_def_ops, i);
633 /* If no previous V_MAY_DEF operand was found for VAR, create one now. */
634 if (source == NULL_TREE)
640 VARRAY_PUSH_TREE (build_v_may_defs, result);
641 VARRAY_PUSH_TREE (build_v_may_defs, source);
645 /* Add VAR to the list of virtual uses for STMT. If PREV_VOPS
646 is not NULL, the existing entries are preserved and no new entries are
647 added here. This is done to preserve the SSA numbering of virtual
651 append_vuse (tree var, tree stmt, voperands_t prev_vops)
658 #ifdef ENABLE_CHECKING
659 if (check_build_stmt != stmt)
663 ann = stmt_ann (stmt);
665 /* Don't allow duplicate entries. */
666 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_vuses); i++)
668 tree vuse_var = VARRAY_TREE (build_vuses, i);
670 || (TREE_CODE (vuse_var) == SSA_NAME
671 && var == SSA_NAME_VAR (vuse_var)))
675 /* If the statement already had virtual uses, see if any of the
676 existing VUSEs matches VAR. If so, re-use it, otherwise add a new
681 for (i = 0; i < NUM_VUSES (prev_vops->vuse_ops); i++)
683 vuse = VUSE_OP (prev_vops->vuse_ops, i);
685 || (TREE_CODE (vuse) == SSA_NAME
686 && SSA_NAME_VAR (vuse) == var))
693 /* If VAR existed already in PREV_VOPS, re-use it. */
697 VARRAY_PUSH_TREE (build_vuses, var);
700 /* Add VAR to the list of virtual must definitions for STMT. If PREV_VOPS
701 is not NULL, the existing entries are preserved and no new entries are
702 added here. This is done to preserve the SSA numbering of virtual
706 append_v_must_def (tree var, tree stmt, voperands_t prev_vops)
713 #ifdef ENABLE_CHECKING
714 if (check_build_stmt != stmt)
718 ann = stmt_ann (stmt);
720 /* Don't allow duplicate entries. */
721 for (i = 0; i < VARRAY_ACTIVE_SIZE (build_v_must_defs); i++)
723 tree v_must_def_var = VARRAY_TREE (build_v_must_defs, i);
724 if (var == v_must_def_var
725 || (TREE_CODE (v_must_def_var) == SSA_NAME
726 && var == SSA_NAME_VAR (v_must_def_var)))
730 /* If the statement already had virtual must defs, see if any of the
731 existing V_MUST_DEFs matches VAR. If so, re-use it, otherwise add a new
732 V_MUST_DEF for VAR. */
734 v_must_def = NULL_TREE;
736 for (i = 0; i < NUM_V_MUST_DEFS (prev_vops->v_must_def_ops); i++)
738 v_must_def = V_MUST_DEF_OP (prev_vops->v_must_def_ops, i);
739 if (v_must_def == var
740 || (TREE_CODE (v_must_def) == SSA_NAME
741 && SSA_NAME_VAR (v_must_def) == var))
748 /* If VAR existed already in PREV_VOPS, re-use it. */
752 VARRAY_PUSH_TREE (build_v_must_defs, var);
756 /* External entry point which by-passes the previous vops mechanism. */
758 add_vuse (tree var, tree stmt)
760 append_vuse (var, stmt, NULL);
764 /* Get the operands of statement STMT. Note that repeated calls to
765 get_stmt_operands for the same statement will do nothing until the
766 statement is marked modified by a call to modify_stmt(). */
769 get_stmt_operands (tree stmt)
773 struct voperands_d prev_vops;
775 #if defined ENABLE_CHECKING
776 /* The optimizers cannot handle statements that are nothing but a
777 _DECL. This indicates a bug in the gimplifier. */
778 if (SSA_VAR_P (stmt))
782 /* Ignore error statements. */
783 if (TREE_CODE (stmt) == ERROR_MARK)
786 ann = get_stmt_ann (stmt);
788 /* If the statement has not been modified, the operands are still valid. */
792 timevar_push (TV_TREE_OPS);
794 /* Initially assume that the statement has no volatile operands.
795 Statements marked with 'has_volatile_ops' are not processed by the
797 ann->has_volatile_ops = false;
799 /* Remove any existing operands as they will be scanned again. */
800 free_defs (&(ann->def_ops), true);
801 free_uses (&(ann->use_ops), true);
803 /* Before removing existing virtual operands, save them in PREV_VOPS so
804 that we can re-use their SSA versions. */
805 prev_vops.v_may_def_ops = V_MAY_DEF_OPS (ann);
806 prev_vops.vuse_ops = VUSE_OPS (ann);
807 prev_vops.v_must_def_ops = V_MUST_DEF_OPS (ann);
809 /* Dont free the previous values to memory since we're still using them. */
810 free_v_may_defs (&(ann->v_may_def_ops), false);
811 free_vuses (&(ann->vuse_ops), false);
812 free_v_must_defs (&(ann->v_must_def_ops), false);
814 start_ssa_stmt_operands (stmt);
816 code = TREE_CODE (stmt);
820 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none, &prev_vops);
821 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF
822 || TREE_CODE (TREE_OPERAND (stmt, 0)) == COMPONENT_REF
823 || TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
824 || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR)
825 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_is_def,
828 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0),
829 opf_is_def | opf_kill_def, &prev_vops);
833 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none, &prev_vops);
837 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none, &prev_vops);
842 int noutputs = list_length (ASM_OUTPUTS (stmt));
843 const char **oconstraints
844 = (const char **) alloca ((noutputs) * sizeof (const char *));
847 const char *constraint;
848 bool allows_mem, allows_reg, is_inout;
850 for (i=0, link = ASM_OUTPUTS (stmt); link;
851 ++i, link = TREE_CHAIN (link))
853 oconstraints[i] = constraint
854 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
855 parse_output_constraint (&constraint, i, 0, 0,
856 &allows_mem, &allows_reg, &is_inout);
857 if (allows_reg && is_inout)
858 /* This should have been split in gimplify_asm_expr. */
861 if (!allows_reg && allows_mem)
863 tree t = get_base_address (TREE_VALUE (link));
865 mark_call_clobbered (t);
868 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def,
872 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
875 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
876 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
877 oconstraints, &allows_mem, &allows_reg);
879 if (!allows_reg && allows_mem)
881 tree t = get_base_address (TREE_VALUE (link));
883 mark_call_clobbered (t);
886 get_expr_operands (stmt, &TREE_VALUE (link), 0, &prev_vops);
889 /* Clobber memory for asm ("" : : : "memory"); */
890 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
891 if (!strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory"))
892 add_call_clobber_ops (stmt, &prev_vops);
897 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none, &prev_vops);
901 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none, &prev_vops);
905 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none, &prev_vops);
908 /* These nodes contain no variable references. */
910 case CASE_LABEL_EXPR:
912 case TRY_FINALLY_EXPR:
919 /* Notice that if get_expr_operands tries to use &STMT as the operand
920 pointer (which may only happen for USE operands), we will abort in
921 append_use. This default will handle statements like empty statements,
922 CALL_EXPRs or VA_ARG_EXPRs that may appear on the RHS of a statement
923 or as statements themselves. */
924 get_expr_operands (stmt, &stmt, opf_none, &prev_vops);
928 finalize_ssa_stmt_operands (stmt);
930 /* Now free the previous virtual ops to memory. */
931 free_v_may_defs (&(prev_vops.v_may_def_ops), true);
932 free_vuses (&(prev_vops.vuse_ops), true);
933 free_v_must_defs (&(prev_vops.v_must_def_ops), true);
935 /* Clear the modified bit for STMT. Subsequent calls to
936 get_stmt_operands for this statement will do nothing until the
937 statement is marked modified by a call to modify_stmt(). */
940 timevar_pop (TV_TREE_OPS);
944 /* Recursively scan the expression pointed by EXPR_P in statement STMT.
945 FLAGS is one of the OPF_* constants modifying how to interpret the
946 operands found. PREV_VOPS is as in append_v_may_def and append_vuse. */
949 get_expr_operands (tree stmt, tree *expr_p, int flags, voperands_t prev_vops)
955 if (expr == NULL || expr == error_mark_node)
958 code = TREE_CODE (expr);
959 class = TREE_CODE_CLASS (code);
961 /* Expressions that make no memory references. */
965 || code == FUNCTION_DECL
966 || code == EXC_PTR_EXPR
967 || code == FILTER_EXPR
968 || code == LABEL_DECL)
971 /* We could have the address of a component, array member, etc which
972 has interesting variable references. */
973 if (code == ADDR_EXPR)
975 enum tree_code subcode = TREE_CODE (TREE_OPERAND (expr, 0));
977 /* Taking the address of a variable does not represent a
978 reference to it, but the fact that STMT takes its address will be
979 of interest to some passes (e.g. alias resolution). */
980 add_stmt_operand (expr_p, stmt, 0, NULL);
982 /* If the address is invariant, there may be no interesting variable
983 references inside. */
984 if (is_gimple_min_invariant (expr))
987 /* There should be no VUSEs created, since the referenced objects are
988 not really accessed. The only operands that we should find here
989 are ARRAY_REF indices which will always be real operands (GIMPLE
990 does not allow non-registers as array indices). */
991 flags |= opf_no_vops;
993 /* Avoid recursion. */
995 class = TREE_CODE_CLASS (code);
996 expr_p = &TREE_OPERAND (expr, 0);
1000 /* If we found a variable, add it to DEFS or USES depending on the
1002 if (SSA_VAR_P (expr))
1004 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1008 /* Pointer dereferences always represent a use of the base pointer. */
1009 if (code == INDIRECT_REF)
1011 tree *pptr = &TREE_OPERAND (expr, 0);
1014 if (SSA_VAR_P (ptr))
1016 if (!aliases_computed_p)
1018 /* If the pointer does not have a memory tag and aliases have not
1019 been computed yet, mark the statement as having volatile
1020 operands to prevent DOM from entering it in equivalence tables
1021 and DCE from killing it. */
1022 stmt_ann (stmt)->has_volatile_ops = true;
1026 struct ptr_info_def *pi = NULL;
1028 /* If we have computed aliasing already, check if PTR has
1029 flow-sensitive points-to information. */
1030 if (TREE_CODE (ptr) == SSA_NAME
1031 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1032 && pi->name_mem_tag)
1034 /* PTR has its own memory tag. Use it. */
1035 add_stmt_operand (&pi->name_mem_tag, stmt, flags,
1040 /* If PTR is not an SSA_NAME or it doesn't have a name
1041 tag, use its type memory tag. */
1044 /* If we are emitting debugging dumps, display a warning if
1045 PTR is an SSA_NAME with no flow-sensitive alias
1046 information. That means that we may need to compute
1049 && TREE_CODE (ptr) == SSA_NAME
1053 "NOTE: no flow-sensitive alias info for ");
1054 print_generic_expr (dump_file, ptr, dump_flags);
1055 fprintf (dump_file, " in ");
1056 print_generic_stmt (dump_file, stmt, dump_flags);
1059 if (TREE_CODE (ptr) == SSA_NAME)
1060 ptr = SSA_NAME_VAR (ptr);
1061 ann = var_ann (ptr);
1062 add_stmt_operand (&ann->type_mem_tag, stmt, flags, prev_vops);
1067 /* If a constant is used as a pointer, we can't generate a real
1068 operand for it but we mark the statement volatile to prevent
1069 optimizations from messing things up. */
1070 else if (TREE_CODE (ptr) == INTEGER_CST)
1072 stmt_ann (stmt)->has_volatile_ops = true;
1076 /* Everything else *should* have been folded elsewhere, but users
1077 are smarter than we in finding ways to write invalid code. We
1078 cannot just abort here. If we were absolutely certain that we
1079 do handle all valid cases, then we could just do nothing here.
1080 That seems optimistic, so attempt to do something logical... */
1081 else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1082 && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1083 && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1085 /* Make sure we know the object is addressable. */
1086 pptr = &TREE_OPERAND (ptr, 0);
1087 add_stmt_operand (pptr, stmt, 0, NULL);
1089 /* Mark the object itself with a VUSE. */
1090 pptr = &TREE_OPERAND (*pptr, 0);
1091 get_expr_operands (stmt, pptr, flags, prev_vops);
1095 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1099 /* Add a USE operand for the base pointer. */
1100 get_expr_operands (stmt, pptr, opf_none, prev_vops);
1104 /* Treat array references as references to the virtual variable
1105 representing the array. The virtual variable for an ARRAY_REF
1106 is the VAR_DECL for the array. */
1107 if (code == ARRAY_REF)
1109 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1110 according to the value of IS_DEF. Recurse if the LHS of the
1111 ARRAY_REF node is not a regular variable. */
1112 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1113 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1115 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1117 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none, prev_vops);
1121 /* Similarly to arrays, references to compound variables (complex types
1122 and structures/unions) are globbed.
1124 FIXME: This means that
1130 will not be constant propagated because the two partial
1131 definitions to 'a' will kill each other. Note that SRA may be
1132 able to fix this problem if 'a' can be scalarized. */
1133 if (code == IMAGPART_EXPR || code == REALPART_EXPR || code == COMPONENT_REF)
1135 /* If the LHS of the compound reference is not a regular variable,
1136 recurse to keep looking for more operands in the subexpression. */
1137 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1138 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1140 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1145 /* Function calls. Add every argument to USES. If the callee is
1146 neither pure nor const, create a VDEF reference for GLOBAL_VAR
1147 (See find_vars_r). */
1148 if (code == CALL_EXPR)
1151 int call_flags = call_expr_flags (expr);
1153 /* Find uses in the called function. */
1154 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none, prev_vops);
1156 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1157 get_expr_operands (stmt, &TREE_VALUE (op), opf_none, prev_vops);
1159 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none, prev_vops);
1161 if (bitmap_first_set_bit (call_clobbered_vars) >= 0)
1163 /* A 'pure' or a 'const' functions never call clobber anything.
1164 A 'noreturn' function might, but since we don't return anyway
1165 there is no point in recording that. */
1167 & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1168 add_call_clobber_ops (stmt, prev_vops);
1169 else if (!(call_flags & (ECF_CONST | ECF_NORETURN)))
1170 add_call_read_ops (stmt, prev_vops);
1172 else if (!aliases_computed_p)
1173 stmt_ann (stmt)->has_volatile_ops = true;
1179 if (code == TREE_LIST)
1183 for (op = expr; op; op = TREE_CHAIN (op))
1184 get_expr_operands (stmt, &TREE_VALUE (op), flags, prev_vops);
1190 if (code == MODIFY_EXPR)
1192 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none, prev_vops);
1193 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ARRAY_REF
1194 || TREE_CODE (TREE_OPERAND (expr, 0)) == COMPONENT_REF
1195 || TREE_CODE (TREE_OPERAND (expr, 0)) == REALPART_EXPR
1196 || TREE_CODE (TREE_OPERAND (expr, 0)) == IMAGPART_EXPR)
1197 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_is_def,
1200 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
1201 opf_is_def | opf_kill_def, prev_vops);
1206 /* Mark VA_ARG_EXPR nodes as making volatile references. FIXME,
1207 this is needed because we currently do not gimplify VA_ARG_EXPR
1209 if (code == VA_ARG_EXPR)
1211 stmt_ann (stmt)->has_volatile_ops = true;
1215 /* Unary expressions. */
1217 || code == TRUTH_NOT_EXPR
1218 || code == BIT_FIELD_REF
1219 || code == CONSTRUCTOR)
1221 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1225 /* Binary expressions. */
1228 || code == TRUTH_AND_EXPR
1229 || code == TRUTH_OR_EXPR
1230 || code == TRUTH_XOR_EXPR
1231 || code == COMPOUND_EXPR)
1233 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1234 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags, prev_vops);
1238 /* If we get here, something has gone wrong. */
1239 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1241 fputs ("\n", stderr);
1246 /* Add *VAR_P to the appropriate operand array of STMT. FLAGS is as in
1247 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1248 the statement's real operands, otherwise it is added to virtual
1251 PREV_VOPS is used when adding virtual operands to statements that
1252 already had them (See append_v_may_def and append_vuse). */
1255 add_stmt_operand (tree *var_p, tree stmt, int flags, voperands_t prev_vops)
1265 s_ann = stmt_ann (stmt);
1267 /* If the operand is an ADDR_EXPR, add its operand to the list of
1268 variables that have had their address taken in this statement. */
1269 if (TREE_CODE (var) == ADDR_EXPR)
1271 note_addressable (TREE_OPERAND (var, 0), s_ann);
1275 /* If the original variable is not a scalar, it will be added to the list
1276 of virtual operands. In that case, use its base symbol as the virtual
1277 variable representing it. */
1278 is_real_op = is_gimple_reg (var);
1279 if (!is_real_op && !DECL_P (var))
1280 var = get_virtual_var (var);
1282 /* If VAR is not a variable that we care to optimize, do nothing. */
1283 if (var == NULL_TREE || !SSA_VAR_P (var))
1286 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1287 v_ann = var_ann (sym);
1289 /* FIXME: We currently refuse to optimize variables that have hidden uses
1290 (variables used in VLA declarations, MD builtin calls and variables
1291 from the parent function in nested functions). This is because not
1292 all uses of these variables are exposed in the IL or the statements
1293 that reference them are not in GIMPLE form. If that's the case, mark
1294 the statement as having volatile operands and return. */
1295 if (v_ann->has_hidden_use)
1297 s_ann->has_volatile_ops = true;
1301 /* Don't expose volatile variables to the optimizers. */
1302 if (TREE_THIS_VOLATILE (sym))
1304 s_ann->has_volatile_ops = true;
1310 /* The variable is a GIMPLE register. Add it to real operands. */
1311 if (flags & opf_is_def)
1312 append_def (var_p, stmt);
1314 append_use (var_p, stmt);
1318 varray_type aliases;
1320 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1321 virtual operands, unless the caller has specifically requested
1322 not to add virtual operands (used when adding operands inside an
1323 ADDR_EXPR expression). */
1324 if (flags & opf_no_vops)
1327 aliases = v_ann->may_aliases;
1329 /* If alias information hasn't been computed yet, then
1330 addressable variables will not be an alias tag nor will they
1331 have aliases. In this case, mark the statement as having
1332 volatile operands. */
1333 if (!aliases_computed_p && may_be_aliased (var))
1334 s_ann->has_volatile_ops = true;
1336 if (aliases == NULL)
1338 /* The variable is not aliased or it is an alias tag. */
1339 if (flags & opf_is_def)
1341 if (v_ann->is_alias_tag)
1343 /* Alias tagged vars get regular V_MAY_DEF */
1344 s_ann->makes_aliased_stores = 1;
1345 append_v_may_def (var, stmt, prev_vops);
1347 else if ((flags & opf_kill_def)
1348 && v_ann->mem_tag_kind == NOT_A_TAG)
1349 /* V_MUST_DEF for non-aliased non-GIMPLE register
1350 variable definitions. Avoid memory tags. */
1351 append_v_must_def (var, stmt, prev_vops);
1353 /* Call-clobbered variables & memory tags get
1355 append_v_may_def (var, stmt, prev_vops);
1359 append_vuse (var, stmt, prev_vops);
1360 if (v_ann->is_alias_tag)
1361 s_ann->makes_aliased_loads = 1;
1368 /* The variable is aliased. Add its aliases to the virtual
1370 if (VARRAY_ACTIVE_SIZE (aliases) == 0)
1373 if (flags & opf_is_def)
1375 /* If the variable is also an alias tag, add a virtual
1376 operand for it, otherwise we will miss representing
1377 references to the members of the variable's alias set.
1378 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1379 if (v_ann->is_alias_tag)
1380 append_v_may_def (var, stmt, prev_vops);
1382 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1383 append_v_may_def (VARRAY_TREE (aliases, i), stmt, prev_vops);
1385 s_ann->makes_aliased_stores = 1;
1389 if (v_ann->is_alias_tag)
1390 append_vuse (var, stmt, prev_vops);
1392 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1393 append_vuse (VARRAY_TREE (aliases, i), stmt, prev_vops);
1395 s_ann->makes_aliased_loads = 1;
1401 /* Record that VAR had its address taken in the statement with annotations
1405 note_addressable (tree var, stmt_ann_t s_ann)
1407 var = get_base_address (var);
1408 if (var && SSA_VAR_P (var))
1410 if (s_ann->addresses_taken == NULL)
1411 s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1412 bitmap_set_bit (s_ann->addresses_taken, var_ann (var)->uid);
1417 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1418 clobbered variables in the function. */
1421 add_call_clobber_ops (tree stmt, voperands_t prev_vops)
1423 /* Functions that are not const, pure or never return may clobber
1424 call-clobbered variables. */
1425 stmt_ann (stmt)->makes_clobbering_call = true;
1427 /* If we had created .GLOBAL_VAR earlier, use it. Otherwise, add
1428 a V_MAY_DEF operand for every call clobbered variable. See
1429 compute_may_aliases for the heuristic used to decide whether
1430 to create .GLOBAL_VAR or not. */
1432 add_stmt_operand (&global_var, stmt, opf_is_def, prev_vops);
1437 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1439 tree var = referenced_var (i);
1441 /* If VAR is read-only, don't add a V_MAY_DEF, just a
1443 if (!TREE_READONLY (var))
1444 add_stmt_operand (&var, stmt, opf_is_def, prev_vops);
1446 add_stmt_operand (&var, stmt, opf_none, prev_vops);
1452 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1456 add_call_read_ops (tree stmt, voperands_t prev_vops)
1458 /* Otherwise, if the function is not pure, it may reference memory. Add
1459 a VUSE for .GLOBAL_VAR if it has been created. Otherwise, add a VUSE
1460 for each call-clobbered variable. See add_referenced_var for the
1461 heuristic used to decide whether to create .GLOBAL_VAR. */
1463 add_stmt_operand (&global_var, stmt, opf_none, prev_vops);
1468 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1470 tree var = referenced_var (i);
1471 add_stmt_operand (&var, stmt, opf_none, prev_vops);
1476 #include "gt-tree-ssa-operands.h"