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, nor
795 makes aliased loads or stores. */
796 ann->has_volatile_ops = false;
797 ann->makes_aliased_stores = false;
798 ann->makes_aliased_loads = false;
800 /* Remove any existing operands as they will be scanned again. */
801 free_defs (&(ann->def_ops), true);
802 free_uses (&(ann->use_ops), true);
804 /* Before removing existing virtual operands, save them in PREV_VOPS so
805 that we can re-use their SSA versions. */
806 prev_vops.v_may_def_ops = V_MAY_DEF_OPS (ann);
807 prev_vops.vuse_ops = VUSE_OPS (ann);
808 prev_vops.v_must_def_ops = V_MUST_DEF_OPS (ann);
810 /* Dont free the previous values to memory since we're still using them. */
811 free_v_may_defs (&(ann->v_may_def_ops), false);
812 free_vuses (&(ann->vuse_ops), false);
813 free_v_must_defs (&(ann->v_must_def_ops), false);
815 start_ssa_stmt_operands (stmt);
817 code = TREE_CODE (stmt);
821 get_expr_operands (stmt, &TREE_OPERAND (stmt, 1), opf_none, &prev_vops);
822 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF
823 || TREE_CODE (TREE_OPERAND (stmt, 0)) == COMPONENT_REF
824 || TREE_CODE (TREE_OPERAND (stmt, 0)) == REALPART_EXPR
825 || TREE_CODE (TREE_OPERAND (stmt, 0)) == IMAGPART_EXPR
826 /* Use a V_MAY_DEF if the RHS might throw, as the LHS won't be
827 modified in that case. FIXME we should represent somehow
828 that it is killed on the fallthrough path. */
829 || tree_could_throw_p (TREE_OPERAND (stmt, 1)))
830 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_is_def,
833 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0),
834 opf_is_def | opf_kill_def, &prev_vops);
838 get_expr_operands (stmt, &COND_EXPR_COND (stmt), opf_none, &prev_vops);
842 get_expr_operands (stmt, &SWITCH_COND (stmt), opf_none, &prev_vops);
847 int noutputs = list_length (ASM_OUTPUTS (stmt));
848 const char **oconstraints
849 = (const char **) alloca ((noutputs) * sizeof (const char *));
852 const char *constraint;
853 bool allows_mem, allows_reg, is_inout;
855 for (i=0, link = ASM_OUTPUTS (stmt); link;
856 ++i, link = TREE_CHAIN (link))
858 oconstraints[i] = constraint
859 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
860 parse_output_constraint (&constraint, i, 0, 0,
861 &allows_mem, &allows_reg, &is_inout);
862 if (allows_reg && is_inout)
863 /* This should have been split in gimplify_asm_expr. */
866 if (!allows_reg && allows_mem)
868 tree t = get_base_address (TREE_VALUE (link));
870 mark_call_clobbered (t);
873 get_expr_operands (stmt, &TREE_VALUE (link), opf_is_def,
877 for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
880 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
881 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
882 oconstraints, &allows_mem, &allows_reg);
884 if (!allows_reg && allows_mem)
886 tree t = get_base_address (TREE_VALUE (link));
888 mark_call_clobbered (t);
891 get_expr_operands (stmt, &TREE_VALUE (link), 0, &prev_vops);
894 /* Clobber memory for asm ("" : : : "memory"); */
895 for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
896 if (!strcmp (TREE_STRING_POINTER (TREE_VALUE (link)), "memory"))
897 add_call_clobber_ops (stmt, &prev_vops);
902 get_expr_operands (stmt, &TREE_OPERAND (stmt, 0), opf_none, &prev_vops);
906 get_expr_operands (stmt, &GOTO_DESTINATION (stmt), opf_none, &prev_vops);
910 get_expr_operands (stmt, &LABEL_EXPR_LABEL (stmt), opf_none, &prev_vops);
913 /* These nodes contain no variable references. */
915 case CASE_LABEL_EXPR:
917 case TRY_FINALLY_EXPR:
924 /* Notice that if get_expr_operands tries to use &STMT as the operand
925 pointer (which may only happen for USE operands), we will abort in
926 append_use. This default will handle statements like empty statements,
927 CALL_EXPRs or VA_ARG_EXPRs that may appear on the RHS of a statement
928 or as statements themselves. */
929 get_expr_operands (stmt, &stmt, opf_none, &prev_vops);
933 finalize_ssa_stmt_operands (stmt);
935 /* Now free the previous virtual ops to memory. */
936 free_v_may_defs (&(prev_vops.v_may_def_ops), true);
937 free_vuses (&(prev_vops.vuse_ops), true);
938 free_v_must_defs (&(prev_vops.v_must_def_ops), true);
940 /* Clear the modified bit for STMT. Subsequent calls to
941 get_stmt_operands for this statement will do nothing until the
942 statement is marked modified by a call to modify_stmt(). */
945 timevar_pop (TV_TREE_OPS);
949 /* Recursively scan the expression pointed by EXPR_P in statement STMT.
950 FLAGS is one of the OPF_* constants modifying how to interpret the
951 operands found. PREV_VOPS is as in append_v_may_def and append_vuse. */
954 get_expr_operands (tree stmt, tree *expr_p, int flags, voperands_t prev_vops)
960 if (expr == NULL || expr == error_mark_node)
963 code = TREE_CODE (expr);
964 class = TREE_CODE_CLASS (code);
966 /* Expressions that make no memory references. */
970 || code == FUNCTION_DECL
971 || code == EXC_PTR_EXPR
972 || code == FILTER_EXPR
973 || code == LABEL_DECL)
976 /* We could have the address of a component, array member, etc which
977 has interesting variable references. */
978 if (code == ADDR_EXPR)
980 enum tree_code subcode = TREE_CODE (TREE_OPERAND (expr, 0));
982 /* Taking the address of a variable does not represent a
983 reference to it, but the fact that STMT takes its address will be
984 of interest to some passes (e.g. alias resolution). */
985 add_stmt_operand (expr_p, stmt, 0, NULL);
987 /* If the address is invariant, there may be no interesting variable
988 references inside. */
989 if (is_gimple_min_invariant (expr))
992 /* There should be no VUSEs created, since the referenced objects are
993 not really accessed. The only operands that we should find here
994 are ARRAY_REF indices which will always be real operands (GIMPLE
995 does not allow non-registers as array indices). */
996 flags |= opf_no_vops;
998 /* Avoid recursion. */
1000 class = TREE_CODE_CLASS (code);
1001 expr_p = &TREE_OPERAND (expr, 0);
1005 /* If we found a variable, add it to DEFS or USES depending on the
1007 if (SSA_VAR_P (expr))
1009 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1013 /* Pointer dereferences always represent a use of the base pointer. */
1014 if (code == INDIRECT_REF)
1016 tree *pptr = &TREE_OPERAND (expr, 0);
1019 if (SSA_VAR_P (ptr))
1021 if (!aliases_computed_p)
1023 /* If the pointer does not have a memory tag and aliases have not
1024 been computed yet, mark the statement as having volatile
1025 operands to prevent DOM from entering it in equivalence tables
1026 and DCE from killing it. */
1027 stmt_ann (stmt)->has_volatile_ops = true;
1031 struct ptr_info_def *pi = NULL;
1033 /* If we have computed aliasing already, check if PTR has
1034 flow-sensitive points-to information. */
1035 if (TREE_CODE (ptr) == SSA_NAME
1036 && (pi = SSA_NAME_PTR_INFO (ptr)) != NULL
1037 && pi->name_mem_tag)
1039 /* PTR has its own memory tag. Use it. */
1040 add_stmt_operand (&pi->name_mem_tag, stmt, flags,
1045 /* If PTR is not an SSA_NAME or it doesn't have a name
1046 tag, use its type memory tag. */
1049 /* If we are emitting debugging dumps, display a warning if
1050 PTR is an SSA_NAME with no flow-sensitive alias
1051 information. That means that we may need to compute
1054 && TREE_CODE (ptr) == SSA_NAME
1058 "NOTE: no flow-sensitive alias info for ");
1059 print_generic_expr (dump_file, ptr, dump_flags);
1060 fprintf (dump_file, " in ");
1061 print_generic_stmt (dump_file, stmt, dump_flags);
1064 if (TREE_CODE (ptr) == SSA_NAME)
1065 ptr = SSA_NAME_VAR (ptr);
1066 ann = var_ann (ptr);
1067 add_stmt_operand (&ann->type_mem_tag, stmt, flags, prev_vops);
1072 /* If a constant is used as a pointer, we can't generate a real
1073 operand for it but we mark the statement volatile to prevent
1074 optimizations from messing things up. */
1075 else if (TREE_CODE (ptr) == INTEGER_CST)
1077 stmt_ann (stmt)->has_volatile_ops = true;
1081 /* Everything else *should* have been folded elsewhere, but users
1082 are smarter than we in finding ways to write invalid code. We
1083 cannot just abort here. If we were absolutely certain that we
1084 do handle all valid cases, then we could just do nothing here.
1085 That seems optimistic, so attempt to do something logical... */
1086 else if ((TREE_CODE (ptr) == PLUS_EXPR || TREE_CODE (ptr) == MINUS_EXPR)
1087 && TREE_CODE (TREE_OPERAND (ptr, 0)) == ADDR_EXPR
1088 && TREE_CODE (TREE_OPERAND (ptr, 1)) == INTEGER_CST)
1090 /* Make sure we know the object is addressable. */
1091 pptr = &TREE_OPERAND (ptr, 0);
1092 add_stmt_operand (pptr, stmt, 0, NULL);
1094 /* Mark the object itself with a VUSE. */
1095 pptr = &TREE_OPERAND (*pptr, 0);
1096 get_expr_operands (stmt, pptr, flags, prev_vops);
1100 /* Ok, this isn't even is_gimple_min_invariant. Something's broke. */
1104 /* Add a USE operand for the base pointer. */
1105 get_expr_operands (stmt, pptr, opf_none, prev_vops);
1109 /* Treat array references as references to the virtual variable
1110 representing the array. The virtual variable for an ARRAY_REF
1111 is the VAR_DECL for the array. */
1112 if (code == ARRAY_REF)
1114 /* Add the virtual variable for the ARRAY_REF to VDEFS or VUSES
1115 according to the value of IS_DEF. Recurse if the LHS of the
1116 ARRAY_REF node is not a regular variable. */
1117 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1118 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1120 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1122 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none, prev_vops);
1126 /* Similarly to arrays, references to compound variables (complex types
1127 and structures/unions) are globbed.
1129 FIXME: This means that
1135 will not be constant propagated because the two partial
1136 definitions to 'a' will kill each other. Note that SRA may be
1137 able to fix this problem if 'a' can be scalarized. */
1138 if (code == IMAGPART_EXPR || code == REALPART_EXPR || code == COMPONENT_REF)
1140 /* If the LHS of the compound reference is not a regular variable,
1141 recurse to keep looking for more operands in the subexpression. */
1142 if (SSA_VAR_P (TREE_OPERAND (expr, 0)))
1143 add_stmt_operand (expr_p, stmt, flags, prev_vops);
1145 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1150 /* Function calls. Add every argument to USES. If the callee is
1151 neither pure nor const, create a VDEF reference for GLOBAL_VAR
1152 (See find_vars_r). */
1153 if (code == CALL_EXPR)
1156 int call_flags = call_expr_flags (expr);
1158 /* Find uses in the called function. */
1159 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none, prev_vops);
1161 for (op = TREE_OPERAND (expr, 1); op; op = TREE_CHAIN (op))
1162 get_expr_operands (stmt, &TREE_VALUE (op), opf_none, prev_vops);
1164 get_expr_operands (stmt, &TREE_OPERAND (expr, 2), opf_none, prev_vops);
1166 if (bitmap_first_set_bit (call_clobbered_vars) >= 0)
1168 /* A 'pure' or a 'const' functions never call clobber anything.
1169 A 'noreturn' function might, but since we don't return anyway
1170 there is no point in recording that. */
1172 & (ECF_PURE | ECF_CONST | ECF_NORETURN)))
1173 add_call_clobber_ops (stmt, prev_vops);
1174 else if (!(call_flags & (ECF_CONST | ECF_NORETURN)))
1175 add_call_read_ops (stmt, prev_vops);
1177 else if (!aliases_computed_p)
1178 stmt_ann (stmt)->has_volatile_ops = true;
1184 if (code == TREE_LIST)
1188 for (op = expr; op; op = TREE_CHAIN (op))
1189 get_expr_operands (stmt, &TREE_VALUE (op), flags, prev_vops);
1195 if (code == MODIFY_EXPR)
1197 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), opf_none, prev_vops);
1198 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ARRAY_REF
1199 || TREE_CODE (TREE_OPERAND (expr, 0)) == COMPONENT_REF
1200 || TREE_CODE (TREE_OPERAND (expr, 0)) == REALPART_EXPR
1201 || TREE_CODE (TREE_OPERAND (expr, 0)) == IMAGPART_EXPR)
1202 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_is_def,
1205 get_expr_operands (stmt, &TREE_OPERAND (expr, 0),
1206 opf_is_def | opf_kill_def, prev_vops);
1211 /* Mark VA_ARG_EXPR nodes as making volatile references. FIXME,
1212 this is needed because we currently do not gimplify VA_ARG_EXPR
1214 if (code == VA_ARG_EXPR)
1216 stmt_ann (stmt)->has_volatile_ops = true;
1220 /* Unary expressions. */
1222 || code == TRUTH_NOT_EXPR
1223 || code == BIT_FIELD_REF
1224 || code == CONSTRUCTOR)
1226 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1230 /* Binary expressions. */
1233 || code == TRUTH_AND_EXPR
1234 || code == TRUTH_OR_EXPR
1235 || code == TRUTH_XOR_EXPR
1236 || code == COMPOUND_EXPR)
1238 get_expr_operands (stmt, &TREE_OPERAND (expr, 0), flags, prev_vops);
1239 get_expr_operands (stmt, &TREE_OPERAND (expr, 1), flags, prev_vops);
1243 /* If we get here, something has gone wrong. */
1244 fprintf (stderr, "unhandled expression in get_expr_operands():\n");
1246 fputs ("\n", stderr);
1251 /* Add *VAR_P to the appropriate operand array of STMT. FLAGS is as in
1252 get_expr_operands. If *VAR_P is a GIMPLE register, it will be added to
1253 the statement's real operands, otherwise it is added to virtual
1256 PREV_VOPS is used when adding virtual operands to statements that
1257 already had them (See append_v_may_def and append_vuse). */
1260 add_stmt_operand (tree *var_p, tree stmt, int flags, voperands_t prev_vops)
1270 s_ann = stmt_ann (stmt);
1272 /* If the operand is an ADDR_EXPR, add its operand to the list of
1273 variables that have had their address taken in this statement. */
1274 if (TREE_CODE (var) == ADDR_EXPR)
1276 note_addressable (TREE_OPERAND (var, 0), s_ann);
1280 /* If the original variable is not a scalar, it will be added to the list
1281 of virtual operands. In that case, use its base symbol as the virtual
1282 variable representing it. */
1283 is_real_op = is_gimple_reg (var);
1284 if (!is_real_op && !DECL_P (var))
1285 var = get_virtual_var (var);
1287 /* If VAR is not a variable that we care to optimize, do nothing. */
1288 if (var == NULL_TREE || !SSA_VAR_P (var))
1291 sym = (TREE_CODE (var) == SSA_NAME ? SSA_NAME_VAR (var) : var);
1292 v_ann = var_ann (sym);
1294 /* FIXME: We currently refuse to optimize variables that have hidden uses
1295 (variables used in VLA declarations, MD builtin calls and variables
1296 from the parent function in nested functions). This is because not
1297 all uses of these variables are exposed in the IL or the statements
1298 that reference them are not in GIMPLE form. If that's the case, mark
1299 the statement as having volatile operands and return. */
1300 if (v_ann->has_hidden_use)
1302 s_ann->has_volatile_ops = true;
1306 /* Don't expose volatile variables to the optimizers. */
1307 if (TREE_THIS_VOLATILE (sym))
1309 s_ann->has_volatile_ops = true;
1315 /* The variable is a GIMPLE register. Add it to real operands. */
1316 if (flags & opf_is_def)
1317 append_def (var_p, stmt);
1319 append_use (var_p, stmt);
1323 varray_type aliases;
1325 /* The variable is not a GIMPLE register. Add it (or its aliases) to
1326 virtual operands, unless the caller has specifically requested
1327 not to add virtual operands (used when adding operands inside an
1328 ADDR_EXPR expression). */
1329 if (flags & opf_no_vops)
1332 aliases = v_ann->may_aliases;
1334 /* If alias information hasn't been computed yet, then
1335 addressable variables will not be an alias tag nor will they
1336 have aliases. In this case, mark the statement as having
1337 volatile operands. */
1338 if (!aliases_computed_p && may_be_aliased (var))
1339 s_ann->has_volatile_ops = true;
1341 if (aliases == NULL)
1343 /* The variable is not aliased or it is an alias tag. */
1344 if (flags & opf_is_def)
1346 if (v_ann->is_alias_tag)
1348 /* Alias tagged vars get regular V_MAY_DEF */
1349 s_ann->makes_aliased_stores = 1;
1350 append_v_may_def (var, stmt, prev_vops);
1352 else if ((flags & opf_kill_def)
1353 && v_ann->mem_tag_kind == NOT_A_TAG)
1354 /* V_MUST_DEF for non-aliased non-GIMPLE register
1355 variable definitions. Avoid memory tags. */
1356 append_v_must_def (var, stmt, prev_vops);
1358 /* Call-clobbered variables & memory tags get
1360 append_v_may_def (var, stmt, prev_vops);
1364 append_vuse (var, stmt, prev_vops);
1365 if (v_ann->is_alias_tag)
1366 s_ann->makes_aliased_loads = 1;
1373 /* The variable is aliased. Add its aliases to the virtual
1375 if (VARRAY_ACTIVE_SIZE (aliases) == 0)
1378 if (flags & opf_is_def)
1380 /* If the variable is also an alias tag, add a virtual
1381 operand for it, otherwise we will miss representing
1382 references to the members of the variable's alias set.
1383 This fixes the bug in gcc.c-torture/execute/20020503-1.c. */
1384 if (v_ann->is_alias_tag)
1385 append_v_may_def (var, stmt, prev_vops);
1387 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1388 append_v_may_def (VARRAY_TREE (aliases, i), stmt, prev_vops);
1390 s_ann->makes_aliased_stores = 1;
1394 if (v_ann->is_alias_tag)
1395 append_vuse (var, stmt, prev_vops);
1397 for (i = 0; i < VARRAY_ACTIVE_SIZE (aliases); i++)
1398 append_vuse (VARRAY_TREE (aliases, i), stmt, prev_vops);
1400 s_ann->makes_aliased_loads = 1;
1406 /* Record that VAR had its address taken in the statement with annotations
1410 note_addressable (tree var, stmt_ann_t s_ann)
1412 var = get_base_address (var);
1413 if (var && SSA_VAR_P (var))
1415 if (s_ann->addresses_taken == NULL)
1416 s_ann->addresses_taken = BITMAP_GGC_ALLOC ();
1417 bitmap_set_bit (s_ann->addresses_taken, var_ann (var)->uid);
1422 /* Add clobbering definitions for .GLOBAL_VAR or for each of the call
1423 clobbered variables in the function. */
1426 add_call_clobber_ops (tree stmt, voperands_t prev_vops)
1428 /* Functions that are not const, pure or never return may clobber
1429 call-clobbered variables. */
1430 stmt_ann (stmt)->makes_clobbering_call = true;
1432 /* If we had created .GLOBAL_VAR earlier, use it. Otherwise, add
1433 a V_MAY_DEF operand for every call clobbered variable. See
1434 compute_may_aliases for the heuristic used to decide whether
1435 to create .GLOBAL_VAR or not. */
1437 add_stmt_operand (&global_var, stmt, opf_is_def, prev_vops);
1442 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1444 tree var = referenced_var (i);
1446 /* If VAR is read-only, don't add a V_MAY_DEF, just a
1448 if (!TREE_READONLY (var))
1449 add_stmt_operand (&var, stmt, opf_is_def, prev_vops);
1451 add_stmt_operand (&var, stmt, opf_none, prev_vops);
1457 /* Add VUSE operands for .GLOBAL_VAR or all call clobbered variables in the
1461 add_call_read_ops (tree stmt, voperands_t prev_vops)
1463 /* Otherwise, if the function is not pure, it may reference memory. Add
1464 a VUSE for .GLOBAL_VAR if it has been created. Otherwise, add a VUSE
1465 for each call-clobbered variable. See add_referenced_var for the
1466 heuristic used to decide whether to create .GLOBAL_VAR. */
1468 add_stmt_operand (&global_var, stmt, opf_none, prev_vops);
1473 EXECUTE_IF_SET_IN_BITMAP (call_clobbered_vars, 0, i,
1475 tree var = referenced_var (i);
1476 add_stmt_operand (&var, stmt, opf_none, prev_vops);
1481 #include "gt-tree-ssa-operands.h"